Skip to content

Commit 55ed42c

Browse files
committed
Fix Android Bazel toolchain setup
1 parent 562e5a9 commit 55ed42c

2 files changed

Lines changed: 37 additions & 9 deletions

File tree

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

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ jobs:
1313
env:
1414
TFJAVA_COMMIT: c065b70c05acefe703fee21a156561e76e54c66d
1515
ANDROID_PLATFORM: android-arm64
16-
ANDROID_API_LEVEL: "33"
16+
ANDROID_API_LEVEL: "29"
17+
ANDROID_BUILD_TOOLS_VERSION: "29.0.3"
1718
ANDROID_NDK_API_LEVEL: "21"
18-
ANDROID_NDK_VERSION: 27.3.13750724
19+
ANDROID_NDK_VERSION: 18.1.5063045
1920
USE_BAZEL_VERSION: 2.0.0
2021
steps:
2122
- uses: actions/checkout@v4
@@ -32,7 +33,7 @@ jobs:
3233
sdkmanager \
3334
"platform-tools" \
3435
"platforms;android-${ANDROID_API_LEVEL}" \
35-
"build-tools;34.0.0" \
36+
"build-tools;${ANDROID_BUILD_TOOLS_VERSION}" \
3637
"ndk;${ANDROID_NDK_VERSION}"
3738
3839
- name: Set Up Java 11
@@ -53,6 +54,22 @@ jobs:
5354
run: |
5455
python3 scripts/patch_tfjava.py tfjava
5556
57+
- name: Write TensorFlow Configure Overrides
58+
shell: bash
59+
run: |
60+
cat > tfjava/tensorflow-core/tensorflow-core-api/.tf_configure.bazelrc <<EOF
61+
build --action_env=ANDROID_NDK_HOME=${ANDROID_SDK_ROOT}/ndk/${ANDROID_NDK_VERSION}
62+
build --action_env=ANDROID_NDK_API_LEVEL=${ANDROID_NDK_API_LEVEL}
63+
build --action_env=ANDROID_SDK_HOME=${ANDROID_SDK_ROOT}
64+
build --action_env=ANDROID_SDK_API_LEVEL=${ANDROID_API_LEVEL}
65+
build --action_env=ANDROID_BUILD_TOOLS_VERSION=${ANDROID_BUILD_TOOLS_VERSION}
66+
build --repo_env=ANDROID_NDK_HOME=${ANDROID_SDK_ROOT}/ndk/${ANDROID_NDK_VERSION}
67+
build --repo_env=ANDROID_NDK_API_LEVEL=${ANDROID_NDK_API_LEVEL}
68+
build --repo_env=ANDROID_SDK_HOME=${ANDROID_SDK_ROOT}
69+
build --repo_env=ANDROID_SDK_API_LEVEL=${ANDROID_API_LEVEL}
70+
build --repo_env=ANDROID_BUILD_TOOLS_VERSION=${ANDROID_BUILD_TOOLS_VERSION}
71+
EOF
72+
5673
- name: Show Patched Snippets
5774
shell: bash
5875
run: |
@@ -62,6 +79,8 @@ jobs:
6279
grep -n 'PLATFORM>${javacpp.platform}' tensorflow-core/tensorflow-core-api/pom.xml || true
6380
echo '---'
6481
grep -n 'android-arm64' tensorflow-core/tensorflow-core-api/src/main/java/org/tensorflow/internal/c_api/presets/tensorflow.java || true
82+
echo '---'
83+
cat tensorflow-core/tensorflow-core-api/.tf_configure.bazelrc
6584
6685
- name: Build Android ARM64 Native Libraries
6786
shell: bash
@@ -71,12 +90,17 @@ jobs:
7190
export ANDROID_NDK_HOME="${ANDROID_SDK_ROOT}/ndk/${ANDROID_NDK_VERSION}"
7291
export ANDROID_NDK_ROOT="${ANDROID_NDK_HOME}"
7392
export ANDROID_SDK_API_LEVEL="${ANDROID_API_LEVEL}"
93+
export ANDROID_BUILD_TOOLS_VERSION="${ANDROID_BUILD_TOOLS_VERSION}"
7494
export ANDROID_NDK_API_LEVEL="${ANDROID_NDK_API_LEVEL}"
7595
export JAVA_HOME="${JAVA_HOME_11_X64}"
96+
export LANG="C.UTF-8"
97+
export LC_ALL="C.UTF-8"
7698
export PATH="${JAVA_HOME}/bin:${PATH}"
7799
mkdir -p "${GITHUB_WORKSPACE}/artifacts"
78100
cd tfjava
79101
mvn -B -e \
102+
-Dproject.build.sourceEncoding=UTF-8 \
103+
-Dproject.reporting.outputEncoding=UTF-8 \
80104
-DskipTests \
81105
-Djavacpp.platform="${ANDROID_PLATFORM}" \
82106
-Djavacpp.platform.properties="${ANDROID_PLATFORM}" \

scripts/patch_tfjava.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,28 @@ def replace_once(text: str, old: str, new: str, path: Path) -> str:
1212

