blob: 97d3fe620491dc63839b8c46460332d1a581f8ca [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
77 library_name = string_replace(meta_json.name, "fuchsia.", "")
78 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 = [
98 "$target_gen_dir/fuchsia/$library_name_slashes/c/fidl.h",
John Rosascob80b2fe2020-01-30 08:39:24 -080099 "$target_gen_dir/fuchsia/$library_name_slashes/c/tables.c",
John Rosasco24cbdab2019-09-25 14:14:35 -0700100 "$target_gen_dir/fuchsia/$library_name_slashes/cpp/fidl.h",
101 "$target_gen_dir/fuchsia/$library_name_slashes/cpp/fidl.cc",
John Rosasco24cbdab2019-09-25 14:14:35 -0700102 ]
103
104 args = [
105 "--fidlc-bin",
Mike Klein96f64012020-04-03 10:59:37 -0500106 rebase_path("$fuchsia_sdk_path/tools/fidlc"),
John Rosasco24cbdab2019-09-25 14:14:35 -0700107 "--fidlgen-bin",
Mike Klein96f64012020-04-03 10:59:37 -0500108 rebase_path("$fuchsia_sdk_path/tools/fidlgen"),
John Rosasco24cbdab2019-09-25 14:14:35 -0700109 "--sdk-base",
Mike Klein96f64012020-04-03 10:59:37 -0500110 rebase_path("$fuchsia_sdk_path"),
John Rosasco24cbdab2019-09-25 14:14:35 -0700111 "--root",
112 rebase_path(invoker.meta),
113 "--json",
114 rebase_path("$target_gen_dir/$library_name_json"),
115 "--include-base",
116 rebase_path("$target_gen_dir"),
117 "--output-base-cc",
118 rebase_path("$target_gen_dir/fuchsia/$library_name_slashes/cpp/fidl"),
119 "--output-c-header",
120 rebase_path("$target_gen_dir/fuchsia/$library_name_slashes/c/fidl.h"),
121 "--output-c-tables",
Mike Klein96f64012020-04-03 10:59:37 -0500122 rebase_path("$target_gen_dir/fuchsia/$library_name_slashes/c/tables.c"),
John Rosasco24cbdab2019-09-25 14:14:35 -0700123 ]
124 }
125
126 source_set(target_name) {
127 public_configs = [ ":$config_name" ]
128
129 sources = get_target_outputs(":$fidl_gen_target_name")
130
Mike Klein96f64012020-04-03 10:59:37 -0500131 deps = [ ":$fidl_gen_target_name" ]
John Rosasco24cbdab2019-09-25 14:14:35 -0700132
133 public_deps = _deps
134 }
135}
136
137#
138# Produce a cc source library from invoker's json file.
139# Primary output is the source_set.
140#
141template("_fuchsia_cc_source_library") {
142 assert(defined(invoker.meta), "The meta.json file path must be specified.")
143
144 meta_json = read_file(invoker.meta, "json")
145
146 assert(meta_json.type == "cc_source_library")
147
148 _output_name = meta_json.name
149 _include_dirs = []
150 _public_headers = []
151 _sources = []
152 _deps = []
153
154 meta_json_include_dir = meta_json.include_dir
155 _include_dirs += [ rebase_path("$fuchsia_sdk_path/$meta_json_include_dir") ]
156
157 foreach(header, meta_json.headers) {
158 rebased_header = []
159 rebased_header = [ rebase_path("$fuchsia_sdk_path/$header") ]
160 _public_headers += rebased_header
161 _sources += rebased_header
162 }
163
164 foreach(source, meta_json.sources) {
165 _sources += [ "$fuchsia_sdk_path/$source" ]
166 }
167
168 config_name = "config_$target_name"
169 config(config_name) {
170 include_dirs = _include_dirs
171 }
172
173 foreach(dep, meta_json.deps) {
174 _deps += [ "../pkg:$dep" ]
175 }
176
177 foreach(dep, meta_json.fidl_deps) {
178 _deps += [ "../fidl:$dep" ]
179 }
180
181 source_set(target_name) {
182 output_name = _output_name
183 public = _public_headers
184 sources = _sources
185 public_configs = [ ":$config_name" ]
186 public_deps = _deps
187 }
188}
189
190template("_fuchsia_cc_prebuilt_library") {
191 assert(defined(invoker.meta), "The meta.json file path must be specified.")
192 meta_json = read_file(invoker.meta, "json")
193
194 _include_dirs = []
195 _deps = []
196 _libs = []
197
198 meta_json_include_dir = meta_json.include_dir
199 _include_dirs += [ "$fuchsia_sdk_path/$meta_json_include_dir" ]
200
201 foreach(dep, meta_json.deps) {
202 _deps += [ ":$dep" ]
203 }
204
205 meta_json_binaries = meta_json.binaries
206 if (target_cpu == "x64") {
207 meta_json_binaries_arch = meta_json_binaries.x64
208 } else {
209 meta_json_binaries_arch = meta_json_binaries.arm64
210 }
211 prebuilt_lib = meta_json_binaries_arch.link
212 _libs = [ "$fuchsia_sdk_path/$prebuilt_lib" ]
213
214 config_name = "config_$target_name"
215 config(config_name) {
216 include_dirs = _include_dirs
217 libs = _libs
218 }
219
220 group(target_name) {
221 public_configs = [ ":$config_name" ]
222 public_deps = _deps
223 }
224}
225
226#
227# Read SDK manifest json file and produce gn build targets for all
228# "enabled_parts" as specified by the template invoker.
229#
230# Fuchsia SDK manifest is primarily a "parts" array.
231#
232template("fuchsia_sdk") {
233 assert(defined(invoker.meta), "The meta.json file path must be specified.")
234 assert(defined(invoker.enabled_parts),
235 "A list containing the parts of the SDK to generate targets for.")
236
237 meta_json = read_file(invoker.meta, "json")
238
239 foreach(part, meta_json.parts) {
240 part_meta_json = {
241 }
242
243 part_meta = part.meta
244 part_meta_rebased = "$fuchsia_sdk_path/$part_meta"
245
246 part_meta_json = read_file(part_meta_rebased, "json")
247 subtarget_name = part_meta_json.name
248
249 foreach(enabled_part, invoker.enabled_parts) {
250 if (part.type == "cc_source_library") {
251 if (part.type == enabled_part) {
252 _fuchsia_cc_source_library(subtarget_name) {
253 meta = part_meta_rebased
254 }
255 }
256 } else if (part.type == "sysroot") {
257 if (part.type == enabled_part) {
258 _fuchsia_sysroot(subtarget_name) {
259 meta = part_meta_rebased
260 }
261 }
262 } else if (part.type == "fidl_library") {
263 if (part.type == enabled_part) {
264 _fuchsia_fidl_library(subtarget_name) {
265 meta = part_meta_rebased
266 }
267 }
268 } else if (part.type == "cc_prebuilt_library") {
269 if (part.type == enabled_part) {
270 _fuchsia_cc_prebuilt_library(subtarget_name) {
271 meta = part_meta_rebased
272 }
273 }
274 }
275 }
276 }
277
278 group(target_name) {
279 }
280}
281
282#
283# Create package in 'gen' directory.
284#
285template("fuchsia_package") {
286 assert(defined(invoker.name), "The name of the package must be specified.")
287 assert(defined(invoker.version), "The package version must be specified.")
288
289 pkg_dir = target_gen_dir
290 pkg_name = invoker.name
291 pkg_version = invoker.version
292 pkg_manifest = invoker.pkg_manifest
293
294 pkg_id_path = "${pkg_dir}/meta/package"
295 gen_far_target_name = "gen_far_${target_name}"
296 pkg_archive = "${pkg_dir}/${pkg_name}-${pkg_version}.far"
297
298 action(gen_far_target_name) {
299 script = "//build/fuchsia/gen_package"
300
301 pm_binary = rebase_path("$fuchsia_sdk_path/tools/pm")
302
Mike Klein96f64012020-04-03 10:59:37 -0500303 inputs = [ pm_binary ]
John Rosasco24cbdab2019-09-25 14:14:35 -0700304
305 outputs = [
306 pkg_id_path,
307 pkg_archive,
308 ]
309
310 args = [
311 "--pm-bin",
312 pm_binary,
313 "--pkg-dir",
314 rebase_path(pkg_dir),
315 "--pkg-name",
316 pkg_name,
317 "--pkg-version",
318 "$pkg_version",
319 "--pkg-manifest",
320 rebase_path(pkg_manifest),
321 ]
322
323 if (defined(invoker.deps)) {
324 deps = invoker.deps
325 }
326 if (defined(invoker.testonly)) {
327 testonly = invoker.testonly
328 }
329 }
330
331 copy(target_name) {
332 if (defined(invoker.testonly)) {
333 testonly = invoker.testonly
334 }
335
Mike Klein96f64012020-04-03 10:59:37 -0500336 sources = [ pkg_archive ]
John Rosasco24cbdab2019-09-25 14:14:35 -0700337
338 output_name = "${root_out_dir}/far/${pkg_name}.far"
Mike Klein96f64012020-04-03 10:59:37 -0500339 outputs = [ output_name ]
John Rosasco24cbdab2019-09-25 14:14:35 -0700340
Mike Klein96f64012020-04-03 10:59:37 -0500341 deps = [ ":$gen_far_target_name" ]
John Rosasco24cbdab2019-09-25 14:14:35 -0700342 }
343}
344
345#
346# Places repo in output ('obj') directory.
347#
348template("fuchsia_repo") {
349 assert(defined(invoker.archives),
350 "The list of archives to publish must be specified.")
351 assert(defined(invoker.repo), "The location of the repo should be specified.")
352
353 action(target_name) {
354 if (defined(invoker.testonly)) {
355 testonly = invoker.testonly
356 }
357 script = "//build/fuchsia/gen_repo"
358
359 pm_binary = rebase_path("$fuchsia_sdk_path/tools/pm")
360 repo_directory = invoker.repo
361
Mike Klein96f64012020-04-03 10:59:37 -0500362 inputs = [ pm_binary ]
John Rosasco24cbdab2019-09-25 14:14:35 -0700363
364 archive_flags = []
365
366 foreach(archive, invoker.archives) {
367 assert(get_path_info(archive, "extension") == "far",
368 "Archive '$archive' does not have the .far extension.")
369 inputs += [ archive ]
370 archive_flags += [
371 "--archive",
372 rebase_path(archive),
373 ]
374 }
375
Mike Klein96f64012020-04-03 10:59:37 -0500376 outputs = [ repo_directory ]
John Rosasco24cbdab2019-09-25 14:14:35 -0700377
378 args = [
379 "--pm-bin",
380 pm_binary,
381 "--repo-dir",
382 rebase_path(repo_directory),
383 ] + archive_flags
384
385 if (defined(invoker.deps)) {
386 deps = invoker.deps
387 }
388 }
389}