9db3d43a1eb2f7528ebd29a5c18ebe415abd45cb
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;
15use Text::ParseWords;
16
17my (%build_structure, %compile_options, @makedry);
18my $out_dir = getcwd();
19my $git_dir = $out_dir;
20$git_dir =~ s=\\=/=g;
21$git_dir = dirname($git_dir) while (!-e "$git_dir/git.c" && "$git_dir" ne "");
22die "Couldn't find Git repo" if ("$git_dir" eq "");
23
24my @gens = Generators::available();
25my $gen = "Vcproj";
26
27sub showUsage
28{
29 my $genlist = join(', ', @gens);
30 print << "EOM";
31generate usage:
32 -g <GENERATOR> --gen <GENERATOR> Specify the buildsystem generator (default: $gen)
33 Available: $genlist
34 -o <PATH> --out <PATH> Specify output directory generation (default: .)
35 -i <FILE> --in <FILE> Specify input file, instead of running GNU Make
36 -h,-? --help This help
37EOM
38 exit 0;
39}
40
41# Parse command-line options
42while (@ARGV) {
43 my $arg = shift @ARGV;
44 if ("$arg" eq "-h" || "$arg" eq "--help" || "$arg" eq "-?") {
45 showUsage();
46 exit(0);
47 } elsif("$arg" eq "--out" || "$arg" eq "-o") {
48 $out_dir = shift @ARGV;
49 } elsif("$arg" eq "--gen" || "$arg" eq "-g") {
50 $gen = shift @ARGV;
51 } elsif("$arg" eq "--in" || "$arg" eq "-i") {
52 my $infile = shift @ARGV;
53 open(F, "<$infile") || die "Couldn't open file $infile";
54 @makedry = <F>;
55 close(F);
56 }
57}
58
59# NOT using File::Spec->rel2abs($path, $base) here, as
60# it fails badly for me in the msysgit environment
61$git_dir = File::Spec->rel2abs($git_dir);
62$out_dir = File::Spec->rel2abs($out_dir);
63my $rel_dir = makeOutRel2Git($git_dir, $out_dir);
64
65# Print some information so the user feels informed
66print << "EOM";
67-----
68Generator: $gen
69Git dir: $git_dir
70Out dir: $out_dir
71-----
72Running GNU Make to figure out build structure...
73EOM
74
75# Pipe a make --dry-run into a variable, if not already loaded from file
76@makedry = `cd $git_dir && make -n MSVC=1 V=1 2>/dev/null` if !@makedry;
77
78# Parse the make output into usable info
79parseMakeOutput();
80
81# Finally, ask the generator to start generating..
82Generators::generate($gen, $git_dir, $out_dir, $rel_dir, %build_structure);
83
84# main flow ends here
85# -------------------------------------------------------------------------------------------------
86
87
88# 1) path: /foo/bar/baz 2) path: /foo/bar/baz 3) path: /foo/bar/baz
89# base: /foo/bar/baz/temp base: /foo/bar base: /tmp
90# rel: .. rel: baz rel: ../foo/bar/baz
91sub makeOutRel2Git
92{
93 my ($path, $base) = @_;
94 my $rel;
95 if ("$path" eq "$base") {
96 return ".";
97 } elsif ($base =~ /^$path/) {
98 # case 1
99 my $tmp = $base;
100 $tmp =~ s/^$path//;
101 foreach (split('/', $tmp)) {
102 $rel .= "../" if ("$_" ne "");
103 }
104 } elsif ($path =~ /^$base/) {
105 # case 2
106 $rel = $path;
107 $rel =~ s/^$base//;
108 $rel = "./$rel";
109 } else {
110 my $tmp = $base;
111 foreach (split('/', $tmp)) {
112 $rel .= "../" if ("$_" ne "");
113 }
114 $rel .= $path;
115 }
116 $rel =~ s/\/\//\//g; # simplify
117 $rel =~ s/\/$//; # don't end with /
118 return $rel;
119}
120
121sub parseMakeOutput
122{
123 print "Parsing GNU Make output to figure out build structure...\n";
124 my $line = 0;
125 while (my $text = shift @makedry) {
126 my $ate_next;
127 do {
128 $ate_next = 0;
129 $line++;
130 chomp $text;
131 chop $text if ($text =~ /\r$/);
132 if ($text =~ /\\$/) {
133 $text =~ s/\\$//;
134 $text .= shift @makedry;
135 $ate_next = 1;
136 }
137 } while($ate_next);
138
139 if ($text =~ /^test /) {
140 # options to test (eg -o) may be mistaken for linker options
141 next;
142 }
143
144 if ($text =~ /^(mkdir|msgfmt) /) {
145 # options to the Portable Object translations
146 # the line "mkdir ... && msgfmt ..." contains no linker options
147 next;
148 }
149
150 if($text =~ / -c /) {
151 # compilation
152 handleCompileLine($text, $line);
153
154 } elsif ($text =~ / -o /) {
155 # linking executable
156 handleLinkLine($text, $line);
157
158 } elsif ($text =~ /\.o / && $text =~ /\.a /) {
159 # libifying
160 handleLibLine($text, $line);
161#
162# } elsif ($text =~ /^cp /) {
163# # copy file around
164#
165# } elsif ($text =~ /^rm -f /) {
166# # shell command
167#
168# } elsif ($text =~ /^make[ \[]/) {
169# # make output
170#
171# } elsif ($text =~ /^echo /) {
172# # echo to file
173#
174# } elsif ($text =~ /^if /) {
175# # shell conditional
176#
177# } elsif ($text =~ /^tclsh /) {
178# # translation stuff
179#
180# } elsif ($text =~ /^umask /) {
181# # handling boilerplates
182#
183# } elsif ($text =~ /\$\(\:\)/) {
184# # ignore
185#
186# } elsif ($text =~ /^FLAGS=/) {
187# # flags check for dependencies
188#
189# } elsif ($text =~ /^'\/usr\/bin\/perl' -MError -e/) {
190# # perl commands for copying files
191#
192# } elsif ($text =~ /generate-cmdlist\.sh/) {
193# # command for generating list of commands
194#
195# } elsif ($text =~ /new locations or Tcl/) {
196# # command for detecting Tcl/Tk changes
197#
198# } elsif ($text =~ /mkdir -p/) {
199# # command creating path
200#
201# } elsif ($text =~ /: no custom templates yet/) {
202# # whatever
203#
204# } else {
205# print "Unhandled (line: $line): $text\n";
206 }
207 }
208
209# use Data::Dumper;
210# print "Parsed build structure:\n";
211# print Dumper(%build_structure);
212}
213
214# variables for the compilation part of each step
215my (@defines, @incpaths, @cflags, @sources);
216
217sub clearCompileStep
218{
219 @defines = ();
220 @incpaths = ();
221 @cflags = ();
222 @sources = ();
223}
224
225sub removeDuplicates
226{
227 my (%dupHash, $entry);
228 %dupHash = map { $_, 1 } @defines;
229 @defines = keys %dupHash;
230
231 %dupHash = map { $_, 1 } @incpaths;
232 @incpaths = keys %dupHash;
233
234 %dupHash = map { $_, 1 } @cflags;
235 @cflags = keys %dupHash;
236}
237
238sub handleCompileLine
239{
240 my ($line, $lineno) = @_;
241 my @parts = shellwords($line);
242 my $sourcefile;
243 shift(@parts); # ignore cmd
244 while (my $part = shift @parts) {
245 if ("$part" eq "-o") {
246 # ignore object file
247 shift @parts;
248 } elsif ("$part" eq "-c") {
249 # ignore compile flag
250 } elsif ("$part" eq "-c") {
251 } elsif ($part =~ /^.?-I/) {
252 push(@incpaths, $part);
253 } elsif ($part =~ /^.?-D/) {
254 push(@defines, $part);
255 } elsif ($part =~ /^-/) {
256 push(@cflags, $part);
257 } elsif ($part =~ /\.(c|cc|cpp)$/) {
258 $sourcefile = $part;
259 } else {
260 die "Unhandled compiler option @ line $lineno: $part";
261 }
262 }
263 @{$compile_options{"${sourcefile}_CFLAGS"}} = @cflags;
264 @{$compile_options{"${sourcefile}_DEFINES"}} = @defines;
265 @{$compile_options{"${sourcefile}_INCPATHS"}} = @incpaths;
266 clearCompileStep();
267}
268
269sub handleLibLine
270{
271 my ($line, $lineno) = @_;
272 my (@objfiles, @lflags, $libout, $part);
273 # kill cmd and rm 'prefix'
274 $line =~ s/^rm -f .* && .* rcs //;
275 my @parts = shellwords($line);
276 while ($part = shift @parts) {
277 if ($part =~ /^-/) {
278 push(@lflags, $part);
279 } elsif ($part =~ /\.(o|obj)$/) {
280 push(@objfiles, $part);
281 } elsif ($part =~ /\.(a|lib)$/) {
282 $libout = $part;
283 $libout =~ s/\.a$//;
284 } else {
285 die "Unhandled lib option @ line $lineno: $part";
286 }
287 }
288# print "LibOut: '$libout'\nLFlags: @lflags\nOfiles: @objfiles\n";
289# exit(1);
290 foreach (@objfiles) {
291 my $sourcefile = $_;
292 $sourcefile =~ s/\.o$/.c/;
293 push(@sources, $sourcefile);
294 push(@cflags, @{$compile_options{"${sourcefile}_CFLAGS"}});
295 push(@defines, @{$compile_options{"${sourcefile}_DEFINES"}});
296 push(@incpaths, @{$compile_options{"${sourcefile}_INCPATHS"}});
297 }
298 removeDuplicates();
299
300 push(@{$build_structure{"LIBS"}}, $libout);
301 @{$build_structure{"LIBS_${libout}"}} = ("_DEFINES", "_INCLUDES", "_CFLAGS", "_SOURCES",
302 "_OBJECTS");
303 @{$build_structure{"LIBS_${libout}_DEFINES"}} = @defines;
304 @{$build_structure{"LIBS_${libout}_INCLUDES"}} = @incpaths;
305 @{$build_structure{"LIBS_${libout}_CFLAGS"}} = @cflags;
306 @{$build_structure{"LIBS_${libout}_LFLAGS"}} = @lflags;
307 @{$build_structure{"LIBS_${libout}_SOURCES"}} = @sources;
308 @{$build_structure{"LIBS_${libout}_OBJECTS"}} = @objfiles;
309 clearCompileStep();
310}
311
312sub handleLinkLine
313{
314 my ($line, $lineno) = @_;
315 my (@objfiles, @lflags, @libs, $appout, $part);
316 my @parts = shellwords($line);
317 shift(@parts); # ignore cmd
318 while ($part = shift @parts) {
319 if ($part =~ /^-IGNORE/) {
320 push(@lflags, $part);
321 } elsif ($part =~ /^-[GRIMDO]/) {
322 # eat compiler flags
323 } elsif ("$part" eq "-o") {
324 $appout = shift @parts;
325 } elsif ("$part" eq "-lz") {
326 push(@libs, "zlib.lib");
327 } elsif ("$part" eq "-lcrypto") {
328 push(@libs, "libeay32.lib");
329 } elsif ("$part" eq "-lssl") {
330 push(@libs, "ssleay32.lib");
331 } elsif ($part =~ /^-/) {
332 push(@lflags, $part);
333 } elsif ($part =~ /\.(a|lib)$/) {
334 $part =~ s/\.a$/.lib/;
335 push(@libs, $part);
336 } elsif ($part eq 'invalidcontinue.obj') {
337 # ignore - known to MSVC
338 } elsif ($part =~ /\.o$/) {
339 push(@objfiles, $part);
340 } elsif ($part =~ /\.obj$/) {
341 # do nothing, 'make' should not be producing .obj, only .o files
342 } else {
343 die "Unhandled link option @ line $lineno: $part";
344 }
345 }
346# print "AppOut: '$appout'\nLFlags: @lflags\nLibs : @libs\nOfiles: @objfiles\n";
347# exit(1);
348 foreach (@objfiles) {
349 my $sourcefile = $_;
350 $sourcefile =~ s/\.o$/.c/;
351 push(@sources, $sourcefile);
352 push(@cflags, @{$compile_options{"${sourcefile}_CFLAGS"}});
353 push(@defines, @{$compile_options{"${sourcefile}_DEFINES"}});
354 push(@incpaths, @{$compile_options{"${sourcefile}_INCPATHS"}});
355 }
356 removeDuplicates();
357
358 removeDuplicates();
359 push(@{$build_structure{"APPS"}}, $appout);
360 @{$build_structure{"APPS_${appout}"}} = ("_DEFINES", "_INCLUDES", "_CFLAGS", "_LFLAGS",
361 "_SOURCES", "_OBJECTS", "_LIBS");
362 @{$build_structure{"APPS_${appout}_DEFINES"}} = @defines;
363 @{$build_structure{"APPS_${appout}_INCLUDES"}} = @incpaths;
364 @{$build_structure{"APPS_${appout}_CFLAGS"}} = @cflags;
365 @{$build_structure{"APPS_${appout}_LFLAGS"}} = @lflags;
366 @{$build_structure{"APPS_${appout}_SOURCES"}} = @sources;
367 @{$build_structure{"APPS_${appout}_OBJECTS"}} = @objfiles;
368 @{$build_structure{"APPS_${appout}_LIBS"}} = @libs;
369 clearCompileStep();
370}