blob: 93b7e9bfd8853fa7017a4e061d1c86b40f0f4db5 [file] [log] [blame]
John Rosasco24cbdab2019-09-25 14:14:35 -07001# Copyright 2019 Google LLC. 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# import("//gn/skia.gni")
6
7declare_args() {
8 using_fuchsia_sdk = true
9
10 # Fuchsia SDK install dir.
11 fuchsia_sdk_path = "//fuchsia/sdk/$host_os"
12
13 # Clang install dir.
14 fuchsia_toolchain_path = "//fuchsia/toolchain/$host_os"
15
16 # Path to GN-generated GN targets derived from parsing json file at
17 # |fuchsia_sdk_manifest_path|. The parsing logic can be found in sdk.gni.
18 fuchsia_sdk_root = "//build/fuchsia"
19}
20
21declare_args() {
22 fuchsia_sdk_manifest_path = "${fuchsia_sdk_path}/meta/manifest.json"
23}
24
25template("_fuchsia_sysroot") {
26 assert(defined(invoker.meta), "The meta.json file path must be specified.")
27 assert(target_cpu == "x64" || target_cpu == "arm64",
28 "We currently only support 'x64' and 'arm64' targets for fuchsia.")
29
30 meta_json = read_file(invoker.meta, "json")
31
32 assert(meta_json.type == "sysroot")
33
34 meta_json_versions = meta_json.versions
35 if (target_cpu == "x64") {
36 defs = meta_json_versions.x64
37 } else {
38 defs = meta_json_versions.arm64
39 }
40
41 _libs = []
42 _lib_dirs = []
43 _include_dirs = []
44
45 foreach(link_lib, defs.link_libs) {
46 if (link_lib != "arch/${target_cpu}/sysroot/lib/Scrt1.o") {
47 _libs += [ rebase_path("$fuchsia_sdk_path/$link_lib") ]
48 }
49 }
50
51 defs_include_dir = defs.include_dir
52 _include_dirs += [ rebase_path("$fuchsia_sdk_path/$defs_include_dir") ]
53
54 config_name = "config_$target_name"
55 config(config_name) {
56 lib_dirs = _lib_dirs
57 libs = _libs
58 include_dirs = _include_dirs
59 }
60
61 group(target_name) {
62 public_configs = [ ":$config_name" ]
63 }
64}
65
66template("_fuchsia_fidl_library") {
67 assert(defined(invoker.meta), "The meta.json file path must be specified.")
68 assert(target_cpu == "x64" || target_cpu == "arm64",
69 "We currently only support 'x64' and 'arm64' targets for fuchsia.")
70
71 meta_json = read_file(invoker.meta, "json")
72
73 assert(meta_json.type == "fidl_library")
74
75 _deps = [ "../pkg:fidl_cpp" ]
76
Yilong Li437c7852020-06-09 15:34:46 -070077 library_name = meta_json.name
John Rosasco24cbdab2019-09-25 14:14:35 -070078 library_name_json = "$library_name.json"
79
80 foreach(dep, meta_json.deps) {
81 _deps += [ ":$dep" ]
82 }
83
84 config_name = "config_$target_name"
85 config(config_name) {
86 include_dirs = [ target_gen_dir ]
87 }
88
89 fidl_gen_target_name = "fidlgen_$target_name"
90 action(fidl_gen_target_name) {
91 script = "//build/fuchsia/fidl_gen_cpp"
92
93 library_name_slashes = string_replace(library_name, ".", "/")
94
Mike Klein96f64012020-04-03 10:59:37 -050095 inputs = [ invoker.meta ]
John Rosasco24cbdab2019-09-25 14:14:35 -070096
97 outputs = [
Yilong Li437c7852020-06-09 15:34:46 -070098 "$target_gen_dir/$library_name_slashes/cpp/fidl.h",
99 "$target_gen_dir/$library_name_slashes/cpp/fidl.cc",
Mitchell Kember0126a832023-12-08 14:49:21 -0800100 "$target_gen_dir/$library_name_slashes/cpp/tables.c",
John Rosasco24cbdab2019-09-25 14:14:35 -0700101 ]
102
103 args = [
104 "--fidlc-bin",
Mike Klein96f64012020-04-03 10:59:37 -0500105 rebase_path("$fuchsia_sdk_path/tools/fidlc"),
John Rosasco24cbdab2019-09-25 14:14:35 -0700106 "--fidlgen-bin",
Mike Klein96f64012020-04-03 10:59:37 -0500107 rebase_path("$fuchsia_sdk_path/tools/fidlgen"),
John Rosasco24cbdab2019-09-25 14:14:35 -0700108 "--sdk-base",
Mike Klein96f64012020-04-03 10:59:37 -0500109 rebase_path("$fuchsia_sdk_path"),
John Rosasco24cbdab2019-09-25 14:14:35 -0700110 "--root",
111 rebase_path(invoker.meta),
112 "--json",
113 rebase_path("$target_gen_dir/$library_name_json"),
114 "--include-base",
115 rebase_path("$target_gen_dir"),
116 "--output-base-cc",
Yilong Li437c7852020-06-09 15:34:46 -0700117 rebase_path("$target_gen_dir/$library_name_slashes/cpp/fidl"),
John Rosasco62ace592020-06-09 19:57:28 -0700118 "--output-c-header",
119 rebase_path("$target_gen_dir/$library_name_slashes/c/fidl.h"),
John Rosasco24cbdab2019-09-25 14:14:35 -0700120 ]
121 }
122
123 source_set(target_name) {
124 public_configs = [ ":$config_name" ]
125
126 sources = get_target_outputs(":$fidl_gen_target_name")
127
Mike Klein96f64012020-04-03 10:59:37 -0500128 deps = [ ":$fidl_gen_target_name" ]
John Rosasco24cbdab2019-09-25 14:14:35 -0700129
130 public_deps = _deps
131 }
132}
133
134#
135# Produce a cc source library from invoker's json file.
136# Primary output is the source_set.
137#
138template("_fuchsia_cc_source_library") {
139 assert(defined(invoker.meta), "The meta.json file path must be specified.")
140
141 meta_json = read_file(invoker.meta, "json")
142
143 assert(meta_json.type == "cc_source_library")
144
145 _output_name = meta_json.name
146 _include_dirs = []
147 _public_headers = []
148 _sources = []
149 _deps = []
150
151 meta_json_include_dir = meta_json.include_dir
152 _include_dirs += [ rebase_path("$fuchsia_sdk_path/$meta_json_include_dir") ]
153
154 foreach(header, meta_json.headers) {
155 rebased_header = []
156 rebased_header = [ rebase_path("$fuchsia_sdk_path/$header") ]
157 _public_headers += rebased_header
158 _sources += rebased_header
159 }
160
161 foreach(source, meta_json.sources) {
162 _sources += [ "$fuchsia_sdk_path/$source" ]
163 }
164
165 config_name = "config_$target_name"
166 config(config_name) {
167 include_dirs = _include_dirs
168 }
169
170 foreach(dep, meta_json.deps) {
171 _deps += [ "../pkg:$dep" ]
172 }
173
174 foreach(dep, meta_json.fidl_deps) {
175 _deps += [ "../fidl:$dep" ]
176 }
177
178 source_set(target_name) {
179 output_name = _output_name
180 public = _public_headers
181 sources = _sources
182 public_configs = [ ":$config_name" ]
183 public_deps = _deps
184 }
185}
186
187template("_fuchsia_cc_prebuilt_library") {
188 assert(defined(invoker.meta), "The meta.json file path must be specified.")
189 meta_json = read_file(invoker.meta, "json")
190
191 _include_dirs = []
192 _deps = []
193 _libs = []
194
195 meta_json_include_dir = meta_json.include_dir
196 _include_dirs += [ "$fuchsia_sdk_path/$meta_json_include_dir" ]
197
198 foreach(dep, meta_json.deps) {
199 _deps += [ ":$dep" ]
200 }
201
202 meta_json_binaries = meta_json.binaries
203 if (target_cpu == "x64") {
204 meta_json_binaries_arch = meta_json_binaries.x64
205 } else {
206 meta_json_binaries_arch = meta_json_binaries.arm64
207 }
208 prebuilt_lib = meta_json_binaries_arch.link
209 _libs = [ "$fuchsia_sdk_path/$prebuilt_lib" ]
210
211 config_name = "config_$target_name"
212 config(config_name) {
213 include_dirs = _include_dirs
214 libs = _libs
215 }
216
217 group(target_name) {
218 public_configs = [ ":$config_name" ]
219 public_deps = _deps
220 }
221}
222
223#
224# Read SDK manifest json file and produce gn build targets for all
225# "enabled_parts" as specified by the template invoker.
226#
227# Fuchsia SDK manifest is primarily a "parts" array.
228#
229template("fuchsia_sdk") {
230 assert(defined(invoker.meta), "The meta.json file path must be specified.")
231 assert(defined(invoker.enabled_parts),
232 "A list containing the parts of the SDK to generate targets for.")
233
234 meta_json = read_file(invoker.meta, "json")
235
236 foreach(part, meta_json.parts) {
237 part_meta_json = {
238 }
239
240 part_meta = part.meta
241 part_meta_rebased = "$fuchsia_sdk_path/$part_meta"
242
243 part_meta_json = read_file(part_meta_rebased, "json")
244 subtarget_name = part_meta_json.name
245
246 foreach(enabled_part, invoker.enabled_parts) {
247 if (part.type == "cc_source_library") {
248 if (part.type == enabled_part) {
249 _fuchsia_cc_source_library(subtarget_name) {
250 meta = part_meta_rebased
251 }
252 }
253 } else if (part.type == "sysroot") {
254 if (part.type == enabled_part) {
255 _fuchsia_sysroot(subtarget_name) {
256 meta = part_meta_rebased
257 }
258 }
259 } else if (part.type == "fidl_library") {
260 if (part.type == enabled_part) {
261 _fuchsia_fidl_library(subtarget_name) {
262 meta = part_meta_rebased
263 }
264 }
265 } else if (part.type == "cc_prebuilt_library") {
266 if (part.type == enabled_part) {
267 _fuchsia_cc_prebuilt_library(subtarget_name) {
268 meta = part_meta_rebased
269 }
270 }
271 }
272 }
273 }
274
275 group(target_name) {
276 }
277}
278
279#
280# Create package in 'gen' directory.
281#
282template("fuchsia_package") {
283 assert(defined(invoker.name), "The name of the package must be specified.")
284 assert(defined(invoker.version), "The package version must be specified.")
285
286 pkg_dir = target_gen_dir
287 pkg_name = invoker.name
288 pkg_version = invoker.version
289 pkg_manifest = invoker.pkg_manifest
290
291 pkg_id_path = "${pkg_dir}/meta/package"
292 gen_far_target_name = "gen_far_${target_name}"
293 pkg_archive = "${pkg_dir}/${pkg_name}-${pkg_version}.far"
294
295 action(gen_far_target_name) {
296 script = "//build/fuchsia/gen_package"
297
298 pm_binary = rebase_path("$fuchsia_sdk_path/tools/pm")
299
Mike Klein96f64012020-04-03 10:59:37 -0500300 inputs = [ pm_binary ]
John Rosasco24cbdab2019-09-25 14:14:35 -0700301
302 outputs = [
303 pkg_id_path,
304 pkg_archive,
305 ]
306
307 args = [
308 "--pm-bin",
309 pm_binary,
310 "--pkg-dir",
311 rebase_path(pkg_dir),
312 "--pkg-name",
313 pkg_name,
314 "--pkg-version",
315 "$pkg_version",
316 "--pkg-manifest",
317 rebase_path(pkg_manifest),
318 ]
319
320 if (defined(invoker.deps)) {
321 deps = invoker.deps
322 }
323 if (defined(invoker.testonly)) {
324 testonly = invoker.testonly
325 }
326 }
327
328 copy(target_name) {
329 if (defined(invoker.testonly)) {
330 testonly = invoker.testonly
331 }
332
Mike Klein96f64012020-04-03 10:59:37 -0500333 sources = [ pkg_archive ]
John Rosasco24cbdab2019-09-25 14:14:35 -0700334
335 output_name = "${root_out_dir}/far/${pkg_name}.far"
Mike Klein96f64012020-04-03 10:59:37 -0500336 outputs = [ output_name ]
John Rosasco24cbdab2019-09-25 14:14:35 -0700337
Mike Klein96f64012020-04-03 10:59:37 -0500338 deps = [ ":$gen_far_target_name" ]
John Rosasco24cbdab2019-09-25 14:14:35 -0700339 }
340}
341
342#
343# Places repo in output ('obj') directory.
344#
345template("fuchsia_repo") {
346 assert(defined(invoker.archives),
347 "The list of archives to publish must be specified.")
348 assert(defined(invoker.repo), "The location of the repo should be specified.")
349
350 action(target_name) {
351 if (defined(invoker.testonly)) {
352 testonly = invoker.testonly
353 }
354 script = "//build/fuchsia/gen_repo"
355
356 pm_binary = rebase_path("$fuchsia_sdk_path/tools/pm")
357 repo_directory = invoker.repo
358
Mike Klein96f64012020-04-03 10:59:37 -0500359 inputs = [ pm_binary ]
John Rosasco24cbdab2019-09-25 14:14:35 -0700360
361 archive_flags = []
362
363 foreach(archive, invoker.archives) {
364 assert(get_path_info(archive, "extension") == "far",
365 "Archive '$archive' does not have the .far extension.")
366 inputs += [ archive ]
367 archive_flags += [
368 "--archive",
369 rebase_path(archive),
370 ]
371 }
372
Mike Klein96f64012020-04-03 10:59:37 -0500373 outputs = [ repo_directory ]
John Rosasco24cbdab2019-09-25 14:14:35 -0700374
375 args = [
376 "--pm-bin",
377 pm_binary,
378 "--repo-dir",
379 rebase_path(repo_directory),
380 ] + archive_flags
381
382 if (defined(invoker.deps)) {
383 deps = invoker.deps
384 }
385 }
386}