1# Copyright (C) 2021 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("//gn/wasm.gni") 15declare_args() { 16 if (target_os == "linux" || target_os == "macx") { 17 cc = "clang" 18 cxx = "clang++" 19 pic = "-fPIC" 20 rebuild_string = "" 21 extra_asmflags = "" 22 asm = "clang" 23 } else if (target_os == "windows") { 24 cc = "gcc.exe" 25 cxx = "g++.exe" 26 rebuild_string = "" 27 extra_asmflags = "" 28 asm = "gcc.exe" 29 pic = "" 30 } 31 if (use_wasm == true) { 32 print("make_wasm") 33 } else if (use_wasm == false) { 34 print("no make_wasm") 35 } 36 cc_wrapper = "" 37} 38toolchain("wasm") { 39 # emsdk_dir and em_config are defined in wasm.gni. 40 print("use gcc_like_chain wasm") 41 if (!is_macx) { 42 ar = "$emsdk_dir/emscripten/emar --em-config $em_config" 43 } 44 cc = "$emsdk_dir/emscripten/emcc --em-config $em_config" 45 cxx = "$emsdk_dir/emscripten/em++ --em-config $em_config" 46 47 lib_switch = "-l" 48 ld_arg = "" 49 lib_dir_switch = "-L" 50 external_cxxflags = "" 51 external_cflags = "" 52 external_ldflags = "" 53 if (defined(linker) && linker != "") { 54 ld_arg = "-fuse-ld=$_invoker_linker" 55 _invoker_linker = linker 56 } 57 if (defined(sysroot) && sysroot != "") { 58 _invoker_sysroot = sysroot 59 cxx = "$cxx --sysroot=$_invoker_sysroot" 60 cc = "$cc --sysroot=$_invoker_sysroot" 61 } 62 if (defined(gcc_toolchain) && gcc_toolchain != "") { 63 ld_arg = "$ld_arg --gcc-toolchain=$gcc_toolchain" 64 } 65 if (defined(external_cxxflags)) { 66 print("defined external_cxxflags") 67 external_cxxflags = external_cxxflags 68 } 69 if (defined(external_cflags)) { 70 external_cflags = external_cflags 71 } 72 73 tool("cc") { 74 depfiles = "{{output}}.d" 75 command = "$cc_wrapper $cc -MMD -MF $depfiles {{defines}} {{include_dirs}} {{cflags}} {{cflags_c}} ${external_cflags} -c {{source}} -o {{output}}" 76 outputfiles = 77 "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o" 78 outputs = [ outputfiles ] 79 description = "step: compile {{source}}" 80 } 81 82 tool("cxx") { 83 depfiles = "{{output}}.d" # must be defined 84 command = "$cc_wrapper $cxx -MMD -MF $depfiles {{defines}} {{include_dirs}} {{cflags}} {{cflags_cc}} ${external_cflags} ${external_cxxflags} -c {{source}} -o {{output}}" 85 86 outputfiles = 87 "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o" 88 outputs = [ outputfiles ] 89 description = "step: compile {{source}}" 90 } 91 tool("alink") { 92 rspfile = "{{output}}.rsp" # must be defined 93 rspfile_content = "{{inputs}}" 94 if (is_macx) { 95 command = "rm -f {{output}} && libtool -static {{arflags}} -o {{output}} -filelist $rspfile" 96 } else { 97 command = "rm -rf {{output}} && $ar rcsD {{output}} @$rspfile" 98 } 99 outputfiles = "{{root_out_dir}}/{{target_output_name}}{{output_extension}}" 100 outputs = [ outputfiles ] 101 output_prefix = "lib" 102 default_output_extension = ".a" 103 description = "step: link {{output}}" 104 } 105 tool("solink") { 106 sonames = "{{target_output_name}}{{output_extension}}" 107 outputfiles = "{{root_out_dir}}/$sonames" 108 unstripped_so = outputfiles 109 rpath = "" 110 outputs = [ outputfiles ] 111 command = "$cc_wrapper $cxx $ld_arg -shared {{ldflags}} ${external_ldflags} {{inputs}} {{solibs}} {{libs}} $rpath -o {{output}}" 112 output_prefix = "lib" 113 description = "step: link $unstripped_so" 114 default_output_extension = ".so" 115 } 116 117 tool("link") { 118 outputfiles = "{{root_out_dir}}/{{target_output_name}}{{output_extension}}" 119 outputs = [ outputfiles ] 120 command = "$cc_wrapper $cxx $ld_arg {{ldflags}} ${external_ldflags} {{inputs}} {{solibs}} {{libs}} -o {{output}}" 121 description = "step:link {{output}}" 122 } 123 124 tool("stamp") { 125 description = "step: stamp {{output}}" 126 command = "touch {{output}}" 127 } 128 129 tool("copy") { 130 description = "step: COPY files from {{source}} to {{output}}" 131 command = "cp -arf {{source}} {{output}}" 132 } 133} 134toolchain("gcc_like") { 135 lib_switch = "-l" 136 lib_dir_switch = "-L" 137 138 tool("cxx") { 139 depfile = "{{output}}.d" # must be defined 140 command = "$cxx -o {{output}} -MMD -MF $depfile {{defines}} $pic {{include_dirs}} {{cflags}} {{cflags_cc}} -c {{source}}" 141 outputfiles = 142 "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o" 143 outputs = [ outputfiles ] 144 description = "step: compile {{source}}" 145 } 146 147 tool("cc") { 148 depfile = "{{output}}.d" 149 command = "$cc -o {{output}} -MMD -MF $depfile {{defines}} $pic {{include_dirs}} {{cflags}} {{cflags_c}} -c {{source}}" 150 outputfiles = 151 "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o" 152 outputs = [ outputfiles ] 153 description = "step: compile {{source}}" 154 } 155 tool("asm") { 156 depfile = "{{output}}.d" 157 command = "$asm -o {{output}} -MMD -MF $depfile ${rebuild_string}{{defines}} {{include_dirs}} {{asmflags}}${extra_asmflags} -c {{source}}" 158 depsformat = "gcc" 159 outputfiles = 160 "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o" 161 outputs = [ outputfiles ] 162 } 163 tool("alink") { 164 rspfile = "{{output}}.rsp" # this must be defined 165 if (is_macx) { 166 rspfile_content = "{{inputs_newline}}" 167 command = "rm -f {{output}} && libtool -static {{arflags}} -filelist $rspfile -o {{output}}" 168 } else { 169 rspfile_content = "{{inputs}}" # this must be defined 170 command = "rm -f {{output}} && $ar rcsD {{output}} @$rspfile" 171 } 172 outputsfiles = "{{root_out_dir}}/{{target_output_name}}{{output_extension}}" 173 outputs = [ outputsfiles ] 174 default_output_extension = ".a" 175 output_prefix = "lib" 176 description = "step: link {{output}}" 177 } 178 179 tool("link") { 180 command = "$cxx -o {{output}} {{ldflags}} {{inputs}} {{solibs}} {{libs}}" 181 outputsfiles = "{{root_out_dir}}/{{target_output_name}}{{output_extension}}" 182 outputs = [ outputsfiles ] 183 description = "step: link {{output}}" 184 } 185 tool("solink") { 186 default_output_extension = ".so" 187 command = 188 "$cxx -o {{output}} {{ldflags}} {{inputs}} {{solibs}} {{libs}} -shared" 189 outputsfiles = "{{root_out_dir}}/{{target_output_name}}{{output_extension}}" 190 outputs = [ outputsfiles ] 191 description = "step: solink {{output}}" 192 } 193 194 tool("stamp") { 195 print("need do nothing") 196 description = "step: touch {{output}}" 197 command = "touch {{output}}" 198 } 199 200 tool("copy") { 201 description = "COPY files from {{source}} to {{output}}" 202 command = "cp -arf {{source}} {{output}}" 203 } 204} 205