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 =~ /^test /) {
139 # options to test (eg -o) may be mistaken for linker options
143 if($text =~ / -c /) {
145 handleCompileLine($text, $line);
147 } elsif ($text =~ / -o /) {
149 handleLinkLine($text, $line);
151 } elsif ($text =~ /\.o / && $text =~ /\.a /) {
153 handleLibLine($text, $line);
155 # } elsif ($text =~ /^cp /) {
158 # } elsif ($text =~ /^rm -f /) {
161 # } elsif ($text =~ /^make[ \[]/) {
164 # } elsif ($text =~ /^echo /) {
167 # } elsif ($text =~ /^if /) {
168 # # shell conditional
170 # } elsif ($text =~ /^tclsh /) {
171 # # translation stuff
173 # } elsif ($text =~ /^umask /) {
174 # # handling boilerplates
176 # } elsif ($text =~ /\$\(\:\)/) {
179 # } elsif ($text =~ /^FLAGS=/) {
180 # # flags check for dependencies
182 # } elsif ($text =~ /^'\/usr\/bin\/perl' -MError -e/) {
183 # # perl commands for copying files
185 # } elsif ($text =~ /generate-cmdlist\.sh/) {
186 # # command for generating list of commands
188 # } elsif ($text =~ /new locations or Tcl/) {
189 # # command for detecting Tcl/Tk changes
191 # } elsif ($text =~ /mkdir -p/) {
192 # # command creating path
194 # } elsif ($text =~ /: no custom templates yet/) {
198 # print "Unhandled (line: $line): $text\n";
203 # print "Parsed build structure:\n";
204 # print Dumper(%build_structure);
207 # variables for the compilation part of each step
208 my (@defines, @incpaths, @cflags, @sources);
220 my (%dupHash, $entry);
221 %dupHash = map { $_, 1 } @defines;
222 @defines = keys %dupHash;
224 %dupHash = map { $_, 1 } @incpaths;
225 @incpaths = keys %dupHash;
227 %dupHash = map { $_, 1 } @cflags;
228 @cflags = keys %dupHash;
231 sub handleCompileLine
233 my ($line, $lineno) = @_;
234 my @parts = split(' ', $line);
236 shift(@parts); # ignore cmd
237 while (my $part = shift @parts) {
238 if ("$part" eq "-o") {
241 } elsif ("$part" eq "-c") {
242 # ignore compile flag
243 } elsif ("$part" eq "-c") {
244 } elsif ($part =~ /^.?-I/) {
245 push(@incpaths, $part);
246 } elsif ($part =~ /^.?-D/) {
247 push(@defines, $part);
248 } elsif ($part =~ /^-/) {
249 push(@cflags, $part);
250 } elsif ($part =~ /\.(c|cc|cpp)$/) {
253 die "Unhandled compiler option @ line $lineno: $part";
256 @{$compile_options{"${sourcefile}_CFLAGS"}} = @cflags;
257 @{$compile_options{"${sourcefile}_DEFINES"}} = @defines;
258 @{$compile_options{"${sourcefile}_INCPATHS"}} = @incpaths;
264 my ($line, $lineno) = @_;
265 my (@objfiles, @lflags, $libout, $part);
266 # kill cmd and rm 'prefix'
267 $line =~ s/^rm -f .* && .* rcs //;
268 my @parts = split(' ', $line);
269 while ($part = shift @parts) {
271 push(@lflags, $part);
272 } elsif ($part =~ /\.(o|obj)$/) {
273 push(@objfiles, $part);
274 } elsif ($part =~ /\.(a|lib)$/) {
278 die "Unhandled lib option @ line $lineno: $part";
281 # print "LibOut: '$libout'\nLFlags: @lflags\nOfiles: @objfiles\n";
283 foreach (@objfiles) {
285 $sourcefile =~ s/\.o/.c/;
286 push(@sources, $sourcefile);
287 push(@cflags, @{$compile_options{"${sourcefile}_CFLAGS"}});
288 push(@defines, @{$compile_options{"${sourcefile}_DEFINES"}});
289 push(@incpaths, @{$compile_options{"${sourcefile}_INCPATHS"}});
293 push(@{$build_structure{"LIBS"}}, $libout);
294 @{$build_structure{"LIBS_${libout}"}} = ("_DEFINES", "_INCLUDES", "_CFLAGS", "_SOURCES",
296 @{$build_structure{"LIBS_${libout}_DEFINES"}} = @defines;
297 @{$build_structure{"LIBS_${libout}_INCLUDES"}} = @incpaths;
298 @{$build_structure{"LIBS_${libout}_CFLAGS"}} = @cflags;
299 @{$build_structure{"LIBS_${libout}_LFLAGS"}} = @lflags;
300 @{$build_structure{"LIBS_${libout}_SOURCES"}} = @sources;
301 @{$build_structure{"LIBS_${libout}_OBJECTS"}} = @objfiles;
307 my ($line, $lineno) = @_;
308 my (@objfiles, @lflags, @libs, $appout, $part);
309 my @parts = split(' ', $line);
310 shift(@parts); # ignore cmd
311 while ($part = shift @parts) {
312 if ($part =~ /^-IGNORE/) {
313 push(@lflags, $part);
314 } elsif ($part =~ /^-[GRIMDO]/) {
316 } elsif ("$part" eq "-o") {
317 $appout = shift @parts;
318 } elsif ("$part" eq "-lz") {
319 push(@libs, "zlib.lib");
320 } elsif ("$part" eq "-lcrypto") {
321 push(@libs, "libeay32.lib");
322 } elsif ("$part" eq "-lssl") {
323 push(@libs, "ssleay32.lib");
324 } elsif ($part =~ /^-/) {
325 push(@lflags, $part);
326 } elsif ($part =~ /\.(a|lib)$/) {
327 $part =~ s/\.a$/.lib/;
329 } elsif ($part =~ /\.(o|obj)$/) {
330 push(@objfiles, $part);
332 die "Unhandled lib option @ line $lineno: $part";
335 # print "AppOut: '$appout'\nLFlags: @lflags\nLibs : @libs\nOfiles: @objfiles\n";
337 foreach (@objfiles) {
339 $sourcefile =~ s/\.o/.c/;
340 push(@sources, $sourcefile);
341 push(@cflags, @{$compile_options{"${sourcefile}_CFLAGS"}});
342 push(@defines, @{$compile_options{"${sourcefile}_DEFINES"}});
343 push(@incpaths, @{$compile_options{"${sourcefile}_INCPATHS"}});
348 push(@{$build_structure{"APPS"}}, $appout);
349 @{$build_structure{"APPS_${appout}"}} = ("_DEFINES", "_INCLUDES", "_CFLAGS", "_LFLAGS",
350 "_SOURCES", "_OBJECTS", "_LIBS");
351 @{$build_structure{"APPS_${appout}_DEFINES"}} = @defines;
352 @{$build_structure{"APPS_${appout}_INCLUDES"}} = @incpaths;
353 @{$build_structure{"APPS_${appout}_CFLAGS"}} = @cflags;
354 @{$build_structure{"APPS_${appout}_LFLAGS"}} = @lflags;
355 @{$build_structure{"APPS_${appout}_SOURCES"}} = @sources;
356 @{$build_structure{"APPS_${appout}_OBJECTS"}} = @objfiles;
357 @{$build_structure{"APPS_${appout}_LIBS"}} = @libs;