Build Flags
You can customize build and linker flags applied to your C, C++, and assembly
source files in Megaton.toml
, using the build.flags
object.
There are 5 properties that can be set:
common
: Common flags for all processesc
: Flags for compiling C sources.c
cxx
: Flags for compiling C++ sources.cpp
,.cc
,.cxx
,.c++
as
: Flags for compiling assembly sources.s
,.asm
ld
: Flags for linking
Each flag property is an array of strings:
[build.flags]
common = ["<default>"]
c = ["<default>", "-DDEBUG"]
cxx = ["<default>"]
as = ["<default>"]
ld = ["<default>"]
C++ compiler is used for linking. Flags for ld
should be specified as -Wl,--flag
instead of --flag
.
The string <default>
is a special token used to include the
default flags that are maintained by the megaton build tool
plus extending from another set of flags (for example, the
default flags of cxx
include all c
flags).
The default flags can be found here, and the flag extension behavior is detailed below.
Property | <default> includes |
---|---|
common | None |
c | common |
cxx | c |
as | cpp |
ld | common |
Each property is also optional. If it's not set, it's equivalent
to ["<default>"]
. Setting a property to an empty array []
means
not add any flags.
Includes and Libraries
Includes and libraries should be specified in build.includes
,
build.libpaths
, and build.libraries
respectively. See here for more details.
The build tool will also automatically include the headers for
libmegaton
and link with your project's rust code if the [rust]
top-level section
is configured.
Profiles
The build flags also support the profile system.
The profile-specific flags for a profile foo
is defined at build.profiles.foo.flags
.
The flags are merged with flags from the base build.flags
in the following way:
- If a property is not specified, it will be the same as the ones in
build.flags
- If a property is specified, it will be appended to the ones in
build.flags
For example:
[build.flags]
c = ["<default>", "-DDEBUG"]
[build.profiles.foo.flags]
c = ["-DFOO=1"]
The C flags for the base profile will be the default flags plus -DDEBUG
,
while the C flags for the foo
profile will be the default flags plus -DDEBUG -DFOO=1
.
There isn't a way to remove flags from the base profile. Typically, when you find yourself needing to remove a flag, you can restructure the flags and configure profile defaults to get the desired behavior for selecting profile with command line.
For example, if you want to build with -DDEBUG
by default,
and change it to -DNODEBUG
for the release
profile, you can do:
[profile]
default = "debug"
allow-base = false
[build.flags]
[build.profiles.debug.flags]
c = ["<default>", "-DDEBUG"]
[build.profiles.release.flags]
c = ["<default>", "-DNODEBUG"]
With this configuration megaton build
will build with -DDEBUG
and megaton build -p release
will build with -DNODEBUG
.