Best-practices for providing an external config header to a dependency #14285
Unanswered
mitchgrout
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
It's not uncommon to have a library that expects a special user-supplied configuration header to be present. This is particular prevalent within embedded libraries, for example FreeRTOS' FreeRTOSConfig.h file.
In older projects, we usually see these in-tree with the project code itself. I'm personally opposed to this as it makes it difficult to properly separate out ours vs theirs, and can cause irritation with tooling such as linters trying to run on thirdparty code.
I've got quite a few libraries like this, each needing their own special set of header files, and sometimes even source files in order to compile. I treat these as external dependencies, providing each their own
.wrap
files. I've trialled a few strategies for making this work, which I've detailed below:1.
packagefiles/
Simply place all the customization files you need in here, and adjust the
.wrap
file to reference the directory.The main downside with this is,
meson
will not re-apply any files after the subproject has been downloaded,so any modifications made to the
packagesfiles/
directory will only be replicated after you eithermanually copy it across, or nuke the subproject. This absolutely sucks in a collaborative environment,
and I don't use it for this reason.
2.
<x>-config
dependenciesEnsure that each library accepts a dependency named
<x>-config
, where<x>
is the project name.For example, for FreeRTOS, have
FreeRTOS-config
, which can be defined in the consuming project via:This resolves the main problem with option 1, as you can freely edit the sources and headers in-tree,
and the subproject will react and recompile as required. It also requires that the library be very careful
about how it uses the
<x>-config
dependency, since if it contains asources:
list, this will need to beremoved via
.partial_dependency()
before the config is used in the public-facingdeclare_dependency()
call.3. Source-only dependencies
Transform the library into a
dependency(sources: ...)
and export this from the subproject,and then in the top-level project, create the build target with any extra configuration required:
The main downside to this is that you no longer have a clean separation between ours and theirs code,
as thirdparty code is being compiled as part of your build target. This means that warnings are harder
to inhibit, and linters may need extra config to exclude these files.4
There's a few other strategies I've explored in the past, but these are the main ones I've seen much success with.
I could see
meson
potentially being enhanced to approach this problem, saving the need for copyingaround the same code everywhere. For example:
Beta Was this translation helpful? Give feedback.
All reactions