Search in sources :

Example 36 with ImmutableMap

use of com.google.common.collect.ImmutableMap in project buck by facebook.

the class AbstractCellConfig method getOverridesByPath.

/**
   * Translates the 'cell name'->override map into a 'Path'->override map.
   * @param pathMapping a map containing paths to all of the cells we want to query.
   * @return 'Path'->override map
   */
public ImmutableMap<Path, RawConfig> getOverridesByPath(ImmutableMap<RelativeCellName, Path> pathMapping) throws MalformedOverridesException {
    ImmutableSet<RelativeCellName> relativeNamesOfCellsWithOverrides = FluentIterable.from(getValues().keySet()).filter(Predicates.not(ALL_CELLS_OVERRIDE::equals)).toSet();
    ImmutableSet.Builder<Path> pathsWithOverrides = ImmutableSet.builder();
    for (RelativeCellName cellWithOverride : relativeNamesOfCellsWithOverrides) {
        if (!pathMapping.containsKey(cellWithOverride)) {
            throw new MalformedOverridesException(String.format("Trying to override settings for unknown cell %s", cellWithOverride));
        }
        pathsWithOverrides.add(pathMapping.get(cellWithOverride));
    }
    ImmutableMultimap<Path, RelativeCellName> pathToRelativeName = Multimaps.index(pathMapping.keySet(), Functions.forMap(pathMapping));
    for (Path pathWithOverrides : pathsWithOverrides.build()) {
        ImmutableCollection<RelativeCellName> namesForPath = pathToRelativeName.get(pathWithOverrides);
        if (namesForPath.size() > 1) {
            throw new MalformedOverridesException(String.format("Configuration override is ambiguous: cell rooted at %s is reachable " + "as [%s]. Please override the config by placing a .buckconfig.local file in the " + "cell's root folder.", pathWithOverrides, Joiner.on(',').join(namesForPath)));
        }
    }
    Map<Path, RawConfig> overridesByPath = new HashMap<>();
    for (Map.Entry<RelativeCellName, Path> entry : pathMapping.entrySet()) {
        RelativeCellName cellRelativeName = entry.getKey();
        Path cellPath = entry.getValue();
        RawConfig configFromOtherRelativeName = overridesByPath.get(cellPath);
        RawConfig config = getForCell(cellRelativeName);
        if (configFromOtherRelativeName != null) {
            Preconditions.checkState(configFromOtherRelativeName.equals(config), "Attempting to create cell %s at %s with conflicting overrides [%s] vs [%s].", cellRelativeName, cellPath, configFromOtherRelativeName, config);
        } else {
            overridesByPath.put(cellPath, config);
        }
    }
    return ImmutableMap.copyOf(overridesByPath);
}
Also used : Path(java.nio.file.Path) HashMap(java.util.HashMap) RelativeCellName(com.facebook.buck.rules.RelativeCellName) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) Map(java.util.Map) MoreCollectors.toImmutableMap(com.facebook.buck.util.MoreCollectors.toImmutableMap)

Example 37 with ImmutableMap

use of com.google.common.collect.ImmutableMap in project buck by facebook.

the class Configs method createDefaultConfig.

/**
   * Generates a Buck config by merging configs from specified locations on disk.
   *
   * In order:
   *
   * <ol>
   *   <li>{@code /etc/buckconfig}</li>
   *   <li>Files (in lexicographical order) in {@code /etc/buckconfig.d}</li>
   *   <li>{@code <HOME>/.buckconfig}</li>
   *   <li>Files (in lexicographical order) in {@code <HOME>/buckconfig.d}</li>
   *   <li>{@code <PROJECT ROOT>/.buckconfig}</li>
   *   <li>{@code <PROJECT ROOT>/.buckconfig.local}</li>
   *   <li>Any overrides (usually from the command line)</li>
   * </ol>
   *
   * @param root Project root.
   * @param configOverrides Config overrides to merge in after the other sources.
   * @return the resulting {@code Config}.
   * @throws IOException on any exceptions during the underlying filesystem operations.
   */
