Alien-xmake

 view release on metacpan or  search on metacpan

builder/xmake.pm  view on Meta::CPAN

            $make // warn 'Please install gmake';
        }
        my $compiler;
        {
            my ( $fh, $filename ) = tempfile();
            syswrite $fh, "#include <stdio.h>\nint main(){return 0;}";
            for (qw[gcc cc clang]) {
                if ( !system $_, qw'-x c', $filename,
                    qw'-o /dev/null -I/usr/include -I/usr/local/include' ) {
                    $compiler = $_;
                }
            }

            #~ $compiler // warn 'Please install a C compiler';
        }
        if ( !defined $make || !defined $compiler ) {
            my $sudo      = sudo();
            my $installer = package_installer();
            my %options   = (
                apt => "$sudo apt install -y git build-essential libreadline-dev ccache"
                ,    # debian, etc.
                yum =>
                    "yum install -y git readline-devel ccache bzip2 && $sudo yum groupinstall -y 'Development Tools'",
                zypper =>
                    "$sudo zypper --non-interactive install git readline-devel ccache && $sudo zypper --non-interactive install -t pattern devel_C_C++",
                pacman =>
                    "$sudo pacman -S --noconfirm --needed git base-devel ncurses readline ccache"
                ,                                                                 # arch, etc.
                emerge     => "$sudo emerge -atv dev-vcs/git ccache",             # Gentoo
                pkg_termux => "$sudo pkg install -y git getconf build-essential readline ccache"
                ,                                                                 # termux (Android)
                pkg_bsd => "$sudo pkg install -y git readline ccache ncurses",    # freebsd
                nixos   => "nix-env -i git gcc readline ncurses;",
                apk     =>
                    "$sudo apk add git gcc g++ make readline-dev ncurses-dev libc-dev linux-headers",
                xbps => "$sudo xbps-install -Sy git base-devel ccache",

                #scoop  => "$sudo ",                                               # Windows
                #winget => "$sudo ",                                               # Windows
                #brew   => 'brew --version',                                       # MacOS
                #dnf    => 'dnf --help',    # Fedora, RHEL, OpenSUSE, CentOS
            );
            warn 'You should probably try running ' . $options{$installer}
                if defined $options{$installer};
            my $prebuilt = install_prebuilt();
            warn 'You could also install a prebuilt version of xmake with ' . $prebuilt
                if defined $prebuilt;
        }

        sub get_host_speed {
            my ($host) = @_;
            my $output = `ping -c 1 -W 1 $host 2>/dev/null`;
            $output =~ /time=([\d.]+)/ if $output;
            return $1 // 65535;
        }

        sub get_fast_host {
            my $gitee_speed  = get_host_speed("gitee.com");
            my $github_speed = get_host_speed("github.com");

            #~ CORE::say "gitee.com mirror took $gitee_speed ms";
            #~ CORE::say "github.com mirror took $github_speed ms";
            if ( $gitee_speed <= $github_speed ) {
                return 'gitee.com';
            }
            else {
                return 'github.com';
            }
        }
        my $cwd        = rel2abs('.');
        my $projectdir = tempdir( CLEANUP => 1 );
        my $workdir;
        my $archive = $s->download( $installer_tar, 'xmake.tar.gz' );
        if ( !$archive ) {
            warn 'Failed to download source snapshot... Looking for git...';
            my $git;
            for (qw[git]) {
                if ( system( $_, '--version' ) == 0 ) {
                    $git = $_;
                    last;
                }
            }
            if ( !$git ) { warn 'Cannot locate git. Giving up'; exit 1; }
            my $mirror = get_fast_host();
            CORE::say "Using $mirror mirror...";
            my ( $gitrepo, $gitrepo_raw );
            if ( $mirror eq 'github.com' ) {
                $gitrepo = 'https://github.com/xmake-io/xmake.git';

                #$gitrepo_raw='https://github.com/xmake-io/xmake/raw/master';
                $gitrepo_raw = 'https://fastly.jsdelivr.net/gh/xmake-io/xmake@master';
            }
            else {
                $gitrepo     = "https://gitee.com/tboox/xmake.git";
                $gitrepo_raw = "https://gitee.com/tboox/xmake/raw/master";
            }
            #
            `git clone --depth=1 -b "v$version" "$gitrepo" --recurse-submodules $projectdir`
                unless -f $projectdir;
            chdir $projectdir;
            $workdir = $projectdir;
        }
        my $tar = Archive::Tar->new;
        $tar->read($archive);
        chdir $projectdir;
        $tar->extract();
        $workdir = $projectdir . '/xmake-' . $version;
        chdir $workdir;
        system './configure' unless -f 'makefile';
        system $make, '--jobs=5';
        system $make, 'install', qq[PREFIX=$share];
        chdir $cwd;    # I really should go to dist base dir
        return $share;
    }

    sub install_prebuilt {
        my $installer = package_installer();
        my $Win32 if $^O eq 'MSWin32';
        my $sudo    = sudo();
        my %options = (
            apt =>
                "$sudo add-apt-repository ppa:xmake-io/xmake && $sudo apt update && $sudo apt install xmake"
            ,                                                                   # debian, etc.
            pacman => ( $Win32 ? 'pacman -Sy mingw-w64-x86_64-xmake' : "$sudo pacman -Sy xmake" )
            ,                                                                   # arch, etc.
            emerge     => "$sudo emerge -a --autounmask dev-util/xmake",        # Gentoo
            pkg_termux => "$sudo pkg install -y xmake",                         # termux (Android)
            xbps       => "$sudo xbps-install -Sy git base-devel ccache",
            scoop      => "scoop install xmake",                                # Windows
            winget     => "winget install xmake",                               # Windows
            brew       => 'brew install xmake',                                 # MacOS
            dnf        =>
                "$sudo dnf copr enable waruqi/xmake && $sudo dnf install xmake" # Fedora, RHEL, OpenSUSE, CentOS
        );
        return $options{$installer} // ();
    }
}
1;



( run in 0.779 second using v1.01-cache-2.11-cpan-483215c6ad5 )