Skip to content

Commit

Permalink
Allow setting built-in options from cross/native files
Browse files Browse the repository at this point in the history
This is like the project options, but for meson builtin options.

The only real differences here have to do with the differences between
meson builtin options and project options. Some meson options can be set
on a per-machine basis (build.pkg_config_path vs pkg_config_path) others
can be set on a per-subproject basis, but should inherit the parent
setting.
  • Loading branch information
dcbaker committed Aug 2, 2020
1 parent af763e0 commit bbba6a7
Show file tree
Hide file tree
Showing 16 changed files with 307 additions and 38 deletions.
2 changes: 1 addition & 1 deletion cross/armcc.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ cpp = 'armcc'
ar = 'armar'
strip = 'armar'

[properties]
[built-in options]
# The '--cpu' option with the appropriate target type should be mentioned
# to cross compile c/c++ code with armcc,.
c_args = ['--cpu=Cortex-M0plus']
Expand Down
5 changes: 2 additions & 3 deletions cross/armclang-linux.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# Armcc is only available in toolchain version 5.
# Armclang is only available in toolchain version 6.
# Start shell with /opt/arm/developmentstudio-2019.0/bin/suite_exec zsh
# Now the compilers will work.
# Now the compilers will work.

[binaries]
# we could set exe_wrapper = qemu-arm-static but to test the case
Expand All @@ -24,8 +24,7 @@ ar = '/opt/arm/developmentstudio-2019.0/sw/ARMCompiler6.12/bin/armar'
#strip = '/usr/arm-linux-gnueabihf/bin/strip'
#pkgconfig = '/usr/bin/arm-linux-gnueabihf-pkg-config'

[properties]

[built-in options]
c_args = ['--target=aarch64-arm-none-eabi']

[host_machine]
Expand Down
2 changes: 1 addition & 1 deletion cross/armclang.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ cpp = 'armclang'
ar = 'armar'
strip = 'armar'

[properties]
[built-in options]
# The '--target', '-mcpu' options with the appropriate values should be mentioned
# to cross compile c/c++ code with armclang.
c_args = ['--target=arm-arm-none-eabi', '-mcpu=cortex-m0plus']
Expand Down
6 changes: 4 additions & 2 deletions cross/c2000.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ cpu_family = 'c2000'
cpu = 'c28x'
endian = 'little'

