Skip to content

Commit 81b2abc

Browse files
committed
Add explicit Android Bazel repositories
1 parent 55ed42c commit 81b2abc

2 files changed

Lines changed: 17 additions & 1 deletion

File tree

.github/workflows/build-android-arm64.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,13 @@ jobs:
5858
shell: bash
5959
run: |
6060
cat > tfjava/tensorflow-core/tensorflow-core-api/.tf_configure.bazelrc <<EOF
61+
build --action_env=ANDROID_HOME=${ANDROID_SDK_ROOT}
6162
build --action_env=ANDROID_NDK_HOME=${ANDROID_SDK_ROOT}/ndk/${ANDROID_NDK_VERSION}
6263
build --action_env=ANDROID_NDK_API_LEVEL=${ANDROID_NDK_API_LEVEL}
6364
build --action_env=ANDROID_SDK_HOME=${ANDROID_SDK_ROOT}
6465
build --action_env=ANDROID_SDK_API_LEVEL=${ANDROID_API_LEVEL}
6566
build --action_env=ANDROID_BUILD_TOOLS_VERSION=${ANDROID_BUILD_TOOLS_VERSION}
67+
build --repo_env=ANDROID_HOME=${ANDROID_SDK_ROOT}
6668
build --repo_env=ANDROID_NDK_HOME=${ANDROID_SDK_ROOT}/ndk/${ANDROID_NDK_VERSION}
6769
build --repo_env=ANDROID_NDK_API_LEVEL=${ANDROID_NDK_API_LEVEL}
6870
build --repo_env=ANDROID_SDK_HOME=${ANDROID_SDK_ROOT}
@@ -78,6 +80,8 @@ jobs:
7880
echo '---'
7981
grep -n 'PLATFORM>${javacpp.platform}' tensorflow-core/tensorflow-core-api/pom.xml || true
8082
echo '---'
83+
grep -n 'androidndk\\|androidsdk' tensorflow-core/tensorflow-core-api/WORKSPACE || true
84+
echo '---'
8185
grep -n 'android-arm64' tensorflow-core/tensorflow-core-api/src/main/java/org/tensorflow/internal/c_api/presets/tensorflow.java || true
8286
echo '---'
8387
cat tensorflow-core/tensorflow-core-api/.tf_configure.bazelrc
@@ -87,6 +91,7 @@ jobs:
8791
run: |
8892
set -o pipefail
8993
export ANDROID_HOME="${ANDROID_SDK_ROOT}"
94+
export ANDROID_SDK_HOME="${ANDROID_SDK_ROOT}"
9095
export ANDROID_NDK_HOME="${ANDROID_SDK_ROOT}/ndk/${ANDROID_NDK_VERSION}"
9196
export ANDROID_NDK_ROOT="${ANDROID_NDK_HOME}"
9297
export ANDROID_SDK_API_LEVEL="${ANDROID_API_LEVEL}"

