Search in sources :

Example 11 with UnflavoredBuildTarget

use of com.facebook.buck.model.UnflavoredBuildTarget in project buck by facebook.

the class AbstractCxxSourceRuleFactory method buildPrecompiledHeader.

/**
   * Look up or build a precompiled header build rule which this build rule is requesting.
   *
   * <p>
   * This method will first try to determine whether a matching PCH was already created;
   * if so, it will be reused.  This is done by searching the cache in the {@link BuildRuleResolver}
   * owned by this class.  If this ends up building a new instance of {@link CxxPrecompiledHeader},
   * it will be added to the resolver cache.
   * </p>
   */
private CxxPrecompiledHeader buildPrecompiledHeader(PreprocessorDelegate preprocessorDelegate, CxxSource.Type sourceType, CxxToolFlags compilerFlags, SourcePath headerPath, DepsBuilder depsBuilder, UnflavoredBuildTarget templateTarget, ImmutableSortedSet<Flavor> flavors) {
    BuildTarget target = BuildTarget.builder(templateTarget).addAllFlavors(flavors).build();
    Optional<CxxPrecompiledHeader> existingRule = getResolver().getRuleOptionalWithType(target, CxxPrecompiledHeader.class);
    if (existingRule.isPresent()) {
        return existingRule.get();
    }
    // Give the PCH a filename that looks like a header file with .gch appended to it, GCC-style.
    // GCC accepts an "-include" flag with the .h file as its arg, and auto-appends ".gch" to
    // automagically use the precompiled header in place of the original header.  Of course in
    // our case we'll only have the ".gch" file, which is alright; the ".h" isn't truly needed.
    Path output = BuildTargets.getGenPath(getParams().getProjectFilesystem(), target, "%s.h.gch");
    CompilerDelegate compilerDelegate = new CompilerDelegate(getPathResolver(), getCxxPlatform().getCompilerDebugPathSanitizer(), CxxSourceTypes.getCompiler(getCxxPlatform(), CxxSourceTypes.getPreprocessorOutputType(sourceType)).resolve(getResolver()), compilerFlags);
    depsBuilder.add(compilerDelegate);
    depsBuilder.add(headerPath);
    BuildRuleParams params = getParams().withBuildTarget(target).copyReplacingDeclaredAndExtraDeps(Suppliers.ofInstance(depsBuilder.build()), Suppliers.ofInstance(ImmutableSortedSet.of()));
    CxxPrecompiledHeader rule = new CxxPrecompiledHeader(params, output, preprocessorDelegate, compilerDelegate, compilerFlags, headerPath, sourceType, getCxxPlatform().getCompilerDebugPathSanitizer(), getCxxPlatform().getAssemblerDebugPathSanitizer());
    getResolver().addToIndex(rule);
    return rule;
}
Also used : FrameworkPath(com.facebook.buck.rules.coercer.FrameworkPath) SourcePath(com.facebook.buck.rules.SourcePath) BuildTargetSourcePath(com.facebook.buck.rules.BuildTargetSourcePath) Path(java.nio.file.Path) ExplicitBuildTargetSourcePath(com.facebook.buck.rules.ExplicitBuildTargetSourcePath) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) UnflavoredBuildTarget(com.facebook.buck.model.UnflavoredBuildTarget) BuildTarget(com.facebook.buck.model.BuildTarget)

Example 12 with UnflavoredBuildTarget

use of com.facebook.buck.model.UnflavoredBuildTarget in project buck by facebook.

the class JsLibraryDescription method changePathPrefix.