[properties]
needs_exe_wrapper = true
[built-in options]
c_args = [
'-v28',
'-ml',
Expand All @@ -24,3 +23,6 @@ c_link_args = [
'\f28004x_flash.cmd']
cpp_args = []
cpp_link_args = []

[properties]
needs_exe_wrapper = true
2 changes: 1 addition & 1 deletion cross/ccrx.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ cpp = 'ccrx'
ar = 'rlink'
strip = 'rlink'

[properties]
[built-in options]
# The '--cpu' option with the appropriate target type should be mentioned
# to cross compile c/c++ code with ccrx,.
c_args = ['-cpu=rx600']
Expand Down
6 changes: 3 additions & 3 deletions cross/iphone.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ cpp = 'clang++'
ar = 'ar'
strip = 'strip'

[properties]
root = '/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer'

[built-in options]
c_args = ['-arch', 'armv7', '-miphoneos-version-min=8.0', '-isysroot', '/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS8.4.sdk']
cpp_args = ['-arch', 'armv7', '-miphoneos-version-min=8.0', '-isysroot', '/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS8.4.sdk']
c_link_args = ['-arch', 'armv7', '-miphoneos-version-min=8.0', '-isysroot', '/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS8.4.sdk']
cpp_link_args = ['-arch', 'armv7', '-miphoneos-version-min=8.0', '-isysroot', '/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS8.4.sdk']

[properties]
root = '/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer'
has_function_printf = true
has_function_hfkerhisadf = false

Expand Down
7 changes: 4 additions & 3 deletions cross/tvos.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ cpp = 'clang++'
ar = 'ar'
strip = 'strip'

[properties]
root = '/Applications/Xcode.app/Contents/Developer/Platforms/AppleTVOS.platform/Developer'

[built-in options]
c_args = ['-arch', 'arm64', '-mtvos-version-min=12.0', '-isysroot', '/Applications/Xcode.app/Contents/Developer/Platforms/AppleTVOS.platform/Developer/SDKs/AppleTVOS.sdk']
cpp_args = ['-arch', 'arm64', '-mtvos-version-min=12.0', '-isysroot', '/Applications/Xcode.app/Contents/Developer/Platforms/AppleTVOS.platform/Developer/SDKs/AppleTVOS.sdk']
c_link_args = ['-arch', 'arm64', '-mtvos-version-min=12.0', '-isysroot', '/Applications/Xcode.app/Contents/Developer/Platforms/AppleTVOS.platform/Developer/SDKs/AppleTVOS.sdk']
cpp_link_args = ['-arch', 'arm64', '-mtvos-version-min=12.0', '-isysroot', '/Applications/Xcode.app/Contents/Developer/Platforms/AppleTVOS.platform/Developer/SDKs/AppleTVOS.sdk']

[properties]
root = '/Applications/Xcode.app/Contents/Developer/Platforms/AppleTVOS.platform/Developer'

has_function_printf = true
has_function_hfkerhisadf = false

Expand Down
6 changes: 4 additions & 2 deletions cross/ubuntu-armhf.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ strip = '/usr/arm-linux-gnueabihf/bin/strip'
pkgconfig = '/usr/bin/arm-linux-gnueabihf-pkg-config'
ld = '/usr/bin/arm-linux/gnueabihf-ld'

[properties]
root = '/usr/arm-linux-gnueabihf'
[built-in options]
# Used in unit test '140 get define'
c_args = ['-DMESON_TEST_ISSUE_1665=1']
cpp_args = '-DMESON_TEST_ISSUE_1665=1'

[properties]
root = '/usr/arm-linux-gnueabihf'

has_function_printf = true
has_function_hfkerhisadf = false

Expand Down
3 changes: 1 addition & 2 deletions cross/wasm.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ c = '/home/jpakkane/emsdk/fastcomp/emscripten/emcc'
cpp = '/home/jpakkane/emsdk/fastcomp/emscripten/em++'
ar = '/home/jpakkane/emsdk/fastcomp/emscripten/emar'

[properties]

[built-in options]
c_args = ['-s', 'WASM=1', '-s', 'EXPORT_ALL=1']
c_link_args = ['-s','EXPORT_ALL=1']
cpp_args = ['-s', 'WASM=1', '-s', 'EXPORT_ALL=1']
Expand Down
2 changes: 2 additions & 0 deletions cross/xc16.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ endian = 'little'

[properties]
needs_exe_wrapper = true

[built-in options]
c_args = [
'-c',
'-mcpu=33EP64MC203',
Expand Down
56 changes: 52 additions & 4 deletions docs/markdown/Machine-files.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ The following sections are allowed:
- paths
- properties
- project options
- built-in options

### constants

Expand Down Expand Up @@ -158,17 +159,23 @@ command line will override any options in the native file. For example, passing

In addition to special data that may be specified in cross files, this
section may contain random key value pairs accessed using the
`meson.get_external_property`, or `meson.get_cross_property`.
`meson.get_external_property()`, or `meson.get_cross_property()`.

*Changed in 0.55.0* putting `<lang>_args` and `<lang>_link_args` in the
properties section has been deprecated, and should be put in the built-in
options section.

### Project specific options

*New in 0.54.0*
*New in 0.55.0*

Path options are not allowed, those must be set in the `[paths]` section.

Being able to set project specific options in a native or cross files can be
Being able to set project specific options in a cross or native file can be
done using the `[project options]` section of the specific file (if doing a
cross build the options from the native file will be ignored)

For setting options in supbprojects use the `<subproject>:project options`
For setting options in subprojects use the `[<subproject>:project options]`
section instead.

```ini
Expand All @@ -179,6 +186,47 @@ build-tests = true
build-tests = false
```


### Meson built-in options

Meson built-in options can be set the same way:

```ini
[built-in options]
c_std = 'c99'
```

You can set some meson built-in options on a per-subproject basis, such as
`default_library` and `werror`. The order of precedence is:
1) Command line
2) Machine file
3) Build system definitions

