diff options
author | cpeyser <cpeyser@google.com> | 2018-02-22 04:33:18 -0800 |
---|---|---|
committer | Copybara-Service <copybara-piper@google.com> | 2018-02-22 04:35:04 -0800 |
commit | c280f67eab763f6d413b7d4cc0a430559f4c2821 (patch) | |
tree | 203da01e0ca35e612f4f2d7b87ac0207b2513faa /src/test/java/com/google/devtools/build/lib/rules/cpp/CppLinkActionTest.java | |
parent | c2b332b45e6ea41a14ecbd3c5f30782bcdeec301 (diff) |
Remove the CppConfiguration member from CppLinkAction. This will make
CppLinkAction more suitable for serialization.
PiperOrigin-RevId: 186598828
Diffstat (limited to 'src/test/java/com/google/devtools/build/lib/rules/cpp/CppLinkActionTest.java')
-rw-r--r-- | src/test/java/com/google/devtools/build/lib/rules/cpp/CppLinkActionTest.java | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/src/test/java/com/google/devtools/build/lib/rules/cpp/CppLinkActionTest.java b/src/test/java/com/google/devtools/build/lib/rules/cpp/CppLinkActionTest.java index f7bf8ebfbd..ab5329f6c8 100644 --- a/src/test/java/com/google/devtools/build/lib/rules/cpp/CppLinkActionTest.java +++ b/src/test/java/com/google/devtools/build/lib/rules/cpp/CppLinkActionTest.java @@ -21,6 +21,7 @@ import com.google.common.base.Joiner; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Iterables; +import com.google.common.primitives.Ints; import com.google.devtools.build.lib.actions.Action; import com.google.devtools.build.lib.actions.ActionAnalysisMetadata; import com.google.devtools.build.lib.actions.ActionInputHelper; @@ -53,6 +54,7 @@ import com.google.devtools.build.lib.rules.cpp.LinkerInputs.LibraryToLink; import com.google.devtools.build.lib.testutil.TestUtils; import com.google.devtools.build.lib.util.OS; import com.google.devtools.build.lib.util.OsUtils; +import com.google.devtools.build.lib.util.Pair; import com.google.devtools.build.lib.vfs.FileSystem; import com.google.devtools.build.lib.vfs.Path; import com.google.devtools.build.lib.vfs.PathFragment; @@ -774,4 +776,99 @@ public class CppLinkActionTest extends BuildViewTestCase { .inOrder(); } } + + /** Tests that -pie is removed when -shared is also present (http://b/5611891#). */ + @Test + public void testPieOptionDisabledForSharedLibraries() throws Exception { + CppLinkAction linkAction = + createLinkBuilder( + LinkTargetType.DYNAMIC_LIBRARY, + "dummyRuleContext/out.so", + ImmutableList.of(), + ImmutableList.of(), + getMockFeatureConfiguration()) + .setLinkStaticness(LinkStaticness.MOSTLY_STATIC) + .addLinkopts(ImmutableList.of("-pie", "-other", "-pie")) + .setLibraryIdentifier("foo") + .build(); + + List<String> argv = linkAction.getLinkCommandLine().getRawLinkArgv(); + assertThat(argv).doesNotContain("-pie"); + assertThat(argv).contains("-other"); + } + + /** Tests that -pie is removed when -shared is also present (http://b/5611891#). */ + @Test + public void testPieOptionKeptForExecutables() throws Exception { + CppLinkAction linkAction = + createLinkBuilder( + LinkTargetType.EXECUTABLE, + "dummyRuleContext/out", + ImmutableList.of(), + ImmutableList.of(), + getMockFeatureConfiguration()) + .setLinkStaticness(LinkStaticness.MOSTLY_STATIC) + .addLinkopts(ImmutableList.of("-pie", "-other", "-pie")) + .build(); + + List<String> argv = linkAction.getLinkCommandLine().getRawLinkArgv(); + assertThat(argv).contains("-pie"); + assertThat(argv).contains("-other"); + } + + @Test + public void testLinkoptsComeAfterLinkerInputs() throws Exception { + String solibPrefix = "_solib_" + CrosstoolConfigurationHelper.defaultCpu(); + Iterable<LibraryToLink> linkerInputs = + LinkerInputs.opaqueLibrariesToLink( + ArtifactCategory.DYNAMIC_LIBRARY, + ImmutableList.of( + getOutputArtifact(solibPrefix + "/FakeLinkerInput1.so"), + getOutputArtifact(solibPrefix + "/FakeLinkerInput2.so"), + getOutputArtifact(solibPrefix + "/FakeLinkerInput3.so"), + getOutputArtifact(solibPrefix + "/FakeLinkerInput4.so"))); + + CppLinkAction linkAction = + createLinkBuilder( + LinkTargetType.EXECUTABLE, + "dummyRuleContext/out", + ImmutableList.of(), + ImmutableList.copyOf(linkerInputs), + getMockFeatureConfiguration()) + .addLinkopts(ImmutableList.of("FakeLinkopt1", "FakeLinkopt2")) + .build(); + + List<String> argv = linkAction.getLinkCommandLine().getRawLinkArgv(); + int lastLinkerInputIndex = + Ints.max( + argv.indexOf("FakeLinkerInput1"), argv.indexOf("FakeLinkerInput2"), + argv.indexOf("FakeLinkerInput3"), argv.indexOf("FakeLinkerInput4")); + int firstLinkoptIndex = Math.min(argv.indexOf("FakeLinkopt1"), argv.indexOf("FakeLinkopt2")); + assertThat(lastLinkerInputIndex).isLessThan(firstLinkoptIndex); + } + + @Test + public void testLinkoptsAreOmittedForStaticLibrary() throws Exception { + CppLinkAction linkAction = + createLinkBuilder(LinkTargetType.STATIC_LIBRARY) + .addLinkopt("FakeLinkopt1") + .setLibraryIdentifier("foo") + .build(); + + assertThat(linkAction.getLinkCommandLine().getLinkopts()).isEmpty(); + } + + @Test + public void testSplitExecutableLinkCommand() throws Exception { + CppLinkAction linkAction = createLinkBuilder(LinkTargetType.EXECUTABLE).build(); + Pair<List<String>, List<String>> result = linkAction.getLinkCommandLine().splitCommandline(); + + String linkCommandLine = Joiner.on(" ").join(result.first); + assertThat(linkCommandLine).contains("gcc_tool"); + assertThat(linkCommandLine).contains("-o"); + assertThat(linkCommandLine).contains("output/path.a"); + assertThat(linkCommandLine).contains("path.a-2.params"); + + assertThat(result.second).contains("-lstdc++"); + } } |