Difference between revisions of "AMBuild API (2.0)"

From AlliedModders Wiki
Jump to: navigation, search
m (remove AddDependency)
m (BAILOPAN moved page AMBuild API to AMBuild API (2.0))
 
(44 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
AMBuild scripts have access to the following object types:
 
AMBuild scripts have access to the following object types:
 
* '''Context''': Exposed as the <tt>builder</tt>, this is the entry point for the API and is known as a ''configure context''.
 
* '''Context''': Exposed as the <tt>builder</tt>, this is the entry point for the API and is known as a ''configure context''.
* '''NodeBuilder''': Represents a node in the dependency graph that may or may not be committed to the database.
+
* '''Entry''': Represents a node in the dependency graph.
 
* '''Compiler''': An abstraction representing a C++ compiler environment.
 
* '''Compiler''': An abstraction representing a C++ compiler environment.
* '''Vendor''': An abstraction representing a C++ compiler vendor (GCC, MSVC, Clang, etc).
+
* '''ProjectBuilder''' and '''BinaryBuilder''': Abstractions for building C++ compilation jobs.
* '''BinaryBuilder''': An abstraction for building C++ compilation jobs.
 
  
 
As a general rule, AMBuild uses the following conventions:
 
As a general rule, AMBuild uses the following conventions:
 
* Methods starting with an upper-case letter are considered public API.
 
* Methods starting with an upper-case letter are considered public API.
* Methods starting with a lower-case letter are considered private and should not be used.
+
* Methods starting with a lower-case letter are considered private and should not be used. There are a few exceptions for brevity or accessor-like functions.
 
* Properties and attributes ending with an underscore are considered private and should not be used.
 
* Properties and attributes ending with an underscore are considered private and should not be used.
 
* Spaces are used instead of tabs, and two-space indents are preferred.
 
* Spaces are used instead of tabs, and two-space indents are preferred.
Line 16: Line 15:
 
Contexts are implemented in <tt>ambuild2/frontend/base_gen.py</tt>. They are the entry point for using AMBuild.
 
Contexts are implemented in <tt>ambuild2/frontend/base_gen.py</tt>. They are the entry point for using AMBuild.
  
When using paths inside a context, it is important to note how AMBuild recognizes paths.
+
When using path strings, it is important to note how AMBuild recognizes paths.
* ''source'' paths are any paths that are absolute. They must not resolve to a path inside the build folder. Source paths are considered inputs to the build.
+
* ''source'' path strings may be any absolute path in the file system, as long as that path is not within the build folder. Source paths may also be expressed as relative to <tt>builder.currentSourcePath</tt>, which is the source folder of the currently executing build script.
* ''output'' paths are any paths that are relative. They are always relative to the build folder. Output paths are generated by the build process.
+
* ''output'' path strings are always relative to <tt>builder.buildFolder</tt>.
  
 
==Attributes==
 
==Attributes==
* ''compiler'' - An instance of a <tt>Compiler</tt> object, or <tt>None</tt> if <tt>DetectCompilers</tt> was never called. Calling <tt>DetectCompilers</tt> will set the <tt>compiler</tt> field for this and all future contexts.
+
* ''compiler'' - An instance of a <tt>Compiler</tt> object, or <tt>None</tt> if <tt>DetectCompilers</tt> was never called. Calling <tt>DetectCompilers</tt> will set the <tt>compiler</tt> field for this and all future contexts. Each context gets a clone of its parent's compiler, so it is safe to modify compilers in inner scripts.
 
* ''parent'' - The context that loaded the current context.
 
* ''parent'' - The context that loaded the current context.
 
* ''script'' - The path, relative to <tt>sourcePath</tt>, of the current build script.
 
* ''script'' - The path, relative to <tt>sourcePath</tt>, of the current build script.
Line 28: Line 27:
 
* ''buildPath'' - The absolute path to the build folder.
 
* ''buildPath'' - The absolute path to the build folder.
 
* ''buildFolder'' - The working directory of jobs in this context, relative to <tt>buildPath</tt>.
 
* ''buildFolder'' - The working directory of jobs in this context, relative to <tt>buildPath</tt>.
 +
* ''localFolder'' - The <tt>Entry</tt> for <tt>buildFolder</tt>; <tt>None</tt> for the root of the build.
 +
* ''currentSourcePath'' - The source folder that the current build script is in.
 +
* ''target_platform'' - The platform name that the build is targeting (see [[#Platforms]]).
 +
* ''host_platform'' - The platform name that the build is being generated on (see [[#Platforms]]).
 +
* ''originalCwd'' - The working directory that was set when the user first configured the build. This is useful when constructing absolute paths from user inputs that contain relative paths.
  
 
==Methods==
 
==Methods==
Line 35: Line 39:
 
* ''DetectCompilers()'' - Detects C and C++ compilers and raises an exception if neither are available or they do not match. Should only be called once.
 
* ''DetectCompilers()'' - Detects C and C++ compilers and raises an exception if neither are available or they do not match. Should only be called once.
 
** Returns a <tt>Compiler</tt> object that is also accessible via the <tt>compiler</tt> attribute.
 
** Returns a <tt>Compiler</tt> object that is also accessible via the <tt>compiler</tt> attribute.
 +
* ''RunScript(files, vars={})'' - Runs one additional build script.
 +
** ''file'' - A string containing a file path, relative to the current script's path.
 +
** ''vars'' - An optional dictionary of global variables to set in each child build script. The dictionary is merged with the global variables passed into our current script. Variables with the same name are overridden by the new dictionary.
 +
** Returns the global variable <tt>rvalue</tt> if set in the child script; otherwise, returns <tt>None</tt>.
 
* ''RunBuildScripts(files, vars={})'' - Runs additional build scripts.
 
* ''RunBuildScripts(files, vars={})'' - Runs additional build scripts.
 
** ''files'' - An iterable containing a list of file paths relative to path of the current build script, or a string containing such a path.
 
** ''files'' - An iterable containing a list of file paths relative to path of the current build script, or a string containing such a path.
 
** ''vars'' - An optional dictionary of global variables to set in each child build script. The dictionary is merged with the global variables passed into our current script. Variables with the same name are overridden by the new dictionary.
 
** ''vars'' - An optional dictionary of global variables to set in each child build script. The dictionary is merged with the global variables passed into our current script. Variables with the same name are overridden by the new dictionary.
 
** Returns <tt>None</tt>.
 
** Returns <tt>None</tt>.
 +
* ''Context(name)'' - creates an object that can be used with the Python <tt>with</tt> keyword. The object available in the <tt>with</tt> scope is a new context. This can be used for creating temporary settings without having a separate script file.
 
* ''Add(taskbuilder)'' - Some jobs are too complex to use a single API call, and instead provide objects to assist in creation. These objects are finalized into the dependency graph with this function. Currently, the only complex jobs built-in are C/C++ compilation jobs.
 
* ''Add(taskbuilder)'' - Some jobs are too complex to use a single API call, and instead provide objects to assist in creation. These objects are finalized into the dependency graph with this function. Currently, the only complex jobs built-in are C/C++ compilation jobs.
 
** ''taskbuilder'' - The job builder instance.
 
** ''taskbuilder'' - The job builder instance.
** Returns a value based on the taskbuilder. For example, C++ objects return a 'CppNode' described under the Compiler section.
+
** Returns a value based on the taskbuilder. For example, BinaryBuilders return a 'CppNode' described under the Compiler section, and ProjectBuilders return a list of CppNodes.
* ''AddSource(source_path)'' - Adds a source file as an object in the dependency graph. Source objects can be dependencies for other objects, but they themselves should not have dependencies. If a source file with the same path already exists, it will be re-used.
 
** ''source_path'' - An absolute path to the source file; it can be anywhere in the filesystem, except that it must not be in the build folder. Anything in the build folder is considered an output.
 
** Returns a <tt>NodeBuilder</tt> instance describing the dependency graph node.
 
 
* ''AddFolder(folder)'' - Ensures that a folder is created when performing a build. The folder is created relative to the context's local build folder.
 
* ''AddFolder(folder)'' - Ensures that a folder is created when performing a build. The folder is created relative to the context's local build folder.
 
** ''folder'' - A relative path specifying the folder. Folder chains can be created all at once; i.e. 'a/b/c' is a valid target even if 'a' or 'a/b' do not exist.
 
** ''folder'' - A relative path specifying the folder. Folder chains can be created all at once; i.e. 'a/b/c' is a valid target even if 'a' or 'a/b' do not exist.
** Returns a <tt>NodeBuilder</tt> instance describing the folder creation node.
+
** Returns an <tt>Entry</tt> instance describing the folder creation node.
 
* ''AddSymlink(source, output_path)'' - Adds a job to the build that will perform a symlink. On systems where symlinks are not available, it is implemented as a copy.
 
* ''AddSymlink(source, output_path)'' - Adds a job to the build that will perform a symlink. On systems where symlinks are not available, it is implemented as a copy.
 
* ''AddCopy(source, output_path)'' - Adds a job to the build that will perform a file copy.
 
* ''AddCopy(source, output_path)'' - Adds a job to the build that will perform a file copy.
 
** ''source'' - Either a string containing a source file path, or a source node, or an output node, representing the file that will be the source of the operation.
 
** ''source'' - Either a string containing a source file path, or a source node, or an output node, representing the file that will be the source of the operation.
 
** ''output_path'' - One of the following:
 
** ''output_path'' - One of the following:
*** A string path ending in a path separator, or <tt>'.'</tt>, or a string path containing a folder added via <tt>AddFolder()</tt>, representing the folder to copy or symlink the file to. It is relative to the context's local build folder.
+
*** A string path ending in a path separator, or <tt>'.'</tt>, specifying the folder to copy or symlink the file to. It is relative to the context's local build folder.
 
*** A string path ending in a filename, representing the destination file of the operation. It is relative to the context's local build folder.
 
*** A string path ending in a filename, representing the destination file of the operation. It is relative to the context's local build folder.
 
*** A folder node created via <tt>AddFolder()</tt>, representing the folder to copy or symlink the file to. In this case, the folder node's path is taken as-is and is not relative to the local build folder.
 
*** A folder node created via <tt>AddFolder()</tt>, representing the folder to copy or symlink the file to. In this case, the folder node's path is taken as-is and is not relative to the local build folder.
** Returns a <tt>NodeBuilder</tt> instance representing the node for this command in the dependency graph.
+
** See <tt>AddCommand</tt> for return values.
* ''AddCommand(argv, outputs)'' - Adds a custom command that will be executed manually. The working directory of the command is the context's local build folder. As created, the command has no dependencies. If it has source dependencies they can be specified via <tt>AddDependency</tt>.
+
* ''AddCommand(inputs, argv, outputs, folder?, dep_type?, weak_inputs?, shared_outputs?)'' - Adds a custom command that will be executed manually. The working directory of the command is the context's local build folder. As created, the command has no dependencies. If it has source dependencies they can be specified via <tt>AddDependency</tt>.
 +
** ''inputs'' - An iterable containing source file paths and/or <tt>Entry</tt> output-file nodes that are incoming dependencies.
 
** ''argv'' - The argument vector that will be passed to <tt>subprocess.Popen</tt>. <tt>argv[0]</tt> should be the executable.
 
** ''argv'' - The argument vector that will be passed to <tt>subprocess.Popen</tt>. <tt>argv[0]</tt> should be the executable.
 
** ''outputs'' - An iterable containing files that are outputs of this command. Each file must be a relative path from the context's local build folder.
 
** ''outputs'' - An iterable containing files that are outputs of this command. Each file must be a relative path from the context's local build folder.
 +
** ''folder'' - The working directory for the command. By default, this is <tt>buildFolder</tt>. It can be <tt>None</tt> to specify the root of the build. Otherwise, it must be an <tt>Entry</tt> from calling <tt>AddFolder</tt> or reading <tt>localFolder</tt>.
 +
** ''dep_type'' - Specifies whether the output of the command contains a dependency list. The following three values are supported:
 +
*** <tt>None</tt> - (default) This command does not support dependency discovery or does not have dynamic dependencies.
 +
*** <tt>'msvc'</tt> - Dependencies are spewed in the exact manner of the Visual Studio C++ compiler, in <tt>stdout</tt>.
 +
*** <tt>'gcc'</tt> - Dependencies are spewed in the exact manner of the GNU C Compiler or Clang, in <tt>stderr</tt>.
 +
*** <tt>'sun'</tt> - Dependencies are spewed in the exact manner of the Sun Pro compiler, in <tt>stderr</tt>.
 +
** ''weak_inputs'' - Optional list of weak dependencies. Weak dependencies enforce an ordering between two commands, but an update only occurs if the command is discovered to actually use the dependency (see the Compiler.sourcedeps attribute).
 +
** ''shared_outputs'' - Optional list of "shared" outputs. Shared outputs are files that are generated by multiple commands. This is a degenerate case in AMBuild, but some systems do this, and AMBuild needs to understand where the files came from. Shared outputs can not be used as an input; they do not participate in the dependency system, except that AMBuild knows when to remove them.
 
** Returns a 2-tuple, containing:
 
** Returns a 2-tuple, containing:
*** A <tt>NodeBuilder</tt> instance representing the node for this command in the dependency graph.
+
*** An <tt>Entry</tt> instance representing the node for this command in the dependency graph.
*** A list of <tt>NodeBuilder</tt> instances, each instance corresponding to one of the output file paths specified.
+
*** A list of <tt>Entry</tt> instances, each instance corresponding to one of the output file paths specified.
 +
* ''AddConfigureFile(path)'' - Adds a source path that will trigger automatic reconfiguring if the file changes. This is useful for files that are conceptually part of a build script, but are not actually loaded as a build script.
 +
** ''path'' - A source path.
 +
* ''ImportScript(path, vars?)'' - Runs another script, similar to RunScript, except that it returns an object with a property corresponding to each global variable in the evaluated script. Initial variables can be supplied through <tt>vars</tt>.
 +
 
 +
==Platforms==
 +
AMBuild will use one of the following as a platform string:
 +
* <tt>"windows"</tt>
 +
* <tt>"mac"</tt>
 +
* <tt>"linux"</tt>
 +
* <tt>"freebsd"</tt>
 +
* <tt>"openbsd"</tt>
 +
* <tt>"netbsd"</tt>
 +
* <tt>"solaris"</tt>
 +
* <tt>"cygwin"</tt>
 +
 
 +
=Entry=
 +
<tt>Entry</tt> objects are considered mostly opaque. However, all frontends must define at least these attributes:
 +
* ''path'' - A file system path that uniquely represents nodes that are present in the filesystem, such as source files, output files, or folders.
 +
 
 +
=Compiler=
 +
Compiler objects encapsulate information about invoking the C or C++ compiler. Most of its attributes are lists of options, so it is best to use += to extend these lists, to avoid replacing previously set options.
 +
 
 +
Whenever options come in pairs - for example, C/C++ flags versus C++-only flags, C++ flags will automatically include all C flags during compilation time.
 +
 
 +
==Attributes==
 +
* ''includes'' - List of C and C++ include paths
 +
* ''cxxincludes'' - List of C++ include paths.
 +
* ''cflags'' - List of C and C++ compiler flags.
 +
* ''cxxflags'' - List of C++ compiler flags.
 +
* ''defines'' - List of C and C++ #defines, in the form of 'KEY' or 'KEY=VALUE'
 +
* ''cxxdefines'' - List of C++ #defines, in the form of 'KEY' or 'KEY=VALUE'
 +
* ''rcdefines'' - List of RC (Resource Compiler) #defines, in the form of 'KEY' or 'KEY=VALUE'
 +
* ''linkflags'' - Link flags (see below).
 +
* ''postlink'' - Array of objects to link, added to the linker flags after linkflags. See below.
 +
* ''sourcedeps'' - An array of output nodes which should be weak dependencies on each source compilation node.
 +
* ''debuginfo'' - One of three values:
 +
**<tt>None</tt> - No debug information is generated.
 +
**<tt>'bundled'</tt> - If possible, debug information will be generated into the binary. On some compilers this is not possible. For example, MSVC always generates .PDB files.
 +
**<tt>'separate'</tt> - Separates debug information into a separate file (a .pdb file, .sym file, or dSYM package depending on the platform).
 +
* ''vendor'' - The vendor string for the detected compiler ('msvc', 'gcc', 'icc', 'clang', or 'sun'). This attribute is read-only.
 +
* ''version'' - Returns an object representing the compiler version. It may be stringified with <tt>str()</tt>. It can also be compared to either integers, other version objects, or strings. For example: <tt>compiler.version >= '4.7.4'</tt> will return true if the compiler's version is at least <tt>4.7.3</tt>. The exact meaning of the version is vendor-dependent; for example, MSVC versions look like large numbers (<tt>1800</tt> corresponds to Visual Studio 2013).
 +
* ''argv'' - Returns a list containing the program name and arguments used to invoke the detected compiler. Note that availability of this is frontend-dependent. It is currently only exposed for the AMBuild 2 frontend.
 +
 
 +
Entries to <tt>linkflags</tt> and <tt>postlink</tt> can be:
 +
* A string representing a linker flag.
 +
* An <tt>Entry</tt> object.
 +
* A <tt>Dep</tt> object. <tt>Dep</tt> objects are useful when precise control is needed over what text is given to the linker in order to link in a file. Essentially, they let you customize the link flag while still including a dependency. They also allow lazy computation of dependencies, since sometimes extra steps must be generated before linking to an object. <tt>Dep</tt> objects have two attributes:
 +
** <tt>text</tt>, the text that will be passed to the linker when constructing its argument list.
 +
** <tt>node</tt>, an object which tells AMBuild how to build a dependency for the linker flag. It can be:
 +
*** <tt>None</tt>, meaning that the text is a file path.
 +
*** A file path.
 +
*** An <tt>Entry</tt> or list of <tt>Entry</tt> objects representing the output files of a command, or
 +
*** A function which returns the above, and has the signature <tt>(builder, binary)</tt>, receiving a <tt>Context</tt> object and a <tt>BinaryBuilder</tt> object.
 +
* ''Note:'' AMBuild does not currently support automatic dependency generation for <tt>-L</tt> style linking.
 +
 
 +
For example, to generate a linker invocation like "<tt>g++ main.o tier1.so -o main</tt>", where <tt>tier1.so</tt> is a generated file, you could do:
 +
<Python>
 +
binary.postlink += [binary.Dep('tier1.so', tier1_so_entry)]
 +
</Python>
 +
 
 +
If this requires symlinking <tt>tier1.so</tt> to be in the local folder, you can get more complex, such as:
 +
<Python>
 +
def make_linker_dep(compiler, name, entry):
 +
  def lazy_dep(builder, binary):
 +
    cmd, (output,) = builder.AddSymlink(entry, '.')
 +
    return output
 +
  return compiler.Dep(name, lazy_dep)
 +
</Python>
 +
 
 +
==Methods==
 +
There are two APIs for creating C++ binaries - <tt>ProjectBuilder</tt> and <tt>BinaryBuilder</tt>. <tt>ProjectBuilder</tt> makes it possible to group related tasks together. For example, if one binary has multiple build configurations, a <tt>Builder</tt> will allow you to group them together with a common set of source files. For building, the results are exactly the same as creating individual <tt>BinaryBuilder</tt>s.
 +
 
 +
For generating IDE project files, the distinction can be important. When the same binary is built across multiple configurations, AMBuild generates a new project file for each configuration. Using <tt>ProjectBuilder</tt> over <tt>BinaryBuilder</tt> allows AMBuild to store each of the configurations in a single project file.
 +
 
 +
* <tt>Program(name)</tt> - Creates a new <tt>BinaryBuilder</tt> instance, with a copy of the compiler settings. The builder is configured to generate an executable (<tt>.exe</tt> is automatically appended on Windows).
 +
* <tt>Library(name)</tt> - Creates a new <tt>BinaryBuilder</tt> instance, with a copy of the compiler settings. The builder is configured to generate a shared library. <tt>.so</tt>, <tt>.dylib</tt>, or <tt>.dll</tt> is automatically appended depending on the platform. Nothing is ever prepended to the name.
 +
* <tt>StaticLibrary(name)</tt> - Creates a new <tt>BinaryBuilder</tt> instance, with a copy of the compiler settings. The builder is configured to generate a static library. <tt>.a</tt> or <tt>.lib</tt> is automatically appended depending on the platform. Nothing is ever prepended to the name.
 +
* <tt>ProgramProject(name)</tt> - Creates a new <tt>ProjectBuilder</tt> instance, with a copy of the compiler settings. The builder is configured to generate an executable (<tt>.exe</tt> is automatically appended on Windows).
 +
* <tt>LibraryProject(name)</tt> - Creates a new <tt>ProjectBuilder</tt> instance, with a copy of the compiler settings. The builder is configured to generate a shared library. <tt>.so</tt>, <tt>.dylib</tt>, or <tt>.dll</tt> is automatically appended depending on the platform. Nothing is ever prepended to the name.
 +
* <tt>StaticLibraryProject(name)</tt> - Creates a new <tt>ProjectBuilder</tt> instance, with a copy of the compiler settings. The builder is configured to generate a static library. <tt>.a</tt> or <tt>.lib</tt> is automatically appended depending on the platform. Nothing is ever prepended to the name.
 +
* <tt>Dep(text, node)</tt> - Creates a <tt>Dep</tt> object instance (see above for more details).
 +
* <tt>like(name)</tt> - Returns whether the compiler is "like" another compiler. This is intended to represent the compatibility hierarchy of modern compilers. For example:
 +
** Visual Studio is "like" 'msvc'.
 +
** GCC is "like" 'gcc'.
 +
** Clang is "like" 'gcc'.
 +
** Emscripten is "like" 'clang' and 'gcc'.
 +
** Note that GCC is not "like" Clang.
 +
* <tt>pkg_config(pkg, link = 'dynamic')</tt> - Runs the pkg-config program for the given package name, and adds relevant includes, cflags, and libraries to the compiler. If <tt>link</tt> is 'dynamic' (default), shared libraries are used. If <tt>link</tt> is 'static', then static libraries are used instead.
 +
 
 +
=BinaryBuilder=
 +
<tt>BinaryBuilder</tt> assists in creating C/C++ compilation tasks. Once you've set all the information needed, they are integrated into the dependency graph by using <tt>builder.Add()</tt>.
 +
 
 +
==Attributes==
 +
* ''compiler'' - A full copy of the compiler settings used when instantiating this <tt>BinaryBuilder</tt>. Modifying this compiler will not modify the original <tt>builder.compiler</tt>.
 +
* ''sources'' - An (initially empty) list of C/C++ source file paths. They can be absolute paths, or paths relative to <tt>currentSourcePath</tt>.
 +
* ''localFolder'' - The name of the folder this binary will be generated in, relative to the <tt>buildFolder</tt> of the context that adds the tasks.
 +
* ''type'' - One of 'static', 'library', or 'program'.
 +
 
 +
==Methods==
 +
* ''Dep(text, node)'' - A wrapper for <tt>compiler.Dep</tt>.
 +
 
 +
=ProjectBuilder=
 +
<tt>ProjectBuilder</tt> assists in creating multiple configurations of a C++ compilation task. Set the source list via <tt>sources</tt>, then use <tt>Configure()</tt> to add a new configuration. After all configurations have been added, use <tt>builder.Add()</tt> to add the project to the dependency graph.
 +
 
 +
When calling <tt>builder.Add()</tt> on a project, the return value is a list of <tt>CppNode</tt>s, one for each configuration in the order they were added.
 +
 
 +
==Attributes==
 +
* ''compiler'' - A full copy of the compiler settings used when instantiating this <tt>ProjectBuilder</tt>. Modifying this compiler will not modify the original <tt>builder.compiler</tt>.
 +
* ''sources'' - An (initially empty) list of C/C++ source file paths. They can be absolute paths, or paths relative to <tt>currentSourcePath</tt>.
 +
* ''localFolder'' - The name of the folder this binary will be generated in, relative to the <tt>buildFolder</tt> of the context that adds the tasks.
 +
 
 +
==Methods==
 +
* ''Configure(name, tag)'' - Returns a <tt>BinaryBuilder</tt> with the given name. The tag is used to describe the configuration for IDE project files.
 +
* ''Dep(text, node)'' - A wrapper for <tt>compiler.Dep</tt>.

Latest revision as of 02:45, 23 November 2015

AMBuild scripts have access to the following object types:

  • Context: Exposed as the builder, this is the entry point for the API and is known as a configure context.
  • Entry: Represents a node in the dependency graph.
  • Compiler: An abstraction representing a C++ compiler environment.
  • ProjectBuilder and BinaryBuilder: Abstractions for building C++ compilation jobs.

As a general rule, AMBuild uses the following conventions:

  • Methods starting with an upper-case letter are considered public API.
  • Methods starting with a lower-case letter are considered private and should not be used. There are a few exceptions for brevity or accessor-like functions.
  • Properties and attributes ending with an underscore are considered private and should not be used.
  • Spaces are used instead of tabs, and two-space indents are preferred.

Contexts

Contexts are implemented in ambuild2/frontend/base_gen.py. They are the entry point for using AMBuild.

When using path strings, it is important to note how AMBuild recognizes paths.

  • source path strings may be any absolute path in the file system, as long as that path is not within the build folder. Source paths may also be expressed as relative to builder.currentSourcePath, which is the source folder of the currently executing build script.
  • output path strings are always relative to builder.buildFolder.

Attributes

  • compiler - An instance of a Compiler object, or None if DetectCompilers was never called. Calling DetectCompilers will set the compiler field for this and all future contexts. Each context gets a clone of its parent's compiler, so it is safe to modify compilers in inner scripts.
  • parent - The context that loaded the current context.
  • script - The path, relative to sourcePath, of the current build script.
  • sourcePath - The absolute path to the source tree.
  • options - The result of evaluating command-line options from configure.py, via the optparse module.
  • buildPath - The absolute path to the build folder.
  • buildFolder - The working directory of jobs in this context, relative to buildPath.
  • localFolder - The Entry for buildFolder; None for the root of the build.
  • currentSourcePath - The source folder that the current build script is in.
  • target_platform - The platform name that the build is targeting (see #Platforms).
  • host_platform - The platform name that the build is being generated on (see #Platforms).
  • originalCwd - The working directory that was set when the user first configured the build. This is useful when constructing absolute paths from user inputs that contain relative paths.

Methods

  • SetBuildFolder(folder) - Sets the working directory of jobs in this context, relative to buildPath.
    • folder - A string representing the folder path. '.' and './' are allowed.
    • Returns None.
  • DetectCompilers() - Detects C and C++ compilers and raises an exception if neither are available or they do not match. Should only be called once.
    • Returns a Compiler object that is also accessible via the compiler attribute.
  • RunScript(files, vars={}) - Runs one additional build script.
    • file - A string containing a file path, relative to the current script's path.
    • vars - An optional dictionary of global variables to set in each child build script. The dictionary is merged with the global variables passed into our current script. Variables with the same name are overridden by the new dictionary.
    • Returns the global variable rvalue if set in the child script; otherwise, returns None.
  • RunBuildScripts(files, vars={}) - Runs additional build scripts.
    • files - An iterable containing a list of file paths relative to path of the current build script, or a string containing such a path.
    • vars - An optional dictionary of global variables to set in each child build script. The dictionary is merged with the global variables passed into our current script. Variables with the same name are overridden by the new dictionary.
    • Returns None.
  • Context(name) - creates an object that can be used with the Python with keyword. The object available in the with scope is a new context. This can be used for creating temporary settings without having a separate script file.
  • Add(taskbuilder) - Some jobs are too complex to use a single API call, and instead provide objects to assist in creation. These objects are finalized into the dependency graph with this function. Currently, the only complex jobs built-in are C/C++ compilation jobs.
    • taskbuilder - The job builder instance.
    • Returns a value based on the taskbuilder. For example, BinaryBuilders return a 'CppNode' described under the Compiler section, and ProjectBuilders return a list of CppNodes.
  • AddFolder(folder) - Ensures that a folder is created when performing a build. The folder is created relative to the context's local build folder.
    • folder - A relative path specifying the folder. Folder chains can be created all at once; i.e. 'a/b/c' is a valid target even if 'a' or 'a/b' do not exist.
    • Returns an Entry instance describing the folder creation node.
  • AddSymlink(source, output_path) - Adds a job to the build that will perform a symlink. On systems where symlinks are not available, it is implemented as a copy.
  • AddCopy(source, output_path) - Adds a job to the build that will perform a file copy.
    • source - Either a string containing a source file path, or a source node, or an output node, representing the file that will be the source of the operation.
    • output_path - One of the following:
      • A string path ending in a path separator, or '.', specifying the folder to copy or symlink the file to. It is relative to the context's local build folder.
      • A string path ending in a filename, representing the destination file of the operation. It is relative to the context's local build folder.
      • A folder node created via AddFolder(), representing the folder to copy or symlink the file to. In this case, the folder node's path is taken as-is and is not relative to the local build folder.
    • See AddCommand for return values.
  • AddCommand(inputs, argv, outputs, folder?, dep_type?, weak_inputs?, shared_outputs?) - Adds a custom command that will be executed manually. The working directory of the command is the context's local build folder. As created, the command has no dependencies. If it has source dependencies they can be specified via AddDependency.
    • inputs - An iterable containing source file paths and/or Entry output-file nodes that are incoming dependencies.
    • argv - The argument vector that will be passed to subprocess.Popen. argv[0] should be the executable.
    • outputs - An iterable containing files that are outputs of this command. Each file must be a relative path from the context's local build folder.
    • folder - The working directory for the command. By default, this is buildFolder. It can be None to specify the root of the build. Otherwise, it must be an Entry from calling AddFolder or reading localFolder.
    • dep_type - Specifies whether the output of the command contains a dependency list. The following three values are supported:
      • None - (default) This command does not support dependency discovery or does not have dynamic dependencies.
      • 'msvc' - Dependencies are spewed in the exact manner of the Visual Studio C++ compiler, in stdout.
      • 'gcc' - Dependencies are spewed in the exact manner of the GNU C Compiler or Clang, in stderr.
      • 'sun' - Dependencies are spewed in the exact manner of the Sun Pro compiler, in stderr.
    • weak_inputs - Optional list of weak dependencies. Weak dependencies enforce an ordering between two commands, but an update only occurs if the command is discovered to actually use the dependency (see the Compiler.sourcedeps attribute).
    • shared_outputs - Optional list of "shared" outputs. Shared outputs are files that are generated by multiple commands. This is a degenerate case in AMBuild, but some systems do this, and AMBuild needs to understand where the files came from. Shared outputs can not be used as an input; they do not participate in the dependency system, except that AMBuild knows when to remove them.
    • Returns a 2-tuple, containing:
      • An Entry instance representing the node for this command in the dependency graph.
      • A list of Entry instances, each instance corresponding to one of the output file paths specified.
  • AddConfigureFile(path) - Adds a source path that will trigger automatic reconfiguring if the file changes. This is useful for files that are conceptually part of a build script, but are not actually loaded as a build script.
    • path - A source path.
  • ImportScript(path, vars?) - Runs another script, similar to RunScript, except that it returns an object with a property corresponding to each global variable in the evaluated script. Initial variables can be supplied through vars.

Platforms

AMBuild will use one of the following as a platform string:

  • "windows"
  • "mac"
  • "linux"
  • "freebsd"
  • "openbsd"
  • "netbsd"
  • "solaris"
  • "cygwin"

Entry

Entry objects are considered mostly opaque. However, all frontends must define at least these attributes:

  • path - A file system path that uniquely represents nodes that are present in the filesystem, such as source files, output files, or folders.

Compiler

Compiler objects encapsulate information about invoking the C or C++ compiler. Most of its attributes are lists of options, so it is best to use += to extend these lists, to avoid replacing previously set options.

Whenever options come in pairs - for example, C/C++ flags versus C++-only flags, C++ flags will automatically include all C flags during compilation time.

Attributes

  • includes - List of C and C++ include paths
  • cxxincludes - List of C++ include paths.
  • cflags - List of C and C++ compiler flags.
  • cxxflags - List of C++ compiler flags.
  • defines - List of C and C++ #defines, in the form of 'KEY' or 'KEY=VALUE'
  • cxxdefines - List of C++ #defines, in the form of 'KEY' or 'KEY=VALUE'
  • rcdefines - List of RC (Resource Compiler) #defines, in the form of 'KEY' or 'KEY=VALUE'
  • linkflags - Link flags (see below).
  • postlink - Array of objects to link, added to the linker flags after linkflags. See below.
  • sourcedeps - An array of output nodes which should be weak dependencies on each source compilation node.
  • debuginfo - One of three values:
    • None - No debug information is generated.
    • 'bundled' - If possible, debug information will be generated into the binary. On some compilers this is not possible. For example, MSVC always generates .PDB files.
    • 'separate' - Separates debug information into a separate file (a .pdb file, .sym file, or dSYM package depending on the platform).
  • vendor - The vendor string for the detected compiler ('msvc', 'gcc', 'icc', 'clang', or 'sun'). This attribute is read-only.
  • version - Returns an object representing the compiler version. It may be stringified with str(). It can also be compared to either integers, other version objects, or strings. For example: compiler.version >= '4.7.4' will return true if the compiler's version is at least 4.7.3. The exact meaning of the version is vendor-dependent; for example, MSVC versions look like large numbers (1800 corresponds to Visual Studio 2013).
  • argv - Returns a list containing the program name and arguments used to invoke the detected compiler. Note that availability of this is frontend-dependent. It is currently only exposed for the AMBuild 2 frontend.

Entries to linkflags and postlink can be:

  • A string representing a linker flag.
  • An Entry object.
  • A Dep object. Dep objects are useful when precise control is needed over what text is given to the linker in order to link in a file. Essentially, they let you customize the link flag while still including a dependency. They also allow lazy computation of dependencies, since sometimes extra steps must be generated before linking to an object. Dep objects have two attributes:
    • text, the text that will be passed to the linker when constructing its argument list.
    • node, an object which tells AMBuild how to build a dependency for the linker flag. It can be:
      • None, meaning that the text is a file path.
      • A file path.
      • An Entry or list of Entry objects representing the output files of a command, or
      • A function which returns the above, and has the signature (builder, binary), receiving a Context object and a BinaryBuilder object.
  • Note: AMBuild does not currently support automatic dependency generation for -L style linking.

For example, to generate a linker invocation like "g++ main.o tier1.so -o main", where tier1.so is a generated file, you could do:

binary.postlink += [binary.Dep('tier1.so', tier1_so_entry)]

If this requires symlinking tier1.so to be in the local folder, you can get more complex, such as:

def make_linker_dep(compiler, name, entry):
  def lazy_dep(builder, binary):
    cmd, (output,) = builder.AddSymlink(entry, '.')
    return output
  return compiler.Dep(name, lazy_dep)

Methods

There are two APIs for creating C++ binaries - ProjectBuilder and BinaryBuilder. ProjectBuilder makes it possible to group related tasks together. For example, if one binary has multiple build configurations, a Builder will allow you to group them together with a common set of source files. For building, the results are exactly the same as creating individual BinaryBuilders.

For generating IDE project files, the distinction can be important. When the same binary is built across multiple configurations, AMBuild generates a new project file for each configuration. Using ProjectBuilder over BinaryBuilder allows AMBuild to store each of the configurations in a single project file.

  • Program(name) - Creates a new BinaryBuilder instance, with a copy of the compiler settings. The builder is configured to generate an executable (.exe is automatically appended on Windows).
  • Library(name) - Creates a new BinaryBuilder instance, with a copy of the compiler settings. The builder is configured to generate a shared library. .so, .dylib, or .dll is automatically appended depending on the platform. Nothing is ever prepended to the name.
  • StaticLibrary(name) - Creates a new BinaryBuilder instance, with a copy of the compiler settings. The builder is configured to generate a static library. .a or .lib is automatically appended depending on the platform. Nothing is ever prepended to the name.
  • ProgramProject(name) - Creates a new ProjectBuilder instance, with a copy of the compiler settings. The builder is configured to generate an executable (.exe is automatically appended on Windows).
  • LibraryProject(name) - Creates a new ProjectBuilder instance, with a copy of the compiler settings. The builder is configured to generate a shared library. .so, .dylib, or .dll is automatically appended depending on the platform. Nothing is ever prepended to the name.
  • StaticLibraryProject(name) - Creates a new ProjectBuilder instance, with a copy of the compiler settings. The builder is configured to generate a static library. .a or .lib is automatically appended depending on the platform. Nothing is ever prepended to the name.
  • Dep(text, node) - Creates a Dep object instance (see above for more details).
  • like(name) - Returns whether the compiler is "like" another compiler. This is intended to represent the compatibility hierarchy of modern compilers. For example:
    • Visual Studio is "like" 'msvc'.
    • GCC is "like" 'gcc'.
    • Clang is "like" 'gcc'.
    • Emscripten is "like" 'clang' and 'gcc'.
    • Note that GCC is not "like" Clang.
  • pkg_config(pkg, link = 'dynamic') - Runs the pkg-config program for the given package name, and adds relevant includes, cflags, and libraries to the compiler. If link is 'dynamic' (default), shared libraries are used. If link is 'static', then static libraries are used instead.

BinaryBuilder

BinaryBuilder assists in creating C/C++ compilation tasks. Once you've set all the information needed, they are integrated into the dependency graph by using builder.Add().

Attributes

  • compiler - A full copy of the compiler settings used when instantiating this BinaryBuilder. Modifying this compiler will not modify the original builder.compiler.
  • sources - An (initially empty) list of C/C++ source file paths. They can be absolute paths, or paths relative to currentSourcePath.
  • localFolder - The name of the folder this binary will be generated in, relative to the buildFolder of the context that adds the tasks.
  • type - One of 'static', 'library', or 'program'.

Methods

  • Dep(text, node) - A wrapper for compiler.Dep.

ProjectBuilder

ProjectBuilder assists in creating multiple configurations of a C++ compilation task. Set the source list via sources, then use Configure() to add a new configuration. After all configurations have been added, use builder.Add() to add the project to the dependency graph.

When calling builder.Add() on a project, the return value is a list of CppNodes, one for each configuration in the order they were added.

Attributes

  • compiler - A full copy of the compiler settings used when instantiating this ProjectBuilder. Modifying this compiler will not modify the original builder.compiler.
  • sources - An (initially empty) list of C/C++ source file paths. They can be absolute paths, or paths relative to currentSourcePath.
  • localFolder - The name of the folder this binary will be generated in, relative to the buildFolder of the context that adds the tasks.

Methods

  • Configure(name, tag) - Returns a BinaryBuilder with the given name. The tag is used to describe the configuration for IDE project files.
  • Dep(text, node) - A wrapper for compiler.Dep.