ambuild2/frontend/prep.py
author David Anderson <dvander@alliedmods.net>
Fri Feb 07 22:54:58 2014 -0800 (2014-02-07)
changeset 236 aa23c47cb040
parent 228 a06e21c3278a
permissions -rw-r--r--
Add an originalCwd property to builders (bug 6037, r=ds).
     1 # vim: set ts=8 sts=2 sw=2 tw=99 et:
     2 #
     3 # This file is part of AMBuild.
     4 # 
     5 # AMBuild is free software: you can Headeristribute it and/or modify
     6 # it under the terms of the GNU General Public License as published by
     7 # the Free Software Foundation, either version 3 of the License, or
     8 # (at your option) any later version.
     9 # 
    10 # AMBuild is distributed in the hope that it will be useful,
    11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
    12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13 # GNU General Public License for more details.
    14 # 
    15 # You should have received a copy of the GNU General Public License
    16 # along with AMBuild. If not, see <http://www.gnu.org/licenses/>.
    17 import os, sys
    18 import platform
    19 from ambuild2 import util
    20 from optparse import OptionParser
    21 from optparse import Values
    22 
    23 class Preparer(object):
    24   def __init__(self, sourcePath, buildPath):
    25     self.sourcePath = sourcePath
    26     self.buildPath = buildPath
    27     self.host_platform = util.Platform()
    28     self.target_platform = util.Platform()
    29 
    30     self.options = OptionParser("usage: %prog [options]")
    31     self.options.add_option("-g", "--gen", type="string", dest="generator", default="ambuild2",
    32                             help="Build system generator to use. See --list-gen")
    33     self.options.add_option("--list-gen", action="store_true", dest="list_gen", default=False,
    34                             help="List available build system generators, then exit.")
    35     self.options.add_option("--make-scripts", action="store_true", dest="make_scripts", default=False,
    36                             help="Generate extra command-line files for building (build.py, Makefile).")
    37     self.options.add_option("--no-color", action="store_true", dest="no_color", default=False,
    38                             help="Disable color output in the terminal.")
    39 
    40   @staticmethod
    41   def default_build_folder(prep):
    42     return 'obj-' + util.Platform() + '-' + platform.machine()
    43 
    44   def Configure(self): 
    45     v_options, args = self.options.parse_args()
    46 
    47     # In order to support pickling, we need to rewrite |options| to not use
    48     # optparse.Values, since its implementation changes across Python versions.
    49     options = util.Expando()
    50     ignore_attrs = set(dir(Values))
    51     for attr in dir(v_options):
    52       if attr in ignore_attrs:
    53         continue
    54       setattr(options, attr, getattr(v_options, attr))
    55 
    56     if options.list_gen:
    57       print('Available build system generators:')
    58       print('  {0:24} - AMBuild 2 (default)'.format('ambuild2'))
    59       sys.exit(0)
    60 
    61     if options.no_color:
    62       util.DisableConsoleColors()
    63 
    64     source_abspath = os.path.normpath(os.path.abspath(self.sourcePath))
    65     build_abspath = os.path.normpath(os.path.abspath(self.buildPath))
    66     if source_abspath == build_abspath:
    67       if util.IsString(self.default_build_folder):
    68         objfolder = self.default_build_folder
    69       else:
    70         objfolder = self.default_build_folder(self)
    71       new_buildpath = os.path.join(self.buildPath, objfolder)
    72 
    73       util.con_err(
    74         util.ConsoleHeader,
    75         'Warning: build is being configured in the source tree.',
    76         util.ConsoleNormal
    77       )
    78       if os.path.exists(os.path.join(new_buildpath)):
    79         has_amb2 = os.path.exists(os.path.join(new_buildpath, '.ambuild2'))
    80         if not has_amb2 and len(os.listdir(new_buildpath)):
    81           util.con_err(util.ConsoleRed, 'Tried to use ',
    82                        util.ConsoleBlue, objfolder,
    83                        util.ConsoleRed, ' as a build folder, but it is not empty!',
    84                        util.ConsoleNormal)
    85           raise Exception('build folder has unrecognized files')
    86 
    87         util.con_err(util.ConsoleHeader, 'Re-using build folder: ',
    88                      util.ConsoleBlue, '{0}'.format(objfolder),
    89                      util.ConsoleNormal)
    90       else:
    91         util.con_err(util.ConsoleHeader, 'Creating "',
    92                      util.ConsoleBlue, '{0}'.format(objfolder),
    93                      util.ConsoleHeader, '" as a build folder.',
    94                      util.ConsoleNormal)
    95         os.mkdir(new_buildpath)
    96       self.buildPath = new_buildpath
    97 
    98     if options.generator == 'ambuild2':
    99       from ambuild2.frontend.amb2 import gen
   100       builder = gen.Generator(self.sourcePath, self.buildPath, os.getcwd(), options, args)
   101     else:
   102       sys.stderr.write('Unrecognized build generator: ' + options.generator + '\n')
   103       sys.exit(1)
   104 
   105     with util.FolderChanger(self.buildPath):
   106       if not builder.generate():
   107         sys.stderr.write('Configure failed.\n')
   108         sys.exit(1)