mirror of https://github.com/bazelbuild/rules_cc
Remove need for exported_by or linked_statically_by
For now we will restrict allowed exports to the same package. At the same time static_deps should be used to take into account what should be linked into a shared library. RELNOTES:none PiperOrigin-RevId: 294668451 Change-Id: Ia087519106983bfa9a980e471d3102ab391a53eb
This commit is contained in:
parent
cd0fa354f6
commit
4de26b53a6
|
@ -56,4 +56,10 @@ rules_proto_dependencies()
|
||||||
rules_proto_toolchains()
|
rules_proto_toolchains()
|
||||||
|
|
||||||
load("//cc:repositories.bzl", "rules_cc_toolchains")
|
load("//cc:repositories.bzl", "rules_cc_toolchains")
|
||||||
|
|
||||||
rules_cc_toolchains()
|
rules_cc_toolchains()
|
||||||
|
|
||||||
|
local_repository(
|
||||||
|
name = "test_repo",
|
||||||
|
path = "examples/test_cc_shared_library2",
|
||||||
|
)
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
load("//cc:defs.bzl", "cc_flags_supplier", "cc_library", "compiler_flag")
|
||||||
|
|
||||||
# Copyright 2018 The Bazel Authors. All rights reserved.
|
# Copyright 2018 The Bazel Authors. All rights reserved.
|
||||||
#
|
#
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
@ -16,8 +18,6 @@ package(default_visibility = ["//visibility:public"])
|
||||||
|
|
||||||
licenses(["notice"]) # Apache 2.0
|
licenses(["notice"]) # Apache 2.0
|
||||||
|
|
||||||
load("//cc:defs.bzl", "cc_flags_supplier", "cc_library", "compiler_flag")
|
|
||||||
|
|
||||||
# It is frequently necessary to constrain platforms based on the cc compiler type.
|
# It is frequently necessary to constrain platforms based on the cc compiler type.
|
||||||
constraint_setting(name = "cc_compiler")
|
constraint_setting(name = "cc_compiler")
|
||||||
|
|
||||||
|
@ -80,7 +80,7 @@ alias(
|
||||||
|
|
||||||
filegroup(
|
filegroup(
|
||||||
name = "srcs",
|
name = "srcs",
|
||||||
srcs = glob(["**"])
|
srcs = glob(["**"]),
|
||||||
)
|
)
|
||||||
|
|
||||||
filegroup(
|
filegroup(
|
||||||
|
|
|
@ -26,12 +26,12 @@ GraphNodeInfo = provider(
|
||||||
CcSharedLibraryInfo = provider(
|
CcSharedLibraryInfo = provider(
|
||||||
fields = {
|
fields = {
|
||||||
"dynamic_deps": "All shared libraries depended on transitively",
|
"dynamic_deps": "All shared libraries depended on transitively",
|
||||||
|
"exports": "cc_libraries that are linked statically and exported",
|
||||||
"linker_input": "the resulting linker input artifact for the shared library",
|
"linker_input": "the resulting linker input artifact for the shared library",
|
||||||
"preloaded_deps": "cc_libraries needed by this cc_shared_library that should" +
|
"preloaded_deps": "cc_libraries needed by this cc_shared_library that should" +
|
||||||
" be linked the binary. If this is set, this cc_shared_library has to " +
|
" be linked the binary. If this is set, this cc_shared_library has to " +
|
||||||
" be a direct dependency of the cc_binary",
|
" be a direct dependency of the cc_binary",
|
||||||
"static_libs": "All libraries linked statically into this library",
|
"static_libs": "All libraries linked statically into this library",
|
||||||
"exports": "cc_libraries that are linked statically and exported",
|
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -50,10 +50,11 @@ def _separate_static_and_dynamic_link_libraries(
|
||||||
break
|
break
|
||||||
node = all_children.pop(0)
|
node = all_children.pop(0)
|
||||||
|
|
||||||
if node.label in can_be_linked_dynamically:
|
node_label = str(node.label)
|
||||||
link_dynamically_labels[node.label] = None
|
if node_label in can_be_linked_dynamically:
|
||||||
elif node.label not in preloaded_deps_direct_labels:
|
link_dynamically_labels[node_label] = None
|
||||||
link_statically_labels[node.label] = node.linked_statically_by
|
elif node_label not in preloaded_deps_direct_labels:
|
||||||
|
link_statically_labels[node_label] = node.linked_statically_by
|
||||||
all_children.extend(node.children)
|
all_children.extend(node.children)
|
||||||
return (link_statically_labels, link_dynamically_labels)
|
return (link_statically_labels, link_dynamically_labels)
|
||||||
|
|
||||||
|
@ -137,6 +138,22 @@ def _wrap_static_library_with_alwayslink(ctx, feature_configuration, cc_toolchai
|
||||||
additional_inputs = depset(direct = linker_input.additional_inputs),
|
additional_inputs = depset(direct = linker_input.additional_inputs),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def _check_if_target_under_path(path, target, target_specified):
|
||||||
|
if not _same_package_or_above(path, target):
|
||||||
|
return False
|
||||||
|
if target_specified:
|
||||||
|
return path.name == target.name
|
||||||
|
return True
|
||||||
|
|
||||||
|
def _is_target_specified(path):
|
||||||
|
if path.startswith("//") or path.startswith("@"):
|
||||||
|
if path.find(":") != -1:
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
return True
|
||||||
|
|
||||||
def _filter_inputs(
|
def _filter_inputs(
|
||||||
ctx,
|
ctx,
|
||||||
feature_configuration,
|
feature_configuration,
|
||||||
|
@ -203,10 +220,12 @@ def _filter_inputs(
|
||||||
|
|
||||||
if not can_be_linked_statically:
|
if not can_be_linked_statically:
|
||||||
for static_dep_path in ctx.attr.static_deps:
|
for static_dep_path in ctx.attr.static_deps:
|
||||||
if owner.startswith(static_dep_path):
|
target_specified = _is_target_specified(static_dep_path)
|
||||||
|
static_dep_path_label = ctx.label.relative(static_dep_path)
|
||||||
|
owner_label = linker_input.owner
|
||||||
|
if _check_if_target_under_path(linker_input.owner, static_dep_path_label, target_specified):
|
||||||
can_be_linked_statically = True
|
can_be_linked_statically = True
|
||||||
break
|
break
|
||||||
|
|
||||||
if can_be_linked_statically:
|
if can_be_linked_statically:
|
||||||
static_linker_inputs.append(linker_input)
|
static_linker_inputs.append(linker_input)
|
||||||
else:
|
else:
|
||||||
|
@ -228,6 +247,18 @@ def _filter_inputs(
|
||||||
|
|
||||||
return (static_linker_inputs, dynamic_linker_inputs)
|
return (static_linker_inputs, dynamic_linker_inputs)
|
||||||
|
|
||||||
|
def _same_package_or_above(label_a, label_b):
|
||||||
|
if label_a.workspace_name != label_b.workspace_name:
|
||||||
|
return False
|
||||||
|
package_a_tokenized = label_a.package.split("/")
|
||||||
|
package_b_tokenized = label_b.package.split("/")
|
||||||
|
if len(package_b_tokenized) < len(package_a_tokenized):
|
||||||
|
return False
|
||||||
|
for i in range(len(package_a_tokenized)):
|
||||||
|
if package_a_tokenized[i] != package_b_tokenized[i]:
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
def _cc_shared_library_impl(ctx):
|
def _cc_shared_library_impl(ctx):
|
||||||
cc_toolchain = find_cc_toolchain(ctx)
|
cc_toolchain = find_cc_toolchain(ctx)
|
||||||
feature_configuration = cc_common.configure_features(
|
feature_configuration = cc_common.configure_features(
|
||||||
|
@ -244,6 +275,11 @@ def _cc_shared_library_impl(ctx):
|
||||||
fail("Trying to export a library already exported by a different shared library: " +
|
fail("Trying to export a library already exported by a different shared library: " +
|
||||||
str(export.label))
|
str(export.label))
|
||||||
|
|
||||||
|
if not _same_package_or_above(ctx.label, export[GraphNodeInfo].label):
|
||||||
|
fail(str(export.label) + " cannot be exported from " + str(ctx.label) +
|
||||||
|
" because " + str(export.label) + " is not in the same package " +
|
||||||
|
" or a sub-package")
|
||||||
|
|
||||||
preloaded_deps_direct_labels = {}
|
preloaded_deps_direct_labels = {}
|
||||||
preloaded_dep_merged_cc_info = None
|
preloaded_dep_merged_cc_info = None
|
||||||
if len(ctx.attr.preloaded_deps) != 0:
|
if len(ctx.attr.preloaded_deps) != 0:
|
||||||
|
@ -333,7 +369,7 @@ def _graph_structure_aspect_impl(target, ctx):
|
||||||
linked_statically_by = [str(label) for label in ctx.rule.attr.linked_statically_by]
|
linked_statically_by = [str(label) for label in ctx.rule.attr.linked_statically_by]
|
||||||
|
|
||||||
return [GraphNodeInfo(
|
return [GraphNodeInfo(
|
||||||
label = str(ctx.label),
|
label = ctx.label,
|
||||||
linked_statically_by = linked_statically_by,
|
linked_statically_by = linked_statically_by,
|
||||||
children = children,
|
children = children,
|
||||||
)]
|
)]
|
||||||
|
@ -347,6 +383,7 @@ cc_shared_library = rule(
|
||||||
implementation = _cc_shared_library_impl,
|
implementation = _cc_shared_library_impl,
|
||||||
attrs = {
|
attrs = {
|
||||||
"dynamic_deps": attr.label_list(providers = [CcSharedLibraryInfo]),
|
"dynamic_deps": attr.label_list(providers = [CcSharedLibraryInfo]),
|
||||||
|
"exports": attr.label_list(providers = [CcInfo], aspects = [graph_structure_aspect]),
|
||||||
"preloaded_deps": attr.label_list(providers = [CcInfo]),
|
"preloaded_deps": attr.label_list(providers = [CcInfo]),
|
||||||
# TODO(plf): Replaces linked_statically_by attribute. Instead of
|
# TODO(plf): Replaces linked_statically_by attribute. Instead of
|
||||||
# linked_statically_by attribute in each cc_library we will have the
|
# linked_statically_by attribute in each cc_library we will have the
|
||||||
|
@ -354,7 +391,6 @@ cc_shared_library = rule(
|
||||||
"static_deps": attr.string_list(),
|
"static_deps": attr.string_list(),
|
||||||
"user_link_flags": attr.string_list(),
|
"user_link_flags": attr.string_list(),
|
||||||
"visibility_file": attr.label(allow_single_file = True),
|
"visibility_file": attr.label(allow_single_file = True),
|
||||||
"exports": attr.label_list(providers = [CcInfo], aspects = [graph_structure_aspect]),
|
|
||||||
"_cc_toolchain": attr.label(default = "@bazel_tools//tools/cpp:current_cc_toolchain"),
|
"_cc_toolchain": attr.label(default = "@bazel_tools//tools/cpp:current_cc_toolchain"),
|
||||||
},
|
},
|
||||||
toolchains = ["@rules_cc//cc:toolchain_type"], # copybara-use-repo-external-label
|
toolchains = ["@rules_cc//cc:toolchain_type"], # copybara-use-repo-external-label
|
||||||
|
|
|
@ -85,8 +85,8 @@ def _my_c_archive_impl(ctx):
|
||||||
my_c_archive = rule(
|
my_c_archive = rule(
|
||||||
implementation = _my_c_archive_impl,
|
implementation = _my_c_archive_impl,
|
||||||
attrs = {
|
attrs = {
|
||||||
"object": attr.label(mandatory = True, providers = [MyCCompileInfo]),
|
|
||||||
"deps": attr.label_list(providers = [CcInfo]),
|
"deps": attr.label_list(providers = [CcInfo]),
|
||||||
|
"object": attr.label(mandatory = True, providers = [MyCCompileInfo]),
|
||||||
"_cc_toolchain": attr.label(default = Label("@bazel_tools//tools/cpp:current_cc_toolchain")),
|
"_cc_toolchain": attr.label(default = Label("@bazel_tools//tools/cpp:current_cc_toolchain")),
|
||||||
},
|
},
|
||||||
fragments = ["cpp"],
|
fragments = ["cpp"],
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
load("//examples/my_c_compile:my_c_compile.bzl", "my_c_compile")
|
||||||
|
|
||||||
# Copyright 2019 The Bazel Authors. All rights reserved.
|
# Copyright 2019 The Bazel Authors. All rights reserved.
|
||||||
#
|
#
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
@ -15,8 +17,6 @@
|
||||||
# Example showing how to create a custom Starlark rule that just compiles C sources
|
# Example showing how to create a custom Starlark rule that just compiles C sources
|
||||||
licenses(["notice"])
|
licenses(["notice"])
|
||||||
|
|
||||||
load("//examples/my_c_compile:my_c_compile.bzl", "my_c_compile")
|
|
||||||
|
|
||||||
my_c_compile(
|
my_c_compile(
|
||||||
name = "foo",
|
name = "foo",
|
||||||
src = "foo.c",
|
src = "foo.c",
|
||||||
|
|
|
@ -77,6 +77,10 @@ cc_library(
|
||||||
|
|
||||||
cc_shared_library(
|
cc_shared_library(
|
||||||
name = "bar_so",
|
name = "bar_so",
|
||||||
|
static_deps = [
|
||||||
|
"//examples/test_cc_shared_library:barX",
|
||||||
|
"@test_repo//:bar",
|
||||||
|
],
|
||||||
visibility_file = "bar.lds",
|
visibility_file = "bar.lds",
|
||||||
exports = [
|
exports = [
|
||||||
"bar",
|
"bar",
|
||||||
|
@ -84,10 +88,22 @@ cc_shared_library(
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
cc_library(
|
||||||
|
name = "barX",
|
||||||
|
srcs = ["bar.cc"],
|
||||||
|
hdrs = ["bar.h"],
|
||||||
|
deps = [
|
||||||
|
"@test_repo//:bar",
|
||||||
|
],
|
||||||
|
)
|
||||||
|
|
||||||
cc_library(
|
cc_library(
|
||||||
name = "bar",
|
name = "bar",
|
||||||
srcs = ["bar.cc"],
|
srcs = ["bar.cc"],
|
||||||
hdrs = ["bar.h"],
|
hdrs = ["bar.h"],
|
||||||
|
deps = [
|
||||||
|
"barX",
|
||||||
|
],
|
||||||
)
|
)
|
||||||
|
|
||||||
cc_library(
|
cc_library(
|
||||||
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
load("@rules_cc//cc:defs.bzl", "cc_library")
|
||||||
|
|
||||||
|
cc_library(
|
||||||
|
name = "bar",
|
||||||
|
srcs = ["bar.cc"],
|
||||||
|
hdrs = ["bar.h"],
|
||||||
|
visibility = ["//visibility:public"],
|
||||||
|
)
|
|
@ -0,0 +1 @@
|
||||||
|
workspace(name = "test_repo")
|
|
@ -1,3 +1,5 @@
|
||||||
|
load("//examples/write_cc_toolchain_cpu:write_cc_toolchain_cpu.bzl", "write_cc_toolchain_cpu")
|
||||||
|
|
||||||
# Copyright 2019 The Bazel Authors. All rights reserved.
|
# Copyright 2019 The Bazel Authors. All rights reserved.
|
||||||
#
|
#
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
@ -15,6 +17,4 @@
|
||||||
# Example showing how to get CcToolchainInfo in a custom starlark rule
|
# Example showing how to get CcToolchainInfo in a custom starlark rule
|
||||||
licenses(["notice"])
|
licenses(["notice"])
|
||||||
|
|
||||||
load("//examples/write_cc_toolchain_cpu:write_cc_toolchain_cpu.bzl", "write_cc_toolchain_cpu")
|
|
||||||
|
|
||||||
write_cc_toolchain_cpu(name = "write_me_the_cpu")
|
write_cc_toolchain_cpu(name = "write_me_the_cpu")
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
licenses(["notice"]) # Apache 2.0
|
|
||||||
|
|
||||||
load("@com_google_protobuf//:protobuf.bzl", "py_proto_library")
|
load("@com_google_protobuf//:protobuf.bzl", "py_proto_library")
|
||||||
load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")
|
load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")
|
||||||
load("@rules_proto//proto:defs.bzl", "proto_library")
|
load("@rules_proto//proto:defs.bzl", "proto_library")
|
||||||
|
|
||||||
|
licenses(["notice"]) # Apache 2.0
|
||||||
|
|
||||||
py_proto_library(
|
py_proto_library(
|
||||||
name = "crosstool_config_py_pb2",
|
name = "crosstool_config_py_pb2",
|
||||||
srcs = ["crosstool_config.proto"],
|
srcs = ["crosstool_config.proto"],
|
||||||
|
|
Loading…
Reference in New Issue