From 4536cef9065a9bc562768c1d2d5d1af993a3a25f Mon Sep 17 00:00:00 2001 From: andy <1414772332@qq.com> Date: Thu, 16 Jan 2025 11:03:48 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8gcc=E7=BC=96=E8=AF=91?= =?UTF-8?q?=E5=99=A8=E6=A8=A1=E6=9D=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build/BUILDCONFIG.gn | 30 ++++++++- build/toolchain/BUILD.gn | 89 +++---------------------- build/toolchain/gcc.gni | 137 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 174 insertions(+), 82 deletions(-) create mode 100644 build/toolchain/gcc.gni diff --git a/build/BUILDCONFIG.gn b/build/BUILDCONFIG.gn index 619fa97..25ff84c 100755 --- a/build/BUILDCONFIG.gn +++ b/build/BUILDCONFIG.gn @@ -2,6 +2,34 @@ # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. + + + +# board_toolchain 用于定义使用的编译工具链 +# 这个值可以取 gcc riscv-gcc + + +if (!defined(board_toolchain)){ + board_toolchain="gcc" +} + +if (board_toolchain=="riscv-gcc"){ + current_cc_command = "riscv64-unknown-elf-gcc" + current_cxx_command = "riscv64-unknown-elf-g++" + current_ar_command = "riscv64-unknown-elf-ar" + current_ld_command = current_cc_command + current_strip_command = + "riscv64-unknown-elf-strip --strip-unneeded" +} else if (board_toolchain=="gcc"){ + current_cc_command = "gcc" + current_cxx_command = "g++" + current_ar_command = "ar" + current_ld_command = current_cc_command + current_strip_command = + "strip --strip-unneeded" +} + + if (target_os == "") { target_os = host_os } @@ -38,4 +66,4 @@ set_defaults("source_set") { configs = _shared_binary_target_configs } -set_default_toolchain("//build/toolchain:gcc") +set_default_toolchain("//build/toolchain:${board_toolchain}") diff --git a/build/toolchain/BUILD.gn b/build/toolchain/BUILD.gn index c04ee1e..8bb44ab 100755 --- a/build/toolchain/BUILD.gn +++ b/build/toolchain/BUILD.gn @@ -2,87 +2,14 @@ # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. -toolchain("gcc") { - tool("cc") { - depfile = "{{output}}.d" - command = "gcc -MMD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_c}} -c {{source}} -o {{output}}" - depsformat = "gcc" - description = "CC {{output}}" - outputs = - [ "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o" ] - } - tool("cxx") { - depfile = "{{output}}.d" - command = "g++ -MMD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_cc}} -c {{source}} -o {{output}}" - depsformat = "gcc" - description = "CXX {{output}}" - outputs = - [ "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o" ] - } +import("//build/toolchain/gcc.gni") - tool("alink") { - command = "rm -f {{output}} && ar rcs {{output}} {{inputs}}" - description = "AR {{target_output_name}}{{output_extension}}" - - outputs = - [ "{{target_out_dir}}/{{target_output_name}}{{output_extension}}" ] - default_output_extension = ".a" - output_prefix = "lib" - } - - tool("solink") { - soname = "{{target_output_name}}{{output_extension}}" # e.g. "libfoo.so". - sofile = "{{output_dir}}/$soname" - rspfile = soname + ".rsp" - if (is_mac) { - os_specific_option = "-install_name @executable_path/$sofile" - rspfile_content = "{{inputs}} {{solibs}} {{libs}}" - } else { - os_specific_option = "-Wl,-soname=$soname" - rspfile_content = "-Wl,--whole-archive {{inputs}} {{solibs}} -Wl,--no-whole-archive {{libs}}" - } - - command = "g++ -shared {{ldflags}} -o $sofile $os_specific_option @$rspfile" - - description = "SOLINK $soname" - - # Use this for {{output_extension}} expansions unless a target manually - # overrides it (in which case {{output_extension}} will be what the target - # specifies). - default_output_extension = ".so" - - # Use this for {{output_dir}} expansions unless a target manually overrides - # it (in which case {{output_dir}} will be what the target specifies). - default_output_dir = "{{root_out_dir}}" - - outputs = [ sofile ] - link_output = sofile - depend_output = sofile - output_prefix = "lib" - } - - tool("link") { - outfile = "{{target_output_name}}{{output_extension}}" - rspfile = "$outfile.rsp" - if (is_mac) { - command = "g++ {{ldflags}} -o $outfile @$rspfile {{solibs}} {{libs}}" - } else { - command = "g++ {{ldflags}} -o $outfile -Wl,--start-group @$rspfile {{solibs}} -Wl,--end-group {{libs}}" - } - description = "LINK $outfile" - default_output_dir = "{{root_out_dir}}" - rspfile_content = "{{inputs}}" - outputs = [ outfile ] - } - - tool("stamp") { - command = "touch {{output}}" - description = "STAMP {{output}}" - } - - tool("copy") { - command = "cp -af {{source}} {{output}}" - description = "COPY {{source}} {{output}}" - } +# 使用模板定义编译工具链 +gcc_toolchain(board_toolchain){ + cc = current_cc_command + cxx = current_cxx_command + ar = current_ar_command + ld = current_ld_command + strip = current_strip_command } diff --git a/build/toolchain/gcc.gni b/build/toolchain/gcc.gni new file mode 100644 index 0000000..2b6531a --- /dev/null +++ b/build/toolchain/gcc.gni @@ -0,0 +1,137 @@ +# Copyright (c) 2020 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# invoker是调用者 应用调用者的变量 +template("gcc_toolchain") { + toolchain(target_name) { + assert(defined(invoker.ar), "gcc toolchain must specify a \"ar\" value") + assert(defined(invoker.cc), "gcc toolchain must specify a \"cc\" value") + assert(defined(invoker.cxx), "gcc toolchain must specify a \"cxx\" value") + assert(defined(invoker.ld), "gcc toolchain must specify a \"ld\" value") + + cc = invoker.cc + cxx = invoker.cxx + ar = invoker.ar + ld = invoker.ld + + need_strip = false + if (defined(invoker.strip)) { + strip = invoker.strip + need_strip = true + } + + if (defined(invoker.extra_ldflags) && invoker.extra_ldflags != "") { + extra_ldflags = " " + invoker.extra_ldflags + } else { + extra_ldflags = "" + } + + tool("cc") { + command = "$cc {{defines}} {{include_dirs}} {{cflags}} {{cflags_c}} -c {{source}} -o {{output}}" + depsformat = "gcc" + description = "gcc cross compiler {{output}}" + outputs = + [ "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o" ] + } + tool("cxx") { + depfile = "{{output}}.d" + command = "$cxx {{defines}} {{include_dirs}} {{cflags_cc}} -c {{source}} -o {{output}}" + depsformat = "gcc" + description = "gcc CXX {{output}}" + outputs = + [ "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o" ] + } + tool("asm") { + depfile = "{{output}}.d" + command = "$cc {{defines}} {{include_dirs}} {{asmflags}} {{source}} -c -o {{output}}" + depsformat = "gcc" + description = "gcc cross compiler {{output}}" + outputs = + [ "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o" ] + } + tool("alink") { + outfile = "{{output_dir}}/{{target_output_name}}{{output_extension}}" + rspfile = "{{output}}.rsp" + rspfile_content = "{{inputs}}" + command = "$ar cr {{output}} @\"$rspfile\"" + + description = "AR {{output}}" + outputs = [ outfile ] + + default_output_dir = "{{root_out_dir}}/libs" + default_output_extension = ".a" + output_prefix = "lib" + } + tool("solink") { + outfile = "{{output_dir}}/{{target_output_name}}{{output_extension}}" + unstripped_outfile = outfile + + rspfile = "$outfile.rsp" + rspfile_content = "{{inputs}}" + command = "" + if (need_strip) { + unstripped_outfile = "{{output_dir}}/unstripped/usr/lib/{{target_output_name}}{{output_extension}}" + } + command += "$ld -shared {{ldflags}} $extra_ldflags " + "-Wl,--start-group {{inputs}} {{libs}} -Wl,--end-group -o $unstripped_outfile" + if (need_strip) { + command += " && $strip \"$unstripped_outfile\" -o \"$outfile\"" + } + description = "SOLINK $outfile" + outputs = [ outfile ] + if (unstripped_outfile != outfile) { + outputs += [ unstripped_outfile ] + } + default_output_dir = "{{root_out_dir}}" + default_output_extension = ".so" + output_prefix = "lib" + } + tool("link") { + outfile = "{{output_dir}}/bin/{{target_output_name}}{{output_extension}}" + unstripped_outfile = outfile + + rspfile = "$outfile.rsp" + command = "" + if (need_strip) { + unstripped_outfile = "{{output_dir}}/unstripped/bin/{{target_output_name}}{{output_extension}}" + } + command += "$ld {{ldflags}} $extra_ldflags " + "-Wl,--start-group {{inputs}} {{libs}} -Wl,--end-group -o $unstripped_outfile " + if (need_strip) { + command += " && $strip \"$unstripped_outfile\" -o \"$outfile\"" + } + + description = "LINK $outfile" + default_output_dir = "{{root_out_dir}}" + rspfile_content = "{{inputs}}" + outputs = [ outfile ] + if (unstripped_outfile != outfile) { + outputs += [ unstripped_outfile ] + } + } + tool("stamp") { + if (host_os == "win") { + command = "cmd /c type nul > \"{{output}}\"" + } else { + command = "/usr/bin/touch {{output}}" + } + description = "STAMP {{output}}" + } + tool("copy") { + if (host_os == "win") { + command = "python $ohos_root_path/build/lite/copy_files.py --src_type=file --src={{source}} --dest_dir={{output}}" + } else if (host_os == "linux") { + command = "cp -afd {{source}} {{output}}" + } + description = "COPY {{source}} {{output}}" + } + } +}