contrib / buildsystems / engine.plon commit contrib/buildsystems: handle quoted spaces in filenames (d8c0701)
   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 =~ / -c /) {
 145            # compilation
 146            handleCompileLine($text, $line);
 147
 148        } elsif ($text =~ / -o /) {
 149            # linking executable
 150            handleLinkLine($text, $line);
 151
 152        } elsif ($text =~ /\.o / && $text =~ /\.a /) {
 153            # libifying
 154            handleLibLine($text, $line);
 155#
 156#        } elsif ($text =~ /^cp /) {
 157#            # copy file around
 158#
 159#        } elsif ($text =~ /^rm -f /) {
 160#            # shell command
 161#
 162#        } elsif ($text =~ /^make[ \[]/) {
 163#            # make output
 164#
 165#        } elsif ($text =~ /^echo /) {
 166#            # echo to file
 167#
 168#        } elsif ($text =~ /^if /) {
 169#            # shell conditional
 170#
 171#        } elsif ($text =~ /^tclsh /) {
 172#            # translation stuff
 173#
 174#        } elsif ($text =~ /^umask /) {
 175#            # handling boilerplates
 176#
 177#        } elsif ($text =~ /\$\(\:\)/) {
 178#            # ignore
 179#
 180#        } elsif ($text =~ /^FLAGS=/) {
 181#            # flags check for dependencies
 182#
 183#        } elsif ($text =~ /^'\/usr\/bin\/perl' -MError -e/) {
 184#            # perl commands for copying files
 185#
 186#        } elsif ($text =~ /generate-cmdlist\.sh/) {
 187#            # command for generating list of commands
 188#
 189#        } elsif ($text =~ /new locations or Tcl/) {
 190#            # command for detecting Tcl/Tk changes
 191#
 192#        } elsif ($text =~ /mkdir -p/) {
 193#            # command creating path
 194#
 195#        } elsif ($text =~ /: no custom templates yet/) {
 196#            # whatever
 197#
 198#        } else {
 199#            print "Unhandled (line: $line): $text\n";
 200        }
 201    }
 202
 203#    use Data::Dumper;
 204#    print "Parsed build structure:\n";
 205#    print Dumper(%build_structure);
 206}
 207
 208# variables for the compilation part of each step
 209my (@defines, @incpaths, @cflags, @sources);
 210
 211sub clearCompileStep
 212{
 213    @defines = ();
 214    @incpaths = ();
 215    @cflags = ();
 216    @sources = ();
 217}
 218
 219sub removeDuplicates
 220{
 221    my (%dupHash, $entry);
 222    %dupHash = map { $_, 1 } @defines;
 223    @defines = keys %dupHash;
 224
 225    %dupHash = map { $_, 1 } @incpaths;
 226    @incpaths = keys %dupHash;
 227
 228    %dupHash = map { $_, 1 } @cflags;
 229    @cflags = keys %dupHash;
 230}
 231
 232sub handleCompileLine
 233{
 234    my ($line, $lineno) = @_;
 235    my @parts = shellwords($line);
 236    my $sourcefile;
 237    shift(@parts); # ignore cmd
 238    while (my $part = shift @parts) {
 239        if ("$part" eq "-o") {
 240            # ignore object file
 241            shift @parts;
 242        } elsif ("$part" eq "-c") {
 243            # ignore compile flag
 244        } elsif ("$part" eq "-c") {
 245        } elsif ($part =~ /^.?-I/) {
 246            push(@incpaths, $part);
 247        } elsif ($part =~ /^.?-D/) {
 248            push(@defines, $part);
 249        } elsif ($part =~ /^-/) {
 250            push(@cflags, $part);
 251        } elsif ($part =~ /\.(c|cc|cpp)$/) {
 252            $sourcefile = $part;
 253        } else {
 254            die "Unhandled compiler option @ line $lineno: $part";
 255        }
 256    }
 257    @{$compile_options{"${sourcefile}_CFLAGS"}} = @cflags;
 258    @{$compile_options{"${sourcefile}_DEFINES"}} = @defines;
 259    @{$compile_options{"${sourcefile}_INCPATHS"}} = @incpaths;
 260    clearCompileStep();
 261}
 262
 263sub handleLibLine
 264{
 265    my ($line, $lineno) = @_;
 266    my (@objfiles, @lflags, $libout, $part);
 267    # kill cmd and rm 'prefix'
 268    $line =~ s/^rm -f .* && .* rcs //;
 269    my @parts = shellwords($line);
 270    while ($part = shift @parts) {
 271        if ($part =~ /^-/) {
 272            push(@lflags, $part);
 273        } elsif ($part =~ /\.(o|obj)$/) {
 274            push(@objfiles, $part);
 275        } elsif ($part =~ /\.(a|lib)$/) {
 276            $libout = $part;
 277            $libout =~ s/\.a$//;
 278        } else {
 279            die "Unhandled lib option @ line $lineno: $part";
 280        }
 281    }
 282#    print "LibOut: '$libout'\nLFlags: @lflags\nOfiles: @objfiles\n";
 283#    exit(1);
 284    foreach (@objfiles) {
 285        my $sourcefile = $_;
 286        $sourcefile =~ s/\.o$/.c/;
 287        push(@sources, $sourcefile);
 288        push(@cflags, @{$compile_options{"${sourcefile}_CFLAGS"}});
 289        push(@defines, @{$compile_options{"${sourcefile}_DEFINES"}});
 290        push(@incpaths, @{$compile_options{"${sourcefile}_INCPATHS"}});
 291    }
 292    removeDuplicates();
 293
 294    push(@{$build_structure{"LIBS"}}, $libout);
 295    @{$build_structure{"LIBS_${libout}"}} = ("_DEFINES", "_INCLUDES", "_CFLAGS", "_SOURCES",
 296                                             "_OBJECTS");
 297    @{$build_structure{"LIBS_${libout}_DEFINES"}} = @defines;
 298    @{$build_structure{"LIBS_${libout}_INCLUDES"}} = @incpaths;
 299    @{$build_structure{"LIBS_${libout}_CFLAGS"}} = @cflags;
 300    @{$build_structure{"LIBS_${libout}_LFLAGS"}} = @lflags;
 301    @{$build_structure{"LIBS_${libout}_SOURCES"}} = @sources;
 302    @{$build_structure{"LIBS_${libout}_OBJECTS"}} = @objfiles;
 303    clearCompileStep();
 304}
 305
 306sub handleLinkLine
 307{
 308    my ($line, $lineno) = @_;
 309    my (@objfiles, @lflags, @libs, $appout, $part);
 310    my @parts = shellwords($line);
 311    shift(@parts); # ignore cmd
 312    while ($part = shift @parts) {
 313        if ($part =~ /^-IGNORE/) {
 314            push(@lflags, $part);
 315        } elsif ($part =~ /^-[GRIMDO]/) {
 316            # eat compiler flags
 317        } elsif ("$part" eq "-o") {
 318            $appout = shift @parts;
 319        } elsif ("$part" eq "-lz") {
 320            push(@libs, "zlib.lib");
 321        } elsif ("$part" eq "-lcrypto") {
 322            push(@libs, "libeay32.lib");
 323        } elsif ("$part" eq "-lssl") {
 324            push(@libs, "ssleay32.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
 352    removeDuplicates();
 353    push(@{$build_structure{"APPS"}}, $appout);
 354    @{$build_structure{"APPS_${appout}"}} = ("_DEFINES", "_INCLUDES", "_CFLAGS", "_LFLAGS",
 355                                             "_SOURCES", "_OBJECTS", "_LIBS");
 356    @{$build_structure{"APPS_${appout}_DEFINES"}} = @defines;
 357    @{$build_structure{"APPS_${appout}_INCLUDES"}} = @incpaths;
 358    @{$build_structure{"APPS_${appout}_CFLAGS"}} = @cflags;
 359    @{$build_structure{"APPS_${appout}_LFLAGS"}} = @lflags;
 360    @{$build_structure{"APPS_${appout}_SOURCES"}} = @sources;
 361    @{$build_structure{"APPS_${appout}_OBJECTS"}} = @objfiles;
 362    @{$build_structure{"APPS_${appout}_LIBS"}} = @libs;
 363    clearCompileStep();
 364}