I have a bit of a challenge for the meson experts out there.
My newest C++ project (a library) is structured according to The Pitchfork Layout. As is allowed by that convention, I have decided to use merged header and merged test placement. This basically means that my header files, source files, and test source files are all part of the same src
directory structure.
Furthermore, my code is organized into multiple subdirectories based on the purpose it fulfills (e.g. utilities, algorithms, containers, ...).
In essence, what I have is (this is only an example):
<project>
├── meson.build
└── src/<project>
├── algorithms
│ ├── meson.build
│ ├── sort.hpp
│ ├── sort.cpp
│ ├── sort.test.cpp
│ ├── transform.hpp
│ ├── transform.cpp
│ └── transform.test.cpp
├── containers
│ ├── meson.build
│ └── ...
└── utilities
├── meson.build
└── ...
The challenge now comes in writing the meson build files to build both the library and the unit tests. The unit tests obviously depend on the library being build and I would not have the library object ready until all build files have been processed at least once. So there is no way for me to also define the test executables in the meson build files because I need them to link with the library.
Solutions I have come up with so far:
- Define a
lib_sources
andtest_sources
lists in the root build file and let all subdirectories add source files to those lists to then in the root build file define the library and test executable. - Let each subdirectory create its own library and test executable. Then visit the subdirectories in the correct order to preserve dependencies (e.g. visit
utilities
first, thenalgorithms
, thencontainers
). The subdirectories that are visited later can then link with the libraries of earlier subdirectories. And in the root build file create a single library that combines all sublibraries.
I am currently in favor of the second approach because each subdirectory defines its own targets so the build configuration is not centralized. The disadvantage is higher complexity, I guess.
How would I ideally configure the meson build files for a project layout like this?
(Note: yes, I could change the layout and have separate test placement, but that defeats the purpose of this "challenge" where merged test placement is a must-have)