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

add support for cc_shared_library deps #1243

Merged
merged 1 commit into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module(
compatibility_level = 1,
)

bazel_dep(name = "bazel_features", version = "1.15.0")
bazel_dep(name = "bazel_skylib", version = "1.3.0")
bazel_dep(name = "platforms", version = "0.0.5")
bazel_dep(name = "rules_python", version = "0.23.1")
Expand Down
4 changes: 4 additions & 0 deletions WORKSPACE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ local_repository(
path = "examples",
)

load("@bazel_features//:deps.bzl", "bazel_features_deps")

bazel_features_deps()

load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")

bazel_skylib_workspace()
Expand Down
4 changes: 4 additions & 0 deletions docs/WORKSPACE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ load("@rules_foreign_cc//foreign_cc:repositories.bzl", "rules_foreign_cc_depende

rules_foreign_cc_dependencies()

load("@bazel_features//:deps.bzl", "bazel_features_deps")

bazel_features_deps()

load("//:stardoc_repository.bzl", "stardoc_repository")

stardoc_repository()
Expand Down
31 changes: 31 additions & 0 deletions examples/third_party/zlib/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
load("@rules_cc//cc:defs.bzl", "cc_binary")
load("@rules_foreign_cc//foreign_cc:defs.bzl", "cmake", "runnable_binary")

exports_files(
[
Expand All @@ -7,6 +8,15 @@ exports_files(
visibility = ["//visibility:public"],
)

filegroup(
name = "shared_usage_srcs",
srcs = [
"CMakeLists.txt",
"zlib-example.cpp",
],
visibility = ["//visibility:public"],
)

cc_binary(
name = "zlib_usage_example",
srcs = ["zlib-example.cpp"],
Expand All @@ -19,3 +29,24 @@ sh_test(
data = [":zlib_usage_example"],
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__"],
)
51 changes: 51 additions & 0 deletions examples/third_party/zlib/BUILD.zlib.bazel
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
load("@rules_cc//cc:defs.bzl", "cc_library")
load("@rules_foreign_cc//foreign_cc:defs.bzl", "cmake")

package(default_visibility = ["//visibility:public"])
Expand Down Expand Up @@ -28,3 +29,53 @@ cmake(
"//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"],
)
13 changes: 13 additions & 0 deletions examples/third_party/zlib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
3 changes: 3 additions & 0 deletions examples/third_party/zlib/test_shared_zlib.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash

$(rlocation rules_foreign_cc/examples/cmake/zlib_shared_usage_example)
1 change: 1 addition & 0 deletions foreign_cc/private/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ bzl_library(
"//foreign_cc:providers",
"//foreign_cc/private/framework:helpers",
"//foreign_cc/private/framework:platform",
"@bazel_features//:features",
"@bazel_skylib//lib:collections",
"@bazel_skylib//lib:paths",
"@bazel_tools//tools/cpp:toolchain_utils.bzl",
Expand Down
30 changes: 30 additions & 0 deletions foreign_cc/private/framework.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
with CMake, configure/make, autotools)
"""

load("@bazel_features//:features.bzl", "bazel_features")
load("@bazel_skylib//lib:collections.bzl", "collections")
load("@bazel_skylib//lib:paths.bzl", "paths")
load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain")
Expand Down Expand Up @@ -98,6 +99,14 @@ CC_EXTERNAL_RULE_ATTRIBUTES = {
default = [],
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(
doc = (
"Environment variables to set during the build. " +
Expand Down Expand Up @@ -866,6 +875,19 @@ def _define_inputs(attrs):
bazel_system_includes += headers_info.include_dirs
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
# (the order is important for the correct linking),
# but filter out repeating directories
Expand Down Expand Up @@ -989,6 +1011,14 @@ def _collect_libs(cc_linking):
libs.append(library)
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):
"""Expand locations and make variables while ensuring that `execpath` is always set to an absolute path

Expand Down
8 changes: 8 additions & 0 deletions foreign_cc/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ def rules_foreign_cc_dependencies(
if register_preinstalled_tools:
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(
http_archive,
name = "bazel_skylib",
Expand Down
Loading