| # Copyright 2020 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. | |
| """objc_library Starlark implementation replacing native""" | |
| load("//cc:build_settings.bzl", "cc") | |
| load("//cc:find_cc_toolchain.bzl", "find_cc_toolchain", "use_cc_toolchain") | |
| load("//cc/common:cc_common.bzl", "cc_common") | |
| load("//cc/common:cc_helper.bzl", "cc_helper") | |
| load("//cc/common:cc_info.bzl", "CcInfo") | |
| load("//cc/common:semantics.bzl", cc_semantics = "semantics") | |
| load(":objc_attrs.bzl", "common_attrs") | |
| load(":objc_common.bzl", "extensions") | |
| load(":objc_compilation_support.bzl", "compilation_support") | |
| load(":objc_semantics.bzl", "semantics") | |
| def _attribute_error(attr_name, msg): | |
| fail("in attribute '" + attr_name + "': " + msg) | |
| def _validate_attributes(srcs, non_arc_srcs, label): | |
| cc_helper.check_file_extensions( | |
| srcs, | |
| extensions.SRCS, | |
| "srcs", | |
| label, | |
| "objc_library", | |
| False, | |
| ) | |
| cc_helper.check_file_extensions( | |
| non_arc_srcs, | |
| extensions.NON_ARC_SRCS, | |
| "non_arc_srcs", | |
| label, | |
| "objc_library", | |
| False, | |
| ) | |
| if label.name.find("/") != -1: | |
| _attribute_error("name", "this attribute has unsupported character '/'") | |
| def _objc_library_impl(ctx): | |
| """Implementation of objc_library.""" | |
| _validate_attributes(srcs = ctx.attr.srcs, non_arc_srcs = ctx.attr.non_arc_srcs, label = ctx.label) | |
| cc_toolchain = find_cc_toolchain(ctx) | |
| requested_features = ctx.features | |
| unsupported_features = ctx.disabled_features | |
| semantics.check_toolchain_supports_objc_compile( | |
| ctx = ctx, | |
| cc_toolchain = cc_toolchain, | |
| requested_features = requested_features, | |
| unsupported_features = unsupported_features, | |
| ) | |
| common_variables = compilation_support.build_common_variables( | |
| ctx = ctx, | |
| toolchain = cc_toolchain, | |
| use_pch = True, | |
| deps = ctx.attr.deps, | |
| implementation_deps = ctx.attr.implementation_deps, | |
| attr_linkopts = ctx.attr.linkopts, | |
| alwayslink = cc.target_should_alwayslink(ctx), | |
| requested_features = requested_features, | |
| unsupported_features = unsupported_features, | |
| ) | |
| files = [] | |
| if common_variables.compilation_artifacts.archive != None: | |
| files.append(common_variables.compilation_artifacts.archive) | |
| compilation_result = compilation_support.register_compile_and_archive_actions( | |
| common_variables, | |
| ) | |
| (compilation_context, linking_context, compilation_outputs, output_groups) = compilation_result | |
| compilation_support.validate_attributes(common_variables) | |
| objc_provider = common_variables.objc_provider | |
| if hasattr(compilation_outputs, "gcno_files"): | |
| gcno_files = compilation_outputs.gcno_files() | |
| else: | |
| gcno_files = compilation_outputs._gcno_files | |
| if hasattr(compilation_outputs, "pic_gcno_files"): | |
| pic_gcno_files = compilation_outputs.pic_gcno_files() | |
| else: | |
| pic_gcno_files = compilation_outputs._pic_gcno_files | |
| feature_configuration = cc_common.configure_features( | |
| ctx = ctx, | |
| cc_toolchain = cc_toolchain, | |
| requested_features = requested_features, | |
| unsupported_features = unsupported_features, | |
| ) | |
| instrumented_files_info = coverage_common.instrumented_files_info( | |
| ctx = ctx, | |
| source_attributes = ["srcs", "non_arc_srcs", "hdrs"], | |
| dependency_attributes = ["deps", "data", "binary", "xctest_app"], | |
| extensions = extensions.NON_CPP_SOURCES + extensions.CPP_SOURCES + extensions.HEADERS, | |
| coverage_environment = cc_helper.get_coverage_environment(ctx, ctx.fragments.cpp, cc_toolchain, feature_configuration), | |
| # TODO(cmita): Use ctx.coverage_instrumented() instead when rules_swift can access | |
| # cc_toolchain.coverage_files and the coverage_support_files parameter of | |
| # coverage_common.instrumented_files_info(...) | |
| coverage_support_files = cc_toolchain._coverage_files if ctx.configuration.coverage_enabled else depset([]), | |
| metadata_files = gcno_files + pic_gcno_files, | |
| ) | |
| 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: | |
| # This branch ensures interop with custom Starlark rules following | |
| # https://bazel.build/extending/rules#runfiles_features_to_avoid | |
| runfiles_list.append(ctx.runfiles(transitive_files = data_dep[DefaultInfo].files)) | |
| runfiles_list.append(data_dep[DefaultInfo].default_runfiles) | |
| for src in ctx.attr.srcs: | |
| runfiles_list.append(src[DefaultInfo].default_runfiles) | |
| for dep in ctx.attr.deps: | |
| runfiles_list.append(dep[DefaultInfo].default_runfiles) | |
| for dep in ctx.attr.implementation_deps: | |
| runfiles_list.append(dep[DefaultInfo].default_runfiles) | |
| runfiles = ctx.runfiles().merge_all(runfiles_list) | |
| return [ | |
| DefaultInfo( | |
| files = depset(files), | |
| runfiles = runfiles, | |
| ), | |
| CcInfo( | |
| compilation_context = compilation_context, | |
| linking_context = linking_context, | |
| ), | |
| objc_provider, | |
| instrumented_files_info, | |
| OutputGroupInfo(**output_groups), | |
| ] | |
| OBJC_LIBRARY_RULE_KWARGS = dict( | |
| implementation = _objc_library_impl, | |
| initializer = common_attrs.alwayslink_initializer, | |
| doc = """ | |
| <p>This rule produces a static library from the given Objective-C source files.</p>""", | |
| attrs = common_attrs.union( | |
| { | |
| "data": attr.label_list(allow_files = True), | |
| "implementation_deps": attr.label_list(providers = [CcInfo], allow_files = False, doc = """ | |
| The list of other libraries that the library target depends on. Unlike with | |
| <code>deps</code>, the headers and include paths of these libraries (and all their | |
| transitive deps) are only used for compilation of this library, and not libraries that | |
| depend on it. Libraries specified with <code>implementation_deps</code> are still linked | |
| in binary targets that depend on this library."""), | |
| }, | |
| common_attrs.APPLE_VENDOR, | |
| common_attrs.ALWAYSLINK_RULE, | |
| common_attrs.COMPILING_RULE, | |
| common_attrs.COMPILE_DEPENDENCY_RULE, | |
| common_attrs.COPTS_RULE, | |
| common_attrs.LICENSES, | |
| common_attrs.SDK_FRAMEWORK_DEPENDER_RULE, | |
| ), | |
| fragments = ["objc", "cpp"], | |
| toolchains = use_cc_toolchain() + cc_semantics.get_runtimes_toolchain(), | |
| provides = [CcInfo], | |
| ) | |
| objc_library = rule( | |
| cfg = semantics.apple_crosstool_transition, | |
| **OBJC_LIBRARY_RULE_KWARGS | |
| ) |