Search in sources :

Example 6 with CxxLink

use of com.facebook.buck.cxx.CxxLink in project buck by facebook.

the class AppleBinaryDescriptionTest method linkerFlagsLocationMacro.

@Test
public void linkerFlagsLocationMacro() throws Exception {
    assumeThat(Platform.detect(), is(Platform.MACOS));
    BuildTarget sandboxTarget = BuildTargetFactory.newInstance("//:rule#sandbox," + FakeAppleRuleDescriptions.DEFAULT_IPHONEOS_I386_PLATFORM.getFlavor());
    BuildRuleResolver resolver = new BuildRuleResolver(TargetGraphFactory.newInstance(new AppleBinaryBuilder(sandboxTarget).build()), new DefaultTargetNodeToBuildRuleTransformer());
    SourcePathResolver pathResolver = new SourcePathResolver(new SourcePathRuleFinder(resolver));
    Genrule dep = GenruleBuilder.newGenruleBuilder(BuildTargetFactory.newInstance("//:dep")).setOut("out").build(resolver);
    AppleBinaryBuilder builder = new AppleBinaryBuilder(BuildTargetFactory.newInstance("//:rule")).setLinkerFlags(ImmutableList.of(StringWithMacrosUtils.format("--linker-script=%s", LocationMacro.of(dep.getBuildTarget()))));
    assertThat(builder.build().getExtraDeps(), Matchers.hasItem(dep.getBuildTarget()));
    BuildRule binary = ((CxxBinary) builder.build(resolver)).getLinkRule();
    assertThat(binary, Matchers.instanceOf(CxxLink.class));
    assertThat(Arg.stringify(((CxxLink) binary).getArgs(), pathResolver), Matchers.hasItem(String.format("--linker-script=%s", dep.getAbsoluteOutputFilePath(pathResolver))));
    assertThat(binary.getDeps(), Matchers.hasItem(dep));
}
Also used : BuildTarget(com.facebook.buck.model.BuildTarget) CxxBinary(com.facebook.buck.cxx.CxxBinary) BuildRule(com.facebook.buck.rules.BuildRule) Genrule(com.facebook.buck.shell.Genrule) CxxLink(com.facebook.buck.cxx.CxxLink) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) Test(org.junit.Test)

Example 7 with CxxLink

use of com.facebook.buck.cxx.CxxLink in project buck by facebook.

the class SwiftLibraryIntegrationTest method testSwiftCompileAndLinkArgs.

