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