meson: Unable to add include paths to headers generated with custom targets that output directories
My use case is to include generated headers from mavgen.py (mavlink generator) script. It generates headers on subdirectories and I can generate them with the following snippet:
mavgen = find_program('modules/mavlink/pymavlink/tools/mavgen.py')
mavgen_wrapper = find_program('tools/mavgen.sh')
gen_mavlink_headers = custom_target('gen_mavlink_headers',
input : ['modules/mavlink/message_definitions/v1.0/ardupilotmega.xml'],
output : ['mavlink_headers.done'],
command : [mavgen_wrapper, mavgen, '@INPUT@', '@OUTPUT@'])
mavlink_includes = include_directories('mavlink/ardupilotmega')
I use a wrapper script that just does a touch on the output file afterwards.
However checking the generated build.ninja file, the places that use mavlink_includes as include include only the src directory, not the build directory. If I do any change on the meson.build file and re-run ninja, then it works.
Checking the source code, the following diff fixes it for me, but doesn’t look to be the right fix:
diff --git a/mesonbuild/backend/ninjabackend.py b/mesonbuild/backend/ninjabackend.py
index e8fae8e..cd4b821 100644
--- a/mesonbuild/backend/ninjabackend.py
+++ b/mesonbuild/backend/ninjabackend.py
@@ -1934,16 +1934,7 @@ rule FORTRAN_DEP_HACK
# Add source subdir first so that the build subdir overrides it
sargs = compiler.get_include_args(srctreedir, i.is_system)
commands += sargs
- # There may be include dirs where a build directory has not been
- # created for some source dir. For example if someone does this:
- #
- # inc = include_directories('foo/bar/baz')
- #
- # But never subdir()s into the actual dir.
- if os.path.isdir(os.path.join(self.environment.get_build_dir(), expdir)):
- bargs = compiler.get_include_args(expdir, i.is_system)
- else:
- bargs = []
+ bargs = compiler.get_include_args(expdir, i.is_system)
commands += bargs
for d in i.get_extra_build_dirs():
commands += compiler.get_include_args(d, i.is_system)
Any ideas? Thanks.
About this issue
- Original URL
- State: open
- Created 9 years ago
- Comments: 33 (17 by maintainers)
Came across this issue while investigating a non-deterministic failure with our meson builds, which came down to meson not generating a custom target before building dependent executables. I should add that this behavior was introduced after we upgraded from v1.2.0 to v1.2.1.
The “ordinary” way ton include custom targets described by @nirbheek works, of course. But we want to minimize redundant compilation by building a library from our generated
.ccfiles and including the headers at compile time for our executables. Unfortunately I think that this cannot be expressed unless meson gives us a way to includecustom_tgtsas an include directory as discussed by @LAK132.Alternatively, we can introduce an auxiliary custom target that filters out non-header files but this an ugly workaround. Perhaps we can have a builtin function to filter out/include files from custom targets?
tl;dr Is there any standard way of avoiding unnecessary compilation by linking against a library from a custom target and including it’s headers?
what about adding a
dependencieskwarg toinclude_directoriesso that you can write something likeInstead of having the headers in an
incdir, have the generator write them to the build dir. If it can not do that then invoke it via a wrapper script that does the fixup. Then you can do something like:and then do this in the target that needs those headers: