| 1 | # App::hopen::T::MSCL::C - support MS cl.exe toolset, C language | ||
| 2 | package App::hopen::T::MSCL::C; | ||
| 3 | use Data::Hopen; | ||
| 4 | use strict; use warnings; | ||
| 5 | use Data::Hopen::Base; | ||
| 6 | |||
| 7 | our $VERSION = '0.000013'; # TRIAL | ||
| 8 | |||
| 9 | use App::hopen::BuildSystemGlobals; # For $DestDir. | ||
| 10 | # TODO make the dirs available to nodes through the context. | ||
| 11 | use App::hopen::Util::BasedPath; | ||
| 12 | |||
| 13 | use App::hopen::T::MSCL::C::CompileCmd; | ||
| 14 | use App::hopen::T::MSCL::C::LinkCmd; | ||
| 15 | |||
| 16 | use Config; | ||
| 17 | use Data::Hopen qw(getparameters); | ||
| 18 | use Data::Hopen::G::GraphBuilder; | ||
| 19 | use Data::Hopen::Util::Data qw(forward_opts); | ||
| 20 | use Data::Hopen::Util::Filename; | ||
| 21 | use File::Which (); | ||
| 22 | use Path::Class; | ||
| 23 | |||
| 24 | my $FN = Data::Hopen::Util::Filename->new; # for brevity | ||
| 25 | our $_CC; # Cached compiler name | ||
| 26 | |||
| 27 | # Docs {{{1 | ||
| 28 | |||
| 29 | =head1 NAME | ||
| 30 | |||
| 31 | App::hopen::T::MSCL::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 | my ($builder, %args) = getparameters('self', [qw(; name)], @_); | ||
| 71 | _find_compiler unless $_CC; | ||
| 72 | my $node = App::hopen::T::MSCL::C::CompileCmd->new( | ||
| 73 | compiler => $_CC, | ||
| 74 | forward_opts(\%args, 'name') | ||
| 75 | ); | ||
| 76 | |||
| 77 | hlog { __PACKAGE__, 'Built compile node', Dumper($node) } 2; | ||
| 78 | |||
| 79 | return $node; # The builder will automatically add it | ||
| 80 | } #compile() | ||
| 81 | |||
| 82 | 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 | my ($builder, %args) = getparameters('self', [qw(exe; name)], @_); | ||
| 99 | _find_compiler unless $_CC; | ||
| 100 | |||
| 101 | my $dest = based_path(path => file($FN->exe($args{exe})), base => $DestDir); | ||
| 102 | |||
| 103 | my $node = App::hopen::T::MSCL::C::LinkCmd->new( | ||
| 104 | linker => $_CC, | ||
| 105 | dest => $dest, | ||
| 106 | forward_opts(\%args, 'name') | ||
| 107 | ); | ||
| 108 | hlog { __PACKAGE__, 'Built link node', Dumper($node) } 2; | ||
| 109 | |||
| 110 | return $node; | ||
| 111 | } #link() | ||
| 112 | |||
| 113 | 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 | foreach my $candidate (qw[cl]) { | ||
| 131 | my $path = File::Which::which($candidate); | ||
| 132 | next unless defined $path; | ||
| 133 | |||
| 134 | hlog { __PACKAGE__, 'using C compiler', $path }; # Got it | ||
| 135 | $_CC = $path; | ||
| 136 | last; | ||
| 137 | } | ||
| 138 | |||
| 139 | croak "Could not find a C compiler" unless $_CC; | ||
| 140 | } #_find_compiler() | ||
| 141 | |||
| 142 | BEGIN { _find_compiler if eval '$App::hopen::RUNNING'; } | ||
| 143 | |||
| 144 | 1; | ||
| 145 | __END__ | ||
| 146 | # vi: set fdm=marker: # |