public static Config createDefaultConfig(Path root, RawConfig configOverrides) throws IOException {
    LOG.debug("Loading configuration for %s", root);
    ImmutableList.Builder<Path> configFileBuilder = ImmutableList.builder();
    configFileBuilder.addAll(listFiles(GLOBAL_BUCK_CONFIG_DIRECTORY_PATH));
    if (Files.isRegularFile(GLOBAL_BUCK_CONFIG_FILE_PATH)) {
        configFileBuilder.add(GLOBAL_BUCK_CONFIG_FILE_PATH);
    }
    Path homeDirectory = Paths.get(System.getProperty("user.home"));
    Path userConfigDir = homeDirectory.resolve(DEFAULT_BUCK_CONFIG_DIRECTORY_NAME);
    configFileBuilder.addAll(listFiles(userConfigDir));
    Path userConfigFile = homeDirectory.resolve(DEFAULT_BUCK_CONFIG_FILE_NAME);
    if (Files.isRegularFile(userConfigFile)) {
        configFileBuilder.add(userConfigFile);
    }
    Path configFile = root.resolve(DEFAULT_BUCK_CONFIG_FILE_NAME);
    if (Files.isRegularFile(configFile)) {
        configFileBuilder.add(configFile);
    }
    Path overrideConfigFile = root.resolve(DEFAULT_BUCK_CONFIG_OVERRIDE_FILE_NAME);
    if (Files.isRegularFile(overrideConfigFile)) {
        configFileBuilder.add(overrideConfigFile);
    }
    ImmutableList<Path> configFiles = configFileBuilder.build();
    RawConfig.Builder builder = RawConfig.builder();
    for (Path file : configFiles) {
        try (Reader reader = Files.newBufferedReader(file, StandardCharsets.UTF_8)) {
            ImmutableMap<String, ImmutableMap<String, String>> parsedConfiguration = Inis.read(reader);
            LOG.debug("Loaded a configuration file %s: %s", file, parsedConfiguration);
            builder.putAll(parsedConfiguration);
        }
    }
    LOG.debug("Adding configuration overrides %s", configOverrides);
    builder.putAll(configOverrides);
    return new Config(builder.build());
}
Also used : Path(java.nio.file.Path) ImmutableList(com.google.common.collect.ImmutableList) Reader(java.io.Reader) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 38 with ImmutableMap

use of com.google.common.collect.ImmutableMap in project buck by facebook.

the class HaskellCompileRule method getBuildSteps.

@Override
public ImmutableList<Step> getBuildSteps(BuildContext buildContext, BuildableContext buildableContext) {
    buildableContext.recordArtifact(getObjectDir());
    buildableContext.recordArtifact(getInterfaceDir());
    buildableContext.recordArtifact(getStubDir());
    return ImmutableList.of(new MakeCleanDirectoryStep(getProjectFilesystem(), getObjectDir()), new MakeCleanDirectoryStep(getProjectFilesystem(), getInterfaceDir()), new MakeCleanDirectoryStep(getProjectFilesystem(), getStubDir()), new ShellStep(getProjectFilesystem().getRootPath()) {

        @Override
        public ImmutableMap<String, String> getEnvironmentVariables(ExecutionContext context) {
            return ImmutableMap.<String, String>builder().putAll(super.getEnvironmentVariables(context)).putAll(compiler.getEnvironment(buildContext.getSourcePathResolver())).build();
        }

        @Override
        protected ImmutableList<String> getShellCommandInternal(ExecutionContext context) {
            SourcePathResolver resolver = buildContext.getSourcePathResolver();
            return ImmutableList.<String>builder().addAll(compiler.getCommandPrefix(resolver)).addAll(flags).add("-no-link").addAll(picType == CxxSourceRuleFactory.PicType.PIC ? ImmutableList.of("-dynamic", "-fPIC", "-hisuf", "dyn_hi") : ImmutableList.of()).addAll(MoreIterables.zipAndConcat(Iterables.cycle("-main-is"), OptionalCompat.asSet(main))).addAll(getPackageNameArgs()).addAll(getPreprocessorFlags(buildContext.getSourcePathResolver())).add("-odir", getProjectFilesystem().resolve(getObjectDir()).toString()).add("-hidir", getProjectFilesystem().resolve(getInterfaceDir()).toString()).add("-stubdir", getProjectFilesystem().resolve(getStubDir()).toString()).add("-i" + includes.stream().map(resolver::getAbsolutePath).map(Object::toString).collect(Collectors.joining(":"))).addAll(getPackageArgs(buildContext.getSourcePathResolver())).addAll(sources.getSourcePaths().stream().map(resolver::getAbsolutePath).map(Object::toString).iterator()).build();
        }

        @Override
        public String getShortName() {
            return "haskell-compile";
        }
    });
}
Also used : ExecutionContext(com.facebook.buck.step.ExecutionContext) ImmutableList(com.google.common.collect.ImmutableList) ShellStep(com.facebook.buck.shell.ShellStep) MakeCleanDirectoryStep(com.facebook.buck.step.fs.MakeCleanDirectoryStep) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 39 with ImmutableMap

