• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
14template("gcc_toolchain") {
15  toolchain(target_name) {
16    assert(defined(invoker.ar), "gcc toolchain must specify a \"ar\" value")
17    assert(defined(invoker.cc), "gcc toolchain must specify a \"cc\" value")
18    assert(defined(invoker.cxx), "gcc toolchain must specify a \"cxx\" value")
19    assert(defined(invoker.ld), "gcc toolchain must specify a \"ld\" value")
20
21    cc = invoker.cc
22    cxx = invoker.cxx
23    ar = invoker.ar
24    ld = invoker.ld
25
26    need_strip = false
27    if (defined(invoker.strip)) {
28      strip = invoker.strip
29      need_strip = true
30    }
31
32    if (defined(invoker.extra_ldflags) && invoker.extra_ldflags != "") {
33      extra_ldflags = " " + invoker.extra_ldflags
34    } else {
35      extra_ldflags = ""
36    }
37
38    tool("cc") {
39      command = "$cc {{defines}} {{include_dirs}} {{cflags}} {{cflags_c}} -c {{source}} -o {{output}}"
40      depsformat = "gcc"
41      description = "gcc cross compiler {{output}}"
42      outputs =
43          [ "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o" ]
44    }
45    tool("cxx") {
46      depfile = "{{output}}.d"
47      command = "$cxx {{defines}} {{include_dirs}} {{cflags_cc}} -c {{source}} -o {{output}}"
48      depsformat = "gcc"
49      description = "gcc CXX {{output}}"
50      outputs =
51          [ "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o" ]
52    }
53    tool("asm") {
54      depfile = "{{output}}.d"
55      command = "$cc {{defines}} {{include_dirs}} {{asmflags}} {{source}} -c -o {{output}}"
56      depsformat = "gcc"
57      description = "gcc cross compiler {{output}}"
58      outputs =
59          [ "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o" ]
60    }
61    tool("alink") {
62      outfile = "{{output_dir}}/{{target_output_name}}{{output_extension}}"
63      rspfile = "{{output}}.rsp"
64      rspfile_content = "{{inputs}}"
65      command = "$ar cr {{output}} @\"$rspfile\""
66
67      description = "AR {{output}}"
68      outputs = [ outfile ]
69
70      default_output_dir = "{{root_out_dir}}/libs"
71      default_output_extension = ".a"
72      output_prefix = "lib"
73    }
74    tool("solink") {
75      outfile = "{{output_dir}}/{{target_output_name}}{{output_extension}}"
76      unstripped_outfile = outfile
77
78      rspfile = "$outfile.rsp"
79      rspfile_content = "{{inputs}}"
80      command = ""
81      if (need_strip) {
82        unstripped_outfile = "{{output_dir}}/unstripped/usr/lib/{{target_output_name}}{{output_extension}}"
83      }
84      command += "$ld -shared {{ldflags}} $extra_ldflags " + "-Wl,--start-group {{inputs}} {{libs}} -Wl,--end-group -o $unstripped_outfile"
85      if (need_strip) {
86        command += " && $strip \"$unstripped_outfile\" -o \"$outfile\""
87      }
88      description = "SOLINK $outfile"
89      outputs = [ outfile ]
90      if (unstripped_outfile != outfile) {
91        outputs += [ unstripped_outfile ]
92      }
93      default_output_dir = "{{root_out_dir}}"
94      default_output_extension = ".so"
95      output_prefix = "lib"
96    }
97    tool("link") {
98      outfile = "{{output_dir}}/bin/{{target_output_name}}{{output_extension}}"
99      unstripped_outfile = outfile
100
101      rspfile = "$outfile.rsp"
102      command = ""
103      if (need_strip) {
104        unstripped_outfile = "{{output_dir}}/unstripped/bin/{{target_output_name}}{{output_extension}}"
105      }
106      command += "$ld {{ldflags}} $extra_ldflags " + "-Wl,--start-group {{inputs}} {{libs}} -Wl,--end-group -o $unstripped_outfile "
107      if (need_strip) {
108        command += " && $strip \"$unstripped_outfile\" -o \"$outfile\""
109      }
110
111      description = "LINK $outfile"
112      default_output_dir = "{{root_out_dir}}"
113      rspfile_content = "{{inputs}}"
114      outputs = [ outfile ]
115      if (unstripped_outfile != outfile) {
116        outputs += [ unstripped_outfile ]
117      }
118    }
119    tool("stamp") {
120      if (host_os == "win") {
121        command = "cmd /c type nul > \"{{output}}\""
122      } else {
123        command = "/usr/bin/touch {{output}}"
124      }
125      description = "STAMP {{output}}"
126    }
127    tool("copy") {
128      if (host_os == "win") {
129        command = "python $ohos_root_path/build/lite/copy_files.py --src_type=file --src={{source}} --dest_dir={{output}}"
130      } else if (host_os == "linux") {
131        command = "cp -afd {{source}} {{output}}"
132      }
133      description = "COPY {{source}} {{output}}"
134    }
135  }
136}
137