Skip to content

Commit 6d4118a

Browse files
Neeeflixfranramirez688memshardedczoido
authored
Feature: Enable bazel >= 7.1 (#16196)
* feat: Rename template to repository template * feat: Make rule_cc a cannonical repository https://bazel.build/external/overview#canonical-repo-name * feat: Add conan_deps_repo_rules to generate repos * feat: Generate module file to be used by consumers * feat: Add appropriate documentation for consumers * feat: Add content based test for conan_deps_module_extension * Added Bazel 7.x templates. Minor BazelDeps changes. Adding functional/integration tests. Conftest with bazel 6.x and 7.x * Added both versions to CI+ * Not version as kwarg * Added Linux CI bazel folders * docstrings * Update conan/tools/google/bazeldeps.py Co-authored-by: James <memsharded@gmail.com> * Typos * Removed rules loading * Using less common word * Using root_module_direct_deps = 'all' * Renamed and deprecated warning * Useless starting msg * Renamed modules * Better within the build() method * Fixed docstrings --------- Co-authored-by: Francisco Ramirez de Anton <franchuti688@gmail.com> Co-authored-by: James <memsharded@gmail.com> Co-authored-by: Carlos Zoido <mrgalleta@gmail.com>
1 parent fc9fded commit 6d4118a

File tree

10 files changed

+503
-36
lines changed

10 files changed

+503
-36
lines changed

conan/api/subapi/new.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ def get_builtin_template(template_name):
2626
from conan.internal.api.new.msbuild_exe import msbuild_exe_files
2727
from conan.internal.api.new.bazel_lib import bazel_lib_files
2828
from conan.internal.api.new.bazel_exe import bazel_exe_files
29+
from conan.internal.api.new.bazel_7_lib import bazel_lib_files_7
30+
from conan.internal.api.new.bazel_7_exe import bazel_exe_files_7
2931
from conan.internal.api.new.autotools_lib import autotools_lib_files
3032
from conan.internal.api.new.autoools_exe import autotools_exe_files
3133
from conan.internal.api.new.local_recipes_index import local_recipes_index_files
@@ -37,8 +39,11 @@ def get_builtin_template(template_name):
3739
"meson_exe": meson_exe_files,
3840
"msbuild_lib": msbuild_lib_files,
3941
"msbuild_exe": msbuild_exe_files,
42+
# TODO: Rename xxx_7 to xxx when dropped Bazel 6.x compatibility
4043
"bazel_lib": bazel_lib_files,
4144
"bazel_exe": bazel_exe_files,
45+
"bazel_7_lib": bazel_lib_files_7,
46+
"bazel_7_exe": bazel_exe_files_7,
4247
"autotools_lib": autotools_lib_files,
4348
"autotools_exe": autotools_exe_files,
4449
"alias": alias_file,

conan/internal/api/new/bazel_7_exe.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
from conan.internal.api.new.cmake_lib import source_cpp, source_h, test_main
2+
3+
4+
conanfile_exe = """
5+
import os
6+
from conan import ConanFile
7+
from conan.tools.google import Bazel, bazel_layout
8+
from conan.tools.files import copy
9+
10+
11+
class {{package_name}}Recipe(ConanFile):
12+
name = "{{name}}"
13+
version = "{{version}}"
14+
package_type = "application"
15+
16+
# Binary configuration
17+
settings = "os", "compiler", "build_type", "arch"
18+
19+
# Sources are located in the same place as this recipe, copy them to the recipe
20+
exports_sources = "main/*", "MODULE.bazel"
21+
22+
generators = "BazelToolchain"
23+
24+
def layout(self):
25+
bazel_layout(self)
26+
27+
def build(self):
28+
bazel = Bazel(self)
29+
bazel.build(target="//main:{{name}}")
30+
31+
def package(self):
32+
dest_bin = os.path.join(self.package_folder, "bin")
33+
build = os.path.join(self.build_folder, "bazel-bin", "main")
34+
copy(self, "{{name}}", build, dest_bin, keep_path=False)
35+
copy(self, "{{name}}.exe", build, dest_bin, keep_path=False)
36+
"""
37+
38+
test_conanfile_exe_v2 = """from conan import ConanFile
39+
from conan.tools.build import can_run
40+
41+
42+
class {{package_name}}Test(ConanFile):
43+
settings = "os", "compiler", "build_type", "arch"
44+
45+
def requirements(self):
46+
self.requires(self.tested_reference_str)
47+
48+
def test(self):
49+
if can_run(self):
50+
self.run("{{name}}", env="conanrun")
51+
"""
52+
53+
_bazel_build_exe = """\
54+
cc_binary(
55+
name = "{{name}}",
56+
srcs = ["main.cpp", "{{name}}.cpp", "{{name}}.h"]
57+
)
58+
"""
59+
60+
_bazel_workspace = " " # Important not empty, so template doesn't discard it
61+
62+
63+
bazel_exe_files_7 = {"conanfile.py": conanfile_exe,
64+
"main/{{name}}.cpp": source_cpp,
65+
"main/{{name}}.h": source_h,
66+
"main/main.cpp": test_main,
67+
"main/BUILD": _bazel_build_exe,
68+
"MODULE.bazel": _bazel_workspace,
69+
"test_package/conanfile.py": test_conanfile_exe_v2
70+
}

conan/internal/api/new/bazel_7_lib.py

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
from conan.internal.api.new.cmake_lib import source_cpp, source_h, test_main
2+
3+
conanfile_sources_v2 = """
4+
import os
5+
from conan import ConanFile
6+
from conan.tools.google import Bazel, bazel_layout
7+
from conan.tools.files import copy
8+
9+
class {{package_name}}Recipe(ConanFile):
10+
name = "{{name}}"
11+
version = "{{version}}"
12+
package_type = "library"
13+
14+
# Binary configuration
15+
settings = "os", "compiler", "build_type", "arch"
16+
options = {"shared": [True, False], "fPIC": [True, False]}
17+
default_options = {"shared": False, "fPIC": True}
18+
19+
# Sources are located in the same place as this recipe, copy them to the recipe
20+
exports_sources = "main/*", "MODULE.bazel"
21+
generators = "BazelToolchain"
22+
23+
def config_options(self):
24+
if self.settings.os == "Windows":
25+
self.options.rm_safe("fPIC")
26+
27+
def configure(self):
28+
if self.options.shared:
29+
self.options.rm_safe("fPIC")
30+
31+
def layout(self):
32+
bazel_layout(self)
33+
34+
def build(self):
35+
bazel = Bazel(self)
36+
# On Linux platforms, Bazel creates both shared and static libraries by default, and
37+
# it is getting naming conflicts if we use the cc_shared_library rule
38+
if self.options.shared and self.settings.os != "Linux":
39+
# We need to add '--experimental_cc_shared_library' because the project uses
40+
# cc_shared_library to create shared libraries
41+
bazel.build(args=["--experimental_cc_shared_library"], target="//main:{{name}}_shared")
42+
else:
43+
bazel.build(target="//main:{{name}}")
44+
45+
def package(self):
46+
dest_lib = os.path.join(self.package_folder, "lib")
47+
dest_bin = os.path.join(self.package_folder, "bin")
48+
build = os.path.join(self.build_folder, "bazel-bin", "main")
49+
copy(self, "*.so", build, dest_lib, keep_path=False)
50+
copy(self, "*.dll", build, dest_bin, keep_path=False)
51+
copy(self, "*.dylib", build, dest_lib, keep_path=False)
52+
copy(self, "*.a", build, dest_lib, keep_path=False)
53+
copy(self, "*.lib", build, dest_lib, keep_path=False)
54+
copy(self, "{{name}}.h", os.path.join(self.source_folder, "main"),
55+
os.path.join(self.package_folder, "include"), keep_path=False)
56+
57+
def package_info(self):
58+
if self.options.shared and self.settings.os != "Linux":
59+
self.cpp_info.libs = ["{{name}}_shared"]
60+
else:
61+
self.cpp_info.libs = ["{{name}}"]
62+
"""
63+
64+
65+
test_conanfile_v2 = """import os
66+
from conan import ConanFile
67+
from conan.tools.google import Bazel, bazel_layout
68+
from conan.tools.build import can_run
69+
70+
71+
class {{package_name}}TestConan(ConanFile):
72+
settings = "os", "compiler", "build_type", "arch"
73+
generators = "BazelToolchain", "BazelDeps"
74+
75+
def requirements(self):
76+
self.requires(self.tested_reference_str)
77+
78+
def build(self):
79+
bazel = Bazel(self)
80+
bazel.build(target="//main:example")
81+
82+
def layout(self):
83+
bazel_layout(self)
84+
85+
def test(self):
86+
if can_run(self):
87+
cmd = os.path.join(self.cpp.build.bindir, "main", "example")
88+
self.run(cmd, env="conanrun")
89+
"""
90+
91+
92+
_bazel_build_test = """\
93+
cc_binary(
94+
name = "example",
95+
srcs = ["example.cpp"],
96+
deps = [
97+
"@{{name}}//:{{name}}",
98+
],
99+
)
100+
"""
101+
102+
_bazel_build = """\
103+
cc_library(
104+
name = "{{name}}",
105+
srcs = ["{{name}}.cpp"],
106+
hdrs = ["{{name}}.h"],
107+
)
108+
"""
109+
110+
_bazel_build_shared = """
111+
cc_shared_library(
112+
name = "{{name}}_shared",
113+
shared_lib_name = "lib{{name}}_shared.%s",
114+
deps = [":{{name}}"],
115+
)
116+
"""
117+
118+
_bazel_workspace = " " # Important not empty, so template doesn't discard it
119+
_test_bazel_module_bazel = """\
120+
load_conan_dependencies = use_extension("//conan:conan_deps_module_extension.bzl", "conan_extension")
121+
use_repo(load_conan_dependencies, "{{name}}")
122+
"""
123+
124+
125+
def _get_bazel_build():
126+
import platform
127+
os_ = platform.system()
128+
ret = _bazel_build
129+
if os_ != "Linux":
130+
ret += _bazel_build_shared % ("dylib" if os_ == "Darwin" else "dll")
131+
return ret
132+
133+
134+
bazel_lib_files_7 = {"conanfile.py": conanfile_sources_v2,
135+
"main/{{name}}.cpp": source_cpp,
136+
"main/{{name}}.h": source_h,
137+
"main/BUILD": _get_bazel_build(),
138+
"MODULE.bazel": _bazel_workspace,
139+
"test_package/conanfile.py": test_conanfile_v2,
140+
"test_package/main/example.cpp": test_main,
141+
"test_package/main/BUILD": _bazel_build_test,
142+
"test_package/MODULE.bazel": _test_bazel_module_bazel}

conan/internal/api/new/bazel_exe.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,16 @@ class {{package_name}}Recipe(ConanFile):
1818
1919
# Sources are located in the same place as this recipe, copy them to the recipe
2020
exports_sources = "main/*", "WORKSPACE"
21-
2221
generators = "BazelToolchain"
2322
2423
def layout(self):
2524
bazel_layout(self)
2625
2726
def build(self):
27+
from conan.api.output import ConanOutput
28+
ConanOutput().warning("This is the template for Bazel 6.x version, "
29+
"but it will be overridden by the 'bazel_7_exe' template "
30+
"(Bazel >= 7.1 compatible).", warn_tag="deprecated")
2831
bazel = Bazel(self)
2932
bazel.build(target="//main:{{name}}")
3033
@@ -51,8 +54,6 @@ def test(self):
5154
"""
5255

5356
_bazel_build_exe = """\
54-
load("@rules_cc//cc:defs.bzl", "cc_binary")
55-
5657
cc_binary(
5758
name = "{{name}}",
5859
srcs = ["main.cpp", "{{name}}.cpp", "{{name}}.h"]

conan/internal/api/new/bazel_lib.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ def layout(self):
3232
bazel_layout(self)
3333
3434
def build(self):
35+
from conan.api.output import ConanOutput
36+
ConanOutput().warning("This is the template for Bazel 6.x version, "
37+
"but it will be overridden by the 'bazel_7_lib' template "
38+
"(Bazel >= 7.1 compatible).", warn_tag="deprecated")
3539
bazel = Bazel(self)
3640
# On Linux platforms, Bazel creates both shared and static libraries by default, and
3741
# it is getting naming conflicts if we use the cc_shared_library rule
@@ -90,8 +94,6 @@ def test(self):
9094

9195

9296
_bazel_build_test = """\
93-
load("@rules_cc//cc:defs.bzl", "cc_binary")
94-
9597
cc_binary(
9698
name = "example",
9799
srcs = ["example.cpp"],
@@ -102,8 +104,6 @@ def test(self):
102104
"""
103105

104106
_bazel_build = """\
105-
load("@rules_cc//cc:defs.bzl", "cc_library")
106-
107107
cc_library(
108108
name = "{{name}}",
109109
srcs = ["{{name}}.cpp"],

conan/tools/google/bazel.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ def build(self, args=None, target="//...", clean=True):
5353
# See more info in https://bazel.build/run/bazelrc
5454
bazelrc_paths.extend(self._conanfile.conf.get("tools.google.bazel:bazelrc_path", default=[],
5555
check_type=list))
56+
# Note: In case of error like this: ... https://bcr.bazel.build/: PKIX path building failed
57+
# Check this comment: https://github.com/bazelbuild/bazel/issues/3915#issuecomment-1120894057
5658
command = "bazel"
5759
for rc in bazelrc_paths:
5860
rc = rc.replace("\\", "/")

0 commit comments

Comments
 (0)