```ini
[zlib:built-in options]
default_library = 'static'
werror = false
```

Options set on a per-subproject basis will inherit the
option from the parent if the parent has a setting but the subproject
doesn't, even when there is a default set meson language.

```ini
[built-in options]
default_library = 'static'
```

will make subprojects use default_library as static.

Some options can be set on a per-machine basis (in other words, the value of
the build machine can be different than the host machine in a cross compile).
In these cases the values from both a cross file and a native file are used.

An incomplete list of options is:
- pkg_config_path
- cmake_prefix_path

## Loading multiple machine files

Native files allow layering (cross files can be layered since meson 0.52.0).
Expand Down
17 changes: 16 additions & 1 deletion docs/markdown/snippets/project_options_in_machine_files.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## Project options can be set in native or cross files
## Project and built-in options can be set in native or cross files

A new set of sections has been added to the cross and native files, `[project
options]` and `[<subproject_name>:project options]`, where `subproject_name`
Expand Down Expand Up @@ -35,3 +35,18 @@ Subproject options are assigned like this:
[zlib:project options]
foo = 'some val'
```

Additionally meson level options can be set in the same way, using the
`[built-in options]` section.

```ini
[built-in options]
c_std = 'c99'
```

These options can also be set on a per-subproject basis, although only
`default_library` and `werror` can currently be set:
```ini
[zlib:built-in options]
default_library = 'static'
```
49 changes: 41 additions & 8 deletions mesonbuild/coredata.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,15 +361,15 @@ def __init__(self, options: argparse.Namespace, scratch_dir: str):
self.install_guid = str(uuid.uuid4()).upper()
self.target_guids = {}
self.version = version
self.builtins = {} # : OptionDictType
self.builtins = {} # type: OptionDictType
self.builtins_per_machine = PerMachine({}, {})
self.backend_options = {} # : OptionDictType
self.user_options = {} # : OptionDictType
self.backend_options = {} # type: OptionDictType
self.user_options = {} # type: OptionDictType
self.compiler_options = PerMachine(
defaultdict(dict),
defaultdict(dict),
) # : PerMachine[T.defaultdict[str, OptionDictType]]
self.base_options = {} # : OptionDictType
self.base_options = {} # type: OptionDictType
self.cross_files = self.__load_config_files(options, scratch_dir, 'cross')
self.compilers = PerMachine(OrderedDict(), OrderedDict())

Expand Down Expand Up @@ -743,19 +743,28 @@ def set_default_options(self, default_options: T.Mapping[str, str], subproject:
mlog.warning('Recommend using either -Dbuildtype or -Doptimization + -Ddebug. '
'Using both is redundant since they override each other. '
'See: https://mesonbuild.com/Builtin-options.html#build-type-options')

cmd_line_options = OrderedDict()
# Set project default_options as if they were passed to the cmdline.
# Set default options as if they were passed to the command line.
# Subprojects can only define default for user options and not yielding
# builtin option.
from . import optinterpreter
for k, v in default_options.items():
for k, v in chain(default_options.items(), env.meson_options.host.get('', {}).items()):
if subproject:
if (k not in builtin_options or builtin_options[k].yielding) \
and optinterpreter.is_invalid_name(k, log=False):
continue
k = subproject + ':' + k
cmd_line_options[k] = v

# IF the subproject options comes from a machine file, then we need to
# set the option as subproject:option
if subproject:
for k, v in env.meson_options.host.get(subproject, {}).items():
if (k not in builtin_options or builtin_options[k].yielding) \
and optinterpreter.is_invalid_name(k, log=False):
continue
cmd_line_options['{}:{}'.format(subproject, k)] = v

# load the values for user options out of the appropriate machine file,
# then overload the command line
for k, v in env.user_options.get(subproject, {}).items():
Expand All @@ -768,8 +777,32 @@ def set_default_options(self, default_options: T.Mapping[str, str], subproject:
if v is not None:
cmd_line_options[k] = v

from .compilers import all_languages
# Report that [properties]c_args
for lang in all_languages:
for args in ['{}_args'.format(lang), '{}_link_args'.format(lang)]:
msg = ('{} in the [properties] section of the machine file is deprecated, '
'use the [built-in options] section.')
if args in env.properties.host or args in env.properties.build:
mlog.deprecation(msg.format(args))

# Currently we don't support any options that are both per-subproject
# and per-machine, but when we do this will need to account for that.
# For cross builds we need to get the build specifc options
if env.meson_options.host != env.meson_options.build and subproject in env.meson_options.build:
for k in builtin_options_per_machine.keys():
if k in env.meson_options.build[subproject]:
cmd_line_options['build.{}'.format(k)] = env.meson_options.build[subproject][k]

# compiler options are always per-machine
for lang in all_languages:
prefix = '{}_'.format(lang)
for k in env.meson_options.build[subproject]:
if k.startswith(prefix):
cmd_line_options['build.{}'.format(k)] = env.meson_options.build[subproject][k]

# Override all the above defaults using the command-line arguments
# actually passed to us
# actually passed to use
cmd_line_options.update(env.cmd_line_options)
env.cmd_line_options = cmd_line_options

Expand Down
28 changes: 23 additions & 5 deletions mesonbuild/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,9 @@ def __init__(self, source_dir, build_dir, options):
# We only need one of these as project options are not per machine
user_options = {}

# meson builtin options, as passed through cross or native files
meson_options = PerMachineDefaultable()

## Setup build machine defaults

# Will be fully initialized later using compilers later.
Expand All @@ -568,14 +571,15 @@ def __init__(self, source_dir, build_dir, options):

## Read in native file(s) to override build machine configuration

def load_user_options():
def load_options(tag: str, store: T.Dict[str, T.Any]) -> None:
for section in config.keys():
if section.endswith('project options'):
if section.endswith(tag):
if ':' in section:
project = section.split(':')[0]
else:
project = ''
user_options[project] = config.get(section, {})
store[project] = config.get(section, {})


if self.coredata.config_files is not None:
config = coredata.parse_machine_files(self.coredata.config_files)
Expand All @@ -586,7 +590,9 @@ def load_user_options():
# Don't run this if there are any cross files, we don't want to use
# the native values if we're doing a cross build
if not self.coredata.cross_files:
load_user_options()
load_options('project options', user_options)
meson_options.build = {}
load_options('built-in options', meson_options.build)

## Read in cross file(s) to override host machine configuration

Expand All @@ -599,7 +605,9 @@ def load_user_options():
if 'target_machine' in config:
machines.target = MachineInfo.from_literal(config['target_machine'])
paths.host = Directories(**config.get('paths', {}))
load_user_options()
load_options('project options', user_options)
meson_options.host = {}
load_options('built-in options', meson_options.host)

## "freeze" now initialized configuration, and "save" to the class.

Expand All @@ -608,6 +616,16 @@ def load_user_options():
self.properties = properties.default_missing()
self.paths = paths.default_missing()
self.user_options = user_options
self.meson_options = meson_options.default_missing()

# Ensure that no paths are passed via built-in options:
if '' in self.meson_options.host:
for each in coredata.BUILTIN_DIR_OPTIONS.keys():
# These are not per-subdirectory and probably never will be
if each in self.meson_options.host['']:
raise EnvironmentException(
'Invalid entry {} in [built-in options] section. '
'Use the [paths] section instead.'.format(each))

exe_wrapper = self.lookup_binary_entry(MachineChoice.HOST, 'exe_wrapper')
if exe_wrapper is not None:
Expand Down
Loading

0 comments on commit bbba6a7

Please sign in to comment.