From 3b2126531a732ddd54cab2404ac1cb8b44fb49f7 Mon Sep 17 00:00:00 2001 From: Daniel Mensinger Date: Wed, 17 Jul 2019 19:57:24 +0200 Subject: [PATCH] cmake: added test case for environment variables --- run_project_tests.py | 12 +++++++++++- run_tests.py | 17 +++++++++++------ .../cmMesonTestDep/cmMesonTestDepConfig.cmake | 9 +++++++++ .../linuxlike/13 cmake dependency/meson.build | 4 ++++ .../13 cmake dependency/setup_env.json | 3 +++ 5 files changed, 38 insertions(+), 7 deletions(-) create mode 100644 test cases/linuxlike/13 cmake dependency/cmake_pref_env/lib/cmake/cmMesonTestDep/cmMesonTestDepConfig.cmake create mode 100644 test cases/linuxlike/13 cmake dependency/setup_env.json diff --git a/run_project_tests.py b/run_project_tests.py index 797e1a967..80b56bb17 100755 --- a/run_project_tests.py +++ b/run_project_tests.py @@ -35,6 +35,7 @@ from mesonbuild import mtest from mesonbuild.mesonlib import MachineChoice, stringlistify, Popen_safe from mesonbuild.coredata import backendlist import argparse +import json import xml.etree.ElementTree as ET import time import multiprocessing @@ -348,6 +349,7 @@ def _run_test(testdir, test_build_dir, install_dir, extra_args, compiler, backen compile_commands, clean_commands, install_commands, uninstall_commands = commands test_args = parse_test_args(testdir) gen_start = time.time() + setup_env = None # Configure in-process if pass_prefix_to_test(testdir): gen_args = ['--prefix', '/usr'] @@ -362,7 +364,15 @@ def _run_test(testdir, test_build_dir, install_dir, extra_args, compiler, backen crossfile = os.path.join(testdir, 'crossfile.ini') if os.path.exists(crossfile): gen_args.extend(['--cross-file', crossfile]) - (returncode, stdo, stde) = run_configure(gen_args) + setup_env_file = os.path.join(testdir, 'setup_env.json') + if os.path.exists(setup_env_file): + setup_env = os.environ.copy() + with open(setup_env_file, 'r') as fp: + data = json.load(fp) + for key, val in data.items(): + val = val.replace('@ROOT@', os.path.abspath(testdir)) + setup_env[key] = val + (returncode, stdo, stde) = run_configure(gen_args, env=setup_env) try: logfile = Path(test_build_dir, 'meson-logs', 'meson-log.txt') mesonlog = logfile.open(errors='ignore', encoding='utf-8').read() diff --git a/run_tests.py b/run_tests.py index 051b91e9c..38c65c5b4 100755 --- a/run_tests.py +++ b/run_tests.py @@ -222,28 +222,33 @@ def clear_meson_configure_class_caches(): mesonbuild.dependencies.PkgConfigDependency.pkgbin_cache = {} mesonbuild.dependencies.PkgConfigDependency.class_pkgbin = mesonlib.PerMachine(None, None) -def run_configure_inprocess(commandlist): +def run_configure_inprocess(commandlist, env=None): old_stdout = sys.stdout sys.stdout = mystdout = StringIO() old_stderr = sys.stderr sys.stderr = mystderr = StringIO() + old_environ = os.environ.copy() + if env is not None: + os.environ.update(env) try: returncode = mesonmain.run(commandlist, get_meson_script()) finally: sys.stdout = old_stdout sys.stderr = old_stderr clear_meson_configure_class_caches() + os.environ.clear() + os.environ.update(old_environ) return returncode, mystdout.getvalue(), mystderr.getvalue() -def run_configure_external(full_command): - pc, o, e = mesonlib.Popen_safe(full_command) +def run_configure_external(full_command, env=None): + pc, o, e = mesonlib.Popen_safe(full_command, env=env) return pc.returncode, o, e -def run_configure(commandlist): +def run_configure(commandlist, env=None): global meson_exe if meson_exe: - return run_configure_external(meson_exe + commandlist) - return run_configure_inprocess(commandlist) + return run_configure_external(meson_exe + commandlist, env=env) + return run_configure_inprocess(commandlist, env=env) def print_system_info(): print(mlog.bold('System information.').get_text(mlog.colorize_console)) diff --git a/test cases/linuxlike/13 cmake dependency/cmake_pref_env/lib/cmake/cmMesonTestDep/cmMesonTestDepConfig.cmake b/test cases/linuxlike/13 cmake dependency/cmake_pref_env/lib/cmake/cmMesonTestDep/cmMesonTestDepConfig.cmake new file mode 100644 index 000000000..06a206071 --- /dev/null +++ b/test cases/linuxlike/13 cmake dependency/cmake_pref_env/lib/cmake/cmMesonTestDep/cmMesonTestDepConfig.cmake @@ -0,0 +1,9 @@ +find_package(ZLIB) + +if(ZLIB_FOUND OR ZLIB_Found) + set(cmMesonTestDep_FOUND ON) + set(cmMesonTestDep_LIBRARIES ${ZLIB_LIBRARY}) + set(cmMesonTestDep_INCLUDE_DIRS ${ZLIB_INCLUDE_DIR}) +else() + set(cmMesonTestDep_FOUND OFF) +endif() diff --git a/test cases/linuxlike/13 cmake dependency/meson.build b/test cases/linuxlike/13 cmake dependency/meson.build index 84c1d3111..411b7a36d 100644 --- a/test cases/linuxlike/13 cmake dependency/meson.build +++ b/test cases/linuxlike/13 cmake dependency/meson.build @@ -36,6 +36,10 @@ depf2 = dependency('ZLIB', required : false, method : 'cmake', modules : 'dfggh: assert(depf2.found() == false, 'Invalid CMake targets should fail') +# Try to find cmMesonTestDep in a custom prefix + +depPrefEnv = dependency('cmMesonTestDep', required : true, method : 'cmake') + # Try to find a dependency with a custom CMake module depm1 = dependency('SomethingLikeZLIB', required : true, method : 'cmake', cmake_module_path : 'cmake') diff --git a/test cases/linuxlike/13 cmake dependency/setup_env.json b/test cases/linuxlike/13 cmake dependency/setup_env.json new file mode 100644 index 000000000..aa1537496 --- /dev/null +++ b/test cases/linuxlike/13 cmake dependency/setup_env.json @@ -0,0 +1,3 @@ +{ + "CMAKE_PREFIX_PATH": "@ROOT@/cmake_pref_env" +}