Search in sources :

Example 1 with BuckConfig

use of com.facebook.buck.cli.BuckConfig in project buck by facebook.

the class DistBuildState method createBuckConfig.

private static BuckConfig createBuckConfig(Config config, ProjectFilesystem projectFilesystem, BuildJobStateBuckConfig remoteBuckConfig) {
    Architecture remoteArchitecture = Architecture.valueOf(remoteBuckConfig.getArchitecture());
    Architecture localArchitecture = Architecture.detect();
    Preconditions.checkState(remoteArchitecture.equals(localArchitecture), "Trying to load config with architecture %s on a machine that is %s. " + "This is not supported.", remoteArchitecture, localArchitecture);
    Platform remotePlatform = Platform.valueOf(remoteBuckConfig.getPlatform());
    Platform localPlatform = Platform.detect();
    Preconditions.checkState(remotePlatform.equals(localPlatform), "Trying to load config with platform %s on a machine that is %s. This is not supported.", remotePlatform, localPlatform);
    return new BuckConfig(config, projectFilesystem, remoteArchitecture, remotePlatform, ImmutableMap.copyOf(remoteBuckConfig.getUserEnvironment()), new DefaultCellPathResolver(projectFilesystem.getRootPath(), config));
}
Also used : DefaultCellPathResolver(com.facebook.buck.rules.DefaultCellPathResolver) Architecture(com.facebook.buck.util.environment.Architecture) Platform(com.facebook.buck.util.environment.Platform) BuckConfig(com.facebook.buck.cli.BuckConfig) BuildJobStateBuckConfig(com.facebook.buck.distributed.thrift.BuildJobStateBuckConfig)

Example 2 with BuckConfig

use of com.facebook.buck.cli.BuckConfig in project buck by facebook.

the class JavaDepsFinder method findDepsForBuildFiles.

