-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
537 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/main/java/com/google/devtools/build/lib/rules/cpp/CcStaticLibraryRule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright 2014 The Bazel Authors. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
package com.google.devtools.build.lib.rules.cpp; | ||
|
||
import static com.google.devtools.build.lib.packages.Attribute.attr; | ||
import static com.google.devtools.build.lib.packages.Type.STRING_LIST; | ||
|
||
import com.google.devtools.build.lib.analysis.BaseRuleClasses; | ||
import com.google.devtools.build.lib.analysis.RuleDefinition; | ||
import com.google.devtools.build.lib.analysis.RuleDefinitionEnvironment; | ||
import com.google.devtools.build.lib.packages.RuleClass; | ||
|
||
/** A dummy rule for <code>cc_static_library</code> rule. */ | ||
public class CcStaticLibraryRule implements RuleDefinition { | ||
@Override | ||
public RuleClass build(RuleClass.Builder builder, RuleDefinitionEnvironment env) { | ||
return builder | ||
.add( | ||
attr("tags", STRING_LIST) | ||
.orderIndependent() | ||
.taggable() | ||
.nonconfigurable("low-level attribute, used in TargetUtils without configurations")) | ||
.build(); | ||
} | ||
|
||
@Override | ||
public Metadata getMetadata() { | ||
return Metadata.builder() | ||
.name("cc_static_library") | ||
.factoryClass(BaseRuleClasses.EmptyRuleConfiguredTargetFactory.class) | ||
.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
205 changes: 205 additions & 0 deletions
205
src/main/starlark/builtins_bzl/common/cc/experimental_cc_static_library.bzl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,205 @@ | ||
# Copyright 2021 The Bazel Authors. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""This is an experimental implementation of cc_static_library. | ||
We may change the implementation at any moment or even delete this file. Do not | ||
rely on this. | ||
""" | ||
|
||
load(":common/cc/action_names.bzl", "ACTION_NAMES") | ||
load(":common/cc/cc_helper.bzl", "artifact_category", "cc_helper") | ||
load(":common/cc/semantics.bzl", "semantics") | ||
|
||
CcInfo = _builtins.toplevel.CcInfo | ||
cc_common = _builtins.toplevel.cc_common | ||
|
||
def _shell_quote(s): | ||
return "'" + s.replace("'", "'\\''") + "'" | ||
|
||
def _get_static_library_artifact(ctx, cc_toolchain, suffix = ""): | ||
name = ctx.label.name | ||
new_name = cc_toolchain.get_artifact_name_for_category( | ||
category = artifact_category.STATIC_LIBRARY, | ||
output_name = cc_helper.get_base_name(name), | ||
) | ||
return ctx.actions.declare_file(cc_helper.replace_name(name, new_name + suffix)) | ||
|
||
def _thicken_required(feature_configuration): | ||
return cc_common.is_enabled( | ||
feature_configuration = feature_configuration, | ||
feature_name = "archive_thicken_required", | ||
) | ||
|
||
def _collect_static_libs(deps): | ||
transitive_linker_inputs = [dep[CcInfo].linking_context.linker_inputs for dep in deps] | ||
|
||
# Flattening a depset to get the action inputs. | ||
linker_inputs = depset(transitive = transitive_linker_inputs).to_list() | ||
|
||
libs = [] | ||
for linker_input in linker_inputs: | ||
for lib in linker_input.libraries: | ||
if lib.pic_static_library: | ||
libs.append(lib.pic_static_library) | ||
elif lib.static_library: | ||
libs.append(lib.static_library) | ||
|
||
return depset(libs) | ||
|
||
def _merge_archives(actions, cc_toolchain, feature_configuration, output, libs): | ||
path = cc_common.get_tool_for_action( | ||
feature_configuration = feature_configuration, | ||
action_name = ACTION_NAMES.archive_merge, | ||
) | ||
|
||
variables = cc_common.create_link_variables( | ||
cc_toolchain = cc_toolchain, | ||
feature_configuration = feature_configuration, | ||
output_file = output.path, | ||
is_using_linker = False, | ||
) | ||
flags = cc_common.get_memory_inefficient_command_line( | ||
feature_configuration = feature_configuration, | ||
action_name = ACTION_NAMES.archive_merge, | ||
variables = variables, | ||
) | ||
env = cc_common.get_environment_variables( | ||
feature_configuration = feature_configuration, | ||
action_name = ACTION_NAMES.archive_merge, | ||
variables = variables, | ||
) | ||
|
||
args = actions.args() | ||
args.add_all(flags) | ||
args.add_all(libs) | ||
|
||
actions.run( | ||
inputs = depset(transitive = [cc_toolchain.all_files, libs]), | ||
outputs = [output], | ||
executable = path, | ||
arguments = [args], | ||
env = env, | ||
mnemonic = "ArchiveMerge", | ||
progress_message = "Merging static library %{output}", | ||
) | ||
|
||
def _thicken_archive(name, actions, cc_toolchain, feature_configuration, output, input, libs): | ||
mri_script = actions.declare_file(name + ".mri") | ||
actions.write( | ||
mri_script, | ||
"create {output}\naddlib {input}\nsave\nend\n".format( | ||
output = output.path, | ||
input = input.path, | ||
), | ||
) | ||
|
||
path = cc_common.get_tool_for_action( | ||
feature_configuration = feature_configuration, | ||
action_name = ACTION_NAMES.archive_thicken, | ||
) | ||
|
||
variables = cc_common.create_link_variables( | ||
cc_toolchain = cc_toolchain, | ||
feature_configuration = feature_configuration, | ||
output_file = output.path, | ||
is_using_linker = False, | ||
) | ||
flags = cc_common.get_memory_inefficient_command_line( | ||
feature_configuration = feature_configuration, | ||
action_name = ACTION_NAMES.archive_thicken, | ||
variables = variables, | ||
) | ||
env = cc_common.get_environment_variables( | ||
feature_configuration = feature_configuration, | ||
action_name = ACTION_NAMES.archive_thicken, | ||
variables = variables, | ||
) | ||
|
||
args = [path] + flags | ||
command = " ".join([_shell_quote(arg) for arg in args]) | ||
|
||
actions.run_shell( | ||
command = "{} < {}".format(command, mri_script.path), | ||
inputs = depset(direct = [input, mri_script], transitive = [cc_toolchain.all_files, libs]), | ||
outputs = [output], | ||
env = env, | ||
mnemonic = "ArchiveThicken", | ||
progress_message = "Thickening static library %{output}", | ||
) | ||
|
||
def _cc_static_library_impl(ctx): | ||
semantics.check_experimental_cc_static_library(ctx) | ||
|
||
cc_toolchain = cc_helper.find_cpp_toolchain(ctx) | ||
feature_configuration = cc_common.configure_features( | ||
ctx = ctx, | ||
cc_toolchain = cc_toolchain, | ||
requested_features = ctx.features, | ||
unsupported_features = ctx.disabled_features, | ||
) | ||
|
||
libs = _collect_static_libs(ctx.attr.deps) | ||
|
||
output_archive = _get_static_library_artifact(ctx, cc_toolchain) | ||
if _thicken_required(feature_configuration): | ||
merge_output_archive = _get_static_library_artifact(ctx, cc_toolchain, suffix = ".thin") | ||
_thicken_archive( | ||
name = ctx.label.name, | ||
actions = ctx.actions, | ||
cc_toolchain = cc_toolchain, | ||
feature_configuration = feature_configuration, | ||
output = output_archive, | ||
input = merge_output_archive, | ||
libs = libs, | ||
) | ||
else: | ||
merge_output_archive = output_archive | ||
|
||
_merge_archives( | ||
actions = ctx.actions, | ||
cc_toolchain = cc_toolchain, | ||
feature_configuration = feature_configuration, | ||
output = merge_output_archive, | ||
libs = libs, | ||
) | ||
|
||
runfiles_list = [] | ||
for data_dep in ctx.attr.data: | ||
if data_dep[DefaultInfo].data_runfiles.files: | ||
runfiles_list.append(data_dep[DefaultInfo].data_runfiles) | ||
else: | ||
runfiles_list.append(ctx.runfiles(transitive_files = data_dep[DefaultInfo].files)) | ||
runfiles_list.append(data_dep[DefaultInfo].default_runfiles) | ||
|
||
runfiles = ctx.runfiles().merge_all(runfiles_list) | ||
|
||
return [ | ||
DefaultInfo( | ||
files = depset([output_archive]), | ||
runfiles = runfiles, | ||
), | ||
] | ||
|
||
cc_static_library = rule( | ||
implementation = _cc_static_library_impl, | ||
attrs = { | ||
"data": attr.label_list(allow_files = True), | ||
"deps": attr.label_list(providers = [CcInfo]), | ||
"_cc_toolchain": attr.label(default = "@" + semantics.get_repo() + "//tools/cpp:current_cc_toolchain"), | ||
}, | ||
toolchains = cc_helper.use_cpp_toolchain(), | ||
fragments = ["cpp"], | ||
incompatible_use_toolchain_transition = True, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.