com.facebook.buck.cxx.CxxLibraryImplicitFlavors.java Source code

Java tutorial

Introduction

Here is the source code for com.facebook.buck.cxx.CxxLibraryImplicitFlavors.java

Source

/*
 * Copyright 2017-present Facebook, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may obtain
 * a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

package com.facebook.buck.cxx;

import com.facebook.buck.core.model.Flavor;
import com.facebook.buck.core.model.RuleType;
import com.facebook.buck.core.toolchain.ToolchainProvider;
import com.facebook.buck.core.util.log.Logger;
import com.facebook.buck.cxx.toolchain.CxxBuckConfig;
import com.facebook.buck.cxx.toolchain.CxxPlatformsProvider;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSortedSet;
import java.util.Arrays;
import java.util.Optional;

public class CxxLibraryImplicitFlavors {
    private static final Logger LOG = Logger.get(CxxLibraryImplicitFlavors.class);

    private final ToolchainProvider toolchainProvider;
    private final CxxBuckConfig cxxBuckConfig;

    public CxxLibraryImplicitFlavors(ToolchainProvider toolchainProvider, CxxBuckConfig cxxBuckConfig) {
        this.toolchainProvider = toolchainProvider;
        this.cxxBuckConfig = cxxBuckConfig;
    }

    public ImmutableSortedSet<Flavor> addImplicitFlavorsForRuleTypes(ImmutableSortedSet<Flavor> argDefaultFlavors,
            RuleType... types) {
        Optional<Flavor> typeFlavor = CxxLibraryDescription.LIBRARY_TYPE.getFlavor(argDefaultFlavors);
        CxxPlatformsProvider cxxPlatformsProvider = getCxxPlatformsProvider();
        Optional<Flavor> platformFlavor = cxxPlatformsProvider.getUnresolvedCxxPlatforms()
                .getFlavor(argDefaultFlavors);

        LOG.debug("Got arg default type %s platform %s", typeFlavor, platformFlavor);

        for (RuleType type : types) {
            ImmutableMap<String, Flavor> libraryDefaults = cxxBuckConfig.getDefaultFlavorsForRuleType(type);

            if (!typeFlavor.isPresent()) {
                typeFlavor = Optional.ofNullable(libraryDefaults.get(CxxBuckConfig.DEFAULT_FLAVOR_LIBRARY_TYPE));
            }

            if (!platformFlavor.isPresent()) {
                platformFlavor = Optional.ofNullable(libraryDefaults.get(CxxBuckConfig.DEFAULT_FLAVOR_PLATFORM));
            }
        }

        ImmutableSortedSet<Flavor> result = ImmutableSortedSet.of(
                // Default to static if not otherwise specified.
                typeFlavor.orElse(CxxDescriptionEnhancer.STATIC_FLAVOR),
                platformFlavor.orElse(cxxPlatformsProvider.getDefaultUnresolvedCxxPlatform().getFlavor()));

        LOG.debug("Got default flavors %s for rule types %s", result, Arrays.toString(types));
        return result;
    }

    private CxxPlatformsProvider getCxxPlatformsProvider() {
        return toolchainProvider.getByName(CxxPlatformsProvider.DEFAULT_NAME, CxxPlatformsProvider.class);
    }
}