build_rs.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. #!/usr/bin/env python
  2. #
  3. # Copyright (C) 2016 The Android Open Source Project
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. #
  17. from __future__ import print_function
  18. import argparse
  19. import glob
  20. import multiprocessing
  21. import os
  22. import shutil
  23. import subprocess
  24. import sys
  25. import re
  26. THIS_DIR = os.path.realpath(os.path.dirname(__file__))
  27. ORIG_ENV = dict(os.environ)
  28. def android_path(*args):
  29. out_dir = os.path.realpath(os.path.join(THIS_DIR, '../..', *args))
  30. return out_dir
  31. def build_path(*args):
  32. # Our multistage build directories will be placed under OUT_DIR if it is in
  33. # the environment. By default they will be placed under
  34. # $ANDROID_BUILD_TOP/out.
  35. top_out = ORIG_ENV.get('OUT_DIR', android_path('out'))
  36. if not os.path.isabs(top_out):
  37. top_out = os.path.realpath(top_out)
  38. out_dir = os.path.join(top_out, *args)
  39. return out_dir
  40. def install_file(src, dst):
  41. print('Copying ' + src)
  42. shutil.copy2(src, dst)
  43. def install_directory(src, dst):
  44. print('Copying ' + src)
  45. shutil.copytree(src, dst)
  46. def build(out_dir):
  47. products = (
  48. 'aosp_arm',
  49. 'aosp_arm64',
  50. # 'aosp_mips',
  51. # 'aosp_mips64',
  52. 'aosp_x86',
  53. 'aosp_x86_64',
  54. )
  55. for product in products:
  56. build_product(out_dir, product)
  57. def build_product(out_dir, product):
  58. env = dict(ORIG_ENV)
  59. env['FORCE_BUILD_LLVM_COMPONENTS'] = 'true'
  60. env['FORCE_BUILD_RS_COMPAT'] = 'true'
  61. env['OUT_DIR'] = out_dir
  62. env['SKIP_LLVM_TESTS'] = 'true'
  63. env['SOONG_ALLOW_MISSING_DEPENDENCIES'] = 'true'
  64. env['TARGET_BUILD_VARIANT'] = 'userdebug'
  65. env['TARGET_PRODUCT'] = product
  66. jobs_arg = '-j{}'.format(multiprocessing.cpu_count())
  67. targets = [
  68. # PHONY target specified in frameworks/rs/Android.mk.
  69. 'rs-prebuilts-full',
  70. # We have to explicitly specify the jar for JACK to build.
  71. android_path('out/target/common/obj/JAVA_LIBRARIES/' +
  72. 'android-support-v8-renderscript_intermediates/classes.jar')
  73. ]
  74. subprocess.check_call(
  75. ['make', jobs_arg] + targets, cwd=android_path(), env=env)
  76. def package_toolchain(build_dir, build_name, host, dist_dir):
  77. package_name = 'renderscript-' + build_name
  78. install_host_dir = build_path('install', host)
  79. install_dir = os.path.join(install_host_dir, package_name)
  80. # Remove any previously installed toolchain so it doesn't pollute the
  81. # build.
  82. if os.path.exists(install_host_dir):
  83. shutil.rmtree(install_host_dir)
  84. install_toolchain(build_dir, install_dir, host)
  85. tarball_name = package_name + '-' + host
  86. package_path = os.path.join(dist_dir, tarball_name) + '.tar.bz2'
  87. print('Packaging ' + package_path)
  88. args = [
  89. 'tar', '-cjC', install_host_dir, '-f', package_path, package_name
  90. ]
  91. subprocess.check_call(args)
  92. def install_toolchain(build_dir, install_dir, host):
  93. install_built_host_files(build_dir, install_dir, host)
  94. install_clang_headers(build_dir, install_dir, host)
  95. install_built_device_files(build_dir, install_dir, host)
  96. install_license_files(install_dir)
  97. # We need to package libwinpthread-1.dll for Windows. This is explicitly
  98. # linked whenever pthreads is used, and the build system doesn't allow
  99. # us to link just that library statically (ldflags are stripped out
  100. # of ldlibs and vice-versa).
  101. # Bug: http://b/34273721
  102. if host.startswith('windows'):
  103. install_winpthreads(install_dir)
  104. def install_winpthreads(install_dir):
  105. """Installs the winpthreads runtime to the Windows bin directory."""
  106. lib_name = 'libwinpthread-1.dll'
  107. mingw_dir = android_path(
  108. 'prebuilts/gcc/linux-x86/host/x86_64-w64-mingw32-4.8')
  109. # RenderScript NDK toolchains for Windows only contains 32-bit binaries.
  110. lib_path = os.path.join(mingw_dir, 'x86_64-w64-mingw32/lib32', lib_name)
  111. lib_install = os.path.join(install_dir, 'bin', lib_name)
  112. install_file(lib_path, lib_install)
  113. def install_built_host_files(build_dir, install_dir, host):
  114. is_windows = host.startswith('windows')
  115. is_darwin = host.startswith('darwin-x86')
  116. bin_ext = '.exe' if is_windows else ''
  117. if is_windows:
  118. lib_ext = '.dll'
  119. elif is_darwin:
  120. lib_ext = '.dylib'
  121. else:
  122. lib_ext = '.so'
  123. built_files = [
  124. 'bin/llvm-rs-cc' + bin_ext,
  125. 'bin/bcc_compat' + bin_ext,
  126. ]
  127. if is_windows:
  128. built_files.extend([
  129. 'lib/libbcc' + lib_ext,
  130. 'lib/libbcinfo' + lib_ext,
  131. 'lib/libclang_android' + lib_ext,
  132. 'lib/libLLVM_android' + lib_ext,
  133. ])
  134. else:
  135. built_files.extend([
  136. 'lib64/libbcc' + lib_ext,
  137. 'lib64/libbcinfo' + lib_ext,
  138. 'lib64/libclang_android' + lib_ext,
  139. 'lib64/libLLVM_android' + lib_ext,
  140. 'lib64/libc++' + lib_ext,
  141. ])
  142. for built_file in built_files:
  143. dirname = os.path.dirname(built_file)
  144. # Put dlls and exes into bin/ for windows.
  145. # Bug: http://b/34273721
  146. if is_windows:
  147. dirname = 'bin'
  148. install_path = os.path.join(install_dir, dirname)
  149. if not os.path.exists(install_path):
  150. os.makedirs(install_path)
  151. built_path = os.path.join(build_dir, 'host', host, built_file)
  152. install_file(built_path, install_path)
  153. file_name = os.path.basename(built_file)
  154. # Only strip bin files (not libs) on darwin.
  155. if not is_darwin or built_file.startswith('bin/'):
  156. subprocess.check_call(
  157. ['strip', os.path.join(install_path, file_name)])
  158. def install_clang_headers(build_dir, install_dir, host):
  159. def should_copy(path):
  160. if os.path.basename(path) in ('Makefile', 'CMakeLists.txt'):
  161. return False
  162. _, ext = os.path.splitext(path)
  163. if ext == '.mk':
  164. return False
  165. return True
  166. headers_src = android_path('external/clang/lib/Headers')
  167. headers_dst = os.path.join(
  168. install_dir, 'clang-include')
  169. os.makedirs(headers_dst)
  170. for header in os.listdir(headers_src):
  171. if not should_copy(header):
  172. continue
  173. install_file(os.path.join(headers_src, header), headers_dst)
  174. install_file(android_path('bionic/libc/include/stdatomic.h'), headers_dst)
  175. def install_built_device_files(build_dir, install_dir, host):
  176. product_to_arch = {
  177. 'generic': 'arm',
  178. 'generic_arm64': 'arm64',
  179. # 'generic_mips': 'mips',
  180. # 'generic_mips64': 'mips64el',
  181. 'generic_x86': 'x86',
  182. 'generic_x86_64': 'x86_64',
  183. }
  184. bc_lib = 'librsrt'
  185. static_libs = {
  186. 'libRScpp_static',
  187. 'libcompiler_rt'
  188. }
  189. shared_libs = {
  190. 'libRSSupport',
  191. 'libRSSupportIO',
  192. 'libblasV8',
  193. }
  194. for product, arch in product_to_arch.items():
  195. lib_dir = os.path.join(install_dir, 'platform', arch)
  196. os.makedirs(lib_dir)
  197. # Copy librsrt_ARCH.bc.
  198. lib_name = bc_lib + '_' + arch + '.bc'
  199. if not host.startswith('windows'):
  200. built_lib = os.path.join(build_dir, 'host', host, 'lib64', lib_name)
  201. else:
  202. built_lib = os.path.join(build_dir, 'host', 'linux-x86', 'lib64', lib_name)
  203. install_file(built_lib, os.path.join(lib_dir, bc_lib + '.bc'))
  204. # Copy static libs and share libs.
  205. product_dir = os.path.join(build_dir, 'target/product', product)
  206. static_lib_dir = os.path.join(product_dir, 'obj/STATIC_LIBRARIES')
  207. shared_lib_dir = os.path.join(product_dir, 'obj/SHARED_LIBRARIES')
  208. for static_lib in static_libs:
  209. built_lib = os.path.join(
  210. static_lib_dir, static_lib + '_intermediates/' + static_lib + '.a')
  211. lib_name = static_lib + '.a'
  212. install_file(built_lib, os.path.join(lib_dir, lib_name))
  213. for shared_lib in shared_libs:
  214. built_lib = os.path.join(
  215. shared_lib_dir, shared_lib + '_intermediates/' + shared_lib + '.so')
  216. lib_name = shared_lib + '.so'
  217. install_file(built_lib, os.path.join(lib_dir, lib_name))
  218. # Copy renderscript-v8.jar.
  219. lib_dir = os.path.join(install_dir, 'platform')
  220. jar_dir = os.path.join(build_dir, 'target/common/obj/JAVA_LIBRARIES/'
  221. 'android-support-v8-renderscript_intermediates/classes.jar')
  222. install_file(jar_dir, os.path.join(lib_dir, 'renderscript-v8.jar'))
  223. # Copy RS runtime headers.
  224. headers_dst_base = os.path.join(install_dir, 'platform', 'rs')
  225. headers_src = android_path('frameworks/rs/script_api/include')
  226. headers_dst = os.path.join(headers_dst_base, 'scriptc')
  227. install_directory(headers_src, headers_dst)
  228. # Copy RS C++ API headers.
  229. headers_src = android_path('frameworks/rs/cpp/util')
  230. headers_dst = os.path.join(headers_dst_base, 'cpp/util')
  231. install_directory(headers_src, headers_dst)
  232. install_file(android_path('frameworks/rs/rsDefines.h'), headers_dst_base)
  233. install_file(android_path('frameworks/rs/cpp/RenderScript.h'), os.path.join(headers_dst_base, 'cpp'))
  234. install_file(android_path('frameworks/rs/cpp/rsCppStructs.h'), os.path.join(headers_dst_base, 'cpp'))
  235. def install_license_files(install_dir):
  236. projects = (
  237. 'external/clang',
  238. 'external/compiler-rt',
  239. 'external/llvm',
  240. 'frameworks/compile/slang',
  241. 'frameworks/compile/libbcc',
  242. # 'frameworks/rs', # No notice license file found.
  243. )
  244. notices = []
  245. for project in projects:
  246. project_path = android_path(project)
  247. license_pattern = os.path.join(project_path, 'MODULE_LICENSE_*')
  248. for license_file in glob.glob(license_pattern):
  249. install_file(license_file, install_dir)
  250. with open(os.path.join(project_path, 'NOTICE')) as notice_file:
  251. notices.append(notice_file.read())
  252. with open(os.path.join(install_dir, 'NOTICE'), 'w') as notice_file:
  253. notice_file.write('\n'.join(notices))
  254. def parse_args():
  255. parser = argparse.ArgumentParser()
  256. parser.add_argument(
  257. '--build-name', default='dev', help='Release name for the package.')
  258. return parser.parse_args()
  259. def main():
  260. args = parse_args()
  261. if sys.platform.startswith('linux'):
  262. hosts = ['linux-x86', 'windows-x86']
  263. elif sys.platform == 'darwin':
  264. hosts = ['darwin-x86']
  265. else:
  266. raise RuntimeError('Unsupported host: {}'.format(sys.platform))
  267. out_dir = build_path()
  268. build(out_dir=out_dir)
  269. dist_dir = ORIG_ENV.get('DIST_DIR', out_dir)
  270. for host in hosts:
  271. package_toolchain(out_dir, args.build_name, host, dist_dir)
  272. if __name__ == '__main__':
  273. main()