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