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 ######################################################################
17 my (%build_structure, %compile_options, @makedry);
18 my $out_dir = getcwd();
19 my $git_dir = $out_dir;
21 $git_dir = dirname($git_dir) while (!-e "$git_dir/git.c" && "$git_dir" ne "");
22 die "Couldn't find Git repo" if ("$git_dir" eq "");
24 my @gens = Generators::available();
29 my $genlist = join(', ', @gens);
32 -g <GENERATOR> --gen <GENERATOR> Specify the buildsystem generator (default: $gen)
34 -o <PATH> --out <PATH> Specify output directory generation (default: .)
35 --make-out <PATH> Write the output of GNU Make into a file
36 -i <FILE> --in <FILE> Specify input file, instead of running GNU Make
37 -h,-? --help This help
42 # Parse command-line options
45 my $arg = shift @ARGV;
46 if ("$arg" eq "-h" || "$arg" eq "--help" || "$arg" eq "-?") {
49 } elsif("$arg" eq "--out" || "$arg" eq "-o") {
50 $out_dir = shift @ARGV;
51 } elsif("$arg" eq "--make-out") {
52 $make_out = shift @ARGV;
53 } elsif("$arg" eq "--gen" || "$arg" eq "-g") {
55 } elsif("$arg" eq "--in" || "$arg" eq "-i") {
56 my $infile = shift @ARGV;
57 open(F, "<$infile") || die "Couldn't open file $infile";
61 die "Unknown option: " . $arg;
65 # NOT using File::Spec->rel2abs($path, $base) here, as
66 # it fails badly for me in the msysgit environment
67 $git_dir = File::Spec->rel2abs($git_dir);
68 $out_dir = File::Spec->rel2abs($out_dir);
69 my $rel_dir = makeOutRel2Git($git_dir, $out_dir);
71 # Print some information so the user feels informed
78 Running GNU Make to figure out build structure...
81 # Pipe a make --dry-run into a variable, if not already loaded from file
82 # Capture the make dry stderr to file for review (will be empty for a release build).
84 my $ErrsFile = "msvc-build-makedryerrors.txt";
85 @makedry = `make -C $git_dir -n MSVC=1 V=1 2>$ErrsFile` if !@makedry;
86 # test for an empty Errors file and remove it
87 unlink $ErrsFile if -f -z $ErrsFile;
89 if (defined $make_out) {
90 open OUT, ">" . $make_out;
95 # Parse the make output into usable info
98 # Finally, ask the generator to start generating..
99 Generators::generate($gen, $git_dir, $out_dir, $rel_dir, %build_structure);
101 # main flow ends here
102 # -------------------------------------------------------------------------------------------------
105 # 1) path: /foo/bar/baz 2) path: /foo/bar/baz 3) path: /foo/bar/baz
106 # base: /foo/bar/baz/temp base: /foo/bar base: /tmp
107 # rel: .. rel: baz rel: ../foo/bar/baz
110 my ($path, $base) = @_;
112 if ("$path" eq "$base") {
114 } elsif ($base =~ /^$path/) {
118 foreach (split('/', $tmp)) {
119 $rel .= "../" if ("$_" ne "");
121 } elsif ($path =~ /^$base/) {
128 foreach (split('/', $tmp)) {
129 $rel .= "../" if ("$_" ne "");
133 $rel =~ s/\/\//\//g; # simplify
134 $rel =~ s/\/$//; # don't end with /
140 print "Parsing GNU Make output to figure out build structure...\n";
142 while (my $text = shift @makedry) {
148 chop $text if ($text =~ /\r$/);
149 if ($text =~ /\\$/) {
151 $text .= shift @makedry;
156 if ($text =~ /^test /) {
157 # options to test (eg -o) may be mistaken for linker options
161 if ($text =~ /^(mkdir|msgfmt) /) {
162 # options to the Portable Object translations
163 # the line "mkdir ... && msgfmt ..." contains no linker options
167 if($text =~ / -c /) {
169 handleCompileLine($text, $line);
171 } elsif ($text =~ / -o /) {
173 handleLinkLine($text, $line);
175 } elsif ($text =~ /\.o / && $text =~ /\.a /) {
177 handleLibLine($text, $line);
179 # } elsif ($text =~ /^cp /) {
182 # } elsif ($text =~ /^rm -f /) {
185 # } elsif ($text =~ /^make[ \[]/) {
188 # } elsif ($text =~ /^echo /) {
191 # } elsif ($text =~ /^if /) {
192 # # shell conditional
194 # } elsif ($text =~ /^tclsh /) {
195 # # translation stuff
197 # } elsif ($text =~ /^umask /) {
198 # # handling boilerplates
200 # } elsif ($text =~ /\$\(\:\)/) {
203 # } elsif ($text =~ /^FLAGS=/) {
204 # # flags check for dependencies
206 # } elsif ($text =~ /^'\/usr\/bin\/perl' -MError -e/) {
207 # # perl commands for copying files
209 # } elsif ($text =~ /generate-cmdlist\.sh/) {
210 # # command for generating list of commands
212 # } elsif ($text =~ /new locations or Tcl/) {
213 # # command for detecting Tcl/Tk changes
215 # } elsif ($text =~ /mkdir -p/) {
216 # # command creating path
218 # } elsif ($text =~ /: no custom templates yet/) {
222 # print "Unhandled (line: $line): $text\n";
227 # print "Parsed build structure:\n";
228 # print Dumper(%build_structure);
231 # variables for the compilation part of each step
232 my (@defines, @incpaths, @cflags, @sources);
244 my (%dupHash, $entry);
245 %dupHash = map { $_, 1 } @defines;
246 @defines = keys %dupHash;
248 %dupHash = map { $_, 1 } @incpaths;
249 @incpaths = keys %dupHash;
251 %dupHash = map { $_, 1 } @cflags;
252 @cflags = keys %dupHash;
255 sub handleCompileLine
257 my ($line, $lineno) = @_;
258 my @parts = shellwords($line);
260 shift(@parts); # ignore cmd
261 while (my $part = shift @parts) {
262 if ("$part" eq "-o") {
265 } elsif ("$part" eq "-c") {
266 # ignore compile flag
267 } elsif ("$part" eq "-c") {
268 } elsif ($part =~ /^.?-I/) {
269 push(@incpaths, $part);
270 } elsif ($part =~ /^.?-D/) {
271 push(@defines, $part);
272 } elsif ($part =~ /^-/) {
273 push(@cflags, $part);
274 } elsif ($part =~ /\.(c|cc|cpp)$/) {
277 die "Unhandled compiler option @ line $lineno: $part";
280 @{$compile_options{"${sourcefile}_CFLAGS"}} = @cflags;
281 @{$compile_options{"${sourcefile}_DEFINES"}} = @defines;
282 @{$compile_options{"${sourcefile}_INCPATHS"}} = @incpaths;
288 my ($line, $lineno) = @_;
289 my (@objfiles, @lflags, $libout, $part);
290 # kill cmd and rm 'prefix'
291 $line =~ s/^rm -f .* && .* rcs //;
292 my @parts = shellwords($line);
293 while ($part = shift @parts) {
295 push(@lflags, $part);
296 } elsif ($part =~ /\.(o|obj)$/) {
297 push(@objfiles, $part);
298 } elsif ($part =~ /\.(a|lib)$/) {
302 die "Unhandled lib option @ line $lineno: $part";
305 # print "LibOut: '$libout'\nLFlags: @lflags\nOfiles: @objfiles\n";
307 foreach (@objfiles) {
309 $sourcefile =~ s/\.o$/.c/;
310 push(@sources, $sourcefile);
311 push(@cflags, @{$compile_options{"${sourcefile}_CFLAGS"}});
312 push(@defines, @{$compile_options{"${sourcefile}_DEFINES"}});
313 push(@incpaths, @{$compile_options{"${sourcefile}_INCPATHS"}});
317 push(@{$build_structure{"LIBS"}}, $libout);
318 @{$build_structure{"LIBS_${libout}"}} = ("_DEFINES", "_INCLUDES", "_CFLAGS", "_SOURCES",
320 @{$build_structure{"LIBS_${libout}_DEFINES"}} = @defines;
321 @{$build_structure{"LIBS_${libout}_INCLUDES"}} = @incpaths;
322 @{$build_structure{"LIBS_${libout}_CFLAGS"}} = @cflags;
323 @{$build_structure{"LIBS_${libout}_LFLAGS"}} = @lflags;
324 @{$build_structure{"LIBS_${libout}_SOURCES"}} = @sources;
325 @{$build_structure{"LIBS_${libout}_OBJECTS"}} = @objfiles;
331 my ($line, $lineno) = @_;
332 my (@objfiles, @lflags, @libs, $appout, $part);
333 my @parts = shellwords($line);
334 shift(@parts); # ignore cmd
335 while ($part = shift @parts) {
336 if ($part =~ /^-IGNORE/) {
337 push(@lflags, $part);
338 } elsif ($part =~ /^-[GRIMDO]/) {
340 } elsif ("$part" eq "-o") {
341 $appout = shift @parts;
342 } elsif ("$part" eq "-lz") {
343 push(@libs, "zlib.lib");
344 } elsif ("$part" eq "-lcrypto") {
345 push(@libs, "libeay32.lib");
346 } elsif ("$part" eq "-lssl") {
347 push(@libs, "ssleay32.lib");
348 } elsif ("$part" eq "-lcurl") {
349 push(@libs, "libcurl.lib");
350 } elsif ("$part" eq "-lexpat") {
351 push(@libs, "expat.lib");
352 } elsif ("$part" eq "-liconv") {
353 push(@libs, "libiconv.lib");
354 } elsif ($part =~ /^[-\/]/) {
355 push(@lflags, $part);
356 } elsif ($part =~ /\.(a|lib)$/) {
357 $part =~ s/\.a$/.lib/;
359 } elsif ($part eq 'invalidcontinue.obj') {
360 # ignore - known to MSVC
361 } elsif ($part =~ /\.o$/) {
362 push(@objfiles, $part);
363 } elsif ($part =~ /\.obj$/) {
364 # do nothing, 'make' should not be producing .obj, only .o files
366 die "Unhandled link option @ line $lineno: $part";
369 # print "AppOut: '$appout'\nLFlags: @lflags\nLibs : @libs\nOfiles: @objfiles\n";
371 foreach (@objfiles) {
373 $sourcefile =~ s/\.o$/.c/;
374 push(@sources, $sourcefile);
375 push(@cflags, @{$compile_options{"${sourcefile}_CFLAGS"}});
376 push(@defines, @{$compile_options{"${sourcefile}_DEFINES"}});
377 push(@incpaths, @{$compile_options{"${sourcefile}_INCPATHS"}});
382 push(@{$build_structure{"APPS"}}, $appout);
383 @{$build_structure{"APPS_${appout}"}} = ("_DEFINES", "_INCLUDES", "_CFLAGS", "_LFLAGS",
384 "_SOURCES", "_OBJECTS", "_LIBS");
385 @{$build_structure{"APPS_${appout}_DEFINES"}} = @defines;
386 @{$build_structure{"APPS_${appout}_INCLUDES"}} = @incpaths;
387 @{$build_structure{"APPS_${appout}_CFLAGS"}} = @cflags;
388 @{$build_structure{"APPS_${appout}_LFLAGS"}} = @lflags;
389 @{$build_structure{"APPS_${appout}_SOURCES"}} = @sources;
390 @{$build_structure{"APPS_${appout}_OBJECTS"}} = @objfiles;
391 @{$build_structure{"APPS_${appout}_LIBS"}} = @libs;