Home | History | Annotate | Download | only in rs
      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 
     19 import argparse
     20 import glob
     21 import multiprocessing
     22 import os
     23 import shutil
     24 import subprocess
     25 import sys
     26 import re
     27 
     28 
     29 THIS_DIR = os.path.realpath(os.path.dirname(__file__))
     30 ORIG_ENV = dict(os.environ)
     31 
     32 
     33 def android_path(*args):
     34     out_dir = os.path.realpath(os.path.join(THIS_DIR, '../..', *args))
     35     return out_dir
     36 
     37 
     38 def build_path(*args):
     39     # Our multistage build directories will be placed under OUT_DIR if it is in
     40     # the environment. By default they will be placed under
     41     # $ANDROID_BUILD_TOP/out.
     42     top_out = ORIG_ENV.get('OUT_DIR', android_path('out'))
     43     if not os.path.isabs(top_out):
     44         top_out = os.path.realpath(top_out)
     45     out_dir = os.path.join(top_out, *args)
     46     return out_dir
     47 
     48 
     49 def install_file(src, dst):
     50     print('Copying ' + src)
     51     shutil.copy2(src, dst)
     52 
     53 
     54 def install_directory(src, dst):
     55     print('Copying ' + src)
     56     shutil.copytree(src, dst)
     57 
     58 
     59 def build(out_dir):
     60     products = (
     61         'aosp_arm',
     62         'aosp_arm64',
     63         'aosp_mips',
     64         # 'aosp_mips64',
     65         'aosp_x86',
     66         'aosp_x86_64',
     67     )
     68     for product in products:
     69         build_product(out_dir, product)
     70 
     71 
     72 def build_product(out_dir, product):
     73     env = dict(ORIG_ENV)
     74     env['ANDROID_USE_BUILDCACHE'] = 'false'
     75     env['FORCE_BUILD_LLVM_COMPONENTS'] = 'true'
     76     env['FORCE_BUILD_RS_COMPAT'] = 'true'
     77     env['OUT_DIR'] = out_dir
     78     env['SKIP_LLVM_TESTS'] = 'true'
     79     env['SOONG_ALLOW_MISSING_DEPENDENCIES'] = 'true'
     80     env['TARGET_BUILD_VARIANT'] = 'userdebug'
     81     env['TARGET_PRODUCT'] = product
     82 
     83     jobs_arg = '-j{}'.format(multiprocessing.cpu_count())
     84     targets = [
     85         # PHONY target specified in frameworks/rs/Android.mk.
     86         'rs-prebuilts-full',
     87         # We have to explicitly specify the jar for JACK to build.
     88         android_path('out/target/common/obj/JAVA_LIBRARIES/' +
     89             'android-support-v8-renderscript_intermediates/classes.jar')
     90     ]
     91     subprocess.check_call(
     92         ['make', jobs_arg] + targets, cwd=android_path(), env=env)
     93 
     94 
     95 def package_toolchain(build_dir, build_name, host, dist_dir):
     96     package_name = 'renderscript-' + build_name
     97     install_host_dir = build_path('install', host)
     98     install_dir = os.path.join(install_host_dir, package_name)
     99 
    100     # Remove any previously installed toolchain so it doesn't pollute the
    101     # build.
    102     if os.path.exists(install_host_dir):
    103         shutil.rmtree(install_host_dir)
    104 
    105     install_toolchain(build_dir, install_dir, host)
    106 
    107     tarball_name = package_name + '-' + host
    108     package_path = os.path.join(dist_dir, tarball_name) + '.tar.bz2'
    109     print('Packaging ' + package_path)
    110     args = [
    111         'tar', '-cjC', install_host_dir, '-f', package_path, package_name
    112     ]
    113     subprocess.check_call(args)
    114 
    115 
    116 def install_toolchain(build_dir, install_dir, host):
    117     install_built_host_files(build_dir, install_dir, host)
    118     install_clang_headers(build_dir, install_dir, host)
    119     install_built_device_files(build_dir, install_dir, host)
    120     install_license_files(install_dir)
    121     # We need to package libwinpthread-1.dll for Windows. This is explicitly
    122     # linked whenever pthreads is used, and the build system doesn't allow
    123     # us to link just that library statically (ldflags are stripped out
    124     # of ldlibs and vice-versa).
    125     # Bug: http://b/34273721
    126     if host.startswith('windows'):
    127         install_winpthreads(install_dir)
    128 
    129 
    130 def install_winpthreads(install_dir):
    131       """Installs the winpthreads runtime to the Windows bin directory."""
    132       lib_name = 'libwinpthread-1.dll'
    133       mingw_dir = android_path(
    134           'prebuilts/gcc/linux-x86/host/x86_64-w64-mingw32-4.8')
    135       # RenderScript NDK toolchains for Windows only contains 32-bit binaries.
    136       lib_path = os.path.join(mingw_dir, 'x86_64-w64-mingw32/lib32', lib_name)
    137 
    138       lib_install = os.path.join(install_dir, 'bin', lib_name)
    139       install_file(lib_path, lib_install)
    140 
    141 
    142 def install_built_host_files(build_dir, install_dir, host):
    143     is_windows = host.startswith('windows')
    144     is_darwin = host.startswith('darwin-x86')
    145     bin_ext = '.exe' if is_windows else ''
    146 
    147     if is_windows:
    148         lib_ext = '.dll'
    149     elif is_darwin:
    150         lib_ext = '.dylib'
    151     else:
    152         lib_ext = '.so'
    153 
    154     built_files = [
    155         'bin/llvm-rs-cc' + bin_ext,
    156         'bin/bcc_compat' + bin_ext,
    157     ]
    158 
    159     if is_windows:
    160         built_files.extend([
    161             'lib/libbcc' + lib_ext,
    162             'lib/libbcinfo' + lib_ext,
    163             'lib/libclang' + lib_ext,
    164             'lib/libLLVM' + lib_ext,
    165         ])
    166     else:
    167         built_files.extend([
    168             'lib64/libbcc' + lib_ext,
    169             'lib64/libbcinfo' + lib_ext,
    170             'lib64/libclang' + lib_ext,
    171             'lib64/libLLVM' + lib_ext,
    172             'lib64/libc++' + lib_ext,
    173         ])
    174 
    175     for built_file in built_files:
    176         dirname = os.path.dirname(built_file)
    177         # Put dlls and exes into bin/ for windows.
    178         # Bug: http://b/34273721
    179         if is_windows:
    180             dirname = 'bin'
    181         install_path = os.path.join(install_dir, dirname)
    182         if not os.path.exists(install_path):
    183             os.makedirs(install_path)
    184 
    185         built_path = os.path.join(build_dir, 'host', host, built_file)
    186         install_file(built_path, install_path)
    187 
    188         file_name = os.path.basename(built_file)
    189 
    190         # Only strip bin files (not libs) on darwin.
    191         if not is_darwin or built_file.startswith('bin/'):
    192             subprocess.check_call(
    193                 ['strip', os.path.join(install_path, file_name)])
    194 
    195 
    196 def install_clang_headers(build_dir, install_dir, host):
    197     def should_copy(path):
    198         if os.path.basename(path) in ('Makefile', 'CMakeLists.txt'):
    199             return False
    200         _, ext = os.path.splitext(path)
    201         if ext == '.mk':
    202             return False
    203         return True
    204 
    205     headers_src = android_path('external/clang/lib/Headers')
    206     headers_dst = os.path.join(
    207         install_dir, 'clang-include')
    208     os.makedirs(headers_dst)
    209     for header in os.listdir(headers_src):
    210         if not should_copy(header):
    211             continue
    212         install_file(os.path.join(headers_src, header), headers_dst)
    213 
    214     install_file(android_path('bionic/libc/include/stdatomic.h'), headers_dst)
    215 
    216 
    217 def install_built_device_files(build_dir, install_dir, host):
    218     product_to_arch = {
    219         'generic': 'arm',
    220         'generic_arm64': 'arm64',
    221         'generic_mips': 'mips',
    222         # 'generic_mips64': 'mips64el',
    223         'generic_x86': 'x86',
    224         'generic_x86_64': 'x86_64',
    225     }
    226 
    227     bc_lib = 'librsrt'
    228 
    229     static_libs = {
    230         'libRScpp_static',
    231         'libcompiler_rt'
    232     }
    233 
    234     shared_libs = {
    235         'libRSSupport.so',
    236         'libRSSupportIO.so',
    237         'libblasV8.so',
    238     }
    239 
    240     for product, arch in product_to_arch.items():
    241         lib_dir = os.path.join(install_dir, 'platform', arch)
    242         os.makedirs(lib_dir)
    243 
    244         # Copy librsrt_ARCH.bc.
    245         lib_name = bc_lib + '_' + arch + '.bc'
    246         if not host.startswith('windows'):
    247             built_lib = os.path.join(build_dir, 'host', host, 'lib64', lib_name)
    248         else:
    249             built_lib = os.path.join(build_dir, 'host', 'linux-x86', 'lib64', lib_name)
    250         install_file(built_lib, os.path.join(lib_dir, bc_lib + '.bc'))
    251 
    252         # Copy static libs and share libs.
    253         product_dir = os.path.join(build_dir, 'target/product', product)
    254         static_lib_dir = os.path.join(product_dir, 'obj/STATIC_LIBRARIES')
    255         shared_lib_dir = os.path.join(product_dir, 'obj/lib')
    256         for static_lib in static_libs:
    257             built_lib = os.path.join(
    258                 static_lib_dir, static_lib + '_intermediates/' + static_lib + '.a')
    259             lib_name = static_lib + '.a'
    260             install_file(built_lib, os.path.join(lib_dir, lib_name))
    261         for shared_lib in shared_libs:
    262             built_lib = os.path.join(shared_lib_dir, shared_lib)
    263             lib_name = shared_lib
    264             install_file(built_lib, os.path.join(lib_dir, lib_name))
    265 
    266     # Copy renderscript-v8.jar.
    267     lib_dir = os.path.join(install_dir, 'platform')
    268     jar_dir = os.path.join(build_dir, 'target/common/obj/JAVA_LIBRARIES/'
    269         'android-support-v8-renderscript_intermediates/classes.jar')
    270     install_file(jar_dir, os.path.join(lib_dir, 'renderscript-v8.jar'))
    271 
    272     # Copy RS runtime headers.
    273     headers_dst_base = os.path.join(install_dir, 'platform', 'rs')
    274 
    275     headers_src = android_path('frameworks/rs/script_api/include')
    276     headers_dst = os.path.join(headers_dst_base, 'scriptc')
    277     install_directory(headers_src, headers_dst)
    278 
    279     # Copy RS C++ API headers.
    280     headers_src = android_path('frameworks/rs/cpp/util')
    281     headers_dst = os.path.join(headers_dst_base, 'cpp/util')
    282     install_directory(headers_src, headers_dst)
    283     install_file(android_path('frameworks/rs/rsDefines.h'), headers_dst_base)
    284     install_file(android_path('frameworks/rs/cpp/RenderScript.h'), os.path.join(headers_dst_base, 'cpp'))
    285     install_file(android_path('frameworks/rs/cpp/rsCppStructs.h'), os.path.join(headers_dst_base, 'cpp'))
    286 
    287 
    288 def install_license_files(install_dir):
    289     projects = (
    290         'external/clang',
    291         'external/compiler-rt',
    292         'external/llvm',
    293         'frameworks/compile/slang',
    294         'frameworks/compile/libbcc',
    295         # 'frameworks/rs', # No notice license file found.
    296     )
    297 
    298     notices = []
    299     for project in projects:
    300         project_path = android_path(project)
    301         license_pattern = os.path.join(project_path, 'MODULE_LICENSE_*')
    302         for license_file in glob.glob(license_pattern):
    303             install_file(license_file, install_dir)
    304         with open(os.path.join(project_path, 'NOTICE')) as notice_file:
    305             notices.append(notice_file.read())
    306     with open(os.path.join(install_dir, 'NOTICE'), 'w') as notice_file:
    307         notice_file.write('\n'.join(notices))
    308 
    309 
    310 def parse_args():
    311     parser = argparse.ArgumentParser()
    312 
    313     parser.add_argument(
    314         '--build-name', default='dev', help='Release name for the package.')
    315 
    316     return parser.parse_args()
    317 
    318 
    319 def main():
    320     args = parse_args()
    321 
    322     if sys.platform.startswith('linux'):
    323         hosts = ['linux-x86', 'windows-x86']
    324     elif sys.platform == 'darwin':
    325         hosts = ['darwin-x86']
    326     else:
    327         raise RuntimeError('Unsupported host: {}'.format(sys.platform))
    328 
    329     out_dir = build_path()
    330     build(out_dir=out_dir)
    331 
    332     dist_dir = ORIG_ENV.get('DIST_DIR', out_dir)
    333     for host in hosts:
    334         package_toolchain(out_dir, args.build_name, host, dist_dir)
    335 
    336 
    337 if __name__ == '__main__':
    338     main()
    339