ambuild2/frontend/base_gen.py
author David Anderson <danderson@mozilla.com>
Sat Dec 28 10:21:31 2013 -0500 (2013-12-28)
changeset 218 032616e296da
parent 217 9cba779ae86e
child 219 84822e8a3205
permissions -rw-r--r--
Allow custom shell commands to specify a dependency discovery scheme (bug 5997 part 3, 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 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.
     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
    18 import sys, copy
    19 from ambuild2 import util
    20 from ambuild2.frontend import cpp
    21 
    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).
    27 #
    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
    30 # scripts change.
    31 
    32 class ConfigureException(Exception):
    33   def __init__(self, *args, **kwargs):
    34     super(ConfigureException, self).__init__(*args, **kwargs)
    35 
    36 class Context(object):
    37   def __init__(self, generator, parent, script):
    38     self.generator = generator
    39     self.parent = parent
    40     self.script = script
    41     self.compiler = None
    42 
    43     if parent:
    44       self.compiler = parent.compiler
    45 
    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)
    49     if parent:
    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)
    53     else:
    54       self.currentSourcePath = generator.sourcePath
    55       self.currentSourceFolder = ''
    56       self.buildFolder = ''
    57     self.buildScript = os.path.join(self.currentSourceFolder, name)
    58     self.localFolder_ = self.buildFolder
    59 
    60   # Root source folder.
    61   @property
    62   def sourcePath(self):
    63     return self.generator.sourcePath
    64 
    65   @property
    66   def options(self):
    67     return self.generator.options
    68 
    69   @property
    70   def buildPath(self):
    71     return self.generator.buildPath
    72 
    73   # In build systems with dependency graphs, this can return a node
    74   # representing buildFolder. Otherwise, it returns buildFolder.
    75   @property
    76   def localFolder(self):
    77     return self.generator.getLocalFolder(self)
    78 
    79   @property
    80   def target_platform(self):
    81     return self.generator.target_platform
    82 
    83   @property
    84   def host_platform(self):
    85     return self.generator.host_platform
    86 
    87   def SetBuildFolder(self, folder):
    88     if folder == '/' or folder == '.' or folder == './':
    89       self.buildFolder = ''
    90     else:
    91       self.buildFolder = os.path.normpath(folder)
    92     self.localFolder_ = self.buildFolder
    93 
    94   def DetectCompilers(self):
    95     if not self.compiler:
    96       self.compiler = self.generator.DetectCompilers()
    97     return self.compiler
    98 
    99   def RunScript(self, file, vars={}):
   100     return self.generator.evalScript(file, vars)
   101 
   102   def RunBuildScripts(self, files, vars={}):
   103     if util.IsString(files):
   104       self.generator.evalScript(files, vars)
   105     else:
   106       for script in files:
   107         self.generator.evalScript(script, vars)
   108 
   109   def Add(self, taskbuilder):
   110     taskbuilder.finish(self)
   111     return taskbuilder.generate(self.generator, self)
   112 
   113   def AddSource(self, source_path):
   114     return self.generator.addSource(self, source_path)
   115 
   116   def AddSymlink(self, source, output_path):
   117     return self.generator.addSymlink(self, source, output_path)
   118 
   119   def AddFolder(self, folder):
   120     return self.generator.addFolder(self, folder)
   121 
   122   def AddCopy(self, source, output_path):
   123     return self.generator.addCopy(self, source, output_path)
   124 
   125   def AddCommand(self, inputs, argv, outputs, dep_type=None):
   126     return self.generator.addShellCommand(self, inputs, argv, outputs, dep_type=dep_type)
   127 
   128   def AddConfigureFile(self, path):
   129     return self.generator.addConfigureFile(self, path)
   130 
   131 class Generator(object):
   132   def __init__(self, sourcePath, buildPath, options, args):
   133     self.sourcePath = sourcePath
   134     self.buildPath = os.path.normpath(buildPath)
   135     self.options = options
   136     self.args = args
   137     self.compiler = None
   138     self.contextStack_ = [None]
   139     self.configure_failed = False
   140 
   141     # This is a hack... if we ever do cross-compiling or something, we'll have
   142     # to change this.
   143     self.host_platform = util.Platform()
   144     self.target_platform = util.Platform()
   145 
   146   def parseBuildScripts(self):
   147     root = os.path.join(self.sourcePath, 'AMBuildScript')
   148     self.evalScript(root)
   149 
   150   def pushContext(self, cx):
   151     self.contextStack_.append(cx)
   152 
   153   def popContext(self):
   154     self.contextStack_.pop()
   155 
   156   def evalScript(self, file, vars={}):
   157     cx = Context(self, self.contextStack_[-1], file)
   158     self.pushContext(cx)
   159 
   160     full_path = os.path.join(self.sourcePath, cx.buildScript)
   161 
   162     self.addConfigureFile(cx, full_path)
   163 
   164     new_vars = copy.copy(vars)
   165     new_vars['builder'] = cx
   166 
   167     # Run it.
   168     rvalue = None
   169     with open(full_path) as fp:
   170       chars = fp.read()
   171 
   172       # Python 2.6 can't compile() with Windows line endings?!?!!?
   173       chars = chars.replace('\r\n', '\n')
   174       chars = chars.replace('\r', '\n')
   175 
   176       code = compile(chars, full_path, 'exec')
   177 
   178     exec(code, new_vars)
   179     if 'rvalue' in new_vars:
   180       rvalue = new_vars['rvalue']
   181       del new_vars['rvalue']
   182 
   183     self.popContext()
   184     return rvalue
   185 
   186   def generateBuildFiles(self):
   187     build_py = os.path.join(self.buildPath, 'build.py')
   188     with open(build_py, 'w') as fp:
   189       fp.write("""
   190 #!{exe}
   191 # vim set: ts=8 sts=2 sw=2 tw=99 et:
   192 import sys
   193 from ambuild2 import run
   194 
   195 if not run.CompatBuild(r"{build}"):
   196   sys.exit(1)
   197 """.format(exe=sys.executable, build=self.buildPath))
   198 
   199     with open(os.path.join(self.buildPath, 'Makefile'), 'w') as fp:
   200       fp.write("""
   201 all:
   202 	"{exe}" "{py}"
   203 """.format(exe=sys.executable, py=build_py))
   204 
   205   def generate(self):
   206     self.preGenerate()
   207     self.parseBuildScripts()
   208     self.postGenerate()
   209     if self.options.make_scripts:
   210       self.generateBuildFiles()
   211     return True
   212 
   213   def DetectCompilers(self):
   214     if self.compiler:
   215       return self.compiler
   216 
   217     with util.FolderChanger('.ambuild2'):
   218       cc = cpp.DetectCompiler(self, os.environ, 'CC')
   219       cxx = cpp.DetectCompiler(self, os.environ, 'CXX')
   220     self.compiler = cpp.Compiler(cc, cxx)
   221     return self.compiler
   222 
   223   def getLocalFolder(self, context):
   224     return context.localFolder_
   225 
   226   def addSymlink(self, context, source, output_path):
   227     if util.host_platform == 'windows':
   228       # Windows pre-Vista does not support symlinks. Windows Vista+ supports
   229       # symlinks via mklink, but it's Administrator-only by default.
   230       return self.addCopy(context, source, output_path)
   231     raise Exception('Must be implemented!')
   232 
   233   def addFolder(self, context, folder):
   234     raise Exception('Must be implemented!')
   235 
   236   def addCopy(self, context, source, output_path):
   237     raise Exception('Must be implemented!')
   238 
   239   def addShellCommand(self, context, inputs, argv, outputs, dep_type=None):
   240     raise Exception('Must be implemented!')
   241 
   242   def addConfigureFile(self, context, path):
   243     raise Exception('Must be implemented!')