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['FORCE_BUILD_LLVM_COMPONENTS'] = 'true'
     75     env['FORCE_BUILD_RS_COMPAT'] = 'true'
     76     env['OUT_DIR'] = out_dir
     77     env['SKIP_LLVM_TESTS'] = 'true'
     78     env['SOONG_ALLOW_MISSING_DEPENDENCIES'] = 'true'
     79     env['TARGET_BUILD_VARIANT'] = 'userdebug'
     80     env['TARGET_PRODUCT'] = product
     81 
     82     jobs_arg = '-j{}'.format(multiprocessing.cpu_count())
     83     targets = [
     84         # PHONY target specified in frameworks/rs/Android.mk.
     85         'rs-prebuilts-full',
     86         # We have to explicitly specify the jar for JACK to build.
     87         android_path('out/target/common/obj/JAVA_LIBRARIES/' +
     88             'android-support-v8-renderscript_intermediates/classes.jar')
     89     ]
     90     subprocess.check_call(
     91         ['make', jobs_arg] + targets, cwd=android_path(), env=env)
     92 
     93 
     94 def package_toolchain(build_dir, build_name, host, dist_dir):
     95     package_name = 'renderscript-' + build_name
     96     install_host_dir = build_path('install', host)
     97     install_dir = os.path.join(install_host_dir, package_name)
     98 
     99     # Remove any previously installed toolchain so it doesn't pollute the
    100     # build.
    101     if os.path.exists(install_host_dir):
    102         shutil.rmtree(install_host_dir)
    103 
    104     install_toolchain(build_dir, install_dir, host)
    105 
    106     tarball_name = package_name + '-' + host
    107     package_path = os.path.join(dist_dir, tarball_name) + '.tar.bz2'
    108     print('Packaging ' + package_path)
    109     args = [
    110         'tar', '-cjC', install_host_dir, '-f', package_path, package_name
    111     ]
    112     subprocess.check_call(args)
    113 
    114 
    115 def install_toolchain(build_dir, install_dir, host):
    116     install_built_host_files(build_dir, install_dir, host)
    117     install_clang_headers(build_dir, install_dir, host)
    118     install_built_device_files(build_dir, install_dir, host)
    119     install_license_files(install_dir)
    120     # We need to package libwinpthread-1.dll for Windows. This is explicitly
    121     # linked whenever pthreads is used, and the build system doesn't allow
    122     # us to link just that library statically (ldflags are stripped out
    123     # of ldlibs and vice-versa).
    124     # Bug: http://b/34273721
    125     if host.startswith('windows'):
    126         install_winpthreads(install_dir)
    127 
    128 
    129 def install_winpthreads(install_dir):
    130       """Installs the winpthreads runtime to the Windows bin directory."""
    131       lib_name = 'libwinpthread-1.dll'
    132       mingw_dir = android_path(
    133           'prebuilts/gcc/linux-x86/host/x86_64-w64-mingw32-4.8')
    134       # RenderScript NDK toolchains for Windows only contains 32-bit binaries.
    135       lib_path = os.path.join(mingw_dir, 'x86_64-w64-mingw32/lib32', lib_name)
    136 
    137       lib_install = os.path.join(install_dir, 'bin', lib_name)
    138       install_file(lib_path, lib_install)
    139 
    140 
    141 def install_built_host_files(build_dir, install_dir, host):
    142     is_windows = host.startswith('windows')
    143     is_darwin = host.startswith('darwin-x86')
    144     bin_ext = '.exe' if is_windows else ''
    145 
    146     if is_windows:
    147         lib_ext = '.dll'
    148     elif is_darwin:
    149         lib_ext = '.dylib'
    150     else:
    151         lib_ext = '.so'
    152 
    153     built_files = [
    154         'bin/llvm-rs-cc' + bin_ext,
    155         'bin/bcc_compat' + bin_ext,
    156     ]
    157 
    158     if is_windows:
    159         built_files.extend([
    160             'lib/libbcc' + lib_ext,
    161             'lib/libbcinfo' + lib_ext,
    162             'lib/libclang_android' + lib_ext,
    163             'lib/libLLVM_android' + lib_ext,
    164         ])
    165     else:
    166         built_files.extend([
    167             'lib64/libbcc' + lib_ext,
    168             'lib64/libbcinfo' + lib_ext,
    169             'lib64/libclang_android' + lib_ext,
    170             'lib64/libLLVM_android' + lib_ext,
    171             'lib64/libc++' + lib_ext,
    172         ])
    173 
    174     for built_file in built_files:
    175         dirname = os.path.dirname(built_file)
    176         # Put dlls and exes into bin/ for windows.
    177         # Bug: http://b/34273721
    178         if is_windows:
    179             dirname = 'bin'
    180         install_path = os.path.join(install_dir, dirname)
    181         if not os.path.exists(install_path):
    182             os.makedirs(install_path)
    183 
    184         built_path = os.path.join(build_dir, 'host', host, built_file)
    185         install_file(built_path, install_path)
    186 
    187         file_name = os.path.basename(built_file)
    188 
    189         # Only strip bin files (not libs) on darwin.
    190         if not is_darwin or built_file.startswith('bin/'):
    191             subprocess.check_call(
    192                 ['strip', os.path.join(install_path, file_name)])
    193 
    194 
    195 def install_clang_headers(build_dir, install_dir, host):
    196     def should_copy(path):
    197         if os.path.basename(path) in ('Makefile', 'CMakeLists.txt'):
    198             return False
    199         _, ext = os.path.splitext(path)
    200         if ext == '.mk':
    201             return False
    202         return True
    203 
    204     headers_src = android_path('external/clang/lib/Headers')
    205     headers_dst = os.path.join(
    206         install_dir, 'clang-include')
    207     os.makedirs(headers_dst)
    208     for header in os.listdir(headers_src):
    209         if not should_copy(header):
    210             continue
    211         install_file(os.path.join(headers_src, header), headers_dst)
    212 
    213     install_file(android_path('bionic/libc/include/stdatomic.h'), headers_dst)
    214 
    215 
    216 def install_built_device_files(build_dir, install_dir, host):
    217     product_to_arch = {
    218         'generic': 'arm',
    219         'generic_arm64': 'arm64',
    220         # 'generic_mips': 'mips',
    221         # 'generic_mips64': 'mips64el',
    222         'generic_x86': 'x86',
    223         'generic_x86_64': 'x86_64',
    224     }
    225 
    226     bc_lib = 'librsrt'
    227 
    228     static_libs = {
    229         'libRScpp_static',
    230         'libcompiler_rt'
    231     }
    232 
    233     shared_libs = {
    234         'libRSSupport',
    235         'libRSSupportIO',
    236         'libblasV8',
    237     }
    238 
    239     for product, arch in product_to_arch.items():
    240         lib_dir = os.path.join(install_dir, 'platform', arch)
    241         os.makedirs(lib_dir)
    242 
    243         # Copy librsrt_ARCH.bc.
    244         lib_name = bc_lib + '_' + arch + '.bc'
    245         if not host.startswith('windows'):
    246             built_lib = os.path.join(build_dir, 'host', host, 'lib64', lib_name)
    247         else:
    248             built_lib = os.path.join(build_dir, 'host', 'linux-x86', 'lib64', lib_name)
    249         install_file(built_lib, os.path.join(lib_dir, bc_lib + '.bc'))
    250 
    251         # Copy static libs and share libs.
    252         product_dir = os.path.join(build_dir, 'target/product', product)
    253         static_lib_dir = os.path.join(product_dir, 'obj/STATIC_LIBRARIES')
    254         shared_lib_dir = os.path.join(product_dir, 'obj/SHARED_LIBRARIES')
    255         for static_lib in static_libs:
    256             built_lib = os.path.join(
    257                 static_lib_dir, static_lib + '_intermediates/' + static_lib + '.a')
    258             lib_name = static_lib + '.a'
    259             install_file(built_lib, os.path.join(lib_dir, lib_name))
    260         for shared_lib in shared_libs:
    261             built_lib = os.path.join(
    262                 shared_lib_dir, shared_lib + '_intermediates/' + shared_lib + '.so')
    263             lib_name = shared_lib + '.so'
    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