THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

395 lines
12KB

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