| 1 | package ExtUtils::Builder::Conf; | ||
| 2 | $ExtUtils::Builder::Conf::VERSION = '0.038'; | ||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Carp; | ||
| 7 | use File::Basename qw/basename dirname/; | ||
| 8 | use File::Spec::Functions qw/catfile curdir/; | ||
| 9 | use File::Temp qw/tempfile tempdir/; | ||
| 10 | |||
| 11 | use ExtUtils::Builder::Action::Command; | ||
| 12 | use ExtUtils::Builder::Planner; | ||
| 13 | |||
| 14 | sub fail { | ||
| 15 | my ($diag) = @_; | ||
| 16 | my $message = defined $diag ? "OS unsupported - $diag\n" : "OS unsupported\n"; | ||
| 17 | die $message; | ||
| 18 | } | ||
| 19 | |||
| 20 | my @names = qw/include_dirs library_dirs libraries extra_compiler_flags extra_linker_flags extra_sources extra_objects/; | ||
| 21 | |||
| 22 | sub add_methods { | ||
| 23 | my ($self, $planner, %args) = @_; | ||
| 24 | |||
| 25 | for my $name (@names) { | ||
| 26 | $planner->add_delegate($name, sub { | ||
| 27 | my $self = shift; | ||
| 28 | return @{ $self->{$name} // [] }; | ||
| 29 | }); | ||
| 30 | $planner->add_delegate("push_$name", sub { | ||
| 31 | my ($self, @args) = @_; | ||
| 32 | push @{ $self->{$name} }, @args; | ||
| 33 | }); | ||
| 34 | } | ||
| 35 | |||
| 36 | $planner->add_delegate(standard => sub { | ||
| 37 | my $self = shift; | ||
| 38 | return $self->{standard}; | ||
| 39 | }); | ||
| 40 | |||
| 41 | $planner->add_delegate(set_standard => sub { | ||
| 42 | my ($self, $new_value) = @_; | ||
| 43 | $self->{standard} = $new_value; | ||
| 44 | }); | ||
| 45 | |||
| 46 | $planner->add_delegate(define => sub { | ||
| 47 | my ($self, $symbol, $value) = @_; | ||
| 48 | $self->{defines}{$symbol} = $value // ''; | ||
| 49 | }); | ||
| 50 | |||
| 51 | $planner->add_delegate(defines => sub { | ||
| 52 | my $self = shift; | ||
| 53 | return %{ $self->{defines} // {} }; | ||
| 54 | }); | ||
| 55 | |||
| 56 | $planner->add_delegate(write_defines => sub { | ||
| 57 | my ($self, $to, %arguments) = @_; | ||
| 58 | |||
| 59 | my @lines; | ||
| 60 | for my $symbol (sort keys %{ $self->{defines} }) { | ||
| 61 | if (not defined $self->{defines}{$symbol}) { | ||
| 62 | push @lines, "#undef $symbol\n"; | ||
| 63 | } elsif ($self->{defines}{$symbol} eq '') { | ||
| 64 | push @lines, "#define $symbol\n" | ||
| 65 | } else { | ||
| 66 | my $value = $self->{defines}{$symbol}; | ||
| 67 | $value =~ s/\n/\\\n/g; | ||
| 68 | push @lines, "#define $symbol $symbol\n" | ||
| 69 | } | ||
| 70 | } | ||
| 71 | |||
| 72 | open my $fh, '>', $to or croak "Cannot open $to for writing: $!"; | ||
| 73 | print join "\n", @lines; | ||
| 74 | close $fh or croak "Cannot write $to for writing: $!" | ||
| 75 | }); | ||
| 76 | |||
| 77 | $planner->add_delegate(try_compile_run => sub { | ||
| 78 | my ($self, %args) = @_; | ||
| 79 | |||
| 80 | my $dir = File::Temp->newdir; | ||
| 81 | |||
| 82 | my ($source_file, $c_file) = tempfile('try_compilerXXXX', DIR => $dir, SUFFIX => '.c'); | ||
| 83 | |||
| 84 | print $source_file $args{source}; | ||
| 85 | |||
| 86 | my $inner = $self->new_planner; | ||
| 87 | $inner->load_extension('ExtUtils::Builder::BuildTools::FromPerl', 0.034); | ||
| 88 | |||
| 89 | my @include_dirs = (@{ $args{include_dirs} // [] }, @{ $self->{include_dirs} // [] }); | ||
| 90 | my @extra_compiler_flags = (@{ $args{extra_compiler_flags} // [] }, @{ $self->{extra_compiler_flags} // [] }); | ||
| 91 | my %defines = (%{ $args{defines} // {} }, %{ $self->{defines} // {} }); | ||
| 92 | my $standard = $args{standard} || $self->{standard}; | ||
| 93 | |||
| 94 | my %compile_args = ( | ||
| 95 | extra_args => \@extra_compiler_flags, | ||
| 96 | include_dirs => \@include_dirs, | ||
| 97 | defines => \%defines, | ||
| 98 | standard => $standard, | ||
| 99 | ); | ||
| 100 | |||
| 101 | my $basename = basename($c_file, '.c'); | ||
| 102 | my $o_file = $inner->obj_file($basename, $dir); | ||
| 103 | $inner->compile($c_file, $o_file, %compile_args); | ||
| 104 | |||
| 105 | my @libraries = (@{ $args{libraries} // [] }, @{ $self->{libraries} // [] }); | ||
| 106 | my @library_dirs = (@{ $args{library_dirs} // [] }, @{ $self->{library_dirs} // [] }); | ||
| 107 | my @extra_linker_flags = (@{ $args{extra_linker_flags} // [] }, @{ $self->{extra_linker_flags} // [] }); | ||
| 108 | |||
| 109 | my %link_args = ( | ||
| 110 | libraries => \@libraries, | ||
| 111 | library_dirs => \@library_dirs, | ||
| 112 | extra_args => \@extra_linker_flags, | ||
| 113 | ); | ||
| 114 | |||
| 115 | my $exe_file = $inner->executable_file($basename, $dir); | ||
| 116 | $inner->link([ $o_file ], $exe_file, %link_args); | ||
| 117 | |||
| 118 | my $run = $args{run} // 1; | ||
| 119 | |||
| 120 | my $target; | ||
| 121 | if ($run) { | ||
| 122 | $inner->create_node( | ||
| 123 | target => 'test', | ||
| 124 | dependencies => [ $exe_file ], | ||
| 125 | actions => [ | ||
| 126 | ExtUtils::Builder::Action::Command->new(command => [ $exe_file ]), | ||
| 127 | ], | ||
| 128 | phony => 1, | ||
| 129 | ); | ||
| 130 | |||
| 131 | $target = 'test'; | ||
| 132 | } else { | ||
| 133 | $target = $exe_file; | ||
| 134 | } | ||
| 135 | |||
| 136 | my $result = eval { $inner->materialize->run($target, quiet => $args{quiet}); 1 }; | ||
| 137 | |||
| 138 | return !!0 if not $result; | ||
| 139 | |||
| 140 | $self->define($args{define}) if defined $args{define}; | ||
| 141 | |||
| 142 | if ($args{push_args}) { | ||
| 143 | for my $name (@names) { | ||
| 144 | if (my $arg = $args{$name}) { | ||
| 145 | push @{ $self->{$name} }, @{$arg}; | ||
| 146 | } | ||
| 147 | } | ||
| 148 | if ($args{defines}) { | ||
| 149 | for my $key (keys %{$args{defines}}) { | ||
| 150 | $self->{defines}{$key} = $args{defines}{$key}; | ||
| 151 | } | ||
| 152 | } | ||
| 153 | |||
| 154 | $self->{standard} = $args{standard} if $args{standard}; | ||
| 155 | } | ||
| 156 | |||
| 157 | if ($args{push_sources}) { | ||
| 158 | push @{ $self->{extra_sources} }, @{ $args{push_sources} }; | ||
| 159 | } | ||
| 160 | if ($args{push_objects}) { | ||
| 161 | push @{ $self->{extra_objects} }, @{ $args{push_objects} }; | ||
| 162 | } | ||
| 163 | |||
| 164 | return !!1; | ||
| 165 | }); | ||
| 166 | |||
| 167 | $planner->add_delegate(assert_compile_run => sub { | ||
| 168 | my ($self, %args) = @_; | ||
| 169 | |||
| 170 | my $diag = delete $args{diag}; | ||
| 171 | $self->try_compile_run(%args) or fail($diag); | ||
| 172 | }); | ||
| 173 | |||
| 174 | $planner->add_delegate(try_find_compiler_flags_for => sub { | ||
| 175 | my ($self, %args) = @_; | ||
| 176 | |||
| 177 | ref(my $cflags = $args{cflags}) eq "ARRAY" or croak "Expected 'cflags' as ARRAY ref"; | ||
| 178 | |||
| 179 | foreach my $f (@$cflags) { | ||
| 180 | ref $f eq "ARRAY" or croak "Expected 'cflags' element as ARRAY ref"; | ||
| 181 | |||
| 182 | $self->try_compile_run(%args, extra_compiler_flags => $f) or next; | ||
| 183 | $self->push_extra_compiler_flags(@$f); | ||
| 184 | return !!1; | ||
| 185 | } | ||
| 186 | |||
| 187 | return !!0; | ||
| 188 | }); | ||
| 189 | |||
| 190 | $planner->add_delegate(try_find_include_dirs_for => sub { | ||
| 191 | my ($self, %args) = @_; | ||
| 192 | |||
| 193 | ref(my $dirs = $args{dirs}) eq "ARRAY" or croak "Expected 'dirs' as ARRAY ref"; | ||
| 194 | |||
| 195 | foreach my $d (@$dirs) { | ||
| 196 | ref $d eq "ARRAY" or croak "Expected 'dirs' element as ARRAY ref"; | ||
| 197 | |||
| 198 | $self->try_compile_run(%args, include_dirs => $d) or next; | ||
| 199 | $self->push_include_dirs(@$d); | ||
| 200 | return !!1; | ||
| 201 | } | ||
| 202 | |||
| 203 | return !!0; | ||
| 204 | }); | ||
| 205 | |||
| 206 | $planner->add_delegate(try_find_libraries_for => sub { | ||
| 207 | my ($self, %args) = @_; | ||
| 208 | |||
| 209 | ref(my $libs = $args{libs}) eq "ARRAY" or croak "Expected 'libs' as ARRAY ref"; | ||
| 210 | |||
| 211 | foreach my $libraries (@$libs) { | ||
| 212 | $self->try_compile_run(%args, libraries => $libraries) or next; | ||
| 213 | $self->push_libraries(@$libraries); | ||
| 214 | return !!1; | ||
| 215 | } | ||
| 216 | |||
| 217 | return !!0; | ||
| 218 | }); | ||
| 219 | |||
| 220 | $planner->add_delegate(try_find_library_dirs_for => sub { | ||
| 221 | my ($self, %args) = @_; | ||
| 222 | |||
| 223 | ref(my $dirs = $args{dirs}) eq "ARRAY" or croak "Expected 'dirs' as ARRAY ref"; | ||
| 224 | |||
| 225 | foreach my $d (@$dirs) { | ||
| 226 | ref $d eq "ARRAY" or croak "Expected 'dirs' element as ARRAY ref"; | ||
| 227 | |||
| 228 | $self->try_compile_run(%args, library_dirs => $d) or next; | ||
| 229 | $self->push_library_dirs(@$d); | ||
| 230 | return !!1; | ||
| 231 | } | ||
| 232 | |||
| 233 | return !!0; | ||
| 234 | }); | ||
| 235 | |||
| 236 | $planner->add_delegate(try_find_standard_for => sub { | ||
| 237 | my ($self, %args) = @_; | ||
| 238 | |||
| 239 | ref(my $standards = $args{standards}) eq "ARRAY" or croak "Expected 'standards' as ARRAY ref"; | ||
| 240 | |||
| 241 | foreach my $standard (@$standards) { | ||
| 242 | $self->try_compile_run(%args, standard => $standard) or next; | ||
| 243 | $self->set_standard($standard); | ||
| 244 | return !!1; | ||
| 245 | } | ||
| 246 | |||
| 247 | return !!0; | ||
| 248 | }); | ||
| 249 | |||
| 250 | foreach my $shortname (qw/compiler_flags libraries include_dirs library_dirs standard/) { | ||
| 251 | my $name = "find_${shortname}_for"; | ||
| 252 | my $trymethod = "try_$name"; | ||
| 253 | |||
| 254 | $planner->add_delegate($name, sub { | ||
| 255 | my ($self, %args) = @_; | ||
| 256 | |||
| 257 | my $diag = delete $args{diag}; | ||
| 258 | $self->$trymethod(%args) or fail($diag); | ||
| 259 | }); | ||
| 260 | } | ||
| 261 | } | ||
| 262 | |||
| 263 | 1; | ||
| 264 | |||
| 265 | # ABSTRACT: Configure-time utilities for using C headers, libraries, or OS features | ||
| 266 | |||
| 267 | __END__ |