From 5c4a83efbafa3bb7b369791b4dd7018799bd73a7 Mon Sep 17 00:00:00 2001 From: zhuoli Date: Thu, 8 Jul 2021 23:11:54 +0800 Subject: [PATCH] Refine musl build configuration 1. Enable compilation of 7_hard_neon-vfpv4 target. 2. Modify cflags/ldflags to match orignal musl Makefile/configure. 3. Add script for configure musl source files, not enabled yet. Signed-off-by: zhuoli --- BUILD.gn | 2 +- musl_config.gni | 1 + musl_template.gni | 104 ++++++++++++++--- scripts/search_source_files.py | 207 +++++++++++++++++++++++++++++++++ 4 files changed, 300 insertions(+), 14 deletions(-) create mode 100755 scripts/search_source_files.py diff --git a/BUILD.gn b/BUILD.gn index 079a47f5c..d3518359c 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -6,7 +6,7 @@ group("musl_all") { ":soft_libs", ":a7_soft_libs", ":a7_softfp_neon_libs", - # ":a7_hard_neon_libs", + ":a7_hard_neon_libs", ] } diff --git a/musl_config.gni b/musl_config.gni index bc99e8607..eeb1881bf 100644 --- a/musl_config.gni +++ b/musl_config.gni @@ -27,6 +27,7 @@ declare_args() { } declare_args() { + runtime_lib_path = "//prebuilts/clang/ohos/linux-x86_64/llvm/lib/clang/10.0.1/lib" user_custom_libc = true musl_ported_dir = "intermidiates/${musl_target_os}/musl_src_ported" musl_inc_out_dir = "usr/include/${musl_target_triple}" diff --git a/musl_template.gni b/musl_template.gni index 69583918b..4b054b3b6 100644 --- a/musl_template.gni +++ b/musl_template.gni @@ -93,35 +93,64 @@ template("musl_libs") { "${target_out_dir}/${musl_inc_out_dir}", ] - cflags = [ + cflags_basic = [ "--target=${musl_target_triple}", - "-std=c99", - "-nostdinc", - "-ffreestanding", - "-Wa,--noexecstack", + "-march=armv7-a", + "-O2", + "-Wall", + "-fstack-protector-strong", + "-D_FORTIFY_SOURCE=2", + "-Wl,-z,relro,-z,now,-z,noexecstack", + ] + + cflags_auto = [ "-D_XOPEN_SOURCE=700", "-Os", "-pipe", - "-fno-omit-frame-pointer", - "-funwind-tables", - "-fasynchronous-unwind-tables", + "-fomit-frame-pointer", + "-fno-unwind-tables", + "-fno-asynchronous-unwind-tables", "-ffunction-sections", "-fdata-sections", "-Werror=implicit-function-declaration", "-Werror=implicit-int", "-Werror=pointer-sign", "-Werror=pointer-arith", - "-Wno-bitwise-op-parentheses", - "-Wno-shift-op-parentheses", + "-Qunused-arguments", + ] + + cflags_c99fse = [ + "-std=c99", + "-nostdinc", + "-ffreestanding", + "-frounding-math", + "-Wa,--noexecstack", ] - if (musl_arch == "aarch64") { - cflags += [ "-Qunused-arguments" ] + # cflags_nossp = [ "-fno-stack-protector" ] + + cflags_all = cflags_basic + + cflags_c99fse + + cflags_auto + + cflags = cflags_all + + if ("${target_abi}" != "a7_hard_neon-vfpv4") { + defines = [ "BROKEN_VFP_ASM" ] } + dynamic_list = rebase_path("${target_out_dir}/${musl_ported_dir}/dynamic.list") + ldflags = [ "--target=${musl_target_triple}", "-fuse-ld=lld", + "-Wl,--sort-section,alignment", + "-Wl,--sort-common", + "-Wl,--gc-sections", + "-Wl,--hash-style=both", + "-Wl,--no-undefined", + "-Wl,--exclude-libs=ALL", + "-Wl,--dynamic-list=${dynamic_list}", ] asmflags = cflags } @@ -159,12 +188,53 @@ template("musl_libs") { sources_orig = musl_src_arch_file + musl_src_file sources_orig -= musl_src_filterout + sources_orig -= [ + "src/string/mempcpy.c", + "src/string/memset.c", + "src/env/__init_tls.c", + "src/env/__libc_start_main.c", + "src/env/__stack_chk_fail.c", + "src/thread/${musl_arch}/__set_thread_area.c", + ] + + if ("${target_abi}" == "a7_hard_neon-vfpv4") { + defines = [ "__ARM_PCS_VFP" ] + } + + foreach(s, sources_orig) { + sources += [ "${target_out_dir}/${musl_ported_dir}/${s}" ] + } + + cflags += [ + "-O3", + "-fPIC", + ] + + configs -= musl_inherited_configs + configs += [ ":${template_prefix}_musl_config" ] + + deps = porting_deps + } + + source_set("${template_prefix}_musl_src_nossp") { + cflags = [] + sources = [] + + sources_orig = [ + "src/string/mempcpy.c", + "src/string/memset.c", + "src/env/__init_tls.c", + "src/env/__libc_start_main.c", + "src/env/__stack_chk_fail.c", + "src/thread/${musl_arch}/__set_thread_area.c", + ] foreach(s, sources_orig) { sources += [ "${target_out_dir}/${musl_ported_dir}/${s}" ] } cflags += [ + "-O3", "-fPIC", "-fno-stack-protector", ] @@ -196,7 +266,10 @@ template("musl_libs") { complete_static_lib = true output_dir = "${target_out_dir}/${static_lib_out_dir}" - deps = [ ":${template_prefix}_musl_src" ] + deps = [ + ":${template_prefix}_musl_src", + ":${template_prefix}_musl_src_nossp", + ] } static_library("${template_prefix}_libm") { @@ -235,9 +308,13 @@ template("musl_libs") { output_name = "libc/${shared_lib_out_dir}/libc" output_extension = "so" + libclang_rt_path = rebase_path("${runtime_lib_path}/${musl_target_triple}/${shared_lib_out_dir}") + ldflags = [ "-nostdlib", "-Wl,-e,_dlstart", + "-L${libclang_rt_path}", + "-lclang_rt.builtins", ] remove_configs = musl_inherited_configs @@ -248,6 +325,7 @@ template("musl_libs") { deps = [ ":${template_prefix}_musl_ldso", ":${template_prefix}_musl_src", + ":${template_prefix}_musl_src_nossp", ] } diff --git a/scripts/search_source_files.py b/scripts/search_source_files.py new file mode 100755 index 000000000..4302fcc0e --- /dev/null +++ b/scripts/search_source_files.py @@ -0,0 +1,207 @@ +import os +import argparse + +SRC_DIRS = ["src", "crt", "ldso"] + +def file_search(path, target_type): + base_srcs = [] + if not os.path.isdir(path): + return base_srcs + src_list = os.listdir(path) + for src in src_list: + cur_path = os.path.join(path, src) + if os.path.isdir(cur_path): + # base_srcs += file_search(cur_path, target_type) + continue + elif target_type == "header": + if (cur_path.endswith(".h")): + base_srcs.append(cur_path) + elif target_type == "src": + if (cur_path.endswith(".c")): + base_srcs.append(cur_path) + return base_srcs + +def arch_file_search(path, arch): + arch_srcs = [] + target_path = path + "/" + arch + if os.path.isdir(target_path): + src_list = os.listdir(target_path) + for src in src_list: + cur_path = os.path.join(target_path, src) + if os.path.isdir(cur_path): + # arch_srcs += file_search(cur_path, target_type) + continue + elif (cur_path.endswith(".c") or cur_path.endswith(".s") or + cur_path.endswith(".S")): + arch_srcs.append(cur_path) + return arch_srcs + +def get_base_srcs(path): + base_srcs = [] + for src_dir in SRC_DIRS: + src_path = path + "/" + src_dir + src_list = os.listdir(src_path) + for src in src_list: + cur_path = os.path.join(src_path, src) + if os.path.isdir(cur_path) and src_dir == "src": + base_srcs += file_search(cur_path, "src") + else: + if (cur_path.endswith(".c")): + base_srcs.append(cur_path) + base_srcs.sort() + return base_srcs + +def get_arch_srcs(path, arch): + arch_srcs = [] + for src_dir in SRC_DIRS: + if src_dir == "src": + src_list = os.listdir(path + "/" + src_dir) + for src in src_list: + cur_path = os.path.join(path + "/" + src_dir, src) + arch_srcs += arch_file_search(cur_path, arch) + elif os.path.isdir(path + "/" + src_dir + "/" + arch): + src_path = path + "/" + src_dir + "/" + arch + src_list = os.listdir(src_path) + for src in src_list: + cur_path = os.path.join(src_path, src) + if os.path.isdir(cur_path): + continue + else: + if (cur_path.endswith(".c") or cur_path.endswith(".s") or + cur_path.endswith(".S")): + arch_srcs.append(cur_path) + arch_srcs.sort() + return arch_srcs + +def change_subfix(name): + new_name = "" + if name.endswith(".s"): + new_name = name.replace(".s", ".c") + elif name.endswith(".S"): + new_name = name.replace(".S", ".c") + else: + return name + return new_name + +def replace_srcs_with_arch(base_srcs, arch_srcs, arch): + target_srcs = base_srcs + arch_removed = [] + for src in arch_srcs: + fake_name = change_subfix(src.replace("/" + arch, "")) + if fake_name in target_srcs: + target_srcs.remove(fake_name) + arch_removed.append(src) + return target_srcs, arch_removed + +def get_libc_srcs(target_srcs, relative_path): + libc_srcs = [] + for src in target_srcs: + clean_path_src = src.replace(relative_path, "") + if clean_path_src.startswith("/src/") or clean_path_src.startswith("/compat/time32/"): + libc_srcs.append(src) + return libc_srcs + +def get_ldso_srcs(target_srcs, relative_path): + ldso_srcs = [] + for src in target_srcs: + clean_path_src = src.replace(relative_path, "") + if clean_path_src.startswith("/ldso/"): + ldso_srcs.append(src) + return ldso_srcs + +def get_crt_srcs(target_srcs, relative_path): + crt_srcs = [] + for src in target_srcs: + clean_path_src = src.replace(relative_path, "") + if clean_path_src.startswith("/crt/"): + crt_srcs.append(src) + return crt_srcs + +def get_header_dirs(path, arch): + header_dirs = { + path + "/arch/" + arch + "/bits", + path + "/arch/generic/bits", + path + "/include" + } + return header_dirs + +def get_generated_header(path): + generated_headers = [ + path + "/include/bits/alltypes.h", + path + "/include/bits/syscall.h", + ] + +def get_all_header(header_dirs): + all_header = [] + for dir in header_dirs: + file_list = os.listdir(dir) + for file in file_list: + cur_path = os.path.join(dir, file) + if os.path.isdir(cur_path): + all_header += file_search(cur_path, "header") + else: + if (cur_path.endswith(".h")): + all_header.append(cur_path) + all_header.sort() + return all_header + +def rm_dup_header(all_header_files, arch): + header_files = all_header_files + header_to_rm = [] + for file in header_files: + if "/arch/generic/bits" in file: + substitute_file = file.replace("/arch/generic/bits", "/arch/"+ arch +"/bits") + if substitute_file in header_files: + header_to_rm.append(file) + + return [i for i in header_files if i not in header_to_rm] + +def print_outputs(outputs): + for out in outputs: + print(out) + for content in outputs[out]: + print(" " + content) + +def search_for_files(path, arch): + if arch == "arm": + SRC_DIRS.append("compat/time32") + + base_srcs = get_base_srcs(path) + arch_srcs = get_arch_srcs(path, arch) + target_srcs, arch_removed = replace_srcs_with_arch(base_srcs, arch_srcs, arch) + target_srcs += arch_srcs + + libc_srcs = get_libc_srcs(target_srcs, path) + ldso_srcs = get_ldso_srcs(target_srcs, path) + crt_srcs = get_crt_srcs(target_srcs, path) + + all_header_files = get_all_header(get_header_dirs(path, arch)) + header_files = rm_dup_header(all_header_files, arch) + + outputs = { + "arch": arch_srcs, + "arch_rm": arch_removed, + "libc": libc_srcs, + "ldso": ldso_srcs, + "crt": crt_srcs, + "header": header_files, + } + # print_outputs(outputs) + return libc_srcs, ldso_srcs, crt_srcs, header_files + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + + parser.add_argument('--path', + required = True, + help = 'The path of musl source files') + parser.add_argument('--arch', + required = True, + help = 'The targeting architecture') + args = parser.parse_args() + + return search_for_files(args.path, args.arch) + + +if __name__ == "__main__": + main() -- Gitee