Skip to content

Commit

Permalink
GN: Enable loading libraries from apks
Browse files Browse the repository at this point in the history
This enables all the bits for an apk that uses uncompressed native
libraries that are loaded directly from the apk without being extracted
on the device.

BUG=359249
TBR=brettw
NOTRY=true

Review URL: https://codereview.chromium.org/654383002

Cr-Commit-Position: refs/heads/master@{#300814}
  • Loading branch information
cjhopman authored and Commit bot committed Oct 23, 2014
1 parent 0d571a8 commit 3151133
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 33 deletions.
3 changes: 2 additions & 1 deletion BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@ group("root") {

if (is_android) {
deps += [
"//base/android:chromium_android_linker",
"//base/android/linker:chromium_android_linker",
"//build/android/rezip",
"//third_party/openmax_dl/dl",
"//content/shell/android:content_shell_apk",
"//chrome/android:chrome_shell_apk",
Expand Down
7 changes: 6 additions & 1 deletion base/android/BUILD.gn → base/android/linker/BUILD.gn
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

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

assert(is_android)
assert(!is_android_webview_build)

# GYP: //base/base.gyp:chromium_android_linker
shared_library("chromium_android_linker") {
sources = [ "linker/linker_jni.cc" ]
sources = [ "linker_jni.cc" ]
# The NDK contains the crazy_linker here:
# '<(android_ndk_root)/crazy_linker.gyp:crazy_linker'
# However, we use our own fork. See bug 384700.
Expand Down
8 changes: 8 additions & 0 deletions base/android/linker/config.gni
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

# TODO(GYP) add "|| profiling_full_stack_frames
# Only enable the chromium linker on regular builds, since the
# component build crashes on Android 4.4. See b/11379966
chromium_linker_supported = !is_component_build
2 changes: 1 addition & 1 deletion base/base.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -1423,7 +1423,7 @@
'includes': [ '../build/java.gypi' ],
},
{
# GN: //base/android/chromium_android_linker
# GN: //base/android/linker:chromium_android_linker
'target_name': 'chromium_android_linker',
'type': 'shared_library',
'conditions': [
Expand Down
1 change: 1 addition & 0 deletions build/android/rezip.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
{
'targets': [
{
# GN: //build/android/rezip:rezip
'target_name': 'rezip_apk_jar',
'type': 'none',
'variables': {
Expand Down
11 changes: 11 additions & 0 deletions build/android/rezip/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import("//build/config/android/rules.gni")

# GYP: //build/android/rezip.gyp:rezip_apk_jar
java_library("rezip") {
jar_path = "$root_build_dir/lib.java/rezip_apk.jar"
DEPRECATED_java_in_dir = "."
}
25 changes: 18 additions & 7 deletions build/config/android/internal_rules.gni
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ template("create_apk") {
_keystore_path = invoker.keystore_path
_keystore_name = invoker.keystore_name
_keystore_password = invoker.keystore_password
_load_library_from_apk = invoker.load_library_from_apk

_deps = []
if (defined(invoker.deps)) {
Expand Down Expand Up @@ -271,7 +272,7 @@ template("create_apk") {

script = "//build/android/gyp/package_resources.py"
depfile = "${target_gen_dir}/${target_name}.d"
source_prereqs = [
inputs = [
_android_manifest,
_resources_zip,
]
Expand Down Expand Up @@ -302,7 +303,7 @@ template("create_apk") {

depfile = "$target_gen_dir/$target_name.d"

source_prereqs = [
inputs = [
_dex_path,
_resource_packaged_apk_path,
_ant_script
Expand Down Expand Up @@ -344,7 +345,7 @@ template("create_apk") {
depfile = "$target_gen_dir/$target_name.d"

sources = [_packaged_apk_path]
source_prereqs = [_keystore_path]
inputs = [_keystore_path]
outputs = [depfile, _final_apk_path]

args = [
Expand All @@ -356,6 +357,16 @@ template("create_apk") {
"--key-name", _keystore_name,
"--key-passwd", _keystore_password,
]
if (_load_library_from_apk) {
_rezip_jar_path = "$root_build_dir/lib.java/rezip_apk.jar"
inputs += [
_rezip_jar_path
]
args += [
"--load-library-from-zip-file=1",
"--rezip-apk-jar-path", rebase_path(_rezip_jar_path, root_build_dir)
]
}
}

group(target_name) {
Expand Down Expand Up @@ -446,7 +457,7 @@ template("java_prebuilt") {
# dependencies srcjar outputs will be compiled and added to the output jar.
# jar_path: Use this to explicitly set the output jar path. Defaults to
# "${target_gen_dir}/${target_name}.jar.
template("java_library") {
template("compile_java") {
if (defined(invoker.testonly)) { testonly = invoker.testonly }

assert(defined(invoker.java_files))
Expand Down Expand Up @@ -598,7 +609,7 @@ template("android_java_library") {
_final_deps = []
_final_datadeps = []

java_library("${target_name}__java_library") {
compile_java("${target_name}__compile_java") {
jar_path = _jar_path
if (defined(invoker.jar_excluded_patterns)) {
jar_excluded_patterns = invoker.jar_excluded_patterns
Expand Down Expand Up @@ -634,7 +645,7 @@ template("android_java_library") {

group(target_name) {
deps = [
":${target_name}__java_library",
":${target_name}__compile_java",
":${target_name}__dex",
] + _final_deps + _final_datadeps
}
Expand Down Expand Up @@ -672,7 +683,7 @@ template("process_resources") {
)
sources = rebase_path(sources_build_rel, ".", root_build_dir)

source_prereqs = [
inputs = [
build_config,
android_manifest,
]
Expand Down
109 changes: 86 additions & 23 deletions build/config/android/rules.gni
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import("//base/android/linker/config.gni")
import("//build/config/android/config.gni")
import("//build/config/android/internal_rules.gni")
import("//tools/grit/grit_rule.gni")
Expand Down Expand Up @@ -732,6 +733,68 @@ template("android_library") {
}
}

template("java_library") {
if (defined(invoker.testonly)) { testonly = invoker.testonly }

assert(defined(invoker.java_files) || defined(invoker.DEPRECATED_java_in_dir)
|| defined(invoker.srcjars))

_srcjar_deps = []
if (defined(invoker.srcjar_deps)) {
_srcjar_deps = invoker.srcjar_deps
}

_srcjars = []
if (defined(invoker.srcjars)) {
_srcjars = invoker.srcjars
}

_java_files = []
if (defined(invoker.java_files)) {
_java_files = invoker.java_files
} else if (defined(invoker.DEPRECATED_java_in_dir)) {
_src_dir = invoker.DEPRECATED_java_in_dir + "/src"
_src_dir_exists = exec_script("//build/dir_exists.py",
[ rebase_path(_src_dir, root_build_dir) ],
"string")
assert(_src_dir_exists == "False",
"In GN, java_in_dir should be the fully specified java directory " +
"(i.e. including the trailing \"/src\")")

_java_files_build_rel = exec_script(
"//build/android/gyp/find.py",
[
"--pattern",
"*.java",
rebase_path(invoker.DEPRECATED_java_in_dir, root_build_dir)
],
"list lines"
)
_java_files = rebase_path(_java_files_build_rel, ".", root_build_dir)
}
assert(_java_files != [] || _srcjar_deps != [] || _srcjars != [])

# TODO(cjhopman): Write a proper build config so that java library
# dependencies work correctly.
_build_config = "$target_gen_dir/$target_name.build_config"
write_file(
_build_config,
"{ \"javac\": { \"classpath\": [], \"srcjars\": [] } }")

_jar_path = "$root_build_dir/lib.java/$target_name.jar"
if (defined(invoker.jar_path)) {
_jar_path = invoker.jar_path
}

compile_java(target_name) {
build_config = _build_config
jar_path = _jar_path
java_files = _java_files
srcjar_deps = _srcjar_deps
srcjars = _srcjars
}
}


# Declare an Android library target for a prebuilt jar
#
Expand Down Expand Up @@ -875,28 +938,6 @@ template("android_apk") {
_dist_jar_path = _dist_jar_path_list[0]

_native_libs = []
if (defined(invoker.native_libs)) {
_use_chromium_linker = false
if (defined(invoker.use_chromium_linker)) {
_use_chromium_linker = invoker.use_chromium_linker
}

# TODO(GYP) add "|| profiling_full_stack_frames
# Only enable the chromium linker on regular builds, since the
# component build crashes on Android 4.4. See b/11379966
if (is_component_build || cpu_arch == "arm64" || cpu_arch == "x64") {
_use_chromium_linker = false
}

_native_libs = invoker.native_libs
_native_libs_dir = base_path + "/libs"

if (_use_chromium_linker) {
_native_libs += [
"$root_build_dir/lib.stripped/libchromium_android_linker.so"
]
}
}

_keystore_path = android_default_keystore_path
_keystore_name = android_default_keystore_name
Expand All @@ -913,6 +954,28 @@ template("android_apk") {
_srcjar_deps += invoker.srcjar_deps
}

_load_library_from_apk = false
if (defined(invoker.load_library_from_apk)) {
_load_library_from_apk = invoker.load_library_from_apk
}

if (defined(invoker.native_libs)) {
_use_chromium_linker = false
if (defined(invoker.use_chromium_linker)) {
_use_chromium_linker = (invoker.use_chromium_linker &&
chromium_linker_supported)
}

_native_libs = invoker.native_libs
_native_libs_dir = base_path + "/libs"

if (_use_chromium_linker) {
_native_libs += [
"$root_build_dir/lib.stripped/libchromium_android_linker.so"
]
}
}

_rebased_build_config = rebase_path(build_config, root_build_dir)

write_build_config("${_template_name}__build_config") {
Expand Down Expand Up @@ -945,7 +1008,6 @@ template("android_apk") {
_enable_chromium_linker_tests = invoker.enable_chromium_linker_tests
}

_load_library_from_apk = false
_native_lib_version_name = ""

java_cpp_template("${_template_name}__native_libraries_java") {
Expand Down Expand Up @@ -1049,6 +1111,7 @@ template("android_apk") {
android_manifest = invoker.android_manifest
resources_zip = all_resources_zip_path
dex_path = final_dex_path
load_library_from_apk = _load_library_from_apk

if (defined(invoker.asset_location)) {
asset_location = invoker.asset_location
Expand Down

0 comments on commit 3151133

Please sign in to comment.