Skip to content

Update documentation for target definitions #398

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 11 commits into from
60 changes: 50 additions & 10 deletions docs/tools/mbed_targets.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Arm Mbed uses JSON as a description language for its build targets. You can find

You are not allowed to redefine existing targets in `custom_targets.json`. To better understand how a target is defined, we'll use this example (taken from `targets.json`):

```
```json
"TEENSY3_1": {
"inherits": ["Target"],
"core": "Cortex-M4",
Expand All @@ -20,6 +20,7 @@ You are not allowed to redefine existing targets in `custom_targets.json`. To be
},
"device_name": "MK20DX256xxx7",
"detect_code": ["0230"]
}
```

The style that we use for targets is:
Expand All @@ -37,7 +38,7 @@ This section lists all the properties the Mbed build system understands. Unless

The description of an Mbed target can "inherit" from one or more descriptions of other targets. When a target, called a _child_ inherits from another target, called its _parent_, the child automatically copies all the properties from the parent. After the child has copied the properties of the parent, it may then overwrite, add or remove from those properties. In our example above, `TEENSY3_1` inherits from `Target`. This is the definition of `Target`:

```
```json
"Target": {
"core": null,
"default_toolchain": "ARM",
Expand All @@ -59,7 +60,7 @@ A child target may add properties that don't exist in any of its parents. For ex

It's possible, but discouraged, to inherit from more than one target. For example:

```
```json
"ImaginaryTarget": {
"inherits": ["Target", "TEENSY3_1"]
}
Expand Down Expand Up @@ -103,7 +104,7 @@ When a target inherits, it's possible to alter the values of `macros` in the chi

For example:

```
```json
"TargetA": {
"macros": ["PARENT_MACRO1", "PARENT_MACRO2"]
},
Expand Down Expand Up @@ -148,6 +149,45 @@ The build system errors when you use features outside of this list.

When you use target inheritance, you may alter the values of `features` using `features_add` and `features_remove`. This is similar to the `macros_add` and `macros_remove` mechanism the previous section describes.

#### `config` and `overrides`

The list of _configs_ provide a way to modify the values of macros in child targets or in a project. Each configuration has a macro name, as well as a default value and an optional help value. For example:

```json
"config": {
"clock_src": {
"help": "Clock source to use, can be XTAL or RC",
"value": "XTAL",
"macro_name": "CLOCK_SRC"
}
}
```

This case defines the config `clock_src`, for the `CLOCK_SRC` macro, and has a default value of `XTAL`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't be easier to show here default naming convention for config and also mention that macro_name overrides it ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, I didnt even realize it defaults to the config name.


_overrides_ allow a child target to change the value of a config. For example, if a child target uses the internal RC clock instead of the crystal, it can add an override:

```json
"overrides": {
"clock_src": "RC"
}
```

Config values can also be modified for a project using the `target_overrides` key in the `mbed_app.json` file, either for specific targets or as a wildcard. For example:

```json
"target_overrides": {
"*": {
"config1": "value_for_all_targets"
},
"NRF51_DK": {
"config2": "value_for_single_target"
}
}
```

This section, in an `mbed_app.json` file, will set a value for `config1` on all targets, as well as a value for `config2` only on the `NRF51_DK` target.

#### `device_has`

The list in `device_has` defines what hardware a device has.
Expand All @@ -164,9 +204,9 @@ The `default_toolchain` property names the toolchain that compiles code for this

#### `post_binary_hook`

Some targets require specific actions to generate a programmable binary image. Specify these actions using the `post_binary_hook` property and custom Python code. The value of `post_binary_hook` must be a JSON object with keys `function` and optionally `toolchain`. Within the `post_binary_hook` JSON object, the `function` key must contain a Python function that is accessible from the namespace of `tools/targets/__init__.py`, and the optional `toolchain` key must contain a list of toolchains that require processing from the `post_binary_hook`. When you do not specify the `toolchains` key for a `post_binary_hook`, you can assume the `post_binary_hook` applies to all toolcahins. For the `TEENSY3_1` target above, the definition of `post_binary_hook` looks like this:
Some targets require specific actions to generate a programmable binary image. Specify these actions using the `post_binary_hook` property and custom Python code. The value of `post_binary_hook` must be a JSON object with keys `function` and optionally `toolchain`. Within the `post_binary_hook` JSON object, the `function` key must contain a Python function that is accessible from the namespace of `tools/targets/__init__.py`, and the optional `toolchain` key must contain a list of toolchains that require processing from the `post_binary_hook`. When you do not specify the `toolchains` key for a `post_binary_hook`, you can assume the `post_binary_hook` applies to all toolchains. For the `TEENSY3_1` target above, the definition of `post_binary_hook` looks like this:

```
```json
"post_binary_hook": {
"function": "TEENSY3_1Code.binary_hook",
"toolchains": ["ARM_STD", "ARM_MICRO", "GCC_ARM"]
Expand All @@ -179,7 +219,7 @@ The build tools call `TEENSY3_1` `post_binary_hook` when they build using the `A

As for the `TEENSY3_1` code, this is how it looks in `tools/targets/__init__.py`:

```
```python
class TEENSY3_1Code:
@staticmethod
def binary_hook(t_self, resources, elf, binf):
Expand Down Expand Up @@ -290,9 +330,9 @@ For each of these target roles, some restrictions are in place:
- `extra_labels`.
- `public`.
- `config`.
- `override`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't this overrides (s missing in here) ?

- `forced_reset_timeout`.
- `target_overrides`
- `macros` are not used. That is intentional: they do not provide any benefit over `config` and `target_overrides` but can be very difficult to use. In practice it is very difficult to override the value of a macro with a value. `config` and `target_overries`, on the other hand, are designed for this use case.
- `macros` are not used. That is intentional: they do not provide any benefit over `config` and `overrides` but can be very difficult to use. In practice it is very difficult to override the value of a macro with a value. `config` and `overrides`, on the other hand, are designed for this use case.
- `extra_labels` may not contain any target names
- `device_has` may only contain values from the following list:
- `ANALOGIN`.
Expand Down Expand Up @@ -361,7 +401,7 @@ target errors:
---
hierarchy: Family (LPCTarget) -> MCU (LPC11U24_301) -> ???
hierarchy errors:
- no boards found in heirarchy
- no boards found in hierarchy
target errors:
LPC11U24_301:
- release_versions not found, and is required
Expand Down