• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2021 The Chromium Authors
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import("//build/rust/rust_executable.gni")
6import("//build/rust/rust_macro.gni")
7import("//build/rust/rust_static_library.gni")
8
9# This template allows for building Cargo crates within gn.
10#
11# It is intended for use with pre-existing (third party) code and
12# is none too efficient. (It will stall the build pipeline whilst
13# it runs build scripts to work out what flags are needed). First
14# party code should directly use first-class gn targets, such as
15# //build/rust/rust_static_library.gni or similar.
16#
17# Because it's intended for third-party code, it automatically
18# defaults to //build/config/compiler:no_chromium_code which
19# suppresses some warnings. If you *do* use this for first party
20# code, you should remove that config and add the equivalent
21# //build/config/compiler:chromium_code config.
22#
23# Arguments:
24#  sources
25#  crate_root
26#  deps
27#  aliased_deps
28#  features
29#  build_native_rust_unit_tests
30#  edition
31#  crate_name
32#    All just as in rust_static_library.gni
33#  library_configs/executable_configs
34#    All just as in rust_target.gni
35#
36#  epoch (optional)
37#    The major version of the library, which is used to differentiate between
38#    multiple versions of the same library name. This includes all leading 0s
39#    and the first non-zero value in the crate's version. This should be left
40#    as the default, which is "0", for first-party code unless there are
41#    multiple versions of a crate present. For third-party code, the version
42#    epoch (matching the directory it is found in) should be specified.
43#
44#    Examples:
45#      1.0.2 => epoch = "1"
46#      4.2.0 => epoch = "4"
47#      0.2.7 => epoch = "0.2"
48#      0.0.3 => epoch = "0.0.3"
49#
50#  dev_deps
51#    Same meaning as test_deps in rust_static_library.gni, but called
52#    dev_deps to match Cargo.toml better.
53#
54#  build_root (optional)
55#    Filename of build.rs build script.
56#
57#  build_deps (optional)
58#    Build script dependencies
59#
60#  build_sources (optional)
61#    List of sources for build script. Must be specified if
62#    build_root is specified.
63#
64#  build_script_outputs (optional)
65#    List of .rs files generated by the build script, if any.
66#    Fine to leave undefined even if you have a build script.
67#    This doesn't directly correspond to any Cargo variable,
68#    but unfortunately is necessary for gn to build its dependency
69#    trees automatically.
70#    Many build scripts just output --cfg directives, in which case
71#    no source code is generated and this can remain empty.
72#
73#  build_script_inputs (optional)
74#    If the build script reads any files generated by build_deps,
75#    as opposed to merely linking against them, add a list of such
76#    files here. Again, this doesn't correspond to a Cargo variable
77#    but is necessary for gn.
78#
79#  native_libs (optional)
80#    Paths to library files that need to be in the linking search path when
81#    depending on the crate's library, as it links against them via #[link]
82#    directives.
83#
84#  crate_type "bin", "proc-macro" or "rlib" (optional)
85#    Whether to build an executable. The default is "rlib".
86#    At present others are not supported.
87#
88#  cargo_pkg_authors
89#  cargo_pkg_version
90#  cargo_pkg_name
91#  cargo_pkg_description
92#    Strings as found within 'version' and similar fields within Cargo.toml.
93#    Converted to environment variables passed to rustc, in case the crate
94#    uses clap `crate_version!` or `crate_authors!` macros (fairly common in
95#    command line tool help)
96
97template("cargo_crate") {
98  _orig_target_name = target_name
99
100  _crate_name = _orig_target_name
101  if (defined(invoker.crate_name)) {
102    _crate_name = invoker.crate_name
103  }
104
105  # Construct metadata from the crate epoch or an explicitly provided metadata
106  # field.
107  _rustc_metadata = ""
108  if (defined(invoker.rustc_metadata)) {
109    _rustc_metadata = invoker.rustc_metadata
110  } else if (defined(invoker.epoch)) {
111    _rustc_metadata = "${_crate_name}-${invoker.epoch}"
112  }
113
114  # Executables need to have unique names. Work out a prefix.
115  if (defined(invoker.build_root)) {
116    _epochlabel = "vunknown"
117    if (defined(invoker.epoch)) {
118      _tempepoch = string_replace(invoker.epoch, ".", "_")
119      _epochlabel = "v${_tempepoch}"
120    }
121
122    # This name includes the target name to ensure it's unique for each possible
123    # build target in the same BUILD.gn file.
124    _build_script_name =
125        "${_crate_name}_${target_name}_${_epochlabel}_build_script"
126
127    # Where the OUT_DIR will point when running the build script exe, and
128    # compiling the crate library/binaries. This directory must include the
129    # target name to avoid collisions between multiple GN targets that exist
130    # in the same BUILD.gn.
131    _build_script_env_out_dir = "$target_gen_dir/$target_name"
132  }
133
134  _rustenv = []
135  if (defined(invoker.rustenv)) {
136    _rustenv = invoker.rustenv
137  }
138  if (defined(invoker.cargo_pkg_authors)) {
139    _rustenv += [ "CARGO_PKG_AUTHORS=${invoker.cargo_pkg_authors}" ]
140  }
141  if (defined(invoker.cargo_pkg_version)) {
142    _rustenv += [ "CARGO_PKG_VERSION=${invoker.cargo_pkg_version}" ]
143  }
144  if (defined(invoker.cargo_pkg_name)) {
145    _rustenv += [ "CARGO_PKG_NAME=${invoker.cargo_pkg_name}" ]
146  }
147  if (defined(invoker.cargo_pkg_description)) {
148    _rustenv += [ "CARGO_PKG_DESCRIPTION=${invoker.cargo_pkg_description}" ]
149  }
150
151  # Try to determine the CARGO_MANIFEST_DIR, preferring the directory
152  # with build.rs and otherwise assuming that the target contains a
153  # `crate/` subdirectory.
154  if (defined(invoker.build_root)) {
155    manifest_dir = "."
156  } else {
157    build_gn_dir = get_label_info(target_name, "dir")
158    manifest_dir = rebase_path(build_gn_dir + "/crate", root_build_dir)
159  }
160  _rustenv += [ "CARGO_MANIFEST_DIR=${manifest_dir}" ]
161
162  # cargo_crate() should set library_configs, executable_configs,
163  # proc_macro_configs. Not configs.
164  assert(!defined(invoker.configs))
165
166  # Work out what we're building.
167  _crate_type = "rlib"
168  if (defined(invoker.crate_type)) {
169    _crate_type = invoker.crate_type
170  }
171  if (_crate_type == "cdylib") {
172    # Crates are rarely cdylibs. The example encountered so far aims
173    # to expose a C API to other code. In a Chromium context, we don't
174    # want to build that as a dylib for a couple of reasons:
175    # * rust_shared_library does not work on Mac. rustc does not know
176    #   how to export the __llvm_profile_raw_version symbol.
177    # * even if it did work, this might require us to distribute extra
178    #   binaries (.so/.dylib etc.)
179    # For the only case we've had so far, it makes more sense to build
180    # the code as a static library which we can then link into downstream
181    # binaries.
182    _crate_type = "rlib"
183  }
184  if (_crate_type == "bin") {
185    _target_type = "rust_executable"
186    assert(!defined(invoker.epoch))
187    if (defined(invoker.executable_configs)) {
188      _configs = invoker.executable_configs
189    }
190  } else if (_crate_type == "proc-macro") {
191    _target_type = "rust_macro"
192    if (defined(invoker.proc_macro_configs)) {
193      _configs = invoker.proc_macro_configs
194    }
195  } else {
196    assert(_crate_type == "rlib")
197    _target_type = "rust_static_library"
198    if (defined(invoker.library_configs)) {
199      _configs = invoker.library_configs
200    }
201  }
202
203  if (defined(invoker.output_name)) {
204    _output_name = invoker.output_name
205  } else if (_crate_type != "bin") {
206    # Note that file names of libraries must start with the crate name in
207    # order for the compiler to find transitive dependencies in the
208    # directory search paths (since they are not all explicitly specified).
209    #
210    # For bin targets, we expect the target name to be unique, and the name
211    # of the exe should not add magic stuff to it. And bin crates can not be
212    # transitive dependencies.
213    _output_name = "${_crate_name}_${_orig_target_name}"
214  }
215
216  _testonly = false
217  if (defined(invoker.testonly)) {
218    _testonly = invoker.testonly
219  }
220
221  if (defined(invoker.native_libs)) {
222    _native_libs_action = "copy_${target_name}_native_libs"
223    _native_libs_config = "config_${target_name}_native_libs"
224    _native_libs_dir =
225        "${root_out_dir}/rustlib/${_crate_name}_${target_name}_${_epochlabel}"
226
227    copy(_native_libs_action) {
228      testonly = _testonly
229      visibility = [ ":$target_name" ]
230      sources = invoker.native_libs
231      outputs = [ "${_native_libs_dir}/{{source_file_part}}" ]
232    }
233    config(_native_libs_config) {
234      lib_dirs = [ "${_native_libs_dir}" ]
235    }
236  }
237
238  # The main target, either a Rust source set or an executable.
239  target(_target_type, target_name) {
240    forward_variables_from(invoker,
241                           "*",
242                           TESTONLY_AND_VISIBILITY + [
243                                 "build_root",
244                                 "build_deps",
245                                 "build_sources",
246                                 "build_script_inputs",
247                                 "build_script_outputs",
248                                 "epoch",
249                                 "unit_test_target",
250                                 "configs",
251                                 "executable_configs",
252                                 "library_configs",
253                                 "proc_macro_configs",
254                                 "rustenv",
255                                 "dev_deps",
256                                 "native_libs_dir",
257                               ])
258
259    testonly = _testonly
260    if (defined(invoker.visibility)) {
261      visibility = invoker.visibility
262    }
263    if (defined(crate_type) && crate_type == "cdylib") {
264      # See comments above about cdylib.
265      crate_type = "rlib"
266    }
267    crate_name = _crate_name
268
269    if (defined(_output_name)) {
270      output_name = _output_name
271    }
272
273    # Don't import the `chromium` crate into third-party code.
274    no_chromium_prelude = true
275
276    rustc_metadata = _rustc_metadata
277
278    # TODO(crbug.com/40259764): don't default to true. This requires changes to
279    # third_party.toml and gnrt when generating third-party build targets.
280    allow_unsafe = true
281
282    configs = []
283    if (defined(_configs)) {
284      configs += _configs
285    }
286
287    if (_crate_type == "rlib") {
288      # Forward configs for unit tests.
289      if (defined(invoker.executable_configs)) {
290        executable_configs = invoker.executable_configs
291      }
292    }
293
294    if (!defined(rustflags)) {
295      rustflags = []
296    }
297    rustenv = _rustenv
298
299    if (!defined(build_native_rust_unit_tests)) {
300      build_native_rust_unit_tests = _crate_type != "proc-macro"
301    }
302    if (build_native_rust_unit_tests) {
303      # Unit tests in a proc-macro crate type don't make sense, you can't
304      # compile executables against the `proc_macro` crate.
305      assert(_crate_type != "proc-macro")
306    }
307
308    # The unit tests for each target, if generated, should be unique as well.
309    # a) It needs to be unique even if multiple build targets have the same
310    #    `crate_name`, but different target names.
311    # b) It needs to be unique even if multiple build targets have the same
312    #    `crate_name` and target name, but different epochs.
313    _unit_test_unique_target_name = ""
314    if (_crate_name != _orig_target_name) {
315      _unit_test_unique_target_name = "${_orig_target_name}_"
316    }
317    _unit_test_unique_epoch = ""
318    if (defined(invoker.epoch)) {
319      _epoch_str = string_replace(invoker.epoch, ".", "_")
320      _unit_test_unique_epoch = "v${_epoch_str}_"
321    }
322    if (defined(output_dir) && output_dir != "") {
323      unit_test_output_dir = output_dir
324    }
325    unit_test_target = "${_unit_test_unique_target_name}${_crate_name}_${_unit_test_unique_epoch}unittests"
326
327    if ((!defined(output_dir) || output_dir == "") && _crate_type == "rlib") {
328      # Cargo crate rlibs can be compiled differently for tests, and must not
329      # collide with the production outputs. This does *not* override the
330      # unit_test_output_dir, which is set above, as that target is not an rlib.
331      output_dir = "$target_out_dir/$_orig_target_name"
332    }
333
334    if (defined(invoker.dev_deps)) {
335      test_deps = invoker.dev_deps
336    }
337
338    if (defined(invoker.build_root)) {
339      # Uh-oh, we have a build script
340      if (!defined(deps)) {
341        deps = []
342      }
343      if (!defined(sources)) {
344        sources = []
345      }
346      if (!defined(inputs)) {
347        inputs = []
348      }
349
350      # This... is a bit weird. We generate a file called cargo_flags.rs which
351      # does not actually contain Rust code, but instead some flags to add
352      # to the rustc command line. We need it to end in a .rs extension so that
353      # we can include it in the 'sources' line and thus have dependency
354      # calculation done correctly. data_deps won't work because targets don't
355      # require them to be present until runtime.
356      flags_file = "$_build_script_env_out_dir/cargo_flags.rs"
357      rustflags += [ "@" + rebase_path(flags_file, root_build_dir) ]
358      sources += [ flags_file ]
359      if (defined(invoker.build_script_outputs)) {
360        # Build scripts may output arbitrary files. They are usually included in
361        # the main Rust target using include! or include_str! and therefore the
362        # filename may be .rs or may be arbitrary. We want to educate ninja
363        # about the dependency either way.
364        foreach(extra_source,
365                filter_include(invoker.build_script_outputs, [ "*.rs" ])) {
366          sources += [ "$_build_script_env_out_dir/$extra_source" ]
367        }
368        foreach(extra_source,
369                filter_exclude(invoker.build_script_outputs, [ "*.rs" ])) {
370          inputs += [ "$_build_script_env_out_dir/$extra_source" ]
371        }
372      }
373      deps += [ ":${_build_script_name}_output" ]
374      if (defined(_native_libs_action)) {
375        deps += [ ":${_native_libs_action}" ]
376        configs += [ ":${_native_libs_config}" ]
377      }
378    }
379  }
380
381  if (defined(invoker.build_root)) {
382    # Extra targets required to make build script work
383    action("${_build_script_name}_output") {
384      script = rebase_path("//build/rust/run_build_script.py")
385      build_script_target = ":${_build_script_name}($rust_macro_toolchain)"
386      deps = [ build_script_target ]
387      testonly = _testonly
388      if (defined(invoker.visibility)) {
389        visibility = invoker.visibility
390      }
391
392      # The build script may be built with a different toolchain when
393      # cross-compiling (the host toolchain) so we must find the path relative
394      # to that.
395      _build_script_root_out_dir =
396          get_label_info(build_script_target, "root_out_dir")
397      _build_script_exe = "$_build_script_root_out_dir/$_build_script_name"
398
399      # The executable is always built with the `rust_macro_toolchain` which
400      # targets the `host_os`. The rule here is on the `target_toolchain` which
401      # can be different (e.g. compiling on Linux, targeting Windows).
402      if (host_os == "win") {
403        _build_script_exe = "${_build_script_exe}.exe"
404      }
405
406      _flags_file = "$_build_script_env_out_dir/cargo_flags.rs"
407
408      inputs = [ _build_script_exe ]
409      outputs = [ _flags_file ]
410      args = [
411        "--build-script",
412        rebase_path(_build_script_exe, root_build_dir),
413        "--output",
414        rebase_path(_flags_file, root_build_dir),
415        "--rust-prefix",
416        rebase_path("${rust_sysroot}/bin", root_build_dir),
417        "--out-dir",
418        rebase_path(_build_script_env_out_dir, root_build_dir),
419        "--src-dir",
420        rebase_path(get_path_info(invoker.build_root, "dir"), root_build_dir),
421      ]
422      if (defined(rust_abi_target) && rust_abi_target != "") {
423        args += [
424          "--target",
425          rust_abi_target,
426        ]
427      }
428      if (defined(invoker.features)) {
429        args += [ "--features" ]
430        args += invoker.features
431      }
432      if (defined(invoker.build_script_outputs)) {
433        args += [ "--generated-files" ]
434        args += invoker.build_script_outputs
435        foreach(generated_file, invoker.build_script_outputs) {
436          outputs += [ "$_build_script_env_out_dir/$generated_file" ]
437        }
438      }
439      if (_rustenv != []) {
440        args += [ "--env" ]
441        args += _rustenv
442      }
443      if (defined(invoker.build_script_inputs)) {
444        inputs += invoker.build_script_inputs
445      }
446    }
447
448    if (toolchain_for_rust_host_build_tools) {
449      # The build script is only available to be built on the host, and we use
450      # the rust_macro_toolchain for it to unblock building them while the
451      # Chromium stdlib is still being compiled.
452      rust_executable(_build_script_name) {
453        crate_name = _build_script_name
454        sources = invoker.build_sources
455        crate_root = invoker.build_root
456        testonly = _testonly
457        if (defined(invoker.visibility)) {
458          visibility = invoker.visibility
459        }
460        if (defined(invoker.build_deps)) {
461          deps = invoker.build_deps
462        }
463        if (defined(invoker.build_script_inputs)) {
464          inputs = invoker.build_script_inputs
465        }
466
467        # Don't import the `chromium` crate into third-party code.
468        no_chromium_prelude = true
469
470        # The ${_build_script_name}_output target looks for the exe in this
471        # location. Due to how the Windows component build works, this has to
472        # be $root_out_dir for all EXEs. In component build, C++ links to the
473        # CRT as a DLL, and if Rust does not match, we can't link mixed target
474        # Rust EXE/DLLs, as the headers in C++ said something different than
475        # what Rust links. Since the CRT DLL is placed in the $root_out_dir,
476        # an EXE can find it if it's also placed in that dir.
477        output_dir = root_out_dir
478        rustenv = _rustenv
479        forward_variables_from(invoker,
480                               [
481                                 "features",
482                                 "edition",
483                                 "rustflags",
484                               ])
485        configs -= [
486          "//build/config/compiler:chromium_code",
487
488          # Avoid generating profiling data for build scripts.
489          #
490          # TODO(crbug.com/40261306): determine for sure whether to remove this
491          # config. I'm not sure of the overlap between PGO instrumentation and
492          # code coverage instrumentation, but we definitely don't want build
493          # script coverage for PGO, while we might for test coverage metrics.
494          #
495          # If we do include build script output in test metrics, it could be
496          # misleading: exercising some code from a build script doesn't give us
497          # the same signal as an actual test.
498          "//build/config/coverage:default_coverage",
499        ]
500        configs += [ "//build/config/compiler:no_chromium_code" ]
501      }
502    } else {
503      not_needed(invoker,
504                 [
505                   "build_sources",
506                   "build_deps",
507                   "build_root",
508                   "build_script_inputs",
509                   "build_script_outputs",
510                 ])
511    }
512  } else {
513    not_needed([
514                 "_name_specific_output_dir",
515                 "_orig_target_name",
516               ])
517  }
518}
519
520set_defaults("cargo_crate") {
521  library_configs = default_compiler_configs
522  executable_configs = default_executable_configs
523  proc_macro_configs = default_rust_proc_macro_configs
524}
525