Skip to content

Commit

Permalink
Add allow_abs_paths option
Browse files Browse the repository at this point in the history
The existing error upon detecting use of an absolute include directory
path (within the source tree) is a well-intentioned but blunt tool that
unnecessarily prevents use of valid and safe absolute include dir paths.

Absolute paths can be constructed from build-relative absolute paths
(e.g. src_root = project_source_root(), ... src_root / 'dir/inc') and so
use consistent 'src_root'-relative path construction across multiple
meson.build files while also making each meson.build file easy to move
without breaking through use of pure relative paths.

Discussed with more context and rationale here -
mesonbuild#12244

Setting the option `allow_abs_paths=true` prevents this error check and
is obviously used at your own risk.
  • Loading branch information
GertyP committed Sep 26, 2023
1 parent 5b317c5 commit 62c1d71
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/markdown/Builtin-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ machine](#specifying-options-per-machine) section for details.
| wrap_mode {default, nofallback,<br>nodownload, forcefallback, nopromote} | default | Wrap mode to use | no | no |
| force_fallback_for | [] | Force fallback for those dependencies | no | no |
| vsenv | false | Activate Visual Studio environment | no | no |
| allow_abs_paths | false | Suppress errors from use of absolute include paths | no | yes |

#### Details for `backend`

Expand Down
1 change: 1 addition & 0 deletions mesonbuild/coredata.py
Original file line number Diff line number Diff line change
Expand Up @@ -1380,6 +1380,7 @@ def add_to_argparse(self, name: str, parser: argparse.ArgumentParser, help_suffi
(OptionKey('wrap_mode'), BuiltinOption(UserComboOption, 'Wrap mode', 'default', choices=['default', 'nofallback', 'nodownload', 'forcefallback', 'nopromote'])),
(OptionKey('force_fallback_for'), BuiltinOption(UserArrayOption, 'Force fallback for those subprojects', [])),
(OptionKey('vsenv'), BuiltinOption(UserBooleanOption, 'Activate Visual Studio environment', False, readonly=True)),
(OptionKey('allow_abs_paths'), BuiltinOption(UserBooleanOption, 'Suppress errors from use of absolute include paths', False, yielding=False)),

# Pkgconfig module
(OptionKey('relocatable', module='pkgconfig'),
Expand Down
11 changes: 9 additions & 2 deletions mesonbuild/interpreter/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2781,11 +2781,12 @@ def build_incdir_object(self, incdir_strings: T.List[str], is_system: bool = Fal
absbase_build = os.path.join(build_root, self.subdir)

for a in incdir_strings:
if path_is_in_root(Path(a), Path(src_root)):
if not self.coredata.options[OptionKey('allow_abs_paths')].value and path_is_in_root(Path(a), Path(src_root)):
raise InvalidArguments(textwrap.dedent('''\
Tried to form an absolute path to a dir in the source tree.
You should not do that but use relative paths instead, for
directories that are part of your project.
directories that are part of your project. This is to help avoid
broken paths when directories change locations.
To get include path to any directory relative to the current dir do
Expand All @@ -2804,6 +2805,12 @@ def build_incdir_object(self, incdir_strings: T.List[str], is_system: bool = Fal
Note that this error message can also be triggered by
external dependencies being installed within your source
tree - it's not recommended to do this.
If you understand the risks and would still prefer to use
relocation-safe absolute paths (e.g. by constructing absolute
paths from project-relative path variables, combined with other
relative paths) then you can suppress this error by setting the
'allow_abs_paths' option to 'true'.
'''))
else:
try:
Expand Down
1 change: 1 addition & 0 deletions mesonbuild/utils/universal.py
Original file line number Diff line number Diff line change
Expand Up @@ -2185,6 +2185,7 @@ class OptionType(enum.IntEnum):
'pkg_config_path',
'cmake_prefix_path',
'vsenv',
'allow_abs_paths',
}


Expand Down
9 changes: 8 additions & 1 deletion test cases/common/18 includedir/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ subdir('src')

errormsg = '''Tried to form an absolute path to a dir in the source tree.
You should not do that but use relative paths instead, for
directories that are part of your project.
directories that are part of your project. This is to help avoid
broken paths when directories change locations.
To get include path to any directory relative to the current dir do
Expand All @@ -24,6 +25,12 @@ different subdirectory.
Note that this error message can also be triggered by
external dependencies being installed within your source
tree - it's not recommended to do this.
If you understand the risks and would still prefer to use
relocation-safe absolute paths (e.g. by constructing absolute
paths from project-relative path variables, combined with other
relative paths) then you can suppress this error by setting the
'allow_abs_paths' option to 'true'.
'''
testcase expect_error(errormsg)
include_directories(meson.current_source_dir() / 'include')
Expand Down
5 changes: 5 additions & 0 deletions test cases/common/271 allow_abs_paths/func.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include "func.h"

int func(void) {
return 0;
}
6 changes: 6 additions & 0 deletions test cases/common/271 allow_abs_paths/include/func.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef FUNC_H__
#define FUNC_H__

int func(void);

#endif
6 changes: 6 additions & 0 deletions test cases/common/271 allow_abs_paths/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
project('include dir test', 'c',
default_options : [ 'allow_abs_paths=true' ]
)

exe = executable('prog', 'prog.c', 'func.c', include_directories : include_directories(meson.current_source_dir() / 'include'))
test('abs inc test', exe)
5 changes: 5 additions & 0 deletions test cases/common/271 allow_abs_paths/prog.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include "func.h"

int main(void) {
return func();
}

0 comments on commit 62c1d71

Please sign in to comment.