add support for cc_shared_library deps (#1243)

This commit is contained in:
Matt Smith 2024-08-13 20:52:54 +10:00 committed by GitHub
parent d70efd6d8c
commit 979172f2fc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 146 additions and 0 deletions

View File

@ -6,6 +6,7 @@ module(
compatibility_level = 1, compatibility_level = 1,
) )
bazel_dep(name = "bazel_features", version = "1.15.0")
bazel_dep(name = "bazel_skylib", version = "1.3.0") bazel_dep(name = "bazel_skylib", version = "1.3.0")
bazel_dep(name = "platforms", version = "0.0.5") bazel_dep(name = "platforms", version = "0.0.5")
bazel_dep(name = "rules_python", version = "0.23.1") bazel_dep(name = "rules_python", version = "0.23.1")

View File

@ -10,6 +10,10 @@ local_repository(
path = "examples", path = "examples",
) )
load("@bazel_features//:deps.bzl", "bazel_features_deps")
bazel_features_deps()
load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace") load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")
bazel_skylib_workspace() bazel_skylib_workspace()

View File

@ -9,6 +9,10 @@ load("@rules_foreign_cc//foreign_cc:repositories.bzl", "rules_foreign_cc_depende
rules_foreign_cc_dependencies() rules_foreign_cc_dependencies()
load("@bazel_features//:deps.bzl", "bazel_features_deps")
bazel_features_deps()
load("//:stardoc_repository.bzl", "stardoc_repository") load("//:stardoc_repository.bzl", "stardoc_repository")
stardoc_repository() stardoc_repository()

View File

@ -1,4 +1,5 @@
load("@rules_cc//cc:defs.bzl", "cc_binary") load("@rules_cc//cc:defs.bzl", "cc_binary")
load("@rules_foreign_cc//foreign_cc:defs.bzl", "cmake", "runnable_binary")
exports_files( exports_files(
[ [
@ -7,6 +8,15 @@ exports_files(
visibility = ["//visibility:public"], visibility = ["//visibility:public"],
) )
filegroup(
name = "shared_usage_srcs",
srcs = [
"CMakeLists.txt",
"zlib-example.cpp",
],
visibility = ["//visibility:public"],
)
cc_binary( cc_binary(
name = "zlib_usage_example", name = "zlib_usage_example",
srcs = ["zlib-example.cpp"], srcs = ["zlib-example.cpp"],
@ -19,3 +29,24 @@ sh_test(
data = [":zlib_usage_example"], data = [":zlib_usage_example"],
visibility = ["//:__pkg__"], visibility = ["//:__pkg__"],
) )
cmake(
name = "zlib_shared_usage_example",
dynamic_deps = ["@zlib//:zlib_shared"],
lib_source = "shared_usage_srcs",
out_binaries = ["zlib-example"],
deps = ["@zlib//:zlib_static"],
)
runnable_binary(
name = "run-zlib-example",
binary = "zlib-example",
foreign_cc_target = ":zlib_shared_usage_example",
)
sh_test(
name = "test_shared_zlib",
srcs = ["test_shared_zlib.sh"],
data = [":run-zlib-example"],
visibility = ["//:__pkg__"],
)

View File

@ -1,3 +1,4 @@
load("@rules_cc//cc:defs.bzl", "cc_library")
load("@rules_foreign_cc//foreign_cc:defs.bzl", "cmake") load("@rules_foreign_cc//foreign_cc:defs.bzl", "cmake")
package(default_visibility = ["//visibility:public"]) package(default_visibility = ["//visibility:public"])
@ -28,3 +29,53 @@ cmake(
"//conditions:default": ["libz.a"], "//conditions:default": ["libz.a"],
}), }),
) )
cc_library(
name = "zlib_static",
srcs = [
"adler32.c",
"compress.c",
"crc32.c",
"deflate.c",
"gzclose.c",
"gzlib.c",
"gzread.c",
"gzwrite.c",
"infback.c",
"inffast.c",
"inflate.c",
"inftrees.c",
"trees.c",
"uncompr.c",
"zutil.c",
],
hdrs = [
"crc32.h",
"deflate.h",
"gzguts.h",
"inffast.h",
"inffixed.h",
"inflate.h",
"inftrees.h",
"trees.h",
"zconf.h",
"zlib.h",
"zutil.h",
],
copts = select({
"@platforms//os:windows": [],
"//conditions:default": [
"-Wno-deprecated-non-prototype",
"-Wno-unused-variable",
"-Wno-implicit-function-declaration",
],
}),
includes = ["."],
linkstatic = True,
)
cc_shared_library(
name = "zlib_shared",
shared_lib_name = "libz.so",
deps = ["zlib_static"],
)

View File

