Search in sources :

Example 16 with PathSourcePath

use of com.facebook.buck.rules.PathSourcePath in project buck by facebook.

the class PrebuiltCxxLibraryDescription method getApplicableSourcePath.

private static SourcePath getApplicableSourcePath(final BuildTarget target, final CellPathResolver cellRoots, final ProjectFilesystem filesystem, final BuildRuleResolver ruleResolver, final CxxPlatform cxxPlatform, Optional<String> versionSubDir, final String basePathString, final Optional<String> addedPathString) {
    ImmutableList<BuildRule> deps;
    MacroHandler handler = getMacroHandler(Optional.of(cxxPlatform));
    try {
        deps = handler.extractBuildTimeDeps(target, cellRoots, ruleResolver, basePathString);
    } catch (MacroException e) {
        deps = ImmutableList.of();
    }
    Path libDirPath = filesystem.getPath(expandMacros(handler, target, cellRoots, ruleResolver, basePathString));
    if (versionSubDir.isPresent()) {
        libDirPath = filesystem.getPath(versionSubDir.get()).resolve(libDirPath);
    }
    // So just expand the macros and return a PathSourcePath
    if (deps.isEmpty()) {
        Path resultPath = libDirPath;
        if (addedPathString.isPresent()) {
            resultPath = libDirPath.resolve(expandMacros(handler, target, cellRoots, ruleResolver, addedPathString.get()));
        }
        resultPath = target.getBasePath().resolve(resultPath);
        return new PathSourcePath(filesystem, resultPath);
    }
    // If we get here then this is referencing the output from a build rule.
    // This always return a ExplicitBuildTargetSourcePath
    Path p = filesystem.resolve(libDirPath);
    if (addedPathString.isPresent()) {
        p = p.resolve(addedPathString.get());
    }
    p = filesystem.relativize(p);
    return new ExplicitBuildTargetSourcePath(deps.iterator().next().getBuildTarget(), p);
}
Also used : Path(java.nio.file.Path) PathSourcePath(com.facebook.buck.rules.PathSourcePath) FrameworkPath(com.facebook.buck.rules.coercer.FrameworkPath) SourcePath(com.facebook.buck.rules.SourcePath) BuildTargetSourcePath(com.facebook.buck.rules.BuildTargetSourcePath) ExplicitBuildTargetSourcePath(com.facebook.buck.rules.ExplicitBuildTargetSourcePath) MacroHandler(com.facebook.buck.rules.macros.MacroHandler) PathSourcePath(com.facebook.buck.rules.PathSourcePath) BuildRule(com.facebook.buck.rules.BuildRule) MacroException(com.facebook.buck.model.MacroException) ExplicitBuildTargetSourcePath(com.facebook.buck.rules.ExplicitBuildTargetSourcePath)

Example 17 with PathSourcePath

use of com.facebook.buck.rules.PathSourcePath in project buck by facebook.

the class PrebuiltOcamlLibraryDescription method createBuildRule.

