Skip to content

Commit

Permalink
Initial setup
Browse files Browse the repository at this point in the history
  • Loading branch information
fmeum committed Dec 8, 2022
1 parent 563b225 commit fbcb331
Show file tree
Hide file tree
Showing 21 changed files with 537 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
import com.google.devtools.build.lib.rules.cpp.CcSharedLibraryPermissionsRule;
import com.google.devtools.build.lib.rules.cpp.CcSharedLibraryRule;
import com.google.devtools.build.lib.rules.cpp.CcStarlarkInternal;
import com.google.devtools.build.lib.rules.cpp.CcStaticLibraryRule;
import com.google.devtools.build.lib.rules.cpp.proto.CcProtoAspect;
import com.google.devtools.build.lib.rules.cpp.proto.CcProtoLibraryRule;
import com.google.devtools.build.lib.rules.objc.BazelObjcStarlarkInternal;
Expand Down Expand Up @@ -504,6 +505,7 @@ public void init(ConfiguredRuleClassProvider.Builder builder) {
builder.addRuleDefinition(new LocalConfigPlatformRule());
builder.addRuleDefinition(new CcSharedLibraryRule());
builder.addRuleDefinition(new CcSharedLibraryPermissionsRule());
builder.addRuleDefinition(new CcStaticLibraryRule());

try {
builder.addWorkspaceFileSuffix(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,18 @@ public final class BuildLanguageOptions extends OptionsBase {
+ "cc_shared_library will be available")
public boolean experimentalCcSharedLibrary;

@Option(
name = "experimental_cc_static_library",
defaultValue = "false",
documentationCategory = OptionDocumentationCategory.STARLARK_SEMANTICS,
effectTags = {OptionEffectTag.BUILD_FILE_SEMANTICS, OptionEffectTag.LOADING_AND_ANALYSIS},
metadataTags = {
OptionMetadataTag.EXPERIMENTAL,
},
help =
"If set to true, rule attributes and Starlark API methods needed for the rule "
+ "cc_static_library will be available")
public boolean experimentalCcStaticLibrary;
@Option(
name = "incompatible_require_linker_input_cc_api",
defaultValue = "true",
Expand Down Expand Up @@ -680,6 +692,7 @@ public StarlarkSemantics toStarlarkSemantics() {
.setBool(EXPERIMENTAL_GOOGLE_LEGACY_API, experimentalGoogleLegacyApi)
.setBool(EXPERIMENTAL_PLATFORMS_API, experimentalPlatformsApi)
.setBool(EXPERIMENTAL_CC_SHARED_LIBRARY, experimentalCcSharedLibrary)
.setBool(EXPERIMENTAL_CC_STATIC_LIBRARY, experimentalCcStaticLibrary)
.setBool(EXPERIMENTAL_REPO_REMOTE_EXEC, experimentalRepoRemoteExec)
.setBool(EXPERIMENTAL_DISABLE_EXTERNAL_PACKAGE, experimentalDisableExternalPackage)
.setBool(EXPERIMENTAL_SIBLING_REPOSITORY_LAYOUT, experimentalSiblingRepositoryLayout)
Expand Down Expand Up @@ -756,6 +769,7 @@ public StarlarkSemantics toStarlarkSemantics() {
public static final String EXPERIMENTAL_BZL_VISIBILITY = "+experimental_bzl_visibility";
public static final String CHECK_BZL_VISIBILITY = "+check_bzl_visibility";
public static final String EXPERIMENTAL_CC_SHARED_LIBRARY = "-experimental_cc_shared_library";
public static final String EXPERIMENTAL_CC_STATIC_LIBRARY = "-experimental_cc_static_library";
public static final String EXPERIMENTAL_DISABLE_EXTERNAL_PACKAGE =
"-experimental_disable_external_package";
public static final String EXPERIMENTAL_ENABLE_ANDROID_MIGRATION_APIS =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -992,6 +992,11 @@ public boolean checkExperimentalCcSharedLibrary(StarlarkThread thread) throws Ev
return thread.getSemantics().getBool(BuildLanguageOptions.EXPERIMENTAL_CC_SHARED_LIBRARY);
}

@Override
public boolean checkExperimentalCcStaticLibrary(StarlarkThread thread) throws EvalException {
return thread.getSemantics().getBool(BuildLanguageOptions.EXPERIMENTAL_CC_STATIC_LIBRARY);
}

@Override
public void checkExperimentalStarlarkCcImport(StarlarkActionFactory starlarkActionFactoryApi)
throws EvalException {
Expand Down
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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,13 @@ LinkerInputT createLinkerInput(
documented = false)
boolean checkExperimentalCcSharedLibrary(StarlarkThread thread) throws EvalException;

@StarlarkMethod(
name = "check_experimental_cc_static_library",
doc = "DO NOT USE. This is to guard use of cc_static_library.",
useStarlarkThread = true,
documented = false)
boolean checkExperimentalCcStaticLibrary(StarlarkThread thread) throws EvalException;

@StarlarkMethod(
name = "check_experimental_starlark_cc_import",
doc = "DO NOT USE. This is to guard use of cc_import.bzl",
Expand Down
5 changes: 5 additions & 0 deletions src/main/starlark/builtins_bzl/common/cc/action_names.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ CLIF_MATCH_ACTION_NAME = "clif-match"
# A string constant for the obj copy actions.
OBJ_COPY_ACTION_NAME = "objcopy_embed_data"

ARCHIVE_MERGE = "archive-merge"
ARCHIVE_THICKEN = "archive-thicken"

ACTION_NAMES = struct(
c_compile = C_COMPILE_ACTION_NAME,
cpp_compile = CPP_COMPILE_ACTION_NAME,
Expand Down Expand Up @@ -130,4 +133,6 @@ ACTION_NAMES = struct(
objcpp_executable = OBJCPP_EXECUTABLE_ACTION_NAME,
clif_match = CLIF_MATCH_ACTION_NAME,
objcopy_embed_data = OBJ_COPY_ACTION_NAME,
archive_merge = ARCHIVE_MERGE,
archive_thicken = ARCHIVE_THICKEN,
)
2 changes: 2 additions & 0 deletions src/main/starlark/builtins_bzl/common/cc/cc_helper.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -1221,4 +1221,6 @@ cc_helper = struct(
get_public_hdrs = _get_public_hdrs,
report_invalid_options = _report_invalid_options,
system_include_dirs = _system_include_dirs,
get_base_name = _get_base_name,
replace_name = _replace_name,
)
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,
)
4 changes: 4 additions & 0 deletions src/main/starlark/builtins_bzl/common/cc/semantics.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ def _check_experimental_cc_shared_library(ctx):
if not cc_common.check_experimental_cc_shared_library():
fail("Pass --experimental_cc_shared_library to use cc_shared_library")

def _check_experimental_cc_static_library(ctx):
if not cc_common.check_experimental_cc_static_library():
fail("Pass --experimental_cc_static_library to use cc_static_library")

def _get_linkstatic_default(ctx):
if ctx.attr._is_test:
# By default Tests do not link statically. Except on Windows.
Expand Down
2 changes: 2 additions & 0 deletions src/main/starlark/builtins_bzl/common/exports.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ load("@_builtins//:common/cc/cc_import.bzl", "cc_import")
load("@_builtins//:common/cc/cc_binary_wrapper.bzl", "cc_binary")
load("@_builtins//:common/cc/cc_test_wrapper.bzl", cc_test = "cc_test_wrapper")
load("@_builtins//:common/cc/experimental_cc_shared_library.bzl", "CcSharedLibraryInfo", "cc_shared_library", "cc_shared_library_permissions")
load("@_builtins//:common/cc/experimental_cc_static_library.bzl", "cc_static_library")
load("@_builtins//:common/objc/objc_import.bzl", "objc_import")
load("@_builtins//:common/objc/objc_library.bzl", "objc_library")
load("@_builtins//:common/objc/compilation_support.bzl", "compilation_support")
Expand Down Expand Up @@ -61,6 +62,7 @@ exported_rules = {
"proto_library": proto_library,
"+cc_shared_library": cc_shared_library,
"+cc_shared_library_permissions": cc_shared_library_permissions,
"cc_static_library": cc_static_library,
"+cc_binary": cc_binary,
"+cc_test": cc_test,
"+cc_library": cc_library,
Expand Down
Loading

0 comments on commit fbcb331

Please sign in to comment.