Skip to content
This repository has been archived by the owner on Nov 10, 2023. It is now read-only.

Support NDK r21e and r22 #2595

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,14 @@ public NdkTargetArchAbi getTargetArchAbi() {
}

public ImmutableList<String> getAssemblerFlags(NdkCompilerType type) {
return getTargetCpuType().getAssemblerFlags(type);
return getTargetCpuType().getAssemblerFlags(type, getTargetAppPlatformLevel());
}

public ImmutableList<String> getCompilerFlags(NdkCompilerType type) {
return getTargetCpuType().getCompilerFlags(type);
return getTargetCpuType().getCompilerFlags(type, getTargetAppPlatformLevel());
}

public ImmutableList<String> getLinkerFlags(NdkCompilerType type) {
return getTargetCpuType().getLinkerFlags(type);
return getTargetCpuType().getLinkerFlags(type, getTargetAppPlatformLevel());
}
}
72 changes: 36 additions & 36 deletions src/com/facebook/buck/android/toolchain/ndk/TargetCpuType.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,41 +45,41 @@ public NdkToolchainTarget getToolchainTarget() {
}

@Override
public ImmutableList<String> getAssemblerFlags(NdkCompilerType compiler) {
public ImmutableList<String> getAssemblerFlags(NdkCompilerType compiler, int androidPlatformLevel) {
switch (compiler) {
case GCC:
return armeabiArchFlags;
case CLANG:
return ImmutableList.<String>builder()
.add("-target", "armv5te-none-linux-androideabi")
.add("-target", "armv5te-none-linux-androideabi" + String.valueOf(androidPlatformLevel))
.addAll(armeabiArchFlags)
.build();
}
throw new AssertionError();
}

@Override
public ImmutableList<String> getCompilerFlags(NdkCompilerType compiler) {
public ImmutableList<String> getCompilerFlags(NdkCompilerType compiler, int androidPlatformLevel) {
switch (compiler) {
case GCC:
return ImmutableList.<String>builder().add("-Os").addAll(armeabiArchFlags).build();
case CLANG:
return ImmutableList.<String>builder()
.add("-target", "armv5te-none-linux-androideabi", "-Os")
.add("-target", "armv5te-none-linux-androideabi" + String.valueOf(androidPlatformLevel), "-Os")
.addAll(armeabiArchFlags)
.build();
}
throw new AssertionError();
}

@Override
public ImmutableList<String> getLinkerFlags(NdkCompilerType compiler) {
public ImmutableList<String> getLinkerFlags(NdkCompilerType compiler, int androidPlatformLevel) {
switch (compiler) {
case GCC:
return ImmutableList.of("-march=armv5te", "-Wl,--fix-cortex-a8");
case CLANG:
return ImmutableList.of(
"-target", "armv5te-none-linux-androideabi", "-march=armv5te", "-Wl,--fix-cortex-a8");
"-target", "armv5te-none-linux-androideabi" + String.valueOf(androidPlatformLevel), "-march=armv5te", "-Wl,--fix-cortex-a8");
}
throw new AssertionError();
}
Expand Down Expand Up @@ -109,21 +109,21 @@ public NdkToolchainTarget getToolchainTarget() {
}

@Override
public ImmutableList<String> getAssemblerFlags(NdkCompilerType compiler) {
public ImmutableList<String> getAssemblerFlags(NdkCompilerType compiler, int androidPlatformLevel) {
switch (compiler) {
case GCC:
return armeabiv7ArchFlags;
case CLANG:
return ImmutableList.<String>builder()
.add("-target", "armv7-none-linux-androideabi")
.add("-target", "armv7-none-linux-androideabi" + String.valueOf(androidPlatformLevel))
.addAll(armeabiv7ArchFlags)
.build();
}
throw new AssertionError();
}

@Override
public ImmutableList<String> getCompilerFlags(NdkCompilerType compiler) {
public ImmutableList<String> getCompilerFlags(NdkCompilerType compiler, int androidPlatformLevel) {
switch (compiler) {
case GCC:
return ImmutableList.<String>builder()
Expand All @@ -132,20 +132,20 @@ public ImmutableList<String> getCompilerFlags(NdkCompilerType compiler) {
.build();
case CLANG:
return ImmutableList.<String>builder()
.add("-target", "armv7-none-linux-androideabi", "-Os")
.add("-target", "armv7-none-linux-androideabi" + String.valueOf(androidPlatformLevel), "-Os")
.addAll(armeabiv7ArchFlags)
.build();
}
throw new AssertionError();
}

@Override
public ImmutableList<String> getLinkerFlags(NdkCompilerType compiler) {
public ImmutableList<String> getLinkerFlags(NdkCompilerType compiler, int androidPlatformLevel) {
switch (compiler) {
case GCC:
return ImmutableList.of();
case CLANG:
return ImmutableList.of("-target", "armv7-none-linux-androideabi");
return ImmutableList.of("-target", "armv7-none-linux-androideabi" + String.valueOf(androidPlatformLevel));
}
throw new AssertionError();
}
Expand Down Expand Up @@ -174,21 +174,21 @@ public NdkToolchainTarget getToolchainTarget() {
}

@Override
public ImmutableList<String> getAssemblerFlags(NdkCompilerType compiler) {
public ImmutableList<String> getAssemblerFlags(NdkCompilerType compiler, int androidPlatformLevel) {
switch (compiler) {
case GCC:
return arm64ArchFlags;
case CLANG:
return ImmutableList.<String>builder()
.add("-target", "aarch64-none-linux-android")
.add("-target", "aarch64-none-linux-android" + String.valueOf(androidPlatformLevel))
.addAll(arm64ArchFlags)
.build();
}
throw new AssertionError();
}

@Override
public ImmutableList<String> getCompilerFlags(NdkCompilerType compiler) {
public ImmutableList<String> getCompilerFlags(NdkCompilerType compiler, int androidPlatformLevel) {
switch (compiler) {
case GCC:
return ImmutableList.<String>builder()
Expand All @@ -201,7 +201,7 @@ public ImmutableList<String> getCompilerFlags(NdkCompilerType compiler) {
.build();
case CLANG:
return ImmutableList.<String>builder()
.add("-target", "aarch64-none-linux-android")
.add("-target", "aarch64-none-linux-android" + String.valueOf(androidPlatformLevel))
.add("-O2")
.add("-fomit-frame-pointer")
.add("-fstrict-aliasing")
Expand All @@ -212,12 +212,12 @@ public ImmutableList<String> getCompilerFlags(NdkCompilerType compiler) {
}

@Override
public ImmutableList<String> getLinkerFlags(NdkCompilerType compiler) {
public ImmutableList<String> getLinkerFlags(NdkCompilerType compiler, int androidPlatformLevel) {
switch (compiler) {
case GCC:
return ImmutableList.of();
case CLANG:
return ImmutableList.of("-target", "aarch64-none-linux-android");
return ImmutableList.of("-target", "aarch64-none-linux-android" + String.valueOf(androidPlatformLevel));
}
throw new AssertionError();
}
Expand All @@ -244,34 +244,34 @@ public NdkToolchainTarget getToolchainTarget() {
}

@Override
public ImmutableList<String> getAssemblerFlags(NdkCompilerType compiler) {
public ImmutableList<String> getAssemblerFlags(NdkCompilerType compiler, int androidPlatformLevel) {
switch (compiler) {
case GCC:
return ImmutableList.of();
case CLANG:
return ImmutableList.of("-target", "i686-none-linux-android");
return ImmutableList.of("-target", "i686-none-linux-android" + String.valueOf(androidPlatformLevel));
}
throw new AssertionError();
}

@Override
public ImmutableList<String> getCompilerFlags(NdkCompilerType compiler) {
public ImmutableList<String> getCompilerFlags(NdkCompilerType compiler, int androidPlatformLevel) {
switch (compiler) {
case GCC:
return ImmutableList.of("-funswitch-loops", "-finline-limit=300", "-O2");
case CLANG:
return ImmutableList.of("-target", "i686-none-linux-android", "-O2");
return ImmutableList.of("-target", "i686-none-linux-android" + String.valueOf(androidPlatformLevel), "-O2");
}
throw new AssertionError();
}

@Override
public ImmutableList<String> getLinkerFlags(NdkCompilerType compiler) {
public ImmutableList<String> getLinkerFlags(NdkCompilerType compiler, int androidPlatformLevel) {
switch (compiler) {
case GCC:
return ImmutableList.of();
case CLANG:
return ImmutableList.of("-target", "i686-none-linux-android");
return ImmutableList.of("-target", "i686-none-linux-android" + String.valueOf(androidPlatformLevel));
}
throw new AssertionError();
}
Expand All @@ -298,34 +298,34 @@ public NdkToolchainTarget getToolchainTarget() {
}

@Override
public ImmutableList<String> getAssemblerFlags(NdkCompilerType compiler) {
public ImmutableList<String> getAssemblerFlags(NdkCompilerType compiler, int androidPlatformLevel) {
switch (compiler) {
case GCC:
return ImmutableList.of();
case CLANG:
return ImmutableList.of("-target", "x86_64-none-linux-android");
return ImmutableList.of("-target", "x86_64-none-linux-android" + String.valueOf(androidPlatformLevel));
}
throw new AssertionError();
}

@Override
public ImmutableList<String> getCompilerFlags(NdkCompilerType compiler) {
public ImmutableList<String> getCompilerFlags(NdkCompilerType compiler, int androidPlatformLevel) {
switch (compiler) {
case GCC:
return ImmutableList.of("-funswitch-loops", "-finline-limit=300", "-O2");
case CLANG:
return ImmutableList.of("-target", "x86_64-none-linux-android", "-O2");
return ImmutableList.of("-target", "x86_64-none-linux-android" + String.valueOf(androidPlatformLevel), "-O2");
}
throw new AssertionError();
}

@Override
public ImmutableList<String> getLinkerFlags(NdkCompilerType compiler) {
public ImmutableList<String> getLinkerFlags(NdkCompilerType compiler, int androidPlatformLevel) {
switch (compiler) {
case GCC:
return ImmutableList.of();
case CLANG:
return ImmutableList.of("-target", "x86_64-none-linux-android");
return ImmutableList.of("-target", "x86_64-none-linux-android" + String.valueOf(androidPlatformLevel));
}
throw new AssertionError();
}
Expand All @@ -352,17 +352,17 @@ public NdkToolchainTarget getToolchainTarget() {
}

@Override
public ImmutableList<String> getAssemblerFlags(NdkCompilerType compiler) {
public ImmutableList<String> getAssemblerFlags(NdkCompilerType compiler, int androidPlatformLevel) {
throw new AssertionError();
}

@Override
public ImmutableList<String> getCompilerFlags(NdkCompilerType compiler) {
public ImmutableList<String> getCompilerFlags(NdkCompilerType compiler, int androidPlatformLevel) {
throw new AssertionError();
}

@Override
public ImmutableList<String> getLinkerFlags(NdkCompilerType compiler) {
public ImmutableList<String> getLinkerFlags(NdkCompilerType compiler, int androidPlatformLevel) {
throw new AssertionError();
}
};
Expand All @@ -375,9 +375,9 @@ public ImmutableList<String> getLinkerFlags(NdkCompilerType compiler) {

public abstract NdkToolchainTarget getToolchainTarget();

public abstract ImmutableList<String> getAssemblerFlags(NdkCompilerType compiler);
public abstract ImmutableList<String> getAssemblerFlags(NdkCompilerType compiler, int androidPlatformLevel);

public abstract ImmutableList<String> getCompilerFlags(NdkCompilerType compiler);
public abstract ImmutableList<String> getCompilerFlags(NdkCompilerType compiler, int androidPlatformLevel);

public abstract ImmutableList<String> getLinkerFlags(NdkCompilerType compiler);
public abstract ImmutableList<String> getLinkerFlags(NdkCompilerType compiler, int androidPlatformLevel);
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class AndroidNdkResolver extends BaseAndroidToolchainResolver {
private static final Logger LOG = Logger.get(AndroidNdkResolver.class);

/** Android NDK versions starting with this number are not supported. */
private static final String NDK_MIN_UNSUPPORTED_VERSION = "22";
private static final String NDK_MIN_UNSUPPORTED_VERSION = "23";

// Pre r11 NDKs store the version at RELEASE.txt.
@VisibleForTesting static final String NDK_PRE_R11_VERSION_FILENAME = "RELEASE.TXT";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,24 @@ public class NdkCxxPlatforms {
"-Wl,--as-needed");

private static final Pattern NDK_MAJOR_VERSION_PATTERN = Pattern.compile("^[rR]?(\\d+).*");
private static final Pattern NDK_MINOR_VERSION_PATTERN = Pattern.compile("^[rR]?\\d+\\.(\\d+).*");

// Utility class, do not instantiate.
private NdkCxxPlatforms() {}

static int getNdkMajorVersion(String ndkVersion) {
return Integer.parseInt(NDK_MAJOR_VERSION_PATTERN.matcher(ndkVersion).replaceAll("$1"));
}

static int getNdkMinorVersion(String ndkVersion) {
Matcher minorMatch = NDK_MINOR_VERSION_PATTERN.matcher(ndkVersion);

// ndkVersion does not always contain the minor version (e.g. r10e)
if(minorMatch.matches()){
return Integer.parseInt(minorMatch.replaceAll("$1"));
}
return 0
}

public static NdkCompilerType getDefaultCompilerTypeForNdk(String ndkVersion) {
return getNdkMajorVersion(ndkVersion) < 18 ? NdkCompilerType.GCC : NdkCompilerType.CLANG;
Expand All @@ -172,6 +183,7 @@ public static String getDefaultGccVersionForNdk(String ndkVersion) {

public static String getDefaultClangVersionForNdk(String ndkVersion) {
int ndkMajorVersion = getNdkMajorVersion(ndkVersion);
int ndkMinorVersion = getNdkMinorVersion(ndkVersion);
if (ndkMajorVersion < 11) {
return "3.5";
} else if (ndkMajorVersion < 15) {
Expand All @@ -186,8 +198,12 @@ public static String getDefaultClangVersionForNdk(String ndkVersion) {
return "8.0.2";
} else if (ndkMajorVersion < 21) {
return "8.0.7";
} else {
} else if (ndkMajorVersion < 22 && ndkMinorVersion < 4 ) { // NDK up to r21d
return "9.0.8";
} else if (ndkMajorVersion < 22 && ndkMinorVersion >= 4 ) { //NDK r21e
return "9.0.9";
} else { // NDK r22 and +
return "11.0.5";
}
}

Expand Down Expand Up @@ -901,10 +917,6 @@ private static ImmutableList<String> getCommonFlags(
// NDK builds enable stack protector and debug symbols by default.
flags.add("-fstack-protector", "-g3");

if (toolchainPaths.isUnifiedHeaders()) {
flags.add("-D__ANDROID_API__=" + targetConfiguration.getTargetAppPlatformLevel());
}

return flags.build();
}

Expand Down Expand Up @@ -1137,6 +1149,7 @@ Path processPathPattern(Path root, String pattern, boolean appendExtension) {
"{gcc_compiler_version}", targetConfiguration.getCompiler().getGccVersion());
s = s.replace("{hostname}", hostName);
s = s.replace("{target_platform}", targetConfiguration.getTargetAppPlatform());
s = s.replace("{target_platform_level}", String.valueOf(targetConfiguration.getTargetAppPlatformLevel()));
s = s.replace("{target_arch}", targetConfiguration.getTargetArch().toString());
s = s.replace("{target_arch_abi}", targetConfiguration.getTargetArchAbi().toString());
}
Expand Down Expand Up @@ -1197,7 +1210,13 @@ private static Path appendExtensionIfNeeded(Path path) {

/** @return the path to arch-specific include files; only use with unified headers */
Path getArchSpecificIncludes() {
return processDirectoryPathPattern("sysroot/usr/include/{toolchain_target}");
// <ndk_root>/sysroot and platforms directory has changed location see
// https://android.googlesource.com/platform/ndk/+/master/docs/BuildSystemMaintainers.md#Sysroot
if(ndkMajorVersion > 21) {
return processDirectoryPathPattern("toolchains/llvm/prebuilt/{hostname}/sysroot/usr/include/{toolchain_target}");
} else {
return processDirectoryPathPattern("sysroot/usr/include/{toolchain_target}");
}
}

/**
Expand All @@ -1206,13 +1225,26 @@ Path getArchSpecificIncludes() {
*/
Path getIncludeSysroot() {
if (isUnifiedHeaders()) {
return processDirectoryPathPattern("sysroot");
// <ndk_root>/sysroot and platforms directory has changed location see
// https://android.googlesource.com/platform/ndk/+/master/docs/BuildSystemMaintainers.md#Sysroot
if(ndkMajorVersion > 21) {
return processDirectoryPathPattern("toolchains/llvm/prebuilt/{hostname}/sysroot");
} else {
return processDirectoryPathPattern("sysroot");
}
}
return getPlatformSysroot();
}

Path getPlatformSysroot() {
return processDirectoryPathPattern("platforms/{target_platform}/arch-{target_arch}");
// <ndk_root>/sysroot and platforms directory has changed location see
// https://android.googlesource.com/platform/ndk/+/master/docs/BuildSystemMaintainers.md#Sysroot
if(ndkMajorVersion > 21) {
return processDirectoryPathPattern("toolchains/llvm/prebuilt/{hostname}/sysroot");
//return processDirectoryPathPattern("toolchains/llvm/prebuilt/{hostname}/sysroot/usr/lib/{toolchain_target}/{target_platform_level}");
} else {
return processDirectoryPathPattern("platforms/{target_platform}/arch-{target_arch}");
}
}

Path getLibexecGccToolPath() {
Expand Down
Loading