Build integration
DDD is not a tool somebody runs by hand before a release; it is a step of the build, in the same way the compiler is. The generated globals have to be regenerated whenever a component changes its declarations, the check has to fail the build rather than a review, and the a2l that goes out with an image has to be the one generated from the components that image actually links. Two integrations are shipped for that: a cmake module that turns the whole workflow into two calls, and a docker image that proves the generated c really compiles.
cmake
Which components an image is made of is already written down in the build system - it is the link graph. A project description that repeats that list by hand is a second source of truth, and it drifts the day somebody links a new component without remembering the json file. The cmake module therefore reads the list out of the link graph: a component registers its own description on its own target, and an image collects the descriptions of everything it links.
The module lives in cmake/Ddd.cmake and is found through CMAKE_MODULE_PATH. Because a
project may be built against several installations of the tool, the directory is printed by
the tool itself rather than guessed:
$ ddd cmake-dir
/home/you/ddd/cmake
find_program(DDD_EXECUTABLE NAMES ddd REQUIRED)
execute_process(COMMAND "${DDD_EXECUTABLE}" cmake-dir
OUTPUT_VARIABLE ddd_cmake_dir OUTPUT_STRIP_TRAILING_WHITESPACE
COMMAND_ERROR_IS_FATAL ANY)
list(APPEND CMAKE_MODULE_PATH "${ddd_cmake_dir}")
include(Ddd)
A project that has copied the module into its tree adds that directory instead, and the module then refuses a tool of another release than the copy (below).
Including the module looks for the ddd executable and remembers it in the cache variable
DDD_EXECUTABLE. Configure with -DDDD_EXECUTABLE=<path> to pin a particular
installation - the one of a virtual environment, or a small wrapper script running
python -m ddd. The module then asks it for its version and refuses, naming both, a tool
of another release than the module’s own: the two are one release, and a project that copied
Ddd.cmake into its tree - which the line above invites - otherwise finds out at build
time, in an unrecognized arguments from the tool’s argument parser, or does not find out
at all and builds under the option set of a release nobody is running. It asks once in every
configure, however many directories include it, and keeps the answer for that configure
alone: a tool upgraded where it stands - pip install -U into the environment the path
names - is another release at the same path, and the next configure refuses it the same way.
The module needs CMake
3.20 and says so at include time; collecting the
components through the link graph (below) needs CMake 3.30. A cross-compile toolchain file
that sets CMAKE_FIND_ROOT_PATH_MODE_PROGRAM ONLY keeps find_program from seeing host
tools, so such a project either allows host programs (BOTH) or passes
-DDDD_EXECUTABLE=<path> explicitly.
Registering a component
ddd_add_component(<target> JSON <file>...) attaches one or more description files to the
target of a component. The files must be named *.ddd.json and, unless they live in the
build tree and are generated later, must exist at configure time; a violation of either is a
fatal error, because the alternative is an image whose data dictionary is quietly incomplete.
A project description is not a component, and a file whose top-level key is project is refused
at configure time, where it exists then - one the build writes later passes, saying nothing of
its kind: only the file itself would be a dependency of the build, so an edit to what it
includes, or to a plugin it names, would build to no work to do over generated sources that
no longer match the project. The error reads ddd_add_component: "<file>" registered on target
"<target>" describes a project, and a component is not one. Name a project file with PROJECT of
ddd_generate(), which follows what it includes.
A vocabulary file registers the same way - a units, sections,
constants, rasters or types file is a *.ddd.json like any other, and the call takes it
without asking what is inside. Which target it belongs on is the question a project meets on
its first shared units file, and the answer follows from the collection: only what an image
links reaches it. A vocabulary the whole device shares therefore goes on a target every
image links - an interface library the components link for nothing else, or the image target
itself, whose own registrations are collected along with the closure’s:
add_library(device_vocabulary INTERFACE)
ddd_add_component(device_vocabulary JSON units.ddd.json sections.ddd.json)
add_library(sensor_hub STATIC sensor_hub.c)
target_link_libraries(sensor_hub PRIVATE device_vocabulary)
ddd_add_component(sensor_hub JSON sensor_hub.ddd.json)
Hanging it off whichever component happened to need it first works only for as long as every
image links that component - a condition nobody writes down, and one the first image that
does not link it discovers as an unknown-unit error.
Registering a component also creates the on-demand target <target>.ddd, which checks that
component on its own - useful long before it is integrated, and useful to a supplier who does
not have the rest of the project at all. That check runs ddd check --standalone, which
holds back the checks that need every component of the project: a component in isolation has
nobody on the other side of its interface, and the types, units, sections, constants and
rasters it names may live in files the supplier does not have. Which checks those are is
declared in the registry - ddd checks marks each one (project), and
Editor integration names the nine the editor holds back for the same reason - and
everything else, from datatypes and conversions to initial values and bitfields, is verified
as usual.
That check runs under the default severity policy, whatever the image asks for. One component
may be linked into several images, and ddd_add_component has no STRICT nor
SEVERITY keyword, so the target has no image whose policy it could take. A supplier’s check is
therefore not the build’s: a warning that the image’s STRICT turns into an error passes
it, and an error the image’s SEVERITY relaxes still fails it. The build decides; the
target tells a supplier what the component itself holds.
A registered vocabulary file is left out of that target: it declares no interface, so handing
one to ddd check is a file-kind error the target could never pass. Such a file is
checked in context instead, through the project of every image that collects it, and a target
that registers nothing else keeps a <target>.ddd that does nothing.
Which kind a file is is read at configure time, so a file whose json does not parse at all
says nothing about itself. It is checked like a component, because what is wrong with it is
exactly what that check reports: ninja <target>.ddd names the line and the column, and
the same target run after the repair checks the repaired file, without configuring again.
Neither does a file the build has yet to write into the build tree; the first build to find
it there configures again by itself and reads its kind (see The targets it creates below),
so a vocabulary the build writes is checked in context from then on.
Generating an image
ddd_generate(<image> ...) collects the descriptions of every component in the link closure
of the image, writes the project description tying them together, runs the generator - ddd
generate all, so the artefact of every plugin named with PLUGINS arrives beside the
built-in files - and links the result back into the image. The helper targets it creates are
named after the image without its extension, so firmware.elf gives
firmware_ddd_generation. A plugin’s file names are its own, known once the plugin has run,
so the generation does not declare them: a target reading one has to come after the
generation, as a component linking <stem>_ddd_headers does, and under Ninja that is not
enough, a change of the file reaching a component compiling it a build late (see The targets
it creates below). It has to be called in the CMakeLists.txt that defines the image, and
after the components have been added, because it hands <stem>_ddd_headers to the
components registered up to that point - which settles both which components get the
generated headers and whose compile usage travels with them.
In the collected mode the image also has to be linked to its components by the time that
directory is done: a component it reaches only through a link made, or a library defined,
later is a configure error naming the component, and so is one registered with
ddd_add_component() later (see The targets it creates below).
The a2l is the one artefact that cannot be complete before the link, which decides every
address it carries: with ADDRESSES_FROM_IMAGE or ADDRESS_MAP the module writes it after
the link instead, out of the linked image or out of a map extracted from it, and one build gives
it whole (see The a2l’s addresses below).
Besides the image it needs one thing: TEMPLATE_DIRECTORY, the directory of jinja2 templates
the generated c code is rendered from. It is required and has no default, because the
alternative would be a build whose generated sources change the day the tool is upgraded. What
that code looks like belongs to the project, so the project says where the templates are.
The example below is examples/cmake/CMakeLists.txt from the source distribution, with its
comments removed; it is what the cmake compose service configures and builds:
cmake_minimum_required(VERSION 3.30)
project(DddCMakeExample LANGUAGES C)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../../cmake")
include(Ddd)
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
if(NOT MSVC)
add_compile_options(-Wall -Wextra -Wpedantic -Werror)
else()
add_compile_options(/W4)
endif()
set(descriptions "${CMAKE_CURRENT_SOURCE_DIR}/../demo")
set(templates "${CMAKE_CURRENT_SOURCE_DIR}/../templates")
add_library(sensor_hub STATIC components/sensor_hub.c)
target_include_directories(sensor_hub PUBLIC "${descriptions}/include")
ddd_add_component(sensor_hub JSON "${descriptions}/components/sensor_hub.ddd.json")
add_library(controller STATIC components/controller.c)
target_link_libraries(controller PRIVATE sensor_hub)
ddd_add_component(controller JSON "${descriptions}/components/controller.ddd.json")
add_library(user_interface STATIC components/user_interface.c)
target_link_libraries(user_interface PRIVATE controller)
ddd_add_component(user_interface JSON "${descriptions}/components/user_interface.ddd.json")
add_library(event_logger STATIC components/event_logger.c)
ddd_add_component(event_logger JSON "${descriptions}/subsystems/logging/event_logger.ddd.json")
add_executable(firmware.elf main.c)
target_link_libraries(firmware.elf PRIVATE user_interface event_logger)
ddd_generate(firmware.elf
NAME DemoDevice
TEMPLATE_DIRECTORY "${templates}"
SCHEMA_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/schemas")
sensor_hub.c then writes #include "SensorHub.h" and nothing else: the header DDD
generated for that component is the one it is meant to include, and a variable it never
declared is one it has no business naming. The isolation is by convention rather than by
construction - what travels to each component is the output directory, one include path
carrying every component’s header and the shared ones beside them, so #include
"Controller.h" from another component’s source compiles. What the build does guarantee is
that the directory holds the headers of this image and no others: a component dropped from
the link graph stops being generated and its header is taken back at the next build (see
What a run owns), so the include that reaches across is at least an include of a
component the image really links. Handing each component a directory of its own would make
the convention a rule, at the price of one include directory per component; that is not what
this module builds. The include directory travels to the components automatically - and with
it, in the collected mode, the compile usage those headers need to be read - which is what
keeps the integration down to two lines per component.
That last part is what the demo’s external type is for. SensorHub declares one, so
sensor_hub is the component that publishes the directory holding the vendor header defining
it. DDD writes that header’s include line into ddd_types.h, and every component’s generated
header includes ddd_types.h - so event_logger compiles it too, although it neither
links sensor_hub nor names the type itself. Nothing hands that directory to
event_logger here. Take it out of what ddd_generate() collects and the build stops on a
header it cannot find.
The templates this example points at are the ones DDD ships as examples, since it sits next to them in the source tree. A real project keeps its own under version control, next to its sources, and starts them off as a copy of that set.
The project description that ties the collected components together is written into the output
directory as <NAME>.ddd.json. It is an ordinary project file, so what an image was
generated from can be read afterwards, and checked, dumped or compared like any other.
What the template directory decides
A build system has to know which files a step produces before it runs it, and now that the
templates come from the project, that list is written down in the project as well. The module
reads the template directory at configure time and derives everything it declares from the
names it finds there. The directory has to exist by then; ddd templates-dir prints a
working set to copy into a project that has none yet, and Templates describes what the
templates receive. There is no equivalent option for the a2l, and that asymmetry is deliberate:
the structure of an a2l is dictated by ASAM rather than by a house style, so that generator
stays internal.
The templates are collected with file(GLOB ... CONFIGURE_DEPENDS "<dir>/*.jinja2"), so a
template added or removed is noticed by the build itself instead of by whoever remembers to
re-run cmake; the directory is read as a directory, whatever characters it has, so a checkout
in pr[1] is no character class. The same files are dependencies of the generation step,
which makes a template behave like any other source: change the banner of
ddd_globals.c.jinja2 and the next build regenerates the file and recompiles it. So is every
*.jinja2 below the directory, which is never rendered and may be imported or included by
a template that is (see Templates), and so is the list of them, kept in the binary
directory of the call as <stem>_ddd_templates.txt and rewritten only when it differs: a
template removed leaves every other input of the generation as it was, and only a run of it
takes back the file that template rendered (see What a run owns).
The names of the templates are then turned into the declared outputs. A helper - a name
starting with an underscore, such as _macros.jinja2 - renders nothing of its own, and a
{component} template renders once per component, under the names the descriptions give
their components: its files are declared as byproducts of the generation, one for each
component of the image - each one it links, or with PROJECT each one the project names -
under the names read when the build is configured (see The targets it creates below).
Everything else contributes one output named like its template without the .jinja2
extension, and every output ending in .c is compiled into the object library that ends up
in the image - so a project splitting its definitions over two source templates gets both of
them compiled, without saying so anywhere.
Three arrangements are refused with a message rather than with a confusing failure later on: a
directory holding no *.jinja2 file at all, a directory holding nothing but helpers and
{component} templates, from which no output could be declared, and a directory in which no
template produces a .c file - the definitions of the global variables have to be compiled
into the image, so one template must produce a source file. For the same reason the directory
calling ddd_generate() has to have C enabled by the time it is done - by
project(<name> LANGUAGES C CXX), by an enable_language(C) in a directory above it
before it is added, or by one in it, before or after the call - since the object library
compiling those files is built there. One that has not is refused, naming the .c files, the
directory and the two ways to enable it: project(<name> LANGUAGES CXX) was left to CMake,
which cannot tell the linker language of that library and names it alone, and C enabled in a
sibling directory alone stopped CMake on a variable of its own that the directory lacks.
Collection follows the link graph
The description files are attached to a component target as a transitive usage requirement:
they travel through the link graph the same way an include directory or a compile definition
does. An image therefore collects exactly the components it links - no more, and, more
importantly, no less. An image linking only sensor_hub gets SensorHub.h and an a2l
holding SensorHub’s variables, and controller does not appear in it at all.
That mechanism is the custom transitive property DDD_JSON, built on the
TRANSITIVE_LINK_PROPERTIES feature introduced in CMake 3.30 - the floor of the
collected mode, on top of the CMake 3.20 the module itself needs. The module checks it and
stops with a fatal error on an older cmake instead of
carrying on, because an older cmake would silently collect an incomplete set of components,
and an incomplete data dictionary is exactly the failure DDD exists to prevent.
The property is resolved at generate time, when the link graph is known but nothing has been compiled yet, so the generation never waits for an object file. There is no dependency cycle either, even though the components depend on the headers generated out of their own descriptions: unlike a tool that reads compiled objects, DDD only ever reads the json files.
A hand written project description
A project that already maintains its own project description - because it is also built with
another build system, because it deliberately lists more than the image links, or because it
carries project-level extensions, the one key the generated description does not - passes
it with PROJECT <file>. That mode needs neither cmake 3.30 (3.20, the module’s own floor,
is enough) nor ddd_add_component, and the
a2l and the dictionary are then named after the project name inside the description, so a
NAME given as well is ignored with a status message. Without ddd_add_component nothing
hands a component <stem>_ddd_headers either - that is done for the components registered -
so a component that includes a generated header links it itself, target_link_libraries(
sensor_hub PRIVATE firmware_ddd_headers) for the image firmware.elf, or fails to compile
for want of the header.
A hand written project pulls its components in through includes, possibly with wildcards,
so the project file alone would be a wholly insufficient dependency. The module therefore asks
the tool which files the project is really built out of, with ddd sources (see
Command line interface), and uses the answer four times: as the dependencies of the
generation step, so that editing a component - or a plugin the project names, whose module
the answer lists too - regenerates the globals and the a2l; as CMAKE_CONFIGURE_DEPENDS, so
that editing any of the files runs configure again; as the files the names of the project’s
components are read from, for the headers the generation declares (see The targets it
creates below); and as a list kept in the build tree, <stem>_ddd_sources.txt in the
binary directory of the call, rewritten only when it differs, which the generation and the step
writing the a2l after the link depend on. Each includes entry, those of the projects it
includes among them, is also globbed with CONFIGURE_DEPENDS, as the templates are, so that
adding a file matching an includes wildcard re-runs configure and picks the new component
up, and removing one makes the next build regenerate, its inputs being fewer and its command
the same: the list is what tells it. A wildcard with ** is scanned below the directory
before its first wildcard at every build. A relative entry is read from the directory of the
file naming it, whatever characters that directory has. The list holds what the
project names whether or not it loaded: a .py plugin that no longer imports, or whose
model raises, is still listed, and so is a module named plugin, by the file the finder
locates for it, and every .py file of a package plugin, so the edit that repairs it
re-runs configure and the generation. What the tool says about the project on its standard
error - that plugin’s defect, a missing include - is printed by the configure as a warning,
graded by the SEVERITY and STRICT of the call, so a check the call relaxed is not
warned of. The project file itself is read when the build is configured - for the files it is
made of, its plugins and its name - so it has to exist, parse as json and name its project by
then: one that does not, a file the build has yet to write among them, is a configure error
naming it. If the tool cannot resolve a project that does all three - a root failing its
schema otherwise - the module warns and falls back to depending on the project file alone,
which stays a dependency of the configure, rather than refusing to configure at all.
What the build tells an editor
Each ddd_generate() writes a ddd-build.json into its output directory, at configure
time, with ddd build-info:
{
"format": 1,
"project": "/home/me/firmware/build/ddd/firmware.elf/firmware.ddd.json",
"image": "firmware.elf",
"strict": false,
"severity": ["unused-output=info"]
}
Nothing in the build reads it. It is written for the same reason SCHEMA_DIRECTORY writes
the json schemas at configure time - so that a tool outside the build can see what the build
sees - and it carries the two things no description file can state.
The first is which project description this image is generated from. In the collected mode
that file is written into the build tree out of the link graph, so the source tree does not
name it anywhere: a tool that reads only *.ddd.json cannot find out which components belong
together, because the answer is a property of the build rather than of any file somebody wrote.
A component linked into both a firmware and a test binary is in two projects, and the image
key is what tells the two records apart.
The second is the severity policy, from STRICT and SEVERITY. A tool that ignores it
reports a different set of findings than the build does, which is worse than reporting none:
the same working tree would be clean in one place and failing in the other. The options handed
to ddd build-info are the very list handed to ddd check and ddd generate, so those
three apply one policy. The <target>.ddd check of a component is not among them: it applies
the default policy, as described under “Registering a component”.
A description handed to ddd_generate() as PROJECT is said to be so: its record has
"origin": "given" and is stamped "format": 2. ddd gui edits the project of such a
record, even where OUTPUT_DIRECTORY is the description’s own directory and the record lies
beside it, and refuses to write into any other, a collected description being written again at
every configure. The record of a collected description, as the example above, has no such key and
keeps the shape of format 1: a record without the key is read as collected, so a project given
with a record of an older module beside it stays refused until the build is configured again. The
key is the one change to the record, and falls on PROJECT builds alone: a DDD from 0.11.0 on
declines a record of format 2 as written by a newer DDD and says so, and an older one skips it in
silence, as it skips any record it cannot validate.
The project description is named rather than read, because in the collected mode it does not
exist yet at that point - file(GENERATE) produces it at the end of the configure run, after
this file is written. A severity that names no known check is refused here, which is a
deliberate choice to fail the configure step where the typo is rather than the build step
where it would land.
The file is not named *.ddd.json: that extension means “a DDD description file”, the
file-extension check enforces it, and this is a document about a project rather than one.
The build watches whether it is there, all the same. It and the collected project description are what the configure writes into the output directory, and no step of the build writes them again, so a deleted output directory stopped a Ninja build on the description, missing and with no rule to make it, until somebody configured by hand. A build that finds the record gone now configures again first, which writes both back, and then generates the rest.
The targets it creates
The helper targets are named after the image without its file extension, because an image is
usually named like its artefact: firmware.elf yields firmware_ddd_headers.
The example above builds the graph below. The check and list targets are left out of it; everything else a build sees is there, and so is every edge between them:
The two arrows between the components and firmware_ddd_headers are what reduce the
integration to two lines per component, and they are not a cycle. The components link the
interface library; the interface library names their usage through $<TARGET_PROPERTY:...>,
which is read at generate time and visits each target once. It is also the picture of what the
propagation costs: the usage collected on the right reaches every component on the left,
including the ones this image happens not to link.
target |
what it is |
|---|---|
|
custom target running the generator. It depends on the project description and the
files collected into it, on the templates - every |
|
interface library carrying the include directory of the generated headers, and
depending on the generation. Every registered component links it, so a component
includes its interface header without knowing where the image put it. In the collected
mode it carries the interface compile usage of every registered component as well -
include directories, compile definitions and compile options, but never link edges - so
that a header an external type names is found and read the
way the component declaring it reads it. The flags matter as much as the paths: a hand
written header may change its layout under the component’s interface defines, and a
file compiled without them finds every header, compiles cleanly, and lays the variables
out differently than the image using them. Carrying it here is what keeps the
integration a two-liner, since linking this one target is then enough; the price is
that every registered component compiles under the union of those flags, including
components it does not link itself. |
|
object library compiling every generated |
|
runs |
|
runs |
|
with |
|
one per registered component, checking that component alone (see above). |
The outputs declared for the generator - under a Makefile generator the outputs of rules of
their own, beside a stamp (below) - are the files the template names already give away, plus
the a2l and the dictionary - the a2l being the output of <stem>_ddd_a2l’s step instead
where ADDRESSES_FROM_IMAGE or ADDRESS_MAP gives it one - and its byproducts are the
files of the {component} templates, one for each component of the image: in the collected
mode each component it links, with PROJECT each component the project names, whatever the
image links. Their names come from inside the description files, so the module reads them
when the build is configured: ddd_add_component reads the component name of each file it
registers, ddd_generate follows the image’s link graph to the components it links once
the directory calling it is done, and with PROJECT the names are read off the files
ddd sources lists. A link the image makes behind $<CONFIG:...> alone -
$<$<CONFIG:Debug>:trace>, say - is read for the CMAKE_BUILD_TYPE the build is
configured with, as CMake reads it, a single-config generator having that one configuration:
a Release image does not link trace, and declares no header of it. A link behind any other
generator expression - one inside that $<CONFIG:...> among them - cannot be evaluated
when the names are read, so every component it names
is declared, whatever the expression comes to; under Ninja one whose header the generation
then never writes makes the generation run at every build. The image may be linked after the
call, in that directory or in one it adds, and not later: a component it reaches only through
a link made, or a library defined, once that directory is done would have a header the
generation does not declare, so the configure stops at it, once the top directory is done,
naming the component - and so it does at a component registered with ddd_add_component()
once that directory is done, on a target the image linked in time, naming it as registered
late.
Declaring the headers is what makes the build after a change right under Ninja, which looks at the files a build reads before it runs anything: a header no step declares was looked at before the generation rewrote it, so the build after a datatype change compiled the components reading it against the old header and linked them with the new globals, and only the build after that one compiled them again. A declared header is looked at again once the generation has run, and since an unchanged file is left untouched, the components whose header changed are recompiled and no others.
A plugin’s files are the exception. Their names are the plugin’s own, known once it has run, so
nothing declares them, and under Ninja a change of one reaches a component compiling it a build
late: ninja looked at the file before the generation rewrote it, however the component is
ordered after the generation. A description change that reaches a plugin’s file reaches the
dictionary as well, which the generation declares, so a project making the component’s object
depend on the dictionary - set_source_files_properties(<file> PROPERTIES OBJECT_DEPENDS
<dictionary>), the path being the image’s DDD_DICTIONARY property - has such a change
reach the component in one build, as does an edit of the plugin’s model, which the dictionary
carries. An edit of the plugin’s own code that changes only what it writes, and not its model,
leaves the dictionary as it was, and reaches the component a build late all the same. Under a
Makefile generator the build after either is right. A clean leaves a plugin’s files,
ninja -t clean and make clean alike.
A Makefile generator is supported as well, and looks at no file again once a step has run. A
run that changed nothing left the first file of the generation older than the description
that ran it, so every build after ran the whole analysis again, and CMake touched every other
file of the step after each run - ddd_types.h among them, which every component header
includes, so every component compiled again. Under a Makefile generator each of the module’s
two steps - the generation, and the a2l’s after the link - has a stamp in the output directory
for its one output, ddd-generation.stamp and ddd-a2l.stamp, touched by every run that
succeeds: a run that changed nothing leaves the build after it nothing to do, and the
components whose header changed are recompiled and no others, as under Ninja.
Make knows a file only as the output of a rule, so each file a step of the project may read by
its path - the a2l, the dictionary and the files the template names give away - is the output of
a rule of its own there, which does nothing and depends on <stem>_ddd_generation, or for the
a2l written after the link on <stem>_ddd_a2l. A step depending on such a file by its path,
in the directory calling ddd_generate(), therefore runs once the generation has, under
make -j too, and reads what it wrote, as under Ninja. A {component} header has no such
rule, nor has a plugin’s file, and a file read from another directory is known to make by no
rule there either: a step reading one depends on <stem>_ddd_generation
(add_dependencies), as it would have to under make in any case.
A file deleted from the output directory by hand is not written again under make until the
step runs for another reason - an edit, a clean - and every build reading the file stops on it
until then: ddd_types.h, ddd_globals.h, the definitions, the dictionary and the a2l
here, as a {component} header did before. Delete ddd-generation.stamp, or
ddd-a2l.stamp, beside it, or run make clean, and the next build writes it again. A
deleted output directory is noticed without either (see What the build tells an editor
above).
The names are read at configure time, so a component renamed in its description needs a
reconfigure: until then its header is not declared under the new name, so under Ninja the
generation runs at every build for the header of the old one, which it no longer writes, and a
change of the new one reaches the components reading it a build late, as an undeclared header
did (above). With PROJECT the configure runs again by itself, every file of the project
being one of its dependencies. A registered description whose json does not parse when the
build is configured names no component, and is a dependency of the configure until it does, so
the build after its repair configures again. One generated into the build tree later names its
component only from the first configure that finds it, and CMake takes no configure dependency
on a file the build writes, so it is watched instead, as a wildcard of includes is: the
first build to find it there configures again before it does anything else, and that configure
reads the component it describes, or that it is a vocabulary. Its header is therefore declared
before a change of it can reach a reader. The configure that does not find it says so, naming
the file.
A header whose component has left the image is another matter: once the image no longer links
the component, nothing declares the header and the build leaves it where it is, which is why
ddd generate cleans it itself. It owns its output directory, records what it wrote there
and removes, on the next run, the files it no longer writes (see What a run owns).
Dropping a component from target_link_libraries therefore takes its header out of the
include path of every other component at the next build, and ninja -t clean followed by a
build leaves what a build from nothing leaves - the record survives the clean, being declared
by nothing.
The path of the generated a2l is published as the DDD_A2L property of the image, so that a
post-build step can pick it up without rebuilding the path by hand:
get_target_property(a2l firmware.elf DDD_A2L)
install(FILES "${a2l}" DESTINATION delivery)
Where ADDRESSES_FROM_IMAGE or ADDRESS_MAP has the a2l written after the link, a step
that reads it in the course of the build depends on <stem>_ddd_a2l, or on the a2l by its
path in the directory calling ddd_generate(), which comes to the same under either
generator (see the Makefile generators above): a POST_BUILD command of the image runs
before that target’s step, and would read the a2l of the build before, which a first build
on a clean tree has none of.
Beside the artefacts the generation step writes <project name>.dictionary.json - the
name the a2l takes, which beside PROJECT is the one written inside that file rather than
NAME - the resolved
data dictionary they were all generated from: what a template author
reads to see what the templates receive, and what a delivery archives for a later
ddd compare (see Comparing deliveries). The generation writes it itself, with
ddd generate --dictionary, in the same write as the artefacts: one analysis and one report
of the findings, and a run that fails its checks writes none of them, so the last dictionary
keeps describing the artefacts still beside it; a regeneration that changes nothing in it
leaves the file untouched. Its path is the DDD_DICTIONARY property of the image, read the
same way - a step copying it, depending on that path in the directory calling
ddd_generate(), runs once the generation has under a Makefile generator too, the file having
a rule of its own there (above) - and NO_DICTIONARY leaves the file and the property out.
Note
Multi-config generators are refused with a fatal error: the project description and the generated sources are written to configuration-agnostic paths, which the configurations would fight over. Use a single-config generator: Ninja, or a Makefile generator.
Options
option |
meaning |
|---|---|
|
use this project description instead of collecting the link closure. It is read when the build is configured, so it has to exist, parse and name its project by then. |
|
the plugins of the collected project: a |
|
project name, and therefore the name of the a2l file and of the dictionary beside it.
Defaults to the image name without its extension, with anything that is not a c
identifier replaced, because the name ends up as the a2l project and module name.
Ignored together with |
|
where the generated files go; defaults to |
|
required: the jinja2 templates the c sources are rendered from. Their names decide which files are generated, and renaming a template is how a project renames a generated file. |
|
write the json schemas of the file formats into this directory at configure time, for
editor validation; they are rewritten on every configure, so they cannot describe a
version of DDD that is no longer installed. They are closed over the project’s plugins
- the |
|
write the a2l once the image is linked, every address it carries read out of this
symbol to address map, which a step of the project’s own extracts from the image after
the link (below): the run before the link leaves the a2l out, as |
|
write the a2l once the image is linked, every address it carries read out of the
image’s debug information, so that one build gives the complete a2l: the run before the
link leaves the a2l out, as |
|
byte order reported in the a2l. |
|
a text file pasted unchanged at the head of the a2l’s |
|
severity overrides, exactly like |
|
usage requirements for compiling the generated definition file, stated by hand. The
manual fallback: in the collected mode |
|
additional files that retrigger the generation. |
|
do not generate the a2l file; no |
|
do not write the resolved data dictionary beside the artefacts; no |
|
treat DDD warnings as errors. |
|
do not hand |
A keyword given no value is a fatal error naming it, in both calls. ADDRESS_MAP
${DDD_MAP} with DDD_MAP unset or empty - the ordinary CMake mistake - reads to
cmake_parse_arguments() exactly like a keyword nobody gave, so it used to be dropped in
silence: the a2l came out with every address 0x00000000, no map was seeded and none was a
dependency, and the map below was never read. PROJECT without a value fell into
the collected mode and generated out of the link graph instead of out of the file the caller
meant.
The quoted form, ADDRESS_MAP "${DDD_MAP}", which every recipe on this page writes, is
refused the same way: a keyword followed by an empty argument has no value either, from CMake
3.20 on. So is an empty argument among a keyword’s values, wherever it is, in either call and
for every keyword taking several: SEVERITY "unused-output=info" "${RAISE}" with RAISE
mistyped would otherwise drop out of the list without a word and leave the policy weaker than
the call says. The error names the keyword, saying that it was given an empty value among its
values.
NO_PROPAGATE_HEADERS is the option a project building several images from the same
components cannot avoid. A component’s interface header is generated for one link closure, so
two images produce two different sets of headers for the same component, and whichever include
directory reached it first would silently decide which set it compiles against - and, since
<stem>_ddd_headers carries the components’ compile usage too, under which flags. Rather
than letting an include order settle that, the second ddd_generate() stops the configure
step with a fatal error. Such a project gives NO_PROPAGATE_HEADERS to both calls and links
the wanted <stem>_ddd_headers into each component explicitly - opting out of only one of
the two would leave the same ambiguity in place, because the automatic set still reaches every
registered component rather than only the ones that image links.
The a2l’s addresses
The address of every object the a2l carries is decided by the linker, so the a2l is the one
artefact a build cannot finish before the link. ADDRESSES_FROM_IMAGE gives it a step of its
own after the link: the generation before the link leaves the a2l out, as NO_A2L does, and
<stem>_ddd_a2l, built by default, runs ddd generate a2l --image over the linked image,
which reads every address the a2l carries out of the image’s debug information - an object by
its name, a structure member by its access path, at the offset the compiler gave it. One build
gives the complete a2l, with no map to extract and no member offset to work out by hand:
ddd_generate(firmware.elf
TEMPLATE_DIRECTORY "${templates}"
ADDRESSES_FROM_IMAGE)
The image has to be a linked ELF image whose DWARF describes the variables
(Command line interface), which a build with debug information writes: -g, which
CMake’s Debug and RelWithDebInfo build types add. The step depends on the image and on
what the project is read from, so it runs again when the image relinks or a description
changes - a conversion’s factor, which reaches the a2l and none of the c, included - and a
build with nothing changed runs nothing at all. It writes the a2l into the
directory the generation before the link wrote the c sources, the headers and the dictionary
into, and takes none of them back (What a run owns), so nothing is compiled again
because the a2l was written.
An image linked with gcc’s link-time optimisation (-flto) cannot be read this way, and the
step stops on it, exit 2, with “its DWARF comes from gcc’s link-time optimisation, which this
tool does not read; build it without -flto”. A project that links with -flto and keeps it
takes its addresses from a map instead (ADDRESS_MAP, below), with the offset of each
structure member worked out beside it, since the a2l has to describe the image that is
flashed.
The step holds the image to the description as well. Each variable it places is compared with
its object on the size and encoding of an element, its shape and the qualifiers the generated c
gives its kind, and one that differs is a warning, elf-mismatch, saying what the description
states and what the image holds. The definition is held on every image: a uint32_t defined
for a uint16, or a template that leaves out a const or a volatile the kind carries
there, is caught in the c that was compiled. A consumer’s declaration is held to the qualifiers
of its kind where the compiler records it in the debug information, which gcc does for a
variable a unit reads and clang does not, so a component header that leaves out volatile is
caught on a gcc image and not on a clang one. Under STRICT the build stops at it.
What the image cannot place keeps address 0 and is reported once, as address-missing, each
symbol’s reason a note beneath it; under STRICT that is an error, and the build stops rather
than ship an a2l with an address of 0. An object that states a condition, and of which the
image defines nothing at all, is not reported that way: the build was made without it, and the
a2l leaves it out. The example above, built as RelWithDebInfo with the keyword added,
places every object of the demo but ValueG, which a condition compiles out of this image.
Built where gcc links position independent by default, as it does on a Debian or Ubuntu host, it
prints:
firmware.elf: info[image-load-relative]: the image is position independent, so the addresses the a2l states are offsets from where it loads
note: a project that adds its load base when it reads the a2l is right, and one that does not finds every address wrong
firmware.elf: info[condition-left-out]: the image has no address for 'ValueG', which is only present in the build when 'defined(FEATURE_X)'; it is left out of the a2l
note: the image's debug information holds no variable named 'ValueG'
2 infos
The first is image-load-relative: a position independent image holds offsets from where it
loads, which the a2l states as they are, right for a project that adds its load base when it
reads the a2l and wrong for any other, and the build of an image linked at fixed addresses, as
an embedded one usually is or as -no-pie links this one, prints the second alone. STRICT
lets both through, being information: the a2l has no record of ValueG, so there is no object
left at address 0 for it to stop at. BYTE_ORDER is held to the byte order the image states:
one that contradicts it stops the build as well. So does an image the reader cannot read, in the reader’s own words - one
built without -g is told to be, and one that is not ELF at all, as a host build on Windows
links, is refused as such. A build like that reads an address map instead, in the same step
(below).
Where the address map comes from
ADDRESS_MAP names a file; writing it is the project’s step. DDD ships no extractor and
runs no toolchain tool of its own, and the generation before the link reads no build output at
all, which is what lets it run before anything has been compiled; with a map, reading the
linked image is left to the build. What it needs is the json Generated artefacts
describes, one flat object of symbol to address; where that comes from is the project’s
business, so a toolchain without nm costs nothing but the recipe below.
The step belongs after the link, so it is a POST_BUILD command on the image, writing into
the very path ADDRESS_MAP names:
set(address_map "${CMAKE_CURRENT_BINARY_DIR}/ddd/firmware.elf/addresses.json")
ddd_generate(firmware.elf
TEMPLATE_DIRECTORY "${templates}"
ADDRESS_MAP "${address_map}")
add_custom_command(TARGET firmware.elf POST_BUILD
COMMAND "${CMAKE_COMMAND}"
-D "NM=${CMAKE_NM}"
-D "IMAGE=$<TARGET_FILE:firmware.elf>"
-D "OUTPUT=${address_map}"
-P "${CMAKE_CURRENT_SOURCE_DIR}/cmake/AddressMap.cmake"
COMMENT "Extracting the address map of firmware.elf"
VERBATIM)
The script it runs is where the toolchain shows through, and it is an example to adapt
rather than a file to copy: this one reads the nm of binutils, and a toolchain whose
symbol lister prints something else needs its own reader of that output.
# cmake/AddressMap.cmake - NM, IMAGE and OUTPUT come from the -D arguments above.
execute_process(COMMAND "${NM}" --defined-only --extern-only --format=posix "${IMAGE}"
OUTPUT_VARIABLE listing
COMMAND_ERROR_IS_FATAL ANY)
string(REGEX REPLACE "\r?\n" ";" lines "${listing}")
set(entries "")
foreach(line IN LISTS lines)
# "<name> <type> <address> <size>", the type letter saying which section it landed in.
if(line MATCHES "^([A-Za-z_][A-Za-z0-9_]*) [BbDdGgRrSs] ([0-9A-Fa-f]+)")
list(APPEND entries " \"${CMAKE_MATCH_1}\": \"0x${CMAKE_MATCH_2}\"")
endif()
endforeach()
list(JOIN entries ",\n" body)
file(WRITE "${OUTPUT}" "{\n${body}\n}\n")
The map is read after the link, as ADDRESSES_FROM_IMAGE reads the image: the generation
before the link leaves the a2l out, and <stem>_ddd_a2l, built by default, runs ddd
generate a2l --address-map once the image is linked. A POST_BUILD command runs as part of
the link, before anything that depends on the image, so the step reads the map this very link
extracted, and the first build of a clean tree gives the a2l of the image it linked. Read
before the link, the map was the one the link before had extracted: the a2l held the addresses
of the image before this one, every one of them 0 on a first build, and under STRICT an
object new to a description, which that map could not name, stopped every build before the
link that would have named it. The step runs again whenever the image relinks, the map changes
or a description does - a map written by hand, or by a step that does not run as part of the
link, has to be written before it runs - and it writes the a2l into the directory the
generation before the link wrote, taking none of its files back (What a run owns). The
build after it has nothing to do: nothing is extracted, read, compiled or linked again.
--extern-only keeps the file-local statics out: every object a dictionary describes is a
global, and two translation units each defining a static of one name would otherwise hand
the map one symbol at two addresses, which load_address_map refuses.
Two things such a script has to get right. A structured object’s members are addressed under
their access path, Inlet.latest rather than Inlet, which a symbol lister does not
print: a project with structured objects adds the member offsets itself, from the type
description or from the debug information - which is what ADDRESSES_FROM_IMAGE reads, for an
image that carries it. And the addresses of the objects DDD does know
have to fit the 0 .. 0xFFFFFFFF of an ECU_ADDRESS, which a host build of an embedded
project runs into first: a 64 bit image is based above 4 GB, and no a2l can describe it. An
entry for a symbol DDD does not know is neither weighed that way nor written anywhere, so
listing the whole image does no harm; those entries are named in the note under
address-missing, where a stale or renamed symbol is read beside the object it belongs to.
docker
Generated c code is only worth something if a compiler accepts it, and “it compiled on my machine” is not a statement anybody can act on. The repository therefore ships a small linux image whose whole purpose is to generate, compile, link and inspect the result on a defined toolchain, and a compose file that gives every routine job a name.
The image (docker/Dockerfile) is python:3.12-slim-bookworm with gcc and libc6-dev to
compile the generated sources, and binutils for the nm that inspects them afterwards. DDD
itself is installed with its development extra, which is also where the cmake and the ninja
that build the cmake example come from: both are wheels from pypi rather than debian packages,
because debian bookworm still ships cmake 3.25 and the module needs 3.30. docker/compile.sh
is installed as the command ddd-compile. The image serves ddd gui too: its pages are
compiled in an earlier stage of the same file, which has Node.js and is thrown away, and only
the pages reach the image, installed with DDD.
Note
The image is a linux image, so on a Windows host run docker from a WSL shell, where docker speaks linux containers:
wsl -d Ubuntu
cd /mnt/c/path/to/ddd # the working tree, seen from inside WSL
docker compose build
docker compose run --rm check # ddd check on the demo project
docker compose run --rm generate # ddd generate into build/gen
docker compose run --rm compile # generate + compile + link + verify
docker compose run --rm cmake # configure and build examples/cmake
docker compose run --rm test # pytest with the coverage gate
docker compose run --rm coverage # the same, plus build/htmlcov/index.html
docker compose run --rm lint # ruff check, ruff format --check, mypy
docker compose run --rm docs # this documentation, into build/docs/html
docker compose run --rm ddd ddd list examples/demo/demo.ddd.json
The working tree is bind mounted at /work and PYTHONPATH=/work/src makes it shadow the
copy installed into the image, so a change to the sources takes effect without rebuilding
anything. The docs service runs sphinx-build with -W on the documentation
requirements the image carries, installing nothing, so a warning - a broken cross reference, a
directive that does not render - fails the build rather than producing a page nobody looks at
twice. There is also a shell service, which is the same container with an interactive bash
in it.
The pages of ddd gui are shadowed with the rest: a service serves the ones compiled in the
working tree, which git ignores, so over a checkout that never compiled them ddd gui refuses
to start there, and the pages the image carries are what a container run without that
PYTHONPATH serves. Either way it answers on the loopback address of the container by default,
which a browser outside the container reaches only if the container shares the host’s network.
The gui service is the exception: it clears PYTHONPATH, so it runs the image’s own code
and serves the pages it carries rather than the working tree’s, and it passes --host 0.0.0.0
so it answers beyond its own loopback. The project it opens is still the checkout’s, though:
examples/demo is bind mounted under /work like every service’s sources, so an edit made
in the browser is written into the developer’s own checkout there, the file keeping its owner and
permissions although this service runs as root, as every service does. docker compose up
gui builds the image first - the code it runs is the image’s, so an image built before a
change would serve what it was built from - and starts it, publishing the same port number on
the host’s loopback alone, -p 127.0.0.1:8123:8123 - a different number outside would have
the Host header ddd gui sees name a port it is not listening on, answered
421 misdirected request, and beyond the container’s loopback the token in the address it prints
is the only guard, which is why nothing wider is published. Opening that address in a browser on
the host is the image’s own ddd gui serving the checkout’s demo project.
What the compile service proves
compile runs docker/compile.sh, which is deliberately more suspicious than a plain
build:
it generates the demo project into
build/genwith the templates named byTEMPLATES, the examples shipped with the tool unless the caller says otherwise, and writes the resolved dictionary next to it withddd dump --format json;it writes one translation unit per generated header which includes that header twice, which proves both that every header is self contained - it compiles with nothing included before it - and that its include guard works;
it compiles everything with
-std=c11 -Wall -Wextra -Wpedantic -Werror -Wconversion -Wshadow -Wcast-qual -Wstrict-prototypes, so a conversion the generator got wrong is a compile error and not a silent truncation on the target;it links all the objects into one binary and runs it, which is where a duplicated definition or a declaration without a definition behind it would show up - the link step is what actually tests the promise that every variable is defined exactly once;
it compares the output of
nmon the generated definition file against the dictionary from step 1 (docker/verify_symbols.py): every declared variable must be defined exactly once, nothing that DDD never declared may be defined, and a variable behind a preprocessor condition is allowed to be absent and is reported as such.
Steps 2 to 5 run twice, once plain and once with the extra defines from the CDEFS
environment variable - -DFEATURE_X in the shipped compose file - so that conditional
declarations are covered in both of their states.
The dictionary rather than ddd list in step 1, because the two count different things: the
definition file defines one symbol per plain object and one per structured instance, while
the list reports what can be described, which for a structured object is its leaves - and a
structure whose members are all external types has storage the linker sees and no leaf at all.
The script takes the project and the output directory as arguments, so it also runs on a real
project rather than only on the demo, and the environment variables CDEFS, GENFLAGS,
TEMPLATES, CFLAGS, CC and INCLUDES change the defines, the ddd generate
flags, the templates, the warning set, the compiler, and where the headers of the project’s
external types are looked for on top of the nearest include directory:
docker compose run --rm compile ddd-compile path/to/project.ddd.json build/mine
docker compose run --rm -e TEMPLATES=path/to/templates compile \
ddd-compile path/to/project.ddd.json build/mine
TEMPLATES defaults to the output of ddd templates-dir, which is what makes the plain
invocation work at all - the generator itself has no templates to fall back on. The second form
is the interesting one for a real project: it answers whether the code that project is about to
ship compiles, links and defines every symbol it promised, and that is a question the example
templates cannot answer on its behalf.
Warning
The container runs as root, so files it writes under build/ belong to root when the
mount is a real linux filesystem. The base images are also still referenced by tag -
python’s, and node’s for the stage that compiles the pages: pin them to digests before a
result from the image is used to release something, as the comment at the top of
docker/Dockerfile describes.
pre-commit
An identity only does its job if every object has one, and the moment a project forgets is the
moment somebody adds an object without one - which nothing notices until a rename two releases
later reads as a removal and an addition. ddd id --assign closes that gap from the command
line; a pre-commit hook closes it without anyone remembering to.
This repository publishes the hook, so a project that uses DDD adds it by naming this repository rather than writing an invocation of its own:
repos:
- repo: https://github.com/Sauci/ddd
rev: <the release you pin>
hooks:
- id: ddd-id
pre-commit passes the staged *.ddd.json files, and the hook stamps an id into every
producing declaration that has none. A project, types or units file among them is a no-op: only
a component file declares data objects.
The hook is a language: python hook, so pre-commit builds it an environment of its own out
of whichever interpreter it finds, and DDD needs Python 3.12 or newer. On a machine whose
default python3 is older - Ubuntu 22.04 ships 3.10 - pre-commit install-hooks fails
inside pip with “requires a different Python” and names no file of yours. Pin the interpreter
in the hook entry rather than in the machine:
repos:
- repo: https://github.com/Sauci/ddd
rev: <the release you pin>
hooks:
- id: ddd-id
language_version: python3.12
The commit then fails, and that is the intended behaviour. pre-commit reports files were
modified by this hook whenever a hook changes something on disk, even when the hook itself
succeeded. So the new ids arrive in the working tree for their author to read and stage, rather
than in a commit nobody reviewed - which is the same bargain ddd id --assign is built on:
the tool proposes, the diff is reviewed, a git checkout undoes it. Run git add and
commit again.
This is the one part of DDD that assumes git, and only because pre-commit is a git mechanism. The tool itself reads no repository and knows nothing about version control.