2 ######################################################################
3 # Do not call this script directly!
5 # The generate script ensures that @INC is correct before the engine
8 # Copyright (C) 2009 Marius Storm-Olsen <mstormo@gmail.com>
9 ######################################################################
16 my (%build_structure, %compile_options, @makedry);
17 my $out_dir = getcwd
();
18 my $git_dir = $out_dir;
20 $git_dir = dirname
($git_dir) while (!-e
"$git_dir/git.c" && "$git_dir" ne "");
21 die "Couldn't find Git repo" if ("$git_dir" eq "");
23 my @gens = Generators
::available
();
28 my $genlist = join(', ', @gens);
31 -g
<GENERATOR
> --gen
<GENERATOR
> Specify the buildsystem generator
(default: $gen)
33 -o
<PATH
> --out
<PATH
> Specify output directory generation
(default: .)
34 -i
<FILE
> --in <FILE
> Specify input file
, instead of running GNU Make
35 -h
,-?
--help This help
40 # Parse command-line options
42 my $arg = shift @ARGV;
43 if ("$arg" eq "-h" || "$arg" eq "--help" || "$arg" eq "-?") {
46 } elsif("$arg" eq "--out" || "$arg" eq "-o") {
47 $out_dir = shift @ARGV;
48 } elsif("$arg" eq "--gen" || "$arg" eq "-g") {
50 } elsif("$arg" eq "--in" || "$arg" eq "-i") {
51 my $infile = shift @ARGV;
52 open(F
, "<$infile") || die "Couldn't open file $infile";
58 # NOT using File::Spec->rel2abs($path, $base) here, as
59 # it fails badly for me in the msysgit environment
60 $git_dir = File
::Spec
->rel2abs($git_dir);
61 $out_dir = File
::Spec
->rel2abs($out_dir);
62 my $rel_dir = makeOutRel2Git
($git_dir, $out_dir);
64 # Print some information so the user feels informed
71 Running GNU Make to figure out build structure
...
74 # Pipe a make --dry-run into a variable, if not already loaded from file
75 @makedry = `cd $git_dir && make -n MSVC=1 V=1 2>/dev/null` if !@makedry;
77 # Parse the make output into usable info
80 # Finally, ask the generator to start generating..
81 Generators
::generate
($gen, $git_dir, $out_dir, $rel_dir, %build_structure);
84 # -------------------------------------------------------------------------------------------------
87 # 1) path: /foo/bar/baz 2) path: /foo/bar/baz 3) path: /foo/bar/baz
88 # base: /foo/bar/baz/temp base: /foo/bar base: /tmp
89 # rel: .. rel: baz rel: ../foo/bar/baz
92 my ($path, $base) = @_;
94 if ("$path" eq "$base") {
96 } elsif ($base =~ /^$path/) {
100 foreach (split('/', $tmp)) {
101 $rel .= "../" if ("$_" ne "");
103 } elsif ($path =~ /^$base/) {
110 foreach (split('/', $tmp)) {
111 $rel .= "../" if ("$_" ne "");
115 $rel =~ s/\/\//\//g
; # simplify
116 $rel =~ s/\/$//; # don't end with /
122 print "Parsing GNU Make output to figure out build structure...\n";
124 while (my $text = shift @makedry) {
130 chop $text if ($text =~ /\r$/);
131 if ($text =~ /\\$/) {
133 $text .= shift @makedry;
138 if($text =~ / -c /) {
140 handleCompileLine
($text, $line);
142 } elsif ($text =~ / -o /) {
144 handleLinkLine
($text, $line);
146 } elsif ($text =~ /\.o / && $text =~ /\.a /) {
148 handleLibLine
($text, $line);
150 # } elsif ($text =~ /^cp /) {
153 # } elsif ($text =~ /^rm -f /) {
156 # } elsif ($text =~ /^make[ \[]/) {
159 # } elsif ($text =~ /^echo /) {
162 # } elsif ($text =~ /^if /) {
163 # # shell conditional
165 # } elsif ($text =~ /^tclsh /) {
166 # # translation stuff
168 # } elsif ($text =~ /^umask /) {
169 # # handling boilerplates
171 # } elsif ($text =~ /\$\(\:\)/) {
174 # } elsif ($text =~ /^FLAGS=/) {
175 # # flags check for dependencies
177 # } elsif ($text =~ /^'\/usr\/bin\/perl' -MError -e/) {
178 # # perl commands for copying files
180 # } elsif ($text =~ /generate-cmdlist\.sh/) {
181 # # command for generating list of commands
183 # } elsif ($text =~ /^test / && $text =~ /|| rm -f /) {
184 # # commands removing executables, if they exist
186 # } elsif ($text =~ /new locations or Tcl/) {
187 # # command for detecting Tcl/Tk changes
189 # } elsif ($text =~ /mkdir -p/) {
190 # # command creating path
192 # } elsif ($text =~ /: no custom templates yet/) {
196 # print "Unhandled (line: $line): $text\n";
201 # print "Parsed build structure:\n";
202 # print Dumper(%build_structure);
205 # variables for the compilation part of each step
206 my (@defines, @incpaths, @cflags, @sources);
218 my (%dupHash, $entry);
219 %dupHash = map { $_, 1 } @defines;
220 @defines = keys %dupHash;
222 %dupHash = map { $_, 1 } @incpaths;
223 @incpaths = keys %dupHash;
225 %dupHash = map { $_, 1 } @cflags;
226 @cflags = keys %dupHash;
229 sub handleCompileLine
231 my ($line, $lineno) = @_;
232 my @parts = split(' ', $line);
234 shift(@parts); # ignore cmd
235 while (my $part = shift @parts) {
236 if ("$part" eq "-o") {
239 } elsif ("$part" eq "-c") {
240 # ignore compile flag
241 } elsif ("$part" eq "-c") {
242 } elsif ($part =~ /^.?-I/) {
243 push(@incpaths, $part);
244 } elsif ($part =~ /^.?-D/) {
245 push(@defines, $part);
246 } elsif ($part =~ /^-/) {
247 push(@cflags, $part);
248 } elsif ($part =~ /\.(c|cc|cpp)$/) {
251 die "Unhandled compiler option @ line $lineno: $part";
254 @
{$compile_options{"${sourcefile}_CFLAGS"}} = @cflags;
255 @
{$compile_options{"${sourcefile}_DEFINES"}} = @defines;
256 @
{$compile_options{"${sourcefile}_INCPATHS"}} = @incpaths;
262 my ($line, $lineno) = @_;
263 my (@objfiles, @lflags, $libout, $part);
264 # kill cmd and rm 'prefix'
265 $line =~ s/^rm -f .* && .* rcs //;
266 my @parts = split(' ', $line);
267 while ($part = shift @parts) {
269 push(@lflags, $part);
270 } elsif ($part =~ /\.(o|obj)$/) {
271 push(@objfiles, $part);
272 } elsif ($part =~ /\.(a|lib)$/) {
276 die "Unhandled lib option @ line $lineno: $part";
279 # print "LibOut: '$libout'\nLFlags: @lflags\nOfiles: @objfiles\n";
281 foreach (@objfiles) {
283 $sourcefile =~ s/\.o/.c/;
284 push(@sources, $sourcefile);
285 push(@cflags, @
{$compile_options{"${sourcefile}_CFLAGS"}});
286 push(@defines, @
{$compile_options{"${sourcefile}_DEFINES"}});
287 push(@incpaths, @
{$compile_options{"${sourcefile}_INCPATHS"}});
291 push(@
{$build_structure{"LIBS"}}, $libout);
292 @
{$build_structure{"LIBS_${libout}"}} = ("_DEFINES", "_INCLUDES", "_CFLAGS", "_SOURCES",
294 @
{$build_structure{"LIBS_${libout}_DEFINES"}} = @defines;
295 @
{$build_structure{"LIBS_${libout}_INCLUDES"}} = @incpaths;
296 @
{$build_structure{"LIBS_${libout}_CFLAGS"}} = @cflags;
297 @
{$build_structure{"LIBS_${libout}_LFLAGS"}} = @lflags;
298 @
{$build_structure{"LIBS_${libout}_SOURCES"}} = @sources;
299 @
{$build_structure{"LIBS_${libout}_OBJECTS"}} = @objfiles;
305 my ($line, $lineno) = @_;
306 my (@objfiles, @lflags, @libs, $appout, $part);
307 my @parts = split(' ', $line);
308 shift(@parts); # ignore cmd
309 while ($part = shift @parts) {
310 if ($part =~ /^-IGNORE/) {
311 push(@lflags, $part);
312 } elsif ($part =~ /^-[GRIMDO]/) {
314 } elsif ("$part" eq "-o") {
315 $appout = shift @parts;
316 } elsif ("$part" eq "-lz") {
317 push(@libs, "zlib.lib");
318 } elsif ("$part" eq "-lcrypto") {
319 push(@libs, "libeay32.lib");
320 push(@libs, "ssleay32.lib");
321 } elsif ($part =~ /^-/) {
322 push(@lflags, $part);
323 } elsif ($part =~ /\.(a|lib)$/) {
324 $part =~ s/\.a$/.lib/;
326 } elsif ($part =~ /\.(o|obj)$/) {
327 push(@objfiles, $part);
329 die "Unhandled lib option @ line $lineno: $part";
332 # print "AppOut: '$appout'\nLFlags: @lflags\nLibs : @libs\nOfiles: @objfiles\n";
334 foreach (@objfiles) {
336 $sourcefile =~ s/\.o/.c/;
337 push(@sources, $sourcefile);
338 push(@cflags, @
{$compile_options{"${sourcefile}_CFLAGS"}});
339 push(@defines, @
{$compile_options{"${sourcefile}_DEFINES"}});
340 push(@incpaths, @
{$compile_options{"${sourcefile}_INCPATHS"}});
345 push(@
{$build_structure{"APPS"}}, $appout);
346 @
{$build_structure{"APPS_${appout}"}} = ("_DEFINES", "_INCLUDES", "_CFLAGS", "_LFLAGS",
347 "_SOURCES", "_OBJECTS", "_LIBS");
348 @
{$build_structure{"APPS_${appout}_DEFINES"}} = @defines;
349 @
{$build_structure{"APPS_${appout}_INCLUDES"}} = @incpaths;
350 @
{$build_structure{"APPS_${appout}_CFLAGS"}} = @cflags;
351 @
{$build_structure{"APPS_${appout}_LFLAGS"}} = @lflags;
352 @
{$build_structure{"APPS_${appout}_SOURCES"}} = @sources;
353 @
{$build_structure{"APPS_${appout}_OBJECTS"}} = @objfiles;
354 @
{$build_structure{"APPS_${appout}_LIBS"}} = @libs;