@Override
public <A extends Arg> OcamlLibrary createBuildRule(TargetGraph targetGraph, final BuildRuleParams params, BuildRuleResolver resolver, final A args) {
    final BuildTarget target = params.getBuildTarget();
    final boolean bytecodeOnly = args.bytecodeOnly.orElse(false);
    final String libDir = args.libDir.orElse("lib");
    final String libName = args.libName.orElse(target.getShortName());
    final String nativeLib = args.nativeLib.orElse(String.format("%s.cmxa", libName));
    final String bytecodeLib = args.bytecodeLib.orElse(String.format("%s.cma", libName));
    final ImmutableList<String> cLibs = args.cLibs;
    final Path libPath = target.getBasePath().resolve(libDir);
    final Path includeDir = libPath.resolve(args.includeDir.orElse(""));
    final Optional<SourcePath> staticNativeLibraryPath = bytecodeOnly ? Optional.empty() : Optional.of(new PathSourcePath(params.getProjectFilesystem(), libPath.resolve(nativeLib)));
    final SourcePath staticBytecodeLibraryPath = new PathSourcePath(params.getProjectFilesystem(), libPath.resolve(bytecodeLib));
    final ImmutableList<SourcePath> staticCLibraryPaths = cLibs.stream().map(input -> new PathSourcePath(params.getProjectFilesystem(), libPath.resolve(input))).collect(MoreCollectors.toImmutableList());
    final SourcePath bytecodeLibraryPath = new PathSourcePath(params.getProjectFilesystem(), libPath.resolve(bytecodeLib));
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(resolver);
    return new PrebuiltOcamlLibrary(params, ruleFinder, staticNativeLibraryPath, staticBytecodeLibraryPath, staticCLibraryPaths, bytecodeLibraryPath, libPath, includeDir);
}
Also used : SourcePath(com.facebook.buck.rules.SourcePath) PathSourcePath(com.facebook.buck.rules.PathSourcePath) Path(java.nio.file.Path) SourcePath(com.facebook.buck.rules.SourcePath) PathSourcePath(com.facebook.buck.rules.PathSourcePath) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) TargetGraph(com.facebook.buck.rules.TargetGraph) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePath(com.facebook.buck.rules.SourcePath) VersionPropagator(com.facebook.buck.versions.VersionPropagator) BuildTarget(com.facebook.buck.model.BuildTarget) SuppressFieldNotInitialized(com.facebook.infer.annotation.SuppressFieldNotInitialized) AbstractDescriptionArg(com.facebook.buck.rules.AbstractDescriptionArg) ImmutableList(com.google.common.collect.ImmutableList) PathSourcePath(com.facebook.buck.rules.PathSourcePath) Optional(java.util.Optional) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) Path(java.nio.file.Path) Description(com.facebook.buck.rules.Description) MoreCollectors(com.facebook.buck.util.MoreCollectors) BuildTarget(com.facebook.buck.model.BuildTarget) PathSourcePath(com.facebook.buck.rules.PathSourcePath) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder)

Example 18 with PathSourcePath

use of com.facebook.buck.rules.PathSourcePath in project buck by facebook.

the class AndroidPackageableCollectorTest method testFindTransitiveDependencies.

/**
   * This is a regression test to ensure that an additional 1 second startup cost is not
   * re-introduced to fb4a.
   */
