• 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 +=
85          "$ld -shared {{ldflags}} $extra_ldflags " +
86          "-Wl,--start-group {{inputs}} {{libs}} -Wl,--end-group -o $unstripped_outfile"
87      if (need_strip) {
88        command += " && $strip \"$unstripped_outfile\" -o \"$outfile\""
89      }
90      description = "SOLINK $outfile"
91      outputs = [ outfile ]
92      if (unstripped_outfile != outfile) {
93        outputs += [ unstripped_outfile ]
94      }
95      default_output_dir = "{{root_out_dir}}"
96      default_output_extension = ".so"
97      output_prefix = "lib"
98    }
99    tool("link") {
100      outfile = "{{output_dir}}/bin/{{target_output_name}}{{output_extension}}"
101      unstripped_outfile = outfile
102
103      rspfile = "$outfile.rsp"
104      command = ""
105      if (need_strip) {
106        unstripped_outfile = "{{output_dir}}/unstripped/bin/{{target_output_name}}{{output_extension}}"
107      }
108      command +=
109          "$ld {{ldflags}} $extra_ldflags " +
110          "-Wl,--start-group {{inputs}} {{libs}} -Wl,--end-group -o $unstripped_outfile "
111      if (need_strip) {
112        command += " && $strip \"$unstripped_outfile\" -o \"$outfile\""
113      }
114
115      description = "LINK $outfile"
116      default_output_dir = "{{root_out_dir}}"
117      rspfile_content = "{{inputs}}"
118      outputs = [ outfile ]
119      if (unstripped_outfile != outfile) {
120        outputs += [ unstripped_outfile ]
121      }
122    }
123    tool("stamp") {
124      if (host_os == "win") {
125        command = "cmd /c type nul > \"{{output}}\""
126      } else {
127        command = "/usr/bin/touch {{output}}"
128      }
129      description = "STAMP {{output}}"
130    }
131    tool("copy") {
132      if (host_os == "win") {
133        command = "python $ohos_root_path/build/lite/copy_files.py --src_type=file --src={{source}} --dest_dir={{output}}"
134      } else if (host_os == "linux") {
135        command = "cp -afd {{source}} {{output}}"
136      }
137      description = "COPY {{source}} {{output}}"
138    }
139  }
140}
141