8e36496d07fb6453e71085509ee261a1e190d2e5d329ea350c9bd84049ea1a551e2b275da05652520e276af60ee30c00394077f1ad38b6d114d54f8e7e1a97 148 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981
  1. # Copyright (c) 2012 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 ntpath
  5. import os
  6. import posixpath
  7. import re
  8. import subprocess
  9. import sys
  10. from collections import OrderedDict
  11. import gyp.common
  12. import gyp.easy_xml as easy_xml
  13. import gyp.generator.ninja as ninja_generator
  14. import gyp.MSVSNew as MSVSNew
  15. import gyp.MSVSProject as MSVSProject
  16. import gyp.MSVSSettings as MSVSSettings
  17. import gyp.MSVSToolFile as MSVSToolFile
  18. import gyp.MSVSUserFile as MSVSUserFile
  19. import gyp.MSVSUtil as MSVSUtil
  20. import gyp.MSVSVersion as MSVSVersion
  21. from gyp.common import GypError
  22. from gyp.common import OrderedSet
  23. # Regular expression for validating Visual Studio GUIDs. If the GUID
  24. # contains lowercase hex letters, MSVS will be fine. However,
  25. # IncrediBuild BuildConsole will parse the solution file, but then
  26. # silently skip building the target causing hard to track down errors.
  27. # Note that this only happens with the BuildConsole, and does not occur
  28. # if IncrediBuild is executed from inside Visual Studio. This regex
  29. # validates that the string looks like a GUID with all uppercase hex
  30. # letters.
  31. VALID_MSVS_GUID_CHARS = re.compile(r"^[A-F0-9\-]+$")
  32. generator_supports_multiple_toolsets = gyp.common.CrossCompileRequested()
  33. generator_default_variables = {
  34. "DRIVER_PREFIX": "",
  35. "DRIVER_SUFFIX": ".sys",
  36. "EXECUTABLE_PREFIX": "",
  37. "EXECUTABLE_SUFFIX": ".exe",
  38. "STATIC_LIB_PREFIX": "",
  39. "SHARED_LIB_PREFIX": "",
  40. "STATIC_LIB_SUFFIX": ".lib",
  41. "SHARED_LIB_SUFFIX": ".dll",
  42. "INTERMEDIATE_DIR": "$(IntDir)",
  43. "SHARED_INTERMEDIATE_DIR": "$(OutDir)/obj/global_intermediate",
  44. "OS": "win",
  45. "PRODUCT_DIR": "$(OutDir)",
  46. "LIB_DIR": "$(OutDir)lib",
  47. "RULE_INPUT_ROOT": "$(InputName)",
  48. "RULE_INPUT_DIRNAME": "$(InputDir)",
  49. "RULE_INPUT_EXT": "$(InputExt)",
  50. "RULE_INPUT_NAME": "$(InputFileName)",
  51. "RULE_INPUT_PATH": "$(InputPath)",
  52. "CONFIGURATION_NAME": "$(ConfigurationName)",
  53. }
  54. # The msvs specific sections that hold paths
  55. generator_additional_path_sections = [
  56. "msvs_cygwin_dirs",
  57. "msvs_props",
  58. ]
  59. generator_additional_non_configuration_keys = [
  60. "msvs_cygwin_dirs",
  61. "msvs_cygwin_shell",
  62. "msvs_large_pdb",
  63. "msvs_shard",
  64. "msvs_external_builder",
  65. "msvs_external_builder_out_dir",
  66. "msvs_external_builder_build_cmd",
  67. "msvs_external_builder_clean_cmd",
  68. "msvs_external_builder_clcompile_cmd",
  69. "msvs_enable_winrt",
  70. "msvs_requires_importlibrary",
  71. "msvs_enable_winphone",
  72. "msvs_application_type_revision",
  73. "msvs_target_platform_version",
  74. "msvs_target_platform_minversion",
  75. ]
  76. generator_filelist_paths = None
  77. # List of precompiled header related keys.
  78. precomp_keys = [
  79. "msvs_precompiled_header",
  80. "msvs_precompiled_source",
  81. ]
  82. cached_username = None
  83. cached_domain = None
  84. # TODO(gspencer): Switch the os.environ calls to be
  85. # win32api.GetDomainName() and win32api.GetUserName() once the
  86. # python version in depot_tools has been updated to work on Vista
  87. # 64-bit.
  88. def _GetDomainAndUserName():
  89. if sys.platform not in ("win32", "cygwin"):
  90. return ("DOMAIN", "USERNAME")
  91. global cached_username
  92. global cached_domain
  93. if not cached_domain or not cached_username:
  94. domain = os.environ.get("USERDOMAIN")
  95. username = os.environ.get("USERNAME")
  96. if not domain or not username:
  97. call = subprocess.Popen(
  98. ["net", "config", "Workstation"], stdout=subprocess.PIPE
  99. )
  100. config = call.communicate()[0].decode("utf-8")
  101. username_re = re.compile(r"^User name\s+(\S+)", re.MULTILINE)
  102. username_match = username_re.search(config)
  103. if username_match:
  104. username = username_match.group(1)
  105. domain_re = re.compile(r"^Logon domain\s+(\S+)", re.MULTILINE)
  106. domain_match = domain_re.search(config)
  107. if domain_match:
  108. domain = domain_match.group(1)
  109. cached_domain = domain
  110. cached_username = username
  111. return (cached_domain, cached_username)
  112. fixpath_prefix = None
  113. def _NormalizedSource(source):
  114. """Normalize the path.
  115. But not if that gets rid of a variable, as this may expand to something
  116. larger than one directory.
  117. Arguments:
  118. source: The path to be normalize.d
  119. Returns:
  120. The normalized path.
  121. """
  122. normalized = os.path.normpath(source)
  123. if source.count("$") == normalized.count("$"):
  124. source = normalized
  125. return source
  126. def _FixPath(path, separator="\\"):
  127. """Convert paths to a form that will make sense in a vcproj file.
  128. Arguments:
  129. path: The path to convert, may contain / etc.
  130. Returns:
  131. The path with all slashes made into backslashes.
  132. """
  133. if (
  134. fixpath_prefix
  135. and path
  136. and not os.path.isabs(path)
  137. and path[0] != "$"
  138. and not _IsWindowsAbsPath(path)
  139. ):
  140. path = os.path.join(fixpath_prefix, path)
  141. if separator == "\\":
  142. path = path.replace("/", "\\")
  143. path = _NormalizedSource(path)
  144. if separator == "/":
  145. path = path.replace("\\", "/")
  146. if path and path[-1] == separator:
  147. path = path[:-1]
  148. return path
  149. def _IsWindowsAbsPath(path):
  150. """
  151. On Cygwin systems Python needs a little help determining if a path
  152. is an absolute Windows path or not, so that
  153. it does not treat those as relative, which results in bad paths like:
  154. '..\\C:\\<some path>\\some_source_code_file.cc'
  155. """
  156. return path.startswith("c:") or path.startswith("C:")
  157. def _FixPaths(paths, separator="\\"):
  158. """Fix each of the paths of the list."""
  159. return [_FixPath(i, separator) for i in paths]
  160. def _ConvertSourcesToFilterHierarchy(
  161. sources, prefix=None, excluded=None, list_excluded=True, msvs_version=None
  162. ):
  163. """Converts a list split source file paths into a vcproj folder hierarchy.
  164. Arguments:
  165. sources: A list of source file paths split.
  166. prefix: A list of source file path layers meant to apply to each of sources.
  167. excluded: A set of excluded files.
  168. msvs_version: A MSVSVersion object.
  169. Returns:
  170. A hierarchy of filenames and MSVSProject.Filter objects that matches the
  171. layout of the source tree.
  172. For example:
  173. _ConvertSourcesToFilterHierarchy([['a', 'bob1.c'], ['b', 'bob2.c']],
  174. prefix=['joe'])
  175. -->
  176. [MSVSProject.Filter('a', contents=['joe\\a\\bob1.c']),
  177. MSVSProject.Filter('b', contents=['joe\\b\\bob2.c'])]
  178. """
  179. if not prefix:
  180. prefix = []
  181. result = []
  182. excluded_result = []
  183. folders = OrderedDict()
  184. # Gather files into the final result, excluded, or folders.
  185. for s in sources:
  186. if len(s) == 1:
  187. filename = _NormalizedSource("\\".join(prefix + s))
  188. if filename in excluded:
  189. excluded_result.append(filename)
  190. else:
  191. result.append(filename)
  192. elif msvs_version and not msvs_version.UsesVcxproj():
  193. # For MSVS 2008 and earlier, we need to process all files before walking
  194. # the sub folders.
  195. if not folders.get(s[0]):
  196. folders[s[0]] = []
  197. folders[s[0]].append(s[1:])
  198. else:
  199. contents = _ConvertSourcesToFilterHierarchy(
  200. [s[1:]],
  201. prefix + [s[0]],
  202. excluded=excluded,
  203. list_excluded=list_excluded,
  204. msvs_version=msvs_version,
  205. )
  206. contents = MSVSProject.Filter(s[0], contents=contents)
  207. result.append(contents)
  208. # Add a folder for excluded files.
  209. if excluded_result and list_excluded:
  210. excluded_folder = MSVSProject.Filter(
  211. "_excluded_files", contents=excluded_result
  212. )
  213. result.append(excluded_folder)
  214. if msvs_version and msvs_version.UsesVcxproj():
  215. return result
  216. # Populate all the folders.
  217. for f in folders:
  218. contents = _ConvertSourcesToFilterHierarchy(
  219. folders[f],
  220. prefix=prefix + [f],
  221. excluded=excluded,
  222. list_excluded=list_excluded,
  223. msvs_version=msvs_version,
  224. )
  225. contents = MSVSProject.Filter(f, contents=contents)
  226. result.append(contents)
  227. return result
  228. def _ToolAppend(tools, tool_name, setting, value, only_if_unset=False):
  229. if not value:
  230. return
  231. _ToolSetOrAppend(tools, tool_name, setting, value, only_if_unset)
  232. def _ToolSetOrAppend(tools, tool_name, setting, value, only_if_unset=False):
  233. # TODO(bradnelson): ugly hack, fix this more generally!!!
  234. if "Directories" in setting or "Dependencies" in setting:
  235. if isinstance(value, str):
  236. value = value.replace("/", "\\")
  237. else:
  238. value = [i.replace("/", "\\") for i in value]
  239. if not tools.get(tool_name):
  240. tools[tool_name] = {}
  241. tool = tools[tool_name]
  242. if setting == "CompileAsWinRT":
  243. return
  244. if tool.get(setting):
  245. if only_if_unset:
  246. return
  247. if isinstance(tool[setting], list) and isinstance(value, list):
  248. tool[setting] += value
  249. else:
  250. raise TypeError(
  251. 'Appending "%s" to a non-list setting "%s" for tool "%s" is '
  252. "not allowed, previous value: %s"
  253. % (value, setting, tool_name, str(tool[setting]))
  254. )
  255. else:
  256. tool[setting] = value
  257. def _ConfigTargetVersion(config_data):
  258. return config_data.get("msvs_target_version", "Windows7")
  259. def _ConfigPlatform(config_data):
  260. return config_data.get("msvs_configuration_platform", "Win32")
  261. def _ConfigBaseName(config_name, platform_name):
  262. if config_name.endswith("_" + platform_name):
  263. return config_name[0 : -len(platform_name) - 1]
  264. else:
  265. return config_name
  266. def _ConfigFullName(config_name, config_data):
  267. platform_name = _ConfigPlatform(config_data)
  268. return f"{_ConfigBaseName(config_name, platform_name)}|{platform_name}"
  269. def _ConfigWindowsTargetPlatformVersion(config_data, version):
  270. target_ver = config_data.get("msvs_windows_target_platform_version")
  271. if target_ver and re.match(r"^\d+", target_ver):
  272. return target_ver
  273. config_ver = config_data.get("msvs_windows_sdk_version")
  274. vers = [config_ver] if config_ver else version.compatible_sdks
  275. for ver in vers:
  276. for key in [
  277. r"HKLM\Software\Microsoft\Microsoft SDKs\Windows\%s",
  278. r"HKLM\Software\Wow6432Node\Microsoft\Microsoft SDKs\Windows\%s",
  279. ]:
  280. sdk_dir = MSVSVersion._RegistryGetValue(key % ver, "InstallationFolder")
  281. if not sdk_dir:
  282. continue
  283. version = MSVSVersion._RegistryGetValue(key % ver, "ProductVersion") or ""
  284. # Find a matching entry in sdk_dir\include.
  285. expected_sdk_dir = r"%s\include" % sdk_dir
  286. names = sorted(
  287. (
  288. x
  289. for x in (
  290. os.listdir(expected_sdk_dir)
  291. if os.path.isdir(expected_sdk_dir)
  292. else []
  293. )
  294. if x.startswith(version)
  295. ),
  296. reverse=True,
  297. )
  298. if names:
  299. return names[0]
  300. else:
  301. print(
  302. "Warning: No include files found for detected "
  303. "Windows SDK version %s" % (version),
  304. file=sys.stdout,
  305. )
  306. def _BuildCommandLineForRuleRaw(
  307. spec, cmd, cygwin_shell, has_input_path, quote_cmd, do_setup_env
  308. ):
  309. if [x for x in cmd if "$(InputDir)" in x]:
  310. input_dir_preamble = (
  311. "set INPUTDIR=$(InputDir)\n"
  312. "if NOT DEFINED INPUTDIR set INPUTDIR=.\\\n"
  313. "set INPUTDIR=%INPUTDIR:~0,-1%\n"
  314. )
  315. else:
  316. input_dir_preamble = ""
  317. if cygwin_shell:
  318. # Find path to cygwin.
  319. cygwin_dir = _FixPath(spec.get("msvs_cygwin_dirs", ["."])[0])
  320. # Prepare command.
  321. direct_cmd = cmd
  322. direct_cmd = [
  323. i.replace("$(IntDir)", '`cygpath -m "${INTDIR}"`') for i in direct_cmd
  324. ]
  325. direct_cmd = [
  326. i.replace("$(OutDir)", '`cygpath -m "${OUTDIR}"`') for i in direct_cmd
  327. ]
  328. direct_cmd = [
  329. i.replace("$(InputDir)", '`cygpath -m "${INPUTDIR}"`') for i in direct_cmd
  330. ]
  331. if has_input_path:
  332. direct_cmd = [
  333. i.replace("$(InputPath)", '`cygpath -m "${INPUTPATH}"`')
  334. for i in direct_cmd
  335. ]
  336. direct_cmd = ['\\"%s\\"' % i.replace('"', '\\\\\\"') for i in direct_cmd]
  337. # direct_cmd = gyp.common.EncodePOSIXShellList(direct_cmd)
  338. direct_cmd = " ".join(direct_cmd)
  339. # TODO(quote): regularize quoting path names throughout the module
  340. cmd = ""
  341. if do_setup_env:
  342. cmd += 'call "$(ProjectDir)%(cygwin_dir)s\\setup_env.bat" && '
  343. cmd += "set CYGWIN=nontsec&& "
  344. if direct_cmd.find("NUMBER_OF_PROCESSORS") >= 0:
  345. cmd += "set /a NUMBER_OF_PROCESSORS_PLUS_1=%%NUMBER_OF_PROCESSORS%%+1&& "
  346. if direct_cmd.find("INTDIR") >= 0:
  347. cmd += "set INTDIR=$(IntDir)&& "
  348. if direct_cmd.find("OUTDIR") >= 0:
  349. cmd += "set OUTDIR=$(OutDir)&& "
  350. if has_input_path and direct_cmd.find("INPUTPATH") >= 0:
  351. cmd += "set INPUTPATH=$(InputPath) && "
  352. cmd += 'bash -c "%(cmd)s"'
  353. cmd = cmd % {"cygwin_dir": cygwin_dir, "cmd": direct_cmd}
  354. return input_dir_preamble + cmd
  355. else:
  356. # Convert cat --> type to mimic unix.
  357. command = ["type"] if cmd[0] == "cat" else [cmd[0].replace("/", "\\")]
  358. # Add call before command to ensure that commands can be tied together one
  359. # after the other without aborting in Incredibuild, since IB makes a bat
  360. # file out of the raw command string, and some commands (like python) are
  361. # actually batch files themselves.
  362. command.insert(0, "call")
  363. # Fix the paths
  364. # TODO(quote): This is a really ugly heuristic, and will miss path fixing
  365. # for arguments like "--arg=path", arg=path, or "/opt:path".
  366. # If the argument starts with a slash or dash, or contains an equal sign,
  367. # it's probably a command line switch.
  368. # Return the path with forward slashes because the command using it might
  369. # not support backslashes.
  370. arguments = [
  371. i if (i[:1] in "/-" or "=" in i) else _FixPath(i, "/")
  372. for i in cmd[1:]
  373. ]
  374. arguments = [i.replace("$(InputDir)", "%INPUTDIR%") for i in arguments]
  375. arguments = [MSVSSettings.FixVCMacroSlashes(i) for i in arguments]
  376. if quote_cmd:
  377. # Support a mode for using cmd directly.
  378. # Convert any paths to native form (first element is used directly).
  379. # TODO(quote): regularize quoting path names throughout the module
  380. command[1] = '"%s"' % command[1]
  381. arguments = ['"%s"' % i for i in arguments]
  382. # Collapse into a single command.
  383. return input_dir_preamble + " ".join(command + arguments)
  384. def _BuildCommandLineForRule(spec, rule, has_input_path, do_setup_env):
  385. # Currently this weird argument munging is used to duplicate the way a
  386. # python script would need to be run as part of the chrome tree.
  387. # Eventually we should add some sort of rule_default option to set this
  388. # per project. For now the behavior chrome needs is the default.
  389. mcs = rule.get("msvs_cygwin_shell")
  390. if mcs is None:
  391. mcs = int(spec.get("msvs_cygwin_shell", 1))
  392. elif isinstance(mcs, str):
  393. mcs = int(mcs)
  394. quote_cmd = int(rule.get("msvs_quote_cmd", 1))
  395. return _BuildCommandLineForRuleRaw(
  396. spec, rule["action"], mcs, has_input_path, quote_cmd, do_setup_env=do_setup_env
  397. )
  398. def _AddActionStep(actions_dict, inputs, outputs, description, command):
  399. """Merge action into an existing list of actions.
  400. Care must be taken so that actions which have overlapping inputs either don't
  401. get assigned to the same input, or get collapsed into one.
  402. Arguments:
  403. actions_dict: dictionary keyed on input name, which maps to a list of
  404. dicts describing the actions attached to that input file.
  405. inputs: list of inputs
  406. outputs: list of outputs
  407. description: description of the action
  408. command: command line to execute
  409. """
  410. # Require there to be at least one input (call sites will ensure this).
  411. assert inputs
  412. action = {
  413. "inputs": inputs,
  414. "outputs": outputs,
  415. "description": description,
  416. "command": command,
  417. }
  418. # Pick where to stick this action.
  419. # While less than optimal in terms of build time, attach them to the first
  420. # input for now.
  421. chosen_input = inputs[0]
  422. # Add it there.
  423. if chosen_input not in actions_dict:
  424. actions_dict[chosen_input] = []
  425. actions_dict[chosen_input].append(action)
  426. def _AddCustomBuildToolForMSVS(
  427. p, spec, primary_input, inputs, outputs, description, cmd
  428. ):
  429. """Add a custom build tool to execute something.
  430. Arguments:
  431. p: the target project
  432. spec: the target project dict
  433. primary_input: input file to attach the build tool to
  434. inputs: list of inputs
  435. outputs: list of outputs
  436. description: description of the action
  437. cmd: command line to execute
  438. """
  439. inputs = _FixPaths(inputs)
  440. outputs = _FixPaths(outputs)
  441. tool = MSVSProject.Tool(
  442. "VCCustomBuildTool",
  443. {
  444. "Description": description,
  445. "AdditionalDependencies": ";".join(inputs),
  446. "Outputs": ";".join(outputs),
  447. "CommandLine": cmd,
  448. },
  449. )
  450. # Add to the properties of primary input for each config.
  451. for config_name, c_data in spec["configurations"].items():
  452. p.AddFileConfig(
  453. _FixPath(primary_input), _ConfigFullName(config_name, c_data), tools=[tool]
  454. )
  455. def _AddAccumulatedActionsToMSVS(p, spec, actions_dict):
  456. """Add actions accumulated into an actions_dict, merging as needed.
  457. Arguments:
  458. p: the target project
  459. spec: the target project dict
  460. actions_dict: dictionary keyed on input name, which maps to a list of
  461. dicts describing the actions attached to that input file.
  462. """
  463. for primary_input in actions_dict:
  464. inputs = OrderedSet()
  465. outputs = OrderedSet()
  466. descriptions = []
  467. commands = []
  468. for action in actions_dict[primary_input]:
  469. inputs.update(OrderedSet(action["inputs"]))
  470. outputs.update(OrderedSet(action["outputs"]))
  471. descriptions.append(action["description"])
  472. commands.append(action["command"])
  473. # Add the custom build step for one input file.
  474. description = ", and also ".join(descriptions)
  475. command = "\r\n".join(commands)
  476. _AddCustomBuildToolForMSVS(
  477. p,
  478. spec,
  479. primary_input=primary_input,
  480. inputs=inputs,
  481. outputs=outputs,
  482. description=description,
  483. cmd=command,
  484. )
  485. def _RuleExpandPath(path, input_file):
  486. """Given the input file to which a rule applied, string substitute a path.
  487. Arguments:
  488. path: a path to string expand
  489. input_file: the file to which the rule applied.
  490. Returns:
  491. The string substituted path.
  492. """
  493. path = path.replace(
  494. "$(InputName)", os.path.splitext(os.path.split(input_file)[1])[0]
  495. )
  496. path = path.replace("$(InputDir)", os.path.dirname(input_file))
  497. path = path.replace(
  498. "$(InputExt)", os.path.splitext(os.path.split(input_file)[1])[1]
  499. )
  500. path = path.replace("$(InputFileName)", os.path.split(input_file)[1])
  501. path = path.replace("$(InputPath)", input_file)
  502. return path
  503. def _FindRuleTriggerFiles(rule, sources):
  504. """Find the list of files which a particular rule applies to.
  505. Arguments:
  506. rule: the rule in question
  507. sources: the set of all known source files for this project
  508. Returns:
  509. The list of sources that trigger a particular rule.
  510. """
  511. return rule.get("rule_sources", [])
  512. def _RuleInputsAndOutputs(rule, trigger_file):
  513. """Find the inputs and outputs generated by a rule.
  514. Arguments:
  515. rule: the rule in question.
  516. trigger_file: the main trigger for this rule.
  517. Returns:
  518. The pair of (inputs, outputs) involved in this rule.
  519. """
  520. raw_inputs = _FixPaths(rule.get("inputs", []))
  521. raw_outputs = _FixPaths(rule.get("outputs", []))
  522. inputs = OrderedSet()
  523. outputs = OrderedSet()
  524. inputs.add(trigger_file)
  525. for i in raw_inputs:
  526. inputs.add(_RuleExpandPath(i, trigger_file))
  527. for o in raw_outputs:
  528. outputs.add(_RuleExpandPath(o, trigger_file))
  529. return (inputs, outputs)
  530. def _GenerateNativeRulesForMSVS(p, rules, output_dir, spec, options):
  531. """Generate a native rules file.
  532. Arguments:
  533. p: the target project
  534. rules: the set of rules to include
  535. output_dir: the directory in which the project/gyp resides
  536. spec: the project dict
  537. options: global generator options
  538. """
  539. rules_filename = "{}{}.rules".format(spec["target_name"], options.suffix)
  540. rules_file = MSVSToolFile.Writer(
  541. os.path.join(output_dir, rules_filename), spec["target_name"]
  542. )
  543. # Add each rule.
  544. for r in rules:
  545. rule_name = r["rule_name"]
  546. rule_ext = r["extension"]
  547. inputs = _FixPaths(r.get("inputs", []))
  548. outputs = _FixPaths(r.get("outputs", []))
  549. # Skip a rule with no action and no inputs.
  550. if "action" not in r and not r.get("rule_sources", []):
  551. continue
  552. cmd = _BuildCommandLineForRule(spec, r, has_input_path=True, do_setup_env=True)
  553. rules_file.AddCustomBuildRule(
  554. name=rule_name,
  555. description=r.get("message", rule_name),
  556. extensions=[rule_ext],
  557. additional_dependencies=inputs,
  558. outputs=outputs,
  559. cmd=cmd,
  560. )
  561. # Write out rules file.
  562. rules_file.WriteIfChanged()
  563. # Add rules file to project.
  564. p.AddToolFile(rules_filename)
  565. def _Cygwinify(path):
  566. path = path.replace("$(OutDir)", "$(OutDirCygwin)")
  567. path = path.replace("$(IntDir)", "$(IntDirCygwin)")
  568. return path
  569. def _GenerateExternalRules(rules, output_dir, spec, sources, options, actions_to_add):
  570. """Generate an external makefile to do a set of rules.
  571. Arguments:
  572. rules: the list of rules to include
  573. output_dir: path containing project and gyp files
  574. spec: project specification data
  575. sources: set of sources known
  576. options: global generator options
  577. actions_to_add: The list of actions we will add to.
  578. """
  579. filename = "{}_rules{}.mk".format(spec["target_name"], options.suffix)
  580. mk_file = gyp.common.WriteOnDiff(os.path.join(output_dir, filename))
  581. # Find cygwin style versions of some paths.
  582. mk_file.write('OutDirCygwin:=$(shell cygpath -u "$(OutDir)")\n')
  583. mk_file.write('IntDirCygwin:=$(shell cygpath -u "$(IntDir)")\n')
  584. # Gather stuff needed to emit all: target.
  585. all_inputs = OrderedSet()
  586. all_outputs = OrderedSet()
  587. all_output_dirs = OrderedSet()
  588. first_outputs = []
  589. for rule in rules:
  590. trigger_files = _FindRuleTriggerFiles(rule, sources)
  591. for tf in trigger_files:
  592. inputs, outputs = _RuleInputsAndOutputs(rule, tf)
  593. all_inputs.update(OrderedSet(inputs))
  594. all_outputs.update(OrderedSet(outputs))
  595. # Only use one target from each rule as the dependency for
  596. # 'all' so we don't try to build each rule multiple times.
  597. first_outputs.append(next(iter(outputs)))
  598. # Get the unique output directories for this rule.
  599. output_dirs = [os.path.split(i)[0] for i in outputs]
  600. for od in output_dirs:
  601. all_output_dirs.add(od)
  602. first_outputs_cyg = [_Cygwinify(i) for i in first_outputs]
  603. # Write out all: target, including mkdir for each output directory.
  604. mk_file.write("all: %s\n" % " ".join(first_outputs_cyg))
  605. for od in all_output_dirs:
  606. if od:
  607. mk_file.write('\tmkdir -p `cygpath -u "%s"`\n' % od)
  608. mk_file.write("\n")
  609. # Define how each output is generated.
  610. for rule in rules:
  611. trigger_files = _FindRuleTriggerFiles(rule, sources)
  612. for tf in trigger_files:
  613. # Get all the inputs and outputs for this rule for this trigger file.
  614. inputs, outputs = _RuleInputsAndOutputs(rule, tf)
  615. inputs = [_Cygwinify(i) for i in inputs]
  616. outputs = [_Cygwinify(i) for i in outputs]
  617. # Prepare the command line for this rule.
  618. cmd = [_RuleExpandPath(c, tf) for c in rule["action"]]
  619. cmd = ['"%s"' % i for i in cmd]
  620. cmd = " ".join(cmd)
  621. # Add it to the makefile.
  622. mk_file.write("{}: {}\n".format(" ".join(outputs), " ".join(inputs)))
  623. mk_file.write("\t%s\n\n" % cmd)
  624. # Close up the file.
  625. mk_file.close()
  626. # Add makefile to list of sources.
  627. sources.add(filename)
  628. # Add a build action to call makefile.
  629. cmd = [
  630. "make",
  631. "OutDir=$(OutDir)",
  632. "IntDir=$(IntDir)",
  633. "-j",
  634. "${NUMBER_OF_PROCESSORS_PLUS_1}",
  635. "-f",
  636. filename,
  637. ]
  638. cmd = _BuildCommandLineForRuleRaw(spec, cmd, True, False, True, True)
  639. # Insert makefile as 0'th input, so it gets the action attached there,
  640. # as this is easier to understand from in the IDE.
  641. all_inputs = list(all_inputs)
  642. all_inputs.insert(0, filename)
  643. _AddActionStep(
  644. actions_to_add,
  645. inputs=_FixPaths(all_inputs),
  646. outputs=_FixPaths(all_outputs),
  647. description="Running external rules for %s" % spec["target_name"],
  648. command=cmd,
  649. )
  650. def _EscapeEnvironmentVariableExpansion(s):
  651. """Escapes % characters.
  652. Escapes any % characters so that Windows-style environment variable
  653. expansions will leave them alone.
  654. See http://connect.microsoft.com/VisualStudio/feedback/details/106127/cl-d-name-text-containing-percentage-characters-doesnt-compile
  655. to understand why we have to do this.
  656. Args:
  657. s: The string to be escaped.
  658. Returns:
  659. The escaped string.
  660. """
  661. s = s.replace("%", "%%")
  662. return s
  663. quote_replacer_regex = re.compile(r'(\\*)"')
  664. def _EscapeCommandLineArgumentForMSVS(s):
  665. """Escapes a Windows command-line argument.
  666. So that the Win32 CommandLineToArgv function will turn the escaped result back
  667. into the original string.
  668. See http://msdn.microsoft.com/en-us/library/17w5ykft.aspx
  669. ("Parsing C++ Command-Line Arguments") to understand why we have to do
  670. this.
  671. Args:
  672. s: the string to be escaped.
  673. Returns:
  674. the escaped string.
  675. """
  676. def _Replace(match):
  677. # For a literal quote, CommandLineToArgv requires an odd number of
  678. # backslashes preceding it, and it produces half as many literal backslashes
  679. # (rounded down). So we need to produce 2n+1 backslashes.
  680. return 2 * match.group(1) + '\\"'
  681. # Escape all quotes so that they are interpreted literally.
  682. s = quote_replacer_regex.sub(_Replace, s)
  683. # Now add unescaped quotes so that any whitespace is interpreted literally.
  684. s = '"' + s + '"'
  685. return s
  686. delimiters_replacer_regex = re.compile(r"(\\*)([,;]+)")
  687. def _EscapeVCProjCommandLineArgListItem(s):
  688. """Escapes command line arguments for MSVS.
  689. The VCProj format stores string lists in a single string using commas and
  690. semi-colons as separators, which must be quoted if they are to be
  691. interpreted literally. However, command-line arguments may already have
  692. quotes, and the VCProj parser is ignorant of the backslash escaping
  693. convention used by CommandLineToArgv, so the command-line quotes and the
  694. VCProj quotes may not be the same quotes. So to store a general
  695. command-line argument in a VCProj list, we need to parse the existing
  696. quoting according to VCProj's convention and quote any delimiters that are
  697. not already quoted by that convention. The quotes that we add will also be
  698. seen by CommandLineToArgv, so if backslashes precede them then we also have
  699. to escape those backslashes according to the CommandLineToArgv
  700. convention.
  701. Args:
  702. s: the string to be escaped.
  703. Returns:
  704. the escaped string.
  705. """
  706. def _Replace(match):
  707. # For a non-literal quote, CommandLineToArgv requires an even number of
  708. # backslashes preceding it, and it produces half as many literal
  709. # backslashes. So we need to produce 2n backslashes.
  710. return 2 * match.group(1) + '"' + match.group(2) + '"'
  711. segments = s.split('"')
  712. # The unquoted segments are at the even-numbered indices.
  713. for i in range(0, len(segments), 2):
  714. segments[i] = delimiters_replacer_regex.sub(_Replace, segments[i])
  715. # Concatenate back into a single string
  716. s = '"'.join(segments)
  717. if len(segments) % 2 == 0:
  718. # String ends while still quoted according to VCProj's convention. This
  719. # means the delimiter and the next list item that follow this one in the
  720. # .vcproj file will be misinterpreted as part of this item. There is nothing
  721. # we can do about this. Adding an extra quote would correct the problem in
  722. # the VCProj but cause the same problem on the final command-line. Moving
  723. # the item to the end of the list does works, but that's only possible if
  724. # there's only one such item. Let's just warn the user.
  725. print(
  726. "Warning: MSVS may misinterpret the odd number of " + "quotes in " + s,
  727. file=sys.stderr,
  728. )
  729. return s
  730. def _EscapeCppDefineForMSVS(s):
  731. """Escapes a CPP define so that it will reach the compiler unaltered."""
  732. s = _EscapeEnvironmentVariableExpansion(s)
  733. s = _EscapeCommandLineArgumentForMSVS(s)
  734. s = _EscapeVCProjCommandLineArgListItem(s)
  735. # cl.exe replaces literal # characters with = in preprocessor definitions for
  736. # some reason. Octal-encode to work around that.
  737. s = s.replace("#", "\\%03o" % ord("#"))
  738. return s
  739. quote_replacer_regex2 = re.compile(r'(\\+)"')
  740. def _EscapeCommandLineArgumentForMSBuild(s):
  741. """Escapes a Windows command-line argument for use by MSBuild."""
  742. def _Replace(match):
  743. return (len(match.group(1)) / 2 * 4) * "\\" + '\\"'
  744. # Escape all quotes so that they are interpreted literally.
  745. s = quote_replacer_regex2.sub(_Replace, s)
  746. return s
  747. def _EscapeMSBuildSpecialCharacters(s):
  748. escape_dictionary = {
  749. "%": "%25",
  750. "$": "%24",
  751. "@": "%40",
  752. "'": "%27",
  753. ";": "%3B",
  754. "?": "%3F",
  755. "*": "%2A",
  756. }
  757. result = "".join([escape_dictionary.get(c, c) for c in s])
  758. return result
  759. def _EscapeCppDefineForMSBuild(s):
  760. """Escapes a CPP define so that it will reach the compiler unaltered."""
  761. s = _EscapeEnvironmentVariableExpansion(s)
  762. s = _EscapeCommandLineArgumentForMSBuild(s)
  763. s = _EscapeMSBuildSpecialCharacters(s)
  764. # cl.exe replaces literal # characters with = in preprocessor definitions for
  765. # some reason. Octal-encode to work around that.
  766. s = s.replace("#", "\\%03o" % ord("#"))
  767. return s
  768. def _GenerateRulesForMSVS(
  769. p, output_dir, options, spec, sources, excluded_sources, actions_to_add
  770. ):
  771. """Generate all the rules for a particular project.
  772. Arguments:
  773. p: the project
  774. output_dir: directory to emit rules to
  775. options: global options passed to the generator
  776. spec: the specification for this project
  777. sources: the set of all known source files in this project
  778. excluded_sources: the set of sources excluded from normal processing
  779. actions_to_add: deferred list of actions to add in
  780. """
  781. rules = spec.get("rules", [])
  782. rules_native = [r for r in rules if not int(r.get("msvs_external_rule", 0))]
  783. rules_external = [r for r in rules if int(r.get("msvs_external_rule", 0))]
  784. # Handle rules that use a native rules file.
  785. if rules_native:
  786. _GenerateNativeRulesForMSVS(p, rules_native, output_dir, spec, options)
  787. # Handle external rules (non-native rules).
  788. if rules_external:
  789. _GenerateExternalRules(
  790. rules_external, output_dir, spec, sources, options, actions_to_add
  791. )
  792. _AdjustSourcesForRules(rules, sources, excluded_sources, False)
  793. def _AdjustSourcesForRules(rules, sources, excluded_sources, is_msbuild):
  794. # Add outputs generated by each rule (if applicable).
  795. for rule in rules:
  796. # Add in the outputs from this rule.
  797. trigger_files = _FindRuleTriggerFiles(rule, sources)
  798. for trigger_file in trigger_files:
  799. # Remove trigger_file from excluded_sources to let the rule be triggered
  800. # (e.g. rule trigger ax_enums.idl is added to excluded_sources
  801. # because it's also in an action's inputs in the same project)
  802. excluded_sources.discard(_FixPath(trigger_file))
  803. # Done if not processing outputs as sources.
  804. if int(rule.get("process_outputs_as_sources", False)):
  805. inputs, outputs = _RuleInputsAndOutputs(rule, trigger_file)
  806. inputs = OrderedSet(_FixPaths(inputs))
  807. outputs = OrderedSet(_FixPaths(outputs))
  808. inputs.remove(_FixPath(trigger_file))
  809. sources.update(inputs)
  810. if not is_msbuild:
  811. excluded_sources.update(inputs)
  812. sources.update(outputs)
  813. def _FilterActionsFromExcluded(excluded_sources, actions_to_add):
  814. """Take inputs with actions attached out of the list of exclusions.
  815. Arguments:
  816. excluded_sources: list of source files not to be built.
  817. actions_to_add: dict of actions keyed on source file they're attached to.
  818. Returns:
  819. excluded_sources with files that have actions attached removed.
  820. """
  821. must_keep = OrderedSet(_FixPaths(actions_to_add.keys()))
  822. return [s for s in excluded_sources if s not in must_keep]
  823. def _GetDefaultConfiguration(spec):
  824. return spec["configurations"][spec["default_configuration"]]
  825. def _GetGuidOfProject(proj_path, spec):
  826. """Get the guid for the project.
  827. Arguments:
  828. proj_path: Path of the vcproj or vcxproj file to generate.
  829. spec: The target dictionary containing the properties of the target.
  830. Returns:
  831. the guid.
  832. Raises:
  833. ValueError: if the specified GUID is invalid.
  834. """
  835. # Pluck out the default configuration.
  836. default_config = _GetDefaultConfiguration(spec)
  837. # Decide the guid of the project.
  838. guid = default_config.get("msvs_guid")
  839. if guid:
  840. if VALID_MSVS_GUID_CHARS.match(guid) is None:
  841. raise ValueError(
  842. 'Invalid MSVS guid: "%s". Must match regex: "%s".'
  843. % (guid, VALID_MSVS_GUID_CHARS.pattern)
  844. )
  845. guid = "{%s}" % guid
  846. guid = guid or MSVSNew.MakeGuid(proj_path)
  847. return guid
  848. def _GetMsbuildToolsetOfProject(proj_path, spec, version):
  849. """Get the platform toolset for the project.
  850. Arguments:
  851. proj_path: Path of the vcproj or vcxproj file to generate.
  852. spec: The target dictionary containing the properties of the target.
  853. version: The MSVSVersion object.
  854. Returns:
  855. the platform toolset string or None.
  856. """
  857. # Pluck out the default configuration.
  858. default_config = _GetDefaultConfiguration(spec)
  859. toolset = default_config.get("msbuild_toolset")
  860. if not toolset and version.DefaultToolset():
  861. toolset = version.DefaultToolset()
  862. if spec["type"] == "windows_driver":
  863. toolset = "WindowsKernelModeDriver10.0"
  864. return toolset
  865. def _GenerateProject(project, options, version, generator_flags, spec):
  866. """Generates a vcproj file.
  867. Arguments:
  868. project: the MSVSProject object.
  869. options: global generator options.
  870. version: the MSVSVersion object.
  871. generator_flags: dict of generator-specific flags.
  872. Returns:
  873. A list of source files that cannot be found on disk.
  874. """
  875. default_config = _GetDefaultConfiguration(project.spec)
  876. # Skip emitting anything if told to with msvs_existing_vcproj option.
  877. if default_config.get("msvs_existing_vcproj"):
  878. return []
  879. if version.UsesVcxproj():
  880. return _GenerateMSBuildProject(project, options, version, generator_flags, spec)
  881. else:
  882. return _GenerateMSVSProject(project, options, version, generator_flags)
  883. def _GenerateMSVSProject(project, options, version, generator_flags):
  884. """Generates a .vcproj file. It may create .rules and .user files too.
  885. Arguments:
  886. project: The project object we will generate the file for.
  887. options: Global options passed to the generator.
  888. version: The VisualStudioVersion object.
  889. generator_flags: dict of generator-specific flags.
  890. """
  891. spec = project.spec
  892. gyp.common.EnsureDirExists(project.path)
  893. platforms = _GetUniquePlatforms(spec)
  894. p = MSVSProject.Writer(
  895. project.path, version, spec["target_name"], project.guid, platforms
  896. )
  897. # Get directory project file is in.
  898. project_dir = os.path.split(project.path)[0]
  899. gyp_path = _NormalizedSource(project.build_file)
  900. relative_path_of_gyp_file = gyp.common.RelativePath(gyp_path, project_dir)
  901. config_type = _GetMSVSConfigurationType(spec, project.build_file)
  902. for config_name, config in spec["configurations"].items():
  903. _AddConfigurationToMSVSProject(p, spec, config_type, config_name, config)
  904. # Prepare list of sources and excluded sources.
  905. gyp_file = os.path.split(project.build_file)[1]
  906. sources, excluded_sources = _PrepareListOfSources(spec, generator_flags, gyp_file)
  907. # Add rules.
  908. actions_to_add = {}
  909. _GenerateRulesForMSVS(
  910. p, project_dir, options, spec, sources, excluded_sources, actions_to_add
  911. )
  912. list_excluded = generator_flags.get("msvs_list_excluded_files", True)
  913. sources, excluded_sources, excluded_idl = _AdjustSourcesAndConvertToFilterHierarchy(
  914. spec, options, project_dir, sources, excluded_sources, list_excluded, version
  915. )
  916. # Add in files.
  917. missing_sources = _VerifySourcesExist(sources, project_dir)
  918. p.AddFiles(sources)
  919. _AddToolFilesToMSVS(p, spec)
  920. _HandlePreCompiledHeaders(p, sources, spec)
  921. _AddActions(actions_to_add, spec, relative_path_of_gyp_file)
  922. _AddCopies(actions_to_add, spec)
  923. _WriteMSVSUserFile(project.path, version, spec)
  924. # NOTE: this stanza must appear after all actions have been decided.
  925. # Don't excluded sources with actions attached, or they won't run.
  926. excluded_sources = _FilterActionsFromExcluded(excluded_sources, actions_to_add)
  927. _ExcludeFilesFromBeingBuilt(p, spec, excluded_sources, excluded_idl, list_excluded)
  928. _AddAccumulatedActionsToMSVS(p, spec, actions_to_add)
  929. # Write it out.
  930. p.WriteIfChanged()
  931. return missing_sources
  932. def _GetUniquePlatforms(spec):
  933. """Returns the list of unique platforms for this spec, e.g ['win32', ...].
  934. Arguments:
  935. spec: The target dictionary containing the properties of the target.
  936. Returns:
  937. The MSVSUserFile object created.
  938. """
  939. # Gather list of unique platforms.
  940. platforms = OrderedSet()
  941. for configuration in spec["configurations"]:
  942. platforms.add(_ConfigPlatform(spec["configurations"][configuration]))
  943. platforms = list(platforms)
  944. return platforms
  945. def _CreateMSVSUserFile(proj_path, version, spec):
  946. """Generates a .user file for the user running this Gyp program.
  947. Arguments:
  948. proj_path: The path of the project file being created. The .user file
  949. shares the same path (with an appropriate suffix).
  950. version: The VisualStudioVersion object.
  951. spec: The target dictionary containing the properties of the target.
  952. Returns:
  953. The MSVSUserFile object created.
  954. """
  955. (domain, username) = _GetDomainAndUserName()
  956. vcuser_filename = ".".join([proj_path, domain, username, "user"])
  957. user_file = MSVSUserFile.Writer(vcuser_filename, version, spec["target_name"])
  958. return user_file
  959. def _GetMSVSConfigurationType(spec, build_file):
  960. """Returns the configuration type for this project.
  961. It's a number defined by Microsoft. May raise an exception.
  962. Args:
  963. spec: The target dictionary containing the properties of the target.
  964. build_file: The path of the gyp file.
  965. Returns:
  966. An integer, the configuration type.
  967. """
  968. try:
  969. config_type = {
  970. "executable": "1", # .exe
  971. "shared_library": "2", # .dll
  972. "loadable_module": "2", # .dll
  973. "static_library": "4", # .lib
  974. "windows_driver": "5", # .sys
  975. "none": "10", # Utility type
  976. }[spec["type"]]
  977. except KeyError:
  978. if spec.get("type"):
  979. raise GypError(
  980. "Target type %s is not a valid target type for "
  981. "target %s in %s." % (spec["type"], spec["target_name"], build_file)
  982. )
  983. else:
  984. raise GypError(
  985. "Missing type field for target %s in %s."
  986. % (spec["target_name"], build_file)
  987. )
  988. return config_type
  989. def _AddConfigurationToMSVSProject(p, spec, config_type, config_name, config):
  990. """Adds a configuration to the MSVS project.
  991. Many settings in a vcproj file are specific to a configuration. This
  992. function the main part of the vcproj file that's configuration specific.
  993. Arguments:
  994. p: The target project being generated.
  995. spec: The target dictionary containing the properties of the target.
  996. config_type: The configuration type, a number as defined by Microsoft.
  997. config_name: The name of the configuration.
  998. config: The dictionary that defines the special processing to be done
  999. for this configuration.
  1000. """
  1001. # Get the information for this configuration
  1002. include_dirs, midl_include_dirs, resource_include_dirs = _GetIncludeDirs(config)
  1003. libraries = _GetLibraries(spec)
  1004. library_dirs = _GetLibraryDirs(config)
  1005. out_file, vc_tool, _ = _GetOutputFilePathAndTool(spec, msbuild=False)
  1006. defines = _GetDefines(config)
  1007. defines = [_EscapeCppDefineForMSVS(d) for d in defines]
  1008. disabled_warnings = _GetDisabledWarnings(config)
  1009. prebuild = config.get("msvs_prebuild")
  1010. postbuild = config.get("msvs_postbuild")
  1011. def_file = _GetModuleDefinition(spec)
  1012. precompiled_header = config.get("msvs_precompiled_header")
  1013. # Prepare the list of tools as a dictionary.
  1014. tools = {}
  1015. # Add in user specified msvs_settings.
  1016. msvs_settings = config.get("msvs_settings", {})
  1017. MSVSSettings.ValidateMSVSSettings(msvs_settings)
  1018. # Prevent default library inheritance from the environment.
  1019. _ToolAppend(tools, "VCLinkerTool", "AdditionalDependencies", ["$(NOINHERIT)"])
  1020. for tool in msvs_settings:
  1021. settings = config["msvs_settings"][tool]
  1022. for setting in settings:
  1023. _ToolAppend(tools, tool, setting, settings[setting])
  1024. # Add the information to the appropriate tool
  1025. _ToolAppend(tools, "VCCLCompilerTool", "AdditionalIncludeDirectories", include_dirs)
  1026. _ToolAppend(tools, "VCMIDLTool", "AdditionalIncludeDirectories", midl_include_dirs)
  1027. _ToolAppend(
  1028. tools,
  1029. "VCResourceCompilerTool",
  1030. "AdditionalIncludeDirectories",
  1031. resource_include_dirs,
  1032. )
  1033. # Add in libraries.
  1034. _ToolAppend(tools, "VCLinkerTool", "AdditionalDependencies", libraries)
  1035. _ToolAppend(tools, "VCLinkerTool", "AdditionalLibraryDirectories", library_dirs)
  1036. if out_file:
  1037. _ToolAppend(tools, vc_tool, "OutputFile", out_file, only_if_unset=True)
  1038. # Add defines.
  1039. _ToolAppend(tools, "VCCLCompilerTool", "PreprocessorDefinitions", defines)
  1040. _ToolAppend(tools, "VCResourceCompilerTool", "PreprocessorDefinitions", defines)
  1041. # Change program database directory to prevent collisions.
  1042. _ToolAppend(
  1043. tools,
  1044. "VCCLCompilerTool",
  1045. "ProgramDataBaseFileName",
  1046. "$(IntDir)$(ProjectName)\\vc80.pdb",
  1047. only_if_unset=True,
  1048. )
  1049. # Add disabled warnings.
  1050. _ToolAppend(tools, "VCCLCompilerTool", "DisableSpecificWarnings", disabled_warnings)
  1051. # Add Pre-build.
  1052. _ToolAppend(tools, "VCPreBuildEventTool", "CommandLine", prebuild)
  1053. # Add Post-build.
  1054. _ToolAppend(tools, "VCPostBuildEventTool", "CommandLine", postbuild)
  1055. # Turn on precompiled headers if appropriate.
  1056. if precompiled_header:
  1057. precompiled_header = os.path.split(precompiled_header)[1]
  1058. _ToolAppend(tools, "VCCLCompilerTool", "UsePrecompiledHeader", "2")
  1059. _ToolAppend(
  1060. tools, "VCCLCompilerTool", "PrecompiledHeaderThrough", precompiled_header
  1061. )
  1062. _ToolAppend(tools, "VCCLCompilerTool", "ForcedIncludeFiles", precompiled_header)
  1063. # Loadable modules don't generate import libraries;
  1064. # tell dependent projects to not expect one.
  1065. if spec["type"] == "loadable_module":
  1066. _ToolAppend(tools, "VCLinkerTool", "IgnoreImportLibrary", "true")
  1067. # Set the module definition file if any.
  1068. if def_file:
  1069. _ToolAppend(tools, "VCLinkerTool", "ModuleDefinitionFile", def_file)
  1070. _AddConfigurationToMSVS(p, spec, tools, config, config_type, config_name)
  1071. def _GetIncludeDirs(config):
  1072. """Returns the list of directories to be used for #include directives.
  1073. Arguments:
  1074. config: The dictionary that defines the special processing to be done
  1075. for this configuration.
  1076. Returns:
  1077. The list of directory paths.
  1078. """
  1079. # TODO(bradnelson): include_dirs should really be flexible enough not to
  1080. # require this sort of thing.
  1081. include_dirs = config.get("include_dirs", []) + config.get(
  1082. "msvs_system_include_dirs", []
  1083. )
  1084. midl_include_dirs = config.get("midl_include_dirs", []) + config.get(
  1085. "msvs_system_include_dirs", []
  1086. )
  1087. resource_include_dirs = config.get("resource_include_dirs", include_dirs)
  1088. include_dirs = _FixPaths(include_dirs)
  1089. midl_include_dirs = _FixPaths(midl_include_dirs)
  1090. resource_include_dirs = _FixPaths(resource_include_dirs)
  1091. return include_dirs, midl_include_dirs, resource_include_dirs
  1092. def _GetLibraryDirs(config):
  1093. """Returns the list of directories to be used for library search paths.
  1094. Arguments:
  1095. config: The dictionary that defines the special processing to be done
  1096. for this configuration.
  1097. Returns:
  1098. The list of directory paths.
  1099. """
  1100. library_dirs = config.get("library_dirs", [])
  1101. library_dirs = _FixPaths(library_dirs)
  1102. return library_dirs
  1103. def _GetLibraries(spec):
  1104. """Returns the list of libraries for this configuration.
  1105. Arguments:
  1106. spec: The target dictionary containing the properties of the target.
  1107. Returns:
  1108. The list of directory paths.
  1109. """
  1110. libraries = spec.get("libraries", [])
  1111. # Strip out -l, as it is not used on windows (but is needed so we can pass
  1112. # in libraries that are assumed to be in the default library path).
  1113. # Also remove duplicate entries, leaving only the last duplicate, while
  1114. # preserving order.
  1115. found = OrderedSet()
  1116. unique_libraries_list = []
  1117. for entry in reversed(libraries):
  1118. library = re.sub(r"^\-l", "", entry)
  1119. if not os.path.splitext(library)[1]:
  1120. library += ".lib"
  1121. if library not in found:
  1122. found.add(library)
  1123. unique_libraries_list.append(library)
  1124. unique_libraries_list.reverse()
  1125. return unique_libraries_list
  1126. def _GetOutputFilePathAndTool(spec, msbuild):
  1127. """Returns the path and tool to use for this target.
  1128. Figures out the path of the file this spec will create and the name of
  1129. the VC tool that will create it.
  1130. Arguments:
  1131. spec: The target dictionary containing the properties of the target.
  1132. Returns:
  1133. A triple of (file path, name of the vc tool, name of the msbuild tool)
  1134. """
  1135. # Select a name for the output file.
  1136. out_file = ""
  1137. vc_tool = ""
  1138. msbuild_tool = ""
  1139. output_file_map = {
  1140. "executable": ("VCLinkerTool", "Link", "$(OutDir)", ".exe"),
  1141. "shared_library": ("VCLinkerTool", "Link", "$(OutDir)", ".dll"),
  1142. "loadable_module": ("VCLinkerTool", "Link", "$(OutDir)", ".dll"),
  1143. "windows_driver": ("VCLinkerTool", "Link", "$(OutDir)", ".sys"),
  1144. "static_library": ("VCLibrarianTool", "Lib", "$(OutDir)lib\\", ".lib"),
  1145. }
  1146. output_file_props = output_file_map.get(spec["type"])
  1147. if output_file_props and int(spec.get("msvs_auto_output_file", 1)):
  1148. vc_tool, msbuild_tool, out_dir, suffix = output_file_props
  1149. if spec.get("standalone_static_library", 0):
  1150. out_dir = "$(OutDir)"
  1151. out_dir = spec.get("product_dir", out_dir)
  1152. product_extension = spec.get("product_extension")
  1153. if product_extension:
  1154. suffix = "." + product_extension
  1155. elif msbuild:
  1156. suffix = "$(TargetExt)"
  1157. prefix = spec.get("product_prefix", "")
  1158. product_name = spec.get("product_name", "$(ProjectName)")
  1159. out_file = ntpath.join(out_dir, prefix + product_name + suffix)
  1160. return out_file, vc_tool, msbuild_tool
  1161. def _GetOutputTargetExt(spec):
  1162. """Returns the extension for this target, including the dot
  1163. If product_extension is specified, set target_extension to this to avoid
  1164. MSB8012, returns None otherwise. Ignores any target_extension settings in
  1165. the input files.
  1166. Arguments:
  1167. spec: The target dictionary containing the properties of the target.
  1168. Returns:
  1169. A string with the extension, or None
  1170. """
  1171. target_extension = spec.get("product_extension")
  1172. if target_extension:
  1173. return "." + target_extension
  1174. return None
  1175. def _GetDefines(config):
  1176. """Returns the list of preprocessor definitions for this configuration.
  1177. Arguments:
  1178. config: The dictionary that defines the special processing to be done
  1179. for this configuration.
  1180. Returns:
  1181. The list of preprocessor definitions.
  1182. """
  1183. defines = []
  1184. for d in config.get("defines", []):
  1185. fd = "=".join([str(dpart) for dpart in d]) if isinstance(d, list) else str(d)
  1186. defines.append(fd)
  1187. return defines
  1188. def _GetDisabledWarnings(config):
  1189. return [str(i) for i in config.get("msvs_disabled_warnings", [])]
  1190. def _GetModuleDefinition(spec):
  1191. def_file = ""
  1192. if spec["type"] in [
  1193. "shared_library",
  1194. "loadable_module",
  1195. "executable",
  1196. "windows_driver",
  1197. ]:
  1198. def_files = [s for s in spec.get("sources", []) if s.endswith(".def")]
  1199. if len(def_files) == 1:
  1200. def_file = _FixPath(def_files[0])
  1201. elif def_files:
  1202. raise ValueError(
  1203. "Multiple module definition files in one target, target %s lists "
  1204. "multiple .def files: %s" % (spec["target_name"], " ".join(def_files))
  1205. )
  1206. return def_file
  1207. def _ConvertToolsToExpectedForm(tools):
  1208. """Convert tools to a form expected by Visual Studio.
  1209. Arguments:
  1210. tools: A dictionary of settings; the tool name is the key.
  1211. Returns:
  1212. A list of Tool objects.
  1213. """
  1214. tool_list = []
  1215. for tool, settings in tools.items():
  1216. # Collapse settings with lists.
  1217. settings_fixed = {}
  1218. for setting, value in settings.items():
  1219. if isinstance(value, list):
  1220. if (
  1221. tool == "VCLinkerTool" and setting == "AdditionalDependencies"
  1222. ) or setting == "AdditionalOptions":
  1223. settings_fixed[setting] = " ".join(value)
  1224. else:
  1225. settings_fixed[setting] = ";".join(value)
  1226. else:
  1227. settings_fixed[setting] = value
  1228. # Add in this tool.
  1229. tool_list.append(MSVSProject.Tool(tool, settings_fixed))
  1230. return tool_list
  1231. def _AddConfigurationToMSVS(p, spec, tools, config, config_type, config_name):
  1232. """Add to the project file the configuration specified by config.
  1233. Arguments:
  1234. p: The target project being generated.
  1235. spec: the target project dict.
  1236. tools: A dictionary of settings; the tool name is the key.
  1237. config: The dictionary that defines the special processing to be done
  1238. for this configuration.
  1239. config_type: The configuration type, a number as defined by Microsoft.
  1240. config_name: The name of the configuration.
  1241. """
  1242. attributes = _GetMSVSAttributes(spec, config, config_type)
  1243. # Add in this configuration.
  1244. tool_list = _ConvertToolsToExpectedForm(tools)
  1245. p.AddConfig(_ConfigFullName(config_name, config), attrs=attributes, tools=tool_list)
  1246. def _GetMSVSAttributes(spec, config, config_type):
  1247. # Prepare configuration attributes.
  1248. prepared_attrs = {}
  1249. source_attrs = config.get("msvs_configuration_attributes", {})
  1250. for a in source_attrs:
  1251. prepared_attrs[a] = source_attrs[a]
  1252. # Add props files.
  1253. vsprops_dirs = config.get("msvs_props", [])
  1254. vsprops_dirs = _FixPaths(vsprops_dirs)
  1255. if vsprops_dirs:
  1256. prepared_attrs["InheritedPropertySheets"] = ";".join(vsprops_dirs)
  1257. # Set configuration type.
  1258. prepared_attrs["ConfigurationType"] = config_type
  1259. output_dir = prepared_attrs.get(
  1260. "OutputDirectory", "$(SolutionDir)$(ConfigurationName)"
  1261. )
  1262. prepared_attrs["OutputDirectory"] = _FixPath(output_dir) + "\\"
  1263. if "IntermediateDirectory" not in prepared_attrs:
  1264. intermediate = "$(ConfigurationName)\\obj\\$(ProjectName)"
  1265. prepared_attrs["IntermediateDirectory"] = _FixPath(intermediate) + "\\"
  1266. else:
  1267. intermediate = _FixPath(prepared_attrs["IntermediateDirectory"]) + "\\"
  1268. intermediate = MSVSSettings.FixVCMacroSlashes(intermediate)
  1269. prepared_attrs["IntermediateDirectory"] = intermediate
  1270. return prepared_attrs
  1271. def _AddNormalizedSources(sources_set, sources_array):
  1272. sources_set.update(_NormalizedSource(s) for s in sources_array)
  1273. def _PrepareListOfSources(spec, generator_flags, gyp_file):
  1274. """Prepare list of sources and excluded sources.
  1275. Besides the sources specified directly in the spec, adds the gyp file so
  1276. that a change to it will cause a re-compile. Also adds appropriate sources
  1277. for actions and copies. Assumes later stage will un-exclude files which
  1278. have custom build steps attached.
  1279. Arguments:
  1280. spec: The target dictionary containing the properties of the target.
  1281. gyp_file: The name of the gyp file.
  1282. Returns:
  1283. A pair of (list of sources, list of excluded sources).
  1284. The sources will be relative to the gyp file.
  1285. """
  1286. sources = OrderedSet()
  1287. _AddNormalizedSources(sources, spec.get("sources", []))
  1288. excluded_sources = OrderedSet()
  1289. # Add in the gyp file.
  1290. if not generator_flags.get("standalone"):
  1291. sources.add(gyp_file)
  1292. # Add in 'action' inputs and outputs.
  1293. for a in spec.get("actions", []):
  1294. inputs = a["inputs"]
  1295. inputs = [_NormalizedSource(i) for i in inputs]
  1296. # Add all inputs to sources and excluded sources.
  1297. inputs = OrderedSet(inputs)
  1298. sources.update(inputs)
  1299. if not spec.get("msvs_external_builder"):
  1300. excluded_sources.update(inputs)
  1301. if int(a.get("process_outputs_as_sources", False)):
  1302. _AddNormalizedSources(sources, a.get("outputs", []))
  1303. # Add in 'copies' inputs and outputs.
  1304. for cpy in spec.get("copies", []):
  1305. _AddNormalizedSources(sources, cpy.get("files", []))
  1306. return (sources, excluded_sources)
  1307. def _AdjustSourcesAndConvertToFilterHierarchy(
  1308. spec, options, gyp_dir, sources, excluded_sources, list_excluded, version
  1309. ):
  1310. """Adjusts the list of sources and excluded sources.
  1311. Also converts the sets to lists.
  1312. Arguments:
  1313. spec: The target dictionary containing the properties of the target.
  1314. options: Global generator options.
  1315. gyp_dir: The path to the gyp file being processed.
  1316. sources: A set of sources to be included for this project.
  1317. excluded_sources: A set of sources to be excluded for this project.
  1318. version: A MSVSVersion object.
  1319. Returns:
  1320. A trio of (list of sources, list of excluded sources,
  1321. path of excluded IDL file)
  1322. """
  1323. # Exclude excluded sources coming into the generator.
  1324. excluded_sources.update(OrderedSet(spec.get("sources_excluded", [])))
  1325. # Add excluded sources into sources for good measure.
  1326. sources.update(excluded_sources)
  1327. # Convert to proper windows form.
  1328. # NOTE: sources goes from being a set to a list here.
  1329. # NOTE: excluded_sources goes from being a set to a list here.
  1330. sources = _FixPaths(sources)
  1331. # Convert to proper windows form.
  1332. excluded_sources = _FixPaths(excluded_sources)
  1333. excluded_idl = _IdlFilesHandledNonNatively(spec, sources)
  1334. precompiled_related = _GetPrecompileRelatedFiles(spec)
  1335. # Find the excluded ones, minus the precompiled header related ones.
  1336. fully_excluded = [i for i in excluded_sources if i not in precompiled_related]
  1337. # Convert to folders and the right slashes.
  1338. sources = [i.split("\\") for i in sources]
  1339. sources = _ConvertSourcesToFilterHierarchy(
  1340. sources,
  1341. excluded=fully_excluded,
  1342. list_excluded=list_excluded,
  1343. msvs_version=version,
  1344. )
  1345. # Prune filters with a single child to flatten ugly directory structures
  1346. # such as ../../src/modules/module1 etc.
  1347. if version.UsesVcxproj():
  1348. while (
  1349. all(isinstance(s, MSVSProject.Filter) for s in sources)
  1350. and len({s.name for s in sources}) == 1
  1351. ):
  1352. assert all(len(s.contents) == 1 for s in sources)
  1353. sources = [s.contents[0] for s in sources]
  1354. else:
  1355. while len(sources) == 1 and isinstance(sources[0], MSVSProject.Filter):
  1356. sources = sources[0].contents
  1357. return sources, excluded_sources, excluded_idl
  1358. def _IdlFilesHandledNonNatively(spec, sources):
  1359. # If any non-native rules use 'idl' as an extension exclude idl files.
  1360. # Gather a list here to use later.
  1361. using_idl = False
  1362. for rule in spec.get("rules", []):
  1363. if rule["extension"] == "idl" and int(rule.get("msvs_external_rule", 0)):
  1364. using_idl = True
  1365. break
  1366. excluded_idl = [i for i in sources if i.endswith(".idl")] if using_idl else []
  1367. return excluded_idl
  1368. def _GetPrecompileRelatedFiles(spec):
  1369. # Gather a list of precompiled header related sources.
  1370. precompiled_related = []
  1371. for _, config in spec["configurations"].items():
  1372. for k in precomp_keys:
  1373. f = config.get(k)
  1374. if f:
  1375. precompiled_related.append(_FixPath(f))
  1376. return precompiled_related
  1377. def _ExcludeFilesFromBeingBuilt(p, spec, excluded_sources, excluded_idl, list_excluded):
  1378. exclusions = _GetExcludedFilesFromBuild(spec, excluded_sources, excluded_idl)
  1379. for file_name, excluded_configs in exclusions.items():
  1380. if not list_excluded and len(excluded_configs) == len(spec["configurations"]):
  1381. # If we're not listing excluded files, then they won't appear in the
  1382. # project, so don't try to configure them to be excluded.
  1383. pass
  1384. else:
  1385. for config_name, config in excluded_configs:
  1386. p.AddFileConfig(
  1387. file_name,
  1388. _ConfigFullName(config_name, config),
  1389. {"ExcludedFromBuild": "true"},
  1390. )
  1391. def _GetExcludedFilesFromBuild(spec, excluded_sources, excluded_idl):
  1392. exclusions = {}
  1393. # Exclude excluded sources from being built.
  1394. for f in excluded_sources:
  1395. excluded_configs = []
  1396. for config_name, config in spec["configurations"].items():
  1397. precomped = [_FixPath(config.get(i, "")) for i in precomp_keys]
  1398. # Don't do this for ones that are precompiled header related.
  1399. if f not in precomped:
  1400. excluded_configs.append((config_name, config))
  1401. exclusions[f] = excluded_configs
  1402. # If any non-native rules use 'idl' as an extension exclude idl files.
  1403. # Exclude them now.
  1404. for f in excluded_idl:
  1405. excluded_configs = []
  1406. for config_name, config in spec["configurations"].items():
  1407. excluded_configs.append((config_name, config))
  1408. exclusions[f] = excluded_configs
  1409. return exclusions
  1410. def _AddToolFilesToMSVS(p, spec):
  1411. # Add in tool files (rules).
  1412. tool_files = OrderedSet()
  1413. for _, config in spec["configurations"].items():
  1414. for f in config.get("msvs_tool_files", []):
  1415. tool_files.add(f)
  1416. for f in tool_files:
  1417. p.AddToolFile(f)
  1418. def _HandlePreCompiledHeaders(p, sources, spec):
  1419. # Pre-compiled header source stubs need a different compiler flag
  1420. # (generate precompiled header) and any source file not of the same
  1421. # kind (i.e. C vs. C++) as the precompiled header source stub needs
  1422. # to have use of precompiled headers disabled.
  1423. extensions_excluded_from_precompile = []
  1424. for config_name, config in spec["configurations"].items():
  1425. source = config.get("msvs_precompiled_source")
  1426. if source:
  1427. source = _FixPath(source)
  1428. # UsePrecompiledHeader=1 for if using precompiled headers.
  1429. tool = MSVSProject.Tool("VCCLCompilerTool", {"UsePrecompiledHeader": "1"})
  1430. p.AddFileConfig(
  1431. source, _ConfigFullName(config_name, config), {}, tools=[tool]
  1432. )
  1433. basename, extension = os.path.splitext(source)
  1434. if extension == ".c":
  1435. extensions_excluded_from_precompile = [".cc", ".cpp", ".cxx"]
  1436. else:
  1437. extensions_excluded_from_precompile = [".c"]
  1438. def DisableForSourceTree(source_tree):
  1439. for source in source_tree:
  1440. if isinstance(source, MSVSProject.Filter):
  1441. DisableForSourceTree(source.contents)
  1442. else:
  1443. basename, extension = os.path.splitext(source)
  1444. if extension in extensions_excluded_from_precompile:
  1445. for config_name, config in spec["configurations"].items():
  1446. tool = MSVSProject.Tool(
  1447. "VCCLCompilerTool",
  1448. {
  1449. "UsePrecompiledHeader": "0",
  1450. "ForcedIncludeFiles": "$(NOINHERIT)",
  1451. },
  1452. )
  1453. p.AddFileConfig(
  1454. _FixPath(source),
  1455. _ConfigFullName(config_name, config),
  1456. {},
  1457. tools=[tool],
  1458. )
  1459. # Do nothing if there was no precompiled source.
  1460. if extensions_excluded_from_precompile:
  1461. DisableForSourceTree(sources)
  1462. def _AddActions(actions_to_add, spec, relative_path_of_gyp_file):
  1463. # Add actions.
  1464. actions = spec.get("actions", [])
  1465. # Don't setup_env every time. When all the actions are run together in one
  1466. # batch file in VS, the PATH will grow too long.
  1467. # Membership in this set means that the cygwin environment has been set up,
  1468. # and does not need to be set up again.
  1469. have_setup_env = set()
  1470. for a in actions:
  1471. # Attach actions to the gyp file if nothing else is there.
  1472. inputs = a.get("inputs") or [relative_path_of_gyp_file]
  1473. attached_to = inputs[0]
  1474. need_setup_env = attached_to not in have_setup_env
  1475. cmd = _BuildCommandLineForRule(
  1476. spec, a, has_input_path=False, do_setup_env=need_setup_env
  1477. )
  1478. have_setup_env.add(attached_to)
  1479. # Add the action.
  1480. _AddActionStep(
  1481. actions_to_add,
  1482. inputs=inputs,
  1483. outputs=a.get("outputs", []),
  1484. description=a.get("message", a["action_name"]),
  1485. command=cmd,
  1486. )
  1487. def _WriteMSVSUserFile(project_path, version, spec):
  1488. # Add run_as and test targets.
  1489. if "run_as" in spec:
  1490. run_as = spec["run_as"]
  1491. action = run_as.get("action", [])
  1492. environment = run_as.get("environment", [])
  1493. working_directory = run_as.get("working_directory", ".")
  1494. elif int(spec.get("test", 0)):
  1495. action = ["$(TargetPath)", "--gtest_print_time"]
  1496. environment = []
  1497. working_directory = "."
  1498. else:
  1499. return # Nothing to add
  1500. # Write out the user file.
  1501. user_file = _CreateMSVSUserFile(project_path, version, spec)
  1502. for config_name, c_data in spec["configurations"].items():
  1503. user_file.AddDebugSettings(
  1504. _ConfigFullName(config_name, c_data), action, environment, working_directory
  1505. )
  1506. user_file.WriteIfChanged()
  1507. def _AddCopies(actions_to_add, spec):
  1508. copies = _GetCopies(spec)
  1509. for inputs, outputs, cmd, description in copies:
  1510. _AddActionStep(
  1511. actions_to_add,
  1512. inputs=inputs,
  1513. outputs=outputs,
  1514. description=description,
  1515. command=cmd,
  1516. )
  1517. def _GetCopies(spec):
  1518. copies = []
  1519. # Add copies.
  1520. for cpy in spec.get("copies", []):
  1521. for src in cpy.get("files", []):
  1522. dst = os.path.join(cpy["destination"], os.path.basename(src))
  1523. # _AddCustomBuildToolForMSVS() will call _FixPath() on the inputs and
  1524. # outputs, so do the same for our generated command line.
  1525. if src.endswith("/"):
  1526. src_bare = src[:-1]
  1527. base_dir = posixpath.split(src_bare)[0]
  1528. outer_dir = posixpath.split(src_bare)[1]
  1529. fixed_dst = _FixPath(dst)
  1530. full_dst = f'"{fixed_dst}\\{outer_dir}\\"'
  1531. cmd = (
  1532. f'mkdir {full_dst} 2>nul & cd "{_FixPath(base_dir)}" '
  1533. f'&& xcopy /e /f /y "{outer_dir}" {full_dst}'
  1534. )
  1535. copies.append(
  1536. (
  1537. [src],
  1538. ["dummy_copies", dst],
  1539. cmd,
  1540. f"Copying {src} to {fixed_dst}",
  1541. )
  1542. )
  1543. else:
  1544. fix_dst = _FixPath(cpy["destination"])
  1545. cmd = (
  1546. f'mkdir "{fix_dst}" 2>nul & set ERRORLEVEL=0 & '
  1547. f'copy /Y "{_FixPath(src)}" "{_FixPath(dst)}"'
  1548. )
  1549. copies.append(([src], [dst], cmd, f"Copying {src} to {fix_dst}"))
  1550. return copies
  1551. def _GetPathDict(root, path):
  1552. # |path| will eventually be empty (in the recursive calls) if it was initially
  1553. # relative; otherwise it will eventually end up as '\', 'D:\', etc.
  1554. if not path or path.endswith(os.sep):
  1555. return root
  1556. parent, folder = os.path.split(path)
  1557. parent_dict = _GetPathDict(root, parent)
  1558. if folder not in parent_dict:
  1559. parent_dict[folder] = {}
  1560. return parent_dict[folder]
  1561. def _DictsToFolders(base_path, bucket, flat):
  1562. # Convert to folders recursively.
  1563. children = []
  1564. for folder, contents in bucket.items():
  1565. if isinstance(contents, dict):
  1566. folder_children = _DictsToFolders(
  1567. os.path.join(base_path, folder), contents, flat
  1568. )
  1569. if flat:
  1570. children += folder_children
  1571. else:
  1572. folder_children = MSVSNew.MSVSFolder(
  1573. os.path.join(base_path, folder),
  1574. name="(" + folder + ")",
  1575. entries=folder_children,
  1576. )
  1577. children.append(folder_children)
  1578. else:
  1579. children.append(contents)
  1580. return children
  1581. def _CollapseSingles(parent, node):
  1582. # Recursively explorer the tree of dicts looking for projects which are
  1583. # the sole item in a folder which has the same name as the project. Bring
  1584. # such projects up one level.
  1585. if (isinstance(node, dict) and len(node) == 1 and
  1586. next(iter(node)) == parent + ".vcproj"):
  1587. return node[next(iter(node))]
  1588. if not isinstance(node, dict):
  1589. return node
  1590. for child in node:
  1591. node[child] = _CollapseSingles(child, node[child])
  1592. return node
  1593. def _GatherSolutionFolders(sln_projects, project_objects, flat):
  1594. root = {}
  1595. # Convert into a tree of dicts on path.
  1596. for p in sln_projects:
  1597. gyp_file, target = gyp.common.ParseQualifiedTarget(p)[0:2]
  1598. if p.endswith("#host"):
  1599. target += "_host"
  1600. gyp_dir = os.path.dirname(gyp_file)
  1601. path_dict = _GetPathDict(root, gyp_dir)
  1602. path_dict[target + ".vcproj"] = project_objects[p]
  1603. # Walk down from the top until we hit a folder that has more than one entry.
  1604. # In practice, this strips the top-level "src/" dir from the hierarchy in
  1605. # the solution.
  1606. while len(root) == 1 and isinstance(root[next(iter(root))], dict):
  1607. root = root[next(iter(root))]
  1608. # Collapse singles.
  1609. root = _CollapseSingles("", root)
  1610. # Merge buckets until everything is a root entry.
  1611. return _DictsToFolders("", root, flat)
  1612. def _GetPathOfProject(qualified_target, spec, options, msvs_version):
  1613. default_config = _GetDefaultConfiguration(spec)
  1614. proj_filename = default_config.get("msvs_existing_vcproj")
  1615. if not proj_filename:
  1616. proj_filename = spec["target_name"]
  1617. if spec["toolset"] == "host":
  1618. proj_filename += "_host"
  1619. proj_filename = proj_filename + options.suffix + msvs_version.ProjectExtension()
  1620. build_file = gyp.common.BuildFile(qualified_target)
  1621. proj_path = os.path.join(os.path.dirname(build_file), proj_filename)
  1622. fix_prefix = None
  1623. if options.generator_output:
  1624. project_dir_path = os.path.dirname(os.path.abspath(proj_path))
  1625. proj_path = os.path.join(options.generator_output, proj_path)
  1626. fix_prefix = gyp.common.RelativePath(
  1627. project_dir_path, os.path.dirname(proj_path)
  1628. )
  1629. return proj_path, fix_prefix
  1630. def _GetPlatformOverridesOfProject(spec):
  1631. # Prepare a dict indicating which project configurations are used for which
  1632. # solution configurations for this target.
  1633. config_platform_overrides = {}
  1634. for config_name, c in spec["configurations"].items():
  1635. config_fullname = _ConfigFullName(config_name, c)
  1636. platform = c.get("msvs_target_platform", _ConfigPlatform(c))
  1637. base_name = _ConfigBaseName(config_name, _ConfigPlatform(c))
  1638. fixed_config_fullname = f"{base_name}|{platform}"
  1639. if spec["toolset"] == "host" and generator_supports_multiple_toolsets:
  1640. fixed_config_fullname = f"{config_name}|x64"
  1641. config_platform_overrides[config_fullname] = fixed_config_fullname
  1642. return config_platform_overrides
  1643. def _CreateProjectObjects(target_list, target_dicts, options, msvs_version):
  1644. """Create a MSVSProject object for the targets found in target list.
  1645. Arguments:
  1646. target_list: the list of targets to generate project objects for.
  1647. target_dicts: the dictionary of specifications.
  1648. options: global generator options.
  1649. msvs_version: the MSVSVersion object.
  1650. Returns:
  1651. A set of created projects, keyed by target.
  1652. """
  1653. global fixpath_prefix
  1654. # Generate each project.
  1655. projects = {}
  1656. for qualified_target in target_list:
  1657. spec = target_dicts[qualified_target]
  1658. proj_path, fixpath_prefix = _GetPathOfProject(
  1659. qualified_target, spec, options, msvs_version
  1660. )
  1661. guid = _GetGuidOfProject(proj_path, spec)
  1662. overrides = _GetPlatformOverridesOfProject(spec)
  1663. build_file = gyp.common.BuildFile(qualified_target)
  1664. # Create object for this project.
  1665. target_name = spec["target_name"]
  1666. if spec["toolset"] == "host":
  1667. target_name += "_host"
  1668. obj = MSVSNew.MSVSProject(
  1669. proj_path,
  1670. name=target_name,
  1671. guid=guid,
  1672. spec=spec,
  1673. build_file=build_file,
  1674. config_platform_overrides=overrides,
  1675. fixpath_prefix=fixpath_prefix,
  1676. )
  1677. # Set project toolset if any (MS build only)
  1678. if msvs_version.UsesVcxproj():
  1679. obj.set_msbuild_toolset(
  1680. _GetMsbuildToolsetOfProject(proj_path, spec, msvs_version)
  1681. )
  1682. projects[qualified_target] = obj
  1683. # Set all the dependencies, but not if we are using an external builder like
  1684. # ninja
  1685. for project in projects.values():
  1686. if not project.spec.get("msvs_external_builder"):
  1687. deps = project.spec.get("dependencies", [])
  1688. deps = [projects[d] for d in deps]
  1689. project.set_dependencies(deps)
  1690. return projects
  1691. def _InitNinjaFlavor(params, target_list, target_dicts):
  1692. """Initialize targets for the ninja flavor.
  1693. This sets up the necessary variables in the targets to generate msvs projects
  1694. that use ninja as an external builder. The variables in the spec are only set
  1695. if they have not been set. This allows individual specs to override the
  1696. default values initialized here.
  1697. Arguments:
  1698. params: Params provided to the generator.
  1699. target_list: List of target pairs: 'base/base.gyp:base'.
  1700. target_dicts: Dict of target properties keyed on target pair.
  1701. """
  1702. for qualified_target in target_list:
  1703. spec = target_dicts[qualified_target]
  1704. if spec.get("msvs_external_builder"):
  1705. # The spec explicitly defined an external builder, so don't change it.
  1706. continue
  1707. path_to_ninja = spec.get("msvs_path_to_ninja", "ninja.exe")
  1708. spec["msvs_external_builder"] = "ninja"
  1709. if not spec.get("msvs_external_builder_out_dir"):
  1710. gyp_file, _, _ = gyp.common.ParseQualifiedTarget(qualified_target)
  1711. gyp_dir = os.path.dirname(gyp_file)
  1712. configuration = "$(Configuration)"
  1713. if params.get("target_arch") == "x64":
  1714. configuration += "_x64"
  1715. if params.get("target_arch") == "arm64":
  1716. configuration += "_arm64"
  1717. spec["msvs_external_builder_out_dir"] = os.path.join(
  1718. gyp.common.RelativePath(params["options"].toplevel_dir, gyp_dir),
  1719. ninja_generator.ComputeOutputDir(params),
  1720. configuration,
  1721. )
  1722. if not spec.get("msvs_external_builder_build_cmd"):
  1723. spec["msvs_external_builder_build_cmd"] = [
  1724. path_to_ninja,
  1725. "-C",
  1726. "$(OutDir)",
  1727. "$(ProjectName)",
  1728. ]
  1729. if not spec.get("msvs_external_builder_clean_cmd"):
  1730. spec["msvs_external_builder_clean_cmd"] = [
  1731. path_to_ninja,
  1732. "-C",
  1733. "$(OutDir)",
  1734. "-tclean",
  1735. "$(ProjectName)",
  1736. ]
  1737. def CalculateVariables(default_variables, params):
  1738. """Generated variables that require params to be known."""
  1739. generator_flags = params.get("generator_flags", {})
  1740. # Select project file format version (if unset, default to auto detecting).
  1741. msvs_version = MSVSVersion.SelectVisualStudioVersion(
  1742. generator_flags.get("msvs_version", "auto")
  1743. )
  1744. # Stash msvs_version for later (so we don't have to probe the system twice).
  1745. params["msvs_version"] = msvs_version
  1746. # Set a variable so conditions can be based on msvs_version.
  1747. default_variables["MSVS_VERSION"] = msvs_version.ShortName()
  1748. # To determine processor word size on Windows, in addition to checking
  1749. # PROCESSOR_ARCHITECTURE (which reflects the word size of the current
  1750. # process), it is also necessary to check PROCESSOR_ARCITEW6432 (which
  1751. # contains the actual word size of the system when running thru WOW64).
  1752. if (
  1753. os.environ.get("PROCESSOR_ARCHITECTURE", "").find("64") >= 0
  1754. or os.environ.get("PROCESSOR_ARCHITEW6432", "").find("64") >= 0
  1755. ):
  1756. default_variables["MSVS_OS_BITS"] = 64
  1757. else:
  1758. default_variables["MSVS_OS_BITS"] = 32
  1759. if gyp.common.GetFlavor(params) == "ninja":
  1760. default_variables["SHARED_INTERMEDIATE_DIR"] = "$(OutDir)gen"
  1761. def PerformBuild(data, configurations, params):
  1762. options = params["options"]
  1763. msvs_version = params["msvs_version"]
  1764. devenv = os.path.join(msvs_version.path, "Common7", "IDE", "devenv.com")
  1765. for build_file, build_file_dict in data.items():
  1766. (build_file_root, build_file_ext) = os.path.splitext(build_file)
  1767. if build_file_ext != ".gyp":
  1768. continue
  1769. sln_path = build_file_root + options.suffix + ".sln"
  1770. if options.generator_output:
  1771. sln_path = os.path.join(options.generator_output, sln_path)
  1772. for config in configurations:
  1773. arguments = [devenv, sln_path, "/Build", config]
  1774. print(f"Building [{config}]: {arguments}")
  1775. subprocess.check_call(arguments)
  1776. def CalculateGeneratorInputInfo(params):
  1777. if params.get("flavor") == "ninja":
  1778. toplevel = params["options"].toplevel_dir
  1779. qualified_out_dir = os.path.normpath(
  1780. os.path.join(
  1781. toplevel,
  1782. ninja_generator.ComputeOutputDir(params),
  1783. "gypfiles-msvs-ninja",
  1784. )
  1785. )
  1786. global generator_filelist_paths
  1787. generator_filelist_paths = {
  1788. "toplevel": toplevel,
  1789. "qualified_out_dir": qualified_out_dir,
  1790. }
  1791. def GenerateOutput(target_list, target_dicts, data, params):
  1792. """Generate .sln and .vcproj files.
  1793. This is the entry point for this generator.
  1794. Arguments:
  1795. target_list: List of target pairs: 'base/base.gyp:base'.
  1796. target_dicts: Dict of target properties keyed on target pair.
  1797. data: Dictionary containing per .gyp data.
  1798. """
  1799. global fixpath_prefix
  1800. options = params["options"]
  1801. # Get the project file format version back out of where we stashed it in
  1802. # GeneratorCalculatedVariables.
  1803. msvs_version = params["msvs_version"]
  1804. generator_flags = params.get("generator_flags", {})
  1805. # Optionally shard targets marked with 'msvs_shard': SHARD_COUNT.
  1806. (target_list, target_dicts) = MSVSUtil.ShardTargets(target_list, target_dicts)
  1807. # Optionally use the large PDB workaround for targets marked with
  1808. # 'msvs_large_pdb': 1.
  1809. (target_list, target_dicts) = MSVSUtil.InsertLargePdbShims(
  1810. target_list, target_dicts, generator_default_variables
  1811. )
  1812. # Optionally configure each spec to use ninja as the external builder.
  1813. if params.get("flavor") == "ninja":
  1814. _InitNinjaFlavor(params, target_list, target_dicts)
  1815. # Prepare the set of configurations.
  1816. configs = set()
  1817. for qualified_target in target_list:
  1818. spec = target_dicts[qualified_target]
  1819. for config_name, config in spec["configurations"].items():
  1820. config_name = _ConfigFullName(config_name, config)
  1821. configs.add(config_name)
  1822. if config_name == "Release|arm64":
  1823. configs.add("Release|x64")
  1824. configs = list(configs)
  1825. # Figure out all the projects that will be generated and their guids
  1826. project_objects = _CreateProjectObjects(
  1827. target_list, target_dicts, options, msvs_version
  1828. )
  1829. # Generate each project.
  1830. missing_sources = []
  1831. for project in project_objects.values():
  1832. fixpath_prefix = project.fixpath_prefix
  1833. missing_sources.extend(
  1834. _GenerateProject(project, options, msvs_version, generator_flags, spec)
  1835. )
  1836. fixpath_prefix = None
  1837. for build_file in data:
  1838. # Validate build_file extension
  1839. target_only_configs = configs
  1840. if generator_supports_multiple_toolsets:
  1841. target_only_configs = [i for i in configs if i.endswith("arm64")]
  1842. if not build_file.endswith(".gyp"):
  1843. continue
  1844. sln_path = os.path.splitext(build_file)[0] + options.suffix + ".sln"
  1845. if options.generator_output:
  1846. sln_path = os.path.join(options.generator_output, sln_path)
  1847. # Get projects in the solution, and their dependents.
  1848. sln_projects = gyp.common.BuildFileTargets(target_list, build_file)
  1849. sln_projects += gyp.common.DeepDependencyTargets(target_dicts, sln_projects)
  1850. # Create folder hierarchy.
  1851. root_entries = _GatherSolutionFolders(
  1852. sln_projects, project_objects, flat=msvs_version.FlatSolution()
  1853. )
  1854. # Create solution.
  1855. sln = MSVSNew.MSVSSolution(
  1856. sln_path,
  1857. entries=root_entries,
  1858. variants=target_only_configs,
  1859. websiteProperties=False,
  1860. version=msvs_version,
  1861. )
  1862. sln.Write()
  1863. if missing_sources:
  1864. error_message = "Missing input files:\n" + "\n".join(set(missing_sources))
  1865. if generator_flags.get("msvs_error_on_missing_sources", False):
  1866. raise GypError(error_message)
  1867. else:
  1868. print("Warning: " + error_message, file=sys.stdout)
  1869. def _GenerateMSBuildFiltersFile(
  1870. filters_path,
  1871. source_files,
  1872. rule_dependencies,
  1873. extension_to_rule_name,
  1874. platforms,
  1875. toolset,
  1876. ):
  1877. """Generate the filters file.
  1878. This file is used by Visual Studio to organize the presentation of source
  1879. files into folders.
  1880. Arguments:
  1881. filters_path: The path of the file to be created.
  1882. source_files: The hierarchical structure of all the sources.
  1883. extension_to_rule_name: A dictionary mapping file extensions to rules.
  1884. """
  1885. filter_group = []
  1886. source_group = []
  1887. _AppendFiltersForMSBuild(
  1888. "",
  1889. source_files,
  1890. rule_dependencies,
  1891. extension_to_rule_name,
  1892. platforms,
  1893. toolset,
  1894. filter_group,
  1895. source_group,
  1896. )
  1897. if filter_group:
  1898. content = [
  1899. "Project",
  1900. {
  1901. "ToolsVersion": "4.0",
  1902. "xmlns": "http://schemas.microsoft.com/developer/msbuild/2003",
  1903. },
  1904. ["ItemGroup"] + filter_group,
  1905. ["ItemGroup"] + source_group,
  1906. ]
  1907. easy_xml.WriteXmlIfChanged(content, filters_path, pretty=True, win32=True)
  1908. elif os.path.exists(filters_path):
  1909. # We don't need this filter anymore. Delete the old filter file.
  1910. os.unlink(filters_path)
  1911. def _AppendFiltersForMSBuild(
  1912. parent_filter_name,
  1913. sources,
  1914. rule_dependencies,
  1915. extension_to_rule_name,
  1916. platforms,
  1917. toolset,
  1918. filter_group,
  1919. source_group,
  1920. ):
  1921. """Creates the list of filters and sources to be added in the filter file.
  1922. Args:
  1923. parent_filter_name: The name of the filter under which the sources are
  1924. found.
  1925. sources: The hierarchy of filters and sources to process.
  1926. extension_to_rule_name: A dictionary mapping file extensions to rules.
  1927. filter_group: The list to which filter entries will be appended.
  1928. source_group: The list to which source entries will be appended.
  1929. """
  1930. for source in sources:
  1931. if isinstance(source, MSVSProject.Filter):
  1932. # We have a sub-filter. Create the name of that sub-filter.
  1933. if not parent_filter_name:
  1934. filter_name = source.name
  1935. else:
  1936. filter_name = f"{parent_filter_name}\\{source.name}"
  1937. # Add the filter to the group.
  1938. filter_group.append(
  1939. [
  1940. "Filter",
  1941. {"Include": filter_name},
  1942. ["UniqueIdentifier", MSVSNew.MakeGuid(source.name)],
  1943. ]
  1944. )
  1945. # Recurse and add its dependents.
  1946. _AppendFiltersForMSBuild(
  1947. filter_name,
  1948. source.contents,
  1949. rule_dependencies,
  1950. extension_to_rule_name,
  1951. platforms,
  1952. toolset,
  1953. filter_group,
  1954. source_group,
  1955. )
  1956. else:
  1957. # It's a source. Create a source entry.
  1958. _, element = _MapFileToMsBuildSourceType(
  1959. source, rule_dependencies, extension_to_rule_name, platforms, toolset
  1960. )
  1961. source_entry = [element, {"Include": source}]
  1962. # Specify the filter it is part of, if any.
  1963. if parent_filter_name:
  1964. source_entry.append(["Filter", parent_filter_name])
  1965. source_group.append(source_entry)
  1966. def _MapFileToMsBuildSourceType(
  1967. source, rule_dependencies, extension_to_rule_name, platforms, toolset
  1968. ):
  1969. """Returns the group and element type of the source file.
  1970. Arguments:
  1971. source: The source file name.
  1972. extension_to_rule_name: A dictionary mapping file extensions to rules.
  1973. Returns:
  1974. A pair of (group this file should be part of, the label of element)
  1975. """
  1976. _, ext = os.path.splitext(source)
  1977. ext = ext.lower()
  1978. if ext in extension_to_rule_name:
  1979. group = "rule"
  1980. element = extension_to_rule_name[ext]
  1981. elif ext in [".cc", ".cpp", ".c", ".cxx", ".mm"]:
  1982. group = "compile"
  1983. element = "ClCompile"
  1984. elif ext in [".h", ".hxx"]:
  1985. group = "include"
  1986. element = "ClInclude"
  1987. elif ext == ".rc":
  1988. group = "resource"
  1989. element = "ResourceCompile"
  1990. elif ext in [".s", ".asm"]:
  1991. group = "masm"
  1992. element = "MASM"
  1993. if "arm64" in platforms and toolset == "target":
  1994. element = "MARMASM"
  1995. elif ext == ".idl":
  1996. group = "midl"
  1997. element = "Midl"
  1998. elif source in rule_dependencies:
  1999. group = "rule_dependency"
  2000. element = "CustomBuild"
  2001. else:
  2002. group = "none"
  2003. element = "None"
  2004. return (group, element)
  2005. def _GenerateRulesForMSBuild(
  2006. output_dir,
  2007. options,
  2008. spec,
  2009. sources,
  2010. excluded_sources,
  2011. props_files_of_rules,
  2012. targets_files_of_rules,
  2013. actions_to_add,
  2014. rule_dependencies,
  2015. extension_to_rule_name,
  2016. ):
  2017. # MSBuild rules are implemented using three files: an XML file, a .targets
  2018. # file and a .props file.
  2019. # For more details see:
  2020. # https://devblogs.microsoft.com/cppblog/quick-help-on-vs2010-custom-build-rule/
  2021. rules = spec.get("rules", [])
  2022. rules_native = [r for r in rules if not int(r.get("msvs_external_rule", 0))]
  2023. rules_external = [r for r in rules if int(r.get("msvs_external_rule", 0))]
  2024. msbuild_rules = []
  2025. for rule in rules_native:
  2026. # Skip a rule with no action and no inputs.
  2027. if "action" not in rule and not rule.get("rule_sources", []):
  2028. continue
  2029. msbuild_rule = MSBuildRule(rule, spec)
  2030. msbuild_rules.append(msbuild_rule)
  2031. rule_dependencies.update(msbuild_rule.additional_dependencies.split(";"))
  2032. extension_to_rule_name[msbuild_rule.extension] = msbuild_rule.rule_name
  2033. if msbuild_rules:
  2034. base = spec["target_name"] + options.suffix
  2035. props_name = base + ".props"
  2036. targets_name = base + ".targets"
  2037. xml_name = base + ".xml"
  2038. props_files_of_rules.add(props_name)
  2039. targets_files_of_rules.add(targets_name)
  2040. props_path = os.path.join(output_dir, props_name)
  2041. targets_path = os.path.join(output_dir, targets_name)
  2042. xml_path = os.path.join(output_dir, xml_name)
  2043. _GenerateMSBuildRulePropsFile(props_path, msbuild_rules)
  2044. _GenerateMSBuildRuleTargetsFile(targets_path, msbuild_rules)
  2045. _GenerateMSBuildRuleXmlFile(xml_path, msbuild_rules)
  2046. if rules_external:
  2047. _GenerateExternalRules(
  2048. rules_external, output_dir, spec, sources, options, actions_to_add
  2049. )
  2050. _AdjustSourcesForRules(rules, sources, excluded_sources, True)
  2051. class MSBuildRule:
  2052. """Used to store information used to generate an MSBuild rule.
  2053. Attributes:
  2054. rule_name: The rule name, sanitized to use in XML.
  2055. target_name: The name of the target.
  2056. after_targets: The name of the AfterTargets element.
  2057. before_targets: The name of the BeforeTargets element.
  2058. depends_on: The name of the DependsOn element.
  2059. compute_output: The name of the ComputeOutput element.
  2060. dirs_to_make: The name of the DirsToMake element.
  2061. inputs: The name of the _inputs element.
  2062. tlog: The name of the _tlog element.
  2063. extension: The extension this rule applies to.
  2064. description: The message displayed when this rule is invoked.
  2065. additional_dependencies: A string listing additional dependencies.
  2066. outputs: The outputs of this rule.
  2067. command: The command used to run the rule.
  2068. """
  2069. def __init__(self, rule, spec):
  2070. self.display_name = rule["rule_name"]
  2071. # Assure that the rule name is only characters and numbers
  2072. self.rule_name = re.sub(r"\W", "_", self.display_name)
  2073. # Create the various element names, following the example set by the
  2074. # Visual Studio 2008 to 2010 conversion. I don't know if VS2010
  2075. # is sensitive to the exact names.
  2076. self.target_name = "_" + self.rule_name
  2077. self.after_targets = self.rule_name + "AfterTargets"
  2078. self.before_targets = self.rule_name + "BeforeTargets"
  2079. self.depends_on = self.rule_name + "DependsOn"
  2080. self.compute_output = "Compute%sOutput" % self.rule_name
  2081. self.dirs_to_make = self.rule_name + "DirsToMake"
  2082. self.inputs = self.rule_name + "_inputs"
  2083. self.tlog = self.rule_name + "_tlog"
  2084. self.extension = rule["extension"]
  2085. if not self.extension.startswith("."):
  2086. self.extension = "." + self.extension
  2087. self.description = MSVSSettings.ConvertVCMacrosToMSBuild(
  2088. rule.get("message", self.rule_name)
  2089. )
  2090. old_additional_dependencies = _FixPaths(rule.get("inputs", []))
  2091. self.additional_dependencies = ";".join(
  2092. [
  2093. MSVSSettings.ConvertVCMacrosToMSBuild(i)
  2094. for i in old_additional_dependencies
  2095. ]
  2096. )
  2097. old_outputs = _FixPaths(rule.get("outputs", []))
  2098. self.outputs = ";".join(
  2099. [MSVSSettings.ConvertVCMacrosToMSBuild(i) for i in old_outputs]
  2100. )
  2101. old_command = _BuildCommandLineForRule(
  2102. spec, rule, has_input_path=True, do_setup_env=True
  2103. )
  2104. self.command = MSVSSettings.ConvertVCMacrosToMSBuild(old_command)
  2105. def _GenerateMSBuildRulePropsFile(props_path, msbuild_rules):
  2106. """Generate the .props file."""
  2107. content = [
  2108. "Project",
  2109. {"xmlns": "http://schemas.microsoft.com/developer/msbuild/2003"},
  2110. ]
  2111. for rule in msbuild_rules:
  2112. content.extend(
  2113. [
  2114. [
  2115. "PropertyGroup",
  2116. {
  2117. "Condition": "'$(%s)' == '' and '$(%s)' == '' and "
  2118. "'$(ConfigurationType)' != 'Makefile'"
  2119. % (rule.before_targets, rule.after_targets)
  2120. },
  2121. [rule.before_targets, "Midl"],
  2122. [rule.after_targets, "CustomBuild"],
  2123. ],
  2124. [
  2125. "PropertyGroup",
  2126. [
  2127. rule.depends_on,
  2128. {"Condition": "'$(ConfigurationType)' != 'Makefile'"},
  2129. "_SelectedFiles;$(%s)" % rule.depends_on,
  2130. ],
  2131. ],
  2132. [
  2133. "ItemDefinitionGroup",
  2134. [
  2135. rule.rule_name,
  2136. ["CommandLineTemplate", rule.command],
  2137. ["Outputs", rule.outputs],
  2138. ["ExecutionDescription", rule.description],
  2139. ["AdditionalDependencies", rule.additional_dependencies],
  2140. ],
  2141. ],
  2142. ]
  2143. )
  2144. easy_xml.WriteXmlIfChanged(content, props_path, pretty=True, win32=True)
  2145. def _GenerateMSBuildRuleTargetsFile(targets_path, msbuild_rules):
  2146. """Generate the .targets file."""
  2147. content = [
  2148. "Project",
  2149. {"xmlns": "http://schemas.microsoft.com/developer/msbuild/2003"},
  2150. ]
  2151. item_group = [
  2152. "ItemGroup",
  2153. [
  2154. "PropertyPageSchema",
  2155. {"Include": "$(MSBuildThisFileDirectory)$(MSBuildThisFileName).xml"},
  2156. ],
  2157. ]
  2158. for rule in msbuild_rules:
  2159. item_group.append(
  2160. [
  2161. "AvailableItemName",
  2162. {"Include": rule.rule_name},
  2163. ["Targets", rule.target_name],
  2164. ]
  2165. )
  2166. content.append(item_group)
  2167. for rule in msbuild_rules:
  2168. content.append(
  2169. [
  2170. "UsingTask",
  2171. {
  2172. "TaskName": rule.rule_name,
  2173. "TaskFactory": "XamlTaskFactory",
  2174. "AssemblyName": "Microsoft.Build.Tasks.v4.0",
  2175. },
  2176. ["Task", "$(MSBuildThisFileDirectory)$(MSBuildThisFileName).xml"],
  2177. ]
  2178. )
  2179. for rule in msbuild_rules:
  2180. rule_name = rule.rule_name
  2181. target_outputs = "%%(%s.Outputs)" % rule_name
  2182. target_inputs = (
  2183. "%%(%s.Identity);%%(%s.AdditionalDependencies);" "$(MSBuildProjectFile)"
  2184. ) % (rule_name, rule_name)
  2185. rule_inputs = "%%(%s.Identity)" % rule_name
  2186. extension_condition = (
  2187. "'%(Extension)'=='.obj' or "
  2188. "'%(Extension)'=='.res' or "
  2189. "'%(Extension)'=='.rsc' or "
  2190. "'%(Extension)'=='.lib'"
  2191. )
  2192. remove_section = [
  2193. "ItemGroup",
  2194. {"Condition": "'@(SelectedFiles)' != ''"},
  2195. [
  2196. rule_name,
  2197. {
  2198. "Remove": "@(%s)" % rule_name,
  2199. "Condition": "'%(Identity)' != '@(SelectedFiles)'",
  2200. },
  2201. ],
  2202. ]
  2203. inputs_section = [
  2204. "ItemGroup",
  2205. [rule.inputs, {"Include": "%%(%s.AdditionalDependencies)" % rule_name}],
  2206. ]
  2207. logging_section = [
  2208. "ItemGroup",
  2209. [
  2210. rule.tlog,
  2211. {
  2212. "Include": "%%(%s.Outputs)" % rule_name,
  2213. "Condition": (
  2214. "'%%(%s.Outputs)' != '' and "
  2215. "'%%(%s.ExcludedFromBuild)' != 'true'" % (rule_name, rule_name)
  2216. ),
  2217. },
  2218. ["Source", "@(%s, '|')" % rule_name],
  2219. ["Inputs", "@(%s -> '%%(Fullpath)', ';')" % rule.inputs],
  2220. ],
  2221. ]
  2222. message_section = [
  2223. "Message",
  2224. {"Importance": "High", "Text": "%%(%s.ExecutionDescription)" % rule_name},
  2225. ]
  2226. write_tlog_section = [
  2227. "WriteLinesToFile",
  2228. {
  2229. "Condition": "'@(%s)' != '' and '%%(%s.ExcludedFromBuild)' != "
  2230. "'true'" % (rule.tlog, rule.tlog),
  2231. "File": "$(IntDir)$(ProjectName).write.1.tlog",
  2232. "Lines": "^%%(%s.Source);@(%s->'%%(Fullpath)')"
  2233. % (rule.tlog, rule.tlog),
  2234. },
  2235. ]
  2236. read_tlog_section = [
  2237. "WriteLinesToFile",
  2238. {
  2239. "Condition": "'@(%s)' != '' and '%%(%s.ExcludedFromBuild)' != "
  2240. "'true'" % (rule.tlog, rule.tlog),
  2241. "File": "$(IntDir)$(ProjectName).read.1.tlog",
  2242. "Lines": f"^%({rule.tlog}.Source);%({rule.tlog}.Inputs)",
  2243. },
  2244. ]
  2245. command_and_input_section = [
  2246. rule_name,
  2247. {
  2248. "Condition": "'@(%s)' != '' and '%%(%s.ExcludedFromBuild)' != "
  2249. "'true'" % (rule_name, rule_name),
  2250. "EchoOff": "true",
  2251. "StandardOutputImportance": "High",
  2252. "StandardErrorImportance": "High",
  2253. "CommandLineTemplate": "%%(%s.CommandLineTemplate)" % rule_name,
  2254. "AdditionalOptions": "%%(%s.AdditionalOptions)" % rule_name,
  2255. "Inputs": rule_inputs,
  2256. },
  2257. ]
  2258. content.extend(
  2259. [
  2260. [
  2261. "Target",
  2262. {
  2263. "Name": rule.target_name,
  2264. "BeforeTargets": "$(%s)" % rule.before_targets,
  2265. "AfterTargets": "$(%s)" % rule.after_targets,
  2266. "Condition": "'@(%s)' != ''" % rule_name,
  2267. "DependsOnTargets": "$(%s);%s"
  2268. % (rule.depends_on, rule.compute_output),
  2269. "Outputs": target_outputs,
  2270. "Inputs": target_inputs,
  2271. },
  2272. remove_section,
  2273. inputs_section,
  2274. logging_section,
  2275. message_section,
  2276. write_tlog_section,
  2277. read_tlog_section,
  2278. command_and_input_section,
  2279. ],
  2280. [
  2281. "PropertyGroup",
  2282. [
  2283. "ComputeLinkInputsTargets",
  2284. "$(ComputeLinkInputsTargets);",
  2285. "%s;" % rule.compute_output,
  2286. ],
  2287. [
  2288. "ComputeLibInputsTargets",
  2289. "$(ComputeLibInputsTargets);",
  2290. "%s;" % rule.compute_output,
  2291. ],
  2292. ],
  2293. [
  2294. "Target",
  2295. {
  2296. "Name": rule.compute_output,
  2297. "Condition": "'@(%s)' != ''" % rule_name,
  2298. },
  2299. [
  2300. "ItemGroup",
  2301. [
  2302. rule.dirs_to_make,
  2303. {
  2304. "Condition": "'@(%s)' != '' and "
  2305. "'%%(%s.ExcludedFromBuild)' != 'true'"
  2306. % (rule_name, rule_name),
  2307. "Include": "%%(%s.Outputs)" % rule_name,
  2308. },
  2309. ],
  2310. [
  2311. "Link",
  2312. {
  2313. "Include": "%%(%s.Identity)" % rule.dirs_to_make,
  2314. "Condition": extension_condition,
  2315. },
  2316. ],
  2317. [
  2318. "Lib",
  2319. {
  2320. "Include": "%%(%s.Identity)" % rule.dirs_to_make,
  2321. "Condition": extension_condition,
  2322. },
  2323. ],
  2324. [
  2325. "ImpLib",
  2326. {
  2327. "Include": "%%(%s.Identity)" % rule.dirs_to_make,
  2328. "Condition": extension_condition,
  2329. },
  2330. ],
  2331. ],
  2332. [
  2333. "MakeDir",
  2334. {
  2335. "Directories": (
  2336. "@(%s->'%%(RootDir)%%(Directory)')" % rule.dirs_to_make
  2337. )
  2338. },
  2339. ],
  2340. ],
  2341. ]
  2342. )
  2343. easy_xml.WriteXmlIfChanged(content, targets_path, pretty=True, win32=True)
  2344. def _GenerateMSBuildRuleXmlFile(xml_path, msbuild_rules):
  2345. # Generate the .xml file
  2346. content = [
  2347. "ProjectSchemaDefinitions",
  2348. {
  2349. "xmlns": (
  2350. "clr-namespace:Microsoft.Build.Framework.XamlTypes;"
  2351. "assembly=Microsoft.Build.Framework"
  2352. ),
  2353. "xmlns:x": "http://schemas.microsoft.com/winfx/2006/xaml",
  2354. "xmlns:sys": "clr-namespace:System;assembly=mscorlib",
  2355. "xmlns:transformCallback": "Microsoft.Cpp.Dev10.ConvertPropertyCallback",
  2356. },
  2357. ]
  2358. for rule in msbuild_rules:
  2359. content.extend(
  2360. [
  2361. [
  2362. "Rule",
  2363. {
  2364. "Name": rule.rule_name,
  2365. "PageTemplate": "tool",
  2366. "DisplayName": rule.display_name,
  2367. "Order": "200",
  2368. },
  2369. [
  2370. "Rule.DataSource",
  2371. [
  2372. "DataSource",
  2373. {"Persistence": "ProjectFile", "ItemType": rule.rule_name},
  2374. ],
  2375. ],
  2376. [
  2377. "Rule.Categories",
  2378. [
  2379. "Category",
  2380. {"Name": "General"},
  2381. ["Category.DisplayName", ["sys:String", "General"]],
  2382. ],
  2383. [
  2384. "Category",
  2385. {"Name": "Command Line", "Subtype": "CommandLine"},
  2386. ["Category.DisplayName", ["sys:String", "Command Line"]],
  2387. ],
  2388. ],
  2389. [
  2390. "StringListProperty",
  2391. {
  2392. "Name": "Inputs",
  2393. "Category": "Command Line",
  2394. "IsRequired": "true",
  2395. "Switch": " ",
  2396. },
  2397. [
  2398. "StringListProperty.DataSource",
  2399. [
  2400. "DataSource",
  2401. {
  2402. "Persistence": "ProjectFile",
  2403. "ItemType": rule.rule_name,
  2404. "SourceType": "Item",
  2405. },
  2406. ],
  2407. ],
  2408. ],
  2409. [
  2410. "StringProperty",
  2411. {
  2412. "Name": "CommandLineTemplate",
  2413. "DisplayName": "Command Line",
  2414. "Visible": "False",
  2415. "IncludeInCommandLine": "False",
  2416. },
  2417. ],
  2418. [
  2419. "DynamicEnumProperty",
  2420. {
  2421. "Name": rule.before_targets,
  2422. "Category": "General",
  2423. "EnumProvider": "Targets",
  2424. "IncludeInCommandLine": "False",
  2425. },
  2426. [
  2427. "DynamicEnumProperty.DisplayName",
  2428. ["sys:String", "Execute Before"],
  2429. ],
  2430. [
  2431. "DynamicEnumProperty.Description",
  2432. [
  2433. "sys:String",
  2434. "Specifies the targets for the build customization"
  2435. " to run before.",
  2436. ],
  2437. ],
  2438. [
  2439. "DynamicEnumProperty.ProviderSettings",
  2440. [
  2441. "NameValuePair",
  2442. {
  2443. "Name": "Exclude",
  2444. "Value": "^%s|^Compute" % rule.before_targets,
  2445. },
  2446. ],
  2447. ],
  2448. [
  2449. "DynamicEnumProperty.DataSource",
  2450. [
  2451. "DataSource",
  2452. {
  2453. "Persistence": "ProjectFile",
  2454. "HasConfigurationCondition": "true",
  2455. },
  2456. ],
  2457. ],
  2458. ],
  2459. [
  2460. "DynamicEnumProperty",
  2461. {
  2462. "Name": rule.after_targets,
  2463. "Category": "General",
  2464. "EnumProvider": "Targets",
  2465. "IncludeInCommandLine": "False",
  2466. },
  2467. [
  2468. "DynamicEnumProperty.DisplayName",
  2469. ["sys:String", "Execute After"],
  2470. ],
  2471. [
  2472. "DynamicEnumProperty.Description",
  2473. [
  2474. "sys:String",
  2475. (
  2476. "Specifies the targets for the build customization"
  2477. " to run after."
  2478. ),
  2479. ],
  2480. ],
  2481. [
  2482. "DynamicEnumProperty.ProviderSettings",
  2483. [
  2484. "NameValuePair",
  2485. {
  2486. "Name": "Exclude",
  2487. "Value": "^%s|^Compute" % rule.after_targets,
  2488. },
  2489. ],
  2490. ],
  2491. [
  2492. "DynamicEnumProperty.DataSource",
  2493. [
  2494. "DataSource",
  2495. {
  2496. "Persistence": "ProjectFile",
  2497. "ItemType": "",
  2498. "HasConfigurationCondition": "true",
  2499. },
  2500. ],
  2501. ],
  2502. ],
  2503. [
  2504. "StringListProperty",
  2505. {
  2506. "Name": "Outputs",
  2507. "DisplayName": "Outputs",
  2508. "Visible": "False",
  2509. "IncludeInCommandLine": "False",
  2510. },
  2511. ],
  2512. [
  2513. "StringProperty",
  2514. {
  2515. "Name": "ExecutionDescription",
  2516. "DisplayName": "Execution Description",
  2517. "Visible": "False",
  2518. "IncludeInCommandLine": "False",
  2519. },
  2520. ],
  2521. [
  2522. "StringListProperty",
  2523. {
  2524. "Name": "AdditionalDependencies",
  2525. "DisplayName": "Additional Dependencies",
  2526. "IncludeInCommandLine": "False",
  2527. "Visible": "false",
  2528. },
  2529. ],
  2530. [
  2531. "StringProperty",
  2532. {
  2533. "Subtype": "AdditionalOptions",
  2534. "Name": "AdditionalOptions",
  2535. "Category": "Command Line",
  2536. },
  2537. [
  2538. "StringProperty.DisplayName",
  2539. ["sys:String", "Additional Options"],
  2540. ],
  2541. [
  2542. "StringProperty.Description",
  2543. ["sys:String", "Additional Options"],
  2544. ],
  2545. ],
  2546. ],
  2547. [
  2548. "ItemType",
  2549. {"Name": rule.rule_name, "DisplayName": rule.display_name},
  2550. ],
  2551. [
  2552. "FileExtension",
  2553. {"Name": "*" + rule.extension, "ContentType": rule.rule_name},
  2554. ],
  2555. [
  2556. "ContentType",
  2557. {
  2558. "Name": rule.rule_name,
  2559. "DisplayName": "",
  2560. "ItemType": rule.rule_name,
  2561. },
  2562. ],
  2563. ]
  2564. )
  2565. easy_xml.WriteXmlIfChanged(content, xml_path, pretty=True, win32=True)
  2566. def _GetConfigurationAndPlatform(name, settings, spec):
  2567. configuration = name.rsplit("_", 1)[0]
  2568. platform = settings.get("msvs_configuration_platform", "Win32")
  2569. if spec["toolset"] == "host" and platform == "arm64":
  2570. platform = "x64" # Host-only tools are always built for x64
  2571. return (configuration, platform)
  2572. def _GetConfigurationCondition(name, settings, spec):
  2573. return r"'$(Configuration)|$(Platform)'=='%s|%s'" % _GetConfigurationAndPlatform(
  2574. name, settings, spec
  2575. )
  2576. def _GetMSBuildProjectConfigurations(configurations, spec):
  2577. group = ["ItemGroup", {"Label": "ProjectConfigurations"}]
  2578. for (name, settings) in sorted(configurations.items()):
  2579. configuration, platform = _GetConfigurationAndPlatform(name, settings, spec)
  2580. designation = f"{configuration}|{platform}"
  2581. group.append(
  2582. [
  2583. "ProjectConfiguration",
  2584. {"Include": designation},
  2585. ["Configuration", configuration],
  2586. ["Platform", platform],
  2587. ]
  2588. )
  2589. return [group]
  2590. def _GetMSBuildGlobalProperties(spec, version, guid, gyp_file_name):
  2591. namespace = os.path.splitext(gyp_file_name)[0]
  2592. properties = [
  2593. [
  2594. "PropertyGroup",
  2595. {"Label": "Globals"},
  2596. ["ProjectGuid", guid],
  2597. ["Keyword", "Win32Proj"],
  2598. ["RootNamespace", namespace],
  2599. ["IgnoreWarnCompileDuplicatedFilename", "true"],
  2600. ]
  2601. ]
  2602. if (
  2603. os.environ.get("PROCESSOR_ARCHITECTURE") == "AMD64"
  2604. or os.environ.get("PROCESSOR_ARCHITEW6432") == "AMD64"
  2605. ):
  2606. properties[0].append(["PreferredToolArchitecture", "x64"])
  2607. if spec.get("msvs_target_platform_version"):
  2608. target_platform_version = spec.get("msvs_target_platform_version")
  2609. properties[0].append(["WindowsTargetPlatformVersion", target_platform_version])
  2610. if spec.get("msvs_target_platform_minversion"):
  2611. target_platform_minversion = spec.get("msvs_target_platform_minversion")
  2612. properties[0].append(
  2613. ["WindowsTargetPlatformMinVersion", target_platform_minversion]
  2614. )
  2615. else:
  2616. properties[0].append(
  2617. ["WindowsTargetPlatformMinVersion", target_platform_version]
  2618. )
  2619. if spec.get("msvs_enable_winrt"):
  2620. properties[0].append(["DefaultLanguage", "en-US"])
  2621. properties[0].append(["AppContainerApplication", "true"])
  2622. if spec.get("msvs_application_type_revision"):
  2623. app_type_revision = spec.get("msvs_application_type_revision")
  2624. properties[0].append(["ApplicationTypeRevision", app_type_revision])
  2625. else:
  2626. properties[0].append(["ApplicationTypeRevision", "8.1"])
  2627. if spec.get("msvs_enable_winphone"):
  2628. properties[0].append(["ApplicationType", "Windows Phone"])
  2629. else:
  2630. properties[0].append(["ApplicationType", "Windows Store"])
  2631. platform_name = None
  2632. msvs_windows_sdk_version = None
  2633. for configuration in spec["configurations"].values():
  2634. platform_name = platform_name or _ConfigPlatform(configuration)
  2635. msvs_windows_sdk_version = (
  2636. msvs_windows_sdk_version
  2637. or _ConfigWindowsTargetPlatformVersion(configuration, version)
  2638. )
  2639. if platform_name and msvs_windows_sdk_version:
  2640. break
  2641. if msvs_windows_sdk_version:
  2642. properties[0].append(
  2643. ["WindowsTargetPlatformVersion", str(msvs_windows_sdk_version)]
  2644. )
  2645. elif version.compatible_sdks:
  2646. raise GypError(
  2647. "%s requires any SDK of %s version, but none were found"
  2648. % (version.description, version.compatible_sdks)
  2649. )
  2650. if platform_name == "ARM":
  2651. properties[0].append(["WindowsSDKDesktopARMSupport", "true"])
  2652. return properties
  2653. def _GetMSBuildConfigurationDetails(spec, build_file):
  2654. properties = {}
  2655. for name, settings in spec["configurations"].items():
  2656. msbuild_attributes = _GetMSBuildAttributes(spec, settings, build_file)
  2657. condition = _GetConfigurationCondition(name, settings, spec)
  2658. character_set = msbuild_attributes.get("CharacterSet")
  2659. vctools_version = msbuild_attributes.get("VCToolsVersion")
  2660. config_type = msbuild_attributes.get("ConfigurationType")
  2661. _AddConditionalProperty(properties, condition, "ConfigurationType", config_type)
  2662. spectre_mitigation = msbuild_attributes.get('SpectreMitigation')
  2663. if spectre_mitigation:
  2664. _AddConditionalProperty(properties, condition, "SpectreMitigation",
  2665. spectre_mitigation)
  2666. if config_type == "Driver":
  2667. _AddConditionalProperty(properties, condition, "DriverType", "WDM")
  2668. _AddConditionalProperty(
  2669. properties, condition, "TargetVersion", _ConfigTargetVersion(settings)
  2670. )
  2671. if character_set and "msvs_enable_winrt" not in spec:
  2672. _AddConditionalProperty(
  2673. properties, condition, "CharacterSet", character_set
  2674. )
  2675. if vctools_version and "msvs_enable_winrt" not in spec:
  2676. _AddConditionalProperty(
  2677. properties, condition, "VCToolsVersion", vctools_version
  2678. )
  2679. return _GetMSBuildPropertyGroup(spec, "Configuration", properties)
  2680. def _GetMSBuildLocalProperties(msbuild_toolset):
  2681. # Currently the only local property we support is PlatformToolset
  2682. properties = {}
  2683. if msbuild_toolset:
  2684. properties = [
  2685. [
  2686. "PropertyGroup",
  2687. {"Label": "Locals"},
  2688. ["PlatformToolset", msbuild_toolset],
  2689. ]
  2690. ]
  2691. return properties
  2692. def _GetMSBuildPropertySheets(configurations, spec):
  2693. user_props = r"$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props"
  2694. additional_props = {}
  2695. props_specified = False
  2696. for name, settings in sorted(configurations.items()):
  2697. configuration = _GetConfigurationCondition(name, settings, spec)
  2698. if "msbuild_props" in settings:
  2699. additional_props[configuration] = _FixPaths(settings["msbuild_props"])
  2700. props_specified = True
  2701. else:
  2702. additional_props[configuration] = ""
  2703. if not props_specified:
  2704. return [
  2705. [
  2706. "ImportGroup",
  2707. {"Label": "PropertySheets"},
  2708. [
  2709. "Import",
  2710. {
  2711. "Project": user_props,
  2712. "Condition": "exists('%s')" % user_props,
  2713. "Label": "LocalAppDataPlatform",
  2714. },
  2715. ],
  2716. ]
  2717. ]
  2718. else:
  2719. sheets = []
  2720. for condition, props in additional_props.items():
  2721. import_group = [
  2722. "ImportGroup",
  2723. {"Label": "PropertySheets", "Condition": condition},
  2724. [
  2725. "Import",
  2726. {
  2727. "Project": user_props,
  2728. "Condition": "exists('%s')" % user_props,
  2729. "Label": "LocalAppDataPlatform",
  2730. },
  2731. ],
  2732. ]
  2733. for props_file in props:
  2734. import_group.append(["Import", {"Project": props_file}])
  2735. sheets.append(import_group)
  2736. return sheets
  2737. def _ConvertMSVSBuildAttributes(spec, config, build_file):
  2738. config_type = _GetMSVSConfigurationType(spec, build_file)
  2739. msvs_attributes = _GetMSVSAttributes(spec, config, config_type)
  2740. msbuild_attributes = {}
  2741. for a in msvs_attributes:
  2742. if a in ["IntermediateDirectory", "OutputDirectory"]:
  2743. directory = MSVSSettings.ConvertVCMacrosToMSBuild(msvs_attributes[a])
  2744. if not directory.endswith("\\"):
  2745. directory += "\\"
  2746. msbuild_attributes[a] = directory
  2747. elif a == "CharacterSet":
  2748. msbuild_attributes[a] = _ConvertMSVSCharacterSet(msvs_attributes[a])
  2749. elif a == "ConfigurationType":
  2750. msbuild_attributes[a] = _ConvertMSVSConfigurationType(msvs_attributes[a])
  2751. elif a == "SpectreMitigation":
  2752. msbuild_attributes[a] = msvs_attributes[a]
  2753. elif a == "VCToolsVersion":
  2754. msbuild_attributes[a] = msvs_attributes[a]
  2755. else:
  2756. print("Warning: Do not know how to convert MSVS attribute " + a)
  2757. return msbuild_attributes
  2758. def _ConvertMSVSCharacterSet(char_set):
  2759. if char_set.isdigit():
  2760. char_set = {"0": "MultiByte", "1": "Unicode", "2": "MultiByte"}[char_set]
  2761. return char_set
  2762. def _ConvertMSVSConfigurationType(config_type):
  2763. if config_type.isdigit():
  2764. config_type = {
  2765. "1": "Application",
  2766. "2": "DynamicLibrary",
  2767. "4": "StaticLibrary",
  2768. "5": "Driver",
  2769. "10": "Utility",
  2770. }[config_type]
  2771. return config_type
  2772. def _GetMSBuildAttributes(spec, config, build_file):
  2773. if "msbuild_configuration_attributes" not in config:
  2774. msbuild_attributes = _ConvertMSVSBuildAttributes(spec, config, build_file)
  2775. else:
  2776. config_type = _GetMSVSConfigurationType(spec, build_file)
  2777. config_type = _ConvertMSVSConfigurationType(config_type)
  2778. msbuild_attributes = config.get("msbuild_configuration_attributes", {})
  2779. msbuild_attributes.setdefault("ConfigurationType", config_type)
  2780. output_dir = msbuild_attributes.get(
  2781. "OutputDirectory", "$(SolutionDir)$(Configuration)"
  2782. )
  2783. msbuild_attributes["OutputDirectory"] = _FixPath(output_dir) + "\\"
  2784. if "IntermediateDirectory" not in msbuild_attributes:
  2785. intermediate = _FixPath("$(Configuration)") + "\\"
  2786. msbuild_attributes["IntermediateDirectory"] = intermediate
  2787. if "CharacterSet" in msbuild_attributes:
  2788. msbuild_attributes["CharacterSet"] = _ConvertMSVSCharacterSet(
  2789. msbuild_attributes["CharacterSet"]
  2790. )
  2791. if "TargetName" not in msbuild_attributes:
  2792. prefix = spec.get("product_prefix", "")
  2793. product_name = spec.get("product_name", "$(ProjectName)")
  2794. target_name = prefix + product_name
  2795. msbuild_attributes["TargetName"] = target_name
  2796. if "TargetExt" not in msbuild_attributes and "product_extension" in spec:
  2797. ext = spec.get("product_extension")
  2798. msbuild_attributes["TargetExt"] = "." + ext
  2799. if spec.get("msvs_external_builder"):
  2800. external_out_dir = spec.get("msvs_external_builder_out_dir", ".")
  2801. msbuild_attributes["OutputDirectory"] = _FixPath(external_out_dir) + "\\"
  2802. # Make sure that 'TargetPath' matches 'Lib.OutputFile' or 'Link.OutputFile'
  2803. # (depending on the tool used) to avoid MSB8012 warning.
  2804. msbuild_tool_map = {
  2805. "executable": "Link",
  2806. "shared_library": "Link",
  2807. "loadable_module": "Link",
  2808. "windows_driver": "Link",
  2809. "static_library": "Lib",
  2810. }
  2811. msbuild_tool = msbuild_tool_map.get(spec["type"])
  2812. if msbuild_tool:
  2813. msbuild_settings = config["finalized_msbuild_settings"]
  2814. out_file = msbuild_settings[msbuild_tool].get("OutputFile")
  2815. if out_file:
  2816. msbuild_attributes["TargetPath"] = _FixPath(out_file)
  2817. target_ext = msbuild_settings[msbuild_tool].get("TargetExt")
  2818. if target_ext:
  2819. msbuild_attributes["TargetExt"] = target_ext
  2820. return msbuild_attributes
  2821. def _GetMSBuildConfigurationGlobalProperties(spec, configurations, build_file):
  2822. # TODO(jeanluc) We could optimize out the following and do it only if
  2823. # there are actions.
  2824. # TODO(jeanluc) Handle the equivalent of setting 'CYGWIN=nontsec'.
  2825. new_paths = []
  2826. cygwin_dirs = spec.get("msvs_cygwin_dirs", ["."])[0]
  2827. if cygwin_dirs:
  2828. cyg_path = "$(MSBuildProjectDirectory)\\%s\\bin\\" % _FixPath(cygwin_dirs)
  2829. new_paths.append(cyg_path)
  2830. # TODO(jeanluc) Change the convention to have both a cygwin_dir and a
  2831. # python_dir.
  2832. python_path = cyg_path.replace("cygwin\\bin", "python_26")
  2833. new_paths.append(python_path)
  2834. if new_paths:
  2835. new_paths = "$(ExecutablePath);" + ";".join(new_paths)
  2836. properties = {}
  2837. for (name, configuration) in sorted(configurations.items()):
  2838. condition = _GetConfigurationCondition(name, configuration, spec)
  2839. attributes = _GetMSBuildAttributes(spec, configuration, build_file)
  2840. msbuild_settings = configuration["finalized_msbuild_settings"]
  2841. _AddConditionalProperty(
  2842. properties, condition, "IntDir", attributes["IntermediateDirectory"]
  2843. )
  2844. _AddConditionalProperty(
  2845. properties, condition, "OutDir", attributes["OutputDirectory"]
  2846. )
  2847. _AddConditionalProperty(
  2848. properties, condition, "TargetName", attributes["TargetName"]
  2849. )
  2850. if "TargetExt" in attributes:
  2851. _AddConditionalProperty(
  2852. properties, condition, "TargetExt", attributes["TargetExt"]
  2853. )
  2854. if attributes.get("TargetPath"):
  2855. _AddConditionalProperty(
  2856. properties, condition, "TargetPath", attributes["TargetPath"]
  2857. )
  2858. if attributes.get("TargetExt"):
  2859. _AddConditionalProperty(
  2860. properties, condition, "TargetExt", attributes["TargetExt"]
  2861. )
  2862. if new_paths:
  2863. _AddConditionalProperty(properties, condition, "ExecutablePath", new_paths)
  2864. tool_settings = msbuild_settings.get("", {})
  2865. for name, value in sorted(tool_settings.items()):
  2866. formatted_value = _GetValueFormattedForMSBuild("", name, value)
  2867. _AddConditionalProperty(properties, condition, name, formatted_value)
  2868. return _GetMSBuildPropertyGroup(spec, None, properties)
  2869. def _AddConditionalProperty(properties, condition, name, value):
  2870. """Adds a property / conditional value pair to a dictionary.
  2871. Arguments:
  2872. properties: The dictionary to be modified. The key is the name of the
  2873. property. The value is itself a dictionary; its key is the value and
  2874. the value a list of condition for which this value is true.
  2875. condition: The condition under which the named property has the value.
  2876. name: The name of the property.
  2877. value: The value of the property.
  2878. """
  2879. if name not in properties:
  2880. properties[name] = {}
  2881. values = properties[name]
  2882. if value not in values:
  2883. values[value] = []
  2884. conditions = values[value]
  2885. conditions.append(condition)
  2886. # Regex for msvs variable references ( i.e. $(FOO) ).
  2887. MSVS_VARIABLE_REFERENCE = re.compile(r"\$\(([a-zA-Z_][a-zA-Z0-9_]*)\)")
  2888. def _GetMSBuildPropertyGroup(spec, label, properties):
  2889. """Returns a PropertyGroup definition for the specified properties.
  2890. Arguments:
  2891. spec: The target project dict.
  2892. label: An optional label for the PropertyGroup.
  2893. properties: The dictionary to be converted. The key is the name of the
  2894. property. The value is itself a dictionary; its key is the value and
  2895. the value a list of condition for which this value is true.
  2896. """
  2897. group = ["PropertyGroup"]
  2898. if label:
  2899. group.append({"Label": label})
  2900. num_configurations = len(spec["configurations"])
  2901. def GetEdges(node):
  2902. # Use a definition of edges such that user_of_variable -> used_variable.
  2903. # This happens to be easier in this case, since a variable's
  2904. # definition contains all variables it references in a single string.
  2905. edges = set()
  2906. for value in sorted(properties[node].keys()):
  2907. # Add to edges all $(...) references to variables.
  2908. #
  2909. # Variable references that refer to names not in properties are excluded
  2910. # These can exist for instance to refer built in definitions like
  2911. # $(SolutionDir).
  2912. #
  2913. # Self references are ignored. Self reference is used in a few places to
  2914. # append to the default value. I.e. PATH=$(PATH);other_path
  2915. edges.update(
  2916. {
  2917. v
  2918. for v in MSVS_VARIABLE_REFERENCE.findall(value)
  2919. if v in properties and v != node
  2920. }
  2921. )
  2922. return edges
  2923. properties_ordered = gyp.common.TopologicallySorted(properties.keys(), GetEdges)
  2924. # Walk properties in the reverse of a topological sort on
  2925. # user_of_variable -> used_variable as this ensures variables are
  2926. # defined before they are used.
  2927. # NOTE: reverse(topsort(DAG)) = topsort(reverse_edges(DAG))
  2928. for name in reversed(properties_ordered):
  2929. values = properties[name]
  2930. for value, conditions in sorted(values.items()):
  2931. if len(conditions) == num_configurations:
  2932. # If the value is the same all configurations,
  2933. # just add one unconditional entry.
  2934. group.append([name, value])
  2935. else:
  2936. for condition in conditions:
  2937. group.append([name, {"Condition": condition}, value])
  2938. return [group]
  2939. def _GetMSBuildToolSettingsSections(spec, configurations):
  2940. groups = []
  2941. for (name, configuration) in sorted(configurations.items()):
  2942. msbuild_settings = configuration["finalized_msbuild_settings"]
  2943. group = [
  2944. "ItemDefinitionGroup",
  2945. {"Condition": _GetConfigurationCondition(name, configuration, spec)},
  2946. ]
  2947. for tool_name, tool_settings in sorted(msbuild_settings.items()):
  2948. # Skip the tool named '' which is a holder of global settings handled
  2949. # by _GetMSBuildConfigurationGlobalProperties.
  2950. if tool_name and tool_settings:
  2951. tool = [tool_name]
  2952. for name, value in sorted(tool_settings.items()):
  2953. formatted_value = _GetValueFormattedForMSBuild(
  2954. tool_name, name, value
  2955. )
  2956. tool.append([name, formatted_value])
  2957. group.append(tool)
  2958. groups.append(group)
  2959. return groups
  2960. def _FinalizeMSBuildSettings(spec, configuration):
  2961. if "msbuild_settings" in configuration:
  2962. converted = False
  2963. msbuild_settings = configuration["msbuild_settings"]
  2964. MSVSSettings.ValidateMSBuildSettings(msbuild_settings)
  2965. else:
  2966. converted = True
  2967. msvs_settings = configuration.get("msvs_settings", {})
  2968. msbuild_settings = MSVSSettings.ConvertToMSBuildSettings(msvs_settings)
  2969. include_dirs, midl_include_dirs, resource_include_dirs = _GetIncludeDirs(
  2970. configuration
  2971. )
  2972. libraries = _GetLibraries(spec)
  2973. library_dirs = _GetLibraryDirs(configuration)
  2974. out_file, _, msbuild_tool = _GetOutputFilePathAndTool(spec, msbuild=True)
  2975. target_ext = _GetOutputTargetExt(spec)
  2976. defines = _GetDefines(configuration)
  2977. if converted:
  2978. # Visual Studio 2010 has TR1
  2979. defines = [d for d in defines if d != "_HAS_TR1=0"]
  2980. # Warn of ignored settings
  2981. ignored_settings = ["msvs_tool_files"]
  2982. for ignored_setting in ignored_settings:
  2983. value = configuration.get(ignored_setting)
  2984. if value:
  2985. print(
  2986. "Warning: The automatic conversion to MSBuild does not handle "
  2987. "%s. Ignoring setting of %s" % (ignored_setting, str(value))
  2988. )
  2989. defines = [_EscapeCppDefineForMSBuild(d) for d in defines]
  2990. disabled_warnings = _GetDisabledWarnings(configuration)
  2991. prebuild = configuration.get("msvs_prebuild")
  2992. postbuild = configuration.get("msvs_postbuild")
  2993. def_file = _GetModuleDefinition(spec)
  2994. precompiled_header = configuration.get("msvs_precompiled_header")
  2995. # Add the information to the appropriate tool
  2996. # TODO(jeanluc) We could optimize and generate these settings only if
  2997. # the corresponding files are found, e.g. don't generate ResourceCompile
  2998. # if you don't have any resources.
  2999. _ToolAppend(
  3000. msbuild_settings, "ClCompile", "AdditionalIncludeDirectories", include_dirs
  3001. )
  3002. _ToolAppend(
  3003. msbuild_settings, "Midl", "AdditionalIncludeDirectories", midl_include_dirs
  3004. )
  3005. _ToolAppend(
  3006. msbuild_settings,
  3007. "ResourceCompile",
  3008. "AdditionalIncludeDirectories",
  3009. resource_include_dirs,
  3010. )
  3011. # Add in libraries, note that even for empty libraries, we want this
  3012. # set, to prevent inheriting default libraries from the environment.
  3013. _ToolSetOrAppend(msbuild_settings, "Link", "AdditionalDependencies", libraries)
  3014. _ToolAppend(msbuild_settings, "Link", "AdditionalLibraryDirectories", library_dirs)
  3015. if out_file:
  3016. _ToolAppend(
  3017. msbuild_settings, msbuild_tool, "OutputFile", out_file, only_if_unset=True
  3018. )
  3019. if target_ext:
  3020. _ToolAppend(
  3021. msbuild_settings, msbuild_tool, "TargetExt", target_ext, only_if_unset=True
  3022. )
  3023. # Add defines.
  3024. _ToolAppend(msbuild_settings, "ClCompile", "PreprocessorDefinitions", defines)
  3025. _ToolAppend(msbuild_settings, "ResourceCompile", "PreprocessorDefinitions", defines)
  3026. # Add disabled warnings.
  3027. _ToolAppend(
  3028. msbuild_settings, "ClCompile", "DisableSpecificWarnings", disabled_warnings
  3029. )
  3030. # Turn on precompiled headers if appropriate.
  3031. if precompiled_header:
  3032. # While MSVC works with just file name eg. "v8_pch.h", ClangCL requires
  3033. # the full path eg. "tools/msvs/pch/v8_pch.h" to find the file.
  3034. # P.S. Only ClangCL defines msbuild_toolset, for MSVC it is None.
  3035. if configuration.get("msbuild_toolset") != 'ClangCL':
  3036. precompiled_header = os.path.split(precompiled_header)[1]
  3037. _ToolAppend(msbuild_settings, "ClCompile", "PrecompiledHeader", "Use")
  3038. _ToolAppend(
  3039. msbuild_settings, "ClCompile", "PrecompiledHeaderFile", precompiled_header
  3040. )
  3041. _ToolAppend(
  3042. msbuild_settings, "ClCompile", "ForcedIncludeFiles", [precompiled_header]
  3043. )
  3044. else:
  3045. _ToolAppend(msbuild_settings, "ClCompile", "PrecompiledHeader", "NotUsing")
  3046. # Turn off WinRT compilation
  3047. _ToolAppend(msbuild_settings, "ClCompile", "CompileAsWinRT", "false")
  3048. # Turn on import libraries if appropriate
  3049. if spec.get("msvs_requires_importlibrary"):
  3050. _ToolAppend(msbuild_settings, "", "IgnoreImportLibrary", "false")
  3051. # Loadable modules don't generate import libraries;
  3052. # tell dependent projects to not expect one.
  3053. if spec["type"] == "loadable_module":
  3054. _ToolAppend(msbuild_settings, "", "IgnoreImportLibrary", "true")
  3055. # Set the module definition file if any.
  3056. if def_file:
  3057. _ToolAppend(msbuild_settings, "Link", "ModuleDefinitionFile", def_file)
  3058. configuration["finalized_msbuild_settings"] = msbuild_settings
  3059. if prebuild:
  3060. _ToolAppend(msbuild_settings, "PreBuildEvent", "Command", prebuild)
  3061. if postbuild:
  3062. _ToolAppend(msbuild_settings, "PostBuildEvent", "Command", postbuild)
  3063. def _GetValueFormattedForMSBuild(tool_name, name, value):
  3064. if isinstance(value, list):
  3065. # For some settings, VS2010 does not automatically extends the settings
  3066. # TODO(jeanluc) Is this what we want?
  3067. if name in [
  3068. "AdditionalIncludeDirectories",
  3069. "AdditionalLibraryDirectories",
  3070. "AdditionalOptions",
  3071. "DelayLoadDLLs",
  3072. "DisableSpecificWarnings",
  3073. "PreprocessorDefinitions",
  3074. ]:
  3075. value.append("%%(%s)" % name)
  3076. # For most tools, entries in a list should be separated with ';' but some
  3077. # settings use a space. Check for those first.
  3078. exceptions = {
  3079. "ClCompile": ["AdditionalOptions"],
  3080. "Link": ["AdditionalOptions"],
  3081. "Lib": ["AdditionalOptions"],
  3082. }
  3083. char = " " if name in exceptions.get(tool_name, []) else ";"
  3084. formatted_value = char.join(
  3085. [MSVSSettings.ConvertVCMacrosToMSBuild(i) for i in value]
  3086. )
  3087. else:
  3088. formatted_value = MSVSSettings.ConvertVCMacrosToMSBuild(value)
  3089. return formatted_value
  3090. def _VerifySourcesExist(sources, root_dir):
  3091. """Verifies that all source files exist on disk.
  3092. Checks that all regular source files, i.e. not created at run time,
  3093. exist on disk. Missing files cause needless recompilation but no otherwise
  3094. visible errors.
  3095. Arguments:
  3096. sources: A recursive list of Filter/file names.
  3097. root_dir: The root directory for the relative path names.
  3098. Returns:
  3099. A list of source files that cannot be found on disk.
  3100. """
  3101. missing_sources = []
  3102. for source in sources:
  3103. if isinstance(source, MSVSProject.Filter):
  3104. missing_sources.extend(_VerifySourcesExist(source.contents, root_dir))
  3105. else:
  3106. if "$" not in source:
  3107. full_path = os.path.join(root_dir, source)
  3108. if not os.path.exists(full_path):
  3109. missing_sources.append(full_path)
  3110. return missing_sources
  3111. def _GetMSBuildSources(
  3112. spec,
  3113. sources,
  3114. exclusions,
  3115. rule_dependencies,
  3116. extension_to_rule_name,
  3117. actions_spec,
  3118. sources_handled_by_action,
  3119. list_excluded,
  3120. ):
  3121. groups = [
  3122. "none",
  3123. "masm",
  3124. "midl",
  3125. "include",
  3126. "compile",
  3127. "resource",
  3128. "rule",
  3129. "rule_dependency",
  3130. ]
  3131. grouped_sources = {}
  3132. for g in groups:
  3133. grouped_sources[g] = []
  3134. _AddSources2(
  3135. spec,
  3136. sources,
  3137. exclusions,
  3138. grouped_sources,
  3139. rule_dependencies,
  3140. extension_to_rule_name,
  3141. sources_handled_by_action,
  3142. list_excluded,
  3143. )
  3144. sources = []
  3145. for g in groups:
  3146. if grouped_sources[g]:
  3147. sources.append(["ItemGroup"] + grouped_sources[g])
  3148. if actions_spec:
  3149. sources.append(["ItemGroup"] + actions_spec)
  3150. return sources
  3151. def _AddSources2(
  3152. spec,
  3153. sources,
  3154. exclusions,
  3155. grouped_sources,
  3156. rule_dependencies,
  3157. extension_to_rule_name,
  3158. sources_handled_by_action,
  3159. list_excluded,
  3160. ):
  3161. extensions_excluded_from_precompile = []
  3162. for source in sources:
  3163. if isinstance(source, MSVSProject.Filter):
  3164. _AddSources2(
  3165. spec,
  3166. source.contents,
  3167. exclusions,
  3168. grouped_sources,
  3169. rule_dependencies,
  3170. extension_to_rule_name,
  3171. sources_handled_by_action,
  3172. list_excluded,
  3173. )
  3174. else:
  3175. if source not in sources_handled_by_action:
  3176. detail = []
  3177. excluded_configurations = exclusions.get(source, [])
  3178. if len(excluded_configurations) == len(spec["configurations"]):
  3179. detail.append(["ExcludedFromBuild", "true"])
  3180. else:
  3181. for config_name, configuration in sorted(excluded_configurations):
  3182. condition = _GetConfigurationCondition(
  3183. config_name, configuration
  3184. )
  3185. detail.append(
  3186. ["ExcludedFromBuild", {"Condition": condition}, "true"]
  3187. )
  3188. # Add precompile if needed
  3189. for config_name, configuration in spec["configurations"].items():
  3190. precompiled_source = configuration.get(
  3191. "msvs_precompiled_source", ""
  3192. )
  3193. if precompiled_source != "":
  3194. precompiled_source = _FixPath(precompiled_source)
  3195. if not extensions_excluded_from_precompile:
  3196. # If the precompiled header is generated by a C source,
  3197. # we must not try to use it for C++ sources,
  3198. # and vice versa.
  3199. basename, extension = os.path.splitext(precompiled_source)
  3200. if extension == ".c":
  3201. extensions_excluded_from_precompile = [
  3202. ".cc",
  3203. ".cpp",
  3204. ".cxx",
  3205. ]
  3206. else:
  3207. extensions_excluded_from_precompile = [".c"]
  3208. if precompiled_source == source:
  3209. condition = _GetConfigurationCondition(
  3210. config_name, configuration, spec
  3211. )
  3212. detail.append(
  3213. ["PrecompiledHeader", {"Condition": condition}, "Create"]
  3214. )
  3215. else:
  3216. # Turn off precompiled header usage for source files of a
  3217. # different type than the file that generated the
  3218. # precompiled header.
  3219. for extension in extensions_excluded_from_precompile:
  3220. if source.endswith(extension):
  3221. detail.append(["PrecompiledHeader", ""])
  3222. detail.append(["ForcedIncludeFiles", ""])
  3223. group, element = _MapFileToMsBuildSourceType(
  3224. source,
  3225. rule_dependencies,
  3226. extension_to_rule_name,
  3227. _GetUniquePlatforms(spec),
  3228. spec["toolset"],
  3229. )
  3230. if group == "compile" and not os.path.isabs(source):
  3231. # Add an <ObjectFileName> value to support duplicate source
  3232. # file basenames, except for absolute paths to avoid paths
  3233. # with more than 260 characters.
  3234. file_name = os.path.splitext(source)[0] + ".obj"
  3235. if file_name.startswith("..\\"):
  3236. file_name = re.sub(r"^(\.\.\\)+", "", file_name)
  3237. elif file_name.startswith("$("):
  3238. file_name = re.sub(r"^\$\([^)]+\)\\", "", file_name)
  3239. detail.append(["ObjectFileName", "$(IntDir)\\" + file_name])
  3240. grouped_sources[group].append([element, {"Include": source}] + detail)
  3241. def _GetMSBuildProjectReferences(project):
  3242. references = []
  3243. if project.dependencies:
  3244. group = ["ItemGroup"]
  3245. added_dependency_set = set()
  3246. for dependency in project.dependencies:
  3247. dependency_spec = dependency.spec
  3248. should_skip_dep = False
  3249. if project.spec["toolset"] == "target":
  3250. if dependency_spec["toolset"] == "host":
  3251. if dependency_spec["type"] == "static_library":
  3252. should_skip_dep = True
  3253. if dependency.name.startswith("run_"):
  3254. should_skip_dep = False
  3255. if should_skip_dep:
  3256. continue
  3257. canonical_name = dependency.name.replace("_host", "")
  3258. added_dependency_set.add(canonical_name)
  3259. guid = dependency.guid
  3260. project_dir = os.path.split(project.path)[0]
  3261. relative_path = gyp.common.RelativePath(dependency.path, project_dir)
  3262. project_ref = [
  3263. "ProjectReference",
  3264. {"Include": relative_path},
  3265. ["Project", guid],
  3266. ["ReferenceOutputAssembly", "false"],
  3267. ]
  3268. for config in dependency.spec.get("configurations", {}).values():
  3269. if config.get("msvs_use_library_dependency_inputs", 0):
  3270. project_ref.append(["UseLibraryDependencyInputs", "true"])
  3271. break
  3272. # If it's disabled in any config, turn it off in the reference.
  3273. if config.get("msvs_2010_disable_uldi_when_referenced", 0):
  3274. project_ref.append(["UseLibraryDependencyInputs", "false"])
  3275. break
  3276. group.append(project_ref)
  3277. references.append(group)
  3278. return references
  3279. def _GenerateMSBuildProject(project, options, version, generator_flags, spec):
  3280. spec = project.spec
  3281. configurations = spec["configurations"]
  3282. toolset = spec["toolset"]
  3283. project_dir, project_file_name = os.path.split(project.path)
  3284. gyp.common.EnsureDirExists(project.path)
  3285. # Prepare list of sources and excluded sources.
  3286. gyp_file = os.path.split(project.build_file)[1]
  3287. sources, excluded_sources = _PrepareListOfSources(spec, generator_flags, gyp_file)
  3288. # Add rules.
  3289. actions_to_add = {}
  3290. props_files_of_rules = set()
  3291. targets_files_of_rules = set()
  3292. rule_dependencies = set()
  3293. extension_to_rule_name = {}
  3294. list_excluded = generator_flags.get("msvs_list_excluded_files", True)
  3295. platforms = _GetUniquePlatforms(spec)
  3296. # Don't generate rules if we are using an external builder like ninja.
  3297. if not spec.get("msvs_external_builder"):
  3298. _GenerateRulesForMSBuild(
  3299. project_dir,
  3300. options,
  3301. spec,
  3302. sources,
  3303. excluded_sources,
  3304. props_files_of_rules,
  3305. targets_files_of_rules,
  3306. actions_to_add,
  3307. rule_dependencies,
  3308. extension_to_rule_name,
  3309. )
  3310. else:
  3311. rules = spec.get("rules", [])
  3312. _AdjustSourcesForRules(rules, sources, excluded_sources, True)
  3313. sources, excluded_sources, excluded_idl = _AdjustSourcesAndConvertToFilterHierarchy(
  3314. spec, options, project_dir, sources, excluded_sources, list_excluded, version
  3315. )
  3316. # Don't add actions if we are using an external builder like ninja.
  3317. if not spec.get("msvs_external_builder"):
  3318. _AddActions(actions_to_add, spec, project.build_file)
  3319. _AddCopies(actions_to_add, spec)
  3320. # NOTE: this stanza must appear after all actions have been decided.
  3321. # Don't excluded sources with actions attached, or they won't run.
  3322. excluded_sources = _FilterActionsFromExcluded(excluded_sources, actions_to_add)
  3323. exclusions = _GetExcludedFilesFromBuild(spec, excluded_sources, excluded_idl)
  3324. actions_spec, sources_handled_by_action = _GenerateActionsForMSBuild(
  3325. spec, actions_to_add
  3326. )
  3327. _GenerateMSBuildFiltersFile(
  3328. project.path + ".filters",
  3329. sources,
  3330. rule_dependencies,
  3331. extension_to_rule_name,
  3332. platforms,
  3333. toolset,
  3334. )
  3335. missing_sources = _VerifySourcesExist(sources, project_dir)
  3336. for configuration in configurations.values():
  3337. _FinalizeMSBuildSettings(spec, configuration)
  3338. # Add attributes to root element
  3339. import_default_section = [
  3340. ["Import", {"Project": r"$(VCTargetsPath)\Microsoft.Cpp.Default.props"}]
  3341. ]
  3342. import_cpp_props_section = [
  3343. ["Import", {"Project": r"$(VCTargetsPath)\Microsoft.Cpp.props"}]
  3344. ]
  3345. import_cpp_targets_section = [
  3346. ["Import", {"Project": r"$(VCTargetsPath)\Microsoft.Cpp.targets"}]
  3347. ]
  3348. import_masm_props_section = [
  3349. ["Import", {"Project": r"$(VCTargetsPath)\BuildCustomizations\masm.props"}]
  3350. ]
  3351. import_masm_targets_section = [
  3352. ["Import", {"Project": r"$(VCTargetsPath)\BuildCustomizations\masm.targets"}]
  3353. ]
  3354. import_marmasm_props_section = [
  3355. ["Import", {"Project": r"$(VCTargetsPath)\BuildCustomizations\marmasm.props"}]
  3356. ]
  3357. import_marmasm_targets_section = [
  3358. ["Import", {"Project": r"$(VCTargetsPath)\BuildCustomizations\marmasm.targets"}]
  3359. ]
  3360. macro_section = [["PropertyGroup", {"Label": "UserMacros"}]]
  3361. content = [
  3362. "Project",
  3363. {
  3364. "xmlns": "http://schemas.microsoft.com/developer/msbuild/2003",
  3365. "ToolsVersion": version.ProjectVersion(),
  3366. "DefaultTargets": "Build",
  3367. },
  3368. ]
  3369. content += _GetMSBuildProjectConfigurations(configurations, spec)
  3370. content += _GetMSBuildGlobalProperties(
  3371. spec, version, project.guid, project_file_name
  3372. )
  3373. content += import_default_section
  3374. content += _GetMSBuildConfigurationDetails(spec, project.build_file)
  3375. if spec.get("msvs_enable_winphone"):
  3376. content += _GetMSBuildLocalProperties("v120_wp81")
  3377. else:
  3378. content += _GetMSBuildLocalProperties(project.msbuild_toolset)
  3379. content += import_cpp_props_section
  3380. content += import_masm_props_section
  3381. if "arm64" in platforms and toolset == "target":
  3382. content += import_marmasm_props_section
  3383. content += _GetMSBuildExtensions(props_files_of_rules)
  3384. content += _GetMSBuildPropertySheets(configurations, spec)
  3385. content += macro_section
  3386. content += _GetMSBuildConfigurationGlobalProperties(
  3387. spec, configurations, project.build_file
  3388. )
  3389. content += _GetMSBuildToolSettingsSections(spec, configurations)
  3390. content += _GetMSBuildSources(
  3391. spec,
  3392. sources,
  3393. exclusions,
  3394. rule_dependencies,
  3395. extension_to_rule_name,
  3396. actions_spec,
  3397. sources_handled_by_action,
  3398. list_excluded,
  3399. )
  3400. content += _GetMSBuildProjectReferences(project)
  3401. content += import_cpp_targets_section
  3402. content += import_masm_targets_section
  3403. if "arm64" in platforms and toolset == "target":
  3404. content += import_marmasm_targets_section
  3405. content += _GetMSBuildExtensionTargets(targets_files_of_rules)
  3406. if spec.get("msvs_external_builder"):
  3407. content += _GetMSBuildExternalBuilderTargets(spec)
  3408. # TODO(jeanluc) File a bug to get rid of runas. We had in MSVS:
  3409. # has_run_as = _WriteMSVSUserFile(project.path, version, spec)
  3410. easy_xml.WriteXmlIfChanged(content, project.path, pretty=True, win32=True)
  3411. return missing_sources
  3412. def _GetMSBuildExternalBuilderTargets(spec):
  3413. """Return a list of MSBuild targets for external builders.
  3414. The "Build" and "Clean" targets are always generated. If the spec contains
  3415. 'msvs_external_builder_clcompile_cmd', then the "ClCompile" target will also
  3416. be generated, to support building selected C/C++ files.
  3417. Arguments:
  3418. spec: The gyp target spec.
  3419. Returns:
  3420. List of MSBuild 'Target' specs.
  3421. """
  3422. build_cmd = _BuildCommandLineForRuleRaw(
  3423. spec, spec["msvs_external_builder_build_cmd"], False, False, False, False
  3424. )
  3425. build_target = ["Target", {"Name": "Build"}]
  3426. build_target.append(["Exec", {"Command": build_cmd}])
  3427. clean_cmd = _BuildCommandLineForRuleRaw(
  3428. spec, spec["msvs_external_builder_clean_cmd"], False, False, False, False
  3429. )
  3430. clean_target = ["Target", {"Name": "Clean"}]
  3431. clean_target.append(["Exec", {"Command": clean_cmd}])
  3432. targets = [build_target, clean_target]
  3433. if spec.get("msvs_external_builder_clcompile_cmd"):
  3434. clcompile_cmd = _BuildCommandLineForRuleRaw(
  3435. spec,
  3436. spec["msvs_external_builder_clcompile_cmd"],
  3437. False,
  3438. False,
  3439. False,
  3440. False,
  3441. )
  3442. clcompile_target = ["Target", {"Name": "ClCompile"}]
  3443. clcompile_target.append(["Exec", {"Command": clcompile_cmd}])
  3444. targets.append(clcompile_target)
  3445. return targets
  3446. def _GetMSBuildExtensions(props_files_of_rules):
  3447. extensions = ["ImportGroup", {"Label": "ExtensionSettings"}]
  3448. for props_file in props_files_of_rules:
  3449. extensions.append(["Import", {"Project": props_file}])
  3450. return [extensions]
  3451. def _GetMSBuildExtensionTargets(targets_files_of_rules):
  3452. targets_node = ["ImportGroup", {"Label": "ExtensionTargets"}]
  3453. for targets_file in sorted(targets_files_of_rules):
  3454. targets_node.append(["Import", {"Project": targets_file}])
  3455. return [targets_node]
  3456. def _GenerateActionsForMSBuild(spec, actions_to_add):
  3457. """Add actions accumulated into an actions_to_add, merging as needed.
  3458. Arguments:
  3459. spec: the target project dict
  3460. actions_to_add: dictionary keyed on input name, which maps to a list of
  3461. dicts describing the actions attached to that input file.
  3462. Returns:
  3463. A pair of (action specification, the sources handled by this action).
  3464. """
  3465. sources_handled_by_action = OrderedSet()
  3466. actions_spec = []
  3467. for primary_input, actions in actions_to_add.items():
  3468. if generator_supports_multiple_toolsets:
  3469. primary_input = primary_input.replace(".exe", "_host.exe")
  3470. inputs = OrderedSet()
  3471. outputs = OrderedSet()
  3472. descriptions = []
  3473. commands = []
  3474. for action in actions:
  3475. def fixup_host_exe(i):
  3476. if "$(OutDir)" in i:
  3477. i = i.replace(".exe", "_host.exe")
  3478. return i
  3479. if generator_supports_multiple_toolsets:
  3480. action["inputs"] = [fixup_host_exe(i) for i in action["inputs"]]
  3481. inputs.update(OrderedSet(action["inputs"]))
  3482. outputs.update(OrderedSet(action["outputs"]))
  3483. descriptions.append(action["description"])
  3484. cmd = action["command"]
  3485. if generator_supports_multiple_toolsets:
  3486. cmd = cmd.replace(".exe", "_host.exe")
  3487. # For most actions, add 'call' so that actions that invoke batch files
  3488. # return and continue executing. msbuild_use_call provides a way to
  3489. # disable this but I have not seen any adverse effect from doing that
  3490. # for everything.
  3491. if action.get("msbuild_use_call", True):
  3492. cmd = "call " + cmd
  3493. commands.append(cmd)
  3494. # Add the custom build action for one input file.
  3495. description = ", and also ".join(descriptions)
  3496. # We can't join the commands simply with && because the command line will
  3497. # get too long. See also _AddActions: cygwin's setup_env mustn't be called
  3498. # for every invocation or the command that sets the PATH will grow too
  3499. # long.
  3500. command = "\r\n".join(
  3501. [c + "\r\nif %errorlevel% neq 0 exit /b %errorlevel%" for c in commands]
  3502. )
  3503. _AddMSBuildAction(
  3504. spec,
  3505. primary_input,
  3506. inputs,
  3507. outputs,
  3508. command,
  3509. description,
  3510. sources_handled_by_action,
  3511. actions_spec,
  3512. )
  3513. return actions_spec, sources_handled_by_action
  3514. def _AddMSBuildAction(
  3515. spec,
  3516. primary_input,
  3517. inputs,
  3518. outputs,
  3519. cmd,
  3520. description,
  3521. sources_handled_by_action,
  3522. actions_spec,
  3523. ):
  3524. command = MSVSSettings.ConvertVCMacrosToMSBuild(cmd)
  3525. primary_input = _FixPath(primary_input)
  3526. inputs_array = _FixPaths(inputs)
  3527. outputs_array = _FixPaths(outputs)
  3528. additional_inputs = ";".join([i for i in inputs_array if i != primary_input])
  3529. outputs = ";".join(outputs_array)
  3530. sources_handled_by_action.add(primary_input)
  3531. action_spec = ["CustomBuild", {"Include": primary_input}]
  3532. action_spec.extend(
  3533. # TODO(jeanluc) 'Document' for all or just if as_sources?
  3534. [
  3535. ["FileType", "Document"],
  3536. ["Command", command],
  3537. ["Message", description],
  3538. ["Outputs", outputs],
  3539. ]
  3540. )
  3541. if additional_inputs:
  3542. action_spec.append(["AdditionalInputs", additional_inputs])
  3543. actions_spec.append(action_spec)