Skip to content

Commit 58101a7

Browse files
Improve handling of NDK environment variables
This addresses two potential issues: 1) If NDK_ROOT and ANDROID_NDK_HOME are both set, but disagree. In principle, one env var could be used by one or more tools, with the other used by other tools. If that happens, debugging any issues related to NDK version could be very difficult. To avoid this, once we determine the NDK to use, we override the other env vars. NDK_ROOT is taken as the priority over ANDROID_NDK_HOME: the reason for this is that the build scripts set and use NDK_ROOT. 2) No NDK env var is set, despite the NDK being present. This happens e.g. on Github Ubuntu VMs. In this case, we check the default location for the NDK, which is the ndk-bundle directory of the Android SDK, which we look for in the ANDROID_HOME env var.
1 parent 95c8d28 commit 58101a7

File tree

1 file changed

+36
-21
lines changed

1 file changed

+36
-21
lines changed

scripts/gha/build_testapps.py

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ def main(argv):
195195

196196
if update_pod_repo and _IOS in platforms:
197197
_run(["pod", "repo", "update"])
198-
198+
199199
config = config_reader.read_config()
200200
cmake_flags = _get_desktop_compiler_flags(FLAGS.compiler, config.compilers)
201201
if _DESKTOP in platforms and FLAGS.use_vcpkg:
@@ -226,7 +226,7 @@ def main(argv):
226226

227227

228228
def _build(
229-
testapp, platforms, api_config, output_dir, sdk_dir, ios_framework_exist,
229+
testapp, platforms, api_config, output_dir, sdk_dir, ios_framework_exist,
230230
timestamp, root_dir, ios_sdk, cmake_flags, execute_desktop_testapp):
231231
"""Builds one testapp on each of the specified platforms."""
232232
testapp_dir = os.path.join(root_dir, api_config.testapp_path)
@@ -362,31 +362,45 @@ def _validate_android_environment_variables():
362362
"""Checks environment variables that may be required for Android."""
363363
# Ultimately we let the gradle build be the source of truth on what env vars
364364
# are required, but try to repair holes and log warnings if we can't.
365-
logging.info("Checking environment variables for the Android build")
366-
if not os.environ.get(_ANDROID_NDK_HOME):
367-
ndk_root = os.environ.get(_NDK_ROOT)
368-
if ndk_root: # Use NDK_ROOT as a backup for ANDROID_NDK_HOME
369-
os.environ[_ANDROID_NDK_HOME] = ndk_root
370-
logging.info("%s not found, using %s", _ANDROID_NDK_HOME, _NDK_ROOT)
371-
else:
372-
logging.warning("Neither %s nor %s is set.", _ANDROID_NDK_HOME, _NDK_ROOT)
365+
android_home = os.environ.get(_ANDROID_HOME)
373366
if not os.environ.get(_JAVA_HOME):
374367
logging.warning("%s not set", _JAVA_HOME)
375368
if not os.environ.get(_ANDROID_SDK_HOME):
376-
android_home = os.environ.get(_ANDROID_HOME)
377369
if android_home: # Use ANDROID_HOME as backup for ANDROID_SDK_HOME
378370
os.environ[_ANDROID_SDK_HOME] = android_home
379371
logging.info("%s not found, using %s", _ANDROID_SDK_HOME, _ANDROID_HOME)
380372
else:
381-
logging.warning(
382-
"Neither %s nor %s is set", _ANDROID_SDK_HOME, _ANDROID_HOME)
373+
logging.warning("Missing: %s and %s", _ANDROID_SDK_HOME, _ANDROID_HOME)
374+
# Different environments may have different NDK env vars specified. We look
375+
# for these, in this order, and set the others to the first found.
376+
# If none are set, we check the default location for the ndk.
377+
ndk_path = None
378+
ndk_vars = [_NDK_ROOT, _ANDROID_NDK_HOME]
379+
for env_var in ndk_vars:
380+
val = os.environ.get(env_var)
381+
if val:
382+
ndk_path = val
383+
break
384+
if not ndk_path:
385+
if android_home:
386+
default_ndk_path = os.path.join(android_home, "ndk-bundle")
387+
if os.path.isdir(default_ndk_path):
388+
ndk_path = default_ndk_path
389+
if ndk_path:
390+
logging.info("Found ndk: %s", ndk_path)
391+
for env_var in ndk_vars:
392+
if os.environ.get(env_var) != ndk_path:
393+
logging.info("Setting %s to %s", env_var, ndk_path)
394+
os.environ[env_var] = ndk_path
395+
else:
396+
logging.warning("No NDK env var set. Set one of %s", ", ".join(ndk_vars))
383397

384398

385-
# If sdk_dir contains no framework, consider it is Github repo, then
399+
# If sdk_dir contains no framework, consider it is Github repo, then
386400
# generate makefiles for ios frameworks
387401
def _generate_makefiles_from_repo(sdk_dir):
388402
ios_framework_builder = os.path.join(
389-
sdk_dir, "build_scripts", "ios", "build.sh")
403+
sdk_dir, "build_scripts", "ios", "build.sh")
390404

391405
framework_builder_args = [
392406
ios_framework_builder,
@@ -400,15 +414,15 @@ def _generate_makefiles_from_repo(sdk_dir):
400414
# build required ios frameworks based on makefiles
401415
def _build_ios_framework_from_repo(sdk_dir, api_config):
402416
ios_framework_builder = os.path.join(
403-
sdk_dir, "build_scripts", "ios", "build.sh")
404-
417+
sdk_dir, "build_scripts", "ios", "build.sh")
418+
405419
# build only required targets to save time
406420
target = set()
407421
for framework in api_config.frameworks:
408422
target.add(os.path.splitext(framework)[0])
409423
# firebase is not a target in CMake, firebase_app is the target
410-
# firebase_app will be built by other target as well
411-
target.remove("firebase")
424+
# firebase_app will be built by other target as well
425+
target.remove("firebase")
412426

413427
framework_builder_args = [
414428
ios_framework_builder,
@@ -421,10 +435,10 @@ def _build_ios_framework_from_repo(sdk_dir, api_config):
421435

422436
def _build_ios(
423437
sdk_dir, ios_framework_exist, project_dir, root_dir, api_config, ios_sdk):
438+
"""Builds an iOS application (.app, .ipa or both)."""
424439
if not ios_framework_exist:
425440
_build_ios_framework_from_repo(sdk_dir, api_config)
426441

427-
"""Builds an iOS application (.app, .ipa or both)."""
428442
build_dir = os.path.join(project_dir, "ios_build")
429443
os.makedirs(build_dir)
430444

@@ -475,7 +489,8 @@ def _build_ios(
475489
ios_sdk=_IOS_SDK_DEVICE,
476490
configuration="Debug"))
477491

478-
xcodebuild.generate_unsigned_ipa(output_dir=build_dir, configuration="Debug")
492+
xcodebuild.generate_unsigned_ipa(
493+
output_dir=build_dir, configuration="Debug")
479494

480495

481496
# This script is responsible for copying shared files into the integration

0 commit comments

Comments
 (0)