c1fb3eb595098db941a71425852175626d07621d638992a6260382bc790a6fd7d70b51617876d1cacd9a6f61c526e1351335cca9e40939f0ee37281904843b 117 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967
  1. # Copyright (c) 2013 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. import collections
  5. import copy
  6. import hashlib
  7. import json
  8. import multiprocessing
  9. import os.path
  10. import re
  11. import signal
  12. import shutil
  13. import subprocess
  14. import sys
  15. import gyp
  16. import gyp.common
  17. import gyp.msvs_emulation
  18. import gyp.MSVSUtil as MSVSUtil
  19. import gyp.xcode_emulation
  20. from io import StringIO
  21. from gyp.common import GetEnvironFallback
  22. import gyp.ninja_syntax as ninja_syntax
  23. generator_default_variables = {
  24. "EXECUTABLE_PREFIX": "",
  25. "EXECUTABLE_SUFFIX": "",
  26. "STATIC_LIB_PREFIX": "lib",
  27. "STATIC_LIB_SUFFIX": ".a",
  28. "SHARED_LIB_PREFIX": "lib",
  29. # Gyp expects the following variables to be expandable by the build
  30. # system to the appropriate locations. Ninja prefers paths to be
  31. # known at gyp time. To resolve this, introduce special
  32. # variables starting with $! and $| (which begin with a $ so gyp knows it
  33. # should be treated specially, but is otherwise an invalid
  34. # ninja/shell variable) that are passed to gyp here but expanded
  35. # before writing out into the target .ninja files; see
  36. # ExpandSpecial.
  37. # $! is used for variables that represent a path and that can only appear at
  38. # the start of a string, while $| is used for variables that can appear
  39. # anywhere in a string.
  40. "INTERMEDIATE_DIR": "$!INTERMEDIATE_DIR",
  41. "SHARED_INTERMEDIATE_DIR": "$!PRODUCT_DIR/gen",
  42. "PRODUCT_DIR": "$!PRODUCT_DIR",
  43. "CONFIGURATION_NAME": "$|CONFIGURATION_NAME",
  44. # Special variables that may be used by gyp 'rule' targets.
  45. # We generate definitions for these variables on the fly when processing a
  46. # rule.
  47. "RULE_INPUT_ROOT": "${root}",
  48. "RULE_INPUT_DIRNAME": "${dirname}",
  49. "RULE_INPUT_PATH": "${source}",
  50. "RULE_INPUT_EXT": "${ext}",
  51. "RULE_INPUT_NAME": "${name}",
  52. }
  53. # Placates pylint.
  54. generator_additional_non_configuration_keys = []
  55. generator_additional_path_sections = []
  56. generator_extra_sources_for_rules = []
  57. generator_filelist_paths = None
  58. generator_supports_multiple_toolsets = gyp.common.CrossCompileRequested()
  59. def StripPrefix(arg, prefix):
  60. if arg.startswith(prefix):
  61. return arg[len(prefix) :]
  62. return arg
  63. def QuoteShellArgument(arg, flavor):
  64. """Quote a string such that it will be interpreted as a single argument
  65. by the shell."""
  66. # Rather than attempting to enumerate the bad shell characters, just
  67. # allow common OK ones and quote anything else.
  68. if re.match(r"^[a-zA-Z0-9_=.\\/-]+$", arg):
  69. return arg # No quoting necessary.
  70. if flavor == "win":
  71. return gyp.msvs_emulation.QuoteForRspFile(arg)
  72. return "'" + arg.replace("'", "'" + '"\'"' + "'") + "'"
  73. def Define(d, flavor):
  74. """Takes a preprocessor define and returns a -D parameter that's ninja- and
  75. shell-escaped."""
  76. if flavor == "win":
  77. # cl.exe replaces literal # characters with = in preprocessor definitions for
  78. # some reason. Octal-encode to work around that.
  79. d = d.replace("#", "\\%03o" % ord("#"))
  80. return QuoteShellArgument(ninja_syntax.escape("-D" + d), flavor)
  81. def AddArch(output, arch):
  82. """Adds an arch string to an output path."""
  83. output, extension = os.path.splitext(output)
  84. return f"{output}.{arch}{extension}"
  85. class Target:
  86. """Target represents the paths used within a single gyp target.
  87. Conceptually, building a single target A is a series of steps:
  88. 1) actions/rules/copies generates source/resources/etc.
  89. 2) compiles generates .o files
  90. 3) link generates a binary (library/executable)
  91. 4) bundle merges the above in a mac bundle
  92. (Any of these steps can be optional.)
  93. From a build ordering perspective, a dependent target B could just
  94. depend on the last output of this series of steps.
  95. But some dependent commands sometimes need to reach inside the box.
  96. For example, when linking B it needs to get the path to the static
  97. library generated by A.
  98. This object stores those paths. To keep things simple, member
  99. variables only store concrete paths to single files, while methods
  100. compute derived values like "the last output of the target".
  101. """
  102. def __init__(self, type):
  103. # Gyp type ("static_library", etc.) of this target.
  104. self.type = type
  105. # File representing whether any input dependencies necessary for
  106. # dependent actions have completed.
  107. self.preaction_stamp = None
  108. # File representing whether any input dependencies necessary for
  109. # dependent compiles have completed.
  110. self.precompile_stamp = None
  111. # File representing the completion of actions/rules/copies, if any.
  112. self.actions_stamp = None
  113. # Path to the output of the link step, if any.
  114. self.binary = None
  115. # Path to the file representing the completion of building the bundle,
  116. # if any.
  117. self.bundle = None
  118. # On Windows, incremental linking requires linking against all the .objs
  119. # that compose a .lib (rather than the .lib itself). That list is stored
  120. # here. In this case, we also need to save the compile_deps for the target,
  121. # so that the target that directly depends on the .objs can also depend
  122. # on those.
  123. self.component_objs = None
  124. self.compile_deps = None
  125. # Windows only. The import .lib is the output of a build step, but
  126. # because dependents only link against the lib (not both the lib and the
  127. # dll) we keep track of the import library here.
  128. self.import_lib = None
  129. # Track if this target contains any C++ files, to decide if gcc or g++
  130. # should be used for linking.
  131. self.uses_cpp = False
  132. def Linkable(self):
  133. """Return true if this is a target that can be linked against."""
  134. return self.type in ("static_library", "shared_library")
  135. def UsesToc(self, flavor):
  136. """Return true if the target should produce a restat rule based on a TOC
  137. file."""
  138. # For bundles, the .TOC should be produced for the binary, not for
  139. # FinalOutput(). But the naive approach would put the TOC file into the
  140. # bundle, so don't do this for bundles for now.
  141. if flavor == "win" or self.bundle:
  142. return False
  143. return self.type in ("shared_library", "loadable_module")
  144. def PreActionInput(self, flavor):
  145. """Return the path, if any, that should be used as a dependency of
  146. any dependent action step."""
  147. if self.UsesToc(flavor):
  148. return self.FinalOutput() + ".TOC"
  149. return self.FinalOutput() or self.preaction_stamp
  150. def PreCompileInput(self):
  151. """Return the path, if any, that should be used as a dependency of
  152. any dependent compile step."""
  153. return self.actions_stamp or self.precompile_stamp
  154. def FinalOutput(self):
  155. """Return the last output of the target, which depends on all prior
  156. steps."""
  157. return self.bundle or self.binary or self.actions_stamp
  158. # A small discourse on paths as used within the Ninja build:
  159. # All files we produce (both at gyp and at build time) appear in the
  160. # build directory (e.g. out/Debug).
  161. #
  162. # Paths within a given .gyp file are always relative to the directory
  163. # containing the .gyp file. Call these "gyp paths". This includes
  164. # sources as well as the starting directory a given gyp rule/action
  165. # expects to be run from. We call the path from the source root to
  166. # the gyp file the "base directory" within the per-.gyp-file
  167. # NinjaWriter code.
  168. #
  169. # All paths as written into the .ninja files are relative to the build
  170. # directory. Call these paths "ninja paths".
  171. #
  172. # We translate between these two notions of paths with two helper
  173. # functions:
  174. #
  175. # - GypPathToNinja translates a gyp path (i.e. relative to the .gyp file)
  176. # into the equivalent ninja path.
  177. #
  178. # - GypPathToUniqueOutput translates a gyp path into a ninja path to write
  179. # an output file; the result can be namespaced such that it is unique
  180. # to the input file name as well as the output target name.
  181. class NinjaWriter:
  182. def __init__(
  183. self,
  184. hash_for_rules,
  185. target_outputs,
  186. base_dir,
  187. build_dir,
  188. output_file,
  189. toplevel_build,
  190. output_file_name,
  191. flavor,
  192. toplevel_dir=None,
  193. ):
  194. """
  195. base_dir: path from source root to directory containing this gyp file,
  196. by gyp semantics, all input paths are relative to this
  197. build_dir: path from source root to build output
  198. toplevel_dir: path to the toplevel directory
  199. """
  200. self.hash_for_rules = hash_for_rules
  201. self.target_outputs = target_outputs
  202. self.base_dir = base_dir
  203. self.build_dir = build_dir
  204. self.ninja = ninja_syntax.Writer(output_file)
  205. self.toplevel_build = toplevel_build
  206. self.output_file_name = output_file_name
  207. self.flavor = flavor
  208. self.abs_build_dir = None
  209. if toplevel_dir is not None:
  210. self.abs_build_dir = os.path.abspath(os.path.join(toplevel_dir, build_dir))
  211. self.obj_ext = ".obj" if flavor == "win" else ".o"
  212. if flavor == "win":
  213. # See docstring of msvs_emulation.GenerateEnvironmentFiles().
  214. self.win_env = {}
  215. for arch in ("x86", "x64"):
  216. self.win_env[arch] = "environment." + arch
  217. # Relative path from build output dir to base dir.
  218. build_to_top = gyp.common.InvertRelativePath(build_dir, toplevel_dir)
  219. self.build_to_base = os.path.join(build_to_top, base_dir)
  220. # Relative path from base dir to build dir.
  221. base_to_top = gyp.common.InvertRelativePath(base_dir, toplevel_dir)
  222. self.base_to_build = os.path.join(base_to_top, build_dir)
  223. def ExpandSpecial(self, path, product_dir=None):
  224. """Expand specials like $!PRODUCT_DIR in |path|.
  225. If |product_dir| is None, assumes the cwd is already the product
  226. dir. Otherwise, |product_dir| is the relative path to the product
  227. dir.
  228. """
  229. PRODUCT_DIR = "$!PRODUCT_DIR"
  230. if PRODUCT_DIR in path:
  231. if product_dir:
  232. path = path.replace(PRODUCT_DIR, product_dir)
  233. else:
  234. path = path.replace(PRODUCT_DIR + "/", "")
  235. path = path.replace(PRODUCT_DIR + "\\", "")
  236. path = path.replace(PRODUCT_DIR, ".")
  237. INTERMEDIATE_DIR = "$!INTERMEDIATE_DIR"
  238. if INTERMEDIATE_DIR in path:
  239. int_dir = self.GypPathToUniqueOutput("gen")
  240. # GypPathToUniqueOutput generates a path relative to the product dir,
  241. # so insert product_dir in front if it is provided.
  242. path = path.replace(
  243. INTERMEDIATE_DIR, os.path.join(product_dir or "", int_dir)
  244. )
  245. CONFIGURATION_NAME = "$|CONFIGURATION_NAME"
  246. path = path.replace(CONFIGURATION_NAME, self.config_name)
  247. return path
  248. def ExpandRuleVariables(self, path, root, dirname, source, ext, name):
  249. if self.flavor == "win":
  250. path = self.msvs_settings.ConvertVSMacros(path, config=self.config_name)
  251. path = path.replace(generator_default_variables["RULE_INPUT_ROOT"], root)
  252. path = path.replace(generator_default_variables["RULE_INPUT_DIRNAME"], dirname)
  253. path = path.replace(generator_default_variables["RULE_INPUT_PATH"], source)
  254. path = path.replace(generator_default_variables["RULE_INPUT_EXT"], ext)
  255. path = path.replace(generator_default_variables["RULE_INPUT_NAME"], name)
  256. return path
  257. def GypPathToNinja(self, path, env=None):
  258. """Translate a gyp path to a ninja path, optionally expanding environment
  259. variable references in |path| with |env|.
  260. See the above discourse on path conversions."""
  261. if env:
  262. if self.flavor == "mac":
  263. path = gyp.xcode_emulation.ExpandEnvVars(path, env)
  264. elif self.flavor == "win":
  265. path = gyp.msvs_emulation.ExpandMacros(path, env)
  266. if path.startswith("$!"):
  267. expanded = self.ExpandSpecial(path)
  268. if self.flavor == "win":
  269. expanded = os.path.normpath(expanded)
  270. return expanded
  271. if "$|" in path:
  272. path = self.ExpandSpecial(path)
  273. assert "$" not in path, path
  274. return os.path.normpath(os.path.join(self.build_to_base, path))
  275. def GypPathToUniqueOutput(self, path, qualified=True):
  276. """Translate a gyp path to a ninja path for writing output.
  277. If qualified is True, qualify the resulting filename with the name
  278. of the target. This is necessary when e.g. compiling the same
  279. path twice for two separate output targets.
  280. See the above discourse on path conversions."""
  281. path = self.ExpandSpecial(path)
  282. assert not path.startswith("$"), path
  283. # Translate the path following this scheme:
  284. # Input: foo/bar.gyp, target targ, references baz/out.o
  285. # Output: obj/foo/baz/targ.out.o (if qualified)
  286. # obj/foo/baz/out.o (otherwise)
  287. # (and obj.host instead of obj for cross-compiles)
  288. #
  289. # Why this scheme and not some other one?
  290. # 1) for a given input, you can compute all derived outputs by matching
  291. # its path, even if the input is brought via a gyp file with '..'.
  292. # 2) simple files like libraries and stamps have a simple filename.
  293. obj = "obj"
  294. if self.toolset != "target":
  295. obj += "." + self.toolset
  296. path_dir, path_basename = os.path.split(path)
  297. assert not os.path.isabs(path_dir), (
  298. "'%s' can not be absolute path (see crbug.com/462153)." % path_dir
  299. )
  300. if qualified:
  301. path_basename = self.name + "." + path_basename
  302. return os.path.normpath(
  303. os.path.join(obj, self.base_dir, path_dir, path_basename)
  304. )
  305. def WriteCollapsedDependencies(self, name, targets, order_only=None):
  306. """Given a list of targets, return a path for a single file
  307. representing the result of building all the targets or None.
  308. Uses a stamp file if necessary."""
  309. assert targets == [item for item in targets if item], targets
  310. if len(targets) == 0:
  311. assert not order_only
  312. return None
  313. if len(targets) > 1 or order_only:
  314. stamp = self.GypPathToUniqueOutput(name + ".stamp")
  315. targets = self.ninja.build(stamp, "stamp", targets, order_only=order_only)
  316. self.ninja.newline()
  317. return targets[0]
  318. def _SubninjaNameForArch(self, arch):
  319. output_file_base = os.path.splitext(self.output_file_name)[0]
  320. return f"{output_file_base}.{arch}.ninja"
  321. def WriteSpec(self, spec, config_name, generator_flags):
  322. """The main entry point for NinjaWriter: write the build rules for a spec.
  323. Returns a Target object, which represents the output paths for this spec.
  324. Returns None if there are no outputs (e.g. a settings-only 'none' type
  325. target)."""
  326. self.config_name = config_name
  327. self.name = spec["target_name"]
  328. self.toolset = spec["toolset"]
  329. config = spec["configurations"][config_name]
  330. self.target = Target(spec["type"])
  331. self.is_standalone_static_library = bool(
  332. spec.get("standalone_static_library", 0)
  333. )
  334. self.target_rpath = generator_flags.get("target_rpath", r"\$$ORIGIN/lib/")
  335. self.is_mac_bundle = gyp.xcode_emulation.IsMacBundle(self.flavor, spec)
  336. self.xcode_settings = self.msvs_settings = None
  337. if self.flavor == "mac":
  338. self.xcode_settings = gyp.xcode_emulation.XcodeSettings(spec)
  339. mac_toolchain_dir = generator_flags.get("mac_toolchain_dir", None)
  340. if mac_toolchain_dir:
  341. self.xcode_settings.mac_toolchain_dir = mac_toolchain_dir
  342. if self.flavor == "win":
  343. self.msvs_settings = gyp.msvs_emulation.MsvsSettings(spec, generator_flags)
  344. arch = self.msvs_settings.GetArch(config_name)
  345. self.ninja.variable("arch", self.win_env[arch])
  346. self.ninja.variable("cc", "$cl_" + arch)
  347. self.ninja.variable("cxx", "$cl_" + arch)
  348. self.ninja.variable("cc_host", "$cl_" + arch)
  349. self.ninja.variable("cxx_host", "$cl_" + arch)
  350. self.ninja.variable("asm", "$ml_" + arch)
  351. if self.flavor == "mac":
  352. self.archs = self.xcode_settings.GetActiveArchs(config_name)
  353. if len(self.archs) > 1:
  354. self.arch_subninjas = {
  355. arch: ninja_syntax.Writer(
  356. OpenOutput(
  357. os.path.join(
  358. self.toplevel_build, self._SubninjaNameForArch(arch)
  359. ),
  360. "w",
  361. )
  362. )
  363. for arch in self.archs
  364. }
  365. # Compute predepends for all rules.
  366. # actions_depends is the dependencies this target depends on before running
  367. # any of its action/rule/copy steps.
  368. # compile_depends is the dependencies this target depends on before running
  369. # any of its compile steps.
  370. actions_depends = []
  371. compile_depends = []
  372. # TODO(evan): it is rather confusing which things are lists and which
  373. # are strings. Fix these.
  374. if "dependencies" in spec:
  375. for dep in spec["dependencies"]:
  376. if dep in self.target_outputs:
  377. target = self.target_outputs[dep]
  378. actions_depends.append(target.PreActionInput(self.flavor))
  379. compile_depends.append(target.PreCompileInput())
  380. if target.uses_cpp:
  381. self.target.uses_cpp = True
  382. actions_depends = [item for item in actions_depends if item]
  383. compile_depends = [item for item in compile_depends if item]
  384. actions_depends = self.WriteCollapsedDependencies(
  385. "actions_depends", actions_depends
  386. )
  387. compile_depends = self.WriteCollapsedDependencies(
  388. "compile_depends", compile_depends
  389. )
  390. self.target.preaction_stamp = actions_depends
  391. self.target.precompile_stamp = compile_depends
  392. # Write out actions, rules, and copies. These must happen before we
  393. # compile any sources, so compute a list of predependencies for sources
  394. # while we do it.
  395. extra_sources = []
  396. mac_bundle_depends = []
  397. self.target.actions_stamp = self.WriteActionsRulesCopies(
  398. spec, extra_sources, actions_depends, mac_bundle_depends
  399. )
  400. # If we have actions/rules/copies, we depend directly on those, but
  401. # otherwise we depend on dependent target's actions/rules/copies etc.
  402. # We never need to explicitly depend on previous target's link steps,
  403. # because no compile ever depends on them.
  404. compile_depends_stamp = self.target.actions_stamp or compile_depends
  405. # Write out the compilation steps, if any.
  406. link_deps = []
  407. try:
  408. sources = extra_sources + spec.get("sources", [])
  409. except TypeError:
  410. print("extra_sources: ", str(extra_sources))
  411. print('spec.get("sources"): ', str(spec.get("sources")))
  412. raise
  413. if sources:
  414. if self.flavor == "mac" and len(self.archs) > 1:
  415. # Write subninja file containing compile and link commands scoped to
  416. # a single arch if a fat binary is being built.
  417. for arch in self.archs:
  418. self.ninja.subninja(self._SubninjaNameForArch(arch))
  419. pch = None
  420. if self.flavor == "win":
  421. gyp.msvs_emulation.VerifyMissingSources(
  422. sources, self.abs_build_dir, generator_flags, self.GypPathToNinja
  423. )
  424. pch = gyp.msvs_emulation.PrecompiledHeader(
  425. self.msvs_settings,
  426. config_name,
  427. self.GypPathToNinja,
  428. self.GypPathToUniqueOutput,
  429. self.obj_ext,
  430. )
  431. else:
  432. pch = gyp.xcode_emulation.MacPrefixHeader(
  433. self.xcode_settings,
  434. self.GypPathToNinja,
  435. lambda path, lang: self.GypPathToUniqueOutput(path + "-" + lang),
  436. )
  437. link_deps = self.WriteSources(
  438. self.ninja,
  439. config_name,
  440. config,
  441. sources,
  442. compile_depends_stamp,
  443. pch,
  444. spec,
  445. )
  446. # Some actions/rules output 'sources' that are already object files.
  447. obj_outputs = [f for f in sources if f.endswith(self.obj_ext)]
  448. if obj_outputs:
  449. if self.flavor != "mac" or len(self.archs) == 1:
  450. link_deps += [self.GypPathToNinja(o) for o in obj_outputs]
  451. else:
  452. print(
  453. "Warning: Actions/rules writing object files don't work with "
  454. "multiarch targets, dropping. (target %s)" % spec["target_name"]
  455. )
  456. elif self.flavor == "mac" and len(self.archs) > 1:
  457. link_deps = collections.defaultdict(list)
  458. compile_deps = self.target.actions_stamp or actions_depends
  459. if self.flavor == "win" and self.target.type == "static_library":
  460. self.target.component_objs = link_deps
  461. self.target.compile_deps = compile_deps
  462. # Write out a link step, if needed.
  463. output = None
  464. is_empty_bundle = not link_deps and not mac_bundle_depends
  465. if link_deps or self.target.actions_stamp or actions_depends:
  466. output = self.WriteTarget(
  467. spec, config_name, config, link_deps, compile_deps
  468. )
  469. if self.is_mac_bundle:
  470. mac_bundle_depends.append(output)
  471. # Bundle all of the above together, if needed.
  472. if self.is_mac_bundle:
  473. output = self.WriteMacBundle(spec, mac_bundle_depends, is_empty_bundle)
  474. if not output:
  475. return None
  476. assert self.target.FinalOutput(), output
  477. return self.target
  478. def _WinIdlRule(self, source, prebuild, outputs):
  479. """Handle the implicit VS .idl rule for one source file. Fills |outputs|
  480. with files that are generated."""
  481. outdir, output, vars, flags = self.msvs_settings.GetIdlBuildData(
  482. source, self.config_name
  483. )
  484. outdir = self.GypPathToNinja(outdir)
  485. def fix_path(path, rel=None):
  486. path = os.path.join(outdir, path)
  487. dirname, basename = os.path.split(source)
  488. root, ext = os.path.splitext(basename)
  489. path = self.ExpandRuleVariables(path, root, dirname, source, ext, basename)
  490. if rel:
  491. path = os.path.relpath(path, rel)
  492. return path
  493. vars = [(name, fix_path(value, outdir)) for name, value in vars]
  494. output = [fix_path(p) for p in output]
  495. vars.append(("outdir", outdir))
  496. vars.append(("idlflags", flags))
  497. input = self.GypPathToNinja(source)
  498. self.ninja.build(output, "idl", input, variables=vars, order_only=prebuild)
  499. outputs.extend(output)
  500. def WriteWinIdlFiles(self, spec, prebuild):
  501. """Writes rules to match MSVS's implicit idl handling."""
  502. assert self.flavor == "win"
  503. if self.msvs_settings.HasExplicitIdlRulesOrActions(spec):
  504. return []
  505. outputs = []
  506. for source in filter(lambda x: x.endswith(".idl"), spec["sources"]):
  507. self._WinIdlRule(source, prebuild, outputs)
  508. return outputs
  509. def WriteActionsRulesCopies(
  510. self, spec, extra_sources, prebuild, mac_bundle_depends
  511. ):
  512. """Write out the Actions, Rules, and Copies steps. Return a path
  513. representing the outputs of these steps."""
  514. outputs = []
  515. if self.is_mac_bundle:
  516. mac_bundle_resources = spec.get("mac_bundle_resources", [])[:]
  517. else:
  518. mac_bundle_resources = []
  519. extra_mac_bundle_resources = []
  520. if "actions" in spec:
  521. outputs += self.WriteActions(
  522. spec["actions"], extra_sources, prebuild, extra_mac_bundle_resources
  523. )
  524. if "rules" in spec:
  525. outputs += self.WriteRules(
  526. spec["rules"],
  527. extra_sources,
  528. prebuild,
  529. mac_bundle_resources,
  530. extra_mac_bundle_resources,
  531. )
  532. if "copies" in spec:
  533. outputs += self.WriteCopies(spec["copies"], prebuild, mac_bundle_depends)
  534. if "sources" in spec and self.flavor == "win":
  535. outputs += self.WriteWinIdlFiles(spec, prebuild)
  536. if self.xcode_settings and self.xcode_settings.IsIosFramework():
  537. self.WriteiOSFrameworkHeaders(spec, outputs, prebuild)
  538. stamp = self.WriteCollapsedDependencies("actions_rules_copies", outputs)
  539. if self.is_mac_bundle:
  540. xcassets = self.WriteMacBundleResources(
  541. extra_mac_bundle_resources + mac_bundle_resources, mac_bundle_depends
  542. )
  543. partial_info_plist = self.WriteMacXCassets(xcassets, mac_bundle_depends)
  544. self.WriteMacInfoPlist(partial_info_plist, mac_bundle_depends)
  545. return stamp
  546. def GenerateDescription(self, verb, message, fallback):
  547. """Generate and return a description of a build step.
  548. |verb| is the short summary, e.g. ACTION or RULE.
  549. |message| is a hand-written description, or None if not available.
  550. |fallback| is the gyp-level name of the step, usable as a fallback.
  551. """
  552. if self.toolset != "target":
  553. verb += "(%s)" % self.toolset
  554. if message:
  555. return f"{verb} {self.ExpandSpecial(message)}"
  556. else:
  557. return f"{verb} {self.name}: {fallback}"
  558. def WriteActions(
  559. self, actions, extra_sources, prebuild, extra_mac_bundle_resources
  560. ):
  561. # Actions cd into the base directory.
  562. env = self.GetToolchainEnv()
  563. all_outputs = []
  564. for action in actions:
  565. # First write out a rule for the action.
  566. name = "{}_{}".format(action["action_name"], self.hash_for_rules)
  567. description = self.GenerateDescription(
  568. "ACTION", action.get("message", None), name
  569. )
  570. win_shell_flags = (
  571. self.msvs_settings.GetRuleShellFlags(action)
  572. if self.flavor == "win"
  573. else None
  574. )
  575. args = action["action"]
  576. depfile = action.get("depfile", None)
  577. if depfile:
  578. depfile = self.ExpandSpecial(depfile, self.base_to_build)
  579. pool = "console" if int(action.get("ninja_use_console", 0)) else None
  580. rule_name, _ = self.WriteNewNinjaRule(
  581. name, args, description, win_shell_flags, env, pool, depfile=depfile
  582. )
  583. inputs = [self.GypPathToNinja(i, env) for i in action["inputs"]]
  584. if int(action.get("process_outputs_as_sources", False)):
  585. extra_sources += action["outputs"]
  586. if int(action.get("process_outputs_as_mac_bundle_resources", False)):
  587. extra_mac_bundle_resources += action["outputs"]
  588. outputs = [self.GypPathToNinja(o, env) for o in action["outputs"]]
  589. # Then write out an edge using the rule.
  590. self.ninja.build(outputs, rule_name, inputs, order_only=prebuild)
  591. all_outputs += outputs
  592. self.ninja.newline()
  593. return all_outputs
  594. def WriteRules(
  595. self,
  596. rules,
  597. extra_sources,
  598. prebuild,
  599. mac_bundle_resources,
  600. extra_mac_bundle_resources,
  601. ):
  602. env = self.GetToolchainEnv()
  603. all_outputs = []
  604. for rule in rules:
  605. # Skip a rule with no action and no inputs.
  606. if "action" not in rule and not rule.get("rule_sources", []):
  607. continue
  608. # First write out a rule for the rule action.
  609. name = "{}_{}".format(rule["rule_name"], self.hash_for_rules)
  610. args = rule["action"]
  611. description = self.GenerateDescription(
  612. "RULE",
  613. rule.get("message", None),
  614. ("%s " + generator_default_variables["RULE_INPUT_PATH"]) % name,
  615. )
  616. win_shell_flags = (
  617. self.msvs_settings.GetRuleShellFlags(rule)
  618. if self.flavor == "win"
  619. else None
  620. )
  621. pool = "console" if int(rule.get("ninja_use_console", 0)) else None
  622. rule_name, args = self.WriteNewNinjaRule(
  623. name, args, description, win_shell_flags, env, pool
  624. )
  625. # TODO: if the command references the outputs directly, we should
  626. # simplify it to just use $out.
  627. # Rules can potentially make use of some special variables which
  628. # must vary per source file.
  629. # Compute the list of variables we'll need to provide.
  630. special_locals = ("source", "root", "dirname", "ext", "name")
  631. needed_variables = {"source"}
  632. for argument in args:
  633. for var in special_locals:
  634. if "${%s}" % var in argument:
  635. needed_variables.add(var)
  636. needed_variables = sorted(needed_variables)
  637. def cygwin_munge(path):
  638. # pylint: disable=cell-var-from-loop
  639. if win_shell_flags and win_shell_flags.cygwin:
  640. return path.replace("\\", "/")
  641. return path
  642. inputs = [self.GypPathToNinja(i, env) for i in rule.get("inputs", [])]
  643. # If there are n source files matching the rule, and m additional rule
  644. # inputs, then adding 'inputs' to each build edge written below will
  645. # write m * n inputs. Collapsing reduces this to m + n.
  646. sources = rule.get("rule_sources", [])
  647. num_inputs = len(inputs)
  648. if prebuild:
  649. num_inputs += 1
  650. if num_inputs > 2 and len(sources) > 2:
  651. inputs = [
  652. self.WriteCollapsedDependencies(
  653. rule["rule_name"], inputs, order_only=prebuild
  654. )
  655. ]
  656. prebuild = []
  657. # For each source file, write an edge that generates all the outputs.
  658. for source in sources:
  659. source = os.path.normpath(source)
  660. dirname, basename = os.path.split(source)
  661. root, ext = os.path.splitext(basename)
  662. # Gather the list of inputs and outputs, expanding $vars if possible.
  663. outputs = [
  664. self.ExpandRuleVariables(o, root, dirname, source, ext, basename)
  665. for o in rule["outputs"]
  666. ]
  667. if int(rule.get("process_outputs_as_sources", False)):
  668. extra_sources += outputs
  669. was_mac_bundle_resource = source in mac_bundle_resources
  670. if was_mac_bundle_resource or int(
  671. rule.get("process_outputs_as_mac_bundle_resources", False)
  672. ):
  673. extra_mac_bundle_resources += outputs
  674. # Note: This is n_resources * n_outputs_in_rule.
  675. # Put to-be-removed items in a set and
  676. # remove them all in a single pass
  677. # if this becomes a performance issue.
  678. if was_mac_bundle_resource:
  679. mac_bundle_resources.remove(source)
  680. extra_bindings = []
  681. for var in needed_variables:
  682. if var == "root":
  683. extra_bindings.append(("root", cygwin_munge(root)))
  684. elif var == "dirname":
  685. # '$dirname' is a parameter to the rule action, which means
  686. # it shouldn't be converted to a Ninja path. But we don't
  687. # want $!PRODUCT_DIR in there either.
  688. dirname_expanded = self.ExpandSpecial(
  689. dirname, self.base_to_build
  690. )
  691. extra_bindings.append(
  692. ("dirname", cygwin_munge(dirname_expanded))
  693. )
  694. elif var == "source":
  695. # '$source' is a parameter to the rule action, which means
  696. # it shouldn't be converted to a Ninja path. But we don't
  697. # want $!PRODUCT_DIR in there either.
  698. source_expanded = self.ExpandSpecial(source, self.base_to_build)
  699. extra_bindings.append(("source", cygwin_munge(source_expanded)))
  700. elif var == "ext":
  701. extra_bindings.append(("ext", ext))
  702. elif var == "name":
  703. extra_bindings.append(("name", cygwin_munge(basename)))
  704. else:
  705. assert var is None, repr(var)
  706. outputs = [self.GypPathToNinja(o, env) for o in outputs]
  707. if self.flavor == "win":
  708. # WriteNewNinjaRule uses unique_name to create a rsp file on win.
  709. extra_bindings.append(
  710. ("unique_name", hashlib.md5(outputs[0]).hexdigest())
  711. )
  712. self.ninja.build(
  713. outputs,
  714. rule_name,
  715. self.GypPathToNinja(source),
  716. implicit=inputs,
  717. order_only=prebuild,
  718. variables=extra_bindings,
  719. )
  720. all_outputs.extend(outputs)
  721. return all_outputs
  722. def WriteCopies(self, copies, prebuild, mac_bundle_depends):
  723. outputs = []
  724. if self.xcode_settings:
  725. extra_env = self.xcode_settings.GetPerTargetSettings()
  726. env = self.GetToolchainEnv(additional_settings=extra_env)
  727. else:
  728. env = self.GetToolchainEnv()
  729. for to_copy in copies:
  730. for path in to_copy["files"]:
  731. # Normalize the path so trailing slashes don't confuse us.
  732. path = os.path.normpath(path)
  733. basename = os.path.split(path)[1]
  734. src = self.GypPathToNinja(path, env)
  735. dst = self.GypPathToNinja(
  736. os.path.join(to_copy["destination"], basename), env
  737. )
  738. outputs += self.ninja.build(dst, "copy", src, order_only=prebuild)
  739. if self.is_mac_bundle:
  740. # gyp has mac_bundle_resources to copy things into a bundle's
  741. # Resources folder, but there's no built-in way to copy files
  742. # to other places in the bundle.
  743. # Hence, some targets use copies for this.
  744. # Check if this file is copied into the current bundle,
  745. # and if so add it to the bundle depends so
  746. # that dependent targets get rebuilt if the copy input changes.
  747. if dst.startswith(
  748. self.xcode_settings.GetBundleContentsFolderPath()
  749. ):
  750. mac_bundle_depends.append(dst)
  751. return outputs
  752. def WriteiOSFrameworkHeaders(self, spec, outputs, prebuild):
  753. """Prebuild steps to generate hmap files and copy headers to destination."""
  754. framework = self.ComputeMacBundleOutput()
  755. all_sources = spec["sources"]
  756. copy_headers = spec["mac_framework_headers"]
  757. output = self.GypPathToUniqueOutput("headers.hmap")
  758. self.xcode_settings.header_map_path = output
  759. all_headers = map(
  760. self.GypPathToNinja, filter(lambda x: x.endswith(".h"), all_sources)
  761. )
  762. variables = [
  763. ("framework", framework),
  764. ("copy_headers", map(self.GypPathToNinja, copy_headers)),
  765. ]
  766. outputs.extend(
  767. self.ninja.build(
  768. output,
  769. "compile_ios_framework_headers",
  770. all_headers,
  771. variables=variables,
  772. order_only=prebuild,
  773. )
  774. )
  775. def WriteMacBundleResources(self, resources, bundle_depends):
  776. """Writes ninja edges for 'mac_bundle_resources'."""
  777. xcassets = []
  778. extra_env = self.xcode_settings.GetPerTargetSettings()
  779. env = self.GetSortedXcodeEnv(additional_settings=extra_env)
  780. env = self.ComputeExportEnvString(env)
  781. isBinary = self.xcode_settings.IsBinaryOutputFormat(self.config_name)
  782. for output, res in gyp.xcode_emulation.GetMacBundleResources(
  783. generator_default_variables["PRODUCT_DIR"],
  784. self.xcode_settings,
  785. map(self.GypPathToNinja, resources),
  786. ):
  787. output = self.ExpandSpecial(output)
  788. if os.path.splitext(output)[-1] != ".xcassets":
  789. self.ninja.build(
  790. output,
  791. "mac_tool",
  792. res,
  793. variables=[
  794. ("mactool_cmd", "copy-bundle-resource"),
  795. ("env", env),
  796. ("binary", isBinary),
  797. ],
  798. )
  799. bundle_depends.append(output)
  800. else:
  801. xcassets.append(res)
  802. return xcassets
  803. def WriteMacXCassets(self, xcassets, bundle_depends):
  804. """Writes ninja edges for 'mac_bundle_resources' .xcassets files.
  805. This add an invocation of 'actool' via the 'mac_tool.py' helper script.
  806. It assumes that the assets catalogs define at least one imageset and
  807. thus an Assets.car file will be generated in the application resources
  808. directory. If this is not the case, then the build will probably be done
  809. at each invocation of ninja."""
  810. if not xcassets:
  811. return
  812. extra_arguments = {}
  813. settings_to_arg = {
  814. "XCASSETS_APP_ICON": "app-icon",
  815. "XCASSETS_LAUNCH_IMAGE": "launch-image",
  816. }
  817. settings = self.xcode_settings.xcode_settings[self.config_name]
  818. for settings_key, arg_name in settings_to_arg.items():
  819. value = settings.get(settings_key)
  820. if value:
  821. extra_arguments[arg_name] = value
  822. partial_info_plist = None
  823. if extra_arguments:
  824. partial_info_plist = self.GypPathToUniqueOutput(
  825. "assetcatalog_generated_info.plist"
  826. )
  827. extra_arguments["output-partial-info-plist"] = partial_info_plist
  828. outputs = []
  829. outputs.append(
  830. os.path.join(self.xcode_settings.GetBundleResourceFolder(), "Assets.car")
  831. )
  832. if partial_info_plist:
  833. outputs.append(partial_info_plist)
  834. keys = QuoteShellArgument(json.dumps(extra_arguments), self.flavor)
  835. extra_env = self.xcode_settings.GetPerTargetSettings()
  836. env = self.GetSortedXcodeEnv(additional_settings=extra_env)
  837. env = self.ComputeExportEnvString(env)
  838. bundle_depends.extend(
  839. self.ninja.build(
  840. outputs,
  841. "compile_xcassets",
  842. xcassets,
  843. variables=[("env", env), ("keys", keys)],
  844. )
  845. )
  846. return partial_info_plist
  847. def WriteMacInfoPlist(self, partial_info_plist, bundle_depends):
  848. """Write build rules for bundle Info.plist files."""
  849. info_plist, out, defines, extra_env = gyp.xcode_emulation.GetMacInfoPlist(
  850. generator_default_variables["PRODUCT_DIR"],
  851. self.xcode_settings,
  852. self.GypPathToNinja,
  853. )
  854. if not info_plist:
  855. return
  856. out = self.ExpandSpecial(out)
  857. if defines:
  858. # Create an intermediate file to store preprocessed results.
  859. intermediate_plist = self.GypPathToUniqueOutput(
  860. os.path.basename(info_plist)
  861. )
  862. defines = " ".join([Define(d, self.flavor) for d in defines])
  863. info_plist = self.ninja.build(
  864. intermediate_plist,
  865. "preprocess_infoplist",
  866. info_plist,
  867. variables=[("defines", defines)],
  868. )
  869. env = self.GetSortedXcodeEnv(additional_settings=extra_env)
  870. env = self.ComputeExportEnvString(env)
  871. if partial_info_plist:
  872. intermediate_plist = self.GypPathToUniqueOutput("merged_info.plist")
  873. info_plist = self.ninja.build(
  874. intermediate_plist, "merge_infoplist", [partial_info_plist, info_plist]
  875. )
  876. keys = self.xcode_settings.GetExtraPlistItems(self.config_name)
  877. keys = QuoteShellArgument(json.dumps(keys), self.flavor)
  878. isBinary = self.xcode_settings.IsBinaryOutputFormat(self.config_name)
  879. self.ninja.build(
  880. out,
  881. "copy_infoplist",
  882. info_plist,
  883. variables=[("env", env), ("keys", keys), ("binary", isBinary)],
  884. )
  885. bundle_depends.append(out)
  886. def WriteSources(
  887. self,
  888. ninja_file,
  889. config_name,
  890. config,
  891. sources,
  892. predepends,
  893. precompiled_header,
  894. spec,
  895. ):
  896. """Write build rules to compile all of |sources|."""
  897. if self.toolset == "host":
  898. self.ninja.variable("ar", "$ar_host")
  899. self.ninja.variable("cc", "$cc_host")
  900. self.ninja.variable("cxx", "$cxx_host")
  901. self.ninja.variable("ld", "$ld_host")
  902. self.ninja.variable("ldxx", "$ldxx_host")
  903. self.ninja.variable("nm", "$nm_host")
  904. self.ninja.variable("readelf", "$readelf_host")
  905. if self.flavor != "mac" or len(self.archs) == 1:
  906. return self.WriteSourcesForArch(
  907. self.ninja,
  908. config_name,
  909. config,
  910. sources,
  911. predepends,
  912. precompiled_header,
  913. spec,
  914. )
  915. else:
  916. return {
  917. arch: self.WriteSourcesForArch(
  918. self.arch_subninjas[arch],
  919. config_name,
  920. config,
  921. sources,
  922. predepends,
  923. precompiled_header,
  924. spec,
  925. arch=arch,
  926. )
  927. for arch in self.archs
  928. }
  929. def WriteSourcesForArch(
  930. self,
  931. ninja_file,
  932. config_name,
  933. config,
  934. sources,
  935. predepends,
  936. precompiled_header,
  937. spec,
  938. arch=None,
  939. ):
  940. """Write build rules to compile all of |sources|."""
  941. extra_defines = []
  942. if self.flavor == "mac":
  943. cflags = self.xcode_settings.GetCflags(config_name, arch=arch)
  944. cflags_c = self.xcode_settings.GetCflagsC(config_name)
  945. cflags_cc = self.xcode_settings.GetCflagsCC(config_name)
  946. cflags_objc = ["$cflags_c"] + self.xcode_settings.GetCflagsObjC(config_name)
  947. cflags_objcc = ["$cflags_cc"] + self.xcode_settings.GetCflagsObjCC(
  948. config_name
  949. )
  950. elif self.flavor == "win":
  951. asmflags = self.msvs_settings.GetAsmflags(config_name)
  952. cflags = self.msvs_settings.GetCflags(config_name)
  953. cflags_c = self.msvs_settings.GetCflagsC(config_name)
  954. cflags_cc = self.msvs_settings.GetCflagsCC(config_name)
  955. extra_defines = self.msvs_settings.GetComputedDefines(config_name)
  956. # See comment at cc_command for why there's two .pdb files.
  957. pdbpath_c = pdbpath_cc = self.msvs_settings.GetCompilerPdbName(
  958. config_name, self.ExpandSpecial
  959. )
  960. if not pdbpath_c:
  961. obj = "obj"
  962. if self.toolset != "target":
  963. obj += "." + self.toolset
  964. pdbpath = os.path.normpath(os.path.join(obj, self.base_dir, self.name))
  965. pdbpath_c = pdbpath + ".c.pdb"
  966. pdbpath_cc = pdbpath + ".cc.pdb"
  967. self.WriteVariableList(ninja_file, "pdbname_c", [pdbpath_c])
  968. self.WriteVariableList(ninja_file, "pdbname_cc", [pdbpath_cc])
  969. self.WriteVariableList(ninja_file, "pchprefix", [self.name])
  970. else:
  971. cflags = config.get("cflags", [])
  972. cflags_c = config.get("cflags_c", [])
  973. cflags_cc = config.get("cflags_cc", [])
  974. # Respect environment variables related to build, but target-specific
  975. # flags can still override them.
  976. if self.toolset == "target":
  977. cflags_c = (
  978. os.environ.get("CPPFLAGS", "").split()
  979. + os.environ.get("CFLAGS", "").split()
  980. + cflags_c
  981. )
  982. cflags_cc = (
  983. os.environ.get("CPPFLAGS", "").split()
  984. + os.environ.get("CXXFLAGS", "").split()
  985. + cflags_cc
  986. )
  987. elif self.toolset == "host":
  988. cflags_c = (
  989. os.environ.get("CPPFLAGS_host", "").split()
  990. + os.environ.get("CFLAGS_host", "").split()
  991. + cflags_c
  992. )
  993. cflags_cc = (
  994. os.environ.get("CPPFLAGS_host", "").split()
  995. + os.environ.get("CXXFLAGS_host", "").split()
  996. + cflags_cc
  997. )
  998. defines = config.get("defines", []) + extra_defines
  999. self.WriteVariableList(
  1000. ninja_file, "defines", [Define(d, self.flavor) for d in defines]
  1001. )
  1002. if self.flavor == "win":
  1003. self.WriteVariableList(
  1004. ninja_file, "asmflags", map(self.ExpandSpecial, asmflags)
  1005. )
  1006. self.WriteVariableList(
  1007. ninja_file,
  1008. "rcflags",
  1009. [
  1010. QuoteShellArgument(self.ExpandSpecial(f), self.flavor)
  1011. for f in self.msvs_settings.GetRcflags(
  1012. config_name, self.GypPathToNinja
  1013. )
  1014. ],
  1015. )
  1016. include_dirs = config.get("include_dirs", [])
  1017. env = self.GetToolchainEnv()
  1018. if self.flavor == "win":
  1019. include_dirs = self.msvs_settings.AdjustIncludeDirs(
  1020. include_dirs, config_name
  1021. )
  1022. self.WriteVariableList(
  1023. ninja_file,
  1024. "includes",
  1025. [
  1026. QuoteShellArgument("-I" + self.GypPathToNinja(i, env), self.flavor)
  1027. for i in include_dirs
  1028. ],
  1029. )
  1030. if self.flavor == "win":
  1031. midl_include_dirs = config.get("midl_include_dirs", [])
  1032. midl_include_dirs = self.msvs_settings.AdjustMidlIncludeDirs(
  1033. midl_include_dirs, config_name
  1034. )
  1035. self.WriteVariableList(
  1036. ninja_file,
  1037. "midl_includes",
  1038. [
  1039. QuoteShellArgument("-I" + self.GypPathToNinja(i, env), self.flavor)
  1040. for i in midl_include_dirs
  1041. ],
  1042. )
  1043. pch_commands = precompiled_header.GetPchBuildCommands(arch)
  1044. if self.flavor == "mac":
  1045. # Most targets use no precompiled headers, so only write these if needed.
  1046. for ext, var in [
  1047. ("c", "cflags_pch_c"),
  1048. ("cc", "cflags_pch_cc"),
  1049. ("m", "cflags_pch_objc"),
  1050. ("mm", "cflags_pch_objcc"),
  1051. ]:
  1052. include = precompiled_header.GetInclude(ext, arch)
  1053. if include:
  1054. ninja_file.variable(var, include)
  1055. arflags = config.get("arflags", [])
  1056. self.WriteVariableList(ninja_file, "cflags", map(self.ExpandSpecial, cflags))
  1057. self.WriteVariableList(
  1058. ninja_file, "cflags_c", map(self.ExpandSpecial, cflags_c)
  1059. )
  1060. self.WriteVariableList(
  1061. ninja_file, "cflags_cc", map(self.ExpandSpecial, cflags_cc)
  1062. )
  1063. if self.flavor == "mac":
  1064. self.WriteVariableList(
  1065. ninja_file, "cflags_objc", map(self.ExpandSpecial, cflags_objc)
  1066. )
  1067. self.WriteVariableList(
  1068. ninja_file, "cflags_objcc", map(self.ExpandSpecial, cflags_objcc)
  1069. )
  1070. self.WriteVariableList(ninja_file, "arflags", map(self.ExpandSpecial, arflags))
  1071. ninja_file.newline()
  1072. outputs = []
  1073. has_rc_source = False
  1074. for source in sources:
  1075. filename, ext = os.path.splitext(source)
  1076. ext = ext[1:]
  1077. obj_ext = self.obj_ext
  1078. if ext in ("cc", "cpp", "cxx"):
  1079. command = "cxx"
  1080. self.target.uses_cpp = True
  1081. elif ext == "c" or (ext == "S" and self.flavor != "win"):
  1082. command = "cc"
  1083. elif ext == "s" and self.flavor != "win": # Doesn't generate .o.d files.
  1084. command = "cc_s"
  1085. elif (
  1086. self.flavor == "win"
  1087. and ext in ("asm", "S")
  1088. and not self.msvs_settings.HasExplicitAsmRules(spec)
  1089. ):
  1090. command = "asm"
  1091. # Add the _asm suffix as msvs is capable of handling .cc and
  1092. # .asm files of the same name without collision.
  1093. obj_ext = "_asm.obj"
  1094. elif self.flavor == "mac" and ext == "m":
  1095. command = "objc"
  1096. elif self.flavor == "mac" and ext == "mm":
  1097. command = "objcxx"
  1098. self.target.uses_cpp = True
  1099. elif self.flavor == "win" and ext == "rc":
  1100. command = "rc"
  1101. obj_ext = ".res"
  1102. has_rc_source = True
  1103. else:
  1104. # Ignore unhandled extensions.
  1105. continue
  1106. input = self.GypPathToNinja(source)
  1107. output = self.GypPathToUniqueOutput(filename + obj_ext)
  1108. if arch is not None:
  1109. output = AddArch(output, arch)
  1110. implicit = precompiled_header.GetObjDependencies([input], [output], arch)
  1111. variables = []
  1112. if self.flavor == "win":
  1113. variables, output, implicit = precompiled_header.GetFlagsModifications(
  1114. input,
  1115. output,
  1116. implicit,
  1117. command,
  1118. cflags_c,
  1119. cflags_cc,
  1120. self.ExpandSpecial,
  1121. )
  1122. ninja_file.build(
  1123. output,
  1124. command,
  1125. input,
  1126. implicit=[gch for _, _, gch in implicit],
  1127. order_only=predepends,
  1128. variables=variables,
  1129. )
  1130. outputs.append(output)
  1131. if has_rc_source:
  1132. resource_include_dirs = config.get("resource_include_dirs", include_dirs)
  1133. self.WriteVariableList(
  1134. ninja_file,
  1135. "resource_includes",
  1136. [
  1137. QuoteShellArgument("-I" + self.GypPathToNinja(i, env), self.flavor)
  1138. for i in resource_include_dirs
  1139. ],
  1140. )
  1141. self.WritePchTargets(ninja_file, pch_commands)
  1142. ninja_file.newline()
  1143. return outputs
  1144. def WritePchTargets(self, ninja_file, pch_commands):
  1145. """Writes ninja rules to compile prefix headers."""
  1146. if not pch_commands:
  1147. return
  1148. for gch, lang_flag, lang, input in pch_commands:
  1149. var_name = {
  1150. "c": "cflags_pch_c",
  1151. "cc": "cflags_pch_cc",
  1152. "m": "cflags_pch_objc",
  1153. "mm": "cflags_pch_objcc",
  1154. }[lang]
  1155. map = {
  1156. "c": "cc",
  1157. "cc": "cxx",
  1158. "m": "objc",
  1159. "mm": "objcxx",
  1160. }
  1161. cmd = map.get(lang)
  1162. ninja_file.build(gch, cmd, input, variables=[(var_name, lang_flag)])
  1163. def WriteLink(self, spec, config_name, config, link_deps, compile_deps):
  1164. """Write out a link step. Fills out target.binary. """
  1165. if self.flavor != "mac" or len(self.archs) == 1:
  1166. return self.WriteLinkForArch(
  1167. self.ninja, spec, config_name, config, link_deps, compile_deps
  1168. )
  1169. else:
  1170. output = self.ComputeOutput(spec)
  1171. inputs = [
  1172. self.WriteLinkForArch(
  1173. self.arch_subninjas[arch],
  1174. spec,
  1175. config_name,
  1176. config,
  1177. link_deps[arch],
  1178. compile_deps,
  1179. arch=arch,
  1180. )
  1181. for arch in self.archs
  1182. ]
  1183. extra_bindings = []
  1184. build_output = output
  1185. if not self.is_mac_bundle:
  1186. self.AppendPostbuildVariable(extra_bindings, spec, output, output)
  1187. # TODO(yyanagisawa): more work needed to fix:
  1188. # https://code.google.com/p/gyp/issues/detail?id=411
  1189. if (
  1190. spec["type"] in ("shared_library", "loadable_module")
  1191. and not self.is_mac_bundle
  1192. ):
  1193. extra_bindings.append(("lib", output))
  1194. self.ninja.build(
  1195. [output, output + ".TOC"],
  1196. "solipo",
  1197. inputs,
  1198. variables=extra_bindings,
  1199. )
  1200. else:
  1201. self.ninja.build(build_output, "lipo", inputs, variables=extra_bindings)
  1202. return output
  1203. def WriteLinkForArch(
  1204. self, ninja_file, spec, config_name, config, link_deps, compile_deps, arch=None
  1205. ):
  1206. """Write out a link step. Fills out target.binary. """
  1207. command = {
  1208. "executable": "link",
  1209. "loadable_module": "solink_module",
  1210. "shared_library": "solink",
  1211. }[spec["type"]]
  1212. command_suffix = ""
  1213. implicit_deps = set()
  1214. solibs = set()
  1215. order_deps = set()
  1216. if compile_deps:
  1217. # Normally, the compiles of the target already depend on compile_deps,
  1218. # but a shared_library target might have no sources and only link together
  1219. # a few static_library deps, so the link step also needs to depend
  1220. # on compile_deps to make sure actions in the shared_library target
  1221. # get run before the link.
  1222. order_deps.add(compile_deps)
  1223. if "dependencies" in spec:
  1224. # Two kinds of dependencies:
  1225. # - Linkable dependencies (like a .a or a .so): add them to the link line.
  1226. # - Non-linkable dependencies (like a rule that generates a file
  1227. # and writes a stamp file): add them to implicit_deps
  1228. extra_link_deps = set()
  1229. for dep in spec["dependencies"]:
  1230. target = self.target_outputs.get(dep)
  1231. if not target:
  1232. continue
  1233. linkable = target.Linkable()
  1234. if linkable:
  1235. new_deps = []
  1236. if (
  1237. self.flavor == "win"
  1238. and target.component_objs
  1239. and self.msvs_settings.IsUseLibraryDependencyInputs(config_name)
  1240. ):
  1241. new_deps = target.component_objs
  1242. if target.compile_deps:
  1243. order_deps.add(target.compile_deps)
  1244. elif self.flavor == "win" and target.import_lib:
  1245. new_deps = [target.import_lib]
  1246. elif target.UsesToc(self.flavor):
  1247. solibs.add(target.binary)
  1248. implicit_deps.add(target.binary + ".TOC")
  1249. else:
  1250. new_deps = [target.binary]
  1251. for new_dep in new_deps:
  1252. if new_dep not in extra_link_deps:
  1253. extra_link_deps.add(new_dep)
  1254. link_deps.append(new_dep)
  1255. final_output = target.FinalOutput()
  1256. if not linkable or final_output != target.binary:
  1257. implicit_deps.add(final_output)
  1258. extra_bindings = []
  1259. if self.target.uses_cpp and self.flavor != "win":
  1260. extra_bindings.append(("ld", "$ldxx"))
  1261. output = self.ComputeOutput(spec, arch)
  1262. if arch is None and not self.is_mac_bundle:
  1263. self.AppendPostbuildVariable(extra_bindings, spec, output, output)
  1264. is_executable = spec["type"] == "executable"
  1265. # The ldflags config key is not used on mac or win. On those platforms
  1266. # linker flags are set via xcode_settings and msvs_settings, respectively.
  1267. if self.toolset == "target":
  1268. env_ldflags = os.environ.get("LDFLAGS", "").split()
  1269. elif self.toolset == "host":
  1270. env_ldflags = os.environ.get("LDFLAGS_host", "").split()
  1271. if self.flavor == "mac":
  1272. ldflags = self.xcode_settings.GetLdflags(
  1273. config_name,
  1274. self.ExpandSpecial(generator_default_variables["PRODUCT_DIR"]),
  1275. self.GypPathToNinja,
  1276. arch,
  1277. )
  1278. ldflags = env_ldflags + ldflags
  1279. elif self.flavor == "win":
  1280. manifest_base_name = self.GypPathToUniqueOutput(
  1281. self.ComputeOutputFileName(spec)
  1282. )
  1283. (
  1284. ldflags,
  1285. intermediate_manifest,
  1286. manifest_files,
  1287. ) = self.msvs_settings.GetLdflags(
  1288. config_name,
  1289. self.GypPathToNinja,
  1290. self.ExpandSpecial,
  1291. manifest_base_name,
  1292. output,
  1293. is_executable,
  1294. self.toplevel_build,
  1295. )
  1296. ldflags = env_ldflags + ldflags
  1297. self.WriteVariableList(ninja_file, "manifests", manifest_files)
  1298. implicit_deps = implicit_deps.union(manifest_files)
  1299. if intermediate_manifest:
  1300. self.WriteVariableList(
  1301. ninja_file, "intermediatemanifest", [intermediate_manifest]
  1302. )
  1303. command_suffix = _GetWinLinkRuleNameSuffix(
  1304. self.msvs_settings.IsEmbedManifest(config_name)
  1305. )
  1306. def_file = self.msvs_settings.GetDefFile(self.GypPathToNinja)
  1307. if def_file:
  1308. implicit_deps.add(def_file)
  1309. else:
  1310. # Respect environment variables related to build, but target-specific
  1311. # flags can still override them.
  1312. ldflags = env_ldflags + config.get("ldflags", [])
  1313. if is_executable and len(solibs):
  1314. rpath = "lib/"
  1315. if self.toolset != "target":
  1316. rpath += self.toolset
  1317. ldflags.append(r"-Wl,-rpath=\$$ORIGIN/%s" % rpath)
  1318. else:
  1319. ldflags.append("-Wl,-rpath=%s" % self.target_rpath)
  1320. ldflags.append("-Wl,-rpath-link=%s" % rpath)
  1321. self.WriteVariableList(ninja_file, "ldflags", map(self.ExpandSpecial, ldflags))
  1322. library_dirs = config.get("library_dirs", [])
  1323. if self.flavor == "win":
  1324. library_dirs = [
  1325. self.msvs_settings.ConvertVSMacros(library_dir, config_name)
  1326. for library_dir in library_dirs
  1327. ]
  1328. library_dirs = [
  1329. "/LIBPATH:"
  1330. + QuoteShellArgument(self.GypPathToNinja(library_dir), self.flavor)
  1331. for library_dir in library_dirs
  1332. ]
  1333. else:
  1334. library_dirs = [
  1335. QuoteShellArgument("-L" + self.GypPathToNinja(library_dir), self.flavor)
  1336. for library_dir in library_dirs
  1337. ]
  1338. libraries = gyp.common.uniquer(
  1339. map(self.ExpandSpecial, spec.get("libraries", []))
  1340. )
  1341. if self.flavor == "mac":
  1342. libraries = self.xcode_settings.AdjustLibraries(libraries, config_name)
  1343. elif self.flavor == "win":
  1344. libraries = self.msvs_settings.AdjustLibraries(libraries)
  1345. self.WriteVariableList(ninja_file, "libs", library_dirs + libraries)
  1346. linked_binary = output
  1347. if command in ("solink", "solink_module"):
  1348. extra_bindings.append(("soname", os.path.split(output)[1]))
  1349. extra_bindings.append(("lib", gyp.common.EncodePOSIXShellArgument(output)))
  1350. if self.flavor != "win":
  1351. link_file_list = output
  1352. if self.is_mac_bundle:
  1353. # 'Dependency Framework.framework/Versions/A/Dependency Framework'
  1354. # -> 'Dependency Framework.framework.rsp'
  1355. link_file_list = self.xcode_settings.GetWrapperName()
  1356. if arch:
  1357. link_file_list += "." + arch
  1358. link_file_list += ".rsp"
  1359. # If an rspfile contains spaces, ninja surrounds the filename with
  1360. # quotes around it and then passes it to open(), creating a file with
  1361. # quotes in its name (and when looking for the rsp file, the name
  1362. # makes it through bash which strips the quotes) :-/
  1363. link_file_list = link_file_list.replace(" ", "_")
  1364. extra_bindings.append(
  1365. (
  1366. "link_file_list",
  1367. gyp.common.EncodePOSIXShellArgument(link_file_list),
  1368. )
  1369. )
  1370. if self.flavor == "win":
  1371. extra_bindings.append(("binary", output))
  1372. if (
  1373. "/NOENTRY" not in ldflags
  1374. and not self.msvs_settings.GetNoImportLibrary(config_name)
  1375. ):
  1376. self.target.import_lib = output + ".lib"
  1377. extra_bindings.append(
  1378. ("implibflag", "/IMPLIB:%s" % self.target.import_lib)
  1379. )
  1380. pdbname = self.msvs_settings.GetPDBName(
  1381. config_name, self.ExpandSpecial, output + ".pdb"
  1382. )
  1383. output = [output, self.target.import_lib]
  1384. if pdbname:
  1385. output.append(pdbname)
  1386. elif not self.is_mac_bundle:
  1387. output = [output, output + ".TOC"]
  1388. else:
  1389. command = command + "_notoc"
  1390. elif self.flavor == "win":
  1391. extra_bindings.append(("binary", output))
  1392. pdbname = self.msvs_settings.GetPDBName(
  1393. config_name, self.ExpandSpecial, output + ".pdb"
  1394. )
  1395. if pdbname:
  1396. output = [output, pdbname]
  1397. if len(solibs):
  1398. extra_bindings.append(
  1399. ("solibs", gyp.common.EncodePOSIXShellList(sorted(solibs)))
  1400. )
  1401. ninja_file.build(
  1402. output,
  1403. command + command_suffix,
  1404. link_deps,
  1405. implicit=sorted(implicit_deps),
  1406. order_only=list(order_deps),
  1407. variables=extra_bindings,
  1408. )
  1409. return linked_binary
  1410. def WriteTarget(self, spec, config_name, config, link_deps, compile_deps):
  1411. extra_link_deps = any(
  1412. self.target_outputs.get(dep).Linkable()
  1413. for dep in spec.get("dependencies", [])
  1414. if dep in self.target_outputs
  1415. )
  1416. if spec["type"] == "none" or (not link_deps and not extra_link_deps):
  1417. # TODO(evan): don't call this function for 'none' target types, as
  1418. # it doesn't do anything, and we fake out a 'binary' with a stamp file.
  1419. self.target.binary = compile_deps
  1420. self.target.type = "none"
  1421. elif spec["type"] == "static_library":
  1422. self.target.binary = self.ComputeOutput(spec)
  1423. if (
  1424. self.flavor not in ("ios", "mac", "netbsd", "openbsd", "win")
  1425. and not self.is_standalone_static_library
  1426. ):
  1427. self.ninja.build(
  1428. self.target.binary, "alink_thin", link_deps, order_only=compile_deps
  1429. )
  1430. else:
  1431. variables = []
  1432. if self.xcode_settings:
  1433. libtool_flags = self.xcode_settings.GetLibtoolflags(config_name)
  1434. if libtool_flags:
  1435. variables.append(("libtool_flags", libtool_flags))
  1436. if self.msvs_settings:
  1437. libflags = self.msvs_settings.GetLibFlags(
  1438. config_name, self.GypPathToNinja
  1439. )
  1440. variables.append(("libflags", libflags))
  1441. if self.flavor != "mac" or len(self.archs) == 1:
  1442. self.AppendPostbuildVariable(
  1443. variables, spec, self.target.binary, self.target.binary
  1444. )
  1445. self.ninja.build(
  1446. self.target.binary,
  1447. "alink",
  1448. link_deps,
  1449. order_only=compile_deps,
  1450. variables=variables,
  1451. )
  1452. else:
  1453. inputs = []
  1454. for arch in self.archs:
  1455. output = self.ComputeOutput(spec, arch)
  1456. self.arch_subninjas[arch].build(
  1457. output,
  1458. "alink",
  1459. link_deps[arch],
  1460. order_only=compile_deps,
  1461. variables=variables,
  1462. )
  1463. inputs.append(output)
  1464. # TODO: It's not clear if
  1465. # libtool_flags should be passed to the alink
  1466. # call that combines single-arch .a files into a fat .a file.
  1467. self.AppendPostbuildVariable(
  1468. variables, spec, self.target.binary, self.target.binary
  1469. )
  1470. self.ninja.build(
  1471. self.target.binary,
  1472. "alink",
  1473. inputs,
  1474. # FIXME: test proving order_only=compile_deps isn't
  1475. # needed.
  1476. variables=variables,
  1477. )
  1478. else:
  1479. self.target.binary = self.WriteLink(
  1480. spec, config_name, config, link_deps, compile_deps
  1481. )
  1482. return self.target.binary
  1483. def WriteMacBundle(self, spec, mac_bundle_depends, is_empty):
  1484. assert self.is_mac_bundle
  1485. package_framework = spec["type"] in ("shared_library", "loadable_module")
  1486. output = self.ComputeMacBundleOutput()
  1487. if is_empty:
  1488. output += ".stamp"
  1489. variables = []
  1490. self.AppendPostbuildVariable(
  1491. variables,
  1492. spec,
  1493. output,
  1494. self.target.binary,
  1495. is_command_start=not package_framework,
  1496. )
  1497. if package_framework and not is_empty:
  1498. if spec["type"] == "shared_library" and self.xcode_settings.isIOS:
  1499. self.ninja.build(
  1500. output,
  1501. "package_ios_framework",
  1502. mac_bundle_depends,
  1503. variables=variables,
  1504. )
  1505. else:
  1506. variables.append(("version", self.xcode_settings.GetFrameworkVersion()))
  1507. self.ninja.build(
  1508. output, "package_framework", mac_bundle_depends, variables=variables
  1509. )
  1510. else:
  1511. self.ninja.build(output, "stamp", mac_bundle_depends, variables=variables)
  1512. self.target.bundle = output
  1513. return output
  1514. def GetToolchainEnv(self, additional_settings=None):
  1515. """Returns the variables toolchain would set for build steps."""
  1516. env = self.GetSortedXcodeEnv(additional_settings=additional_settings)
  1517. if self.flavor == "win":
  1518. env = self.GetMsvsToolchainEnv(additional_settings=additional_settings)
  1519. return env
  1520. def GetMsvsToolchainEnv(self, additional_settings=None):
  1521. """Returns the variables Visual Studio would set for build steps."""
  1522. return self.msvs_settings.GetVSMacroEnv(
  1523. "$!PRODUCT_DIR", config=self.config_name
  1524. )
  1525. def GetSortedXcodeEnv(self, additional_settings=None):
  1526. """Returns the variables Xcode would set for build steps."""
  1527. assert self.abs_build_dir
  1528. abs_build_dir = self.abs_build_dir
  1529. return gyp.xcode_emulation.GetSortedXcodeEnv(
  1530. self.xcode_settings,
  1531. abs_build_dir,
  1532. os.path.join(abs_build_dir, self.build_to_base),
  1533. self.config_name,
  1534. additional_settings,
  1535. )
  1536. def GetSortedXcodePostbuildEnv(self):
  1537. """Returns the variables Xcode would set for postbuild steps."""
  1538. postbuild_settings = {}
  1539. # CHROMIUM_STRIP_SAVE_FILE is a chromium-specific hack.
  1540. # TODO(thakis): It would be nice to have some general mechanism instead.
  1541. strip_save_file = self.xcode_settings.GetPerTargetSetting(
  1542. "CHROMIUM_STRIP_SAVE_FILE"
  1543. )
  1544. if strip_save_file:
  1545. postbuild_settings["CHROMIUM_STRIP_SAVE_FILE"] = strip_save_file
  1546. return self.GetSortedXcodeEnv(additional_settings=postbuild_settings)
  1547. def AppendPostbuildVariable(
  1548. self, variables, spec, output, binary, is_command_start=False
  1549. ):
  1550. """Adds a 'postbuild' variable if there is a postbuild for |output|."""
  1551. postbuild = self.GetPostbuildCommand(spec, output, binary, is_command_start)
  1552. if postbuild:
  1553. variables.append(("postbuilds", postbuild))
  1554. def GetPostbuildCommand(self, spec, output, output_binary, is_command_start):
  1555. """Returns a shell command that runs all the postbuilds, and removes
  1556. |output| if any of them fails. If |is_command_start| is False, then the
  1557. returned string will start with ' && '."""
  1558. if not self.xcode_settings or spec["type"] == "none" or not output:
  1559. return ""
  1560. output = QuoteShellArgument(output, self.flavor)
  1561. postbuilds = gyp.xcode_emulation.GetSpecPostbuildCommands(spec, quiet=True)
  1562. if output_binary is not None:
  1563. postbuilds = self.xcode_settings.AddImplicitPostbuilds(
  1564. self.config_name,
  1565. os.path.normpath(os.path.join(self.base_to_build, output)),
  1566. QuoteShellArgument(
  1567. os.path.normpath(os.path.join(self.base_to_build, output_binary)),
  1568. self.flavor,
  1569. ),
  1570. postbuilds,
  1571. quiet=True,
  1572. )
  1573. if not postbuilds:
  1574. return ""
  1575. # Postbuilds expect to be run in the gyp file's directory, so insert an
  1576. # implicit postbuild to cd to there.
  1577. postbuilds.insert(
  1578. 0, gyp.common.EncodePOSIXShellList(["cd", self.build_to_base])
  1579. )
  1580. env = self.ComputeExportEnvString(self.GetSortedXcodePostbuildEnv())
  1581. # G will be non-null if any postbuild fails. Run all postbuilds in a
  1582. # subshell.
  1583. commands = (
  1584. env
  1585. + " ("
  1586. + " && ".join([ninja_syntax.escape(command) for command in postbuilds])
  1587. )
  1588. command_string = (
  1589. commands
  1590. + "); G=$$?; "
  1591. # Remove the final output if any postbuild failed.
  1592. "((exit $$G) || rm -rf %s) " % output
  1593. + "&& exit $$G)"
  1594. )
  1595. if is_command_start:
  1596. return "(" + command_string + " && "
  1597. else:
  1598. return "$ && (" + command_string
  1599. def ComputeExportEnvString(self, env):
  1600. """Given an environment, returns a string looking like
  1601. 'export FOO=foo; export BAR="${FOO} bar;'
  1602. that exports |env| to the shell."""
  1603. export_str = []
  1604. for k, v in env:
  1605. export_str.append(
  1606. "export %s=%s;"
  1607. % (k, ninja_syntax.escape(gyp.common.EncodePOSIXShellArgument(v)))
  1608. )
  1609. return " ".join(export_str)
  1610. def ComputeMacBundleOutput(self):
  1611. """Return the 'output' (full output path) to a bundle output directory."""
  1612. assert self.is_mac_bundle
  1613. path = generator_default_variables["PRODUCT_DIR"]
  1614. return self.ExpandSpecial(
  1615. os.path.join(path, self.xcode_settings.GetWrapperName())
  1616. )
  1617. def ComputeOutputFileName(self, spec, type=None):
  1618. """Compute the filename of the final output for the current target."""
  1619. if not type:
  1620. type = spec["type"]
  1621. default_variables = copy.copy(generator_default_variables)
  1622. CalculateVariables(default_variables, {"flavor": self.flavor})
  1623. # Compute filename prefix: the product prefix, or a default for
  1624. # the product type.
  1625. DEFAULT_PREFIX = {
  1626. "loadable_module": default_variables["SHARED_LIB_PREFIX"],
  1627. "shared_library": default_variables["SHARED_LIB_PREFIX"],
  1628. "static_library": default_variables["STATIC_LIB_PREFIX"],
  1629. "executable": default_variables["EXECUTABLE_PREFIX"],
  1630. }
  1631. prefix = spec.get("product_prefix", DEFAULT_PREFIX.get(type, ""))
  1632. # Compute filename extension: the product extension, or a default
  1633. # for the product type.
  1634. DEFAULT_EXTENSION = {
  1635. "loadable_module": default_variables["SHARED_LIB_SUFFIX"],
  1636. "shared_library": default_variables["SHARED_LIB_SUFFIX"],
  1637. "static_library": default_variables["STATIC_LIB_SUFFIX"],
  1638. "executable": default_variables["EXECUTABLE_SUFFIX"],
  1639. }
  1640. extension = spec.get("product_extension")
  1641. extension = "." + extension if extension else DEFAULT_EXTENSION.get(type, "")
  1642. if "product_name" in spec:
  1643. # If we were given an explicit name, use that.
  1644. target = spec["product_name"]
  1645. else:
  1646. # Otherwise, derive a name from the target name.
  1647. target = spec["target_name"]
  1648. if prefix == "lib":
  1649. # Snip out an extra 'lib' from libs if appropriate.
  1650. target = StripPrefix(target, "lib")
  1651. if type in (
  1652. "static_library",
  1653. "loadable_module",
  1654. "shared_library",
  1655. "executable",
  1656. ):
  1657. return f"{prefix}{target}{extension}"
  1658. elif type == "none":
  1659. return "%s.stamp" % target
  1660. else:
  1661. raise Exception("Unhandled output type %s" % type)
  1662. def ComputeOutput(self, spec, arch=None):
  1663. """Compute the path for the final output of the spec."""
  1664. type = spec["type"]
  1665. if self.flavor == "win":
  1666. override = self.msvs_settings.GetOutputName(
  1667. self.config_name, self.ExpandSpecial
  1668. )
  1669. if override:
  1670. return override
  1671. if (
  1672. arch is None
  1673. and self.flavor == "mac"
  1674. and type
  1675. in ("static_library", "executable", "shared_library", "loadable_module")
  1676. ):
  1677. filename = self.xcode_settings.GetExecutablePath()
  1678. else:
  1679. filename = self.ComputeOutputFileName(spec, type)
  1680. if arch is None and "product_dir" in spec:
  1681. path = os.path.join(spec["product_dir"], filename)
  1682. return self.ExpandSpecial(path)
  1683. # Some products go into the output root, libraries go into shared library
  1684. # dir, and everything else goes into the normal place.
  1685. type_in_output_root = ["executable", "loadable_module"]
  1686. if self.flavor == "mac" and self.toolset == "target":
  1687. type_in_output_root += ["shared_library", "static_library"]
  1688. elif self.flavor == "win" and self.toolset == "target":
  1689. type_in_output_root += ["shared_library"]
  1690. if arch is not None:
  1691. # Make sure partial executables don't end up in a bundle or the regular
  1692. # output directory.
  1693. archdir = "arch"
  1694. if self.toolset != "target":
  1695. archdir = os.path.join("arch", "%s" % self.toolset)
  1696. return os.path.join(archdir, AddArch(filename, arch))
  1697. elif type in type_in_output_root or self.is_standalone_static_library:
  1698. return filename
  1699. elif type == "shared_library":
  1700. libdir = "lib"
  1701. if self.toolset != "target":
  1702. libdir = os.path.join("lib", "%s" % self.toolset)
  1703. return os.path.join(libdir, filename)
  1704. else:
  1705. return self.GypPathToUniqueOutput(filename, qualified=False)
  1706. def WriteVariableList(self, ninja_file, var, values):
  1707. assert not isinstance(values, str)
  1708. if values is None:
  1709. values = []
  1710. ninja_file.variable(var, " ".join(values))
  1711. def WriteNewNinjaRule(
  1712. self, name, args, description, win_shell_flags, env, pool, depfile=None
  1713. ):
  1714. """Write out a new ninja "rule" statement for a given command.
  1715. Returns the name of the new rule, and a copy of |args| with variables
  1716. expanded."""
  1717. if self.flavor == "win":
  1718. args = [
  1719. self.msvs_settings.ConvertVSMacros(
  1720. arg, self.base_to_build, config=self.config_name
  1721. )
  1722. for arg in args
  1723. ]
  1724. description = self.msvs_settings.ConvertVSMacros(
  1725. description, config=self.config_name
  1726. )
  1727. elif self.flavor == "mac":
  1728. # |env| is an empty list on non-mac.
  1729. args = [gyp.xcode_emulation.ExpandEnvVars(arg, env) for arg in args]
  1730. description = gyp.xcode_emulation.ExpandEnvVars(description, env)
  1731. # TODO: we shouldn't need to qualify names; we do it because
  1732. # currently the ninja rule namespace is global, but it really
  1733. # should be scoped to the subninja.
  1734. rule_name = self.name
  1735. if self.toolset == "target":
  1736. rule_name += "." + self.toolset
  1737. rule_name += "." + name
  1738. rule_name = re.sub("[^a-zA-Z0-9_]", "_", rule_name)
  1739. # Remove variable references, but not if they refer to the magic rule
  1740. # variables. This is not quite right, as it also protects these for
  1741. # actions, not just for rules where they are valid. Good enough.
  1742. protect = ["${root}", "${dirname}", "${source}", "${ext}", "${name}"]
  1743. protect = "(?!" + "|".join(map(re.escape, protect)) + ")"
  1744. description = re.sub(protect + r"\$", "_", description)
  1745. # gyp dictates that commands are run from the base directory.
  1746. # cd into the directory before running, and adjust paths in
  1747. # the arguments to point to the proper locations.
  1748. rspfile = None
  1749. rspfile_content = None
  1750. args = [self.ExpandSpecial(arg, self.base_to_build) for arg in args]
  1751. if self.flavor == "win":
  1752. rspfile = rule_name + ".$unique_name.rsp"
  1753. # The cygwin case handles this inside the bash sub-shell.
  1754. run_in = "" if win_shell_flags.cygwin else " " + self.build_to_base
  1755. if win_shell_flags.cygwin:
  1756. rspfile_content = self.msvs_settings.BuildCygwinBashCommandLine(
  1757. args, self.build_to_base
  1758. )
  1759. else:
  1760. rspfile_content = gyp.msvs_emulation.EncodeRspFileList(
  1761. args, win_shell_flags.quote)
  1762. command = (
  1763. "%s gyp-win-tool action-wrapper $arch " % sys.executable
  1764. + rspfile
  1765. + run_in
  1766. )
  1767. else:
  1768. env = self.ComputeExportEnvString(env)
  1769. command = gyp.common.EncodePOSIXShellList(args)
  1770. command = "cd %s; " % self.build_to_base + env + command
  1771. # GYP rules/actions express being no-ops by not touching their outputs.
  1772. # Avoid executing downstream dependencies in this case by specifying
  1773. # restat=1 to ninja.
  1774. self.ninja.rule(
  1775. rule_name,
  1776. command,
  1777. description,
  1778. depfile=depfile,
  1779. restat=True,
  1780. pool=pool,
  1781. rspfile=rspfile,
  1782. rspfile_content=rspfile_content,
  1783. )
  1784. self.ninja.newline()
  1785. return rule_name, args
  1786. def CalculateVariables(default_variables, params):
  1787. """Calculate additional variables for use in the build (called by gyp)."""
  1788. global generator_additional_non_configuration_keys
  1789. global generator_additional_path_sections
  1790. flavor = gyp.common.GetFlavor(params)
  1791. if flavor == "mac":
  1792. default_variables.setdefault("OS", "mac")
  1793. default_variables.setdefault("SHARED_LIB_SUFFIX", ".dylib")
  1794. default_variables.setdefault(
  1795. "SHARED_LIB_DIR", generator_default_variables["PRODUCT_DIR"]
  1796. )
  1797. default_variables.setdefault(
  1798. "LIB_DIR", generator_default_variables["PRODUCT_DIR"]
  1799. )
  1800. # Copy additional generator configuration data from Xcode, which is shared
  1801. # by the Mac Ninja generator.
  1802. import gyp.generator.xcode as xcode_generator
  1803. generator_additional_non_configuration_keys = getattr(
  1804. xcode_generator, "generator_additional_non_configuration_keys", []
  1805. )
  1806. generator_additional_path_sections = getattr(
  1807. xcode_generator, "generator_additional_path_sections", []
  1808. )
  1809. global generator_extra_sources_for_rules
  1810. generator_extra_sources_for_rules = getattr(
  1811. xcode_generator, "generator_extra_sources_for_rules", []
  1812. )
  1813. elif flavor == "win":
  1814. exts = gyp.MSVSUtil.TARGET_TYPE_EXT
  1815. default_variables.setdefault("OS", "win")
  1816. default_variables["EXECUTABLE_SUFFIX"] = "." + exts["executable"]
  1817. default_variables["STATIC_LIB_PREFIX"] = ""
  1818. default_variables["STATIC_LIB_SUFFIX"] = "." + exts["static_library"]
  1819. default_variables["SHARED_LIB_PREFIX"] = ""
  1820. default_variables["SHARED_LIB_SUFFIX"] = "." + exts["shared_library"]
  1821. # Copy additional generator configuration data from VS, which is shared
  1822. # by the Windows Ninja generator.
  1823. import gyp.generator.msvs as msvs_generator
  1824. generator_additional_non_configuration_keys = getattr(
  1825. msvs_generator, "generator_additional_non_configuration_keys", []
  1826. )
  1827. generator_additional_path_sections = getattr(
  1828. msvs_generator, "generator_additional_path_sections", []
  1829. )
  1830. gyp.msvs_emulation.CalculateCommonVariables(default_variables, params)
  1831. else:
  1832. operating_system = flavor
  1833. if flavor == "android":
  1834. operating_system = "linux" # Keep this legacy behavior for now.
  1835. default_variables.setdefault("OS", operating_system)
  1836. default_variables.setdefault("SHARED_LIB_SUFFIX", ".so")
  1837. default_variables.setdefault(
  1838. "SHARED_LIB_DIR", os.path.join("$!PRODUCT_DIR", "lib")
  1839. )
  1840. default_variables.setdefault("LIB_DIR", os.path.join("$!PRODUCT_DIR", "obj"))
  1841. def ComputeOutputDir(params):
  1842. """Returns the path from the toplevel_dir to the build output directory."""
  1843. # generator_dir: relative path from pwd to where make puts build files.
  1844. # Makes migrating from make to ninja easier, ninja doesn't put anything here.
  1845. generator_dir = os.path.relpath(params["options"].generator_output or ".")
  1846. # output_dir: relative path from generator_dir to the build directory.
  1847. output_dir = params.get("generator_flags", {}).get("output_dir", "out")
  1848. # Relative path from source root to our output files. e.g. "out"
  1849. return os.path.normpath(os.path.join(generator_dir, output_dir))
  1850. def CalculateGeneratorInputInfo(params):
  1851. """Called by __init__ to initialize generator values based on params."""
  1852. # E.g. "out/gypfiles"
  1853. toplevel = params["options"].toplevel_dir
  1854. qualified_out_dir = os.path.normpath(
  1855. os.path.join(toplevel, ComputeOutputDir(params), "gypfiles")
  1856. )
  1857. global generator_filelist_paths
  1858. generator_filelist_paths = {
  1859. "toplevel": toplevel,
  1860. "qualified_out_dir": qualified_out_dir,
  1861. }
  1862. def OpenOutput(path, mode="w"):
  1863. """Open |path| for writing, creating directories if necessary."""
  1864. gyp.common.EnsureDirExists(path)
  1865. return open(path, mode)
  1866. def CommandWithWrapper(cmd, wrappers, prog):
  1867. wrapper = wrappers.get(cmd, "")
  1868. if wrapper:
  1869. return wrapper + " " + prog
  1870. return prog
  1871. def GetDefaultConcurrentLinks():
  1872. """Returns a best-guess for a number of concurrent links."""
  1873. pool_size = int(os.environ.get("GYP_LINK_CONCURRENCY", 0))
  1874. if pool_size:
  1875. return pool_size
  1876. if sys.platform in ("win32", "cygwin"):
  1877. import ctypes
  1878. class MEMORYSTATUSEX(ctypes.Structure):
  1879. _fields_ = [
  1880. ("dwLength", ctypes.c_ulong),
  1881. ("dwMemoryLoad", ctypes.c_ulong),
  1882. ("ullTotalPhys", ctypes.c_ulonglong),
  1883. ("ullAvailPhys", ctypes.c_ulonglong),
  1884. ("ullTotalPageFile", ctypes.c_ulonglong),
  1885. ("ullAvailPageFile", ctypes.c_ulonglong),
  1886. ("ullTotalVirtual", ctypes.c_ulonglong),
  1887. ("ullAvailVirtual", ctypes.c_ulonglong),
  1888. ("sullAvailExtendedVirtual", ctypes.c_ulonglong),
  1889. ]
  1890. stat = MEMORYSTATUSEX()
  1891. stat.dwLength = ctypes.sizeof(stat)
  1892. ctypes.windll.kernel32.GlobalMemoryStatusEx(ctypes.byref(stat))
  1893. # VS 2015 uses 20% more working set than VS 2013 and can consume all RAM
  1894. # on a 64 GiB machine.
  1895. mem_limit = max(1, stat.ullTotalPhys // (5 * (2 ** 30))) # total / 5GiB
  1896. hard_cap = max(1, int(os.environ.get("GYP_LINK_CONCURRENCY_MAX", 2 ** 32)))
  1897. return min(mem_limit, hard_cap)
  1898. elif sys.platform.startswith("linux"):
  1899. if os.path.exists("/proc/meminfo"):
  1900. with open("/proc/meminfo") as meminfo:
  1901. memtotal_re = re.compile(r"^MemTotal:\s*(\d*)\s*kB")
  1902. for line in meminfo:
  1903. match = memtotal_re.match(line)
  1904. if not match:
  1905. continue
  1906. # Allow 8Gb per link on Linux because Gold is quite memory hungry
  1907. return max(1, int(match.group(1)) // (8 * (2 ** 20)))
  1908. return 1
  1909. elif sys.platform == "darwin":
  1910. try:
  1911. avail_bytes = int(subprocess.check_output(["sysctl", "-n", "hw.memsize"]))
  1912. # A static library debug build of Chromium's unit_tests takes ~2.7GB, so
  1913. # 4GB per ld process allows for some more bloat.
  1914. return max(1, avail_bytes // (4 * (2 ** 30))) # total / 4GB
  1915. except subprocess.CalledProcessError:
  1916. return 1
  1917. else:
  1918. # TODO(scottmg): Implement this for other platforms.
  1919. return 1
  1920. def _GetWinLinkRuleNameSuffix(embed_manifest):
  1921. """Returns the suffix used to select an appropriate linking rule depending on
  1922. whether the manifest embedding is enabled."""
  1923. return "_embed" if embed_manifest else ""
  1924. def _AddWinLinkRules(master_ninja, embed_manifest):
  1925. """Adds link rules for Windows platform to |master_ninja|."""
  1926. def FullLinkCommand(ldcmd, out, binary_type):
  1927. resource_name = {"exe": "1", "dll": "2"}[binary_type]
  1928. return (
  1929. "%(python)s gyp-win-tool link-with-manifests $arch %(embed)s "
  1930. '%(out)s "%(ldcmd)s" %(resname)s $mt $rc "$intermediatemanifest" '
  1931. "$manifests"
  1932. % {
  1933. "python": sys.executable,
  1934. "out": out,
  1935. "ldcmd": ldcmd,
  1936. "resname": resource_name,
  1937. "embed": embed_manifest,
  1938. }
  1939. )
  1940. rule_name_suffix = _GetWinLinkRuleNameSuffix(embed_manifest)
  1941. use_separate_mspdbsrv = int(os.environ.get("GYP_USE_SEPARATE_MSPDBSRV", "0")) != 0
  1942. dlldesc = "LINK%s(DLL) $binary" % rule_name_suffix.upper()
  1943. dllcmd = (
  1944. "%s gyp-win-tool link-wrapper $arch %s "
  1945. "$ld /nologo $implibflag /DLL /OUT:$binary "
  1946. "@$binary.rsp" % (sys.executable, use_separate_mspdbsrv)
  1947. )
  1948. dllcmd = FullLinkCommand(dllcmd, "$binary", "dll")
  1949. master_ninja.rule(
  1950. "solink" + rule_name_suffix,
  1951. description=dlldesc,
  1952. command=dllcmd,
  1953. rspfile="$binary.rsp",
  1954. rspfile_content="$libs $in_newline $ldflags",
  1955. restat=True,
  1956. pool="link_pool",
  1957. )
  1958. master_ninja.rule(
  1959. "solink_module" + rule_name_suffix,
  1960. description=dlldesc,
  1961. command=dllcmd,
  1962. rspfile="$binary.rsp",
  1963. rspfile_content="$libs $in_newline $ldflags",
  1964. restat=True,
  1965. pool="link_pool",
  1966. )
  1967. # Note that ldflags goes at the end so that it has the option of
  1968. # overriding default settings earlier in the command line.
  1969. exe_cmd = (
  1970. "%s gyp-win-tool link-wrapper $arch %s "
  1971. "$ld /nologo /OUT:$binary @$binary.rsp"
  1972. % (sys.executable, use_separate_mspdbsrv)
  1973. )
  1974. exe_cmd = FullLinkCommand(exe_cmd, "$binary", "exe")
  1975. master_ninja.rule(
  1976. "link" + rule_name_suffix,
  1977. description="LINK%s $binary" % rule_name_suffix.upper(),
  1978. command=exe_cmd,
  1979. rspfile="$binary.rsp",
  1980. rspfile_content="$in_newline $libs $ldflags",
  1981. pool="link_pool",
  1982. )
  1983. def GenerateOutputForConfig(target_list, target_dicts, data, params, config_name):
  1984. options = params["options"]
  1985. flavor = gyp.common.GetFlavor(params)
  1986. generator_flags = params.get("generator_flags", {})
  1987. generate_compile_commands = generator_flags.get("compile_commands", False)
  1988. # build_dir: relative path from source root to our output files.
  1989. # e.g. "out/Debug"
  1990. build_dir = os.path.normpath(os.path.join(ComputeOutputDir(params), config_name))
  1991. toplevel_build = os.path.join(options.toplevel_dir, build_dir)
  1992. master_ninja_file = OpenOutput(os.path.join(toplevel_build, "build.ninja"))
  1993. master_ninja = ninja_syntax.Writer(master_ninja_file, width=120)
  1994. # Put build-time support tools in out/{config_name}.
  1995. gyp.common.CopyTool(flavor, toplevel_build, generator_flags)
  1996. # Grab make settings for CC/CXX.
  1997. # The rules are
  1998. # - The priority from low to high is gcc/g++, the 'make_global_settings' in
  1999. # gyp, the environment variable.
  2000. # - If there is no 'make_global_settings' for CC.host/CXX.host or
  2001. # 'CC_host'/'CXX_host' environment variable, cc_host/cxx_host should be set
  2002. # to cc/cxx.
  2003. if flavor == "win":
  2004. ar = "lib.exe"
  2005. # cc and cxx must be set to the correct architecture by overriding with one
  2006. # of cl_x86 or cl_x64 below.
  2007. cc = "UNSET"
  2008. cxx = "UNSET"
  2009. ld = "link.exe"
  2010. ld_host = "$ld"
  2011. else:
  2012. ar = "ar"
  2013. cc = "cc"
  2014. cxx = "c++"
  2015. ld = "$cc"
  2016. ldxx = "$cxx"
  2017. ld_host = "$cc_host"
  2018. ldxx_host = "$cxx_host"
  2019. ar_host = ar
  2020. cc_host = None
  2021. cxx_host = None
  2022. cc_host_global_setting = None
  2023. cxx_host_global_setting = None
  2024. clang_cl = None
  2025. nm = "nm"
  2026. nm_host = "nm"
  2027. readelf = "readelf"
  2028. readelf_host = "readelf"
  2029. build_file, _, _ = gyp.common.ParseQualifiedTarget(target_list[0])
  2030. make_global_settings = data[build_file].get("make_global_settings", [])
  2031. build_to_root = gyp.common.InvertRelativePath(build_dir, options.toplevel_dir)
  2032. wrappers = {}
  2033. for key, value in make_global_settings:
  2034. if key == "AR":
  2035. ar = os.path.join(build_to_root, value)
  2036. if key == "AR.host":
  2037. ar_host = os.path.join(build_to_root, value)
  2038. if key == "CC":
  2039. cc = os.path.join(build_to_root, value)
  2040. if cc.endswith("clang-cl"):
  2041. clang_cl = cc
  2042. if key == "CXX":
  2043. cxx = os.path.join(build_to_root, value)
  2044. if key == "CC.host":
  2045. cc_host = os.path.join(build_to_root, value)
  2046. cc_host_global_setting = value
  2047. if key == "CXX.host":
  2048. cxx_host = os.path.join(build_to_root, value)
  2049. cxx_host_global_setting = value
  2050. if key == "LD":
  2051. ld = os.path.join(build_to_root, value)
  2052. if key == "LD.host":
  2053. ld_host = os.path.join(build_to_root, value)
  2054. if key == "LDXX":
  2055. ldxx = os.path.join(build_to_root, value)
  2056. if key == "LDXX.host":
  2057. ldxx_host = os.path.join(build_to_root, value)
  2058. if key == "NM":
  2059. nm = os.path.join(build_to_root, value)
  2060. if key == "NM.host":
  2061. nm_host = os.path.join(build_to_root, value)
  2062. if key == "READELF":
  2063. readelf = os.path.join(build_to_root, value)
  2064. if key == "READELF.host":
  2065. readelf_host = os.path.join(build_to_root, value)
  2066. if key.endswith("_wrapper"):
  2067. wrappers[key[: -len("_wrapper")]] = os.path.join(build_to_root, value)
  2068. # Support wrappers from environment variables too.
  2069. for key, value in os.environ.items():
  2070. if key.lower().endswith("_wrapper"):
  2071. key_prefix = key[: -len("_wrapper")]
  2072. key_prefix = re.sub(r"\.HOST$", ".host", key_prefix)
  2073. wrappers[key_prefix] = os.path.join(build_to_root, value)
  2074. mac_toolchain_dir = generator_flags.get("mac_toolchain_dir", None)
  2075. if mac_toolchain_dir:
  2076. wrappers["LINK"] = "export DEVELOPER_DIR='%s' &&" % mac_toolchain_dir
  2077. if flavor == "win":
  2078. configs = [
  2079. target_dicts[qualified_target]["configurations"][config_name]
  2080. for qualified_target in target_list
  2081. ]
  2082. shared_system_includes = None
  2083. if not generator_flags.get("ninja_use_custom_environment_files", 0):
  2084. shared_system_includes = gyp.msvs_emulation.ExtractSharedMSVSSystemIncludes(
  2085. configs, generator_flags
  2086. )
  2087. cl_paths = gyp.msvs_emulation.GenerateEnvironmentFiles(
  2088. toplevel_build, generator_flags, shared_system_includes, OpenOutput
  2089. )
  2090. for arch, path in sorted(cl_paths.items()):
  2091. if clang_cl:
  2092. # If we have selected clang-cl, use that instead.
  2093. path = clang_cl
  2094. command = CommandWithWrapper(
  2095. "CC", wrappers, QuoteShellArgument(path, "win")
  2096. )
  2097. if clang_cl:
  2098. # Use clang-cl to cross-compile for x86 or x86_64.
  2099. command += " -m32" if arch == "x86" else " -m64"
  2100. master_ninja.variable("cl_" + arch, command)
  2101. cc = GetEnvironFallback(["CC_target", "CC"], cc)
  2102. master_ninja.variable("cc", CommandWithWrapper("CC", wrappers, cc))
  2103. cxx = GetEnvironFallback(["CXX_target", "CXX"], cxx)
  2104. master_ninja.variable("cxx", CommandWithWrapper("CXX", wrappers, cxx))
  2105. if flavor == "win":
  2106. master_ninja.variable("ld", ld)
  2107. master_ninja.variable("idl", "midl.exe")
  2108. master_ninja.variable("ar", ar)
  2109. master_ninja.variable("rc", "rc.exe")
  2110. master_ninja.variable("ml_x86", "ml.exe")
  2111. master_ninja.variable("ml_x64", "ml64.exe")
  2112. master_ninja.variable("mt", "mt.exe")
  2113. else:
  2114. master_ninja.variable("ld", CommandWithWrapper("LINK", wrappers, ld))
  2115. master_ninja.variable("ldxx", CommandWithWrapper("LINK", wrappers, ldxx))
  2116. master_ninja.variable("ar", GetEnvironFallback(["AR_target", "AR"], ar))
  2117. if flavor != "mac":
  2118. # Mac does not use readelf/nm for .TOC generation, so avoiding polluting
  2119. # the master ninja with extra unused variables.
  2120. master_ninja.variable("nm", GetEnvironFallback(["NM_target", "NM"], nm))
  2121. master_ninja.variable(
  2122. "readelf", GetEnvironFallback(["READELF_target", "READELF"], readelf)
  2123. )
  2124. if generator_supports_multiple_toolsets:
  2125. if not cc_host:
  2126. cc_host = cc
  2127. if not cxx_host:
  2128. cxx_host = cxx
  2129. master_ninja.variable("ar_host", GetEnvironFallback(["AR_host"], ar_host))
  2130. master_ninja.variable("nm_host", GetEnvironFallback(["NM_host"], nm_host))
  2131. master_ninja.variable(
  2132. "readelf_host", GetEnvironFallback(["READELF_host"], readelf_host)
  2133. )
  2134. cc_host = GetEnvironFallback(["CC_host"], cc_host)
  2135. cxx_host = GetEnvironFallback(["CXX_host"], cxx_host)
  2136. # The environment variable could be used in 'make_global_settings', like
  2137. # ['CC.host', '$(CC)'] or ['CXX.host', '$(CXX)'], transform them here.
  2138. if "$(CC)" in cc_host and cc_host_global_setting:
  2139. cc_host = cc_host_global_setting.replace("$(CC)", cc)
  2140. if "$(CXX)" in cxx_host and cxx_host_global_setting:
  2141. cxx_host = cxx_host_global_setting.replace("$(CXX)", cxx)
  2142. master_ninja.variable(
  2143. "cc_host", CommandWithWrapper("CC.host", wrappers, cc_host)
  2144. )
  2145. master_ninja.variable(
  2146. "cxx_host", CommandWithWrapper("CXX.host", wrappers, cxx_host)
  2147. )
  2148. if flavor == "win":
  2149. master_ninja.variable("ld_host", ld_host)
  2150. else:
  2151. master_ninja.variable(
  2152. "ld_host", CommandWithWrapper("LINK", wrappers, ld_host)
  2153. )
  2154. master_ninja.variable(
  2155. "ldxx_host", CommandWithWrapper("LINK", wrappers, ldxx_host)
  2156. )
  2157. master_ninja.newline()
  2158. master_ninja.pool("link_pool", depth=GetDefaultConcurrentLinks())
  2159. master_ninja.newline()
  2160. deps = "msvc" if flavor == "win" else "gcc"
  2161. if flavor != "win":
  2162. master_ninja.rule(
  2163. "cc",
  2164. description="CC $out",
  2165. command=(
  2166. "$cc -MMD -MF $out.d $defines $includes $cflags $cflags_c "
  2167. "$cflags_pch_c -c $in -o $out"
  2168. ),
  2169. depfile="$out.d",
  2170. deps=deps,
  2171. )
  2172. master_ninja.rule(
  2173. "cc_s",
  2174. description="CC $out",
  2175. command=(
  2176. "$cc $defines $includes $cflags $cflags_c "
  2177. "$cflags_pch_c -c $in -o $out"
  2178. ),
  2179. )
  2180. master_ninja.rule(
  2181. "cxx",
  2182. description="CXX $out",
  2183. command=(
  2184. "$cxx -MMD -MF $out.d $defines $includes $cflags $cflags_cc "
  2185. "$cflags_pch_cc -c $in -o $out"
  2186. ),
  2187. depfile="$out.d",
  2188. deps=deps,
  2189. )
  2190. else:
  2191. # TODO(scottmg) Separate pdb names is a test to see if it works around
  2192. # http://crbug.com/142362. It seems there's a race between the creation of
  2193. # the .pdb by the precompiled header step for .cc and the compilation of
  2194. # .c files. This should be handled by mspdbsrv, but rarely errors out with
  2195. # c1xx : fatal error C1033: cannot open program database
  2196. # By making the rules target separate pdb files this might be avoided.
  2197. cc_command = (
  2198. "ninja -t msvc -e $arch " + "-- "
  2199. "$cc /nologo /showIncludes /FC "
  2200. "@$out.rsp /c $in /Fo$out /Fd$pdbname_c "
  2201. )
  2202. cxx_command = (
  2203. "ninja -t msvc -e $arch " + "-- "
  2204. "$cxx /nologo /showIncludes /FC "
  2205. "@$out.rsp /c $in /Fo$out /Fd$pdbname_cc "
  2206. )
  2207. master_ninja.rule(
  2208. "cc",
  2209. description="CC $out",
  2210. command=cc_command,
  2211. rspfile="$out.rsp",
  2212. rspfile_content="$defines $includes $cflags $cflags_c",
  2213. deps=deps,
  2214. )
  2215. master_ninja.rule(
  2216. "cxx",
  2217. description="CXX $out",
  2218. command=cxx_command,
  2219. rspfile="$out.rsp",
  2220. rspfile_content="$defines $includes $cflags $cflags_cc",
  2221. deps=deps,
  2222. )
  2223. master_ninja.rule(
  2224. "idl",
  2225. description="IDL $in",
  2226. command=(
  2227. "%s gyp-win-tool midl-wrapper $arch $outdir "
  2228. "$tlb $h $dlldata $iid $proxy $in "
  2229. "$midl_includes $idlflags" % sys.executable
  2230. ),
  2231. )
  2232. master_ninja.rule(
  2233. "rc",
  2234. description="RC $in",
  2235. # Note: $in must be last otherwise rc.exe complains.
  2236. command=(
  2237. "%s gyp-win-tool rc-wrapper "
  2238. "$arch $rc $defines $resource_includes $rcflags /fo$out $in"
  2239. % sys.executable
  2240. ),
  2241. )
  2242. master_ninja.rule(
  2243. "asm",
  2244. description="ASM $out",
  2245. command=(
  2246. "%s gyp-win-tool asm-wrapper "
  2247. "$arch $asm $defines $includes $asmflags /c /Fo $out $in"
  2248. % sys.executable
  2249. ),
  2250. )
  2251. if flavor not in ("ios", "mac", "win"):
  2252. master_ninja.rule(
  2253. "alink",
  2254. description="AR $out",
  2255. command="rm -f $out && $ar rcs $arflags $out $in",
  2256. )
  2257. master_ninja.rule(
  2258. "alink_thin",
  2259. description="AR $out",
  2260. command="rm -f $out && $ar rcsT $arflags $out $in",
  2261. )
  2262. # This allows targets that only need to depend on $lib's API to declare an
  2263. # order-only dependency on $lib.TOC and avoid relinking such downstream
  2264. # dependencies when $lib changes only in non-public ways.
  2265. # The resulting string leaves an uninterpolated %{suffix} which
  2266. # is used in the final substitution below.
  2267. mtime_preserving_solink_base = (
  2268. "if [ ! -e $lib -o ! -e $lib.TOC ]; then "
  2269. "%(solink)s && %(extract_toc)s > $lib.TOC; else "
  2270. "%(solink)s && %(extract_toc)s > $lib.tmp && "
  2271. "if ! cmp -s $lib.tmp $lib.TOC; then mv $lib.tmp $lib.TOC ; "
  2272. "fi; fi"
  2273. % {
  2274. "solink": "$ld -shared $ldflags -o $lib -Wl,-soname=$soname %(suffix)s",
  2275. "extract_toc": (
  2276. "{ $readelf -d $lib | grep SONAME ; "
  2277. "$nm -gD -f p $lib | cut -f1-2 -d' '; }"
  2278. ),
  2279. }
  2280. )
  2281. master_ninja.rule(
  2282. "solink",
  2283. description="SOLINK $lib",
  2284. restat=True,
  2285. command=mtime_preserving_solink_base
  2286. % {"suffix": "@$link_file_list"},
  2287. rspfile="$link_file_list",
  2288. rspfile_content=(
  2289. "-Wl,--whole-archive $in $solibs -Wl," "--no-whole-archive $libs"
  2290. ),
  2291. pool="link_pool",
  2292. )
  2293. master_ninja.rule(
  2294. "solink_module",
  2295. description="SOLINK(module) $lib",
  2296. restat=True,
  2297. command=mtime_preserving_solink_base % {"suffix": "@$link_file_list"},
  2298. rspfile="$link_file_list",
  2299. rspfile_content="-Wl,--start-group $in $solibs $libs -Wl,--end-group",
  2300. pool="link_pool",
  2301. )
  2302. master_ninja.rule(
  2303. "link",
  2304. description="LINK $out",
  2305. command=(
  2306. "$ld $ldflags -o $out "
  2307. "-Wl,--start-group $in $solibs $libs -Wl,--end-group"
  2308. ),
  2309. pool="link_pool",
  2310. )
  2311. elif flavor == "win":
  2312. master_ninja.rule(
  2313. "alink",
  2314. description="LIB $out",
  2315. command=(
  2316. "%s gyp-win-tool link-wrapper $arch False "
  2317. "$ar /nologo /ignore:4221 /OUT:$out @$out.rsp" % sys.executable
  2318. ),
  2319. rspfile="$out.rsp",
  2320. rspfile_content="$in_newline $libflags",
  2321. )
  2322. _AddWinLinkRules(master_ninja, embed_manifest=True)
  2323. _AddWinLinkRules(master_ninja, embed_manifest=False)
  2324. else:
  2325. master_ninja.rule(
  2326. "objc",
  2327. description="OBJC $out",
  2328. command=(
  2329. "$cc -MMD -MF $out.d $defines $includes $cflags $cflags_objc "
  2330. "$cflags_pch_objc -c $in -o $out"
  2331. ),
  2332. depfile="$out.d",
  2333. deps=deps,
  2334. )
  2335. master_ninja.rule(
  2336. "objcxx",
  2337. description="OBJCXX $out",
  2338. command=(
  2339. "$cxx -MMD -MF $out.d $defines $includes $cflags $cflags_objcc "
  2340. "$cflags_pch_objcc -c $in -o $out"
  2341. ),
  2342. depfile="$out.d",
  2343. deps=deps,
  2344. )
  2345. master_ninja.rule(
  2346. "alink",
  2347. description="LIBTOOL-STATIC $out, POSTBUILDS",
  2348. command="rm -f $out && "
  2349. "%s gyp-mac-tool filter-libtool libtool $libtool_flags "
  2350. "-static -o $out $in"
  2351. "$postbuilds" % sys.executable,
  2352. )
  2353. master_ninja.rule(
  2354. "lipo",
  2355. description="LIPO $out, POSTBUILDS",
  2356. command="rm -f $out && lipo -create $in -output $out$postbuilds",
  2357. )
  2358. master_ninja.rule(
  2359. "solipo",
  2360. description="SOLIPO $out, POSTBUILDS",
  2361. command=(
  2362. "rm -f $lib $lib.TOC && lipo -create $in -output $lib$postbuilds &&"
  2363. "%(extract_toc)s > $lib.TOC"
  2364. % {
  2365. "extract_toc": "{ otool -l $lib | grep LC_ID_DYLIB -A 5; "
  2366. "nm -gP $lib | cut -f1-2 -d' ' | grep -v U$$; true; }"
  2367. }
  2368. ),
  2369. )
  2370. # Record the public interface of $lib in $lib.TOC. See the corresponding
  2371. # comment in the posix section above for details.
  2372. solink_base = "$ld %(type)s $ldflags -o $lib %(suffix)s"
  2373. mtime_preserving_solink_base = (
  2374. "if [ ! -e $lib -o ! -e $lib.TOC ] || "
  2375. # Always force dependent targets to relink if this library
  2376. # reexports something. Handling this correctly would require
  2377. # recursive TOC dumping but this is rare in practice, so punt.
  2378. "otool -l $lib | grep -q LC_REEXPORT_DYLIB ; then "
  2379. "%(solink)s && %(extract_toc)s > $lib.TOC; "
  2380. "else "
  2381. "%(solink)s && %(extract_toc)s > $lib.tmp && "
  2382. "if ! cmp -s $lib.tmp $lib.TOC; then "
  2383. "mv $lib.tmp $lib.TOC ; "
  2384. "fi; "
  2385. "fi"
  2386. % {
  2387. "solink": solink_base,
  2388. "extract_toc": "{ otool -l $lib | grep LC_ID_DYLIB -A 5; "
  2389. "nm -gP $lib | cut -f1-2 -d' ' | grep -v U$$; true; }",
  2390. }
  2391. )
  2392. solink_suffix = "@$link_file_list$postbuilds"
  2393. master_ninja.rule(
  2394. "solink",
  2395. description="SOLINK $lib, POSTBUILDS",
  2396. restat=True,
  2397. command=mtime_preserving_solink_base
  2398. % {"suffix": solink_suffix, "type": "-shared"},
  2399. rspfile="$link_file_list",
  2400. rspfile_content="$in $solibs $libs",
  2401. pool="link_pool",
  2402. )
  2403. master_ninja.rule(
  2404. "solink_notoc",
  2405. description="SOLINK $lib, POSTBUILDS",
  2406. restat=True,
  2407. command=solink_base % {"suffix": solink_suffix, "type": "-shared"},
  2408. rspfile="$link_file_list",
  2409. rspfile_content="$in $solibs $libs",
  2410. pool="link_pool",
  2411. )
  2412. master_ninja.rule(
  2413. "solink_module",
  2414. description="SOLINK(module) $lib, POSTBUILDS",
  2415. restat=True,
  2416. command=mtime_preserving_solink_base
  2417. % {"suffix": solink_suffix, "type": "-bundle"},
  2418. rspfile="$link_file_list",
  2419. rspfile_content="$in $solibs $libs",
  2420. pool="link_pool",
  2421. )
  2422. master_ninja.rule(
  2423. "solink_module_notoc",
  2424. description="SOLINK(module) $lib, POSTBUILDS",
  2425. restat=True,
  2426. command=solink_base % {"suffix": solink_suffix, "type": "-bundle"},
  2427. rspfile="$link_file_list",
  2428. rspfile_content="$in $solibs $libs",
  2429. pool="link_pool",
  2430. )
  2431. master_ninja.rule(
  2432. "link",
  2433. description="LINK $out, POSTBUILDS",
  2434. command=("$ld $ldflags -o $out " "$in $solibs $libs$postbuilds"),
  2435. pool="link_pool",
  2436. )
  2437. master_ninja.rule(
  2438. "preprocess_infoplist",
  2439. description="PREPROCESS INFOPLIST $out",
  2440. command=(
  2441. "$cc -E -P -Wno-trigraphs -x c $defines $in -o $out && "
  2442. "plutil -convert xml1 $out $out"
  2443. ),
  2444. )
  2445. master_ninja.rule(
  2446. "copy_infoplist",
  2447. description="COPY INFOPLIST $in",
  2448. command="$env %s gyp-mac-tool copy-info-plist $in $out $binary $keys"
  2449. % sys.executable,
  2450. )
  2451. master_ninja.rule(
  2452. "merge_infoplist",
  2453. description="MERGE INFOPLISTS $in",
  2454. command="$env %s gyp-mac-tool merge-info-plist $out $in" % sys.executable,
  2455. )
  2456. master_ninja.rule(
  2457. "compile_xcassets",
  2458. description="COMPILE XCASSETS $in",
  2459. command="$env %s gyp-mac-tool compile-xcassets $keys $in" % sys.executable,
  2460. )
  2461. master_ninja.rule(
  2462. "compile_ios_framework_headers",
  2463. description="COMPILE HEADER MAPS AND COPY FRAMEWORK HEADERS $in",
  2464. command="$env %(python)s gyp-mac-tool compile-ios-framework-header-map "
  2465. "$out $framework $in && $env %(python)s gyp-mac-tool "
  2466. "copy-ios-framework-headers $framework $copy_headers"
  2467. % {'python': sys.executable},
  2468. )
  2469. master_ninja.rule(
  2470. "mac_tool",
  2471. description="MACTOOL $mactool_cmd $in",
  2472. command="$env %s gyp-mac-tool $mactool_cmd $in $out $binary"
  2473. % sys.executable,
  2474. )
  2475. master_ninja.rule(
  2476. "package_framework",
  2477. description="PACKAGE FRAMEWORK $out, POSTBUILDS",
  2478. command="%s gyp-mac-tool package-framework $out $version$postbuilds "
  2479. "&& touch $out" % sys.executable,
  2480. )
  2481. master_ninja.rule(
  2482. "package_ios_framework",
  2483. description="PACKAGE IOS FRAMEWORK $out, POSTBUILDS",
  2484. command="%s gyp-mac-tool package-ios-framework $out $postbuilds "
  2485. "&& touch $out" % sys.executable,
  2486. )
  2487. if flavor == "win":
  2488. master_ninja.rule(
  2489. "stamp",
  2490. description="STAMP $out",
  2491. command="%s gyp-win-tool stamp $out" % sys.executable,
  2492. )
  2493. else:
  2494. master_ninja.rule(
  2495. "stamp", description="STAMP $out", command="${postbuilds}touch $out"
  2496. )
  2497. if flavor == "win":
  2498. master_ninja.rule(
  2499. "copy",
  2500. description="COPY $in $out",
  2501. command="%s gyp-win-tool recursive-mirror $in $out" % sys.executable,
  2502. )
  2503. elif flavor == "zos":
  2504. master_ninja.rule(
  2505. "copy",
  2506. description="COPY $in $out",
  2507. command="rm -rf $out && cp -fRP $in $out",
  2508. )
  2509. else:
  2510. master_ninja.rule(
  2511. "copy",
  2512. description="COPY $in $out",
  2513. command="ln -f $in $out 2>/dev/null || (rm -rf $out && cp -af $in $out)",
  2514. )
  2515. master_ninja.newline()
  2516. all_targets = set()
  2517. for build_file in params["build_files"]:
  2518. for target in gyp.common.AllTargets(
  2519. target_list, target_dicts, os.path.normpath(build_file)
  2520. ):
  2521. all_targets.add(target)
  2522. all_outputs = set()
  2523. # target_outputs is a map from qualified target name to a Target object.
  2524. target_outputs = {}
  2525. # target_short_names is a map from target short name to a list of Target
  2526. # objects.
  2527. target_short_names = {}
  2528. # short name of targets that were skipped because they didn't contain anything
  2529. # interesting.
  2530. # NOTE: there may be overlap between this an non_empty_target_names.
  2531. empty_target_names = set()
  2532. # Set of non-empty short target names.
  2533. # NOTE: there may be overlap between this an empty_target_names.
  2534. non_empty_target_names = set()
  2535. for qualified_target in target_list:
  2536. # qualified_target is like: third_party/icu/icu.gyp:icui18n#target
  2537. build_file, name, toolset = gyp.common.ParseQualifiedTarget(qualified_target)
  2538. this_make_global_settings = data[build_file].get("make_global_settings", [])
  2539. assert make_global_settings == this_make_global_settings, (
  2540. "make_global_settings needs to be the same for all targets. "
  2541. f"{this_make_global_settings} vs. {make_global_settings}"
  2542. )
  2543. spec = target_dicts[qualified_target]
  2544. if flavor == "mac":
  2545. gyp.xcode_emulation.MergeGlobalXcodeSettingsToSpec(data[build_file], spec)
  2546. # If build_file is a symlink, we must not follow it because there's a chance
  2547. # it could point to a path above toplevel_dir, and we cannot correctly deal
  2548. # with that case at the moment.
  2549. build_file = gyp.common.RelativePath(build_file, options.toplevel_dir, False)
  2550. qualified_target_for_hash = gyp.common.QualifiedTarget(
  2551. build_file, name, toolset
  2552. )
  2553. qualified_target_for_hash = qualified_target_for_hash.encode("utf-8")
  2554. hash_for_rules = hashlib.md5(qualified_target_for_hash).hexdigest()
  2555. base_path = os.path.dirname(build_file)
  2556. obj = "obj"
  2557. if toolset != "target":
  2558. obj += "." + toolset
  2559. output_file = os.path.join(obj, base_path, name + ".ninja")
  2560. ninja_output = StringIO()
  2561. writer = NinjaWriter(
  2562. hash_for_rules,
  2563. target_outputs,
  2564. base_path,
  2565. build_dir,
  2566. ninja_output,
  2567. toplevel_build,
  2568. output_file,
  2569. flavor,
  2570. toplevel_dir=options.toplevel_dir,
  2571. )
  2572. target = writer.WriteSpec(spec, config_name, generator_flags)
  2573. if ninja_output.tell() > 0:
  2574. # Only create files for ninja files that actually have contents.
  2575. with OpenOutput(os.path.join(toplevel_build, output_file)) as ninja_file:
  2576. ninja_file.write(ninja_output.getvalue())
  2577. ninja_output.close()
  2578. master_ninja.subninja(output_file)
  2579. if target:
  2580. if name != target.FinalOutput() and spec["toolset"] == "target":
  2581. target_short_names.setdefault(name, []).append(target)
  2582. target_outputs[qualified_target] = target
  2583. if qualified_target in all_targets:
  2584. all_outputs.add(target.FinalOutput())
  2585. non_empty_target_names.add(name)
  2586. else:
  2587. empty_target_names.add(name)
  2588. if target_short_names:
  2589. # Write a short name to build this target. This benefits both the
  2590. # "build chrome" case as well as the gyp tests, which expect to be
  2591. # able to run actions and build libraries by their short name.
  2592. master_ninja.newline()
  2593. master_ninja.comment("Short names for targets.")
  2594. for short_name in sorted(target_short_names):
  2595. master_ninja.build(
  2596. short_name,
  2597. "phony",
  2598. [x.FinalOutput() for x in target_short_names[short_name]],
  2599. )
  2600. # Write phony targets for any empty targets that weren't written yet. As
  2601. # short names are not necessarily unique only do this for short names that
  2602. # haven't already been output for another target.
  2603. empty_target_names = empty_target_names - non_empty_target_names
  2604. if empty_target_names:
  2605. master_ninja.newline()
  2606. master_ninja.comment("Empty targets (output for completeness).")
  2607. for name in sorted(empty_target_names):
  2608. master_ninja.build(name, "phony")
  2609. if all_outputs:
  2610. master_ninja.newline()
  2611. master_ninja.build("all", "phony", sorted(all_outputs))
  2612. master_ninja.default(generator_flags.get("default_target", "all"))
  2613. master_ninja_file.close()
  2614. if generate_compile_commands:
  2615. compile_db = GenerateCompileDBWithNinja(toplevel_build)
  2616. compile_db_file = OpenOutput(
  2617. os.path.join(toplevel_build, "compile_commands.json")
  2618. )
  2619. compile_db_file.write(json.dumps(compile_db, indent=2))
  2620. compile_db_file.close()
  2621. def GenerateCompileDBWithNinja(path, targets=["all"]):
  2622. """Generates a compile database using ninja.
  2623. Args:
  2624. path: The build directory to generate a compile database for.
  2625. targets: Additional targets to pass to ninja.
  2626. Returns:
  2627. List of the contents of the compile database.
  2628. """
  2629. ninja_path = shutil.which("ninja")
  2630. if ninja_path is None:
  2631. raise Exception("ninja not found in PATH")
  2632. json_compile_db = subprocess.check_output(
  2633. [ninja_path, "-C", path]
  2634. + targets
  2635. + ["-t", "compdb", "cc", "cxx", "objc", "objcxx"]
  2636. )
  2637. return json.loads(json_compile_db)
  2638. def PerformBuild(data, configurations, params):
  2639. options = params["options"]
  2640. for config in configurations:
  2641. builddir = os.path.join(options.toplevel_dir, "out", config)
  2642. arguments = ["ninja", "-C", builddir]
  2643. print(f"Building [{config}]: {arguments}")
  2644. subprocess.check_call(arguments)
  2645. def CallGenerateOutputForConfig(arglist):
  2646. # Ignore the interrupt signal so that the parent process catches it and
  2647. # kills all multiprocessing children.
  2648. signal.signal(signal.SIGINT, signal.SIG_IGN)
  2649. (target_list, target_dicts, data, params, config_name) = arglist
  2650. GenerateOutputForConfig(target_list, target_dicts, data, params, config_name)
  2651. def GenerateOutput(target_list, target_dicts, data, params):
  2652. # Update target_dicts for iOS device builds.
  2653. target_dicts = gyp.xcode_emulation.CloneConfigurationForDeviceAndEmulator(
  2654. target_dicts
  2655. )
  2656. user_config = params.get("generator_flags", {}).get("config", None)
  2657. if gyp.common.GetFlavor(params) == "win":
  2658. target_list, target_dicts = MSVSUtil.ShardTargets(target_list, target_dicts)
  2659. target_list, target_dicts = MSVSUtil.InsertLargePdbShims(
  2660. target_list, target_dicts, generator_default_variables
  2661. )
  2662. if user_config:
  2663. GenerateOutputForConfig(target_list, target_dicts, data, params, user_config)
  2664. else:
  2665. config_names = target_dicts[target_list[0]]["configurations"]
  2666. if params["parallel"]:
  2667. try:
  2668. pool = multiprocessing.Pool(len(config_names))
  2669. arglists = []
  2670. for config_name in config_names:
  2671. arglists.append(
  2672. (target_list, target_dicts, data, params, config_name)
  2673. )
  2674. pool.map(CallGenerateOutputForConfig, arglists)
  2675. except KeyboardInterrupt as e:
  2676. pool.terminate()
  2677. raise e
  2678. else:
  2679. for config_name in config_names:
  2680. GenerateOutputForConfig(
  2681. target_list, target_dicts, data, params, config_name
  2682. )