Skip to content

Commit

Permalink
Add Android builds optionally to unified build (#2393)
Browse files Browse the repository at this point in the history
To enable, specify android_ndk_root & enable_android_builds=true.
  • Loading branch information
mspang authored Sep 3, 2020
1 parent 723f2da commit fdc49a3
Show file tree
Hide file tree
Showing 16 changed files with 145 additions and 73 deletions.
8 changes: 3 additions & 5 deletions .github/workflows/android.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ jobs:

strategy:
matrix:
type: [arm, arm64]
type: [arm, arm64, x64]

env:
BUILD_TYPE: ${{ matrix.type }}
BUILD_TYPE: android_${{ matrix.type }}

runs-on: ubuntu-latest

Expand All @@ -45,10 +45,8 @@ jobs:
run: scripts/build/gn_bootstrap.sh
- name: Setup Build
run: |
GN_ARGS="is_clang=true target_os=\"android\" target_cpu=\"${BUILD_TYPE}\""
export PATH=$PATH:/usr/local/share/android-ndk-r21b/toolchains/llvm/prebuilt/linux-x86_64/bin/
GN_ARGS="is_clang=true target_os=\"android\" target_cpu=\"${{ matrix.type }}\" android_ndk_root=\"/usr/local/share/android-ndk-r21b\""
scripts/build/gn_gen.sh --args="$GN_ARGS"
- name: Run Build
run: |
export PATH=$PATH:/usr/local/share/android-ndk-r21b/toolchains/llvm/prebuilt/linux-x86_64/bin/
scripts/build/gn_build.sh
29 changes: 29 additions & 0 deletions BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ if (current_toolchain != "${dir_pw_toolchain}/dummy:dummy") {
# Set this to false to disable all builds by default.
enable_default_builds = true

# Enable building for Android.
enable_android_builds = false

# Set this to true to enable nRF5 builds by default.
enable_nrf5_builds = false

Expand Down Expand Up @@ -155,6 +158,24 @@ if (current_toolchain != "${dir_pw_toolchain}/dummy:dummy") {
toolchain = "${chip_root}/config/mbedtls/toolchain:${host_os}_${host_cpu}_gcc_mbedtls"
}

if (enable_android_builds) {
chip_build("android_arm") {
toolchain = "//build/toolchain/android:android_arm"
}

chip_build("android_arm64") {
toolchain = "//build/toolchain/android:android_arm64"
}

chip_build("android_x64") {
toolchain = "//build/toolchain/android:android_x64"
}

chip_build("android_x86") {
toolchain = "//build/toolchain/android:android_x86"
}
}

if (host_os == "linux") {
chip_build("linux_embedded") {
toolchain =
Expand Down Expand Up @@ -201,6 +222,14 @@ if (current_toolchain != "${dir_pw_toolchain}/dummy:dummy") {
if (enable_host_gcc_mbedtls_build) {
deps += [ ":all_host_gcc_mbedtls" ]
}
if (enable_android_builds) {
deps += [
":all_android_arm",
":all_android_arm64",
":all_android_x64",
":all_android_x86",
]
}
if (enable_linux_embedded_build) {
deps += [ ":all_linux_embedded" ]
}
Expand Down
10 changes: 5 additions & 5 deletions gn/build/config/BUILDCONFIG.gn
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@ if (_chip_defaults.custom_toolchain != "") {
}
} else if (target_os == "android") {
if (current_cpu == "arm") {
_default_toolchain = "//build/toolchain/android:armv7_clang"
_default_toolchain = "//build/toolchain/android:android_arm"
} else if (current_cpu == "arm64") {
_default_toolchain = "//build/toolchain/android:aarch64_clang"
} else if (current_cpu == "x86_64") {
_default_toolchain = "//build/toolchain/android:x86_64_clang"
_default_toolchain = "//build/toolchain/android:android_arm64"
} else if (current_cpu == "x64") {
_default_toolchain = "//build/toolchain/android:android_x64"
} else if (current_cpu == "x86") {
_default_toolchain = "//build/toolchain/android:x86_clang"
_default_toolchain = "//build/toolchain/android:android_x86"
} else {
assert(false, "Unsupported target_cpu: ${current_cpu}")
}
Expand Down
18 changes: 18 additions & 0 deletions gn/build/config/android/config.gni
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright (c) 2020 Project CHIP Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

declare_args() {
# Location of the Android NDK.
android_ndk_root = ""
}
4 changes: 2 additions & 2 deletions gn/build/config/android_abi.gni
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
if (target_os == "android") {
if (current_os == "android") {
if (current_cpu == "arm") {
android_abi = "armeabi-v7a"
} else if (current_cpu == "arm64") {
android_abi = "arm64-v8a"
} else if (current_cpu == "x86_64") {
} else if (current_cpu == "x64") {
android_abi = "x86-64"
} else if (current_cpu == "x86") {
android_abi = "x86"
Expand Down
6 changes: 6 additions & 0 deletions gn/build/config/arm.gni
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ if (current_cpu == "arm" || current_cpu == "arm64") {
arm_platform_config = ""
}

if (arm_platform_config == "") {
if (current_os == "android") {
arm_platform_config = "//build/toolchain/android/android_arm.gni"
}
}

# Allow platforms to override how ARM architecture flags are chosen by
# providing a file to import.
if (arm_platform_config != "") {
Expand Down
4 changes: 0 additions & 4 deletions gn/build/config/x86.gni
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ if (current_cpu == "x86" || current_cpu == "x86_64") {
x86_arch = ""
x86_cpu = ""
x86_tune = ""
x86_sysroot = ""

# Update defaults with platform values, if any.
if (x86_platform_config != "") {
Expand All @@ -47,8 +46,5 @@ if (current_cpu == "x86" || current_cpu == "x86_64") {

# ARM tuning (value for -mtune flag).
x86_tune = _defaults.x86_tune

# ARM sysroot (value for --sysroot flag).
x86_sysroot = _defaults.x86_sysroot
}
}
56 changes: 26 additions & 30 deletions gn/build/toolchain/android/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -15,38 +15,34 @@

import("android_toolchain.gni")

if (current_cpu == "arm") {
android_clang_toolchain("armv7_clang") {
# TODO: Drop API level to 21 after getifaddrs issue fixed
api_level = "24"
toolchain_args = {
current_cpu = "arm"
}
android_clang_toolchain("android_arm") {
# TODO: Drop API level to 21 after getifaddrs issue fixed
api_level = "24"
toolchain_args = {
current_cpu = "arm"
}
} else if (current_cpu == "arm64") {
android_clang_toolchain("aarch64_clang") {
# TODO: Drop API level to 21 after getifaddrs issue fixed
api_level = "24"
toolchain_args = {
current_cpu = "arm64"
}
}

android_clang_toolchain("android_arm64") {
# TODO: Drop API level to 21 after getifaddrs issue fixed
api_level = "24"
toolchain_args = {
current_cpu = "arm64"
}
} else if (current_cpu == "x86_64") {
android_clang_toolchain("x86_64_clang") {
# TODO: Drop API level to 21 after getifaddrs issue fixed
api_level = "24"
toolchain_args = {
current_cpu = "x86_64"
}
}

android_clang_toolchain("android_x64") {
# TODO: Drop API level to 21 after getifaddrs issue fixed
api_level = "24"
toolchain_args = {
current_cpu = "x64"
}
} else if (current_cpu == "x86") {
android_clang_toolchain("x86_clang") {
# TODO: Drop API level to 21 after getifaddrs issue fixed
api_level = "24"
toolchain_args = {
current_cpu = "x86"
}
}

android_clang_toolchain("android_x86") {
# TODO: Drop API level to 21 after getifaddrs issue fixed
api_level = "24"
toolchain_args = {
current_cpu = "x86"
}
} else {
assert(false, "Unknown target cpu ${current_cpu}")
}
21 changes: 21 additions & 0 deletions gn/build/toolchain/android/android_arm.gni
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright (c) 2020 Project CHIP Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

if (current_cpu == "arm") {
arm_arch = "armv7-a"
arm_fpu = "vfpv3-d16"
arm_float_abi = "softfp"
} else if (current_cpu == "arm64") {
arm_arch = "armv8-a"
}
34 changes: 12 additions & 22 deletions gn/build/toolchain/android/android_toolchain.gni
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,20 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import("//build/config/android/config.gni")
import("//build/toolchain/gcc_toolchain.gni")

template("android_clang_toolchain") {
_invoker_toolchain_args = invoker.toolchain_args

if (_invoker_toolchain_args.current_cpu == "arm") {
_tool_name_root = "armv7a-linux-androideabi${invoker.api_level}-"
_toolchain_args = {
arm_arch = "armv7-a"
arm_fpu = "vfpv3-d16"
arm_float_abi = "softfp"
}
} else if (_invoker_toolchain_args.current_cpu == "arm64") {
_tool_name_root = "aarch64-linux-android${invoker.api_level}-"
_toolchain_args = {
arm_arch = "armv8-a"
arm_fpu = "vfpv3-d16"
arm_float_abi = "softfp"
}
} else if (_invoker_toolchain_args.current_cpu == "x86_64") {
} else if (_invoker_toolchain_args.current_cpu == "x64") {
_tool_name_root = "x86_64-linux-android${invoker.api_level}-"
_toolchain_args = {
x86_arch = "x86-64"
x86_tune = "intel"
}
} else if (_invoker_toolchain_args.current_cpu == "x86") {
_tool_name_root = "i686-linux-android${invoker.api_level}-"
_toolchain_args = {
x86_arch = "i686"
x86_tune = "intel"
}
} else {
assert(
false,
Expand All @@ -53,14 +36,21 @@ template("android_clang_toolchain") {
current_os = "android"
is_clang = true

forward_variables_from(_toolchain_args, "*")
forward_variables_from(_invoker_toolchain_args, "*")
}

_ndk_prefix = ""
if (android_ndk_root != "") {
if (host_os == "linux" && host_cpu == "x64") {
_ndk_prefix =
"${android_ndk_root}/toolchains/llvm/prebuilt/linux-x86_64/bin/"
}
}

gcc_toolchain(target_name) {
toolchain_args = _android_toolchain_args
ar = "llvm-ar"
cc = _tool_name_root + "clang"
cxx = _tool_name_root + "clang++"
cc = _ndk_prefix + _tool_name_root + "clang"
cxx = _ndk_prefix + _tool_name_root + "clang++"
}
}
9 changes: 6 additions & 3 deletions gn/chip/java/rules.gni
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@ template("java_library") {
}

# Additional flags
_javac_flags = []
_javac_flags = [
"-Werror",
"-Xlint:all",
]
if (defined(invoker.javac_flags)) {
_javac_flags = invoker.javac_flags
_javac_flags += invoker.javac_flags
}

_deps = []
Expand Down Expand Up @@ -83,7 +86,7 @@ template("java_library") {
}

# Bundles all files within the 'class directory' into a jar file
_jar_output = "$root_build_dir/lib/$_jar_name"
_jar_output = "$root_out_dir/lib/$_jar_name"
action(target_name) {
deps = [ ":$_javac_target_name" ] + _deps

Expand Down
2 changes: 1 addition & 1 deletion gn/chip/tools.gni
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@

declare_args() {
# Build CHIP tools.
chip_build_tools = current_os != "freertos"
chip_build_tools = current_os != "freertos" && current_os != "android"
}
14 changes: 13 additions & 1 deletion gn_build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,21 @@ echo 'To build a custom build (for help run "gn args --list out/debug")'
echo gn args "$CHIP_ROOT/out/custom"
echo ninja -C "$CHIP_ROOT/out/custom"

extra_args=""

# Android NDK setup
android_ndk_args=""

if [[ -d "${ANDROID_NDK_HOME}/toolchains/llvm/prebuilt/linux-x86_64/bin" ]]; then
android_ndk_args+="android_ndk_root=\"$ANDROID_NDK_HOME\""
extra_args+=" $android_ndk_args enable_android_builds=true"
else
echo
echo "Hint: Set \$ANDROID_NDK_HOME to enable building for Android"
fi

# nRF5 SDK setup
nrf5_sdk_args=""
extra_args=""

if [[ -d "$NRF5_SDK_ROOT/components/libraries" ]]; then
nrf5_sdk_args+="nrf5_sdk_root=\"$NRF5_SDK_ROOT\""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public void onError(Throwable error) {
System.loadLibrary("CHIPController");
}

@SuppressWarnings("deprecation")
protected void finalize() throws Throwable {
super.finalize();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

/** Exception thrown from CHIPDeviceController. */
public class ChipDeviceControllerException extends Exception {
private static final long serialVersionUID = 1L;

public int errorCode;

Expand Down
1 change: 1 addition & 0 deletions third_party/mbedtls/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ if (mbedtls_target != "") {
"repo/library/md5.c",
"repo/library/md_wrap.c",
"repo/library/oid.c",
"repo/library/padlock.c",
"repo/library/pem.c",
"repo/library/pk.c",
"repo/library/pk_wrap.c",
Expand Down

0 comments on commit fdc49a3

Please sign in to comment.