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 redistribute 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 ambuild2.frontend import cpp
22 # AMBuild 2 scripts are parsed recursively. Each script is supplied with a
23 # "builder" object, which maps to a Context object. Each script gets its own
24 # context. The context describes the parent build file generator, the local
25 # input and output folders, and the global compiler that was detected in the
26 # root script (if any).
28 # Contexts form a tree that matches the build script hierarchy. This can be
29 # utilized by backends for minimal reparsing and DAG updates when build
32 class ConfigureException(Exception):
33 def __init__(self, *args, **kwargs):
34 super(ConfigureException, self).__init__(*args, **kwargs)
36 class Context(object):
37 def __init__(self, generator, parent, script):
38 self.generator = generator
44 self.compiler = parent.compiler
46 # By default, all generated files for a build script are placed in a path
47 # matching its layout in the source tree.
48 path, name = os.path.split(script)
50 self.currentSourcePath = os.path.join(parent.currentSourcePath, path)
51 self.currentSourceFolder = os.path.join(parent.currentSourceFolder, path)
52 self.buildFolder = os.path.join(parent.buildFolder, path)
54 self.currentSourcePath = generator.sourcePath
55 self.currentSourceFolder = ''
57 self.buildScript = os.path.join(self.currentSourceFolder, name)
58 self.localFolder_ = self.buildFolder
63 return self.generator.sourcePath
67 return self.generator.options
71 return self.generator.buildPath
73 # In build systems with dependency graphs, this can return a node
74 # representing buildFolder. Otherwise, it returns buildFolder.
76 def localFolder(self):
77 return self.generator.getLocalFolder(self)
80 def target_platform(self):
81 return self.generator.target_platform
84 def host_platform(self):
85 return self.generator.host_platform
87 def SetBuildFolder(self, folder):
88 if folder == '/' or folder == '.' or folder == './':
91 self.buildFolder = os.path.normpath(folder)
92 self.localFolder_ = self.buildFolder
94 def DetectCompilers(self):
96 self.compiler = self.generator.DetectCompilers()
99 def RunScript(self, file, vars={}):
100 return self.generator.evalScript(file, vars)
102 def RunBuildScripts(self, files, vars={}):
103 if util.IsString(files):
104 self.generator.evalScript(files, vars)
107 self.generator.evalScript(script, vars)
109 def Add(self, taskbuilder):
110 taskbuilder.finish(self)
111 return taskbuilder.generate(self.generator, self)
113 def AddSource(self, source_path):
114 return self.generator.addSource(self, source_path)
116 def AddSymlink(self, source, output_path):
117 return self.generator.addSymlink(self, source, output_path)
119 def AddFolder(self, folder):
120 return self.generator.addFolder(self, folder)
122 def AddCopy(self, source, output_path):
123 return self.generator.addCopy(self, source, output_path)
125 def AddCommand(self, inputs, argv, outputs, folder=-1, dep_type=None, weak_inputs=[],
127 return self.generator.addShellCommand(
134 weak_inputs = weak_inputs,
135 shared_outputs = shared_outputs
138 def AddConfigureFile(self, path):
139 return self.generator.addConfigureFile(self, path)
141 class Generator(object):
142 def __init__(self, sourcePath, buildPath, options, args):
143 self.sourcePath = sourcePath
144 self.buildPath = os.path.normpath(buildPath)
145 self.options = options
148 self.contextStack_ = [None]
149 self.configure_failed = False
151 # This is a hack... if we ever do cross-compiling or something, we'll have
153 self.host_platform = util.Platform()
154 self.target_platform = util.Platform()
156 def parseBuildScripts(self):
157 root = os.path.join(self.sourcePath, 'AMBuildScript')
158 self.evalScript(root)
160 def pushContext(self, cx):
161 self.contextStack_.append(cx)
163 def popContext(self):
164 self.contextStack_.pop()
166 def evalScript(self, file, vars={}):
167 cx = Context(self, self.contextStack_[-1], file)
170 full_path = os.path.join(self.sourcePath, cx.buildScript)
172 self.addConfigureFile(cx, full_path)
174 new_vars = copy.copy(vars)
175 new_vars['builder'] = cx
179 with open(full_path) as fp:
182 # Python 2.6 can't compile() with Windows line endings?!?!!?
183 chars = chars.replace('\r\n', '\n')
184 chars = chars.replace('\r', '\n')
186 code = compile(chars, full_path, 'exec')
189 if 'rvalue' in new_vars:
190 rvalue = new_vars['rvalue']
191 del new_vars['rvalue']
196 def generateBuildFiles(self):
197 build_py = os.path.join(self.buildPath, 'build.py')
198 with open(build_py, 'w') as fp:
201 # vim set: ts=8 sts=2 sw=2 tw=99 et:
203 from ambuild2 import run
205 if not run.CompatBuild(r"{build}"):
207 """.format(exe=sys.executable, build=self.buildPath))
209 with open(os.path.join(self.buildPath, 'Makefile'), 'w') as fp:
213 """.format(exe=sys.executable, py=build_py))
217 self.parseBuildScripts()
219 if self.options.make_scripts:
220 self.generateBuildFiles()
223 def DetectCompilers(self):
227 with util.FolderChanger('.ambuild2'):
228 cc = cpp.DetectCompiler(self, os.environ, 'CC')
229 cxx = cpp.DetectCompiler(self, os.environ, 'CXX')
230 self.compiler = cpp.Compiler(cc, cxx)
233 def getLocalFolder(self, context):
234 return context.localFolder_
236 def addSymlink(self, context, source, output_path):
237 if util.host_platform == 'windows':
238 # Windows pre-Vista does not support symlinks. Windows Vista+ supports
239 # symlinks via mklink, but it's Administrator-only by default.
240 return self.addCopy(context, source, output_path)
241 raise Exception('Must be implemented!')
243 def addFolder(self, context, folder):
244 raise Exception('Must be implemented!')
246 def addCopy(self, context, source, output_path):
247 raise Exception('Must be implemented!')
249 def addShellCommand(self, context, inputs, argv, outputs, folder=-1, dep_type=None,
250 weak_inputs=[], shared_outputs=[]):
251 raise Exception('Must be implemented!')
253 def addConfigureFile(self, context, path):
254 raise Exception('Must be implemented!')