use of com.google.common.collect.ImmutableMap in project buck by facebook.

the class HaskellLibraryDescription method createBuildRule.

@Override
public <A extends Arg> BuildRule createBuildRule(TargetGraph targetGraph, final BuildRuleParams params, final BuildRuleResolver resolver, final A args) throws NoSuchBuildTargetException {
    final BuildTarget buildTarget = params.getBuildTarget();
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(resolver);
    final SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
    // See if we're building a particular "type" and "platform" of this library, and if so, extract
    // them from the flavors attached to the build target.
    Optional<Map.Entry<Flavor, Type>> type = LIBRARY_TYPE.getFlavorAndValue(buildTarget);
    Optional<CxxPlatform> cxxPlatform = cxxPlatforms.getValue(buildTarget);
    if (type.isPresent()) {
        Preconditions.checkState(cxxPlatform.isPresent());
        // Get the base build, without any flavors referring to the library type or platform.
        BuildTarget baseTarget = params.getBuildTarget().withoutFlavors(Sets.union(Type.FLAVOR_VALUES, cxxPlatforms.getFlavors()));
        switch(type.get().getValue()) {
            case PACKAGE_SHARED:
            case PACKAGE_STATIC:
            case PACKAGE_STATIC_PIC:
                Linker.LinkableDepType depType;
                if (type.get().getValue().equals(Type.PACKAGE_SHARED)) {
                    depType = Linker.LinkableDepType.SHARED;
                } else if (type.get().getValue().equals(Type.PACKAGE_STATIC)) {
                    depType = Linker.LinkableDepType.STATIC;
                } else {
                    depType = Linker.LinkableDepType.STATIC_PIC;
                }
                return requirePackage(baseTarget, params, resolver, pathResolver, ruleFinder, cxxPlatform.get(), args, depType);
            case SHARED:
                return requireSharedLibrary(baseTarget, params, resolver, pathResolver, ruleFinder, cxxPlatform.get(), args);
            case STATIC_PIC:
            case STATIC:
                return requireStaticLibrary(baseTarget, params, resolver, pathResolver, ruleFinder, cxxPlatform.get(), args, type.get().getValue() == Type.STATIC ? Linker.LinkableDepType.STATIC : Linker.LinkableDepType.STATIC_PIC);
        }
        throw new IllegalStateException(String.format("%s: unexpected type `%s`", params.getBuildTarget(), type.get().getValue()));
    }
    return new HaskellLibrary(params) {

        @Override
        public HaskellCompileInput getCompileInput(CxxPlatform cxxPlatform, Linker.LinkableDepType depType) throws NoSuchBuildTargetException {
            HaskellPackageRule rule = requirePackage(getBaseBuildTarget(getBuildTarget()), params, resolver, pathResolver, ruleFinder, cxxPlatform, args, depType);
            return HaskellCompileInput.builder().addPackages(rule.getPackage()).build();
        }

        @Override
        public Iterable<? extends NativeLinkable> getNativeLinkableDeps() {
            return ImmutableList.of();
        }

        @Override
        public Iterable<? extends NativeLinkable> getNativeLinkableExportedDeps() {
            return FluentIterable.from(getDeps()).filter(NativeLinkable.class);
        }

        @Override
        public NativeLinkableInput getNativeLinkableInput(CxxPlatform cxxPlatform, Linker.LinkableDepType type) throws NoSuchBuildTargetException {
            Iterable<com.facebook.buck.rules.args.Arg> linkArgs;
            switch(type) {
                case STATIC:
                case STATIC_PIC:
                    Archive archive = requireStaticLibrary(getBaseBuildTarget(getBuildTarget()), params, resolver, pathResolver, ruleFinder, cxxPlatform, args, type);
                    linkArgs = args.linkWhole.orElse(false) ? cxxPlatform.getLd().resolve(resolver).linkWhole(archive.toArg()) : ImmutableList.of(archive.toArg());
                    break;
                case SHARED:
                    BuildRule rule = requireSharedLibrary(getBaseBuildTarget(getBuildTarget()), params, resolver, pathResolver, ruleFinder, cxxPlatform, args);
                    linkArgs = ImmutableList.of(SourcePathArg.of(rule.getSourcePathToOutput()));
                    break;
                default:
                    throw new IllegalStateException();
            }
            return NativeLinkableInput.builder().addAllArgs(linkArgs).build();
        }

        @Override
        public Linkage getPreferredLinkage(CxxPlatform cxxPlatform) {
            return args.preferredLinkage.orElse(Linkage.ANY);
        }

        @Override
        public ImmutableMap<String, SourcePath> getSharedLibraries(CxxPlatform cxxPlatform) throws NoSuchBuildTargetException {
            ImmutableMap.Builder<String, SourcePath> libs = ImmutableMap.builder();
            String sharedLibrarySoname = CxxDescriptionEnhancer.getSharedLibrarySoname(Optional.empty(), getBuildTarget(), cxxPlatform);
            BuildRule sharedLibraryBuildRule = requireSharedLibrary(getBaseBuildTarget(getBuildTarget()), params, resolver, pathResolver, ruleFinder, cxxPlatform, args);
            libs.put(sharedLibrarySoname, sharedLibraryBuildRule.getSourcePathToOutput());
            return libs.build();
        }
    };
}
Also used : Archive(com.facebook.buck.cxx.Archive) CxxPlatform(com.facebook.buck.cxx.CxxPlatform) Linker(com.facebook.buck.cxx.Linker) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) ImmutableMap(com.google.common.collect.ImmutableMap) SourcePath(com.facebook.buck.rules.SourcePath) BuildTarget(com.facebook.buck.model.BuildTarget) SourcePathArg(com.facebook.buck.rules.args.SourcePathArg) BuildRule(com.facebook.buck.rules.BuildRule)