@Test
public void testFindTransitiveDependencies() throws Exception {
    ProjectFilesystem projectFilesystem = new FakeProjectFilesystem();
    Path prebuiltNativeLibraryPath = Paths.get("java/com/facebook/prebuilt_native_library/libs");
    projectFilesystem.mkdirs(prebuiltNativeLibraryPath);
    // Create an AndroidBinaryRule that transitively depends on two prebuilt JARs. One of the two
    // prebuilt JARs will be listed in the AndroidBinaryRule's no_dx list.
    BuildTarget guavaTarget = BuildTargetFactory.newInstance("//third_party/guava:guava");
    TargetNode<?, ?> guava = PrebuiltJarBuilder.createBuilder(guavaTarget).setBinaryJar(Paths.get("third_party/guava/guava-10.0.1.jar")).build();
    BuildTarget jsr305Target = BuildTargetFactory.newInstance("//third_party/jsr-305:jsr-305");
    TargetNode<?, ?> jsr = PrebuiltJarBuilder.createBuilder(jsr305Target).setBinaryJar(Paths.get("third_party/jsr-305/jsr305.jar")).build();
    TargetNode<?, ?> ndkLibrary = new NdkLibraryBuilder(BuildTargetFactory.newInstance("//java/com/facebook/native_library:library"), projectFilesystem).build();
    BuildTarget prebuiltNativeLibraryTarget = BuildTargetFactory.newInstance("//java/com/facebook/prebuilt_native_library:library");
    TargetNode<?, ?> prebuiltNativeLibraryBuild = PrebuiltNativeLibraryBuilder.newBuilder(prebuiltNativeLibraryTarget, projectFilesystem).setNativeLibs(prebuiltNativeLibraryPath).setIsAsset(true).build();
    BuildTarget libraryRuleTarget = BuildTargetFactory.newInstance("//java/src/com/facebook:example");
    TargetNode<?, ?> library = JavaLibraryBuilder.createBuilder(libraryRuleTarget).setProguardConfig(new FakeSourcePath("debug.pro")).addSrc(Paths.get("Example.java")).addDep(guavaTarget).addDep(jsr305Target).addDep(prebuiltNativeLibraryBuild.getBuildTarget()).addDep(ndkLibrary.getBuildTarget()).build();
    BuildTarget manifestTarget = BuildTargetFactory.newInstance("//java/src/com/facebook:res");
    TargetNode<?, ?> manifest = AndroidResourceBuilder.createBuilder(manifestTarget).setManifest(new PathSourcePath(projectFilesystem, Paths.get("java/src/com/facebook/module/AndroidManifest.xml"))).setAssets(new FakeSourcePath("assets")).build();
    BuildTarget keystoreTarget = BuildTargetFactory.newInstance("//keystore:debug");
    TargetNode<?, ?> keystore = KeystoreBuilder.createBuilder(keystoreTarget).setStore(new FakeSourcePath(projectFilesystem, "keystore/debug.keystore")).setProperties(new FakeSourcePath(projectFilesystem, "keystore/debug.keystore.properties")).build();
    ImmutableSortedSet<BuildTarget> originalDepsTargets = ImmutableSortedSet.of(libraryRuleTarget, manifestTarget);
    BuildTarget binaryTarget = BuildTargetFactory.newInstance("//java/src/com/facebook:app");
    TargetNode<?, ?> binary = AndroidBinaryBuilder.createBuilder(binaryTarget).setOriginalDeps(originalDepsTargets).setBuildTargetsToExcludeFromDex(ImmutableSet.of(BuildTargetFactory.newInstance("//third_party/guava:guava"))).setManifest(new FakeSourcePath("java/src/com/facebook/AndroidManifest.xml")).setKeystore(keystoreTarget).build();
    TargetGraph targetGraph = TargetGraphFactory.newInstance(binary, library, manifest, keystore, ndkLibrary, prebuiltNativeLibraryBuild, guava, jsr);
    BuildRuleResolver ruleResolver = new BuildRuleResolver(targetGraph, new DefaultTargetNodeToBuildRuleTransformer());
    SourcePathResolver pathResolver = new SourcePathResolver(new SourcePathRuleFinder(ruleResolver));
    AndroidBinary binaryRule = (AndroidBinary) ruleResolver.requireRule(binaryTarget);
    NdkLibrary ndkLibraryRule = (NdkLibrary) ruleResolver.requireRule(ndkLibrary.getBuildTarget());
    NativeLibraryBuildRule prebuildNativeLibraryRule = (NativeLibraryBuildRule) ruleResolver.requireRule(prebuiltNativeLibraryTarget);
    // Verify that the correct transitive dependencies are found.
    AndroidPackageableCollection packageableCollection = binaryRule.getAndroidPackageableCollection();
    assertResolvedEquals("Because guava was passed to no_dx, it should not be in the classpathEntriesToDex list", pathResolver, ImmutableSet.of(ruleResolver.getRule(jsr305Target).getSourcePathToOutput(), ruleResolver.getRule(libraryRuleTarget).getSourcePathToOutput()), packageableCollection.getClasspathEntriesToDex());
    assertResolvedEquals("Because guava was passed to no_dx, it should not be treated as a third-party JAR whose " + "resources need to be extracted and repacked in the APK. If this is done, then code " + "in the guava-10.0.1.dex.1.jar in the APK's assets/ tmp may try to load the resource " + "from the APK as a ZipFileEntry rather than as a resource within " + "guava-10.0.1.dex.1.jar. Loading a resource in this way could take substantially " + "longer. Specifically, this was observed to take over one second longer to load " + "the resource in fb4a. Because the resource was loaded on startup, this introduced a " + "substantial regression in the startup time for the fb4a app.", pathResolver, ImmutableSet.of(ruleResolver.getRule(jsr305Target).getSourcePathToOutput()), packageableCollection.getPathsToThirdPartyJars());
    assertResolvedEquals("Because assets directory was passed an AndroidResourceRule it should be added to the " + "transitive dependencies", pathResolver, ImmutableSet.of(new DefaultBuildTargetSourcePath(manifestTarget.withAppendedFlavors(AndroidResourceDescription.ASSETS_SYMLINK_TREE_FLAVOR))), packageableCollection.getAssetsDirectories());
    assertResolvedEquals("Because a native library was declared as a dependency, it should be added to the " + "transitive dependencies.", pathResolver, ImmutableSet.<SourcePath>of(new PathSourcePath(new FakeProjectFilesystem(), ndkLibraryRule.getLibraryPath())), ImmutableSet.copyOf(packageableCollection.getNativeLibsDirectories().values()));
    assertResolvedEquals("Because a prebuilt native library  was declared as a dependency (and asset), it should " + "be added to the transitive dependecies.", pathResolver, ImmutableSet.<SourcePath>of(new PathSourcePath(new FakeProjectFilesystem(), prebuildNativeLibraryRule.getLibraryPath())), ImmutableSet.copyOf(packageableCollection.getNativeLibAssetsDirectories().values()));
    assertEquals(ImmutableSet.of(new FakeSourcePath("debug.pro")), packageableCollection.getProguardConfigs());
}
Also used : SourcePath(com.facebook.buck.rules.SourcePath) FakeSourcePath(com.facebook.buck.rules.FakeSourcePath) Path(java.nio.file.Path) DefaultBuildTargetSourcePath(com.facebook.buck.rules.DefaultBuildTargetSourcePath) PathSourcePath(com.facebook.buck.rules.PathSourcePath) FakeSourcePath(com.facebook.buck.rules.FakeSourcePath) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) PathSourcePath(com.facebook.buck.rules.PathSourcePath) TargetGraph(com.facebook.buck.rules.TargetGraph) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) DefaultBuildTargetSourcePath(com.facebook.buck.rules.DefaultBuildTargetSourcePath) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) BuildTarget(com.facebook.buck.model.BuildTarget) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) Test(org.junit.Test)

