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 '-march=armv7-a', 37 '-mfloat-abi=softfp', 38 '-mtune=generic-armv7-a', 39 '-mfpu=neon', 40 '-mthumb', 41 '--target=arm-linux-ohosmusl', 42 '--sysroot=sysroot_stub', 43 '-fPIC'] 44 45cpp_args = [ 46 '-march=armv7-a', 47 '--target=arm-linux-ohosmusl', 48 '--sysroot=sysroot_stub', 49 '-fPIC'] 50 51c_link_args = [ 52 '-march=armv7-a', 53 '--target=arm-linux-ohosmusl', 54 '-fPIC', 55 '--sysroot=sysroot_stub', 56 '-Lsysroot_stub/usr/lib/arm-linux-ohos', 57 '-Lproject_stub/prebuilts/clang/ohos/linux-x86_64/llvm/lib/clang/10.0.1/lib/arm-linux-ohos', 58 '-Lproject_stub/prebuilts/clang/ohos/linux-x87_64/llvm/lib/arm-linux-ohos/c++', 59 '--rtlib=compiler-rt', 60 ] 61 62cpp_link_args = [ 63 '-march=armv7-a', 64 '--target=arm-linux-ohosmusl', 65 '--sysroot=sysroot_stub', 66 '-Lsysroot_stub/usr/lib/arm-linux-ohos', 67 '-Lproject_stub/prebuilts/clang/ohos/linux-x86_64/llvm/lib/clang/10.0.1/lib/arm-linux-ohos', 68 '-Lproject_stub/prebuilts/clang/ohos/linux-x87_64/llvm/lib/arm-linux-ohos/c++', 69 '-fPIC', 70 '-Wl,--exclude-libs=libunwind_llvm.a', 71 '-Wl,--exclude-libs=libc++_static.a', 72 '-Wl,--exclude-libs=libvpx_assembly_arm.a', 73 '-Wl,--warn-shared-textrel', 74 '--rtlib=compiler-rt', 75 ] 76 77[binaries] 78ar = 'project_stub/prebuilts/clang/ohos/linux-x86_64/llvm/bin/llvm-ar' 79c = ['ccache', 'project_stub/prebuilts/clang/ohos/linux-x86_64/llvm/bin/clang'] 80cpp = ['ccache', 'project_stub/prebuilts/clang/ohos/linux-x86_64/llvm/bin/clang++'] 81c_ld= 'lld' 82cpp_ld = 'lld' 83strip = 'project_stub/prebuilts/clang/ohos/linux-x86_64/llvm/bin/llvm-strip' 84pkgconfig = '/usr/bin/pkg-config' 85 86[host_machine] 87system = 'linux' 88cpu_family = 'arm' 89cpu = 'armv7' 90endian = 'little' 91''' 92 93def generate_cross_file(project_stub_in, sysroot_stub_in): 94 with open("cross_file", 'w+') as file: 95 result = corss_file_content.replace("project_stub", project_stub_in) 96 result = result.replace("sysroot_stub", sysroot_stub_in) 97 file.write(result) 98 print("generate_cross_file") 99 100def generate_pc_file(file_raw, project_dir, product_name): 101 print(file_raw) 102 if not os.path.exists('pkgconfig'): 103 os.makedirs('pkgconfig') 104 filename = 'pkgconfig/'+ ntpath.basename(file_raw) 105 with open(file_raw, 'r+') as file_raw: 106 with open(filename, "w+") as pc_file: 107 raw_content = file_raw.read() 108 raw_content = raw_content.replace("ohos_project_directory_stub", project_dir) 109 raw_content = raw_content.replace("ohos-arm-release", product_name) 110 pc_file.write(raw_content) 111 print("generate_pc_file") 112 113def process_pkgconfig(project_dir, product_name): 114 template_dir = os.path.split(os.path.abspath( __file__))[0] + r"/pkgconfig_template" 115 templates = os.listdir(template_dir) 116 for template in templates: 117 if not os.path.isdir(template): 118 generate_pc_file(template_dir + '/' + template, project_dir, product_name) 119 print("process_pkgconfig") 120 121def prepare_environment(project_path, product): 122 global project_stub 123 global sysroot_stub 124 product = product.lower() 125 project_stub = project_path 126 sysroot_stub = os.path.join(project_stub, "out", product, "obj", "third_party", "musl") 127 generate_cross_file(project_path, sysroot_stub) 128 process_pkgconfig(project_path, product) 129 130if __name__ == '__main__': 131 if len(sys.argv) < 3: 132 print("must input the OpenHarmony directory and the product name") 133 exit(-1) 134 prepare_environment(sys.argv[1], sys.argv[2]) 135