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 SKIP_VCPKG=1 V=1 2>$ErrsFile`
87 # test for an empty Errors file and remove it
88 unlink $ErrsFile if -f -z $ErrsFile;
90 if (defined $make_out) {
91 open OUT, ">" . $make_out;
96 # Parse the make output into usable info
99 # Finally, ask the generator to start generating..
100 Generators::generate($gen, $git_dir, $out_dir, $rel_dir, %build_structure);
102 # main flow ends here
103 # -------------------------------------------------------------------------------------------------
106 # 1) path: /foo/bar/baz 2) path: /foo/bar/baz 3) path: /foo/bar/baz
107 # base: /foo/bar/baz/temp base: /foo/bar base: /tmp
108 # rel: .. rel: baz rel: ../foo/bar/baz
111 my ($path, $base) = @_;
113 if ("$path" eq "$base") {
115 } elsif ($base =~ /^$path/) {
119 foreach (split('/', $tmp)) {
120 $rel .= "../" if ("$_" ne "");
122 } elsif ($path =~ /^$base/) {
129 foreach (split('/', $tmp)) {
130 $rel .= "../" if ("$_" ne "");
134 $rel =~ s/\/\//\//g; # simplify
135 $rel =~ s/\/$//; # don't end with /
141 print "Parsing GNU Make output to figure out build structure...\n";
143 while (my $text = shift @makedry) {
149 chop $text if ($text =~ /\r$/);
150 if ($text =~ /\\$/) {
152 $text .= shift @makedry;
157 if ($text =~ /^test /) {
158 # options to test (eg -o) may be mistaken for linker options
162 if ($text =~ /^(mkdir|msgfmt) /) {
163 # options to the Portable Object translations
164 # the line "mkdir ... && msgfmt ..." contains no linker options
168 if($text =~ / -c /) {
170 handleCompileLine($text, $line);
172 } elsif ($text =~ / -o /) {
174 handleLinkLine($text, $line);
176 } elsif ($text =~ /\.o / && $text =~ /\.a /) {
178 handleLibLine($text, $line);
180 # } elsif ($text =~ /^cp /) {
183 # } elsif ($text =~ /^rm -f /) {
186 # } elsif ($text =~ /^make[ \[]/) {
189 # } elsif ($text =~ /^echo /) {
192 # } elsif ($text =~ /^if /) {
193 # # shell conditional
195 # } elsif ($text =~ /^tclsh /) {
196 # # translation stuff
198 # } elsif ($text =~ /^umask /) {
199 # # handling boilerplates
201 # } elsif ($text =~ /\$\(\:\)/) {
204 # } elsif ($text =~ /^FLAGS=/) {
205 # # flags check for dependencies
207 # } elsif ($text =~ /^'\/usr\/bin\/perl' -MError -e/) {
208 # # perl commands for copying files
210 # } elsif ($text =~ /generate-cmdlist\.sh/) {
211 # # command for generating list of commands
213 # } elsif ($text =~ /new locations or Tcl/) {
214 # # command for detecting Tcl/Tk changes
216 # } elsif ($text =~ /mkdir -p/) {
217 # # command creating path
219 # } elsif ($text =~ /: no custom templates yet/) {
223 # print "Unhandled (line: $line): $text\n";
228 # print "Parsed build structure:\n";
229 # print Dumper(%build_structure);
232 # variables for the compilation part of each step
233 my (@defines, @incpaths, @cflags, @sources);
245 my (%dupHash, $entry);
246 %dupHash = map { $_, 1 } @defines;
247 @defines = keys %dupHash;
249 %dupHash = map { $_, 1 } @incpaths;
250 @incpaths = keys %dupHash;
252 %dupHash = map { $_, 1 } @cflags;
253 @cflags = keys %dupHash;
256 sub handleCompileLine
258 my ($line, $lineno) = @_;
259 my @parts = shellwords($line);
261 shift(@parts); # ignore cmd
262 while (my $part = shift @parts) {
263 if ("$part" eq "-o") {
266 } elsif ("$part" eq "-c") {
267 # ignore compile flag
268 } elsif ("$part" eq "-c") {
269 } elsif ($part =~ /^.?-I/) {
270 push(@incpaths, $part);
271 } elsif ($part =~ /^.?-D/) {
272 push(@defines, $part);
273 } elsif ($part =~ /^-/) {
274 push(@cflags, $part);
275 } elsif ($part =~ /\.(c|cc|cpp)$/) {
278 die "Unhandled compiler option @ line $lineno: $part";
281 @{$compile_options{"${sourcefile}_CFLAGS"}} = @cflags;
282 @{$compile_options{"${sourcefile}_DEFINES"}} = @defines;
283 @{$compile_options{"${sourcefile}_INCPATHS"}} = @incpaths;
289 my ($line, $lineno) = @_;
290 my (@objfiles, @lflags, $libout, $part);
291 # kill cmd and rm 'prefix'
292 $line =~ s/^rm -f .* && .* rcs //;
293 my @parts = shellwords($line);
294 while ($part = shift @parts) {
296 push(@lflags, $part);
297 } elsif ($part =~ /\.(o|obj)$/) {
298 push(@objfiles, $part);
299 } elsif ($part =~ /\.(a|lib)$/) {
303 die "Unhandled lib option @ line $lineno: $part";
306 # print "LibOut: '$libout'\nLFlags: @lflags\nOfiles: @objfiles\n";
308 foreach (@objfiles) {
310 $sourcefile =~ s/\.o$/.c/;
311 push(@sources, $sourcefile);
312 push(@cflags, @{$compile_options{"${sourcefile}_CFLAGS"}});
313 push(@defines, @{$compile_options{"${sourcefile}_DEFINES"}});
314 push(@incpaths, @{$compile_options{"${sourcefile}_INCPATHS"}});
318 push(@{$build_structure{"LIBS"}}, $libout);
319 @{$build_structure{"LIBS_${libout}"}} = ("_DEFINES", "_INCLUDES", "_CFLAGS", "_SOURCES",
321 @{$build_structure{"LIBS_${libout}_DEFINES"}} = @defines;
322 @{$build_structure{"LIBS_${libout}_INCLUDES"}} = @incpaths;
323 @{$build_structure{"LIBS_${libout}_CFLAGS"}} = @cflags;
324 @{$build_structure{"LIBS_${libout}_LFLAGS"}} = @lflags;
325 @{$build_structure{"LIBS_${libout}_SOURCES"}} = @sources;
326 @{$build_structure{"LIBS_${libout}_OBJECTS"}} = @objfiles;
332 my ($line, $lineno) = @_;
333 my (@objfiles, @lflags, @libs, $appout, $part);
334 my @parts = shellwords($line);
335 shift(@parts); # ignore cmd
336 while ($part = shift @parts) {
337 if ($part =~ /^-IGNORE/) {
338 push(@lflags, $part);
339 } elsif ($part =~ /^-[GRIMDO]/) {
341 } elsif ("$part" eq "-o") {
342 $appout = shift @parts;
343 } elsif ("$part" eq "-lz") {
344 push(@libs, "zlib.lib");
345 } elsif ("$part" eq "-lcrypto") {
346 push(@libs, "libcrypto.lib");
347 } elsif ("$part" eq "-lssl") {
348 push(@libs, "libssl.lib");
349 } elsif ("$part" eq "-lcurl") {
350 push(@libs, "libcurl.lib");
351 } elsif ("$part" eq "-lexpat") {
352 push(@libs, "libexpat.lib");
353 } elsif ("$part" eq "-liconv") {
354 push(@libs, "iconv.lib");
355 } elsif ($part =~ /^[-\/]/) {
356 push(@lflags, $part);
357 } elsif ($part =~ /\.(a|lib)$/) {
358 $part =~ s/\.a$/.lib/;
360 } elsif ($part eq 'invalidcontinue.obj') {
361 # ignore - known to MSVC
362 } elsif ($part =~ /\.o$/) {
363 push(@objfiles, $part);
364 } elsif ($part =~ /\.obj$/) {
365 # do nothing, 'make' should not be producing .obj, only .o files
367 die "Unhandled link option @ line $lineno: $part";
370 # print "AppOut: '$appout'\nLFlags: @lflags\nLibs : @libs\nOfiles: @objfiles\n";
372 foreach (@objfiles) {
374 $sourcefile =~ s/\.o$/.c/;
375 push(@sources, $sourcefile);
376 push(@cflags, @{$compile_options{"${sourcefile}_CFLAGS"}});
377 push(@defines, @{$compile_options{"${sourcefile}_DEFINES"}});
378 push(@incpaths, @{$compile_options{"${sourcefile}_INCPATHS"}});
383 push(@{$build_structure{"APPS"}}, $appout);
384 @{$build_structure{"APPS_${appout}"}} = ("_DEFINES", "_INCLUDES", "_CFLAGS", "_LFLAGS",
385 "_SOURCES", "_OBJECTS", "_LIBS");
386 @{$build_structure{"APPS_${appout}_DEFINES"}} = @defines;
387 @{$build_structure{"APPS_${appout}_INCLUDES"}} = @incpaths;
388 @{$build_structure{"APPS_${appout}_CFLAGS"}} = @cflags;
389 @{$build_structure{"APPS_${appout}_LFLAGS"}} = @lflags;
390 @{$build_structure{"APPS_${appout}_SOURCES"}} = @sources;
391 @{$build_structure{"APPS_${appout}_OBJECTS"}} = @objfiles;
392 @{$build_structure{"APPS_${appout}_LIBS"}} = @libs;