使用gcc编译器模板

This commit is contained in:
2025-01-16 11:03:48 +08:00
parent 5f380eaa04
commit 4536cef906
3 changed files with 174 additions and 82 deletions

View File

@@ -2,6 +2,34 @@
# Use of this source code is governed by a BSD-style license that can be # Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file. # 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 == "") { if (target_os == "") {
target_os = host_os target_os = host_os
} }
@@ -38,4 +66,4 @@ set_defaults("source_set") {
configs = _shared_binary_target_configs configs = _shared_binary_target_configs
} }
set_default_toolchain("//build/toolchain:gcc") set_default_toolchain("//build/toolchain:${board_toolchain}")

View File

@@ -2,87 +2,14 @@
# Use of this source code is governed by a BSD-style license that can be # Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file. # 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") { import("//build/toolchain/gcc.gni")
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" ]
}
tool("alink") { # 使用模板定义编译工具链
command = "rm -f {{output}} && ar rcs {{output}} {{inputs}}" gcc_toolchain(board_toolchain){
description = "AR {{target_output_name}}{{output_extension}}" cc = current_cc_command
cxx = current_cxx_command
outputs = ar = current_ar_command
[ "{{target_out_dir}}/{{target_output_name}}{{output_extension}}" ] ld = current_ld_command
default_output_extension = ".a" strip = current_strip_command
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}}"
}
} }

137
build/toolchain/gcc.gni Normal file
View File

@@ -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}}"
}
}
}