Example 40 with ImmutableMap

use of com.google.common.collect.ImmutableMap in project buck by facebook.

the class GoDescriptors method makeSymlinkTree.

private static SymlinkTree makeSymlinkTree(BuildRuleParams params, SourcePathResolver pathResolver, SourcePathRuleFinder ruleFinder, ImmutableSet<GoLinkable> linkables) {
    ImmutableMap.Builder<Path, SourcePath> treeMapBuilder = ImmutableMap.builder();
    for (GoLinkable linkable : linkables) {
        for (Map.Entry<Path, SourcePath> linkInput : linkable.getGoLinkInput().entrySet()) {
            treeMapBuilder.put(getPathInSymlinkTree(pathResolver, linkInput.getKey(), linkInput.getValue()), linkInput.getValue());
        }
    }
    ImmutableMap<Path, SourcePath> treeMap;
    try {
        treeMap = treeMapBuilder.build();
    } catch (IllegalArgumentException ex) {
        throw new HumanReadableException(ex, "Multiple go targets have the same package name when compiling %s", params.getBuildTarget().getFullyQualifiedName());
    }
    Path root = BuildTargets.getScratchPath(params.getProjectFilesystem(), params.getBuildTarget(), "__%s__tree");
    return new SymlinkTree(params.getBuildTarget(), params.getProjectFilesystem(), root, treeMap, ruleFinder);
}
Also used : SourcePath(com.facebook.buck.rules.SourcePath) Path(java.nio.file.Path) SourcePath(com.facebook.buck.rules.SourcePath) SymlinkTree(com.facebook.buck.rules.SymlinkTree) HumanReadableException(com.facebook.buck.util.HumanReadableException) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) ImmutableMap(com.google.common.collect.ImmutableMap)

Aggregations

ImmutableMap (com.google.common.collect.ImmutableMap)702 Map (java.util.Map)346 Test (org.junit.Test)195 HashMap (java.util.HashMap)144 ImmutableList (com.google.common.collect.ImmutableList)126 Path (java.nio.file.Path)104 List (java.util.List)100 ImmutableSet (com.google.common.collect.ImmutableSet)89 IOException (java.io.IOException)84 ArrayList (java.util.ArrayList)74 Set (java.util.Set)69 Optional (java.util.Optional)61 BuildTarget (com.facebook.buck.model.BuildTarget)57 File (java.io.File)57 Collectors (java.util.stream.Collectors)45 HashSet (java.util.HashSet)44 SourcePath (com.facebook.buck.rules.SourcePath)41 VisibleForTesting (com.google.common.annotations.VisibleForTesting)39 Nullable (javax.annotation.Nullable)39 LinkedHashMap (java.util.LinkedHashMap)36