1313
def patch_module_pom(path: Path) -> None:
1414
text = path.read_text(encoding="utf-8")
15-
old = """ <environmentVariables>\n <EXTENSION>${javacpp.platform.extension}</EXTENSION>\n </environmentVariables>"""
16-
new = """ <environmentVariables>\n <EXTENSION>${javacpp.platform.extension}</EXTENSION>\n <PLATFORM>${javacpp.platform}</PLATFORM>\n </environmentVariables>"""
15+
if "<PLATFORM>${javacpp.platform}</PLATFORM>" in text:
16+
return
17+
old = "<EXTENSION>${javacpp.platform.extension}</EXTENSION>"
18+
new = """<EXTENSION>${javacpp.platform.extension}</EXTENSION>\n <PLATFORM>${javacpp.platform}</PLATFORM>"""
1719
text = replace_once(text, old, new, path)
1820
path.write_text(text, encoding="utf-8")
1921

2022

2123
def patch_build_sh(path: Path) -> None:
2224
text = path.read_text(encoding="utf-8")
23-
old = """export 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"""
24-
new = """export 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"""
25+
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"""
2527
text = replace_once(text, old, new, path)
2628
path.write_text(text, encoding="utf-8")
2729

2830

2931
def patch_preset(path: Path) -> None:
3032
text = path.read_text(encoding="utf-8")
31-
old = """ @Platform(\n value = {\"linux\", \"macosx\", \"windows\"},\n compiler = \"cpp11\",\n include = {\n \"tensorflow/c/tf_attrtype.h\",\n \"tensorflow/c/tf_datatype.h\",\n \"tensorflow/c/tf_status.h\",\n \"tensorflow/c/tf_tensor.h\",\n \"tensorflow/c/c_api.h\",\n// \"tensorflow/c/env.h\",\n \"tensorflow/c/kernels.h\",\n \"tensorflow/c/ops.h\",\n \"tensorflow/c/eager/c_api.h\"\n },\n link = \"tensorflow@.2\",\n preload = {\"iomp5\", \"mklml\", \"mklml_intel\", \"tensorflow_framework@.2\"},\n preloadresource = \"/org/bytedeco/mkldnn/\",\n resource = {\"LICENSE\", \"THIRD_PARTY_TF_JNI_LICENSES\"}\n ),\n"""
32-
new = """ @Platform(\n value = {\"linux\", \"macosx\", \"windows\"},\n compiler = \"cpp11\",\n include = {\n \"tensorflow/c/tf_attrtype.h\",\n \"tensorflow/c/tf_datatype.h\",\n \"tensorflow/c/tf_status.h\",\n \"tensorflow/c/tf_tensor.h\",\n \"tensorflow/c/c_api.h\",\n// \"tensorflow/c/env.h\",\n \"tensorflow/c/kernels.h\",\n \"tensorflow/c/ops.h\",\n \"tensorflow/c/eager/c_api.h\"\n },\n link = \"tensorflow@.2\",\n preload = {\"iomp5\", \"mklml\", \"mklml_intel\", \"tensorflow_framework@.2\"},\n preloadresource = \"/org/bytedeco/mkldnn/\",\n resource = {\"LICENSE\", \"THIRD_PARTY_TF_JNI_LICENSES\"}\n ),\n @Platform(\n value = \"android-arm64\",\n compiler = \"cpp11\",\n include = {\n \"tensorflow/c/tf_attrtype.h\",\n \"tensorflow/c/tf_datatype.h\",\n \"tensorflow/c/tf_status.h\",\n \"tensorflow/c/tf_tensor.h\",\n \"tensorflow/c/c_api.h\",\n// \"tensorflow/c/env.h\",\n \"tensorflow/c/kernels.h\",\n \"tensorflow/c/ops.h\",\n \"tensorflow/c/eager/c_api.h\"\n },\n link = \"tensorflow@.2\",\n preload = {\"tensorflow_framework@.2\"},\n resource = {\"LICENSE\", \"THIRD_PARTY_TF_JNI_LICENSES\"}\n ),\n"""
33+
if 'value = "android-arm64"' in text:
34+
return
35+
old = """ preloadresource = "/org/bytedeco/mkldnn/",\n resource = {"LICENSE", "THIRD_PARTY_TF_JNI_LICENSES"}\n ),\n"""
36+
new = """ preloadresource = "/org/bytedeco/mkldnn/",\n resource = {"LICENSE", "THIRD_PARTY_TF_JNI_LICENSES"}\n ),\n @Platform(\n value = "android-arm64",\n compiler = "cpp11",\n include = {\n "tensorflow/c/tf_attrtype.h",\n "tensorflow/c/tf_datatype.h",\n "tensorflow/c/tf_status.h",\n "tensorflow/c/tf_tensor.h",\n "tensorflow/c/c_api.h",\n// "tensorflow/c/env.h",\n "tensorflow/c/kernels.h",\n "tensorflow/c/ops.h",\n "tensorflow/c/eager/c_api.h"\n },\n link = "tensorflow@.2",\n preload = {"tensorflow_framework@.2"},\n resource = {"LICENSE", "THIRD_PARTY_TF_JNI_LICENSES"}\n ),\n"""
3337
text = replace_once(text, old, new, path)
3438
path.write_text(text, encoding="utf-8")
3539

0 commit comments

Comments
 (0)