Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[new templates][bazel] Added .bazelrc to new templates #16620

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion conan/cli/commands/new.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
franramirez688 marked this conversation as resolved.
Show resolved Hide resolved
template_vars = list(set(template_vars) - injected_vars - optional_vars)
template_vars.sort()
return template_vars
Expand Down
7 changes: 5 additions & 2 deletions conan/internal/api/new/bazel_7_exe.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -58,13 +58,16 @@ 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,
"main/{{name}}.h": source_h,
"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
}
9 changes: 7 additions & 2 deletions conan/internal/api/new/bazel_7_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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}}")
Expand All @@ -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}
7 changes: 5 additions & 2 deletions conan/internal/api/new/bazel_exe.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
czoido marked this conversation as resolved.
Show resolved Hide resolved
generators = "BazelToolchain"

def layout(self):
Expand Down Expand Up @@ -61,13 +61,16 @@ 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,
"main/{{name}}.h": source_h,
"main/main.cpp": test_main,
"main/BUILD": _bazel_build_exe,
"WORKSPACE": _bazel_workspace,
".bazelrc": _bazel_rc,
"test_package/conanfile.py": test_conanfile_exe_v2
}
9 changes: 7 additions & 2 deletions conan/internal/api/new/bazel_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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()
Expand All @@ -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}
26 changes: 16 additions & 10 deletions test/functional/toolchains/google/test_bazel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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).replace("\\", "/")


@pytest.fixture(scope="module")
def bazelrc():
return textwrap.dedent("""
Expand Down Expand Up @@ -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):
franramirez688 marked this conversation as resolved.
Show resolved Hide resolved
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:<config> define several configurations that can be activated by passing
# the bazel config with tools.google.bazel:configs
client.save({"mybazelrc": bazelrc})
Expand All @@ -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:<config> define several configurations that can be activated by passing
# the bazel config with tools.google.bazel:configs
client.save({"mybazelrc": bazelrc})
Expand All @@ -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

Expand Down Expand Up @@ -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\\"")
Expand All @@ -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'
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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\\"")
Expand All @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -60,11 +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={bazel_root_dir}",
"main/BUILD": BUILD,
"main/myapp.cpp": gen_function_cpp(name="myapp"),
"main/myapp.h": gen_function_h(name="myapp"),
Expand Down