analyzer.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  1. # Copyright (c) 2014 Google Inc. 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. This script is intended for use as a GYP_GENERATOR. It takes as input (by way of
  6. the generator flag config_path) the path of a json file that dictates the files
  7. and targets to search for. The following keys are supported:
  8. files: list of paths (relative) of the files to search for.
  9. test_targets: unqualified target names to search for. Any target in this list
  10. that depends upon a file in |files| is output regardless of the type of target
  11. or chain of dependencies.
  12. additional_compile_targets: Unqualified targets to search for in addition to
  13. test_targets. Targets in the combined list that depend upon a file in |files|
  14. are not necessarily output. For example, if the target is of type none then the
  15. target is not output (but one of the descendants of the target will be).
  16. The following is output:
  17. error: only supplied if there is an error.
  18. compile_targets: minimal set of targets that directly or indirectly (for
  19. targets of type none) depend on the files in |files| and is one of the
  20. supplied targets or a target that one of the supplied targets depends on.
  21. The expectation is this set of targets is passed into a build step. This list
  22. always contains the output of test_targets as well.
  23. test_targets: set of targets from the supplied |test_targets| that either
  24. directly or indirectly depend upon a file in |files|. This list if useful
  25. if additional processing needs to be done for certain targets after the
  26. build, such as running tests.
  27. status: outputs one of three values: none of the supplied files were found,
  28. one of the include files changed so that it should be assumed everything
  29. changed (in this case test_targets and compile_targets are not output) or at
  30. least one file was found.
  31. invalid_targets: list of supplied targets that were not found.
  32. Example:
  33. Consider a graph like the following:
  34. A D
  35. / \
  36. B C
  37. A depends upon both B and C, A is of type none and B and C are executables.
  38. D is an executable, has no dependencies and nothing depends on it.
  39. If |additional_compile_targets| = ["A"], |test_targets| = ["B", "C"] and
  40. files = ["b.cc", "d.cc"] (B depends upon b.cc and D depends upon d.cc), then
  41. the following is output:
  42. |compile_targets| = ["B"] B must built as it depends upon the changed file b.cc
  43. and the supplied target A depends upon it. A is not output as a build_target
  44. as it is of type none with no rules and actions.
  45. |test_targets| = ["B"] B directly depends upon the change file b.cc.
  46. Even though the file d.cc, which D depends upon, has changed D is not output
  47. as it was not supplied by way of |additional_compile_targets| or |test_targets|.
  48. If the generator flag analyzer_output_path is specified, output is written
  49. there. Otherwise output is written to stdout.
  50. In Gyp the "all" target is shorthand for the root targets in the files passed
  51. to gyp. For example, if file "a.gyp" contains targets "a1" and
  52. "a2", and file "b.gyp" contains targets "b1" and "b2" and "a2" has a dependency
  53. on "b2" and gyp is supplied "a.gyp" then "all" consists of "a1" and "a2".
  54. Notice that "b1" and "b2" are not in the "all" target as "b.gyp" was not
  55. directly supplied to gyp. OTOH if both "a.gyp" and "b.gyp" are supplied to gyp
  56. then the "all" target includes "b1" and "b2".
  57. """
  58. import json
  59. import os
  60. import posixpath
  61. import gyp.common
  62. debug = False
  63. found_dependency_string = "Found dependency"
  64. no_dependency_string = "No dependencies"
  65. # Status when it should be assumed that everything has changed.
  66. all_changed_string = "Found dependency (all)"
  67. # MatchStatus is used indicate if and how a target depends upon the supplied
  68. # sources.
  69. # The target's sources contain one of the supplied paths.
  70. MATCH_STATUS_MATCHES = 1
  71. # The target has a dependency on another target that contains one of the
  72. # supplied paths.
  73. MATCH_STATUS_MATCHES_BY_DEPENDENCY = 2
  74. # The target's sources weren't in the supplied paths and none of the target's
  75. # dependencies depend upon a target that matched.
  76. MATCH_STATUS_DOESNT_MATCH = 3
  77. # The target doesn't contain the source, but the dependent targets have not yet
  78. # been visited to determine a more specific status yet.
  79. MATCH_STATUS_TBD = 4
  80. generator_supports_multiple_toolsets = gyp.common.CrossCompileRequested()
  81. generator_wants_static_library_dependencies_adjusted = False
  82. generator_default_variables = {}
  83. for dirname in [
  84. "INTERMEDIATE_DIR",
  85. "SHARED_INTERMEDIATE_DIR",
  86. "PRODUCT_DIR",
  87. "LIB_DIR",
  88. "SHARED_LIB_DIR",
  89. ]:
  90. generator_default_variables[dirname] = "!!!"
  91. for unused in [
  92. "RULE_INPUT_PATH",
  93. "RULE_INPUT_ROOT",
  94. "RULE_INPUT_NAME",
  95. "RULE_INPUT_DIRNAME",
  96. "RULE_INPUT_EXT",
  97. "EXECUTABLE_PREFIX",
  98. "EXECUTABLE_SUFFIX",
  99. "STATIC_LIB_PREFIX",
  100. "STATIC_LIB_SUFFIX",
  101. "SHARED_LIB_PREFIX",
  102. "SHARED_LIB_SUFFIX",
  103. "CONFIGURATION_NAME",
  104. ]:
  105. generator_default_variables[unused] = ""
  106. def _ToGypPath(path):
  107. """Converts a path to the format used by gyp."""
  108. if os.sep == "\\" and os.altsep == "/":
  109. return path.replace("\\", "/")
  110. return path
  111. def _ResolveParent(path, base_path_components):
  112. """Resolves |path|, which starts with at least one '../'. Returns an empty
  113. string if the path shouldn't be considered. See _AddSources() for a
  114. description of |base_path_components|."""
  115. depth = 0
  116. while path.startswith("../"):
  117. depth += 1
  118. path = path[3:]
  119. # Relative includes may go outside the source tree. For example, an action may
  120. # have inputs in /usr/include, which are not in the source tree.
  121. if depth > len(base_path_components):
  122. return ""
  123. if depth == len(base_path_components):
  124. return path
  125. return (
  126. "/".join(base_path_components[0 : len(base_path_components) - depth])
  127. + "/"
  128. + path
  129. )
  130. def _AddSources(sources, base_path, base_path_components, result):
  131. """Extracts valid sources from |sources| and adds them to |result|. Each
  132. source file is relative to |base_path|, but may contain '..'. To make
  133. resolving '..' easier |base_path_components| contains each of the
  134. directories in |base_path|. Additionally each source may contain variables.
  135. Such sources are ignored as it is assumed dependencies on them are expressed
  136. and tracked in some other means."""
  137. # NOTE: gyp paths are always posix style.
  138. for source in sources:
  139. if not len(source) or source.startswith(("!!!", "$")):
  140. continue
  141. # variable expansion may lead to //.
  142. org_source = source
  143. source = source[0] + source[1:].replace("//", "/")
  144. if source.startswith("../"):
  145. source = _ResolveParent(source, base_path_components)
  146. if len(source):
  147. result.append(source)
  148. continue
  149. result.append(base_path + source)
  150. if debug:
  151. print("AddSource", org_source, result[len(result) - 1])
  152. def _ExtractSourcesFromAction(action, base_path, base_path_components, results):
  153. if "inputs" in action:
  154. _AddSources(action["inputs"], base_path, base_path_components, results)
  155. def _ToLocalPath(toplevel_dir, path):
  156. """Converts |path| to a path relative to |toplevel_dir|."""
  157. if path == toplevel_dir:
  158. return ""
  159. if path.startswith(toplevel_dir + "/"):
  160. return path[len(toplevel_dir) + len("/") :]
  161. return path
  162. def _ExtractSources(target, target_dict, toplevel_dir):
  163. # |target| is either absolute or relative and in the format of the OS. Gyp
  164. # source paths are always posix. Convert |target| to a posix path relative to
  165. # |toplevel_dir_|. This is done to make it easy to build source paths.
  166. base_path = posixpath.dirname(_ToLocalPath(toplevel_dir, _ToGypPath(target)))
  167. base_path_components = base_path.split("/")
  168. # Add a trailing '/' so that _AddSources() can easily build paths.
  169. if len(base_path):
  170. base_path += "/"
  171. if debug:
  172. print("ExtractSources", target, base_path)
  173. results = []
  174. if "sources" in target_dict:
  175. _AddSources(target_dict["sources"], base_path, base_path_components, results)
  176. # Include the inputs from any actions. Any changes to these affect the
  177. # resulting output.
  178. if "actions" in target_dict:
  179. for action in target_dict["actions"]:
  180. _ExtractSourcesFromAction(action, base_path, base_path_components, results)
  181. if "rules" in target_dict:
  182. for rule in target_dict["rules"]:
  183. _ExtractSourcesFromAction(rule, base_path, base_path_components, results)
  184. return results
  185. class Target:
  186. """Holds information about a particular target:
  187. deps: set of Targets this Target depends upon. This is not recursive, only the
  188. direct dependent Targets.
  189. match_status: one of the MatchStatus values.
  190. back_deps: set of Targets that have a dependency on this Target.
  191. visited: used during iteration to indicate whether we've visited this target.
  192. This is used for two iterations, once in building the set of Targets and
  193. again in _GetBuildTargets().
  194. name: fully qualified name of the target.
  195. requires_build: True if the target type is such that it needs to be built.
  196. See _DoesTargetTypeRequireBuild for details.
  197. added_to_compile_targets: used when determining if the target was added to the
  198. set of targets that needs to be built.
  199. in_roots: true if this target is a descendant of one of the root nodes.
  200. is_executable: true if the type of target is executable.
  201. is_static_library: true if the type of target is static_library.
  202. is_or_has_linked_ancestor: true if the target does a link (eg executable), or
  203. if there is a target in back_deps that does a link."""
  204. def __init__(self, name):
  205. self.deps = set()
  206. self.match_status = MATCH_STATUS_TBD
  207. self.back_deps = set()
  208. self.name = name
  209. # TODO(sky): I don't like hanging this off Target. This state is specific
  210. # to certain functions and should be isolated there.
  211. self.visited = False
  212. self.requires_build = False
  213. self.added_to_compile_targets = False
  214. self.in_roots = False
  215. self.is_executable = False
  216. self.is_static_library = False
  217. self.is_or_has_linked_ancestor = False
  218. class Config:
  219. """Details what we're looking for
  220. files: set of files to search for
  221. targets: see file description for details."""
  222. def __init__(self):
  223. self.files = []
  224. self.targets = set()
  225. self.additional_compile_target_names = set()
  226. self.test_target_names = set()
  227. def Init(self, params):
  228. """Initializes Config. This is a separate method as it raises an exception
  229. if there is a parse error."""
  230. generator_flags = params.get("generator_flags", {})
  231. config_path = generator_flags.get("config_path", None)
  232. if not config_path:
  233. return
  234. try:
  235. f = open(config_path)
  236. config = json.load(f)
  237. f.close()
  238. except OSError:
  239. raise Exception("Unable to open file " + config_path)
  240. except ValueError as e:
  241. raise Exception("Unable to parse config file " + config_path + str(e))
  242. if not isinstance(config, dict):
  243. raise Exception("config_path must be a JSON file containing a dictionary")
  244. self.files = config.get("files", [])
  245. self.additional_compile_target_names = set(
  246. config.get("additional_compile_targets", [])
  247. )
  248. self.test_target_names = set(config.get("test_targets", []))
  249. def _WasBuildFileModified(build_file, data, files, toplevel_dir):
  250. """Returns true if the build file |build_file| is either in |files| or
  251. one of the files included by |build_file| is in |files|. |toplevel_dir| is
  252. the root of the source tree."""
  253. if _ToLocalPath(toplevel_dir, _ToGypPath(build_file)) in files:
  254. if debug:
  255. print("gyp file modified", build_file)
  256. return True
  257. # First element of included_files is the file itself.
  258. if len(data[build_file]["included_files"]) <= 1:
  259. return False
  260. for include_file in data[build_file]["included_files"][1:]:
  261. # |included_files| are relative to the directory of the |build_file|.
  262. rel_include_file = _ToGypPath(
  263. gyp.common.UnrelativePath(include_file, build_file)
  264. )
  265. if _ToLocalPath(toplevel_dir, rel_include_file) in files:
  266. if debug:
  267. print(
  268. "included gyp file modified, gyp_file=",
  269. build_file,
  270. "included file=",
  271. rel_include_file,
  272. )
  273. return True
  274. return False
  275. def _GetOrCreateTargetByName(targets, target_name):
  276. """Creates or returns the Target at targets[target_name]. If there is no
  277. Target for |target_name| one is created. Returns a tuple of whether a new
  278. Target was created and the Target."""
  279. if target_name in targets:
  280. return False, targets[target_name]
  281. target = Target(target_name)
  282. targets[target_name] = target
  283. return True, target
  284. def _DoesTargetTypeRequireBuild(target_dict):
  285. """Returns true if the target type is such that it needs to be built."""
  286. # If a 'none' target has rules or actions we assume it requires a build.
  287. return bool(
  288. target_dict["type"] != "none"
  289. or target_dict.get("actions")
  290. or target_dict.get("rules")
  291. )
  292. def _GenerateTargets(data, target_list, target_dicts, toplevel_dir, files, build_files):
  293. """Returns a tuple of the following:
  294. . A dictionary mapping from fully qualified name to Target.
  295. . A list of the targets that have a source file in |files|.
  296. . Targets that constitute the 'all' target. See description at top of file
  297. for details on the 'all' target.
  298. This sets the |match_status| of the targets that contain any of the source
  299. files in |files| to MATCH_STATUS_MATCHES.
  300. |toplevel_dir| is the root of the source tree."""
  301. # Maps from target name to Target.
  302. name_to_target = {}
  303. # Targets that matched.
  304. matching_targets = []
  305. # Queue of targets to visit.
  306. targets_to_visit = target_list[:]
  307. # Maps from build file to a boolean indicating whether the build file is in
  308. # |files|.
  309. build_file_in_files = {}
  310. # Root targets across all files.
  311. roots = set()
  312. # Set of Targets in |build_files|.
  313. build_file_targets = set()
  314. while len(targets_to_visit) > 0:
  315. target_name = targets_to_visit.pop()
  316. created_target, target = _GetOrCreateTargetByName(name_to_target, target_name)
  317. if created_target:
  318. roots.add(target)
  319. elif target.visited:
  320. continue
  321. target.visited = True
  322. target.requires_build = _DoesTargetTypeRequireBuild(target_dicts[target_name])
  323. target_type = target_dicts[target_name]["type"]
  324. target.is_executable = target_type == "executable"
  325. target.is_static_library = target_type == "static_library"
  326. target.is_or_has_linked_ancestor = (
  327. target_type in {"executable", "shared_library"}
  328. )
  329. build_file = gyp.common.ParseQualifiedTarget(target_name)[0]
  330. if build_file not in build_file_in_files:
  331. build_file_in_files[build_file] = _WasBuildFileModified(
  332. build_file, data, files, toplevel_dir
  333. )
  334. if build_file in build_files:
  335. build_file_targets.add(target)
  336. # If a build file (or any of its included files) is modified we assume all
  337. # targets in the file are modified.
  338. if build_file_in_files[build_file]:
  339. print("matching target from modified build file", target_name)
  340. target.match_status = MATCH_STATUS_MATCHES
  341. matching_targets.append(target)
  342. else:
  343. sources = _ExtractSources(
  344. target_name, target_dicts[target_name], toplevel_dir
  345. )
  346. for source in sources:
  347. if _ToGypPath(os.path.normpath(source)) in files:
  348. print("target", target_name, "matches", source)
  349. target.match_status = MATCH_STATUS_MATCHES
  350. matching_targets.append(target)
  351. break
  352. # Add dependencies to visit as well as updating back pointers for deps.
  353. for dep in target_dicts[target_name].get("dependencies", []):
  354. targets_to_visit.append(dep)
  355. created_dep_target, dep_target = _GetOrCreateTargetByName(
  356. name_to_target, dep
  357. )
  358. if not created_dep_target:
  359. roots.discard(dep_target)
  360. target.deps.add(dep_target)
  361. dep_target.back_deps.add(target)
  362. return name_to_target, matching_targets, roots & build_file_targets
  363. def _GetUnqualifiedToTargetMapping(all_targets, to_find):
  364. """Returns a tuple of the following:
  365. . mapping (dictionary) from unqualified name to Target for all the
  366. Targets in |to_find|.
  367. . any target names not found. If this is empty all targets were found."""
  368. result = {}
  369. if not to_find:
  370. return {}, []
  371. to_find = set(to_find)
  372. for target_name in all_targets:
  373. extracted = gyp.common.ParseQualifiedTarget(target_name)
  374. if len(extracted) > 1 and extracted[1] in to_find:
  375. to_find.remove(extracted[1])
  376. result[extracted[1]] = all_targets[target_name]
  377. if not to_find:
  378. return result, []
  379. return result, list(to_find)
  380. def _DoesTargetDependOnMatchingTargets(target):
  381. """Returns true if |target| or any of its dependencies is one of the
  382. targets containing the files supplied as input to analyzer. This updates
  383. |matches| of the Targets as it recurses.
  384. target: the Target to look for."""
  385. if target.match_status == MATCH_STATUS_DOESNT_MATCH:
  386. return False
  387. if (
  388. target.match_status in {MATCH_STATUS_MATCHES,
  389. MATCH_STATUS_MATCHES_BY_DEPENDENCY}
  390. ):
  391. return True
  392. for dep in target.deps:
  393. if _DoesTargetDependOnMatchingTargets(dep):
  394. target.match_status = MATCH_STATUS_MATCHES_BY_DEPENDENCY
  395. print("\t", target.name, "matches by dep", dep.name)
  396. return True
  397. target.match_status = MATCH_STATUS_DOESNT_MATCH
  398. return False
  399. def _GetTargetsDependingOnMatchingTargets(possible_targets):
  400. """Returns the list of Targets in |possible_targets| that depend (either
  401. directly on indirectly) on at least one of the targets containing the files
  402. supplied as input to analyzer.
  403. possible_targets: targets to search from."""
  404. found = []
  405. print("Targets that matched by dependency:")
  406. for target in possible_targets:
  407. if _DoesTargetDependOnMatchingTargets(target):
  408. found.append(target)
  409. return found
  410. def _AddCompileTargets(target, roots, add_if_no_ancestor, result):
  411. """Recurses through all targets that depend on |target|, adding all targets
  412. that need to be built (and are in |roots|) to |result|.
  413. roots: set of root targets.
  414. add_if_no_ancestor: If true and there are no ancestors of |target| then add
  415. |target| to |result|. |target| must still be in |roots|.
  416. result: targets that need to be built are added here."""
  417. if target.visited:
  418. return
  419. target.visited = True
  420. target.in_roots = target in roots
  421. for back_dep_target in target.back_deps:
  422. _AddCompileTargets(back_dep_target, roots, False, result)
  423. target.added_to_compile_targets |= back_dep_target.added_to_compile_targets
  424. target.in_roots |= back_dep_target.in_roots
  425. target.is_or_has_linked_ancestor |= back_dep_target.is_or_has_linked_ancestor
  426. # Always add 'executable' targets. Even though they may be built by other
  427. # targets that depend upon them it makes detection of what is going to be
  428. # built easier.
  429. # And always add static_libraries that have no dependencies on them from
  430. # linkables. This is necessary as the other dependencies on them may be
  431. # static libraries themselves, which are not compile time dependencies.
  432. if target.in_roots and (
  433. target.is_executable
  434. or (
  435. not target.added_to_compile_targets
  436. and (add_if_no_ancestor or target.requires_build)
  437. )
  438. or (
  439. target.is_static_library
  440. and add_if_no_ancestor
  441. and not target.is_or_has_linked_ancestor
  442. )
  443. ):
  444. print(
  445. "\t\tadding to compile targets",
  446. target.name,
  447. "executable",
  448. target.is_executable,
  449. "added_to_compile_targets",
  450. target.added_to_compile_targets,
  451. "add_if_no_ancestor",
  452. add_if_no_ancestor,
  453. "requires_build",
  454. target.requires_build,
  455. "is_static_library",
  456. target.is_static_library,
  457. "is_or_has_linked_ancestor",
  458. target.is_or_has_linked_ancestor,
  459. )
  460. result.add(target)
  461. target.added_to_compile_targets = True
  462. def _GetCompileTargets(matching_targets, supplied_targets):
  463. """Returns the set of Targets that require a build.
  464. matching_targets: targets that changed and need to be built.
  465. supplied_targets: set of targets supplied to analyzer to search from."""
  466. result = set()
  467. for target in matching_targets:
  468. print("finding compile targets for match", target.name)
  469. _AddCompileTargets(target, supplied_targets, True, result)
  470. return result
  471. def _WriteOutput(params, **values):
  472. """Writes the output, either to stdout or a file is specified."""
  473. if "error" in values:
  474. print("Error:", values["error"])
  475. if "status" in values:
  476. print(values["status"])
  477. if "targets" in values:
  478. values["targets"].sort()
  479. print("Supplied targets that depend on changed files:")
  480. for target in values["targets"]:
  481. print("\t", target)
  482. if "invalid_targets" in values:
  483. values["invalid_targets"].sort()
  484. print("The following targets were not found:")
  485. for target in values["invalid_targets"]:
  486. print("\t", target)
  487. if "build_targets" in values:
  488. values["build_targets"].sort()
  489. print("Targets that require a build:")
  490. for target in values["build_targets"]:
  491. print("\t", target)
  492. if "compile_targets" in values:
  493. values["compile_targets"].sort()
  494. print("Targets that need to be built:")
  495. for target in values["compile_targets"]:
  496. print("\t", target)
  497. if "test_targets" in values:
  498. values["test_targets"].sort()
  499. print("Test targets:")
  500. for target in values["test_targets"]:
  501. print("\t", target)
  502. output_path = params.get("generator_flags", {}).get("analyzer_output_path", None)
  503. if not output_path:
  504. print(json.dumps(values))
  505. return
  506. try:
  507. f = open(output_path, "w")
  508. f.write(json.dumps(values) + "\n")
  509. f.close()
  510. except OSError as e:
  511. print("Error writing to output file", output_path, str(e))
  512. def _WasGypIncludeFileModified(params, files):
  513. """Returns true if one of the files in |files| is in the set of included
  514. files."""
  515. if params["options"].includes:
  516. for include in params["options"].includes:
  517. if _ToGypPath(os.path.normpath(include)) in files:
  518. print("Include file modified, assuming all changed", include)
  519. return True
  520. return False
  521. def _NamesNotIn(names, mapping):
  522. """Returns a list of the values in |names| that are not in |mapping|."""
  523. return [name for name in names if name not in mapping]
  524. def _LookupTargets(names, mapping):
  525. """Returns a list of the mapping[name] for each value in |names| that is in
  526. |mapping|."""
  527. return [mapping[name] for name in names if name in mapping]
  528. def CalculateVariables(default_variables, params):
  529. """Calculate additional variables for use in the build (called by gyp)."""
  530. flavor = gyp.common.GetFlavor(params)
  531. if flavor == "mac":
  532. default_variables.setdefault("OS", "mac")
  533. elif flavor == "win":
  534. default_variables.setdefault("OS", "win")
  535. gyp.msvs_emulation.CalculateCommonVariables(default_variables, params)
  536. else:
  537. operating_system = flavor
  538. if flavor == "android":
  539. operating_system = "linux" # Keep this legacy behavior for now.
  540. default_variables.setdefault("OS", operating_system)
  541. class TargetCalculator:
  542. """Calculates the matching test_targets and matching compile_targets."""
  543. def __init__(
  544. self,
  545. files,
  546. additional_compile_target_names,
  547. test_target_names,
  548. data,
  549. target_list,
  550. target_dicts,
  551. toplevel_dir,
  552. build_files,
  553. ):
  554. self._additional_compile_target_names = set(additional_compile_target_names)
  555. self._test_target_names = set(test_target_names)
  556. (
  557. self._name_to_target,
  558. self._changed_targets,
  559. self._root_targets,
  560. ) = _GenerateTargets(
  561. data, target_list, target_dicts, toplevel_dir, frozenset(files), build_files
  562. )
  563. (
  564. self._unqualified_mapping,
  565. self.invalid_targets,
  566. ) = _GetUnqualifiedToTargetMapping(
  567. self._name_to_target, self._supplied_target_names_no_all()
  568. )
  569. def _supplied_target_names(self):
  570. return self._additional_compile_target_names | self._test_target_names
  571. def _supplied_target_names_no_all(self):
  572. """Returns the supplied test targets without 'all'."""
  573. result = self._supplied_target_names()
  574. result.discard("all")
  575. return result
  576. def is_build_impacted(self):
  577. """Returns true if the supplied files impact the build at all."""
  578. return self._changed_targets
  579. def find_matching_test_target_names(self):
  580. """Returns the set of output test targets."""
  581. assert self.is_build_impacted()
  582. # Find the test targets first. 'all' is special cased to mean all the
  583. # root targets. To deal with all the supplied |test_targets| are expanded
  584. # to include the root targets during lookup. If any of the root targets
  585. # match, we remove it and replace it with 'all'.
  586. test_target_names_no_all = set(self._test_target_names)
  587. test_target_names_no_all.discard("all")
  588. test_targets_no_all = _LookupTargets(
  589. test_target_names_no_all, self._unqualified_mapping
  590. )
  591. test_target_names_contains_all = "all" in self._test_target_names
  592. if test_target_names_contains_all:
  593. test_targets = list(set(test_targets_no_all) | set(self._root_targets))
  594. else:
  595. test_targets = list(test_targets_no_all)
  596. print("supplied test_targets")
  597. for target_name in self._test_target_names:
  598. print("\t", target_name)
  599. print("found test_targets")
  600. for target in test_targets:
  601. print("\t", target.name)
  602. print("searching for matching test targets")
  603. matching_test_targets = _GetTargetsDependingOnMatchingTargets(test_targets)
  604. matching_test_targets_contains_all = test_target_names_contains_all and set(
  605. matching_test_targets
  606. ) & set(self._root_targets)
  607. if matching_test_targets_contains_all:
  608. # Remove any of the targets for all that were not explicitly supplied,
  609. # 'all' is subsequently added to the matching names below.
  610. matching_test_targets = list(
  611. set(matching_test_targets) & set(test_targets_no_all)
  612. )
  613. print("matched test_targets")
  614. for target in matching_test_targets:
  615. print("\t", target.name)
  616. matching_target_names = [
  617. gyp.common.ParseQualifiedTarget(target.name)[1]
  618. for target in matching_test_targets
  619. ]
  620. if matching_test_targets_contains_all:
  621. matching_target_names.append("all")
  622. print("\tall")
  623. return matching_target_names
  624. def find_matching_compile_target_names(self):
  625. """Returns the set of output compile targets."""
  626. assert self.is_build_impacted()
  627. # Compile targets are found by searching up from changed targets.
  628. # Reset the visited status for _GetBuildTargets.
  629. for target in self._name_to_target.values():
  630. target.visited = False
  631. supplied_targets = _LookupTargets(
  632. self._supplied_target_names_no_all(), self._unqualified_mapping
  633. )
  634. if "all" in self._supplied_target_names():
  635. supplied_targets = list(set(supplied_targets) | set(self._root_targets))
  636. print("Supplied test_targets & compile_targets")
  637. for target in supplied_targets:
  638. print("\t", target.name)
  639. print("Finding compile targets")
  640. compile_targets = _GetCompileTargets(self._changed_targets, supplied_targets)
  641. return [
  642. gyp.common.ParseQualifiedTarget(target.name)[1]
  643. for target in compile_targets
  644. ]
  645. def GenerateOutput(target_list, target_dicts, data, params):
  646. """Called by gyp as the final stage. Outputs results."""
  647. config = Config()
  648. try:
  649. config.Init(params)
  650. if not config.files:
  651. raise Exception(
  652. "Must specify files to analyze via config_path generator flag"
  653. )
  654. toplevel_dir = _ToGypPath(os.path.abspath(params["options"].toplevel_dir))
  655. if debug:
  656. print("toplevel_dir", toplevel_dir)
  657. if _WasGypIncludeFileModified(params, config.files):
  658. result_dict = {
  659. "status": all_changed_string,
  660. "test_targets": list(config.test_target_names),
  661. "compile_targets": list(
  662. config.additional_compile_target_names | config.test_target_names
  663. ),
  664. }
  665. _WriteOutput(params, **result_dict)
  666. return
  667. calculator = TargetCalculator(
  668. config.files,
  669. config.additional_compile_target_names,
  670. config.test_target_names,
  671. data,
  672. target_list,
  673. target_dicts,
  674. toplevel_dir,
  675. params["build_files"],
  676. )
  677. if not calculator.is_build_impacted():
  678. result_dict = {
  679. "status": no_dependency_string,
  680. "test_targets": [],
  681. "compile_targets": [],
  682. }
  683. if calculator.invalid_targets:
  684. result_dict["invalid_targets"] = calculator.invalid_targets
  685. _WriteOutput(params, **result_dict)
  686. return
  687. test_target_names = calculator.find_matching_test_target_names()
  688. compile_target_names = calculator.find_matching_compile_target_names()
  689. found_at_least_one_target = compile_target_names or test_target_names
  690. result_dict = {
  691. "test_targets": test_target_names,
  692. "status": found_dependency_string
  693. if found_at_least_one_target
  694. else no_dependency_string,
  695. "compile_targets": list(set(compile_target_names) | set(test_target_names)),
  696. }
  697. if calculator.invalid_targets:
  698. result_dict["invalid_targets"] = calculator.invalid_targets
  699. _WriteOutput(params, **result_dict)
  700. except Exception as e:
  701. _WriteOutput(params, error=str(e))