• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4# Copyright (c) 2022 Huawei Device Co., Ltd.
5
6# Permission is hereby granted, free of charge, to any person obtaining a copy
7# of this software and associated documentation files (the "Software"), to deal
8# in the Software without restriction, including without limitation the rights
9# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10# copies of the Software, and to permit persons to whom the Software is
11# furnished to do so, subject to the following conditions:
12
13# The above copyright notice and this permission notice shall be included in
14# all copies or substantial portions of the Software.
15
16# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22# SOFTWARE.
23
24import sys
25import ntpath
26import os
27
28sysroot_stub = 'sysroot_stub'
29project_stub = 'project_stub'
30
31corss_file_content='''
32[properties]
33needs_exe_wrapper = true
34
35c_args = [
36    '--target=aarch64-linux-ohosmusl',
37    '--sysroot=sysroot_stub',
38    '-fno-emulated-tls',
39    '-fPIC',
40    '-g',
41    '-O2',
42    '-D_FORTIFY_SOURCE=2',
43    '-fstack-protector-all',
44    compile_args_stub
45    ]
46
47cpp_args = [
48    '--target=aarch64-linux-ohosmusl',
49    '--sysroot=sysroot_stub',
50    '-fno-emulated-tls',
51    '-fPIC',
52    '-g',
53    '-O2',
54    '-D_FORTIFY_SOURCE=2',
55    '-fstack-protector-all',
56    compile_args_stub
57    ]
58
59c_link_args = [
60    '--target=aarch64-linux-ohosmusl',
61    '-fPIC',
62    '--sysroot=sysroot_stub',
63    '-Lsysroot_stub/usr/lib/aarch64-linux-ohos',
64    '-Lproject_stub/prebuilts/clang/ohos/linux-x86_64/llvm/lib/clang/current/lib/aarch64-linux-ohos',
65    '-Lproject_stub/prebuilts/clang/ohos/linux-x86_64/llvm/lib/aarch64-linux-ohos/c++',
66    '--rtlib=compiler-rt',
67    link_args_stub
68    ]
69
70cpp_link_args = [
71    '--target=aarch64-linux-ohosmusl',
72    '--sysroot=sysroot_stub',
73    '-Lsysroot_stub/usr/lib/aarch64-linux-ohos',
74    '-Lproject_stub/prebuilts/clang/ohos/linux-x86_64/llvm/lib/clang/current/lib/aarch64-linux-ohos',
75    '-Lproject_stub/prebuilts/clang/ohos/linux-x86_64/llvm/lib/aarch64-linux-ohos/c++',
76    '-fPIC',
77    '-Wl,--exclude-libs=libunwind_llvm.a',
78    '-Wl,--exclude-libs=libc++_static.a',
79    '-Wl,--exclude-libs=libvpx_assembly_arm.a',
80    '-Wl,--warn-shared-textrel',
81    '--rtlib=compiler-rt',
82    link_args_stub
83    ]
84
85[binaries]
86ar = 'project_stub/prebuilts/clang/ohos/linux-x86_64/llvm/bin/llvm-ar'
87c = ['ccache', 'project_stub/prebuilts/clang/ohos/linux-x86_64/llvm/bin/clang']
88cpp = ['ccache', 'project_stub/prebuilts/clang/ohos/linux-x86_64/llvm/bin/clang++']
89c_ld= 'lld'
90cpp_ld = 'lld'
91strip = 'project_stub/prebuilts/clang/ohos/linux-x86_64/llvm/bin/llvm-strip'
92pkgconfig = '/usr/bin/pkg-config'
93
94[host_machine]
95system = 'linux'
96cpu_family = 'aarch64'
97cpu = 'armv8'
98endian = 'little'
99'''
100
101hwasan_compile_args = '''
102    '-shared-libasan',
103    '-fsanitize=hwaddress',
104    '-mllvm',
105    '-hwasan-globals=0',
106    '-fno-lto',
107    '-fno-whole-program-vtables',
108'''
109hwasan_link_args = '''
110    '-shared-libasan',
111    '-fsanitize=hwaddress',
112'''
113
114swasan_compile_args = '''
115    '-fsanitize=address',
116    '-fno-inline-functions',
117    '-fno-inline',
118    '-fsanitize-address-use-after-scope',
119    '-fno-omit-frame-pointer',
120'''
121swasan_link_args = '''
122    '-lclang_rt.asan',
123'''
124noasan_compile_args = '''
125'''
126noasan_link_args = '''
127'''
128
129coverage_compile_args = '''
130    '--coverage',
131'''
132coverage_link_args = '''
133    '--coverage',
134'''
135def generate_cross_file(project_stub_in, sysroot_stub_in, asan_option, coverage_option):
136    with open("thirdparty/mesa3d/cross_file", 'w+') as file:
137        result = corss_file_content.replace("project_stub", project_stub_in)
138        result = result.replace("sysroot_stub", sysroot_stub_in)
139        compile_args = ""
140        link_args = ""
141        if asan_option == "hwasan":
142            compile_args = hwasan_compile_args
143            link_args = hwasan_link_args
144        elif asan_option == "swasan":
145            compile_args = swasan_compile_args
146            link_args = swasan_link_args
147        else:
148            compile_args = noasan_compile_args
149            link_args = noasan_link_args
150        if coverage_option == "use_clang_coverage":
151            compile_args = compile_args + coverage_compile_args
152            link_args = link_args + coverage_link_args
153        result = result.replace("compile_args_stub", compile_args)
154        result = result.replace("link_args_stub", link_args)
155        file.write(result)
156    print("generate_cross_file")
157
158def generate_pc_file(file_raw, project_dir, product_name, skia_version):
159    print(file_raw)
160    if not os.path.exists('thirdparty/mesa3d/pkgconfig'):
161        os.makedirs('thirdparty/mesa3d/pkgconfig')
162    shortfilename = ntpath.basename(file_raw)
163    filename = 'thirdparty/mesa3d/pkgconfig/'+ shortfilename
164    with open(file_raw, 'r+') as file_raw:
165        with open(filename, "w+") as pc_file:
166            raw_content = file_raw.read()
167            raw_content = raw_content.replace("ohos_project_directory_stub", project_dir)
168            raw_content = raw_content.replace("ohos-arm-release", product_name)
169            raw_content = raw_content.replace("ohos-arm", "ohos-arm64")
170            if shortfilename == "expat.pc":
171                if skia_version == "new_skia":
172                    raw_content = raw_content.replace("skia_folder_stub", "skia/m133")
173                    raw_content = raw_content.replace("expat_lib_stub", "expatm133")
174                else:
175                    raw_content = raw_content.replace("skia_folder_stub", "skia")
176                    raw_content = raw_content.replace("expat_lib_stub", "expat")
177            pc_file.write(raw_content)
178    print("generate_pc_file")
179
180def process_pkgconfig(project_dir, product_name, skia_version):
181    template_dir = os.path.split(os.path.abspath( __file__))[0] + r"/pkgconfig_template"
182    templates = os.listdir(template_dir)
183    for template in templates:
184        if not os.path.isdir(template):
185            generate_pc_file(template_dir + '/' + template, project_dir, product_name, skia_version)
186    print("process_pkgconfig")
187
188def prepare_environment(project_path, product, asan_option, coverage_option, skia_version):
189    global project_stub
190    global sysroot_stub
191    product = product.lower()
192    project_stub = project_path
193    sysroot_stub = os.path.join(project_stub, "out", product, "obj", "third_party", "musl")
194    generate_cross_file(project_path, sysroot_stub, asan_option, coverage_option)
195    process_pkgconfig(project_path, product, skia_version)
196
197if __name__ == '__main__':
198    if len(sys.argv) < 6:
199        print("must input the OpenHarmony directory, the product name, the asan option and the coverage option")
200        exit(-1)
201    prepare_environment(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4], sys.argv[5])
202