use of com.facebook.buck.cxx.NativeLinkableInput in project buck by facebook.
the class CxxPythonExtensionDescriptionTest method createBuildRuleNativeLinkableDep.
@Test
public void createBuildRuleNativeLinkableDep() throws Exception {
ProjectFilesystem filesystem = new FakeProjectFilesystem();
BuildTarget target = BuildTargetFactory.newInstance("//:target");
// Setup a C/C++ library that we'll depend on form the C/C++ binary description.
BuildTarget cxxLibraryTarget = BuildTargetFactory.newInstance("//:dep");
CxxLibraryBuilder cxxLibraryBuilder = new CxxLibraryBuilder(cxxLibraryTarget).setSrcs(ImmutableSortedSet.of(SourceWithFlags.of(new FakeSourcePath("something.cpp"), ImmutableList.of())));
CxxPythonExtensionBuilder builder = new CxxPythonExtensionBuilder(target, FlavorDomain.of("Python Platform", PY2, PY3), new CxxBuckConfig(FakeBuckConfig.builder().build()), CxxTestBuilder.createDefaultPlatforms()).setDeps(ImmutableSortedSet.of(cxxLibraryTarget));
TargetGraph targetGraph = TargetGraphFactory.newInstance(cxxLibraryBuilder.build(), builder.build());
BuildRuleResolver resolver = new BuildRuleResolver(targetGraph, new DefaultTargetNodeToBuildRuleTransformer());
SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(resolver);
CxxLibrary dep = (CxxLibrary) cxxLibraryBuilder.build(resolver, filesystem, targetGraph);
CxxPythonExtension extension = builder.build(resolver, filesystem, targetGraph);
NativeLinkableInput depInput = dep.getNativeLinkableInput(CxxPlatformUtils.DEFAULT_PLATFORM, Linker.LinkableDepType.SHARED);
// Verify that the shared library dep propagated to the link rule.
extension.getPythonPackageComponents(PY2, CxxPlatformUtils.DEFAULT_PLATFORM);
BuildRule rule = resolver.getRule(CxxPythonExtensionDescription.getExtensionTarget(target, PY2.getFlavor(), CxxPlatformUtils.DEFAULT_PLATFORM.getFlavor()));
assertThat(rule.getDeps(), Matchers.hasItems(FluentIterable.from(depInput.getArgs()).transformAndConcat(arg -> arg.getDeps(ruleFinder)).toArray(BuildRule.class)));
}
use of com.facebook.buck.cxx.NativeLinkableInput in project buck by facebook.
the class OcamlRuleBuilder method createFineGrainedBuildRule.
public static BuildRule createFineGrainedBuildRule(OcamlBuckConfig ocamlBuckConfig, final BuildRuleParams params, BuildRuleResolver resolver, ImmutableList<OcamlSource> srcs, boolean isLibrary, boolean bytecodeOnly, ImmutableList<Arg> argFlags, final ImmutableList<String> linkerFlags, boolean buildNativePlugin) throws NoSuchBuildTargetException {
CxxPreprocessorInput cxxPreprocessorInputFromDeps = CxxPreprocessorInput.concat(CxxPreprocessables.getTransitiveCxxPreprocessorInput(ocamlBuckConfig.getCxxPlatform(), FluentIterable.from(params.getDeps()).filter(CxxPreprocessorDep.class::isInstance)));
SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(resolver);
SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
ImmutableList<String> nativeIncludes = FluentIterable.from(params.getDeps()).transformAndConcat(getLibInclude(false)).toList();
ImmutableList<String> bytecodeIncludes = FluentIterable.from(params.getDeps()).transformAndConcat(getLibInclude(true)).toList();
NativeLinkableInput nativeLinkableInput = getNativeLinkableInput(params.getDeps());
NativeLinkableInput bytecodeLinkableInput = getBytecodeLinkableInput(params.getDeps());
NativeLinkableInput cLinkableInput = getCLinkableInput(ocamlBuckConfig.getCxxPlatform(), params.getDeps());
ImmutableList<OcamlLibrary> ocamlInput = OcamlUtil.getTransitiveOcamlInput(params.getDeps());
BuildTarget buildTarget = isLibrary ? createStaticLibraryBuildTarget(params.getBuildTarget()) : createOcamlLinkTarget(params.getBuildTarget());
final BuildRuleParams compileParams = params.withBuildTarget(buildTarget).copyReplacingDeclaredAndExtraDeps(Suppliers.ofInstance(ImmutableSortedSet.<BuildRule>naturalOrder().addAll(ruleFinder.filterBuildRuleInputs(getInput(srcs))).addAll(Stream.of(nativeLinkableInput, bytecodeLinkableInput, cLinkableInput).flatMap(input -> input.getArgs().stream()).flatMap(arg -> arg.getDeps(ruleFinder).stream()).iterator()).addAll(argFlags.stream().flatMap(arg -> arg.getDeps(ruleFinder).stream()).iterator()).addAll(ruleFinder.filterBuildRuleInputs(ocamlBuckConfig.getCCompiler().resolve(resolver).getInputs())).addAll(ruleFinder.filterBuildRuleInputs(ocamlBuckConfig.getCxxCompiler().resolve(resolver).getInputs())).build()), Suppliers.ofInstance(ImmutableSortedSet.of()));
ImmutableList.Builder<Arg> flagsBuilder = ImmutableList.builder();
flagsBuilder.addAll(argFlags);
ImmutableSortedSet.Builder<BuildRule> nativeCompileDepsBuilder = ImmutableSortedSet.naturalOrder();
ImmutableSortedSet.Builder<BuildRule> bytecodeCompileDepsBuilder = ImmutableSortedSet.naturalOrder();
ImmutableSortedSet.Builder<BuildRule> bytecodeLinkDepsBuilder = ImmutableSortedSet.naturalOrder();
for (OcamlLibrary library : ocamlInput) {
nativeCompileDepsBuilder.addAll(library.getNativeCompileDeps());
bytecodeCompileDepsBuilder.addAll(library.getBytecodeCompileDeps());
bytecodeLinkDepsBuilder.addAll(library.getBytecodeLinkDeps());
}
OcamlBuildContext ocamlContext = OcamlBuildContext.builder(ocamlBuckConfig).setProjectFilesystem(params.getProjectFilesystem()).setSourcePathResolver(pathResolver).setFlags(flagsBuilder.build()).setNativeIncludes(nativeIncludes).setBytecodeIncludes(bytecodeIncludes).setOcamlInput(ocamlInput).setNativeLinkableInput(nativeLinkableInput).setBytecodeLinkableInput(bytecodeLinkableInput).setCLinkableInput(cLinkableInput).setBuildTarget(buildTarget.getUnflavoredBuildTarget()).setLibrary(isLibrary).setCxxPreprocessorInput(cxxPreprocessorInputFromDeps).setInput(getInput(srcs)).setNativeCompileDeps(nativeCompileDepsBuilder.build()).setBytecodeCompileDeps(bytecodeCompileDepsBuilder.build()).setBytecodeLinkDeps(bytecodeLinkDepsBuilder.build()).setCPreprocessor(ocamlBuckConfig.getCPreprocessor().resolve(resolver)).build();
Path baseDir = params.getProjectFilesystem().getRootPath().toAbsolutePath();
ImmutableMap<Path, ImmutableList<Path>> mlInput = getMLInputWithDeps(baseDir, ocamlContext);
ImmutableList<SourcePath> cInput = getCInput(pathResolver, getInput(srcs));
OcamlBuildRulesGenerator generator = new OcamlBuildRulesGenerator(compileParams, pathResolver, ruleFinder, resolver, ocamlContext, mlInput, cInput, ocamlBuckConfig.getCCompiler().resolve(resolver), ocamlBuckConfig.getCxxCompiler().resolve(resolver), bytecodeOnly, buildNativePlugin);
OcamlGeneratedBuildRules result = generator.generate();
if (isLibrary) {
return new OcamlStaticLibrary(params.copyReplacingDeclaredAndExtraDeps(Suppliers.ofInstance(ImmutableSortedSet.<BuildRule>naturalOrder().addAll(params.getDeclaredDeps().get()).addAll(result.getRules()).build()), params.getExtraDeps()), compileParams, linkerFlags, result.getObjectFiles(), ocamlContext, result.getRules().get(0), result.getNativeCompileDeps(), result.getBytecodeCompileDeps(), ImmutableSortedSet.<BuildRule>naturalOrder().add(result.getBytecodeLink()).addAll(ruleFinder.filterBuildRuleInputs(result.getObjectFiles())).build());
} else {
return new OcamlBinary(params.copyReplacingDeclaredAndExtraDeps(Suppliers.ofInstance(ImmutableSortedSet.<BuildRule>naturalOrder().addAll(params.getDeclaredDeps().get()).addAll(result.getRules()).build()), params.getExtraDeps()), result.getRules().get(0));
}
}
use of com.facebook.buck.cxx.NativeLinkableInput in project buck by facebook.
the class OcamlRuleBuilder method getNativeLinkableInput.
private static NativeLinkableInput getNativeLinkableInput(Iterable<BuildRule> deps) {
List<NativeLinkableInput> inputs = Lists.newArrayList();
// Add in the linkable input from OCaml libraries.
ImmutableList<BuildRule> ocamlDeps = getTransitiveOcamlLibraryDeps(deps);
for (BuildRule dep : ocamlDeps) {
inputs.add(((OcamlLibrary) dep).getNativeLinkableInput());
}
return NativeLinkableInput.concat(inputs);
}
use of com.facebook.buck.cxx.NativeLinkableInput in project buck by facebook.
the class OcamlRuleBuilder method createBulkBuildRule.
public static BuildRule createBulkBuildRule(OcamlBuckConfig ocamlBuckConfig, final BuildRuleParams params, BuildRuleResolver resolver, ImmutableList<OcamlSource> srcs, boolean isLibrary, boolean bytecodeOnly, ImmutableList<Arg> argFlags, final ImmutableList<String> linkerFlags) throws NoSuchBuildTargetException {
CxxPreprocessorInput cxxPreprocessorInputFromDeps = CxxPreprocessorInput.concat(CxxPreprocessables.getTransitiveCxxPreprocessorInput(ocamlBuckConfig.getCxxPlatform(), FluentIterable.from(params.getDeps()).filter(CxxPreprocessorDep.class::isInstance)));
SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(resolver);
SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
ImmutableList<String> nativeIncludes = FluentIterable.from(params.getDeps()).transformAndConcat(getLibInclude(false)).toList();
ImmutableList<String> bytecodeIncludes = FluentIterable.from(params.getDeps()).transformAndConcat(getLibInclude(true)).toList();
NativeLinkableInput nativeLinkableInput = getNativeLinkableInput(params.getDeps());
NativeLinkableInput bytecodeLinkableInput = getBytecodeLinkableInput(params.getDeps());
NativeLinkableInput cLinkableInput = getCLinkableInput(ocamlBuckConfig.getCxxPlatform(), params.getDeps());
ImmutableList<OcamlLibrary> ocamlInput = OcamlUtil.getTransitiveOcamlInput(params.getDeps());
ImmutableSortedSet.Builder<BuildRule> allDepsBuilder = ImmutableSortedSet.naturalOrder();
allDepsBuilder.addAll(ruleFinder.filterBuildRuleInputs(getInput(srcs)));
allDepsBuilder.addAll(Stream.of(nativeLinkableInput, bytecodeLinkableInput, cLinkableInput).flatMap(input -> input.getArgs().stream()).flatMap(arg -> arg.getDeps(ruleFinder).stream()).iterator());
for (OcamlLibrary library : ocamlInput) {
allDepsBuilder.addAll(library.getNativeCompileDeps());
allDepsBuilder.addAll(library.getBytecodeCompileDeps());
}
allDepsBuilder.addAll(ruleFinder.filterBuildRuleInputs(ocamlBuckConfig.getCCompiler().resolve(resolver).getInputs()));
allDepsBuilder.addAll(ruleFinder.filterBuildRuleInputs(ocamlBuckConfig.getCxxCompiler().resolve(resolver).getInputs()));
allDepsBuilder.addAll(argFlags.stream().flatMap(arg -> arg.getDeps(ruleFinder).stream()).iterator());
// The bulk rule will do preprocessing on sources, and so needs deps from the preprocessor
// input object.
allDepsBuilder.addAll(cxxPreprocessorInputFromDeps.getDeps(resolver, ruleFinder));
ImmutableSortedSet<BuildRule> allDeps = allDepsBuilder.build();
BuildTarget buildTarget = isLibrary ? createStaticLibraryBuildTarget(params.getBuildTarget()) : createOcamlLinkTarget(params.getBuildTarget());
final BuildRuleParams compileParams = params.withBuildTarget(buildTarget).copyReplacingDeclaredAndExtraDeps(Suppliers.ofInstance(allDeps), Suppliers.ofInstance(ImmutableSortedSet.of()));
ImmutableList.Builder<Arg> flagsBuilder = ImmutableList.builder();
flagsBuilder.addAll(argFlags);
ImmutableSortedSet.Builder<BuildRule> nativeCompileDepsBuilder = ImmutableSortedSet.naturalOrder();
ImmutableSortedSet.Builder<BuildRule> bytecodeCompileDepsBuilder = ImmutableSortedSet.naturalOrder();
ImmutableSortedSet.Builder<BuildRule> bytecodeLinkDepsBuilder = ImmutableSortedSet.naturalOrder();
for (OcamlLibrary library : ocamlInput) {
nativeCompileDepsBuilder.addAll(library.getNativeCompileDeps());
bytecodeCompileDepsBuilder.addAll(library.getBytecodeCompileDeps());
bytecodeLinkDepsBuilder.addAll(library.getBytecodeLinkDeps());
}
OcamlBuildContext ocamlContext = OcamlBuildContext.builder(ocamlBuckConfig).setProjectFilesystem(params.getProjectFilesystem()).setSourcePathResolver(pathResolver).setFlags(flagsBuilder.build()).setNativeIncludes(nativeIncludes).setBytecodeIncludes(bytecodeIncludes).setOcamlInput(ocamlInput).setNativeLinkableInput(nativeLinkableInput).setBytecodeLinkableInput(bytecodeLinkableInput).setCLinkableInput(cLinkableInput).setBuildTarget(buildTarget.getUnflavoredBuildTarget()).setLibrary(isLibrary).setCxxPreprocessorInput(cxxPreprocessorInputFromDeps).setInput(getInput(srcs)).setNativeCompileDeps(nativeCompileDepsBuilder.build()).setBytecodeCompileDeps(bytecodeCompileDepsBuilder.build()).setBytecodeLinkDeps(bytecodeLinkDepsBuilder.build()).setCPreprocessor(ocamlBuckConfig.getCPreprocessor().resolve(resolver)).build();
final OcamlBuild ocamlLibraryBuild = new OcamlBuild(compileParams, ocamlContext, ocamlBuckConfig.getCCompiler().resolve(resolver), ocamlBuckConfig.getCxxCompiler().resolve(resolver), bytecodeOnly);
resolver.addToIndex(ocamlLibraryBuild);
if (isLibrary) {
return new OcamlStaticLibrary(params.copyReplacingDeclaredAndExtraDeps(Suppliers.ofInstance(ImmutableSortedSet.<BuildRule>naturalOrder().addAll(params.getDeclaredDeps().get()).add(ocamlLibraryBuild).build()), params.getExtraDeps()), compileParams, linkerFlags, FluentIterable.from(srcs).transform(OcamlSource::getSource).transform(pathResolver::getAbsolutePath).filter(OcamlUtil.ext(OcamlCompilables.OCAML_C)).transform(ocamlContext::getCOutput).transform(input -> new ExplicitBuildTargetSourcePath(compileParams.getBuildTarget(), input)).toList(), ocamlContext, ocamlLibraryBuild, ImmutableSortedSet.of(ocamlLibraryBuild), ImmutableSortedSet.of(ocamlLibraryBuild), ImmutableSortedSet.of(ocamlLibraryBuild));
} else {
return new OcamlBinary(params.copyReplacingDeclaredAndExtraDeps(Suppliers.ofInstance(ImmutableSortedSet.<BuildRule>naturalOrder().addAll(params.getDeclaredDeps().get()).add(ocamlLibraryBuild).build()), params.getExtraDeps()), ocamlLibraryBuild);
}
}
use of com.facebook.buck.cxx.NativeLinkableInput in project buck by facebook.
the class PrebuiltAppleFramework method getNativeLinkableInput.
@Override
public NativeLinkableInput getNativeLinkableInput(CxxPlatform cxxPlatform, Linker.LinkableDepType type) throws NoSuchBuildTargetException {
Pair<Flavor, Linker.LinkableDepType> key = new Pair<>(cxxPlatform.getFlavor(), type);
NativeLinkableInput input = nativeLinkableCache.get(key);
if (input == null) {
input = getNativeLinkableInputUncached(cxxPlatform, type);
nativeLinkableCache.put(key, input);
}
return input;
}
Aggregations