private static Path changePathPrefix(SourcePath sourcePath, String basePath, BuildRuleParams params, SourcePathResolver sourcePathResolver, UnflavoredBuildTarget target) {
    final Path directoryOfBuildFile = target.getCellPath().resolve(target.getBasePath());
    final Path transplantTo = MorePaths.normalize(directoryOfBuildFile.resolve(basePath));
    final Path absolutePath = sourcePathResolver.getPathSourcePath(sourcePath).map(// for sub paths, replace the leading directory with the base path
    pathSourcePath -> transplantTo.resolve(MorePaths.relativize(directoryOfBuildFile, sourcePathResolver.getAbsolutePath(sourcePath)))).orElse(// build target output paths are replaced completely
    transplantTo);
    return params.getProjectFilesystem().getPathRelativeToProjectRoot(absolutePath).orElseThrow(() -> new HumanReadableException("%s: Using '%s' as base path for '%s' would move the file " + "out of the project root.", target, basePath, sourcePathResolver.getRelativePath(sourcePath)));
}
Also used : SourcePath(com.facebook.buck.rules.SourcePath) Path(java.nio.file.Path) UnflavoredBuildTarget(com.facebook.buck.model.UnflavoredBuildTarget) Iterables(com.google.common.collect.Iterables) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePath(com.facebook.buck.rules.SourcePath) Either(com.facebook.buck.model.Either) Flavored(com.facebook.buck.model.Flavored) Function(java.util.function.Function) BuildRule(com.facebook.buck.rules.BuildRule) ImmutableBiMap(com.google.common.collect.ImmutableBiMap) ImmutableList(com.google.common.collect.ImmutableList) NoSuchBuildTargetException(com.facebook.buck.parser.NoSuchBuildTargetException) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) Pair(com.facebook.buck.model.Pair) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) Path(java.nio.file.Path) Nullable(javax.annotation.Nullable) MoreCollectors(com.facebook.buck.util.MoreCollectors) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) ImmutableSet(com.google.common.collect.ImmutableSet) TargetGraph(com.facebook.buck.rules.TargetGraph) HumanReadableException(com.facebook.buck.util.HumanReadableException) BuildTarget(com.facebook.buck.model.BuildTarget) SuppressFieldNotInitialized(com.facebook.infer.annotation.SuppressFieldNotInitialized) MorePaths(com.facebook.buck.io.MorePaths) AbstractDescriptionArg(com.facebook.buck.rules.AbstractDescriptionArg) Paths(java.nio.file.Paths) Hint(com.facebook.buck.rules.Hint) WorkerTool(com.facebook.buck.shell.WorkerTool) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) Flavor(com.facebook.buck.model.Flavor) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) Description(com.facebook.buck.rules.Description) HumanReadableException(com.facebook.buck.util.HumanReadableException)

Example 13 with UnflavoredBuildTarget

use of com.facebook.buck.model.UnflavoredBuildTarget in project buck by facebook.

the class DefaultParserTargetGroupFactory method createTargetNode.

@Override
public TargetGroup createTargetNode(Cell cell, Path buildFile, BuildTarget target, Map<String, Object> rawNode, Function<PerfEventId, SimplePerfEvent.Scope> perfEventScope) {
    Preconditions.checkArgument(!target.isFlavored());
    UnflavoredBuildTarget unflavoredBuildTarget = target.withoutCell().getUnflavoredBuildTarget();
    UnflavoredBuildTarget unflavoredBuildTargetFromRawData = RawNodeParsePipeline.parseBuildTargetFromRawRule(cell.getRoot(), rawNode, buildFile);
    if (!unflavoredBuildTarget.equals(unflavoredBuildTargetFromRawData)) {
        throw new IllegalStateException(String.format("Inconsistent internal state, target from data: %s, expected: %s, raw data: %s", unflavoredBuildTargetFromRawData, unflavoredBuildTarget, Joiner.on(',').withKeyValueSeparator("->").join(rawNode)));
    }
    BuildRuleType buildRuleType = parseBuildRuleTypeFromRawRule(cell, rawNode);
    // Because of the way that the parser works, we know this can never return null.
    Description<?> description = cell.getDescription(buildRuleType);
    Cell targetCell = cell.getCell(target);
    TargetGroupDescription.Arg constructorArg = (TargetGroupDescription.Arg) description.createUnpopulatedConstructorArg();
    try {
        ImmutableSet.Builder<BuildTarget> declaredDeps = ImmutableSet.builder();
        ImmutableSet.Builder<VisibilityPattern> visibilityPatterns = ImmutableSet.builder();
        try (SimplePerfEvent.Scope scope = perfEventScope.apply(PerfEventId.of("MarshalledConstructorArg"))) {
            marshaller.populate(targetCell.getCellPathResolver(), targetCell.getFilesystem(), target, constructorArg, declaredDeps, visibilityPatterns, rawNode);
        }
        try (SimplePerfEvent.Scope scope = perfEventScope.apply(PerfEventId.of("CreatedTargetNode"))) {
            Hasher hasher = Hashing.sha1().newHasher();
            hasher.putString(BuckVersion.getVersion(), UTF_8);
            JsonObjectHashing.hashJsonObject(hasher, rawNode);
            TargetGroup node = new TargetGroup(constructorArg.targets, constructorArg.restrictOutboundVisibility, target);
            return node;
        }
    } catch (ParamInfoException e) {
        throw new HumanReadableException("%s: %s", target, e.getMessage());
    }
}
Also used : TargetGroupDescription(com.facebook.buck.groups.TargetGroupDescription) UnflavoredBuildTarget(com.facebook.buck.model.UnflavoredBuildTarget) VisibilityPattern(com.facebook.buck.rules.VisibilityPattern) ParamInfoException(com.facebook.buck.rules.ParamInfoException) Hasher(com.google.common.hash.Hasher) ImmutableSet(com.google.common.collect.ImmutableSet) UnflavoredBuildTarget(com.facebook.buck.model.UnflavoredBuildTarget) BuildTarget(com.facebook.buck.model.BuildTarget) HumanReadableException(com.facebook.buck.util.HumanReadableException) TargetGroup(com.facebook.buck.rules.TargetGroup) BuildRuleType(com.facebook.buck.rules.BuildRuleType) SimplePerfEvent(com.facebook.buck.event.SimplePerfEvent) Cell(com.facebook.buck.rules.Cell)

