use of com.google.devtools.build.lib.analysis.ConfiguredTarget in project bazel by bazelbuild.
the class LibraryLinkingTest method testCcLibraryLinkopts.
/**
* Tests that the shared library version of a cc_library includes linkopts settings
* in its link command line, but the archive library version doesn't.
*/
@Test
public void testCcLibraryLinkopts() throws Exception {
scratch.overwriteFile("custom_malloc/BUILD", "cc_library(name = 'custom_malloc',", " srcs = ['custom_malloc.cc'],", " linkopts = ['-Lmalloc_dir -lmalloc_opt']);");
ConfiguredTarget ccLib = getConfiguredTarget("//custom_malloc:custom_malloc");
final String linkOpt1 = "-Lmalloc_dir";
final String linkOpt2 = "-lmalloc_opt";
// Archive library version:
Artifact archiveLib = Iterables.getOnlyElement(Iterables.filter(ccLib.getProvider(FileProvider.class).getFilesToBuild(), new Predicate<Artifact>() {
@Override
public boolean apply(Artifact artifact) {
return artifact.getFilename().equals("libcustom_malloc.a");
}
}));
CppLinkAction archiveLink = (CppLinkAction) getGeneratingAction(archiveLib);
List<String> args = archiveLink.getArgv();
assertThat(args).doesNotContain(linkOpt1);
assertThat(args).doesNotContain(linkOpt2);
// Shared library version:
Artifact soLib = Iterables.getOnlyElement(ccLib.getProvider(CcExecutionDynamicLibrariesProvider.class).getExecutionDynamicLibraryArtifacts());
// This artifact is generated by a SolibSymlinkAction, so we need to go back two levels.
CppLinkAction solibLink = (CppLinkAction) getGeneratingAction(getGeneratingAction(soLib).getPrimaryInput());
args = solibLink.getArgv();
assertThat(args).contains(linkOpt1);
assertThat(args).contains(linkOpt2);
}
use of com.google.devtools.build.lib.analysis.ConfiguredTarget in project bazel by bazelbuild.
the class LibraryLinkingTest method testGeneratedLib.
@Test
public void testGeneratedLib() throws Exception {
useConfiguration("--cpu=k8");
ConfiguredTarget genlib = scratchConfiguredTarget("genrule", "thebinary.so", "genrule(name = 'genlib',", " outs = ['genlib.a'],", " cmd = '')", "cc_library(name = 'thelib',", " srcs = [':genlib'],", " linkstatic = 1)", "cc_binary(name = 'thebinary.so',", " deps = [':thelib'],", " linkstatic = 1,", " linkshared = 1)");
Artifact executable = getExecutable(genlib);
CppLinkAction linkAction = (CppLinkAction) getGeneratingAction(executable);
assertLinkopts(linkAction, "-shared", "-o", analysisMock.getProductName() + "-out/.+/genrule/thebinary.so", "-Wl,-whole-archive", analysisMock.getProductName() + "-out/.+/genrule/genlib.a", "-Wl,-no-whole-archive");
}
use of com.google.devtools.build.lib.analysis.ConfiguredTarget in project bazel by bazelbuild.
the class LinkBuildVariablesTest method testLinkstampBuildVariable.
@Test
public void testLinkstampBuildVariable() throws Exception {
scratch.file("x/BUILD", "cc_binary(", " name = 'bin',", " srcs = ['a.cc'],", " deps = [':lib'],", ")", "cc_library(", " name = 'lib',", " srcs = ['b.cc'],", " linkstamp = 'c.cc',", ")");
scratch.file("x/a.cc");
scratch.file("x/b.cc");
scratch.file("x/c.cc");
ConfiguredTarget target = getConfiguredTarget("//x:bin");
Variables variables = getLinkBuildVariables(target, Link.LinkTargetType.EXECUTABLE);
List<String> variableValue = getVariableValue(variables, CppLinkActionBuilder.LINKSTAMP_PATHS_VARIABLE);
assertThat(Iterables.getOnlyElement(variableValue)).contains("c.o");
}
use of com.google.devtools.build.lib.analysis.ConfiguredTarget in project bazel by bazelbuild.
the class LinkBuildVariablesTest method testInterfaceLibraryBuildingVariablesWhenGenerationPossible.
@Test
public void testInterfaceLibraryBuildingVariablesWhenGenerationPossible() throws Exception {
// Make sure the interface shared object generation is enabled in the configuration
// (which it is not by default for some windows toolchains)
AnalysisMock.get().ccSupport().setupCrosstool(mockToolsConfig, "supports_interface_shared_objects: true");
useConfiguration();
scratch.file("x/BUILD", "cc_library(name = 'foo', srcs = ['a.cc'])");
scratch.file("x/a.cc");
ConfiguredTarget target = getConfiguredTarget("//x:foo");
Variables variables = getLinkBuildVariables(target, LinkTargetType.DYNAMIC_LIBRARY);
String interfaceLibraryBuilder = Iterables.getOnlyElement(getVariableValue(variables, CppLinkActionBuilder.INTERFACE_LIBRARY_BUILDER_VARIABLE));
String interfaceLibraryInput = Iterables.getOnlyElement(getVariableValue(variables, CppLinkActionBuilder.INTERFACE_LIBRARY_INPUT_VARIABLE));
String interfaceLibraryOutput = Iterables.getOnlyElement(getVariableValue(variables, CppLinkActionBuilder.INTERFACE_LIBRARY_OUTPUT_VARIABLE));
String generateInterfaceLibrary = Iterables.getOnlyElement(getVariableValue(variables, CppLinkActionBuilder.GENERATE_INTERFACE_LIBRARY_VARIABLE));
assertThat(generateInterfaceLibrary).isEqualTo("yes");
assertThat(interfaceLibraryInput).endsWith("libfoo.so");
assertThat(interfaceLibraryOutput).endsWith("libfoo.ifso");
assertThat(interfaceLibraryBuilder).endsWith("build_interface_so");
}
use of com.google.devtools.build.lib.analysis.ConfiguredTarget in project bazel by bazelbuild.
the class LinkBuildVariablesTest method testLibrariesToLinkAreExported.
@Test
public void testLibrariesToLinkAreExported() throws Exception {
AnalysisMock.get().ccSupport().setupCrosstool(mockToolsConfig);
useConfiguration();
scratch.file("x/BUILD", "cc_library(name = 'foo', srcs = ['a.cc'])");
scratch.file("x/a.cc");
ConfiguredTarget target = getConfiguredTarget("//x:foo");
Variables variables = getLinkBuildVariables(target, LinkTargetType.DYNAMIC_LIBRARY);
VariableValue librariesToLinkSequence = variables.getVariable(CppLinkActionBuilder.LIBRARIES_TO_LINK_VARIABLE);
assertThat(librariesToLinkSequence).isNotNull();
Iterable<? extends VariableValue> librariesToLink = librariesToLinkSequence.getSequenceValue(CppLinkActionBuilder.LIBRARIES_TO_LINK_VARIABLE);
assertThat(librariesToLink).hasSize(1);
VariableValue nameValue = librariesToLink.iterator().next().getFieldValue(CppLinkActionBuilder.LIBRARIES_TO_LINK_VARIABLE, LibraryToLinkValue.NAME_FIELD_NAME);
assertThat(nameValue).isNotNull();
String name = nameValue.getStringValue(LibraryToLinkValue.NAME_FIELD_NAME);
assertThat(name).matches(".*a\\..*o");
}
Aggregations