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

Commit

Permalink
Switch to using a map for app_platform_per_cpu_abi
Browse files Browse the repository at this point in the history
  • Loading branch information
LegNeato committed Jun 15, 2018
1 parent ca612dc commit 1c13944
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 59 deletions.
48 changes: 26 additions & 22 deletions docs/concept/buckconfig.soy
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ The root of your project must contain a configuration file
named <code>.buckconfig</code>. If present, Buck will read this file
before executing its business logic so that any customizations specified
in <code>.buckconfig</code> will take effect. This file uses the
{sp}<a href="http://en.wikipedia.org/wiki/INI_file">INI file format</a> with
{sp}<a href="http://en.wikipedia.org/wiki/INI_file">INI file format</a> with
a few extensions discussed below.
</p>

Expand Down Expand Up @@ -237,7 +237,7 @@ $ buck targets --resolve-alias app#src_jar
{param example_value: '/Library/Android/sdk' /}
{param description}
This specifies the absolute path to the Android SDK that all Android code in the project should
be built against. This is equivalent to setting the environment variable <code>ANDROID_SDK=/Library/Android/sdk</code>.
be built against. This is equivalent to setting the environment variable <code>ANDROID_SDK=/Library/Android/sdk</code>.
The environment variables <code>ANDROID_SDK</code> and <code>ANDROID_HOME</code> both supersede the buckconfig setting.
The default is empty.
{/param}
Expand Down Expand Up @@ -618,7 +618,7 @@ osx_toolchains_override = tools.stable,tools.swift40,tools.common
{param raw_example}
{literal}<pre class="prettyprint lang-ini">
[apple]
# Use (my_clang|my_actool) executable which exists in one of the
# Use (my_clang|my_actool) executable which exists in one of the
# imported SDKs and toolchains, instead of the defaults.
clang_xcode_tool_name_override=my_clang
actool_xcode_tool_name_override=my_actool
Expand Down Expand Up @@ -837,7 +837,7 @@ osx_toolchains_override = tools.stable,tools.swift40,tools.common
{param name: 'name' /}
{param example_value: 'BUILD' /}
{param description}
The name of {call buck.build_file /}s within a project.
The name of {call buck.build_file /}s within a project.
This defaults to <code>BUCK</code>.
We recommend that you use the default name. However, you could
specify a different name&mdash;such as <code>BUILD</code> shown
Expand Down Expand Up @@ -2629,7 +2629,7 @@ cxx_library(
{param description}
The maximum number of threads allowed to run the dexing steps with. Since the dexing steps can
use a lot of memory, it might be useful to set this to a lower value to avoid out-of-memory
on systems that have a lot of CPU cores. This parameter is mostly useful
on systems that have a lot of CPU cores. This parameter is mostly useful
when {call buckconfig.dx_threads /} is not specified and the number of threads is obtained based on
hardware.
{/param}
Expand Down Expand Up @@ -2890,24 +2890,28 @@ your <code>.buckjavaargs</code> file</a>:
{call buckconfig.entry}
{param section: 'ndk' /}
{param name: 'app_platform' /}
{param example_value: 'android-21' /}
{param description}
<p>
The android platform libraries that the code is targeting. This is equivalent to the{sp}
<code>APP_TARGET</code> in the NDK build system. The default is <code>android-16</code>.
{/param}
{/call}

{call buckconfig.entry}
{param section: 'ndk' /}
{param name: 'app_platform_per_cpu_abi' /}
{param example_value: 'arm64 => android-19, i386 => android-22' /}
{param description}
<p>
The android platform libraries that the code is targeting, set on a{sp}
per-CPU ABI basis. This is equivalent to the <code>APP_TARGET</code> in{sp}
the NDK build system.
</p>
<p>
This setting can also be set on a per-CPU ABI basis. Each key should begin with the{sp}
prefix <code>app_platform-</code>, followed by the CPU ABI name. ABI-specific values{sp}
have higher precedence.
If no value is set for a particular CPU ABI, the value from{sp}
<a href="#ndk.app_platform"><code>app_platform</code></a> is used as a fallback.
</p>
{/param}
{param raw_example}
{literal}<pre class="prettyprint lang-ini">
[ndk]
app_platform = android-19
app_platform-arm64 = android-21
</pre>{/literal}
{/param}
{/call}

{call buckconfig.entry}
Expand Down Expand Up @@ -3918,19 +3922,19 @@ your <code>.buckjavaargs</code> file</a>:
</p>
<p>
Specifies a comma-separated list of mappings from regular expressions
(regexes) to message strings.
(regexes) to message strings.
</p>
<p>
If the text of a Buck parser error
matches one of the specified regexes, the corresponding message string
is appended to the error. You can use the message string to provide
additional helpful information to the user.
additional helpful information to the user.
</p>
<p>
If the regex contains unescaped parentheses, <code>()</code>, the text
that the parentheses enclose is captured. You can then insert this captured text
in the appended string by using <code>$1</code> for the first captured text
string, <code>$2</code> for the second, and so on. This works exactly like
that the parentheses enclose is captured. You can then insert this captured text
in the appended string by using <code>$1</code> for the first captured text
string, <code>$2</code> for the second, and so on. This works exactly like
Java regex replacement strings.
</p>
{/param}
Expand Down Expand Up @@ -3979,7 +3983,7 @@ your <code>.buckjavaargs</code> file</a>:
{param description}
Whether the super console is enabled. If so, a more reactive UI will be shown.
Valid values are ENABLED, DISABLED, and AUTO. By default, this is set to AUTO
which will take OS, terminal settings and other things into account. In most
which will take OS, terminal settings and other things into account. In most
interactive cases, it will be enabled.
{/param}
{/call}
Expand Down
26 changes: 8 additions & 18 deletions src/com/facebook/buck/android/AndroidBuckConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,10 @@
import com.google.common.collect.ImmutableSet;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

public class AndroidBuckConfig {
private static final String APP_PLATFORM_KEY_PREFIX = "app_platform-";

private final BuckConfig delegate;
private final Platform platform;

Expand Down Expand Up @@ -68,10 +65,14 @@ public Optional<String> getNdkRepositoryPath() {
return delegate.getValue("ndk", "ndk_repository_path");
}

public Optional<String> getNdkCpuAbiAgnosticAppPlatform() {
public Optional<String> getNdkCpuAbiFallbackAppPlatform() {
return delegate.getValue("ndk", "app_platform");
}

public ImmutableMap<String, String> getNdkCpuAbiAppPlatformMap() {
return delegate.getMap("ndk", "app_platform_per_cpu_abi");
}

public Optional<Set<String>> getNdkCpuAbis() {
return delegate.getOptionalListWithoutComments("ndk", "cpu_abis").map(ImmutableSet::copyOf);
}
Expand Down Expand Up @@ -109,27 +110,16 @@ public boolean isGrayscaleImageProcessingEnabled() {
}

/**
* Returns the CPU specific app platform, or the agnostic one if set. If neither are set, returns
* Returns the CPU specific app platform, or the fallback one if set. If neither are set, returns
* `Optional.empty` instead of a default value so callers can determine the difference between
* user-set and buck defaults.
*/
public Optional<String> getNdkAppPlatformForCpuAbi(String cpuAbi) {
ImmutableMap<String, String> platformMap = getNdkAppPlatformMap();
ImmutableMap<String, String> platformMap = getNdkCpuAbiAppPlatformMap();
Optional<String> specificAppPlatform = Optional.ofNullable(platformMap.get(cpuAbi));
return specificAppPlatform.isPresent()
? specificAppPlatform
: getNdkCpuAbiAgnosticAppPlatform();
}

private ImmutableMap<String, String> getNdkAppPlatformMap() {
ImmutableMap<String, String> allEntries = delegate.getEntriesForSection("ndk");
ImmutableMap.Builder<String, String> platforms = ImmutableMap.builder();
for (Map.Entry<String, String> entry : allEntries.entrySet()) {
if (entry.getKey().startsWith(APP_PLATFORM_KEY_PREFIX)) {
platforms.put(entry.getKey().substring(APP_PLATFORM_KEY_PREFIX.length()), entry.getValue());
}
}
return platforms.build();
: getNdkCpuAbiFallbackAppPlatform();
}

/**
Expand Down
27 changes: 13 additions & 14 deletions test/com/facebook/buck/android/AndroidBuckConfigTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,19 @@ public AndroidBuckConfig makeAndroidBuckConfig(ImmutableMap<String, String> ndkS
public void testNdkAppPlatformForCpuAbi() throws IOException {
ImmutableMap<String, String> ndkSection =
new ImmutableMap.Builder<String, String>()
.put("app_platform-i386", "foo")
.put("app_platform-arm64", "bar")
.put("app_platform_per_cpu_abi", "i386 => foo, arm64 => bar")
.build();
AndroidBuckConfig androidBuckConfig = makeAndroidBuckConfig(ndkSection);

// Make sure we don't have an agnostic value.
assertEquals(androidBuckConfig.getNdkCpuAbiAgnosticAppPlatform(), Optional.empty());
// Make sure we don't have an fallback value.
assertEquals(androidBuckConfig.getNdkCpuAbiFallbackAppPlatform(), Optional.empty());

// Make sure we get our ABI values back.
assertEquals(androidBuckConfig.getNdkAppPlatformForCpuAbi("i386"), Optional.of("foo"));
assertEquals(androidBuckConfig.getNdkAppPlatformForCpuAbi("arm64"), Optional.of("bar"));

// Make sure unset ABI values don't return anything, as
// we didn't set the agnostic value.
// we didn't set the fallback value.
assertEquals(androidBuckConfig.getNdkAppPlatformForCpuAbi("fake"), Optional.empty());
}

Expand All @@ -60,8 +59,8 @@ public void testNdkAppPlatformUnset() throws IOException {
ImmutableMap<String, String> ndkSection = new ImmutableMap.Builder<String, String>().build();
AndroidBuckConfig androidBuckConfig = makeAndroidBuckConfig(ndkSection);

// Make sure we don't have an agnostic value.
assertEquals(androidBuckConfig.getNdkCpuAbiAgnosticAppPlatform(), Optional.empty());
// Make sure we don't have an fallback value.
assertEquals(androidBuckConfig.getNdkCpuAbiFallbackAppPlatform(), Optional.empty());

// Make sure we don't get anything ABI-specific.
assertEquals(androidBuckConfig.getNdkAppPlatformForCpuAbi("i386"), Optional.empty());
Expand All @@ -71,18 +70,18 @@ public void testNdkAppPlatformUnset() throws IOException {
public void testNdkAppPlatformPriority() throws IOException {
ImmutableMap<String, String> ndkSection =
new ImmutableMap.Builder<String, String>()
.put("app_platform", "agnostic")
.put("app_platform-arm64", "specific")
.put("app_platform", "fallback")
.put("app_platform_per_cpu_abi", "arm64 => specific")
.build();
AndroidBuckConfig androidBuckConfig = makeAndroidBuckConfig(ndkSection);

// Make sure we have an agnostic value.
assertEquals(androidBuckConfig.getNdkCpuAbiAgnosticAppPlatform(), Optional.of("agnostic"));
// Make sure we have an fallback value.
assertEquals(androidBuckConfig.getNdkCpuAbiFallbackAppPlatform(), Optional.of("fallback"));

// Make sure ABI-specific values override the agnostic one.
// Make sure ABI-specific values override the fallback one.
assertEquals(androidBuckConfig.getNdkAppPlatformForCpuAbi("arm64"), Optional.of("specific"));

// Make sure we default to agnostic.
assertEquals(androidBuckConfig.getNdkAppPlatformForCpuAbi("fake"), Optional.of("agnostic"));
// Make sure we default to fallback.
assertEquals(androidBuckConfig.getNdkAppPlatformForCpuAbi("fake"), Optional.of("fallback"));
}
}
10 changes: 5 additions & 5 deletions test/com/facebook/buck/android/NdkLibraryIntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@
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.rules.SourcePathRuleFinder;
import com.facebook.buck.core.rules.resolver.impl.TestActionGraphBuilder;
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;
Expand Down Expand Up @@ -127,7 +127,7 @@ public void ndkLibraryAppPlatformDefaultCpuAbi() throws InterruptedException, IO
workspace.replaceFileContents(
".buckconfig",
"#app_platform",
"app_platform = android-16\n app_platform-armv7 = android-19");
"app_platform = android-16\n app_platform_per_cpu_abi = armv7 => android-19");
Path apkPath = workspace.buildAndReturnOutput("//apps/sample:app_cxx_lib_app_platform");

SymbolsAndDtNeeded info;
Expand Down Expand Up @@ -157,7 +157,7 @@ public void ndkLibraryAppPlatformByCpuAbi() throws InterruptedException, IOExcep
workspace.replaceFileContents(
".buckconfig",
"#app_platform",
"app_platform-x86 = android-18\n app_platform-armv7 = android-19");
"app_platform_per_cpu_abi = x86 => android-18, armv7 => android-19");
Path apkPath = workspace.buildAndReturnOutput("//apps/sample:app_cxx_lib_app_platform");

SymbolsAndDtNeeded info;
Expand All @@ -182,7 +182,7 @@ private SymbolGetter getSymbolGetter(ProjectFilesystem filesystem, TemporaryPath
throws IOException, InterruptedException {
NdkCxxPlatform platform = AndroidNdkHelper.getNdkCxxPlatform(filesystem);
SourcePathResolver pathResolver =
DefaultSourcePathResolver.from(new SourcePathRuleFinder(new TestBuildRuleResolver()));
DefaultSourcePathResolver.from(new SourcePathRuleFinder(new TestActionGraphBuilder()));
Path tmpDir = tempLocation.newFolder("symbols_tmp");
return new SymbolGetter(
new DefaultProcessExecutor(new TestConsole()), tmpDir, platform.getObjdump(), pathResolver);
Expand Down

0 comments on commit 1c13944

Please sign in to comment.