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

Commit

Permalink
Add integration tests for ABI-specific NDK app platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
LegNeato committed May 25, 2018
1 parent beaf18f commit 785ea91
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 8 deletions.
101 changes: 94 additions & 7 deletions test/com/facebook/buck/android/NdkLibraryIntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,33 @@

package com.facebook.buck.android;

import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;

import static org.junit.Assert.assertThat;

import com.facebook.buck.android.toolchain.ndk.NdkCxxPlatform;
import com.facebook.buck.android.toolchain.ndk.impl.AndroidNdkHelper;
import com.facebook.buck.android.toolchain.ndk.impl.AndroidNdkHelper.SymbolGetter;
import com.facebook.buck.android.toolchain.ndk.impl.AndroidNdkHelper.SymbolsAndDtNeeded;
import com.facebook.buck.core.rules.resolver.impl.TestBuildRuleResolver;
import com.facebook.buck.core.sourcepath.resolver.SourcePathResolver;
import com.facebook.buck.core.sourcepath.resolver.impl.DefaultSourcePathResolver;
import com.facebook.buck.io.filesystem.ProjectFilesystem;
import com.facebook.buck.io.filesystem.TestProjectFilesystems;
import com.facebook.buck.rules.SourcePathRuleFinder;
import com.facebook.buck.testutil.ProcessResult;
import com.facebook.buck.testutil.TemporaryPaths;
import com.facebook.buck.testutil.TestConsole;
import com.facebook.buck.testutil.integration.ProjectWorkspace;
import com.facebook.buck.testutil.integration.TestDataHelper;
import com.facebook.buck.testutil.integration.ZipInspector;
import com.facebook.buck.util.DefaultProcessExecutor;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.hamcrest.Matchers;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

