Skip to content

Commit

Permalink
gnome.mkenum_simple(): Fix include path when header is in subdir
Browse files Browse the repository at this point in the history
It was generating #include with the basename of every header file. That
assumes that every directory where there are headers are also included
into search path when compiling the .c file.

Change to use path relative to current subdir, which can be both in
build or source directory. That means that we assume that when the .c
file is compiled, the target has a include_directories pointing to the
directory where gnome.mkenum_simple() has been called, which is
generally '.' and added automatically.

Also fix type annotation to only allow str and File sources, other types
have never been working, it would require to iterate over custom target
outputs, etc.

Fixes: #7582
  • Loading branch information
xclaesse committed Sep 10, 2023
1 parent 5f46ea1 commit 1070867
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 9 deletions.
7 changes: 7 additions & 0 deletions docs/markdown/Gnome-module.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,13 @@ for a build target, you must add the generated header to the build
target's list of sources to codify the dependency. This is true for
all generated sources, not just `mkenums_simple`.

The generated source file includes all headers passed to the sources keyword
argument, using paths relative to current build or source directory. That means
that targets that compile the generated source file must have the current
directory in its `include_directories`. *Since 1.3.0* `sources` outside of
current directory do not require adding those directories into
`include_directories` anymore.

* `body_prefix`: additional prefix at the top of the body file,
e.g. for extra includes
* `decorator`: optional decorator for the function declarations,
Expand Down
27 changes: 18 additions & 9 deletions mesonbuild/modules/gnome.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,21 +176,22 @@ class GenerateVapi(TypedDict):

class _MkEnumsCommon(TypedDict):

sources: T.List[T.Union[FileOrString, build.GeneratedTypes]]
install_header: bool
install_dir: T.Optional[str]
identifier_prefix: T.Optional[str]
symbol_prefix: T.Optional[str]

class MkEnumsSimple(_MkEnumsCommon):

sources: T.List[FileOrString]
header_prefix: str
decorator: str
function_prefix: str
body_prefix: str

class MkEnums(_MkEnumsCommon):

sources: T.List[T.Union[FileOrString, build.GeneratedTypes]]
c_template: T.Optional[FileOrString]
h_template: T.Optional[FileOrString]
comments: T.Optional[str]
Expand Down Expand Up @@ -221,12 +222,6 @@ class MkEnums(_MkEnumsCommon):
_MK_ENUMS_COMMON_KWS: T.List[KwargInfo] = [
INSTALL_KW.evolve(name='install_header'),
INSTALL_DIR_KW,
KwargInfo(
'sources',
ContainerTypeInfo(list, (str, mesonlib.File, CustomTarget, CustomTargetIndex, GeneratedList)),
listify=True,
required=True,
),
KwargInfo('identifier_prefix', (str, NoneType)),
KwargInfo('symbol_prefix', (str, NoneType)),
]
Expand Down Expand Up @@ -1749,6 +1744,13 @@ def gdbus_codegen(self, state: 'ModuleState', args: T.Tuple[str, T.Optional[T.Un
'gnome.mkenums',
*_MK_ENUMS_COMMON_KWS,
DEPENDS_KW,
KwargInfo(
'sources',
ContainerTypeInfo(list, (str, mesonlib.File, CustomTarget, CustomTargetIndex,
GeneratedList)),
listify=True,
required=True,
),
KwargInfo('c_template', (str, mesonlib.File, NoneType)),
KwargInfo('h_template', (str, mesonlib.File, NoneType)),
KwargInfo('comments', (str, NoneType)),
Expand Down Expand Up @@ -1824,6 +1826,12 @@ def mkenums(self, state: 'ModuleState', args: T.Tuple[str], kwargs: 'MkEnums') -
@typed_kwargs(
'gnome.mkenums_simple',
*_MK_ENUMS_COMMON_KWS,
KwargInfo(
'sources',
ContainerTypeInfo(list, (str, mesonlib.File)),
listify=True,
required=True,
),
KwargInfo('header_prefix', str, default=''),
KwargInfo('function_prefix', str, default=''),
KwargInfo('body_prefix', str, default=''),
Expand Down Expand Up @@ -1851,8 +1859,9 @@ def mkenums_simple(self, state: 'ModuleState', args: T.Tuple[str], kwargs: 'MkEn
if body_prefix != '':
fhead += '%s\n' % body_prefix
fhead += '#include "%s"\n' % hdr_filename
for hdr in kwargs['sources']:
fhead += '#include "{}"\n'.format(os.path.basename(str(hdr)))
for hdr in self.interpreter.source_strings_to_files(kwargs['sources']):
hdr_path = os.path.relpath(hdr.relative_name(), state.subdir)
fhead += f'#include "{hdr_path}"\n'
fhead += textwrap.dedent(
'''
#define C_ENUM(v) ((gint) v)
Expand Down
10 changes: 10 additions & 0 deletions test cases/frameworks/7 gnome/mkenums/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,13 @@ main = configure_file(
enumexe6 = executable('enumprog6', main, enums_c2, enums_h6,
dependencies : gobj)
test('enum test 4', enumexe6)

# Test with headers coming from other directories
# https://github.com/mesonbuild/meson/pull/10855
subdir('subdir')
enums7 = gnome.mkenums_simple('enums7', sources: ['meson-sample.h', h2, h3])
main = configure_file(
input : 'main.c',
output : 'mai7.c',
configuration : {'ENUM_FILE': 'enums7.h'})
test('enums7 test', executable('enumprog7', main, enums7, dependencies : gobj))
5 changes: 5 additions & 0 deletions test cases/frameworks/7 gnome/mkenums/subdir/h2.h.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

typedef enum {
MESON_SUBDIR_FOO,
} MesonSubdir;
5 changes: 5 additions & 0 deletions test cases/frameworks/7 gnome/mkenums/subdir/h3.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

typedef enum {
MESON_SUBDIR2_FOO,
} MesonSubdir2;
2 changes: 2 additions & 0 deletions test cases/frameworks/7 gnome/mkenums/subdir/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
h2 = configure_file(input: 'h2.h.in', output: 'h2.h', copy: true)
h3 = files('h3.h')

0 comments on commit 1070867

Please sign in to comment.