Search in sources :

Example 26 with CxxPlatform

use of com.facebook.buck.cxx.CxxPlatform in project buck by facebook.

the class NdkCxxPlatforms method build.

@VisibleForTesting
static NdkCxxPlatform build(CxxBuckConfig config, ProjectFilesystem filesystem, Flavor flavor, Platform platform, Path ndkRoot, NdkCxxPlatformTargetConfiguration targetConfiguration, CxxRuntime cxxRuntime, ExecutableFinder executableFinder, boolean strictToolchainPaths) {
    // Create a version string to use when generating rule keys via the NDK tools we'll generate
    // below.  This will be used in lieu of hashing the contents of the tools, so that builds from
    // different host platforms (which produce identical output) will share the cache with one
    // another.
    NdkCxxPlatformCompiler.Type compilerType = targetConfiguration.getCompiler().getType();
    String version = Joiner.on('-').join(ImmutableList.of(readVersion(ndkRoot), targetConfiguration.getToolchain(), targetConfiguration.getTargetAppPlatform(), compilerType, targetConfiguration.getCompiler().getVersion(), targetConfiguration.getCompiler().getGccVersion(), cxxRuntime));
    Host host = Preconditions.checkNotNull(BUILD_PLATFORMS.get(platform));
    NdkCxxToolchainPaths toolchainPaths = new NdkCxxToolchainPaths(filesystem, ndkRoot, targetConfiguration, host.toString(), cxxRuntime, strictToolchainPaths);
    // Sanitized paths will have magic placeholders for parts of the paths that
    // are machine/host-specific. See comments on ANDROID_NDK_ROOT and
    // BUILD_HOST_SUBST above.
    NdkCxxToolchainPaths sanitizedPaths = toolchainPaths.getSanitizedPaths();
    // Build up the map of paths that must be sanitized.
    ImmutableBiMap.Builder<Path, Path> sanitizePathsBuilder = ImmutableBiMap.builder();
    sanitizePathsBuilder.put(toolchainPaths.getNdkToolRoot(), sanitizedPaths.getNdkToolRoot());
    if (compilerType != NdkCxxPlatformCompiler.Type.GCC) {
        sanitizePathsBuilder.put(toolchainPaths.getNdkGccToolRoot(), sanitizedPaths.getNdkGccToolRoot());
    }
    sanitizePathsBuilder.put(ndkRoot, Paths.get(ANDROID_NDK_ROOT));
    CxxToolProvider.Type type = compilerType == NdkCxxPlatformCompiler.Type.CLANG ? CxxToolProvider.Type.CLANG : CxxToolProvider.Type.GCC;
    ToolProvider ccTool = new ConstantToolProvider(getCTool(toolchainPaths, compilerType.getCc(), version, executableFinder));
    ToolProvider cxxTool = new ConstantToolProvider(getCTool(toolchainPaths, compilerType.getCxx(), version, executableFinder));
    CompilerProvider cc = new CompilerProvider(ccTool, type);
    PreprocessorProvider cpp = new PreprocessorProvider(ccTool, type);
    CompilerProvider cxx = new CompilerProvider(cxxTool, type);
    PreprocessorProvider cxxpp = new PreprocessorProvider(cxxTool, type);
    CxxPlatform.Builder cxxPlatformBuilder = CxxPlatform.builder();
    ImmutableBiMap<Path, Path> sanitizePaths = sanitizePathsBuilder.build();
    PrefixMapDebugPathSanitizer compilerDebugPathSanitizer = new PrefixMapDebugPathSanitizer(config.getDebugPathSanitizerLimit(), File.separatorChar, Paths.get("."), sanitizePaths, filesystem.getRootPath().toAbsolutePath(), type, filesystem);
    MungingDebugPathSanitizer assemblerDebugPathSanitizer = new MungingDebugPathSanitizer(config.getDebugPathSanitizerLimit(), File.separatorChar, Paths.get("."), sanitizePaths);
    cxxPlatformBuilder.setFlavor(flavor).setAs(cc).addAllAsflags(getAsflags(targetConfiguration, toolchainPaths)).setAspp(cpp).setCc(cc).addAllCflags(getCflagsInternal(targetConfiguration, toolchainPaths)).setCpp(cpp).addAllCppflags(getCppflags(targetConfiguration, toolchainPaths)).setCxx(cxx).addAllCxxflags(getCxxflagsInternal(targetConfiguration, toolchainPaths)).setCxxpp(cxxpp).addAllCxxppflags(getCxxppflags(targetConfiguration, toolchainPaths)).setLd(new DefaultLinkerProvider(LinkerProvider.Type.GNU, new ConstantToolProvider(getCcLinkTool(targetConfiguration, toolchainPaths, compilerType.getCxx(), version, cxxRuntime, executableFinder)))).addAllLdflags(targetConfiguration.getLinkerFlags(compilerType)).addLdflags(// We use it to find symbols from arbitrary binaries.
    "-Wl,--build-id", // Enforce the NX (no execute) security feature
    "-Wl,-z,noexecstack", // Strip unused code
    "-Wl,--gc-sections", // Refuse to produce dynamic objects with undefined symbols
    "-Wl,-z,defs", // Forbid dangerous copy "relocations"
    "-Wl,-z,nocopyreloc", // means the resulting link will only use it if it was actually needed it.
    "-Wl,--as-needed").setStrip(getGccTool(toolchainPaths, "strip", version, executableFinder)).setSymbolNameTool(new PosixNmSymbolNameTool(getGccTool(toolchainPaths, "nm", version, executableFinder))).setAr(new GnuArchiver(getGccTool(toolchainPaths, "ar", version, executableFinder))).setRanlib(getGccTool(toolchainPaths, "ranlib", version, executableFinder)).setCompilerDebugPathSanitizer(compilerDebugPathSanitizer).setAssemblerDebugPathSanitizer(assemblerDebugPathSanitizer).setSharedLibraryExtension("so").setSharedLibraryVersionedExtensionFormat("so.%s").setStaticLibraryExtension("a").setObjectFileExtension("o").setSharedLibraryInterfaceFactory(config.shouldUseSharedLibraryInterfaces() ? Optional.of(ElfSharedLibraryInterfaceFactory.of(new ConstantToolProvider(getGccTool(toolchainPaths, "objcopy", version, executableFinder)))) : Optional.empty());
    // Add the NDK root path to the white-list so that headers from the NDK won't trigger the
    // verification warnings.  Ideally, long-term, we'd model NDK libs/headers via automatically
    // generated nodes/descriptions so that they wouldn't need to special case it here.
    HeaderVerification headerVerification = config.getHeaderVerification();
    try {
        headerVerification = headerVerification.withPlatformWhitelist(ImmutableList.of("^" + Pattern.quote(ndkRoot.toRealPath().toString() + File.separatorChar) + ".*"));
    } catch (IOException e) {
        LOG.warn(e, "NDK path could not be resolved: %s", ndkRoot);
    }
    cxxPlatformBuilder.setHeaderVerification(headerVerification);
    LOG.debug("NDK root: %s", ndkRoot.toString());
    LOG.debug("Headers verification platform whitelist: %s", headerVerification.getPlatformWhitelist());
    if (cxxRuntime != CxxRuntime.SYSTEM) {
        cxxPlatformBuilder.putRuntimeLdflags(Linker.LinkableDepType.SHARED, "-l" + cxxRuntime.getSharedName());
        cxxPlatformBuilder.putRuntimeLdflags(Linker.LinkableDepType.STATIC, "-l" + cxxRuntime.getStaticName());
    }
    CxxPlatform cxxPlatform = cxxPlatformBuilder.build();
    NdkCxxPlatform.Builder builder = NdkCxxPlatform.builder();
    builder.setCxxPlatform(cxxPlatform).setCxxRuntime(cxxRuntime).setObjdump(getGccTool(toolchainPaths, "objdump", version, executableFinder));
    if (cxxRuntime != CxxRuntime.SYSTEM) {
        builder.setCxxSharedRuntimePath(toolchainPaths.getCxxRuntimeLibsDirectory().resolve(cxxRuntime.getSoname()));
    }
    return builder.build();
}
Also used : ConstantToolProvider(com.facebook.buck.rules.ConstantToolProvider) CompilerProvider(com.facebook.buck.cxx.CompilerProvider) GnuArchiver(com.facebook.buck.cxx.GnuArchiver) DefaultLinkerProvider(com.facebook.buck.cxx.DefaultLinkerProvider) HeaderVerification(com.facebook.buck.cxx.HeaderVerification) Path(java.nio.file.Path) ToolProvider(com.facebook.buck.rules.ToolProvider) CxxToolProvider(com.facebook.buck.cxx.CxxToolProvider) ConstantToolProvider(com.facebook.buck.rules.ConstantToolProvider) PrefixMapDebugPathSanitizer(com.facebook.buck.cxx.PrefixMapDebugPathSanitizer) CxxPlatform(com.facebook.buck.cxx.CxxPlatform) MungingDebugPathSanitizer(com.facebook.buck.cxx.MungingDebugPathSanitizer) IOException(java.io.IOException) ImmutableBiMap(com.google.common.collect.ImmutableBiMap) PreprocessorProvider(com.facebook.buck.cxx.PreprocessorProvider) CxxToolProvider(com.facebook.buck.cxx.CxxToolProvider) PosixNmSymbolNameTool(com.facebook.buck.cxx.PosixNmSymbolNameTool) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 27 with CxxPlatform

