• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved.
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_mac) {
42    ar = "emar"
43  }
44  cc = "emcc"
45  cxx = "em++"
46  if (!use_local_emsdk) {
47    if (!is_mac) {
48      ar = "$emsdk_dir/emscripten/emar --em-config $em_config"
49    }
50    cc = "$emsdk_dir/emscripten/emcc --em-config $em_config"
51    cxx = "$emsdk_dir/emscripten/em++ --em-config $em_config"
52  }
53  lib_switch = "-l"
54  ld_arg = ""
55  lib_dir_switch = "-L"
56  external_cxxflags = ""
57  external_cflags = ""
58  external_ldflags = ""
59  if (defined(linker) && linker != "") {
60    ld_arg = "-fuse-ld=$_invoker_linker"
61    _invoker_linker = linker
62  }
63  if (defined(sysroot) && sysroot != "") {
64    _invoker_sysroot = sysroot
65    cxx = "$cxx --sysroot=$_invoker_sysroot"
66    cc = "$cc --sysroot=$_invoker_sysroot"
67  }
68  if (defined(gcc_toolchain) && gcc_toolchain != "") {
69    ld_arg = "$ld_arg --gcc-toolchain=$gcc_toolchain"
70  }
71  if (defined(external_cxxflags)) {
72    print("defined external_cxxflags")
73    external_cxxflags = external_cxxflags
74  }
75  if (defined(external_cflags)) {
76    external_cflags = external_cflags
77  }
78
79  tool("cc") {
80    depfiles = "{{output}}.d"
81    command = "$cc_wrapper $cc -MMD -MF $depfiles {{defines}} {{include_dirs}} {{cflags}} {{cflags_c}} ${external_cflags} -c {{source}} -o {{output}}"
82    outputfiles =
83        "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o"
84    outputs = [ outputfiles ]
85    description = "step: compile {{source}}"
86  }
87
88  tool("cxx") {
89    depfiles = "{{output}}.d"  # must be defined
90    command = "$cc_wrapper $cxx -MMD -MF $depfiles {{defines}} {{include_dirs}} {{cflags}} {{cflags_cc}}  ${external_cflags} ${external_cxxflags} -c {{source}} -o {{output}}"
91
92    outputfiles =
93        "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o"
94    outputs = [ outputfiles ]
95    description = "step: compile {{source}}"
96  }
97  tool("alink") {
98    rspfile = "{{output}}.rsp"  # must be defined
99    rspfile_content = "{{inputs}}"
100    if (is_mac) {
101      command = "rm -f {{output}} && libtool -static {{arflags}} -o {{output}} -filelist $rspfile"
102    } else {
103      command = "rm -rf {{output}} && $ar rcsD {{output}} @$rspfile"
104    }
105    outputfiles = "{{root_out_dir}}/{{target_output_name}}{{output_extension}}"
106    outputs = [ outputfiles ]
107    output_prefix = "lib"
108    default_output_extension = ".a"
109    description = "step: link {{output}}"
110  }
111  tool("solink") {
112    sonames = "{{target_output_name}}{{output_extension}}"
113    outputfiles = "{{root_out_dir}}/$sonames"
114    unstripped_so = outputfiles
115    rpath = ""
116    outputs = [ outputfiles ]
117    command = "$cc_wrapper $cxx $ld_arg -shared {{ldflags}} ${external_ldflags} {{inputs}} {{solibs}} {{libs}} $rpath -o {{output}}"
118    output_prefix = "lib"
119    description = "step: link $unstripped_so"
120    default_output_extension = ".so"
121  }
122
123  tool("link") {
124    outputfiles = "{{root_out_dir}}/{{target_output_name}}{{output_extension}}"
125    outputs = [ outputfiles ]
126    command = "$cc_wrapper $cxx $ld_arg {{ldflags}} ${external_ldflags} {{inputs}} {{solibs}} {{libs}} -o {{output}}"
127    description = "step:link {{output}}"
128  }
129
130  tool("stamp") {
131    description = "step: stamp {{output}}"
132    command = "touch {{output}}"
133  }
134
135  tool("copy") {
136    description = "step: COPY files from {{source}} to {{output}}"
137    command = "cp -arf {{source}} {{output}}"
138  }
139}
140toolchain("gcc_like") {
141  lib_switch = "-l"
142  lib_dir_switch = "-L"
143
144  tool("cxx") {
145    depfile = "{{output}}.d"  # must be defined
146    command = "$cxx -o {{output}} -MMD -MF $depfile {{defines}} $pic {{include_dirs}} {{cflags}} {{cflags_cc}} -c {{source}}"
147    outputfiles =
148        "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o"
149    outputs = [ outputfiles ]
150    description = "step: compile {{source}}"
151  }
152
153  tool("cc") {
154    depfile = "{{output}}.d"
155    command = "$cc -o {{output}} -MMD -MF $depfile {{defines}} $pic {{include_dirs}} {{cflags}} {{cflags_c}} -c {{source}}"
156    outputfiles =
157        "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o"
158    outputs = [ outputfiles ]
159    description = "step: compile {{source}}"
160  }
161  tool("asm") {
162    depfile = "{{output}}.d"
163    command = "$asm -o {{output}} -MMD -MF $depfile ${rebuild_string}{{defines}} {{include_dirs}} {{asmflags}}${extra_asmflags} -c {{source}}"
164    depsformat = "gcc"
165    outputfiles =
166        "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o"
167    outputs = [ outputfiles ]
168  }
169  tool("alink") {
170    rspfile = "{{output}}.rsp"  # this must be defined
171    if (is_mac) {
172      rspfile_content = "{{inputs_newline}}"
173      command = "rm -f {{output}} && libtool -static {{arflags}} -filelist $rspfile -o {{output}}"
174    } else {
175      rspfile_content = "{{inputs}}"  # this must be defined
176      command = "rm -f {{output}} && $ar rcsD {{output}} @$rspfile"
177      if (is_mingw) {
178        command = "$ar rcsD {{output}} @$rspfile"
179      }
180    }
181    outputsfiles = "{{root_out_dir}}/{{target_output_name}}{{output_extension}}"
182    outputs = [ outputsfiles ]
183    default_output_extension = ".a"
184    output_prefix = "lib"
185    description = "step: link {{output}}"
186  }
187
188  tool("link") {
189    if (is_mac) {
190      command = "$cxx -o {{output}} {{ldflags}} {{inputs}} {{solibs}} {{libs}}"
191    } else if (is_win) {
192      command = "$cxx {{ldflags}} -o {{output}} --static -Wl,--start-group {{inputs}} {{solibs}} {{libs}} -Wl,--end-group -lshell32 -lole32 -luuid -ladvapi32"
193    } else {
194      command = "$cxx {{ldflags}} -o {{output}} -static-libstdc++ -Wl,--start-group {{inputs}} {{solibs}} {{libs}} -Wl,--end-group"
195    }
196    outputsfiles = "{{root_out_dir}}/{{target_output_name}}{{output_extension}}"
197    outputs = [ outputsfiles ]
198    description = "step: link {{output}}"
199  }
200  tool("solink") {
201    default_output_extension = ".so"
202    command =
203        "$cxx -o {{output}} {{ldflags}} {{inputs}}  {{solibs}} {{libs}} -shared"
204    outputsfiles = "{{root_out_dir}}/{{target_output_name}}{{output_extension}}"
205    outputs = [ outputsfiles ]
206    description = "step: solink {{output}}"
207  }
208
209  tool("stamp") {
210    print("need do nothing")
211    description = "step: touch {{output}}"
212    command = "touch {{output}}"
213  }
214
215  tool("copy") {
216    description = "COPY files from  {{source}} to {{output}}"
217    command = "cp -arf {{source}} {{output}}"
218  }
219}
220