From 5440ce003b5ffad9220496df910a52fb9d798a91 Mon Sep 17 00:00:00 2001 From: Xavier Claessens Date: Fri, 9 Apr 2021 08:46:01 -0400 Subject: [PATCH] Add global_source/build_root() --- docs/markdown/Reference-manual.md | 16 ++++++++++++++++ docs/markdown/snippets/global_root.md | 10 ++++++++++ mesonbuild/interpreter/mesonmain.py | 18 ++++++++++++++++-- test cases/common/221 fs module/meson.build | 5 +++-- .../subprojects/subbie/meson.build | 2 ++ 5 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 docs/markdown/snippets/global_root.md diff --git a/docs/markdown/Reference-manual.md b/docs/markdown/Reference-manual.md index 74f332438..e4c470306 100644 --- a/docs/markdown/Reference-manual.md +++ b/docs/markdown/Reference-manual.md @@ -1989,6 +1989,9 @@ the following methods. root directory. *(deprecated since 0.56.0)*: this function will return the build root of the parent project if called from a subproject, which is usually not what you want. Try using `current_build_dir()` or `project_build_root()`. + In the rare cases where the root of the main project is needed, + use `global_build_root()` that has the same behaviour but with a more explicit + name. - `source_root()`: returns a string with the absolute path to the source root directory. Note: you should use the `files()` function @@ -1997,6 +2000,9 @@ the following methods. *(deprecated since 0.56.0)*: This function will return the source root of the parent project if called from a subproject, which is usually not what you want. Try using `current_source_dir()` or `project_source_root()`. + In the rare cases where the root of the main project is needed, + use `global_source_root()` that has the same behaviour but with a more explicit + name. - `project_build_root()` *(since 0.56.0)*: returns a string with the absolute path to the build root directory of the current (sub)project. @@ -2004,6 +2010,16 @@ the following methods. - `project_source_root()` *(since 0.56.0)*: returns a string with the absolute path to the source root directory of the current (sub)project. +- `global_build_root()` *(since 0.58.0)*: returns a string with the absolute path + to the build root directory. This function will return the build root of the + main project if called from a subproject, which is usually not what you want. + It is usually preferable to use `current_build_dir()` or `project_build_root()`. + +- `global_source_root()` *(since 0.58.0)*: returns a string with the absolute path + to the source root directory. This function will return the source root of the + main project if called from a subproject, which is usually not what you want. + It is usually preferable to use `current_source_dir()` or `project_source_root()`. + - `current_build_dir()`: returns a string with the absolute path to the current build directory. diff --git a/docs/markdown/snippets/global_root.md b/docs/markdown/snippets/global_root.md new file mode 100644 index 000000000..8590f824a --- /dev/null +++ b/docs/markdown/snippets/global_root.md @@ -0,0 +1,10 @@ +## New `meson.global_build_root()` and `meson.global_source_root()` methods + +Returns the root source and build directory of the main project. + +Those are direct replacement for `meson.build_root()` and `meson.source_root()` +that have been deprecated since 0.56.0. In some rare occasions they could not be +replaced by `meson.project_source_root()` or `meson.current_source_dir()`, in +which case the new methods can now be used instead. Old methods are still +deprecated because their names are not explicit enough and created many issues +when a project is being used as a subproject. diff --git a/mesonbuild/interpreter/mesonmain.py b/mesonbuild/interpreter/mesonmain.py index eef1ccf49..e76ad2ec5 100644 --- a/mesonbuild/interpreter/mesonmain.py +++ b/mesonbuild/interpreter/mesonmain.py @@ -35,6 +35,8 @@ class MesonMain(InterpreterObject): 'build_root': self.build_root_method, 'project_source_root': self.project_source_root_method, 'project_build_root': self.project_build_root_method, + 'global_source_root': self.global_source_root_method, + 'global_build_root': self.global_build_root_method, 'add_install_script': self.add_install_script_method, 'add_postconf_script': self.add_postconf_script_method, 'add_dist_script': self.add_dist_script_method, @@ -173,13 +175,13 @@ class MesonMain(InterpreterObject): @noPosargs @permittedKwargs({}) - @FeatureDeprecated('meson.source_root', '0.56.0', 'use meson.current_source_dir instead.') + @FeatureDeprecated('meson.source_root', '0.56.0', 'use meson.project_source_root() or meson.global_source_root() instead.') def source_root_method(self, args, kwargs): return self.interpreter.environment.source_dir @noPosargs @permittedKwargs({}) - @FeatureDeprecated('meson.build_root', '0.56.0', 'use meson.current_build_dir instead.') + @FeatureDeprecated('meson.build_root', '0.56.0', 'use meson.project_build_root() or meson.global_build_root() instead.') def build_root_method(self, args, kwargs): return self.interpreter.environment.build_dir @@ -203,6 +205,18 @@ class MesonMain(InterpreterObject): return src return os.path.join(src, sub) + @noPosargs + @noKwargs + @FeatureNew('meson.global_source_root', '0.58.0') + def global_source_root_method(self, args, kwargs): + return self.interpreter.environment.source_dir + + @noPosargs + @noKwargs + @FeatureNew('meson.global_build_root', '0.58.0') + def global_build_root_method(self, args, kwargs): + return self.interpreter.environment.build_dir + @noPosargs @permittedKwargs({}) @FeatureDeprecated('meson.has_exe_wrapper', '0.55.0', 'use meson.can_run_host_binaries instead.') diff --git a/test cases/common/221 fs module/meson.build b/test cases/common/221 fs module/meson.build index f090d354c..300e77718 100644 --- a/test cases/common/221 fs module/meson.build +++ b/test cases/common/221 fs module/meson.build @@ -97,8 +97,9 @@ f2 = 'subdir/../meson.build' assert(fs.is_samepath(f1, f2), 'is_samepath not detercting same files') assert(fs.is_samepath(meson.source_root(), 'subdir/..'), 'is_samepath not detecting same directory') assert(fs.is_samepath(meson.project_source_root(), 'subdir/..'), 'is_samepath not detecting same directory') -# This fails with python3.5. It can be uncommented when we depend on python >= 3.6 -#assert(fs.is_samepath(meson.project_build_root(), meson.current_build_dir() / 'subdir/..'), 'is_samepath not detecting same directory') +assert(fs.is_samepath(meson.project_build_root(), meson.current_build_dir() / 'subdir/..'), 'is_samepath not detecting same directory') +assert(fs.is_samepath(meson.global_source_root(), meson.current_source_dir()), 'is_samepath not detecting same directory') +assert(fs.is_samepath(meson.global_build_root(), meson.current_build_dir()), 'is_samepath not detecting same directory') assert(not fs.is_samepath(f1, 'subdir/subdirfile.txt'), 'is_samepath known bad comparison') assert(not fs.is_samepath('not-a-path', f2), 'is_samepath should not error if path(s) do not exist') diff --git a/test cases/common/221 fs module/subprojects/subbie/meson.build b/test cases/common/221 fs module/subprojects/subbie/meson.build index ca6d36b4a..f1e21ea10 100644 --- a/test cases/common/221 fs module/subprojects/subbie/meson.build +++ b/test cases/common/221 fs module/subprojects/subbie/meson.build @@ -5,5 +5,7 @@ fs = import('fs') assert(fs.exists('subprojectfile.txt'), 'Subproject root file not found.') assert(fs.is_samepath(meson.project_source_root(), meson.current_source_dir()), 'is_samepath not detecting same directory') assert(fs.is_samepath(meson.project_build_root(), meson.current_build_dir()), 'is_samepath not detecting same directory') +assert(fs.is_samepath(meson.global_source_root(), meson.current_source_dir() / '../..'), 'is_samepath not detecting same directory') +assert(fs.is_samepath(meson.global_build_root(), meson.current_build_dir() / '../..'), 'is_samepath not detecting same directory') subdir('subsub')