1#!/usr/bin/perl -w
2######################################################################
3# Do not call this script directly!
4#
5# The generate script ensures that @INC is correct before the engine
6# is executed.
7#
8# Copyright (C) 2009 Marius Storm-Olsen <mstormo@gmail.com>
9######################################################################
10use strict;
11use File::Basename;
12use File::Spec;
13use Cwd;
14use Generators;
15
16my (%build_structure, %compile_options, @makedry);
17my $out_dir = getcwd();
18my $git_dir = $out_dir;
19$git_dir =~ s=\\=/=g;
20$git_dir = dirname($git_dir) while (!-e "$git_dir/git.c" && "$git_dir" ne "");
21die "Couldn't find Git repo" if ("$git_dir" eq "");
22
23my @gens = Generators::available();
24my $gen = "Vcproj";
25
26sub showUsage
27{
28 my $genlist = join(', ', @gens);
29 print << "EOM";
30generate usage:
31 -g <GENERATOR> --gen <GENERATOR> Specify the buildsystem generator (default: $gen)
32 Available: $genlist
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
36EOM
37 exit 0;
38}
39
40# Parse command-line options
41while (@ARGV) {
42 my $arg = shift @ARGV;
43 if ("$arg" eq "-h" || "$arg" eq "--help" || "$arg" eq "-?") {
44 showUsage();
45 exit(0);
46 } elsif("$arg" eq "--out" || "$arg" eq "-o") {
47 $out_dir = shift @ARGV;
48 } elsif("$arg" eq "--gen" || "$arg" eq "-g") {
49 $gen = shift @ARGV;
50 } elsif("$arg" eq "--in" || "$arg" eq "-i") {
51 my $infile = shift @ARGV;
52 open(F, "<$infile") || die "Couldn't open file $infile";
53 @makedry = <F>;
54 close(F);
55 }
56}
57
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);
62my $rel_dir = makeOutRel2Git($git_dir, $out_dir);
63
64# Print some information so the user feels informed
65print << "EOM";
66-----
67Generator: $gen
68Git dir: $git_dir
69Out dir: $out_dir
70-----
71Running GNU Make to figure out build structure...
72EOM
73
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;
76
77# Parse the make output into usable info
78parseMakeOutput();
79
80# Finally, ask the generator to start generating..
81Generators::generate($gen, $git_dir, $out_dir, $rel_dir, %build_structure);
82
83# main flow ends here
84# -------------------------------------------------------------------------------------------------
85
86
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
90sub makeOutRel2Git
91{
92 my ($path, $base) = @_;
93 my $rel;
94 if ("$path" eq "$base") {
95 return ".";
96 } elsif ($base =~ /^$path/) {
97 # case 1
98 my $tmp = $base;
99 $tmp =~ s/^$path//;
100 foreach (split('/', $tmp)) {
101 $rel .= "../" if ("$_" ne "");
102 }
103 } elsif ($path =~ /^$base/) {
104 # case 2
105 $rel = $path;
106 $rel =~ s/^$base//;
107 $rel = "./$rel";
108 } else {
109 my $tmp = $base;
110 foreach (split('/', $tmp)) {
111 $rel .= "../" if ("$_" ne "");
112 }
113 $rel .= $path;
114 }
115 $rel =~ s/\/\//\//g; # simplify
116 $rel =~ s/\/$//; # don't end with /
117 return $rel;
118}
119
120sub parseMakeOutput
121{
122 print "Parsing GNU Make output to figure out build structure...\n";
123 my $line = 0;
124 while (my $text = shift @makedry) {
125 my $ate_next;
126 do {
127 $ate_next = 0;
128 $line++;
129 chomp $text;
130 chop $text if ($text =~ /\r$/);
131 if ($text =~ /\\$/) {
132 $text =~ s/\\$//;
133 $text .= shift @makedry;
134 $ate_next = 1;
135 }
136 } while($ate_next);
137
138 if($text =~ / -c /) {
139 # compilation
140 handleCompileLine($text, $line);
141
142 } elsif ($text =~ / -o /) {
143 # linking executable
144 handleLinkLine($text, $line);
145
146 } elsif ($text =~ /\.o / && $text =~ /\.a /) {
147 # libifying
148 handleLibLine($text, $line);
149#
150# } elsif ($text =~ /^cp /) {
151# # copy file around
152#
153# } elsif ($text =~ /^rm -f /) {
154# # shell command
155#
156# } elsif ($text =~ /^make[ \[]/) {
157# # make output
158#
159# } elsif ($text =~ /^echo /) {
160# # echo to file
161#
162# } elsif ($text =~ /^if /) {
163# # shell conditional
164#
165# } elsif ($text =~ /^tclsh /) {
166# # translation stuff
167#
168# } elsif ($text =~ /^umask /) {
169# # handling boilerplates
170#
171# } elsif ($text =~ /\$\(\:\)/) {
172# # ignore
173#
174# } elsif ($text =~ /^FLAGS=/) {
175# # flags check for dependencies
176#
177# } elsif ($text =~ /^'\/usr\/bin\/perl' -MError -e/) {
178# # perl commands for copying files
179#
180# } elsif ($text =~ /generate-cmdlist\.sh/) {
181# # command for generating list of commands
182#
183# } elsif ($text =~ /^test / && $text =~ /|| rm -f /) {
184# # commands removing executables, if they exist
185#
186# } elsif ($text =~ /new locations or Tcl/) {
187# # command for detecting Tcl/Tk changes
188#
189# } elsif ($text =~ /mkdir -p/) {
190# # command creating path
191#
192# } elsif ($text =~ /: no custom templates yet/) {
193# # whatever
194#
195# } else {
196# print "Unhandled (line: $line): $text\n";
197 }
198 }
199
200# use Data::Dumper;
201# print "Parsed build structure:\n";
202# print Dumper(%build_structure);
203}
204
205# variables for the compilation part of each step
206my (@defines, @incpaths, @cflags, @sources);
207
208sub clearCompileStep
209{
210 @defines = ();
211 @incpaths = ();
212 @cflags = ();
213 @sources = ();
214}
215
216sub removeDuplicates
217{
218 my (%dupHash, $entry);
219 %dupHash = map { $_, 1 } @defines;
220 @defines = keys %dupHash;
221
222 %dupHash = map { $_, 1 } @incpaths;
223 @incpaths = keys %dupHash;
224
225 %dupHash = map { $_, 1 } @cflags;
226 @cflags = keys %dupHash;
227}
228
229sub handleCompileLine
230{
231 my ($line, $lineno) = @_;
232 my @parts = split(' ', $line);
233 my $sourcefile;
234 shift(@parts); # ignore cmd
235 while (my $part = shift @parts) {
236 if ("$part" eq "-o") {
237 # ignore object file
238 shift @parts;
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)$/) {
249 $sourcefile = $part;
250 } else {
251 die "Unhandled compiler option @ line $lineno: $part";
252 }
253 }
254 @{$compile_options{"${sourcefile}_CFLAGS"}} = @cflags;
255 @{$compile_options{"${sourcefile}_DEFINES"}} = @defines;
256 @{$compile_options{"${sourcefile}_INCPATHS"}} = @incpaths;
257 clearCompileStep();
258}
259
260sub handleLibLine
261{
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) {
268 if ($part =~ /^-/) {
269 push(@lflags, $part);
270 } elsif ($part =~ /\.(o|obj)$/) {
271 push(@objfiles, $part);
272 } elsif ($part =~ /\.(a|lib)$/) {
273 $libout = $part;
274 $libout =~ s/\.a$//;
275 } else {
276 die "Unhandled lib option @ line $lineno: $part";
277 }
278 }
279# print "LibOut: '$libout'\nLFlags: @lflags\nOfiles: @objfiles\n";
280# exit(1);
281 foreach (@objfiles) {
282 my $sourcefile = $_;
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"}});
288 }
289 removeDuplicates();
290
291 push(@{$build_structure{"LIBS"}}, $libout);
292 @{$build_structure{"LIBS_${libout}"}} = ("_DEFINES", "_INCLUDES", "_CFLAGS", "_SOURCES",
293 "_OBJECTS");
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;
300 clearCompileStep();
301}
302
303sub handleLinkLine
304{
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]/) {
313 # eat compiler flags
314 } elsif ("$part" eq "-o") {
315 $appout = shift @parts;
316 } elsif ("$part" eq "-lz") {
317 push(@libs, "zlib.lib");
318 } elsif ($part =~ /^-/) {
319 push(@lflags, $part);
320 } elsif ($part =~ /\.(a|lib)$/) {
321 $part =~ s/\.a$/.lib/;
322 push(@libs, $part);
323 } elsif ($part =~ /\.(o|obj)$/) {
324 push(@objfiles, $part);
325 } else {
326 die "Unhandled lib option @ line $lineno: $part";
327 }
328 }
329# print "AppOut: '$appout'\nLFlags: @lflags\nLibs : @libs\nOfiles: @objfiles\n";
330# exit(1);
331 foreach (@objfiles) {
332 my $sourcefile = $_;
333 $sourcefile =~ s/\.o/.c/;
334 push(@sources, $sourcefile);
335 push(@cflags, @{$compile_options{"${sourcefile}_CFLAGS"}});
336 push(@defines, @{$compile_options{"${sourcefile}_DEFINES"}});
337 push(@incpaths, @{$compile_options{"${sourcefile}_INCPATHS"}});
338 }
339 removeDuplicates();
340
341 removeDuplicates();
342 push(@{$build_structure{"APPS"}}, $appout);
343 @{$build_structure{"APPS_${appout}"}} = ("_DEFINES", "_INCLUDES", "_CFLAGS", "_LFLAGS",
344 "_SOURCES", "_OBJECTS", "_LIBS");
345 @{$build_structure{"APPS_${appout}_DEFINES"}} = @defines;
346 @{$build_structure{"APPS_${appout}_INCLUDES"}} = @incpaths;
347 @{$build_structure{"APPS_${appout}_CFLAGS"}} = @cflags;
348 @{$build_structure{"APPS_${appout}_LFLAGS"}} = @lflags;
349 @{$build_structure{"APPS_${appout}_SOURCES"}} = @sources;
350 @{$build_structure{"APPS_${appout}_OBJECTS"}} = @objfiles;
351 @{$build_structure{"APPS_${appout}_LIBS"}} = @libs;
352 clearCompileStep();
353}