diff options
author | James <james@conan.io> | 2021-09-14 15:00:05 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-14 15:00:05 +0200 |
commit | e55ef3d709d484a26db3a2ea2f00bc760db75c76 (patch) | |
tree | 7c8750f9913e355ba8532fff0f3f367f3971c423 | |
parent | 1a644a89ad037df8eefd771ff0f20721908c9327 (diff) |
making new env multi (#9543)
* making new env multi
* fixing tests
* fix lowercase file
* fixing Win test
* trying new group approach
* fix autotools
* more fix autotools
* wip
* fix autotools
* review
* remove unnecessary test filename for env
-rw-r--r-- | conan/tools/env/environment.py | 18 | ||||
-rw-r--r-- | conan/tools/env/virtualbuildenv.py | 23 | ||||
-rw-r--r-- | conan/tools/env/virtualrunenv.py | 21 | ||||
-rw-r--r-- | conan/tools/gnu/autotoolsdeps.py | 4 | ||||
-rw-r--r-- | conan/tools/gnu/autotoolstoolchain.py | 4 | ||||
-rw-r--r-- | conan/tools/microsoft/subsystems.py | 4 | ||||
-rw-r--r-- | conan/tools/microsoft/visual.py | 12 | ||||
-rw-r--r-- | conans/assets/templates/new_v2_cmake.py | 4 | ||||
-rw-r--r-- | conans/client/generators/__init__.py | 52 | ||||
-rw-r--r-- | conans/model/conan_file.py | 11 | ||||
-rw-r--r-- | conans/test/assets/pkg_cmake.py | 2 | ||||
-rw-r--r-- | conans/test/functional/layout/test_editable_cmake.py | 9 | ||||
-rw-r--r-- | conans/test/functional/toolchains/cmake/test_shared_cmake.py | 2 | ||||
-rw-r--r-- | conans/test/functional/toolchains/env/test_complete.py | 5 | ||||
-rw-r--r-- | conans/test/functional/toolchains/gnu/autotools/test_win_bash.py | 4 | ||||
-rw-r--r-- | conans/test/integration/toolchains/env/test_virtualenv_default_apply.py | 23 | ||||
-rw-r--r-- | conans/test/integration/toolchains/microsoft/vcvars_test.py | 12 | ||||
-rw-r--r-- | conans/test/utils/mocks.py | 3 |
18 files changed, 129 insertions, 84 deletions
diff --git a/conan/tools/env/environment.py b/conan/tools/env/environment.py index 48bc1572..9708d000 100644 --- a/conan/tools/env/environment.py +++ b/conan/tools/env/environment.py @@ -273,7 +273,7 @@ class Environment: content = "\n".join(result) save(filename, content) - def save_script(self, name, auto_activate=True): + def save_script(self, name, group="build"): # FIXME: using platform is not ideal but settings might be incomplete if platform.system() == "Windows" and not self._conanfile.win_bash: path = os.path.join(self._conanfile.generators_folder, "{}.bat".format(name)) @@ -282,8 +282,8 @@ class Environment: path = os.path.join(self._conanfile.generators_folder, "{}.sh".format(name)) self.save_sh(path) - if auto_activate: - register_environment_script(self._conanfile, path) + if group: + register_env_script(self._conanfile, path, group) def compose_env(self, other): """ @@ -427,6 +427,12 @@ class ProfileEnvironment: return result -def register_environment_script(conanfile, path): - if path not in conanfile.environment_scripts: - conanfile.environment_scripts.append(path) +def register_env_script(conanfile, env_script_path, group): + """ + Add the "env_script_path" to the current list of registered scripts for defined "group" + These will be mapped to files: + - conan{group}.bat|sh = calls env_script_path1,... env_script_pathN + """ + existing = conanfile.env_scripts.setdefault(group, []) + if env_script_path not in existing: + existing.append(env_script_path) diff --git a/conan/tools/env/virtualbuildenv.py b/conan/tools/env/virtualbuildenv.py index 09d0de6c..1e7e258e 100644 --- a/conan/tools/env/virtualbuildenv.py +++ b/conan/tools/env/virtualbuildenv.py @@ -9,7 +9,24 @@ class VirtualBuildEnv: def __init__(self, conanfile): self._conanfile = conanfile - self._conanfile.virtualenv = False + self._conanfile.virtualbuildenv = False + self.basename = "conanbuildenv" + # TODO: Make this use the settings_build + self.configuration = conanfile.settings.get_safe("build_type") + if self.configuration: + self.configuration = self.configuration.lower() + self.arch = conanfile.settings.get_safe("arch") + if self.arch: + self.arch = self.arch.lower() + + @property + def _filename(self): + f = self.basename + if self.configuration: + f += "-" + self.configuration + if self.arch: + f += "-" + self.arch + return f def environment(self): """ collects the buildtime information from dependencies. This is the typical use case @@ -42,7 +59,7 @@ class VirtualBuildEnv: return build_env - def generate(self, auto_activate=True): + def generate(self, group="build"): build_env = self.environment() if build_env: # Only if there is something defined - build_env.save_script("conanbuildenv", auto_activate=auto_activate) + build_env.save_script(self._filename, group=group) diff --git a/conan/tools/env/virtualrunenv.py b/conan/tools/env/virtualrunenv.py index 8681dc9b..e8ef1588 100644 --- a/conan/tools/env/virtualrunenv.py +++ b/conan/tools/env/virtualrunenv.py @@ -25,6 +25,23 @@ class VirtualRunEnv: def __init__(self, conanfile): self._conanfile = conanfile + self._conanfile.virtualrunenv = False + self.basename = "conanrunenv" + self.configuration = conanfile.settings.get_safe("build_type") + if self.configuration: + self.configuration = self.configuration.lower() + self.arch = conanfile.settings.get_safe("arch") + if self.arch: + self.arch = self.arch.lower() + + @property + def _filename(self): + f = self.basename + if self.configuration: + f += "-" + self.configuration + if self.arch: + f += "-" + self.arch + return f def environment(self): """ collects the runtime information from dependencies. For normal libraries should be @@ -43,7 +60,7 @@ class VirtualRunEnv: return runenv - def generate(self, auto_activate=False): + def generate(self, group="run"): run_env = self.environment() if run_env: - run_env.save_script("conanrunenv", auto_activate=auto_activate) + run_env.save_script(self._filename, group) diff --git a/conan/tools/gnu/autotoolsdeps.py b/conan/tools/gnu/autotoolsdeps.py index 0bcd9cb1..40a89e3e 100644 --- a/conan/tools/gnu/autotoolsdeps.py +++ b/conan/tools/gnu/autotoolsdeps.py @@ -59,5 +59,5 @@ class AutotoolsDeps: self._environment = env return self._environment - def generate(self, auto_activate=True): - self.environment.save_script("conanautotoolsdeps", auto_activate=auto_activate) + def generate(self, group="build"): + self.environment.save_script("conanautotoolsdeps", group=group) diff --git a/conan/tools/gnu/autotoolstoolchain.py b/conan/tools/gnu/autotoolstoolchain.py index fac0399c..11fb747b 100644 --- a/conan/tools/gnu/autotoolstoolchain.py +++ b/conan/tools/gnu/autotoolstoolchain.py @@ -138,9 +138,9 @@ class AutotoolsToolchain: env.append("LDFLAGS", self.ldflags) return env - def generate(self, env=None, auto_activate=True): + def generate(self, env=None, group="build"): env = env or self.environment() - env.save_script("conanautotoolstoolchain", auto_activate=auto_activate) + env.save_script("conanautotoolstoolchain", group=group) self.generate_args() def generate_args(self): diff --git a/conan/tools/microsoft/subsystems.py b/conan/tools/microsoft/subsystems.py index 0f448eb1..8bad929f 100644 --- a/conan/tools/microsoft/subsystems.py +++ b/conan/tools/microsoft/subsystems.py @@ -21,8 +21,8 @@ def run_in_windows_bash(conanfile, command, cwd=None, env=None): env_win = [env] if not isinstance(env, list) else env env_shell = [] else: - env_shell = ["conanenv.sh"] - env_win = ["conanenv.bat"] + env_shell = ["conanbuild.sh"] + env_win = ["conanbuild.bat"] subsystem = conanfile.conf["tools.microsoft.bash:subsystem"] shell_path = conanfile.conf["tools.microsoft.bash:path"] diff --git a/conan/tools/microsoft/visual.py b/conan/tools/microsoft/visual.py index 06da985a..f7e43da4 100644 --- a/conan/tools/microsoft/visual.py +++ b/conan/tools/microsoft/visual.py @@ -1,7 +1,7 @@ import os import textwrap -from conan.tools.env.environment import register_environment_script +from conan.tools.env.environment import register_env_script from conans.client.tools import intel_compilervars_command from conans.client.tools.win import vs_installation_path from conans.errors import ConanException @@ -14,11 +14,11 @@ class VCVars: def __init__(self, conanfile): self._conanfile = conanfile - def generate(self, auto_activate=True): - _write_conanvcvars(self._conanfile, auto_activate=auto_activate) + def generate(self, group="build"): + _write_conanvcvars(self._conanfile, group=group) -def _write_conanvcvars(conanfile, auto_activate=True): +def _write_conanvcvars(conanfile, group): """ write a conanvcvars.bat file with the good args from settings """ @@ -59,8 +59,8 @@ def _write_conanvcvars(conanfile, auto_activate=True): path = os.path.join(conanfile.generators_folder, CONAN_VCVARS_FILE) save(path, content) - if auto_activate: - register_environment_script(conanfile, path) + if group: + register_env_script(conanfile, path, group) def vs_ide_version(conanfile): diff --git a/conans/assets/templates/new_v2_cmake.py b/conans/assets/templates/new_v2_cmake.py index 5b9a299b..2327c927 100644 --- a/conans/assets/templates/new_v2_cmake.py +++ b/conans/assets/templates/new_v2_cmake.py @@ -72,7 +72,7 @@ class {package_name}TestConan(ConanFile): def test(self): if not tools.cross_building(self): cmd = os.path.join(self.cpp.build.bindirs[0], "example") - self.run(cmd, env="conanrunenv") + self.run(cmd, env="conanrun") """ @@ -289,7 +289,7 @@ class {package_name}TestConan(ConanFile): def test(self): if not tools.cross_building(self): - self.run("{name}", env="conanrunenv") + self.run("{name}", env="conanrun") """ diff --git a/conans/client/generators/__init__.py b/conans/client/generators/__init__.py index 7cf22fdd..117f0459 100644 --- a/conans/client/generators/__init__.py +++ b/conans/client/generators/__init__.py @@ -236,14 +236,15 @@ def write_toolchain(conanfile, path, output): conanfile.generate() # tools.env.virtualenv:auto_use will be always True in Conan 2.0 - if conanfile.conf["tools.env.virtualenv:auto_use"] and conanfile.virtualenv: + if conanfile.conf["tools.env.virtualenv:auto_use"]: with chdir(path): - from conan.tools.env.virtualbuildenv import VirtualBuildEnv - env = VirtualBuildEnv(conanfile) - env.generate() - - env = VirtualRunEnv(conanfile) - env.generate() + if conanfile.virtualbuildenv: + from conan.tools.env.virtualbuildenv import VirtualBuildEnv + env = VirtualBuildEnv(conanfile) + env.generate() + if conanfile.virtualrunenv: + env = VirtualRunEnv(conanfile) + env.generate() output.highlight("Aggregating env generators") _generate_aggregated_env(conanfile) @@ -251,23 +252,24 @@ def write_toolchain(conanfile, path, output): def _generate_aggregated_env(conanfile): from conan.tools.microsoft import unix_path - bats = [] - shs = [] - for s in conanfile.environment_scripts: - path = os.path.join(conanfile.generators_folder, s) - if path.lower().endswith(".bat"): - bats.append(path) - elif path.lower().endswith(".sh"): - shs.append(unix_path(conanfile, path)) - if shs: - sh_content = ". " + " && . ".join('"{}"'.format(s) for s in shs) - save(os.path.join(conanfile.generators_folder, "conanenv.sh"), sh_content) - if bats: - lines = "\r\n".join('call "{}"'.format(b) for b in bats) - bat_content = textwrap.dedent("""\ - @echo off - {} - """.format(lines)) - save(os.path.join(conanfile.generators_folder, "conanenv.bat"), bat_content) + for group, env_scripts in conanfile.env_scripts.items(): + bats = [] + shs = [] + for env_script in env_scripts: + path = os.path.join(conanfile.generators_folder, env_script) + if env_script.endswith(".bat"): + bats.append(path) + elif env_script.endswith(".sh"): + shs.append(unix_path(conanfile, path)) + if shs: + sh_content = ". " + " && . ".join('"{}"'.format(s) for s in shs) + save(os.path.join(conanfile.generators_folder, "conan{}.sh".format(group)), sh_content) + if bats: + lines = "\r\n".join('call "{}"'.format(b) for b in bats) + bat_content = textwrap.dedent("""\ + @echo off + {} + """.format(lines)) + save(os.path.join(conanfile.generators_folder, "conan{}.bat".format(group)), bat_content) diff --git a/conans/model/conan_file.py b/conans/model/conan_file.py index 280b3d47..1e3d9105 100644 --- a/conans/model/conan_file.py +++ b/conans/model/conan_file.py @@ -165,7 +165,7 @@ class ConanFile(object): self._conan_new_cpp_info = None # Will be calculated lazy in the getter self._conan_dependencies = None - self.environment_scripts = [] # Accumulate the env scripts generated in order + self.env_scripts = {} # Accumulate the env scripts generated in order # layout() method related variables: self.folders = Folders() @@ -247,8 +247,10 @@ class ConanFile(object): if self.description is not None and not isinstance(self.description, six.string_types): raise ConanException("Recipe 'description' must be a string.") - if not hasattr(self, "virtualenv"): # Allow the user to override it with True or False - self.virtualenv = True + if not hasattr(self, "virtualbuildenv"): # Allow the user to override it with True or False + self.virtualbuildenv = True + if not hasattr(self, "virtualrunenv"): # Allow the user to override it with True or False + self.virtualrunenv = True @property def new_cpp_info(self): @@ -403,7 +405,8 @@ class ConanFile(object): elif self.win_bash: # New, Conan 2.0 from conan.tools.microsoft.subsystems import run_in_windows_bash return run_in_windows_bash(self, command=cmd, cwd=cwd, env=_env) - _env = _env or "conanenv" + if _env is None: + _env = "conanbuild" wrapped_cmd = environment_wrap_command(self, _env, cmd, cwd=self.generators_folder) return self._conan_runner(wrapped_cmd, output, os.path.abspath(RUN_LOG_NAME), cwd) diff --git a/conans/test/assets/pkg_cmake.py b/conans/test/assets/pkg_cmake.py index b0f15e10..bb77d4a4 100644 --- a/conans/test/assets/pkg_cmake.py +++ b/conans/test/assets/pkg_cmake.py @@ -89,7 +89,7 @@ def pkg_cmake_test(require_name): def test(self): cmd = os.path.join(self.cpp.build.bindirs[0], "test") - self.run(cmd, env=["conanrunenv"]) + self.run(cmd, env="conanrun") """) deps = [require_name] diff --git a/conans/test/functional/layout/test_editable_cmake.py b/conans/test/functional/layout/test_editable_cmake.py index 1dc16f58..731046c5 100644 --- a/conans/test/functional/layout/test_editable_cmake.py +++ b/conans/test/functional/layout/test_editable_cmake.py @@ -105,19 +105,18 @@ def editable_cmake_exe(generator): def run_pkg(msg): # FIXME: This only works with ``--install-folder``, layout() will break this - cmd_release = environment_wrap_command(ConanFileMock(), "install_release/conanrunenv", + cmd_release = environment_wrap_command(ConanFileMock(), "conanrunenv-release-x86_64", "dep_app", cwd=c.current_folder) c.run_command(cmd_release) assert "{}: Release!".format(msg) in c.out - cmd_release = environment_wrap_command(ConanFileMock(), "install_debug/conanrunenv", + cmd_release = environment_wrap_command(ConanFileMock(), "conanrunenv-debug-x86_64", "dep_app", cwd=c.current_folder) c.run_command(cmd_release) assert "{}: Debug!".format(msg) in c.out with c.chdir("pkg"): - c.run("install dep/0.1@ -o dep:shared=True -if=install_release -g VirtualRunEnv") - c.run("install dep/0.1@ -o dep:shared=True -if=install_debug -s build_type=Debug " - "-g VirtualRunEnv") + c.run("install dep/0.1@ -o dep:shared=True -g VirtualRunEnv") + c.run("install dep/0.1@ -o dep:shared=True -s build_type=Debug -g VirtualRunEnv") run_pkg("dep") # Do a source change in the editable! diff --git a/conans/test/functional/toolchains/cmake/test_shared_cmake.py b/conans/test/functional/toolchains/cmake/test_shared_cmake.py index 5e81f8f6..088f091d 100644 --- a/conans/test/functional/toolchains/cmake/test_shared_cmake.py +++ b/conans/test/functional/toolchains/cmake/test_shared_cmake.py @@ -19,7 +19,7 @@ def test_shared_cmake_toolchain(): client = TestClient(servers=client.servers, users=client.users) client.run("install app/0.1@ -o chat:shared=True -o hello:shared=True -g VirtualRunEnv") conanfile = ConanFileMock() - command = environment_wrap_command(conanfile, "conanrunenv", "app", cwd=client.current_folder) + command = environment_wrap_command(conanfile, "conanrun", "app", cwd=client.current_folder) client.run_command(command) assert "main: Release!" in client.out diff --git a/conans/test/functional/toolchains/env/test_complete.py b/conans/test/functional/toolchains/env/test_complete.py index a6efde6e..c995ef7f 100644 --- a/conans/test/functional/toolchains/env/test_complete.py +++ b/conans/test/functional/toolchains/env/test_complete.py @@ -122,6 +122,7 @@ def test_complete(): default_options = {"myopenssl:shared": True} exports_sources = "CMakeLists.txt", "main.cpp" generators = "CMakeDeps", "CMakeToolchain", "VirtualBuildEnv", "VirtualRunEnv" + apply_env = False def build(self): cmake = CMake(self) @@ -131,9 +132,9 @@ def test_complete(): self.output.info("RUNNING MYAPP") if self.settings.os == "Windows": self.run(os.sep.join([".", str(self.settings.build_type), "myapp"]), - env="conanrunenv") + env="conanrun") else: - self.run(os.sep.join([".", "myapp"]), env="conanrunenv") + self.run(os.sep.join([".", "myapp"]), env=["conanrun"]) """) cmakelists = textwrap.dedent(""" diff --git a/conans/test/functional/toolchains/gnu/autotools/test_win_bash.py b/conans/test/functional/toolchains/gnu/autotools/test_win_bash.py index df7df5ea..100dd739 100644 --- a/conans/test/functional/toolchains/gnu/autotools/test_win_bash.py +++ b/conans/test/functional/toolchains/gnu/autotools/test_win_bash.py @@ -70,8 +70,8 @@ def test_autotools_bash_complete(): client.run_command("main.exe") check_exe_run(client.out, "main", "msvc", None, "Release", "x86_64", None) - bat_contents = client.load("conanenv.bat") - sh_contents = client.load("conanenv.sh") + bat_contents = client.load("conanbuild.bat") + sh_contents = client.load("conanbuild.sh") assert "conanvcvars.bat" in bat_contents assert "conan_compiler.sh" in sh_contents and "conanautotoolstoolchain.sh" in sh_contents diff --git a/conans/test/integration/toolchains/env/test_virtualenv_default_apply.py b/conans/test/integration/toolchains/env/test_virtualenv_default_apply.py index a69d0562..d95153a4 100644 --- a/conans/test/integration/toolchains/env/test_virtualenv_default_apply.py +++ b/conans/test/integration/toolchains/env/test_virtualenv_default_apply.py @@ -26,8 +26,8 @@ def client(): @pytest.mark.parametrize("default_virtualenv", [True, False, None]) def test_virtualenv_deactivated(client, default_virtualenv): - format_str = {True: "virtualenv = True", - False: "virtualenv = False", + format_str = {True: "virtualbuildenv = True", + False: "virtualbuildenv = False", None: ""}[default_virtualenv] conanfile = textwrap.dedent(""" from conans import ConanFile @@ -41,7 +41,8 @@ def test_virtualenv_deactivated(client, default_virtualenv): client.save({"conanfile.py": conanfile}) client.run("install . ") extension = "bat" if platform.system() == "Windows" else "sh" - exists_file = os.path.exists(os.path.join(client.current_folder, "conanbuildenv.{}".format(extension))) + exists_file = os.path.exists(os.path.join(client.current_folder, + "conanbuildenv.{}".format(extension))) if default_virtualenv is True or default_virtualenv is None: assert exists_file elif default_virtualenv is False: @@ -64,10 +65,10 @@ def test_virtualrunenv_not_applied(client): client.run("install . ") extension = "bat" if platform.system() == "Windows" else "sh" exists_file = os.path.exists(os.path.join(client.current_folder, - "conanrunenv.{}".format(extension))) + "conanrun.{}".format(extension))) assert exists_file - global_env = client.load("conanenv.{}".format(extension)) + global_env = client.load("conanbuild.{}".format(extension)) assert "conanrunenv" not in global_env @@ -86,19 +87,19 @@ def test_virtualrunenv_explicit_declare(client, explicit_declare): def generate(self): VirtualRunEnv(self).generate({}) - """).format({True: "auto_activate=True", - False: "auto_activate=False", + """).format({True: "group='build'", + False: "group='run'", None: ""}.get(explicit_declare)) client.save({"conanfile.py": conanfile}) client.run("install . ") extension = "bat" if platform.system() == "Windows" else "sh" exists_file = os.path.exists(os.path.join(client.current_folder, - "conanrunenv.{}".format(extension))) + "conanbuild.{}".format(extension))) assert exists_file - global_env = client.load("conanenv.{}".format(extension)) + global_env = client.load("conanbuild.{}".format(extension)) if explicit_declare: - assert "conanrunenv" in global_env + assert "conanrun" in global_env else: - assert "conanrunenv" not in global_env + assert "conanrun" not in global_env diff --git a/conans/test/integration/toolchains/microsoft/vcvars_test.py b/conans/test/integration/toolchains/microsoft/vcvars_test.py index 7ea099d8..780f8a65 100644 --- a/conans/test/integration/toolchains/microsoft/vcvars_test.py +++ b/conans/test/integration/toolchains/microsoft/vcvars_test.py @@ -8,8 +8,8 @@ from conans.test.utils.tools import TestClient @pytest.mark.skipif(platform.system() not in ["Windows"], reason="Requires Windows") -@pytest.mark.parametrize("auto_activate", [False, True, None]) -def test_vcvars_generator(auto_activate): +@pytest.mark.parametrize("group", ["build", "run", None]) +def test_vcvars_generator(group): client = TestClient(path_with_spaces=False) conanfile = textwrap.dedent(""" @@ -21,7 +21,7 @@ def test_vcvars_generator(auto_activate): def generate(self): VCVars(self).generate({}) - """.format("True" if auto_activate else "False" if auto_activate is False else "")) + """.format('group="{}"'.format(group) if group else "")) client.save({"conanfile.py": conanfile}) client.run('install . -s os=Windows -s compiler="msvc" -s compiler.version=19.1 ' @@ -29,11 +29,11 @@ def test_vcvars_generator(auto_activate): assert os.path.exists(os.path.join(client.current_folder, "conanvcvars.bat")) - if auto_activate is True or auto_activate is None: - bat_contents = client.load("conanenv.bat") + if group in ("build", None): + bat_contents = client.load("conanbuild.bat") assert "conanvcvars.bat" in bat_contents else: - assert not os.path.exists(os.path.join(client.current_folder, "conanenv.bat")) + assert not os.path.exists(os.path.join(client.current_folder, "conanbuild.bat")) @pytest.mark.skipif(platform.system() not in ["Windows"], reason="Requires Windows") diff --git a/conans/test/utils/mocks.py b/conans/test/utils/mocks.py index 222df975..3fdd74ef 100644 --- a/conans/test/utils/mocks.py +++ b/conans/test/utils/mocks.py @@ -180,11 +180,10 @@ class ConanFileMock(ConanFile): self.folders.set_base_generators(".") self._conan_user = None self._conan_channel = None - self.environment_scripts = [] + self.env_scripts = {} self.win_bash = None self.conf = ConfDefinition().get_conanfile_conf(None) - def run(self, command, win_bash=False, subsystem=None, env=None): assert win_bash is False assert subsystem is None |