Example 14 with UnflavoredBuildTarget

use of com.facebook.buck.model.UnflavoredBuildTarget in project buck by facebook.

the class BuildTargetParser method parse.

/**
   * @param buildTargetName either a fully-qualified name or relative to the {@link BuildTargetPatternParser}.
   *     For example, inside {@code first-party/orca/orcaapp/BUCK}, which can be obtained by
   *     calling {@code ParseContext.forBaseName("first-party/orca/orcaapp")},
   *     {@code //first-party/orca/orcaapp:assets} and {@code :assets} refer to the same target.
   *     However, from the command line the context is obtained by calling
   *     {@link BuildTargetPatternParser#fullyQualified()} and relative names are
   *     not recognized.
   * @param buildTargetPatternParser how targets should be interpreted, such in the context of a
   *     specific build file or only as fully-qualified names (as is the case for targets from the
   *     command line).
   */
public BuildTarget parse(String buildTargetName, BuildTargetPatternParser<?> buildTargetPatternParser, CellPathResolver cellNames) {
    if (buildTargetName.endsWith(BUILD_RULE_SEPARATOR) && !buildTargetPatternParser.isWildCardAllowed()) {
        throw new BuildTargetParseException(String.format("%s cannot end with a colon", buildTargetName));
    }
    Optional<String> givenCellName = Optional.empty();
    String targetAfterCell = buildTargetName;
    if (buildTargetName.contains(BUILD_RULE_PREFIX) && !buildTargetName.startsWith(BUILD_RULE_PREFIX)) {
        int slashIndex = buildTargetName.indexOf(BUILD_RULE_PREFIX);
        givenCellName = Optional.of(buildTargetName.substring(0, slashIndex));
        targetAfterCell = buildTargetName.substring(slashIndex);
    }
    if (givenCellName.isPresent() && givenCellName.get().isEmpty()) {
        throw new BuildTargetParseException("Cell name must not be empty.");
    }
    List<String> parts = BUILD_RULE_SEPARATOR_SPLITTER.splitToList(targetAfterCell);
    if (parts.size() != 2) {
        throw new BuildTargetParseException(String.format("%s must contain exactly one colon (found %d)", buildTargetName, parts.size() - 1));
    }
    String baseName = parts.get(0).isEmpty() ? buildTargetPatternParser.getBaseName() : parts.get(0);
    String shortName = parts.get(1);
    Iterable<String> flavorNames = new HashSet<>();
    int hashIndex = shortName.indexOf("#");
    if (hashIndex != -1 && hashIndex < shortName.length()) {
        flavorNames = flavorParser.parseFlavorString(shortName.substring(hashIndex + 1));
        shortName = shortName.substring(0, hashIndex);
    }
    Preconditions.checkNotNull(baseName);
    // On Windows, baseName may contain backslashes, which are not permitted by BuildTarget.
    baseName = baseName.replace("\\", "/");
    checkBaseName(baseName, buildTargetName);
    UnflavoredBuildTarget.Builder unflavoredBuilder = UnflavoredBuildTarget.builder().setBaseName(baseName).setShortName(shortName).setCellPath(cellNames.getCellPath(givenCellName)).setCell(givenCellName);
    UnflavoredBuildTarget unflavoredBuildTarget = unflavoredBuilder.build();
    BuildTarget.Builder builder = BuildTarget.builder(unflavoredBuildTarget);
    for (String flavor : flavorNames) {
        builder.addFlavors(InternalFlavor.of(flavor));
    }
    return flavoredTargetCache.intern(builder.build());
}
Also used : UnflavoredBuildTarget(com.facebook.buck.model.UnflavoredBuildTarget) BuildTarget(com.facebook.buck.model.BuildTarget) UnflavoredBuildTarget(com.facebook.buck.model.UnflavoredBuildTarget) HashSet(java.util.HashSet)