@Test
public void testSwiftCompileAndLinkArgs() throws NoSuchBuildTargetException {
    BuildTarget buildTarget = BuildTargetFactory.newInstance("//foo:bar#iphoneos-x86_64");
    BuildTarget swiftCompileTarget = buildTarget.withAppendedFlavors(SwiftLibraryDescription.SWIFT_COMPILE_FLAVOR);
    BuildRuleParams params = new FakeBuildRuleParamsBuilder(swiftCompileTarget).build();
    SwiftLibraryDescription.Arg args = createDummySwiftArg();
    SwiftCompile buildRule = (SwiftCompile) FakeAppleRuleDescriptions.SWIFT_LIBRARY_DESCRIPTION.createBuildRule(TargetGraph.EMPTY, params, resolver, args);
    resolver.addToIndex(buildRule);
    ImmutableList<Arg> astArgs = buildRule.getAstLinkArgs();
    assertThat(astArgs, Matchers.hasSize(3));
    assertThat(astArgs.get(0), Matchers.equalTo(StringArg.of("-Xlinker")));
    assertThat(astArgs.get(1), Matchers.equalTo(StringArg.of("-add_ast_path")));
    assertThat(astArgs.get(2), Matchers.instanceOf(SourcePathArg.class));
    SourcePathArg sourcePathArg = (SourcePathArg) astArgs.get(2);
    assertThat(sourcePathArg.getPath(), Matchers.equalTo(new ExplicitBuildTargetSourcePath(swiftCompileTarget, pathResolver.getRelativePath(buildRule.getSourcePathToOutput()).resolve("bar.swiftmodule"))));
    Arg objArg = buildRule.getFileListLinkArg();
    assertThat(objArg, Matchers.instanceOf(FileListableLinkerInputArg.class));
    FileListableLinkerInputArg fileListArg = (FileListableLinkerInputArg) objArg;
    ExplicitBuildTargetSourcePath fileListSourcePath = new ExplicitBuildTargetSourcePath(swiftCompileTarget, pathResolver.getRelativePath(buildRule.getSourcePathToOutput()).resolve("bar.o"));
    assertThat(fileListArg.getPath(), Matchers.equalTo(fileListSourcePath));
    BuildTarget linkTarget = buildTarget.withAppendedFlavors(CxxDescriptionEnhancer.SHARED_FLAVOR);
    CxxLink linkRule = (CxxLink) FakeAppleRuleDescriptions.SWIFT_LIBRARY_DESCRIPTION.createBuildRule(TargetGraphFactory.newInstance(FakeTargetNodeBuilder.build(buildRule)), params.withBuildTarget(linkTarget), resolver, args);
    assertThat(linkRule.getArgs(), Matchers.hasItem(objArg));
    assertThat(linkRule.getArgs(), Matchers.not(Matchers.hasItem(SourcePathArg.of(fileListSourcePath))));
}
Also used : SourcePathArg(com.facebook.buck.rules.args.SourcePathArg) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) FileListableLinkerInputArg(com.facebook.buck.rules.args.FileListableLinkerInputArg) BuildTarget(com.facebook.buck.model.BuildTarget) SourcePathArg(com.facebook.buck.rules.args.SourcePathArg) StringArg(com.facebook.buck.rules.args.StringArg) FileListableLinkerInputArg(com.facebook.buck.rules.args.FileListableLinkerInputArg) Arg(com.facebook.buck.rules.args.Arg) FakeBuildRuleParamsBuilder(com.facebook.buck.rules.FakeBuildRuleParamsBuilder) CxxLink(com.facebook.buck.cxx.CxxLink) ExplicitBuildTargetSourcePath(com.facebook.buck.rules.ExplicitBuildTargetSourcePath) Test(org.junit.Test)

Example 8 with CxxLink

use of com.facebook.buck.cxx.CxxLink in project buck by facebook.

the class AppleLibraryDescriptionTest method linkerFlagsLocationMacro.

@Test
public void linkerFlagsLocationMacro() throws Exception {
    assumeThat(Platform.detect(), is(Platform.MACOS));
    BuildTarget sandboxTarget = BuildTargetFactory.newInstance("//:rule").withFlavors(CxxDescriptionEnhancer.SANDBOX_TREE_FLAVOR, DefaultCxxPlatforms.FLAVOR);
    BuildRuleResolver resolver = new BuildRuleResolver(TargetGraphFactory.newInstance(new AppleLibraryBuilder(sandboxTarget).build()), new DefaultTargetNodeToBuildRuleTransformer());
    SourcePathResolver pathResolver = new SourcePathResolver(new SourcePathRuleFinder(resolver));
    BuildTarget target = BuildTargetFactory.newInstance("//:rule").withFlavors(DefaultCxxPlatforms.FLAVOR, CxxDescriptionEnhancer.SHARED_FLAVOR);
    Genrule dep = GenruleBuilder.newGenruleBuilder(BuildTargetFactory.newInstance("//:dep")).setOut("out").build(resolver);
    AppleLibraryBuilder builder = new AppleLibraryBuilder(target).setLinkerFlags(ImmutableList.of(StringWithMacrosUtils.format("--linker-script=%s", LocationMacro.of(dep.getBuildTarget())))).setSrcs(ImmutableSortedSet.of(SourceWithFlags.of(new FakeSourcePath("foo.c"))));
    assertThat(builder.build().getExtraDeps(), Matchers.hasItem(dep.getBuildTarget()));
    BuildRule binary = builder.build(resolver);
    assertThat(binary, Matchers.instanceOf(CxxLink.class));
    assertThat(Arg.stringify(((CxxLink) binary).getArgs(), pathResolver), Matchers.hasItem(String.format("--linker-script=%s", dep.getAbsoluteOutputFilePath(pathResolver))));
    assertThat(binary.getDeps(), Matchers.hasItem(dep));
}
Also used : FakeSourcePath(com.facebook.buck.rules.FakeSourcePath) BuildTarget(com.facebook.buck.model.BuildTarget) BuildRule(com.facebook.buck.rules.BuildRule) Genrule(com.facebook.buck.shell.Genrule) CxxLink(com.facebook.buck.cxx.CxxLink) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) Test(org.junit.Test)

