build_rule.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. # python3
  2. # Copyright 2021 The Chromium Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. from __future__ import annotations
  6. from lib import consts
  7. from lib import common
  8. from lib import cargo
  9. from lib.compiler import BuildConditionSet
  10. import argparse
  11. from typing import Optional, Tuple
  12. class BuildRuleUsage:
  13. """Container for data that differs depending how the crate is consumed.
  14. There are 3 ways a crate can be consumed, and data for generating the
  15. resulting build rule will differ:
  16. * For normal usage. The crate is used from another crate, in their library
  17. or executable outputs.
  18. * For build scripts. The crate is used from build.rs in another crate.
  19. * For tests. The crate is used from tests in another crate.
  20. """
  21. def __init__(self):
  22. # Whether the crate should be allowed for direct use from first-party
  23. # code or not.
  24. self.for_first_party: bool = False
  25. # Set of architectures where another crate consumes this crate. If
  26. # empty, no GN target needs to be written.
  27. self.used_on_archs: BuildConditionSet = BuildConditionSet.EMPTY()
  28. # Dictionary of features, each defining a feature for building each of
  29. # the crate's targets (lib, binaries, build.rs, and tests). This list
  30. # includes "default" if default features should be used, or omits it
  31. # otherwise, which differs slightly from how rustc and cargo represent
  32. # including or excluding default features (they use a separate bool
  33. # and command line flag.
  34. # key: The name of the feature as string
  35. # value: `BuildConditionSet` for where the feature is enabled.
  36. self.features: list[tuple[str, BuildConditionSet]] = []
  37. # List of dictionaries, each defining a dependency for building the
  38. # crate's library and binaries.
  39. # deppath: GN target path of the dependency as string.
  40. # compile_modes: `BuildConditionSet` for where the dependency is used.
  41. self.deps: list[dict] = []
  42. # List of dictionaries, each defining a dependency for building the
  43. # crate's build.rs (or equivalent) file, defined in `build_root` if it
  44. # exists.
  45. # deppath: GN target path of the dependency as string.
  46. # compile_modes: `BuildConditionSet` for where the dependency is used.
  47. self.build_deps: list[dict] = []
  48. # List of dictionaries, each defining a dependency for building the
  49. # crate's tests.
  50. # deppath: GN target path of the dependency as string.
  51. # compile_modes: `BuildConditionSet` for where the dependency is used.
  52. self.dev_deps: list[dict] = []
  53. def sort_internals(self):
  54. """Sorts any sortable containers to help make reproducible output."""
  55. self.features.sort(key=lambda t: t[0])
  56. self.build_deps.sort(key=lambda d: d["deppath"])
  57. self.deps.sort(key=lambda d: d["deppath"])
  58. self.dev_deps.sort(key=lambda d: d["deppath"])
  59. class BuildRule:
  60. """A structured representation of the data used to generate a BUILD file.
  61. Contains data from the crate's Cargo.toml as well as data gathered from
  62. cargo with this tool."""
  63. def __init__(self, crate_name: str, epoch: str):
  64. # Name of the crate, not normalized. This is how rust code would refer
  65. # to the crate.
  66. self.crate_name: str = crate_name
  67. # The version epoch of the crate, which is used to generate metadata for
  68. # the crate's output.
  69. self.epoch: str = epoch
  70. # None if there is no lib build target, or the relative GN file path
  71. # string to the lib's root .rs file.
  72. self.lib_root: Optional[str] = None
  73. # Empty if there is no lib build target, or the relative GN file path
  74. # string to all Rust files that are part of the lib build, including
  75. # files generated by the build script if any.
  76. self.lib_sources: list[str] = []
  77. # If lib_root is not None, then this is "rlib" or "proc-macro",
  78. # specifying the type of lib.
  79. self.lib_type: Optional[str] = None
  80. # Empty if there are no bin targets. List of dictionaries with keys:
  81. # name: executable name
  82. # root: The relative GN path string to the bin's root .rs file.
  83. # sources: A list of relative GN path strings to all Rust files that
  84. # are part of the bin build, including files generated by
  85. # the build script if any.
  86. self.bins: list[dict] = []
  87. # Stuff shared between the lib (if present) and bins (if present).
  88. # If not none, a GN file path string to the build.rs file (or
  89. # equivalent) which is the root module of the build script.
  90. self.build_root: Optional[str] = None
  91. # The full set of source files including the root .rs file and those
  92. # used by it.
  93. self.build_sources: list[str] = []
  94. # The set of output files the build.rs would create, if there are any. A
  95. # human has to go and figure this out, so it must come from
  96. # third_party.toml.
  97. self.build_script_outputs: list[str] = []
  98. # The rust edition to build the crate with.
  99. self.edition: Optional[str] = None
  100. self.cargo_pkg_name: Optional[str] = None
  101. self.cargo_pkg_description: Optional[str] = None
  102. self.cargo_pkg_version: Optional[str] = None
  103. self.cargo_pkg_authors: Optional[str] = None
  104. self.normal_usage = BuildRuleUsage()
  105. self.buildrs_usage = BuildRuleUsage()
  106. self.test_usage = BuildRuleUsage()
  107. def get_usage(self, usage: cargo.CrateUsage) -> BuildRuleUsage:
  108. """Returns a `BuildRuleUsage` for a `cargo.CrateUsage`.
  109. These are also accessible directly as fields on the class, but this
  110. method helps to choose the correct one based on `cargo.CrateUsage`."""
  111. if usage == cargo.CrateUsage.FOR_NORMAL:
  112. return self.normal_usage
  113. if usage == cargo.CrateUsage.FOR_BUILDRS:
  114. return self.buildrs_usage
  115. if usage == cargo.CrateUsage.FOR_TESTS:
  116. return self.test_usage
  117. assert False # Unhandled CrateUsage?
  118. def sort_internals(self):
  119. """Sorts any sortable containers to help make reproducible output."""
  120. self.bins.sort(key=lambda d: d["name"])
  121. self.build_sources.sort()
  122. self.build_script_outputs.sort()
  123. self.lib_sources.sort()
  124. self.normal_usage.sort_internals()
  125. self.buildrs_usage.sort_internals()
  126. self.test_usage.sort_internals()
  127. def _write(self, indent: int, s: str):
  128. """Append a string onto `self.out`.
  129. The string is indented with a number of spaces based on the `indent`
  130. argument."""
  131. self.out.append("{}{}\n".format(" " * indent, s))
  132. def _write_compile_modes_conditions(self, indent: int,
  133. compile_modes: BuildConditionSet):
  134. """Write a GN if statement that is true for the `BuildConditionSet`.
  135. This appends the generated text to `out`."""
  136. conds = compile_modes.get_gn_conditions()
  137. if len(conds) == 1:
  138. self._write(indent, "if ({}) {{".format(conds[0]))
  139. elif len(conds) > 1:
  140. self._write(indent, "if (({}) ||".format(conds[0]))
  141. for cond in conds[1:-1]:
  142. self._write(indent + 4, "({}) ||".format(cond))
  143. self._write(indent + 4, "({})) {{".format(conds[-1]))
  144. def _write_for_compile_modes(self, indent: int,
  145. compile_modes: BuildConditionSet,
  146. to_write: list[tuple[int, str]]):
  147. """Write a GN if statement and body for a `BuildConditionSet`
  148. This appends the generated text to `out` and returns the result.
  149. Args:
  150. indent: How much to indent each line generated by this function.
  151. compile_modes: Defines when the if statement should resolve to true.
  152. to_write: A list of strings to place in the body of the if
  153. statement. Each string comes with an indent value, which will
  154. be added to the top level `indent`.
  155. """
  156. self._write_compile_modes_conditions(indent, compile_modes)
  157. conds = compile_modes.get_gn_conditions()
  158. if conds:
  159. indent += 2
  160. for (write_indent, write_str) in to_write:
  161. self._write(indent + write_indent, write_str)
  162. if conds:
  163. indent -= 2
  164. self._write(indent, "}")
  165. def _write_common(self, indent: int, build_rule: BuildRule, sources: list,
  166. usage: cargo.CrateUsage):
  167. """Write the GN content that's common for libraries and executables.
  168. This appends the generated text to `out` and returns the result.
  169. Args:
  170. build_rule: The BuildRule being written from.
  171. sources: The set of Rust source files. This is passed separately
  172. since it is stored differently for libraries vs executables in
  173. the BuildRule.
  174. usage: How this GN target is going to be used by other crates (or
  175. cargo.CrateUsage.FOR_NORMAL if it's generating an executable).
  176. """
  177. assert sources # There's always a root source at least.
  178. self._write(indent, "sources = [")
  179. for s in sources:
  180. self._write(indent + 2, "\"{}\",".format(s))
  181. self._write(indent, "]")
  182. self._write(indent, "edition = \"{}\"".format(build_rule.edition))
  183. if build_rule.cargo_pkg_version:
  184. self._write(
  185. indent, "cargo_pkg_version = \"{}\"".format(
  186. build_rule.cargo_pkg_version))
  187. if build_rule.cargo_pkg_authors:
  188. self._write(
  189. indent, "cargo_pkg_authors = \"{}\"".format(", ".join(
  190. build_rule.cargo_pkg_authors)))
  191. if build_rule.cargo_pkg_name:
  192. self._write(
  193. indent, "cargo_pkg_name = \"{}\"".format(
  194. build_rule.cargo_pkg_name.replace('\n', '')))
  195. if build_rule.cargo_pkg_description:
  196. self._write(
  197. indent, "cargo_pkg_description = \"{}\"".format(
  198. build_rule.cargo_pkg_description.replace('\n', '').replace(
  199. '"', '\'')))
  200. # Add these if, in the future, we want to explicitly mark each
  201. # third-party crate instead of doing so from the GN template.
  202. #
  203. # self._write(indent,
  204. # "configs -= [ \"//build/config/compiler:chromium_code\" ]")
  205. # self._write(indent,
  206. # "configs += [ \"//build/config/compiler:no_chromium_code\" ]")
  207. build_rule_usage = build_rule.get_usage(usage)
  208. for (deps, gn_name) in [(build_rule_usage.deps, "deps"),
  209. (build_rule_usage.build_deps, "build_deps"),
  210. (build_rule_usage.dev_deps, "test_deps")]:
  211. if not deps:
  212. continue
  213. global_deps = []
  214. specific_deps = []
  215. for d in deps:
  216. compile_modes = d["compile_modes"]
  217. if compile_modes.is_always_true(
  218. ) or compile_modes == build_rule_usage.used_on_archs:
  219. global_deps += [d]
  220. else:
  221. specific_deps += [d]
  222. if global_deps or specific_deps:
  223. self._write(indent, "{} = [".format(gn_name))
  224. for d in global_deps:
  225. self._write(indent + 2, "\"{}\",".format(d["deppath"]))
  226. if global_deps or specific_deps:
  227. self._write(indent, "]")
  228. for d in specific_deps:
  229. compile_modes = d["compile_modes"]
  230. self._write_for_compile_modes(
  231. indent, compile_modes,
  232. [(0, "deps += [ \"{}\" ]".format(d["deppath"]))])
  233. global_features = []
  234. specific_features = []
  235. for (feature, compile_modes) in build_rule_usage.features:
  236. # The default feature is a cargo thing, and just translates to
  237. # other features specified by the Cargo.toml.
  238. if feature == "default":
  239. continue
  240. if compile_modes.is_always_true(
  241. ) or compile_modes == build_rule_usage.used_on_archs:
  242. global_features += [feature]
  243. else:
  244. specific_features += [(feature, compile_modes)]
  245. if global_features or specific_features:
  246. self._write(indent, "features = [")
  247. for f in global_features:
  248. self._write(indent + 2, "\"{}\",".format(f))
  249. if global_features or specific_features:
  250. self._write(indent, "]")
  251. for (f, compile_modes) in specific_features:
  252. self._write_for_compile_modes(
  253. 2, compile_modes, [(0, "features += [ \"{}\" ]".format(f))])
  254. if build_rule.build_root:
  255. self._write(indent,
  256. "build_root = \"{}\"".format(build_rule.build_root))
  257. self._write(indent, "build_sources = [")
  258. for s in build_rule.build_sources:
  259. self._write(indent + 2, "\"{}\",".format(s))
  260. self._write(indent, "]")
  261. if build_rule.build_script_outputs:
  262. self._write(indent, "build_script_outputs = [")
  263. for o in build_rule.build_script_outputs:
  264. self._write(indent + 2, "\"{}\",".format(o))
  265. self._write(indent, "]")
  266. def generate_gn(self, args: argparse.Namespace, copyright_year: str) -> str:
  267. """Generate a BUILD.gn file contents.
  268. The BuildRule has all data needed to construct a BUILD file. This
  269. generates a BUILD.gn file.
  270. Args:
  271. args: The command-line arguments.
  272. copyright_year: The year as a string.
  273. """
  274. self.out: list[str] = []
  275. self._write(0, consts.GN_HEADER.format(year=copyright_year))
  276. for bin in self.bins:
  277. self._write(0, "cargo_crate(\"{}\") {{".format(bin["name"]))
  278. self._write(2, "crate_type = \"bin\"")
  279. self._write(2, "crate_root = \"{}\"".format(bin["root"]))
  280. self._write_common(2, self, bin["sources"],
  281. cargo.CrateUsage.FOR_NORMAL)
  282. self._write(0, "}")
  283. if self.lib_root:
  284. for usage in cargo.CrateUsage:
  285. # TODO(danakj): If the BuildRuleUsage is the same as another we
  286. # should only generate one, and point the duplicate over by
  287. # using a GN group() target. This would avoid building a crate
  288. # multiple times if the same features will be used each time.
  289. used_on_archs = self.get_usage(usage).used_on_archs
  290. if not used_on_archs:
  291. continue
  292. indent = 0
  293. if not used_on_archs.is_always_true():
  294. self._write_compile_modes_conditions(indent, used_on_archs)
  295. indent += 2
  296. self._write(
  297. indent,
  298. "cargo_crate(\"{}\") {{".format(usage.gn_target_name()))
  299. self._write(indent + 2,
  300. "crate_name = \"{}\"".format(
  301. common.crate_name_normalized(
  302. self.crate_name))) # yapf: disable
  303. self._write(indent + 2, "epoch = \"{}\"".format(self.epoch))
  304. self._write(indent + 2,
  305. "crate_type = \"{}\"".format(self.lib_type))
  306. if not self.get_usage(usage).for_first_party:
  307. for c in consts.GN_VISIBILITY_COMMENT.split("\n"):
  308. self._write(indent + 2, c)
  309. self._write(indent + 2,
  310. "visibility = [ \"{}\" ]".format(
  311. common.gn_third_party_path(
  312. rel_path=["*"]))) # yapf: disable
  313. if usage == cargo.CrateUsage.FOR_TESTS:
  314. self._write(indent + 2, "testonly = \"true\"")
  315. self._write(indent + 2,
  316. "crate_root = \"{}\"".format(self.lib_root))
  317. if usage == cargo.CrateUsage.FOR_NORMAL:
  318. if args.with_tests:
  319. self._write(indent + 2,
  320. "build_native_rust_unit_tests = true")
  321. else:
  322. for c in consts.GN_TESTS_COMMENT.split("\n"):
  323. self._write(indent + 2, c)
  324. self._write(indent + 2,
  325. "build_native_rust_unit_tests = false")
  326. else:
  327. self._write(indent + 2,
  328. "build_native_rust_unit_tests = false")
  329. self._write_common(indent + 2, self, self.lib_sources, usage)
  330. self._write(indent, "}")
  331. if not used_on_archs.is_always_true():
  332. indent -= 2
  333. self._write(indent, "}")
  334. return ''.join(self.out)