use of org.apache.beam.vendor.calcite.v1_28_0.com.google.common.collect.ImmutableMap in project buck by facebook.
the class ArtifactCaches method createHttpArtifactCache.
private static ArtifactCache createHttpArtifactCache(HttpCacheEntry cacheDescription, final String hostToReportToRemote, final BuckEventBus buckEventBus, ProjectFilesystem projectFilesystem, ListeningExecutorService httpWriteExecutorService, ArtifactCacheBuckConfig config, NetworkCacheFactory factory, boolean distributedBuildModeEnabled) {
// Setup the default client to use.
OkHttpClient.Builder storeClientBuilder = new OkHttpClient.Builder();
storeClientBuilder.networkInterceptors().add(chain -> chain.proceed(chain.request().newBuilder().addHeader("X-BuckCache-User", stripNonAscii(System.getProperty("user.name", "<unknown>"))).addHeader("X-BuckCache-Host", stripNonAscii(hostToReportToRemote)).build()));
int timeoutSeconds = cacheDescription.getTimeoutSeconds();
setTimeouts(storeClientBuilder, timeoutSeconds);
storeClientBuilder.connectionPool(new ConnectionPool(/* maxIdleConnections */
(int) config.getThreadPoolSize(), /* keepAliveDurationMs */
config.getThreadPoolKeepAliveDurationMillis(), TimeUnit.MILLISECONDS));
// The artifact cache effectively only connects to a single host at a time. We should allow as
// many concurrent connections to that host as we allow threads.
Dispatcher dispatcher = new Dispatcher();
dispatcher.setMaxRequestsPerHost((int) config.getThreadPoolSize());
storeClientBuilder.dispatcher(dispatcher);
final ImmutableMap<String, String> readHeaders = cacheDescription.getReadHeaders();
final ImmutableMap<String, String> writeHeaders = cacheDescription.getWriteHeaders();
// If write headers are specified, add them to every default client request.
if (!writeHeaders.isEmpty()) {
storeClientBuilder.networkInterceptors().add(chain -> chain.proceed(addHeadersToBuilder(chain.request().newBuilder(), writeHeaders).build()));
}
OkHttpClient storeClient = storeClientBuilder.build();
// For fetches, use a client with a read timeout.
OkHttpClient.Builder fetchClientBuilder = storeClient.newBuilder();
setTimeouts(fetchClientBuilder, timeoutSeconds);
// If read headers are specified, add them to every read client request.
if (!readHeaders.isEmpty()) {
fetchClientBuilder.networkInterceptors().add(chain -> chain.proceed(addHeadersToBuilder(chain.request().newBuilder(), readHeaders).build()));
}
fetchClientBuilder.networkInterceptors().add((chain -> {
Response originalResponse = chain.proceed(chain.request());
return originalResponse.newBuilder().body(new ProgressResponseBody(originalResponse.body(), buckEventBus)).build();
}));
OkHttpClient fetchClient = fetchClientBuilder.build();
HttpService fetchService;
HttpService storeService;
switch(config.getLoadBalancingType()) {
case CLIENT_SLB:
HttpLoadBalancer clientSideSlb = config.getSlbConfig().createClientSideSlb(new DefaultClock(), buckEventBus, new CommandThreadFactory("ArtifactCaches.HttpLoadBalancer", SLB_THREAD_PRIORITY));
fetchService = new RetryingHttpService(buckEventBus, new LoadBalancedService(clientSideSlb, fetchClient, buckEventBus), config.getMaxFetchRetries());
storeService = new LoadBalancedService(clientSideSlb, storeClient, buckEventBus);
break;
case SINGLE_SERVER:
URI url = cacheDescription.getUrl();
fetchService = new SingleUriService(url, fetchClient);
storeService = new SingleUriService(url, storeClient);
break;
default:
throw new IllegalArgumentException("Unknown HttpLoadBalancer type: " + config.getLoadBalancingType());
}
String cacheName = cacheDescription.getName().map(input -> "http-" + input).orElse("http");
boolean doStore = cacheDescription.getCacheReadMode().isDoStore();
return factory.newInstance(NetworkCacheArgs.builder().setThriftEndpointPath(config.getHybridThriftEndpoint()).setCacheName(cacheName).setRepository(config.getRepository()).setScheduleType(config.getScheduleType()).setFetchClient(fetchService).setStoreClient(storeService).setDoStore(doStore).setProjectFilesystem(projectFilesystem).setBuckEventBus(buckEventBus).setHttpWriteExecutorService(httpWriteExecutorService).setErrorTextTemplate(cacheDescription.getErrorMessageFormat()).setDistributedBuildModeEnabled(distributedBuildModeEnabled).build());
}
use of org.apache.beam.vendor.calcite.v1_28_0.com.google.common.collect.ImmutableMap in project buck by facebook.
the class CxxLibrary method getSharedLibraries.
@Override
public ImmutableMap<String, SourcePath> getSharedLibraries(CxxPlatform cxxPlatform) throws NoSuchBuildTargetException {
if (headerOnly.apply(cxxPlatform)) {
return ImmutableMap.of();
}
if (!isPlatformSupported(cxxPlatform)) {
return ImmutableMap.of();
}
ImmutableMap.Builder<String, SourcePath> libs = ImmutableMap.builder();
String sharedLibrarySoname = CxxDescriptionEnhancer.getSharedLibrarySoname(soname, getBuildTarget(), cxxPlatform);
BuildRule sharedLibraryBuildRule = requireBuildRule(cxxPlatform.getFlavor(), CxxDescriptionEnhancer.SHARED_FLAVOR);
libs.put(sharedLibrarySoname, sharedLibraryBuildRule.getSourcePathToOutput());
return libs.build();
}
use of org.apache.beam.vendor.calcite.v1_28_0.com.google.common.collect.ImmutableMap in project buck by facebook.
the class CxxLibraryDescription method createMetadata.
@Override
public <A extends Arg, U> Optional<U> createMetadata(BuildTarget buildTarget, BuildRuleResolver resolver, A args, Optional<ImmutableMap<BuildTarget, Version>> selectedVersions, final Class<U> metadataClass) throws NoSuchBuildTargetException {
Map.Entry<Flavor, MetadataType> type = METADATA_TYPE.getFlavorAndValue(buildTarget).orElseThrow(IllegalArgumentException::new);
BuildTarget baseTarget = buildTarget.withoutFlavors(type.getKey());
switch(type.getValue()) {
case CXX_HEADERS:
{
Optional<CxxHeaders> symlinkTree = Optional.empty();
if (!args.exportedHeaders.isEmpty()) {
CxxPreprocessables.HeaderMode mode = HEADER_MODE.getRequiredValue(buildTarget);
baseTarget = baseTarget.withoutFlavors(mode.getFlavor());
symlinkTree = Optional.of(CxxSymlinkTreeHeaders.from((HeaderSymlinkTree) resolver.requireRule(baseTarget.withAppendedFlavors(Type.EXPORTED_HEADERS.getFlavor(), mode.getFlavor())), CxxPreprocessables.IncludeType.LOCAL));
}
return symlinkTree.map(metadataClass::cast);
}
case CXX_PREPROCESSOR_INPUT:
{
Map.Entry<Flavor, CxxPlatform> platform = cxxPlatforms.getFlavorAndValue(buildTarget).orElseThrow(IllegalArgumentException::new);
Map.Entry<Flavor, HeaderVisibility> visibility = HEADER_VISIBILITY.getFlavorAndValue(buildTarget).orElseThrow(IllegalArgumentException::new);
baseTarget = baseTarget.withoutFlavors(platform.getKey(), visibility.getKey());
CxxPreprocessorInput.Builder cxxPreprocessorInputBuilder = CxxPreprocessorInput.builder();
// TODO(andrewjcg): We currently always add exported flags and frameworks to the
// preprocessor input to mimic existing behavior, but this should likely be fixed.
cxxPreprocessorInputBuilder.putAllPreprocessorFlags(CxxFlags.getLanguageFlags(args.exportedPreprocessorFlags, args.exportedPlatformPreprocessorFlags, args.exportedLangPreprocessorFlags, platform.getValue()));
cxxPreprocessorInputBuilder.addAllFrameworks(args.frameworks);
if (visibility.getValue() == HeaderVisibility.PRIVATE && !args.headers.isEmpty()) {
HeaderSymlinkTree symlinkTree = (HeaderSymlinkTree) resolver.requireRule(baseTarget.withAppendedFlavors(platform.getKey(), Type.HEADERS.getFlavor()));
cxxPreprocessorInputBuilder.addIncludes(CxxSymlinkTreeHeaders.from(symlinkTree, CxxPreprocessables.IncludeType.LOCAL));
}
if (visibility.getValue() == HeaderVisibility.PUBLIC) {
// Add platform-agnostic headers.
boolean shouldCreatePublicHeaderSymlinks = args.xcodePublicHeadersSymlinks.orElse(true);
CxxPreprocessables.HeaderMode mode = CxxDescriptionEnhancer.getHeaderModeForPlatform(resolver, platform.getValue(), shouldCreatePublicHeaderSymlinks);
Optional<CxxHeaders> exportedHeaders = resolver.requireMetadata(baseTarget.withAppendedFlavors(MetadataType.CXX_HEADERS.getFlavor(), mode.getFlavor()), CxxHeaders.class);
exportedHeaders.ifPresent(cxxPreprocessorInputBuilder::addIncludes);
// Add platform-specific headers.
if (!args.exportedPlatformHeaders.getMatchingValues(platform.getKey().toString()).isEmpty()) {
HeaderSymlinkTree symlinkTree = (HeaderSymlinkTree) resolver.requireRule(baseTarget.withAppendedFlavors(platform.getKey(), Type.EXPORTED_HEADERS.getFlavor()));
cxxPreprocessorInputBuilder.addIncludes(CxxSymlinkTreeHeaders.from(symlinkTree, CxxPreprocessables.IncludeType.LOCAL));
}
}
CxxPreprocessorInput cxxPreprocessorInput = cxxPreprocessorInputBuilder.build();
return Optional.of(cxxPreprocessorInput).map(metadataClass::cast);
}
case COMPILATION_DATABASE_DEPS:
{
return CxxDescriptionEnhancer.createCompilationDatabaseDependencies(buildTarget, cxxPlatforms, resolver, args).map(metadataClass::cast);
}
}
throw new IllegalStateException(String.format("unhandled metadata type: %s", type.getValue()));
}
use of org.apache.beam.vendor.calcite.v1_28_0.com.google.common.collect.ImmutableMap in project buck by facebook.
the class NativeLinkables method getNativeLinkables.
/**
* Extract from the dependency graph all the libraries which must be considered for linking.
*
* Traversal proceeds depending on whether each dependency is to be statically or dynamically
* linked.
*
* @param linkStyle how dependencies should be linked, if their preferred_linkage is
* {@code NativeLinkable.Linkage.ANY}.
*/
public static ImmutableMap<BuildTarget, NativeLinkable> getNativeLinkables(final CxxPlatform cxxPlatform, Iterable<? extends NativeLinkable> inputs, final Linker.LinkableDepType linkStyle, final Predicate<? super NativeLinkable> traverse) {
final Map<BuildTarget, NativeLinkable> nativeLinkables = Maps.newHashMap();
for (NativeLinkable nativeLinkable : inputs) {
nativeLinkables.put(nativeLinkable.getBuildTarget(), nativeLinkable);
}
final MutableDirectedGraph<BuildTarget> graph = new MutableDirectedGraph<>();
AbstractBreadthFirstTraversal<BuildTarget> visitor = new AbstractBreadthFirstTraversal<BuildTarget>(nativeLinkables.keySet()) {
@Override
public ImmutableSet<BuildTarget> visit(BuildTarget target) {
NativeLinkable nativeLinkable = Preconditions.checkNotNull(nativeLinkables.get(target));
graph.addNode(target);
// We always traverse a rule's exported native linkables.
Iterable<? extends NativeLinkable> nativeLinkableDeps = nativeLinkable.getNativeLinkableExportedDepsForPlatform(cxxPlatform);
boolean shouldTraverse = true;
switch(nativeLinkable.getPreferredLinkage(cxxPlatform)) {
case ANY:
shouldTraverse = linkStyle != Linker.LinkableDepType.SHARED;
break;
case SHARED:
shouldTraverse = false;
break;
case STATIC:
shouldTraverse = true;
break;
}
// If we're linking this dependency statically, we also need to traverse its deps.
if (shouldTraverse) {
nativeLinkableDeps = Iterables.concat(nativeLinkableDeps, nativeLinkable.getNativeLinkableDepsForPlatform(cxxPlatform));
}
// Process all the traversable deps.
ImmutableSet.Builder<BuildTarget> deps = ImmutableSet.builder();
for (NativeLinkable dep : nativeLinkableDeps) {
if (traverse.apply(dep)) {
BuildTarget depTarget = dep.getBuildTarget();
graph.addEdge(target, depTarget);
deps.add(depTarget);
nativeLinkables.put(depTarget, dep);
}
}
return deps.build();
}
};
visitor.start();
// Topologically sort the rules.
Iterable<BuildTarget> ordered = TopologicalSort.sort(graph).reverse();
// Return a map of of the results.
ImmutableMap.Builder<BuildTarget, NativeLinkable> result = ImmutableMap.builder();
for (BuildTarget target : ordered) {
result.put(target, nativeLinkables.get(target));
}
return result.build();
}
use of org.apache.beam.vendor.calcite.v1_28_0.com.google.common.collect.ImmutableMap in project buck by facebook.
the class Omnibus method createRoot.
// Create a build rule which links the given root node against the merged omnibus library
// described by the given spec file.
protected static OmnibusRoot createRoot(BuildRuleParams params, BuildRuleResolver ruleResolver, SourcePathRuleFinder ruleFinder, CxxBuckConfig cxxBuckConfig, CxxPlatform cxxPlatform, ImmutableList<? extends Arg> extraLdflags, OmnibusSpec spec, SourcePath omnibus, NativeLinkTarget root, BuildTarget rootTargetBase, Optional<Path> output) throws NoSuchBuildTargetException {
ImmutableList.Builder<Arg> argsBuilder = ImmutableList.builder();
// Add any extra flags to the link.
argsBuilder.addAll(extraLdflags);
// Since the dummy omnibus library doesn't actually contain any symbols, make sure the linker
// won't drop its runtime reference to it.
argsBuilder.addAll(StringArg.from(cxxPlatform.getLd().resolve(ruleResolver).getNoAsNeededSharedLibsFlags()));
// Since we're linking against a dummy libomnibus, ignore undefined symbols.
argsBuilder.addAll(StringArg.from(cxxPlatform.getLd().resolve(ruleResolver).getIgnoreUndefinedSymbolsFlags()));
// Add the args for the root link target first.
NativeLinkableInput input = root.getNativeLinkTargetInput(cxxPlatform);
argsBuilder.addAll(input.getArgs());
// Grab a topologically sorted mapping of all the root's deps.
ImmutableMap<BuildTarget, NativeLinkable> deps = NativeLinkables.getNativeLinkables(cxxPlatform, root.getNativeLinkTargetDeps(cxxPlatform), Linker.LinkableDepType.SHARED);
// Now process the dependencies in topological order, to assemble the link line.
boolean alreadyAddedOmnibusToArgs = false;
for (Map.Entry<BuildTarget, NativeLinkable> entry : deps.entrySet()) {
BuildTarget target = entry.getKey();
NativeLinkable nativeLinkable = entry.getValue();
Linker.LinkableDepType linkStyle = NativeLinkables.getLinkStyle(nativeLinkable.getPreferredLinkage(cxxPlatform), Linker.LinkableDepType.SHARED);
// If this dep needs to be linked statically, then we always link it directly.
if (linkStyle != Linker.LinkableDepType.SHARED) {
Preconditions.checkState(linkStyle == Linker.LinkableDepType.STATIC_PIC);
argsBuilder.addAll(nativeLinkable.getNativeLinkableInput(cxxPlatform, linkStyle).getArgs());
continue;
}
// If this dep is another root node, substitute in the custom linked library we built for it.
if (spec.getRoots().containsKey(target)) {
argsBuilder.add(SourcePathArg.of(new DefaultBuildTargetSourcePath(getRootTarget(params.getBuildTarget(), target))));
continue;
}
// libomnibus instead.
if (spec.getBody().containsKey(target)) {
// && linkStyle == Linker.LinkableDepType.SHARED) {
if (!alreadyAddedOmnibusToArgs) {
argsBuilder.add(SourcePathArg.of(omnibus));
alreadyAddedOmnibusToArgs = true;
}
continue;
}
// Otherwise, this is either an explicitly statically linked or excluded node, so link it
// normally.
Preconditions.checkState(spec.getExcluded().containsKey(target));
argsBuilder.addAll(nativeLinkable.getNativeLinkableInput(cxxPlatform, linkStyle).getArgs());
}
// Create the root library rule using the arguments assembled above.
BuildTarget rootTarget = getRootTarget(params.getBuildTarget(), rootTargetBase);
NativeLinkTargetMode rootTargetMode = root.getNativeLinkTargetMode(cxxPlatform);
CxxLink rootLinkRule;
switch(rootTargetMode.getType()) {
// Link the root as a shared library.
case SHARED:
{
Optional<String> rootSoname = rootTargetMode.getLibraryName();
rootLinkRule = CxxLinkableEnhancer.createCxxLinkableSharedBuildRule(cxxBuckConfig, cxxPlatform, params, ruleResolver, ruleFinder, rootTarget, output.orElse(BuildTargets.getGenPath(params.getProjectFilesystem(), rootTarget, "%s").resolve(rootSoname.orElse(String.format("%s.%s", rootTarget.getShortName(), cxxPlatform.getSharedLibraryExtension())))), rootSoname, argsBuilder.build());
break;
}
// Link the root as an executable.
case EXECUTABLE:
{
rootLinkRule = CxxLinkableEnhancer.createCxxLinkableBuildRule(cxxBuckConfig, cxxPlatform, params, ruleResolver, ruleFinder, rootTarget, output.orElse(BuildTargets.getGenPath(params.getProjectFilesystem(), rootTarget, "%s").resolve(rootTarget.getShortName())), argsBuilder.build(), Linker.LinkableDepType.SHARED, Optional.empty());
break;
}
// $CASES-OMITTED$
default:
throw new IllegalStateException(String.format("%s: unexpected omnibus root type: %s %s", params.getBuildTarget(), root.getBuildTarget(), rootTargetMode.getType()));
}
CxxLink rootRule = ruleResolver.addToIndex(rootLinkRule);
return OmnibusRoot.of(rootRule.getSourcePathToOutput());
}
Aggregations