use of com.facebook.buck.cxx.CxxPlatform in project buck by facebook.

the class AppleLibraryDescription method createLibraryBuildRule.

/**
   * @param targetGraph The target graph.
   * @param bundleLoader The binary in which the current library will be (dynamically) loaded into.
   *                     Only valid when building a shared library with MACH_O_BUNDLE link type.
   */
public <A extends AppleNativeTargetDescriptionArg> BuildRule createLibraryBuildRule(TargetGraph targetGraph, BuildRuleParams params, BuildRuleResolver resolver, A args, Optional<Linker.LinkableDepType> linkableDepType, Optional<SourcePath> bundleLoader, ImmutableSet<BuildTarget> blacklist) throws NoSuchBuildTargetException {
    // We explicitly remove flavors from params to make sure rule
    // has the same output regardless if we will strip or not.
    Optional<StripStyle> flavoredStripStyle = StripStyle.FLAVOR_DOMAIN.getValue(params.getBuildTarget());
    params = CxxStrip.removeStripStyleFlavorInParams(params, flavoredStripStyle);
    SourcePathResolver pathResolver = new SourcePathResolver(new SourcePathRuleFinder(resolver));
    BuildRule unstrippedBinaryRule = requireUnstrippedBuildRule(params, resolver, targetGraph, args, linkableDepType, bundleLoader, blacklist, pathResolver);
    if (!shouldWrapIntoDebuggableBinary(params.getBuildTarget(), unstrippedBinaryRule)) {
        return unstrippedBinaryRule;
    }
    // If we built a multiarch binary, we can just use the strip tool from any platform.
    // We pick the platform in this odd way due to FlavorDomain's restriction of allowing only one
    // matching flavor in the build target.
    CxxPlatform representativePlatform = delegate.getCxxPlatforms().getValue(Iterables.getFirst(Sets.intersection(delegate.getCxxPlatforms().getFlavors(), params.getBuildTarget().getFlavors()), defaultCxxPlatform.getFlavor()));
    params = CxxStrip.restoreStripStyleFlavorInParams(params, flavoredStripStyle);
    BuildRule strippedBinaryRule = CxxDescriptionEnhancer.createCxxStripRule(params, resolver, flavoredStripStyle.orElse(StripStyle.NON_GLOBAL_SYMBOLS), unstrippedBinaryRule, representativePlatform);
    return AppleDescriptions.createAppleDebuggableBinary(params, resolver, strippedBinaryRule, (ProvidesLinkedBinaryDeps) unstrippedBinaryRule, AppleDebugFormat.FLAVOR_DOMAIN.getValue(params.getBuildTarget()).orElse(appleConfig.getDefaultDebugInfoFormatForLibraries()), delegate.getCxxPlatforms(), delegate.getDefaultCxxPlatform(), appleCxxPlatformFlavorDomain);
}
Also used : StripStyle(com.facebook.buck.cxx.StripStyle) CxxPlatform(com.facebook.buck.cxx.CxxPlatform) BuildRule(com.facebook.buck.rules.BuildRule) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder)

