• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2014 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5# This provides the yasm_assemble() template which uses YASM to assemble
6# assembly files.
7#
8# Files to be assembled with YASM should have an extension of .asm.
9#
10# Parameters
11#
12#   yasm_flags (optional)
13#       [list of strings] Pass additional flags into YASM. These are appended
14#       to the command line. Note that the target machine type and system is
15#       already set up based on the current toolchain so you don't need to
16#       specify these things (see below).
17#
18#       Example: yasm_flags = [ "--force-strict" ]
19#
20#   include_dirs (optional)
21#       [list of dir names] List of additional include dirs. Note that the
22#       source root and the root generated file dir is always added, just like
23#       our C++ build sets up.
24#
25#       Example: include_dirs = [ "//some/other/path", target_gen_dir ]
26#
27#   defines (optional)
28#       [list of strings] List of defines, as with the native code defines.
29#
30#       Example: defines = [ "FOO", "BAR=1" ]
31#
32#   inputs, deps, visibility  (optional)
33#       These have the same meaning as in an action.
34#
35# Example
36#
37#   yasm_assemble("my_yasm_target") {
38#     sources = [
39#       "ultra_optimized_awesome.asm",
40#     ]
41#     include_dirs = [ "assembly_include" ]
42#   }
43
44if (is_mac || is_ios) {
45  if (current_cpu == "x86") {
46    _yasm_flags = [
47      "-fmacho32",
48      "-m",
49      "x86",
50    ]
51  } else if (current_cpu == "x64") {
52    _yasm_flags = [
53      "-fmacho64",
54      "-m",
55      "amd64",
56    ]
57  }
58} else if (is_posix || is_fuchsia) {
59  if (current_cpu == "x86") {
60    _yasm_flags = [
61      "-felf32",
62      "-m",
63      "x86",
64    ]
65  } else if (current_cpu == "x64") {
66    _yasm_flags = [
67      "-DPIC",
68      "-felf64",
69      "-m",
70      "amd64",
71    ]
72  }
73} else if (is_win) {
74  if (current_cpu == "x86") {
75    _yasm_flags = [
76      "-DPREFIX",
77      "-fwin32",
78      "-m",
79      "x86",
80    ]
81  } else if (current_cpu == "x64") {
82    _yasm_flags = [
83      "-fwin64",
84      "-m",
85      "amd64",
86    ]
87  }
88}
89
90if (is_win) {
91  asm_obj_extension = "obj"
92} else {
93  asm_obj_extension = "o"
94}
95
96template("yasm_assemble") {
97  assert(defined(invoker.sources), "Need sources defined for $target_name")
98
99  # Only depend on YASM on x86 systems. Force compilation of .asm files for
100  # ARM to fail.
101  assert(current_cpu == "x86" || current_cpu == "x64")
102
103  action_name = "${target_name}_action"
104  source_set_name = target_name
105
106  action_foreach(action_name) {
107    # Only the source set can depend on this.
108    visibility = [ ":$source_set_name" ]
109
110    script = "//third_party/yasm/run_yasm.py"
111    sources = invoker.sources
112
113    if (defined(invoker.inputs)) {
114      inputs = invoker.inputs
115    }
116
117    # Executable (first in the args). The binary might be in the root build dir
118    # (no cross-compiling) or in a toolchain-specific subdirectory of that
119    # (when cross-compiling).
120    yasm_label = "//third_party/yasm($host_toolchain)"
121    args = [ "./" +  # Force current dir.
122             rebase_path(get_label_info(yasm_label, "root_out_dir") + "/yasm",
123                         root_build_dir) ]
124
125    # Deps.
126    deps = [
127      yasm_label,
128    ]
129    if (defined(invoker.deps)) {
130      deps += invoker.deps
131    }
132
133    # Flags.
134    args += _yasm_flags
135    if (defined(invoker.yasm_flags)) {
136      args += invoker.yasm_flags
137    }
138
139    # User defined include dirs go first.
140    if (defined(invoker.include_dirs)) {
141      foreach(include, invoker.include_dirs) {
142        args += [ "-I" + rebase_path(include, root_build_dir) ]
143      }
144    }
145
146    # Default yasm include dirs. Make it match the native build (source root and
147    # root generated code directory).
148    # This goes to the end of include list.
149    args += [
150      "-I.",
151
152      # Using "//." will produce a relative path "../.." which looks better than
153      # "../../" which will result from using "//" as the base (although both
154      # work). This is because rebase_path will terminate the result in a
155      # slash if the input ends in a slash.
156      "-I" + rebase_path("//.", root_build_dir),
157      "-I" + rebase_path(root_gen_dir, root_build_dir),
158    ]
159
160    # Extra defines.
161    if (defined(invoker.defines)) {
162      foreach(def, invoker.defines) {
163        args += [ "-D$def" ]
164      }
165    }
166
167    # Output file.
168    outputs = [
169      "$target_out_dir/$source_set_name/{{source_name_part}}.o",
170    ]
171    args += [
172      "-o",
173      rebase_path(outputs[0], root_build_dir),
174      "{{source}}",
175    ]
176
177    # The wrapper script run_yasm will write the depfile to the same name as
178    # the output but with .d appended (like gcc will).
179    depfile = outputs[0] + ".d"
180  }
181
182  # Gather the .o files into a linkable thing. This doesn't actually link
183  # anything (a source set just compiles files to link later), but will pass
184  # the object files generated by the action up the dependency chain.
185  static_library(source_set_name) {
186    if (defined(invoker.visibility)) {
187      visibility = invoker.visibility
188    }
189
190    sources = get_target_outputs(":$action_name")
191
192    # Do not publicize any header to remove build dependency.
193    public = []
194
195    deps = [
196      ":$action_name",
197    ]
198  }
199}
200