scripts/patch_tfjava.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def patch_module_pom(path: Path) -> None:
2323
def patch_build_sh(path: Path) -> None:
2424
text = path.read_text(encoding="utf-8")
2525
old = """# Allows us to use ccache with Bazel on Mac\nexport BAZEL_USE_CPP_ONLY_TOOLCHAIN=1\n\nexport BAZEL_VC=\"${VCINSTALLDIR:-}\"\nif [[ -d $BAZEL_VC ]]; then\n # Work around compiler issues on Windows documented mainly in configure.py but also elsewhere\n export BUILD_FLAGS=\"--copt=//arch:AVX `#--copt=//arch:AVX2` --copt=-DWIN32_LEAN_AND_MEAN --host_copt=-DWIN32_LEAN_AND_MEAN --copt=-DNOGDI --host_copt=-DNOGDI --copt=-D_USE_MATH_DEFINES --host_copt=-D_USE_MATH_DEFINES --define=override_eigen_strong_inline=true\"\n # https://software.intel.com/en-us/articles/intel-optimization-for-tensorflow-installation-guide#wind_B_S\n export PATH=$PATH:$(pwd)/bazel-tensorflow-core-api/external/mkl_windows/lib/\n export PYTHON_BIN_PATH=$(which python.exe)\nelse\n export BUILD_FLAGS=\"--copt=-msse4.1 --copt=-msse4.2 --copt=-mavx `#--copt=-mavx2 --copt=-mfma` --cxxopt=-std=c++14 --host_cxxopt=-std=c++14 --linkopt=-lstdc++ --host_linkopt=-lstdc++\"\n export PYTHON_BIN_PATH=$(which python3)\nfi\n"""
26-
new = """# Allows us to use ccache with Bazel on Mac, but Android needs Bazel's Android crosstool.\nif [[ \"${PLATFORM:-}\" != \"android-arm64\" ]]; then\n export BAZEL_USE_CPP_ONLY_TOOLCHAIN=1\nfi\n\nexport BAZEL_VC=\"${VCINSTALLDIR:-}\"\nif [[ \"${PLATFORM:-}\" == \"android-arm64\" ]]; then\n export BUILD_FLAGS=\"--config=android_arm64 --cxxopt=-std=c++14 --host_cxxopt=-std=c++14\"\n export PYTHON_BIN_PATH=$(which python3)\nelif [[ -d $BAZEL_VC ]]; then\n # Work around compiler issues on Windows documented mainly in configure.py but also elsewhere\n export BUILD_FLAGS=\"--copt=//arch:AVX `#--copt=//arch:AVX2` --copt=-DWIN32_LEAN_AND_MEAN --host_copt=-DWIN32_LEAN_AND_MEAN --copt=-DNOGDI --host_copt=-DNOGDI --copt=-D_USE_MATH_DEFINES --host_copt=-D_USE_MATH_DEFINES --define=override_eigen_strong_inline=true\"\n # https://software.intel.com/en-us/articles/intel-optimization-for-tensorflow-installation-guide#wind_B_S\n export PATH=$PATH:$(pwd)/bazel-tensorflow-core-api/external/mkl_windows/lib/\n export PYTHON_BIN_PATH=$(which python.exe)\nelse\n export BUILD_FLAGS=\"--copt=-msse4.1 --copt=-msse4.2 --copt=-mavx `#--copt=-mavx2 --copt=-mfma` --cxxopt=-std=c++14 --host_cxxopt=-std=c++14 --linkopt=-lstdc++ --host_linkopt=-lstdc++\"\n export PYTHON_BIN_PATH=$(which python3)\nfi\n"""
26+
new = """# Allows us to use ccache with Bazel on Mac, but Android needs Bazel's Android crosstool.\nif [[ \"${PLATFORM:-}\" != \"android-arm64\" ]]; then\n export BAZEL_USE_CPP_ONLY_TOOLCHAIN=1\nfi\n\nexport BAZEL_VC=\"${VCINSTALLDIR:-}\"\nif [[ \"${PLATFORM:-}\" == \"android-arm64\" ]]; then\n export BUILD_FLAGS=\"--crosstool_top=@androidndk//:default_crosstool --cpu=arm64-v8a --fat_apk_cpu=arm64-v8a --host_crosstool_top=@bazel_tools//tools/cpp:toolchain --cxxopt=-std=c++14 --host_cxxopt=-std=c++14\"\n export PYTHON_BIN_PATH=$(which python3)\nelif [[ -d $BAZEL_VC ]]; then\n # Work around compiler issues on Windows documented mainly in configure.py but also elsewhere\n export BUILD_FLAGS=\"--copt=//arch:AVX `#--copt=//arch:AVX2` --copt=-DWIN32_LEAN_AND_MEAN --host_copt=-DWIN32_LEAN_AND_MEAN --copt=-DNOGDI --host_copt=-DNOGDI --copt=-D_USE_MATH_DEFINES --host_copt=-D_USE_MATH_DEFINES --define=override_eigen_strong_inline=true\"\n # https://software.intel.com/en-us/articles/intel-optimization-for-tensorflow-installation-guide#wind_B_S\n export PATH=$PATH:$(pwd)/bazel-tensorflow-core-api/external/mkl_windows/lib/\n export PYTHON_BIN_PATH=$(which python.exe)\nelse\n export BUILD_FLAGS=\"--copt=-msse4.1 --copt=-msse4.2 --copt=-mavx `#--copt=-mavx2 --copt=-mfma` --cxxopt=-std=c++14 --host_cxxopt=-std=c++14 --linkopt=-lstdc++ --host_linkopt=-lstdc++\"\n export PYTHON_BIN_PATH=$(which python3)\nfi\n"""
2727
text = replace_once(text, old, new, path)
2828
path.write_text(text, encoding="utf-8")
2929

@@ -38,12 +38,23 @@ def patch_preset(path: Path) -> None:
3838
path.write_text(text, encoding="utf-8")
3939

4040

41+
def patch_workspace(path: Path) -> None:
42+
text = path.read_text(encoding="utf-8")
43+
if "android_ndk_repository(" in text or "android_sdk_repository(" in text:
44+
return
45+
old = 'bazel_version_repository(name = "bazel_version")\n'
46+
new = """bazel_version_repository(name = "bazel_version")\n\nandroid_sdk_repository(\n name = "androidsdk",\n api_level = 29,\n build_tools_version = "29.0.3",\n)\n\nandroid_ndk_repository(\n name = "androidndk",\n api_level = 21,\n)\n"""
47+
text = replace_once(text, old, new, path)
48+
path.write_text(text, encoding="utf-8")
49+
50+
4151
def main() -> None:
4252
if len(sys.argv) != 2:
4353
raise SystemExit("usage: patch_tfjava.py <tfjava-root>")
4454
root = Path(sys.argv[1]).resolve()
4555
patch_module_pom(root / "tensorflow-core" / "tensorflow-core-api" / "pom.xml")
4656
patch_build_sh(root / "tensorflow-core" / "tensorflow-core-api" / "build.sh")
57+
patch_workspace(root / "tensorflow-core" / "tensorflow-core-api" / "WORKSPACE")
4758
patch_preset(
4859
root
4960
/ "tensorflow-core"

0 commit comments

Comments
 (0)