diff options
author | 2018-06-20 04:14:38 -0700 | |
---|---|---|
committer | 2018-06-20 04:15:41 -0700 | |
commit | ece8d5fc1adbbb924122b9f1201a877839da2d27 (patch) | |
tree | 9eb54d192425b85613cebda33f9dfd5ca9a8f10c /src/main/java | |
parent | d784b5f91868161eb3be7e392dcf29e1079ad50b (diff) |
C++: Re-writes cc_import in Skylark.
I will put cc_import.bzl in Bazel in a follow up CL.
RELNOTES:none
PiperOrigin-RevId: 201332133
Diffstat (limited to 'src/main/java')
9 files changed, 161 insertions, 28 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/ArtifactCategory.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/ArtifactCategory.java index eb4913ba5f..b4ddfec19e 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/ArtifactCategory.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/ArtifactCategory.java @@ -13,7 +13,11 @@ // limitations under the License package com.google.devtools.build.lib.rules.cpp; +import com.google.common.base.Joiner; import com.google.common.collect.ImmutableList; +import com.google.devtools.build.lib.events.Location; +import com.google.devtools.build.lib.syntax.EvalException; +import java.util.Arrays; /** * A category of artifacts that are candidate input/output to an action, for which the toolchain can @@ -40,8 +44,13 @@ public enum ArtifactCategory { // the options passed to the clif_matcher. CLIF_OUTPUT_PROTO("", ".opb"); + private static final ArtifactCategory[] ALLOWED_FROM_SKYLARK = { + STATIC_LIBRARY, ALWAYSLINK_STATIC_LIBRARY, DYNAMIC_LIBRARY, INTERFACE_LIBRARY + }; + private final String defaultPrefix; private final String defaultExtension; + private final String skylarkName; // The extensions allowed for this artifact name pattern, Bazel should recognized them as // corresponding file type in CppFileTypes.java final ImmutableList<String> allowedExtensions; @@ -57,6 +66,31 @@ public enum ArtifactCategory { .add(defaultExtension) .add(extraAllowedExtensions) .build(); + + this.skylarkName = toString().toLowerCase(); + } + + public String getSkylarkName() { + return skylarkName; + } + + public static ArtifactCategory fromString( + String skylarkName, Location location, String fieldForError) throws EvalException { + for (ArtifactCategory registerActions : ALLOWED_FROM_SKYLARK) { + if (registerActions.getSkylarkName().equals(skylarkName)) { + return registerActions; + } + } + throw new EvalException( + location, + String.format( + "Possible values for %s: %s", + fieldForError, + Joiner.on(", ") + .join( + Arrays.stream(ALLOWED_FROM_SKYLARK) + .map(ArtifactCategory::getSkylarkName) + .collect(ImmutableList.toImmutableList())))); } /** Returns the name of the category. */ diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcImport.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcImport.java index 60dbdbc037..d06979bdb2 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcImport.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcImport.java @@ -107,12 +107,12 @@ public abstract class CcImport implements RuleConfiguredTargetFactory { linkingHelper.addPicStaticLibraries( ImmutableList.of( LinkerInputs.opaqueLibraryToLink( - staticLibrary, staticLibraryCategory, libraryIdentifier, alwayslink))); + staticLibrary, staticLibraryCategory, libraryIdentifier))); } else { linkingHelper.addStaticLibraries( ImmutableList.of( LinkerInputs.opaqueLibraryToLink( - staticLibrary, staticLibraryCategory, libraryIdentifier, alwayslink))); + staticLibrary, staticLibraryCategory, libraryIdentifier))); } } diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcLinkParams.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcLinkParams.java index db73a3ca4e..44bc16eb8c 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcLinkParams.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcLinkParams.java @@ -29,6 +29,7 @@ import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable; import com.google.devtools.build.lib.rules.cpp.LinkerInputs.LibraryToLink; import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec; import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec.VisibleForSerialization; +import com.google.devtools.build.lib.skylarkbuildapi.cpp.CcLinkParamsApi; import java.util.Collection; import java.util.Objects; import javax.annotation.Nullable; @@ -43,7 +44,7 @@ import javax.annotation.Nullable; * link order (preorder) and linkstamps are sorted. */ @AutoCodec -public final class CcLinkParams { +public final class CcLinkParams implements CcLinkParamsApi { /** * A list of link options contributed by a single configured target. * @@ -142,6 +143,10 @@ public final class CcLinkParams { return new Builder(linkingStatically, linkShared); } + public static final Builder builder() { + return new Builder(); + } + /** * Builder for {@link CcLinkParams}. */ @@ -154,12 +159,16 @@ public final class CcLinkParams { * libraries, which are not handled by CcLinkParams). When this is false, we want to use dynamic * versions of any libraries that this target depends on. */ - private final boolean linkingStatically; + private boolean linkingStatically; - /** - * linkShared is true when we're linking with "-shared" (linkshared=1). - */ - private final boolean linkShared; + /** linkShared is true when we're linking with "-shared" (linkshared=1). */ + private boolean linkShared; + + // TODO(plf): Ideally the two booleans above are removed from this Builder. We would pass the + // specific instances of CcLinkParams that are needed from transitive dependencies instead of + // calling the convenience methods that dig them out from the CcLinkParamsStore using these + // booleans. + private boolean linkingStaticallyLinkSharedSet; private ImmutableList.Builder<String> localLinkoptsBuilder = ImmutableList.builder(); @@ -183,11 +192,15 @@ public final class CcLinkParams { private boolean built = false; + /** The static builder methods of {@link CcLinkParams} should be used for instantiation. */ private Builder(boolean linkingStatically, boolean linkShared) { this.linkingStatically = linkingStatically; this.linkShared = linkShared; + this.linkingStaticallyLinkSharedSet = true; } + private Builder() {} + /** * Builds a {@link CcLinkParams} object. */ @@ -217,6 +230,7 @@ public final class CcLinkParams { } public boolean add(AbstractCcLinkParamsStore store) { + Preconditions.checkState(linkingStaticallyLinkSharedSet); if (store != null) { CcLinkParams args = store.get(linkingStatically, linkShared); addTransitiveArgs(args); diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcLinkParamsStore.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcLinkParamsStore.java index f93943e1b6..cd3026e514 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcLinkParamsStore.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcLinkParamsStore.java @@ -19,10 +19,12 @@ import com.google.devtools.build.lib.analysis.TransitiveInfoCollection; import com.google.devtools.build.lib.skyframe.serialization.ObjectCodec; import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec; import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec.VisibleForSerialization; +import com.google.devtools.build.lib.skylarkbuildapi.cpp.CcLinkParamsStoreApi; /** An implementation class for the AbstractCcLinkParamsStore. */ @AutoCodec -public final class CcLinkParamsStore extends AbstractCcLinkParamsStore { +public final class CcLinkParamsStore extends AbstractCcLinkParamsStore + implements CcLinkParamsStoreApi { public static final ObjectCodec<com.google.devtools.build.lib.rules.cpp.CcLinkParamsStore> CODEC = new CcLinkParamsStore_AutoCodec(); public static final Function<TransitiveInfoCollection, AbstractCcLinkParamsStore> TO_LINK_PARAMS = @@ -63,7 +65,7 @@ public final class CcLinkParamsStore extends AbstractCcLinkParamsStore { @VisibleForSerialization @AutoCodec.Instantiator - CcLinkParamsStore( + public CcLinkParamsStore( CcLinkParams staticSharedParams, CcLinkParams staticNoSharedParams, CcLinkParams noStaticSharedParams, diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcLinkingInfo.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcLinkingInfo.java index c9489df947..cb9021ef8a 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcLinkingInfo.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcLinkingInfo.java @@ -26,7 +26,9 @@ import com.google.devtools.build.lib.skylarkbuildapi.cpp.CcLinkingInfoApi; import com.google.devtools.build.lib.syntax.Environment; import com.google.devtools.build.lib.syntax.EvalException; import com.google.devtools.build.lib.syntax.FunctionSignature; +import com.google.devtools.build.lib.syntax.Runtime; import com.google.devtools.build.lib.syntax.SkylarkType; +import javax.annotation.Nullable; /** Wrapper for every C++ linking provider. */ @Immutable @@ -38,12 +40,24 @@ public final class CcLinkingInfo extends NativeInfo implements CcLinkingInfoApi FunctionSignature.of( /* numMandatoryPositionals= */ 0, /* numOptionalPositionals= */ 0, - /* numMandatoryNamedOnly= */ 1, + /* numMandatoryNamedOnly= */ 0, /* starArg= */ false, /* kwArg= */ false, + "cc_link_params_store", "cc_runfiles"), - /* defaultValues= */ ImmutableList.of(), - /* types= */ ImmutableList.of(SkylarkType.of(CcRunfiles.class))); + /* defaultValues= */ ImmutableList.of(Runtime.NONE, Runtime.NONE), + /* types= */ ImmutableList.of( + SkylarkType.of(CcLinkParamsStore.class), SkylarkType.of(CcRunfiles.class))); + + @Nullable + private static Object nullIfNone(Object object) { + return nullIfNone(object, Object.class); + } + + @Nullable + private static <T> T nullIfNone(Object object, Class<T> type) { + return object != Runtime.NONE ? type.cast(object) : null; + } public static final NativeProvider<CcLinkingInfo> PROVIDER = new NativeProvider<CcLinkingInfo>(CcLinkingInfo.class, "CcLinkingInfo", SIGNATURE) { @@ -52,8 +66,16 @@ public final class CcLinkingInfo extends NativeInfo implements CcLinkingInfoApi protected CcLinkingInfo createInstanceFromSkylark( Object[] args, Environment env, Location loc) throws EvalException { CcCommon.checkLocationWhitelisted(loc); + int i = 0; + CcLinkParamsStore ccLinkParamsStore = (CcLinkParamsStore) nullIfNone(args[i++]); + CcRunfiles ccRunfiles = (CcRunfiles) nullIfNone(args[i++]); CcLinkingInfo.Builder ccLinkingInfoBuilder = CcLinkingInfo.Builder.create(); - ccLinkingInfoBuilder.setCcRunfiles((CcRunfiles) args[0]); + ccLinkingInfoBuilder.setCcLinkParamsStore(ccLinkParamsStore); + // TODO(plf): The CcDynamicLibrariesForRuntime provider can be removed perhaps. Do not + // add to the API until we know for sure. The CcRunfiles provider is already in the API + // at the time of this comment (cl/200184914). Perhaps it can be removed but Skylark rules + // using it will have to be migrated. + ccLinkingInfoBuilder.setCcRunfiles(ccRunfiles); return ccLinkingInfoBuilder.build(); } }; diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/LinkerInput.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/LinkerInput.java index c0692cd183..9cbcf67434 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/LinkerInput.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/LinkerInput.java @@ -15,6 +15,7 @@ package com.google.devtools.build.lib.rules.cpp; import com.google.devtools.build.lib.actions.Artifact; +import com.google.devtools.build.lib.skylarkinterface.SkylarkCallable; /** * Something that appears on the command line of the linker. Since we sometimes expand archive files @@ -27,9 +28,8 @@ public interface LinkerInput { */ ArtifactCategory getArtifactCategory(); - /** - * Returns the artifact that is the input of the linker. - */ + /** Returns the artifact that is the input of the linker. */ + @SkylarkCallable(name = "artifact", doc = "Artifact passed to the linker.") Artifact getArtifact(); /** diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/LinkerInputs.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/LinkerInputs.java index ad811e0f3c..5997105792 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/LinkerInputs.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/LinkerInputs.java @@ -22,6 +22,8 @@ import com.google.devtools.build.lib.collect.CollectionUtils; import com.google.devtools.build.lib.concurrent.ThreadSafety; import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec; import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec.VisibleForSerialization; +import com.google.devtools.build.lib.skylarkinterface.SkylarkModule; +import com.google.devtools.build.lib.skylarkinterface.SkylarkModuleCategory; /** * Factory for creating new {@link LinkerInput} objects. @@ -152,6 +154,11 @@ public abstract class LinkerInputs { * A library the user can link to. This is different from a simple linker input in that it also * has a library identifier. */ + @SkylarkModule( + name = "LibraryToLink", + category = SkylarkModuleCategory.BUILTIN, + documented = false, + doc = "A library the user can link to.") public interface LibraryToLink extends LinkerInput { ImmutableMap<Artifact, Artifact> getLtoBitcodeFiles(); @@ -505,17 +512,8 @@ public abstract class LinkerInputs { /* objectFiles= */ null, /* ltoBitcodeFiles= */ null, /* sharedNonLtoBackends= */ null, - /* allowArchiveTypeInAlwayslink= */ false, - /* mustKeepDebug= */ false); - } - - public static LibraryToLink opaqueLibraryToLink( - Artifact artifact, - ArtifactCategory category, - String libraryIdentifier, - boolean allowArchiveTypeInAlwayslink) { - return new CompoundLibraryToLink( - artifact, category, libraryIdentifier, null, null, null, allowArchiveTypeInAlwayslink, + /* allowArchiveTypeInAlwayslink= */ category.equals( + ArtifactCategory.ALWAYSLINK_STATIC_LIBRARY), /* mustKeepDebug= */ false); } diff --git a/src/main/java/com/google/devtools/build/lib/skylarkbuildapi/cpp/CcLinkParamsApi.java b/src/main/java/com/google/devtools/build/lib/skylarkbuildapi/cpp/CcLinkParamsApi.java new file mode 100644 index 0000000000..2759c14370 --- /dev/null +++ b/src/main/java/com/google/devtools/build/lib/skylarkbuildapi/cpp/CcLinkParamsApi.java @@ -0,0 +1,34 @@ +// 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.skylarkbuildapi.cpp; + +import com.google.devtools.build.lib.skylarkinterface.SkylarkModule; +import com.google.devtools.build.lib.skylarkinterface.SkylarkModuleCategory; + +/** + * Parameters that affect linking actions. + * + * <p>The parameters concerned are the link options (strings) passed to the linker, linkstamps, a + * list of libraries to be linked in, and a list of libraries to build at link time. + * + * <p>Items in the collections are stored in nested sets. Link options and libraries are stored in + * link order (preorder) and linkstamps are sorted. + */ +@SkylarkModule( + name = "CcLinkParams", + documented = false, + category = SkylarkModuleCategory.BUILTIN, + doc = "Parameters that affect linking actions.") +public interface CcLinkParamsApi {} diff --git a/src/main/java/com/google/devtools/build/lib/skylarkbuildapi/cpp/CcLinkParamsStoreApi.java b/src/main/java/com/google/devtools/build/lib/skylarkbuildapi/cpp/CcLinkParamsStoreApi.java new file mode 100644 index 0000000000..94e0bb65ff --- /dev/null +++ b/src/main/java/com/google/devtools/build/lib/skylarkbuildapi/cpp/CcLinkParamsStoreApi.java @@ -0,0 +1,29 @@ +// 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.skylarkbuildapi.cpp; + +import com.google.devtools.build.lib.skylarkinterface.SkylarkModule; +import com.google.devtools.build.lib.skylarkinterface.SkylarkModuleCategory; + +/** Skylark interface for CcLinkParamsStore. */ +@SkylarkModule( + name = "CcLinkParamsStore", + documented = false, + doc = + "Store for all possible combinations of CcLinkParams that may be passed to the linker " + + "depending on whether we have static linking mode and whether we are building a " + + "dynamic library", + category = SkylarkModuleCategory.BUILTIN) +public interface CcLinkParamsStoreApi {} |