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

Feature: Enable bazel >= 7.1 #16196

Merged
merged 22 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
5 changes: 5 additions & 0 deletions conan/api/subapi/new.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ def get_builtin_template(template_name):
from conan.internal.api.new.msbuild_exe import msbuild_exe_files
from conan.internal.api.new.bazel_lib import bazel_lib_files
from conan.internal.api.new.bazel_exe import bazel_exe_files
from conan.internal.api.new.bazel_7_lib import bazel_lib_files_7
from conan.internal.api.new.bazel_7_exe import bazel_exe_files_7
from conan.internal.api.new.autotools_lib import autotools_lib_files
from conan.internal.api.new.autoools_exe import autotools_exe_files
from conan.internal.api.new.local_recipes_index import local_recipes_index_files
Expand All @@ -36,8 +38,11 @@ def get_builtin_template(template_name):
"meson_exe": meson_exe_files,
"msbuild_lib": msbuild_lib_files,
"msbuild_exe": msbuild_exe_files,
# TODO: Rename xxx_7 to xxx when dropped Bazel 6.x compatibility
"bazel_lib": bazel_lib_files,
"bazel_exe": bazel_exe_files,
"bazel_7_lib": bazel_lib_files_7,
"bazel_7_exe": bazel_exe_files_7,
"autotools_lib": autotools_lib_files,
"autotools_exe": autotools_exe_files,
"alias": alias_file,
Expand Down
70 changes: 70 additions & 0 deletions conan/internal/api/new/bazel_7_exe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
from conan.internal.api.new.cmake_lib import source_cpp, source_h, test_main


conanfile_exe = """
import os
from conan import ConanFile
from conan.tools.google import Bazel, bazel_layout
from conan.tools.files import copy


class {{package_name}}Recipe(ConanFile):
name = "{{name}}"
version = "{{version}}"
package_type = "application"

# Binary configuration
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"

generators = "BazelToolchain"

def layout(self):
bazel_layout(self)

def build(self):
bazel = Bazel(self)
bazel.build(target="//main:{{name}}")

def package(self):
dest_bin = os.path.join(self.package_folder, "bin")
build = os.path.join(self.build_folder, "bazel-bin", "main")
copy(self, "{{name}}", build, dest_bin, keep_path=False)
copy(self, "{{name}}.exe", build, dest_bin, keep_path=False)
"""

test_conanfile_exe_v2 = """from conan import ConanFile
from conan.tools.build import can_run


class {{package_name}}Test(ConanFile):
settings = "os", "compiler", "build_type", "arch"

def requirements(self):
self.requires(self.tested_reference_str)

def test(self):
if can_run(self):
self.run("{{name}}", env="conanrun")
"""

_bazel_build_exe = """\
cc_binary(
name = "{{name}}",
srcs = ["main.cpp", "{{name}}.cpp", "{{name}}.h"]
)
"""

_bazel_workspace = " " # Important not empty, so template doesn't discard it


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,
"test_package/conanfile.py": test_conanfile_exe_v2
}
142 changes: 142 additions & 0 deletions conan/internal/api/new/bazel_7_lib.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
from conan.internal.api.new.cmake_lib import source_cpp, source_h, test_main

conanfile_sources_v2 = """
import os
from conan import ConanFile
from conan.tools.google import Bazel, bazel_layout
from conan.tools.files import copy

class {{package_name}}Recipe(ConanFile):
name = "{{name}}"
version = "{{version}}"
package_type = "library"

# Binary configuration
settings = "os", "compiler", "build_type", "arch"
options = {"shared": [True, False], "fPIC": [True, False]}
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"
generators = "BazelToolchain"

def config_options(self):
if self.settings.os == "Windows":
self.options.rm_safe("fPIC")

def configure(self):
if self.options.shared:
self.options.rm_safe("fPIC")

def layout(self):
bazel_layout(self)

def build(self):
bazel = Bazel(self)
# On Linux platforms, Bazel creates both shared and static libraries by default, and
# it is getting naming conflicts if we use the cc_shared_library rule
if self.options.shared and self.settings.os != "Linux":
# We need to add '--experimental_cc_shared_library' because the project uses
# cc_shared_library to create shared libraries
bazel.build(args=["--experimental_cc_shared_library"], target="//main:{{name}}_shared")
else:
bazel.build(target="//main:{{name}}")

def package(self):
dest_lib = os.path.join(self.package_folder, "lib")
dest_bin = os.path.join(self.package_folder, "bin")
build = os.path.join(self.build_folder, "bazel-bin", "main")
copy(self, "*.so", build, dest_lib, keep_path=False)
copy(self, "*.dll", build, dest_bin, keep_path=False)
copy(self, "*.dylib", build, dest_lib, keep_path=False)
copy(self, "*.a", build, dest_lib, keep_path=False)
copy(self, "*.lib", build, dest_lib, keep_path=False)
copy(self, "{{name}}.h", os.path.join(self.source_folder, "main"),
os.path.join(self.package_folder, "include"), keep_path=False)

def package_info(self):
if self.options.shared and self.settings.os != "Linux":
self.cpp_info.libs = ["{{name}}_shared"]
else:
self.cpp_info.libs = ["{{name}}"]
"""