Example 28 with CxxPlatform

use of com.facebook.buck.cxx.CxxPlatform in project buck by facebook.

the class CxxPlatformXcodeConfigGeneratorTest method testResultHasOtherCxxFlagsTakenFromPlatformCxxflags.

@Test
public void testResultHasOtherCxxFlagsTakenFromPlatformCxxflags() {
    CxxPlatform platform = CxxPlatform.builder().from(DEFAULT_PLATFORM).setCxxflags(ImmutableList.of("-Wno-warning", "-someflag", "-g")).build();
    ImmutableMap<String, ImmutableMap<String, String>> buildConfigs = CxxPlatformXcodeConfigGenerator.getDefaultXcodeBuildConfigurationsFromCxxPlatform(platform, new LinkedHashMap<String, String>());
    ImmutableMap<String, String> config = buildConfigs.get("Debug");
    assertThat(config.get(CxxPlatformXcodeConfigGenerator.OTHER_CPLUSPLUSFLAGS), Matchers.equalTo("-Wno-warning -someflag -g"));
}
Also used : CxxPlatform(com.facebook.buck.cxx.CxxPlatform) ImmutableMap(com.google.common.collect.ImmutableMap) Test(org.junit.Test)

Example 29 with CxxPlatform

use of com.facebook.buck.cxx.CxxPlatform in project buck by facebook.