Example 9 with CxxLink

use of com.facebook.buck.cxx.CxxLink in project buck by facebook.

the class AppleTestDescriptionTest method linkerFlagsLocationMacro.

@Test
public void linkerFlagsLocationMacro() throws Exception {
    assumeThat(Platform.detect(), is(Platform.MACOS));
    GenruleBuilder depBuilder = GenruleBuilder.newGenruleBuilder(BuildTargetFactory.newInstance("//:dep")).setOut("out");
    AppleTestBuilder builder = new AppleTestBuilder(BuildTargetFactory.newInstance("//:rule#macosx-x86_64")).setLinkerFlags(ImmutableList.of(StringWithMacrosUtils.format("--linker-script=%s", LocationMacro.of(depBuilder.getTarget())))).setSrcs(ImmutableSortedSet.of(SourceWithFlags.of(new FakeSourcePath("foo.c"))));
    TargetGraph targetGraph = TargetGraphFactory.newInstance(builder.build(), depBuilder.build());
    BuildRuleResolver resolver = new BuildRuleResolver(targetGraph, new DefaultTargetNodeToBuildRuleTransformer());
    SourcePathResolver pathResolver = new SourcePathResolver(new SourcePathRuleFinder(resolver));
    Genrule dep = depBuilder.build(resolver, targetGraph);
    assertThat(builder.build().getExtraDeps(), Matchers.hasItem(dep.getBuildTarget()));
    AppleTest test = builder.build(resolver, targetGraph);
    CxxStrip strip = (CxxStrip) RichStream.from(test.getDeps()).filter(AppleBundle.class).findFirst().get().getBinary().get();
    BuildRule binary = strip.getDeps().first();
    assertThat(binary, Matchers.instanceOf(CxxLink.class));
    assertThat(Arg.stringify(((CxxLink) binary).getArgs(), pathResolver), Matchers.hasItem(String.format("--linker-script=%s", dep.getAbsoluteOutputFilePath(pathResolver))));
    assertThat(binary.getDeps(), Matchers.hasItem(dep));
}
Also used : FakeSourcePath(com.facebook.buck.rules.FakeSourcePath) TargetGraph(com.facebook.buck.rules.TargetGraph) GenruleBuilder(com.facebook.buck.shell.GenruleBuilder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) BuildRule(com.facebook.buck.rules.BuildRule) Genrule(com.facebook.buck.shell.Genrule) CxxLink(com.facebook.buck.cxx.CxxLink) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) CxxStrip(com.facebook.buck.cxx.CxxStrip) Test(org.junit.Test)

Example 10 with CxxLink

use of com.facebook.buck.cxx.CxxLink in project buck by facebook.

the class RelinkerRule method getBuildSteps.