Example 15 with UnflavoredBuildTarget

use of com.facebook.buck.model.UnflavoredBuildTarget in project buck by facebook.

the class DaemonicCellState method invalidatePath.

int invalidatePath(Path path) {
    try (AutoCloseableLock writeLock = rawAndComputedNodesLock.writeLock()) {
        int invalidatedRawNodes = 0;
        ImmutableSet<Map<String, Object>> rawNodes = allRawNodes.getIfPresent(path);
        if (rawNodes != null) {
            // Increment the counter
            invalidatedRawNodes = rawNodes.size();
            for (Map<String, Object> rawNode : rawNodes) {
                UnflavoredBuildTarget target = RawNodeParsePipeline.parseBuildTargetFromRawRule(cellRoot, rawNode, path);
                LOG.debug("Invalidating target for path %s: %s", path, target);
                for (CacheImpl<?> cache : typedNodeCaches.values()) {
                    cache.allComputedNodes.invalidateAll(targetsCornucopia.get(target));
                }
                targetsCornucopia.removeAll(target);
            }
            allRawNodes.invalidate(path);
        }
        // We may have been given a file that other build files depend on. Iteratively remove those.
        Iterable<Path> dependents = buildFileDependents.get(path);
        LOG.debug("Invalidating dependents for path %s: %s", path, dependents);
        for (Path dependent : dependents) {
            if (dependent.equals(path)) {
                continue;
            }
            invalidatedRawNodes += invalidatePath(dependent);
        }
        buildFileDependents.removeAll(path);
        buildFileConfigs.remove(path);
        buildFileEnv.remove(path);
        return invalidatedRawNodes;
    }
}
Also used : Path(java.nio.file.Path) AutoCloseableLock(com.facebook.buck.util.concurrent.AutoCloseableLock) UnflavoredBuildTarget(com.facebook.buck.model.UnflavoredBuildTarget) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map)

Aggregations

UnflavoredBuildTarget (com.facebook.buck.model.UnflavoredBuildTarget)19 BuildTarget (com.facebook.buck.model.BuildTarget)9 ImmutableSet (com.google.common.collect.ImmutableSet)7 HumanReadableException (com.facebook.buck.util.HumanReadableException)6 Path (java.nio.file.Path)6 BuildRule (com.facebook.buck.rules.BuildRule)4 BuildRuleParams (com.facebook.buck.rules.BuildRuleParams)4 SourcePath (com.facebook.buck.rules.SourcePath)4 Flavor (com.facebook.buck.model.Flavor)3 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)3 ImmutableList (com.google.common.collect.ImmutableList)3 ImmutableMap (com.google.common.collect.ImmutableMap)3 SimplePerfEvent (com.facebook.buck.event.SimplePerfEvent)2 MavenPublishable (com.facebook.buck.jvm.java.MavenPublishable)2 Flavored (com.facebook.buck.model.Flavored)2 BuildRuleType (com.facebook.buck.rules.BuildRuleType)2 Cell (com.facebook.buck.rules.Cell)2 ExplicitBuildTargetSourcePath (com.facebook.buck.rules.ExplicitBuildTargetSourcePath)2 ParamInfoException (com.facebook.buck.rules.ParamInfoException)2 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)2