de5c0b6b25a489937a9580df17f4704b3a33b1a6
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# Capture the make dry stderr to file for review (will be empty for a release build).
77
78my $ErrsFile = "msvc-build-makedryerrors.txt";
79@makedry = `make -C $git_dir -n MSVC=1 V=1 2>$ErrsFile` if !@makedry;
80# test for an empty Errors file and remove it
81unlink $ErrsFile if -f -z $ErrsFile;
82
83# Parse the make output into usable info
84parseMakeOutput();
85
86# Finally, ask the generator to start generating..
87Generators::generate($gen, $git_dir, $out_dir, $rel_dir, %build_structure);
88
89# main flow ends here
90# -------------------------------------------------------------------------------------------------
91
92
93# 1) path: /foo/bar/baz 2) path: /foo/bar/baz 3) path: /foo/bar/baz
94# base: /foo/bar/baz/temp base: /foo/bar base: /tmp
95# rel: .. rel: baz rel: ../foo/bar/baz
96sub makeOutRel2Git
97{
98 my ($path, $base) = @_;
99 my $rel;
100 if ("$path" eq "$base") {
101 return ".";
102 } elsif ($base =~ /^$path/) {
103 # case 1
104 my $tmp = $base;
105 $tmp =~ s/^$path//;
106 foreach (split('/', $tmp)) {
107 $rel .= "../" if ("$_" ne "");
108 }
109 } elsif ($path =~ /^$base/) {
110 # case 2
111 $rel = $path;
112 $rel =~ s/^$base//;
113 $rel = "./$rel";
114 } else {
115 my $tmp = $base;
116 foreach (split('/', $tmp)) {
117 $rel .= "../" if ("$_" ne "");
118 }
119 $rel .= $path;
120 }
121 $rel =~ s/\/\//\//g; # simplify
122 $rel =~ s/\/$//; # don't end with /
123 return $rel;
124}
125
126sub parseMakeOutput
127{
128 print "Parsing GNU Make output to figure out build structure...\n";
129 my $line = 0;
130 while (my $text = shift @makedry) {
131 my $ate_next;
132 do {
133 $ate_next = 0;
134 $line++;
135 chomp $text;
136 chop $text if ($text =~ /\r$/);
137 if ($text =~ /\\$/) {
138 $text =~ s/\\$//;
139 $text .= shift @makedry;
140 $ate_next = 1;
141 }
142 } while($ate_next);
143
144 if ($text =~ /^test /) {
145 # options to test (eg -o) may be mistaken for linker options
146 next;
147 }
148
149 if ($text =~ /^(mkdir|msgfmt) /) {
150 # options to the Portable Object translations
151 # the line "mkdir ... && msgfmt ..." contains no linker options
152 next;
153 }
154
155 if($text =~ / -c /) {
156 # compilation
157 handleCompileLine($text, $line);
158
159 } elsif ($text =~ / -o /) {
160 # linking executable
161 handleLinkLine($text, $line);
162
163 } elsif ($text =~ /\.o / && $text =~ /\.a /) {
164 # libifying
165 handleLibLine($text, $line);
166#
167# } elsif ($text =~ /^cp /) {
168# # copy file around
169#
170# } elsif ($text =~ /^rm -f /) {
171# # shell command
172#
173# } elsif ($text =~ /^make[ \[]/) {
174# # make output
175#
176# } elsif ($text =~ /^echo /) {
177# # echo to file
178#
179# } elsif ($text =~ /^if /) {
180# # shell conditional
181#
182# } elsif ($text =~ /^tclsh /) {
183# # translation stuff
184#
185# } elsif ($text =~ /^umask /) {
186# # handling boilerplates
187#
188# } elsif ($text =~ /\$\(\:\)/) {
189# # ignore
190#
191# } elsif ($text =~ /^FLAGS=/) {
192# # flags check for dependencies
193#
194# } elsif ($text =~ /^'\/usr\/bin\/perl' -MError -e/) {
195# # perl commands for copying files
196#
197# } elsif ($text =~ /generate-cmdlist\.sh/) {
198# # command for generating list of commands
199#
200# } elsif ($text =~ /new locations or Tcl/) {
201# # command for detecting Tcl/Tk changes
202#
203# } elsif ($text =~ /mkdir -p/) {
204# # command creating path
205#
206# } elsif ($text =~ /: no custom templates yet/) {
207# # whatever
208#
209# } else {
210# print "Unhandled (line: $line): $text\n";
211 }
212 }
213
214# use Data::Dumper;
215# print "Parsed build structure:\n";
216# print Dumper(%build_structure);
217}
218
219# variables for the compilation part of each step
220my (@defines, @incpaths, @cflags, @sources);
221
222sub clearCompileStep
223{
224 @defines = ();
225 @incpaths = ();
226 @cflags = ();
227 @sources = ();
228}
229
230sub removeDuplicates
231{
232 my (%dupHash, $entry);
233 %dupHash = map { $_, 1 } @defines;
234 @defines = keys %dupHash;
235
236 %dupHash = map { $_, 1 } @incpaths;
237 @incpaths = keys %dupHash;
238
239 %dupHash = map { $_, 1 } @cflags;
240 @cflags = keys %dupHash;
241}
242
243sub handleCompileLine
244{
245 my ($line, $lineno) = @_;
246 my @parts = shellwords($line);
247 my $sourcefile;
248 shift(@parts); # ignore cmd
249 while (my $part = shift @parts) {
250 if ("$part" eq "-o") {
251 # ignore object file
252 shift @parts;
253 } elsif ("$part" eq "-c") {
254 # ignore compile flag
255 } elsif ("$part" eq "-c") {
256 } elsif ($part =~ /^.?-I/) {
257 push(@incpaths, $part);
258 } elsif ($part =~ /^.?-D/) {
259 push(@defines, $part);
260 } elsif ($part =~ /^-/) {
261 push(@cflags, $part);
262 } elsif ($part =~ /\.(c|cc|cpp)$/) {
263 $sourcefile = $part;
264 } else {
265 die "Unhandled compiler option @ line $lineno: $part";
266 }
267 }
268 @{$compile_options{"${sourcefile}_CFLAGS"}} = @cflags;
269 @{$compile_options{"${sourcefile}_DEFINES"}} = @defines;
270 @{$compile_options{"${sourcefile}_INCPATHS"}} = @incpaths;
271 clearCompileStep();
272}
273
274sub handleLibLine
275{
276 my ($line, $lineno) = @_;
277 my (@objfiles, @lflags, $libout, $part);
278 # kill cmd and rm 'prefix'
279 $line =~ s/^rm -f .* && .* rcs //;
280 my @parts = shellwords($line);
281 while ($part = shift @parts) {
282 if ($part =~ /^-/) {
283 push(@lflags, $part);
284 } elsif ($part =~ /\.(o|obj)$/) {
285 push(@objfiles, $part);
286 } elsif ($part =~ /\.(a|lib)$/) {
287 $libout = $part;
288 $libout =~ s/\.a$//;
289 } else {
290 die "Unhandled lib option @ line $lineno: $part";
291 }
292 }
293# print "LibOut: '$libout'\nLFlags: @lflags\nOfiles: @objfiles\n";
294# exit(1);
295 foreach (@objfiles) {
296 my $sourcefile = $_;
297 $sourcefile =~ s/\.o$/.c/;
298 push(@sources, $sourcefile);
299 push(@cflags, @{$compile_options{"${sourcefile}_CFLAGS"}});
300 push(@defines, @{$compile_options{"${sourcefile}_DEFINES"}});
301 push(@incpaths, @{$compile_options{"${sourcefile}_INCPATHS"}});
302 }
303 removeDuplicates();
304
305 push(@{$build_structure{"LIBS"}}, $libout);
306 @{$build_structure{"LIBS_${libout}"}} = ("_DEFINES", "_INCLUDES", "_CFLAGS", "_SOURCES",
307 "_OBJECTS");
308 @{$build_structure{"LIBS_${libout}_DEFINES"}} = @defines;
309 @{$build_structure{"LIBS_${libout}_INCLUDES"}} = @incpaths;
310 @{$build_structure{"LIBS_${libout}_CFLAGS"}} = @cflags;
311 @{$build_structure{"LIBS_${libout}_LFLAGS"}} = @lflags;
312 @{$build_structure{"LIBS_${libout}_SOURCES"}} = @sources;
313 @{$build_structure{"LIBS_${libout}_OBJECTS"}} = @objfiles;
314 clearCompileStep();
315}
316
317sub handleLinkLine
318{
319 my ($line, $lineno) = @_;
320 my (@objfiles, @lflags, @libs, $appout, $part);
321 my @parts = shellwords($line);
322 shift(@parts); # ignore cmd
323 while ($part = shift @parts) {
324 if ($part =~ /^-IGNORE/) {
325 push(@lflags, $part);
326 } elsif ($part =~ /^-[GRIMDO]/) {
327 # eat compiler flags
328 } elsif ("$part" eq "-o") {
329 $appout = shift @parts;
330 } elsif ("$part" eq "-lz") {
331 push(@libs, "zlib.lib");
332 } elsif ("$part" eq "-lcrypto") {
333 push(@libs, "libeay32.lib");
334 } elsif ("$part" eq "-lssl") {
335 push(@libs, "ssleay32.lib");
336 } elsif ($part =~ /^-/) {
337 push(@lflags, $part);
338 } elsif ($part =~ /\.(a|lib)$/) {
339 $part =~ s/\.a$/.lib/;
340 push(@libs, $part);
341 } elsif ($part eq 'invalidcontinue.obj') {
342 # ignore - known to MSVC
343 } elsif ($part =~ /\.o$/) {
344 push(@objfiles, $part);
345 } elsif ($part =~ /\.obj$/) {
346 # do nothing, 'make' should not be producing .obj, only .o files
347 } else {
348 die "Unhandled link option @ line $lineno: $part";
349 }
350 }
351# print "AppOut: '$appout'\nLFlags: @lflags\nLibs : @libs\nOfiles: @objfiles\n";
352# exit(1);
353 foreach (@objfiles) {
354 my $sourcefile = $_;
355 $sourcefile =~ s/\.o$/.c/;
356 push(@sources, $sourcefile);
357 push(@cflags, @{$compile_options{"${sourcefile}_CFLAGS"}});
358 push(@defines, @{$compile_options{"${sourcefile}_DEFINES"}});
359 push(@incpaths, @{$compile_options{"${sourcefile}_INCPATHS"}});
360 }
361 removeDuplicates();
362
363 removeDuplicates();
364 push(@{$build_structure{"APPS"}}, $appout);
365 @{$build_structure{"APPS_${appout}"}} = ("_DEFINES", "_INCLUDES", "_CFLAGS", "_LFLAGS",
366 "_SOURCES", "_OBJECTS", "_LIBS");
367 @{$build_structure{"APPS_${appout}_DEFINES"}} = @defines;
368 @{$build_structure{"APPS_${appout}_INCLUDES"}} = @incpaths;
369 @{$build_structure{"APPS_${appout}_CFLAGS"}} = @cflags;
370 @{$build_structure{"APPS_${appout}_LFLAGS"}} = @lflags;
371 @{$build_structure{"APPS_${appout}_SOURCES"}} = @sources;
372 @{$build_structure{"APPS_${appout}_OBJECTS"}} = @objfiles;
373 @{$build_structure{"APPS_${appout}_LIBS"}} = @libs;
374 clearCompileStep();
375}