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