private DepsForBuildFiles findDepsForBuildFiles(final TargetGraph graph, final DependencyInfo dependencyInfo, final Console console) {
    // For the rules that expect to have their deps generated, look through all of their required
    // symbols and try to find the build rule that provides each symbols. Store these build rules in
    // the depsForBuildFiles data structure.
    //
    // Currently, we process each rule with autodeps=True on a single thread. See the class overview
    // for DepsForBuildFiles about what it would take to do this work in a multi-threaded way.
    DepsForBuildFiles depsForBuildFiles = new DepsForBuildFiles();
    for (final TargetNode<?, ?> rule : dependencyInfo.rulesWithAutodeps) {
        final Set<BuildTarget> providedDeps = dependencyInfo.rulesWithAutodepsToProvidedDeps.get(rule);
        final Predicate<TargetNode<?, ?>> isVisibleDepNotAlreadyInProvidedDeps = provider -> provider.isVisibleTo(graph, rule) && !providedDeps.contains(provider.getBuildTarget());
        final boolean isJavaTestRule = rule.getDescription() instanceof JavaTestDescription;
        for (DependencyType type : DependencyType.values()) {
            HashMultimap<TargetNode<?, ?>, String> ruleToSymbolsMap;
            switch(type) {
                case DEPS:
                    ruleToSymbolsMap = dependencyInfo.ruleToRequiredSymbols;
                    break;
                case EXPORTED_DEPS:
                    ruleToSymbolsMap = dependencyInfo.ruleToExportedSymbols;
                    break;
                default:
                    throw new IllegalStateException("Unrecognized type: " + type);
            }
            final DependencyType typeOfDepToAdd;
            if (isJavaTestRule) {
                // java_test rules do not honor exported_deps: add all dependencies to the ordinary deps.
                typeOfDepToAdd = DependencyType.DEPS;
            } else {
                typeOfDepToAdd = type;
            }
            for (String requiredSymbol : ruleToSymbolsMap.get(rule)) {
                BuildTarget provider = findProviderForSymbolFromBuckConfig(requiredSymbol);
                if (provider != null) {
                    depsForBuildFiles.addDep(rule.getBuildTarget(), provider, typeOfDepToAdd);
                    continue;
                }
                Set<TargetNode<?, ?>> providers = dependencyInfo.symbolToProviders.get(requiredSymbol);
                SortedSet<TargetNode<?, ?>> candidateProviders = providers.stream().filter(isVisibleDepNotAlreadyInProvidedDeps).collect(MoreCollectors.toImmutableSortedSet(Comparator.<TargetNode<?, ?>>naturalOrder()));
                int numCandidates = candidateProviders.size();
                if (numCandidates == 1) {
                    depsForBuildFiles.addDep(rule.getBuildTarget(), Iterables.getOnlyElement(candidateProviders).getBuildTarget(), typeOfDepToAdd);
                } else if (numCandidates > 1) {
                    // Warn the user that there is an ambiguity. This could be very common with macros that
                    // generate multiple versions of a java_library() with the same sources.
                    // If numProviders is 0, then hopefully the dep is provided by something the user
                    // hardcoded in the BUCK file.
                    console.printErrorText(String.format("WARNING: Multiple providers for %s: %s. " + "Consider adding entry to .buckconfig to eliminate ambiguity:\n" + "[autodeps]\n" + "java-package-mappings = %s => %s", requiredSymbol, Joiner.on(", ").join(candidateProviders), requiredSymbol, Iterables.getFirst(candidateProviders, null)));
                } else {
                    // If there aren't any candidates, then see if there is a visible rule that can provide
                    // the symbol via its exported_deps. We make this a secondary check because we prefer to
                    // depend on the rule that defines the symbol directly rather than one of possibly many
                    // rules that provides it via its exported_deps.
                    ImmutableSortedSet<TargetNode<?, ?>> newCandidates = providers.stream().flatMap(candidate -> dependencyInfo.ruleToRulesThatExportIt.get(candidate).stream()).filter(ruleThatExportsCandidate -> ruleThatExportsCandidate.isVisibleTo(graph, rule)).collect(MoreCollectors.toImmutableSortedSet(Comparator.<TargetNode<?, ?>>naturalOrder()));
                    int numNewCandidates = newCandidates.size();
                    if (numNewCandidates == 1) {
                        depsForBuildFiles.addDep(rule.getBuildTarget(), Iterables.getOnlyElement(newCandidates).getBuildTarget(), typeOfDepToAdd);
                    } else if (numNewCandidates > 1) {
                        console.printErrorText(String.format("WARNING: No providers found for '%s' for build rule %s, " + "but there are multiple rules that export a rule to provide %s: %s", requiredSymbol, rule.getBuildTarget(), requiredSymbol, Joiner.on(", ").join(newCandidates)));
                    }
                // In the case that numNewCandidates is 0, we assume that the user is taking
                // responsibility for declaring a provider for the symbol by hardcoding it in the deps.
                }
            }
        }
    }
    return depsForBuildFiles;
}
Also used : Iterables(com.google.common.collect.Iterables) BuildRuleType(com.facebook.buck.rules.BuildRuleType) CellPathResolver(com.facebook.buck.rules.CellPathResolver) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) SortedSet(java.util.SortedSet) JavaBuckConfig(com.facebook.buck.jvm.java.JavaBuckConfig) ExecutionContext(com.facebook.buck.step.ExecutionContext) HashSet(java.util.HashSet) AndroidLibraryDescription(com.facebook.buck.android.AndroidLibraryDescription) BuckConfig(com.facebook.buck.cli.BuckConfig) HashMultimap(com.google.common.collect.HashMultimap) PrebuiltJarDescription(com.facebook.buck.jvm.java.PrebuiltJarDescription) JavaFileParser(com.facebook.buck.jvm.java.JavaFileParser) BuildTargetPatternParser(com.facebook.buck.parser.BuildTargetPatternParser) BuildTargetParser(com.facebook.buck.parser.BuildTargetParser) Map(java.util.Map) JavaLibraryDescription(com.facebook.buck.jvm.java.JavaLibraryDescription) Splitter(com.google.common.base.Splitter) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) Nullable(javax.annotation.Nullable) MoreCollectors(com.facebook.buck.util.MoreCollectors) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) ImmutableSet(com.google.common.collect.ImmutableSet) DepsForBuildFiles(com.facebook.buck.autodeps.DepsForBuildFiles) Predicate(java.util.function.Predicate) TargetGraph(com.facebook.buck.rules.TargetGraph) TargetNode(com.facebook.buck.rules.TargetNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) CharMatcher(com.google.common.base.CharMatcher) Set(java.util.Set) JavacOptions(com.facebook.buck.jvm.java.JavacOptions) Console(com.facebook.buck.util.Console) BuildTarget(com.facebook.buck.model.BuildTarget) Maps(com.google.common.collect.Maps) JavaTestDescription(com.facebook.buck.jvm.java.JavaTestDescription) BuildResult(com.facebook.buck.rules.BuildResult) Futures(com.google.common.util.concurrent.Futures) BuildEngineBuildContext(com.facebook.buck.rules.BuildEngineBuildContext) Stream(java.util.stream.Stream) DependencyType(com.facebook.buck.autodeps.DepsForBuildFiles.DependencyType) BuildEngine(com.facebook.buck.rules.BuildEngine) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) Comparator(java.util.Comparator) Description(com.facebook.buck.rules.Description) Joiner(com.google.common.base.Joiner) TargetNode(com.facebook.buck.rules.TargetNode) DepsForBuildFiles(com.facebook.buck.autodeps.DepsForBuildFiles) BuildTarget(com.facebook.buck.model.BuildTarget) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) DependencyType(com.facebook.buck.autodeps.DepsForBuildFiles.DependencyType) JavaTestDescription(com.facebook.buck.jvm.java.JavaTestDescription)

