diff options
12 files changed, 537 insertions, 328 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 {} diff --git a/src/test/java/com/google/devtools/build/lib/rules/cpp/BUILD b/src/test/java/com/google/devtools/build/lib/rules/cpp/BUILD index c3b8be1ac1..035d5eb9a9 100644 --- a/src/test/java/com/google/devtools/build/lib/rules/cpp/BUILD +++ b/src/test/java/com/google/devtools/build/lib/rules/cpp/BUILD @@ -12,7 +12,10 @@ filegroup( java_test( name = "cpp-rules-tests", - srcs = glob(["*.java"]) + ["proto/CcProtoLibraryTest.java"], + srcs = glob( + ["*.java"], + exclude = ["CcImportBaseConfiguredTargetTest.java"], + ) + ["proto/CcProtoLibraryTest.java"], resources = [ "//tools/cpp:crosstool_utils", "//tools/cpp:lib_cc_configure", @@ -20,6 +23,7 @@ java_test( tags = ["rules"], test_class = "com.google.devtools.build.lib.AllTests", deps = [ + ":CcImportBaseConfiguredTargetTest", "//src/main/java/com/google/devtools/build/lib:bazel-main", "//src/main/java/com/google/devtools/build/lib:bazel-rules", "//src/main/java/com/google/devtools/build/lib:build-base", @@ -82,6 +86,21 @@ java_library( ], ) +java_library( + name = "CcImportBaseConfiguredTargetTest", + srcs = ["CcImportBaseConfiguredTargetTest.java"], + deps = [ + "//src/main/java/com/google/devtools/build/lib:build-base", + "//src/main/java/com/google/devtools/build/lib/actions", + "//src/main/java/com/google/devtools/build/lib/rules/cpp", + "//src/test/java/com/google/devtools/build/lib:actions_testutil", + "//src/test/java/com/google/devtools/build/lib:analysis_testutil", + "//src/test/java/com/google/devtools/build/lib:packages_testutil", + "//third_party:junit4", + "//third_party:truth", + ], +) + test_suite( name = "windows_tests", tags = [ diff --git a/src/test/java/com/google/devtools/build/lib/rules/cpp/CcImportBaseConfiguredTargetTest.java b/src/test/java/com/google/devtools/build/lib/rules/cpp/CcImportBaseConfiguredTargetTest.java new file mode 100644 index 0000000000..548bf8e09f --- /dev/null +++ b/src/test/java/com/google/devtools/build/lib/rules/cpp/CcImportBaseConfiguredTargetTest.java @@ -0,0 +1,352 @@ +// Copyright 2015 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. +// Copyright 2006 Google Inc. All rights reserved. + +package com.google.devtools.build.lib.rules.cpp; + +import static com.google.common.truth.Truth.assertThat; + +import com.google.devtools.build.lib.actions.Artifact; +import com.google.devtools.build.lib.analysis.ConfiguredTarget; +import com.google.devtools.build.lib.analysis.util.AnalysisMock; +import com.google.devtools.build.lib.analysis.util.BuildViewTestCase; +import com.google.devtools.build.lib.packages.util.MockCcSupport; +import com.google.devtools.build.lib.rules.cpp.LinkerInputs.LibraryToLink; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** "White-box" unit test of cc_import rule. */ +@RunWith(JUnit4.class) +public abstract class CcImportBaseConfiguredTargetTest extends BuildViewTestCase { + protected String skylarkImplementationLoadStatement = ""; + + @Before + public void setSkylarkImplementationLoadStatement() { + setIsSkylarkImplementation(); + } + + protected abstract void setIsSkylarkImplementation(); + + @Test + public void testCcImportRule() throws Exception { + scratch.file( + "third_party/BUILD", + skylarkImplementationLoadStatement, + "cc_import(", + " name = 'a_import',", + " static_library = 'A.a',", + " shared_library = 'A.so',", + " interface_library = 'A.ifso',", + " hdrs = ['a.h'],", + " alwayslink = 1,", + " system_provided = 0,", + ")"); + getConfiguredTarget("//third_party:a_import"); + } + + @Test + public void testWrongCcImportDefinitions() throws Exception { + checkError( + "a", + "foo", + "does not produce any cc_import static_library files " + "(expected .a, .lib or .pic.a)", + skylarkImplementationLoadStatement, + "cc_import(", + " name = 'foo',", + " static_library = 'libfoo.so',", + ")"); + checkError( + "b", + "foo", + "does not produce any cc_import shared_library files (expected .so, .dylib or .dll)", + skylarkImplementationLoadStatement, + "cc_import(", + " name = 'foo',", + " shared_library = 'libfoo.a',", + ")"); + checkError( + "c", + "foo", + "does not produce any cc_import interface_library files " + + "(expected .ifso, .tbd, .lib, .so or .dylib)", + skylarkImplementationLoadStatement, + "cc_import(", + " name = 'foo',", + " shared_library = 'libfoo.dll',", + " interface_library = 'libfoo.a',", + ")"); + checkError( + "d", + "foo", + "'shared_library' shouldn't be specified when 'system_provided' is true", + skylarkImplementationLoadStatement, + "cc_import(", + " name = 'foo',", + " shared_library = 'libfoo.so',", + " system_provided = 1,", + ")"); + checkError( + "e", + "foo", + "'shared_library' should be specified when 'system_provided' is false", + skylarkImplementationLoadStatement, + "cc_import(", + " name = 'foo',", + " interface_library = 'libfoo.ifso',", + " system_provided = 0,", + ")"); + } + + @Test + public void testWrongCcImportDefinitionsOnWindows() throws Exception { + AnalysisMock.get() + .ccSupport() + .setupCrosstool( + mockToolsConfig, + MockCcSupport.COPY_DYNAMIC_LIBRARIES_TO_BINARY_CONFIGURATION, + MockCcSupport.TARGETS_WINDOWS_CONFIGURATION); + useConfiguration(); + checkError( + "a", + "foo", + "'interface library' must be specified when using cc_import for shared library on Windows", + skylarkImplementationLoadStatement, + "cc_import(", + " name = 'foo',", + " shared_library = 'libfoo.dll',", + ")"); + } + + @Test + public void testCcImportWithStaticLibrary() throws Exception { + ConfiguredTarget target = + scratchConfiguredTarget( + "a", + "foo", + skylarkImplementationLoadStatement, + "cc_import(name = 'foo', static_library = 'libfoo.a')"); + Iterable<Artifact> libraries = + LinkerInputs.toNonSolibArtifacts( + target + .get(CcLinkingInfo.PROVIDER) + .getCcLinkParamsStore() + .getCcLinkParams(false, false) + .getLibraries()); + assertThat(artifactsToStrings(libraries)).containsExactly("src a/libfoo.a"); + + libraries = + LinkerInputs.toNonSolibArtifacts( + target + .get(CcLinkingInfo.PROVIDER) + .getCcLinkParamsStore() + .getCcLinkParams(false, true) + .getLibraries()); + assertThat(artifactsToStrings(libraries)).containsExactly("src a/libfoo.a"); + + libraries = + LinkerInputs.toNonSolibArtifacts( + target + .get(CcLinkingInfo.PROVIDER) + .getCcLinkParamsStore() + .getCcLinkParams(true, false) + .getLibraries()); + assertThat(artifactsToStrings(libraries)).containsExactly("src a/libfoo.a"); + + libraries = + LinkerInputs.toNonSolibArtifacts( + target + .get(CcLinkingInfo.PROVIDER) + .getCcLinkParamsStore() + .getCcLinkParams(true, true) + .getLibraries()); + assertThat(artifactsToStrings(libraries)).containsExactly("src a/libfoo.a"); + } + + @Test + public void testCcImportWithSharedLibrary() throws Exception { + useConfiguration("--cpu=k8"); + ConfiguredTarget target = + scratchConfiguredTarget( + "a", + "foo", + skylarkImplementationLoadStatement, + "cc_import(name = 'foo', shared_library = 'libfoo.so')"); + CcLinkParams ccLinkParams = + target.get(CcLinkingInfo.PROVIDER).getCcLinkParamsStore().getCcLinkParams(false, false); + Iterable<Artifact> libraries = LinkerInputs.toNonSolibArtifacts(ccLinkParams.getLibraries()); + Iterable<Artifact> dynamicLibrariesForRuntime = ccLinkParams.getDynamicLibrariesForRuntime(); + assertThat(artifactsToStrings(libraries)).containsExactly("src a/libfoo.so"); + assertThat(artifactsToStrings(dynamicLibrariesForRuntime)) + .containsExactly("bin _solib_k8/_U_S_Sa_Cfoo___Ua/libfoo.so"); + + ccLinkParams = + target.get(CcLinkingInfo.PROVIDER).getCcLinkParamsStore().getCcLinkParams(false, true); + libraries = LinkerInputs.toNonSolibArtifacts(ccLinkParams.getLibraries()); + dynamicLibrariesForRuntime = ccLinkParams.getDynamicLibrariesForRuntime(); + assertThat(artifactsToStrings(libraries)).containsExactly("src a/libfoo.so"); + assertThat(artifactsToStrings(dynamicLibrariesForRuntime)) + .containsExactly("bin _solib_k8/_U_S_Sa_Cfoo___Ua/libfoo.so"); + + ccLinkParams = + target.get(CcLinkingInfo.PROVIDER).getCcLinkParamsStore().getCcLinkParams(true, false); + libraries = LinkerInputs.toNonSolibArtifacts(ccLinkParams.getLibraries()); + dynamicLibrariesForRuntime = ccLinkParams.getDynamicLibrariesForRuntime(); + assertThat(artifactsToStrings(libraries)).containsExactly("src a/libfoo.so"); + assertThat(artifactsToStrings(dynamicLibrariesForRuntime)) + .containsExactly("bin _solib_k8/_U_S_Sa_Cfoo___Ua/libfoo.so"); + + ccLinkParams = + target.get(CcLinkingInfo.PROVIDER).getCcLinkParamsStore().getCcLinkParams(true, true); + libraries = LinkerInputs.toNonSolibArtifacts(ccLinkParams.getLibraries()); + dynamicLibrariesForRuntime = ccLinkParams.getDynamicLibrariesForRuntime(); + assertThat(artifactsToStrings(libraries)).containsExactly("src a/libfoo.so"); + assertThat(artifactsToStrings(dynamicLibrariesForRuntime)) + .containsExactly("bin _solib_k8/_U_S_Sa_Cfoo___Ua/libfoo.so"); + } + + @Test + public void testCcImportWithInterfaceSharedLibrary() throws Exception { + useConfiguration("--cpu=k8"); + ConfiguredTarget target = + scratchConfiguredTarget( + "b", + "foo", + skylarkImplementationLoadStatement, + "cc_import(name = 'foo', shared_library = 'libfoo.so'," + + " interface_library = 'libfoo.ifso')"); + CcLinkParams ccLinkParams = + target.get(CcLinkingInfo.PROVIDER).getCcLinkParamsStore().getCcLinkParams(false, false); + Iterable<Artifact> libraries = LinkerInputs.toNonSolibArtifacts(ccLinkParams.getLibraries()); + Iterable<Artifact> dynamicLibrariesForRuntime = ccLinkParams.getDynamicLibrariesForRuntime(); + assertThat(artifactsToStrings(libraries)).containsExactly("src b/libfoo.ifso"); + assertThat(artifactsToStrings(dynamicLibrariesForRuntime)) + .containsExactly("bin _solib_k8/_U_S_Sb_Cfoo___Ub/libfoo.so"); + + ccLinkParams = + target.get(CcLinkingInfo.PROVIDER).getCcLinkParamsStore().getCcLinkParams(false, true); + libraries = LinkerInputs.toNonSolibArtifacts(ccLinkParams.getLibraries()); + dynamicLibrariesForRuntime = ccLinkParams.getDynamicLibrariesForRuntime(); + assertThat(artifactsToStrings(libraries)).containsExactly("src b/libfoo.ifso"); + assertThat(artifactsToStrings(dynamicLibrariesForRuntime)) + .containsExactly("bin _solib_k8/_U_S_Sb_Cfoo___Ub/libfoo.so"); + + ccLinkParams = + target.get(CcLinkingInfo.PROVIDER).getCcLinkParamsStore().getCcLinkParams(true, false); + libraries = LinkerInputs.toNonSolibArtifacts(ccLinkParams.getLibraries()); + dynamicLibrariesForRuntime = ccLinkParams.getDynamicLibrariesForRuntime(); + assertThat(artifactsToStrings(libraries)).containsExactly("src b/libfoo.ifso"); + assertThat(artifactsToStrings(dynamicLibrariesForRuntime)) + .containsExactly("bin _solib_k8/_U_S_Sb_Cfoo___Ub/libfoo.so"); + + ccLinkParams = + target.get(CcLinkingInfo.PROVIDER).getCcLinkParamsStore().getCcLinkParams(true, true); + libraries = LinkerInputs.toNonSolibArtifacts(ccLinkParams.getLibraries()); + dynamicLibrariesForRuntime = ccLinkParams.getDynamicLibrariesForRuntime(); + assertThat(artifactsToStrings(libraries)).containsExactly("src b/libfoo.ifso"); + assertThat(artifactsToStrings(dynamicLibrariesForRuntime)) + .containsExactly("bin _solib_k8/_U_S_Sb_Cfoo___Ub/libfoo.so"); + } + + @Test + public void testCcImportWithBothStaticAndSharedLibraries() throws Exception { + useConfiguration("--cpu=k8"); + ConfiguredTarget target = + scratchConfiguredTarget( + "a", + "foo", + skylarkImplementationLoadStatement, + "cc_import(name = 'foo', static_library = 'libfoo.a', shared_library = 'libfoo.so')"); + CcLinkParams ccLinkParams = + target.get(CcLinkingInfo.PROVIDER).getCcLinkParamsStore().getCcLinkParams(false, false); + Iterable<Artifact> libraries = LinkerInputs.toNonSolibArtifacts(ccLinkParams.getLibraries()); + Iterable<Artifact> dynamicLibrariesForRuntime = ccLinkParams.getDynamicLibrariesForRuntime(); + assertThat(artifactsToStrings(libraries)).containsExactly("src a/libfoo.so"); + assertThat(artifactsToStrings(dynamicLibrariesForRuntime)) + .containsExactly("bin _solib_k8/_U_S_Sa_Cfoo___Ua/libfoo.so"); + + ccLinkParams = + target.get(CcLinkingInfo.PROVIDER).getCcLinkParamsStore().getCcLinkParams(false, true); + libraries = LinkerInputs.toNonSolibArtifacts(ccLinkParams.getLibraries()); + dynamicLibrariesForRuntime = ccLinkParams.getDynamicLibrariesForRuntime(); + assertThat(artifactsToStrings(libraries)).containsExactly("src a/libfoo.so"); + assertThat(artifactsToStrings(dynamicLibrariesForRuntime)) + .containsExactly("bin _solib_k8/_U_S_Sa_Cfoo___Ua/libfoo.so"); + + ccLinkParams = + target.get(CcLinkingInfo.PROVIDER).getCcLinkParamsStore().getCcLinkParams(true, false); + libraries = LinkerInputs.toNonSolibArtifacts(ccLinkParams.getLibraries()); + dynamicLibrariesForRuntime = ccLinkParams.getDynamicLibrariesForRuntime(); + assertThat(artifactsToStrings(libraries)).containsExactly("src a/libfoo.a"); + assertThat(artifactsToStrings(dynamicLibrariesForRuntime)).isEmpty(); + + ccLinkParams = + target.get(CcLinkingInfo.PROVIDER).getCcLinkParamsStore().getCcLinkParams(true, true); + libraries = LinkerInputs.toNonSolibArtifacts(ccLinkParams.getLibraries()); + dynamicLibrariesForRuntime = ccLinkParams.getDynamicLibrariesForRuntime(); + assertThat(artifactsToStrings(libraries)).containsExactly("src a/libfoo.a"); + assertThat(artifactsToStrings(dynamicLibrariesForRuntime)).isEmpty(); + } + + @Test + public void testCcImportWithAlwaysLinkStaticLibrary() throws Exception { + ConfiguredTarget target = + scratchConfiguredTarget( + "a", + "foo", + skylarkImplementationLoadStatement, + "cc_import(name = 'foo', static_library = 'libfoo.a', alwayslink = 1)"); + LibraryToLink libraryToLink = + target + .get(CcLinkingInfo.PROVIDER) + .getCcLinkParamsStore() + .getCcLinkParams(false, false) + .getLibraries() + .toList() + .get(0); + assertThat(libraryToLink.getArtifactCategory()) + .isEqualTo(ArtifactCategory.ALWAYSLINK_STATIC_LIBRARY); + } + + @Test + public void testCcImportSystemProvidedIsTrue() throws Exception { + ConfiguredTarget target = + scratchConfiguredTarget( + "a", + "foo", + skylarkImplementationLoadStatement, + "cc_import(name = 'foo', interface_library = 'libfoo.ifso', system_provided = 1)"); + CcLinkParams ccLinkParams = + target.get(CcLinkingInfo.PROVIDER).getCcLinkParamsStore().getCcLinkParams(false, false); + Iterable<Artifact> libraries = LinkerInputs.toNonSolibArtifacts(ccLinkParams.getLibraries()); + Iterable<Artifact> dynamicLibrariesForRuntime = ccLinkParams.getDynamicLibrariesForRuntime(); + assertThat(artifactsToStrings(libraries)).containsExactly("src a/libfoo.ifso"); + assertThat(artifactsToStrings(dynamicLibrariesForRuntime)).isEmpty(); + } + + @Test + public void testCcImportProvideHeaderFiles() throws Exception { + Iterable<Artifact> headers = + scratchConfiguredTarget( + "a", + "foo", + skylarkImplementationLoadStatement, + "cc_import(name = 'foo', static_library = 'libfoo.a', hdrs = ['foo.h'])") + .get(CcCompilationInfo.PROVIDER) + .getCcCompilationContext() + .getDeclaredIncludeSrcs(); + assertThat(artifactsToStrings(headers)).containsExactly("src a/foo.h"); + } +} diff --git a/src/test/java/com/google/devtools/build/lib/rules/cpp/CcImportConfiguredTargetTest.java b/src/test/java/com/google/devtools/build/lib/rules/cpp/CcImportConfiguredTargetTest.java index 0f6ce9bc9d..938cbea33a 100644 --- a/src/test/java/com/google/devtools/build/lib/rules/cpp/CcImportConfiguredTargetTest.java +++ b/src/test/java/com/google/devtools/build/lib/rules/cpp/CcImportConfiguredTargetTest.java @@ -15,307 +15,12 @@ package com.google.devtools.build.lib.rules.cpp; -import static com.google.common.truth.Truth.assertThat; - -import com.google.devtools.build.lib.actions.Artifact; -import com.google.devtools.build.lib.analysis.ConfiguredTarget; -import com.google.devtools.build.lib.analysis.util.AnalysisMock; -import com.google.devtools.build.lib.analysis.util.BuildViewTestCase; -import com.google.devtools.build.lib.packages.util.MockCcSupport; -import com.google.devtools.build.lib.rules.cpp.LinkerInputs.LibraryToLink; -import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; -/** - * "White-box" unit test of cc_import rule. - */ +/** "White-box" unit test of cc_import rule. */ @RunWith(JUnit4.class) -public class CcImportConfiguredTargetTest extends BuildViewTestCase { - - @Test - public void testCcImportRule() throws Exception { - scratch.file( - "third_party/BUILD", - "cc_import(", - " name = 'a_import',", - " static_library = 'A.a',", - " shared_library = 'A.so',", - " interface_library = 'A.ifso',", - " hdrs = ['a.h'],", - " alwayslink = 1,", - " system_provided = 0,", - ")"); - getConfiguredTarget("//third_party:a_import"); - } - - @Test - public void testWrongCcImportDefinitions() throws Exception { - checkError("a", "foo", - "'//a:libfoo.so' does not produce any cc_import static_library files " - + "(expected .a, .lib or .pic.a)", - "cc_import(", - " name = 'foo',", - " static_library = 'libfoo.so',", - ")" - ); - checkError("b", "foo", - "'//b:libfoo.a' does not produce any cc_import shared_library files " - + "(expected .so, .dylib or .dll)", - "cc_import(", - " name = 'foo',", - " shared_library = 'libfoo.a',", - ")" - ); - checkError( - "c", - "foo", - "'//c:libfoo.a' does not produce any cc_import interface_library files " - + "(expected .ifso, .tbd, .lib, .so or .dylib)", - "cc_import(", - " name = 'foo',", - " shared_library = 'libfoo.dll',", - " interface_library = 'libfoo.a',", - ")"); - checkError("d", "foo", - "'shared_library' shouldn't be specified when 'system_provided' is true", - "cc_import(", - " name = 'foo',", - " shared_library = 'libfoo.so',", - " system_provided = 1,", - ")" - ); - checkError("e", "foo", - "'shared_library' should be specified when 'system_provided' is false", - "cc_import(", - " name = 'foo',", - " interface_library = 'libfoo.ifso',", - " system_provided = 0,", - ")" - ); - } - - @Test - public void testWrongCcImportDefinitionsOnWindows() throws Exception { - AnalysisMock.get() - .ccSupport() - .setupCrosstool( - mockToolsConfig, - MockCcSupport.COPY_DYNAMIC_LIBRARIES_TO_BINARY_CONFIGURATION, - MockCcSupport.TARGETS_WINDOWS_CONFIGURATION); - useConfiguration(); - checkError("a", "foo", - "'interface library' must be specified when using cc_import for shared library on Windows", - "cc_import(", - " name = 'foo',", - " shared_library = 'libfoo.dll',", - ")" - ); - } - - @Test - public void testCcImportWithStaticLibrary() throws Exception { - ConfiguredTarget target = - scratchConfiguredTarget("a", "foo", "cc_import(name = 'foo', static_library = 'libfoo.a')"); - Iterable<Artifact> libraries = - LinkerInputs.toNonSolibArtifacts( - target - .get(CcLinkingInfo.PROVIDER) - .getCcLinkParamsStore() - .getCcLinkParams(false, false) - .getLibraries()); - assertThat(artifactsToStrings(libraries)).containsExactly("src a/libfoo.a"); - - libraries = - LinkerInputs.toNonSolibArtifacts( - target - .get(CcLinkingInfo.PROVIDER) - .getCcLinkParamsStore() - .getCcLinkParams(false, true) - .getLibraries()); - assertThat(artifactsToStrings(libraries)).containsExactly("src a/libfoo.a"); - - libraries = - LinkerInputs.toNonSolibArtifacts( - target - .get(CcLinkingInfo.PROVIDER) - .getCcLinkParamsStore() - .getCcLinkParams(true, false) - .getLibraries()); - assertThat(artifactsToStrings(libraries)).containsExactly("src a/libfoo.a"); - - libraries = - LinkerInputs.toNonSolibArtifacts( - target - .get(CcLinkingInfo.PROVIDER) - .getCcLinkParamsStore() - .getCcLinkParams(true, true) - .getLibraries()); - assertThat(artifactsToStrings(libraries)).containsExactly("src a/libfoo.a"); - } - - @Test - public void testCcImportWithSharedLibrary() throws Exception { - useConfiguration("--cpu=k8"); - ConfiguredTarget target = - scratchConfiguredTarget( - "a", "foo", "cc_import(name = 'foo', shared_library = 'libfoo.so')"); - CcLinkParams ccLinkParams = - target.get(CcLinkingInfo.PROVIDER).getCcLinkParamsStore().getCcLinkParams(false, false); - Iterable<Artifact> libraries = LinkerInputs.toNonSolibArtifacts(ccLinkParams.getLibraries()); - Iterable<Artifact> dynamicLibrariesForRuntime = ccLinkParams.getDynamicLibrariesForRuntime(); - assertThat(artifactsToStrings(libraries)).containsExactly("src a/libfoo.so"); - assertThat(artifactsToStrings(dynamicLibrariesForRuntime)) - .containsExactly("bin _solib_k8/_U_S_Sa_Cfoo___Ua/libfoo.so"); - - ccLinkParams = - target.get(CcLinkingInfo.PROVIDER).getCcLinkParamsStore().getCcLinkParams(false, true); - libraries = LinkerInputs.toNonSolibArtifacts(ccLinkParams.getLibraries()); - dynamicLibrariesForRuntime = ccLinkParams.getDynamicLibrariesForRuntime(); - assertThat(artifactsToStrings(libraries)).containsExactly("src a/libfoo.so"); - assertThat(artifactsToStrings(dynamicLibrariesForRuntime)) - .containsExactly("bin _solib_k8/_U_S_Sa_Cfoo___Ua/libfoo.so"); - - ccLinkParams = - target.get(CcLinkingInfo.PROVIDER).getCcLinkParamsStore().getCcLinkParams(true, false); - libraries = LinkerInputs.toNonSolibArtifacts(ccLinkParams.getLibraries()); - dynamicLibrariesForRuntime = ccLinkParams.getDynamicLibrariesForRuntime(); - assertThat(artifactsToStrings(libraries)).containsExactly("src a/libfoo.so"); - assertThat(artifactsToStrings(dynamicLibrariesForRuntime)) - .containsExactly("bin _solib_k8/_U_S_Sa_Cfoo___Ua/libfoo.so"); - - ccLinkParams = - target.get(CcLinkingInfo.PROVIDER).getCcLinkParamsStore().getCcLinkParams(true, true); - libraries = LinkerInputs.toNonSolibArtifacts(ccLinkParams.getLibraries()); - dynamicLibrariesForRuntime = ccLinkParams.getDynamicLibrariesForRuntime(); - assertThat(artifactsToStrings(libraries)).containsExactly("src a/libfoo.so"); - assertThat(artifactsToStrings(dynamicLibrariesForRuntime)) - .containsExactly("bin _solib_k8/_U_S_Sa_Cfoo___Ua/libfoo.so"); - } - - @Test - public void testCcImportWithInterfaceSharedLibrary() throws Exception { - useConfiguration("--cpu=k8"); - ConfiguredTarget target = - scratchConfiguredTarget( - "b", - "foo", - "cc_import(name = 'foo', shared_library = 'libfoo.so'," - + " interface_library = 'libfoo.ifso')"); - CcLinkParams ccLinkParams = - target.get(CcLinkingInfo.PROVIDER).getCcLinkParamsStore().getCcLinkParams(false, false); - Iterable<Artifact> libraries = LinkerInputs.toNonSolibArtifacts(ccLinkParams.getLibraries()); - Iterable<Artifact> dynamicLibrariesForRuntime = ccLinkParams.getDynamicLibrariesForRuntime(); - assertThat(artifactsToStrings(libraries)).containsExactly("src b/libfoo.ifso"); - assertThat(artifactsToStrings(dynamicLibrariesForRuntime)) - .containsExactly("bin _solib_k8/_U_S_Sb_Cfoo___Ub/libfoo.so"); - - ccLinkParams = - target.get(CcLinkingInfo.PROVIDER).getCcLinkParamsStore().getCcLinkParams(false, true); - libraries = LinkerInputs.toNonSolibArtifacts(ccLinkParams.getLibraries()); - dynamicLibrariesForRuntime = ccLinkParams.getDynamicLibrariesForRuntime(); - assertThat(artifactsToStrings(libraries)).containsExactly("src b/libfoo.ifso"); - assertThat(artifactsToStrings(dynamicLibrariesForRuntime)) - .containsExactly("bin _solib_k8/_U_S_Sb_Cfoo___Ub/libfoo.so"); - - ccLinkParams = - target.get(CcLinkingInfo.PROVIDER).getCcLinkParamsStore().getCcLinkParams(true, false); - libraries = LinkerInputs.toNonSolibArtifacts(ccLinkParams.getLibraries()); - dynamicLibrariesForRuntime = ccLinkParams.getDynamicLibrariesForRuntime(); - assertThat(artifactsToStrings(libraries)).containsExactly("src b/libfoo.ifso"); - assertThat(artifactsToStrings(dynamicLibrariesForRuntime)) - .containsExactly("bin _solib_k8/_U_S_Sb_Cfoo___Ub/libfoo.so"); - - ccLinkParams = - target.get(CcLinkingInfo.PROVIDER).getCcLinkParamsStore().getCcLinkParams(true, true); - libraries = LinkerInputs.toNonSolibArtifacts(ccLinkParams.getLibraries()); - dynamicLibrariesForRuntime = ccLinkParams.getDynamicLibrariesForRuntime(); - assertThat(artifactsToStrings(libraries)).containsExactly("src b/libfoo.ifso"); - assertThat(artifactsToStrings(dynamicLibrariesForRuntime)) - .containsExactly("bin _solib_k8/_U_S_Sb_Cfoo___Ub/libfoo.so"); - } - - @Test - public void testCcImportWithBothStaticAndSharedLibraries() throws Exception { - useConfiguration("--cpu=k8"); - ConfiguredTarget target = - scratchConfiguredTarget( - "a", - "foo", - "cc_import(name = 'foo', static_library = 'libfoo.a', shared_library = 'libfoo.so')"); - CcLinkParams ccLinkParams = - target.get(CcLinkingInfo.PROVIDER).getCcLinkParamsStore().getCcLinkParams(false, false); - Iterable<Artifact> libraries = LinkerInputs.toNonSolibArtifacts(ccLinkParams.getLibraries()); - Iterable<Artifact> dynamicLibrariesForRuntime = ccLinkParams.getDynamicLibrariesForRuntime(); - assertThat(artifactsToStrings(libraries)).containsExactly("src a/libfoo.so"); - assertThat(artifactsToStrings(dynamicLibrariesForRuntime)) - .containsExactly("bin _solib_k8/_U_S_Sa_Cfoo___Ua/libfoo.so"); - - ccLinkParams = - target.get(CcLinkingInfo.PROVIDER).getCcLinkParamsStore().getCcLinkParams(false, true); - libraries = LinkerInputs.toNonSolibArtifacts(ccLinkParams.getLibraries()); - dynamicLibrariesForRuntime = ccLinkParams.getDynamicLibrariesForRuntime(); - assertThat(artifactsToStrings(libraries)).containsExactly("src a/libfoo.so"); - assertThat(artifactsToStrings(dynamicLibrariesForRuntime)) - .containsExactly("bin _solib_k8/_U_S_Sa_Cfoo___Ua/libfoo.so"); - - ccLinkParams = - target.get(CcLinkingInfo.PROVIDER).getCcLinkParamsStore().getCcLinkParams(true, false); - libraries = LinkerInputs.toNonSolibArtifacts(ccLinkParams.getLibraries()); - dynamicLibrariesForRuntime = ccLinkParams.getDynamicLibrariesForRuntime(); - assertThat(artifactsToStrings(libraries)).containsExactly("src a/libfoo.a"); - assertThat(artifactsToStrings(dynamicLibrariesForRuntime)).isEmpty(); - - ccLinkParams = - target.get(CcLinkingInfo.PROVIDER).getCcLinkParamsStore().getCcLinkParams(true, true); - libraries = LinkerInputs.toNonSolibArtifacts(ccLinkParams.getLibraries()); - dynamicLibrariesForRuntime = ccLinkParams.getDynamicLibrariesForRuntime(); - assertThat(artifactsToStrings(libraries)).containsExactly("src a/libfoo.a"); - assertThat(artifactsToStrings(dynamicLibrariesForRuntime)).isEmpty(); - } - - @Test - public void testCcImportWithAlwaysLinkStaticLibrary() throws Exception { - ConfiguredTarget target = - scratchConfiguredTarget( - "a", "foo", "cc_import(name = 'foo', static_library = 'libfoo.a', alwayslink = 1)"); - LibraryToLink libraryToLink = - target - .get(CcLinkingInfo.PROVIDER) - .getCcLinkParamsStore() - .getCcLinkParams(false, false) - .getLibraries() - .toList() - .get(0); - assertThat(libraryToLink.getArtifactCategory()) - .isEqualTo(ArtifactCategory.ALWAYSLINK_STATIC_LIBRARY); - } - - @Test - public void testCcImportSystemProvidedIsTrue() throws Exception { - ConfiguredTarget target = - scratchConfiguredTarget( - "a", - "foo", - "cc_import(name = 'foo', interface_library = 'libfoo.ifso', system_provided = 1)"); - CcLinkParams ccLinkParams = - target.get(CcLinkingInfo.PROVIDER).getCcLinkParamsStore().getCcLinkParams(false, false); - Iterable<Artifact> libraries = LinkerInputs.toNonSolibArtifacts(ccLinkParams.getLibraries()); - Iterable<Artifact> dynamicLibrariesForRuntime = ccLinkParams.getDynamicLibrariesForRuntime(); - assertThat(artifactsToStrings(libraries)).containsExactly("src a/libfoo.ifso"); - assertThat(artifactsToStrings(dynamicLibrariesForRuntime)).isEmpty(); - } - - @Test - public void testCcImportProvideHeaderFiles() throws Exception { - Iterable<Artifact> headers = - scratchConfiguredTarget( - "a", - "foo", - "cc_import(name = 'foo', static_library = 'libfoo.a', hdrs = ['foo.h'])") - .get(CcCompilationInfo.PROVIDER) - .getCcCompilationContext() - .getDeclaredIncludeSrcs(); - assertThat(artifactsToStrings(headers)).containsExactly("src a/foo.h"); - } +public class CcImportConfiguredTargetTest extends CcImportBaseConfiguredTargetTest { + @Override + protected void setIsSkylarkImplementation() {} } |