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 7 years ago
  • Comments: 32 (17 by maintainers)

Commits related to this issue

Most upvoted comments

Instead of having the headers in an inc dir, 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:

gen_headers = custom_target(...,
    output: ['file1.h', 'file2.h', ...],
    command: [copier_wrapper_script, ...])
gen_inc = include_directories('.')

and then do this in the target that needs those headers:

executable(..., gen_headers,
    include_directories: [gen_inc, ...])