Example 3 with BuckConfig

use of com.facebook.buck.cli.BuckConfig in project buck by facebook.

the class CellProvider method createForLocalBuild.

/**
   * Create a cell provider at a given root.
   */
public static CellProvider createForLocalBuild(ProjectFilesystem rootFilesystem, Watchman watchman, BuckConfig rootConfig, CellConfig rootCellConfigOverrides, KnownBuildRuleTypesFactory knownBuildRuleTypesFactory) throws IOException {
    DefaultCellPathResolver rootCellCellPathResolver = new DefaultCellPathResolver(rootFilesystem.getRootPath(), rootConfig.getConfig());
    ImmutableMap<RelativeCellName, Path> transitiveCellPathMapping = rootCellCellPathResolver.getTransitivePathMapping();
    ImmutableMap<Path, RawConfig> pathToConfigOverrides;
    try {
        pathToConfigOverrides = rootCellConfigOverrides.getOverridesByPath(transitiveCellPathMapping);
    } catch (CellConfig.MalformedOverridesException e) {
        throw new HumanReadableException(e.getMessage());
    }
    ImmutableSet<Path> allRoots = ImmutableSet.copyOf(transitiveCellPathMapping.values());
    return new CellProvider(cellProvider -> new CacheLoader<Path, Cell>() {

        @Override
        public Cell load(Path cellPath) throws IOException, InterruptedException {
            Path normalizedCellPath = cellPath.toRealPath().normalize();
            Preconditions.checkState(allRoots.contains(normalizedCellPath), "Cell %s outside of transitive closure of root cell (%s).", normalizedCellPath, allRoots);
            RawConfig configOverrides = Optional.ofNullable(pathToConfigOverrides.get(normalizedCellPath)).orElse(RawConfig.of(ImmutableMap.of()));
            Config config = Configs.createDefaultConfig(normalizedCellPath, configOverrides);
            DefaultCellPathResolver cellPathResolver = new DefaultCellPathResolver(normalizedCellPath, config);
            cellPathResolver.getCellPaths().forEach((name, path) -> {
                Path pathInRootResolver = rootCellCellPathResolver.getCellPaths().get(name);
                if (pathInRootResolver == null) {
                    throw new HumanReadableException("In the config of %s:  %s.%s must exist in the root cell's cell mappings.", cellPath.toString(), DefaultCellPathResolver.REPOSITORIES_SECTION, name);
                } else if (!pathInRootResolver.equals(path)) {
                    throw new HumanReadableException("In the config of %s:  %s.%s must point to the same directory as the root " + "cell's cell mapping: (root) %s != (current) %s", cellPath.toString(), DefaultCellPathResolver.REPOSITORIES_SECTION, name, pathInRootResolver, path);
                }
            });
            ProjectFilesystem cellFilesystem = new ProjectFilesystem(normalizedCellPath, config);
            BuckConfig buckConfig = new BuckConfig(config, cellFilesystem, rootConfig.getArchitecture(), rootConfig.getPlatform(), rootConfig.getEnvironment(), cellPathResolver);
            return new Cell(cellPathResolver.getKnownRoots(), cellFilesystem, watchman, buckConfig, knownBuildRuleTypesFactory, cellProvider);
        }
    }, cellProvider -> {
        try {
            return new Cell(rootCellCellPathResolver.getKnownRoots(), rootFilesystem, watchman, rootConfig, knownBuildRuleTypesFactory, cellProvider);
        } catch (InterruptedException e) {
            throw new RuntimeException("Interrupted while loading root cell", e);
        } catch (IOException e) {
            throw new HumanReadableException("Failed to load root cell", e);
        }
    });
}
Also used : Path(java.nio.file.Path) LoadingCache(com.google.common.cache.LoadingCache) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) Config(com.facebook.buck.config.Config) Throwables(com.google.common.base.Throwables) IOException(java.io.IOException) Configs(com.facebook.buck.config.Configs) HumanReadableException(com.facebook.buck.util.HumanReadableException) Watchman(com.facebook.buck.io.Watchman) Function(java.util.function.Function) RawConfig(com.facebook.buck.config.RawConfig) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) CacheLoader(com.google.common.cache.CacheLoader) ExecutionException(java.util.concurrent.ExecutionException) BuckConfig(com.facebook.buck.cli.BuckConfig) CellConfig(com.facebook.buck.config.CellConfig) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) CacheBuilder(com.google.common.cache.CacheBuilder) Path(java.nio.file.Path) Nullable(javax.annotation.Nullable) Config(com.facebook.buck.config.Config) RawConfig(com.facebook.buck.config.RawConfig) BuckConfig(com.facebook.buck.cli.BuckConfig) CellConfig(com.facebook.buck.config.CellConfig) IOException(java.io.IOException) RawConfig(com.facebook.buck.config.RawConfig) BuckConfig(com.facebook.buck.cli.BuckConfig) HumanReadableException(com.facebook.buck.util.HumanReadableException) CellConfig(com.facebook.buck.config.CellConfig) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem)