@Override
public ImmutableList<Step> getBuildSteps(BuildContext context, final BuildableContext buildableContext) {
    final ImmutableList.Builder<Step> relinkerSteps = ImmutableList.builder();
    if (linker != null) {
        ImmutableList<Arg> args = ImmutableList.<Arg>builder().addAll(linkerArgs).add(StringArg.of("-Wl,--version-script=" + getRelativeVersionFilePath().toString())).build();
        relinkerSteps.addAll(new CxxLink(buildRuleParams.withAppendedFlavor(InternalFlavor.of("cxx-link")).withoutFlavor(LinkerMapMode.NO_LINKER_MAP.getFlavor()), linker, getLibFilePath(), args, cxxBuckConfig.getLinkScheduleInfo(), cxxBuckConfig.shouldCacheLinks()).getBuildSteps(context, buildableContext));
        buildableContext.recordArtifact(getRelativeVersionFilePath());
    }
    buildableContext.recordArtifact(getSymbolsNeededOutPath());
    return ImmutableList.of(new MakeCleanDirectoryStep(getProjectFilesystem(), getScratchDirPath()), new AbstractExecutionStep("xdso-dce relinker") {

        @Override
        public StepExecutionResult execute(ExecutionContext context) throws IOException, InterruptedException {
            ImmutableSet<String> symbolsNeeded = readSymbolsNeeded();
            if (linker == null) {
                getProjectFilesystem().copyFile(getBaseLibPath(), getLibFilePath());
                buildableContext.recordArtifact(getLibFilePath());
            } else {
                writeVersionScript(context.getProcessExecutor(), symbolsNeeded);
                for (Step s : relinkerSteps.build()) {
                    StepExecutionResult executionResult = s.execute(context);
                    if (!executionResult.isSuccess()) {
                        return StepExecutionResult.ERROR;
                    }
                }
            }
            writeSymbols(getSymbolsNeededOutPath(), Sets.union(symbolsNeeded, getSymbols(context.getProcessExecutor(), getLibFilePath()).undefined));
            return StepExecutionResult.SUCCESS;
        }
    });
}
Also used : AbstractExecutionStep(com.facebook.buck.step.AbstractExecutionStep) StepExecutionResult(com.facebook.buck.step.StepExecutionResult) ImmutableList(com.google.common.collect.ImmutableList) Step(com.facebook.buck.step.Step) AbstractExecutionStep(com.facebook.buck.step.AbstractExecutionStep) MakeCleanDirectoryStep(com.facebook.buck.step.fs.MakeCleanDirectoryStep) IOException(java.io.IOException) ExecutionContext(com.facebook.buck.step.ExecutionContext) ImmutableSet(com.google.common.collect.ImmutableSet) StringArg(com.facebook.buck.rules.args.StringArg) Arg(com.facebook.buck.rules.args.Arg) MakeCleanDirectoryStep(com.facebook.buck.step.fs.MakeCleanDirectoryStep) CxxLink(com.facebook.buck.cxx.CxxLink)

Aggregations

CxxLink (com.facebook.buck.cxx.CxxLink)13 BuildTarget (com.facebook.buck.model.BuildTarget)6 BuildRule (com.facebook.buck.rules.BuildRule)6 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)6 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)6 Test (org.junit.Test)6 BuildRuleResolver (com.facebook.buck.rules.BuildRuleResolver)5 DefaultTargetNodeToBuildRuleTransformer (com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer)5 FakeSourcePath (com.facebook.buck.rules.FakeSourcePath)3 SourcePath (com.facebook.buck.rules.SourcePath)3 Arg (com.facebook.buck.rules.args.Arg)3 Genrule (com.facebook.buck.shell.Genrule)3 CxxBinary (com.facebook.buck.cxx.CxxBinary)2 PrebuiltCxxLibraryBuilder (com.facebook.buck.cxx.PrebuiltCxxLibraryBuilder)2 BuildRuleParams (com.facebook.buck.rules.BuildRuleParams)2 BuildTargetSourcePath (com.facebook.buck.rules.BuildTargetSourcePath)2 ExplicitBuildTargetSourcePath (com.facebook.buck.rules.ExplicitBuildTargetSourcePath)2 TargetGraph (com.facebook.buck.rules.TargetGraph)2 StringArg (com.facebook.buck.rules.args.StringArg)2 MakeCleanDirectoryStep (com.facebook.buck.step.fs.MakeCleanDirectoryStep)2