| 1 | # Build::Hopen::T::Gnu::C - support GNU toolset, C language | ||
| 2 | # TODO RESUME HERE - put .o files in the dest dir | ||
| 3 | package Build::Hopen::T::Gnu::C; | ||
| 4 | use Build::Hopen; | ||
| 5 | use Build::Hopen::Base; | ||
| 6 | |||
| 7 | our $VERSION = '0.000008'; # TRIAL | ||
| 8 | |||
| 9 | use parent 'Build::Hopen::Tool'; | ||
| 10 | use Class::Tiny qw(op files _cc); | ||
| 11 | |||
| 12 | use Build::Hopen::Arrrgs; | ||
| 13 | use Build::Hopen::BuildSystemGlobals; # For $DestDir. | ||
| 14 | # TODO make the dirs available to nodes through the context. | ||
| 15 | use Build::Hopen::G::GraphBuilder; | ||
| 16 | use Build::Hopen::Util::Data qw(forward_opts); | ||
| 17 | use Build::Hopen::Util::Filename; | ||
| 18 | use Config; | ||
| 19 | use Deep::Hash::Utils qw(deepvalue); | ||
| 20 | use File::Which (); | ||
| 21 | use Path::Class; | ||
| 22 | |||
| 23 | my $FN = Build::Hopen::Util::Filename->new; # for brevity | ||
| 24 | our $_CC; # Cached compiler name | ||
| 25 | |||
| 26 | # Docs {{{1 | ||
| 27 | |||
| 28 | =head1 NAME | ||
| 29 | |||
| 30 | Build::Hopen::T::Gnu::C - support for the GNU toolset, C language | ||
| 31 | |||
| 32 | =head1 SYNOPSIS | ||
| 33 | |||
| 34 | In a hopen file: | ||
| 35 | |||
| 36 | use language 'C'; | ||
| 37 | my $op = C->new(op=>'compile'); | ||
| 38 | # Create instances manually | ||
| 39 | |||
| 40 | # Or use via Build::Hopen::G::GraphBuilder: | ||
| 41 | $Build->H::files(...)->C::compile->default_goal; | ||
| 42 | |||
| 43 | The inputs come from earlier in the build graph. | ||
| 44 | TODO support specifying compiler arguments. | ||
| 45 | |||
| 46 | =head1 ATTRIBUTES | ||
| 47 | |||
| 48 | =head2 op | ||
| 49 | |||
| 50 | What this node is going to do: C<compile> or C<link>. | ||
| 51 | |||
| 52 | =head2 files | ||
| 53 | |||
| 54 | Arrayref of which files this node will process. Values are | ||
| 55 | destination file names. Extensions may be added. | ||
| 56 | |||
| 57 | =cut | ||
| 58 | |||
| 59 | # }}}1 | ||
| 60 | |||
| 61 | =head1 STATIC FUNCTIONS | ||
| 62 | |||
| 63 | Arguments to the static functions are parsed using L<Build::Hopen::Arrrgs>. | ||
| 64 | Therefore, named arguments start with a hyphen (e.g., C<< -name=>'foo' >>, | ||
| 65 | not C<< name=>'foo' >>). | ||
| 66 | |||
| 67 | =head2 compile | ||
| 68 | |||
| 69 | Create a new with L</op> set to C<compile>. Inputs come from the build graph, | ||
| 70 | so parameters other than C<-name> are disregarded (TODO permit specifying | ||
| 71 | compilation options or object-file names). Usage: | ||
| 72 | |||
| 73 | use language 'C'; | ||
| 74 | $builder_or_dag->H::files('file1.c')->C::compile([-name=>'node name']); | ||
| 75 | |||
| 76 | =cut | ||
| 77 | |||
| 78 | sub compile { | ||
| 79 | my ($builder, %args) = parameters('self', [qw(; name)], @_); | ||
| 80 | my $node = __PACKAGE__->new(op=>'compile', | ||
| 81 | forward_opts(\%args, 'name') | ||
| 82 | ); | ||
| 83 | |||
| 84 | hlog { __PACKAGE__, 'Built compile node', Dumper($node) } 2; | ||
| 85 | return $node; # The builder will automatically add it | ||
| 86 | } #compile() | ||
| 87 | |||
| 88 | make_GraphBuilder 'compile'; | ||
| 89 | |||
| 90 | =head2 link | ||
| 91 | |||
| 92 | Create a new with L</op> set to C<link>. Pass the name of the | ||
| 93 | executable. Usage: | ||
| 94 | |||
| 95 | use language 'C'; | ||
| 96 | $builder_or_dag->C::link('file1'[, -name=>'node name'); | ||
| 97 | |||
| 98 | =cut | ||
| 99 | |||
| 100 | sub link { | ||
| 101 | my ($builder, %args) = parameters('self', [qw(exe; name)], @_); | ||
| 102 | croak 'Need the name of the executable' unless $args{exe}; | ||
| 103 | |||
| 104 | my $node = __PACKAGE__->new( | ||
| 105 | op=>'link', files => [$DestDir->file($FN->exe($args{exe}))->absolute], | ||
| 106 | forward_opts(\%args, 'name') | ||
| 107 | ); | ||
| 108 | hlog { __PACKAGE__, 'Built link node', Dumper($node) } 2; | ||
| 109 | return $node; | ||
| 110 | } #link() | ||
| 111 | |||
| 112 | make_GraphBuilder 'link'; | ||
| 113 | |||
| 114 | =head1 MEMBER FUNCTIONS | ||
| 115 | |||
| 116 | =head2 _run | ||
| 117 | |||
| 118 | Create the compile or link command lines. | ||
| 119 | |||
| 120 | =cut | ||
| 121 | |||
| 122 | sub _run { | ||
| 123 | my ($self, %args) = parameters('self', [qw(phase ; generator *)], @_); | ||
| 124 | |||
| 125 | # Currently we only do things at gen time. | ||
| 126 | return $self->passthrough(-nocontext=>1) if $args{phase} ne 'Gen'; | ||
| 127 | |||
| 128 | # Find the work up to this point | ||
| 129 | my $hrOldWork = | ||
| 130 | $self->scope->find(-name => 'work', -set => '*', -levels => 'local') // {}; | ||
| 131 | |||
| 132 | if($self->op eq 'compile' && scalar keys %$hrOldWork != 1) { | ||
| 133 | die "C::compile nodes can only take one input at present"; | ||
| 134 | # TODO relax this requirement | ||
| 135 | } | ||
| 136 | my $lrOldWork = %$hrOldWork{(keys %$hrOldWork)[0]}; # list of hashrefs | ||
| 137 | |||
| 138 | hlog { 'found old work', Dumper($lrOldWork) } 2; | ||
| 139 | my ($lrFrom, @work); | ||
| 140 | |||
| 141 | $lrFrom = deepvalue($lrOldWork, qw(0 to)) // []; # don't autovivify | ||
| 142 | if($self->op eq 'compile' && $#$lrFrom != 0) { | ||
| 143 | die "C::compile nodes can only take one input filename at present"; | ||
| 144 | # TODO relax this requirement | ||
| 145 | } | ||
| 146 | |||
| 147 | # Add the new work | ||
| 148 | foreach my $file (@{$lrFrom}) { | ||
| 149 | my $hr = { from => [ $file ] }; | ||
| 150 | $hr->{to} = [ $self->op eq 'compile' ? | ||
| 151 | $FN->obj($file) : | ||
| 152 | $self->files->[0] ]; | ||
| 153 | $hr->{how} = $self->op eq 'compile' ? | ||
| 154 | $self->_cc . " -c #first -o #out" : | ||
| 155 | $self->_cc . " #first -o #out"; | ||
| 156 | push @work, $hr; | ||
| 157 | $lrFrom = [$file]; | ||
| 158 | } | ||
| 159 | |||
| 160 | # Add the existing work at the end | ||
| 161 | push @work, @$lrOldWork if @$lrOldWork; | ||
| 162 | |||
| 163 | return { work => \@work }; | ||
| 164 | } #run() | ||
| 165 | |||
| 166 | =head2 BUILD | ||
| 167 | |||
| 168 | Find the C compiler. | ||
| 169 | |||
| 170 | TODO should this happen when the DAG runs? | ||
| 171 | Maybe toolsets should get the chance to add a node to the beginning of | ||
| 172 | the graph, before anything else runs. TODO figure this out. | ||
| 173 | |||
| 174 | =cut | ||
| 175 | |||
| 176 | sub BUILD { | ||
| 177 | my ($self, $args) = @_; | ||
| 178 | |||
| 179 | if($_CC) { # Use the cached one if we already found it | ||
| 180 | $self->_cc($_CC); | ||
| 181 | return; | ||
| 182 | } | ||
| 183 | |||
| 184 | # Look for the compiler | ||
| 185 | foreach my $candidate ($Config{cc}, qw[cc gcc clang]) { # TODO also c89 or xlc? | ||
| 186 | my $path = File::Which::which($candidate); | ||
| 187 | next unless defined $path; | ||
| 188 | |||
| 189 | hlog { __PACKAGE__, 'using C compiler', $path }; # Got it | ||
| 190 | $self->_cc($path); | ||
| 191 | $_CC = $path; | ||
| 192 | last; | ||
| 193 | } | ||
| 194 | } #BUILD() | ||
| 195 | |||
| 196 | 1; | ||
| 197 | __END__ |