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