Example 19 with PathSourcePath

use of com.facebook.buck.rules.PathSourcePath in project buck by facebook.

the class AndroidResourceTest method testGetRDotJavaPackageWhenPackageIsNotSpecified.

@Test
public void testGetRDotJavaPackageWhenPackageIsNotSpecified() throws IOException {
    ProjectFilesystem projectFilesystem = FakeProjectFilesystem.createRealTempFilesystem();
    BuildTarget buildTarget = BuildTargetFactory.newInstance(projectFilesystem, "//java/src/com/facebook/base:res");
    BuildRuleParams params = new FakeBuildRuleParamsBuilder(buildTarget).setProjectFilesystem(projectFilesystem).build();
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer()));
    SourcePathResolver resolver = new SourcePathResolver(ruleFinder);
    AndroidResource androidResource = new AndroidResource(params, ruleFinder, /* deps */
    ImmutableSortedSet.of(), new FakeSourcePath("foo/res"), ImmutableSortedMap.of(Paths.get("values/strings.xml"), new FakeSourcePath("foo/res/values/strings.xml")), /* rDotJavaPackage */
    null, /* assets */
    null, /* assetsSrcs */
    ImmutableSortedMap.of(), /* manifestFile */
    new PathSourcePath(projectFilesystem, Paths.get("foo/AndroidManifest.xml")), /* hasWhitelistedStrings */
    false);
    projectFilesystem.writeContentsToPath("com.ex.pkg\n", resolver.getRelativePath(androidResource.getPathToRDotJavaPackageFile()));
    FakeOnDiskBuildInfo onDiskBuildInfo = new FakeOnDiskBuildInfo();
    androidResource.initializeFromDisk(onDiskBuildInfo);
    assertEquals("com.ex.pkg", androidResource.getRDotJavaPackage());
}
Also used : FakeSourcePath(com.facebook.buck.rules.FakeSourcePath) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) BuildTarget(com.facebook.buck.model.BuildTarget) PathSourcePath(com.facebook.buck.rules.PathSourcePath) FakeOnDiskBuildInfo(com.facebook.buck.rules.FakeOnDiskBuildInfo) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) FakeBuildRuleParamsBuilder(com.facebook.buck.rules.FakeBuildRuleParamsBuilder) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) Test(org.junit.Test)

