1# Copyright (c) 2020 Huawei Device Co., Ltd. 2# Licensed under the Apache License, Version 2.0 (the "License"); 3# you may not use this file except in compliance with the License. 4# You may obtain a copy of the License at 5# 6# http://www.apache.org/licenses/LICENSE-2.0 7# 8# Unless required by applicable law or agreed to in writing, software 9# distributed under the License is distributed on an "AS IS" BASIS, 10# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11# See the License for the specific language governing permissions and 12# limitations under the License. 13 14import("//build/lite/config/toolchain/config.gni") 15 16template("clang_toolchain") { 17 toolchain(target_name) { 18 assert(defined(invoker.cc), "clang toolchain must specify a \"cc\" value") 19 assert(defined(invoker.cxx), "clang toolchain must specify a \"cxx\" value") 20 assert(defined(invoker.ar), "clang toolchain must specify a \"ar\" value") 21 assert(defined(invoker.ld), "clang toolchain must specify a \"ld\" value") 22 23 cc = invoker.cc 24 cxx = invoker.cxx 25 ar = invoker.ar 26 ld = invoker.ld 27 28 need_strip = false 29 if (defined(invoker.strip)) { 30 strip = invoker.strip 31 need_strip = true 32 } 33 34 tool("cc") { 35 save_temps = "" 36 if (enable_save_temps) { 37 save_temps = "-save-temps=obj" 38 } 39 command = "$cc {{defines}} {{cflags}} {{cflags_c}} {{include_dirs}} -c {{source}} $save_temps -o {{output}}" 40 description = "clang {{output}}" 41 outputs = 42 [ "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o" ] 43 } 44 tool("cxx") { 45 save_temps = "" 46 if (enable_save_temps) { 47 save_temps = "-save-temps=obj" 48 } 49 depfile = "{{output}}.d" 50 command = "$cxx {{defines}} {{cflags_cc}} {{include_dirs}} -c {{source}} $save_temps -o {{output}}" 51 description = "clang++ {{output}}" 52 outputs = 53 [ "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o" ] 54 } 55 tool("asm") { 56 depfile = "{{output}}.d" 57 command = "$cc {{defines}} {{include_dirs}} {{asmflags}} -c {{source}} -o {{output}}" 58 description = "ASM {{output}}" 59 outputs = 60 [ "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o" ] 61 } 62 tool("alink") { 63 outfile = "{{output_dir}}/{{target_output_name}}{{output_extension}}" 64 rspfile = "{{output}}.rsp" 65 rspfile_content = "{{inputs}}" 66 command = "$ar -cr {{output}} @\"$rspfile\"" 67 description = "AR {{output}}" 68 outputs = [ outfile ] 69 default_output_dir = "{{root_out_dir}}/libs" 70 default_output_extension = ".a" 71 output_prefix = "lib" 72 } 73 tool("solink") { 74 outfile = "{{output_dir}}/{{target_output_name}}{{output_extension}}" 75 unstripped_outfile = outfile 76 77 rspfile = "$outfile.rsp" 78 rspfile_content = "{{inputs}}" 79 command = "" 80 if (need_strip) { 81 unstripped_outfile = "{{output_dir}}/unstripped/usr/lib/{{target_output_name}}{{output_extension}}" 82 } 83 84 command += 85 "$ld -shared {{ldflags}} {{inputs}} {{libs}} -o $unstripped_outfile" 86 if (need_strip) { 87 command += " && $strip \"$unstripped_outfile\" -o \"$outfile\"" 88 } 89 90 default_output_extension = ".so" 91 description = "SOLINK $outfile" 92 default_output_dir = "{{root_out_dir}}" 93 output_prefix = "lib" 94 outputs = [ outfile ] 95 if (unstripped_outfile != outfile) { 96 outputs += [ unstripped_outfile ] 97 } 98 } 99 tool("link") { 100 outfile = "{{output_dir}}/bin/{{target_output_name}}{{output_extension}}" 101 unstripped_outfile = outfile 102 103 rspfile = "$outfile.rsp" 104 if (defined(board_custom_ld_flags)) { 105 custom_ld_flags = "" 106 foreach(ld_flags, board_custom_ld_flags) { 107 custom_ld_flags += "-Wl," 108 custom_ld_flags += ld_flags 109 custom_ld_flags += " " 110 } 111 } 112 command = "" 113 if (need_strip) { 114 unstripped_outfile = "{{output_dir}}/unstripped/bin/{{target_output_name}}{{output_extension}}" 115 } 116 command += "$ld {{ldflags}} " 117 if (defined(board_custom_ld_flags)) { 118 command += "-Wl,--whole-archive -Wl,--start-group " 119 } 120 command += "{{inputs}} {{libs}} " 121 if (defined(board_custom_ld_flags)) { 122 command += "-Wl,--end-group -Wl,--no-whole-archive $custom_ld_flags " 123 } 124 command += "-o $unstripped_outfile" 125 if (need_strip) { 126 command += " && $strip \"$unstripped_outfile\" -o \"$outfile\"" 127 } 128 129 description = "LLVM LINK $outfile" 130 default_output_dir = "{{root_out_dir}}" 131 rspfile_content = "{{inputs}}" 132 outputs = [ outfile ] 133 if (unstripped_outfile != outfile) { 134 outputs += [ unstripped_outfile ] 135 } 136 } 137 tool("stamp") { 138 if (host_os == "win") { 139 command = "cmd /c type nul > \"{{output}}\"" 140 } else { 141 command = "/usr/bin/touch {{output}}" 142 } 143 description = "STAMP {{output}}" 144 } 145 146 tool("copy") { 147 command = "cp -afd {{source}} {{output}}" 148 description = "COPY {{source}} {{output}}" 149 } 150 } 151} 152