use of com.facebook.buck.cxx.GnuArchiver 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();
}
Aggregations