Affix

 view release on metacpan or  search on metacpan

builder/Affix/Builder.pm  view on Meta::CPAN

            $ar_cmd      = 'ar';
            @cflags      = ( '-std=c11', '-Wall', '-Wextra', '-O2', '-fPIC', @includes );
            @cflags      = ( @cflags, '-DINFIX_DEBUG_ENABLED=1' ) if $verbose;
            @arflags     = ('rcs');
            $out_flag_cc = '-o';
            $out_flag_ar = '';

            # Pass pthread to compilation of static lib too (safe practice)
            push @cflags, '-pthread' unless $^O eq 'MSWin32';
        }

        # Compile infix.c -> infix.o
        my $obj_ext  = $cc_type eq 'msvc' ? '.obj' : '.o';
        my $obj_file = $build_lib->child( 'infix' . $obj_ext );
        my @compile_cmd;
        if ( $cc_type eq 'msvc' ) {
            @compile_cmd = ( $cc_cmd, @cflags, $out_flag_cc . $obj_file, $src_file );
        }
        else {
            @compile_cmd = ( $cc_cmd, @cflags, '-c', $src_file, $out_flag_cc, $obj_file );
        }
        warn "  Compiling: @compile_cmd\n" if $verbose;
        if ( system(@compile_cmd) != 0 ) {
            die "Failed to compile infix.c";
        }

        # Archive infix.o -> libinfix.a
        my @archive_cmd;
        if ( $cc_type eq 'msvc' ) {
            @archive_cmd = ( $ar_cmd, @arflags, $out_flag_ar . $lib_file, $obj_file );
        }
        else {
            @archive_cmd = ( $ar_cmd, @arflags, $lib_file, $obj_file );
        }
        warn "  Archiving: @archive_cmd\n" if $verbose;
        if ( system(@archive_cmd) != 0 ) {
            die "Failed to create infix static library";
        }
        warn "Infix library built: $lib_file\n" if $verbose;
        return 0;
    }

    # Detects if linking against librt is required (common on Linux/BSD/Solaris for shm_open)
    method check_for_lrt() {
        return ''                                if $^O eq 'MSWin32';
        warn "Checking if -lrt is required...\n" if $verbose;
        my $cc        = $Config{cc} || 'cc';
        my $test_code = <<'END_C';
#include <sys/mman.h>
#include <fcntl.h>
int main(void) { shm_open("/test", O_RDONLY, 0); return 0; }
END_C
        my ( $fh, $src ) = tempfile( SUFFIX => '.c', UNLINK => 1 );
        print $fh $test_code;
        close $fh;
        my ( $ofh, $out ) = tempfile( UNLINK => 1 );
        close $ofh;

        # Try without -lrt (list-form system to avoid shell injection)
        open( my $devnull, '>', '/dev/null' ) if $^O ne 'MSWin32';
        my $old_stdout = select $devnull      if $devnull;
        system( $cc, '-o', $out, $src );
        select $old_stdout if $old_stdout;
        close $devnull     if $devnull;
        return ''          if $? == 0;

        # Try with -lrt
        open( $devnull, '>', '/dev/null' ) if $^O ne 'MSWin32';
        $old_stdout = select $devnull      if $devnull;
        system( $cc, '-o', $out, $src, '-lrt' );
        select $old_stdout if $old_stdout;
        close $devnull     if $devnull;
        return '-lrt'      if $? == 0;
        return '';
    }

    sub command_exists {
        my ($cmd) = @_;
        if ( $Config{osname} eq 'MSWin32' ) {
            return system( 'where', $cmd ) == 0;
        }
        else {
            return system( 'command', '-v', $cmd ) == 0;
        }
    }

    method step_affix {
        $self->step_infix;
        my $cwd = cwd->absolute;
        my @objs;
        require ExtUtils::CBuilder;
        my %config = %Config;
        if ($debug) {
            $config{ldflags}   =~ s/-s //g;
            $config{ldflags}   =~ s/ -s//g;
            $config{lddlflags} =~ s/-s //g;
            $config{lddlflags} =~ s/ -s//g;
        }
        my $builder = ExtUtils::CBuilder->new( quiet => !$verbose, config => \%config );
        my $pre     = $cwd->child(qw[blib arch auto])->absolute;
        require DynaLoader;
        my $mod2fname = defined &DynaLoader::mod2fname ? \&DynaLoader::mod2fname : sub { return $_[0][-1] };
        my @parts     = ('Affix');
        my $archdir   = rel2abs catdir( curdir, qw[. blib arch auto], @parts );
        my $err;
        make_path( $archdir, { chmod => 0755, error => \$err, verbose => $verbose } );
        my $lib_file = catfile( $archdir, $mod2fname->( \@parts ) . '.' . $Config{dlext} );
        my @dirs;
        push @dirs, '../';
        my $has_cxx    = !1;
        my $recompiled = 0;
        my @sources    = $cwd->child('lib/Affix.c');

        #~ warn "Sources to process: @sources\n";
        for my $source (@sources) {

            #~ warn "Processing source: $source\n";
            my $cxx       = $source =~ /cx+$/;
            my $file_base = $source->basename(qr[.c$]);
            my $tempdir   = path('lib');
            $tempdir->mkdir( { verbose => $verbose, mode => oct '755' } );
            my $version = $meta->version;
            my $obj     = $builder->object_file($source);

            #~ warn "Checking obj: $obj\n";
            # Check mtimes of all .c and .h files under lib/ (includes Affix.c, marshal.c, Affix.h)
            my $newest_dep = $source->stat->mtime;
            my $iter       = path('lib')->iterator;
            while ( my $entry = $iter->() ) {
                next unless $entry->is_file;
                next unless $entry =~ /\.[ch]$/;



( run in 0.524 second using v1.01-cache-2.11-cpan-8dfa8b56332 )