1 # vim: set ts=8 sts=2 sw=2 tw=99 et:
3 # This file is part of AMBuild.
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.
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.
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/>.
19 from ambuild2 import util
20 from optparse import OptionParser
21 from optparse import Values
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()
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.")
41 def default_build_folder(prep):
42 return 'obj-' + util.Platform() + '-' + platform.machine()
45 v_options, args = self.options.parse_args()
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:
54 setattr(options, attr, getattr(v_options, attr))
57 print('Available build system generators:')
58 print(' {0:24} - AMBuild 2 (default)'.format('ambuild2'))
62 util.DisableConsoleColors()
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
70 objfolder = self.default_build_folder(self)
71 new_buildpath = os.path.join(self.buildPath, objfolder)
75 'Warning: build is being configured in the source tree.',
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!',
85 raise Exception('build folder has unrecognized files')
87 util.con_err(util.ConsoleHeader, 'Re-using build folder: ',
88 util.ConsoleBlue, '{0}'.format(objfolder),
91 util.con_err(util.ConsoleHeader, 'Creating "',
92 util.ConsoleBlue, '{0}'.format(objfolder),
93 util.ConsoleHeader, '" as a build folder.',
95 os.mkdir(new_buildpath)
96 self.buildPath = new_buildpath
98 if options.generator == 'ambuild2':
99 from ambuild2.frontend.amb2 import gen
100 builder = gen.Generator(self.sourcePath, self.buildPath, os.getcwd(), options, args)
102 sys.stderr.write('Unrecognized build generator: ' + options.generator + '\n')
105 with util.FolderChanger(self.buildPath):
106 if not builder.generate():
107 sys.stderr.write('Configure failed.\n')