Expand All @@ -34,10 +52,13 @@ public class NdkLibraryIntegrationTest {

@Rule public TemporaryPaths tmp2 = new TemporaryPaths();

@Test
public void cxxLibraryDep() throws InterruptedException, IOException {
@Before
public void setUp() throws InterruptedException, IOException {
AssumeAndroidPlatform.assumeNdkIsAvailable();
}

@Test
public void cxxLibraryDep() throws InterruptedException, IOException {
ProjectWorkspace workspace1 =
TestDataHelper.createProjectWorkspaceForScenarioWithoutDefaultCell(this, "cxx_deps", tmp1);
workspace1.setUp();
Expand All @@ -58,8 +79,6 @@ public void cxxLibraryDep() throws InterruptedException, IOException {

@Test
public void sourceFilesChangeTargetHash() throws InterruptedException, IOException {
AssumeAndroidPlatform.assumeNdkIsAvailable();

ProjectWorkspace workspace =
TestDataHelper.createProjectWorkspaceForScenario(this, "cxx_deps", tmp1);
workspace.setUp();
Expand Down Expand Up @@ -89,8 +108,6 @@ public void sourceFilesChangeTargetHash() throws InterruptedException, IOExcepti

@Test
public void ndkLibraryOwnsItsSources() throws InterruptedException, IOException {
AssumeAndroidPlatform.assumeNdkIsAvailable();

ProjectWorkspace workspace =
TestDataHelper.createProjectWorkspaceForScenario(this, "cxx_deps", tmp1);
workspace.setUp();
Expand All @@ -100,4 +117,74 @@ public void ndkLibraryOwnsItsSources() throws InterruptedException, IOException
result.assertSuccess();
assertEquals("//jni:foo", result.getStdout().trim());
}

@Test
public void ndkLibraryAppPlatformDefaultCpuAbi() throws InterruptedException, IOException {
ProjectWorkspace workspace =
TestDataHelper.createProjectWorkspaceForScenario(this, "android_project", tmp1);
workspace.setUp();
workspace.replaceFileContents(".buckconfig", "#cpu_abis", "cpu_abis = armv7, x86");
workspace.replaceFileContents(
".buckconfig",
"#app_platform",
"app_platform = android-16\n app_platform-armv7 = android-19");
Path apkPath = workspace.buildAndReturnOutput("//apps/sample:app_cxx_lib_app_platform");

SymbolsAndDtNeeded info;
ProjectFilesystem filesystem =
TestProjectFilesystems.createProjectFilesystem(workspace.getDestPath());
SymbolGetter syms = getSymbolGetter(filesystem, tmp1);

ZipInspector zipInspector = new ZipInspector(apkPath);
zipInspector.assertFileExists("lib/x86/libnative_app_platform_lib.so");
zipInspector.assertFileExists("lib/armeabi-v7a/libnative_app_platform_lib.so");

info = syms.getSymbolsAndDtNeeded(apkPath, "lib/x86/libnative_app_platform_lib.so");
assertThat(info.symbols.global, Matchers.hasItem("Android16"));
assertThat(info.symbols.global, not(Matchers.hasItem("Android19")));

info = syms.getSymbolsAndDtNeeded(apkPath, "lib/armeabi-v7a/libnative_app_platform_lib.so");
assertThat(info.symbols.global, Matchers.hasItem("Android19"));
assertThat(info.symbols.global, not(Matchers.hasItem("Android16")));
}

@Test
public void ndkLibraryAppPlatformByCpuAbi() throws InterruptedException, IOException {
ProjectWorkspace workspace =
TestDataHelper.createProjectWorkspaceForScenario(this, "android_project", tmp1);
workspace.setUp();
workspace.replaceFileContents(".buckconfig", "#cpu_abis", "cpu_abis = armv7, x86");
workspace.replaceFileContents(
".buckconfig",
"#app_platform",
"app_platform-x86 = android-18\n app_platform-armv7 = android-19");
Path apkPath = workspace.buildAndReturnOutput("//apps/sample:app_cxx_lib_app_platform");

SymbolsAndDtNeeded info;
ProjectFilesystem filesystem =
TestProjectFilesystems.createProjectFilesystem(workspace.getDestPath());
SymbolGetter syms = getSymbolGetter(filesystem, tmp1);

ZipInspector zipInspector = new ZipInspector(apkPath);
zipInspector.assertFileExists("lib/x86/libnative_app_platform_lib.so");
zipInspector.assertFileExists("lib/armeabi-v7a/libnative_app_platform_lib.so");

info = syms.getSymbolsAndDtNeeded(apkPath, "lib/x86/libnative_app_platform_lib.so");
assertThat(info.symbols.global, Matchers.hasItem("Android18"));
assertThat(info.symbols.global, not(Matchers.hasItem("Android19")));

info = syms.getSymbolsAndDtNeeded(apkPath, "lib/armeabi-v7a/libnative_app_platform_lib.so");
assertThat(info.symbols.global, Matchers.hasItem("Android19"));
assertThat(info.symbols.global, not(Matchers.hasItem("Android18")));
}

private SymbolGetter getSymbolGetter(ProjectFilesystem filesystem, TemporaryPaths tempLocation)
throws IOException, InterruptedException {
NdkCxxPlatform platform = AndroidNdkHelper.getNdkCxxPlatform(filesystem);
SourcePathResolver pathResolver =
DefaultSourcePathResolver.from(new SourcePathRuleFinder(new TestBuildRuleResolver()));
Path tmpDir = tempLocation.newFolder("symbols_tmp");
return new SymbolGetter(
new DefaultProcessExecutor(new TestConsole()), tmpDir, platform.getObjdump(), pathResolver);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[ndk]
#cpu_abis
app_platform = android-21
#app_platform

[scala]
library = //scala:scala-library
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,20 @@ android_binary(
],
)

android_binary(
name = "app_cxx_lib_app_platform",
cpu_filters = [
"armv7",
"x86",
],
keystore = "//keystores:debug",
manifest = "AndroidManifest.xml",
deps = [
"//native/app_platform:lib",
"//res/com/sample/base:base",
],
)

android_binary(
name = "app_cxx_lib_asset",
cpu_filters = [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
cxx_library(
name = "lib",
srcs = ["lib.c"],
visibility = [
"PUBLIC",
],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#ifdef __ANDROID__
# include <android/api-level.h>
#endif

#ifdef __ANDROID_API__
# if __ANDROID_API__ == 15
void Android15() {}
# endif
# if __ANDROID_API__ == 16
void Android16() {}
# endif
# if __ANDROID_API__ == 17
void Android17() {}
# endif
# if __ANDROID_API__ == 18
void Android18() {}
# endif
# if __ANDROID_API__ == 19
void Android19() {}
# endif
# if __ANDROID_API__ == 20
void Android20() {}
# endif
# if __ANDROID_API__ == 21
void Android21() {}
# endif
# if __ANDROID_API__ == 22
void Android22() {}
# endif
# if __ANDROID_API__ == 23
void Android23() {}
# endif
# if __ANDROID_API__ == 24
void Android24() {}
# endif
#endif

0 comments on commit 785ea91

Please sign in to comment.