From 0a31aae35239d50e72b0ac28abd8c8ac6c57f98f Mon Sep 17 00:00:00 2001 From: Francisco Ramirez de Anton Date: Fri, 5 Jul 2024 16:10:13 +0200 Subject: [PATCH 1/3] Added .bazelrc to WORKSPACE/MODULE.build in new template (mostly for testing purpose --- conan/cli/commands/new.py | 2 +- conan/internal/api/new/bazel_7_exe.py | 7 +++-- conan/internal/api/new/bazel_7_lib.py | 9 +++++-- conan/internal/api/new/bazel_exe.py | 7 +++-- conan/internal/api/new/bazel_lib.py | 9 +++++-- .../toolchains/google/test_bazel.py | 26 ++++++++++++------- .../test_bazeltoolchain_cross_compilation.py | 2 ++ 7 files changed, 43 insertions(+), 19 deletions(-) diff --git a/conan/cli/commands/new.py b/conan/cli/commands/new.py index f09ebd27541..ab73c483512 100644 --- a/conan/cli/commands/new.py +++ b/conan/cli/commands/new.py @@ -68,7 +68,7 @@ def get_template_vars(): template_vars.extend(meta.find_undeclared_variables(ast)) injected_vars = {"conan_version", "package_name", "as_name"} - optional_vars = {"requires", "tool_requires"} + optional_vars = {"requires", "tool_requires", "outputRootDir"} template_vars = list(set(template_vars) - injected_vars - optional_vars) template_vars.sort() return template_vars diff --git a/conan/internal/api/new/bazel_7_exe.py b/conan/internal/api/new/bazel_7_exe.py index 570482a1d65..02a882870b2 100644 --- a/conan/internal/api/new/bazel_7_exe.py +++ b/conan/internal/api/new/bazel_7_exe.py @@ -17,7 +17,7 @@ class {{package_name}}Recipe(ConanFile): settings = "os", "compiler", "build_type", "arch" # Sources are located in the same place as this recipe, copy them to the recipe - exports_sources = "main/*", "MODULE.bazel" + exports_sources = "main/*", "MODULE.bazel", ".bazelrc" generators = "BazelToolchain" @@ -58,7 +58,9 @@ def test(self): """ _bazel_workspace = " " # Important not empty, so template doesn't discard it - +_bazel_rc = """\ +{% if outputRootDir is defined %}startup --output_user_root={{outputRootDir}}{% endif %} +""" bazel_exe_files_7 = {"conanfile.py": conanfile_exe, "main/{{name}}.cpp": source_cpp, @@ -66,5 +68,6 @@ def test(self): "main/main.cpp": test_main, "main/BUILD": _bazel_build_exe, "MODULE.bazel": _bazel_workspace, + ".bazelrc": _bazel_rc, "test_package/conanfile.py": test_conanfile_exe_v2 } diff --git a/conan/internal/api/new/bazel_7_lib.py b/conan/internal/api/new/bazel_7_lib.py index 024a0f23e8c..0b12959fa18 100644 --- a/conan/internal/api/new/bazel_7_lib.py +++ b/conan/internal/api/new/bazel_7_lib.py @@ -17,7 +17,7 @@ class {{package_name}}Recipe(ConanFile): default_options = {"shared": False, "fPIC": True} # Sources are located in the same place as this recipe, copy them to the recipe - exports_sources = "main/*", "MODULE.bazel" + exports_sources = "main/*", "MODULE.bazel", ".bazelrc" generators = "BazelToolchain" def config_options(self): @@ -116,6 +116,9 @@ def test(self): """ _bazel_workspace = " " # Important not empty, so template doesn't discard it +_bazel_rc = """\ +{% if outputRootDir is defined %}startup --output_user_root={{outputRootDir}}{% endif %} +""" _test_bazel_module_bazel = """\ load_conan_dependencies = use_extension("//conan:conan_deps_module_extension.bzl", "conan_extension") use_repo(load_conan_dependencies, "{{name}}") @@ -136,7 +139,9 @@ def _get_bazel_build(): "main/{{name}}.h": source_h, "main/BUILD": _get_bazel_build(), "MODULE.bazel": _bazel_workspace, + ".bazelrc": _bazel_rc, "test_package/conanfile.py": test_conanfile_v2, "test_package/main/example.cpp": test_main, "test_package/main/BUILD": _bazel_build_test, - "test_package/MODULE.bazel": _test_bazel_module_bazel} + "test_package/MODULE.bazel": _test_bazel_module_bazel, + "test_package/.bazelrc": _bazel_rc} diff --git a/conan/internal/api/new/bazel_exe.py b/conan/internal/api/new/bazel_exe.py index a68271d8556..222bb7c6a09 100644 --- a/conan/internal/api/new/bazel_exe.py +++ b/conan/internal/api/new/bazel_exe.py @@ -17,7 +17,7 @@ class {{package_name}}Recipe(ConanFile): settings = "os", "compiler", "build_type", "arch" # Sources are located in the same place as this recipe, copy them to the recipe - exports_sources = "main/*", "WORKSPACE" + exports_sources = "main/*", "WORKSPACE", ".bazelrc" generators = "BazelToolchain" def layout(self): @@ -61,7 +61,9 @@ def test(self): """ _bazel_workspace = " " # Important not empty, so template doesn't discard it - +_bazel_rc = """\ +{% if outputRootDir is defined %}startup --output_user_root={{outputRootDir}}{% endif %} +""" bazel_exe_files = {"conanfile.py": conanfile_exe, "main/{{name}}.cpp": source_cpp, @@ -69,5 +71,6 @@ def test(self): "main/main.cpp": test_main, "main/BUILD": _bazel_build_exe, "WORKSPACE": _bazel_workspace, + ".bazelrc": _bazel_rc, "test_package/conanfile.py": test_conanfile_exe_v2 } diff --git a/conan/internal/api/new/bazel_lib.py b/conan/internal/api/new/bazel_lib.py index 28328b20d97..5e8a2bb6937 100644 --- a/conan/internal/api/new/bazel_lib.py +++ b/conan/internal/api/new/bazel_lib.py @@ -17,7 +17,7 @@ class {{package_name}}Recipe(ConanFile): default_options = {"shared": False, "fPIC": True} # Sources are located in the same place as this recipe, copy them to the recipe - exports_sources = "main/*", "WORKSPACE" + exports_sources = "main/*", "WORKSPACE", ".bazelrc" generators = "BazelToolchain" def config_options(self): @@ -120,6 +120,9 @@ def test(self): """ _bazel_workspace = " " # Important not empty, so template doesn't discard it +_bazel_rc = """\ +{% if outputRootDir is defined %}startup --output_user_root={{outputRootDir}}{% endif %} +""" _test_bazel_workspace = """ load("@//conan:dependencies.bzl", "load_conan_dependencies") load_conan_dependencies() @@ -140,7 +143,9 @@ def _get_bazel_build(): "main/{{name}}.h": source_h, "main/BUILD": _get_bazel_build(), "WORKSPACE": _bazel_workspace, + ".bazelrc": _bazel_rc, "test_package/conanfile.py": test_conanfile_v2, "test_package/main/example.cpp": test_main, "test_package/main/BUILD": _bazel_build_test, - "test_package/WORKSPACE": _test_bazel_workspace} + "test_package/WORKSPACE": _test_bazel_workspace, + "test_package/.bazelrc": _bazel_rc} diff --git a/test/functional/toolchains/google/test_bazel.py b/test/functional/toolchains/google/test_bazel.py index f9ce5e02608..0610fbb5d14 100644 --- a/test/functional/toolchains/google/test_bazel.py +++ b/test/functional/toolchains/google/test_bazel.py @@ -3,9 +3,15 @@ import pytest +from conan.test.utils.test_files import temp_folder from conan.test.utils.tools import TestClient +@pytest.fixture(scope="module") +def bazel_output_root_dir(): + return temp_folder(path_with_spaces=False) + + @pytest.fixture(scope="module") def bazelrc(): return textwrap.dedent(""" @@ -33,9 +39,9 @@ def base_profile(): @pytest.mark.parametrize("build_type", ["Debug", "Release", "RelWithDebInfo", "MinSizeRel"]) @pytest.mark.tool("bazel", "6.3.2") -def test_basic_exe_6x(bazelrc, build_type, base_profile): +def test_basic_exe_6x(bazelrc, build_type, base_profile, bazel_output_root_dir): client = TestClient(path_with_spaces=False) - client.run("new bazel_exe -d name=myapp -d version=1.0") + client.run(f"new bazel_exe -d name=myapp -d version=1.0 -d outputRootDir={bazel_output_root_dir}") # The build: define several configurations that can be activated by passing # the bazel config with tools.google.bazel:configs client.save({"mybazelrc": bazelrc}) @@ -51,9 +57,9 @@ def test_basic_exe_6x(bazelrc, build_type, base_profile): @pytest.mark.parametrize("build_type", ["Debug", "Release", "RelWithDebInfo", "MinSizeRel"]) @pytest.mark.tool("bazel", "7.1.2") -def test_basic_exe(bazelrc, build_type, base_profile): +def test_basic_exe(bazelrc, build_type, base_profile, bazel_output_root_dir): client = TestClient(path_with_spaces=False) - client.run("new bazel_7_exe -d name=myapp -d version=1.0") + client.run(f"new bazel_7_exe -d name=myapp -d version=1.0 -d outputRootDir={bazel_output_root_dir}") # The build: define several configurations that can be activated by passing # the bazel config with tools.google.bazel:configs client.save({"mybazelrc": bazelrc}) @@ -69,7 +75,7 @@ def test_basic_exe(bazelrc, build_type, base_profile): @pytest.mark.parametrize("shared", [False, True]) @pytest.mark.tool("bazel", "6.3.2") -def test_transitive_libs_consuming_6x(shared): +def test_transitive_libs_consuming_6x(shared, bazel_output_root_dir): """ Testing the next dependencies structure for shared/static libs @@ -98,7 +104,7 @@ def test_transitive_libs_consuming_6x(shared): client = TestClient(path_with_spaces=False) # A regular library made with Bazel with client.chdir("myfirstlib"): - client.run("new bazel_lib -d name=myfirstlib -d version=1.2.11") + client.run(f"new bazel_lib -d name=myfirstlib -d version=1.2.11 -d outputRootDir={bazel_output_root_dir}") conanfile = client.load("conanfile.py") conanfile += """ self.cpp_info.defines.append("MY_DEFINE=\\"MY_VALUE\\"") @@ -115,7 +121,7 @@ def test_transitive_libs_consuming_6x(shared): # We prepare a consumer with Bazel (library mysecondlib using myfirstlib) # and a test_package with an example executable os_ = platform.system() - client.run("new bazel_lib -d name=mysecondlib -d version=1.0") + client.run(f"new bazel_lib -d name=mysecondlib -d version=1.0 -d outputRootDir={bazel_output_root_dir}") conanfile = client.load("conanfile.py") conanfile = conanfile.replace('generators = "BazelToolchain"', 'generators = "BazelToolchain", "BazelDeps"\n' @@ -195,7 +201,7 @@ def test_transitive_libs_consuming_6x(shared): @pytest.mark.parametrize("shared", [False, True]) @pytest.mark.tool("bazel", "7.1.2") -def test_transitive_libs_consuming(shared): +def test_transitive_libs_consuming(shared, bazel_output_root_dir): """ Testing the next dependencies structure for shared/static libs @@ -224,7 +230,7 @@ def test_transitive_libs_consuming(shared): client = TestClient(path_with_spaces=False) # A regular library made with Bazel with client.chdir("myfirstlib"): - client.run("new bazel_7_lib -d name=myfirstlib -d version=1.2.11") + client.run(f"new bazel_7_lib -d name=myfirstlib -d version=1.2.11 -d outputRootDir={bazel_output_root_dir}") conanfile = client.load("conanfile.py") conanfile += """ self.cpp_info.defines.append("MY_DEFINE=\\"MY_VALUE\\"") @@ -241,7 +247,7 @@ def test_transitive_libs_consuming(shared): # We prepare a consumer with Bazel (library mysecondlib using myfirstlib) # and a test_package with an example executable os_ = platform.system() - client.run("new bazel_7_lib -d name=mysecondlib -d version=1.0") + client.run(f"new bazel_7_lib -d name=mysecondlib -d version=1.0 -d outputRootDir={bazel_output_root_dir}") conanfile = client.load("conanfile.py") conanfile = conanfile.replace('generators = "BazelToolchain"', 'generators = "BazelToolchain", "BazelDeps"\n' diff --git a/test/functional/toolchains/google/test_bazeltoolchain_cross_compilation.py b/test/functional/toolchains/google/test_bazeltoolchain_cross_compilation.py index 1768210ef21..504858a30c3 100644 --- a/test/functional/toolchains/google/test_bazeltoolchain_cross_compilation.py +++ b/test/functional/toolchains/google/test_bazeltoolchain_cross_compilation.py @@ -5,6 +5,7 @@ import pytest from conan.test.assets.sources import gen_function_cpp, gen_function_h +from conan.test.utils.test_files import temp_folder from conan.test.utils.tools import TestClient @@ -65,6 +66,7 @@ def build(self): "profile_host": profile_host, "conanfile.py": conanfile, "WORKSPACE": "", + ".bazelrc": f"startup --output_user_root={temp_folder(path_with_spaces=False)}", "main/BUILD": BUILD, "main/myapp.cpp": gen_function_cpp(name="myapp"), "main/myapp.h": gen_function_h(name="myapp"), From f14167595742ffa3086207aa6b58a7cb6d813e9f Mon Sep 17 00:00:00 2001 From: Francisco Ramirez de Anton Date: Fri, 5 Jul 2024 16:41:32 +0200 Subject: [PATCH 2/3] Unix paths --- test/functional/toolchains/google/test_bazel.py | 2 +- .../toolchains/google/test_bazeltoolchain_cross_compilation.py | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/test/functional/toolchains/google/test_bazel.py b/test/functional/toolchains/google/test_bazel.py index 0610fbb5d14..dec23306f72 100644 --- a/test/functional/toolchains/google/test_bazel.py +++ b/test/functional/toolchains/google/test_bazel.py @@ -9,7 +9,7 @@ @pytest.fixture(scope="module") def bazel_output_root_dir(): - return temp_folder(path_with_spaces=False) + return temp_folder(path_with_spaces=False).replace("\\", "/") @pytest.fixture(scope="module") diff --git a/test/functional/toolchains/google/test_bazeltoolchain_cross_compilation.py b/test/functional/toolchains/google/test_bazeltoolchain_cross_compilation.py index 504858a30c3..3a67abe972e 100644 --- a/test/functional/toolchains/google/test_bazeltoolchain_cross_compilation.py +++ b/test/functional/toolchains/google/test_bazeltoolchain_cross_compilation.py @@ -61,12 +61,13 @@ def build(self): ) """) client = TestClient(path_with_spaces=False) + bazel_root_dir = temp_folder(path_with_spaces=False).replace("\\", "/") client.save({ "profile": profile, "profile_host": profile_host, "conanfile.py": conanfile, "WORKSPACE": "", - ".bazelrc": f"startup --output_user_root={temp_folder(path_with_spaces=False)}", + ".bazelrc": f"startup --output_user_root={bazel_root_dir}", "main/BUILD": BUILD, "main/myapp.cpp": gen_function_cpp(name="myapp"), "main/myapp.h": gen_function_h(name="myapp"), From d5ebe3f9bd3df2c50383576bd891cb2cabc5bbba Mon Sep 17 00:00:00 2001 From: Francisco Ramirez de Anton Date: Mon, 8 Jul 2024 10:31:04 +0200 Subject: [PATCH 3/3] snake case instead --- conan/cli/commands/new.py | 2 +- conan/internal/api/new/bazel_7_exe.py | 2 +- conan/internal/api/new/bazel_7_lib.py | 2 +- conan/internal/api/new/bazel_exe.py | 2 +- conan/internal/api/new/bazel_lib.py | 2 +- test/functional/toolchains/google/test_bazel.py | 12 ++++++------ 6 files changed, 11 insertions(+), 11 deletions(-) diff --git a/conan/cli/commands/new.py b/conan/cli/commands/new.py index ab73c483512..1461eaff450 100644 --- a/conan/cli/commands/new.py +++ b/conan/cli/commands/new.py @@ -68,7 +68,7 @@ def get_template_vars(): template_vars.extend(meta.find_undeclared_variables(ast)) injected_vars = {"conan_version", "package_name", "as_name"} - optional_vars = {"requires", "tool_requires", "outputRootDir"} + optional_vars = {"requires", "tool_requires", "output_root_dir"} template_vars = list(set(template_vars) - injected_vars - optional_vars) template_vars.sort() return template_vars diff --git a/conan/internal/api/new/bazel_7_exe.py b/conan/internal/api/new/bazel_7_exe.py index 02a882870b2..f949088ca48 100644 --- a/conan/internal/api/new/bazel_7_exe.py +++ b/conan/internal/api/new/bazel_7_exe.py @@ -59,7 +59,7 @@ def test(self): _bazel_workspace = " " # Important not empty, so template doesn't discard it _bazel_rc = """\ -{% if outputRootDir is defined %}startup --output_user_root={{outputRootDir}}{% endif %} +{% if output_root_dir is defined %}startup --output_user_root={{output_root_dir}}{% endif %} """ bazel_exe_files_7 = {"conanfile.py": conanfile_exe, diff --git a/conan/internal/api/new/bazel_7_lib.py b/conan/internal/api/new/bazel_7_lib.py index 0b12959fa18..fdc51f6d618 100644 --- a/conan/internal/api/new/bazel_7_lib.py +++ b/conan/internal/api/new/bazel_7_lib.py @@ -117,7 +117,7 @@ def test(self): _bazel_workspace = " " # Important not empty, so template doesn't discard it _bazel_rc = """\ -{% if outputRootDir is defined %}startup --output_user_root={{outputRootDir}}{% endif %} +{% if output_root_dir is defined %}startup --output_user_root={{output_root_dir}}{% endif %} """ _test_bazel_module_bazel = """\ load_conan_dependencies = use_extension("//conan:conan_deps_module_extension.bzl", "conan_extension") diff --git a/conan/internal/api/new/bazel_exe.py b/conan/internal/api/new/bazel_exe.py index 222bb7c6a09..92c70de36a9 100644 --- a/conan/internal/api/new/bazel_exe.py +++ b/conan/internal/api/new/bazel_exe.py @@ -62,7 +62,7 @@ def test(self): _bazel_workspace = " " # Important not empty, so template doesn't discard it _bazel_rc = """\ -{% if outputRootDir is defined %}startup --output_user_root={{outputRootDir}}{% endif %} +{% if output_root_dir is defined %}startup --output_user_root={{output_root_dir}}{% endif %} """ bazel_exe_files = {"conanfile.py": conanfile_exe, diff --git a/conan/internal/api/new/bazel_lib.py b/conan/internal/api/new/bazel_lib.py index 5e8a2bb6937..d888f7f7dd3 100644 --- a/conan/internal/api/new/bazel_lib.py +++ b/conan/internal/api/new/bazel_lib.py @@ -121,7 +121,7 @@ def test(self): _bazel_workspace = " " # Important not empty, so template doesn't discard it _bazel_rc = """\ -{% if outputRootDir is defined %}startup --output_user_root={{outputRootDir}}{% endif %} +{% if output_root_dir is defined %}startup --output_user_root={{output_root_dir}}{% endif %} """ _test_bazel_workspace = """ load("@//conan:dependencies.bzl", "load_conan_dependencies") diff --git a/test/functional/toolchains/google/test_bazel.py b/test/functional/toolchains/google/test_bazel.py index dec23306f72..271cc4c06ce 100644 --- a/test/functional/toolchains/google/test_bazel.py +++ b/test/functional/toolchains/google/test_bazel.py @@ -41,7 +41,7 @@ def base_profile(): @pytest.mark.tool("bazel", "6.3.2") def test_basic_exe_6x(bazelrc, build_type, base_profile, bazel_output_root_dir): client = TestClient(path_with_spaces=False) - client.run(f"new bazel_exe -d name=myapp -d version=1.0 -d outputRootDir={bazel_output_root_dir}") + client.run(f"new bazel_exe -d name=myapp -d version=1.0 -d output_root_dir={bazel_output_root_dir}") # The build: define several configurations that can be activated by passing # the bazel config with tools.google.bazel:configs client.save({"mybazelrc": bazelrc}) @@ -59,7 +59,7 @@ def test_basic_exe_6x(bazelrc, build_type, base_profile, bazel_output_root_dir): @pytest.mark.tool("bazel", "7.1.2") def test_basic_exe(bazelrc, build_type, base_profile, bazel_output_root_dir): client = TestClient(path_with_spaces=False) - client.run(f"new bazel_7_exe -d name=myapp -d version=1.0 -d outputRootDir={bazel_output_root_dir}") + client.run(f"new bazel_7_exe -d name=myapp -d version=1.0 -d output_root_dir={bazel_output_root_dir}") # The build: define several configurations that can be activated by passing # the bazel config with tools.google.bazel:configs client.save({"mybazelrc": bazelrc}) @@ -104,7 +104,7 @@ def test_transitive_libs_consuming_6x(shared, bazel_output_root_dir): client = TestClient(path_with_spaces=False) # A regular library made with Bazel with client.chdir("myfirstlib"): - client.run(f"new bazel_lib -d name=myfirstlib -d version=1.2.11 -d outputRootDir={bazel_output_root_dir}") + client.run(f"new bazel_lib -d name=myfirstlib -d version=1.2.11 -d output_root_dir={bazel_output_root_dir}") conanfile = client.load("conanfile.py") conanfile += """ self.cpp_info.defines.append("MY_DEFINE=\\"MY_VALUE\\"") @@ -121,7 +121,7 @@ def test_transitive_libs_consuming_6x(shared, bazel_output_root_dir): # We prepare a consumer with Bazel (library mysecondlib using myfirstlib) # and a test_package with an example executable os_ = platform.system() - client.run(f"new bazel_lib -d name=mysecondlib -d version=1.0 -d outputRootDir={bazel_output_root_dir}") + client.run(f"new bazel_lib -d name=mysecondlib -d version=1.0 -d output_root_dir={bazel_output_root_dir}") conanfile = client.load("conanfile.py") conanfile = conanfile.replace('generators = "BazelToolchain"', 'generators = "BazelToolchain", "BazelDeps"\n' @@ -230,7 +230,7 @@ def test_transitive_libs_consuming(shared, bazel_output_root_dir): client = TestClient(path_with_spaces=False) # A regular library made with Bazel with client.chdir("myfirstlib"): - client.run(f"new bazel_7_lib -d name=myfirstlib -d version=1.2.11 -d outputRootDir={bazel_output_root_dir}") + client.run(f"new bazel_7_lib -d name=myfirstlib -d version=1.2.11 -d output_root_dir={bazel_output_root_dir}") conanfile = client.load("conanfile.py") conanfile += """ self.cpp_info.defines.append("MY_DEFINE=\\"MY_VALUE\\"") @@ -247,7 +247,7 @@ def test_transitive_libs_consuming(shared, bazel_output_root_dir): # We prepare a consumer with Bazel (library mysecondlib using myfirstlib) # and a test_package with an example executable os_ = platform.system() - client.run(f"new bazel_7_lib -d name=mysecondlib -d version=1.0 -d outputRootDir={bazel_output_root_dir}") + client.run(f"new bazel_7_lib -d name=mysecondlib -d version=1.0 -d output_root_dir={bazel_output_root_dir}") conanfile = client.load("conanfile.py") conanfile = conanfile.replace('generators = "BazelToolchain"', 'generators = "BazelToolchain", "BazelDeps"\n'