| 1 | # App::hopen::T::Gnu::C - support GNU toolset, C language | ||||||||||
| 2 | package App::hopen::T::Gnu::C; | ||||||||||
| 3 | 1 | ▶ | use Data::Hopen; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use strict; use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use Data::Hopen::Base; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 1 | our $VERSION = '0.000013'; # TRIAL | |||||||||
| 8 | |||||||||||
| 9 | 1 | ▶ | use App::hopen::BuildSystemGlobals; # For $DestDir. | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | # TODO make the dirs available to nodes through the context. | ||||||||||
| 11 | 1 | ▶ | use App::hopen::Util::BasedPath; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | 1 | ▶ | use App::hopen::T::Gnu::C::CompileCmd; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 1 | ▶ | use App::hopen::T::Gnu::C::LinkCmd; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | |||||||||||
| 16 | 1 | ▶ | use Config; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | 1 | ▶ | use Data::Hopen qw(getparameters); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | 1 | ▶ | use Data::Hopen::G::GraphBuilder; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 19 | 1 | ▶ | use Data::Hopen::Util::Data qw(forward_opts); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 20 | 1 | ▶ | use Data::Hopen::Util::Filename; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 21 | 1 | ▶ | use File::Which (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 22 | 1 | ▶ | use Path::Class; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 23 | |||||||||||
| 24 | 1 | my $FN = Data::Hopen::Util::Filename->new; # for brevity | |||||||||
| 25 | 1 | our $_CC; # Cached compiler name | |||||||||
| 26 | |||||||||||
| 27 | # Docs {{{1 | ||||||||||
| 28 | |||||||||||
| 29 | =head1 NAME | ||||||||||
| 30 | |||||||||||
| 31 | App::hopen::T::Gnu::C - support for the GNU toolset, C language | ||||||||||
| 32 | |||||||||||
| 33 | =head1 SYNOPSIS | ||||||||||
| 34 | |||||||||||
| 35 | In a hopen file: | ||||||||||
| 36 | |||||||||||
| 37 | use language 'C'; | ||||||||||
| 38 | |||||||||||
| 39 | # Use via Data::Hopen::G::GraphBuilder: | ||||||||||
| 40 | $Build->H::files(...)->C::compile->default_goal; | ||||||||||
| 41 | |||||||||||
| 42 | The inputs come from earlier in the build graph. | ||||||||||
| 43 | TODO support specifying compiler arguments. | ||||||||||
| 44 | |||||||||||
| 45 | =cut | ||||||||||
| 46 | |||||||||||
| 47 | # }}}1 | ||||||||||
| 48 | |||||||||||
| 49 | =head1 STATIC FUNCTIONS | ||||||||||
| 50 | |||||||||||
| 51 | Arguments to the static functions are parsed using L<Getargs::Mixed> | ||||||||||
| 52 | (via L<Data::Hopen/getparameters>). | ||||||||||
| 53 | Therefore, named arguments start with a hyphen (e.g., C<< -name=>'foo' >>, | ||||||||||
| 54 | not C<< name=>'foo' >>). | ||||||||||
| 55 | |||||||||||
| 56 | =head2 compile | ||||||||||
| 57 | |||||||||||
| 58 | Create a new compilation command. Inputs come from the build graph, | ||||||||||
| 59 | so parameters other than C<-name> are disregarded (TODO permit specifying | ||||||||||
| 60 | compilation options or object-file names). Usage: | ||||||||||
| 61 | |||||||||||
| 62 | use language 'C'; | ||||||||||
| 63 | $builder_or_dag->H::files('file1.c')->C::compile([-name=>'node name']); | ||||||||||
| 64 | |||||||||||
| 65 | =cut | ||||||||||
| 66 | |||||||||||
| 67 | sub _find_compiler; # forward | ||||||||||
| 68 | |||||||||||
| 69 | sub compile { | ||||||||||
| 70 | 3 | ▶ | my ($builder, %args) = getparameters('self', [qw(; name)], @_); | ||||||||
|
Subroutine
compile: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||
| 71 | 3 | ▶ | _find_compiler unless $_CC; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 72 | 3 | my $node = App::hopen::T::Gnu::C::CompileCmd->new( | |||||||||
| 73 | compiler => $_CC, | ||||||||||
| 74 | forward_opts(\%args, 'name') | ||||||||||
| 75 | ); | ||||||||||
| 76 | |||||||||||
| 77 | 3 | hlog { __PACKAGE__, 'Built compile node', Dumper($node) } 2; | |||||||||
| 78 | |||||||||||
| 79 | 3 | return $node; # The builder will automatically add it | |||||||||
| 80 | } #compile() | ||||||||||
| 81 | |||||||||||
| 82 | 1 | make_GraphBuilder 'compile'; | |||||||||
| 83 | |||||||||||
| 84 | =head2 link | ||||||||||
| 85 | |||||||||||
| 86 | Create a new link command. Pass the name of the | ||||||||||
| 87 | executable. Object files are on the incoming asset-graph edges. Usage: | ||||||||||
| 88 | |||||||||||
| 89 | use language 'C'; | ||||||||||
| 90 | $builder_or_dag->C::link([-exe=>]'output_file_name'[, [-name=>]'node name']); | ||||||||||
| 91 | |||||||||||
| 92 | TODO? Permit specifying that you want C<ld> or another linker instead of | ||||||||||
| 93 | using the compiler? | ||||||||||
| 94 | |||||||||||
| 95 | =cut | ||||||||||
| 96 | |||||||||||
| 97 | sub link { | ||||||||||
| 98 | 3 | ▶ | my ($builder, %args) = getparameters('self', [qw(exe; name)], @_); | ||||||||
|
Subroutine
link: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||
| 99 | 3 | ▶ | _find_compiler unless $_CC; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 100 | |||||||||||
| 101 | 3 | my $dest = based_path(path => file($FN->exe($args{exe})), base => $DestDir); | |||||||||
| 102 | |||||||||||
| 103 | 3 | my $node = App::hopen::T::Gnu::C::LinkCmd->new( | |||||||||
| 104 | linker => $_CC, | ||||||||||
| 105 | dest => $dest, | ||||||||||
| 106 | forward_opts(\%args, 'name') | ||||||||||
| 107 | ); | ||||||||||
| 108 | 3 | hlog { __PACKAGE__, 'Built link node', Dumper($node) } 2; | |||||||||
| 109 | |||||||||||
| 110 | 3 | return $node; | |||||||||
| 111 | } #link() | ||||||||||
| 112 | |||||||||||
| 113 | 1 | make_GraphBuilder 'link'; | |||||||||
| 114 | |||||||||||
| 115 | =head1 INTERNALS | ||||||||||
| 116 | |||||||||||
| 117 | =head2 _find_compiler | ||||||||||
| 118 | |||||||||||
| 119 | Find the C compiler. Called when this package is first loaded. | ||||||||||
| 120 | |||||||||||
| 121 | TODO permit the user to specify an alternative compiler to use | ||||||||||
| 122 | |||||||||||
| 123 | TODO should this happen when the DAG runs? | ||||||||||
| 124 | Maybe toolsets should get the chance to add a node to the beginning of | ||||||||||
| 125 | the graph, before anything else runs. TODO figure this out. | ||||||||||
| 126 | |||||||||||
| 127 | =cut | ||||||||||
| 128 | |||||||||||
| 129 | sub _find_compiler { | ||||||||||
| 130 | 1 | ▶ | foreach my $candidate ($Config{cc}, qw[cc gcc clang]) { # TODO also c89 or xlc? | ||||||||
|
Subroutine
_find_compiler: called CC=4 SCAR=14.5
| |||||||||||
| 131 | 1 | my $path = File::Which::which($candidate); | |||||||||
| 132 | 1 | ▶ | next unless defined $path; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 133 | |||||||||||
| 134 | 1 | ▶ | hlog { __PACKAGE__, 'using C compiler', $path }; # Got it | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||
| 135 | 1 | $_CC = $path; | |||||||||
| 136 | 1 | last; | |||||||||
| 137 | } | ||||||||||
| 138 | |||||||||||
| 139 | 1 | ▶ | croak "Could not find a C compiler" unless $_CC; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 140 | } #_find_compiler() | ||||||||||
| 141 | |||||||||||
| 142 | 1 | ▶ | BEGIN { _find_compiler if eval '$App::hopen::RUNNING'; } | ||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||
| 143 | |||||||||||
| 144 | 1 | 1; | |||||||||
| 145 | __END__ | ||||||||||
| 146 | # vi: set fdm=marker: # | ||||||||||