test_conanfile_v2 = """import os
from conan import ConanFile
from conan.tools.google import Bazel, bazel_layout
from conan.tools.build import can_run


class {{package_name}}TestConan(ConanFile):
settings = "os", "compiler", "build_type", "arch"
generators = "BazelToolchain", "BazelDeps"

def requirements(self):
self.requires(self.tested_reference_str)

def build(self):
bazel = Bazel(self)
bazel.build(target="//main:example")

def layout(self):
bazel_layout(self)

def test(self):
if can_run(self):
cmd = os.path.join(self.cpp.build.bindir, "main", "example")
self.run(cmd, env="conanrun")
"""


_bazel_build_test = """\
cc_binary(
name = "example",
srcs = ["example.cpp"],
deps = [
"@{{name}}//:{{name}}",
],
)
"""

_bazel_build = """\
cc_library(
name = "{{name}}",
srcs = ["{{name}}.cpp"],
hdrs = ["{{name}}.h"],
)
"""

_bazel_build_shared = """
cc_shared_library(
name = "{{name}}_shared",
shared_lib_name = "lib{{name}}_shared.%s",
deps = [":{{name}}"],
)
"""

_bazel_workspace = " " # Important not empty, so template doesn't discard it
_test_bazel_module_bazel = """\
load_conan_dependencies = use_extension("//conan:conan_deps_module_extension.bzl", "conan_extension")
use_repo(load_conan_dependencies, "{{name}}")
"""


def _get_bazel_build():
import platform
os_ = platform.system()
ret = _bazel_build
if os_ != "Linux":
ret += _bazel_build_shared % ("dylib" if os_ == "Darwin" else "dll")
return ret


bazel_lib_files_7 = {"conanfile.py": conanfile_sources_v2,
"main/{{name}}.cpp": source_cpp,
"main/{{name}}.h": source_h,
"main/BUILD": _get_bazel_build(),
"MODULE.bazel": _bazel_workspace,
"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}
7 changes: 4 additions & 3 deletions conan/internal/api/new/bazel_exe.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ class {{package_name}}Recipe(ConanFile):

# Sources are located in the same place as this recipe, copy them to the recipe
exports_sources = "main/*", "WORKSPACE"

generators = "BazelToolchain"

def layout(self):
bazel_layout(self)

def build(self):
from conan.api.output import ConanOutput
ConanOutput().warning("This is the template for Bazel 6.x version, "
"but it will be overridden by the 'bazel_7_exe' template "
"(Bazel >= 7.1 compatible).", warn_tag="deprecated")
bazel = Bazel(self)
bazel.build(target="//main:{{name}}")

Expand All @@ -51,8 +54,6 @@ def test(self):
"""

_bazel_build_exe = """\
load("@rules_cc//cc:defs.bzl", "cc_binary")

cc_binary(
name = "{{name}}",
srcs = ["main.cpp", "{{name}}.cpp", "{{name}}.h"]
Expand Down
8 changes: 4 additions & 4 deletions conan/internal/api/new/bazel_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ def layout(self):
bazel_layout(self)

def build(self):
from conan.api.output import ConanOutput
ConanOutput().warning("This is the template for Bazel 6.x version, "
"but it will be overridden by the 'bazel_7_lib' template "
"(Bazel >= 7.1 compatible).", warn_tag="deprecated")
bazel = Bazel(self)
# On Linux platforms, Bazel creates both shared and static libraries by default, and
# it is getting naming conflicts if we use the cc_shared_library rule
Expand Down Expand Up @@ -90,8 +94,6 @@ def test(self):


_bazel_build_test = """\
load("@rules_cc//cc:defs.bzl", "cc_binary")

cc_binary(
name = "example",
srcs = ["example.cpp"],
Expand All @@ -102,8 +104,6 @@ def test(self):
"""

_bazel_build = """\
load("@rules_cc//cc:defs.bzl", "cc_library")

cc_library(
name = "{{name}}",
srcs = ["{{name}}.cpp"],
Expand Down
2 changes: 2 additions & 0 deletions conan/tools/google/bazel.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ def build(self, args=None, target="//...", clean=True):
# See more info in https://bazel.build/run/bazelrc
bazelrc_paths.extend(self._conanfile.conf.get("tools.google.bazel:bazelrc_path", default=[],
check_type=list))
# Note: In case of error like this: ... https://bcr.bazel.build/: PKIX path building failed
# Check this comment: https://github.com/bazelbuild/bazel/issues/3915#issuecomment-1120894057
command = "bazel"
for rc in bazelrc_paths:
rc = rc.replace("\\", "/")
Expand Down
Loading