Example 20 with PathSourcePath

use of com.facebook.buck.rules.PathSourcePath in project buck by facebook.

the class AndroidManifestTest method testBuildInternal.

@Test
public void testBuildInternal() throws IOException {
    AndroidManifest androidManifest = createSimpleAndroidManifestRule();
    ProjectFilesystem filesystem = androidManifest.getProjectFilesystem();
    Path skeletonPath = Paths.get("java/com/example/AndroidManifestSkeleton.xml");
    // Mock out a BuildContext whose DependencyGraph will be traversed.
    BuildContext buildContext = FakeBuildContext.NOOP_CONTEXT;
    SourcePathResolver pathResolver = buildContext.getSourcePathResolver();
    expect(pathResolver.getAbsolutePath(new PathSourcePath(filesystem, skeletonPath))).andStubReturn(filesystem.resolve(skeletonPath));
    expect(pathResolver.getAllAbsolutePaths(ImmutableSortedSet.of())).andStubReturn(ImmutableSortedSet.of());
    Path outPath = Paths.get("foo/bar");
    expect(pathResolver.getRelativePath(androidManifest.getSourcePathToOutput())).andStubReturn(outPath);
    replay(pathResolver);
    List<Step> steps = androidManifest.getBuildSteps(buildContext, new FakeBuildableContext());
    Step generateManifestStep = steps.get(2);
    assertEquals(new GenerateManifestStep(filesystem, filesystem.resolve(skeletonPath), /* libraryManifestPaths */
    ImmutableSet.of(), outPath), generateManifestStep);
}
Also used : FakeSourcePath(com.facebook.buck.rules.FakeSourcePath) Path(java.nio.file.Path) ExplicitBuildTargetSourcePath(com.facebook.buck.rules.ExplicitBuildTargetSourcePath) PathSourcePath(com.facebook.buck.rules.PathSourcePath) FakeBuildableContext(com.facebook.buck.rules.FakeBuildableContext) FakeBuildContext(com.facebook.buck.rules.FakeBuildContext) BuildContext(com.facebook.buck.rules.BuildContext) PathSourcePath(com.facebook.buck.rules.PathSourcePath) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) Step(com.facebook.buck.step.Step) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) Test(org.junit.Test)

Aggregations

PathSourcePath (com.facebook.buck.rules.PathSourcePath)114 Test (org.junit.Test)82 BuildRuleResolver (com.facebook.buck.rules.BuildRuleResolver)69 FakeProjectFilesystem (com.facebook.buck.testutil.FakeProjectFilesystem)68 SourcePath (com.facebook.buck.rules.SourcePath)65 Path (java.nio.file.Path)63 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)60 DefaultTargetNodeToBuildRuleTransformer (com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer)58 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)56 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)52 BuildTarget (com.facebook.buck.model.BuildTarget)51 TargetGraph (com.facebook.buck.rules.TargetGraph)37 BuildRule (com.facebook.buck.rules.BuildRule)26 FakeSourcePath (com.facebook.buck.rules.FakeSourcePath)23 DefaultBuildTargetSourcePath (com.facebook.buck.rules.DefaultBuildTargetSourcePath)21 StackedFileHashCache (com.facebook.buck.util.cache.StackedFileHashCache)16 RuleKey (com.facebook.buck.rules.RuleKey)15 DefaultFileHashCache (com.facebook.buck.util.cache.DefaultFileHashCache)15 BuildRuleParams (com.facebook.buck.rules.BuildRuleParams)14 ImmutableList (com.google.common.collect.ImmutableList)14