@ -0,0 +1,13 @@
cmake_minimum_required(VERSION 3.15)
project(zlib-example)
find_package(ZLIB REQUIRED)
set(SRCS zlib-example.cpp)
add_executable(${PROJECT_NAME} ${SRCS})
target_link_libraries(${PROJECT_NAME} ZLIB::ZLIB)
install(TARGETS ${PROJECT_NAME} DESTINATION bin)

View File

@ -0,0 +1,3 @@
#!/usr/bin/env bash
$(rlocation rules_foreign_cc/examples/cmake/zlib_shared_usage_example)

View File

@ -44,6 +44,7 @@ bzl_library(
"//foreign_cc:providers", "//foreign_cc:providers",
"//foreign_cc/private/framework:helpers", "//foreign_cc/private/framework:helpers",
"//foreign_cc/private/framework:platform", "//foreign_cc/private/framework:platform",
"@bazel_features//:features",
"@bazel_skylib//lib:collections", "@bazel_skylib//lib:collections",
"@bazel_skylib//lib:paths", "@bazel_skylib//lib:paths",
"@bazel_tools//tools/cpp:toolchain_utils.bzl", "@bazel_tools//tools/cpp:toolchain_utils.bzl",

View File

@ -2,6 +2,7 @@
with CMake, configure/make, autotools) with CMake, configure/make, autotools)
""" """
load("@bazel_features//:features.bzl", "bazel_features")
load("@bazel_skylib//lib:collections.bzl", "collections") load("@bazel_skylib//lib:collections.bzl", "collections")
load("@bazel_skylib//lib:paths.bzl", "paths") load("@bazel_skylib//lib:paths.bzl", "paths")
load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain") load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain")
@ -98,6 +99,14 @@ CC_EXTERNAL_RULE_ATTRIBUTES = {
default = [], default = [],
providers = [CcInfo], providers = [CcInfo],
), ),
"dynamic_deps": attr.label_list(
doc = (
"Same as deps but for cc_shared_library."
),
mandatory = False,
default = [],
# providers = [CcSharedLibraryInfo],
),
"env": attr.string_dict( "env": attr.string_dict(
doc = ( doc = (
"Environment variables to set during the build. " + "Environment variables to set during the build. " +
@ -866,6 +875,19 @@ def _define_inputs(attrs):
bazel_system_includes += headers_info.include_dirs bazel_system_includes += headers_info.include_dirs
bazel_libs += _collect_libs(dep[CcInfo].linking_context) bazel_libs += _collect_libs(dep[CcInfo].linking_context)
for dynamic_dep in attrs.dynamic_deps:
if not bazel_features.globals.CcSharedLibraryInfo:
fail("CcSharedLibraryInfo is only available in Bazel 7 or greater")
linker_input = dynamic_dep[bazel_features.globals.CcSharedLibraryInfo].linker_input
bazel_libs += _collect_shared_libs(linker_input)
linking_context = cc_common.create_linking_context(
linker_inputs = depset(direct = [linker_input]),
)
# create a new CcInfo from the CcSharedLibraryInfo linker_input
cc_infos.append(CcInfo(linking_context = linking_context))
# Keep the order of the transitive foreign dependencies # Keep the order of the transitive foreign dependencies
# (the order is important for the correct linking), # (the order is important for the correct linking),
# but filter out repeating directories # but filter out repeating directories
@ -989,6 +1011,14 @@ def _collect_libs(cc_linking):
libs.append(library) libs.append(library)
return collections.uniq(libs) return collections.uniq(libs)
def _collect_shared_libs(cc_linker_input):
libs = []
for library_to_link in cc_linker_input.libraries:
for library in _extract_libraries(library_to_link):
if library:
libs.append(library)
return collections.uniq(libs)
def expand_locations_and_make_variables(ctx, unexpanded, attr_name, data): def expand_locations_and_make_variables(ctx, unexpanded, attr_name, data):
"""Expand locations and make variables while ensuring that `execpath` is always set to an absolute path """Expand locations and make variables while ensuring that `execpath` is always set to an absolute path

View File

@ -83,6 +83,14 @@ def rules_foreign_cc_dependencies(
if register_preinstalled_tools: if register_preinstalled_tools:
preinstalled_toolchains() preinstalled_toolchains()
maybe(
http_archive,
name = "bazel_features",
sha256 = "ba1282c1aa1d1fffdcf994ab32131d7c7551a9bc960fbf05f42d55a1b930cbfb",
strip_prefix = "bazel_features-1.15.0",
url = "https://github.com/bazel-contrib/bazel_features/releases/download/v1.15.0/bazel_features-v1.15.0.tar.gz",
)
maybe( maybe(
http_archive, http_archive,
name = "bazel_skylib", name = "bazel_skylib",