blob: 4c3d534502dd91dffa02aaab6be6962f9a85da3c [file] [log] [blame]
Dale Curtis9596cc02018-10-31 14:25:55 -07001# Copyright 2018 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 nasm_assemble() template which uses NASM to assemble
6# assembly files.
7#
8# Files to be assembled with NASM should have an extension of .asm.
9#
10# Parameters
11#
12# nasm_flags (optional)
13# [list of strings] Pass additional flags into NASM. These are appended
14# to the command line. Note that the output format is already set up
15# based on the current toolchain so you don't need to specify these
16# things (see below).
17#
18# Example: nasm_flags = [ "-Wall" ]
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# nasm_assemble("my_nasm_target") {
38# sources = [
39# "ultra_optimized_awesome.asm",
40# ]
41# include_dirs = [ "assembly_include" ]
42# }
43
44import("//build/compiled_action.gni")
Byoungchan Lee3dc3d472021-07-27 05:09:09 +090045import("//build/config/ios/config.gni")
46import("//build/config/ios/ios_sdk_overrides.gni")
47if (is_mac) {
48 import("//build/config/mac/mac_sdk.gni")
49}
Dale Curtis9596cc02018-10-31 14:25:55 -070050
Byoungchan Lee3dc3d472021-07-27 05:09:09 +090051if ((is_mac || is_ios) && (current_cpu == "x86" || current_cpu == "x64")) {
Dale Curtis9596cc02018-10-31 14:25:55 -070052 if (current_cpu == "x86") {
53 _nasm_flags = [ "-fmacho32" ]
54 } else if (current_cpu == "x64") {
55 _nasm_flags = [ "-fmacho64" ]
56 }
Byoungchan Lee3dc3d472021-07-27 05:09:09 +090057 if (is_mac) {
Byoungchan Lee335fe832021-09-17 20:17:59 +090058 _nasm_flags += [ "--macho-min-os=macos$mac_deployment_target" ]
Byoungchan Lee3dc3d472021-07-27 05:09:09 +090059 } else if (is_ios) {
60 if (target_environment == "device") {
61 _nasm_flags += [ "--macho-min-os=ios$ios_deployment_target" ]
62 } else {
63 _nasm_flags +=
64 [ "--macho-min-os=ios$ios_deployment_target-$target_environment" ]
65 }
66 }
Dale Curtis9596cc02018-10-31 14:25:55 -070067} else if (is_posix || is_fuchsia) {
68 if (current_cpu == "x86") {
69 _nasm_flags = [ "-felf32" ]
70 } else if (current_cpu == "x64") {
71 _nasm_flags = [
72 "-DPIC",
73 "-felf64",
74 ]
75 }
76} else if (is_win) {
77 if (current_cpu == "x86") {
78 _nasm_flags = [
79 "-DPREFIX",
80 "-fwin32",
81 ]
82 } else if (current_cpu == "x64") {
83 _nasm_flags = [ "-fwin64" ]
84 }
85}
86
87if (is_win) {
88 asm_obj_extension = "obj"
89} else {
90 asm_obj_extension = "o"
91}
92
93template("nasm_assemble") {
94 assert(defined(invoker.sources), "Need sources defined for $target_name")
95
96 # Only depend on NASM on x86 systems. Force compilation of .asm files for
97 # ARM to fail.
98 assert(current_cpu == "x86" || current_cpu == "x64")
99
100 action_name = "${target_name}_action"
101 source_set_name = target_name
102
103 compiled_action_foreach(action_name) {
104 # Only the source set can depend on this.
105 visibility = [ ":$source_set_name" ]
106
107 tool = "//third_party/nasm"
108
109 forward_variables_from(invoker,
110 [
111 "sources",
112 "inputs",
113 "deps",
114 ])
115
116 # Flags.
117 args = _nasm_flags
118 if (defined(invoker.nasm_flags)) {
119 args += invoker.nasm_flags
120 }
121
122 # User defined include dirs go first.
123 if (defined(invoker.include_dirs)) {
124 foreach(include, invoker.include_dirs) {
125 # NASM does not append path separators when processing the -I flags, so
126 # -Ifoo means includes of bar look up "foobar" rather than "foo/bar".
127 # Add the trailing slash for it.
128 args += [ "-I" + rebase_path(include, root_build_dir) + "/" ]
129 }
130 }
131
132 # Default nasm include dirs. Make it match the native build (source root and
133 # root generated code directory).
134 # This goes to the end of include list. Note that, as above, we must append
135 # path separators because NASM does not do it itself.
136 args += [
137 "-I./",
138
139 # rebase_path("//") already includes a trailing slash.
140 "-I" + rebase_path("//", root_build_dir),
141 "-I" + rebase_path(root_gen_dir, root_build_dir) + "/",
142 ]
143
144 # Extra defines.
145 if (defined(invoker.defines)) {
146 foreach(def, invoker.defines) {
147 args += [ "-D$def" ]
148 }
149 }
150
151 # Output file.
152 outputs = [
153 "$target_out_dir/$source_set_name/{{source_name_part}}.o",
154 ]
155 args += [
156 "-MD",
157 rebase_path(outputs[0] + ".d", root_build_dir),
158 "-o",
159 rebase_path(outputs[0], root_build_dir),
160 "{{source}}",
161 ]
162
163 depfile = outputs[0] + ".d"
164 }
165
166 # Gather the .o files into a linkable thing. This doesn't actually link
167 # anything (a source set just compiles files to link later), but will pass
168 # the object files generated by the action up the dependency chain.
169 static_library(source_set_name) {
170 if (defined(invoker.visibility)) {
171 visibility = invoker.visibility
172 }
173
Dale Curtis7268fcf2019-02-21 16:35:06 -0800174 if (defined(invoker.all_dependent_configs)) {
175 all_dependent_configs = invoker.all_dependent_configs
176 }
177
Dale Curtis9596cc02018-10-31 14:25:55 -0700178 sources = get_target_outputs(":$action_name")
179
180 # Do not publicize any header to remove build dependency.
181 public = []
182
183 deps = [
184 ":$action_name",
185 ]
186 }
187}