Skip to content

Commit 979172f

Browse files
authored
add support for cc_shared_library deps (#1243)
1 parent d70efd6 commit 979172f

File tree

10 files changed

+146
-0
lines changed

10 files changed

+146
-0
lines changed

MODULE.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ module(
66
compatibility_level = 1,
77
)
88

9+
bazel_dep(name = "bazel_features", version = "1.15.0")
910
bazel_dep(name = "bazel_skylib", version = "1.3.0")
1011
bazel_dep(name = "platforms", version = "0.0.5")
1112
bazel_dep(name = "rules_python", version = "0.23.1")

WORKSPACE.bazel

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ local_repository(
1010
path = "examples",
1111
)
1212

13+
load("@bazel_features//:deps.bzl", "bazel_features_deps")
14+
15+
bazel_features_deps()
16+
1317
load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")
1418

1519
bazel_skylib_workspace()

docs/WORKSPACE.bazel

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ load("@rules_foreign_cc//foreign_cc:repositories.bzl", "rules_foreign_cc_depende
99

1010
rules_foreign_cc_dependencies()
1111

12+
load("@bazel_features//:deps.bzl", "bazel_features_deps")
13+
14+
bazel_features_deps()
15+
1216
load("//:stardoc_repository.bzl", "stardoc_repository")
1317

1418
stardoc_repository()

examples/third_party/zlib/BUILD.bazel

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
load("@rules_cc//cc:defs.bzl", "cc_binary")
2+
load("@rules_foreign_cc//foreign_cc:defs.bzl", "cmake", "runnable_binary")
23

34
exports_files(
45
[
@@ -7,6 +8,15 @@ exports_files(
78
visibility = ["//visibility:public"],
89
)
910

11+
filegroup(
12+
name = "shared_usage_srcs",
13+
srcs = [
14+
"CMakeLists.txt",
15+
"zlib-example.cpp",
16+
],
17+
visibility = ["//visibility:public"],
18+
)
19+
1020
cc_binary(
1121
name = "zlib_usage_example",
1222
srcs = ["zlib-example.cpp"],
@@ -19,3 +29,24 @@ sh_test(
1929
data = [":zlib_usage_example"],
2030
visibility = ["//:__pkg__"],
2131
)
32+
33+
cmake(
34+
name = "zlib_shared_usage_example",
35+
dynamic_deps = ["@zlib//:zlib_shared"],
36+
lib_source = "shared_usage_srcs",
37+
out_binaries = ["zlib-example"],
38+
deps = ["@zlib//:zlib_static"],
39+
)
40+
41+
runnable_binary(
42+
name = "run-zlib-example",
43+
binary = "zlib-example",
44+
foreign_cc_target = ":zlib_shared_usage_example",
45+
)
46+
47+
sh_test(
48+
name = "test_shared_zlib",
49+
srcs = ["test_shared_zlib.sh"],
50+
data = [":run-zlib-example"],
51+
visibility = ["//:__pkg__"],
52+
)

examples/third_party/zlib/BUILD.zlib.bazel

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
load("@rules_cc//cc:defs.bzl", "cc_library")
12
load("@rules_foreign_cc//foreign_cc:defs.bzl", "cmake")
23

34
package(default_visibility = ["//visibility:public"])
@@ -28,3 +29,53 @@ cmake(
2829
"//conditions:default": ["libz.a"],
2930
}),
3031
)
32+
33+
cc_library(
34+
name = "zlib_static",
35+
srcs = [
36+
"adler32.c",
37+
"compress.c",
38+
"crc32.c",
39+
"deflate.c",
40+
"gzclose.c",
41+
"gzlib.c",
42+
"gzread.c",
43+
"gzwrite.c",
44+
"infback.c",
45+
"inffast.c",
46+
"inflate.c",
47+
"inftrees.c",
48+
"trees.c",
49+
"uncompr.c",
50+
"zutil.c",
51+
],
52+
hdrs = [
53+
"crc32.h",
54+
"deflate.h",
55+
"gzguts.h",
56+
"inffast.h",
57+
"inffixed.h",
58+
"inflate.h",
59+
"inftrees.h",
60+
"trees.h",
61+
"zconf.h",
62+
"zlib.h",
63+
"zutil.h",
64+
],
65+
copts = select({
66+
"@platforms//os:windows": [],
67+
"//conditions:default": [
68+
"-Wno-deprecated-non-prototype",
69+
"-Wno-unused-variable",
70+
"-Wno-implicit-function-declaration",
71+
],
72+
}),
73+
includes = ["."],
74+
linkstatic = True,
75+
)
76+
77+
cc_shared_library(
78+
name = "zlib_shared",
79+
shared_lib_name = "libz.so",
80+
deps = ["zlib_static"],
81+
)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
cmake_minimum_required(VERSION 3.15)
2+
3+
project(zlib-example)
4+
5+
find_package(ZLIB REQUIRED)
6+
7+
set(SRCS zlib-example.cpp)
8+
9+
add_executable(${PROJECT_NAME} ${SRCS})
10+
11+
target_link_libraries(${PROJECT_NAME} ZLIB::ZLIB)
12+
13+
install(TARGETS ${PROJECT_NAME} DESTINATION bin)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env bash
2+
3+
$(rlocation rules_foreign_cc/examples/cmake/zlib_shared_usage_example)

foreign_cc/private/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ bzl_library(
4444
"//foreign_cc:providers",
4545
"//foreign_cc/private/framework:helpers",
4646
"//foreign_cc/private/framework:platform",
47+
"@bazel_features//:features",
4748
"@bazel_skylib//lib:collections",
4849
"@bazel_skylib//lib:paths",
4950
"@bazel_tools//tools/cpp:toolchain_utils.bzl",

foreign_cc/private/framework.bzl

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
with CMake, configure/make, autotools)
33
"""
44

5+
load("@bazel_features//:features.bzl", "bazel_features")
56
load("@bazel_skylib//lib:collections.bzl", "collections")
67
load("@bazel_skylib//lib:paths.bzl", "paths")
78
load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain")
@@ -98,6 +99,14 @@ CC_EXTERNAL_RULE_ATTRIBUTES = {
9899
default = [],
99100
providers = [CcInfo],
100101
),
102+
"dynamic_deps": attr.label_list(
103+
doc = (
104+
"Same as deps but for cc_shared_library."
105+
),
106+
mandatory = False,
107+
default = [],
108+
# providers = [CcSharedLibraryInfo],
109+
),
101110
"env": attr.string_dict(
102111
doc = (
103112
"Environment variables to set during the build. " +
@@ -866,6 +875,19 @@ def _define_inputs(attrs):
866875
bazel_system_includes += headers_info.include_dirs
867876
bazel_libs += _collect_libs(dep[CcInfo].linking_context)
868877

878+
for dynamic_dep in attrs.dynamic_deps:
879+
if not bazel_features.globals.CcSharedLibraryInfo:
880+
fail("CcSharedLibraryInfo is only available in Bazel 7 or greater")
881+
882+
linker_input = dynamic_dep[bazel_features.globals.CcSharedLibraryInfo].linker_input
883+
bazel_libs += _collect_shared_libs(linker_input)
884+
linking_context = cc_common.create_linking_context(
885+
linker_inputs = depset(direct = [linker_input]),
886+
)
887+
888+
# create a new CcInfo from the CcSharedLibraryInfo linker_input
889+
cc_infos.append(CcInfo(linking_context = linking_context))
890+
869891
# Keep the order of the transitive foreign dependencies
870892
# (the order is important for the correct linking),
871893
# but filter out repeating directories
@@ -989,6 +1011,14 @@ def _collect_libs(cc_linking):
9891011
libs.append(library)
9901012
return collections.uniq(libs)
9911013

1014+
def _collect_shared_libs(cc_linker_input):
1015+
libs = []
1016+
for library_to_link in cc_linker_input.libraries:
1017+
for library in _extract_libraries(library_to_link):
1018+
if library:
1019+
libs.append(library)
1020+
return collections.uniq(libs)
1021+
9921022
def expand_locations_and_make_variables(ctx, unexpanded, attr_name, data):
9931023
"""Expand locations and make variables while ensuring that `execpath` is always set to an absolute path
9941024

foreign_cc/repositories.bzl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,14 @@ def rules_foreign_cc_dependencies(
8383
if register_preinstalled_tools:
8484
preinstalled_toolchains()
8585

86+
maybe(
87+
http_archive,
88+
name = "bazel_features",
89+
sha256 = "ba1282c1aa1d1fffdcf994ab32131d7c7551a9bc960fbf05f42d55a1b930cbfb",
90+
strip_prefix = "bazel_features-1.15.0",
91+
url = "https://github.com/bazel-contrib/bazel_features/releases/download/v1.15.0/bazel_features-v1.15.0.tar.gz",
92+
)
93+
8694
maybe(
8795
http_archive,
8896
name = "bazel_skylib",

0 commit comments

Comments
 (0)