Download emk: .zip .tar.gz View On GitHub

C Module

The c module is an emk module for automatically detecting and compiling C and C++ source code. This module adds the compiled object files to the link module, which will link them into libraries/executables as desired. The object files are added to the link module’s objects property (each mapped to the source file that the object file was built from), so that the link module can autodetect main() functions from the source (if link.detect_exe == “simple”). See the link module documentation for details of main() autodetection.

The c module also sets the link module’s link_cxx flag if there are any C++ source files being compiled.

The C module can support various compilers by setting the compiler property to a different instance. The default compiler instance uses GCC for compilation. You can create a new instance of c.GccCompiler to use a different version of gcc (eg for cross-compilation). To support other compilers, you would need to implement a compiler class (see “Compiler Support” below).

This module defines emk rules during the prebuild stage, to allow autodiscovery of generated source files from rules defined before the prebuild stage (ie, in the post_rules() method of other modules). See the autodetect and autodetect_from_targets properties for more information about autodiscovery of source files.

Note that the compilation rules are not built automatically; the link module (or other modules/user code) is responsible for marking the object files as autobuild if desired.

Classes

GccCompiler: A compiler class that uses gcc/g++ to compile.

Properties (defaults set based on the path prefix passed to the constructor):

MsvcCompiler: A compiler class that uses the Microsoft Visual Studio command line tools to compile.

Properties (defaults set based on the path prefix passed to the constructor):

Properties

All properties are inherited from the parent scope if there is one.

Compiler Support

To add support for a new compiler, you must implement a compiler class for use by the c module (and then set the c module’s compiler property to an instance of the compiler class). A compiler class must provide the following methods:

load_extra_dependencies(self, path)

This function should load any extra dependencies for the given object file path (ie, header files). The extra dependencies could be loaded from a generated dependency file for that path, or loaded from the emk.scope_cache(path) (or some other mechanism).

Arguments:

compile_c(self, source, dest, includes, defines, flags)

This function will be called to compile a C file into an object file. This function is not required if no C files will be compiled.

Arguments:

compile_cxx(self, source, dest, includes, defines, flags)

This function will be called to compile a C++ file into an object file. This function is not required if no C++ files will be compiled.

Arguments:

obj_ext(self)

This function will be called to get the extension of object files built by this compiler.