Alien-Xmake

 view release on metacpan or  search on metacpan

builder/Alien/Xmake/Builder.pm  view on Meta::CPAN

        # On FreeBSD/NetBSD/OpenBSD/DragonFly, 'make' is BSD make.
        # Xmake generates GNU makefiles. We MUST use gmake.
        my $make_cmd = 'make';
        if ( $^O =~ /bsd/i || $^O eq 'dragonfly' ) {
            if ( $self->_run_cmd('gmake --version >/dev/null 2>&1') ) {
                $make_cmd = 'gmake';
            }
            else {
                # This should have been caught by _test_tools, but safe guard here
                die 'gmake is required on BSD systems to build Xmake.';
            }
        }
        elsif ( $self->_run_cmd('gmake --version >/dev/null 2>&1') ) {
            $make_cmd = 'gmake';
        }
        if ( -f 'configure' ) {
            say "Configuring with make=$make_cmd...";
            system( './configure', "--make=$make_cmd" ) == 0 or die 'Configure failed';
            system( $make_cmd,     '-j4' ) == 0              or die 'Make failed';
            say "Installing to $installdir...";
            system( $make_cmd, 'install', "PREFIX=$installdir" ) == 0 or die 'Install failed';
        }
        else {
            system( $make_cmd, 'build',   '-j4' ) == 0                or die 'Make build failed';
            system( $make_cmd, 'install', "prefix=$installdir" ) == 0 or die 'Make install failed';
        }
        chdir $cwd;
    }

    method _get_host_speed ($host) {
        my $cmd;
        if ( $^O eq 'darwin' ) {
            $cmd = "ping -c 1 -t 1 $host 2>/dev/null";
        }
        else {
            $cmd = "ping -c 1 -W 1 $host 2>/dev/null";
        }
        my $output = `$cmd`;
        if ( $output =~ /time=(\d+)/ ) {
            return $1;
        }
        return 65535;
    }

    method _get_fast_host ( ) {
        if ( $ENV{GITHUB_ACTIONS} ) {
            return 'github.com';
        }
        say 'Testing connection speed to github.com vs gitee.com...';
        my $speed_gitee  = $self->_get_host_speed('gitee.com');
        my $speed_github = $self->_get_host_speed('github.com');
        if ( $speed_gitee <= $speed_github ) {
            return 'gitee.com';
        }
        return 'github.com';
    }

    method _download_file ( $url, $dest ) {
        my $dest_str = "$dest";

        # Try HTTP::Tiny + IO::Socket::SSL
        if ( eval { require IO::Socket::SSL; 1 } ) {
            say 'Downloading with HTTP::Tiny...';
            my $http = HTTP::Tiny->new( verify_SSL => 1 );
            my $res  = $http->mirror( $url, $dest_str );
            if ( $res->{success} ) {
                return 1;
            }
            say "HTTP::Tiny failed: $res->{status} $res->{reason}";
        }
        else {
            say 'HTTP::Tiny skipped: IO::Socket::SSL not installed.';
        }

        # Try curl
        if ( $self->_run_cmd('curl --version >/dev/null 2>&1') ) {
            say 'Downloading with curl...';

            # -L: Follow redirects, -f: Fail on error, -o: Output
            if ( $self->_run_cmd( 'curl', '-L', '-f', '-o', $dest_str, $url ) ) {
                return 1;
            }
            say 'curl failed.';
        }

        # Try wget
        if ( $self->_run_cmd('wget --version >/dev/null 2>&1') ) {
            say 'Downloading with wget...';
            if ( $self->_run_cmd( 'wget', '--quiet', '-O', $dest_str, $url ) ) {
                return 1;
            }
            say 'wget failed.';
        }
        return 0;
    }

    method _test_tools ( ) {
        say 'Checking build tools...';
        my $ok = 1;
        if ( $self->_run_cmd('git --version >/dev/null 2>&1') ) {
            say ' - git: Found';
        }
        else {
            say ' - git: Missing';
            $ok = 0;
        }

        # GNU or BSD make
        my $found_make = 0;
        if ( $self->_run_cmd('gmake --version >/dev/null 2>&1') ) {
            say ' - make: Found (gmake)';
            $found_make = 1;
        }
        elsif ( $self->_run_cmd('make --version >/dev/null 2>&1') ) {
            say ' - make: Found (make - likely GNU compatible)';
            $found_make = 1;
        }
        elsif ( $self->_run_cmd('make -V MACHINE >/dev/null 2>&1') ) {
            say ' - make: Found (make - BSD)';

            # If we are on BSD, this is technically 'found', but we know it won't work for Xmake.
            # We must fail here to trigger the installer if we are on BSD.
            if ( $^O =~ /bsd/i || $^O eq 'dragonfly' ) {
                say '   ! Note: BSD make is not compatible with Xmake build (needs gmake).';
            }
            else {
                $found_make = 1;    # On non-BSD systems, maybe they have a different make setup.
            }
        }

        # STRICT CHECK for BSDs
        if ( $^O =~ /bsd/i || $^O eq 'dragonfly' ) {



( run in 0.551 second using v1.01-cache-2.11-cpan-39bf76dae61 )