/*
* Copyright 2015-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.go;
import com.facebook.buck.model.BuildTarget;
import com.facebook.buck.model.Flavor;
import com.facebook.buck.model.Flavored;
import com.facebook.buck.parser.NoSuchBuildTargetException;
import com.facebook.buck.rules.BuildRule;
import com.facebook.buck.rules.BuildRuleParams;
import com.facebook.buck.rules.BuildRuleResolver;
import com.facebook.buck.rules.CellPathResolver;
import com.facebook.buck.rules.CommonDescriptionArg;
import com.facebook.buck.rules.Description;
import com.facebook.buck.rules.HasDeclaredDeps;
import com.facebook.buck.rules.HasSrcs;
import com.facebook.buck.rules.HasTests;
import com.facebook.buck.rules.MetadataProvidingDescription;
import com.facebook.buck.rules.NoopBuildRule;
import com.facebook.buck.rules.SourcePath;
import com.facebook.buck.rules.TargetGraph;
import com.facebook.buck.util.immutables.BuckStyleImmutable;
import com.facebook.buck.versions.Version;
import com.google.common.base.Preconditions;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedSet;
import com.google.common.collect.Iterables;
import java.nio.file.Paths;
import java.util.Optional;
import org.immutables.value.Value;
public class GoLibraryDescription
implements Description<GoLibraryDescriptionArg>,
Flavored,
MetadataProvidingDescription<GoLibraryDescriptionArg> {
private final GoBuckConfig goBuckConfig;
public GoLibraryDescription(GoBuckConfig goBuckConfig) {
this.goBuckConfig = goBuckConfig;
}
@Override
public Class<GoLibraryDescriptionArg> getConstructorArgType() {
return GoLibraryDescriptionArg.class;
}
@Override
public boolean hasFlavors(ImmutableSet<Flavor> flavors) {
return goBuckConfig.getPlatformFlavorDomain().containsAnyOf(flavors);
}
@Override
public <U> Optional<U> createMetadata(
BuildTarget buildTarget,
final BuildRuleResolver resolver,
GoLibraryDescriptionArg args,
Optional<ImmutableMap<BuildTarget, Version>> selectedVersions,
Class<U> metadataClass)
throws NoSuchBuildTargetException {
Optional<GoPlatform> platform = goBuckConfig.getPlatformFlavorDomain().getValue(buildTarget);
if (metadataClass.isAssignableFrom(GoLinkable.class)) {
Preconditions.checkState(platform.isPresent());
SourcePath output = resolver.requireRule(buildTarget).getSourcePathToOutput();
return Optional.of(
metadataClass.cast(
GoLinkable.builder()
.setGoLinkInput(
ImmutableMap.of(
args.getPackageName()
.map(Paths::get)
.orElse(goBuckConfig.getDefaultPackageName(buildTarget)),
output))
.setExportedDeps(args.getExportedDeps())
.build()));
} else if (buildTarget.getFlavors().contains(GoDescriptors.TRANSITIVE_LINKABLES_FLAVOR)) {
Preconditions.checkState(platform.isPresent());
return Optional.of(
metadataClass.cast(
GoDescriptors.requireTransitiveGoLinkables(
buildTarget,
resolver,
platform.get(),
Iterables.concat(args.getDeps(), args.getExportedDeps()),
/* includeSelf */ true)));
} else {
return Optional.empty();
}
}
@Override
public BuildRule createBuildRule(
TargetGraph targetGraph,
BuildRuleParams params,
BuildRuleResolver resolver,
CellPathResolver cellRoots,
GoLibraryDescriptionArg args)
throws NoSuchBuildTargetException {
Optional<GoPlatform> platform =
goBuckConfig.getPlatformFlavorDomain().getValue(params.getBuildTarget());
if (platform.isPresent()) {
return GoDescriptors.createGoCompileRule(
params,
resolver,
goBuckConfig,
args.getPackageName()
.map(Paths::get)
.orElse(goBuckConfig.getDefaultPackageName(params.getBuildTarget())),
args.getSrcs(),
args.getCompilerFlags(),
args.getAssemblerFlags(),
platform.get(),
FluentIterable.from(params.getDeclaredDeps().get())
.transform(BuildRule::getBuildTarget)
.append(args.getExportedDeps()));
}
return new NoopBuildRule(params);
}
@BuckStyleImmutable
@Value.Immutable
interface AbstractGoLibraryDescriptionArg
extends CommonDescriptionArg, HasDeclaredDeps, HasSrcs, HasTests {
ImmutableList<String> getCompilerFlags();
ImmutableList<String> getAssemblerFlags();
Optional<String> getPackageName();
@Value.NaturalOrder
ImmutableSortedSet<BuildTarget> getExportedDeps();
}
}