the class CxxPlatformXcodeConfigGeneratorTest method testResultHasValuesMergedFromAppendConfig.

@Test
public void testResultHasValuesMergedFromAppendConfig() {
    final String someCrazyKey = "SOME_CRAZY_KEY";
    LinkedHashMap<String, String> appendConfig = new LinkedHashMap<String, String>();
    appendConfig.put(someCrazyKey, "value");
    CxxPlatform platform = CxxPlatform.builder().from(DEFAULT_PLATFORM).setCxxflags(ImmutableList.of("-Wno-warning", "-someflag", "-g")).build();
    ImmutableMap<String, ImmutableMap<String, String>> buildConfigs = CxxPlatformXcodeConfigGenerator.getDefaultXcodeBuildConfigurationsFromCxxPlatform(platform, appendConfig);
    ImmutableMap<String, String> config = buildConfigs.get(CxxPlatformXcodeConfigGenerator.DEBUG_BUILD_CONFIGURATION_NAME);
    assertThat(config.get(CxxPlatformXcodeConfigGenerator.OTHER_CPLUSPLUSFLAGS), Matchers.equalTo("-Wno-warning -someflag -g"));
    assertThat(config.get(someCrazyKey), Matchers.equalTo(appendConfig.get(someCrazyKey)));
}
Also used : CxxPlatform(com.facebook.buck.cxx.CxxPlatform) ImmutableMap(com.google.common.collect.ImmutableMap) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 30 with CxxPlatform

use of com.facebook.buck.cxx.CxxPlatform in project buck by facebook.

the class CxxPlatformXcodeConfigGeneratorTest method testResultHasCxxLanguageStandardValueTakenFromPlatformCxxflags.

@Test
public void testResultHasCxxLanguageStandardValueTakenFromPlatformCxxflags() {
    CxxPlatform platform = CxxPlatform.builder().from(DEFAULT_PLATFORM).setCxxflags(ImmutableList.of("-std=somevalue")).build();
    ImmutableMap<String, ImmutableMap<String, String>> buildConfigs = CxxPlatformXcodeConfigGenerator.getDefaultXcodeBuildConfigurationsFromCxxPlatform(platform, new LinkedHashMap<String, String>());
    ImmutableMap<String, String> config = buildConfigs.get(CxxPlatformXcodeConfigGenerator.DEBUG_BUILD_CONFIGURATION_NAME);
    assertThat(config.get(CxxPlatformXcodeConfigGenerator.CLANG_CXX_LANGUAGE_STANDARD), Matchers.equalTo("somevalue"));
}
Also used : CxxPlatform(com.facebook.buck.cxx.CxxPlatform) ImmutableMap(com.google.common.collect.ImmutableMap) Test(org.junit.Test)

Aggregations

CxxPlatform (com.facebook.buck.cxx.CxxPlatform)52 Test (org.junit.Test)25 ImmutableMap (com.google.common.collect.ImmutableMap)24 Path (java.nio.file.Path)22 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)20 BuildTarget (com.facebook.buck.model.BuildTarget)19 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)19 SourcePath (com.facebook.buck.rules.SourcePath)17 BuildRule (com.facebook.buck.rules.BuildRule)14 ImmutableList (com.google.common.collect.ImmutableList)11 HumanReadableException (com.facebook.buck.util.HumanReadableException)9 ImmutableSet (com.google.common.collect.ImmutableSet)9 FakeExecutableFinder (com.facebook.buck.io.FakeExecutableFinder)8 BuildRuleResolver (com.facebook.buck.rules.BuildRuleResolver)8 FakeSourcePath (com.facebook.buck.rules.FakeSourcePath)8 Optional (java.util.Optional)8 Flavor (com.facebook.buck.model.Flavor)7 ImmutableSortedSet (com.google.common.collect.ImmutableSortedSet)7 CxxBuckConfig (com.facebook.buck.cxx.CxxBuckConfig)6 Linker (com.facebook.buck.cxx.Linker)6