Example 4 with BuckConfig

use of com.facebook.buck.cli.BuckConfig in project buck by facebook.

the class AppleConfigTest method getXcodeSelectDetectedAppleDeveloperDirectorySupplier.

@Test
public void getXcodeSelectDetectedAppleDeveloperDirectorySupplier() {
    BuckConfig buckConfig = FakeBuckConfig.builder().build();
    AppleConfig config = new AppleConfig(buckConfig);
    ProcessExecutorParams xcodeSelectParams = ProcessExecutorParams.builder().setCommand(ImmutableList.of("xcode-select", "--print-path")).build();
    FakeProcess fakeXcodeSelect = new FakeProcess(0, "/path/to/another/place", "");
    FakeProcessExecutor processExecutor = new FakeProcessExecutor(ImmutableMap.of(xcodeSelectParams, fakeXcodeSelect));
    Supplier<Optional<Path>> supplier = config.getAppleDeveloperDirectorySupplier(processExecutor);
    assertNotNull(supplier);
    assertEquals(Optional.of(Paths.get("/path/to/another/place")), supplier.get());
}
Also used : ProcessExecutorParams(com.facebook.buck.util.ProcessExecutorParams) FakeBuckConfig(com.facebook.buck.cli.FakeBuckConfig) BuckConfig(com.facebook.buck.cli.BuckConfig) Optional(java.util.Optional) FakeProcessExecutor(com.facebook.buck.util.FakeProcessExecutor) FakeProcess(com.facebook.buck.util.FakeProcess) Test(org.junit.Test)

Example 5 with BuckConfig

use of com.facebook.buck.cli.BuckConfig in project buck by facebook.

the class AppleConfigTest method getUnspecifiedAppleDeveloperDirectorySupplier.

@Test
public void getUnspecifiedAppleDeveloperDirectorySupplier() {
    BuckConfig buckConfig = FakeBuckConfig.builder().build();
    AppleConfig config = new AppleConfig(buckConfig);
    assertNotNull(config.getAppleDeveloperDirectorySupplier(new FakeProcessExecutor()));
}
Also used : FakeBuckConfig(com.facebook.buck.cli.FakeBuckConfig) BuckConfig(com.facebook.buck.cli.BuckConfig) FakeProcessExecutor(com.facebook.buck.util.FakeProcessExecutor) Test(org.junit.Test)

Aggregations

BuckConfig (com.facebook.buck.cli.BuckConfig)98 FakeBuckConfig (com.facebook.buck.cli.FakeBuckConfig)89 Test (org.junit.Test)74 Path (java.nio.file.Path)46 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)37 TestCellBuilder (com.facebook.buck.rules.TestCellBuilder)29 PathSourcePath (com.facebook.buck.rules.PathSourcePath)27 Cell (com.facebook.buck.rules.Cell)22 FakeProjectFilesystem (com.facebook.buck.testutil.FakeProjectFilesystem)17 ImmutableMap (com.google.common.collect.ImmutableMap)17 Config (com.facebook.buck.config.Config)15 DefaultCellPathResolver (com.facebook.buck.rules.DefaultCellPathResolver)14 BuildTarget (com.facebook.buck.model.BuildTarget)12 FakeAndroidDirectoryResolver (com.facebook.buck.android.FakeAndroidDirectoryResolver)9 ParserConfig (com.facebook.buck.parser.ParserConfig)8 ConstructorArgMarshaller (com.facebook.buck.rules.ConstructorArgMarshaller)7 DefaultTypeCoercerFactory (com.facebook.buck.rules.coercer.DefaultTypeCoercerFactory)7 TestConsole (com.facebook.buck.testutil.TestConsole)7 Optional (java.util.Optional)7 BuildJobState (com.facebook.buck.distributed.thrift.BuildJobState)6