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