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;
}
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)));
}
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());
}
}
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());
}
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;
}
}
Aggregations