android-ndk.configure 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. # -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*-
  2. # This Source Code Form is subject to the terms of the Mozilla Public
  3. # License, v. 2.0. If a copy of the MPL was not distributed with this
  4. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
  5. js_option('--with-android-ndk', nargs=1,
  6. help='location where the Android NDK can be found')
  7. js_option('--with-android-toolchain', nargs=1,
  8. help='location of the Android toolchain')
  9. js_option('--with-android-gnu-compiler-version', nargs=1,
  10. help='GNU compiler version to use')
  11. min_android_version = dependable(lambda: '9')
  12. js_option('--with-android-version',
  13. nargs=1,
  14. help='android platform version',
  15. default=min_android_version)
  16. @depends('--with-android-version', min_android_version)
  17. @imports(_from='__builtin__', _import='ValueError')
  18. def android_version(value, min_version):
  19. if not value:
  20. # Someone has passed --without-android-version.
  21. die('--with-android-version cannot be disabled.')
  22. try:
  23. version = int(value[0])
  24. except ValueError:
  25. die('--with-android-version expects an integer value')
  26. if version < int(min_version):
  27. die('--with-android-version must be at least %s (got %s)',
  28. min_version, value[0])
  29. return version
  30. add_old_configure_assignment('android_version', android_version)
  31. @depends('--with-android-ndk', build_project)
  32. def ndk(value, build_project):
  33. if build_project == 'mobile/android' and not value:
  34. die('You must specify --with-android-ndk=/path/to/ndk when '
  35. 'building mobile/android')
  36. if value:
  37. return value[0]
  38. set_config('ANDROID_NDK', ndk)
  39. add_old_configure_assignment('android_ndk', ndk)
  40. @depends(target, android_version, ndk)
  41. @checking('for android platform directory')
  42. @imports(_from='os.path', _import='isdir')
  43. def android_platform(target, android_version, ndk):
  44. if target.os != 'Android':
  45. return
  46. if 'mips' in target.cpu:
  47. target_dir_name = 'mips'
  48. elif 'aarch64' == target.cpu:
  49. target_dir_name = 'arm64'
  50. else:
  51. target_dir_name = target.cpu
  52. # Not all Android releases have their own platform release. We use
  53. # the next lower platform version in these cases.
  54. if android_version in (11, 10):
  55. platform_version = 9
  56. elif android_version in (20, 22):
  57. platform_version = android_version - 1
  58. else:
  59. platform_version = android_version
  60. platform_dir = os.path.join(ndk,
  61. 'platforms',
  62. 'android-%s' % platform_version,
  63. 'arch-%s' % target_dir_name)
  64. if not isdir(platform_dir):
  65. die("Android platform directory not found. With the current "
  66. "configuration, it should be in %s" % platform_dir)
  67. return platform_dir
  68. add_old_configure_assignment('android_platform', android_platform)
  69. @depends(android_platform)
  70. def extra_toolchain_flags(platform_dir):
  71. if not platform_dir:
  72. return []
  73. return ['-idirafter',
  74. os.path.join(platform_dir, 'usr', 'include')]
  75. @depends(target, host, ndk, '--with-android-toolchain',
  76. '--with-android-gnu-compiler-version')
  77. @checking('for the Android toolchain directory', lambda x: x or 'not found')
  78. @imports(_from='os.path', _import='isdir')
  79. @imports(_from='mozbuild.shellutil', _import='quote')
  80. def android_toolchain(target, host, ndk, toolchain, gnu_compiler_version):
  81. if not ndk:
  82. return
  83. if toolchain:
  84. return toolchain[0]
  85. else:
  86. if target.cpu == 'arm' and target.endianness == 'little':
  87. target_base = 'arm-linux-androideabi'
  88. elif target.cpu == 'x86':
  89. target_base = 'x86'
  90. elif target.cpu == 'mips32' and target.endianness == 'little':
  91. target_base = 'mipsel-linux-android'
  92. elif target.cpu == 'aarch64' and target.endianness == 'little':
  93. target_base = 'aarch64-linux-android'
  94. else:
  95. die('Target cpu is not supported.')
  96. toolchain_format = '%s/toolchains/%s-%s/prebuilt/%s-%s'
  97. for version in gnu_compiler_version or ['4.9', '4.8', '4.7']:
  98. toolchain = toolchain_format % (ndk, target_base, version,
  99. host.kernel.lower(), host.cpu)
  100. log.debug('Trying %s' % quote(toolchain))
  101. if not isdir(toolchain) and host.cpu == 'x86_64':
  102. toolchain = toolchain_format % (ndk, target_base, version,
  103. host.kernel.lower(), 'x86')
  104. log.debug('Trying %s' % quote(toolchain))
  105. if isdir(toolchain):
  106. return toolchain
  107. else:
  108. if gnu_compiler_version:
  109. die('Your --with-android-gnu-compiler-version may be wrong')
  110. die('You have to specify --with-android-toolchain='
  111. '/path/to/ndk/toolchain.')
  112. set_config('ANDROID_TOOLCHAIN', android_toolchain)
  113. @depends(target, android_toolchain)
  114. def android_toolchain_prefix(target, toolchain):
  115. if toolchain:
  116. if target.cpu == 'x86':
  117. # Ideally, the --target should just have the right x86 variant
  118. # in the first place.
  119. return '%s/bin/i686-linux-android-' % toolchain
  120. return '%s/bin/%s-' % (toolchain, target.toolchain)
  121. imply_option('--with-toolchain-prefix', android_toolchain_prefix,
  122. reason='--with-android-ndk')