// Copyright 2015 Google Inc. 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.nativedeps; import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; import com.google.devtools.build.lib.Constants; import com.google.devtools.build.lib.actions.Artifact; import com.google.devtools.build.lib.actions.Root; import com.google.devtools.build.lib.analysis.RuleContext; import com.google.devtools.build.lib.analysis.actions.SymlinkAction; import com.google.devtools.build.lib.analysis.config.BuildConfiguration; import com.google.devtools.build.lib.collect.nestedset.NestedSet; import com.google.devtools.build.lib.rules.cpp.CcLinkParams; import com.google.devtools.build.lib.rules.cpp.CcToolchainProvider; import com.google.devtools.build.lib.rules.cpp.CppBuildInfo; import com.google.devtools.build.lib.rules.cpp.CppConfiguration; import com.google.devtools.build.lib.rules.cpp.CppHelper; import com.google.devtools.build.lib.rules.cpp.CppLinkAction; import com.google.devtools.build.lib.rules.cpp.Link.LinkStaticness; import com.google.devtools.build.lib.rules.cpp.Link.LinkTargetType; import com.google.devtools.build.lib.rules.cpp.LinkerInputs; import com.google.devtools.build.lib.rules.cpp.LinkerInputs.LibraryToLink; import com.google.devtools.build.lib.util.Fingerprint; import com.google.devtools.build.lib.vfs.PathFragment; import java.util.ArrayList; import java.util.Collection; import java.util.LinkedList; import java.util.List; import java.util.Map; /** * Helper class to create a dynamic library for rules which support integration with native code. * *
This library gets created by the build system by linking all C++ libraries in the transitive * closure of the dependencies into a standalone dynamic library, with some exceptions. It usually * does not include neverlink libraries or C++ binaries (or their transitive dependencies). Note * that some rules are implicitly neverlink. */ public abstract class NativeDepsHelper { /** * An implementation of {@link * com.google.devtools.build.lib.rules.cpp.CppLinkAction.LinkArtifactFactory} that can create * artifacts anywhere. * *
Necessary because the actions of nativedeps libraries should be shareable, and thus cannot * be under the package directory. */ private static final CppLinkAction.LinkArtifactFactory SHAREABLE_LINK_ARTIFACT_FACTORY = new CppLinkAction.LinkArtifactFactory() { @Override public Artifact create(RuleContext ruleContext, PathFragment rootRelativePath) { return ruleContext.getShareableArtifact(rootRelativePath, ruleContext.getConfiguration().getBinDirectory()); } }; private NativeDepsHelper() {} /** * Creates an Action to create a dynamic library by linking all native code * (C/C++) libraries in the transitive dependency closure of a rule. * *
This is used for all native code in a rule's dependencies, regardless of type. * *
We link the native deps in the equivalent of linkstatic=1, linkshared=1 * mode. * *
linkstatic=1 means mostly-static mode, i.e. we select the ".a" (or * ".pic.a") files, but we don't include "-static" in linkopts. * *
linkshared=1 means we prefer the ".pic.a" files to the ".a" files, and
* the LinkTargetType is set to DYNAMIC_LIBRARY which causes Link.java to
* include "-shared" in the linker options.
*
* @param ruleContext the rule context to determine the native library
* @param linkParams the {@link CcLinkParams} for the rule, collected with
* linkstatic = 1 and linkshared = 1
* @param extraLinkOpts additional parameters to the linker
* @param includeMalloc whether or not to link in the rule's malloc dependency
* @param configuration the {@link BuildConfiguration} to run the link action under
* @return the native library runfiles. If the transitive deps closure of
* the rule contains no native code libraries, its fields are null.
*/
public static NativeDepsRunfiles maybeCreateNativeDepsAction(final RuleContext ruleContext,
CcLinkParams linkParams, Collection We link the native deps in the equivalent of linkstatic=1, linkshared=1 mode.
*
* linkstatic=1 means mostly-static mode, i.e. we select the ".a" (or ".pic.a") files, but we
* don't include "-static" in linkopts.
*
* linkshared=1 means we prefer the ".pic.a" files to the ".a" files, and the LinkTargetType is
* set to DYNAMIC_LIBRARY which causes Link.java to include "-shared" in the linker options.
*
* @param ruleContext the rule context to determine the native deps library
* @param linkParams the {@link CcLinkParams} for the rule, collected with linkstatic = 1 and
* linkshared = 1
* @return the native deps library runfiles. If the transitive deps closure of the rule contains
* no native code libraries, its fields are null.
*/
public static Artifact maybeCreateAndroidNativeDepsAction(final RuleContext ruleContext,
CcLinkParams linkParams, final BuildConfiguration configuration,
CcToolchainProvider toolchain) {
if (linkParams.getLibraries().isEmpty()) {
return null;
}
PathFragment labelName = new PathFragment(ruleContext.getLabel().getName());
Artifact nativeDeps = ruleContext.getUniqueDirectoryArtifact(ANDROID_UNIQUE_DIR,
labelName.replaceName("lib" + labelName.getBaseName() + ".so"),
configuration.getBinDirectory());
return createNativeDepsAction(
ruleContext,
linkParams, /** extraLinkOpts */
ImmutableList. TODO(bazel-team): (2010) Currently process of identifying parameters that can
* affect native library name is manual and should be kept in sync with the
* code in the CppLinkAction.Builder/CppLinkAction/Link classes which are
* responsible for generating linker command line. Ideally we should reuse
* generated command line for both purposes - selecting a name of the
* native library and using it as link action payload. For now, correctness
* of the method below is only ensured by validations in the
* CppLinkAction.Builder.build() method.
*/
private static PathFragment getSharedNativeDepsPath(Iterable