diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index f492eb65e683e6..df10388a2215ee 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -24,6 +24,12 @@ /doc/guides/offboarding.md @nodejs/tsc /doc/guides/onboarding-extras.md @nodejs/tsc +# streams + +/lib/_stream* @nodejs/streams +/lib/stream.js @nodejs/streams +/lib/internal/streams/* @nodejs/streams + # net /deps/cares @nodejs/net @@ -98,3 +104,13 @@ /src/node_wasi* @nodejs/wasi /test/wasi/ @nodejs/wasi /test/fixtures/wasi/ @nodejs/wasi + +# Startup + +/benchmark/misc/startup.js @nodejs/startup +/src/node.cc @nodejs/startup +/src/node_code_cache_stub.cc @nodejs/startup +/src/node_native_module* @nodejs/startup +/lib/internal/bootstrap/* @nodejs/startup +/tools/code_cache/* @nodejs/startup +/tools/snapshot/* @nodejs/startup diff --git a/.github/workflows/commit-queue.yml b/.github/workflows/commit-queue.yml index 6f4affe656c9a1..cca9564f6c873b 100644 --- a/.github/workflows/commit-queue.yml +++ b/.github/workflows/commit-queue.yml @@ -21,6 +21,9 @@ jobs: steps: - uses: actions/checkout@v2 with: + # Needs the whole git history for ncu to work + # See https://github.com/nodejs/node-core-utils/pull/486 + fetch-depth: 0 # A personal token is required because pushing with GITHUB_TOKEN will # prevent commits from running CI after they land on master. It needs # to be set here because `checkout` configures GitHub authentication diff --git a/.gitignore b/.gitignore index 75039956cb18b1..4592f603e060ac 100644 --- a/.gitignore +++ b/.gitignore @@ -38,6 +38,9 @@ /out /*.msi /*.wixpdb +/*.qlog +# For GitHub Codespaces +/pythonenv* # === Rules for artifacts of `./configure` === /icu_config.gypi diff --git a/.mailmap b/.mailmap index ae585f1023dbc9..4597ae9e47f61d 100644 --- a/.mailmap +++ b/.mailmap @@ -53,13 +53,15 @@ Benjamin Coe Benjamin Fleischer Benjamin Fleischer Benjamin Gruenbaum Benjamin Gruenbaum +Benjamin Gruenbaum Benjamin Waters Bert Belder Bert Belder Bert Belder -Beth Griggs Bethany Griggs -Beth Griggs Bethany N Griggs -Beth Griggs BethGriggs +Beth Griggs Beth Griggs +Beth Griggs Bethany Griggs +Beth Griggs Bethany N Griggs +Beth Griggs BethGriggs Bidisha Pyne Brad Decker brad-decker Brad Larson BradLarson @@ -282,8 +284,9 @@ Minwoo Jung Minwoo Jung Miroslav Bajtoš Mitar Milutinovic -Myles Borins -Myles Borins +Myles Borins +Myles Borins +Myles Borins Nam Nguyen Nebu Pookins Netto Farah nettofarah @@ -314,6 +317,7 @@ Rebecca Turner Refael Ackermann Reza Akhavan jedireza Ricardo Sánchez Gregorio richnologies +Richard Lau Rick Olson Rob Adelmann Rob Adelmann adelmann @@ -324,6 +328,7 @@ Roman Reiss Ron Korving Ron Korving ronkorving Ruben Bridgewater +Ruben Bridgewater Russell Dempsey Ryan Dahl Ryan Emery @@ -369,8 +374,9 @@ Stefan Bühler Stephen Belanger Steve Mao Steven R. Loomis -Stewart X Addison Stewart Addison -Stewart X Addison sxa555 +Stewart X Addison Stewart Addison +Stewart X Addison sxa555 +Stewart X Addison Stewart X Addison Suraiya Hameed suraiyah Suramya shah ss22ever Surya Panikkal surya panikkal @@ -404,6 +410,8 @@ Tim Smart Tim Smart Timothy Leverett Timothy Tobias Nießen +Tom Atkinson +Tom Atkinson Tom Hughes Tom Hughes-Croucher Tom Purcell tpurcell diff --git a/AUTHORS b/AUTHORS index 3d637a17bc0fce..a61c4010be5d3c 100644 --- a/AUTHORS +++ b/AUTHORS @@ -808,7 +808,7 @@ David Boivin Liang-Chi Hsieh Timothy Gu Fábio Santos -Myles Borins +Myles Borins Jonas Dohse Коренберг Марк Caleb Boyd @@ -888,9 +888,9 @@ Julien Waechter Vitor Cortez Michael Theriot Peter Geiss -Richard Lau +Richard Lau José Moreira -Tom Atkinson +Tom Atkinson Glen Arrowsmith Jeff Harris Alexander Makarenko @@ -1035,7 +1035,7 @@ Joran Siu Vitaly Tomilov Ratikesh Misra Alex Perkins -Beth Griggs +Beth Griggs Joe Esposito Erin Spiceland Ravindra Barthwal @@ -1429,7 +1429,6 @@ Rj Bernaldo John F. Mercer Dejon "DJ" Gill Ahmad Nassri -Tom Atkinson Tobias Nießen Joseph Gentle Arunesh Chandra @@ -3021,5 +3020,124 @@ David Gilbertson Sergey Zelenov Eric Bickle Joe Pea +ExE Boss <3889017+ExE-Boss@users.noreply.github.com> +Mateusz Krawczuk +Jonathan MERCIER +Ujjwal Sharma +Jichan +Hassaan Pasha +Eric Dobbertin +Victor +Ling Samuel +Syohei YOSHIDA +Nitin Kumar +Christian Niederer +Sk Sajidul Kadir +Bartlomiej Brzozowski +Saajan +Yash Ladha +Xu Meng +Alex R +Hachimi Aa (Sfeir) +Daniel Estiven Rico Posada +Andrés M. Gómez +Nick Kreeger +Moni <40552237+NotMoni@users.noreply.github.com> +Dave Vandyke +Agustin Daguerre +Jesus Hernandez +龙腾道 +John Kleinschmidt +Cecchi MacNaughton +Reşat SABIQ +daemon1024 +Qinhui Chen +William Bonawentura +Vadzim Zieńka +David Daza <03dazal@gmail.com> +雨夜带刀 +karan singh virdi +William Armiros <54150514+willarmiros@users.noreply.github.com> +Matt Kulukundis +Edward Elric +Prosper Opara +Eileen +Ishaan Jain <37652866+ishaanjain1898@users.noreply.github.com> +Christopher Beeson +thecodrr +Chris Holland +Ranjan Purbey +白一梓 +John Leidegren +Karol Walasek +osher +szTheory +Jonathan Buhacoff +Paolo Insogna +Richard Townsend +Dan Fabulich +Deep310 <55121371+Deep310@users.noreply.github.com> +Dominykas Blyžė +Wenning Zhang <1527841714@qq.com> +shisama +Kirill Shatskiy +Ben Bucksch +Eli Schwartz +Maciej Kacper Jagiełło +Tom Nagle +rickyes +sapics +Sagar Jadhav +Dennis Ameling +Todd Short +Lucas Pardue +Pragyan Das +Shakil-Shahadat +wenningplus +WilliamConnatser <43946230+WilliamConnatser@users.noreply.github.com> +Benjamin Kaduk +Tatsuhiro Tsujikawa +Nathan Blair +patr0nus +Alec Davidson +falguniraina <48027052+falguniraina@users.noreply.github.com> +Rodion Abdurakhimov +Anentropic +Saleem +Julien Poissonnier +zombieleet +Nikola Glavina +Johannes Schöpp +Francisco Ryan Tolmasky I +Ye-hyoung Kang +Jucke +Pekka Nikander +iandrc +Tim Perry +Corey Butler +aetheryx +Robey Pointer +Priyank Singh +Gabriele Greco +Renato Mariscal +Matthew McEachen +Voltra +davkor +Carlos Lopez +Hilla Shahrabani +Frank Lemanschik +Justice Almanzar +Maksim Sinik +João Lucas Lucchetta +Danny Guo +Turner Jabbour +Igor Mikhalev +DeeDeeG +KuthorX +Pooja D.P +Shigma <33423008+Shigma@users.noreply.github.com> +atian25@qq.com +Amila Welihinda +schamberg97 <50446906+schamberg97@users.noreply.github.com> # Generated by tools/update-authors.js diff --git a/CHANGELOG.md b/CHANGELOG.md index ce5d46bf1382ca..56d6fd4d344600 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,7 +31,8 @@ release. -14.13.0
+14.13.1
+14.13.0
14.12.0
14.11.0
14.10.1
diff --git a/README.md b/README.md index ecacab8921ceba..5818362c6e431c 100644 --- a/README.md +++ b/README.md @@ -157,7 +157,7 @@ For information about the governance of the Node.js project, see * [apapirovski](https://github.com/apapirovski) - **Anatoli Papirovski** <apapirovski@mac.com> (he/him) * [BethGriggs](https://github.com/BethGriggs) - -**Beth Griggs** <Bethany.Griggs@uk.ibm.com> (she/her) +**Beth Griggs** <bgriggs@redhat.com> (she/her) * [BridgeAR](https://github.com/BridgeAR) - **Ruben Bridgewater** <ruben@bridgewater.de> (he/him) * [ChALkeR](https://github.com/ChALkeR) - @@ -257,7 +257,7 @@ For information about the governance of the Node.js project, see * [benjamingr](https://github.com/benjamingr) - **Benjamin Gruenbaum** <benjamingr@gmail.com> * [BethGriggs](https://github.com/BethGriggs) - -**Beth Griggs** <Bethany.Griggs@uk.ibm.com> (she/her) +**Beth Griggs** <bgriggs@redhat.com> (she/her) * [bmeck](https://github.com/bmeck) - **Bradley Farias** <bradley.meck@gmail.com> * [bmeurer](https://github.com/bmeurer) - @@ -383,7 +383,7 @@ For information about the governance of the Node.js project, see * [rexagod](https://github.com/rexagod) - **Pranshu Srivastava** <rexagod@gmail.com> (he/him) * [richardlau](https://github.com/richardlau) - -**Richard Lau** <riclau@uk.ibm.com> +**Richard Lau** <rlau@redhat.com> * [rickyes](https://github.com/rickyes) - **Ricky Zhou** <0x19951125@gmail.com> (he/him) * [ronag](https://github.com/ronag) - @@ -415,7 +415,7 @@ For information about the governance of the Node.js project, see * [starkwang](https://github.com/starkwang) - **Weijia Wang** <starkwang@126.com> * [sxa](https://github.com/sxa) - -**Stewart X Addison** <sxa@uk.ibm.com> +**Stewart X Addison** <sxa@redhat.com> (he/him) * [targos](https://github.com/targos) - **Michaël Zasso** <targos@protonmail.com> (he/him) * [TimothyGu](https://github.com/TimothyGu) - @@ -572,7 +572,7 @@ maintaining the Node.js project. Primary GPG keys for Node.js Releasers (some Releasers sign with subkeys): -* **Beth Griggs** <bethany.griggs@uk.ibm.com> +* **Beth Griggs** <bgriggs@redhat.com> `4ED778F539E3634C779C87C6D7062848A1AB005C` * **Colin Ihrig** <cjihrig@gmail.com> `94AE36675C464D64BAFA68DD7434390BDBE9B9C5` @@ -582,7 +582,7 @@ Primary GPG keys for Node.js Releasers (some Releasers sign with subkeys): `8FCCA13FEF1D0C2E91008E09770F7A9A5AE15600` * **Myles Borins** <myles.borins@gmail.com> `C4F0DFFF4E8C1A8236409D08E73BC641CC11F4C8` -* **Richard Lau** <riclau@uk.ibm.com> +* **Richard Lau** <rlau@redhat.com> `C82FA3AE1CBEDC6BE46B9360C43CEC45C17AB93C` * **Rod Vagg** <rod@vagg.org> `DD8F2338BAE7501E3DD5AC78C273792F7D83545D` diff --git a/common.gypi b/common.gypi index 46b4633f5d7e85..33b1325eb513ea 100644 --- a/common.gypi +++ b/common.gypi @@ -111,6 +111,9 @@ ['target_arch in "ppc64 s390x"', { 'v8_enable_backtrace': 1, }], + ['OS=="linux"', { + 'node_section_ordering_info%': '' + }] ], }, @@ -172,6 +175,20 @@ }, 'cflags': [ '-O3' ], 'conditions': [ + ['OS=="linux"', { + 'conditions': [ + ['node_section_ordering_info!=""', { + 'cflags': [ + '-fuse-ld=gold', + '-ffunction-sections', + ], + 'ldflags': [ + '-fuse-ld=gold', + '-Wl,--section-ordering-file=<(node_section_ordering_info)', + ], + }], + ], + }], ['OS=="solaris"', { # pull in V8's postmortem metadata 'ldflags': [ '-Wl,-z,allextract' ] diff --git a/configure.py b/configure.py index ff09f27f4d14af..a015b8ad1ae254 100755 --- a/configure.py +++ b/configure.py @@ -455,6 +455,14 @@ dest='node_use_large_pages_script_lld', help='This option has no effect. --use-largepages is now a runtime option.') +parser.add_option('--use-section-ordering-file', + action='store', + dest='node_section_ordering_info', + default='', + help='Pass a section ordering file to the linker. This requires that ' + + 'Node.js be linked using the gold linker. The gold linker must have ' + + 'version 1.2 or greater.') + intl_optgroup.add_option('--with-intl', action='store', dest='with_intl', @@ -1709,6 +1717,29 @@ def configure_inspector(o): options.without_ssl) o['variables']['v8_enable_inspector'] = 0 if disable_inspector else 1 +def configure_section_file(o): + try: + proc = subprocess.Popen(['ld.gold'] + ['-v'], stdin = subprocess.PIPE, + stdout = subprocess.PIPE, stderr = subprocess.PIPE) + except OSError: + warn('''No acceptable ld.gold linker found!''') + return 0 + + match = re.match(r"^GNU gold.*([0-9]+)\.([0-9]+)$", + proc.communicate()[0].decode("utf-8")) + + if match: + gold_major_version = match.group(1) + gold_minor_version = match.group(2) + if int(gold_major_version) == 1 and int(gold_minor_version) <= 1: + error('''GNU gold version must be greater than 1.2 in order to use section + reordering''') + + if options.node_section_ordering_info != "": + o['variables']['node_section_ordering_info'] = os.path.realpath( + str(options.node_section_ordering_info)) + else: + o['variables']['node_section_ordering_info'] = "" def make_bin_override(): if sys.platform == 'win32': @@ -1774,6 +1805,7 @@ def make_bin_override(): configure_intl(output) configure_static(output) configure_inspector(output) +configure_section_file(output) # Forward OSS-Fuzz settings output['variables']['ossfuzz'] = b(options.ossfuzz) diff --git a/deps/cjs-module-lexer/lexer.js b/deps/cjs-module-lexer/lexer.js index 6a9eef9edd7c21..7f7c971fcc15bd 100755 --- a/deps/cjs-module-lexer/lexer.js +++ b/deps/cjs-module-lexer/lexer.js @@ -10,6 +10,7 @@ let openTokenDepth, nextBraceIsClass, starExportMap, lastStarExportSpecifier, + lastExportsAssignSpecifier, _exports, reexports; @@ -25,11 +26,17 @@ function resetState () { nextBraceIsClass = false; starExportMap = Object.create(null); lastStarExportSpecifier = null; + lastExportsAssignSpecifier = null; _exports = new Set(); reexports = new Set(); } +// RequireType +const Import = 0; +const ExportAssign = 1; +const ExportStar = 2; + const strictReserved = new Set(['implements', 'interface', 'let', 'package', 'private', 'protected', 'public', 'static', 'yield', 'enum']); module.exports = function parseCJS (source, name = '@') { @@ -42,6 +49,8 @@ module.exports = function parseCJS (source, name = '@') { e.loc = pos; throw e; } + if (lastExportsAssignSpecifier) + reexports.add(lastExportsAssignSpecifier); const result = { exports: [..._exports], reexports: [...reexports] }; resetState(); return result; @@ -79,25 +88,25 @@ function parseSource (cjsSource) { if (openTokenDepth === 0) { switch (ch) { case 105/*i*/: - if (source.slice(pos + 1, pos + 6) === 'mport' && keywordStart(pos)) + if (source.startsWith('mport', pos + 1) && keywordStart(pos)) throwIfImportStatement(); lastTokenPos = pos; continue; case 114/*r*/: const startPos = pos; - if (tryParseRequire(false) && keywordStart(startPos)) + if (tryParseRequire(Import) && keywordStart(startPos)) tryBacktrackAddStarExportBinding(startPos - 1); lastTokenPos = pos; continue; case 95/*_*/: - if (source.slice(pos + 1, pos + 8) === '_export' && (keywordStart(pos) || source.charCodeAt(pos - 1) === 46/*.*/)) { + if (source.startsWith('_export', pos + 1) && (keywordStart(pos) || source.charCodeAt(pos - 1) === 46/*.*/)) { pos += 8; - if (source.slice(pos, pos + 4) === 'Star') + if (source.startsWith('Star', pos)) pos += 4; if (source.charCodeAt(pos) === 40/*(*/) { openTokenPosStack[openTokenDepth++] = lastTokenPos; if (source.charCodeAt(++pos) === 114/*r*/) - tryParseRequire(true); + tryParseRequire(ExportStar); } } lastTokenPos = pos; @@ -107,7 +116,7 @@ function parseSource (cjsSource) { switch (ch) { case 101/*e*/: - if (source.slice(pos + 1, pos + 6) === 'xport' && keywordStart(pos)) { + if (source.startsWith('xport', pos + 1) && keywordStart(pos)) { if (source.charCodeAt(pos + 6) === 115/*s*/) tryParseExportsDotAssign(false); else if (openTokenDepth === 0) @@ -115,15 +124,15 @@ function parseSource (cjsSource) { } break; case 99/*c*/: - if (keywordStart(pos) && source.slice(pos + 1, pos + 5) === 'lass' && isBrOrWs(source.charCodeAt(pos + 5))) + if (keywordStart(pos) && source.startsWith('lass', pos + 1) && isBrOrWs(source.charCodeAt(pos + 5))) nextBraceIsClass = true; break; case 109/*m*/: - if (source.slice(pos + 1, pos + 6) === 'odule' && keywordStart(pos)) + if (source.startsWith('odule', pos + 1) && keywordStart(pos)) tryParseModuleExportsDotAssign(); break; case 79/*O*/: - if (source.slice(pos + 1, pos + 6) === 'bject' && keywordStart(pos)) + if (source.startsWith('bject', pos + 1) && keywordStart(pos)) tryParseObjectDefineOrKeys(openTokenDepth === 0); break; case 40/*(*/: @@ -233,11 +242,11 @@ function tryBacktrackAddStarExportBinding (bPos) { bPos--; switch (source.charCodeAt(bPos)) { case 114/*r*/: - if (source.slice(bPos - 2, bPos) !== 'va') + if (!source.startsWith('va', bPos - 2)) return; break; case 116/*t*/: - if (source.slice(bPos - 2, bPos) !== 'le' && source.slice(bPos - 4, bPos) !== 'cons') + if (!source.startsWith('le', bPos - 2) && !source.startsWith('cons', bPos - 4)) return; break; default: return; @@ -254,7 +263,7 @@ function tryParseObjectDefineOrKeys (keys) { if (ch === 46/*.*/) { pos++; ch = commentWhitespace(); - if (ch === 100/*d*/ && source.slice(pos + 1, pos + 14) === 'efineProperty') { + if (ch === 100/*d*/ && source.startsWith('efineProperty', pos + 1)) { pos += 14; revertPos = pos - 1; ch = commentWhitespace(); @@ -279,7 +288,7 @@ function tryParseObjectDefineOrKeys (keys) { } } } - else if (keys && ch === 107/*k*/ && source.slice(pos + 1, pos + 4) === 'eys') { + else if (keys && ch === 107/*k*/ && source.startsWith('eys', pos + 1)) { while (true) { pos += 4; revertPos = pos - 1; @@ -298,14 +307,14 @@ function tryParseObjectDefineOrKeys (keys) { if (ch !== 46/*.*/) break; pos++; ch = commentWhitespace(); - if (ch !== 102/*f*/ || source.slice(pos + 1, pos + 7) !== 'orEach') break; + if (ch !== 102/*f*/ || !source.startsWith('orEach', pos + 1)) break; pos += 7; ch = commentWhitespace(); revertPos = pos - 1; if (ch !== 40/*(*/) break; pos++; ch = commentWhitespace(); - if (ch !== 102/*f*/ || source.slice(pos + 1, pos + 8) !== 'unction') break; + if (ch !== 102/*f*/ || !source.startsWith('unction', pos + 1)) break; pos += 8; ch = commentWhitespace(); if (ch !== 40/*(*/) break; @@ -321,23 +330,23 @@ function tryParseObjectDefineOrKeys (keys) { if (ch !== 123/*{*/) break; pos++; ch = commentWhitespace(); - if (ch !== 105/*i*/ || source.slice(pos + 1, pos + 3) !== 'f ') break; + if (ch !== 105/*i*/ || !source.startsWith('f ', pos + 1)) break; pos += 3; ch = commentWhitespace(); if (ch !== 40/*(*/) break; pos++; ch = commentWhitespace(); - if (it_id !== source.slice(pos, pos + it_id.length)) break; + if (!source.startsWith(it_id, pos)) break; pos += it_id.length; ch = commentWhitespace(); // `if (` IDENTIFIER$2 `===` ( `'default'` | `"default"` ) `||` IDENTIFIER$2 `===` ( '__esModule' | `"__esModule"` ) `) return` `;`? | if (ch === 61/*=*/) { - if (source.slice(pos + 1, pos + 3) !== '==') break; + if (!source.startsWith('==', pos + 1)) break; pos += 3; ch = commentWhitespace(); if (ch !== 34/*"*/ && ch !== 39/*'*/) break; let quot = ch; - if (source.slice(pos + 1, pos + 8) !== 'default') break; + if (!source.startsWith('default', pos + 1)) break; pos += 8; ch = commentWhitespace(); if (ch !== quot) break; @@ -354,7 +363,7 @@ function tryParseObjectDefineOrKeys (keys) { ch = commentWhitespace(); if (ch !== 34/*"*/ && ch !== 39/*'*/) break; quot = ch; - if (source.slice(pos + 1, pos + 11) !== '__esModule') break; + if (!source.startsWith('__esModule', pos + 1)) break; pos += 11; ch = commentWhitespace(); if (ch !== quot) break; @@ -363,7 +372,7 @@ function tryParseObjectDefineOrKeys (keys) { if (ch !== 41/*)*/) break; pos += 1; ch = commentWhitespace(); - if (ch !== 114/*r*/ || source.slice(pos + 1, pos + 6) !== 'eturn') break; + if (ch !== 114/*r*/ || !source.startsWith('eturn', pos + 1)) break; pos += 6; ch = commentWhitespace(); if (ch === 59/*;*/) @@ -372,12 +381,12 @@ function tryParseObjectDefineOrKeys (keys) { } // `if (` IDENTIFIER$2 `!==` ( `'default'` | `"default"` ) `)` else if (ch === 33/*!*/) { - if (source.slice(pos + 1, pos + 3) !== '==') break; + if (!source.startsWith('==', pos + 1)) break; pos += 3; ch = commentWhitespace(); if (ch !== 34/*"*/ && ch !== 39/*'*/) break; const quot = ch; - if (source.slice(pos + 1, pos + 8) !== 'default') break; + if (!source.startsWith('default', pos + 1)) break; pos += 8; ch = commentWhitespace(); if (ch !== quot) break; @@ -429,7 +438,7 @@ function tryParseObjectDefineOrKeys (keys) { if (ch !== 46/*.*/) break; pos++; ch = commentWhitespace(); - if (ch !== 100/*d*/ || source.slice(pos + 1, pos + 14) !== 'efineProperty') break; + if (ch !== 100/*d*/ || !source.startsWith('efineProperty', pos + 1)) break; pos += 14; ch = commentWhitespace(); if (ch !== 40/*(*/) break; @@ -440,7 +449,7 @@ function tryParseObjectDefineOrKeys (keys) { if (ch !== 44/*,*/) break; pos++; ch = commentWhitespace(); - if (source.slice(pos, pos + it_id.length) !== it_id) break; + if (!source.startsWith(it_id, pos)) break; pos += it_id.length; ch = commentWhitespace(); if (ch !== 44/*,*/) break; @@ -449,25 +458,25 @@ function tryParseObjectDefineOrKeys (keys) { if (ch !== 123/*{*/) break; pos++; ch = commentWhitespace(); - if (ch !== 101/*e*/ || source.slice(pos + 1, pos + 10) !== 'numerable') break; + if (ch !== 101/*e*/ || !source.startsWith('numerable', pos + 1)) break; pos += 10; ch = commentWhitespace(); if (ch !== 58/*:*/) break; pos++; ch = commentWhitespace(); - if (ch !== 116/*t*/ && source.slice(pos + 1, pos + 4) !== 'rue') break; + if (ch !== 116/*t*/ && !source.startsWith('rue', pos + 1)) break; pos += 4; ch = commentWhitespace(); if (ch !== 44/*,*/) break; pos++; ch = commentWhitespace(); - if (ch !== 103/*g*/ || source.slice(pos + 1, pos + 3) !== 'et') break; + if (ch !== 103/*g*/ || !source.startsWith('et', pos + 1)) break; pos += 3; ch = commentWhitespace(); if (ch !== 58/*:*/) break; pos++; ch = commentWhitespace(); - if (ch !== 102/*f*/ || source.slice(pos + 1, pos + 8) !== 'unction') break; + if (ch !== 102/*f*/ || !source.startsWith('unction', pos + 1)) break; pos += 8; ch = commentWhitespace(); if (ch !== 40/*(*/) break; @@ -479,16 +488,16 @@ function tryParseObjectDefineOrKeys (keys) { if (ch !== 123/*{*/) break; pos++; ch = commentWhitespace(); - if (ch !== 114/*r*/ || source.slice(pos + 1, pos + 6) !== 'eturn') break; + if (ch !== 114/*r*/ || !source.startsWith('eturn', pos + 1)) break; pos += 6; ch = commentWhitespace(); - if (source.slice(pos, pos + id.length) !== id) break; + if (!source.startsWith(id, pos)) break; pos += id.length; ch = commentWhitespace(); if (ch !== 91/*[*/) break; pos++; ch = commentWhitespace(); - if (source.slice(pos, pos + it_id.length) !== it_id) break; + if (!source.startsWith(it_id, pos)) break; pos += it_id.length; ch = commentWhitespace(); if (ch !== 93/*]*/) break; @@ -534,7 +543,7 @@ function tryParseObjectDefineOrKeys (keys) { function readExportsOrModuleDotExports (ch) { const revertPos = pos; - if (ch === 109/*m*/ && source.slice(pos + 1, pos + 6) === 'odule') { + if (ch === 109/*m*/ && source.startsWith('odule', pos + 1)) { pos += 6; ch = commentWhitespace(); if (ch !== 46/*.*/) { @@ -544,7 +553,7 @@ function readExportsOrModuleDotExports (ch) { pos++; ch = commentWhitespace(); } - if (ch === 101/*e*/ && source.slice(pos + 1, pos + 7) === 'xports') { + if (ch === 101/*e*/ && source.startsWith('xports', pos + 1)) { pos += 7; return true; } @@ -561,7 +570,7 @@ function tryParseModuleExportsDotAssign () { if (ch === 46/*.*/) { pos++; ch = commentWhitespace(); - if (ch === 101/*e*/ && source.slice(pos + 1, pos + 7) === 'xports') { + if (ch === 101/*e*/ && source.startsWith('xports', pos + 1)) { tryParseExportsDotAssign(true); return; } @@ -623,16 +632,16 @@ function tryParseExportsDotAssign (assign) { // require('...') if (ch === 114/*r*/) - tryParseRequire(true); + tryParseRequire(ExportAssign); } } } pos = revertPos; } -function tryParseRequire (directStarExport) { +function tryParseRequire (requireType) { // require('...') - if (source.slice(pos + 1, pos + 7) === 'equire') { + if (source.startsWith('equire', pos + 1)) { pos += 7; const revertPos = pos - 1; let ch = commentWhitespace(); @@ -645,13 +654,17 @@ function tryParseRequire (directStarExport) { const reexportEnd = pos++; ch = commentWhitespace(); if (ch === 41/*)*/) { - if (directStarExport) { - reexports.add(source.slice(reexportStart, reexportEnd)); - } - else { - lastStarExportSpecifier = source.slice(reexportStart, reexportEnd); + switch (requireType) { + case ExportAssign: + lastExportsAssignSpecifier = source.slice(reexportStart, reexportEnd); + return true; + case ExportStar: + reexports.add(source.slice(reexportStart, reexportEnd)); + return true; + default: + lastStarExportSpecifier = source.slice(reexportStart, reexportEnd); + return true; } - return true; } } else if (ch === 34/*"*/) { @@ -659,13 +672,17 @@ function tryParseRequire (directStarExport) { const reexportEnd = pos++; ch = commentWhitespace(); if (ch === 41/*)*/) { - if (directStarExport) { - reexports.add(source.slice(reexportStart, reexportEnd)); - } - else { - lastStarExportSpecifier = source.slice(reexportStart, reexportEnd); + switch (requireType) { + case ExportAssign: + lastExportsAssignSpecifier = source.slice(reexportStart, reexportEnd); + return true; + case ExportStar: + reexports.add(source.slice(reexportStart, reexportEnd)); + return true; + default: + lastStarExportSpecifier = source.slice(reexportStart, reexportEnd); + return true; } - return true; } } } @@ -1029,7 +1046,7 @@ function keywordStart (pos) { function readPrecedingKeyword (pos, match) { if (pos < match.length - 1) return false; - return source.slice(pos - match.length + 1, pos + 1) === match && (pos === 0 || isBrOrWsOrPunctuatorNotDot(source.charCodeAt(pos - match.length))); + return source.startsWith(match, pos - match.length + 1) && (pos === 0 || isBrOrWsOrPunctuatorNotDot(source.charCodeAt(pos - match.length))); } function readPrecedingKeyword1 (pos, ch) { @@ -1111,8 +1128,8 @@ function isExpressionKeyword (pos) { } function isParenKeyword (curPos) { - return source.charCodeAt(curPos) === 101/*e*/ && source.slice(curPos - 4, curPos) === 'whil' || - source.charCodeAt(curPos) === 114/*r*/ && source.slice(curPos - 2, curPos) === 'fo' || + return source.charCodeAt(curPos) === 101/*e*/ && source.startsWith('whil', curPos - 4) || + source.charCodeAt(curPos) === 114/*r*/ && source.startsWith('fo', curPos - 2) || source.charCodeAt(curPos - 1) === 105/*i*/ && source.charCodeAt(curPos) === 102/*f*/; } @@ -1142,11 +1159,11 @@ function isExpressionTerminator (curPos) { case 41/*)*/: return true; case 104/*h*/: - return source.slice(curPos - 4, curPos) === 'catc'; + return source.startsWith('catc', curPos - 4); case 121/*y*/: - return source.slice(curPos - 6, curPos) === 'finall'; + return source.startsWith('finall', curPos - 6); case 101/*e*/: - return source.slice(curPos - 3, curPos) === 'els'; + return source.startsWith('els', curPos - 3); } return false; } diff --git a/deps/cjs-module-lexer/package.json b/deps/cjs-module-lexer/package.json index 3f411a29dd3799..9859ccaaaaca46 100755 --- a/deps/cjs-module-lexer/package.json +++ b/deps/cjs-module-lexer/package.json @@ -1,6 +1,6 @@ { "name": "cjs-module-lexer", - "version": "0.3.3", + "version": "0.4.0", "description": "Lexes CommonJS modules, returning their named exports metadata", "main": "lexer.js", "exports": { diff --git a/deps/llhttp/README.md b/deps/llhttp/README.md index f1998e923af4f9..c95e8d045843e0 100644 --- a/deps/llhttp/README.md +++ b/deps/llhttp/README.md @@ -22,7 +22,7 @@ were tried. However, all of them failed due to resulting significant performance degradation. This project is a port of [http_parser][0] to TypeScript. [llparse][1] is used -to generate the output C and/or bitcode artifacts, which could be compiled and +to generate the output C source file, which could be compiled and linked with the embedder's program (like [Node.js][7]). ## Performance @@ -31,7 +31,7 @@ So far llhttp outperforms http_parser: | | input size | bandwidth | reqs/sec | time | |:----------------|-----------:|-------------:|-----------:|--------:| -| **llhttp** _(C)_ | 8192.00 mb | 1777.24 mb/s | 3583799.39 ops/sec | 4.61 s | +| **llhttp** | 8192.00 mb | 1777.24 mb/s | 3583799.39 ops/sec | 4.61 s | | **http_parser** | 8192.00 mb | 694.66 mb/s | 1406180.33 req/sec | 11.79 s | llhttp is faster by approximately **156%**. diff --git a/deps/llhttp/include/llhttp.h b/deps/llhttp/include/llhttp.h index bfa290880580c1..4bf4d8b0cef85f 100644 --- a/deps/llhttp/include/llhttp.h +++ b/deps/llhttp/include/llhttp.h @@ -3,7 +3,7 @@ #define LLHTTP_VERSION_MAJOR 2 #define LLHTTP_VERSION_MINOR 1 -#define LLHTTP_VERSION_PATCH 2 +#define LLHTTP_VERSION_PATCH 3 #ifndef LLHTTP_STRICT_MODE # define LLHTTP_STRICT_MODE 0 diff --git a/deps/llhttp/src/llhttp.c b/deps/llhttp/src/llhttp.c index 56cd1f58486b38..acc35479f88ca6 100644 --- a/deps/llhttp/src/llhttp.c +++ b/deps/llhttp/src/llhttp.c @@ -49,7 +49,7 @@ static const unsigned char llparse_blob7[] = { }; #ifdef __SSE4_2__ static const unsigned char ALIGN(16) llparse_blob8[] = { - 0x9, 0x9, ' ', '~', 0x80, 0xfe, 0x0, 0x0, 0x0, 0x0, 0x0, + 0x9, 0x9, ' ', '~', 0x80, 0xff, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 }; #endif /* __SSE4_2__ */ @@ -616,9 +616,9 @@ int llhttp__internal__c_mul_add_content_length( if (state->content_length > 0xffffffffffffffffULL / 16) { return 1; } - + state->content_length *= 16; - + /* Addition overflow */ if (match >= 0) { if (state->content_length > 0xffffffffffffffffULL - match) { @@ -780,9 +780,9 @@ int llhttp__internal__c_mul_add_content_length_1( if (state->content_length > 0xffffffffffffffffULL / 10) { return 1; } - + state->content_length *= 10; - + /* Addition overflow */ if (match >= 0) { if (state->content_length > 0xffffffffffffffffULL - match) { @@ -871,9 +871,9 @@ int llhttp__internal__c_mul_add_status_code( if (state->status_code > 0xffff / 10) { return 1; } - + state->status_code *= 10; - + /* Addition overflow */ if (match >= 0) { if (state->status_code > 0xffff - match) { @@ -885,7 +885,7 @@ int llhttp__internal__c_mul_add_status_code( } } state->status_code += match; - + /* Enforce maximum */ if (state->status_code > 999) { return 1; @@ -991,7 +991,7 @@ static llparse_state_t llhttp__internal__run( case s_n_llhttp__internal__n_chunk_data_almost_done: s_n_llhttp__internal__n_chunk_data_almost_done: { llparse_match_t match_seq; - + if (p == endp) { return s_n_llhttp__internal__n_chunk_data_almost_done; } @@ -1016,7 +1016,7 @@ static llparse_state_t llhttp__internal__run( s_n_llhttp__internal__n_consume_content_length: { size_t avail; size_t need; - + avail = endp - p; need = state->content_length; if (avail >= need) { @@ -1024,7 +1024,7 @@ static llparse_state_t llhttp__internal__run( state->content_length = 0; goto s_n_llhttp__internal__n_span_end_llhttp__on_body; } - + state->content_length -= avail; return s_n_llhttp__internal__n_consume_content_length; /* UNREACHABLE */; @@ -1371,7 +1371,7 @@ static llparse_state_t llhttp__internal__run( s_n_llhttp__internal__n_consume_content_length_1: { size_t avail; size_t need; - + avail = endp - p; need = state->content_length; if (avail >= need) { @@ -1379,7 +1379,7 @@ static llparse_state_t llhttp__internal__run( state->content_length = 0; goto s_n_llhttp__internal__n_span_end_llhttp__on_body_1; } - + state->content_length -= avail; return s_n_llhttp__internal__n_consume_content_length_1; /* UNREACHABLE */; @@ -1595,7 +1595,7 @@ static llparse_state_t llhttp__internal__run( 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }; if (p == endp) { return s_n_llhttp__internal__n_header_value_connection_token; @@ -1646,7 +1646,7 @@ static llparse_state_t llhttp__internal__run( case s_n_llhttp__internal__n_header_value_connection_1: s_n_llhttp__internal__n_header_value_connection_1: { llparse_match_t match_seq; - + if (p == endp) { return s_n_llhttp__internal__n_header_value_connection_1; } @@ -1670,7 +1670,7 @@ static llparse_state_t llhttp__internal__run( case s_n_llhttp__internal__n_header_value_connection_2: s_n_llhttp__internal__n_header_value_connection_2: { llparse_match_t match_seq; - + if (p == endp) { return s_n_llhttp__internal__n_header_value_connection_2; } @@ -1694,7 +1694,7 @@ static llparse_state_t llhttp__internal__run( case s_n_llhttp__internal__n_header_value_connection_3: s_n_llhttp__internal__n_header_value_connection_3: { llparse_match_t match_seq; - + if (p == endp) { return s_n_llhttp__internal__n_header_value_connection_3; } @@ -1916,7 +1916,7 @@ static llparse_state_t llhttp__internal__run( 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }; if (p == endp) { return s_n_llhttp__internal__n_header_value; @@ -1927,17 +1927,17 @@ static llparse_state_t llhttp__internal__run( __m128i input; int avail; int match_len; - + /* Load input */ input = _mm_loadu_si128((__m128i const*) p); ranges = _mm_loadu_si128((__m128i const*) llparse_blob8); - + /* Find first character that does not match `ranges` */ match_len = _mm_cmpestri(ranges, 6, input, 16, _SIDD_UBYTE_OPS | _SIDD_CMP_RANGES | _SIDD_NEGATIVE_POLARITY); - + if (match_len != 0) { p += match_len; goto s_n_llhttp__internal__n_header_value; @@ -1975,7 +1975,7 @@ static llparse_state_t llhttp__internal__run( 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }; if (p == endp) { return s_n_llhttp__internal__n_header_value_te_token; @@ -1999,7 +1999,7 @@ static llparse_state_t llhttp__internal__run( case s_n_llhttp__internal__n_header_value_te_chunked: s_n_llhttp__internal__n_header_value_te_chunked: { llparse_match_t match_seq; - + if (p == endp) { return s_n_llhttp__internal__n_header_value_te_chunked; } @@ -2105,29 +2105,29 @@ static llparse_state_t llhttp__internal__run( __m128i input; int avail; int match_len; - + /* Load input */ input = _mm_loadu_si128((__m128i const*) p); ranges = _mm_loadu_si128((__m128i const*) llparse_blob9); - + /* Find first character that does not match `ranges` */ match_len = _mm_cmpestri(ranges, 16, input, 16, _SIDD_UBYTE_OPS | _SIDD_CMP_RANGES | _SIDD_NEGATIVE_POLARITY); - + if (match_len != 0) { p += match_len; goto s_n_llhttp__internal__n_header_field_general; } ranges = _mm_loadu_si128((__m128i const*) llparse_blob10); - + /* Find first character that does not match `ranges` */ match_len = _mm_cmpestri(ranges, 2, input, 16, _SIDD_UBYTE_OPS | _SIDD_CMP_RANGES | _SIDD_NEGATIVE_POLARITY); - + if (match_len != 0) { p += match_len; goto s_n_llhttp__internal__n_header_field_general; @@ -2170,7 +2170,7 @@ static llparse_state_t llhttp__internal__run( case s_n_llhttp__internal__n_header_field_3: s_n_llhttp__internal__n_header_field_3: { llparse_match_t match_seq; - + if (p == endp) { return s_n_llhttp__internal__n_header_field_3; } @@ -2195,7 +2195,7 @@ static llparse_state_t llhttp__internal__run( case s_n_llhttp__internal__n_header_field_4: s_n_llhttp__internal__n_header_field_4: { llparse_match_t match_seq; - + if (p == endp) { return s_n_llhttp__internal__n_header_field_4; } @@ -2241,7 +2241,7 @@ static llparse_state_t llhttp__internal__run( case s_n_llhttp__internal__n_header_field_1: s_n_llhttp__internal__n_header_field_1: { llparse_match_t match_seq; - + if (p == endp) { return s_n_llhttp__internal__n_header_field_1; } @@ -2265,7 +2265,7 @@ static llparse_state_t llhttp__internal__run( case s_n_llhttp__internal__n_header_field_5: s_n_llhttp__internal__n_header_field_5: { llparse_match_t match_seq; - + if (p == endp) { return s_n_llhttp__internal__n_header_field_5; } @@ -2290,7 +2290,7 @@ static llparse_state_t llhttp__internal__run( case s_n_llhttp__internal__n_header_field_6: s_n_llhttp__internal__n_header_field_6: { llparse_match_t match_seq; - + if (p == endp) { return s_n_llhttp__internal__n_header_field_6; } @@ -2315,7 +2315,7 @@ static llparse_state_t llhttp__internal__run( case s_n_llhttp__internal__n_header_field_7: s_n_llhttp__internal__n_header_field_7: { llparse_match_t match_seq; - + if (p == endp) { return s_n_llhttp__internal__n_header_field_7; } @@ -2666,7 +2666,7 @@ static llparse_state_t llhttp__internal__run( case s_n_llhttp__internal__n_req_http_start_1: s_n_llhttp__internal__n_req_http_start_1: { llparse_match_t match_seq; - + if (p == endp) { return s_n_llhttp__internal__n_req_http_start_1; } @@ -2690,7 +2690,7 @@ static llparse_state_t llhttp__internal__run( case s_n_llhttp__internal__n_req_http_start_2: s_n_llhttp__internal__n_req_http_start_2: { llparse_match_t match_seq; - + if (p == endp) { return s_n_llhttp__internal__n_req_http_start_2; } @@ -3369,7 +3369,7 @@ static llparse_state_t llhttp__internal__run( case s_n_llhttp__internal__n_start_req_1: s_n_llhttp__internal__n_start_req_1: { llparse_match_t match_seq; - + if (p == endp) { return s_n_llhttp__internal__n_start_req_1; } @@ -3394,7 +3394,7 @@ static llparse_state_t llhttp__internal__run( case s_n_llhttp__internal__n_start_req_2: s_n_llhttp__internal__n_start_req_2: { llparse_match_t match_seq; - + if (p == endp) { return s_n_llhttp__internal__n_start_req_2; } @@ -3419,7 +3419,7 @@ static llparse_state_t llhttp__internal__run( case s_n_llhttp__internal__n_start_req_4: s_n_llhttp__internal__n_start_req_4: { llparse_match_t match_seq; - + if (p == endp) { return s_n_llhttp__internal__n_start_req_4; } @@ -3444,7 +3444,7 @@ static llparse_state_t llhttp__internal__run( case s_n_llhttp__internal__n_start_req_6: s_n_llhttp__internal__n_start_req_6: { llparse_match_t match_seq; - + if (p == endp) { return s_n_llhttp__internal__n_start_req_6; } @@ -3529,7 +3529,7 @@ static llparse_state_t llhttp__internal__run( case s_n_llhttp__internal__n_start_req_8: s_n_llhttp__internal__n_start_req_8: { llparse_match_t match_seq; - + if (p == endp) { return s_n_llhttp__internal__n_start_req_8; } @@ -3554,7 +3554,7 @@ static llparse_state_t llhttp__internal__run( case s_n_llhttp__internal__n_start_req_9: s_n_llhttp__internal__n_start_req_9: { llparse_match_t match_seq; - + if (p == endp) { return s_n_llhttp__internal__n_start_req_9; } @@ -3579,7 +3579,7 @@ static llparse_state_t llhttp__internal__run( case s_n_llhttp__internal__n_start_req_10: s_n_llhttp__internal__n_start_req_10: { llparse_match_t match_seq; - + if (p == endp) { return s_n_llhttp__internal__n_start_req_10; } @@ -3604,7 +3604,7 @@ static llparse_state_t llhttp__internal__run( case s_n_llhttp__internal__n_start_req_12: s_n_llhttp__internal__n_start_req_12: { llparse_match_t match_seq; - + if (p == endp) { return s_n_llhttp__internal__n_start_req_12; } @@ -3629,7 +3629,7 @@ static llparse_state_t llhttp__internal__run( case s_n_llhttp__internal__n_start_req_13: s_n_llhttp__internal__n_start_req_13: { llparse_match_t match_seq; - + if (p == endp) { return s_n_llhttp__internal__n_start_req_13; } @@ -3675,7 +3675,7 @@ static llparse_state_t llhttp__internal__run( case s_n_llhttp__internal__n_start_req_15: s_n_llhttp__internal__n_start_req_15: { llparse_match_t match_seq; - + if (p == endp) { return s_n_llhttp__internal__n_start_req_15; } @@ -3700,7 +3700,7 @@ static llparse_state_t llhttp__internal__run( case s_n_llhttp__internal__n_start_req_16: s_n_llhttp__internal__n_start_req_16: { llparse_match_t match_seq; - + if (p == endp) { return s_n_llhttp__internal__n_start_req_16; } @@ -3725,7 +3725,7 @@ static llparse_state_t llhttp__internal__run( case s_n_llhttp__internal__n_start_req_18: s_n_llhttp__internal__n_start_req_18: { llparse_match_t match_seq; - + if (p == endp) { return s_n_llhttp__internal__n_start_req_18; } @@ -3750,7 +3750,7 @@ static llparse_state_t llhttp__internal__run( case s_n_llhttp__internal__n_start_req_20: s_n_llhttp__internal__n_start_req_20: { llparse_match_t match_seq; - + if (p == endp) { return s_n_llhttp__internal__n_start_req_20; } @@ -3835,7 +3835,7 @@ static llparse_state_t llhttp__internal__run( case s_n_llhttp__internal__n_start_req_22: s_n_llhttp__internal__n_start_req_22: { llparse_match_t match_seq; - + if (p == endp) { return s_n_llhttp__internal__n_start_req_22; } @@ -3889,7 +3889,7 @@ static llparse_state_t llhttp__internal__run( case s_n_llhttp__internal__n_start_req_23: s_n_llhttp__internal__n_start_req_23: { llparse_match_t match_seq; - + if (p == endp) { return s_n_llhttp__internal__n_start_req_23; } @@ -3914,7 +3914,7 @@ static llparse_state_t llhttp__internal__run( case s_n_llhttp__internal__n_start_req_24: s_n_llhttp__internal__n_start_req_24: { llparse_match_t match_seq; - + if (p == endp) { return s_n_llhttp__internal__n_start_req_24; } @@ -3939,7 +3939,7 @@ static llparse_state_t llhttp__internal__run( case s_n_llhttp__internal__n_start_req_26: s_n_llhttp__internal__n_start_req_26: { llparse_match_t match_seq; - + if (p == endp) { return s_n_llhttp__internal__n_start_req_26; } @@ -3964,7 +3964,7 @@ static llparse_state_t llhttp__internal__run( case s_n_llhttp__internal__n_start_req_27: s_n_llhttp__internal__n_start_req_27: { llparse_match_t match_seq; - + if (p == endp) { return s_n_llhttp__internal__n_start_req_27; } @@ -3989,7 +3989,7 @@ static llparse_state_t llhttp__internal__run( case s_n_llhttp__internal__n_start_req_31: s_n_llhttp__internal__n_start_req_31: { llparse_match_t match_seq; - + if (p == endp) { return s_n_llhttp__internal__n_start_req_31; } @@ -4014,7 +4014,7 @@ static llparse_state_t llhttp__internal__run( case s_n_llhttp__internal__n_start_req_32: s_n_llhttp__internal__n_start_req_32: { llparse_match_t match_seq; - + if (p == endp) { return s_n_llhttp__internal__n_start_req_32; } @@ -4099,7 +4099,7 @@ static llparse_state_t llhttp__internal__run( case s_n_llhttp__internal__n_start_req_34: s_n_llhttp__internal__n_start_req_34: { llparse_match_t match_seq; - + if (p == endp) { return s_n_llhttp__internal__n_start_req_34; } @@ -4175,7 +4175,7 @@ static llparse_state_t llhttp__internal__run( case s_n_llhttp__internal__n_start_req_37: s_n_llhttp__internal__n_start_req_37: { llparse_match_t match_seq; - + if (p == endp) { return s_n_llhttp__internal__n_start_req_37; } @@ -4200,7 +4200,7 @@ static llparse_state_t llhttp__internal__run( case s_n_llhttp__internal__n_start_req_38: s_n_llhttp__internal__n_start_req_38: { llparse_match_t match_seq; - + if (p == endp) { return s_n_llhttp__internal__n_start_req_38; } @@ -4263,7 +4263,7 @@ static llparse_state_t llhttp__internal__run( case s_n_llhttp__internal__n_start_req_40: s_n_llhttp__internal__n_start_req_40: { llparse_match_t match_seq; - + if (p == endp) { return s_n_llhttp__internal__n_start_req_40; } @@ -4288,7 +4288,7 @@ static llparse_state_t llhttp__internal__run( case s_n_llhttp__internal__n_start_req_41: s_n_llhttp__internal__n_start_req_41: { llparse_match_t match_seq; - + if (p == endp) { return s_n_llhttp__internal__n_start_req_41; } @@ -4313,7 +4313,7 @@ static llparse_state_t llhttp__internal__run( case s_n_llhttp__internal__n_start_req_42: s_n_llhttp__internal__n_start_req_42: { llparse_match_t match_seq; - + if (p == endp) { return s_n_llhttp__internal__n_start_req_42; } @@ -4363,7 +4363,7 @@ static llparse_state_t llhttp__internal__run( case s_n_llhttp__internal__n_start_req_43: s_n_llhttp__internal__n_start_req_43: { llparse_match_t match_seq; - + if (p == endp) { return s_n_llhttp__internal__n_start_req_43; } @@ -4388,7 +4388,7 @@ static llparse_state_t llhttp__internal__run( case s_n_llhttp__internal__n_start_req_46: s_n_llhttp__internal__n_start_req_46: { llparse_match_t match_seq; - + if (p == endp) { return s_n_llhttp__internal__n_start_req_46; } @@ -4413,7 +4413,7 @@ static llparse_state_t llhttp__internal__run( case s_n_llhttp__internal__n_start_req_48: s_n_llhttp__internal__n_start_req_48: { llparse_match_t match_seq; - + if (p == endp) { return s_n_llhttp__internal__n_start_req_48; } @@ -4438,7 +4438,7 @@ static llparse_state_t llhttp__internal__run( case s_n_llhttp__internal__n_start_req_49: s_n_llhttp__internal__n_start_req_49: { llparse_match_t match_seq; - + if (p == endp) { return s_n_llhttp__internal__n_start_req_49; } @@ -4484,7 +4484,7 @@ static llparse_state_t llhttp__internal__run( case s_n_llhttp__internal__n_start_req_50: s_n_llhttp__internal__n_start_req_50: { llparse_match_t match_seq; - + if (p == endp) { return s_n_llhttp__internal__n_start_req_50; } @@ -4939,7 +4939,7 @@ static llparse_state_t llhttp__internal__run( case s_n_llhttp__internal__n_start_res: s_n_llhttp__internal__n_start_res: { llparse_match_t match_seq; - + if (p == endp) { return s_n_llhttp__internal__n_start_res; } @@ -4963,7 +4963,7 @@ static llparse_state_t llhttp__internal__run( case s_n_llhttp__internal__n_req_or_res_method_2: s_n_llhttp__internal__n_req_or_res_method_2: { llparse_match_t match_seq; - + if (p == endp) { return s_n_llhttp__internal__n_req_or_res_method_2; } @@ -4988,7 +4988,7 @@ static llparse_state_t llhttp__internal__run( case s_n_llhttp__internal__n_req_or_res_method_3: s_n_llhttp__internal__n_req_or_res_method_3: { llparse_match_t match_seq; - + if (p == endp) { return s_n_llhttp__internal__n_req_or_res_method_3; } @@ -5291,7 +5291,7 @@ static llparse_state_t llhttp__internal__run( s_n_llhttp__internal__n_span_end_llhttp__on_body: { const unsigned char* start; int err; - + start = state->_span_pos0; state->_span_pos0 = NULL; err = llhttp__on_body(state, start, p); @@ -5383,7 +5383,7 @@ static llparse_state_t llhttp__internal__run( s_n_llhttp__internal__n_span_end_llhttp__on_body_1: { const unsigned char* start; int err; - + start = state->_span_pos0; state->_span_pos0 = NULL; err = llhttp__on_body(state, start, p); @@ -5573,7 +5573,7 @@ static llparse_state_t llhttp__internal__run( s_n_llhttp__internal__n_span_end_llhttp__on_header_value: { const unsigned char* start; int err; - + start = state->_span_pos0; state->_span_pos0 = NULL; err = llhttp__on_header_value(state, start, p); @@ -5730,7 +5730,7 @@ static llparse_state_t llhttp__internal__run( s_n_llhttp__internal__n_span_end_llhttp__on_header_value_1: { const unsigned char* start; int err; - + start = state->_span_pos0; state->_span_pos0 = NULL; err = llhttp__on_header_value(state, start, p); @@ -5747,7 +5747,7 @@ static llparse_state_t llhttp__internal__run( s_n_llhttp__internal__n_span_end_llhttp__on_header_value_2: { const unsigned char* start; int err; - + start = state->_span_pos0; state->_span_pos0 = NULL; err = llhttp__on_header_value(state, start, p); @@ -5765,7 +5765,7 @@ static llparse_state_t llhttp__internal__run( s_n_llhttp__internal__n_span_end_llhttp__on_header_value_3: { const unsigned char* start; int err; - + start = state->_span_pos0; state->_span_pos0 = NULL; err = llhttp__on_header_value(state, start, p); @@ -5890,7 +5890,7 @@ static llparse_state_t llhttp__internal__run( s_n_llhttp__internal__n_span_end_llhttp__on_header_value_4: { const unsigned char* start; int err; - + start = state->_span_pos0; state->_span_pos0 = NULL; err = llhttp__on_header_value(state, start, p); @@ -5925,7 +5925,7 @@ static llparse_state_t llhttp__internal__run( s_n_llhttp__internal__n_span_end_llhttp__on_header_value_5: { const unsigned char* start; int err; - + start = state->_span_pos0; state->_span_pos0 = NULL; err = llhttp__on_header_value(state, start, p); @@ -6009,7 +6009,7 @@ static llparse_state_t llhttp__internal__run( s_n_llhttp__internal__n_span_end_llhttp__on_header_field: { const unsigned char* start; int err; - + start = state->_span_pos0; state->_span_pos0 = NULL; err = llhttp__on_header_field(state, start, p); @@ -6027,7 +6027,7 @@ static llparse_state_t llhttp__internal__run( s_n_llhttp__internal__n_span_end_llhttp__on_header_field_1: { const unsigned char* start; int err; - + start = state->_span_pos0; state->_span_pos0 = NULL; err = llhttp__on_header_field(state, start, p); @@ -6094,7 +6094,7 @@ static llparse_state_t llhttp__internal__run( s_n_llhttp__internal__n_span_end_llhttp__on_url_3: { const unsigned char* start; int err; - + start = state->_span_pos0; state->_span_pos0 = NULL; err = llhttp__on_url(state, start, p); @@ -6120,7 +6120,7 @@ static llparse_state_t llhttp__internal__run( s_n_llhttp__internal__n_span_end_llhttp__on_url_4: { const unsigned char* start; int err; - + start = state->_span_pos0; state->_span_pos0 = NULL; err = llhttp__on_url(state, start, p); @@ -6217,7 +6217,7 @@ static llparse_state_t llhttp__internal__run( s_n_llhttp__internal__n_span_end_llhttp__on_url_5: { const unsigned char* start; int err; - + start = state->_span_pos0; state->_span_pos0 = NULL; err = llhttp__on_url(state, start, p); @@ -6234,7 +6234,7 @@ static llparse_state_t llhttp__internal__run( s_n_llhttp__internal__n_span_end_llhttp__on_url_6: { const unsigned char* start; int err; - + start = state->_span_pos0; state->_span_pos0 = NULL; err = llhttp__on_url(state, start, p); @@ -6251,7 +6251,7 @@ static llparse_state_t llhttp__internal__run( s_n_llhttp__internal__n_span_end_llhttp__on_url_7: { const unsigned char* start; int err; - + start = state->_span_pos0; state->_span_pos0 = NULL; err = llhttp__on_url(state, start, p); @@ -6268,7 +6268,7 @@ static llparse_state_t llhttp__internal__run( s_n_llhttp__internal__n_span_end_llhttp__on_url_8: { const unsigned char* start; int err; - + start = state->_span_pos0; state->_span_pos0 = NULL; err = llhttp__on_url(state, start, p); @@ -6294,7 +6294,7 @@ static llparse_state_t llhttp__internal__run( s_n_llhttp__internal__n_span_end_llhttp__on_url_9: { const unsigned char* start; int err; - + start = state->_span_pos0; state->_span_pos0 = NULL; err = llhttp__on_url(state, start, p); @@ -6311,7 +6311,7 @@ static llparse_state_t llhttp__internal__run( s_n_llhttp__internal__n_span_end_llhttp__on_url_10: { const unsigned char* start; int err; - + start = state->_span_pos0; state->_span_pos0 = NULL; err = llhttp__on_url(state, start, p); @@ -6328,7 +6328,7 @@ static llparse_state_t llhttp__internal__run( s_n_llhttp__internal__n_span_end_llhttp__on_url_11: { const unsigned char* start; int err; - + start = state->_span_pos0; state->_span_pos0 = NULL; err = llhttp__on_url(state, start, p); @@ -6363,7 +6363,7 @@ static llparse_state_t llhttp__internal__run( s_n_llhttp__internal__n_span_end_llhttp__on_url: { const unsigned char* start; int err; - + start = state->_span_pos0; state->_span_pos0 = NULL; err = llhttp__on_url(state, start, p); @@ -6380,7 +6380,7 @@ static llparse_state_t llhttp__internal__run( s_n_llhttp__internal__n_span_end_llhttp__on_url_1: { const unsigned char* start; int err; - + start = state->_span_pos0; state->_span_pos0 = NULL; err = llhttp__on_url(state, start, p); @@ -6397,7 +6397,7 @@ static llparse_state_t llhttp__internal__run( s_n_llhttp__internal__n_span_end_llhttp__on_url_2: { const unsigned char* start; int err; - + start = state->_span_pos0; state->_span_pos0 = NULL; err = llhttp__on_url(state, start, p); @@ -6414,7 +6414,7 @@ static llparse_state_t llhttp__internal__run( s_n_llhttp__internal__n_span_end_llhttp__on_url_12: { const unsigned char* start; int err; - + start = state->_span_pos0; state->_span_pos0 = NULL; err = llhttp__on_url(state, start, p); @@ -6431,7 +6431,7 @@ static llparse_state_t llhttp__internal__run( s_n_llhttp__internal__n_span_end_llhttp__on_url_13: { const unsigned char* start; int err; - + start = state->_span_pos0; state->_span_pos0 = NULL; err = llhttp__on_url(state, start, p); @@ -6448,7 +6448,7 @@ static llparse_state_t llhttp__internal__run( s_n_llhttp__internal__n_span_end_llhttp__on_url_14: { const unsigned char* start; int err; - + start = state->_span_pos0; state->_span_pos0 = NULL; err = llhttp__on_url(state, start, p); @@ -6583,7 +6583,7 @@ static llparse_state_t llhttp__internal__run( s_n_llhttp__internal__n_span_end_llhttp__on_status: { const unsigned char* start; int err; - + start = state->_span_pos0; state->_span_pos0 = NULL; err = llhttp__on_status(state, start, p); @@ -6601,7 +6601,7 @@ static llparse_state_t llhttp__internal__run( s_n_llhttp__internal__n_span_end_llhttp__on_status_1: { const unsigned char* start; int err; - + start = state->_span_pos0; state->_span_pos0 = NULL; err = llhttp__on_status(state, start, p); @@ -6787,7 +6787,7 @@ int llhttp__internal_execute(llhttp__internal_t* state, const char* p, const cha if (state->_span_pos0 != NULL) { state->_span_pos0 = (void*) p; } - + next = llhttp__internal__run(state, (const unsigned char*) p, (const unsigned char*) endp); if (next == s_error) { return state->error; @@ -6797,7 +6797,7 @@ int llhttp__internal_execute(llhttp__internal_t* state, const char* p, const cha /* execute spans */ if (state->_span_pos0 != NULL) { int error; - + error = ((llhttp__internal__span_cb) state->_span_cb0)(state, state->_span_pos0, (const char*) endp); if (error != 0) { state->error = error; @@ -6805,7 +6805,7 @@ int llhttp__internal_execute(llhttp__internal_t* state, const char* p, const cha return error; } } - + return 0; } @@ -6863,7 +6863,7 @@ static const unsigned char llparse_blob7[] = { }; #ifdef __SSE4_2__ static const unsigned char ALIGN(16) llparse_blob8[] = { - 0x9, 0x9, ' ', '~', 0x80, 0xfe, 0x0, 0x0, 0x0, 0x0, 0x0, + 0x9, 0x9, ' ', '~', 0x80, 0xff, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 }; #endif /* __SSE4_2__ */ @@ -8358,7 +8358,7 @@ static llparse_state_t llhttp__internal__run( 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }; if (p == endp) { return s_n_llhttp__internal__n_header_value_connection_token; @@ -8679,7 +8679,7 @@ static llparse_state_t llhttp__internal__run( 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }; if (p == endp) { return s_n_llhttp__internal__n_header_value; @@ -8690,17 +8690,17 @@ static llparse_state_t llhttp__internal__run( __m128i input; int avail; int match_len; - + /* Load input */ input = _mm_loadu_si128((__m128i const*) p); ranges = _mm_loadu_si128((__m128i const*) llparse_blob8); - + /* Find first character that does not match `ranges` */ match_len = _mm_cmpestri(ranges, 6, input, 16, _SIDD_UBYTE_OPS | _SIDD_CMP_RANGES | _SIDD_NEGATIVE_POLARITY); - + if (match_len != 0) { p += match_len; goto s_n_llhttp__internal__n_header_value; @@ -8738,7 +8738,7 @@ static llparse_state_t llhttp__internal__run( 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }; if (p == endp) { return s_n_llhttp__internal__n_header_value_te_token; @@ -8762,7 +8762,7 @@ static llparse_state_t llhttp__internal__run( case s_n_llhttp__internal__n_header_value_te_chunked: s_n_llhttp__internal__n_header_value_te_chunked: { llparse_match_t match_seq; - + if (p == endp) { return s_n_llhttp__internal__n_header_value_te_chunked; } diff --git a/doc/api/addons.md b/doc/api/addons.md index bfe527cac992e9..b3402a7bf427ab 100644 --- a/doc/api/addons.md +++ b/doc/api/addons.md @@ -10,7 +10,7 @@ Addons provide an interface between JavaScript and C/C++ libraries. There are three options for implementing addons: N-API, nan, or direct use of internal V8, libuv and Node.js libraries. Unless there is a need for direct access to functionality which is not exposed by N-API, use N-API. -Refer to [C/C++ addons with N-API](n-api.html) for more information on N-API. +Refer to [C/C++ addons with N-API](n-api.md) for more information on N-API. When not using N-API, implementing addons is complicated, involving knowledge of several components and APIs: @@ -452,7 +452,7 @@ in the N-API are used. Creating and maintaining an addon that benefits from the ABI stability provided by N-API carries with it certain -[implementation considerations](n-api.html#n_api_implications_of_abi_stability). +[implementation considerations](n-api.md#n_api_implications_of_abi_stability). To use N-API in the above "Hello world" example, replace the content of `hello.cc` with the following. All other instructions remain the same. @@ -490,7 +490,7 @@ NAPI_MODULE(NODE_GYP_MODULE_NAME, init) ``` The functions available and how to use them are documented in -[C/C++ addons with N-API](n-api.html). +[C/C++ addons with N-API](n-api.md). ## Addon examples @@ -1356,16 +1356,16 @@ console.log(result); // Prints: 30 ``` -[`Worker`]: worker_threads.html#worker_threads_class_worker [Electron]: https://electronjs.org/ [Embedder's Guide]: https://github.com/v8/v8/wiki/Embedder's%20Guide [Linking to libraries included with Node.js]: #addons_linking_to_libraries_included_with_node_js [Native Abstractions for Node.js]: https://github.com/nodejs/nan +[`Worker`]: worker_threads.md#worker_threads_class_worker [bindings]: https://github.com/TooTallNate/node-bindings [download]: https://github.com/nodejs/node-addon-examples [examples]: https://github.com/nodejs/nan/tree/master/examples/ [installation instructions]: https://github.com/nodejs/node-gyp#installation [libuv]: https://github.com/libuv/libuv [node-gyp]: https://github.com/nodejs/node-gyp -[require]: modules.html#modules_require_id +[require]: modules.md#modules_require_id [v8-docs]: https://v8docs.nodesource.com/ diff --git a/doc/api/assert.md b/doc/api/assert.md index e074107cf566c1..b1b77b8f3d6ea7 100644 --- a/doc/api/assert.md +++ b/doc/api/assert.md @@ -64,7 +64,7 @@ assert.deepEqual([[[1, 2, 3]], 4, 5], [[[1, 2, '3']], 4, 5]); To deactivate the colors, use the `NO_COLOR` or `NODE_DISABLE_COLORS` environment variables. This will also deactivate the colors in the REPL. For more on color support in terminal environments, read the tty -[getColorDepth()](tty.html#tty_writestream_getcolordepth_env) documentation. +[getColorDepth()](tty.md#tty_writestream_getcolordepth_env) documentation. ## Legacy assertion mode @@ -1556,17 +1556,22 @@ assert.throws(throwingFirst, /Second$/); Due to the confusing error-prone notation, avoid a string as the second argument. +[Abstract Equality Comparison]: https://tc39.github.io/ecma262/#sec-abstract-equality-comparison +[Object wrappers]: https://developer.mozilla.org/en-US/docs/Glossary/Primitive#Primitive_wrapper_objects_in_JavaScript +[Object.prototype.toString()]: https://tc39.github.io/ecma262/#sec-object.prototype.tostring +[SameValue Comparison]: https://tc39.github.io/ecma262/#sec-samevalue +[Strict Equality Comparison]: https://tc39.github.io/ecma262/#sec-strict-equality-comparison [`AssertionError`]: #assert_class_assert_assertionerror [`Class`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes -[`ERR_INVALID_RETURN_VALUE`]: errors.html#errors_err_invalid_return_value -[`Error.captureStackTrace`]: errors.html#errors_error_capturestacktrace_targetobject_constructoropt -[`Error`]: errors.html#errors_class_error +[`ERR_INVALID_RETURN_VALUE`]: errors.md#errors_err_invalid_return_value +[`Error.captureStackTrace`]: errors.md#errors_error_capturestacktrace_targetobject_constructoropt +[`Error`]: errors.md#errors_class_error [`Map`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map [`Object.is()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is [`RegExp`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions [`Set`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set [`Symbol`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol -[`TypeError`]: errors.html#errors_class_typeerror +[`TypeError`]: errors.md#errors_class_typeerror [`WeakMap`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap [`WeakSet`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakSet [`CallTracker`]: #assert_class_assert_calltracker @@ -1581,14 +1586,9 @@ argument. [`assert.ok()`]: #assert_assert_ok_value_message [`assert.strictEqual()`]: #assert_assert_strictequal_actual_expected_message [`assert.throws()`]: #assert_assert_throws_fn_error_message -[`process.on('exit')`]: process.html#process_event_exit +[`process.on('exit')`]: process.md#process_event_exit [`tracker.calls()`]: #assert_tracker_calls_fn_exact [`tracker.verify()`]: #assert_tracker_verify [strict assertion mode]: #assert_strict_assertion_mode -[Abstract Equality Comparison]: https://tc39.github.io/ecma262/#sec-abstract-equality-comparison -[Object wrappers]: https://developer.mozilla.org/en-US/docs/Glossary/Primitive#Primitive_wrapper_objects_in_JavaScript -[Object.prototype.toString()]: https://tc39.github.io/ecma262/#sec-object.prototype.tostring -[SameValue Comparison]: https://tc39.github.io/ecma262/#sec-samevalue -[Strict Equality Comparison]: https://tc39.github.io/ecma262/#sec-strict-equality-comparison [enumerable "own" properties]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Enumerability_and_ownership_of_properties [prototype-spec]: https://tc39.github.io/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots diff --git a/doc/api/async_hooks.md b/doc/api/async_hooks.md index 50baa8d4dde79e..0a8b8d1c5200ad 100644 --- a/doc/api/async_hooks.md +++ b/doc/api/async_hooks.md @@ -182,7 +182,7 @@ of asynchronous operations. * Returns: {AsyncHook} A reference to `asyncHook`. Enable the callbacks for a given `AsyncHook` instance. If no callbacks are -provided enabling is a noop. +provided, enabling is a no-op. The `AsyncHook` instance is disabled by default. If the `AsyncHook` instance should be enabled immediately after creation, the following pattern can be used. @@ -1167,16 +1167,16 @@ If you need to keep using callback-based API, or your code assumes a custom thenable implementation, use the [`AsyncResource`][] class to associate the asynchronous operation with the correct execution context. +[Hook Callbacks]: #async_hooks_hook_callbacks +[PromiseHooks]: https://docs.google.com/document/d/1rda3yKGHimKIhg5YeoAmCOtyURgsbTH_qaYR79FELlk/edit [`AsyncResource`]: #async_hooks_class_asyncresource [`after` callback]: #async_hooks_after_asyncid [`before` callback]: #async_hooks_before_asyncid [`destroy` callback]: #async_hooks_destroy_asyncid [`init` callback]: #async_hooks_init_asyncid_type_triggerasyncid_resource [`promiseResolve` callback]: #async_hooks_promiseresolve_asyncid -[`EventEmitter`]: events.html#events_class_eventemitter -[Hook Callbacks]: #async_hooks_hook_callbacks -[PromiseHooks]: https://docs.google.com/document/d/1rda3yKGHimKIhg5YeoAmCOtyURgsbTH_qaYR79FELlk/edit -[`Stream`]: stream.html#stream_stream -[`Worker`]: worker_threads.html#worker_threads_class_worker +[`EventEmitter`]: events.md#events_class_eventemitter +[`Stream`]: stream.md#stream_stream +[`Worker`]: worker_threads.md#worker_threads_class_worker +[`util.promisify()`]: util.md#util_util_promisify_original [promise execution tracking]: #async_hooks_promise_execution_tracking -[`util.promisify()`]: util.html#util_util_promisify_original diff --git a/doc/api/buffer.md b/doc/api/buffer.md index 56449d1a6ec193..4aaa59ef92853e 100644 --- a/doc/api/buffer.md +++ b/doc/api/buffer.md @@ -3261,7 +3261,12 @@ While there are clear performance advantages to using [`Buffer.allocUnsafe()`][], extra care *must* be taken in order to avoid introducing security vulnerabilities into an application. +[ASCII]: https://en.wikipedia.org/wiki/ASCII +[Base64]: https://en.wikipedia.org/wiki/Base64 +[ISO-8859-1]: https://en.wikipedia.org/wiki/ISO-8859-1 [RFC 4648, Section 5]: https://tools.ietf.org/html/rfc4648#section-5 +[UTF-16]: https://en.wikipedia.org/wiki/UTF-16 +[UTF-8]: https://en.wikipedia.org/wiki/UTF-8 [WHATWG Encoding Standard]: https://encoding.spec.whatwg.org/ [`ArrayBuffer`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer [`Buffer.alloc()`]: #buffer_static_method_buffer_alloc_size_fill_encoding @@ -3273,18 +3278,18 @@ introducing security vulnerabilities into an application. [`Buffer.from(string)`]: #buffer_static_method_buffer_from_string_encoding [`Buffer.poolSize`]: #buffer_class_property_buffer_poolsize [`DataView`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView -[`ERR_INVALID_BUFFER_SIZE`]: errors.html#ERR_INVALID_BUFFER_SIZE -[`ERR_INVALID_OPT_VALUE`]: errors.html#ERR_INVALID_OPT_VALUE -[`ERR_OUT_OF_RANGE`]: errors.html#ERR_OUT_OF_RANGE +[`ERR_INVALID_BUFFER_SIZE`]: errors.md#ERR_INVALID_BUFFER_SIZE +[`ERR_INVALID_OPT_VALUE`]: errors.md#ERR_INVALID_OPT_VALUE +[`ERR_OUT_OF_RANGE`]: errors.md#ERR_OUT_OF_RANGE [`JSON.stringify()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify [`SharedArrayBuffer`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer [`String#indexOf()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf [`String#lastIndexOf()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/lastIndexOf [`String.prototype.length`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/length -[`TypedArray.from()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/from [`TypedArray#set()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/set [`TypedArray#slice()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/slice [`TypedArray#subarray()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/subarray +[`TypedArray.from()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/from [`TypedArray`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray [`Uint8Array`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array [`buf.buffer`]: #buffer_buf_buffer @@ -3300,12 +3305,7 @@ introducing security vulnerabilities into an application. [`buffer.constants.MAX_LENGTH`]: #buffer_buffer_constants_max_length [`buffer.constants.MAX_STRING_LENGTH`]: #buffer_buffer_constants_max_string_length [`buffer.kMaxLength`]: #buffer_buffer_kmaxlength -[`util.inspect()`]: util.html#util_util_inspect_object_options -[ASCII]: https://en.wikipedia.org/wiki/ASCII -[Base64]: https://en.wikipedia.org/wiki/Base64 -[ISO-8859-1]: https://en.wikipedia.org/wiki/ISO-8859-1 -[UTF-8]: https://en.wikipedia.org/wiki/UTF-8 -[UTF-16]: https://en.wikipedia.org/wiki/UTF-16 +[`util.inspect()`]: util.md#util_util_inspect_object_options [binary strings]: https://developer.mozilla.org/en-US/docs/Web/API/DOMString/Binary [endianness]: https://en.wikipedia.org/wiki/Endianness [iterator]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols diff --git a/doc/api/child_process.md b/doc/api/child_process.md index 3a5f64d622885a..c180176353d4a4 100644 --- a/doc/api/child_process.md +++ b/doc/api/child_process.md @@ -6,7 +6,7 @@ -The `child_process` module provides the ability to spawn child processes in +The `child_process` module provides the ability to spawn subprocesses in a manner that is similar, but not identical, to popen(3). This capability is primarily provided by the [`child_process.spawn()`][] function: @@ -28,20 +28,23 @@ ls.on('close', (code) => { ``` By default, pipes for `stdin`, `stdout`, and `stderr` are established between -the parent Node.js process and the spawned child. These pipes have -limited (and platform-specific) capacity. If the child process writes to -stdout in excess of that limit without the output being captured, the child -process will block waiting for the pipe buffer to accept more data. This is +the parent Node.js process and the spawned subprocess. These pipes have +limited (and platform-specific) capacity. If the subprocess writes to +stdout in excess of that limit without the output being captured, the +subprocess blocks waiting for the pipe buffer to accept more data. This is identical to the behavior of pipes in the shell. Use the `{ stdio: 'ignore' }` option if the output will not be consumed. -The command lookup will be performed using `options.env.PATH` environment -variable if passed in `options` object, otherwise `process.env.PATH` will be -used. To account for the fact that Windows environment variables are -case-insensitive Node.js will lexicographically sort all `env` keys and choose -the first one case-insensitively matching `PATH` to perform command lookup. -This may lead to issues on Windows when passing objects to `env` option that -have multiple variants of `PATH` variable. +The command lookup is performed using the `options.env.PATH` environment +variable if it is in the `options` object. Otherwise, `process.env.PATH` is +used. + +On Windows, environment variables are case-insensitive. Node.js +lexicographically sorts the `env` keys and uses the first one that +case-insensitively matches. Only first (in lexicographic order) entry will be +passed to the subprocess. This might lead to issues on Windows when passing +objects to the `env` option that have multiple variants of the same key, such as +`PATH` and `Path`. The [`child_process.spawn()`][] method spawns the child process asynchronously, without blocking the Node.js event loop. The [`child_process.spawnSync()`][] @@ -1580,13 +1583,16 @@ Therefore, this feature requires opting in by setting the or [`child_process.fork()`][]. [Advanced serialization]: #child_process_advanced_serialization -[`'disconnect'`]: process.html#process_event_disconnect +[Default Windows shell]: #child_process_default_windows_shell +[HTML structured clone algorithm]: https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm +[Shell requirements]: #child_process_shell_requirements +[`'disconnect'`]: process.md#process_event_disconnect [`'error'`]: #child_process_event_error [`'exit'`]: #child_process_event_exit -[`'message'`]: process.html#process_event_message +[`'message'`]: process.md#process_event_message [`ChildProcess`]: #child_process_child_process -[`Error`]: errors.html#errors_class_error -[`EventEmitter`]: events.html#events_class_eventemitter +[`Error`]: errors.md#errors_class_error +[`EventEmitter`]: events.md#events_class_eventemitter [`child_process.exec()`]: #child_process_child_process_exec_command_options_callback [`child_process.execFile()`]: #child_process_child_process_execfile_file_args_options_callback [`child_process.execFileSync()`]: #child_process_child_process_execfilesync_file_args_options @@ -1595,13 +1601,13 @@ or [`child_process.fork()`][]. [`child_process.spawn()`]: #child_process_child_process_spawn_command_args_options [`child_process.spawnSync()`]: #child_process_child_process_spawnsync_command_args_options [`maxBuffer` and Unicode]: #child_process_maxbuffer_and_unicode -[`net.Server`]: net.html#net_class_net_server -[`net.Socket`]: net.html#net_class_net_socket +[`net.Server`]: net.md#net_class_net_server +[`net.Socket`]: net.md#net_class_net_socket [`options.detached`]: #child_process_options_detached -[`process.disconnect()`]: process.html#process_process_disconnect -[`process.env`]: process.html#process_process_env -[`process.execPath`]: process.html#process_process_execpath -[`process.send()`]: process.html#process_process_send_message_sendhandle_options_callback +[`process.disconnect()`]: process.md#process_process_disconnect +[`process.env`]: process.md#process_process_env +[`process.execPath`]: process.md#process_process_execpath +[`process.send()`]: process.md#process_process_send_message_sendhandle_options_callback [`stdio`]: #child_process_options_stdio [`subprocess.connected`]: #child_process_subprocess_connected [`subprocess.disconnect()`]: #child_process_subprocess_disconnect @@ -1611,9 +1617,6 @@ or [`child_process.fork()`][]. [`subprocess.stdin`]: #child_process_subprocess_stdin [`subprocess.stdio`]: #child_process_subprocess_stdio [`subprocess.stdout`]: #child_process_subprocess_stdout -[`util.promisify()`]: util.html#util_util_promisify_original -[Default Windows shell]: #child_process_default_windows_shell -[HTML structured clone algorithm]: https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm -[Shell requirements]: #child_process_shell_requirements +[`util.promisify()`]: util.md#util_util_promisify_original [synchronous counterparts]: #child_process_synchronous_process_creation -[v8.serdes]: v8.html#v8_serialization_api +[v8.serdes]: v8.md#v8_serialization_api diff --git a/doc/api/cli.md b/doc/api/cli.md index b0ff3fe30c2f13..a5c678b30cb405 100644 --- a/doc/api/cli.md +++ b/doc/api/cli.md @@ -1554,27 +1554,27 @@ $ node --max-old-space-size=1536 index.js ``` [Chrome DevTools Protocol]: https://chromedevtools.github.io/devtools-protocol/ -[REPL]: repl.html +[REPL]: repl.md [ScriptCoverage]: https://chromedevtools.github.io/devtools-protocol/tot/Profiler#type-ScriptCoverage [Source Map]: https://sourcemaps.info/spec.html [Subresource Integrity]: https://developer.mozilla.org/en-US/docs/Web/Security/Subresource_Integrity [V8 JavaScript code coverage]: https://v8project.blogspot.com/2017/12/javascript-code-coverage.html [`--openssl-config`]: #cli_openssl_config_file [`Atomics.wait()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Atomics/wait -[`Buffer`]: buffer.html#buffer_class_buffer +[`Buffer`]: buffer.md#buffer_class_buffer [`NODE_OPTIONS`]: #cli_node_options_options -[`SlowBuffer`]: buffer.html#buffer_class_slowbuffer -[`process.setUncaughtExceptionCaptureCallback()`]: process.html#process_process_setuncaughtexceptioncapturecallback_fn -[`tls.DEFAULT_MAX_VERSION`]: tls.html#tls_tls_default_max_version -[`tls.DEFAULT_MIN_VERSION`]: tls.html#tls_tls_default_min_version -[`unhandledRejection`]: process.html#process_event_unhandledrejection -[`worker_threads.threadId`]: worker_threads.html#worker_threads_worker_threadid -[context-aware]: addons.html#addons_context_aware_addons -[customizing ESM specifier resolution]: esm.html#esm_customizing_esm_specifier_resolution_algorithm -[debugger]: debugger.html +[`SlowBuffer`]: buffer.md#buffer_class_slowbuffer +[`process.setUncaughtExceptionCaptureCallback()`]: process.md#process_process_setuncaughtexceptioncapturecallback_fn +[`tls.DEFAULT_MAX_VERSION`]: tls.md#tls_tls_default_max_version +[`tls.DEFAULT_MIN_VERSION`]: tls.md#tls_tls_default_min_version +[`unhandledRejection`]: process.md#process_event_unhandledrejection +[`worker_threads.threadId`]: worker_threads.md#worker_threads_worker_threadid +[context-aware]: addons.md#addons_context_aware_addons +[customizing ESM specifier resolution]: esm.md#esm_customizing_esm_specifier_resolution_algorithm +[debugger]: debugger.md [debugging security implications]: https://nodejs.org/en/docs/guides/debugging-getting-started/#security-implications -[emit_warning]: process.html#process_process_emitwarning_warning_type_code_ctor -[experimental ECMAScript Module loader]: esm.html#esm_experimental_loaders +[emit_warning]: process.md#process_process_emitwarning_warning_type_code_ctor +[experimental ECMAScript Module loader]: esm.md#esm_experimental_loaders [jitless]: https://v8.dev/blog/jitless [libuv threadpool documentation]: https://docs.libuv.org/en/latest/threadpool.html [remote code execution]: https://www.owasp.org/index.php/Code_Injection diff --git a/doc/api/cluster.md b/doc/api/cluster.md index 92e4d42d60e3fd..0b3586d10cd439 100644 --- a/doc/api/cluster.md +++ b/doc/api/cluster.md @@ -873,17 +873,17 @@ socket.on('data', (id) => { }); ``` +[Advanced serialization for `child_process`]: child_process.md#child_process_advanced_serialization +[Child Process module]: child_process.md#child_process_child_process_fork_modulepath_args_options [`.fork()`]: #cluster_cluster_fork_env [`.setupMaster()`]: #cluster_cluster_setupmaster_settings -[`ChildProcess.send()`]: child_process.html#child_process_subprocess_send_message_sendhandle_options_callback -[`child_process.fork()`]: child_process.html#child_process_child_process_fork_modulepath_args_options -[`child_process` event: `'exit'`]: child_process.html#child_process_event_exit -[`child_process` event: `'message'`]: child_process.html#child_process_event_message +[`ChildProcess.send()`]: child_process.md#child_process_subprocess_send_message_sendhandle_options_callback +[`child_process.fork()`]: child_process.md#child_process_child_process_fork_modulepath_args_options +[`child_process` event: `'exit'`]: child_process.md#child_process_event_exit +[`child_process` event: `'message'`]: child_process.md#child_process_event_message [`cluster.settings`]: #cluster_cluster_settings -[`disconnect()`]: child_process.html#child_process_subprocess_disconnect -[`kill()`]: process.html#process_process_kill_pid_signal -[`process` event: `'message'`]: process.html#process_event_message -[`server.close()`]: net.html#net_event_close +[`disconnect()`]: child_process.md#child_process_subprocess_disconnect +[`kill()`]: process.md#process_process_kill_pid_signal +[`process` event: `'message'`]: process.md#process_event_message +[`server.close()`]: net.md#net_event_close [`worker.exitedAfterDisconnect`]: #cluster_worker_exitedafterdisconnect -[Advanced serialization for `child_process`]: child_process.html#child_process_advanced_serialization -[Child Process module]: child_process.html#child_process_child_process_fork_modulepath_args_options diff --git a/doc/api/console.md b/doc/api/console.md index 9e4548581b5f70..636352f3d45d4f 100644 --- a/doc/api/console.md +++ b/doc/api/console.md @@ -416,12 +416,16 @@ added: v0.1.104 Starts a timer that can be used to compute the duration of an operation. Timers are identified by a unique `label`. Use the same `label` when calling [`console.timeEnd()`][] to stop the timer and output the elapsed time in -milliseconds to `stdout`. Timer durations are accurate to the sub-millisecond. +suitable time units to `stdout`. For example, if the elapsed +time is 3869ms, `console.timeEnd()` displays "3.869s". ### `console.timeEnd([label])` + > Stability: 1 - Experimental @@ -57,7 +78,7 @@ Node.js treats JavaScript code as CommonJS modules by default. Authors can tell Node.js to treat JavaScript code as ECMAScript modules via the `.mjs` file extension, the `package.json` [`"type"`][] field, or the `--input-type` flag. See -[Modules: Packages](packages.html#packages_determining_module_system) for more +[Modules: Packages](packages.md#packages_determining_module_system) for more details. @@ -78,7 +99,7 @@ details. ## Packages -This section was moved to [Modules: Packages](packages.html). +This section was moved to [Modules: Packages](packages.md). ## `import` Specifiers @@ -105,13 +126,27 @@ There are four types of specifiers: Bare specifiers, and the bare specifier portion of deep import specifiers, are strings; but everything else in a specifier is a URL. -Only `file:` and `data:` URLs are supported. A specifier like +`file:`, `node:`, and `data:` URLs are supported. A specifier like `'https://example.com/app.js'` may be supported by browsers but it is not supported in Node.js. Specifiers may not begin with `/` or `//`. These are reserved for potential future use. The root of the current volume may be referenced via `file:///`. +#### `node:` Imports + + + +`node:` URLs are supported as a means to load Node.js builtin modules. This +URL scheme allows for builtin modules to be referenced by valid absolute URL +strings. + +```js +import fs from 'node:fs/promises'; +``` + #### `data:` Imports ```js @@ -348,8 +383,8 @@ directly on the ES module namespace when the module is imported. Live binding updates or new exports added to `module.exports` are not detected for these named exports. -The detection of named exports is based on common syntax patterns but will not -always correctly detect named exports, in these cases using the default +The detection of named exports is based on common syntax patterns but does not +always correctly detect named exports. In these cases, using the default import form described above can be a better option. Named exports detection covers many common export patterns, reexport patterns @@ -358,7 +393,7 @@ semantics implemented. ## Builtin modules -[Core modules][] will provide named exports of their public API. A +[Core modules][] provide named exports of their public API. A default export is also provided which is the value of the CommonJS exports. The default export can be used for, among other things, modifying the named exports. Named exports of builtin modules are updated only by calling @@ -415,11 +450,11 @@ and are loaded using the CJS loader. [WHATWG JSON modules specification][] are still being standardized, and are experimentally supported by including the additional flag `--experimental-json-modules` when running Node.js. -When the `--experimental-json-modules` flag is included both the -`commonjs` and `module` mode will use the new experimental JSON -loader. The imported JSON only exposes a `default`, there is no +When the `--experimental-json-modules` flag is included, both the +`commonjs` and `module` mode use the new experimental JSON +loader. The imported JSON only exposes a `default`. There is no support for named exports. A cache entry is created in the CommonJS -cache, to avoid duplication. The same object will be returned in +cache to avoid duplication. The same object is returned in CommonJS if the JSON module has already been imported from the same path. @@ -523,7 +558,7 @@ The `conditions` property on the `context` is an array of conditions for for looking up conditional mappings elsewhere or to modify the list when calling the default resolution logic. -The current [package exports conditions][Conditional Exports] will always be in +The current [package exports conditions][Conditional Exports] are always in the `context.conditions` array passed into the hook. To guarantee _default Node.js module specifier resolution behavior_ when calling `defaultResolve`, the `context.conditions` array passed to it _must_ include _all_ elements of the @@ -593,7 +628,7 @@ Note: These types all correspond to classes defined in ECMAScript. * The specific [`TypedArray`][] object is a [`Uint8Array`][]. Note: If the source value of a text-based format (i.e., `'json'`, `'module'`) is -not a string, it will be converted to a string using [`util.TextDecoder`][]. +not a string, it is converted to a string using [`util.TextDecoder`][]. ```js /** @@ -704,15 +739,15 @@ export async function transformSource(source, context, defaultTransformSource) { * Returns: {string} -Sometimes it can be necessary to run some code inside of the same global scope -that the application will run in. This hook allows to return a string that will -be ran as sloppy-mode script on startup. +Sometimes it might be necessary to run some code inside of the same global scope +that the application runs in. This hook allows the return of a string that is +run as sloppy-mode script on startup. Similar to how CommonJS wrappers work, the code runs in an implicit function scope. The only argument is a `require`-like function that can be used to load builtins like "fs": `getBuiltin(request: string)`. -If the code needs more advanced `require` features, it will have to construct +If the code needs more advanced `require` features, it has to construct its own `require` using `module.createRequire()`. ```js @@ -807,13 +842,9 @@ import { VERSION } from 'https://coffeescript.org/browser-compiler-modern/coffee console.log(VERSION); ``` -With this loader, running: - -```bash -node --experimental-loader ./https-loader.mjs ./main.mjs -``` - -Will print the current version of CoffeeScript per the module at the URL in +With the preceding loader, running +`node --experimental-loader ./https-loader.mjs ./main.mjs` +prints the current version of CoffeeScript per the module at the URL in `main.mjs`. #### Transpiler loader @@ -894,13 +925,9 @@ console.log "Brought to you by Node.js version #{version}" export scream = (str) -> str.toUpperCase() ``` -With this loader, running: - -```console -node --experimental-loader ./coffeescript-loader.mjs main.coffee -``` - -Will cause `main.coffee` to be turned into JavaScript after its source code is +With the preceding loader, running +`node --experimental-loader ./coffeescript-loader.mjs main.coffee` +causes `main.coffee` to be turned into JavaScript after its source code is loaded from disk but before Node.js executes it; and so on for any `.coffee`, `.litcoffee` or `.coffee.md` files referenced via `import` statements of any loaded file. @@ -949,8 +976,7 @@ The resolver can throw the following errors: * _Package Import Not Defined_: Package imports do not define the specifier. * _Module Not Found_: The package or module requested does not exist. -
-Resolver algorithm specification +### Resolver Algorithm Specification **ESM_RESOLVE**(_specifier_, _parentURL_) @@ -1003,7 +1029,7 @@ The resolver can throw the following errors: > 1. If _selfUrl_ is not **undefined**, return _selfUrl_. > 1. If _packageSubpath_ is _"."_ and _packageName_ is a Node.js builtin > module, then -> 1. Return the string _"nodejs:"_ concatenated with _packageSpecifier_. +> 1. Return the string _"node:"_ concatenated with _packageSpecifier_. > 1. While _parentURL_ is not the file system root, > 1. Let _packageURL_ be the URL resolution of _"node_modules/"_ > concatenated with _packageSpecifier_, relative to _parentURL_. @@ -1204,8 +1230,6 @@ _internal_, _conditions_) > 1. Throw an _Invalid Package Configuration_ error. > 1. Return the parsed JSON source of the file at _pjsonURL_. -
- ### Customizing ESM specifier resolution algorithm The current specifier resolution does not support all default behavior of @@ -1229,36 +1253,36 @@ success! ``` -[CommonJS]: modules.html -[Conditional exports]: packages.html#packages_conditional_exports +[6.1.7 Array Index]: https://tc39.es/ecma262/#integer-index +[CommonJS]: modules.md +[Conditional exports]: packages.md#packages_conditional_exports +[Core modules]: modules.md#modules_core_modules [Dynamic `import()`]: https://wiki.developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#Dynamic_Imports -[ECMAScript-modules implementation]: https://github.com/nodejs/modules/blob/master/doc/plan-for-new-modules-implementation.md [ECMAScript Top-Level `await` proposal]: https://github.com/tc39/proposal-top-level-await/ +[ECMAScript-modules implementation]: https://github.com/nodejs/modules/blob/master/doc/plan-for-new-modules-implementation.md [ES Module Integration Proposal for Web Assembly]: https://github.com/webassembly/esm-integration [Node.js EP for ES Modules]: https://github.com/nodejs/node-eps/blob/master/002-es-modules.md [Terminology]: #esm_terminology +[Top-Level Await]: https://github.com/tc39/proposal-top-level-await [WHATWG JSON modules specification]: https://html.spec.whatwg.org/#creating-a-json-module-script +[`"exports"`]: packages.md#packages_exports +[`"type"`]: packages.md#packages_type +[`ArrayBuffer`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer +[`SharedArrayBuffer`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer +[`TypedArray`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray +[`Uint8Array`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array [`data:` URLs]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs [`export`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export [`import()`]: #esm_import_expressions [`import.meta.url`]: #esm_import_meta [`import`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import -[`module.createRequire()`]: module.html#module_module_createrequire_filename -[`module.syncBuiltinESMExports()`]: module.html#module_module_syncbuiltinesmexports +[`module.createRequire()`]: module.md#module_module_createrequire_filename +[`module.syncBuiltinESMExports()`]: module.md#module_module_syncbuiltinesmexports +[`package.json`]: packages.md#packages_node_js_package_json_field_definitions [`transformSource` hook]: #esm_transformsource_source_context_defaulttransformsource -[`ArrayBuffer`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer -[`SharedArrayBuffer`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer [`string`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String -[`TypedArray`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray -[`Uint8Array`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array -[`util.TextDecoder`]: util.html#util_class_util_textdecoder -[cjs-module-lexer]: https://github.com/guybedford/cjs-module-lexer/tree/0.3.1 +[`util.TextDecoder`]: util.md#util_class_util_textdecoder +[cjs-module-lexer]: https://github.com/guybedford/cjs-module-lexer/tree/0.4.0 [special scheme]: https://url.spec.whatwg.org/#special-scheme [the official standard format]: https://tc39.github.io/ecma262/#sec-modules [transpiler loader example]: #esm_transpiler_loader -[6.1.7 Array Index]: https://tc39.es/ecma262/#integer-index -[Top-Level Await]: https://github.com/tc39/proposal-top-level-await -[Core modules]: modules.html#modules_core_modules -[`package.json`]: packages.html#packages_node_js_package_json_field_definitions -[`"exports"`]: packages.html#packages_exports -[`"type"`]: packages.html#packages_type diff --git a/doc/api/events.md b/doc/api/events.md index a3b77553bab46e..77ba17f6561a5b 100644 --- a/doc/api/events.md +++ b/doc/api/events.md @@ -1467,19 +1467,19 @@ and `removeEventListener()` is that `removeListener()` will return a reference to the `EventTarget`. [WHATWG-EventTarget]: https://dom.spec.whatwg.org/#interface-eventtarget -[`--trace-warnings`]: cli.html#cli_trace_warnings +[`--trace-warnings`]: cli.md#cli_trace_warnings [`EventEmitter.defaultMaxListeners`]: #events_eventemitter_defaultmaxlisteners -[`domain`]: domain.html +[`EventTarget` Web API]: https://dom.spec.whatwg.org/#eventtarget +[`EventTarget` error handling]: #events_eventtarget_error_handling +[`Event` Web API]: https://dom.spec.whatwg.org/#event +[`domain`]: domain.md [`emitter.listenerCount()`]: #events_emitter_listenercount_eventname [`emitter.removeListener()`]: #events_emitter_removelistener_eventname_listener [`emitter.setMaxListeners(n)`]: #events_emitter_setmaxlisteners_n -[`Event` Web API]: https://dom.spec.whatwg.org/#event -[`EventTarget` error handling]: #events_eventtarget_error_handling -[`EventTarget` Web API]: https://dom.spec.whatwg.org/#eventtarget -[`fs.ReadStream`]: fs.html#fs_class_fs_readstream -[`net.Server`]: net.html#net_class_net_server -[`process.on('warning')`]: process.html#process_event_warning -[stream]: stream.html +[`fs.ReadStream`]: fs.md#fs_class_fs_readstream +[`net.Server`]: net.md#net_class_net_server +[`process.on('warning')`]: process.md#process_event_warning +[stream]: stream.md [capturerejections]: #events_capture_rejections_of_promises [rejection]: #events_emitter_symbol_for_nodejs_rejection_err_eventname_args [rejectionsymbol]: #events_events_capturerejectionsymbol diff --git a/doc/api/fs.md b/doc/api/fs.md index ece4e5a783b6bd..36d3fc10f4df54 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -2909,6 +2909,9 @@ If `position` is an integer, the file position will remain unchanged. The callback is given the three arguments, `(err, bytesRead, buffer)`. +If the file is not modified concurrently, the end-of-file is reached when the +number of bytes read is zero. + If this method is invoked as its [`util.promisify()`][]ed version, it returns a `Promise` for an `Object` with `bytesRead` and `buffer` properties. @@ -3502,8 +3505,6 @@ changes: it will emit a deprecation warning with id DEP0013. --> -> Stability: 1 - Recursive removal is experimental. - * `path` {string|Buffer|URL} * `options` {Object} * `maxRetries` {integer} If an `EBUSY`, `EMFILE`, `ENFILE`, `ENOTEMPTY`, or @@ -3526,6 +3527,12 @@ to the completion callback. Using `fs.rmdir()` on a file (not a directory) results in an `ENOENT` error on Windows and an `ENOTDIR` error on POSIX. +Setting `recursive` to `true` results in behavior similar to the Unix command +`rm -rf`: an error will not be raised for paths that do not exist, and paths +that represent files will be deleted. The permissive behavior of the +`recursive` option is deprecated, `ENOTDIR` and `ENOENT` will be thrown in +the future. + ## `fs.rmdirSync(path[, options])` -> Stability: 1 - Recursive removal is experimental. - * `path` {string|Buffer|URL} * `options` {Object} * `maxRetries` {integer} If an `EBUSY`, `EMFILE`, `ENFILE`, `ENOTEMPTY`, or @@ -3570,6 +3575,12 @@ Synchronous rmdir(2). Returns `undefined`. Using `fs.rmdirSync()` on a file (not a directory) results in an `ENOENT` error on Windows and an `ENOTDIR` error on POSIX. +Setting `recursive` to `true` results in behavior similar to the Unix command +`rm -rf`: an error will not be raised for paths that do not exist, and paths +that represent files will be deleted. The permissive behavior of the +`recursive` option is deprecated, `ENOTDIR` and `ENOENT` will be thrown in +the future. + ## `fs.stat(path[, options], callback)` -* [About this documentation](documentation.html) -* [Usage and example](synopsis.html) +* [About this documentation](documentation.md) +* [Usage and example](synopsis.md)
-* [Assertion testing](assert.html) -* [Async hooks](async_hooks.html) -* [Buffer](buffer.html) -* [C++ addons](addons.html) -* [C/C++ addons with N-API](n-api.html) -* [C++ embedder API](embedding.html) -* [Child processes](child_process.html) -* [Cluster](cluster.html) -* [Command-line options](cli.html) -* [Console](console.html) -* [Crypto](crypto.html) -* [Debugger](debugger.html) -* [Deprecated APIs](deprecations.html) -* [DNS](dns.html) -* [Domain](domain.html) -* [Errors](errors.html) -* [Events](events.html) -* [File system](fs.html) -* [Globals](globals.html) -* [HTTP](http.html) -* [HTTP/2](http2.html) -* [HTTPS](https.html) -* [Inspector](inspector.html) -* [Internationalization](intl.html) -* [Modules: CommonJS modules](modules.html) -* [Modules: ECMAScript modules](esm.html) -* [Modules: `module` API](module.html) -* [Modules: Packages](packages.html) -* [Net](net.html) -* [OS](os.html) -* [Path](path.html) -* [Performance hooks](perf_hooks.html) -* [Policies](policy.html) -* [Process](process.html) -* [Punycode](punycode.html) -* [Query strings](querystring.html) -* [Readline](readline.html) -* [REPL](repl.html) -* [Report](report.html) -* [Stream](stream.html) -* [String decoder](string_decoder.html) -* [Timers](timers.html) -* [TLS/SSL](tls.html) -* [Trace events](tracing.html) -* [TTY](tty.html) -* [UDP/datagram](dgram.html) -* [URL](url.html) -* [Utilities](util.html) -* [V8](v8.html) -* [VM](vm.html) -* [WASI](wasi.html) -* [Worker threads](worker_threads.html) -* [Zlib](zlib.html) +* [Assertion testing](assert.md) +* [Async hooks](async_hooks.md) +* [Buffer](buffer.md) +* [C++ addons](addons.md) +* [C/C++ addons with N-API](n-api.md) +* [C++ embedder API](embedding.md) +* [Child processes](child_process.md) +* [Cluster](cluster.md) +* [Command-line options](cli.md) +* [Console](console.md) +* [Crypto](crypto.md) +* [Debugger](debugger.md) +* [Deprecated APIs](deprecations.md) +* [DNS](dns.md) +* [Domain](domain.md) +* [Errors](errors.md) +* [Events](events.md) +* [File system](fs.md) +* [Globals](globals.md) +* [HTTP](http.md) +* [HTTP/2](http2.md) +* [HTTPS](https.md) +* [Inspector](inspector.md) +* [Internationalization](intl.md) +* [Modules: CommonJS modules](modules.md) +* [Modules: ECMAScript modules](esm.md) +* [Modules: `module` API](module.md) +* [Modules: Packages](packages.md) +* [Net](net.md) +* [OS](os.md) +* [Path](path.md) +* [Performance hooks](perf_hooks.md) +* [Policies](policy.md) +* [Process](process.md) +* [Punycode](punycode.md) +* [Query strings](querystring.md) +* [Readline](readline.md) +* [REPL](repl.md) +* [Report](report.md) +* [Stream](stream.md) +* [String decoder](string_decoder.md) +* [Timers](timers.md) +* [TLS/SSL](tls.md) +* [Trace events](tracing.md) +* [TTY](tty.md) +* [UDP/datagram](dgram.md) +* [URL](url.md) +* [Utilities](util.md) +* [V8](v8.md) +* [VM](vm.md) +* [WASI](wasi.md) +* [Worker threads](worker_threads.md) +* [Zlib](zlib.md)
diff --git a/doc/api/inspector.md b/doc/api/inspector.md index 7dc359a0185404..c22dbca03bcbf1 100644 --- a/doc/api/inspector.md +++ b/doc/api/inspector.md @@ -45,7 +45,7 @@ started. If wait is `true`, will block until a client has connected to the inspect port and flow control has been passed to the debugger client. -See the [security warning](cli.html#inspector_security) regarding the `host` +See the [security warning](cli.md#inspector_security) regarding the `host` parameter usage. ## `inspector.url()` diff --git a/doc/api/intl.md b/doc/api/intl.md index 8b7dee489b4e9b..3d6132f9dc5c9b 100644 --- a/doc/api/intl.md +++ b/doc/api/intl.md @@ -189,26 +189,26 @@ to be helpful: dedicated to ECMA-402. ["ICU Data"]: http://userguide.icu-project.org/icudata -[`--icu-data-dir`]: cli.html#cli_icu_data_dir_file +[BUILDING.md]: https://github.com/nodejs/node/blob/master/BUILDING.md +[ECMA-262]: https://tc39.github.io/ecma262/ +[ECMA-402]: https://tc39.github.io/ecma402/ +[ICU]: http://site.icu-project.org/ +[REPL]: repl.md#repl_repl +[Test262]: https://github.com/tc39/test262/tree/master/test/intl402 +[WHATWG URL parser]: url.md#url_the_whatwg_url_api +[`--icu-data-dir`]: cli.md#cli_icu_data_dir_file [`Date.prototype.toLocaleString()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString [`Intl.DateTimeFormat`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat [`Intl`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl -[`NODE_ICU_DATA`]: cli.html#cli_node_icu_data_file +[`NODE_ICU_DATA`]: cli.md#cli_node_icu_data_file [`Number.prototype.toLocaleString()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString [`RegExp` Unicode Property Escapes]: https://github.com/tc39/proposal-regexp-unicode-property-escapes [`String.prototype.localeCompare()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare [`String.prototype.normalize()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/normalize [`String.prototype.toLowerCase()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toLowerCase [`String.prototype.toUpperCase()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toUpperCase -[`require('buffer').transcode()`]: buffer.html#buffer_buffer_transcode_source_fromenc_toenc -[`require('util').TextDecoder`]: util.html#util_class_util_textdecoder -[BUILDING.md]: https://github.com/nodejs/node/blob/master/BUILDING.md -[ECMA-262]: https://tc39.github.io/ecma262/ -[ECMA-402]: https://tc39.github.io/ecma402/ -[ICU]: http://site.icu-project.org/ -[REPL]: repl.html#repl_repl -[Test262]: https://github.com/tc39/test262/tree/master/test/intl402 -[WHATWG URL parser]: url.html#url_the_whatwg_url_api +[`require('buffer').transcode()`]: buffer.md#buffer_buffer_transcode_source_fromenc_toenc +[`require('util').TextDecoder`]: util.md#util_class_util_textdecoder [btest402]: https://github.com/srl295/btest402 [full-icu]: https://www.npmjs.com/package/full-icu [internationalized domain names]: https://en.wikipedia.org/wiki/Internationalized_domain_name diff --git a/doc/api/module.md b/doc/api/module.md index 797c50b9d6e6b5..2fb6ca884b4443 100644 --- a/doc/api/module.md +++ b/doc/api/module.md @@ -200,14 +200,14 @@ consists of the following keys: * originalLine: {number} * originalColumn: {number} -[CommonJS]: modules.html -[ES Modules]: esm.html +[CommonJS]: modules.md +[ES Modules]: esm.md [Source map v3 format]: https://sourcemaps.info/spec.html#h.mofvlxcwqzej -[`--enable-source-maps`]: cli.html#cli_enable_source_maps +[`--enable-source-maps`]: cli.md#cli_enable_source_maps [`Error.prepareStackTrace(error, trace)`]: https://v8.dev/docs/stack-trace-api#customizing-stack-traces -[`NODE_V8_COVERAGE=dir`]: cli.html#cli_node_v8_coverage_dir +[`NODE_V8_COVERAGE=dir`]: cli.md#cli_node_v8_coverage_dir [`SourceMap`]: #module_class_module_sourcemap [`createRequire()`]: #module_module_createrequire_filename -[`module`]: modules.html#modules_the_module_object -[module wrapper]: modules_cjs.html#modules_cjs_the_module_wrapper +[`module`]: modules.md#modules_the_module_object +[module wrapper]: modules.md#modules_the_module_wrapper [source map include directives]: https://sourcemaps.info/spec.html#h.lmz475t4mvbx diff --git a/doc/api/modules.md b/doc/api/modules.md index 4e7b3da3408827..0816b7257f1b89 100644 --- a/doc/api/modules.md +++ b/doc/api/modules.md @@ -952,7 +952,7 @@ in order to be used. ## The `Module` object This section was moved to -[Modules: `module` core module](module.html#module_the_module_object). +[Modules: `module` core module](module.md#module_the_module_object). * `module.builtinModules` @@ -963,7 +963,7 @@ This section was moved to ## Source map v3 support This section was moved to -[Modules: `module` core module](module.html#module_source_map_v3_support). +[Modules: `module` core module](module.md#module_source_map_v3_support). * `module.findSourceMap(path[, error])` @@ -972,19 +972,19 @@ This section was moved to * `sourceMap.payload` * `sourceMap.findEntry(lineNumber, columnNumber)` +[ECMAScript Modules]: esm.md [GLOBAL_FOLDERS]: #modules_loading_from_the_global_folders -[`Error`]: errors.html#errors_class_error +[`"main"`]: packages.md#packages_main +[`Error`]: errors.md#errors_class_error [`__dirname`]: #modules_dirname [`__filename`]: #modules_filename [`module` object]: #modules_the_module_object [`module.id`]: #modules_module_id [`module.children`]: #modules_module_children -[`path.dirname()`]: path.html#path_path_dirname_path -[ECMAScript Modules]: esm.html -[an error]: errors.html#errors_err_require_esm +[`package.json`]: packages.md#packages_node_js_package_json_field_definitions +[`path.dirname()`]: path.md#path_path_dirname_path +[`require.main`]: #modules_require_main +[an error]: errors.md#errors_err_require_esm [exports shortcut]: #modules_exports_shortcut [module resolution]: #modules_all_together -[native addons]: addons.html -[`require.main`]: #modules_require_main -[`package.json`]: packages.html#packages_node_js_package_json_field_definitions -[`"main"`]: packages.html#packages_main +[native addons]: addons.md diff --git a/doc/api/n-api.md b/doc/api/n-api.md index 49e3986b418657..e6ff1f393fb9f2 100644 --- a/doc/api/n-api.md +++ b/doc/api/n-api.md @@ -5885,9 +5885,9 @@ This API may only be called from the main thread. [ABI Stability]: https://nodejs.org/en/docs/guides/abi-stability/ [AppVeyor]: https://www.appveyor.com -[C++ Addons]: addons.html -[CMake.js]: https://github.com/cmake-js/cmake-js +[C++ Addons]: addons.md [CMake]: https://cmake.org +[CMake.js]: https://github.com/cmake-js/cmake-js [ECMAScript Language Specification]: https://tc39.github.io/ecma262/ [Error handling]: #n_api_error_handling [GCC]: https://gcc.gnu.org @@ -5902,37 +5902,37 @@ This API may only be called from the main thread. [Section 19.2]: https://tc39.github.io/ecma262/#sec-function-objects [Section 19.4]: https://tc39.github.io/ecma262/#sec-symbol-objects [Section 20.3]: https://tc39.github.io/ecma262/#sec-date-objects -[Section 22.1.4.1]: https://tc39.github.io/ecma262/#sec-properties-of-array-instances-length [Section 22.1]: https://tc39.github.io/ecma262/#sec-array-objects +[Section 22.1.4.1]: https://tc39.github.io/ecma262/#sec-properties-of-array-instances-length [Section 22.2]: https://tc39.github.io/ecma262/#sec-typedarray-objects -[Section 24.1.1.3]: https://tc39.es/ecma262/#sec-detacharraybuffer [Section 24.1]: https://tc39.github.io/ecma262/#sec-arraybuffer-objects +[Section 24.1.1.2]: https://tc39.es/ecma262/#sec-isdetachedbuffer +[Section 24.1.1.3]: https://tc39.es/ecma262/#sec-detacharraybuffer [Section 24.3]: https://tc39.github.io/ecma262/#sec-dataview-objects [Section 25.4]: https://tc39.github.io/ecma262/#sec-promise-objects +[Section 6]: https://tc39.github.io/ecma262/#sec-ecmascript-data-types-and-values +[Section 6.1]: https://tc39.github.io/ecma262/#sec-ecmascript-language-types [Section 6.1.4]: https://tc39.github.io/ecma262/#sec-ecmascript-language-types-string-type [Section 6.1.6]: https://tc39.github.io/ecma262/#sec-ecmascript-language-types-number-type -[Section 6.1.7.1]: https://tc39.github.io/ecma262/#table-2 [Section 6.1.7]: https://tc39.github.io/ecma262/#sec-object-type -[Section 6.1]: https://tc39.github.io/ecma262/#sec-ecmascript-language-types -[Section 6]: https://tc39.github.io/ecma262/#sec-ecmascript-data-types-and-values +[Section 6.1.7.1]: https://tc39.github.io/ecma262/#table-2 +[Section 7]: https://tc39.github.io/ecma262/#sec-abstract-operations [Section 7.1.13]: https://tc39.github.io/ecma262/#sec-toobject [Section 7.1.2]: https://tc39.github.io/ecma262/#sec-toboolean [Section 7.1.3]: https://tc39.github.io/ecma262/#sec-tonumber [Section 7.2.14]: https://tc39.github.io/ecma262/#sec-strict-equality-comparison [Section 7.2.2]: https://tc39.github.io/ecma262/#sec-isarray -[Section 7]: https://tc39.github.io/ecma262/#sec-abstract-operations [Section 8.7]: https://tc39.es/ecma262/#sec-agents [Section 9.1.6]: https://tc39.github.io/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-defineownproperty-p-desc -[Section 24.1.1.2]: https://tc39.es/ecma262/#sec-isdetachedbuffer [Travis CI]: https://travis-ci.org [Visual Studio]: https://visualstudio.microsoft.com [Working with JavaScript properties]: #n_api_working_with_javascript_properties [Xcode]: https://developer.apple.com/xcode/ [`Number.MAX_SAFE_INTEGER`]: https://tc39.github.io/ecma262/#sec-number.max_safe_integer [`Number.MIN_SAFE_INTEGER`]: https://tc39.github.io/ecma262/#sec-number.min_safe_integer -[`Worker`]: worker_threads.html#worker_threads_class_worker -[`global`]: globals.html#globals_global -[`init` hooks]: async_hooks.html#async_hooks_init_asyncid_type_triggerasyncid_resource +[`Worker`]: worker_threads.md#worker_threads_class_worker +[`global`]: globals.md#globals_global +[`init` hooks]: async_hooks.md#async_hooks_init_asyncid_type_triggerasyncid_resource [`napi_add_async_cleanup_hook`]: #n_api_napi_add_async_cleanup_hook [`napi_add_env_cleanup_hook`]: #n_api_napi_add_env_cleanup_hook [`napi_add_finalizer`]: #n_api_napi_add_finalizer @@ -5988,14 +5988,14 @@ This API may only be called from the main thread. [`napi_wrap`]: #n_api_napi_wrap [`node-addon-api`]: https://github.com/nodejs/node-addon-api [`node_api.h`]: https://github.com/nodejs/node/blob/master/src/node_api.h -[`process.release`]: process.html#process_process_release +[`process.release`]: process.md#process_process_release [`uv_ref`]: https://docs.libuv.org/en/v1.x/handle.html#c.uv_ref [`uv_unref`]: https://docs.libuv.org/en/v1.x/handle.html#c.uv_unref -[async_hooks `type`]: async_hooks.html#async_hooks_type -[context-aware addons]: addons.html#addons_context_aware_addons +[async_hooks `type`]: async_hooks.md#async_hooks_type +[context-aware addons]: addons.md#addons_context_aware_addons [docs]: https://github.com/nodejs/node-addon-api#api-documentation -[global scope]: globals.html -[module scope]: modules.html#modules_the_module_scope +[global scope]: globals.md +[module scope]: modules.md#modules_the_module_scope [node-gyp]: https://github.com/nodejs/node-gyp [node-pre-gyp]: https://github.com/mapbox/node-pre-gyp [prebuild]: https://github.com/prebuild/prebuild diff --git a/doc/api/net.md b/doc/api/net.md index 28de44d8897246..103a462bc66e20 100644 --- a/doc/api/net.md +++ b/doc/api/net.md @@ -1248,7 +1248,7 @@ Returns `true` if input is a version 6 IP address, otherwise returns `false`. [IPC]: #net_ipc_support [Identifying paths for IPC connections]: #net_identifying_paths_for_ipc_connections -[Readable Stream]: stream.html#stream_class_stream_readable +[Readable Stream]: stream.md#stream_class_stream_readable [`'close'`]: #net_event_close [`'connect'`]: #net_event_connect [`'connection'`]: #net_event_connection @@ -1258,10 +1258,10 @@ Returns `true` if input is a version 6 IP address, otherwise returns `false`. [`'error'`]: #net_event_error_1 [`'listening'`]: #net_event_listening [`'timeout'`]: #net_event_timeout -[`EventEmitter`]: events.html#events_class_eventemitter -[`child_process.fork()`]: child_process.html#child_process_child_process_fork_modulepath_args_options -[`dns.lookup()` hints]: dns.html#dns_supported_getaddrinfo_flags -[`dns.lookup()`]: dns.html#dns_dns_lookup_hostname_options_callback +[`EventEmitter`]: events.md#events_class_eventemitter +[`child_process.fork()`]: child_process.md#child_process_child_process_fork_modulepath_args_options +[`dns.lookup()`]: dns.md#dns_dns_lookup_hostname_options_callback +[`dns.lookup()` hints]: dns.md#dns_supported_getaddrinfo_flags [`net.Server`]: #net_class_net_server [`net.Socket`]: #net_class_net_socket [`net.connect()`]: #net_net_connect @@ -1274,7 +1274,7 @@ Returns `true` if input is a version 6 IP address, otherwise returns `false`. [`net.createConnection(port, host)`]: #net_net_createconnection_port_host_connectlistener [`net.createServer()`]: #net_net_createserver_options_connectionlistener [`new net.Socket(options)`]: #net_new_net_socket_options -[`readable.setEncoding()`]: stream.html#stream_readable_setencoding_encoding +[`readable.setEncoding()`]: stream.md#stream_readable_setencoding_encoding [`server.close()`]: #net_server_close_callback [`server.getConnections()`]: #net_server_getconnections_callback [`server.listen()`]: #net_server_listen @@ -1294,11 +1294,11 @@ Returns `true` if input is a version 6 IP address, otherwise returns `false`. [`socket.setEncoding()`]: #net_socket_setencoding_encoding [`socket.setTimeout()`]: #net_socket_settimeout_timeout_callback [`socket.setTimeout(timeout)`]: #net_socket_settimeout_timeout_callback -[`writable.writableLength`]: stream.html#stream_writable_writablelength -[`writable.destroyed`]: stream.html#stream_writable_destroyed -[`writable.destroy()`]: stream.html#stream_writable_destroy_error -[`writable.end()`]: stream.html#stream_writable_end_chunk_encoding_callback +[`writable.destroy()`]: stream.md#stream_writable_destroy_error +[`writable.destroyed`]: stream.md#stream_writable_destroyed +[`writable.end()`]: stream.md#stream_writable_end_chunk_encoding_callback +[`writable.writableLength`]: stream.md#stream_writable_writablelength [half-closed]: https://tools.ietf.org/html/rfc1122 -[stream_writable_write]: stream.html#stream_writable_write_chunk_encoding_callback +[stream_writable_write]: stream.md#stream_writable_write_chunk_encoding_callback [unspecified IPv4 address]: https://en.wikipedia.org/wiki/0.0.0.0 [unspecified IPv6 address]: https://en.wikipedia.org/wiki/IPv6_address#Unspecified_address diff --git a/doc/api/os.md b/doc/api/os.md index 9dfe629a97d2f8..239255147d4f03 100644 --- a/doc/api/os.md +++ b/doc/api/os.md @@ -1267,9 +1267,9 @@ The following process scheduling constants are exported by -[`SystemError`]: errors.html#errors_class_systemerror -[`process.arch`]: process.html#process_process_arch -[`process.platform`]: process.html#process_process_platform -[`uname(3)`]: https://linux.die.net/man/3/uname [Android building]: https://github.com/nodejs/node/blob/master/BUILDING.md#androidandroid-based-devices-eg-firefox-os [EUID]: https://en.wikipedia.org/wiki/User_identifier#Effective_user_ID +[`SystemError`]: errors.md#errors_class_systemerror +[`process.arch`]: process.md#process_process_arch +[`process.platform`]: process.md#process_process_platform +[`uname(3)`]: https://linux.die.net/man/3/uname diff --git a/doc/api/packages.md b/doc/api/packages.md index 77183fb23ec499..68dd30aca3d149 100644 --- a/doc/api/packages.md +++ b/doc/api/packages.md @@ -1,6 +1,36 @@ # Modules: Packages + ## Introduction @@ -90,7 +120,7 @@ import 'commonjs-package/src/index.mjs'; // Loaded as ES module since .mjs is always loaded as ES module. ``` -The `.mjs` and `.cjs` extensions may be used to mix types within the same +The `.mjs` and `.cjs` extensions can be used to mix types within the same package: * Within a `"type": "module"` package, Node.js can be instructed to @@ -106,7 +136,7 @@ package: ### `--input-type` flag Strings passed in as an argument to `--eval` (or `-e`), or piped to `node` via -`STDIN`, will be treated as [ES modules][] when the `--input-type=module` flag +`STDIN`, are treated as [ES modules][] when the `--input-type=module` flag is set. ```bash @@ -134,7 +164,7 @@ their package. If both [`"exports"`][] and [`"main"`][] are defined, the [`"exports"`][] field takes precedence over [`"main"`][]. [`"exports"`][] are not specific to ES -modules or CommonJS; [`"main"`][] will be overridden by [`"exports"`][] if it +modules or CommonJS; [`"main"`][] is overridden by [`"exports"`][] if it exists. As such [`"main"`][] cannot be used as a fallback for CommonJS but it can be used as a fallback for legacy versions of Node.js that do not support the [`"exports"`][] field. @@ -188,13 +218,13 @@ Alternatively a project could choose to export entire folders: ``` As a last resort, package encapsulation can be disabled entirely by creating an -export for the root of the package `"./*": "./*"`. This will expose every file +export for the root of the package `"./*": "./*"`. This exposes every file in the package at the cost of disabling the encapsulation and potential tooling benefits this provides. As the ES Module loader in Node.js enforces the use of [the full specifier path][], exporting the root rather than being explicit about entry is less expressive than either of the prior examples. Not only -will encapsulation be lost but module consumers will be unable to -`import feature from 'my-mod/feature'` as they will need to provide the full +is encapsulation lost but module consumers are unable to +`import feature from 'my-mod/feature'` as they need to provide the full path `import feature from 'my-mod/feature/index.js`. ### Main entry point export @@ -209,9 +239,9 @@ To set the main entry point for a package, it is advisable to define both } ``` -When defining the [`"exports"`][] field, all subpaths of the package will be +When the [`"exports"`][] field is defined, all subpaths of the package are encapsulated and no longer available to importers. For example, -`require('pkg/subpath.js')` would throw an [`ERR_PACKAGE_PATH_NOT_EXPORTED`][] +`require('pkg/subpath.js')` throws an [`ERR_PACKAGE_PATH_NOT_EXPORTED`][] error. This encapsulation of exports provides more reliable guarantees @@ -343,20 +373,20 @@ For example, a package that wants to provide different ES module exports for Node.js supports the following conditions out of the box: -* `"import"` - matched when the package is loaded via `import` or +* `"import"` - matches when the package is loaded via `import` or `import()`, or via any top-level import or resolve operation by the ECMAScript module loader. Applies regardless of the module format of the target file. _Always mutually exclusive with `"require"`._ -* `"require"` - matched when the package is loaded via `require()`. The +* `"require"` - matches when the package is loaded via `require()`. The referenced file should be loadable with `require()` although the condition - will be matched regardless of the module format of the target file. Expected + matches regardless of the module format of the target file. Expected formats include CommonJS, JSON, and native addons but not ES modules as `require()` doesn't support them. _Always mutually exclusive with `"import"`._ -* `"node"` - matched for any Node.js environment. Can be a CommonJS or ES +* `"node"` - matches for any Node.js environment. Can be a CommonJS or ES module file. _This condition should always come after `"import"` or `"require"`._ -* `"default"` - the generic fallback that will always match. Can be a CommonJS +* `"default"` - the generic fallback that always matches. Can be a CommonJS or ES module file. _This condition should always come last._ Within the [`"exports"`][] object, key order is significant. During condition @@ -365,9 +395,9 @@ entries. _The general rule is that conditions should be from most specific to least specific in object order_. Other conditions such as `"browser"`, `"electron"`, `"deno"`, `"react-native"`, -etc. are unknown to, and thus ignored by Node.js. Runtimes or tools other than -Node.js may use them at their discretion. Further restrictions, definitions, or -guidance on condition names may occur in the future. +etc., are unknown to Node.js, and thus ignored. Runtimes or tools other than +Node.js can use them at their discretion. Further restrictions, definitions, or +guidance on condition names might occur in the future. Using the `"import"` and `"require"` conditions can lead to some hazards, which are further explained in [the dual CommonJS/ES module packages section][]. @@ -534,13 +564,13 @@ when using transpilation via tools like [Babel][] or [`esm`][]. First, the hazard described in the previous section occurs when a package contains both CommonJS and ES module sources and both sources are provided for use in Node.js, either via separate main entry points or exported paths. A -package could instead be written where any version of Node.js receives only -CommonJS sources, and any separate ES module sources the package may contain -could be intended only for other environments such as browsers. Such a package +package might instead be written where any version of Node.js receives only +CommonJS sources, and any separate ES module sources the package might contain +are intended only for other environments such as browsers. Such a package would be usable by any version of Node.js, since `import` can refer to CommonJS files; but it would not provide any of the advantages of using ES module syntax. -A package could also switch from CommonJS to ES module syntax in a [breaking +A package might also switch from CommonJS to ES module syntax in a [breaking change](https://semver.org/) version bump. This has the disadvantage that the newest version of the package would only be usable in ES module-supporting versions of Node.js. @@ -669,7 +699,7 @@ the package’s management of state is carefully isolated (or the package is stateless). The reason that state is an issue is because both the CommonJS and ES module -versions of the package may get used within an application; for example, the +versions of the package might get used within an application; for example, the user’s application code could `import` the ES module version while a dependency `require`s the CommonJS version. If that were to occur, two copies of the package would be loaded in memory and therefore two separate states would be @@ -751,7 +781,7 @@ conditional exports for consumers could be to add an export, e.g. ## Node.js `package.json` field definitions This section describes the fields used by the Node.js runtime. Other tools (such -as [npm](https://docs.npmjs.com/creating-a-package-json-file)) may use +as [npm](https://docs.npmjs.com/creating-a-package-json-file)) use additional fields which are ignored by Node.js and not documented here. The following fields in `package.json` files are used in Node.js: @@ -761,7 +791,7 @@ The following fields in `package.json` files are used in Node.js: * [`"type"`][] - The package type determining whether to load `.js` files as CommonJS or ES modules. * [`"exports"`][] - Package exports and conditional exports. When present, - limits which submodules may be loaded from within the package. + limits which submodules can be loaded from within the package. * [`"main"`][] - The default module when loading the package, if exports is not specified, and in versions of Node.js prior to the introduction of exports. * [`"imports"`][] - Package imports, for use by modules within the package @@ -789,7 +819,7 @@ changes: ``` The `"name"` field defines your package’s name. Publishing to the -_npm_ registry may require a name that satisfies +_npm_ registry requires a name that satisfies [certain requirements](https://docs.npmjs.com/files/package.json#name). The `"name"` field can be used in addition to the [`"exports"`][] field to @@ -808,10 +838,10 @@ changes: * Type: {string} -The `"type"` field defines the module format that Node.js will use for all +The `"type"` field defines the module format that Node.js uses for all `.js` files that have that `package.json` file as their nearest parent. -Files ending with `.js` will be loaded as ES modules when the nearest parent +Files ending with `.js` are loaded as ES modules when the nearest parent `package.json` file contains a top-level field `"type"` with a value of `"module"`. @@ -857,7 +887,7 @@ changes: pr-url: https://github.com/nodejs/node/pull/29978 description: Implement conditional exports. - version: - - v12.15.0 + - v12.16.0 - v13.7.0 pr-url: https://github.com/nodejs/node/pull/31001 description: Remove the `--experimental-conditional-exports` option. @@ -903,7 +933,7 @@ added: v0.4.0 ``` The `"main"` field defines the script that is used when the [package directory -is loaded via `require()`](modules.html#modules_folders_as_modules). Its value +is loaded via `require()`](modules.md#modules_folders_as_modules). Its value is interpreted as a path. ```js @@ -959,19 +989,19 @@ analogous to the exports field. [Babel]: https://babeljs.io/ [Conditional exports]: #packages_conditional_exports -[CommonJS]: modules.html -[`ERR_PACKAGE_PATH_NOT_EXPORTED`]: errors.html#errors_err_package_path_not_exported -[ES modules]: esm.html -[ES module]: esm.html +[CommonJS]: modules.md +[ES module]: esm.md +[ES modules]: esm.md +[`ERR_PACKAGE_PATH_NOT_EXPORTED`]: errors.md#errors_err_package_path_not_exported [`esm`]: https://github.com/standard-things/esm#readme [`"exports"`]: #packages_exports [`"main"`]: #packages_main [`"name"`]: #packages_name [`"imports"`]: #packages_imports [`"type"`]: #packages_type -[entry points]: #packages_package_entry_points [`package.json`]: #packages_node_js_package_json_field_definitions +[entry points]: #packages_package_entry_points [self-reference]: #packages_self_referencing_a_package_using_its_name [subpath exports]: #packages_subpath_exports -[the full specifier path]: modules_esm.html#modules_esm_mandatory_file_extensions +[the full specifier path]: esm.md#esm_mandatory_file_extensions [the dual CommonJS/ES module packages section]: #packages_dual_commonjs_es_module_packages diff --git a/doc/api/path.md b/doc/api/path.md index e78850fb7d2114..c26a98e59c9d6d 100644 --- a/doc/api/path.md +++ b/doc/api/path.md @@ -575,10 +575,10 @@ added: v0.11.15 The `path.win32` property provides access to Windows-specific implementations of the `path` methods. -[`TypeError`]: errors.html#errors_class_typeerror +[MSDN-Rel-Path]: https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file#fully-qualified-vs-relative-paths +[`TypeError`]: errors.md#errors_class_typeerror [`path.parse()`]: #path_path_parse_path [`path.posix`]: #path_path_posix [`path.sep`]: #path_path_sep [`path.win32`]: #path_path_win32 -[MSDN-Rel-Path]: https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file#fully-qualified-vs-relative-paths [namespace-prefixed path]: https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file#namespaces diff --git a/doc/api/perf_hooks.md b/doc/api/perf_hooks.md index 67f791653f9421..6b14a23c474a26 100644 --- a/doc/api/perf_hooks.md +++ b/doc/api/perf_hooks.md @@ -58,9 +58,10 @@ Performance Timeline. If `name` is provided, removes only the named mark. added: v14.10.0 --> -* `utilization1` {Object} The result of a previous call to `eventLoopUtilization()` -* `utilization2` {Object} The result of a previous call to `eventLoopUtilization()` - prior to `util1` +* `utilization1` {Object} The result of a previous call to + `eventLoopUtilization()`. +* `utilization2` {Object} The result of a previous call to + `eventLoopUtilization()` prior to `utilization1`. * Returns {Object} * `idle` {number} * `active` {number} @@ -70,9 +71,9 @@ The `eventLoopUtilization()` method returns an object that contains the cumulative duration of time the event loop has been both idle and active as a high resolution milliseconds timer. The `utilization` value is the calculated Event Loop Utilization (ELU). If bootstrapping has not yet finished, the -properties have the value of 0. +properties have the value of `0`. -`utilization1` and `utilization2` are optional parameters. +Both `utilization1` and `utilization2` are optional parameters. If `utilization1` is passed, then the delta between the current call's `active` and `idle` times, as well as the corresponding `utilization` value are @@ -102,8 +103,8 @@ setImmediate(() => { ``` Although the CPU is mostly idle while running this script, the value of -`utilization` is 1. This is because the call to [`child_process.spawnSync()`][] -blocks the event loop from proceeding. +`utilization` is `1`. This is because the call to +[`child_process.spawnSync()`][] blocks the event loop from proceeding. Passing in a user-defined object instead of the result of a previous call to `eventLoopUtilization()` will lead to undefined behavior. The return values @@ -752,13 +753,13 @@ obs.observe({ entryTypes: ['function'], buffered: true }); require('some-module'); ``` -[`'exit'`]: process.html#process_event_exit -[`process.hrtime()`]: process.html#process_process_hrtime_time -[`child_process.spawnSync()`]: child_process.html#child_process_child_process_spawnsync_command_args_options -[`timeOrigin`]: https://w3c.github.io/hr-time/#dom-performance-timeorigin -[`window.performance`]: https://developer.mozilla.org/en-US/docs/Web/API/Window/performance -[Async Hooks]: async_hooks.html +[Async Hooks]: async_hooks.md [High Resolution Time]: https://www.w3.org/TR/hr-time-2 [Performance Timeline]: https://w3c.github.io/performance-timeline/ -[Web Performance APIs]: https://w3c.github.io/perf-timing-primer/ [User Timing]: https://www.w3.org/TR/user-timing/ +[Web Performance APIs]: https://w3c.github.io/perf-timing-primer/ +[`'exit'`]: process.md#process_event_exit +[`child_process.spawnSync()`]: child_process.md#child_process_child_process_spawnsync_command_args_options +[`process.hrtime()`]: process.md#process_process_hrtime_time +[`timeOrigin`]: https://w3c.github.io/hr-time/#dom-performance-timeorigin +[`window.performance`]: https://developer.mozilla.org/en-US/docs/Web/API/Window/performance diff --git a/doc/api/process.md b/doc/api/process.md index 0f20ade9799225..bfe9df10d685d5 100644 --- a/doc/api/process.md +++ b/doc/api/process.md @@ -2584,24 +2584,37 @@ cases: For example, signal `SIGABRT` has value `6`, so the expected exit code will be `128` + `6`, or `134`. +[Advanced serialization for `child_process`]: child_process.md#child_process_advanced_serialization +[Android building]: https://github.com/nodejs/node/blob/master/BUILDING.md#androidandroid-based-devices-eg-firefox-os +[Child Process]: child_process.md +[Cluster]: cluster.md +[Duplex]: stream.md#stream_duplex_and_transform_streams +[Event Loop]: https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/#process-nexttick +[LTS]: https://github.com/nodejs/Release +[Readable]: stream.md#stream_readable_streams +[Signal Events]: #process_signal_events +[Stream compatibility]: stream.md#stream_compatibility_with_older_node_js_versions +[TTY]: tty.md#tty_tty +[Writable]: stream.md#stream_writable_streams [`'exit'`]: #process_event_exit -[`'message'`]: child_process.html#child_process_event_message +[`'message'`]: child_process.md#child_process_event_message [`'uncaughtException'`]: #process_event_uncaughtexception -[`--unhandled-rejections`]: cli.html#cli_unhandled_rejections_mode -[`Buffer`]: buffer.html -[`ChildProcess.disconnect()`]: child_process.html#child_process_subprocess_disconnect -[`ChildProcess.send()`]: child_process.html#child_process_subprocess_send_message_sendhandle_options_callback -[`ChildProcess`]: child_process.html#child_process_class_childprocess -[`Error`]: errors.html#errors_class_error -[`EventEmitter`]: events.html#events_class_eventemitter -[`NODE_OPTIONS`]: cli.html#cli_node_options_options -[`Worker`]: worker_threads.html#worker_threads_class_worker -[`console.error()`]: console.html#console_console_error_data_args -[`console.log()`]: console.html#console_console_log_data_args -[`domain`]: domain.html -[`net.Server`]: net.html#net_class_net_server -[`net.Socket`]: net.html#net_class_net_socket -[`os.constants.dlopen`]: os.html#os_dlopen_constants +[`--unhandled-rejections`]: cli.md#cli_unhandled_rejections_mode +[`Buffer`]: buffer.md +[`ChildProcess.disconnect()`]: child_process.md#child_process_subprocess_disconnect +[`ChildProcess.send()`]: child_process.md#child_process_subprocess_send_message_sendhandle_options_callback +[`ChildProcess`]: child_process.md#child_process_class_childprocess +[`Error`]: errors.md#errors_class_error +[`EventEmitter`]: events.md#events_class_eventemitter +[`NODE_OPTIONS`]: cli.md#cli_node_options_options +[`Promise.race()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/race +[`Worker`]: worker_threads.md#worker_threads_class_worker +[`console.error()`]: console.md#console_console_error_data_args +[`console.log()`]: console.md#console_console_log_data_args +[`domain`]: domain.md +[`net.Server`]: net.md#net_class_net_server +[`net.Socket`]: net.md#net_class_net_socket +[`os.constants.dlopen`]: os.md#os_dlopen_constants [`process.argv`]: #process_process_argv [`process.config`]: #process_process_config [`process.execPath`]: #process_process_execpath @@ -2610,34 +2623,21 @@ cases: [`process.hrtime()`]: #process_process_hrtime_time [`process.hrtime.bigint()`]: #process_process_hrtime_bigint [`process.kill()`]: #process_process_kill_pid_signal -[`process.setUncaughtExceptionCaptureCallback()`]: process.html#process_process_setuncaughtexceptioncapturecallback_fn +[`process.setUncaughtExceptionCaptureCallback()`]: process.md#process_process_setuncaughtexceptioncapturecallback_fn [`promise.catch()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/catch -[`Promise.race()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/race -[`require()`]: globals.html#globals_require -[`require.main`]: modules.html#modules_accessing_the_main_module -[`require.resolve()`]: modules.html#modules_require_resolve_request_options -[`subprocess.kill()`]: child_process.html#child_process_subprocess_kill_signal -[`v8.setFlagsFromString()`]: v8.html#v8_v8_setflagsfromstring_flags -[Advanced serialization for `child_process`]: child_process.html#child_process_advanced_serialization -[Android building]: https://github.com/nodejs/node/blob/master/BUILDING.md#androidandroid-based-devices-eg-firefox-os -[Child Process]: child_process.html -[Cluster]: cluster.html -[Duplex]: stream.html#stream_duplex_and_transform_streams -[Event Loop]: https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/#process-nexttick -[LTS]: https://github.com/nodejs/Release -[Readable]: stream.html#stream_readable_streams -[`readable.read()`]: stream.html#stream_readable_read_size -[Signal Events]: #process_signal_events -[Stream compatibility]: stream.html#stream_compatibility_with_older_node_js_versions -[TTY]: tty.html#tty_tty -[Writable]: stream.html#stream_writable_streams -[debugger]: debugger.html -[note on process I/O]: process.html#process_a_note_on_process_i_o +[`readable.read()`]: stream.md#stream_readable_read_size +[`require()`]: globals.md#globals_require +[`require.main`]: modules.md#modules_accessing_the_main_module +[`require.resolve()`]: modules.md#modules_require_resolve_request_options +[`subprocess.kill()`]: child_process.md#child_process_subprocess_kill_signal +[`v8.setFlagsFromString()`]: v8.md#v8_v8_setflagsfromstring_flags +[debugger]: debugger.md +[note on process I/O]: process.md#process_a_note_on_process_i_o [process.cpuUsage]: #process_process_cpuusage_previousvalue [process_emit_warning]: #process_process_emitwarning_warning_type_code_ctor [process_warning]: #process_event_warning -[report documentation]: report.html -[terminal raw mode]: tty.html#tty_readstream_setrawmode_mode +[report documentation]: report.md +[terminal raw mode]: tty.md#tty_readstream_setrawmode_mode [uv_rusage_t]: https://docs.libuv.org/en/v1.x/misc.html#c.uv_rusage_t -[wikipedia_minor_fault]: https://en.wikipedia.org/wiki/Page_fault#Minor [wikipedia_major_fault]: https://en.wikipedia.org/wiki/Page_fault#Major +[wikipedia_minor_fault]: https://en.wikipedia.org/wiki/Page_fault#Minor diff --git a/doc/api/readline.md b/doc/api/readline.md index 8d3eef1ab42aee..2e82fdf91a1f10 100644 --- a/doc/api/readline.md +++ b/doc/api/readline.md @@ -865,15 +865,15 @@ const { createInterface } = require('readline'); -[`'SIGCONT'`]: readline.html#readline_event_sigcont -[`'SIGTSTP'`]: readline.html#readline_event_sigtstp +[Readable]: stream.md#stream_readable_streams +[TTY]: tty.md +[TTY keybindings]: #readline_tty_keybindings +[Writable]: stream.md#stream_writable_streams +[`'SIGCONT'`]: readline.md#readline_event_sigcont +[`'SIGTSTP'`]: readline.md#readline_event_sigtstp [`'line'`]: #readline_event_line -[`fs.ReadStream`]: fs.html#fs_class_fs_readstream -[`process.stdin`]: process.html#process_process_stdin -[`process.stdout`]: process.html#process_process_stdout +[`fs.ReadStream`]: fs.md#fs_class_fs_readstream +[`process.stdin`]: process.md#process_process_stdin +[`process.stdout`]: process.md#process_process_stdout [`rl.close()`]: #readline_rl_close -[Readable]: stream.html#stream_readable_streams -[TTY]: tty.html -[TTY keybindings]: #readline_tty_keybindings -[Writable]: stream.html#stream_writable_streams [reading files]: #readline_example_read_file_stream_line_by_line diff --git a/doc/api/repl.md b/doc/api/repl.md index a194cf05a8148a..84b82ec8d6d56f 100644 --- a/doc/api/repl.md +++ b/doc/api/repl.md @@ -35,7 +35,7 @@ feature set. The following special commands are supported by all REPL instances: * `.break`: When in the process of inputting a multi-line expression, enter - the `.break` command (or press **Ctrl+C**) to abort + the `.break` command (or press Ctrl+C) to abort further input or processing of that expression. * `.clear`: Resets the REPL `context` to an empty object and clears any multi-line expression being input. @@ -45,7 +45,8 @@ The following special commands are supported by all REPL instances: `> .save ./file/to/save.js` * `.load`: Load a file into the current REPL session. `> .load ./file/to/load.js` -* `.editor`: Enter editor mode (**Ctrl+D** to finish, **Ctrl+C** to cancel). +* `.editor`: Enter editor mode (Ctrl+D to finish, + Ctrl+C to cancel). ```console > .editor @@ -63,11 +64,12 @@ welcome('Node.js User'); The following key combinations in the REPL have these special effects: -* **Ctrl+C**: When pressed once, has the same effect as the `.break` command. +* Ctrl+C: When pressed once, has the same effect as the + `.break` command. When pressed twice on a blank line, has the same effect as the `.exit` command. -* **Ctrl+D**: Has the same effect as the `.exit` command. -* ``: When pressed on a blank line, displays global and local (scope) +* Ctrl+D: Has the same effect as the `.exit` command. +* Tab: When pressed on a blank line, displays global and local (scope) variables. When pressed while entering other input, displays relevant autocompletion options. @@ -246,14 +248,15 @@ added: v13.6.0 --> The REPL supports bi-directional reverse-i-search similar to [ZSH][]. It is -triggered with **Ctrl+R** to search backward and **Ctrl+S** to search +triggered with Ctrl+R to search backward and +Ctrl+S to search forwards. Duplicated history entires will be skipped. Entries are accepted as soon as any button is pressed that doesn't correspond -with the reverse search. Cancelling is possible by pressing **Esc** or -**Ctrl+C**. +with the reverse search. Cancelling is possible by pressing Esc or +Ctrl+C. Changing the direction immediately searches for the next entry in the expected direction from the current position on. @@ -379,8 +382,10 @@ added: v0.7.7 --> The `'exit'` event is emitted when the REPL is exited either by receiving the -`.exit` command as input, the user pressing **Ctrl+C** twice to signal `SIGINT`, -or by pressing **Ctrl+D** to signal `'end'` on the input stream. The listener +`.exit` command as input, the user pressing Ctrl+C twice +to signal `SIGINT`, +or by pressing Ctrl+D to signal `'end'` on the input +stream. The listener callback is invoked without any arguments. ```js @@ -611,7 +616,8 @@ changes: * `repl.REPL_MODE_STRICT` to evaluate expressions in strict mode. This is equivalent to prefacing every repl statement with `'use strict'`. * `breakEvalOnSigint` {boolean} Stop evaluating the current piece of code when - `SIGINT` is received, such as when `Ctrl+C` is pressed. This cannot be used + `SIGINT` is received, such as when Ctrl+C is pressed. + This cannot be used together with a custom `eval` function. **Default:** `false`. * `preview` {boolean} Defines if the repl prints autocomplete and output previews or not. **Default:** `true` with the default eval function and @@ -744,18 +750,18 @@ a `net.Server` and `net.Socket` instance, see: For an example of running a REPL instance over [`curl(1)`][], see: . -[TTY keybindings]: readline.html#readline_tty_keybindings +[TTY keybindings]: readline.md#readline_tty_keybindings [ZSH]: https://en.wikipedia.org/wiki/Z_shell -[`'uncaughtException'`]: process.html#process_event_uncaughtexception -[`--experimental-repl-await`]: cli.html#cli_experimental_repl_await -[`ERR_DOMAIN_CANNOT_SET_UNCAUGHT_EXCEPTION_CAPTURE`]: errors.html#errors_err_domain_cannot_set_uncaught_exception_capture -[`ERR_INVALID_REPL_INPUT`]: errors.html#errors_err_invalid_repl_input +[`'uncaughtException'`]: process.md#process_event_uncaughtexception +[`--experimental-repl-await`]: cli.md#cli_experimental_repl_await +[`ERR_DOMAIN_CANNOT_SET_UNCAUGHT_EXCEPTION_CAPTURE`]: errors.md#errors_err_domain_cannot_set_uncaught_exception_capture +[`ERR_INVALID_REPL_INPUT`]: errors.md#errors_err_invalid_repl_input [`curl(1)`]: https://curl.haxx.se/docs/manpage.html -[`domain`]: domain.html -[`process.setUncaughtExceptionCaptureCallback()`]: process.html#process_process_setuncaughtexceptioncapturecallback_fn -[`readline.InterfaceCompleter`]: readline.html#readline_use_of_the_completer_function +[`domain`]: domain.md +[`process.setUncaughtExceptionCaptureCallback()`]: process.md#process_process_setuncaughtexceptioncapturecallback_fn +[`readline.InterfaceCompleter`]: readline.md#readline_use_of_the_completer_function [`repl.ReplServer`]: #repl_class_replserver [`repl.start()`]: #repl_repl_start_options [`reverse-i-search`]: #repl_reverse_i_search -[`util.inspect()`]: util.html#util_util_inspect_object_options -[stream]: stream.html +[`util.inspect()`]: util.md#util_util_inspect_object_options +[stream]: stream.md diff --git a/doc/api/report.md b/doc/api/report.md index 95d53697f0df4b..b62de913be2b29 100644 --- a/doc/api/report.md +++ b/doc/api/report.md @@ -598,5 +598,5 @@ The thread which is generating the report will wait for the reports from Worker threads to finish. However, the latency for this will usually be low, as both running JavaScript and the event loop are interrupted to generate the report. -[`process API documentation`]: process.html -[`Worker`]: worker_threads.html +[`Worker`]: worker_threads.md +[`process API documentation`]: process.md diff --git a/doc/api/stream.md b/doc/api/stream.md index e3a3b7c3890015..9144c3debcbf8f 100644 --- a/doc/api/stream.md +++ b/doc/api/stream.md @@ -382,7 +382,7 @@ added: v8.0.0 changes: - version: v14.0.0 pr-url: https://github.com/nodejs/node/pull/29197 - description: Work as noop when called on an already `destroyed` stream. + description: Work as a no-op on a stream that has already been destroyed. --> * `error` {Error} Optional, an error to emit with `'error'` event. @@ -397,8 +397,8 @@ This is a destructive and immediate way to destroy a stream. Previous calls to Use `end()` instead of destroy if data should flush before close, or wait for the `'drain'` event before destroying the stream. -Once `destroy()` has been called any further calls will be a noop and no -further errors except from `_destroy` may be emitted as `'error'`. +Once `destroy()` has been called any further calls will be a no-op and no +further errors except from `_destroy()` may be emitted as `'error'`. Implementors should not override this method, but instead implement [`writable._destroy()`][writable-_destroy]. @@ -968,7 +968,7 @@ added: v8.0.0 changes: - version: v14.0.0 pr-url: https://github.com/nodejs/node/pull/29197 - description: Work as noop when called on an already `destroyed` stream. + description: Work as a no-op on a stream that has already been destroyed. --> * `error` {Error} Error which will be passed as payload in `'error'` event @@ -979,8 +979,8 @@ event (unless `emitClose` is set to `false`). After this call, the readable stream will release any internal resources and subsequent calls to `push()` will be ignored. -Once `destroy()` has been called any further calls will be a noop and no -further errors except from `_destroy` may be emitted as `'error'`. +Once `destroy()` has been called any further calls will be a no-op and no +further errors except from `_destroy()` may be emitted as `'error'`. Implementors should not override this method, but instead implement [`readable._destroy()`][readable-_destroy]. @@ -1536,7 +1536,7 @@ added: v8.0.0 changes: - version: v14.0.0 pr-url: https://github.com/nodejs/node/pull/29197 - description: Work as noop when called on an already `destroyed` stream. + description: Work as a no-op on a stream that has already been destroyed. --> * `error` {Error} @@ -1549,8 +1549,8 @@ Implementors should not override this method, but instead implement The default implementation of `_destroy()` for `Transform` also emit `'close'` unless `emitClose` is set in false. -Once `destroy()` has been called any further calls will be a noop and no -further errors except from `_destroy` may be emitted as `'error'`. +Once `destroy()` has been called, any further calls will be a no-op and no +further errors except from `_destroy()` may be emitted as `'error'`. ### `stream.finished(stream[, options], callback)` + +* `callback` {Function} Call this function (optionally with an error + argument) when the stream has finished initializing. + +The `_construct()` method MUST NOT be called directly. It may be implemented +by child classes, and if so, will be called by the internal `Writable` +class methods only. + +This optional function will be called in a tick after the stream constructor +has returned, delaying any `_write()`, `_final()` and `_destroy()` calls until +`callback` is called. This is useful to initialize state or asynchronously +initialize resources before the stream can be used. + +```js +const { Writable } = require('stream'); +const fs = require('fs'); + +class WriteStream extends Writable { + constructor(filename) { + super(); + this.filename = filename; + this.fd = fd; + } + _construct(callback) { + fs.open(this.filename, (fd, err) => { + if (err) { + callback(err); + } else { + this.fd = fd; + callback(); + } + }); + } + _write(chunk, encoding, callback) { + fs.write(this.fd, chunk, callback); + } + _destroy(err, callback) { + if (this.fd) { + fs.close(this.fd, (er) => callback(er || err)); + } else { + callback(err); + } + } +} +``` + #### `writable._write(chunk, encoding, callback)` + +* `callback` {Function} Call this function (optionally with an error + argument) when the stream has finished initializing. + +The `_construct()` method MUST NOT be called directly. It may be implemented +by child classes, and if so, will be called by the internal `Readable` +class methods only. + +This optional function will be scheduled in the next tick by the stream +constructor, delaying any `_read()` and `_destroy()` calls until `callback` is +called. This is useful to initialize state or asynchronously initialize +resources before the stream can be used. + +```js +const { Readable } = require('stream'); +const fs = require('fs'); + +class ReadStream extends Readable { + constructor(filename) { + super(); + this.filename = filename; + this.fd = null; + } + _construct(callback) { + fs.open(this.filename, (fd, err) => { + if (err) { + callback(err); + } else { + this.fd = fd; + callback(); + } + }); + } + _read(n) { + const buf = Buffer.alloc(n); + fs.read(this.fd, buf, 0, n, null, (err, bytesRead) => { + if (err) { + this.destroy(err); + } else { + this.push(bytesRead > 0 ? buf.slice(0, bytesRead) : null); + } + }); + } + _destroy(err, callback) { + if (this.fd) { + fs.close(this.fd, (er) => callback(er || err)); + } else { + callback(err); + } + } +} +``` + #### `readable._read(size)` + +* {string} The request method. +``` For contributing C++ code, you may want to look at the [C++ Style Guide](../cpp-style-guide.md), as well as the diff --git a/doc/guides/maintaining-icu.md b/doc/guides/maintaining-icu.md index f83a435b847c90..4b0c413c3f9fcb 100644 --- a/doc/guides/maintaining-icu.md +++ b/doc/guides/maintaining-icu.md @@ -83,7 +83,7 @@ V8 will not compile. ```c // deps/v8/src/objects/intl-objects.h -#define V8_MINIMUM_ICU_VERSION 64 +#define V8_MINIMUM_ICU_VERSION 65 ``` V8 in Node.js depends on the ICU version supplied by Node.js. @@ -104,7 +104,7 @@ should be sufficient). ```bash ./configure \ --with-intl=full-icu \ - --with-icu-source=http://download.icu-project.org/files/icu4c/58.1/icu4c-58_1-src.tgz + --with-icu-source=https://github.com/unicode-org/icu/releases/download/release-67-1/icu4c-67_1-src.tgz make ``` diff --git a/lib/fs.js b/lib/fs.js index 67a050b4e8c480..e118b065be2290 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -28,6 +28,10 @@ // See https://github.com/libuv/libuv/pull/1501. const kIoMaxLength = 2 ** 31 - 1; +// When using FSReqCallback, make sure to create the object only *after* all +// parameter validation has happened, so that the objects are not kept in memory +// in case they are created but never used due to an exception. + const { Map, MathMax, @@ -198,8 +202,10 @@ function access(path, mode, callback) { path = getValidatedPath(path); mode = getValidMode(mode, 'access'); + callback = makeCallback(callback); + const req = new FSReqCallback(); - req.oncomplete = makeCallback(callback); + req.oncomplete = callback; binding.access(pathModule.toNamespacedPath(path), mode, req); } @@ -307,19 +313,19 @@ function readFile(path, options, callback) { const context = new ReadFileContext(callback, options.encoding); context.isUserFd = isFd(path); // File descriptor ownership - const req = new FSReqCallback(); - req.context = context; - req.oncomplete = readFileAfterOpen; - if (context.isUserFd) { - process.nextTick(function tick() { - req.oncomplete(null, path); - }); + process.nextTick(function tick(context) { + readFileAfterOpen.call({ context }, null, path); + }, context); return; } - path = getValidatedPath(path); const flagsNumber = stringToFlags(options.flags); + path = getValidatedPath(path); + + const req = new FSReqCallback(); + req.context = context; + req.oncomplete = readFileAfterOpen; binding.open(pathModule.toNamespacedPath(path), flagsNumber, 0o666, @@ -416,8 +422,10 @@ function readFileSync(path, options) { function close(fd, callback) { validateInt32(fd, 'fd', 0); + callback = makeCallback(callback); + const req = new FSReqCallback(); - req.oncomplete = makeCallback(callback); + req.oncomplete = callback; binding.close(fd, req); } @@ -590,12 +598,11 @@ function readv(fd, buffers, position, callback) { validateInt32(fd, 'fd', /* min */ 0); validateBufferArray(buffers); + callback = maybeCallback(callback || position); const req = new FSReqCallback(); req.oncomplete = wrapper; - callback = maybeCallback(callback || position); - if (typeof position !== 'number') position = null; @@ -712,12 +719,11 @@ function writev(fd, buffers, position, callback) { validateInt32(fd, 'fd', 0); validateBufferArray(buffers); + callback = maybeCallback(callback || position); const req = new FSReqCallback(); req.oncomplete = wrapper; - callback = maybeCallback(callback || position); - if (typeof position !== 'number') position = null; @@ -819,8 +825,10 @@ function ftruncate(fd, len = 0, callback) { validateInt32(fd, 'fd', 0); validateInteger(len, 'len'); len = MathMax(0, len); + callback = makeCallback(callback); + const req = new FSReqCallback(); - req.oncomplete = makeCallback(callback); + req.oncomplete = callback; binding.ftruncate(fd, len, req); } @@ -989,8 +997,10 @@ function fstat(fd, options = { bigint: false }, callback) { options = {}; } validateInt32(fd, 'fd', 0); + callback = makeStatsCallback(callback); + const req = new FSReqCallback(options.bigint); - req.oncomplete = makeStatsCallback(callback); + req.oncomplete = callback; binding.fstat(fd, options.bigint, req); } @@ -1001,6 +1011,7 @@ function lstat(path, options = { bigint: false }, callback) { } callback = makeStatsCallback(callback); path = getValidatedPath(path); + const req = new FSReqCallback(options.bigint); req.oncomplete = callback; binding.lstat(pathModule.toNamespacedPath(path), options.bigint, req); @@ -1013,6 +1024,7 @@ function stat(path, options = { bigint: false }, callback) { } callback = makeStatsCallback(callback); path = getValidatedPath(path); + const req = new FSReqCallback(options.bigint); req.oncomplete = callback; binding.stat(pathModule.toNamespacedPath(path), options.bigint, req); @@ -1070,9 +1082,6 @@ function symlink(target, path, type_, callback_) { target = getValidatedPath(target, 'target'); path = getValidatedPath(path); - const req = new FSReqCallback(); - req.oncomplete = callback; - if (isWindows && type === null) { let absoluteTarget; try { @@ -1087,18 +1096,25 @@ function symlink(target, path, type_, callback_) { stat(absoluteTarget, (err, stat) => { const resolvedType = !err && stat.isDirectory() ? 'dir' : 'file'; const resolvedFlags = stringToSymlinkType(resolvedType); - binding.symlink(preprocessSymlinkDestination(target, - resolvedType, - path), + const destination = preprocessSymlinkDestination(target, + resolvedType, + path); + + const req = new FSReqCallback(); + req.oncomplete = callback; + binding.symlink(destination, pathModule.toNamespacedPath(path), resolvedFlags, req); }); return; } } + const destination = preprocessSymlinkDestination(target, type, path); + const flags = stringToSymlinkType(type); - binding.symlink(preprocessSymlinkDestination(target, type, path), - pathModule.toNamespacedPath(path), flags, req); + const req = new FSReqCallback(); + req.oncomplete = callback; + binding.symlink(destination, pathModule.toNamespacedPath(path), flags, req); } function symlinkSync(target, path, type) { @@ -1255,9 +1271,10 @@ function fchown(fd, uid, gid, callback) { validateInt32(fd, 'fd', 0); validateInteger(uid, 'uid', -1, kMaxUserId); validateInteger(gid, 'gid', -1, kMaxUserId); + callback = makeCallback(callback); const req = new FSReqCallback(); - req.oncomplete = makeCallback(callback); + req.oncomplete = callback; binding.fchown(fd, uid, gid, req); } @@ -1316,8 +1333,10 @@ function futimes(fd, atime, mtime, callback) { validateInt32(fd, 'fd', 0); atime = toUnixTimestamp(atime, 'atime'); mtime = toUnixTimestamp(mtime, 'mtime'); + callback = makeCallback(callback); + const req = new FSReqCallback(); - req.oncomplete = makeCallback(callback); + req.oncomplete = callback; binding.futimes(fd, atime, mtime, req); } @@ -1598,10 +1617,7 @@ if (isWindows) { const emptyObj = ObjectCreate(null); function realpathSync(p, options) { - if (!options) - options = emptyObj; - else - options = getOptions(options, emptyObj); + options = getOptions(options, emptyObj); p = toPathIfFileURL(p); if (typeof p !== 'string') { p += ''; @@ -1633,7 +1649,7 @@ function realpathSync(p, options) { pos = current.length; // On windows, check that the root exists. On unix there is no need. - if (isWindows && !knownHard[base]) { + if (isWindows) { const ctx = { path: base }; binding.lstat(pathModule.toNamespacedPath(base), false, undefined, ctx); handleErrorFromBinding(ctx); @@ -1831,7 +1847,7 @@ function realpath(p, options, callback) { const ino = stats.ino.toString(32); id = `${dev}:${ino}`; if (seenLinks[id]) { - return gotTarget(null, seenLinks[id], base); + return gotTarget(null, seenLinks[id]); } } fs.stat(base, (err) => { @@ -1844,7 +1860,7 @@ function realpath(p, options, callback) { }); } - function gotTarget(err, target, base) { + function gotTarget(err, target) { if (err) return callback(err); gotResolvedLink(pathModule.resolve(previous, target)); @@ -1923,8 +1939,10 @@ function copyFile(src, dest, mode, callback) { src = pathModule._makeLong(src); dest = pathModule._makeLong(dest); mode = getValidMode(mode, 'copyFile'); + callback = makeCallback(callback); + const req = new FSReqCallback(); - req.oncomplete = makeCallback(callback); + req.oncomplete = callback; binding.copyFile(src, dest, mode, req); } diff --git a/lib/internal/console/constructor.js b/lib/internal/console/constructor.js index dfad0811b25201..aae511cb6c7ca0 100644 --- a/lib/internal/console/constructor.js +++ b/lib/internal/console/constructor.js @@ -19,6 +19,7 @@ const { ReflectOwnKeys, Symbol, SymbolHasInstance, + SymbolToStringTag, WeakMap, } = primordials; @@ -233,6 +234,12 @@ ObjectDefineProperties(Console.prototype, { ...consolePropAttributes, value: groupIndentation }, + [SymbolToStringTag]: { + writable: false, + enumerable: false, + configurable: true, + value: 'console' + } }); } }, diff --git a/lib/internal/fs/promises.js b/lib/internal/fs/promises.js index 60a28735a982ec..b6af3e48bd5c9f 100644 --- a/lib/internal/fs/promises.js +++ b/lib/internal/fs/promises.js @@ -15,6 +15,7 @@ const { MathMin, NumberIsSafeInteger, Promise, + PromiseResolve, Symbol, Uint8Array, } = primordials; @@ -155,7 +156,7 @@ class FileHandle extends JSTransferable { close = () => { if (this[kFd] === -1) { - return Promise.resolve(); + return PromiseResolve(); } if (this[kClosePromise]) { diff --git a/lib/internal/fs/read_file_context.js b/lib/internal/fs/read_file_context.js index d7b0e368006f05..5091a34231665b 100644 --- a/lib/internal/fs/read_file_context.js +++ b/lib/internal/fs/read_file_context.js @@ -100,18 +100,18 @@ class ReadFileContext { } close(err) { + if (this.isUserFd) { + process.nextTick(function tick(context) { + readFileAfterClose.call({ context }, null); + }, this); + return; + } + const req = new FSReqCallback(); req.oncomplete = readFileAfterClose; req.context = this; this.err = err; - if (this.isUserFd) { - process.nextTick(function tick() { - req.oncomplete(null); - }); - return; - } - close(this.fd, req); } } diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index 17ee0324225393..223c6b21e39f48 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -70,7 +70,6 @@ const fs = require('fs'); const internalFS = require('internal/fs/utils'); const path = require('path'); const { sep } = path; -const { emitWarningSync } = require('internal/process/warning'); const { internalModuleStat } = internalBinding('fs'); const packageJsonReader = require('internal/modules/package_json_reader'); const { safeGetenv } = internalBinding('credentials'); @@ -115,6 +114,7 @@ const { } = require('internal/util/types'); const asyncESM = require('internal/process/esm_loader'); +const { enrichCJSError } = require('internal/modules/esm/translators'); const { kEvaluated } = internalBinding('module_wrap'); const { encodedSepRegEx, @@ -129,28 +129,6 @@ const relativeResolveCache = ObjectCreate(null); let requireDepth = 0; let statCache = null; -function enrichCJSError(err) { - const stack = err.stack.split('\n'); - - const lineWithErr = stack[1]; - - /* - The regular expression below targets the most common import statement - usage. However, some cases are not matching, cases like import statement - after a comment block and/or after a variable definition. - */ - if (err.message.startsWith('Unexpected token \'export\'') || - (RegExpPrototypeTest(/^\s*import(?=[ {'"*])\s*(?![ (])/, lineWithErr))) { - // Emit the warning synchronously because we are in the middle of handling - // a SyntaxError that will throw and likely terminate the process before an - // asynchronous warning would be emitted. - emitWarningSync( - 'To load an ES module, set "type": "module" in the package.json or use ' + - 'the .mjs extension.' - ); - } -} - function stat(filename) { filename = path.toNamespacedPath(filename); if (statCache !== null) { diff --git a/lib/internal/modules/esm/get_format.js b/lib/internal/modules/esm/get_format.js index 616b2cf52309ea..16e2ad5e2d5c3e 100644 --- a/lib/internal/modules/esm/get_format.js +++ b/lib/internal/modules/esm/get_format.js @@ -34,7 +34,7 @@ if (experimentalJsonModules) extensionFormatMap['.json'] = legacyExtensionFormatMap['.json'] = 'json'; function defaultGetFormat(url, context, defaultGetFormatUnused) { - if (StringPrototypeStartsWith(url, 'nodejs:')) { + if (StringPrototypeStartsWith(url, 'node:')) { return { format: 'builtin' }; } const parsed = new URL(url); diff --git a/lib/internal/modules/esm/resolve.js b/lib/internal/modules/esm/resolve.js index 12a9b14a577ce9..21459cf3d1e753 100644 --- a/lib/internal/modules/esm/resolve.js +++ b/lib/internal/modules/esm/resolve.js @@ -776,13 +776,13 @@ function defaultResolve(specifier, context = {}, defaultResolveUnused) { }; } } catch {} - if (parsed && parsed.protocol === 'nodejs:') + if (parsed && parsed.protocol === 'node:') return { url: specifier }; if (parsed && parsed.protocol !== 'file:' && parsed.protocol !== 'data:') throw new ERR_UNSUPPORTED_ESM_URL_SCHEME(parsed); if (NativeModule.canBeRequiredByUsers(specifier)) { return { - url: 'nodejs:' + specifier + url: 'node:' + specifier }; } if (parentURL && StringPrototypeStartsWith(parentURL, 'data:')) { diff --git a/lib/internal/modules/esm/translators.js b/lib/internal/modules/esm/translators.js index 5e4de5d5af0f39..f9a76d599e880a 100644 --- a/lib/internal/modules/esm/translators.js +++ b/lib/internal/modules/esm/translators.js @@ -9,9 +9,12 @@ const { ObjectKeys, PromisePrototypeCatch, PromiseReject, + RegExpPrototypeTest, SafeMap, SafeSet, StringPrototypeReplace, + StringPrototypeSplit, + StringPrototypeStartsWith, } = primordials; let _TYPES = null; @@ -21,7 +24,7 @@ function lazyTypes() { } const { readFileSync } = require('fs'); -const { extname } = require('path'); +const { extname, isAbsolute } = require('path'); const { stripBOM, loadNativeModule @@ -54,9 +57,11 @@ const experimentalImportMetaResolve = getOptionValue('--experimental-import-meta-resolve'); const asyncESM = require('internal/process/esm_loader'); const cjsParse = require('internal/deps/cjs-module-lexer/lexer'); +const { emitWarningSync } = require('internal/process/warning'); const translators = new SafeMap(); exports.translators = translators; +exports.enrichCJSError = enrichCJSError; let DECODER = null; function assertBufferSource(body, allowString, hookName) { @@ -130,6 +135,25 @@ translators.set('module', async function moduleStrategy(url) { return module; }); +function enrichCJSError(err) { + const stack = StringPrototypeSplit(err.stack, '\n'); + /* + * The regular expression below targets the most common import statement + * usage. However, some cases are not matching, cases like import statement + * after a comment block and/or after a variable definition. + */ + if (StringPrototypeStartsWith(err.message, 'Unexpected token \'export\'') || + RegExpPrototypeTest(/^\s*import(?=[ {'"*])\s*(?![ (])/, stack[1])) { + // Emit the warning synchronously because we are in the middle of handling + // a SyntaxError that will throw and likely terminate the process before an + // asynchronous warning would be emitted. + emitWarningSync( + 'To load an ES module, set "type": "module" in the package.json or use ' + + 'the .mjs extension.' + ); + } +} + // Strategy for loading a node-style CommonJS module const isWindows = process.platform === 'win32'; const winSepRegEx = /\//g; @@ -152,7 +176,12 @@ translators.set('commonjs', async function commonjsStrategy(url, isMain) { exports = asyncESM.ESMLoader.cjsCache.get(module); asyncESM.ESMLoader.cjsCache.delete(module); } else { - exports = CJSModule._load(filename, undefined, isMain); + try { + exports = CJSModule._load(filename, undefined, isMain); + } catch (err) { + enrichCJSError(err); + throw err; + } } for (const exportName of exportNames) { @@ -190,7 +219,13 @@ function cjsPreparseModuleExports(filename) { source = readFileSync(filename, 'utf8'); } catch {} - const { exports, reexports } = cjsParse(source || ''); + let exports, reexports; + try { + ({ exports, reexports } = cjsParse(source || '')); + } catch { + exports = []; + reexports = []; + } const exportNames = new SafeSet(exports); @@ -209,7 +244,8 @@ function cjsPreparseModuleExports(filename) { continue; } const ext = extname(resolved); - if (ext === '.js' || ext === '.cjs' || !CJSModule._extensions[ext]) { + if ((ext === '.js' || ext === '.cjs' || !CJSModule._extensions[ext]) && + isAbsolute(resolved)) { const { exportNames: reexportNames } = cjsPreparseModuleExports(resolved); for (const name of reexportNames) exportNames.add(name); @@ -223,11 +259,11 @@ function cjsPreparseModuleExports(filename) { // through normal resolution translators.set('builtin', async function builtinStrategy(url) { debug(`Translating BuiltinModule ${url}`); - // Slice 'nodejs:' scheme - const id = url.slice(7); + // Slice 'node:' scheme + const id = url.slice(5); const module = loadNativeModule(id, url, true); - if (!url.startsWith('nodejs:') || !module) { - throw new ERR_UNKNOWN_BUILTIN_MODULE(id); + if (!url.startsWith('node:') || !module) { + throw new ERR_UNKNOWN_BUILTIN_MODULE(url); } debug(`Loading BuiltinModule ${url}`); return module.getESMFacade(); diff --git a/src/api/callback.cc b/src/api/callback.cc index 84664c089594eb..3d4f91a866ea39 100644 --- a/src/api/callback.cc +++ b/src/api/callback.cc @@ -223,10 +223,19 @@ MaybeLocal MakeCallback(Isolate* isolate, int argc, Local argv[], async_context asyncContext) { - Local callback_v = - recv->Get(isolate->GetCurrentContext(), symbol).ToLocalChecked(); - if (callback_v.IsEmpty()) return Local(); - if (!callback_v->IsFunction()) return Local(); + // Check can_call_into_js() first because calling Get() might do so. + Environment* env = Environment::GetCurrent(recv->CreationContext()); + CHECK_NOT_NULL(env); + if (!env->can_call_into_js()) return Local(); + + Local callback_v; + if (!recv->Get(isolate->GetCurrentContext(), symbol).ToLocal(&callback_v)) + return Local(); + if (!callback_v->IsFunction()) { + // This used to return an empty value, but Undefined() makes more sense + // since no exception is pending here. + return Undefined(isolate); + } Local callback = callback_v.As(); return MakeCallback(isolate, recv, callback, argc, argv, asyncContext); } diff --git a/src/api/hooks.cc b/src/api/hooks.cc index e1536193f95730..a719a861dbe9d8 100644 --- a/src/api/hooks.cc +++ b/src/api/hooks.cc @@ -45,7 +45,9 @@ void EmitBeforeExit(Environment* env) { Local exit_code; if (!exit_code_v->ToInteger(env->context()).ToLocal(&exit_code)) return; - ProcessEmit(env, "beforeExit", exit_code).ToLocalChecked(); + // TODO(addaleax): Provide variants of EmitExit() and EmitBeforeExit() that + // actually forward empty MaybeLocal<>s (and check env->can_call_into_js()). + USE(ProcessEmit(env, "beforeExit", exit_code)); } int EmitExit(Environment* env) { diff --git a/src/node_crypto.cc b/src/node_crypto.cc index 05da3af09c63dc..ed886abd749661 100644 --- a/src/node_crypto.cc +++ b/src/node_crypto.cc @@ -3265,8 +3265,11 @@ size_t KeyObjectData::GetSymmetricKeySize() const { return symmetric_key_len_; } -Local KeyObjectHandle::Initialize(Environment* env, - Local target) { +Local KeyObjectHandle::Initialize(Environment* env) { + Local templ = env->crypto_key_object_handle_constructor(); + if (!templ.IsEmpty()) { + return templ; + } Local t = env->NewFunctionTemplate(New); t->InstanceTemplate()->SetInternalFieldCount( KeyObjectHandle::kInternalFieldCount); @@ -3280,20 +3283,16 @@ Local KeyObjectHandle::Initialize(Environment* env, env->SetProtoMethod(t, "export", Export); auto function = t->GetFunction(env->context()).ToLocalChecked(); - target->Set(env->context(), - FIXED_ONE_BYTE_STRING(env->isolate(), "KeyObjectHandle"), - function).Check(); - - return function; + env->set_crypto_key_object_handle_constructor(function); + return KeyObjectHandle::Initialize(env); } MaybeLocal KeyObjectHandle::Create( Environment* env, std::shared_ptr data) { Local obj; - if (!env->crypto_key_object_handle_constructor() - ->NewInstance(env->context(), 0, nullptr) - .ToLocal(&obj)) { + Local fctun = KeyObjectHandle::Initialize(env); + if (!fctun->NewInstance(env->context(), 0, nullptr).ToLocal(&obj)) { return MaybeLocal(); } @@ -3466,6 +3465,11 @@ BaseObjectPtr NativeKeyObject::KeyObjectTransferData::Deserialize( Local handle = KeyObjectHandle::Create(env, data_).ToLocalChecked(); Local key_ctor; + Local arg = FIXED_ONE_BYTE_STRING(env->isolate(), + "internal/crypto/keys"); + if (env->native_module_require()-> + Call(context, Null(env->isolate()), 1, &arg).IsEmpty()) + return {}; switch (data_->GetKeyType()) { case kKeyTypeSecret: key_ctor = env->crypto_key_object_secret_constructor(); @@ -6950,8 +6954,9 @@ void Initialize(Local target, Environment* env = Environment::GetCurrent(context); SecureContext::Initialize(env, target); - env->set_crypto_key_object_handle_constructor( - KeyObjectHandle::Initialize(env, target)); + target->Set(env->context(), + FIXED_ONE_BYTE_STRING(env->isolate(), "KeyObjectHandle"), + KeyObjectHandle::Initialize(env)).Check(); env->SetMethod(target, "createNativeKeyObjectClass", CreateNativeKeyObjectClass); CipherBase::Initialize(env, target); diff --git a/src/node_crypto.h b/src/node_crypto.h index 63468e4f18dad1..bef98b3e244d9e 100644 --- a/src/node_crypto.h +++ b/src/node_crypto.h @@ -447,8 +447,7 @@ class KeyObjectData { class KeyObjectHandle : public BaseObject { public: - static v8::Local Initialize(Environment* env, - v8::Local target); + static v8::Local Initialize(Environment* env); static v8::MaybeLocal Create(Environment* env, std::shared_ptr data); @@ -520,7 +519,9 @@ class NativeKeyObject : public BaseObject { v8::Local wrap, const std::shared_ptr& handle_data) : BaseObject(env, wrap), - handle_data_(handle_data) {} + handle_data_(handle_data) { + MakeWeak(); + } std::shared_ptr handle_data_; }; diff --git a/src/node_http2.cc b/src/node_http2.cc index b5b38fc976158b..f4f8eeb17c6ebf 100644 --- a/src/node_http2.cc +++ b/src/node_http2.cc @@ -1518,12 +1518,12 @@ void Http2Session::ClearOutgoing(int status) { std::vector current_outgoing_buffers_; current_outgoing_buffers_.swap(outgoing_buffers_); for (const NgHttp2StreamWrite& wr : current_outgoing_buffers_) { - WriteWrap* wrap = wr.req_wrap; - if (wrap != nullptr) { + BaseObjectPtr wrap = std::move(wr.req_wrap); + if (wrap) { // TODO(addaleax): Pass `status` instead of 0, so that we actually error // out with the error from the write to the underlying protocol, // if one occurred. - wrap->Done(0); + WriteWrap::FromObject(wrap)->Done(0); } } } @@ -1806,7 +1806,7 @@ void Http2Session::OnStreamRead(ssize_t nread, const uv_buf_t& buf_) { bool Http2Session::HasWritesOnSocketForStream(Http2Stream* stream) { for (const NgHttp2StreamWrite& wr : outgoing_buffers_) { - if (wr.req_wrap != nullptr && wr.req_wrap->stream() == stream) + if (wr.req_wrap && WriteWrap::FromObject(wr.req_wrap)->stream() == stream) return true; } return false; @@ -1959,8 +1959,8 @@ void Http2Stream::Destroy() { // we still have queued outbound writes. while (!queue_.empty()) { NgHttp2StreamWrite& head = queue_.front(); - if (head.req_wrap != nullptr) - head.req_wrap->Done(UV_ECANCELED); + if (head.req_wrap) + WriteWrap::FromObject(head.req_wrap)->Done(UV_ECANCELED); queue_.pop(); } @@ -2189,7 +2189,8 @@ int Http2Stream::DoWrite(WriteWrap* req_wrap, // Store the req_wrap on the last write info in the queue, so that it is // only marked as finished once all buffers associated with it are finished. queue_.emplace(NgHttp2StreamWrite { - i == nbufs - 1 ? req_wrap : nullptr, + BaseObjectPtr( + i == nbufs - 1 ? req_wrap->GetAsyncWrap() : nullptr), bufs[i] }); IncrementAvailableOutboundLength(bufs[i].len); @@ -2283,10 +2284,11 @@ ssize_t Http2Stream::Provider::Stream::OnRead(nghttp2_session* handle, // find out when the HTTP2 stream wants to consume data, and because the // StreamBase API allows empty input chunks. while (!stream->queue_.empty() && stream->queue_.front().buf.len == 0) { - WriteWrap* finished = stream->queue_.front().req_wrap; + BaseObjectPtr finished = + std::move(stream->queue_.front().req_wrap); stream->queue_.pop(); - if (finished != nullptr) - finished->Done(0); + if (finished) + WriteWrap::FromObject(finished)->Done(0); } if (!stream->queue_.empty()) { @@ -2912,8 +2914,8 @@ void Http2Ping::DetachFromSession() { } void NgHttp2StreamWrite::MemoryInfo(MemoryTracker* tracker) const { - if (req_wrap != nullptr) - tracker->TrackField("req_wrap", req_wrap->GetAsyncWrap()); + if (req_wrap) + tracker->TrackField("req_wrap", req_wrap); tracker->TrackField("buf", buf); } diff --git a/src/node_http2.h b/src/node_http2.h index 4ef9e9fbab0c43..e49c3a60f32bcc 100644 --- a/src/node_http2.h +++ b/src/node_http2.h @@ -145,12 +145,12 @@ using Http2Headers = NgHeaders; using Http2RcBufferPointer = NgRcBufPointer; struct NgHttp2StreamWrite : public MemoryRetainer { - WriteWrap* req_wrap = nullptr; + BaseObjectPtr req_wrap; uv_buf_t buf; inline explicit NgHttp2StreamWrite(uv_buf_t buf_) : buf(buf_) {} - inline NgHttp2StreamWrite(WriteWrap* req, uv_buf_t buf_) : - req_wrap(req), buf(buf_) {} + inline NgHttp2StreamWrite(BaseObjectPtr req_wrap, uv_buf_t buf_) : + req_wrap(std::move(req_wrap)), buf(buf_) {} void MemoryInfo(MemoryTracker* tracker) const override; SET_MEMORY_INFO_NAME(NgHttp2StreamWrite) diff --git a/src/node_perf_common.h b/src/node_perf_common.h index 75d266afc257e9..ba396740c27de5 100644 --- a/src/node_perf_common.h +++ b/src/node_perf_common.h @@ -3,6 +3,7 @@ #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS +#include "aliased_buffer.h" #include "node.h" #include "uv.h" #include "v8.h" diff --git a/src/node_platform.cc b/src/node_platform.cc index 4bb9b919f60a91..7e68b7af891ffb 100644 --- a/src/node_platform.cc +++ b/src/node_platform.cc @@ -409,6 +409,9 @@ void PerIsolatePlatformData::RunForegroundTask(std::unique_ptr task) { InternalCallbackScope::kNoFlags); task->Run(); } else { + // The task is moved out of InternalCallbackScope if env is not available. + // This is a required else block, and should not be removed. + // See comment: https://github.com/nodejs/node/pull/34688#pullrequestreview-463867489 task->Run(); } } diff --git a/src/node_version.h b/src/node_version.h index a56222f35d9015..19fa04ebe755ec 100644 --- a/src/node_version.h +++ b/src/node_version.h @@ -29,7 +29,7 @@ #define NODE_VERSION_IS_LTS 0 #define NODE_VERSION_LTS_CODENAME "" -#define NODE_VERSION_IS_RELEASE 0 +#define NODE_VERSION_IS_RELEASE 1 #ifndef NODE_STRINGIFY #define NODE_STRINGIFY(n) NODE_STRINGIFY_HELPER(n) diff --git a/src/node_wasi.cc b/src/node_wasi.cc index 80182724afea9c..4dd534af4167ee 100644 --- a/src/node_wasi.cc +++ b/src/node_wasi.cc @@ -104,9 +104,9 @@ static MaybeLocal WASIException(Local context, js_msg = String::Concat(isolate, js_msg, FIXED_ONE_BYTE_STRING(isolate, ", ")); js_msg = String::Concat(isolate, js_msg, js_syscall); - Local e = - Exception::Error(js_msg)->ToObject(context) - .ToLocalChecked(); + Local e; + if (!Exception::Error(js_msg)->ToObject(context).ToLocal(&e)) + return MaybeLocal(); if (e->Set(context, env->errno_string(), @@ -128,13 +128,11 @@ WASI::WASI(Environment* env, options->allocator = &alloc_info_; int err = uvwasi_init(&uvw_, options); if (err != UVWASI_ESUCCESS) { - Local context = env->context(); - MaybeLocal exception = WASIException(context, err, "uvwasi_init"); - - if (exception.IsEmpty()) + Local exception; + if (!WASIException(env->context(), err, "uvwasi_init").ToLocal(&exception)) return; - context->GetIsolate()->ThrowException(exception.ToLocalChecked()); + env->isolate()->ThrowException(exception); } } diff --git a/src/pipe_wrap.cc b/src/pipe_wrap.cc index 9e6831b2ed3b04..1396395463dfed 100644 --- a/src/pipe_wrap.cc +++ b/src/pipe_wrap.cc @@ -40,7 +40,6 @@ using v8::Function; using v8::FunctionCallbackInfo; using v8::FunctionTemplate; using v8::Int32; -using v8::Isolate; using v8::Local; using v8::MaybeLocal; using v8::Object; @@ -216,9 +215,8 @@ void PipeWrap::Open(const FunctionCallbackInfo& args) { int err = uv_pipe_open(&wrap->handle_, fd); wrap->set_fd(fd); - Isolate* isolate = env->isolate(); if (err != 0) - isolate->ThrowException(UVException(isolate, err, "uv_pipe_open")); + env->ThrowUVException(err, "uv_pipe_open"); } diff --git a/src/stream_base-inl.h b/src/stream_base-inl.h index bd7224e9c0245e..c003ffc1ef6595 100644 --- a/src/stream_base-inl.h +++ b/src/stream_base-inl.h @@ -243,6 +243,28 @@ StreamBase* StreamBase::FromObject(v8::Local obj) { StreamBase::kStreamBaseField)); } +WriteWrap* WriteWrap::FromObject(v8::Local req_wrap_obj) { + return static_cast(StreamReq::FromObject(req_wrap_obj)); +} + +template +WriteWrap* WriteWrap::FromObject( + const BaseObjectPtrImpl& base_obj) { + if (!base_obj) return nullptr; + return FromObject(base_obj->object()); +} + +ShutdownWrap* ShutdownWrap::FromObject(v8::Local req_wrap_obj) { + return static_cast(StreamReq::FromObject(req_wrap_obj)); +} + +template +ShutdownWrap* ShutdownWrap::FromObject( + const BaseObjectPtrImpl& base_obj) { + if (!base_obj) return nullptr; + return FromObject(base_obj->object()); +} + void WriteWrap::SetAllocatedStorage(AllocatedBuffer&& storage) { CHECK_NULL(storage_.data()); storage_ = std::move(storage); diff --git a/src/stream_base.cc b/src/stream_base.cc index b35df39afe9d8c..3ad201746007dc 100644 --- a/src/stream_base.cc +++ b/src/stream_base.cc @@ -621,12 +621,16 @@ StreamResource::~StreamResource() { ShutdownWrap* StreamBase::CreateShutdownWrap( Local object) { - return new SimpleShutdownWrap(this, object); + auto* wrap = new SimpleShutdownWrap(this, object); + wrap->MakeWeak(); + return wrap; } WriteWrap* StreamBase::CreateWriteWrap( Local object) { - return new SimpleWriteWrap(this, object); + auto* wrap = new SimpleWriteWrap(this, object); + wrap->MakeWeak(); + return wrap; } } // namespace node diff --git a/src/stream_base.h b/src/stream_base.h index 7c6bcba81edd03..72142309fe1902 100644 --- a/src/stream_base.h +++ b/src/stream_base.h @@ -77,6 +77,11 @@ class ShutdownWrap : public StreamReq { StreamBase* stream, v8::Local req_wrap_obj); + static inline ShutdownWrap* FromObject(v8::Local req_wrap_obj); + template + static inline ShutdownWrap* FromObject( + const BaseObjectPtrImpl& base_obj); + // Call stream()->EmitAfterShutdown() and dispose of this request wrap. void OnDone(int status) override; }; @@ -89,6 +94,11 @@ class WriteWrap : public StreamReq { StreamBase* stream, v8::Local req_wrap_obj); + static inline WriteWrap* FromObject(v8::Local req_wrap_obj); + template + static inline WriteWrap* FromObject( + const BaseObjectPtrImpl& base_obj); + // Call stream()->EmitAfterWrite() and dispose of this request wrap. void OnDone(int status) override; diff --git a/src/tls_wrap.cc b/src/tls_wrap.cc index 91faeafb62b660..85591902d5fabd 100644 --- a/src/tls_wrap.cc +++ b/src/tls_wrap.cc @@ -95,9 +95,10 @@ bool TLSWrap::InvokeQueued(int status, const char* error_str) { if (!write_callback_scheduled_) return false; - if (current_write_ != nullptr) { - WriteWrap* w = current_write_; - current_write_ = nullptr; + if (current_write_) { + BaseObjectPtr current_write = std::move(current_write_); + current_write_.reset(); + WriteWrap* w = WriteWrap::FromObject(current_write); w->Done(status, error_str); } @@ -301,7 +302,7 @@ void TLSWrap::EncOut() { } // Split-off queue - if (established_ && current_write_ != nullptr) { + if (established_ && current_write_) { Debug(this, "EncOut() setting write_callback_scheduled_"); write_callback_scheduled_ = true; } @@ -372,10 +373,12 @@ void TLSWrap::EncOut() { void TLSWrap::OnStreamAfterWrite(WriteWrap* req_wrap, int status) { Debug(this, "OnStreamAfterWrite(status = %d)", status); - if (current_empty_write_ != nullptr) { + if (current_empty_write_) { Debug(this, "Had empty write"); - WriteWrap* finishing = current_empty_write_; - current_empty_write_ = nullptr; + BaseObjectPtr current_empty_write = + std::move(current_empty_write_); + current_empty_write_.reset(); + WriteWrap* finishing = WriteWrap::FromObject(current_empty_write); finishing->Done(status); return; } @@ -735,14 +738,14 @@ int TLSWrap::DoWrite(WriteWrap* w, ClearOut(); if (BIO_pending(enc_out_) == 0) { Debug(this, "No pending encrypted output, writing to underlying stream"); - CHECK_NULL(current_empty_write_); - current_empty_write_ = w; + CHECK(!current_empty_write_); + current_empty_write_.reset(w->GetAsyncWrap()); StreamWriteResult res = underlying_stream()->Write(bufs, count, send_handle); if (!res.async) { BaseObjectPtr strong_ref{this}; env()->SetImmediate([this, strong_ref](Environment* env) { - OnStreamAfterWrite(current_empty_write_, 0); + OnStreamAfterWrite(WriteWrap::FromObject(current_empty_write_), 0); }); } return 0; @@ -750,8 +753,8 @@ int TLSWrap::DoWrite(WriteWrap* w, } // Store the current write wrap - CHECK_NULL(current_write_); - current_write_ = w; + CHECK(!current_write_); + current_write_.reset(w->GetAsyncWrap()); // Write encrypted data to underlying stream and call Done(). if (length == 0) { @@ -804,7 +807,7 @@ int TLSWrap::DoWrite(WriteWrap* w, // If we stopped writing because of an error, it's fatal, discard the data. if (!arg.IsEmpty()) { Debug(this, "Got SSL error (%d), returning UV_EPROTO", err); - current_write_ = nullptr; + current_write_.reset(); return UV_EPROTO; } diff --git a/src/tls_wrap.h b/src/tls_wrap.h index 7b8e50de9d4689..579f53cf9a391a 100644 --- a/src/tls_wrap.h +++ b/src/tls_wrap.h @@ -194,9 +194,9 @@ class TLSWrap : public AsyncWrap, // Waiting for ClearIn() to pass to SSL_write(). AllocatedBuffer pending_cleartext_input_; size_t write_size_ = 0; - WriteWrap* current_write_ = nullptr; + BaseObjectPtr current_write_; bool in_dowrite_ = false; - WriteWrap* current_empty_write_ = nullptr; + BaseObjectPtr current_empty_write_; bool write_callback_scheduled_ = false; bool started_ = false; bool established_ = false; diff --git a/src/util.cc b/src/util.cc index c604c4c9555b60..01e15acb0e5c09 100644 --- a/src/util.cc +++ b/src/util.cc @@ -144,7 +144,10 @@ std::string GetProcessTitle(const char* default_title) { if (rc == 0) break; - if (rc != UV_ENOBUFS) + // If uv_setup_args() was not called, `uv_get_process_title()` will always + // return `UV_ENOBUFS`, no matter the input size. Guard against a possible + // infinite loop by limiting the buffer size. + if (rc != UV_ENOBUFS || buf.size() >= 1024 * 1024) return default_title; buf.resize(2 * buf.size()); diff --git a/test/doctool/test-local-md-file-reference-regex.js b/test/doctool/test-local-md-file-reference-regex.js new file mode 100644 index 00000000000000..f0fa922d89c860 --- /dev/null +++ b/test/doctool/test-local-md-file-reference-regex.js @@ -0,0 +1,47 @@ +'use strict'; + +require('../common'); +const assert = require('assert'); + +const { referenceToLocalMdFile } = require('../../tools/doc/markdown.js'); + +{ + const shouldBeSpotted = [ + 'test.md', + 'TEST.MD', + 'test.js.md', + '.test.md', + './test.md', + 'subfolder/test.md', + '../test.md', + 'test.md#anchor', + 'subfolder/test.md#anchor', + '/test.md', + ]; + + shouldBeSpotted.forEach((url) => { + assert.match(url, referenceToLocalMdFile); + }); +} + +{ + const shouldNotBeSpotted = [ + 'https://example.com/test.md', + 'HTTPS://EXAMPLE.COM/TEST.MD', + 'git+https://example.com/test.md', + 'ftp://1.1.1.1/test.md', + 'urn:isbn:9780307476463.md', + 'file://./test.md', + '/dev/null', + 'test.html', + 'test.html#anchor.md', + 'test.html?anchor.md', + 'test.md5', + 'testmd', + '.md', + ]; + + shouldNotBeSpotted.forEach((url) => { + assert.doesNotMatch(url, referenceToLocalMdFile); + }); +} diff --git a/test/es-module/test-esm-cjs-builtins.js b/test/es-module/test-esm-cjs-builtins.js new file mode 100644 index 00000000000000..63aae732904137 --- /dev/null +++ b/test/es-module/test-esm-cjs-builtins.js @@ -0,0 +1,21 @@ +'use strict'; + +const common = require('../common'); +const fixtures = require('../common/fixtures'); +const { spawn } = require('child_process'); +const assert = require('assert'); + +const entry = fixtures.path('/es-modules/builtin-imports-case.mjs'); + +const child = spawn(process.execPath, [entry]); +child.stderr.setEncoding('utf8'); +let stdout = ''; +child.stdout.setEncoding('utf8'); +child.stdout.on('data', (data) => { + stdout += data; +}); +child.on('close', common.mustCall((code, signal) => { + assert.strictEqual(code, 0); + assert.strictEqual(signal, null); + assert.strictEqual(stdout, 'ok\n'); +})); diff --git a/test/es-module/test-esm-cjs-exports.js b/test/es-module/test-esm-cjs-exports.js index 37aa70d3880f2b..7db2c6fdb5971b 100644 --- a/test/es-module/test-esm-cjs-exports.js +++ b/test/es-module/test-esm-cjs-exports.js @@ -7,7 +7,7 @@ const assert = require('assert'); const entry = fixtures.path('/es-modules/cjs-exports.mjs'); -const child = spawn(process.execPath, [entry]); +let child = spawn(process.execPath, [entry]); child.stderr.setEncoding('utf8'); let stdout = ''; child.stdout.setEncoding('utf8'); @@ -19,3 +19,17 @@ child.on('close', common.mustCall((code, signal) => { assert.strictEqual(signal, null); assert.strictEqual(stdout, 'ok\n'); })); + +const entryInvalid = fixtures.path('/es-modules/cjs-exports-invalid.mjs'); +child = spawn(process.execPath, [entryInvalid]); +let stderr = ''; +child.stderr.setEncoding('utf8'); +child.stderr.on('data', (data) => { + stderr += data; +}); +child.on('close', common.mustCall((code, signal) => { + assert.strictEqual(code, 1); + assert.strictEqual(signal, null); + assert.ok(stderr.includes('Warning: To load an ES module')); + assert.ok(stderr.includes('Unexpected token \'export\'')); +})); diff --git a/test/es-module/test-esm-dynamic-import.js b/test/es-module/test-esm-dynamic-import.js index 0cb56dff2c04c5..6f8757da1b914e 100644 --- a/test/es-module/test-esm-dynamic-import.js +++ b/test/es-module/test-esm-dynamic-import.js @@ -48,9 +48,9 @@ function expectFsNamespace(result) { expectFsNamespace(import('fs')); expectFsNamespace(eval('import("fs")')); expectFsNamespace(eval('import("fs")')); - expectFsNamespace(import('nodejs:fs')); + expectFsNamespace(import('node:fs')); - expectModuleError(import('nodejs:unknown'), + expectModuleError(import('node:unknown'), 'ERR_UNKNOWN_BUILTIN_MODULE'); expectModuleError(import('./not-an-existing-module.mjs'), 'ERR_MODULE_NOT_FOUND'); diff --git a/test/fixtures/es-module-loaders/builtin-named-exports-loader.mjs b/test/fixtures/es-module-loaders/builtin-named-exports-loader.mjs index f476c676cdea5b..f06e9e90ff97c3 100644 --- a/test/fixtures/es-module-loaders/builtin-named-exports-loader.mjs +++ b/test/fixtures/es-module-loaders/builtin-named-exports-loader.mjs @@ -15,7 +15,7 @@ export function getGlobalPreloadCode() { export function resolve(specifier, context, defaultResolve) { const def = defaultResolve(specifier, context); - if (def.url.startsWith('nodejs:')) { + if (def.url.startsWith('node:')) { return { url: `custom-${def.url}`, }; @@ -24,7 +24,7 @@ export function resolve(specifier, context, defaultResolve) { } export function getSource(url, context, defaultGetSource) { - if (url.startsWith('custom-nodejs:')) { + if (url.startsWith('custom-node:')) { const urlObj = new URL(url); return { source: generateBuiltinModule(urlObj.pathname), @@ -35,7 +35,7 @@ export function getSource(url, context, defaultGetSource) { } export function getFormat(url, context, defaultGetFormat) { - if (url.startsWith('custom-nodejs:')) { + if (url.startsWith('custom-node:')) { return { format: 'module' }; } return defaultGetFormat(url, context, defaultGetFormat); diff --git a/test/fixtures/es-module-loaders/example-loader.mjs b/test/fixtures/es-module-loaders/example-loader.mjs index 1ed18bda51070d..be4808738035f9 100644 --- a/test/fixtures/es-module-loaders/example-loader.mjs +++ b/test/fixtures/es-module-loaders/example-loader.mjs @@ -11,7 +11,7 @@ baseURL.pathname = process.cwd() + '/'; export function resolve(specifier, { parentURL = baseURL }, defaultResolve) { if (builtinModules.includes(specifier)) { return { - url: 'nodejs:' + specifier + url: 'node:' + specifier }; } if (/^\.{1,2}[/]/.test(specifier) !== true && !specifier.startsWith('file:')) { @@ -27,7 +27,7 @@ export function resolve(specifier, { parentURL = baseURL }, defaultResolve) { } export function getFormat(url, context, defaultGetFormat) { - if (url.startsWith('nodejs:') && builtinModules.includes(url.slice(7))) { + if (url.startsWith('node:') && builtinModules.includes(url.slice(5))) { return { format: 'builtin' }; diff --git a/test/fixtures/es-module-loaders/loader-unknown-builtin-module.mjs b/test/fixtures/es-module-loaders/loader-unknown-builtin-module.mjs index e976343e47e9bc..4ef089fd6eb3fd 100644 --- a/test/fixtures/es-module-loaders/loader-unknown-builtin-module.mjs +++ b/test/fixtures/es-module-loaders/loader-unknown-builtin-module.mjs @@ -1,14 +1,14 @@ export async function resolve(specifier, { parentURL }, defaultResolve) { if (specifier === 'unknown-builtin-module') { return { - url: 'nodejs:unknown-builtin-module' + url: 'node:unknown-builtin-module' }; } return defaultResolve(specifier, {parentURL}, defaultResolve); } export async function getFormat(url, context, defaultGetFormat) { - if (url === 'nodejs:unknown-builtin-module') { + if (url === 'node:unknown-builtin-module') { return { format: 'builtin' }; diff --git a/test/fixtures/es-module-loaders/not-found-assert-loader.mjs b/test/fixtures/es-module-loaders/not-found-assert-loader.mjs index 2130bad5f52698..9a2cd735a2fd66 100644 --- a/test/fixtures/es-module-loaders/not-found-assert-loader.mjs +++ b/test/fixtures/es-module-loaders/not-found-assert-loader.mjs @@ -14,7 +14,7 @@ export async function resolve(specifier, { parentURL }, defaultResolve) { catch (e) { assert.strictEqual(e.code, 'ERR_MODULE_NOT_FOUND'); return { - url: 'nodejs:fs' + url: 'node:fs' }; } assert.fail(`Module resolution for ${specifier} should be throw ERR_MODULE_NOT_FOUND`); diff --git a/test/fixtures/es-modules/builtin-imports-case.mjs b/test/fixtures/es-modules/builtin-imports-case.mjs new file mode 100644 index 00000000000000..fb2f247c9e5ed8 --- /dev/null +++ b/test/fixtures/es-modules/builtin-imports-case.mjs @@ -0,0 +1,5 @@ +import { strictEqual } from 'assert'; +import './dep1.js'; +import { assert as depAssert } from './dep2.js'; +strictEqual(depAssert.strictEqual, strictEqual); +console.log('ok'); diff --git a/test/fixtures/es-modules/cjs-exports-invalid.mjs b/test/fixtures/es-modules/cjs-exports-invalid.mjs new file mode 100644 index 00000000000000..67428fc27c277d --- /dev/null +++ b/test/fixtures/es-modules/cjs-exports-invalid.mjs @@ -0,0 +1 @@ +import cjs from './invalid-cjs.js'; diff --git a/test/fixtures/es-modules/dep1.js b/test/fixtures/es-modules/dep1.js new file mode 100644 index 00000000000000..650a43189657ce --- /dev/null +++ b/test/fixtures/es-modules/dep1.js @@ -0,0 +1 @@ +module.exports = require('assert'); diff --git a/test/fixtures/es-modules/dep2.js b/test/fixtures/es-modules/dep2.js new file mode 100644 index 00000000000000..4d3120ab97e5c7 --- /dev/null +++ b/test/fixtures/es-modules/dep2.js @@ -0,0 +1 @@ +exports.assert = require('assert'); diff --git a/test/fixtures/es-modules/invalid-cjs.js b/test/fixtures/es-modules/invalid-cjs.js new file mode 100644 index 00000000000000..15cae6690f1362 --- /dev/null +++ b/test/fixtures/es-modules/invalid-cjs.js @@ -0,0 +1 @@ +export var name = 5; diff --git a/test/fixtures/wpt/README.md b/test/fixtures/wpt/README.md index 041e8fe1f2eec3..236661050e5bec 100644 --- a/test/fixtures/wpt/README.md +++ b/test/fixtures/wpt/README.md @@ -11,7 +11,7 @@ See [test/wpt](../../wpt/README.md) for information on how these tests are run. Last update: - console: https://github.com/web-platform-tests/wpt/tree/9786a4b131/console -- encoding: https://github.com/web-platform-tests/wpt/tree/5059d2c777/encoding +- encoding: https://github.com/web-platform-tests/wpt/tree/d7f9e16c9a/encoding - url: https://github.com/web-platform-tests/wpt/tree/418f7fabeb/url - resources: https://github.com/web-platform-tests/wpt/tree/e1fddfbf80/resources - interfaces: https://github.com/web-platform-tests/wpt/tree/8ada332aea/interfaces diff --git a/test/fixtures/wpt/encoding/idlharness.any.js b/test/fixtures/wpt/encoding/idlharness.any.js index 7a057f14e3a78c..acd100c43b461c 100644 --- a/test/fixtures/wpt/encoding/idlharness.any.js +++ b/test/fixtures/wpt/encoding/idlharness.any.js @@ -4,7 +4,7 @@ idl_test( ['encoding'], - [], // No deps + ['streams'], idl_array => { idl_array.add_objects({ TextEncoder: ['new TextEncoder()'], diff --git a/test/fixtures/wpt/versions.json b/test/fixtures/wpt/versions.json index 566041b6362862..e83603da4c8694 100644 --- a/test/fixtures/wpt/versions.json +++ b/test/fixtures/wpt/versions.json @@ -4,7 +4,7 @@ "path": "console" }, "encoding": { - "commit": "5059d2c77703d67d2f76931b44e6d2437526b6e9", + "commit": "d7f9e16c9a9d578a05b59787ba4de68b710de725", "path": "encoding" }, "url": { diff --git a/test/parallel/test-crypto-worker-thread.js b/test/parallel/test-crypto-worker-thread.js new file mode 100644 index 00000000000000..c3b978ceb3d37c --- /dev/null +++ b/test/parallel/test-crypto-worker-thread.js @@ -0,0 +1,18 @@ +'use strict'; +const common = require('../common'); +if (!common.hasCrypto) + common.skip('missing crypto'); + +// Issue https://github.com/nodejs/node/issues/35263 +/* Description: test for checking keyobject passed to worker thread + does not crash */ +const { createSecretKey } = require('crypto'); + +const { Worker, isMainThread, workerData } = require('worker_threads'); + +if (isMainThread) { + const key = createSecretKey(Buffer.from('hello')); + new Worker(__filename, { workerData: key }); +} else { + console.log(workerData); +} diff --git a/test/parallel/test-eventtarget.js b/test/parallel/test-eventtarget.js index 1e505cc7c48e5a..9487c22e26d1af 100644 --- a/test/parallel/test-eventtarget.js +++ b/test/parallel/test-eventtarget.js @@ -56,6 +56,21 @@ let asyncTest = Promise.resolve(); ev.preventDefault(); strictEqual(ev.defaultPrevented, false); } +{ + [ + 'foo', + 1, + false, + function() {}, + ].forEach((i) => ( + throws(() => new Event('foo', i), { + code: 'ERR_INVALID_ARG_TYPE', + name: 'TypeError', + message: 'The "options" argument must be of type object.' + + common.invalidArgTypeHelper(i) + }) + )); +} { const ev = new Event('foo'); strictEqual(ev.cancelBubble, false); @@ -221,31 +236,33 @@ let asyncTest = Promise.resolve(); false ].forEach((i) => { throws(() => target.dispatchEvent(i), { - code: 'ERR_INVALID_ARG_TYPE' + code: 'ERR_INVALID_ARG_TYPE', + name: 'TypeError', + message: 'The "event" argument must be an instance of Event.' + + common.invalidArgTypeHelper(i) }); }); + const err = (arg) => ({ + code: 'ERR_INVALID_ARG_TYPE', + name: 'TypeError', + message: 'The "listener" argument must be an instance of EventListener.' + + common.invalidArgTypeHelper(arg) + }); + [ 'foo', 1, {}, // No handleEvent function - false, - ].forEach((i) => { - throws(() => target.addEventListener('foo', i), { - code: 'ERR_INVALID_ARG_TYPE' - }); - }); + false + ].forEach((i) => throws(() => target.addEventListener('foo', i), err(i))); [ 'foo', 1, {}, // No handleEvent function false - ].forEach((i) => { - throws(() => target.removeEventListener('foo', i), { - code: 'ERR_INVALID_ARG_TYPE' - }); - }); + ].forEach((i) => throws(() => target.addEventListener('foo', i), err(i))); } { diff --git a/test/parallel/test-loaders-unknown-builtin-module.mjs b/test/parallel/test-loaders-unknown-builtin-module.mjs index 464dbeb22a9b31..85181a2b73b54c 100644 --- a/test/parallel/test-loaders-unknown-builtin-module.mjs +++ b/test/parallel/test-loaders-unknown-builtin-module.mjs @@ -2,7 +2,7 @@ import { expectsError, mustCall } from '../common/index.mjs'; import assert from 'assert'; -const unknownBuiltinModule = 'unknown-builtin-module'; +const unknownBuiltinModule = 'node:unknown-builtin-module'; import(unknownBuiltinModule) .then(assert.fail, expectsError({ diff --git a/test/parallel/test-repl.js b/test/parallel/test-repl.js index e200ebbaeee4ea..fb7006fbc1db48 100644 --- a/test/parallel/test-repl.js +++ b/test/parallel/test-repl.js @@ -757,7 +757,7 @@ const errorTests = [ { send: 'console', expect: [ - '{', + 'Object [console] {', ' log: [Function: log],', ' warn: [Function: warn],', ' dir: [Function: dir],', diff --git a/tools/code_cache/mkcodecache.cc b/tools/code_cache/mkcodecache.cc index c314e07fd78b22..3a1d0ee50e5078 100644 --- a/tools/code_cache/mkcodecache.cc +++ b/tools/code_cache/mkcodecache.cc @@ -24,6 +24,7 @@ using v8::Local; int wmain(int argc, wchar_t* argv[]) { #else // UNIX int main(int argc, char* argv[]) { + argv = uv_setup_args(argc, argv); #endif // _WIN32 v8::V8::SetFlagsFromString("--random_seed=42"); diff --git a/tools/doc/checkLinks.js b/tools/doc/checkLinks.js index f3415f521cc6d5..19247c9340e75a 100644 --- a/tools/doc/checkLinks.js +++ b/tools/doc/checkLinks.js @@ -26,11 +26,11 @@ function findMarkdownFilesRecursively(dirPath) { if ( entry.isDirectory() && - entry.name !== 'api' && entry.name !== 'build' && entry.name !== 'changelogs' && entry.name !== 'deps' && entry.name !== 'fixtures' && + entry.name !== 'gyp' && entry.name !== 'node_modules' && entry.name !== 'out' && entry.name !== 'tmp' @@ -65,7 +65,7 @@ function checkFile(path) { console.error((process.env.GITHUB_ACTIONS ? `::error file=${path},line=${line},col=${column}::` : '') + `Unordered reference at ${path}:${line}:${column} (` + - `"${node.label}" should be before "${previousDefinitionLabel})"` + `"${node.label}" should be before "${previousDefinitionLabel}")` ); process.exitCode = 1; } diff --git a/tools/doc/html.js b/tools/doc/html.js index 2e60d0786448b5..b74b8ac15462c9 100644 --- a/tools/doc/html.js +++ b/tools/doc/html.js @@ -51,7 +51,9 @@ function navClasses() { } const gtocPath = path.join(docPath, 'api', 'index.md'); -const gtocMD = fs.readFileSync(gtocPath, 'utf8').replace(/^/gms, ''); +const gtocMD = fs.readFileSync(gtocPath, 'utf8') + .replace(/\(([^#?]+?)\.md\)/ig, (_, filename) => `(${filename}.html)`) + .replace(/^/gms, ''); const gtocHTML = unified() .use(markdown) .use(remark2rehype, { allowDangerousHtml: true }) diff --git a/tools/doc/markdown.js b/tools/doc/markdown.js index 97feadbf9990bb..a3b2f0a94ec561 100644 --- a/tools/doc/markdown.js +++ b/tools/doc/markdown.js @@ -2,14 +2,25 @@ const visit = require('unist-util-visit'); +const referenceToLocalMdFile = /^(?![+a-z]+:)([^#?]+)\.md(#.+)?$/i; + module.exports = { - replaceLinks + replaceLinks, + referenceToLocalMdFile, }; function replaceLinks({ filename, linksMapper }) { return (tree) => { const fileHtmlUrls = linksMapper[filename]; + visit(tree, (node) => { + if (node.url) { + node.url = node.url.replace( + referenceToLocalMdFile, + (_, filename, hash) => `${filename}.html${hash || ''}` + ); + } + }); visit(tree, 'definition', (node) => { const htmlUrl = fileHtmlUrls && fileHtmlUrls[node.identifier]; diff --git a/tools/doc/type-parser.js b/tools/doc/type-parser.js index 0b7424f8e72c42..36db179b87c8b2 100644 --- a/tools/doc/type-parser.js +++ b/tools/doc/type-parser.js @@ -114,7 +114,7 @@ const customTypesMap = { 'module': 'modules.html#modules_the_module_object', 'module.SourceMap': - 'modules_module.html#modules_module_class_module_sourcemap', + 'module.html#module_class_module_sourcemap', 'require': 'modules.html#modules_require_id', diff --git a/tools/gyp/.flake8 b/tools/gyp/.flake8 new file mode 100644 index 00000000000000..ea0c7680ef87b2 --- /dev/null +++ b/tools/gyp/.flake8 @@ -0,0 +1,4 @@ +[flake8] +max-complexity = 101 +max-line-length = 88 +extend-ignore = E203 # whitespace before ':' to agree with psf/black diff --git a/tools/gyp/.github/workflows/Python_tests.yml b/tools/gyp/.github/workflows/Python_tests.yml new file mode 100644 index 00000000000000..319e40972b12fd --- /dev/null +++ b/tools/gyp/.github/workflows/Python_tests.yml @@ -0,0 +1,31 @@ +# TODO: Enable os: windows-latest +# TODO: Enable python-version: 3.5 +# TODO: Enable pytest --doctest-modules + +name: Python_tests +on: [push, pull_request] +jobs: + Python_tests: + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + max-parallel: 15 + matrix: + os: [macos-latest, ubuntu-latest] # , windows-latest] + python-version: [2.7, 3.6, 3.7, 3.8, 3.9.0-rc.1] # 3.5, + steps: + - uses: actions/checkout@v2 + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v2 + with: + python-version: ${{ matrix.python-version }} + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install -r requirements_dev.txt + - name: Lint with flake8 + run: flake8 . --count --show-source --statistics + - name: Test with pytest + run: pytest + # - name: Run doctests with pytest + # run: pytest --doctest-modules diff --git a/tools/gyp/.github/workflows/nodejs-windows.yml b/tools/gyp/.github/workflows/nodejs-windows.yml new file mode 100644 index 00000000000000..48a42372c2f0b0 --- /dev/null +++ b/tools/gyp/.github/workflows/nodejs-windows.yml @@ -0,0 +1,27 @@ +name: Node.js Windows integration + +on: [push, pull_request] + +jobs: + build-windows: + runs-on: windows-latest + steps: + - name: Clone node-gyp + uses: actions/checkout@v2 + with: + path: gyp-next + - name: Clone nodejs/node + uses: actions/checkout@v2 + with: + repository: nodejs/node + path: node + - name: Install deps + run: choco install nasm + - name: Replace gyp in Node.js + run: | + rm -Recurse node/tools/gyp + cp -Recurse gyp-next node/tools/gyp + - name: Build Node.js + run: | + cd node + ./vcbuild.bat diff --git a/tools/gyp/.gitignore b/tools/gyp/.gitignore index 0d20b6487c61e7..d82fa7a96c9015 100644 --- a/tools/gyp/.gitignore +++ b/tools/gyp/.gitignore @@ -1 +1,143 @@ -*.pyc +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +pip-wheel-metadata/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +# static files generated from Django application using `collectstatic` +media +static diff --git a/tools/gyp/AUTHORS b/tools/gyp/AUTHORS index 130c8160589364..f49a357b9ed104 100644 --- a/tools/gyp/AUTHORS +++ b/tools/gyp/AUTHORS @@ -13,3 +13,4 @@ Eric N. Vander Weele Tom Freudenberg Julien Brianceau Refael Ackermann +Ujjwal Sharma diff --git a/tools/gyp/CHANGELOG.md b/tools/gyp/CHANGELOG.md new file mode 100644 index 00000000000000..082f96a4a940de --- /dev/null +++ b/tools/gyp/CHANGELOG.md @@ -0,0 +1,54 @@ +# Changelog + +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). + +## [Unreleased] + +## [0.5.0] - 2020-09-30 + +### Added +- Extended compile_commands_json generator to consider more file extensions than + just `c` and `cc`. `cpp` and `cxx` are now supported. +- Source files with duplicate basenames are now supported. + +### Removed +- The `--no-duplicate-basename-check` option was removed. +- The `msvs_enable_marmasm` configuration option was removed in favor of + auto-inclusion of the "marmasm" sections for Windows on ARM. + +## [0.4.0] - 2020-07-14 + +### Added +- Added support for passing arbitrary architectures to Xcode builds, enables `arm64` builds. + +### Fixed +- Fixed a bug on Solaris where copying archives failed. + +## [0.3.0] - 2020-06-06 + +### Added +- Added support for MSVC cross-compilation. This allows compilation on x64 for + a Windows ARM target. + +### Fixed +- Fixed XCode CLT version detection on macOS Catalina. + +## [0.2.1] - 2020-05-05 + +### Fixed +- Relicensed to Node.js contributors. +- Fixed Windows bug introduced in v0.2.0. + +## [0.2.0] - 2020-04-06 + +This is the first release of this project, based on https://chromium.googlesource.com/external/gyp +with changes made over the years in Node.js and node-gyp. + +[Unreleased]: https://github.com/nodejs/gyp-next/compare/v0.5.0...HEAD +[0.5.0]: https://github.com/nodejs/gyp-next/compare/v0.4.0...v0.5.0 +[0.4.0]: https://github.com/nodejs/gyp-next/compare/v0.3.0...v0.4.0 +[0.3.0]: https://github.com/nodejs/gyp-next/compare/v0.2.1...v0.3.0 +[0.2.1]: https://github.com/nodejs/gyp-next/compare/v0.2.0...v0.2.1 +[0.2.0]: https://github.com/nodejs/gyp-next/releases/tag/v0.2.0 diff --git a/tools/gyp/CODE_OF_CONDUCT.md b/tools/gyp/CODE_OF_CONDUCT.md new file mode 100644 index 00000000000000..4c211405596cb4 --- /dev/null +++ b/tools/gyp/CODE_OF_CONDUCT.md @@ -0,0 +1,4 @@ +# Code of Conduct + +* [Node.js Code of Conduct](https://github.com/nodejs/admin/blob/master/CODE_OF_CONDUCT.md) +* [Node.js Moderation Policy](https://github.com/nodejs/admin/blob/master/Moderation-Policy.md) diff --git a/tools/gyp/CONTRIBUTING.md b/tools/gyp/CONTRIBUTING.md new file mode 100644 index 00000000000000..f9dd574a47cd9d --- /dev/null +++ b/tools/gyp/CONTRIBUTING.md @@ -0,0 +1,32 @@ +# Contributing to gyp-next + +## Code of Conduct + +This project is bound to the [Node.js Code of Conduct](https://github.com/nodejs/admin/blob/master/CODE_OF_CONDUCT.md). + + +## Developer's Certificate of Origin 1.1 + +By making a contribution to this project, I certify that: + +* (a) The contribution was created in whole or in part by me and I + have the right to submit it under the open source license + indicated in the file; or + +* (b) The contribution is based upon previous work that, to the best + of my knowledge, is covered under an appropriate open source + license and I have the right under that license to submit that + work with modifications, whether created in whole or in part + by me, under the same open source license (unless I am + permitted to submit under a different license), as indicated + in the file; or + +* (c) The contribution was provided directly to me by some other + person who certified (a), (b) or (c) and I have not modified + it. + +* (d) I understand and agree that this project and the contribution + are public and that a record of the contribution (including all + personal information I submit with it, including my sign-off) is + maintained indefinitely and may be redistributed consistent with + this project or the open source license(s) involved. diff --git a/tools/gyp/LICENSE b/tools/gyp/LICENSE index ab6b011a109335..c6944c5e4ed488 100644 --- a/tools/gyp/LICENSE +++ b/tools/gyp/LICENSE @@ -1,3 +1,4 @@ +Copyright (c) 2020 Node.js contributors. All rights reserved. Copyright (c) 2009 Google Inc. All rights reserved. Redistribution and use in source and binary forms, with or without diff --git a/tools/gyp/OWNERS b/tools/gyp/OWNERS deleted file mode 100644 index 72e8ffc0db8aad..00000000000000 --- a/tools/gyp/OWNERS +++ /dev/null @@ -1 +0,0 @@ -* diff --git a/tools/gyp/PRESUBMIT.py b/tools/gyp/PRESUBMIT.py deleted file mode 100644 index e52f9d2d22d607..00000000000000 --- a/tools/gyp/PRESUBMIT.py +++ /dev/null @@ -1,138 +0,0 @@ -# Copyright (c) 2012 Google Inc. All rights reserved. -# Use of this source code is governed by a BSD-style license that can be -# found in the LICENSE file. - - -"""Top-level presubmit script for GYP. - -See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts -for more details about the presubmit API built into gcl. -""" - - -PYLINT_BLACKLIST = [ - # TODO: fix me. - # From SCons, not done in google style. - 'test/lib/TestCmd.py', - 'test/lib/TestCommon.py', - 'test/lib/TestGyp.py', -] - - -PYLINT_DISABLED_WARNINGS = [ - # TODO: fix me. - # Many tests include modules they don't use. - 'W0611', - # Possible unbalanced tuple unpacking with sequence. - 'W0632', - # Attempting to unpack a non-sequence. - 'W0633', - # Include order doesn't properly include local files? - 'F0401', - # Some use of built-in names. - 'W0622', - # Some unused variables. - 'W0612', - # Operator not preceded/followed by space. - 'C0323', - 'C0322', - # Unnecessary semicolon. - 'W0301', - # Unused argument. - 'W0613', - # String has no effect (docstring in wrong place). - 'W0105', - # map/filter on lambda could be replaced by comprehension. - 'W0110', - # Use of eval. - 'W0123', - # Comma not followed by space. - 'C0324', - # Access to a protected member. - 'W0212', - # Bad indent. - 'W0311', - # Line too long. - 'C0301', - # Undefined variable. - 'E0602', - # Not exception type specified. - 'W0702', - # No member of that name. - 'E1101', - # Dangerous default {}. - 'W0102', - # Cyclic import. - 'R0401', - # Others, too many to sort. - 'W0201', 'W0232', 'E1103', 'W0621', 'W0108', 'W0223', 'W0231', - 'R0201', 'E0101', 'C0321', - # ************* Module copy - # W0104:427,12:_test.odict.__setitem__: Statement seems to have no effect - 'W0104', -] - - -def _LicenseHeader(input_api): - # Accept any year number from 2009 to the current year. - current_year = int(input_api.time.strftime('%Y')) - allowed_years = (str(s) for s in reversed(range(2009, current_year + 1))) - years_re = '(' + '|'.join(allowed_years) + ')' - - # The (c) is deprecated, but tolerate it until it's removed from all files. - return ( - r'.*? Copyright (\(c\) )?%(year)s Google Inc\. All rights reserved\.\n' - r'.*? Use of this source code is governed by a BSD-style license that ' - r'can be\n' - r'.*? found in the LICENSE file\.\n' - ) % { - 'year': years_re, - } - -def CheckChangeOnUpload(input_api, output_api): - report = [] - report.extend(input_api.canned_checks.PanProjectChecks( - input_api, output_api, license_header=_LicenseHeader(input_api))) - return report - - -def CheckChangeOnCommit(input_api, output_api): - report = [] - - report.extend(input_api.canned_checks.PanProjectChecks( - input_api, output_api, license_header=_LicenseHeader(input_api))) - report.extend(input_api.canned_checks.CheckTreeIsOpen( - input_api, output_api, - 'http://gyp-status.appspot.com/status', - 'http://gyp-status.appspot.com/current')) - - import os - import sys - old_sys_path = sys.path - try: - sys.path = ['pylib', 'test/lib'] + sys.path - blacklist = PYLINT_BLACKLIST - if sys.platform == 'win32': - blacklist = [os.path.normpath(x).replace('\\', '\\\\') - for x in PYLINT_BLACKLIST] - report.extend(input_api.canned_checks.RunPylint( - input_api, - output_api, - black_list=blacklist, - disabled_warnings=PYLINT_DISABLED_WARNINGS)) - finally: - sys.path = old_sys_path - return report - - -TRYBOTS = [ - 'linux_try', - 'mac_try', - 'win_try', -] - - -def GetPreferredTryMasters(_, change): - return { - 'client.gyp': { t: set(['defaulttests']) for t in TRYBOTS }, - } diff --git a/tools/gyp/README.md b/tools/gyp/README.md index c0d73ac9587af3..9ffc2b21e81b8b 100644 --- a/tools/gyp/README.md +++ b/tools/gyp/README.md @@ -2,3 +2,6 @@ GYP can Generate Your Projects. =================================== Documents are available at [gyp.gsrc.io](https://gyp.gsrc.io), or you can check out ```md-pages``` branch to read those documents offline. + +__gyp-next__ is [released](https://github.com/nodejs/gyp-next/releases) to the [__Python Packaging Index__](https://pypi.org/project/gyp-next) (PyPI) and can be installed with the command: +* `python3 -m pip install gyp-next` diff --git a/tools/gyp/gyp_main.py b/tools/gyp/gyp_main.py index f738e8009f71e7..da696cfc4b1c3f 100755 --- a/tools/gyp/gyp_main.py +++ b/tools/gyp/gyp_main.py @@ -10,41 +10,42 @@ PY3 = bytes != str -# Below IsCygwin() function copied from pylib/gyp/common.py + def IsCygwin(): - try: - out = subprocess.Popen("uname", - stdout=subprocess.PIPE, - stderr=subprocess.STDOUT) - stdout, stderr = out.communicate() - if PY3: - stdout = stdout.decode("utf-8") - return "CYGWIN" in str(stdout) - except Exception: - return False + # Function copied from pylib/gyp/common.py + try: + out = subprocess.Popen( + "uname", stdout=subprocess.PIPE, stderr=subprocess.STDOUT + ) + stdout, stderr = out.communicate() + if PY3: + stdout = stdout.decode("utf-8") + return "CYGWIN" in str(stdout) + except Exception: + return False def UnixifyPath(path): - try: - if not IsCygwin(): - return path - out = subprocess.Popen(["cygpath", "-u", path], - stdout=subprocess.PIPE, - stderr=subprocess.STDOUT) - stdout, _ = out.communicate() - if PY3: - stdout = stdout.decode("utf-8") - return str(stdout) - except Exception: - return path + try: + if not IsCygwin(): + return path + out = subprocess.Popen( + ["cygpath", "-u", path], stdout=subprocess.PIPE, stderr=subprocess.STDOUT + ) + stdout, _ = out.communicate() + if PY3: + stdout = stdout.decode("utf-8") + return str(stdout) + except Exception: + return path # Make sure we're using the version of pylib in this repo, not one installed # elsewhere on the system. Also convert to Unix style path on Cygwin systems, # else the 'gyp' library will not be found path = UnixifyPath(sys.argv[0]) -sys.path.insert(0, os.path.join(os.path.dirname(path), 'pylib')) -import gyp +sys.path.insert(0, os.path.join(os.path.dirname(path), "pylib")) +import gyp # noqa: E402 -if __name__ == '__main__': - sys.exit(gyp.script_main()) +if __name__ == "__main__": + sys.exit(gyp.script_main()) diff --git a/tools/gyp/pylib/gyp/MSVSNew.py b/tools/gyp/pylib/gyp/MSVSNew.py index 740ef2c73fa71b..04bbb3df718727 100644 --- a/tools/gyp/pylib/gyp/MSVSNew.py +++ b/tools/gyp/pylib/gyp/MSVSNew.py @@ -12,26 +12,28 @@ import gyp.common try: - cmp + cmp except NameError: - def cmp(x, y): - return (x > y) - (x < y) + + def cmp(x, y): + return (x > y) - (x < y) + # Initialize random number generator random.seed() # GUIDs for project types ENTRY_TYPE_GUIDS = { - 'project': '{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}', - 'folder': '{2150E333-8FDC-42A3-9474-1A3956D46DE8}', + "project": "{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}", + "folder": "{2150E333-8FDC-42A3-9474-1A3956D46DE8}", } -#------------------------------------------------------------------------------ +# ------------------------------------------------------------------------------ # Helper functions -def MakeGuid(name, seed='msvs_new'): - """Returns a GUID for the specified target name. +def MakeGuid(name, seed="msvs_new"): + """Returns a GUID for the specified target name. Args: name: Target name. @@ -45,28 +47,39 @@ def MakeGuid(name, seed='msvs_new'): determine the GUID to refer to explicitly. It also means that the GUID will not change when the project for a target is rebuilt. """ - # Calculate a MD5 signature for the seed and name. - d = hashlib.md5((str(seed) + str(name)).encode('utf-8')).hexdigest().upper() - # Convert most of the signature to GUID form (discard the rest) - guid = ('{' + d[:8] + '-' + d[8:12] + '-' + d[12:16] + '-' + d[16:20] - + '-' + d[20:32] + '}') - return guid - -#------------------------------------------------------------------------------ + # Calculate a MD5 signature for the seed and name. + d = hashlib.md5((str(seed) + str(name)).encode("utf-8")).hexdigest().upper() + # Convert most of the signature to GUID form (discard the rest) + guid = ( + "{" + + d[:8] + + "-" + + d[8:12] + + "-" + + d[12:16] + + "-" + + d[16:20] + + "-" + + d[20:32] + + "}" + ) + return guid + + +# ------------------------------------------------------------------------------ class MSVSSolutionEntry(object): - def __cmp__(self, other): - # Sort by name then guid (so things are in order on vs2008). - return cmp((self.name, self.get_guid()), (other.name, other.get_guid())) + def __cmp__(self, other): + # Sort by name then guid (so things are in order on vs2008). + return cmp((self.name, self.get_guid()), (other.name, other.get_guid())) class MSVSFolder(MSVSSolutionEntry): - """Folder in a Visual Studio project or solution.""" + """Folder in a Visual Studio project or solution.""" - def __init__(self, path, name = None, entries = None, - guid = None, items = None): - """Initializes the folder. + def __init__(self, path, name=None, entries=None, guid=None, items=None): + """Initializes the folder. Args: path: Full path to the folder. @@ -77,38 +90,46 @@ def __init__(self, path, name = None, entries = None, items: List of solution items to include in the folder project. May be None, if the folder does not directly contain items. """ - if name: - self.name = name - else: - # Use last layer. - self.name = os.path.basename(path) + if name: + self.name = name + else: + # Use last layer. + self.name = os.path.basename(path) - self.path = path - self.guid = guid + self.path = path + self.guid = guid - # Copy passed lists (or set to empty lists) - self.entries = sorted(entries or [], key=attrgetter('path')) - self.items = list(items or []) + # Copy passed lists (or set to empty lists) + self.entries = sorted(entries or [], key=attrgetter("path")) + self.items = list(items or []) - self.entry_type_guid = ENTRY_TYPE_GUIDS['folder'] + self.entry_type_guid = ENTRY_TYPE_GUIDS["folder"] - def get_guid(self): - if self.guid is None: - # Use consistent guids for folders (so things don't regenerate). - self.guid = MakeGuid(self.path, seed='msvs_folder') - return self.guid + def get_guid(self): + if self.guid is None: + # Use consistent guids for folders (so things don't regenerate). + self.guid = MakeGuid(self.path, seed="msvs_folder") + return self.guid -#------------------------------------------------------------------------------ +# ------------------------------------------------------------------------------ class MSVSProject(MSVSSolutionEntry): - """Visual Studio project.""" - - def __init__(self, path, name = None, dependencies = None, guid = None, - spec = None, build_file = None, config_platform_overrides = None, - fixpath_prefix = None): - """Initializes the project. + """Visual Studio project.""" + + def __init__( + self, + path, + name=None, + dependencies=None, + guid=None, + spec=None, + build_file=None, + config_platform_overrides=None, + fixpath_prefix=None, + ): + """Initializes the project. Args: path: Absolute path to the project file. @@ -123,57 +144,59 @@ def __init__(self, path, name = None, dependencies = None, guid = None, used in place of the default for this target. fixpath_prefix: the path used to adjust the behavior of _fixpath """ - self.path = path - self.guid = guid - self.spec = spec - self.build_file = build_file - # Use project filename if name not specified - self.name = name or os.path.splitext(os.path.basename(path))[0] - - # Copy passed lists (or set to empty lists) - self.dependencies = list(dependencies or []) - - self.entry_type_guid = ENTRY_TYPE_GUIDS['project'] - - if config_platform_overrides: - self.config_platform_overrides = config_platform_overrides - else: - self.config_platform_overrides = {} - self.fixpath_prefix = fixpath_prefix - self.msbuild_toolset = None - - def set_dependencies(self, dependencies): - self.dependencies = list(dependencies or []) - - def get_guid(self): - if self.guid is None: - # Set GUID from path - # TODO(rspangler): This is fragile. - # 1. We can't just use the project filename sans path, since there could - # be multiple projects with the same base name (for example, - # foo/unittest.vcproj and bar/unittest.vcproj). - # 2. The path needs to be relative to $SOURCE_ROOT, so that the project - # GUID is the same whether it's included from base/base.sln or - # foo/bar/baz/baz.sln. - # 3. The GUID needs to be the same each time this builder is invoked, so - # that we don't need to rebuild the solution when the project changes. - # 4. We should be able to handle pre-built project files by reading the - # GUID from the files. - self.guid = MakeGuid(self.name) - return self.guid - - def set_msbuild_toolset(self, msbuild_toolset): - self.msbuild_toolset = msbuild_toolset - -#------------------------------------------------------------------------------ + self.path = path + self.guid = guid + self.spec = spec + self.build_file = build_file + # Use project filename if name not specified + self.name = name or os.path.splitext(os.path.basename(path))[0] + + # Copy passed lists (or set to empty lists) + self.dependencies = list(dependencies or []) + + self.entry_type_guid = ENTRY_TYPE_GUIDS["project"] + + if config_platform_overrides: + self.config_platform_overrides = config_platform_overrides + else: + self.config_platform_overrides = {} + self.fixpath_prefix = fixpath_prefix + self.msbuild_toolset = None + + def set_dependencies(self, dependencies): + self.dependencies = list(dependencies or []) + + def get_guid(self): + if self.guid is None: + # Set GUID from path + # TODO(rspangler): This is fragile. + # 1. We can't just use the project filename sans path, since there could + # be multiple projects with the same base name (for example, + # foo/unittest.vcproj and bar/unittest.vcproj). + # 2. The path needs to be relative to $SOURCE_ROOT, so that the project + # GUID is the same whether it's included from base/base.sln or + # foo/bar/baz/baz.sln. + # 3. The GUID needs to be the same each time this builder is invoked, so + # that we don't need to rebuild the solution when the project changes. + # 4. We should be able to handle pre-built project files by reading the + # GUID from the files. + self.guid = MakeGuid(self.name) + return self.guid + + def set_msbuild_toolset(self, msbuild_toolset): + self.msbuild_toolset = msbuild_toolset + + +# ------------------------------------------------------------------------------ class MSVSSolution(object): - """Visual Studio solution.""" + """Visual Studio solution.""" - def __init__(self, path, version, entries=None, variants=None, - websiteProperties=True): - """Initializes the solution. + def __init__( + self, path, version, entries=None, variants=None, websiteProperties=True + ): + """Initializes the solution. Args: path: Path to solution file. @@ -185,152 +208,163 @@ def __init__(self, path, version, entries=None, variants=None, websiteProperties: Flag to decide if the website properties section is generated. """ - self.path = path - self.websiteProperties = websiteProperties - self.version = version - - # Copy passed lists (or set to empty lists) - self.entries = list(entries or []) - - if variants: - # Copy passed list - self.variants = variants[:] - else: - # Use default - self.variants = ['Debug|Win32', 'Release|Win32'] - # TODO(rspangler): Need to be able to handle a mapping of solution config - # to project config. Should we be able to handle variants being a dict, - # or add a separate variant_map variable? If it's a dict, we can't - # guarantee the order of variants since dict keys aren't ordered. - - - # TODO(rspangler): Automatically write to disk for now; should delay until - # node-evaluation time. - self.Write() - - - def Write(self, writer=gyp.common.WriteOnDiff): - """Writes the solution file to disk. + self.path = path + self.websiteProperties = websiteProperties + self.version = version + + # Copy passed lists (or set to empty lists) + self.entries = list(entries or []) + + if variants: + # Copy passed list + self.variants = variants[:] + else: + # Use default + self.variants = ["Debug|Win32", "Release|Win32"] + # TODO(rspangler): Need to be able to handle a mapping of solution config + # to project config. Should we be able to handle variants being a dict, + # or add a separate variant_map variable? If it's a dict, we can't + # guarantee the order of variants since dict keys aren't ordered. + + # TODO(rspangler): Automatically write to disk for now; should delay until + # node-evaluation time. + self.Write() + + def Write(self, writer=gyp.common.WriteOnDiff): + """Writes the solution file to disk. Raises: IndexError: An entry appears multiple times. """ - # Walk the entry tree and collect all the folders and projects. - all_entries = set() - entries_to_check = self.entries[:] - while entries_to_check: - e = entries_to_check.pop(0) - - # If this entry has been visited, nothing to do. - if e in all_entries: - continue - - all_entries.add(e) - - # If this is a folder, check its entries too. - if isinstance(e, MSVSFolder): - entries_to_check += e.entries - - all_entries = sorted(all_entries, key=attrgetter('path')) - - # Open file and print header - f = writer(self.path) - f.write('Microsoft Visual Studio Solution File, ' - 'Format Version %s\r\n' % self.version.SolutionVersion()) - f.write('# %s\r\n' % self.version.Description()) - - # Project entries - sln_root = os.path.split(self.path)[0] - for e in all_entries: - relative_path = gyp.common.RelativePath(e.path, sln_root) - # msbuild does not accept an empty folder_name. - # use '.' in case relative_path is empty. - folder_name = relative_path.replace('/', '\\') or '.' - f.write('Project("%s") = "%s", "%s", "%s"\r\n' % ( - e.entry_type_guid, # Entry type GUID - e.name, # Folder name - folder_name, # Folder name (again) - e.get_guid(), # Entry GUID - )) - - # TODO(rspangler): Need a way to configure this stuff - if self.websiteProperties: - f.write('\tProjectSection(WebsiteProperties) = preProject\r\n' - '\t\tDebug.AspNetCompiler.Debug = "True"\r\n' - '\t\tRelease.AspNetCompiler.Debug = "False"\r\n' - '\tEndProjectSection\r\n') - - if isinstance(e, MSVSFolder): - if e.items: - f.write('\tProjectSection(SolutionItems) = preProject\r\n') - for i in e.items: - f.write('\t\t%s = %s\r\n' % (i, i)) - f.write('\tEndProjectSection\r\n') - - if isinstance(e, MSVSProject): - if e.dependencies: - f.write('\tProjectSection(ProjectDependencies) = postProject\r\n') - for d in e.dependencies: - f.write('\t\t%s = %s\r\n' % (d.get_guid(), d.get_guid())) - f.write('\tEndProjectSection\r\n') - - f.write('EndProject\r\n') - - # Global section - f.write('Global\r\n') - - # Configurations (variants) - f.write('\tGlobalSection(SolutionConfigurationPlatforms) = preSolution\r\n') - for v in self.variants: - f.write('\t\t%s = %s\r\n' % (v, v)) - f.write('\tEndGlobalSection\r\n') - - # Sort config guids for easier diffing of solution changes. - config_guids = [] - config_guids_overrides = {} - for e in all_entries: - if isinstance(e, MSVSProject): - config_guids.append(e.get_guid()) - config_guids_overrides[e.get_guid()] = e.config_platform_overrides - config_guids.sort() - - f.write('\tGlobalSection(ProjectConfigurationPlatforms) = postSolution\r\n') - for g in config_guids: - for v in self.variants: - nv = config_guids_overrides[g].get(v, v) - # Pick which project configuration to build for this solution - # configuration. - f.write('\t\t%s.%s.ActiveCfg = %s\r\n' % ( - g, # Project GUID - v, # Solution build configuration - nv, # Project build config for that solution config - )) - - # Enable project in this solution configuration. - f.write('\t\t%s.%s.Build.0 = %s\r\n' % ( - g, # Project GUID - v, # Solution build configuration - nv, # Project build config for that solution config - )) - f.write('\tEndGlobalSection\r\n') - - # TODO(rspangler): Should be able to configure this stuff too (though I've - # never seen this be any different) - f.write('\tGlobalSection(SolutionProperties) = preSolution\r\n') - f.write('\t\tHideSolutionNode = FALSE\r\n') - f.write('\tEndGlobalSection\r\n') - - # Folder mappings - # Omit this section if there are no folders - if any([e.entries for e in all_entries if isinstance(e, MSVSFolder)]): - f.write('\tGlobalSection(NestedProjects) = preSolution\r\n') - for e in all_entries: - if not isinstance(e, MSVSFolder): - continue # Does not apply to projects, only folders - for subentry in e.entries: - f.write('\t\t%s = %s\r\n' % (subentry.get_guid(), e.get_guid())) - f.write('\tEndGlobalSection\r\n') - - f.write('EndGlobal\r\n') - - f.close() + # Walk the entry tree and collect all the folders and projects. + all_entries = set() + entries_to_check = self.entries[:] + while entries_to_check: + e = entries_to_check.pop(0) + + # If this entry has been visited, nothing to do. + if e in all_entries: + continue + + all_entries.add(e) + + # If this is a folder, check its entries too. + if isinstance(e, MSVSFolder): + entries_to_check += e.entries + + all_entries = sorted(all_entries, key=attrgetter("path")) + + # Open file and print header + f = writer(self.path) + f.write( + "Microsoft Visual Studio Solution File, " + "Format Version %s\r\n" % self.version.SolutionVersion() + ) + f.write("# %s\r\n" % self.version.Description()) + + # Project entries + sln_root = os.path.split(self.path)[0] + for e in all_entries: + relative_path = gyp.common.RelativePath(e.path, sln_root) + # msbuild does not accept an empty folder_name. + # use '.' in case relative_path is empty. + folder_name = relative_path.replace("/", "\\") or "." + f.write( + 'Project("%s") = "%s", "%s", "%s"\r\n' + % ( + e.entry_type_guid, # Entry type GUID + e.name, # Folder name + folder_name, # Folder name (again) + e.get_guid(), # Entry GUID + ) + ) + + # TODO(rspangler): Need a way to configure this stuff + if self.websiteProperties: + f.write( + "\tProjectSection(WebsiteProperties) = preProject\r\n" + '\t\tDebug.AspNetCompiler.Debug = "True"\r\n' + '\t\tRelease.AspNetCompiler.Debug = "False"\r\n' + "\tEndProjectSection\r\n" + ) + + if isinstance(e, MSVSFolder): + if e.items: + f.write("\tProjectSection(SolutionItems) = preProject\r\n") + for i in e.items: + f.write("\t\t%s = %s\r\n" % (i, i)) + f.write("\tEndProjectSection\r\n") + + if isinstance(e, MSVSProject): + if e.dependencies: + f.write("\tProjectSection(ProjectDependencies) = postProject\r\n") + for d in e.dependencies: + f.write("\t\t%s = %s\r\n" % (d.get_guid(), d.get_guid())) + f.write("\tEndProjectSection\r\n") + + f.write("EndProject\r\n") + + # Global section + f.write("Global\r\n") + + # Configurations (variants) + f.write("\tGlobalSection(SolutionConfigurationPlatforms) = preSolution\r\n") + for v in self.variants: + f.write("\t\t%s = %s\r\n" % (v, v)) + f.write("\tEndGlobalSection\r\n") + + # Sort config guids for easier diffing of solution changes. + config_guids = [] + config_guids_overrides = {} + for e in all_entries: + if isinstance(e, MSVSProject): + config_guids.append(e.get_guid()) + config_guids_overrides[e.get_guid()] = e.config_platform_overrides + config_guids.sort() + + f.write("\tGlobalSection(ProjectConfigurationPlatforms) = postSolution\r\n") + for g in config_guids: + for v in self.variants: + nv = config_guids_overrides[g].get(v, v) + # Pick which project configuration to build for this solution + # configuration. + f.write( + "\t\t%s.%s.ActiveCfg = %s\r\n" + % ( + g, # Project GUID + v, # Solution build configuration + nv, # Project build config for that solution config + ) + ) + + # Enable project in this solution configuration. + f.write( + "\t\t%s.%s.Build.0 = %s\r\n" + % ( + g, # Project GUID + v, # Solution build configuration + nv, # Project build config for that solution config + ) + ) + f.write("\tEndGlobalSection\r\n") + + # TODO(rspangler): Should be able to configure this stuff too (though I've + # never seen this be any different) + f.write("\tGlobalSection(SolutionProperties) = preSolution\r\n") + f.write("\t\tHideSolutionNode = FALSE\r\n") + f.write("\tEndGlobalSection\r\n") + + # Folder mappings + # Omit this section if there are no folders + if any([e.entries for e in all_entries if isinstance(e, MSVSFolder)]): + f.write("\tGlobalSection(NestedProjects) = preSolution\r\n") + for e in all_entries: + if not isinstance(e, MSVSFolder): + continue # Does not apply to projects, only folders + for subentry in e.entries: + f.write("\t\t%s = %s\r\n" % (subentry.get_guid(), e.get_guid())) + f.write("\tEndGlobalSection\r\n") + + f.write("EndGlobal\r\n") + + f.close() diff --git a/tools/gyp/pylib/gyp/MSVSProject.py b/tools/gyp/pylib/gyp/MSVSProject.py index db1ceede344c60..f953d52cd03d36 100644 --- a/tools/gyp/pylib/gyp/MSVSProject.py +++ b/tools/gyp/pylib/gyp/MSVSProject.py @@ -4,55 +4,55 @@ """Visual Studio project reader/writer.""" -import gyp.common import gyp.easy_xml as easy_xml -#------------------------------------------------------------------------------ +# ------------------------------------------------------------------------------ class Tool(object): - """Visual Studio tool.""" + """Visual Studio tool.""" - def __init__(self, name, attrs=None): - """Initializes the tool. + def __init__(self, name, attrs=None): + """Initializes the tool. Args: name: Tool name. attrs: Dict of tool attributes; may be None. """ - self._attrs = attrs or {} - self._attrs['Name'] = name + self._attrs = attrs or {} + self._attrs["Name"] = name - def _GetSpecification(self): - """Creates an element for the tool. + def _GetSpecification(self): + """Creates an element for the tool. Returns: A new xml.dom.Element for the tool. """ - return ['Tool', self._attrs] + return ["Tool", self._attrs] + class Filter(object): - """Visual Studio filter - that is, a virtual folder.""" + """Visual Studio filter - that is, a virtual folder.""" - def __init__(self, name, contents=None): - """Initializes the folder. + def __init__(self, name, contents=None): + """Initializes the folder. Args: name: Filter (folder) name. contents: List of filenames and/or Filter objects contained. """ - self.name = name - self.contents = list(contents or []) + self.name = name + self.contents = list(contents or []) -#------------------------------------------------------------------------------ +# ------------------------------------------------------------------------------ class Writer(object): - """Visual Studio XML project writer.""" + """Visual Studio XML project writer.""" - def __init__(self, project_path, version, name, guid=None, platforms=None): - """Initializes the project. + def __init__(self, project_path, version, name, guid=None, platforms=None): + """Initializes the project. Args: project_path: Path to the project file. @@ -61,36 +61,36 @@ def __init__(self, project_path, version, name, guid=None, platforms=None): guid: GUID to use for project, if not None. platforms: Array of string, the supported platforms. If null, ['Win32'] """ - self.project_path = project_path - self.version = version - self.name = name - self.guid = guid + self.project_path = project_path + self.version = version + self.name = name + self.guid = guid - # Default to Win32 for platforms. - if not platforms: - platforms = ['Win32'] + # Default to Win32 for platforms. + if not platforms: + platforms = ["Win32"] - # Initialize the specifications of the various sections. - self.platform_section = ['Platforms'] - for platform in platforms: - self.platform_section.append(['Platform', {'Name': platform}]) - self.tool_files_section = ['ToolFiles'] - self.configurations_section = ['Configurations'] - self.files_section = ['Files'] + # Initialize the specifications of the various sections. + self.platform_section = ["Platforms"] + for platform in platforms: + self.platform_section.append(["Platform", {"Name": platform}]) + self.tool_files_section = ["ToolFiles"] + self.configurations_section = ["Configurations"] + self.files_section = ["Files"] - # Keep a dict keyed on filename to speed up access. - self.files_dict = dict() + # Keep a dict keyed on filename to speed up access. + self.files_dict = dict() - def AddToolFile(self, path): - """Adds a tool file to the project. + def AddToolFile(self, path): + """Adds a tool file to the project. Args: path: Relative path from project to tool file. """ - self.tool_files_section.append(['ToolFile', {'RelativePath': path}]) + self.tool_files_section.append(["ToolFile", {"RelativePath": path}]) - def _GetSpecForConfiguration(self, config_type, config_name, attrs, tools): - """Returns the specification for a configuration. + def _GetSpecForConfiguration(self, config_type, config_name, attrs, tools): + """Returns the specification for a configuration. Args: config_type: Type of configuration node. @@ -99,40 +99,39 @@ def _GetSpecForConfiguration(self, config_type, config_name, attrs, tools): tools: List of tools (strings or Tool objects); may be None. Returns: """ - # Handle defaults - if not attrs: - attrs = {} - if not tools: - tools = [] - - # Add configuration node and its attributes - node_attrs = attrs.copy() - node_attrs['Name'] = config_name - specification = [config_type, node_attrs] - - # Add tool nodes and their attributes - if tools: - for t in tools: - if isinstance(t, Tool): - specification.append(t._GetSpecification()) - else: - specification.append(Tool(t)._GetSpecification()) - return specification - - - def AddConfig(self, name, attrs=None, tools=None): - """Adds a configuration to the project. + # Handle defaults + if not attrs: + attrs = {} + if not tools: + tools = [] + + # Add configuration node and its attributes + node_attrs = attrs.copy() + node_attrs["Name"] = config_name + specification = [config_type, node_attrs] + + # Add tool nodes and their attributes + if tools: + for t in tools: + if isinstance(t, Tool): + specification.append(t._GetSpecification()) + else: + specification.append(Tool(t)._GetSpecification()) + return specification + + def AddConfig(self, name, attrs=None, tools=None): + """Adds a configuration to the project. Args: name: Configuration name. attrs: Dict of configuration attributes; may be None. tools: List of tools (strings or Tool objects); may be None. """ - spec = self._GetSpecForConfiguration('Configuration', name, attrs, tools) - self.configurations_section.append(spec) + spec = self._GetSpecForConfiguration("Configuration", name, attrs, tools) + self.configurations_section.append(spec) - def _AddFilesToNode(self, parent, files): - """Adds files and/or filters to the parent node. + def _AddFilesToNode(self, parent, files): + """Adds files and/or filters to the parent node. Args: parent: Destination node @@ -140,17 +139,17 @@ def _AddFilesToNode(self, parent, files): Will call itself recursively, if the files list contains Filter objects. """ - for f in files: - if isinstance(f, Filter): - node = ['Filter', {'Name': f.name}] - self._AddFilesToNode(node, f.contents) - else: - node = ['File', {'RelativePath': f}] - self.files_dict[f] = node - parent.append(node) - - def AddFiles(self, files): - """Adds files to the project. + for f in files: + if isinstance(f, Filter): + node = ["Filter", {"Name": f.name}] + self._AddFilesToNode(node, f.contents) + else: + node = ["File", {"RelativePath": f}] + self.files_dict[f] = node + parent.append(node) + + def AddFiles(self, files): + """Adds files to the project. Args: files: A list of Filter objects and/or relative paths to files. @@ -159,12 +158,12 @@ def AddFiles(self, files): later add files to a Filter object which was passed into a previous call to AddFiles(), it will not be reflected in this project. """ - self._AddFilesToNode(self.files_section, files) - # TODO(rspangler) This also doesn't handle adding files to an existing - # filter. That is, it doesn't merge the trees. + self._AddFilesToNode(self.files_section, files) + # TODO(rspangler) This also doesn't handle adding files to an existing + # filter. That is, it doesn't merge the trees. - def AddFileConfig(self, path, config, attrs=None, tools=None): - """Adds a configuration to a file. + def AddFileConfig(self, path, config, attrs=None, tools=None): + """Adds a configuration to a file. Args: path: Relative path to the file. @@ -175,34 +174,33 @@ def AddFileConfig(self, path, config, attrs=None, tools=None): Raises: ValueError: Relative path does not match any file added via AddFiles(). """ - # Find the file node with the right relative path - parent = self.files_dict.get(path) - if not parent: - raise ValueError('AddFileConfig: file "%s" not in project.' % path) - - # Add the config to the file node - spec = self._GetSpecForConfiguration('FileConfiguration', config, attrs, - tools) - parent.append(spec) - - def WriteIfChanged(self): - """Writes the project file.""" - # First create XML content definition - content = [ - 'VisualStudioProject', - {'ProjectType': 'Visual C++', - 'Version': self.version.ProjectVersion(), - 'Name': self.name, - 'ProjectGUID': self.guid, - 'RootNamespace': self.name, - 'Keyword': 'Win32Proj' - }, - self.platform_section, - self.tool_files_section, - self.configurations_section, - ['References'], # empty section - self.files_section, - ['Globals'] # empty section - ] - easy_xml.WriteXmlIfChanged(content, self.project_path, - encoding="Windows-1252") + # Find the file node with the right relative path + parent = self.files_dict.get(path) + if not parent: + raise ValueError('AddFileConfig: file "%s" not in project.' % path) + + # Add the config to the file node + spec = self._GetSpecForConfiguration("FileConfiguration", config, attrs, tools) + parent.append(spec) + + def WriteIfChanged(self): + """Writes the project file.""" + # First create XML content definition + content = [ + "VisualStudioProject", + { + "ProjectType": "Visual C++", + "Version": self.version.ProjectVersion(), + "Name": self.name, + "ProjectGUID": self.guid, + "RootNamespace": self.name, + "Keyword": "Win32Proj", + }, + self.platform_section, + self.tool_files_section, + self.configurations_section, + ["References"], # empty section + self.files_section, + ["Globals"], # empty section + ] + easy_xml.WriteXmlIfChanged(content, self.project_path, encoding="Windows-1252") diff --git a/tools/gyp/pylib/gyp/MSVSSettings.py b/tools/gyp/pylib/gyp/MSVSSettings.py index 5dd8f8c1e6e242..6ef16f2a0b23e2 100644 --- a/tools/gyp/pylib/gyp/MSVSSettings.py +++ b/tools/gyp/pylib/gyp/MSVSSettings.py @@ -37,42 +37,42 @@ class _Tool(object): - """Represents a tool used by MSVS or MSBuild. + """Represents a tool used by MSVS or MSBuild. Attributes: msvs_name: The name of the tool in MSVS. msbuild_name: The name of the tool in MSBuild. """ - def __init__(self, msvs_name, msbuild_name): - self.msvs_name = msvs_name - self.msbuild_name = msbuild_name + def __init__(self, msvs_name, msbuild_name): + self.msvs_name = msvs_name + self.msbuild_name = msbuild_name def _AddTool(tool): - """Adds a tool to the four dictionaries used to process settings. + """Adds a tool to the four dictionaries used to process settings. This only defines the tool. Each setting also needs to be added. Args: tool: The _Tool object to be added. """ - _msvs_validators[tool.msvs_name] = {} - _msbuild_validators[tool.msbuild_name] = {} - _msvs_to_msbuild_converters[tool.msvs_name] = {} - _msbuild_name_of_tool[tool.msvs_name] = tool.msbuild_name + _msvs_validators[tool.msvs_name] = {} + _msbuild_validators[tool.msbuild_name] = {} + _msvs_to_msbuild_converters[tool.msvs_name] = {} + _msbuild_name_of_tool[tool.msvs_name] = tool.msbuild_name def _GetMSBuildToolSettings(msbuild_settings, tool): - """Returns an MSBuild tool dictionary. Creates it if needed.""" - return msbuild_settings.setdefault(tool.msbuild_name, {}) + """Returns an MSBuild tool dictionary. Creates it if needed.""" + return msbuild_settings.setdefault(tool.msbuild_name, {}) class _Type(object): - """Type of settings (Base class).""" + """Type of settings (Base class).""" - def ValidateMSVS(self, value): - """Verifies that the value is legal for MSVS. + def ValidateMSVS(self, value): + """Verifies that the value is legal for MSVS. Args: value: the value to check for this type. @@ -81,8 +81,8 @@ def ValidateMSVS(self, value): ValueError if value is not valid for MSVS. """ - def ValidateMSBuild(self, value): - """Verifies that the value is legal for MSBuild. + def ValidateMSBuild(self, value): + """Verifies that the value is legal for MSBuild. Args: value: the value to check for this type. @@ -91,8 +91,8 @@ def ValidateMSBuild(self, value): ValueError if value is not valid for MSBuild. """ - def ConvertToMSBuild(self, value): - """Returns the MSBuild equivalent of the MSVS value given. + def ConvertToMSBuild(self, value): + """Returns the MSBuild equivalent of the MSVS value given. Args: value: the MSVS value to convert. @@ -103,84 +103,84 @@ def ConvertToMSBuild(self, value): Raises: ValueError if value is not valid. """ - return value + return value class _String(_Type): - """A setting that's just a string.""" + """A setting that's just a string.""" - def ValidateMSVS(self, value): - if not isinstance(value, string_types): - raise ValueError('expected string; got %r' % value) + def ValidateMSVS(self, value): + if not isinstance(value, string_types): + raise ValueError("expected string; got %r" % value) - def ValidateMSBuild(self, value): - if not isinstance(value, string_types): - raise ValueError('expected string; got %r' % value) + def ValidateMSBuild(self, value): + if not isinstance(value, string_types): + raise ValueError("expected string; got %r" % value) - def ConvertToMSBuild(self, value): - # Convert the macros - return ConvertVCMacrosToMSBuild(value) + def ConvertToMSBuild(self, value): + # Convert the macros + return ConvertVCMacrosToMSBuild(value) class _StringList(_Type): - """A settings that's a list of strings.""" + """A settings that's a list of strings.""" - def ValidateMSVS(self, value): - if not isinstance(value, string_types) and not isinstance(value, list): - raise ValueError('expected string list; got %r' % value) + def ValidateMSVS(self, value): + if not isinstance(value, string_types) and not isinstance(value, list): + raise ValueError("expected string list; got %r" % value) - def ValidateMSBuild(self, value): - if not isinstance(value, string_types) and not isinstance(value, list): - raise ValueError('expected string list; got %r' % value) + def ValidateMSBuild(self, value): + if not isinstance(value, string_types) and not isinstance(value, list): + raise ValueError("expected string list; got %r" % value) - def ConvertToMSBuild(self, value): - # Convert the macros - if isinstance(value, list): - return [ConvertVCMacrosToMSBuild(i) for i in value] - else: - return ConvertVCMacrosToMSBuild(value) + def ConvertToMSBuild(self, value): + # Convert the macros + if isinstance(value, list): + return [ConvertVCMacrosToMSBuild(i) for i in value] + else: + return ConvertVCMacrosToMSBuild(value) class _Boolean(_Type): - """Boolean settings, can have the values 'false' or 'true'.""" + """Boolean settings, can have the values 'false' or 'true'.""" - def _Validate(self, value): - if value != 'true' and value != 'false': - raise ValueError('expected bool; got %r' % value) + def _Validate(self, value): + if value != "true" and value != "false": + raise ValueError("expected bool; got %r" % value) - def ValidateMSVS(self, value): - self._Validate(value) + def ValidateMSVS(self, value): + self._Validate(value) - def ValidateMSBuild(self, value): - self._Validate(value) + def ValidateMSBuild(self, value): + self._Validate(value) - def ConvertToMSBuild(self, value): - self._Validate(value) - return value + def ConvertToMSBuild(self, value): + self._Validate(value) + return value class _Integer(_Type): - """Integer settings.""" + """Integer settings.""" - def __init__(self, msbuild_base=10): - _Type.__init__(self) - self._msbuild_base = msbuild_base + def __init__(self, msbuild_base=10): + _Type.__init__(self) + self._msbuild_base = msbuild_base - def ValidateMSVS(self, value): - # Try to convert, this will raise ValueError if invalid. - self.ConvertToMSBuild(value) + def ValidateMSVS(self, value): + # Try to convert, this will raise ValueError if invalid. + self.ConvertToMSBuild(value) - def ValidateMSBuild(self, value): - # Try to convert, this will raise ValueError if invalid. - int(value, self._msbuild_base) + def ValidateMSBuild(self, value): + # Try to convert, this will raise ValueError if invalid. + int(value, self._msbuild_base) - def ConvertToMSBuild(self, value): - msbuild_format = (self._msbuild_base == 10) and '%d' or '0x%04x' - return msbuild_format % int(value) + def ConvertToMSBuild(self, value): + msbuild_format = (self._msbuild_base == 10) and "%d" or "0x%04x" + return msbuild_format % int(value) class _Enumeration(_Type): - """Type of settings that is an enumeration. + """Type of settings that is an enumeration. In MSVS, the values are indexes like '0', '1', and '2'. MSBuild uses text labels that are more representative, like 'Win32'. @@ -192,31 +192,32 @@ class _Enumeration(_Type): new: an array of labels that are new to MSBuild. """ - def __init__(self, label_list, new=None): - _Type.__init__(self) - self._label_list = label_list - self._msbuild_values = set(value for value in label_list - if value is not None) - if new is not None: - self._msbuild_values.update(new) - - def ValidateMSVS(self, value): - # Try to convert. It will raise an exception if not valid. - self.ConvertToMSBuild(value) - - def ValidateMSBuild(self, value): - if value not in self._msbuild_values: - raise ValueError('unrecognized enumerated value %s' % value) - - def ConvertToMSBuild(self, value): - index = int(value) - if index < 0 or index >= len(self._label_list): - raise ValueError('index value (%d) not in expected range [0, %d)' % - (index, len(self._label_list))) - label = self._label_list[index] - if label is None: - raise ValueError('converted value for %s not specified.' % value) - return label + def __init__(self, label_list, new=None): + _Type.__init__(self) + self._label_list = label_list + self._msbuild_values = set(value for value in label_list if value is not None) + if new is not None: + self._msbuild_values.update(new) + + def ValidateMSVS(self, value): + # Try to convert. It will raise an exception if not valid. + self.ConvertToMSBuild(value) + + def ValidateMSBuild(self, value): + if value not in self._msbuild_values: + raise ValueError("unrecognized enumerated value %s" % value) + + def ConvertToMSBuild(self, value): + index = int(value) + if index < 0 or index >= len(self._label_list): + raise ValueError( + "index value (%d) not in expected range [0, %d)" + % (index, len(self._label_list)) + ) + label = self._label_list[index] + if label is None: + raise ValueError("converted value for %s not specified." % value) + return label # Instantiate the various generic types. @@ -231,22 +232,22 @@ def ConvertToMSBuild(self, value): _string_list = _StringList() # Some boolean settings went from numerical values to boolean. The # mapping is 0: default, 1: false, 2: true. -_newly_boolean = _Enumeration(['', 'false', 'true']) +_newly_boolean = _Enumeration(["", "false", "true"]) def _Same(tool, name, setting_type): - """Defines a setting that has the same name in MSVS and MSBuild. + """Defines a setting that has the same name in MSVS and MSBuild. Args: tool: a dictionary that gives the names of the tool for MSVS and MSBuild. name: the name of the setting. setting_type: the type of this setting. """ - _Renamed(tool, name, name, setting_type) + _Renamed(tool, name, name, setting_type) def _Renamed(tool, msvs_name, msbuild_name, setting_type): - """Defines a setting for which the name has changed. + """Defines a setting for which the name has changed. Args: tool: a dictionary that gives the names of the tool for MSVS and MSBuild. @@ -255,24 +256,25 @@ def _Renamed(tool, msvs_name, msbuild_name, setting_type): setting_type: the type of this setting. """ - def _Translate(value, msbuild_settings): - msbuild_tool_settings = _GetMSBuildToolSettings(msbuild_settings, tool) - msbuild_tool_settings[msbuild_name] = setting_type.ConvertToMSBuild(value) + def _Translate(value, msbuild_settings): + msbuild_tool_settings = _GetMSBuildToolSettings(msbuild_settings, tool) + msbuild_tool_settings[msbuild_name] = setting_type.ConvertToMSBuild(value) - _msvs_validators[tool.msvs_name][msvs_name] = setting_type.ValidateMSVS - _msbuild_validators[tool.msbuild_name][msbuild_name] = ( - setting_type.ValidateMSBuild) - _msvs_to_msbuild_converters[tool.msvs_name][msvs_name] = _Translate + _msvs_validators[tool.msvs_name][msvs_name] = setting_type.ValidateMSVS + _msbuild_validators[tool.msbuild_name][msbuild_name] = setting_type.ValidateMSBuild + _msvs_to_msbuild_converters[tool.msvs_name][msvs_name] = _Translate def _Moved(tool, settings_name, msbuild_tool_name, setting_type): - _MovedAndRenamed(tool, settings_name, msbuild_tool_name, settings_name, - setting_type) + _MovedAndRenamed( + tool, settings_name, msbuild_tool_name, settings_name, setting_type + ) -def _MovedAndRenamed(tool, msvs_settings_name, msbuild_tool_name, - msbuild_settings_name, setting_type): - """Defines a setting that may have moved to a new section. +def _MovedAndRenamed( + tool, msvs_settings_name, msbuild_tool_name, msbuild_settings_name, setting_type +): + """Defines a setting that may have moved to a new section. Args: tool: a dictionary that gives the names of the tool for MSVS and MSBuild. @@ -282,19 +284,18 @@ def _MovedAndRenamed(tool, msvs_settings_name, msbuild_tool_name, setting_type: the type of this setting. """ - def _Translate(value, msbuild_settings): - tool_settings = msbuild_settings.setdefault(msbuild_tool_name, {}) - tool_settings[msbuild_settings_name] = setting_type.ConvertToMSBuild(value) + def _Translate(value, msbuild_settings): + tool_settings = msbuild_settings.setdefault(msbuild_tool_name, {}) + tool_settings[msbuild_settings_name] = setting_type.ConvertToMSBuild(value) - _msvs_validators[tool.msvs_name][msvs_settings_name] = ( - setting_type.ValidateMSVS) - validator = setting_type.ValidateMSBuild - _msbuild_validators[msbuild_tool_name][msbuild_settings_name] = validator - _msvs_to_msbuild_converters[tool.msvs_name][msvs_settings_name] = _Translate + _msvs_validators[tool.msvs_name][msvs_settings_name] = setting_type.ValidateMSVS + validator = setting_type.ValidateMSBuild + _msbuild_validators[msbuild_tool_name][msbuild_settings_name] = validator + _msvs_to_msbuild_converters[tool.msvs_name][msvs_settings_name] = _Translate def _MSVSOnly(tool, name, setting_type): - """Defines a setting that is only found in MSVS. + """Defines a setting that is only found in MSVS. Args: tool: a dictionary that gives the names of the tool for MSVS and MSBuild. @@ -302,16 +303,16 @@ def _MSVSOnly(tool, name, setting_type): setting_type: the type of this setting. """ - def _Translate(unused_value, unused_msbuild_settings): - # Since this is for MSVS only settings, no translation will happen. - pass + def _Translate(unused_value, unused_msbuild_settings): + # Since this is for MSVS only settings, no translation will happen. + pass - _msvs_validators[tool.msvs_name][name] = setting_type.ValidateMSVS - _msvs_to_msbuild_converters[tool.msvs_name][name] = _Translate + _msvs_validators[tool.msvs_name][name] = setting_type.ValidateMSVS + _msvs_to_msbuild_converters[tool.msvs_name][name] = _Translate def _MSBuildOnly(tool, name, setting_type): - """Defines a setting that is only found in MSBuild. + """Defines a setting that is only found in MSBuild. Args: tool: a dictionary that gives the names of the tool for MSVS and MSBuild. @@ -319,17 +320,17 @@ def _MSBuildOnly(tool, name, setting_type): setting_type: the type of this setting. """ - def _Translate(value, msbuild_settings): - # Let msbuild-only properties get translated as-is from msvs_settings. - tool_settings = msbuild_settings.setdefault(tool.msbuild_name, {}) - tool_settings[name] = value + def _Translate(value, msbuild_settings): + # Let msbuild-only properties get translated as-is from msvs_settings. + tool_settings = msbuild_settings.setdefault(tool.msbuild_name, {}) + tool_settings[name] = value - _msbuild_validators[tool.msbuild_name][name] = setting_type.ValidateMSBuild - _msvs_to_msbuild_converters[tool.msvs_name][name] = _Translate + _msbuild_validators[tool.msbuild_name][name] = setting_type.ValidateMSBuild + _msvs_to_msbuild_converters[tool.msvs_name][name] = _Translate def _ConvertedToAdditionalOption(tool, msvs_name, flag): - """Defines a setting that's handled via a command line option in MSBuild. + """Defines a setting that's handled via a command line option in MSBuild. Args: tool: a dictionary that gives the names of the tool for MSVS and MSBuild. @@ -337,53 +338,55 @@ def _ConvertedToAdditionalOption(tool, msvs_name, flag): flag: the flag to insert at the end of the AdditionalOptions """ - def _Translate(value, msbuild_settings): - if value == 'true': - tool_settings = _GetMSBuildToolSettings(msbuild_settings, tool) - if 'AdditionalOptions' in tool_settings: - new_flags = '%s %s' % (tool_settings['AdditionalOptions'], flag) - else: - new_flags = flag - tool_settings['AdditionalOptions'] = new_flags - _msvs_validators[tool.msvs_name][msvs_name] = _boolean.ValidateMSVS - _msvs_to_msbuild_converters[tool.msvs_name][msvs_name] = _Translate + def _Translate(value, msbuild_settings): + if value == "true": + tool_settings = _GetMSBuildToolSettings(msbuild_settings, tool) + if "AdditionalOptions" in tool_settings: + new_flags = "%s %s" % (tool_settings["AdditionalOptions"], flag) + else: + new_flags = flag + tool_settings["AdditionalOptions"] = new_flags + + _msvs_validators[tool.msvs_name][msvs_name] = _boolean.ValidateMSVS + _msvs_to_msbuild_converters[tool.msvs_name][msvs_name] = _Translate def _CustomGeneratePreprocessedFile(tool, msvs_name): - def _Translate(value, msbuild_settings): - tool_settings = _GetMSBuildToolSettings(msbuild_settings, tool) - if value == '0': - tool_settings['PreprocessToFile'] = 'false' - tool_settings['PreprocessSuppressLineNumbers'] = 'false' - elif value == '1': # /P - tool_settings['PreprocessToFile'] = 'true' - tool_settings['PreprocessSuppressLineNumbers'] = 'false' - elif value == '2': # /EP /P - tool_settings['PreprocessToFile'] = 'true' - tool_settings['PreprocessSuppressLineNumbers'] = 'true' - else: - raise ValueError('value must be one of [0, 1, 2]; got %s' % value) - # Create a bogus validator that looks for '0', '1', or '2' - msvs_validator = _Enumeration(['a', 'b', 'c']).ValidateMSVS - _msvs_validators[tool.msvs_name][msvs_name] = msvs_validator - msbuild_validator = _boolean.ValidateMSBuild - msbuild_tool_validators = _msbuild_validators[tool.msbuild_name] - msbuild_tool_validators['PreprocessToFile'] = msbuild_validator - msbuild_tool_validators['PreprocessSuppressLineNumbers'] = msbuild_validator - _msvs_to_msbuild_converters[tool.msvs_name][msvs_name] = _Translate - - -fix_vc_macro_slashes_regex_list = ('IntDir', 'OutDir') + def _Translate(value, msbuild_settings): + tool_settings = _GetMSBuildToolSettings(msbuild_settings, tool) + if value == "0": + tool_settings["PreprocessToFile"] = "false" + tool_settings["PreprocessSuppressLineNumbers"] = "false" + elif value == "1": # /P + tool_settings["PreprocessToFile"] = "true" + tool_settings["PreprocessSuppressLineNumbers"] = "false" + elif value == "2": # /EP /P + tool_settings["PreprocessToFile"] = "true" + tool_settings["PreprocessSuppressLineNumbers"] = "true" + else: + raise ValueError("value must be one of [0, 1, 2]; got %s" % value) + + # Create a bogus validator that looks for '0', '1', or '2' + msvs_validator = _Enumeration(["a", "b", "c"]).ValidateMSVS + _msvs_validators[tool.msvs_name][msvs_name] = msvs_validator + msbuild_validator = _boolean.ValidateMSBuild + msbuild_tool_validators = _msbuild_validators[tool.msbuild_name] + msbuild_tool_validators["PreprocessToFile"] = msbuild_validator + msbuild_tool_validators["PreprocessSuppressLineNumbers"] = msbuild_validator + _msvs_to_msbuild_converters[tool.msvs_name][msvs_name] = _Translate + + +fix_vc_macro_slashes_regex_list = ("IntDir", "OutDir") fix_vc_macro_slashes_regex = re.compile( - r'(\$\((?:%s)\))(?:[\\/]+)' % "|".join(fix_vc_macro_slashes_regex_list) + r"(\$\((?:%s)\))(?:[\\/]+)" % "|".join(fix_vc_macro_slashes_regex_list) ) # Regular expression to detect keys that were generated by exclusion lists -_EXCLUDED_SUFFIX_RE = re.compile('^(.*)_excluded$') +_EXCLUDED_SUFFIX_RE = re.compile("^(.*)_excluded$") def _ValidateExclusionSetting(setting, settings, error_msg, stderr=sys.stderr): - """Verify that 'setting' is valid if it is generated from an exclusion list. + """Verify that 'setting' is valid if it is generated from an exclusion list. If the setting appears to be generated from an exclusion list, the root name is checked. @@ -394,57 +397,57 @@ def _ValidateExclusionSetting(setting, settings, error_msg, stderr=sys.stderr): error_msg: The message to emit in the event of error stderr: The stream receiving the error messages. """ - # This may be unrecognized because it's an exclusion list. If the - # setting name has the _excluded suffix, then check the root name. - unrecognized = True - m = re.match(_EXCLUDED_SUFFIX_RE, setting) - if m: - root_setting = m.group(1) - unrecognized = root_setting not in settings + # This may be unrecognized because it's an exclusion list. If the + # setting name has the _excluded suffix, then check the root name. + unrecognized = True + m = re.match(_EXCLUDED_SUFFIX_RE, setting) + if m: + root_setting = m.group(1) + unrecognized = root_setting not in settings - if unrecognized: - # We don't know this setting. Give a warning. - print(error_msg, file=stderr) + if unrecognized: + # We don't know this setting. Give a warning. + print(error_msg, file=stderr) def FixVCMacroSlashes(s): - """Replace macros which have excessive following slashes. + """Replace macros which have excessive following slashes. These macros are known to have a built-in trailing slash. Furthermore, many scripts hiccup on processing paths with extra slashes in the middle. This list is probably not exhaustive. Add as needed. """ - if '$' in s: - s = fix_vc_macro_slashes_regex.sub(r'\1', s) - return s + if "$" in s: + s = fix_vc_macro_slashes_regex.sub(r"\1", s) + return s def ConvertVCMacrosToMSBuild(s): - """Convert the MSVS macros found in the string to the MSBuild equivalent. + """Convert the MSVS macros found in the string to the MSBuild equivalent. This list is probably not exhaustive. Add as needed. """ - if '$' in s: - replace_map = { - '$(ConfigurationName)': '$(Configuration)', - '$(InputDir)': '%(RelativeDir)', - '$(InputExt)': '%(Extension)', - '$(InputFileName)': '%(Filename)%(Extension)', - '$(InputName)': '%(Filename)', - '$(InputPath)': '%(Identity)', - '$(ParentName)': '$(ProjectFileName)', - '$(PlatformName)': '$(Platform)', - '$(SafeInputName)': '%(Filename)', - } - for old, new in replace_map.items(): - s = s.replace(old, new) - s = FixVCMacroSlashes(s) - return s + if "$" in s: + replace_map = { + "$(ConfigurationName)": "$(Configuration)", + "$(InputDir)": "%(RelativeDir)", + "$(InputExt)": "%(Extension)", + "$(InputFileName)": "%(Filename)%(Extension)", + "$(InputName)": "%(Filename)", + "$(InputPath)": "%(Identity)", + "$(ParentName)": "$(ProjectFileName)", + "$(PlatformName)": "$(Platform)", + "$(SafeInputName)": "%(Filename)", + } + for old, new in replace_map.items(): + s = s.replace(old, new) + s = FixVCMacroSlashes(s) + return s def ConvertToMSBuildSettings(msvs_settings, stderr=sys.stderr): - """Converts MSVS settings (VS2008 and earlier) to MSBuild settings (VS2010+). + """Converts MSVS settings (VS2008 and earlier) to MSBuild settings (VS2010+). Args: msvs_settings: A dictionary. The key is the tool name. The values are @@ -456,55 +459,65 @@ def ConvertToMSBuildSettings(msvs_settings, stderr=sys.stderr): or the empty string (for the global settings). The values are themselves dictionaries of settings and their values. """ - msbuild_settings = {} - for msvs_tool_name, msvs_tool_settings in msvs_settings.items(): - if msvs_tool_name in _msvs_to_msbuild_converters: - msvs_tool = _msvs_to_msbuild_converters[msvs_tool_name] - for msvs_setting, msvs_value in msvs_tool_settings.items(): - if msvs_setting in msvs_tool: - # Invoke the translation function. - try: - msvs_tool[msvs_setting](msvs_value, msbuild_settings) - except ValueError as e: - print('Warning: while converting %s/%s to MSBuild, ' - '%s' % (msvs_tool_name, msvs_setting, e), file=stderr) + msbuild_settings = {} + for msvs_tool_name, msvs_tool_settings in msvs_settings.items(): + if msvs_tool_name in _msvs_to_msbuild_converters: + msvs_tool = _msvs_to_msbuild_converters[msvs_tool_name] + for msvs_setting, msvs_value in msvs_tool_settings.items(): + if msvs_setting in msvs_tool: + # Invoke the translation function. + try: + msvs_tool[msvs_setting](msvs_value, msbuild_settings) + except ValueError as e: + print( + "Warning: while converting %s/%s to MSBuild, " + "%s" % (msvs_tool_name, msvs_setting, e), + file=stderr, + ) + else: + _ValidateExclusionSetting( + msvs_setting, + msvs_tool, + ( + "Warning: unrecognized setting %s/%s " + "while converting to MSBuild." + % (msvs_tool_name, msvs_setting) + ), + stderr, + ) else: - _ValidateExclusionSetting(msvs_setting, - msvs_tool, - ('Warning: unrecognized setting %s/%s ' - 'while converting to MSBuild.' % - (msvs_tool_name, msvs_setting)), - stderr) - else: - print('Warning: unrecognized tool %s while converting to ' - 'MSBuild.' % msvs_tool_name, file=stderr) - return msbuild_settings + print( + "Warning: unrecognized tool %s while converting to " + "MSBuild." % msvs_tool_name, + file=stderr, + ) + return msbuild_settings def ValidateMSVSSettings(settings, stderr=sys.stderr): - """Validates that the names of the settings are valid for MSVS. + """Validates that the names of the settings are valid for MSVS. Args: settings: A dictionary. The key is the tool name. The values are themselves dictionaries of settings and their values. stderr: The stream receiving the error messages. """ - _ValidateSettings(_msvs_validators, settings, stderr) + _ValidateSettings(_msvs_validators, settings, stderr) def ValidateMSBuildSettings(settings, stderr=sys.stderr): - """Validates that the names of the settings are valid for MSBuild. + """Validates that the names of the settings are valid for MSBuild. Args: settings: A dictionary. The key is the tool name. The values are themselves dictionaries of settings and their values. stderr: The stream receiving the error messages. """ - _ValidateSettings(_msbuild_validators, settings, stderr) + _ValidateSettings(_msbuild_validators, settings, stderr) def _ValidateSettings(validators, settings, stderr): - """Validates that the settings are valid for MSBuild or MSVS. + """Validates that the settings are valid for MSBuild or MSVS. We currently only validate the names of the settings, not their values. @@ -514,36 +527,39 @@ def _ValidateSettings(validators, settings, stderr): themselves dictionaries of settings and their values. stderr: The stream receiving the error messages. """ - for tool_name in settings: - if tool_name in validators: - tool_validators = validators[tool_name] - for setting, value in settings[tool_name].items(): - if setting in tool_validators: - try: - tool_validators[setting](value) - except ValueError as e: - print('Warning: for %s/%s, %s' % - (tool_name, setting, e), file=stderr) - else: - _ValidateExclusionSetting(setting, - tool_validators, - ('Warning: unrecognized setting %s/%s' % - (tool_name, setting)), - stderr) + for tool_name in settings: + if tool_name in validators: + tool_validators = validators[tool_name] + for setting, value in settings[tool_name].items(): + if setting in tool_validators: + try: + tool_validators[setting](value) + except ValueError as e: + print( + "Warning: for %s/%s, %s" % (tool_name, setting, e), + file=stderr, + ) + else: + _ValidateExclusionSetting( + setting, + tool_validators, + ("Warning: unrecognized setting %s/%s" % (tool_name, setting)), + stderr, + ) - else: - print('Warning: unrecognized tool %s' % (tool_name), file=stderr) + else: + print("Warning: unrecognized tool %s" % (tool_name), file=stderr) # MSVS and MBuild names of the tools. -_compile = _Tool('VCCLCompilerTool', 'ClCompile') -_link = _Tool('VCLinkerTool', 'Link') -_midl = _Tool('VCMIDLTool', 'Midl') -_rc = _Tool('VCResourceCompilerTool', 'ResourceCompile') -_lib = _Tool('VCLibrarianTool', 'Lib') -_manifest = _Tool('VCManifestTool', 'Manifest') -_masm = _Tool('MASM', 'MASM') -_armasm = _Tool('ARMASM', 'ARMASM') +_compile = _Tool("VCCLCompilerTool", "ClCompile") +_link = _Tool("VCLinkerTool", "Link") +_midl = _Tool("VCMIDLTool", "Midl") +_rc = _Tool("VCResourceCompilerTool", "ResourceCompile") +_lib = _Tool("VCLibrarianTool", "Lib") +_manifest = _Tool("VCManifestTool", "Manifest") +_masm = _Tool("MASM", "MASM") +_armasm = _Tool("ARMASM", "ARMASM") _AddTool(_compile) @@ -555,9 +571,9 @@ def _ValidateSettings(validators, settings, stderr): _AddTool(_masm) _AddTool(_armasm) # Add sections only found in the MSBuild settings. -_msbuild_validators[''] = {} -_msbuild_validators['ProjectReference'] = {} -_msbuild_validators['ManifestResourceCompile'] = {} +_msbuild_validators[""] = {} +_msbuild_validators["ProjectReference"] = {} +_msbuild_validators["ManifestResourceCompile"] = {} # Descriptions of the compiler options, i.e. VCCLCompilerTool in MSVS and # ClCompile in MSBuild. @@ -565,167 +581,231 @@ def _ValidateSettings(validators, settings, stderr): # the schema of the MSBuild ClCompile settings. # Options that have the same name in MSVS and MSBuild -_Same(_compile, 'AdditionalIncludeDirectories', _folder_list) # /I -_Same(_compile, 'AdditionalOptions', _string_list) -_Same(_compile, 'AdditionalUsingDirectories', _folder_list) # /AI -_Same(_compile, 'AssemblerListingLocation', _file_name) # /Fa -_Same(_compile, 'BrowseInformationFile', _file_name) -_Same(_compile, 'BufferSecurityCheck', _boolean) # /GS -_Same(_compile, 'DisableLanguageExtensions', _boolean) # /Za -_Same(_compile, 'DisableSpecificWarnings', _string_list) # /wd -_Same(_compile, 'EnableFiberSafeOptimizations', _boolean) # /GT -_Same(_compile, 'EnablePREfast', _boolean) # /analyze Visible='false' -_Same(_compile, 'ExpandAttributedSource', _boolean) # /Fx -_Same(_compile, 'FloatingPointExceptions', _boolean) # /fp:except -_Same(_compile, 'ForceConformanceInForLoopScope', _boolean) # /Zc:forScope -_Same(_compile, 'ForcedIncludeFiles', _file_list) # /FI -_Same(_compile, 'ForcedUsingFiles', _file_list) # /FU -_Same(_compile, 'GenerateXMLDocumentationFiles', _boolean) # /doc -_Same(_compile, 'IgnoreStandardIncludePath', _boolean) # /X -_Same(_compile, 'MinimalRebuild', _boolean) # /Gm -_Same(_compile, 'OmitDefaultLibName', _boolean) # /Zl -_Same(_compile, 'OmitFramePointers', _boolean) # /Oy -_Same(_compile, 'PreprocessorDefinitions', _string_list) # /D -_Same(_compile, 'ProgramDataBaseFileName', _file_name) # /Fd -_Same(_compile, 'RuntimeTypeInfo', _boolean) # /GR -_Same(_compile, 'ShowIncludes', _boolean) # /showIncludes -_Same(_compile, 'SmallerTypeCheck', _boolean) # /RTCc -_Same(_compile, 'StringPooling', _boolean) # /GF -_Same(_compile, 'SuppressStartupBanner', _boolean) # /nologo -_Same(_compile, 'TreatWChar_tAsBuiltInType', _boolean) # /Zc:wchar_t -_Same(_compile, 'UndefineAllPreprocessorDefinitions', _boolean) # /u -_Same(_compile, 'UndefinePreprocessorDefinitions', _string_list) # /U -_Same(_compile, 'UseFullPaths', _boolean) # /FC -_Same(_compile, 'WholeProgramOptimization', _boolean) # /GL -_Same(_compile, 'XMLDocumentationFileName', _file_name) -_Same(_compile, 'CompileAsWinRT', _boolean) # /ZW - -_Same(_compile, 'AssemblerOutput', - _Enumeration(['NoListing', - 'AssemblyCode', # /FA - 'All', # /FAcs - 'AssemblyAndMachineCode', # /FAc - 'AssemblyAndSourceCode'])) # /FAs -_Same(_compile, 'BasicRuntimeChecks', - _Enumeration(['Default', - 'StackFrameRuntimeCheck', # /RTCs - 'UninitializedLocalUsageCheck', # /RTCu - 'EnableFastChecks'])) # /RTC1 -_Same(_compile, 'BrowseInformation', - _Enumeration(['false', - 'true', # /FR - 'true'])) # /Fr -_Same(_compile, 'CallingConvention', - _Enumeration(['Cdecl', # /Gd - 'FastCall', # /Gr - 'StdCall', # /Gz - 'VectorCall'])) # /Gv -_Same(_compile, 'CompileAs', - _Enumeration(['Default', - 'CompileAsC', # /TC - 'CompileAsCpp'])) # /TP -_Same(_compile, 'DebugInformationFormat', - _Enumeration(['', # Disabled - 'OldStyle', # /Z7 - None, - 'ProgramDatabase', # /Zi - 'EditAndContinue'])) # /ZI -_Same(_compile, 'EnableEnhancedInstructionSet', - _Enumeration(['NotSet', - 'StreamingSIMDExtensions', # /arch:SSE - 'StreamingSIMDExtensions2', # /arch:SSE2 - 'AdvancedVectorExtensions', # /arch:AVX (vs2012+) - 'NoExtensions', # /arch:IA32 (vs2012+) - # This one only exists in the new msbuild format. - 'AdvancedVectorExtensions2', # /arch:AVX2 (vs2013r2+) - ])) -_Same(_compile, 'ErrorReporting', - _Enumeration(['None', # /errorReport:none - 'Prompt', # /errorReport:prompt - 'Queue'], # /errorReport:queue - new=['Send'])) # /errorReport:send" -_Same(_compile, 'ExceptionHandling', - _Enumeration(['false', - 'Sync', # /EHsc - 'Async'], # /EHa - new=['SyncCThrow'])) # /EHs -_Same(_compile, 'FavorSizeOrSpeed', - _Enumeration(['Neither', - 'Speed', # /Ot - 'Size'])) # /Os -_Same(_compile, 'FloatingPointModel', - _Enumeration(['Precise', # /fp:precise - 'Strict', # /fp:strict - 'Fast'])) # /fp:fast -_Same(_compile, 'InlineFunctionExpansion', - _Enumeration(['Default', - 'OnlyExplicitInline', # /Ob1 - 'AnySuitable'], # /Ob2 - new=['Disabled'])) # /Ob0 -_Same(_compile, 'Optimization', - _Enumeration(['Disabled', # /Od - 'MinSpace', # /O1 - 'MaxSpeed', # /O2 - 'Full'])) # /Ox -_Same(_compile, 'RuntimeLibrary', - _Enumeration(['MultiThreaded', # /MT - 'MultiThreadedDebug', # /MTd - 'MultiThreadedDLL', # /MD - 'MultiThreadedDebugDLL'])) # /MDd -_Same(_compile, 'StructMemberAlignment', - _Enumeration(['Default', - '1Byte', # /Zp1 - '2Bytes', # /Zp2 - '4Bytes', # /Zp4 - '8Bytes', # /Zp8 - '16Bytes'])) # /Zp16 -_Same(_compile, 'WarningLevel', - _Enumeration(['TurnOffAllWarnings', # /W0 - 'Level1', # /W1 - 'Level2', # /W2 - 'Level3', # /W3 - 'Level4'], # /W4 - new=['EnableAllWarnings'])) # /Wall +_Same(_compile, "AdditionalIncludeDirectories", _folder_list) # /I +_Same(_compile, "AdditionalOptions", _string_list) +_Same(_compile, "AdditionalUsingDirectories", _folder_list) # /AI +_Same(_compile, "AssemblerListingLocation", _file_name) # /Fa +_Same(_compile, "BrowseInformationFile", _file_name) +_Same(_compile, "BufferSecurityCheck", _boolean) # /GS +_Same(_compile, "DisableLanguageExtensions", _boolean) # /Za +_Same(_compile, "DisableSpecificWarnings", _string_list) # /wd +_Same(_compile, "EnableFiberSafeOptimizations", _boolean) # /GT +_Same(_compile, "EnablePREfast", _boolean) # /analyze Visible='false' +_Same(_compile, "ExpandAttributedSource", _boolean) # /Fx +_Same(_compile, "FloatingPointExceptions", _boolean) # /fp:except +_Same(_compile, "ForceConformanceInForLoopScope", _boolean) # /Zc:forScope +_Same(_compile, "ForcedIncludeFiles", _file_list) # /FI +_Same(_compile, "ForcedUsingFiles", _file_list) # /FU +_Same(_compile, "GenerateXMLDocumentationFiles", _boolean) # /doc +_Same(_compile, "IgnoreStandardIncludePath", _boolean) # /X +_Same(_compile, "MinimalRebuild", _boolean) # /Gm +_Same(_compile, "OmitDefaultLibName", _boolean) # /Zl +_Same(_compile, "OmitFramePointers", _boolean) # /Oy +_Same(_compile, "PreprocessorDefinitions", _string_list) # /D +_Same(_compile, "ProgramDataBaseFileName", _file_name) # /Fd +_Same(_compile, "RuntimeTypeInfo", _boolean) # /GR +_Same(_compile, "ShowIncludes", _boolean) # /showIncludes +_Same(_compile, "SmallerTypeCheck", _boolean) # /RTCc +_Same(_compile, "StringPooling", _boolean) # /GF +_Same(_compile, "SuppressStartupBanner", _boolean) # /nologo +_Same(_compile, "TreatWChar_tAsBuiltInType", _boolean) # /Zc:wchar_t +_Same(_compile, "UndefineAllPreprocessorDefinitions", _boolean) # /u +_Same(_compile, "UndefinePreprocessorDefinitions", _string_list) # /U +_Same(_compile, "UseFullPaths", _boolean) # /FC +_Same(_compile, "WholeProgramOptimization", _boolean) # /GL +_Same(_compile, "XMLDocumentationFileName", _file_name) +_Same(_compile, "CompileAsWinRT", _boolean) # /ZW + +_Same( + _compile, + "AssemblerOutput", + _Enumeration( + [ + "NoListing", + "AssemblyCode", # /FA + "All", # /FAcs + "AssemblyAndMachineCode", # /FAc + "AssemblyAndSourceCode", + ] + ), +) # /FAs +_Same( + _compile, + "BasicRuntimeChecks", + _Enumeration( + [ + "Default", + "StackFrameRuntimeCheck", # /RTCs + "UninitializedLocalUsageCheck", # /RTCu + "EnableFastChecks", + ] + ), +) # /RTC1 +_Same( + _compile, "BrowseInformation", _Enumeration(["false", "true", "true"]) # /FR +) # /Fr +_Same( + _compile, + "CallingConvention", + _Enumeration(["Cdecl", "FastCall", "StdCall", "VectorCall"]), # /Gd # /Gr # /Gz +) # /Gv +_Same( + _compile, + "CompileAs", + _Enumeration(["Default", "CompileAsC", "CompileAsCpp"]), # /TC +) # /TP +_Same( + _compile, + "DebugInformationFormat", + _Enumeration( + [ + "", # Disabled + "OldStyle", # /Z7 + None, + "ProgramDatabase", # /Zi + "EditAndContinue", + ] + ), +) # /ZI +_Same( + _compile, + "EnableEnhancedInstructionSet", + _Enumeration( + [ + "NotSet", + "StreamingSIMDExtensions", # /arch:SSE + "StreamingSIMDExtensions2", # /arch:SSE2 + "AdvancedVectorExtensions", # /arch:AVX (vs2012+) + "NoExtensions", # /arch:IA32 (vs2012+) + # This one only exists in the new msbuild format. + "AdvancedVectorExtensions2", # /arch:AVX2 (vs2013r2+) + ] + ), +) +_Same( + _compile, + "ErrorReporting", + _Enumeration( + [ + "None", # /errorReport:none + "Prompt", # /errorReport:prompt + "Queue", + ], # /errorReport:queue + new=["Send"], + ), +) # /errorReport:send" +_Same( + _compile, + "ExceptionHandling", + _Enumeration(["false", "Sync", "Async"], new=["SyncCThrow"]), # /EHsc # /EHa +) # /EHs +_Same( + _compile, "FavorSizeOrSpeed", _Enumeration(["Neither", "Speed", "Size"]) # /Ot +) # /Os +_Same( + _compile, + "FloatingPointModel", + _Enumeration(["Precise", "Strict", "Fast"]), # /fp:precise # /fp:strict +) # /fp:fast +_Same( + _compile, + "InlineFunctionExpansion", + _Enumeration( + ["Default", "OnlyExplicitInline", "AnySuitable"], # /Ob1 # /Ob2 + new=["Disabled"], + ), +) # /Ob0 +_Same( + _compile, + "Optimization", + _Enumeration(["Disabled", "MinSpace", "MaxSpeed", "Full"]), # /Od # /O1 # /O2 +) # /Ox +_Same( + _compile, + "RuntimeLibrary", + _Enumeration( + [ + "MultiThreaded", # /MT + "MultiThreadedDebug", # /MTd + "MultiThreadedDLL", # /MD + "MultiThreadedDebugDLL", + ] + ), +) # /MDd +_Same( + _compile, + "StructMemberAlignment", + _Enumeration( + [ + "Default", + "1Byte", # /Zp1 + "2Bytes", # /Zp2 + "4Bytes", # /Zp4 + "8Bytes", # /Zp8 + "16Bytes", + ] + ), +) # /Zp16 +_Same( + _compile, + "WarningLevel", + _Enumeration( + [ + "TurnOffAllWarnings", # /W0 + "Level1", # /W1 + "Level2", # /W2 + "Level3", # /W3 + "Level4", + ], # /W4 + new=["EnableAllWarnings"], + ), +) # /Wall # Options found in MSVS that have been renamed in MSBuild. -_Renamed(_compile, 'EnableFunctionLevelLinking', 'FunctionLevelLinking', - _boolean) # /Gy -_Renamed(_compile, 'EnableIntrinsicFunctions', 'IntrinsicFunctions', - _boolean) # /Oi -_Renamed(_compile, 'KeepComments', 'PreprocessKeepComments', _boolean) # /C -_Renamed(_compile, 'ObjectFile', 'ObjectFileName', _file_name) # /Fo -_Renamed(_compile, 'OpenMP', 'OpenMPSupport', _boolean) # /openmp -_Renamed(_compile, 'PrecompiledHeaderThrough', 'PrecompiledHeaderFile', - _file_name) # Used with /Yc and /Yu -_Renamed(_compile, 'PrecompiledHeaderFile', 'PrecompiledHeaderOutputFile', - _file_name) # /Fp -_Renamed(_compile, 'UsePrecompiledHeader', 'PrecompiledHeader', - _Enumeration(['NotUsing', # VS recognized '' for this value too. - 'Create', # /Yc - 'Use'])) # /Yu -_Renamed(_compile, 'WarnAsError', 'TreatWarningAsError', _boolean) # /WX - -_ConvertedToAdditionalOption(_compile, 'DefaultCharIsUnsigned', '/J') +_Renamed( + _compile, "EnableFunctionLevelLinking", "FunctionLevelLinking", _boolean +) # /Gy +_Renamed(_compile, "EnableIntrinsicFunctions", "IntrinsicFunctions", _boolean) # /Oi +_Renamed(_compile, "KeepComments", "PreprocessKeepComments", _boolean) # /C +_Renamed(_compile, "ObjectFile", "ObjectFileName", _file_name) # /Fo +_Renamed(_compile, "OpenMP", "OpenMPSupport", _boolean) # /openmp +_Renamed( + _compile, "PrecompiledHeaderThrough", "PrecompiledHeaderFile", _file_name +) # Used with /Yc and /Yu +_Renamed( + _compile, "PrecompiledHeaderFile", "PrecompiledHeaderOutputFile", _file_name +) # /Fp +_Renamed( + _compile, + "UsePrecompiledHeader", + "PrecompiledHeader", + _Enumeration( + ["NotUsing", "Create", "Use"] # VS recognized '' for this value too. # /Yc + ), +) # /Yu +_Renamed(_compile, "WarnAsError", "TreatWarningAsError", _boolean) # /WX + +_ConvertedToAdditionalOption(_compile, "DefaultCharIsUnsigned", "/J") # MSVS options not found in MSBuild. -_MSVSOnly(_compile, 'Detect64BitPortabilityProblems', _boolean) -_MSVSOnly(_compile, 'UseUnicodeResponseFiles', _boolean) +_MSVSOnly(_compile, "Detect64BitPortabilityProblems", _boolean) +_MSVSOnly(_compile, "UseUnicodeResponseFiles", _boolean) # MSBuild options not found in MSVS. -_MSBuildOnly(_compile, 'BuildingInIDE', _boolean) -_MSBuildOnly(_compile, 'CompileAsManaged', - _Enumeration([], new=['false', - 'true'])) # /clr -_MSBuildOnly(_compile, 'CreateHotpatchableImage', _boolean) # /hotpatch -_MSBuildOnly(_compile, 'MultiProcessorCompilation', _boolean) # /MP -_MSBuildOnly(_compile, 'PreprocessOutputPath', _string) # /Fi -_MSBuildOnly(_compile, 'ProcessorNumber', _integer) # the number of processors -_MSBuildOnly(_compile, 'TrackerLogDirectory', _folder_name) -_MSBuildOnly(_compile, 'TreatSpecificWarningsAsErrors', _string_list) # /we -_MSBuildOnly(_compile, 'UseUnicodeForAssemblerListing', _boolean) # /FAu +_MSBuildOnly(_compile, "BuildingInIDE", _boolean) +_MSBuildOnly( + _compile, "CompileAsManaged", _Enumeration([], new=["false", "true"]) +) # /clr +_MSBuildOnly(_compile, "CreateHotpatchableImage", _boolean) # /hotpatch +_MSBuildOnly(_compile, "MultiProcessorCompilation", _boolean) # /MP +_MSBuildOnly(_compile, "PreprocessOutputPath", _string) # /Fi +_MSBuildOnly(_compile, "ProcessorNumber", _integer) # the number of processors +_MSBuildOnly(_compile, "TrackerLogDirectory", _folder_name) +_MSBuildOnly(_compile, "TreatSpecificWarningsAsErrors", _string_list) # /we +_MSBuildOnly(_compile, "UseUnicodeForAssemblerListing", _boolean) # /FAu # Defines a setting that needs very customized processing -_CustomGeneratePreprocessedFile(_compile, 'GeneratePreprocessedFile') +_CustomGeneratePreprocessedFile(_compile, "GeneratePreprocessedFile") # Directives for converting MSVS VCLinkerTool to MSBuild Link. @@ -733,326 +813,411 @@ def _ValidateSettings(validators, settings, stderr): # the schema of the MSBuild Link settings. # Options that have the same name in MSVS and MSBuild -_Same(_link, 'AdditionalDependencies', _file_list) -_Same(_link, 'AdditionalLibraryDirectories', _folder_list) # /LIBPATH +_Same(_link, "AdditionalDependencies", _file_list) +_Same(_link, "AdditionalLibraryDirectories", _folder_list) # /LIBPATH # /MANIFESTDEPENDENCY: -_Same(_link, 'AdditionalManifestDependencies', _file_list) -_Same(_link, 'AdditionalOptions', _string_list) -_Same(_link, 'AddModuleNamesToAssembly', _file_list) # /ASSEMBLYMODULE -_Same(_link, 'AllowIsolation', _boolean) # /ALLOWISOLATION -_Same(_link, 'AssemblyLinkResource', _file_list) # /ASSEMBLYLINKRESOURCE -_Same(_link, 'BaseAddress', _string) # /BASE -_Same(_link, 'CLRUnmanagedCodeCheck', _boolean) # /CLRUNMANAGEDCODECHECK -_Same(_link, 'DelayLoadDLLs', _file_list) # /DELAYLOAD -_Same(_link, 'DelaySign', _boolean) # /DELAYSIGN -_Same(_link, 'EmbedManagedResourceFile', _file_list) # /ASSEMBLYRESOURCE -_Same(_link, 'EnableUAC', _boolean) # /MANIFESTUAC -_Same(_link, 'EntryPointSymbol', _string) # /ENTRY -_Same(_link, 'ForceSymbolReferences', _file_list) # /INCLUDE -_Same(_link, 'FunctionOrder', _file_name) # /ORDER -_Same(_link, 'GenerateDebugInformation', _boolean) # /DEBUG -_Same(_link, 'GenerateMapFile', _boolean) # /MAP -_Same(_link, 'HeapCommitSize', _string) -_Same(_link, 'HeapReserveSize', _string) # /HEAP -_Same(_link, 'IgnoreAllDefaultLibraries', _boolean) # /NODEFAULTLIB -_Same(_link, 'IgnoreEmbeddedIDL', _boolean) # /IGNOREIDL -_Same(_link, 'ImportLibrary', _file_name) # /IMPLIB -_Same(_link, 'KeyContainer', _file_name) # /KEYCONTAINER -_Same(_link, 'KeyFile', _file_name) # /KEYFILE -_Same(_link, 'ManifestFile', _file_name) # /ManifestFile -_Same(_link, 'MapExports', _boolean) # /MAPINFO:EXPORTS -_Same(_link, 'MapFileName', _file_name) -_Same(_link, 'MergedIDLBaseFileName', _file_name) # /IDLOUT -_Same(_link, 'MergeSections', _string) # /MERGE -_Same(_link, 'MidlCommandFile', _file_name) # /MIDL -_Same(_link, 'ModuleDefinitionFile', _file_name) # /DEF -_Same(_link, 'OutputFile', _file_name) # /OUT -_Same(_link, 'PerUserRedirection', _boolean) -_Same(_link, 'Profile', _boolean) # /PROFILE -_Same(_link, 'ProfileGuidedDatabase', _file_name) # /PGD -_Same(_link, 'ProgramDatabaseFile', _file_name) # /PDB -_Same(_link, 'RegisterOutput', _boolean) -_Same(_link, 'SetChecksum', _boolean) # /RELEASE -_Same(_link, 'StackCommitSize', _string) -_Same(_link, 'StackReserveSize', _string) # /STACK -_Same(_link, 'StripPrivateSymbols', _file_name) # /PDBSTRIPPED -_Same(_link, 'SupportUnloadOfDelayLoadedDLL', _boolean) # /DELAY:UNLOAD -_Same(_link, 'SuppressStartupBanner', _boolean) # /NOLOGO -_Same(_link, 'SwapRunFromCD', _boolean) # /SWAPRUN:CD -_Same(_link, 'TurnOffAssemblyGeneration', _boolean) # /NOASSEMBLY -_Same(_link, 'TypeLibraryFile', _file_name) # /TLBOUT -_Same(_link, 'TypeLibraryResourceID', _integer) # /TLBID -_Same(_link, 'UACUIAccess', _boolean) # /uiAccess='true' -_Same(_link, 'Version', _string) # /VERSION - -_Same(_link, 'EnableCOMDATFolding', _newly_boolean) # /OPT:ICF -_Same(_link, 'FixedBaseAddress', _newly_boolean) # /FIXED -_Same(_link, 'LargeAddressAware', _newly_boolean) # /LARGEADDRESSAWARE -_Same(_link, 'OptimizeReferences', _newly_boolean) # /OPT:REF -_Same(_link, 'RandomizedBaseAddress', _newly_boolean) # /DYNAMICBASE -_Same(_link, 'TerminalServerAware', _newly_boolean) # /TSAWARE +_Same(_link, "AdditionalManifestDependencies", _file_list) +_Same(_link, "AdditionalOptions", _string_list) +_Same(_link, "AddModuleNamesToAssembly", _file_list) # /ASSEMBLYMODULE +_Same(_link, "AllowIsolation", _boolean) # /ALLOWISOLATION +_Same(_link, "AssemblyLinkResource", _file_list) # /ASSEMBLYLINKRESOURCE +_Same(_link, "BaseAddress", _string) # /BASE +_Same(_link, "CLRUnmanagedCodeCheck", _boolean) # /CLRUNMANAGEDCODECHECK +_Same(_link, "DelayLoadDLLs", _file_list) # /DELAYLOAD +_Same(_link, "DelaySign", _boolean) # /DELAYSIGN +_Same(_link, "EmbedManagedResourceFile", _file_list) # /ASSEMBLYRESOURCE +_Same(_link, "EnableUAC", _boolean) # /MANIFESTUAC +_Same(_link, "EntryPointSymbol", _string) # /ENTRY +_Same(_link, "ForceSymbolReferences", _file_list) # /INCLUDE +_Same(_link, "FunctionOrder", _file_name) # /ORDER +_Same(_link, "GenerateDebugInformation", _boolean) # /DEBUG +_Same(_link, "GenerateMapFile", _boolean) # /MAP +_Same(_link, "HeapCommitSize", _string) +_Same(_link, "HeapReserveSize", _string) # /HEAP +_Same(_link, "IgnoreAllDefaultLibraries", _boolean) # /NODEFAULTLIB +_Same(_link, "IgnoreEmbeddedIDL", _boolean) # /IGNOREIDL +_Same(_link, "ImportLibrary", _file_name) # /IMPLIB +_Same(_link, "KeyContainer", _file_name) # /KEYCONTAINER +_Same(_link, "KeyFile", _file_name) # /KEYFILE +_Same(_link, "ManifestFile", _file_name) # /ManifestFile +_Same(_link, "MapExports", _boolean) # /MAPINFO:EXPORTS +_Same(_link, "MapFileName", _file_name) +_Same(_link, "MergedIDLBaseFileName", _file_name) # /IDLOUT +_Same(_link, "MergeSections", _string) # /MERGE +_Same(_link, "MidlCommandFile", _file_name) # /MIDL +_Same(_link, "ModuleDefinitionFile", _file_name) # /DEF +_Same(_link, "OutputFile", _file_name) # /OUT +_Same(_link, "PerUserRedirection", _boolean) +_Same(_link, "Profile", _boolean) # /PROFILE +_Same(_link, "ProfileGuidedDatabase", _file_name) # /PGD +_Same(_link, "ProgramDatabaseFile", _file_name) # /PDB +_Same(_link, "RegisterOutput", _boolean) +_Same(_link, "SetChecksum", _boolean) # /RELEASE +_Same(_link, "StackCommitSize", _string) +_Same(_link, "StackReserveSize", _string) # /STACK +_Same(_link, "StripPrivateSymbols", _file_name) # /PDBSTRIPPED +_Same(_link, "SupportUnloadOfDelayLoadedDLL", _boolean) # /DELAY:UNLOAD +_Same(_link, "SuppressStartupBanner", _boolean) # /NOLOGO +_Same(_link, "SwapRunFromCD", _boolean) # /SWAPRUN:CD +_Same(_link, "TurnOffAssemblyGeneration", _boolean) # /NOASSEMBLY +_Same(_link, "TypeLibraryFile", _file_name) # /TLBOUT +_Same(_link, "TypeLibraryResourceID", _integer) # /TLBID +_Same(_link, "UACUIAccess", _boolean) # /uiAccess='true' +_Same(_link, "Version", _string) # /VERSION + +_Same(_link, "EnableCOMDATFolding", _newly_boolean) # /OPT:ICF +_Same(_link, "FixedBaseAddress", _newly_boolean) # /FIXED +_Same(_link, "LargeAddressAware", _newly_boolean) # /LARGEADDRESSAWARE +_Same(_link, "OptimizeReferences", _newly_boolean) # /OPT:REF +_Same(_link, "RandomizedBaseAddress", _newly_boolean) # /DYNAMICBASE +_Same(_link, "TerminalServerAware", _newly_boolean) # /TSAWARE _subsystem_enumeration = _Enumeration( - ['NotSet', - 'Console', # /SUBSYSTEM:CONSOLE - 'Windows', # /SUBSYSTEM:WINDOWS - 'Native', # /SUBSYSTEM:NATIVE - 'EFI Application', # /SUBSYSTEM:EFI_APPLICATION - 'EFI Boot Service Driver', # /SUBSYSTEM:EFI_BOOT_SERVICE_DRIVER - 'EFI ROM', # /SUBSYSTEM:EFI_ROM - 'EFI Runtime', # /SUBSYSTEM:EFI_RUNTIME_DRIVER - 'WindowsCE'], # /SUBSYSTEM:WINDOWSCE - new=['POSIX']) # /SUBSYSTEM:POSIX + [ + "NotSet", + "Console", # /SUBSYSTEM:CONSOLE + "Windows", # /SUBSYSTEM:WINDOWS + "Native", # /SUBSYSTEM:NATIVE + "EFI Application", # /SUBSYSTEM:EFI_APPLICATION + "EFI Boot Service Driver", # /SUBSYSTEM:EFI_BOOT_SERVICE_DRIVER + "EFI ROM", # /SUBSYSTEM:EFI_ROM + "EFI Runtime", # /SUBSYSTEM:EFI_RUNTIME_DRIVER + "WindowsCE", + ], # /SUBSYSTEM:WINDOWSCE + new=["POSIX"], +) # /SUBSYSTEM:POSIX _target_machine_enumeration = _Enumeration( - ['NotSet', - 'MachineX86', # /MACHINE:X86 - None, - 'MachineARM', # /MACHINE:ARM - 'MachineEBC', # /MACHINE:EBC - 'MachineIA64', # /MACHINE:IA64 - None, - 'MachineMIPS', # /MACHINE:MIPS - 'MachineMIPS16', # /MACHINE:MIPS16 - 'MachineMIPSFPU', # /MACHINE:MIPSFPU - 'MachineMIPSFPU16', # /MACHINE:MIPSFPU16 - None, - None, - None, - 'MachineSH4', # /MACHINE:SH4 - None, - 'MachineTHUMB', # /MACHINE:THUMB - 'MachineX64']) # /MACHINE:X64 - -_Same(_link, 'AssemblyDebug', - _Enumeration(['', - 'true', # /ASSEMBLYDEBUG - 'false'])) # /ASSEMBLYDEBUG:DISABLE -_Same(_link, 'CLRImageType', - _Enumeration(['Default', - 'ForceIJWImage', # /CLRIMAGETYPE:IJW - 'ForcePureILImage', # /Switch="CLRIMAGETYPE:PURE - 'ForceSafeILImage'])) # /Switch="CLRIMAGETYPE:SAFE -_Same(_link, 'CLRThreadAttribute', - _Enumeration(['DefaultThreadingAttribute', # /CLRTHREADATTRIBUTE:NONE - 'MTAThreadingAttribute', # /CLRTHREADATTRIBUTE:MTA - 'STAThreadingAttribute'])) # /CLRTHREADATTRIBUTE:STA -_Same(_link, 'DataExecutionPrevention', - _Enumeration(['', - 'false', # /NXCOMPAT:NO - 'true'])) # /NXCOMPAT -_Same(_link, 'Driver', - _Enumeration(['NotSet', - 'Driver', # /Driver - 'UpOnly', # /DRIVER:UPONLY - 'WDM'])) # /DRIVER:WDM -_Same(_link, 'LinkTimeCodeGeneration', - _Enumeration(['Default', - 'UseLinkTimeCodeGeneration', # /LTCG - 'PGInstrument', # /LTCG:PGInstrument - 'PGOptimization', # /LTCG:PGOptimize - 'PGUpdate'])) # /LTCG:PGUpdate -_Same(_link, 'ShowProgress', - _Enumeration(['NotSet', - 'LinkVerbose', # /VERBOSE - 'LinkVerboseLib'], # /VERBOSE:Lib - new=['LinkVerboseICF', # /VERBOSE:ICF - 'LinkVerboseREF', # /VERBOSE:REF - 'LinkVerboseSAFESEH', # /VERBOSE:SAFESEH - 'LinkVerboseCLR'])) # /VERBOSE:CLR -_Same(_link, 'SubSystem', _subsystem_enumeration) -_Same(_link, 'TargetMachine', _target_machine_enumeration) -_Same(_link, 'UACExecutionLevel', - _Enumeration(['AsInvoker', # /level='asInvoker' - 'HighestAvailable', # /level='highestAvailable' - 'RequireAdministrator'])) # /level='requireAdministrator' -_Same(_link, 'MinimumRequiredVersion', _string) -_Same(_link, 'TreatLinkerWarningAsErrors', _boolean) # /WX + [ + "NotSet", + "MachineX86", # /MACHINE:X86 + None, + "MachineARM", # /MACHINE:ARM + "MachineEBC", # /MACHINE:EBC + "MachineIA64", # /MACHINE:IA64 + None, + "MachineMIPS", # /MACHINE:MIPS + "MachineMIPS16", # /MACHINE:MIPS16 + "MachineMIPSFPU", # /MACHINE:MIPSFPU + "MachineMIPSFPU16", # /MACHINE:MIPSFPU16 + None, + None, + None, + "MachineSH4", # /MACHINE:SH4 + None, + "MachineTHUMB", # /MACHINE:THUMB + "MachineX64", + ] +) # /MACHINE:X64 + +_Same( + _link, "AssemblyDebug", _Enumeration(["", "true", "false"]) # /ASSEMBLYDEBUG +) # /ASSEMBLYDEBUG:DISABLE +_Same( + _link, + "CLRImageType", + _Enumeration( + [ + "Default", + "ForceIJWImage", # /CLRIMAGETYPE:IJW + "ForcePureILImage", # /Switch="CLRIMAGETYPE:PURE + "ForceSafeILImage", + ] + ), +) # /Switch="CLRIMAGETYPE:SAFE +_Same( + _link, + "CLRThreadAttribute", + _Enumeration( + [ + "DefaultThreadingAttribute", # /CLRTHREADATTRIBUTE:NONE + "MTAThreadingAttribute", # /CLRTHREADATTRIBUTE:MTA + "STAThreadingAttribute", + ] + ), +) # /CLRTHREADATTRIBUTE:STA +_Same( + _link, + "DataExecutionPrevention", + _Enumeration(["", "false", "true"]), # /NXCOMPAT:NO +) # /NXCOMPAT +_Same( + _link, + "Driver", + _Enumeration(["NotSet", "Driver", "UpOnly", "WDM"]), # /Driver # /DRIVER:UPONLY +) # /DRIVER:WDM +_Same( + _link, + "LinkTimeCodeGeneration", + _Enumeration( + [ + "Default", + "UseLinkTimeCodeGeneration", # /LTCG + "PGInstrument", # /LTCG:PGInstrument + "PGOptimization", # /LTCG:PGOptimize + "PGUpdate", + ] + ), +) # /LTCG:PGUpdate +_Same( + _link, + "ShowProgress", + _Enumeration( + ["NotSet", "LinkVerbose", "LinkVerboseLib"], # /VERBOSE # /VERBOSE:Lib + new=[ + "LinkVerboseICF", # /VERBOSE:ICF + "LinkVerboseREF", # /VERBOSE:REF + "LinkVerboseSAFESEH", # /VERBOSE:SAFESEH + "LinkVerboseCLR", + ], + ), +) # /VERBOSE:CLR +_Same(_link, "SubSystem", _subsystem_enumeration) +_Same(_link, "TargetMachine", _target_machine_enumeration) +_Same( + _link, + "UACExecutionLevel", + _Enumeration( + [ + "AsInvoker", # /level='asInvoker' + "HighestAvailable", # /level='highestAvailable' + "RequireAdministrator", + ] + ), +) # /level='requireAdministrator' +_Same(_link, "MinimumRequiredVersion", _string) +_Same(_link, "TreatLinkerWarningAsErrors", _boolean) # /WX # Options found in MSVS that have been renamed in MSBuild. -_Renamed(_link, 'ErrorReporting', 'LinkErrorReporting', - _Enumeration(['NoErrorReport', # /ERRORREPORT:NONE - 'PromptImmediately', # /ERRORREPORT:PROMPT - 'QueueForNextLogin'], # /ERRORREPORT:QUEUE - new=['SendErrorReport'])) # /ERRORREPORT:SEND -_Renamed(_link, 'IgnoreDefaultLibraryNames', 'IgnoreSpecificDefaultLibraries', - _file_list) # /NODEFAULTLIB -_Renamed(_link, 'ResourceOnlyDLL', 'NoEntryPoint', _boolean) # /NOENTRY -_Renamed(_link, 'SwapRunFromNet', 'SwapRunFromNET', _boolean) # /SWAPRUN:NET - -_Moved(_link, 'GenerateManifest', '', _boolean) -_Moved(_link, 'IgnoreImportLibrary', '', _boolean) -_Moved(_link, 'LinkIncremental', '', _newly_boolean) -_Moved(_link, 'LinkLibraryDependencies', 'ProjectReference', _boolean) -_Moved(_link, 'UseLibraryDependencyInputs', 'ProjectReference', _boolean) +_Renamed( + _link, + "ErrorReporting", + "LinkErrorReporting", + _Enumeration( + [ + "NoErrorReport", # /ERRORREPORT:NONE + "PromptImmediately", # /ERRORREPORT:PROMPT + "QueueForNextLogin", + ], # /ERRORREPORT:QUEUE + new=["SendErrorReport"], + ), +) # /ERRORREPORT:SEND +_Renamed( + _link, "IgnoreDefaultLibraryNames", "IgnoreSpecificDefaultLibraries", _file_list +) # /NODEFAULTLIB +_Renamed(_link, "ResourceOnlyDLL", "NoEntryPoint", _boolean) # /NOENTRY +_Renamed(_link, "SwapRunFromNet", "SwapRunFromNET", _boolean) # /SWAPRUN:NET + +_Moved(_link, "GenerateManifest", "", _boolean) +_Moved(_link, "IgnoreImportLibrary", "", _boolean) +_Moved(_link, "LinkIncremental", "", _newly_boolean) +_Moved(_link, "LinkLibraryDependencies", "ProjectReference", _boolean) +_Moved(_link, "UseLibraryDependencyInputs", "ProjectReference", _boolean) # MSVS options not found in MSBuild. -_MSVSOnly(_link, 'OptimizeForWindows98', _newly_boolean) -_MSVSOnly(_link, 'UseUnicodeResponseFiles', _boolean) +_MSVSOnly(_link, "OptimizeForWindows98", _newly_boolean) +_MSVSOnly(_link, "UseUnicodeResponseFiles", _boolean) # MSBuild options not found in MSVS. -_MSBuildOnly(_link, 'BuildingInIDE', _boolean) -_MSBuildOnly(_link, 'ImageHasSafeExceptionHandlers', _boolean) # /SAFESEH -_MSBuildOnly(_link, 'LinkDLL', _boolean) # /DLL Visible='false' -_MSBuildOnly(_link, 'LinkStatus', _boolean) # /LTCG:STATUS -_MSBuildOnly(_link, 'PreventDllBinding', _boolean) # /ALLOWBIND -_MSBuildOnly(_link, 'SupportNobindOfDelayLoadedDLL', _boolean) # /DELAY:NOBIND -_MSBuildOnly(_link, 'TrackerLogDirectory', _folder_name) -_MSBuildOnly(_link, 'MSDOSStubFileName', _file_name) # /STUB Visible='false' -_MSBuildOnly(_link, 'SectionAlignment', _integer) # /ALIGN -_MSBuildOnly(_link, 'SpecifySectionAttributes', _string) # /SECTION -_MSBuildOnly(_link, 'ForceFileOutput', - _Enumeration([], new=['Enabled', # /FORCE - # /FORCE:MULTIPLE - 'MultiplyDefinedSymbolOnly', - 'UndefinedSymbolOnly'])) # /FORCE:UNRESOLVED -_MSBuildOnly(_link, 'CreateHotPatchableImage', - _Enumeration([], new=['Enabled', # /FUNCTIONPADMIN - 'X86Image', # /FUNCTIONPADMIN:5 - 'X64Image', # /FUNCTIONPADMIN:6 - 'ItaniumImage'])) # /FUNCTIONPADMIN:16 -_MSBuildOnly(_link, 'CLRSupportLastError', - _Enumeration([], new=['Enabled', # /CLRSupportLastError - 'Disabled', # /CLRSupportLastError:NO - # /CLRSupportLastError:SYSTEMDLL - 'SystemDlls'])) +_MSBuildOnly(_link, "BuildingInIDE", _boolean) +_MSBuildOnly(_link, "ImageHasSafeExceptionHandlers", _boolean) # /SAFESEH +_MSBuildOnly(_link, "LinkDLL", _boolean) # /DLL Visible='false' +_MSBuildOnly(_link, "LinkStatus", _boolean) # /LTCG:STATUS +_MSBuildOnly(_link, "PreventDllBinding", _boolean) # /ALLOWBIND +_MSBuildOnly(_link, "SupportNobindOfDelayLoadedDLL", _boolean) # /DELAY:NOBIND +_MSBuildOnly(_link, "TrackerLogDirectory", _folder_name) +_MSBuildOnly(_link, "MSDOSStubFileName", _file_name) # /STUB Visible='false' +_MSBuildOnly(_link, "SectionAlignment", _integer) # /ALIGN +_MSBuildOnly(_link, "SpecifySectionAttributes", _string) # /SECTION +_MSBuildOnly( + _link, + "ForceFileOutput", + _Enumeration( + [], + new=[ + "Enabled", # /FORCE + # /FORCE:MULTIPLE + "MultiplyDefinedSymbolOnly", + "UndefinedSymbolOnly", + ], + ), +) # /FORCE:UNRESOLVED +_MSBuildOnly( + _link, + "CreateHotPatchableImage", + _Enumeration( + [], + new=[ + "Enabled", # /FUNCTIONPADMIN + "X86Image", # /FUNCTIONPADMIN:5 + "X64Image", # /FUNCTIONPADMIN:6 + "ItaniumImage", + ], + ), +) # /FUNCTIONPADMIN:16 +_MSBuildOnly( + _link, + "CLRSupportLastError", + _Enumeration( + [], + new=[ + "Enabled", # /CLRSupportLastError + "Disabled", # /CLRSupportLastError:NO + # /CLRSupportLastError:SYSTEMDLL + "SystemDlls", + ], + ), +) # Directives for converting VCResourceCompilerTool to ResourceCompile. # See "c:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\1033\rc.xml" for # the schema of the MSBuild ResourceCompile settings. -_Same(_rc, 'AdditionalOptions', _string_list) -_Same(_rc, 'AdditionalIncludeDirectories', _folder_list) # /I -_Same(_rc, 'Culture', _Integer(msbuild_base=16)) -_Same(_rc, 'IgnoreStandardIncludePath', _boolean) # /X -_Same(_rc, 'PreprocessorDefinitions', _string_list) # /D -_Same(_rc, 'ResourceOutputFileName', _string) # /fo -_Same(_rc, 'ShowProgress', _boolean) # /v +_Same(_rc, "AdditionalOptions", _string_list) +_Same(_rc, "AdditionalIncludeDirectories", _folder_list) # /I +_Same(_rc, "Culture", _Integer(msbuild_base=16)) +_Same(_rc, "IgnoreStandardIncludePath", _boolean) # /X +_Same(_rc, "PreprocessorDefinitions", _string_list) # /D +_Same(_rc, "ResourceOutputFileName", _string) # /fo +_Same(_rc, "ShowProgress", _boolean) # /v # There is no UI in VisualStudio 2008 to set the following properties. # However they are found in CL and other tools. Include them here for # completeness, as they are very likely to have the same usage pattern. -_Same(_rc, 'SuppressStartupBanner', _boolean) # /nologo -_Same(_rc, 'UndefinePreprocessorDefinitions', _string_list) # /u +_Same(_rc, "SuppressStartupBanner", _boolean) # /nologo +_Same(_rc, "UndefinePreprocessorDefinitions", _string_list) # /u # MSBuild options not found in MSVS. -_MSBuildOnly(_rc, 'NullTerminateStrings', _boolean) # /n -_MSBuildOnly(_rc, 'TrackerLogDirectory', _folder_name) +_MSBuildOnly(_rc, "NullTerminateStrings", _boolean) # /n +_MSBuildOnly(_rc, "TrackerLogDirectory", _folder_name) # Directives for converting VCMIDLTool to Midl. # See "c:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\1033\midl.xml" for # the schema of the MSBuild Midl settings. -_Same(_midl, 'AdditionalIncludeDirectories', _folder_list) # /I -_Same(_midl, 'AdditionalOptions', _string_list) -_Same(_midl, 'CPreprocessOptions', _string) # /cpp_opt -_Same(_midl, 'ErrorCheckAllocations', _boolean) # /error allocation -_Same(_midl, 'ErrorCheckBounds', _boolean) # /error bounds_check -_Same(_midl, 'ErrorCheckEnumRange', _boolean) # /error enum -_Same(_midl, 'ErrorCheckRefPointers', _boolean) # /error ref -_Same(_midl, 'ErrorCheckStubData', _boolean) # /error stub_data -_Same(_midl, 'GenerateStublessProxies', _boolean) # /Oicf -_Same(_midl, 'GenerateTypeLibrary', _boolean) -_Same(_midl, 'HeaderFileName', _file_name) # /h -_Same(_midl, 'IgnoreStandardIncludePath', _boolean) # /no_def_idir -_Same(_midl, 'InterfaceIdentifierFileName', _file_name) # /iid -_Same(_midl, 'MkTypLibCompatible', _boolean) # /mktyplib203 -_Same(_midl, 'OutputDirectory', _string) # /out -_Same(_midl, 'PreprocessorDefinitions', _string_list) # /D -_Same(_midl, 'ProxyFileName', _file_name) # /proxy -_Same(_midl, 'RedirectOutputAndErrors', _file_name) # /o -_Same(_midl, 'SuppressStartupBanner', _boolean) # /nologo -_Same(_midl, 'TypeLibraryName', _file_name) # /tlb -_Same(_midl, 'UndefinePreprocessorDefinitions', _string_list) # /U -_Same(_midl, 'WarnAsError', _boolean) # /WX - -_Same(_midl, 'DefaultCharType', - _Enumeration(['Unsigned', # /char unsigned - 'Signed', # /char signed - 'Ascii'])) # /char ascii7 -_Same(_midl, 'TargetEnvironment', - _Enumeration(['NotSet', - 'Win32', # /env win32 - 'Itanium', # /env ia64 - 'X64', # /env x64 - 'ARM64', # /env arm64 - ])) -_Same(_midl, 'EnableErrorChecks', - _Enumeration(['EnableCustom', - 'None', # /error none - 'All'])) # /error all -_Same(_midl, 'StructMemberAlignment', - _Enumeration(['NotSet', - '1', # Zp1 - '2', # Zp2 - '4', # Zp4 - '8'])) # Zp8 -_Same(_midl, 'WarningLevel', - _Enumeration(['0', # /W0 - '1', # /W1 - '2', # /W2 - '3', # /W3 - '4'])) # /W4 - -_Renamed(_midl, 'DLLDataFileName', 'DllDataFileName', _file_name) # /dlldata -_Renamed(_midl, 'ValidateParameters', 'ValidateAllParameters', - _boolean) # /robust +_Same(_midl, "AdditionalIncludeDirectories", _folder_list) # /I +_Same(_midl, "AdditionalOptions", _string_list) +_Same(_midl, "CPreprocessOptions", _string) # /cpp_opt +_Same(_midl, "ErrorCheckAllocations", _boolean) # /error allocation +_Same(_midl, "ErrorCheckBounds", _boolean) # /error bounds_check +_Same(_midl, "ErrorCheckEnumRange", _boolean) # /error enum +_Same(_midl, "ErrorCheckRefPointers", _boolean) # /error ref +_Same(_midl, "ErrorCheckStubData", _boolean) # /error stub_data +_Same(_midl, "GenerateStublessProxies", _boolean) # /Oicf +_Same(_midl, "GenerateTypeLibrary", _boolean) +_Same(_midl, "HeaderFileName", _file_name) # /h +_Same(_midl, "IgnoreStandardIncludePath", _boolean) # /no_def_idir +_Same(_midl, "InterfaceIdentifierFileName", _file_name) # /iid +_Same(_midl, "MkTypLibCompatible", _boolean) # /mktyplib203 +_Same(_midl, "OutputDirectory", _string) # /out +_Same(_midl, "PreprocessorDefinitions", _string_list) # /D +_Same(_midl, "ProxyFileName", _file_name) # /proxy +_Same(_midl, "RedirectOutputAndErrors", _file_name) # /o +_Same(_midl, "SuppressStartupBanner", _boolean) # /nologo +_Same(_midl, "TypeLibraryName", _file_name) # /tlb +_Same(_midl, "UndefinePreprocessorDefinitions", _string_list) # /U +_Same(_midl, "WarnAsError", _boolean) # /WX + +_Same( + _midl, + "DefaultCharType", + _Enumeration(["Unsigned", "Signed", "Ascii"]), # /char unsigned # /char signed +) # /char ascii7 +_Same( + _midl, + "TargetEnvironment", + _Enumeration( + [ + "NotSet", + "Win32", # /env win32 + "Itanium", # /env ia64 + "X64", # /env x64 + "ARM64", # /env arm64 + ] + ), +) +_Same( + _midl, + "EnableErrorChecks", + _Enumeration(["EnableCustom", "None", "All"]), # /error none +) # /error all +_Same( + _midl, + "StructMemberAlignment", + _Enumeration(["NotSet", "1", "2", "4", "8"]), # Zp1 # Zp2 # Zp4 +) # Zp8 +_Same( + _midl, + "WarningLevel", + _Enumeration(["0", "1", "2", "3", "4"]), # /W0 # /W1 # /W2 # /W3 +) # /W4 + +_Renamed(_midl, "DLLDataFileName", "DllDataFileName", _file_name) # /dlldata +_Renamed(_midl, "ValidateParameters", "ValidateAllParameters", _boolean) # /robust # MSBuild options not found in MSVS. -_MSBuildOnly(_midl, 'ApplicationConfigurationMode', _boolean) # /app_config -_MSBuildOnly(_midl, 'ClientStubFile', _file_name) # /cstub -_MSBuildOnly(_midl, 'GenerateClientFiles', - _Enumeration([], new=['Stub', # /client stub - 'None'])) # /client none -_MSBuildOnly(_midl, 'GenerateServerFiles', - _Enumeration([], new=['Stub', # /client stub - 'None'])) # /client none -_MSBuildOnly(_midl, 'LocaleID', _integer) # /lcid DECIMAL -_MSBuildOnly(_midl, 'ServerStubFile', _file_name) # /sstub -_MSBuildOnly(_midl, 'SuppressCompilerWarnings', _boolean) # /no_warn -_MSBuildOnly(_midl, 'TrackerLogDirectory', _folder_name) -_MSBuildOnly(_midl, 'TypeLibFormat', - _Enumeration([], new=['NewFormat', # /newtlb - 'OldFormat'])) # /oldtlb +_MSBuildOnly(_midl, "ApplicationConfigurationMode", _boolean) # /app_config +_MSBuildOnly(_midl, "ClientStubFile", _file_name) # /cstub +_MSBuildOnly( + _midl, "GenerateClientFiles", _Enumeration([], new=["Stub", "None"]) # /client stub +) # /client none +_MSBuildOnly( + _midl, "GenerateServerFiles", _Enumeration([], new=["Stub", "None"]) # /client stub +) # /client none +_MSBuildOnly(_midl, "LocaleID", _integer) # /lcid DECIMAL +_MSBuildOnly(_midl, "ServerStubFile", _file_name) # /sstub +_MSBuildOnly(_midl, "SuppressCompilerWarnings", _boolean) # /no_warn +_MSBuildOnly(_midl, "TrackerLogDirectory", _folder_name) +_MSBuildOnly( + _midl, "TypeLibFormat", _Enumeration([], new=["NewFormat", "OldFormat"]) # /newtlb +) # /oldtlb # Directives for converting VCLibrarianTool to Lib. # See "c:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\1033\lib.xml" for # the schema of the MSBuild Lib settings. -_Same(_lib, 'AdditionalDependencies', _file_list) -_Same(_lib, 'AdditionalLibraryDirectories', _folder_list) # /LIBPATH -_Same(_lib, 'AdditionalOptions', _string_list) -_Same(_lib, 'ExportNamedFunctions', _string_list) # /EXPORT -_Same(_lib, 'ForceSymbolReferences', _string) # /INCLUDE -_Same(_lib, 'IgnoreAllDefaultLibraries', _boolean) # /NODEFAULTLIB -_Same(_lib, 'IgnoreSpecificDefaultLibraries', _file_list) # /NODEFAULTLIB -_Same(_lib, 'ModuleDefinitionFile', _file_name) # /DEF -_Same(_lib, 'OutputFile', _file_name) # /OUT -_Same(_lib, 'SuppressStartupBanner', _boolean) # /NOLOGO -_Same(_lib, 'UseUnicodeResponseFiles', _boolean) -_Same(_lib, 'LinkTimeCodeGeneration', _boolean) # /LTCG -_Same(_lib, 'TargetMachine', _target_machine_enumeration) +_Same(_lib, "AdditionalDependencies", _file_list) +_Same(_lib, "AdditionalLibraryDirectories", _folder_list) # /LIBPATH +_Same(_lib, "AdditionalOptions", _string_list) +_Same(_lib, "ExportNamedFunctions", _string_list) # /EXPORT +_Same(_lib, "ForceSymbolReferences", _string) # /INCLUDE +_Same(_lib, "IgnoreAllDefaultLibraries", _boolean) # /NODEFAULTLIB +_Same(_lib, "IgnoreSpecificDefaultLibraries", _file_list) # /NODEFAULTLIB +_Same(_lib, "ModuleDefinitionFile", _file_name) # /DEF +_Same(_lib, "OutputFile", _file_name) # /OUT +_Same(_lib, "SuppressStartupBanner", _boolean) # /NOLOGO +_Same(_lib, "UseUnicodeResponseFiles", _boolean) +_Same(_lib, "LinkTimeCodeGeneration", _boolean) # /LTCG +_Same(_lib, "TargetMachine", _target_machine_enumeration) # TODO(jeanluc) _link defines the same value that gets moved to # ProjectReference. We may want to validate that they are consistent. -_Moved(_lib, 'LinkLibraryDependencies', 'ProjectReference', _boolean) - -_MSBuildOnly(_lib, 'DisplayLibrary', _string) # /LIST Visible='false' -_MSBuildOnly(_lib, 'ErrorReporting', - _Enumeration([], new=['PromptImmediately', # /ERRORREPORT:PROMPT - 'QueueForNextLogin', # /ERRORREPORT:QUEUE - 'SendErrorReport', # /ERRORREPORT:SEND - 'NoErrorReport'])) # /ERRORREPORT:NONE -_MSBuildOnly(_lib, 'MinimumRequiredVersion', _string) -_MSBuildOnly(_lib, 'Name', _file_name) # /NAME -_MSBuildOnly(_lib, 'RemoveObjects', _file_list) # /REMOVE -_MSBuildOnly(_lib, 'SubSystem', _subsystem_enumeration) -_MSBuildOnly(_lib, 'TrackerLogDirectory', _folder_name) -_MSBuildOnly(_lib, 'TreatLibWarningAsErrors', _boolean) # /WX -_MSBuildOnly(_lib, 'Verbose', _boolean) +_Moved(_lib, "LinkLibraryDependencies", "ProjectReference", _boolean) + +_MSBuildOnly(_lib, "DisplayLibrary", _string) # /LIST Visible='false' +_MSBuildOnly( + _lib, + "ErrorReporting", + _Enumeration( + [], + new=[ + "PromptImmediately", # /ERRORREPORT:PROMPT + "QueueForNextLogin", # /ERRORREPORT:QUEUE + "SendErrorReport", # /ERRORREPORT:SEND + "NoErrorReport", + ], + ), +) # /ERRORREPORT:NONE +_MSBuildOnly(_lib, "MinimumRequiredVersion", _string) +_MSBuildOnly(_lib, "Name", _file_name) # /NAME +_MSBuildOnly(_lib, "RemoveObjects", _file_list) # /REMOVE +_MSBuildOnly(_lib, "SubSystem", _subsystem_enumeration) +_MSBuildOnly(_lib, "TrackerLogDirectory", _folder_name) +_MSBuildOnly(_lib, "TreatLibWarningAsErrors", _boolean) # /WX +_MSBuildOnly(_lib, "Verbose", _boolean) # Directives for converting VCManifestTool to Mt. @@ -1060,41 +1225,45 @@ def _ValidateSettings(validators, settings, stderr): # the schema of the MSBuild Lib settings. # Options that have the same name in MSVS and MSBuild -_Same(_manifest, 'AdditionalManifestFiles', _file_list) # /manifest -_Same(_manifest, 'AdditionalOptions', _string_list) -_Same(_manifest, 'AssemblyIdentity', _string) # /identity: -_Same(_manifest, 'ComponentFileName', _file_name) # /dll -_Same(_manifest, 'GenerateCatalogFiles', _boolean) # /makecdfs -_Same(_manifest, 'InputResourceManifests', _string) # /inputresource -_Same(_manifest, 'OutputManifestFile', _file_name) # /out -_Same(_manifest, 'RegistrarScriptFile', _file_name) # /rgs -_Same(_manifest, 'ReplacementsFile', _file_name) # /replacements -_Same(_manifest, 'SuppressStartupBanner', _boolean) # /nologo -_Same(_manifest, 'TypeLibraryFile', _file_name) # /tlb: -_Same(_manifest, 'UpdateFileHashes', _boolean) # /hashupdate -_Same(_manifest, 'UpdateFileHashesSearchPath', _file_name) -_Same(_manifest, 'VerboseOutput', _boolean) # /verbose +_Same(_manifest, "AdditionalManifestFiles", _file_list) # /manifest +_Same(_manifest, "AdditionalOptions", _string_list) +_Same(_manifest, "AssemblyIdentity", _string) # /identity: +_Same(_manifest, "ComponentFileName", _file_name) # /dll +_Same(_manifest, "GenerateCatalogFiles", _boolean) # /makecdfs +_Same(_manifest, "InputResourceManifests", _string) # /inputresource +_Same(_manifest, "OutputManifestFile", _file_name) # /out +_Same(_manifest, "RegistrarScriptFile", _file_name) # /rgs +_Same(_manifest, "ReplacementsFile", _file_name) # /replacements +_Same(_manifest, "SuppressStartupBanner", _boolean) # /nologo +_Same(_manifest, "TypeLibraryFile", _file_name) # /tlb: +_Same(_manifest, "UpdateFileHashes", _boolean) # /hashupdate +_Same(_manifest, "UpdateFileHashesSearchPath", _file_name) +_Same(_manifest, "VerboseOutput", _boolean) # /verbose # Options that have moved location. -_MovedAndRenamed(_manifest, 'ManifestResourceFile', - 'ManifestResourceCompile', - 'ResourceOutputFileName', - _file_name) -_Moved(_manifest, 'EmbedManifest', '', _boolean) +_MovedAndRenamed( + _manifest, + "ManifestResourceFile", + "ManifestResourceCompile", + "ResourceOutputFileName", + _file_name, +) +_Moved(_manifest, "EmbedManifest", "", _boolean) # MSVS options not found in MSBuild. -_MSVSOnly(_manifest, 'DependencyInformationFile', _file_name) -_MSVSOnly(_manifest, 'UseFAT32Workaround', _boolean) -_MSVSOnly(_manifest, 'UseUnicodeResponseFiles', _boolean) +_MSVSOnly(_manifest, "DependencyInformationFile", _file_name) +_MSVSOnly(_manifest, "UseFAT32Workaround", _boolean) +_MSVSOnly(_manifest, "UseUnicodeResponseFiles", _boolean) # MSBuild options not found in MSVS. -_MSBuildOnly(_manifest, 'EnableDPIAwareness', _boolean) -_MSBuildOnly(_manifest, 'GenerateCategoryTags', _boolean) # /category -_MSBuildOnly(_manifest, 'ManifestFromManagedAssembly', - _file_name) # /managedassemblyname -_MSBuildOnly(_manifest, 'OutputResourceManifests', _string) # /outputresource -_MSBuildOnly(_manifest, 'SuppressDependencyElement', _boolean) # /nodependency -_MSBuildOnly(_manifest, 'TrackerLogDirectory', _folder_name) +_MSBuildOnly(_manifest, "EnableDPIAwareness", _boolean) +_MSBuildOnly(_manifest, "GenerateCategoryTags", _boolean) # /category +_MSBuildOnly( + _manifest, "ManifestFromManagedAssembly", _file_name +) # /managedassemblyname +_MSBuildOnly(_manifest, "OutputResourceManifests", _string) # /outputresource +_MSBuildOnly(_manifest, "SuppressDependencyElement", _boolean) # /nodependency +_MSBuildOnly(_manifest, "TrackerLogDirectory", _folder_name) # Directives for MASM. @@ -1102,4 +1271,4 @@ def _ValidateSettings(validators, settings, stderr): # MSBuild MASM settings. # Options that have the same name in MSVS and MSBuild. -_Same(_masm, 'UseSafeExceptionHandlers', _boolean) # /safeseh +_Same(_masm, "UseSafeExceptionHandlers", _boolean) # /safeseh diff --git a/tools/gyp/pylib/gyp/MSVSSettings_test.py b/tools/gyp/pylib/gyp/MSVSSettings_test.py index 77b79e650d8b4e..99860c880ec5d0 100755 --- a/tools/gyp/pylib/gyp/MSVSSettings_test.py +++ b/tools/gyp/pylib/gyp/MSVSSettings_test.py @@ -10,1090 +10,1141 @@ import gyp.MSVSSettings as MSVSSettings try: - from StringIO import StringIO # Python 2 + from StringIO import StringIO # Python 2 except ImportError: - from io import StringIO # Python 3 + from io import StringIO # Python 3 class TestSequenceFunctions(unittest.TestCase): + def setUp(self): + self.stderr = StringIO() - def setUp(self): - self.stderr = StringIO() + def _ExpectedWarnings(self, expected): + """Compares recorded lines to expected warnings.""" + self.stderr.seek(0) + actual = self.stderr.read().split("\n") + actual = [line for line in actual if line] + self.assertEqual(sorted(expected), sorted(actual)) - def _ExpectedWarnings(self, expected): - """Compares recorded lines to expected warnings.""" - self.stderr.seek(0) - actual = self.stderr.read().split('\n') - actual = [line for line in actual if line] - self.assertEqual(sorted(expected), sorted(actual)) - - def testValidateMSVSSettings_tool_names(self): - """Tests that only MSVS tool names are allowed.""" - MSVSSettings.ValidateMSVSSettings( - {'VCCLCompilerTool': {}, - 'VCLinkerTool': {}, - 'VCMIDLTool': {}, - 'foo': {}, - 'VCResourceCompilerTool': {}, - 'VCLibrarianTool': {}, - 'VCManifestTool': {}, - 'ClCompile': {}}, - self.stderr) - self._ExpectedWarnings([ - 'Warning: unrecognized tool foo', - 'Warning: unrecognized tool ClCompile']) + def testValidateMSVSSettings_tool_names(self): + """Tests that only MSVS tool names are allowed.""" + MSVSSettings.ValidateMSVSSettings( + { + "VCCLCompilerTool": {}, + "VCLinkerTool": {}, + "VCMIDLTool": {}, + "foo": {}, + "VCResourceCompilerTool": {}, + "VCLibrarianTool": {}, + "VCManifestTool": {}, + "ClCompile": {}, + }, + self.stderr, + ) + self._ExpectedWarnings( + ["Warning: unrecognized tool foo", "Warning: unrecognized tool ClCompile"] + ) - def testValidateMSVSSettings_settings(self): - """Tests that for invalid MSVS settings.""" - MSVSSettings.ValidateMSVSSettings( - {'VCCLCompilerTool': { - 'AdditionalIncludeDirectories': 'folder1;folder2', - 'AdditionalOptions': ['string1', 'string2'], - 'AdditionalUsingDirectories': 'folder1;folder2', - 'AssemblerListingLocation': 'a_file_name', - 'AssemblerOutput': '0', - 'BasicRuntimeChecks': '5', - 'BrowseInformation': 'fdkslj', - 'BrowseInformationFile': 'a_file_name', - 'BufferSecurityCheck': 'true', - 'CallingConvention': '-1', - 'CompileAs': '1', - 'DebugInformationFormat': '2', - 'DefaultCharIsUnsigned': 'true', - 'Detect64BitPortabilityProblems': 'true', - 'DisableLanguageExtensions': 'true', - 'DisableSpecificWarnings': 'string1;string2', - 'EnableEnhancedInstructionSet': '1', - 'EnableFiberSafeOptimizations': 'true', - 'EnableFunctionLevelLinking': 'true', - 'EnableIntrinsicFunctions': 'true', - 'EnablePREfast': 'true', - 'Enableprefast': 'bogus', - 'ErrorReporting': '1', - 'ExceptionHandling': '1', - 'ExpandAttributedSource': 'true', - 'FavorSizeOrSpeed': '1', - 'FloatingPointExceptions': 'true', - 'FloatingPointModel': '1', - 'ForceConformanceInForLoopScope': 'true', - 'ForcedIncludeFiles': 'file1;file2', - 'ForcedUsingFiles': 'file1;file2', - 'GeneratePreprocessedFile': '1', - 'GenerateXMLDocumentationFiles': 'true', - 'IgnoreStandardIncludePath': 'true', - 'InlineFunctionExpansion': '1', - 'KeepComments': 'true', - 'MinimalRebuild': 'true', - 'ObjectFile': 'a_file_name', - 'OmitDefaultLibName': 'true', - 'OmitFramePointers': 'true', - 'OpenMP': 'true', - 'Optimization': '1', - 'PrecompiledHeaderFile': 'a_file_name', - 'PrecompiledHeaderThrough': 'a_file_name', - 'PreprocessorDefinitions': 'string1;string2', - 'ProgramDataBaseFileName': 'a_file_name', - 'RuntimeLibrary': '1', - 'RuntimeTypeInfo': 'true', - 'ShowIncludes': 'true', - 'SmallerTypeCheck': 'true', - 'StringPooling': 'true', - 'StructMemberAlignment': '1', - 'SuppressStartupBanner': 'true', - 'TreatWChar_tAsBuiltInType': 'true', - 'UndefineAllPreprocessorDefinitions': 'true', - 'UndefinePreprocessorDefinitions': 'string1;string2', - 'UseFullPaths': 'true', - 'UsePrecompiledHeader': '1', - 'UseUnicodeResponseFiles': 'true', - 'WarnAsError': 'true', - 'WarningLevel': '1', - 'WholeProgramOptimization': 'true', - 'XMLDocumentationFileName': 'a_file_name', - 'ZZXYZ': 'bogus'}, - 'VCLinkerTool': { - 'AdditionalDependencies': 'file1;file2', - 'AdditionalDependencies_excluded': 'file3', - 'AdditionalLibraryDirectories': 'folder1;folder2', - 'AdditionalManifestDependencies': 'file1;file2', - 'AdditionalOptions': 'a string1', - 'AddModuleNamesToAssembly': 'file1;file2', - 'AllowIsolation': 'true', - 'AssemblyDebug': '2', - 'AssemblyLinkResource': 'file1;file2', - 'BaseAddress': 'a string1', - 'CLRImageType': '2', - 'CLRThreadAttribute': '2', - 'CLRUnmanagedCodeCheck': 'true', - 'DataExecutionPrevention': '2', - 'DelayLoadDLLs': 'file1;file2', - 'DelaySign': 'true', - 'Driver': '2', - 'EmbedManagedResourceFile': 'file1;file2', - 'EnableCOMDATFolding': '2', - 'EnableUAC': 'true', - 'EntryPointSymbol': 'a string1', - 'ErrorReporting': '2', - 'FixedBaseAddress': '2', - 'ForceSymbolReferences': 'file1;file2', - 'FunctionOrder': 'a_file_name', - 'GenerateDebugInformation': 'true', - 'GenerateManifest': 'true', - 'GenerateMapFile': 'true', - 'HeapCommitSize': 'a string1', - 'HeapReserveSize': 'a string1', - 'IgnoreAllDefaultLibraries': 'true', - 'IgnoreDefaultLibraryNames': 'file1;file2', - 'IgnoreEmbeddedIDL': 'true', - 'IgnoreImportLibrary': 'true', - 'ImportLibrary': 'a_file_name', - 'KeyContainer': 'a_file_name', - 'KeyFile': 'a_file_name', - 'LargeAddressAware': '2', - 'LinkIncremental': '2', - 'LinkLibraryDependencies': 'true', - 'LinkTimeCodeGeneration': '2', - 'ManifestFile': 'a_file_name', - 'MapExports': 'true', - 'MapFileName': 'a_file_name', - 'MergedIDLBaseFileName': 'a_file_name', - 'MergeSections': 'a string1', - 'MidlCommandFile': 'a_file_name', - 'ModuleDefinitionFile': 'a_file_name', - 'OptimizeForWindows98': '1', - 'OptimizeReferences': '2', - 'OutputFile': 'a_file_name', - 'PerUserRedirection': 'true', - 'Profile': 'true', - 'ProfileGuidedDatabase': 'a_file_name', - 'ProgramDatabaseFile': 'a_file_name', - 'RandomizedBaseAddress': '2', - 'RegisterOutput': 'true', - 'ResourceOnlyDLL': 'true', - 'SetChecksum': 'true', - 'ShowProgress': '2', - 'StackCommitSize': 'a string1', - 'StackReserveSize': 'a string1', - 'StripPrivateSymbols': 'a_file_name', - 'SubSystem': '2', - 'SupportUnloadOfDelayLoadedDLL': 'true', - 'SuppressStartupBanner': 'true', - 'SwapRunFromCD': 'true', - 'SwapRunFromNet': 'true', - 'TargetMachine': '2', - 'TerminalServerAware': '2', - 'TurnOffAssemblyGeneration': 'true', - 'TypeLibraryFile': 'a_file_name', - 'TypeLibraryResourceID': '33', - 'UACExecutionLevel': '2', - 'UACUIAccess': 'true', - 'UseLibraryDependencyInputs': 'true', - 'UseUnicodeResponseFiles': 'true', - 'Version': 'a string1'}, - 'VCMIDLTool': { - 'AdditionalIncludeDirectories': 'folder1;folder2', - 'AdditionalOptions': 'a string1', - 'CPreprocessOptions': 'a string1', - 'DefaultCharType': '1', - 'DLLDataFileName': 'a_file_name', - 'EnableErrorChecks': '1', - 'ErrorCheckAllocations': 'true', - 'ErrorCheckBounds': 'true', - 'ErrorCheckEnumRange': 'true', - 'ErrorCheckRefPointers': 'true', - 'ErrorCheckStubData': 'true', - 'GenerateStublessProxies': 'true', - 'GenerateTypeLibrary': 'true', - 'HeaderFileName': 'a_file_name', - 'IgnoreStandardIncludePath': 'true', - 'InterfaceIdentifierFileName': 'a_file_name', - 'MkTypLibCompatible': 'true', - 'notgood': 'bogus', - 'OutputDirectory': 'a string1', - 'PreprocessorDefinitions': 'string1;string2', - 'ProxyFileName': 'a_file_name', - 'RedirectOutputAndErrors': 'a_file_name', - 'StructMemberAlignment': '1', - 'SuppressStartupBanner': 'true', - 'TargetEnvironment': '1', - 'TypeLibraryName': 'a_file_name', - 'UndefinePreprocessorDefinitions': 'string1;string2', - 'ValidateParameters': 'true', - 'WarnAsError': 'true', - 'WarningLevel': '1'}, - 'VCResourceCompilerTool': { - 'AdditionalOptions': 'a string1', - 'AdditionalIncludeDirectories': 'folder1;folder2', - 'Culture': '1003', - 'IgnoreStandardIncludePath': 'true', - 'notgood2': 'bogus', - 'PreprocessorDefinitions': 'string1;string2', - 'ResourceOutputFileName': 'a string1', - 'ShowProgress': 'true', - 'SuppressStartupBanner': 'true', - 'UndefinePreprocessorDefinitions': 'string1;string2'}, - 'VCLibrarianTool': { - 'AdditionalDependencies': 'file1;file2', - 'AdditionalLibraryDirectories': 'folder1;folder2', - 'AdditionalOptions': 'a string1', - 'ExportNamedFunctions': 'string1;string2', - 'ForceSymbolReferences': 'a string1', - 'IgnoreAllDefaultLibraries': 'true', - 'IgnoreSpecificDefaultLibraries': 'file1;file2', - 'LinkLibraryDependencies': 'true', - 'ModuleDefinitionFile': 'a_file_name', - 'OutputFile': 'a_file_name', - 'SuppressStartupBanner': 'true', - 'UseUnicodeResponseFiles': 'true'}, - 'VCManifestTool': { - 'AdditionalManifestFiles': 'file1;file2', - 'AdditionalOptions': 'a string1', - 'AssemblyIdentity': 'a string1', - 'ComponentFileName': 'a_file_name', - 'DependencyInformationFile': 'a_file_name', - 'GenerateCatalogFiles': 'true', - 'InputResourceManifests': 'a string1', - 'ManifestResourceFile': 'a_file_name', - 'OutputManifestFile': 'a_file_name', - 'RegistrarScriptFile': 'a_file_name', - 'ReplacementsFile': 'a_file_name', - 'SuppressStartupBanner': 'true', - 'TypeLibraryFile': 'a_file_name', - 'UpdateFileHashes': 'truel', - 'UpdateFileHashesSearchPath': 'a_file_name', - 'UseFAT32Workaround': 'true', - 'UseUnicodeResponseFiles': 'true', - 'VerboseOutput': 'true'}}, - self.stderr) - self._ExpectedWarnings([ - 'Warning: for VCCLCompilerTool/BasicRuntimeChecks, ' - 'index value (5) not in expected range [0, 4)', - 'Warning: for VCCLCompilerTool/BrowseInformation, ' - "invalid literal for int() with base 10: 'fdkslj'", - 'Warning: for VCCLCompilerTool/CallingConvention, ' - 'index value (-1) not in expected range [0, 4)', - 'Warning: for VCCLCompilerTool/DebugInformationFormat, ' - 'converted value for 2 not specified.', - 'Warning: unrecognized setting VCCLCompilerTool/Enableprefast', - 'Warning: unrecognized setting VCCLCompilerTool/ZZXYZ', - 'Warning: for VCLinkerTool/TargetMachine, ' - 'converted value for 2 not specified.', - 'Warning: unrecognized setting VCMIDLTool/notgood', - 'Warning: unrecognized setting VCResourceCompilerTool/notgood2', - 'Warning: for VCManifestTool/UpdateFileHashes, ' - "expected bool; got 'truel'" - '']) + def testValidateMSVSSettings_settings(self): + """Tests that for invalid MSVS settings.""" + MSVSSettings.ValidateMSVSSettings( + { + "VCCLCompilerTool": { + "AdditionalIncludeDirectories": "folder1;folder2", + "AdditionalOptions": ["string1", "string2"], + "AdditionalUsingDirectories": "folder1;folder2", + "AssemblerListingLocation": "a_file_name", + "AssemblerOutput": "0", + "BasicRuntimeChecks": "5", + "BrowseInformation": "fdkslj", + "BrowseInformationFile": "a_file_name", + "BufferSecurityCheck": "true", + "CallingConvention": "-1", + "CompileAs": "1", + "DebugInformationFormat": "2", + "DefaultCharIsUnsigned": "true", + "Detect64BitPortabilityProblems": "true", + "DisableLanguageExtensions": "true", + "DisableSpecificWarnings": "string1;string2", + "EnableEnhancedInstructionSet": "1", + "EnableFiberSafeOptimizations": "true", + "EnableFunctionLevelLinking": "true", + "EnableIntrinsicFunctions": "true", + "EnablePREfast": "true", + "Enableprefast": "bogus", + "ErrorReporting": "1", + "ExceptionHandling": "1", + "ExpandAttributedSource": "true", + "FavorSizeOrSpeed": "1", + "FloatingPointExceptions": "true", + "FloatingPointModel": "1", + "ForceConformanceInForLoopScope": "true", + "ForcedIncludeFiles": "file1;file2", + "ForcedUsingFiles": "file1;file2", + "GeneratePreprocessedFile": "1", + "GenerateXMLDocumentationFiles": "true", + "IgnoreStandardIncludePath": "true", + "InlineFunctionExpansion": "1", + "KeepComments": "true", + "MinimalRebuild": "true", + "ObjectFile": "a_file_name", + "OmitDefaultLibName": "true", + "OmitFramePointers": "true", + "OpenMP": "true", + "Optimization": "1", + "PrecompiledHeaderFile": "a_file_name", + "PrecompiledHeaderThrough": "a_file_name", + "PreprocessorDefinitions": "string1;string2", + "ProgramDataBaseFileName": "a_file_name", + "RuntimeLibrary": "1", + "RuntimeTypeInfo": "true", + "ShowIncludes": "true", + "SmallerTypeCheck": "true", + "StringPooling": "true", + "StructMemberAlignment": "1", + "SuppressStartupBanner": "true", + "TreatWChar_tAsBuiltInType": "true", + "UndefineAllPreprocessorDefinitions": "true", + "UndefinePreprocessorDefinitions": "string1;string2", + "UseFullPaths": "true", + "UsePrecompiledHeader": "1", + "UseUnicodeResponseFiles": "true", + "WarnAsError": "true", + "WarningLevel": "1", + "WholeProgramOptimization": "true", + "XMLDocumentationFileName": "a_file_name", + "ZZXYZ": "bogus", + }, + "VCLinkerTool": { + "AdditionalDependencies": "file1;file2", + "AdditionalDependencies_excluded": "file3", + "AdditionalLibraryDirectories": "folder1;folder2", + "AdditionalManifestDependencies": "file1;file2", + "AdditionalOptions": "a string1", + "AddModuleNamesToAssembly": "file1;file2", + "AllowIsolation": "true", + "AssemblyDebug": "2", + "AssemblyLinkResource": "file1;file2", + "BaseAddress": "a string1", + "CLRImageType": "2", + "CLRThreadAttribute": "2", + "CLRUnmanagedCodeCheck": "true", + "DataExecutionPrevention": "2", + "DelayLoadDLLs": "file1;file2", + "DelaySign": "true", + "Driver": "2", + "EmbedManagedResourceFile": "file1;file2", + "EnableCOMDATFolding": "2", + "EnableUAC": "true", + "EntryPointSymbol": "a string1", + "ErrorReporting": "2", + "FixedBaseAddress": "2", + "ForceSymbolReferences": "file1;file2", + "FunctionOrder": "a_file_name", + "GenerateDebugInformation": "true", + "GenerateManifest": "true", + "GenerateMapFile": "true", + "HeapCommitSize": "a string1", + "HeapReserveSize": "a string1", + "IgnoreAllDefaultLibraries": "true", + "IgnoreDefaultLibraryNames": "file1;file2", + "IgnoreEmbeddedIDL": "true", + "IgnoreImportLibrary": "true", + "ImportLibrary": "a_file_name", + "KeyContainer": "a_file_name", + "KeyFile": "a_file_name", + "LargeAddressAware": "2", + "LinkIncremental": "2", + "LinkLibraryDependencies": "true", + "LinkTimeCodeGeneration": "2", + "ManifestFile": "a_file_name", + "MapExports": "true", + "MapFileName": "a_file_name", + "MergedIDLBaseFileName": "a_file_name", + "MergeSections": "a string1", + "MidlCommandFile": "a_file_name", + "ModuleDefinitionFile": "a_file_name", + "OptimizeForWindows98": "1", + "OptimizeReferences": "2", + "OutputFile": "a_file_name", + "PerUserRedirection": "true", + "Profile": "true", + "ProfileGuidedDatabase": "a_file_name", + "ProgramDatabaseFile": "a_file_name", + "RandomizedBaseAddress": "2", + "RegisterOutput": "true", + "ResourceOnlyDLL": "true", + "SetChecksum": "true", + "ShowProgress": "2", + "StackCommitSize": "a string1", + "StackReserveSize": "a string1", + "StripPrivateSymbols": "a_file_name", + "SubSystem": "2", + "SupportUnloadOfDelayLoadedDLL": "true", + "SuppressStartupBanner": "true", + "SwapRunFromCD": "true", + "SwapRunFromNet": "true", + "TargetMachine": "2", + "TerminalServerAware": "2", + "TurnOffAssemblyGeneration": "true", + "TypeLibraryFile": "a_file_name", + "TypeLibraryResourceID": "33", + "UACExecutionLevel": "2", + "UACUIAccess": "true", + "UseLibraryDependencyInputs": "true", + "UseUnicodeResponseFiles": "true", + "Version": "a string1", + }, + "VCMIDLTool": { + "AdditionalIncludeDirectories": "folder1;folder2", + "AdditionalOptions": "a string1", + "CPreprocessOptions": "a string1", + "DefaultCharType": "1", + "DLLDataFileName": "a_file_name", + "EnableErrorChecks": "1", + "ErrorCheckAllocations": "true", + "ErrorCheckBounds": "true", + "ErrorCheckEnumRange": "true", + "ErrorCheckRefPointers": "true", + "ErrorCheckStubData": "true", + "GenerateStublessProxies": "true", + "GenerateTypeLibrary": "true", + "HeaderFileName": "a_file_name", + "IgnoreStandardIncludePath": "true", + "InterfaceIdentifierFileName": "a_file_name", + "MkTypLibCompatible": "true", + "notgood": "bogus", + "OutputDirectory": "a string1", + "PreprocessorDefinitions": "string1;string2", + "ProxyFileName": "a_file_name", + "RedirectOutputAndErrors": "a_file_name", + "StructMemberAlignment": "1", + "SuppressStartupBanner": "true", + "TargetEnvironment": "1", + "TypeLibraryName": "a_file_name", + "UndefinePreprocessorDefinitions": "string1;string2", + "ValidateParameters": "true", + "WarnAsError": "true", + "WarningLevel": "1", + }, + "VCResourceCompilerTool": { + "AdditionalOptions": "a string1", + "AdditionalIncludeDirectories": "folder1;folder2", + "Culture": "1003", + "IgnoreStandardIncludePath": "true", + "notgood2": "bogus", + "PreprocessorDefinitions": "string1;string2", + "ResourceOutputFileName": "a string1", + "ShowProgress": "true", + "SuppressStartupBanner": "true", + "UndefinePreprocessorDefinitions": "string1;string2", + }, + "VCLibrarianTool": { + "AdditionalDependencies": "file1;file2", + "AdditionalLibraryDirectories": "folder1;folder2", + "AdditionalOptions": "a string1", + "ExportNamedFunctions": "string1;string2", + "ForceSymbolReferences": "a string1", + "IgnoreAllDefaultLibraries": "true", + "IgnoreSpecificDefaultLibraries": "file1;file2", + "LinkLibraryDependencies": "true", + "ModuleDefinitionFile": "a_file_name", + "OutputFile": "a_file_name", + "SuppressStartupBanner": "true", + "UseUnicodeResponseFiles": "true", + }, + "VCManifestTool": { + "AdditionalManifestFiles": "file1;file2", + "AdditionalOptions": "a string1", + "AssemblyIdentity": "a string1", + "ComponentFileName": "a_file_name", + "DependencyInformationFile": "a_file_name", + "GenerateCatalogFiles": "true", + "InputResourceManifests": "a string1", + "ManifestResourceFile": "a_file_name", + "OutputManifestFile": "a_file_name", + "RegistrarScriptFile": "a_file_name", + "ReplacementsFile": "a_file_name", + "SuppressStartupBanner": "true", + "TypeLibraryFile": "a_file_name", + "UpdateFileHashes": "truel", + "UpdateFileHashesSearchPath": "a_file_name", + "UseFAT32Workaround": "true", + "UseUnicodeResponseFiles": "true", + "VerboseOutput": "true", + }, + }, + self.stderr, + ) + self._ExpectedWarnings( + [ + "Warning: for VCCLCompilerTool/BasicRuntimeChecks, " + "index value (5) not in expected range [0, 4)", + "Warning: for VCCLCompilerTool/BrowseInformation, " + "invalid literal for int() with base 10: 'fdkslj'", + "Warning: for VCCLCompilerTool/CallingConvention, " + "index value (-1) not in expected range [0, 4)", + "Warning: for VCCLCompilerTool/DebugInformationFormat, " + "converted value for 2 not specified.", + "Warning: unrecognized setting VCCLCompilerTool/Enableprefast", + "Warning: unrecognized setting VCCLCompilerTool/ZZXYZ", + "Warning: for VCLinkerTool/TargetMachine, " + "converted value for 2 not specified.", + "Warning: unrecognized setting VCMIDLTool/notgood", + "Warning: unrecognized setting VCResourceCompilerTool/notgood2", + "Warning: for VCManifestTool/UpdateFileHashes, " + "expected bool; got 'truel'" + "", + ] + ) - def testValidateMSBuildSettings_settings(self): - """Tests that for invalid MSBuild settings.""" - MSVSSettings.ValidateMSBuildSettings( - {'ClCompile': { - 'AdditionalIncludeDirectories': 'folder1;folder2', - 'AdditionalOptions': ['string1', 'string2'], - 'AdditionalUsingDirectories': 'folder1;folder2', - 'AssemblerListingLocation': 'a_file_name', - 'AssemblerOutput': 'NoListing', - 'BasicRuntimeChecks': 'StackFrameRuntimeCheck', - 'BrowseInformation': 'false', - 'BrowseInformationFile': 'a_file_name', - 'BufferSecurityCheck': 'true', - 'BuildingInIDE': 'true', - 'CallingConvention': 'Cdecl', - 'CompileAs': 'CompileAsC', - 'CompileAsManaged': 'true', - 'CreateHotpatchableImage': 'true', - 'DebugInformationFormat': 'ProgramDatabase', - 'DisableLanguageExtensions': 'true', - 'DisableSpecificWarnings': 'string1;string2', - 'EnableEnhancedInstructionSet': 'StreamingSIMDExtensions', - 'EnableFiberSafeOptimizations': 'true', - 'EnablePREfast': 'true', - 'Enableprefast': 'bogus', - 'ErrorReporting': 'Prompt', - 'ExceptionHandling': 'SyncCThrow', - 'ExpandAttributedSource': 'true', - 'FavorSizeOrSpeed': 'Neither', - 'FloatingPointExceptions': 'true', - 'FloatingPointModel': 'Precise', - 'ForceConformanceInForLoopScope': 'true', - 'ForcedIncludeFiles': 'file1;file2', - 'ForcedUsingFiles': 'file1;file2', - 'FunctionLevelLinking': 'false', - 'GenerateXMLDocumentationFiles': 'true', - 'IgnoreStandardIncludePath': 'true', - 'InlineFunctionExpansion': 'OnlyExplicitInline', - 'IntrinsicFunctions': 'false', - 'MinimalRebuild': 'true', - 'MultiProcessorCompilation': 'true', - 'ObjectFileName': 'a_file_name', - 'OmitDefaultLibName': 'true', - 'OmitFramePointers': 'true', - 'OpenMPSupport': 'true', - 'Optimization': 'Disabled', - 'PrecompiledHeader': 'NotUsing', - 'PrecompiledHeaderFile': 'a_file_name', - 'PrecompiledHeaderOutputFile': 'a_file_name', - 'PreprocessKeepComments': 'true', - 'PreprocessorDefinitions': 'string1;string2', - 'PreprocessOutputPath': 'a string1', - 'PreprocessSuppressLineNumbers': 'false', - 'PreprocessToFile': 'false', - 'ProcessorNumber': '33', - 'ProgramDataBaseFileName': 'a_file_name', - 'RuntimeLibrary': 'MultiThreaded', - 'RuntimeTypeInfo': 'true', - 'ShowIncludes': 'true', - 'SmallerTypeCheck': 'true', - 'StringPooling': 'true', - 'StructMemberAlignment': '1Byte', - 'SuppressStartupBanner': 'true', - 'TrackerLogDirectory': 'a_folder', - 'TreatSpecificWarningsAsErrors': 'string1;string2', - 'TreatWarningAsError': 'true', - 'TreatWChar_tAsBuiltInType': 'true', - 'UndefineAllPreprocessorDefinitions': 'true', - 'UndefinePreprocessorDefinitions': 'string1;string2', - 'UseFullPaths': 'true', - 'UseUnicodeForAssemblerListing': 'true', - 'WarningLevel': 'TurnOffAllWarnings', - 'WholeProgramOptimization': 'true', - 'XMLDocumentationFileName': 'a_file_name', - 'ZZXYZ': 'bogus'}, - 'Link': { - 'AdditionalDependencies': 'file1;file2', - 'AdditionalLibraryDirectories': 'folder1;folder2', - 'AdditionalManifestDependencies': 'file1;file2', - 'AdditionalOptions': 'a string1', - 'AddModuleNamesToAssembly': 'file1;file2', - 'AllowIsolation': 'true', - 'AssemblyDebug': '', - 'AssemblyLinkResource': 'file1;file2', - 'BaseAddress': 'a string1', - 'BuildingInIDE': 'true', - 'CLRImageType': 'ForceIJWImage', - 'CLRSupportLastError': 'Enabled', - 'CLRThreadAttribute': 'MTAThreadingAttribute', - 'CLRUnmanagedCodeCheck': 'true', - 'CreateHotPatchableImage': 'X86Image', - 'DataExecutionPrevention': 'false', - 'DelayLoadDLLs': 'file1;file2', - 'DelaySign': 'true', - 'Driver': 'NotSet', - 'EmbedManagedResourceFile': 'file1;file2', - 'EnableCOMDATFolding': 'false', - 'EnableUAC': 'true', - 'EntryPointSymbol': 'a string1', - 'FixedBaseAddress': 'false', - 'ForceFileOutput': 'Enabled', - 'ForceSymbolReferences': 'file1;file2', - 'FunctionOrder': 'a_file_name', - 'GenerateDebugInformation': 'true', - 'GenerateMapFile': 'true', - 'HeapCommitSize': 'a string1', - 'HeapReserveSize': 'a string1', - 'IgnoreAllDefaultLibraries': 'true', - 'IgnoreEmbeddedIDL': 'true', - 'IgnoreSpecificDefaultLibraries': 'a_file_list', - 'ImageHasSafeExceptionHandlers': 'true', - 'ImportLibrary': 'a_file_name', - 'KeyContainer': 'a_file_name', - 'KeyFile': 'a_file_name', - 'LargeAddressAware': 'false', - 'LinkDLL': 'true', - 'LinkErrorReporting': 'SendErrorReport', - 'LinkStatus': 'true', - 'LinkTimeCodeGeneration': 'UseLinkTimeCodeGeneration', - 'ManifestFile': 'a_file_name', - 'MapExports': 'true', - 'MapFileName': 'a_file_name', - 'MergedIDLBaseFileName': 'a_file_name', - 'MergeSections': 'a string1', - 'MidlCommandFile': 'a_file_name', - 'MinimumRequiredVersion': 'a string1', - 'ModuleDefinitionFile': 'a_file_name', - 'MSDOSStubFileName': 'a_file_name', - 'NoEntryPoint': 'true', - 'OptimizeReferences': 'false', - 'OutputFile': 'a_file_name', - 'PerUserRedirection': 'true', - 'PreventDllBinding': 'true', - 'Profile': 'true', - 'ProfileGuidedDatabase': 'a_file_name', - 'ProgramDatabaseFile': 'a_file_name', - 'RandomizedBaseAddress': 'false', - 'RegisterOutput': 'true', - 'SectionAlignment': '33', - 'SetChecksum': 'true', - 'ShowProgress': 'LinkVerboseREF', - 'SpecifySectionAttributes': 'a string1', - 'StackCommitSize': 'a string1', - 'StackReserveSize': 'a string1', - 'StripPrivateSymbols': 'a_file_name', - 'SubSystem': 'Console', - 'SupportNobindOfDelayLoadedDLL': 'true', - 'SupportUnloadOfDelayLoadedDLL': 'true', - 'SuppressStartupBanner': 'true', - 'SwapRunFromCD': 'true', - 'SwapRunFromNET': 'true', - 'TargetMachine': 'MachineX86', - 'TerminalServerAware': 'false', - 'TrackerLogDirectory': 'a_folder', - 'TreatLinkerWarningAsErrors': 'true', - 'TurnOffAssemblyGeneration': 'true', - 'TypeLibraryFile': 'a_file_name', - 'TypeLibraryResourceID': '33', - 'UACExecutionLevel': 'AsInvoker', - 'UACUIAccess': 'true', - 'Version': 'a string1'}, - 'ResourceCompile': { - 'AdditionalIncludeDirectories': 'folder1;folder2', - 'AdditionalOptions': 'a string1', - 'Culture': '0x236', - 'IgnoreStandardIncludePath': 'true', - 'NullTerminateStrings': 'true', - 'PreprocessorDefinitions': 'string1;string2', - 'ResourceOutputFileName': 'a string1', - 'ShowProgress': 'true', - 'SuppressStartupBanner': 'true', - 'TrackerLogDirectory': 'a_folder', - 'UndefinePreprocessorDefinitions': 'string1;string2'}, - 'Midl': { - 'AdditionalIncludeDirectories': 'folder1;folder2', - 'AdditionalOptions': 'a string1', - 'ApplicationConfigurationMode': 'true', - 'ClientStubFile': 'a_file_name', - 'CPreprocessOptions': 'a string1', - 'DefaultCharType': 'Signed', - 'DllDataFileName': 'a_file_name', - 'EnableErrorChecks': 'EnableCustom', - 'ErrorCheckAllocations': 'true', - 'ErrorCheckBounds': 'true', - 'ErrorCheckEnumRange': 'true', - 'ErrorCheckRefPointers': 'true', - 'ErrorCheckStubData': 'true', - 'GenerateClientFiles': 'Stub', - 'GenerateServerFiles': 'None', - 'GenerateStublessProxies': 'true', - 'GenerateTypeLibrary': 'true', - 'HeaderFileName': 'a_file_name', - 'IgnoreStandardIncludePath': 'true', - 'InterfaceIdentifierFileName': 'a_file_name', - 'LocaleID': '33', - 'MkTypLibCompatible': 'true', - 'OutputDirectory': 'a string1', - 'PreprocessorDefinitions': 'string1;string2', - 'ProxyFileName': 'a_file_name', - 'RedirectOutputAndErrors': 'a_file_name', - 'ServerStubFile': 'a_file_name', - 'StructMemberAlignment': 'NotSet', - 'SuppressCompilerWarnings': 'true', - 'SuppressStartupBanner': 'true', - 'TargetEnvironment': 'Itanium', - 'TrackerLogDirectory': 'a_folder', - 'TypeLibFormat': 'NewFormat', - 'TypeLibraryName': 'a_file_name', - 'UndefinePreprocessorDefinitions': 'string1;string2', - 'ValidateAllParameters': 'true', - 'WarnAsError': 'true', - 'WarningLevel': '1'}, - 'Lib': { - 'AdditionalDependencies': 'file1;file2', - 'AdditionalLibraryDirectories': 'folder1;folder2', - 'AdditionalOptions': 'a string1', - 'DisplayLibrary': 'a string1', - 'ErrorReporting': 'PromptImmediately', - 'ExportNamedFunctions': 'string1;string2', - 'ForceSymbolReferences': 'a string1', - 'IgnoreAllDefaultLibraries': 'true', - 'IgnoreSpecificDefaultLibraries': 'file1;file2', - 'LinkTimeCodeGeneration': 'true', - 'MinimumRequiredVersion': 'a string1', - 'ModuleDefinitionFile': 'a_file_name', - 'Name': 'a_file_name', - 'OutputFile': 'a_file_name', - 'RemoveObjects': 'file1;file2', - 'SubSystem': 'Console', - 'SuppressStartupBanner': 'true', - 'TargetMachine': 'MachineX86i', - 'TrackerLogDirectory': 'a_folder', - 'TreatLibWarningAsErrors': 'true', - 'UseUnicodeResponseFiles': 'true', - 'Verbose': 'true'}, - 'Manifest': { - 'AdditionalManifestFiles': 'file1;file2', - 'AdditionalOptions': 'a string1', - 'AssemblyIdentity': 'a string1', - 'ComponentFileName': 'a_file_name', - 'EnableDPIAwareness': 'fal', - 'GenerateCatalogFiles': 'truel', - 'GenerateCategoryTags': 'true', - 'InputResourceManifests': 'a string1', - 'ManifestFromManagedAssembly': 'a_file_name', - 'notgood3': 'bogus', - 'OutputManifestFile': 'a_file_name', - 'OutputResourceManifests': 'a string1', - 'RegistrarScriptFile': 'a_file_name', - 'ReplacementsFile': 'a_file_name', - 'SuppressDependencyElement': 'true', - 'SuppressStartupBanner': 'true', - 'TrackerLogDirectory': 'a_folder', - 'TypeLibraryFile': 'a_file_name', - 'UpdateFileHashes': 'true', - 'UpdateFileHashesSearchPath': 'a_file_name', - 'VerboseOutput': 'true'}, - 'ProjectReference': { - 'LinkLibraryDependencies': 'true', - 'UseLibraryDependencyInputs': 'true'}, - 'ManifestResourceCompile': { - 'ResourceOutputFileName': 'a_file_name'}, - '': { - 'EmbedManifest': 'true', - 'GenerateManifest': 'true', - 'IgnoreImportLibrary': 'true', - 'LinkIncremental': 'false'}}, - self.stderr) - self._ExpectedWarnings([ - 'Warning: unrecognized setting ClCompile/Enableprefast', - 'Warning: unrecognized setting ClCompile/ZZXYZ', - 'Warning: unrecognized setting Manifest/notgood3', - 'Warning: for Manifest/GenerateCatalogFiles, ' - "expected bool; got 'truel'", - 'Warning: for Lib/TargetMachine, unrecognized enumerated value ' - 'MachineX86i', - "Warning: for Manifest/EnableDPIAwareness, expected bool; got 'fal'"]) + def testValidateMSBuildSettings_settings(self): + """Tests that for invalid MSBuild settings.""" + MSVSSettings.ValidateMSBuildSettings( + { + "ClCompile": { + "AdditionalIncludeDirectories": "folder1;folder2", + "AdditionalOptions": ["string1", "string2"], + "AdditionalUsingDirectories": "folder1;folder2", + "AssemblerListingLocation": "a_file_name", + "AssemblerOutput": "NoListing", + "BasicRuntimeChecks": "StackFrameRuntimeCheck", + "BrowseInformation": "false", + "BrowseInformationFile": "a_file_name", + "BufferSecurityCheck": "true", + "BuildingInIDE": "true", + "CallingConvention": "Cdecl", + "CompileAs": "CompileAsC", + "CompileAsManaged": "true", + "CreateHotpatchableImage": "true", + "DebugInformationFormat": "ProgramDatabase", + "DisableLanguageExtensions": "true", + "DisableSpecificWarnings": "string1;string2", + "EnableEnhancedInstructionSet": "StreamingSIMDExtensions", + "EnableFiberSafeOptimizations": "true", + "EnablePREfast": "true", + "Enableprefast": "bogus", + "ErrorReporting": "Prompt", + "ExceptionHandling": "SyncCThrow", + "ExpandAttributedSource": "true", + "FavorSizeOrSpeed": "Neither", + "FloatingPointExceptions": "true", + "FloatingPointModel": "Precise", + "ForceConformanceInForLoopScope": "true", + "ForcedIncludeFiles": "file1;file2", + "ForcedUsingFiles": "file1;file2", + "FunctionLevelLinking": "false", + "GenerateXMLDocumentationFiles": "true", + "IgnoreStandardIncludePath": "true", + "InlineFunctionExpansion": "OnlyExplicitInline", + "IntrinsicFunctions": "false", + "MinimalRebuild": "true", + "MultiProcessorCompilation": "true", + "ObjectFileName": "a_file_name", + "OmitDefaultLibName": "true", + "OmitFramePointers": "true", + "OpenMPSupport": "true", + "Optimization": "Disabled", + "PrecompiledHeader": "NotUsing", + "PrecompiledHeaderFile": "a_file_name", + "PrecompiledHeaderOutputFile": "a_file_name", + "PreprocessKeepComments": "true", + "PreprocessorDefinitions": "string1;string2", + "PreprocessOutputPath": "a string1", + "PreprocessSuppressLineNumbers": "false", + "PreprocessToFile": "false", + "ProcessorNumber": "33", + "ProgramDataBaseFileName": "a_file_name", + "RuntimeLibrary": "MultiThreaded", + "RuntimeTypeInfo": "true", + "ShowIncludes": "true", + "SmallerTypeCheck": "true", + "StringPooling": "true", + "StructMemberAlignment": "1Byte", + "SuppressStartupBanner": "true", + "TrackerLogDirectory": "a_folder", + "TreatSpecificWarningsAsErrors": "string1;string2", + "TreatWarningAsError": "true", + "TreatWChar_tAsBuiltInType": "true", + "UndefineAllPreprocessorDefinitions": "true", + "UndefinePreprocessorDefinitions": "string1;string2", + "UseFullPaths": "true", + "UseUnicodeForAssemblerListing": "true", + "WarningLevel": "TurnOffAllWarnings", + "WholeProgramOptimization": "true", + "XMLDocumentationFileName": "a_file_name", + "ZZXYZ": "bogus", + }, + "Link": { + "AdditionalDependencies": "file1;file2", + "AdditionalLibraryDirectories": "folder1;folder2", + "AdditionalManifestDependencies": "file1;file2", + "AdditionalOptions": "a string1", + "AddModuleNamesToAssembly": "file1;file2", + "AllowIsolation": "true", + "AssemblyDebug": "", + "AssemblyLinkResource": "file1;file2", + "BaseAddress": "a string1", + "BuildingInIDE": "true", + "CLRImageType": "ForceIJWImage", + "CLRSupportLastError": "Enabled", + "CLRThreadAttribute": "MTAThreadingAttribute", + "CLRUnmanagedCodeCheck": "true", + "CreateHotPatchableImage": "X86Image", + "DataExecutionPrevention": "false", + "DelayLoadDLLs": "file1;file2", + "DelaySign": "true", + "Driver": "NotSet", + "EmbedManagedResourceFile": "file1;file2", + "EnableCOMDATFolding": "false", + "EnableUAC": "true", + "EntryPointSymbol": "a string1", + "FixedBaseAddress": "false", + "ForceFileOutput": "Enabled", + "ForceSymbolReferences": "file1;file2", + "FunctionOrder": "a_file_name", + "GenerateDebugInformation": "true", + "GenerateMapFile": "true", + "HeapCommitSize": "a string1", + "HeapReserveSize": "a string1", + "IgnoreAllDefaultLibraries": "true", + "IgnoreEmbeddedIDL": "true", + "IgnoreSpecificDefaultLibraries": "a_file_list", + "ImageHasSafeExceptionHandlers": "true", + "ImportLibrary": "a_file_name", + "KeyContainer": "a_file_name", + "KeyFile": "a_file_name", + "LargeAddressAware": "false", + "LinkDLL": "true", + "LinkErrorReporting": "SendErrorReport", + "LinkStatus": "true", + "LinkTimeCodeGeneration": "UseLinkTimeCodeGeneration", + "ManifestFile": "a_file_name", + "MapExports": "true", + "MapFileName": "a_file_name", + "MergedIDLBaseFileName": "a_file_name", + "MergeSections": "a string1", + "MidlCommandFile": "a_file_name", + "MinimumRequiredVersion": "a string1", + "ModuleDefinitionFile": "a_file_name", + "MSDOSStubFileName": "a_file_name", + "NoEntryPoint": "true", + "OptimizeReferences": "false", + "OutputFile": "a_file_name", + "PerUserRedirection": "true", + "PreventDllBinding": "true", + "Profile": "true", + "ProfileGuidedDatabase": "a_file_name", + "ProgramDatabaseFile": "a_file_name", + "RandomizedBaseAddress": "false", + "RegisterOutput": "true", + "SectionAlignment": "33", + "SetChecksum": "true", + "ShowProgress": "LinkVerboseREF", + "SpecifySectionAttributes": "a string1", + "StackCommitSize": "a string1", + "StackReserveSize": "a string1", + "StripPrivateSymbols": "a_file_name", + "SubSystem": "Console", + "SupportNobindOfDelayLoadedDLL": "true", + "SupportUnloadOfDelayLoadedDLL": "true", + "SuppressStartupBanner": "true", + "SwapRunFromCD": "true", + "SwapRunFromNET": "true", + "TargetMachine": "MachineX86", + "TerminalServerAware": "false", + "TrackerLogDirectory": "a_folder", + "TreatLinkerWarningAsErrors": "true", + "TurnOffAssemblyGeneration": "true", + "TypeLibraryFile": "a_file_name", + "TypeLibraryResourceID": "33", + "UACExecutionLevel": "AsInvoker", + "UACUIAccess": "true", + "Version": "a string1", + }, + "ResourceCompile": { + "AdditionalIncludeDirectories": "folder1;folder2", + "AdditionalOptions": "a string1", + "Culture": "0x236", + "IgnoreStandardIncludePath": "true", + "NullTerminateStrings": "true", + "PreprocessorDefinitions": "string1;string2", + "ResourceOutputFileName": "a string1", + "ShowProgress": "true", + "SuppressStartupBanner": "true", + "TrackerLogDirectory": "a_folder", + "UndefinePreprocessorDefinitions": "string1;string2", + }, + "Midl": { + "AdditionalIncludeDirectories": "folder1;folder2", + "AdditionalOptions": "a string1", + "ApplicationConfigurationMode": "true", + "ClientStubFile": "a_file_name", + "CPreprocessOptions": "a string1", + "DefaultCharType": "Signed", + "DllDataFileName": "a_file_name", + "EnableErrorChecks": "EnableCustom", + "ErrorCheckAllocations": "true", + "ErrorCheckBounds": "true", + "ErrorCheckEnumRange": "true", + "ErrorCheckRefPointers": "true", + "ErrorCheckStubData": "true", + "GenerateClientFiles": "Stub", + "GenerateServerFiles": "None", + "GenerateStublessProxies": "true", + "GenerateTypeLibrary": "true", + "HeaderFileName": "a_file_name", + "IgnoreStandardIncludePath": "true", + "InterfaceIdentifierFileName": "a_file_name", + "LocaleID": "33", + "MkTypLibCompatible": "true", + "OutputDirectory": "a string1", + "PreprocessorDefinitions": "string1;string2", + "ProxyFileName": "a_file_name", + "RedirectOutputAndErrors": "a_file_name", + "ServerStubFile": "a_file_name", + "StructMemberAlignment": "NotSet", + "SuppressCompilerWarnings": "true", + "SuppressStartupBanner": "true", + "TargetEnvironment": "Itanium", + "TrackerLogDirectory": "a_folder", + "TypeLibFormat": "NewFormat", + "TypeLibraryName": "a_file_name", + "UndefinePreprocessorDefinitions": "string1;string2", + "ValidateAllParameters": "true", + "WarnAsError": "true", + "WarningLevel": "1", + }, + "Lib": { + "AdditionalDependencies": "file1;file2", + "AdditionalLibraryDirectories": "folder1;folder2", + "AdditionalOptions": "a string1", + "DisplayLibrary": "a string1", + "ErrorReporting": "PromptImmediately", + "ExportNamedFunctions": "string1;string2", + "ForceSymbolReferences": "a string1", + "IgnoreAllDefaultLibraries": "true", + "IgnoreSpecificDefaultLibraries": "file1;file2", + "LinkTimeCodeGeneration": "true", + "MinimumRequiredVersion": "a string1", + "ModuleDefinitionFile": "a_file_name", + "Name": "a_file_name", + "OutputFile": "a_file_name", + "RemoveObjects": "file1;file2", + "SubSystem": "Console", + "SuppressStartupBanner": "true", + "TargetMachine": "MachineX86i", + "TrackerLogDirectory": "a_folder", + "TreatLibWarningAsErrors": "true", + "UseUnicodeResponseFiles": "true", + "Verbose": "true", + }, + "Manifest": { + "AdditionalManifestFiles": "file1;file2", + "AdditionalOptions": "a string1", + "AssemblyIdentity": "a string1", + "ComponentFileName": "a_file_name", + "EnableDPIAwareness": "fal", + "GenerateCatalogFiles": "truel", + "GenerateCategoryTags": "true", + "InputResourceManifests": "a string1", + "ManifestFromManagedAssembly": "a_file_name", + "notgood3": "bogus", + "OutputManifestFile": "a_file_name", + "OutputResourceManifests": "a string1", + "RegistrarScriptFile": "a_file_name", + "ReplacementsFile": "a_file_name", + "SuppressDependencyElement": "true", + "SuppressStartupBanner": "true", + "TrackerLogDirectory": "a_folder", + "TypeLibraryFile": "a_file_name", + "UpdateFileHashes": "true", + "UpdateFileHashesSearchPath": "a_file_name", + "VerboseOutput": "true", + }, + "ProjectReference": { + "LinkLibraryDependencies": "true", + "UseLibraryDependencyInputs": "true", + }, + "ManifestResourceCompile": {"ResourceOutputFileName": "a_file_name"}, + "": { + "EmbedManifest": "true", + "GenerateManifest": "true", + "IgnoreImportLibrary": "true", + "LinkIncremental": "false", + }, + }, + self.stderr, + ) + self._ExpectedWarnings( + [ + "Warning: unrecognized setting ClCompile/Enableprefast", + "Warning: unrecognized setting ClCompile/ZZXYZ", + "Warning: unrecognized setting Manifest/notgood3", + "Warning: for Manifest/GenerateCatalogFiles, " + "expected bool; got 'truel'", + "Warning: for Lib/TargetMachine, unrecognized enumerated value " + "MachineX86i", + "Warning: for Manifest/EnableDPIAwareness, expected bool; got 'fal'", + ] + ) - def testConvertToMSBuildSettings_empty(self): - """Tests an empty conversion.""" - msvs_settings = {} - expected_msbuild_settings = {} - actual_msbuild_settings = MSVSSettings.ConvertToMSBuildSettings( - msvs_settings, - self.stderr) - self.assertEqual(expected_msbuild_settings, actual_msbuild_settings) - self._ExpectedWarnings([]) + def testConvertToMSBuildSettings_empty(self): + """Tests an empty conversion.""" + msvs_settings = {} + expected_msbuild_settings = {} + actual_msbuild_settings = MSVSSettings.ConvertToMSBuildSettings( + msvs_settings, self.stderr + ) + self.assertEqual(expected_msbuild_settings, actual_msbuild_settings) + self._ExpectedWarnings([]) - def testConvertToMSBuildSettings_minimal(self): - """Tests a minimal conversion.""" - msvs_settings = { - 'VCCLCompilerTool': { - 'AdditionalIncludeDirectories': 'dir1', - 'AdditionalOptions': '/foo', - 'BasicRuntimeChecks': '0', + def testConvertToMSBuildSettings_minimal(self): + """Tests a minimal conversion.""" + msvs_settings = { + "VCCLCompilerTool": { + "AdditionalIncludeDirectories": "dir1", + "AdditionalOptions": "/foo", + "BasicRuntimeChecks": "0", }, - 'VCLinkerTool': { - 'LinkTimeCodeGeneration': '1', - 'ErrorReporting': '1', - 'DataExecutionPrevention': '2', + "VCLinkerTool": { + "LinkTimeCodeGeneration": "1", + "ErrorReporting": "1", + "DataExecutionPrevention": "2", }, } - expected_msbuild_settings = { - 'ClCompile': { - 'AdditionalIncludeDirectories': 'dir1', - 'AdditionalOptions': '/foo', - 'BasicRuntimeChecks': 'Default', + expected_msbuild_settings = { + "ClCompile": { + "AdditionalIncludeDirectories": "dir1", + "AdditionalOptions": "/foo", + "BasicRuntimeChecks": "Default", }, - 'Link': { - 'LinkTimeCodeGeneration': 'UseLinkTimeCodeGeneration', - 'LinkErrorReporting': 'PromptImmediately', - 'DataExecutionPrevention': 'true', + "Link": { + "LinkTimeCodeGeneration": "UseLinkTimeCodeGeneration", + "LinkErrorReporting": "PromptImmediately", + "DataExecutionPrevention": "true", }, } - actual_msbuild_settings = MSVSSettings.ConvertToMSBuildSettings( - msvs_settings, - self.stderr) - self.assertEqual(expected_msbuild_settings, actual_msbuild_settings) - self._ExpectedWarnings([]) - - def testConvertToMSBuildSettings_warnings(self): - """Tests conversion that generates warnings.""" - msvs_settings = { - 'VCCLCompilerTool': { - 'AdditionalIncludeDirectories': '1', - 'AdditionalOptions': '2', - # These are incorrect values: - 'BasicRuntimeChecks': '12', - 'BrowseInformation': '21', - 'UsePrecompiledHeader': '13', - 'GeneratePreprocessedFile': '14'}, - 'VCLinkerTool': { - # These are incorrect values: - 'Driver': '10', - 'LinkTimeCodeGeneration': '31', - 'ErrorReporting': '21', - 'FixedBaseAddress': '6'}, - 'VCResourceCompilerTool': { - # Custom - 'Culture': '1003'}} - expected_msbuild_settings = { - 'ClCompile': { - 'AdditionalIncludeDirectories': '1', - 'AdditionalOptions': '2'}, - 'Link': {}, - 'ResourceCompile': { - # Custom - 'Culture': '0x03eb'}} - actual_msbuild_settings = MSVSSettings.ConvertToMSBuildSettings( - msvs_settings, - self.stderr) - self.assertEqual(expected_msbuild_settings, actual_msbuild_settings) - self._ExpectedWarnings([ - 'Warning: while converting VCCLCompilerTool/BasicRuntimeChecks to ' - 'MSBuild, index value (12) not in expected range [0, 4)', - 'Warning: while converting VCCLCompilerTool/BrowseInformation to ' - 'MSBuild, index value (21) not in expected range [0, 3)', - 'Warning: while converting VCCLCompilerTool/UsePrecompiledHeader to ' - 'MSBuild, index value (13) not in expected range [0, 3)', - 'Warning: while converting VCCLCompilerTool/GeneratePreprocessedFile to ' - 'MSBuild, value must be one of [0, 1, 2]; got 14', + actual_msbuild_settings = MSVSSettings.ConvertToMSBuildSettings( + msvs_settings, self.stderr + ) + self.assertEqual(expected_msbuild_settings, actual_msbuild_settings) + self._ExpectedWarnings([]) - 'Warning: while converting VCLinkerTool/Driver to ' - 'MSBuild, index value (10) not in expected range [0, 4)', - 'Warning: while converting VCLinkerTool/LinkTimeCodeGeneration to ' - 'MSBuild, index value (31) not in expected range [0, 5)', - 'Warning: while converting VCLinkerTool/ErrorReporting to ' - 'MSBuild, index value (21) not in expected range [0, 3)', - 'Warning: while converting VCLinkerTool/FixedBaseAddress to ' - 'MSBuild, index value (6) not in expected range [0, 3)', - ]) + def testConvertToMSBuildSettings_warnings(self): + """Tests conversion that generates warnings.""" + msvs_settings = { + "VCCLCompilerTool": { + "AdditionalIncludeDirectories": "1", + "AdditionalOptions": "2", + # These are incorrect values: + "BasicRuntimeChecks": "12", + "BrowseInformation": "21", + "UsePrecompiledHeader": "13", + "GeneratePreprocessedFile": "14", + }, + "VCLinkerTool": { + # These are incorrect values: + "Driver": "10", + "LinkTimeCodeGeneration": "31", + "ErrorReporting": "21", + "FixedBaseAddress": "6", + }, + "VCResourceCompilerTool": { + # Custom + "Culture": "1003" + }, + } + expected_msbuild_settings = { + "ClCompile": { + "AdditionalIncludeDirectories": "1", + "AdditionalOptions": "2", + }, + "Link": {}, + "ResourceCompile": { + # Custom + "Culture": "0x03eb" + }, + } + actual_msbuild_settings = MSVSSettings.ConvertToMSBuildSettings( + msvs_settings, self.stderr + ) + self.assertEqual(expected_msbuild_settings, actual_msbuild_settings) + self._ExpectedWarnings( + [ + "Warning: while converting VCCLCompilerTool/BasicRuntimeChecks to " + "MSBuild, index value (12) not in expected range [0, 4)", + "Warning: while converting VCCLCompilerTool/BrowseInformation to " + "MSBuild, index value (21) not in expected range [0, 3)", + "Warning: while converting VCCLCompilerTool/UsePrecompiledHeader to " + "MSBuild, index value (13) not in expected range [0, 3)", + "Warning: while converting " + "VCCLCompilerTool/GeneratePreprocessedFile to " + "MSBuild, value must be one of [0, 1, 2]; got 14", + "Warning: while converting VCLinkerTool/Driver to " + "MSBuild, index value (10) not in expected range [0, 4)", + "Warning: while converting VCLinkerTool/LinkTimeCodeGeneration to " + "MSBuild, index value (31) not in expected range [0, 5)", + "Warning: while converting VCLinkerTool/ErrorReporting to " + "MSBuild, index value (21) not in expected range [0, 3)", + "Warning: while converting VCLinkerTool/FixedBaseAddress to " + "MSBuild, index value (6) not in expected range [0, 3)", + ] + ) - def testConvertToMSBuildSettings_full_synthetic(self): - """Tests conversion of all the MSBuild settings.""" - msvs_settings = { - 'VCCLCompilerTool': { - 'AdditionalIncludeDirectories': 'folder1;folder2;folder3', - 'AdditionalOptions': 'a_string', - 'AdditionalUsingDirectories': 'folder1;folder2;folder3', - 'AssemblerListingLocation': 'a_file_name', - 'AssemblerOutput': '0', - 'BasicRuntimeChecks': '1', - 'BrowseInformation': '2', - 'BrowseInformationFile': 'a_file_name', - 'BufferSecurityCheck': 'true', - 'CallingConvention': '0', - 'CompileAs': '1', - 'DebugInformationFormat': '4', - 'DefaultCharIsUnsigned': 'true', - 'Detect64BitPortabilityProblems': 'true', - 'DisableLanguageExtensions': 'true', - 'DisableSpecificWarnings': 'd1;d2;d3', - 'EnableEnhancedInstructionSet': '0', - 'EnableFiberSafeOptimizations': 'true', - 'EnableFunctionLevelLinking': 'true', - 'EnableIntrinsicFunctions': 'true', - 'EnablePREfast': 'true', - 'ErrorReporting': '1', - 'ExceptionHandling': '2', - 'ExpandAttributedSource': 'true', - 'FavorSizeOrSpeed': '0', - 'FloatingPointExceptions': 'true', - 'FloatingPointModel': '1', - 'ForceConformanceInForLoopScope': 'true', - 'ForcedIncludeFiles': 'file1;file2;file3', - 'ForcedUsingFiles': 'file1;file2;file3', - 'GeneratePreprocessedFile': '1', - 'GenerateXMLDocumentationFiles': 'true', - 'IgnoreStandardIncludePath': 'true', - 'InlineFunctionExpansion': '2', - 'KeepComments': 'true', - 'MinimalRebuild': 'true', - 'ObjectFile': 'a_file_name', - 'OmitDefaultLibName': 'true', - 'OmitFramePointers': 'true', - 'OpenMP': 'true', - 'Optimization': '3', - 'PrecompiledHeaderFile': 'a_file_name', - 'PrecompiledHeaderThrough': 'a_file_name', - 'PreprocessorDefinitions': 'd1;d2;d3', - 'ProgramDataBaseFileName': 'a_file_name', - 'RuntimeLibrary': '0', - 'RuntimeTypeInfo': 'true', - 'ShowIncludes': 'true', - 'SmallerTypeCheck': 'true', - 'StringPooling': 'true', - 'StructMemberAlignment': '1', - 'SuppressStartupBanner': 'true', - 'TreatWChar_tAsBuiltInType': 'true', - 'UndefineAllPreprocessorDefinitions': 'true', - 'UndefinePreprocessorDefinitions': 'd1;d2;d3', - 'UseFullPaths': 'true', - 'UsePrecompiledHeader': '1', - 'UseUnicodeResponseFiles': 'true', - 'WarnAsError': 'true', - 'WarningLevel': '2', - 'WholeProgramOptimization': 'true', - 'XMLDocumentationFileName': 'a_file_name'}, - 'VCLinkerTool': { - 'AdditionalDependencies': 'file1;file2;file3', - 'AdditionalLibraryDirectories': 'folder1;folder2;folder3', - 'AdditionalLibraryDirectories_excluded': 'folder1;folder2;folder3', - 'AdditionalManifestDependencies': 'file1;file2;file3', - 'AdditionalOptions': 'a_string', - 'AddModuleNamesToAssembly': 'file1;file2;file3', - 'AllowIsolation': 'true', - 'AssemblyDebug': '0', - 'AssemblyLinkResource': 'file1;file2;file3', - 'BaseAddress': 'a_string', - 'CLRImageType': '1', - 'CLRThreadAttribute': '2', - 'CLRUnmanagedCodeCheck': 'true', - 'DataExecutionPrevention': '0', - 'DelayLoadDLLs': 'file1;file2;file3', - 'DelaySign': 'true', - 'Driver': '1', - 'EmbedManagedResourceFile': 'file1;file2;file3', - 'EnableCOMDATFolding': '0', - 'EnableUAC': 'true', - 'EntryPointSymbol': 'a_string', - 'ErrorReporting': '0', - 'FixedBaseAddress': '1', - 'ForceSymbolReferences': 'file1;file2;file3', - 'FunctionOrder': 'a_file_name', - 'GenerateDebugInformation': 'true', - 'GenerateManifest': 'true', - 'GenerateMapFile': 'true', - 'HeapCommitSize': 'a_string', - 'HeapReserveSize': 'a_string', - 'IgnoreAllDefaultLibraries': 'true', - 'IgnoreDefaultLibraryNames': 'file1;file2;file3', - 'IgnoreEmbeddedIDL': 'true', - 'IgnoreImportLibrary': 'true', - 'ImportLibrary': 'a_file_name', - 'KeyContainer': 'a_file_name', - 'KeyFile': 'a_file_name', - 'LargeAddressAware': '2', - 'LinkIncremental': '1', - 'LinkLibraryDependencies': 'true', - 'LinkTimeCodeGeneration': '2', - 'ManifestFile': 'a_file_name', - 'MapExports': 'true', - 'MapFileName': 'a_file_name', - 'MergedIDLBaseFileName': 'a_file_name', - 'MergeSections': 'a_string', - 'MidlCommandFile': 'a_file_name', - 'ModuleDefinitionFile': 'a_file_name', - 'OptimizeForWindows98': '1', - 'OptimizeReferences': '0', - 'OutputFile': 'a_file_name', - 'PerUserRedirection': 'true', - 'Profile': 'true', - 'ProfileGuidedDatabase': 'a_file_name', - 'ProgramDatabaseFile': 'a_file_name', - 'RandomizedBaseAddress': '1', - 'RegisterOutput': 'true', - 'ResourceOnlyDLL': 'true', - 'SetChecksum': 'true', - 'ShowProgress': '0', - 'StackCommitSize': 'a_string', - 'StackReserveSize': 'a_string', - 'StripPrivateSymbols': 'a_file_name', - 'SubSystem': '2', - 'SupportUnloadOfDelayLoadedDLL': 'true', - 'SuppressStartupBanner': 'true', - 'SwapRunFromCD': 'true', - 'SwapRunFromNet': 'true', - 'TargetMachine': '3', - 'TerminalServerAware': '2', - 'TurnOffAssemblyGeneration': 'true', - 'TypeLibraryFile': 'a_file_name', - 'TypeLibraryResourceID': '33', - 'UACExecutionLevel': '1', - 'UACUIAccess': 'true', - 'UseLibraryDependencyInputs': 'false', - 'UseUnicodeResponseFiles': 'true', - 'Version': 'a_string'}, - 'VCResourceCompilerTool': { - 'AdditionalIncludeDirectories': 'folder1;folder2;folder3', - 'AdditionalOptions': 'a_string', - 'Culture': '1003', - 'IgnoreStandardIncludePath': 'true', - 'PreprocessorDefinitions': 'd1;d2;d3', - 'ResourceOutputFileName': 'a_string', - 'ShowProgress': 'true', - 'SuppressStartupBanner': 'true', - 'UndefinePreprocessorDefinitions': 'd1;d2;d3'}, - 'VCMIDLTool': { - 'AdditionalIncludeDirectories': 'folder1;folder2;folder3', - 'AdditionalOptions': 'a_string', - 'CPreprocessOptions': 'a_string', - 'DefaultCharType': '0', - 'DLLDataFileName': 'a_file_name', - 'EnableErrorChecks': '2', - 'ErrorCheckAllocations': 'true', - 'ErrorCheckBounds': 'true', - 'ErrorCheckEnumRange': 'true', - 'ErrorCheckRefPointers': 'true', - 'ErrorCheckStubData': 'true', - 'GenerateStublessProxies': 'true', - 'GenerateTypeLibrary': 'true', - 'HeaderFileName': 'a_file_name', - 'IgnoreStandardIncludePath': 'true', - 'InterfaceIdentifierFileName': 'a_file_name', - 'MkTypLibCompatible': 'true', - 'OutputDirectory': 'a_string', - 'PreprocessorDefinitions': 'd1;d2;d3', - 'ProxyFileName': 'a_file_name', - 'RedirectOutputAndErrors': 'a_file_name', - 'StructMemberAlignment': '3', - 'SuppressStartupBanner': 'true', - 'TargetEnvironment': '1', - 'TypeLibraryName': 'a_file_name', - 'UndefinePreprocessorDefinitions': 'd1;d2;d3', - 'ValidateParameters': 'true', - 'WarnAsError': 'true', - 'WarningLevel': '4'}, - 'VCLibrarianTool': { - 'AdditionalDependencies': 'file1;file2;file3', - 'AdditionalLibraryDirectories': 'folder1;folder2;folder3', - 'AdditionalLibraryDirectories_excluded': 'folder1;folder2;folder3', - 'AdditionalOptions': 'a_string', - 'ExportNamedFunctions': 'd1;d2;d3', - 'ForceSymbolReferences': 'a_string', - 'IgnoreAllDefaultLibraries': 'true', - 'IgnoreSpecificDefaultLibraries': 'file1;file2;file3', - 'LinkLibraryDependencies': 'true', - 'ModuleDefinitionFile': 'a_file_name', - 'OutputFile': 'a_file_name', - 'SuppressStartupBanner': 'true', - 'UseUnicodeResponseFiles': 'true'}, - 'VCManifestTool': { - 'AdditionalManifestFiles': 'file1;file2;file3', - 'AdditionalOptions': 'a_string', - 'AssemblyIdentity': 'a_string', - 'ComponentFileName': 'a_file_name', - 'DependencyInformationFile': 'a_file_name', - 'EmbedManifest': 'true', - 'GenerateCatalogFiles': 'true', - 'InputResourceManifests': 'a_string', - 'ManifestResourceFile': 'my_name', - 'OutputManifestFile': 'a_file_name', - 'RegistrarScriptFile': 'a_file_name', - 'ReplacementsFile': 'a_file_name', - 'SuppressStartupBanner': 'true', - 'TypeLibraryFile': 'a_file_name', - 'UpdateFileHashes': 'true', - 'UpdateFileHashesSearchPath': 'a_file_name', - 'UseFAT32Workaround': 'true', - 'UseUnicodeResponseFiles': 'true', - 'VerboseOutput': 'true'}} - expected_msbuild_settings = { - 'ClCompile': { - 'AdditionalIncludeDirectories': 'folder1;folder2;folder3', - 'AdditionalOptions': 'a_string /J', - 'AdditionalUsingDirectories': 'folder1;folder2;folder3', - 'AssemblerListingLocation': 'a_file_name', - 'AssemblerOutput': 'NoListing', - 'BasicRuntimeChecks': 'StackFrameRuntimeCheck', - 'BrowseInformation': 'true', - 'BrowseInformationFile': 'a_file_name', - 'BufferSecurityCheck': 'true', - 'CallingConvention': 'Cdecl', - 'CompileAs': 'CompileAsC', - 'DebugInformationFormat': 'EditAndContinue', - 'DisableLanguageExtensions': 'true', - 'DisableSpecificWarnings': 'd1;d2;d3', - 'EnableEnhancedInstructionSet': 'NotSet', - 'EnableFiberSafeOptimizations': 'true', - 'EnablePREfast': 'true', - 'ErrorReporting': 'Prompt', - 'ExceptionHandling': 'Async', - 'ExpandAttributedSource': 'true', - 'FavorSizeOrSpeed': 'Neither', - 'FloatingPointExceptions': 'true', - 'FloatingPointModel': 'Strict', - 'ForceConformanceInForLoopScope': 'true', - 'ForcedIncludeFiles': 'file1;file2;file3', - 'ForcedUsingFiles': 'file1;file2;file3', - 'FunctionLevelLinking': 'true', - 'GenerateXMLDocumentationFiles': 'true', - 'IgnoreStandardIncludePath': 'true', - 'InlineFunctionExpansion': 'AnySuitable', - 'IntrinsicFunctions': 'true', - 'MinimalRebuild': 'true', - 'ObjectFileName': 'a_file_name', - 'OmitDefaultLibName': 'true', - 'OmitFramePointers': 'true', - 'OpenMPSupport': 'true', - 'Optimization': 'Full', - 'PrecompiledHeader': 'Create', - 'PrecompiledHeaderFile': 'a_file_name', - 'PrecompiledHeaderOutputFile': 'a_file_name', - 'PreprocessKeepComments': 'true', - 'PreprocessorDefinitions': 'd1;d2;d3', - 'PreprocessSuppressLineNumbers': 'false', - 'PreprocessToFile': 'true', - 'ProgramDataBaseFileName': 'a_file_name', - 'RuntimeLibrary': 'MultiThreaded', - 'RuntimeTypeInfo': 'true', - 'ShowIncludes': 'true', - 'SmallerTypeCheck': 'true', - 'StringPooling': 'true', - 'StructMemberAlignment': '1Byte', - 'SuppressStartupBanner': 'true', - 'TreatWarningAsError': 'true', - 'TreatWChar_tAsBuiltInType': 'true', - 'UndefineAllPreprocessorDefinitions': 'true', - 'UndefinePreprocessorDefinitions': 'd1;d2;d3', - 'UseFullPaths': 'true', - 'WarningLevel': 'Level2', - 'WholeProgramOptimization': 'true', - 'XMLDocumentationFileName': 'a_file_name'}, - 'Link': { - 'AdditionalDependencies': 'file1;file2;file3', - 'AdditionalLibraryDirectories': 'folder1;folder2;folder3', - 'AdditionalManifestDependencies': 'file1;file2;file3', - 'AdditionalOptions': 'a_string', - 'AddModuleNamesToAssembly': 'file1;file2;file3', - 'AllowIsolation': 'true', - 'AssemblyDebug': '', - 'AssemblyLinkResource': 'file1;file2;file3', - 'BaseAddress': 'a_string', - 'CLRImageType': 'ForceIJWImage', - 'CLRThreadAttribute': 'STAThreadingAttribute', - 'CLRUnmanagedCodeCheck': 'true', - 'DataExecutionPrevention': '', - 'DelayLoadDLLs': 'file1;file2;file3', - 'DelaySign': 'true', - 'Driver': 'Driver', - 'EmbedManagedResourceFile': 'file1;file2;file3', - 'EnableCOMDATFolding': '', - 'EnableUAC': 'true', - 'EntryPointSymbol': 'a_string', - 'FixedBaseAddress': 'false', - 'ForceSymbolReferences': 'file1;file2;file3', - 'FunctionOrder': 'a_file_name', - 'GenerateDebugInformation': 'true', - 'GenerateMapFile': 'true', - 'HeapCommitSize': 'a_string', - 'HeapReserveSize': 'a_string', - 'IgnoreAllDefaultLibraries': 'true', - 'IgnoreEmbeddedIDL': 'true', - 'IgnoreSpecificDefaultLibraries': 'file1;file2;file3', - 'ImportLibrary': 'a_file_name', - 'KeyContainer': 'a_file_name', - 'KeyFile': 'a_file_name', - 'LargeAddressAware': 'true', - 'LinkErrorReporting': 'NoErrorReport', - 'LinkTimeCodeGeneration': 'PGInstrument', - 'ManifestFile': 'a_file_name', - 'MapExports': 'true', - 'MapFileName': 'a_file_name', - 'MergedIDLBaseFileName': 'a_file_name', - 'MergeSections': 'a_string', - 'MidlCommandFile': 'a_file_name', - 'ModuleDefinitionFile': 'a_file_name', - 'NoEntryPoint': 'true', - 'OptimizeReferences': '', - 'OutputFile': 'a_file_name', - 'PerUserRedirection': 'true', - 'Profile': 'true', - 'ProfileGuidedDatabase': 'a_file_name', - 'ProgramDatabaseFile': 'a_file_name', - 'RandomizedBaseAddress': 'false', - 'RegisterOutput': 'true', - 'SetChecksum': 'true', - 'ShowProgress': 'NotSet', - 'StackCommitSize': 'a_string', - 'StackReserveSize': 'a_string', - 'StripPrivateSymbols': 'a_file_name', - 'SubSystem': 'Windows', - 'SupportUnloadOfDelayLoadedDLL': 'true', - 'SuppressStartupBanner': 'true', - 'SwapRunFromCD': 'true', - 'SwapRunFromNET': 'true', - 'TargetMachine': 'MachineARM', - 'TerminalServerAware': 'true', - 'TurnOffAssemblyGeneration': 'true', - 'TypeLibraryFile': 'a_file_name', - 'TypeLibraryResourceID': '33', - 'UACExecutionLevel': 'HighestAvailable', - 'UACUIAccess': 'true', - 'Version': 'a_string'}, - 'ResourceCompile': { - 'AdditionalIncludeDirectories': 'folder1;folder2;folder3', - 'AdditionalOptions': 'a_string', - 'Culture': '0x03eb', - 'IgnoreStandardIncludePath': 'true', - 'PreprocessorDefinitions': 'd1;d2;d3', - 'ResourceOutputFileName': 'a_string', - 'ShowProgress': 'true', - 'SuppressStartupBanner': 'true', - 'UndefinePreprocessorDefinitions': 'd1;d2;d3'}, - 'Midl': { - 'AdditionalIncludeDirectories': 'folder1;folder2;folder3', - 'AdditionalOptions': 'a_string', - 'CPreprocessOptions': 'a_string', - 'DefaultCharType': 'Unsigned', - 'DllDataFileName': 'a_file_name', - 'EnableErrorChecks': 'All', - 'ErrorCheckAllocations': 'true', - 'ErrorCheckBounds': 'true', - 'ErrorCheckEnumRange': 'true', - 'ErrorCheckRefPointers': 'true', - 'ErrorCheckStubData': 'true', - 'GenerateStublessProxies': 'true', - 'GenerateTypeLibrary': 'true', - 'HeaderFileName': 'a_file_name', - 'IgnoreStandardIncludePath': 'true', - 'InterfaceIdentifierFileName': 'a_file_name', - 'MkTypLibCompatible': 'true', - 'OutputDirectory': 'a_string', - 'PreprocessorDefinitions': 'd1;d2;d3', - 'ProxyFileName': 'a_file_name', - 'RedirectOutputAndErrors': 'a_file_name', - 'StructMemberAlignment': '4', - 'SuppressStartupBanner': 'true', - 'TargetEnvironment': 'Win32', - 'TypeLibraryName': 'a_file_name', - 'UndefinePreprocessorDefinitions': 'd1;d2;d3', - 'ValidateAllParameters': 'true', - 'WarnAsError': 'true', - 'WarningLevel': '4'}, - 'Lib': { - 'AdditionalDependencies': 'file1;file2;file3', - 'AdditionalLibraryDirectories': 'folder1;folder2;folder3', - 'AdditionalOptions': 'a_string', - 'ExportNamedFunctions': 'd1;d2;d3', - 'ForceSymbolReferences': 'a_string', - 'IgnoreAllDefaultLibraries': 'true', - 'IgnoreSpecificDefaultLibraries': 'file1;file2;file3', - 'ModuleDefinitionFile': 'a_file_name', - 'OutputFile': 'a_file_name', - 'SuppressStartupBanner': 'true', - 'UseUnicodeResponseFiles': 'true'}, - 'Manifest': { - 'AdditionalManifestFiles': 'file1;file2;file3', - 'AdditionalOptions': 'a_string', - 'AssemblyIdentity': 'a_string', - 'ComponentFileName': 'a_file_name', - 'GenerateCatalogFiles': 'true', - 'InputResourceManifests': 'a_string', - 'OutputManifestFile': 'a_file_name', - 'RegistrarScriptFile': 'a_file_name', - 'ReplacementsFile': 'a_file_name', - 'SuppressStartupBanner': 'true', - 'TypeLibraryFile': 'a_file_name', - 'UpdateFileHashes': 'true', - 'UpdateFileHashesSearchPath': 'a_file_name', - 'VerboseOutput': 'true'}, - 'ManifestResourceCompile': { - 'ResourceOutputFileName': 'my_name'}, - 'ProjectReference': { - 'LinkLibraryDependencies': 'true', - 'UseLibraryDependencyInputs': 'false'}, - '': { - 'EmbedManifest': 'true', - 'GenerateManifest': 'true', - 'IgnoreImportLibrary': 'true', - 'LinkIncremental': 'false'}} - self.maxDiff = 9999 # on failure display a long diff - actual_msbuild_settings = MSVSSettings.ConvertToMSBuildSettings( - msvs_settings, - self.stderr) - self.assertEqual(expected_msbuild_settings, actual_msbuild_settings) - self._ExpectedWarnings([]) + def testConvertToMSBuildSettings_full_synthetic(self): + """Tests conversion of all the MSBuild settings.""" + msvs_settings = { + "VCCLCompilerTool": { + "AdditionalIncludeDirectories": "folder1;folder2;folder3", + "AdditionalOptions": "a_string", + "AdditionalUsingDirectories": "folder1;folder2;folder3", + "AssemblerListingLocation": "a_file_name", + "AssemblerOutput": "0", + "BasicRuntimeChecks": "1", + "BrowseInformation": "2", + "BrowseInformationFile": "a_file_name", + "BufferSecurityCheck": "true", + "CallingConvention": "0", + "CompileAs": "1", + "DebugInformationFormat": "4", + "DefaultCharIsUnsigned": "true", + "Detect64BitPortabilityProblems": "true", + "DisableLanguageExtensions": "true", + "DisableSpecificWarnings": "d1;d2;d3", + "EnableEnhancedInstructionSet": "0", + "EnableFiberSafeOptimizations": "true", + "EnableFunctionLevelLinking": "true", + "EnableIntrinsicFunctions": "true", + "EnablePREfast": "true", + "ErrorReporting": "1", + "ExceptionHandling": "2", + "ExpandAttributedSource": "true", + "FavorSizeOrSpeed": "0", + "FloatingPointExceptions": "true", + "FloatingPointModel": "1", + "ForceConformanceInForLoopScope": "true", + "ForcedIncludeFiles": "file1;file2;file3", + "ForcedUsingFiles": "file1;file2;file3", + "GeneratePreprocessedFile": "1", + "GenerateXMLDocumentationFiles": "true", + "IgnoreStandardIncludePath": "true", + "InlineFunctionExpansion": "2", + "KeepComments": "true", + "MinimalRebuild": "true", + "ObjectFile": "a_file_name", + "OmitDefaultLibName": "true", + "OmitFramePointers": "true", + "OpenMP": "true", + "Optimization": "3", + "PrecompiledHeaderFile": "a_file_name", + "PrecompiledHeaderThrough": "a_file_name", + "PreprocessorDefinitions": "d1;d2;d3", + "ProgramDataBaseFileName": "a_file_name", + "RuntimeLibrary": "0", + "RuntimeTypeInfo": "true", + "ShowIncludes": "true", + "SmallerTypeCheck": "true", + "StringPooling": "true", + "StructMemberAlignment": "1", + "SuppressStartupBanner": "true", + "TreatWChar_tAsBuiltInType": "true", + "UndefineAllPreprocessorDefinitions": "true", + "UndefinePreprocessorDefinitions": "d1;d2;d3", + "UseFullPaths": "true", + "UsePrecompiledHeader": "1", + "UseUnicodeResponseFiles": "true", + "WarnAsError": "true", + "WarningLevel": "2", + "WholeProgramOptimization": "true", + "XMLDocumentationFileName": "a_file_name", + }, + "VCLinkerTool": { + "AdditionalDependencies": "file1;file2;file3", + "AdditionalLibraryDirectories": "folder1;folder2;folder3", + "AdditionalLibraryDirectories_excluded": "folder1;folder2;folder3", + "AdditionalManifestDependencies": "file1;file2;file3", + "AdditionalOptions": "a_string", + "AddModuleNamesToAssembly": "file1;file2;file3", + "AllowIsolation": "true", + "AssemblyDebug": "0", + "AssemblyLinkResource": "file1;file2;file3", + "BaseAddress": "a_string", + "CLRImageType": "1", + "CLRThreadAttribute": "2", + "CLRUnmanagedCodeCheck": "true", + "DataExecutionPrevention": "0", + "DelayLoadDLLs": "file1;file2;file3", + "DelaySign": "true", + "Driver": "1", + "EmbedManagedResourceFile": "file1;file2;file3", + "EnableCOMDATFolding": "0", + "EnableUAC": "true", + "EntryPointSymbol": "a_string", + "ErrorReporting": "0", + "FixedBaseAddress": "1", + "ForceSymbolReferences": "file1;file2;file3", + "FunctionOrder": "a_file_name", + "GenerateDebugInformation": "true", + "GenerateManifest": "true", + "GenerateMapFile": "true", + "HeapCommitSize": "a_string", + "HeapReserveSize": "a_string", + "IgnoreAllDefaultLibraries": "true", + "IgnoreDefaultLibraryNames": "file1;file2;file3", + "IgnoreEmbeddedIDL": "true", + "IgnoreImportLibrary": "true", + "ImportLibrary": "a_file_name", + "KeyContainer": "a_file_name", + "KeyFile": "a_file_name", + "LargeAddressAware": "2", + "LinkIncremental": "1", + "LinkLibraryDependencies": "true", + "LinkTimeCodeGeneration": "2", + "ManifestFile": "a_file_name", + "MapExports": "true", + "MapFileName": "a_file_name", + "MergedIDLBaseFileName": "a_file_name", + "MergeSections": "a_string", + "MidlCommandFile": "a_file_name", + "ModuleDefinitionFile": "a_file_name", + "OptimizeForWindows98": "1", + "OptimizeReferences": "0", + "OutputFile": "a_file_name", + "PerUserRedirection": "true", + "Profile": "true", + "ProfileGuidedDatabase": "a_file_name", + "ProgramDatabaseFile": "a_file_name", + "RandomizedBaseAddress": "1", + "RegisterOutput": "true", + "ResourceOnlyDLL": "true", + "SetChecksum": "true", + "ShowProgress": "0", + "StackCommitSize": "a_string", + "StackReserveSize": "a_string", + "StripPrivateSymbols": "a_file_name", + "SubSystem": "2", + "SupportUnloadOfDelayLoadedDLL": "true", + "SuppressStartupBanner": "true", + "SwapRunFromCD": "true", + "SwapRunFromNet": "true", + "TargetMachine": "3", + "TerminalServerAware": "2", + "TurnOffAssemblyGeneration": "true", + "TypeLibraryFile": "a_file_name", + "TypeLibraryResourceID": "33", + "UACExecutionLevel": "1", + "UACUIAccess": "true", + "UseLibraryDependencyInputs": "false", + "UseUnicodeResponseFiles": "true", + "Version": "a_string", + }, + "VCResourceCompilerTool": { + "AdditionalIncludeDirectories": "folder1;folder2;folder3", + "AdditionalOptions": "a_string", + "Culture": "1003", + "IgnoreStandardIncludePath": "true", + "PreprocessorDefinitions": "d1;d2;d3", + "ResourceOutputFileName": "a_string", + "ShowProgress": "true", + "SuppressStartupBanner": "true", + "UndefinePreprocessorDefinitions": "d1;d2;d3", + }, + "VCMIDLTool": { + "AdditionalIncludeDirectories": "folder1;folder2;folder3", + "AdditionalOptions": "a_string", + "CPreprocessOptions": "a_string", + "DefaultCharType": "0", + "DLLDataFileName": "a_file_name", + "EnableErrorChecks": "2", + "ErrorCheckAllocations": "true", + "ErrorCheckBounds": "true", + "ErrorCheckEnumRange": "true", + "ErrorCheckRefPointers": "true", + "ErrorCheckStubData": "true", + "GenerateStublessProxies": "true", + "GenerateTypeLibrary": "true", + "HeaderFileName": "a_file_name", + "IgnoreStandardIncludePath": "true", + "InterfaceIdentifierFileName": "a_file_name", + "MkTypLibCompatible": "true", + "OutputDirectory": "a_string", + "PreprocessorDefinitions": "d1;d2;d3", + "ProxyFileName": "a_file_name", + "RedirectOutputAndErrors": "a_file_name", + "StructMemberAlignment": "3", + "SuppressStartupBanner": "true", + "TargetEnvironment": "1", + "TypeLibraryName": "a_file_name", + "UndefinePreprocessorDefinitions": "d1;d2;d3", + "ValidateParameters": "true", + "WarnAsError": "true", + "WarningLevel": "4", + }, + "VCLibrarianTool": { + "AdditionalDependencies": "file1;file2;file3", + "AdditionalLibraryDirectories": "folder1;folder2;folder3", + "AdditionalLibraryDirectories_excluded": "folder1;folder2;folder3", + "AdditionalOptions": "a_string", + "ExportNamedFunctions": "d1;d2;d3", + "ForceSymbolReferences": "a_string", + "IgnoreAllDefaultLibraries": "true", + "IgnoreSpecificDefaultLibraries": "file1;file2;file3", + "LinkLibraryDependencies": "true", + "ModuleDefinitionFile": "a_file_name", + "OutputFile": "a_file_name", + "SuppressStartupBanner": "true", + "UseUnicodeResponseFiles": "true", + }, + "VCManifestTool": { + "AdditionalManifestFiles": "file1;file2;file3", + "AdditionalOptions": "a_string", + "AssemblyIdentity": "a_string", + "ComponentFileName": "a_file_name", + "DependencyInformationFile": "a_file_name", + "EmbedManifest": "true", + "GenerateCatalogFiles": "true", + "InputResourceManifests": "a_string", + "ManifestResourceFile": "my_name", + "OutputManifestFile": "a_file_name", + "RegistrarScriptFile": "a_file_name", + "ReplacementsFile": "a_file_name", + "SuppressStartupBanner": "true", + "TypeLibraryFile": "a_file_name", + "UpdateFileHashes": "true", + "UpdateFileHashesSearchPath": "a_file_name", + "UseFAT32Workaround": "true", + "UseUnicodeResponseFiles": "true", + "VerboseOutput": "true", + }, + } + expected_msbuild_settings = { + "ClCompile": { + "AdditionalIncludeDirectories": "folder1;folder2;folder3", + "AdditionalOptions": "a_string /J", + "AdditionalUsingDirectories": "folder1;folder2;folder3", + "AssemblerListingLocation": "a_file_name", + "AssemblerOutput": "NoListing", + "BasicRuntimeChecks": "StackFrameRuntimeCheck", + "BrowseInformation": "true", + "BrowseInformationFile": "a_file_name", + "BufferSecurityCheck": "true", + "CallingConvention": "Cdecl", + "CompileAs": "CompileAsC", + "DebugInformationFormat": "EditAndContinue", + "DisableLanguageExtensions": "true", + "DisableSpecificWarnings": "d1;d2;d3", + "EnableEnhancedInstructionSet": "NotSet", + "EnableFiberSafeOptimizations": "true", + "EnablePREfast": "true", + "ErrorReporting": "Prompt", + "ExceptionHandling": "Async", + "ExpandAttributedSource": "true", + "FavorSizeOrSpeed": "Neither", + "FloatingPointExceptions": "true", + "FloatingPointModel": "Strict", + "ForceConformanceInForLoopScope": "true", + "ForcedIncludeFiles": "file1;file2;file3", + "ForcedUsingFiles": "file1;file2;file3", + "FunctionLevelLinking": "true", + "GenerateXMLDocumentationFiles": "true", + "IgnoreStandardIncludePath": "true", + "InlineFunctionExpansion": "AnySuitable", + "IntrinsicFunctions": "true", + "MinimalRebuild": "true", + "ObjectFileName": "a_file_name", + "OmitDefaultLibName": "true", + "OmitFramePointers": "true", + "OpenMPSupport": "true", + "Optimization": "Full", + "PrecompiledHeader": "Create", + "PrecompiledHeaderFile": "a_file_name", + "PrecompiledHeaderOutputFile": "a_file_name", + "PreprocessKeepComments": "true", + "PreprocessorDefinitions": "d1;d2;d3", + "PreprocessSuppressLineNumbers": "false", + "PreprocessToFile": "true", + "ProgramDataBaseFileName": "a_file_name", + "RuntimeLibrary": "MultiThreaded", + "RuntimeTypeInfo": "true", + "ShowIncludes": "true", + "SmallerTypeCheck": "true", + "StringPooling": "true", + "StructMemberAlignment": "1Byte", + "SuppressStartupBanner": "true", + "TreatWarningAsError": "true", + "TreatWChar_tAsBuiltInType": "true", + "UndefineAllPreprocessorDefinitions": "true", + "UndefinePreprocessorDefinitions": "d1;d2;d3", + "UseFullPaths": "true", + "WarningLevel": "Level2", + "WholeProgramOptimization": "true", + "XMLDocumentationFileName": "a_file_name", + }, + "Link": { + "AdditionalDependencies": "file1;file2;file3", + "AdditionalLibraryDirectories": "folder1;folder2;folder3", + "AdditionalManifestDependencies": "file1;file2;file3", + "AdditionalOptions": "a_string", + "AddModuleNamesToAssembly": "file1;file2;file3", + "AllowIsolation": "true", + "AssemblyDebug": "", + "AssemblyLinkResource": "file1;file2;file3", + "BaseAddress": "a_string", + "CLRImageType": "ForceIJWImage", + "CLRThreadAttribute": "STAThreadingAttribute", + "CLRUnmanagedCodeCheck": "true", + "DataExecutionPrevention": "", + "DelayLoadDLLs": "file1;file2;file3", + "DelaySign": "true", + "Driver": "Driver", + "EmbedManagedResourceFile": "file1;file2;file3", + "EnableCOMDATFolding": "", + "EnableUAC": "true", + "EntryPointSymbol": "a_string", + "FixedBaseAddress": "false", + "ForceSymbolReferences": "file1;file2;file3", + "FunctionOrder": "a_file_name", + "GenerateDebugInformation": "true", + "GenerateMapFile": "true", + "HeapCommitSize": "a_string", + "HeapReserveSize": "a_string", + "IgnoreAllDefaultLibraries": "true", + "IgnoreEmbeddedIDL": "true", + "IgnoreSpecificDefaultLibraries": "file1;file2;file3", + "ImportLibrary": "a_file_name", + "KeyContainer": "a_file_name", + "KeyFile": "a_file_name", + "LargeAddressAware": "true", + "LinkErrorReporting": "NoErrorReport", + "LinkTimeCodeGeneration": "PGInstrument", + "ManifestFile": "a_file_name", + "MapExports": "true", + "MapFileName": "a_file_name", + "MergedIDLBaseFileName": "a_file_name", + "MergeSections": "a_string", + "MidlCommandFile": "a_file_name", + "ModuleDefinitionFile": "a_file_name", + "NoEntryPoint": "true", + "OptimizeReferences": "", + "OutputFile": "a_file_name", + "PerUserRedirection": "true", + "Profile": "true", + "ProfileGuidedDatabase": "a_file_name", + "ProgramDatabaseFile": "a_file_name", + "RandomizedBaseAddress": "false", + "RegisterOutput": "true", + "SetChecksum": "true", + "ShowProgress": "NotSet", + "StackCommitSize": "a_string", + "StackReserveSize": "a_string", + "StripPrivateSymbols": "a_file_name", + "SubSystem": "Windows", + "SupportUnloadOfDelayLoadedDLL": "true", + "SuppressStartupBanner": "true", + "SwapRunFromCD": "true", + "SwapRunFromNET": "true", + "TargetMachine": "MachineARM", + "TerminalServerAware": "true", + "TurnOffAssemblyGeneration": "true", + "TypeLibraryFile": "a_file_name", + "TypeLibraryResourceID": "33", + "UACExecutionLevel": "HighestAvailable", + "UACUIAccess": "true", + "Version": "a_string", + }, + "ResourceCompile": { + "AdditionalIncludeDirectories": "folder1;folder2;folder3", + "AdditionalOptions": "a_string", + "Culture": "0x03eb", + "IgnoreStandardIncludePath": "true", + "PreprocessorDefinitions": "d1;d2;d3", + "ResourceOutputFileName": "a_string", + "ShowProgress": "true", + "SuppressStartupBanner": "true", + "UndefinePreprocessorDefinitions": "d1;d2;d3", + }, + "Midl": { + "AdditionalIncludeDirectories": "folder1;folder2;folder3", + "AdditionalOptions": "a_string", + "CPreprocessOptions": "a_string", + "DefaultCharType": "Unsigned", + "DllDataFileName": "a_file_name", + "EnableErrorChecks": "All", + "ErrorCheckAllocations": "true", + "ErrorCheckBounds": "true", + "ErrorCheckEnumRange": "true", + "ErrorCheckRefPointers": "true", + "ErrorCheckStubData": "true", + "GenerateStublessProxies": "true", + "GenerateTypeLibrary": "true", + "HeaderFileName": "a_file_name", + "IgnoreStandardIncludePath": "true", + "InterfaceIdentifierFileName": "a_file_name", + "MkTypLibCompatible": "true", + "OutputDirectory": "a_string", + "PreprocessorDefinitions": "d1;d2;d3", + "ProxyFileName": "a_file_name", + "RedirectOutputAndErrors": "a_file_name", + "StructMemberAlignment": "4", + "SuppressStartupBanner": "true", + "TargetEnvironment": "Win32", + "TypeLibraryName": "a_file_name", + "UndefinePreprocessorDefinitions": "d1;d2;d3", + "ValidateAllParameters": "true", + "WarnAsError": "true", + "WarningLevel": "4", + }, + "Lib": { + "AdditionalDependencies": "file1;file2;file3", + "AdditionalLibraryDirectories": "folder1;folder2;folder3", + "AdditionalOptions": "a_string", + "ExportNamedFunctions": "d1;d2;d3", + "ForceSymbolReferences": "a_string", + "IgnoreAllDefaultLibraries": "true", + "IgnoreSpecificDefaultLibraries": "file1;file2;file3", + "ModuleDefinitionFile": "a_file_name", + "OutputFile": "a_file_name", + "SuppressStartupBanner": "true", + "UseUnicodeResponseFiles": "true", + }, + "Manifest": { + "AdditionalManifestFiles": "file1;file2;file3", + "AdditionalOptions": "a_string", + "AssemblyIdentity": "a_string", + "ComponentFileName": "a_file_name", + "GenerateCatalogFiles": "true", + "InputResourceManifests": "a_string", + "OutputManifestFile": "a_file_name", + "RegistrarScriptFile": "a_file_name", + "ReplacementsFile": "a_file_name", + "SuppressStartupBanner": "true", + "TypeLibraryFile": "a_file_name", + "UpdateFileHashes": "true", + "UpdateFileHashesSearchPath": "a_file_name", + "VerboseOutput": "true", + }, + "ManifestResourceCompile": {"ResourceOutputFileName": "my_name"}, + "ProjectReference": { + "LinkLibraryDependencies": "true", + "UseLibraryDependencyInputs": "false", + }, + "": { + "EmbedManifest": "true", + "GenerateManifest": "true", + "IgnoreImportLibrary": "true", + "LinkIncremental": "false", + }, + } + self.maxDiff = 9999 # on failure display a long diff + actual_msbuild_settings = MSVSSettings.ConvertToMSBuildSettings( + msvs_settings, self.stderr + ) + self.assertEqual(expected_msbuild_settings, actual_msbuild_settings) + self._ExpectedWarnings([]) - def testConvertToMSBuildSettings_actual(self): - """Tests the conversion of an actual project. + def testConvertToMSBuildSettings_actual(self): + """Tests the conversion of an actual project. A VS2008 project with most of the options defined was created through the VS2008 IDE. It was then converted to VS2010. The tool settings found in @@ -1136,354 +1187,364 @@ def testConvertToMSBuildSettings_actual(self): AdditionalOptions: ' %(AdditionalOptions)', InputResourceManifests: ';%(InputResourceManifests)', """ - msvs_settings = { - 'VCCLCompilerTool': { - 'AdditionalIncludeDirectories': 'dir1', - 'AdditionalOptions': '/more', - 'AdditionalUsingDirectories': 'test', - 'AssemblerListingLocation': '$(IntDir)\\a', - 'AssemblerOutput': '1', - 'BasicRuntimeChecks': '3', - 'BrowseInformation': '1', - 'BrowseInformationFile': '$(IntDir)\\e', - 'BufferSecurityCheck': 'false', - 'CallingConvention': '1', - 'CompileAs': '1', - 'DebugInformationFormat': '4', - 'DefaultCharIsUnsigned': 'true', - 'Detect64BitPortabilityProblems': 'true', - 'DisableLanguageExtensions': 'true', - 'DisableSpecificWarnings': 'abc', - 'EnableEnhancedInstructionSet': '1', - 'EnableFiberSafeOptimizations': 'true', - 'EnableFunctionLevelLinking': 'true', - 'EnableIntrinsicFunctions': 'true', - 'EnablePREfast': 'true', - 'ErrorReporting': '2', - 'ExceptionHandling': '2', - 'ExpandAttributedSource': 'true', - 'FavorSizeOrSpeed': '2', - 'FloatingPointExceptions': 'true', - 'FloatingPointModel': '1', - 'ForceConformanceInForLoopScope': 'false', - 'ForcedIncludeFiles': 'def', - 'ForcedUsingFiles': 'ge', - 'GeneratePreprocessedFile': '2', - 'GenerateXMLDocumentationFiles': 'true', - 'IgnoreStandardIncludePath': 'true', - 'InlineFunctionExpansion': '1', - 'KeepComments': 'true', - 'MinimalRebuild': 'true', - 'ObjectFile': '$(IntDir)\\b', - 'OmitDefaultLibName': 'true', - 'OmitFramePointers': 'true', - 'OpenMP': 'true', - 'Optimization': '3', - 'PrecompiledHeaderFile': '$(IntDir)\\$(TargetName).pche', - 'PrecompiledHeaderThrough': 'StdAfx.hd', - 'PreprocessorDefinitions': 'WIN32;_DEBUG;_CONSOLE', - 'ProgramDataBaseFileName': '$(IntDir)\\vc90b.pdb', - 'RuntimeLibrary': '3', - 'RuntimeTypeInfo': 'false', - 'ShowIncludes': 'true', - 'SmallerTypeCheck': 'true', - 'StringPooling': 'true', - 'StructMemberAlignment': '3', - 'SuppressStartupBanner': 'false', - 'TreatWChar_tAsBuiltInType': 'false', - 'UndefineAllPreprocessorDefinitions': 'true', - 'UndefinePreprocessorDefinitions': 'wer', - 'UseFullPaths': 'true', - 'UsePrecompiledHeader': '0', - 'UseUnicodeResponseFiles': 'false', - 'WarnAsError': 'true', - 'WarningLevel': '3', - 'WholeProgramOptimization': 'true', - 'XMLDocumentationFileName': '$(IntDir)\\c'}, - 'VCLinkerTool': { - 'AdditionalDependencies': 'zx', - 'AdditionalLibraryDirectories': 'asd', - 'AdditionalManifestDependencies': 's2', - 'AdditionalOptions': '/mor2', - 'AddModuleNamesToAssembly': 'd1', - 'AllowIsolation': 'false', - 'AssemblyDebug': '1', - 'AssemblyLinkResource': 'd5', - 'BaseAddress': '23423', - 'CLRImageType': '3', - 'CLRThreadAttribute': '1', - 'CLRUnmanagedCodeCheck': 'true', - 'DataExecutionPrevention': '0', - 'DelayLoadDLLs': 'd4', - 'DelaySign': 'true', - 'Driver': '2', - 'EmbedManagedResourceFile': 'd2', - 'EnableCOMDATFolding': '1', - 'EnableUAC': 'false', - 'EntryPointSymbol': 'f5', - 'ErrorReporting': '2', - 'FixedBaseAddress': '1', - 'ForceSymbolReferences': 'd3', - 'FunctionOrder': 'fssdfsd', - 'GenerateDebugInformation': 'true', - 'GenerateManifest': 'false', - 'GenerateMapFile': 'true', - 'HeapCommitSize': '13', - 'HeapReserveSize': '12', - 'IgnoreAllDefaultLibraries': 'true', - 'IgnoreDefaultLibraryNames': 'flob;flok', - 'IgnoreEmbeddedIDL': 'true', - 'IgnoreImportLibrary': 'true', - 'ImportLibrary': 'f4', - 'KeyContainer': 'f7', - 'KeyFile': 'f6', - 'LargeAddressAware': '2', - 'LinkIncremental': '0', - 'LinkLibraryDependencies': 'false', - 'LinkTimeCodeGeneration': '1', - 'ManifestFile': - '$(IntDir)\\$(TargetFileName).2intermediate.manifest', - 'MapExports': 'true', - 'MapFileName': 'd5', - 'MergedIDLBaseFileName': 'f2', - 'MergeSections': 'f5', - 'MidlCommandFile': 'f1', - 'ModuleDefinitionFile': 'sdsd', - 'OptimizeForWindows98': '2', - 'OptimizeReferences': '2', - 'OutputFile': '$(OutDir)\\$(ProjectName)2.exe', - 'PerUserRedirection': 'true', - 'Profile': 'true', - 'ProfileGuidedDatabase': '$(TargetDir)$(TargetName).pgdd', - 'ProgramDatabaseFile': 'Flob.pdb', - 'RandomizedBaseAddress': '1', - 'RegisterOutput': 'true', - 'ResourceOnlyDLL': 'true', - 'SetChecksum': 'false', - 'ShowProgress': '1', - 'StackCommitSize': '15', - 'StackReserveSize': '14', - 'StripPrivateSymbols': 'd3', - 'SubSystem': '1', - 'SupportUnloadOfDelayLoadedDLL': 'true', - 'SuppressStartupBanner': 'false', - 'SwapRunFromCD': 'true', - 'SwapRunFromNet': 'true', - 'TargetMachine': '1', - 'TerminalServerAware': '1', - 'TurnOffAssemblyGeneration': 'true', - 'TypeLibraryFile': 'f3', - 'TypeLibraryResourceID': '12', - 'UACExecutionLevel': '2', - 'UACUIAccess': 'true', - 'UseLibraryDependencyInputs': 'true', - 'UseUnicodeResponseFiles': 'false', - 'Version': '333'}, - 'VCResourceCompilerTool': { - 'AdditionalIncludeDirectories': 'f3', - 'AdditionalOptions': '/more3', - 'Culture': '3084', - 'IgnoreStandardIncludePath': 'true', - 'PreprocessorDefinitions': '_UNICODE;UNICODE2', - 'ResourceOutputFileName': '$(IntDir)/$(InputName)3.res', - 'ShowProgress': 'true'}, - 'VCManifestTool': { - 'AdditionalManifestFiles': 'sfsdfsd', - 'AdditionalOptions': 'afdsdafsd', - 'AssemblyIdentity': 'sddfdsadfsa', - 'ComponentFileName': 'fsdfds', - 'DependencyInformationFile': '$(IntDir)\\mt.depdfd', - 'EmbedManifest': 'false', - 'GenerateCatalogFiles': 'true', - 'InputResourceManifests': 'asfsfdafs', - 'ManifestResourceFile': - '$(IntDir)\\$(TargetFileName).embed.manifest.resfdsf', - 'OutputManifestFile': '$(TargetPath).manifestdfs', - 'RegistrarScriptFile': 'sdfsfd', - 'ReplacementsFile': 'sdffsd', - 'SuppressStartupBanner': 'false', - 'TypeLibraryFile': 'sfsd', - 'UpdateFileHashes': 'true', - 'UpdateFileHashesSearchPath': 'sfsd', - 'UseFAT32Workaround': 'true', - 'UseUnicodeResponseFiles': 'false', - 'VerboseOutput': 'true'}} - expected_msbuild_settings = { - 'ClCompile': { - 'AdditionalIncludeDirectories': 'dir1', - 'AdditionalOptions': '/more /J', - 'AdditionalUsingDirectories': 'test', - 'AssemblerListingLocation': '$(IntDir)a', - 'AssemblerOutput': 'AssemblyCode', - 'BasicRuntimeChecks': 'EnableFastChecks', - 'BrowseInformation': 'true', - 'BrowseInformationFile': '$(IntDir)e', - 'BufferSecurityCheck': 'false', - 'CallingConvention': 'FastCall', - 'CompileAs': 'CompileAsC', - 'DebugInformationFormat': 'EditAndContinue', - 'DisableLanguageExtensions': 'true', - 'DisableSpecificWarnings': 'abc', - 'EnableEnhancedInstructionSet': 'StreamingSIMDExtensions', - 'EnableFiberSafeOptimizations': 'true', - 'EnablePREfast': 'true', - 'ErrorReporting': 'Queue', - 'ExceptionHandling': 'Async', - 'ExpandAttributedSource': 'true', - 'FavorSizeOrSpeed': 'Size', - 'FloatingPointExceptions': 'true', - 'FloatingPointModel': 'Strict', - 'ForceConformanceInForLoopScope': 'false', - 'ForcedIncludeFiles': 'def', - 'ForcedUsingFiles': 'ge', - 'FunctionLevelLinking': 'true', - 'GenerateXMLDocumentationFiles': 'true', - 'IgnoreStandardIncludePath': 'true', - 'InlineFunctionExpansion': 'OnlyExplicitInline', - 'IntrinsicFunctions': 'true', - 'MinimalRebuild': 'true', - 'ObjectFileName': '$(IntDir)b', - 'OmitDefaultLibName': 'true', - 'OmitFramePointers': 'true', - 'OpenMPSupport': 'true', - 'Optimization': 'Full', - 'PrecompiledHeader': 'NotUsing', # Actual conversion gives '' - 'PrecompiledHeaderFile': 'StdAfx.hd', - 'PrecompiledHeaderOutputFile': '$(IntDir)$(TargetName).pche', - 'PreprocessKeepComments': 'true', - 'PreprocessorDefinitions': 'WIN32;_DEBUG;_CONSOLE', - 'PreprocessSuppressLineNumbers': 'true', - 'PreprocessToFile': 'true', - 'ProgramDataBaseFileName': '$(IntDir)vc90b.pdb', - 'RuntimeLibrary': 'MultiThreadedDebugDLL', - 'RuntimeTypeInfo': 'false', - 'ShowIncludes': 'true', - 'SmallerTypeCheck': 'true', - 'StringPooling': 'true', - 'StructMemberAlignment': '4Bytes', - 'SuppressStartupBanner': 'false', - 'TreatWarningAsError': 'true', - 'TreatWChar_tAsBuiltInType': 'false', - 'UndefineAllPreprocessorDefinitions': 'true', - 'UndefinePreprocessorDefinitions': 'wer', - 'UseFullPaths': 'true', - 'WarningLevel': 'Level3', - 'WholeProgramOptimization': 'true', - 'XMLDocumentationFileName': '$(IntDir)c'}, - 'Link': { - 'AdditionalDependencies': 'zx', - 'AdditionalLibraryDirectories': 'asd', - 'AdditionalManifestDependencies': 's2', - 'AdditionalOptions': '/mor2', - 'AddModuleNamesToAssembly': 'd1', - 'AllowIsolation': 'false', - 'AssemblyDebug': 'true', - 'AssemblyLinkResource': 'd5', - 'BaseAddress': '23423', - 'CLRImageType': 'ForceSafeILImage', - 'CLRThreadAttribute': 'MTAThreadingAttribute', - 'CLRUnmanagedCodeCheck': 'true', - 'DataExecutionPrevention': '', - 'DelayLoadDLLs': 'd4', - 'DelaySign': 'true', - 'Driver': 'UpOnly', - 'EmbedManagedResourceFile': 'd2', - 'EnableCOMDATFolding': 'false', - 'EnableUAC': 'false', - 'EntryPointSymbol': 'f5', - 'FixedBaseAddress': 'false', - 'ForceSymbolReferences': 'd3', - 'FunctionOrder': 'fssdfsd', - 'GenerateDebugInformation': 'true', - 'GenerateMapFile': 'true', - 'HeapCommitSize': '13', - 'HeapReserveSize': '12', - 'IgnoreAllDefaultLibraries': 'true', - 'IgnoreEmbeddedIDL': 'true', - 'IgnoreSpecificDefaultLibraries': 'flob;flok', - 'ImportLibrary': 'f4', - 'KeyContainer': 'f7', - 'KeyFile': 'f6', - 'LargeAddressAware': 'true', - 'LinkErrorReporting': 'QueueForNextLogin', - 'LinkTimeCodeGeneration': 'UseLinkTimeCodeGeneration', - 'ManifestFile': '$(IntDir)$(TargetFileName).2intermediate.manifest', - 'MapExports': 'true', - 'MapFileName': 'd5', - 'MergedIDLBaseFileName': 'f2', - 'MergeSections': 'f5', - 'MidlCommandFile': 'f1', - 'ModuleDefinitionFile': 'sdsd', - 'NoEntryPoint': 'true', - 'OptimizeReferences': 'true', - 'OutputFile': '$(OutDir)$(ProjectName)2.exe', - 'PerUserRedirection': 'true', - 'Profile': 'true', - 'ProfileGuidedDatabase': '$(TargetDir)$(TargetName).pgdd', - 'ProgramDatabaseFile': 'Flob.pdb', - 'RandomizedBaseAddress': 'false', - 'RegisterOutput': 'true', - 'SetChecksum': 'false', - 'ShowProgress': 'LinkVerbose', - 'StackCommitSize': '15', - 'StackReserveSize': '14', - 'StripPrivateSymbols': 'd3', - 'SubSystem': 'Console', - 'SupportUnloadOfDelayLoadedDLL': 'true', - 'SuppressStartupBanner': 'false', - 'SwapRunFromCD': 'true', - 'SwapRunFromNET': 'true', - 'TargetMachine': 'MachineX86', - 'TerminalServerAware': 'false', - 'TurnOffAssemblyGeneration': 'true', - 'TypeLibraryFile': 'f3', - 'TypeLibraryResourceID': '12', - 'UACExecutionLevel': 'RequireAdministrator', - 'UACUIAccess': 'true', - 'Version': '333'}, - 'ResourceCompile': { - 'AdditionalIncludeDirectories': 'f3', - 'AdditionalOptions': '/more3', - 'Culture': '0x0c0c', - 'IgnoreStandardIncludePath': 'true', - 'PreprocessorDefinitions': '_UNICODE;UNICODE2', - 'ResourceOutputFileName': '$(IntDir)%(Filename)3.res', - 'ShowProgress': 'true'}, - 'Manifest': { - 'AdditionalManifestFiles': 'sfsdfsd', - 'AdditionalOptions': 'afdsdafsd', - 'AssemblyIdentity': 'sddfdsadfsa', - 'ComponentFileName': 'fsdfds', - 'GenerateCatalogFiles': 'true', - 'InputResourceManifests': 'asfsfdafs', - 'OutputManifestFile': '$(TargetPath).manifestdfs', - 'RegistrarScriptFile': 'sdfsfd', - 'ReplacementsFile': 'sdffsd', - 'SuppressStartupBanner': 'false', - 'TypeLibraryFile': 'sfsd', - 'UpdateFileHashes': 'true', - 'UpdateFileHashesSearchPath': 'sfsd', - 'VerboseOutput': 'true'}, - 'ProjectReference': { - 'LinkLibraryDependencies': 'false', - 'UseLibraryDependencyInputs': 'true'}, - '': { - 'EmbedManifest': 'false', - 'GenerateManifest': 'false', - 'IgnoreImportLibrary': 'true', - 'LinkIncremental': '' + msvs_settings = { + "VCCLCompilerTool": { + "AdditionalIncludeDirectories": "dir1", + "AdditionalOptions": "/more", + "AdditionalUsingDirectories": "test", + "AssemblerListingLocation": "$(IntDir)\\a", + "AssemblerOutput": "1", + "BasicRuntimeChecks": "3", + "BrowseInformation": "1", + "BrowseInformationFile": "$(IntDir)\\e", + "BufferSecurityCheck": "false", + "CallingConvention": "1", + "CompileAs": "1", + "DebugInformationFormat": "4", + "DefaultCharIsUnsigned": "true", + "Detect64BitPortabilityProblems": "true", + "DisableLanguageExtensions": "true", + "DisableSpecificWarnings": "abc", + "EnableEnhancedInstructionSet": "1", + "EnableFiberSafeOptimizations": "true", + "EnableFunctionLevelLinking": "true", + "EnableIntrinsicFunctions": "true", + "EnablePREfast": "true", + "ErrorReporting": "2", + "ExceptionHandling": "2", + "ExpandAttributedSource": "true", + "FavorSizeOrSpeed": "2", + "FloatingPointExceptions": "true", + "FloatingPointModel": "1", + "ForceConformanceInForLoopScope": "false", + "ForcedIncludeFiles": "def", + "ForcedUsingFiles": "ge", + "GeneratePreprocessedFile": "2", + "GenerateXMLDocumentationFiles": "true", + "IgnoreStandardIncludePath": "true", + "InlineFunctionExpansion": "1", + "KeepComments": "true", + "MinimalRebuild": "true", + "ObjectFile": "$(IntDir)\\b", + "OmitDefaultLibName": "true", + "OmitFramePointers": "true", + "OpenMP": "true", + "Optimization": "3", + "PrecompiledHeaderFile": "$(IntDir)\\$(TargetName).pche", + "PrecompiledHeaderThrough": "StdAfx.hd", + "PreprocessorDefinitions": "WIN32;_DEBUG;_CONSOLE", + "ProgramDataBaseFileName": "$(IntDir)\\vc90b.pdb", + "RuntimeLibrary": "3", + "RuntimeTypeInfo": "false", + "ShowIncludes": "true", + "SmallerTypeCheck": "true", + "StringPooling": "true", + "StructMemberAlignment": "3", + "SuppressStartupBanner": "false", + "TreatWChar_tAsBuiltInType": "false", + "UndefineAllPreprocessorDefinitions": "true", + "UndefinePreprocessorDefinitions": "wer", + "UseFullPaths": "true", + "UsePrecompiledHeader": "0", + "UseUnicodeResponseFiles": "false", + "WarnAsError": "true", + "WarningLevel": "3", + "WholeProgramOptimization": "true", + "XMLDocumentationFileName": "$(IntDir)\\c", + }, + "VCLinkerTool": { + "AdditionalDependencies": "zx", + "AdditionalLibraryDirectories": "asd", + "AdditionalManifestDependencies": "s2", + "AdditionalOptions": "/mor2", + "AddModuleNamesToAssembly": "d1", + "AllowIsolation": "false", + "AssemblyDebug": "1", + "AssemblyLinkResource": "d5", + "BaseAddress": "23423", + "CLRImageType": "3", + "CLRThreadAttribute": "1", + "CLRUnmanagedCodeCheck": "true", + "DataExecutionPrevention": "0", + "DelayLoadDLLs": "d4", + "DelaySign": "true", + "Driver": "2", + "EmbedManagedResourceFile": "d2", + "EnableCOMDATFolding": "1", + "EnableUAC": "false", + "EntryPointSymbol": "f5", + "ErrorReporting": "2", + "FixedBaseAddress": "1", + "ForceSymbolReferences": "d3", + "FunctionOrder": "fssdfsd", + "GenerateDebugInformation": "true", + "GenerateManifest": "false", + "GenerateMapFile": "true", + "HeapCommitSize": "13", + "HeapReserveSize": "12", + "IgnoreAllDefaultLibraries": "true", + "IgnoreDefaultLibraryNames": "flob;flok", + "IgnoreEmbeddedIDL": "true", + "IgnoreImportLibrary": "true", + "ImportLibrary": "f4", + "KeyContainer": "f7", + "KeyFile": "f6", + "LargeAddressAware": "2", + "LinkIncremental": "0", + "LinkLibraryDependencies": "false", + "LinkTimeCodeGeneration": "1", + "ManifestFile": "$(IntDir)\\$(TargetFileName).2intermediate.manifest", + "MapExports": "true", + "MapFileName": "d5", + "MergedIDLBaseFileName": "f2", + "MergeSections": "f5", + "MidlCommandFile": "f1", + "ModuleDefinitionFile": "sdsd", + "OptimizeForWindows98": "2", + "OptimizeReferences": "2", + "OutputFile": "$(OutDir)\\$(ProjectName)2.exe", + "PerUserRedirection": "true", + "Profile": "true", + "ProfileGuidedDatabase": "$(TargetDir)$(TargetName).pgdd", + "ProgramDatabaseFile": "Flob.pdb", + "RandomizedBaseAddress": "1", + "RegisterOutput": "true", + "ResourceOnlyDLL": "true", + "SetChecksum": "false", + "ShowProgress": "1", + "StackCommitSize": "15", + "StackReserveSize": "14", + "StripPrivateSymbols": "d3", + "SubSystem": "1", + "SupportUnloadOfDelayLoadedDLL": "true", + "SuppressStartupBanner": "false", + "SwapRunFromCD": "true", + "SwapRunFromNet": "true", + "TargetMachine": "1", + "TerminalServerAware": "1", + "TurnOffAssemblyGeneration": "true", + "TypeLibraryFile": "f3", + "TypeLibraryResourceID": "12", + "UACExecutionLevel": "2", + "UACUIAccess": "true", + "UseLibraryDependencyInputs": "true", + "UseUnicodeResponseFiles": "false", + "Version": "333", + }, + "VCResourceCompilerTool": { + "AdditionalIncludeDirectories": "f3", + "AdditionalOptions": "/more3", + "Culture": "3084", + "IgnoreStandardIncludePath": "true", + "PreprocessorDefinitions": "_UNICODE;UNICODE2", + "ResourceOutputFileName": "$(IntDir)/$(InputName)3.res", + "ShowProgress": "true", + }, + "VCManifestTool": { + "AdditionalManifestFiles": "sfsdfsd", + "AdditionalOptions": "afdsdafsd", + "AssemblyIdentity": "sddfdsadfsa", + "ComponentFileName": "fsdfds", + "DependencyInformationFile": "$(IntDir)\\mt.depdfd", + "EmbedManifest": "false", + "GenerateCatalogFiles": "true", + "InputResourceManifests": "asfsfdafs", + "ManifestResourceFile": + "$(IntDir)\\$(TargetFileName).embed.manifest.resfdsf", + "OutputManifestFile": "$(TargetPath).manifestdfs", + "RegistrarScriptFile": "sdfsfd", + "ReplacementsFile": "sdffsd", + "SuppressStartupBanner": "false", + "TypeLibraryFile": "sfsd", + "UpdateFileHashes": "true", + "UpdateFileHashesSearchPath": "sfsd", + "UseFAT32Workaround": "true", + "UseUnicodeResponseFiles": "false", + "VerboseOutput": "true", + }, + } + expected_msbuild_settings = { + "ClCompile": { + "AdditionalIncludeDirectories": "dir1", + "AdditionalOptions": "/more /J", + "AdditionalUsingDirectories": "test", + "AssemblerListingLocation": "$(IntDir)a", + "AssemblerOutput": "AssemblyCode", + "BasicRuntimeChecks": "EnableFastChecks", + "BrowseInformation": "true", + "BrowseInformationFile": "$(IntDir)e", + "BufferSecurityCheck": "false", + "CallingConvention": "FastCall", + "CompileAs": "CompileAsC", + "DebugInformationFormat": "EditAndContinue", + "DisableLanguageExtensions": "true", + "DisableSpecificWarnings": "abc", + "EnableEnhancedInstructionSet": "StreamingSIMDExtensions", + "EnableFiberSafeOptimizations": "true", + "EnablePREfast": "true", + "ErrorReporting": "Queue", + "ExceptionHandling": "Async", + "ExpandAttributedSource": "true", + "FavorSizeOrSpeed": "Size", + "FloatingPointExceptions": "true", + "FloatingPointModel": "Strict", + "ForceConformanceInForLoopScope": "false", + "ForcedIncludeFiles": "def", + "ForcedUsingFiles": "ge", + "FunctionLevelLinking": "true", + "GenerateXMLDocumentationFiles": "true", + "IgnoreStandardIncludePath": "true", + "InlineFunctionExpansion": "OnlyExplicitInline", + "IntrinsicFunctions": "true", + "MinimalRebuild": "true", + "ObjectFileName": "$(IntDir)b", + "OmitDefaultLibName": "true", + "OmitFramePointers": "true", + "OpenMPSupport": "true", + "Optimization": "Full", + "PrecompiledHeader": "NotUsing", # Actual conversion gives '' + "PrecompiledHeaderFile": "StdAfx.hd", + "PrecompiledHeaderOutputFile": "$(IntDir)$(TargetName).pche", + "PreprocessKeepComments": "true", + "PreprocessorDefinitions": "WIN32;_DEBUG;_CONSOLE", + "PreprocessSuppressLineNumbers": "true", + "PreprocessToFile": "true", + "ProgramDataBaseFileName": "$(IntDir)vc90b.pdb", + "RuntimeLibrary": "MultiThreadedDebugDLL", + "RuntimeTypeInfo": "false", + "ShowIncludes": "true", + "SmallerTypeCheck": "true", + "StringPooling": "true", + "StructMemberAlignment": "4Bytes", + "SuppressStartupBanner": "false", + "TreatWarningAsError": "true", + "TreatWChar_tAsBuiltInType": "false", + "UndefineAllPreprocessorDefinitions": "true", + "UndefinePreprocessorDefinitions": "wer", + "UseFullPaths": "true", + "WarningLevel": "Level3", + "WholeProgramOptimization": "true", + "XMLDocumentationFileName": "$(IntDir)c", + }, + "Link": { + "AdditionalDependencies": "zx", + "AdditionalLibraryDirectories": "asd", + "AdditionalManifestDependencies": "s2", + "AdditionalOptions": "/mor2", + "AddModuleNamesToAssembly": "d1", + "AllowIsolation": "false", + "AssemblyDebug": "true", + "AssemblyLinkResource": "d5", + "BaseAddress": "23423", + "CLRImageType": "ForceSafeILImage", + "CLRThreadAttribute": "MTAThreadingAttribute", + "CLRUnmanagedCodeCheck": "true", + "DataExecutionPrevention": "", + "DelayLoadDLLs": "d4", + "DelaySign": "true", + "Driver": "UpOnly", + "EmbedManagedResourceFile": "d2", + "EnableCOMDATFolding": "false", + "EnableUAC": "false", + "EntryPointSymbol": "f5", + "FixedBaseAddress": "false", + "ForceSymbolReferences": "d3", + "FunctionOrder": "fssdfsd", + "GenerateDebugInformation": "true", + "GenerateMapFile": "true", + "HeapCommitSize": "13", + "HeapReserveSize": "12", + "IgnoreAllDefaultLibraries": "true", + "IgnoreEmbeddedIDL": "true", + "IgnoreSpecificDefaultLibraries": "flob;flok", + "ImportLibrary": "f4", + "KeyContainer": "f7", + "KeyFile": "f6", + "LargeAddressAware": "true", + "LinkErrorReporting": "QueueForNextLogin", + "LinkTimeCodeGeneration": "UseLinkTimeCodeGeneration", + "ManifestFile": "$(IntDir)$(TargetFileName).2intermediate.manifest", + "MapExports": "true", + "MapFileName": "d5", + "MergedIDLBaseFileName": "f2", + "MergeSections": "f5", + "MidlCommandFile": "f1", + "ModuleDefinitionFile": "sdsd", + "NoEntryPoint": "true", + "OptimizeReferences": "true", + "OutputFile": "$(OutDir)$(ProjectName)2.exe", + "PerUserRedirection": "true", + "Profile": "true", + "ProfileGuidedDatabase": "$(TargetDir)$(TargetName).pgdd", + "ProgramDatabaseFile": "Flob.pdb", + "RandomizedBaseAddress": "false", + "RegisterOutput": "true", + "SetChecksum": "false", + "ShowProgress": "LinkVerbose", + "StackCommitSize": "15", + "StackReserveSize": "14", + "StripPrivateSymbols": "d3", + "SubSystem": "Console", + "SupportUnloadOfDelayLoadedDLL": "true", + "SuppressStartupBanner": "false", + "SwapRunFromCD": "true", + "SwapRunFromNET": "true", + "TargetMachine": "MachineX86", + "TerminalServerAware": "false", + "TurnOffAssemblyGeneration": "true", + "TypeLibraryFile": "f3", + "TypeLibraryResourceID": "12", + "UACExecutionLevel": "RequireAdministrator", + "UACUIAccess": "true", + "Version": "333", + }, + "ResourceCompile": { + "AdditionalIncludeDirectories": "f3", + "AdditionalOptions": "/more3", + "Culture": "0x0c0c", + "IgnoreStandardIncludePath": "true", + "PreprocessorDefinitions": "_UNICODE;UNICODE2", + "ResourceOutputFileName": "$(IntDir)%(Filename)3.res", + "ShowProgress": "true", + }, + "Manifest": { + "AdditionalManifestFiles": "sfsdfsd", + "AdditionalOptions": "afdsdafsd", + "AssemblyIdentity": "sddfdsadfsa", + "ComponentFileName": "fsdfds", + "GenerateCatalogFiles": "true", + "InputResourceManifests": "asfsfdafs", + "OutputManifestFile": "$(TargetPath).manifestdfs", + "RegistrarScriptFile": "sdfsfd", + "ReplacementsFile": "sdffsd", + "SuppressStartupBanner": "false", + "TypeLibraryFile": "sfsd", + "UpdateFileHashes": "true", + "UpdateFileHashesSearchPath": "sfsd", + "VerboseOutput": "true", + }, + "ProjectReference": { + "LinkLibraryDependencies": "false", + "UseLibraryDependencyInputs": "true", + }, + "": { + "EmbedManifest": "false", + "GenerateManifest": "false", + "IgnoreImportLibrary": "true", + "LinkIncremental": "", + }, + "ManifestResourceCompile": { + "ResourceOutputFileName": + "$(IntDir)$(TargetFileName).embed.manifest.resfdsf" }, - 'ManifestResourceCompile': { - 'ResourceOutputFileName': - '$(IntDir)$(TargetFileName).embed.manifest.resfdsf'} } - self.maxDiff = 9999 # on failure display a long diff - actual_msbuild_settings = MSVSSettings.ConvertToMSBuildSettings( - msvs_settings, - self.stderr) - self.assertEqual(expected_msbuild_settings, actual_msbuild_settings) - self._ExpectedWarnings([]) + self.maxDiff = 9999 # on failure display a long diff + actual_msbuild_settings = MSVSSettings.ConvertToMSBuildSettings( + msvs_settings, self.stderr + ) + self.assertEqual(expected_msbuild_settings, actual_msbuild_settings) + self._ExpectedWarnings([]) -if __name__ == '__main__': - unittest.main() +if __name__ == "__main__": + unittest.main() diff --git a/tools/gyp/pylib/gyp/MSVSToolFile.py b/tools/gyp/pylib/gyp/MSVSToolFile.py index 74e529a17f1dca..2c08589e06dc6d 100644 --- a/tools/gyp/pylib/gyp/MSVSToolFile.py +++ b/tools/gyp/pylib/gyp/MSVSToolFile.py @@ -4,28 +4,27 @@ """Visual Studio project reader/writer.""" -import gyp.common import gyp.easy_xml as easy_xml class Writer(object): - """Visual Studio XML tool file writer.""" + """Visual Studio XML tool file writer.""" - def __init__(self, tool_file_path, name): - """Initializes the tool file. + def __init__(self, tool_file_path, name): + """Initializes the tool file. Args: tool_file_path: Path to the tool file. name: Name of the tool file. """ - self.tool_file_path = tool_file_path - self.name = name - self.rules_section = ['Rules'] + self.tool_file_path = tool_file_path + self.name = name + self.rules_section = ["Rules"] - def AddCustomBuildRule(self, name, cmd, description, - additional_dependencies, - outputs, extensions): - """Adds a rule to the tool file. + def AddCustomBuildRule( + self, name, cmd, description, additional_dependencies, outputs, extensions + ): + """Adds a rule to the tool file. Args: name: Name of the rule. @@ -35,24 +34,26 @@ def AddCustomBuildRule(self, name, cmd, description, outputs: outputs of the rule. extensions: extensions handled by the rule. """ - rule = ['CustomBuildRule', - {'Name': name, - 'ExecutionDescription': description, - 'CommandLine': cmd, - 'Outputs': ';'.join(outputs), - 'FileExtensions': ';'.join(extensions), - 'AdditionalDependencies': - ';'.join(additional_dependencies) - }] - self.rules_section.append(rule) - - def WriteIfChanged(self): - """Writes the tool file.""" - content = ['VisualStudioToolFile', - {'Version': '8.00', - 'Name': self.name - }, - self.rules_section - ] - easy_xml.WriteXmlIfChanged(content, self.tool_file_path, - encoding="Windows-1252") + rule = [ + "CustomBuildRule", + { + "Name": name, + "ExecutionDescription": description, + "CommandLine": cmd, + "Outputs": ";".join(outputs), + "FileExtensions": ";".join(extensions), + "AdditionalDependencies": ";".join(additional_dependencies), + }, + ] + self.rules_section.append(rule) + + def WriteIfChanged(self): + """Writes the tool file.""" + content = [ + "VisualStudioToolFile", + {"Version": "8.00", "Name": self.name}, + self.rules_section, + ] + easy_xml.WriteXmlIfChanged( + content, self.tool_file_path, encoding="Windows-1252" + ) diff --git a/tools/gyp/pylib/gyp/MSVSUserFile.py b/tools/gyp/pylib/gyp/MSVSUserFile.py index 2264d640152a29..de0896e6931816 100644 --- a/tools/gyp/pylib/gyp/MSVSUserFile.py +++ b/tools/gyp/pylib/gyp/MSVSUserFile.py @@ -6,78 +6,81 @@ import os import re -import socket # for gethostname +import socket # for gethostname -import gyp.common import gyp.easy_xml as easy_xml -#------------------------------------------------------------------------------ +# ------------------------------------------------------------------------------ + def _FindCommandInPath(command): - """If there are no slashes in the command given, this function + """If there are no slashes in the command given, this function searches the PATH env to find the given command, and converts it to an absolute path. We have to do this because MSVS is looking for an actual file to launch a debugger on, not just a command line. Note that this happens at GYP time, so anything needing to be built needs to have a full path.""" - if '/' in command or '\\' in command: - # If the command already has path elements (either relative or - # absolute), then assume it is constructed properly. + if "/" in command or "\\" in command: + # If the command already has path elements (either relative or + # absolute), then assume it is constructed properly. + return command + else: + # Search through the path list and find an existing file that + # we can access. + paths = os.environ.get("PATH", "").split(os.pathsep) + for path in paths: + item = os.path.join(path, command) + if os.path.isfile(item) and os.access(item, os.X_OK): + return item return command - else: - # Search through the path list and find an existing file that - # we can access. - paths = os.environ.get('PATH','').split(os.pathsep) - for path in paths: - item = os.path.join(path, command) - if os.path.isfile(item) and os.access(item, os.X_OK): - return item - return command + def _QuoteWin32CommandLineArgs(args): - new_args = [] - for arg in args: - # Replace all double-quotes with double-double-quotes to escape - # them for cmd shell, and then quote the whole thing if there - # are any. - if arg.find('"') != -1: - arg = '""'.join(arg.split('"')) - arg = '"%s"' % arg - - # Otherwise, if there are any spaces, quote the whole arg. - elif re.search(r'[ \t\n]', arg): - arg = '"%s"' % arg - new_args.append(arg) - return new_args + new_args = [] + for arg in args: + # Replace all double-quotes with double-double-quotes to escape + # them for cmd shell, and then quote the whole thing if there + # are any. + if arg.find('"') != -1: + arg = '""'.join(arg.split('"')) + arg = '"%s"' % arg + + # Otherwise, if there are any spaces, quote the whole arg. + elif re.search(r"[ \t\n]", arg): + arg = '"%s"' % arg + new_args.append(arg) + return new_args + class Writer(object): - """Visual Studio XML user user file writer.""" + """Visual Studio XML user user file writer.""" - def __init__(self, user_file_path, version, name): - """Initializes the user file. + def __init__(self, user_file_path, version, name): + """Initializes the user file. Args: user_file_path: Path to the user file. version: Version info. name: Name of the user file. """ - self.user_file_path = user_file_path - self.version = version - self.name = name - self.configurations = {} + self.user_file_path = user_file_path + self.version = version + self.name = name + self.configurations = {} - def AddConfig(self, name): - """Adds a configuration to the project. + def AddConfig(self, name): + """Adds a configuration to the project. Args: name: Configuration name. """ - self.configurations[name] = ['Configuration', {'Name': name}] + self.configurations[name] = ["Configuration", {"Name": name}] - def AddDebugSettings(self, config_name, command, environment = {}, - working_directory=""): - """Adds a DebugSettings node to the user file for a particular config. + def AddDebugSettings( + self, config_name, command, environment={}, working_directory="" + ): + """Adds a DebugSettings node to the user file for a particular config. Args: command: command line to run. First element in the list is the @@ -85,63 +88,66 @@ def AddDebugSettings(self, config_name, command, environment = {}, necessary. working_directory: other files which may trigger the rule. (optional) """ - command = _QuoteWin32CommandLineArgs(command) - - abs_command = _FindCommandInPath(command[0]) - - if environment and isinstance(environment, dict): - env_list = ['%s="%s"' % (key, val) - for (key,val) in environment.items()] - environment = ' '.join(env_list) - else: - environment = '' - - n_cmd = ['DebugSettings', - {'Command': abs_command, - 'WorkingDirectory': working_directory, - 'CommandArguments': " ".join(command[1:]), - 'RemoteMachine': socket.gethostname(), - 'Environment': environment, - 'EnvironmentMerge': 'true', - # Currently these are all "dummy" values that we're just setting - # in the default manner that MSVS does it. We could use some of - # these to add additional capabilities, I suppose, but they might - # not have parity with other platforms then. - 'Attach': 'false', - 'DebuggerType': '3', # 'auto' debugger - 'Remote': '1', - 'RemoteCommand': '', - 'HttpUrl': '', - 'PDBPath': '', - 'SQLDebugging': '', - 'DebuggerFlavor': '0', - 'MPIRunCommand': '', - 'MPIRunArguments': '', - 'MPIRunWorkingDirectory': '', - 'ApplicationCommand': '', - 'ApplicationArguments': '', - 'ShimCommand': '', - 'MPIAcceptMode': '', - 'MPIAcceptFilter': '' - }] - - # Find the config, and add it if it doesn't exist. - if config_name not in self.configurations: - self.AddConfig(config_name) - - # Add the DebugSettings onto the appropriate config. - self.configurations[config_name].append(n_cmd) - - def WriteIfChanged(self): - """Writes the user file.""" - configs = ['Configurations'] - for config, spec in sorted(self.configurations.items()): - configs.append(spec) - - content = ['VisualStudioUserFile', - {'Version': self.version.ProjectVersion(), - 'Name': self.name - }, - configs] - easy_xml.WriteXmlIfChanged(content, self.user_file_path, - encoding="Windows-1252") + command = _QuoteWin32CommandLineArgs(command) + + abs_command = _FindCommandInPath(command[0]) + + if environment and isinstance(environment, dict): + env_list = ['%s="%s"' % (key, val) for (key, val) in environment.items()] + environment = " ".join(env_list) + else: + environment = "" + + n_cmd = [ + "DebugSettings", + { + "Command": abs_command, + "WorkingDirectory": working_directory, + "CommandArguments": " ".join(command[1:]), + "RemoteMachine": socket.gethostname(), + "Environment": environment, + "EnvironmentMerge": "true", + # Currently these are all "dummy" values that we're just setting + # in the default manner that MSVS does it. We could use some of + # these to add additional capabilities, I suppose, but they might + # not have parity with other platforms then. + "Attach": "false", + "DebuggerType": "3", # 'auto' debugger + "Remote": "1", + "RemoteCommand": "", + "HttpUrl": "", + "PDBPath": "", + "SQLDebugging": "", + "DebuggerFlavor": "0", + "MPIRunCommand": "", + "MPIRunArguments": "", + "MPIRunWorkingDirectory": "", + "ApplicationCommand": "", + "ApplicationArguments": "", + "ShimCommand": "", + "MPIAcceptMode": "", + "MPIAcceptFilter": "", + }, + ] + + # Find the config, and add it if it doesn't exist. + if config_name not in self.configurations: + self.AddConfig(config_name) + + # Add the DebugSettings onto the appropriate config. + self.configurations[config_name].append(n_cmd) + + def WriteIfChanged(self): + """Writes the user file.""" + configs = ["Configurations"] + for config, spec in sorted(self.configurations.items()): + configs.append(spec) + + content = [ + "VisualStudioUserFile", + {"Version": self.version.ProjectVersion(), "Name": self.name}, + configs, + ] + easy_xml.WriteXmlIfChanged( + content, self.user_file_path, encoding="Windows-1252" + ) diff --git a/tools/gyp/pylib/gyp/MSVSUtil.py b/tools/gyp/pylib/gyp/MSVSUtil.py index f24530b275f7cd..83a9c297ed6e14 100644 --- a/tools/gyp/pylib/gyp/MSVSUtil.py +++ b/tools/gyp/pylib/gyp/MSVSUtil.py @@ -10,25 +10,25 @@ # A dictionary mapping supported target types to extensions. TARGET_TYPE_EXT = { - 'executable': 'exe', - 'loadable_module': 'dll', - 'shared_library': 'dll', - 'static_library': 'lib', - 'windows_driver': 'sys', + "executable": "exe", + "loadable_module": "dll", + "shared_library": "dll", + "static_library": "lib", + "windows_driver": "sys", } def _GetLargePdbShimCcPath(): - """Returns the path of the large_pdb_shim.cc file.""" - this_dir = os.path.abspath(os.path.dirname(__file__)) - src_dir = os.path.abspath(os.path.join(this_dir, '..', '..')) - win_data_dir = os.path.join(src_dir, 'data', 'win') - large_pdb_shim_cc = os.path.join(win_data_dir, 'large-pdb-shim.cc') - return large_pdb_shim_cc + """Returns the path of the large_pdb_shim.cc file.""" + this_dir = os.path.abspath(os.path.dirname(__file__)) + src_dir = os.path.abspath(os.path.join(this_dir, "..", "..")) + win_data_dir = os.path.join(src_dir, "data", "win") + large_pdb_shim_cc = os.path.join(win_data_dir, "large-pdb-shim.cc") + return large_pdb_shim_cc def _DeepCopySomeKeys(in_dict, keys): - """Performs a partial deep-copy on |in_dict|, only copying the keys in |keys|. + """Performs a partial deep-copy on |in_dict|, only copying the keys in |keys|. Arguments: in_dict: The dictionary to copy. @@ -37,16 +37,16 @@ def _DeepCopySomeKeys(in_dict, keys): Returns: The partially deep-copied dictionary. """ - d = {} - for key in keys: - if key not in in_dict: - continue - d[key] = copy.deepcopy(in_dict[key]) - return d + d = {} + for key in keys: + if key not in in_dict: + continue + d[key] = copy.deepcopy(in_dict[key]) + return d def _SuffixName(name, suffix): - """Add a suffix to the end of a target. + """Add a suffix to the end of a target. Arguments: name: name of the target (foo#target) @@ -54,13 +54,13 @@ def _SuffixName(name, suffix): Returns: Target name with suffix added (foo_suffix#target) """ - parts = name.rsplit('#', 1) - parts[0] = '%s_%s' % (parts[0], suffix) - return '#'.join(parts) + parts = name.rsplit("#", 1) + parts[0] = "%s_%s" % (parts[0], suffix) + return "#".join(parts) def _ShardName(name, number): - """Add a shard number to the end of a target. + """Add a shard number to the end of a target. Arguments: name: name of the target (foo#target) @@ -68,11 +68,11 @@ def _ShardName(name, number): Returns: Target name with shard added (foo_1#target) """ - return _SuffixName(name, str(number)) + return _SuffixName(name, str(number)) def ShardTargets(target_list, target_dicts): - """Shard some targets apart to work around the linkers limits. + """Shard some targets apart to work around the linkers limits. Arguments: target_list: List of target pairs: 'base/base.gyp:base'. @@ -80,54 +80,55 @@ def ShardTargets(target_list, target_dicts): Returns: Tuple of the new sharded versions of the inputs. """ - # Gather the targets to shard, and how many pieces. - targets_to_shard = {} - for t in target_dicts: - shards = int(target_dicts[t].get('msvs_shard', 0)) - if shards: - targets_to_shard[t] = shards - # Shard target_list. - new_target_list = [] - for t in target_list: - if t in targets_to_shard: - for i in range(targets_to_shard[t]): - new_target_list.append(_ShardName(t, i)) - else: - new_target_list.append(t) - # Shard target_dict. - new_target_dicts = {} - for t in target_dicts: - if t in targets_to_shard: - for i in range(targets_to_shard[t]): - name = _ShardName(t, i) - new_target_dicts[name] = copy.copy(target_dicts[t]) - new_target_dicts[name]['target_name'] = _ShardName( - new_target_dicts[name]['target_name'], i) - sources = new_target_dicts[name].get('sources', []) - new_sources = [] - for pos in range(i, len(sources), targets_to_shard[t]): - new_sources.append(sources[pos]) - new_target_dicts[name]['sources'] = new_sources - else: - new_target_dicts[t] = target_dicts[t] - # Shard dependencies. - for t in sorted(new_target_dicts): - for deptype in ('dependencies', 'dependencies_original'): - dependencies = copy.copy(new_target_dicts[t].get(deptype, [])) - new_dependencies = [] - for d in dependencies: - if d in targets_to_shard: - for i in range(targets_to_shard[d]): - new_dependencies.append(_ShardName(d, i)) + # Gather the targets to shard, and how many pieces. + targets_to_shard = {} + for t in target_dicts: + shards = int(target_dicts[t].get("msvs_shard", 0)) + if shards: + targets_to_shard[t] = shards + # Shard target_list. + new_target_list = [] + for t in target_list: + if t in targets_to_shard: + for i in range(targets_to_shard[t]): + new_target_list.append(_ShardName(t, i)) else: - new_dependencies.append(d) - new_target_dicts[t][deptype] = new_dependencies - - return (new_target_list, new_target_dicts) + new_target_list.append(t) + # Shard target_dict. + new_target_dicts = {} + for t in target_dicts: + if t in targets_to_shard: + for i in range(targets_to_shard[t]): + name = _ShardName(t, i) + new_target_dicts[name] = copy.copy(target_dicts[t]) + new_target_dicts[name]["target_name"] = _ShardName( + new_target_dicts[name]["target_name"], i + ) + sources = new_target_dicts[name].get("sources", []) + new_sources = [] + for pos in range(i, len(sources), targets_to_shard[t]): + new_sources.append(sources[pos]) + new_target_dicts[name]["sources"] = new_sources + else: + new_target_dicts[t] = target_dicts[t] + # Shard dependencies. + for t in sorted(new_target_dicts): + for deptype in ("dependencies", "dependencies_original"): + dependencies = copy.copy(new_target_dicts[t].get(deptype, [])) + new_dependencies = [] + for d in dependencies: + if d in targets_to_shard: + for i in range(targets_to_shard[d]): + new_dependencies.append(_ShardName(d, i)) + else: + new_dependencies.append(d) + new_target_dicts[t][deptype] = new_dependencies + + return (new_target_list, new_target_dicts) def _GetPdbPath(target_dict, config_name, vars): - """Returns the path to the PDB file that will be generated by a given + """Returns the path to the PDB file that will be generated by a given configuration. The lookup proceeds as follows: @@ -144,30 +145,29 @@ def _GetPdbPath(target_dict, config_name, vars): Returns: The path of the corresponding PDB file. """ - config = target_dict['configurations'][config_name] - msvs = config.setdefault('msvs_settings', {}) - - linker = msvs.get('VCLinkerTool', {}) + config = target_dict["configurations"][config_name] + msvs = config.setdefault("msvs_settings", {}) - pdb_path = linker.get('ProgramDatabaseFile') - if pdb_path: - return pdb_path + linker = msvs.get("VCLinkerTool", {}) - variables = target_dict.get('variables', {}) - pdb_path = variables.get('msvs_large_pdb_path', None) - if pdb_path: - return pdb_path + pdb_path = linker.get("ProgramDatabaseFile") + if pdb_path: + return pdb_path + variables = target_dict.get("variables", {}) + pdb_path = variables.get("msvs_large_pdb_path", None) + if pdb_path: + return pdb_path - pdb_base = target_dict.get('product_name', target_dict['target_name']) - pdb_base = '%s.%s.pdb' % (pdb_base, TARGET_TYPE_EXT[target_dict['type']]) - pdb_path = vars['PRODUCT_DIR'] + '/' + pdb_base + pdb_base = target_dict.get("product_name", target_dict["target_name"]) + pdb_base = "%s.%s.pdb" % (pdb_base, TARGET_TYPE_EXT[target_dict["type"]]) + pdb_path = vars["PRODUCT_DIR"] + "/" + pdb_base - return pdb_path + return pdb_path def InsertLargePdbShims(target_list, target_dicts, vars): - """Insert a shim target that forces the linker to use 4KB pagesize PDBs. + """Insert a shim target that forces the linker to use 4KB pagesize PDBs. This is a workaround for targets with PDBs greater than 1GB in size, the limit for the 1KB pagesize PDBs created by the linker by default. @@ -179,93 +179,93 @@ def InsertLargePdbShims(target_list, target_dicts, vars): Returns: Tuple of the shimmed version of the inputs. """ - # Determine which targets need shimming. - targets_to_shim = [] - for t in target_dicts: - target_dict = target_dicts[t] - - # We only want to shim targets that have msvs_large_pdb enabled. - if not int(target_dict.get('msvs_large_pdb', 0)): - continue - # This is intended for executable, shared_library and loadable_module - # targets where every configuration is set up to produce a PDB output. - # If any of these conditions is not true then the shim logic will fail - # below. - targets_to_shim.append(t) - - large_pdb_shim_cc = _GetLargePdbShimCcPath() - - for t in targets_to_shim: - target_dict = target_dicts[t] - target_name = target_dict.get('target_name') - - base_dict = _DeepCopySomeKeys(target_dict, - ['configurations', 'default_configuration', 'toolset']) - - # This is the dict for copying the source file (part of the GYP tree) - # to the intermediate directory of the project. This is necessary because - # we can't always build a relative path to the shim source file (on Windows - # GYP and the project may be on different drives), and Ninja hates absolute - # paths (it ends up generating the .obj and .obj.d alongside the source - # file, polluting GYPs tree). - copy_suffix = 'large_pdb_copy' - copy_target_name = target_name + '_' + copy_suffix - full_copy_target_name = _SuffixName(t, copy_suffix) - shim_cc_basename = os.path.basename(large_pdb_shim_cc) - shim_cc_dir = vars['SHARED_INTERMEDIATE_DIR'] + '/' + copy_target_name - shim_cc_path = shim_cc_dir + '/' + shim_cc_basename - copy_dict = copy.deepcopy(base_dict) - copy_dict['target_name'] = copy_target_name - copy_dict['type'] = 'none' - copy_dict['sources'] = [ large_pdb_shim_cc ] - copy_dict['copies'] = [{ - 'destination': shim_cc_dir, - 'files': [ large_pdb_shim_cc ] - }] - - # This is the dict for the PDB generating shim target. It depends on the - # copy target. - shim_suffix = 'large_pdb_shim' - shim_target_name = target_name + '_' + shim_suffix - full_shim_target_name = _SuffixName(t, shim_suffix) - shim_dict = copy.deepcopy(base_dict) - shim_dict['target_name'] = shim_target_name - shim_dict['type'] = 'static_library' - shim_dict['sources'] = [ shim_cc_path ] - shim_dict['dependencies'] = [ full_copy_target_name ] - - # Set up the shim to output its PDB to the same location as the final linker - # target. - for config_name, config in shim_dict.get('configurations').items(): - pdb_path = _GetPdbPath(target_dict, config_name, vars) - - # A few keys that we don't want to propagate. - for key in ['msvs_precompiled_header', 'msvs_precompiled_source', 'test']: - config.pop(key, None) - - msvs = config.setdefault('msvs_settings', {}) - - # Update the compiler directives in the shim target. - compiler = msvs.setdefault('VCCLCompilerTool', {}) - compiler['DebugInformationFormat'] = '3' - compiler['ProgramDataBaseFileName'] = pdb_path - - # Set the explicit PDB path in the appropriate configuration of the - # original target. - config = target_dict['configurations'][config_name] - msvs = config.setdefault('msvs_settings', {}) - linker = msvs.setdefault('VCLinkerTool', {}) - linker['GenerateDebugInformation'] = 'true' - linker['ProgramDatabaseFile'] = pdb_path - - # Add the new targets. They must go to the beginning of the list so that - # the dependency generation works as expected in ninja. - target_list.insert(0, full_copy_target_name) - target_list.insert(0, full_shim_target_name) - target_dicts[full_copy_target_name] = copy_dict - target_dicts[full_shim_target_name] = shim_dict - - # Update the original target to depend on the shim target. - target_dict.setdefault('dependencies', []).append(full_shim_target_name) - - return (target_list, target_dicts) + # Determine which targets need shimming. + targets_to_shim = [] + for t in target_dicts: + target_dict = target_dicts[t] + + # We only want to shim targets that have msvs_large_pdb enabled. + if not int(target_dict.get("msvs_large_pdb", 0)): + continue + # This is intended for executable, shared_library and loadable_module + # targets where every configuration is set up to produce a PDB output. + # If any of these conditions is not true then the shim logic will fail + # below. + targets_to_shim.append(t) + + large_pdb_shim_cc = _GetLargePdbShimCcPath() + + for t in targets_to_shim: + target_dict = target_dicts[t] + target_name = target_dict.get("target_name") + + base_dict = _DeepCopySomeKeys( + target_dict, ["configurations", "default_configuration", "toolset"] + ) + + # This is the dict for copying the source file (part of the GYP tree) + # to the intermediate directory of the project. This is necessary because + # we can't always build a relative path to the shim source file (on Windows + # GYP and the project may be on different drives), and Ninja hates absolute + # paths (it ends up generating the .obj and .obj.d alongside the source + # file, polluting GYPs tree). + copy_suffix = "large_pdb_copy" + copy_target_name = target_name + "_" + copy_suffix + full_copy_target_name = _SuffixName(t, copy_suffix) + shim_cc_basename = os.path.basename(large_pdb_shim_cc) + shim_cc_dir = vars["SHARED_INTERMEDIATE_DIR"] + "/" + copy_target_name + shim_cc_path = shim_cc_dir + "/" + shim_cc_basename + copy_dict = copy.deepcopy(base_dict) + copy_dict["target_name"] = copy_target_name + copy_dict["type"] = "none" + copy_dict["sources"] = [large_pdb_shim_cc] + copy_dict["copies"] = [ + {"destination": shim_cc_dir, "files": [large_pdb_shim_cc]} + ] + + # This is the dict for the PDB generating shim target. It depends on the + # copy target. + shim_suffix = "large_pdb_shim" + shim_target_name = target_name + "_" + shim_suffix + full_shim_target_name = _SuffixName(t, shim_suffix) + shim_dict = copy.deepcopy(base_dict) + shim_dict["target_name"] = shim_target_name + shim_dict["type"] = "static_library" + shim_dict["sources"] = [shim_cc_path] + shim_dict["dependencies"] = [full_copy_target_name] + + # Set up the shim to output its PDB to the same location as the final linker + # target. + for config_name, config in shim_dict.get("configurations").items(): + pdb_path = _GetPdbPath(target_dict, config_name, vars) + + # A few keys that we don't want to propagate. + for key in ["msvs_precompiled_header", "msvs_precompiled_source", "test"]: + config.pop(key, None) + + msvs = config.setdefault("msvs_settings", {}) + + # Update the compiler directives in the shim target. + compiler = msvs.setdefault("VCCLCompilerTool", {}) + compiler["DebugInformationFormat"] = "3" + compiler["ProgramDataBaseFileName"] = pdb_path + + # Set the explicit PDB path in the appropriate configuration of the + # original target. + config = target_dict["configurations"][config_name] + msvs = config.setdefault("msvs_settings", {}) + linker = msvs.setdefault("VCLinkerTool", {}) + linker["GenerateDebugInformation"] = "true" + linker["ProgramDatabaseFile"] = pdb_path + + # Add the new targets. They must go to the beginning of the list so that + # the dependency generation works as expected in ninja. + target_list.insert(0, full_copy_target_name) + target_list.insert(0, full_shim_target_name) + target_dicts[full_copy_target_name] = copy_dict + target_dicts[full_shim_target_name] = shim_dict + + # Update the original target to depend on the shim target. + target_dict.setdefault("dependencies", []).append(full_shim_target_name) + + return (target_list, target_dicts) diff --git a/tools/gyp/pylib/gyp/MSVSVersion.py b/tools/gyp/pylib/gyp/MSVSVersion.py index ce9b349834c947..36b006aaa93759 100644 --- a/tools/gyp/pylib/gyp/MSVSVersion.py +++ b/tools/gyp/pylib/gyp/MSVSVersion.py @@ -9,139 +9,152 @@ import re import subprocess import sys -import gyp import glob PY3 = bytes != str def JoinPath(*args): - return os.path.normpath(os.path.join(*args)) + return os.path.normpath(os.path.join(*args)) class VisualStudioVersion(object): - """Information regarding a version of Visual Studio.""" - - def __init__(self, short_name, description, - solution_version, project_version, flat_sln, uses_vcxproj, - path, sdk_based, default_toolset=None, compatible_sdks=None): - self.short_name = short_name - self.description = description - self.solution_version = solution_version - self.project_version = project_version - self.flat_sln = flat_sln - self.uses_vcxproj = uses_vcxproj - self.path = path - self.sdk_based = sdk_based - self.default_toolset = default_toolset - compatible_sdks = compatible_sdks or [] - compatible_sdks.sort(key=lambda v: float(v.replace('v', '')), reverse=True) - self.compatible_sdks = compatible_sdks - - def ShortName(self): - return self.short_name - - def Description(self): - """Get the full description of the version.""" - return self.description - - def SolutionVersion(self): - """Get the version number of the sln files.""" - return self.solution_version - - def ProjectVersion(self): - """Get the version number of the vcproj or vcxproj files.""" - return self.project_version - - def FlatSolution(self): - return self.flat_sln - - def UsesVcxproj(self): - """Returns true if this version uses a vcxproj file.""" - return self.uses_vcxproj - - def ProjectExtension(self): - """Returns the file extension for the project.""" - return self.uses_vcxproj and '.vcxproj' or '.vcproj' - - def Path(self): - """Returns the path to Visual Studio installation.""" - return self.path - - def ToolPath(self, tool): - """Returns the path to a given compiler tool. """ - return os.path.normpath(os.path.join(self.path, "VC/bin", tool)) - - def DefaultToolset(self): - """Returns the msbuild toolset version that will be used in the absence + """Information regarding a version of Visual Studio.""" + + def __init__( + self, + short_name, + description, + solution_version, + project_version, + flat_sln, + uses_vcxproj, + path, + sdk_based, + default_toolset=None, + compatible_sdks=None, + ): + self.short_name = short_name + self.description = description + self.solution_version = solution_version + self.project_version = project_version + self.flat_sln = flat_sln + self.uses_vcxproj = uses_vcxproj + self.path = path + self.sdk_based = sdk_based + self.default_toolset = default_toolset + compatible_sdks = compatible_sdks or [] + compatible_sdks.sort(key=lambda v: float(v.replace("v", "")), reverse=True) + self.compatible_sdks = compatible_sdks + + def ShortName(self): + return self.short_name + + def Description(self): + """Get the full description of the version.""" + return self.description + + def SolutionVersion(self): + """Get the version number of the sln files.""" + return self.solution_version + + def ProjectVersion(self): + """Get the version number of the vcproj or vcxproj files.""" + return self.project_version + + def FlatSolution(self): + return self.flat_sln + + def UsesVcxproj(self): + """Returns true if this version uses a vcxproj file.""" + return self.uses_vcxproj + + def ProjectExtension(self): + """Returns the file extension for the project.""" + return self.uses_vcxproj and ".vcxproj" or ".vcproj" + + def Path(self): + """Returns the path to Visual Studio installation.""" + return self.path + + def ToolPath(self, tool): + """Returns the path to a given compiler tool. """ + return os.path.normpath(os.path.join(self.path, "VC/bin", tool)) + + def DefaultToolset(self): + """Returns the msbuild toolset version that will be used in the absence of a user override.""" - return self.default_toolset + return self.default_toolset - - def _SetupScriptInternal(self, target_arch): - """Returns a command (with arguments) to be used to set up the + def _SetupScriptInternal(self, target_arch): + """Returns a command (with arguments) to be used to set up the environment.""" - assert target_arch in ('x86', 'x64'), "target_arch not supported" - # If WindowsSDKDir is set and SetEnv.Cmd exists then we are using the - # depot_tools build tools and should run SetEnv.Cmd to set up the - # environment. The check for WindowsSDKDir alone is not sufficient because - # this is set by running vcvarsall.bat. - sdk_dir = os.environ.get('WindowsSDKDir', '') - setup_path = JoinPath(sdk_dir, 'Bin', 'SetEnv.Cmd') - if self.sdk_based and sdk_dir and os.path.exists(setup_path): - return [setup_path, '/' + target_arch] - - is_host_arch_x64 = ( - os.environ.get('PROCESSOR_ARCHITECTURE') == 'AMD64' or - os.environ.get('PROCESSOR_ARCHITEW6432') == 'AMD64' - ) - - # For VS2017 (and newer) it's fairly easy - if self.short_name >= '2017': - script_path = JoinPath(self.path, - 'VC', 'Auxiliary', 'Build', 'vcvarsall.bat') - - # Always use a native executable, cross-compiling if necessary. - host_arch = 'amd64' if is_host_arch_x64 else 'x86' - msvc_target_arch = 'amd64' if target_arch == 'x64' else 'x86' - arg = host_arch - if host_arch != msvc_target_arch: - arg += '_' + msvc_target_arch - - return [script_path, arg] - - # We try to find the best version of the env setup batch. - vcvarsall = JoinPath(self.path, 'VC', 'vcvarsall.bat') - if target_arch == 'x86': - if self.short_name >= '2013' and self.short_name[-1] != 'e' and \ - is_host_arch_x64: - # VS2013 and later, non-Express have a x64-x86 cross that we want - # to prefer. - return [vcvarsall, 'amd64_x86'] - else: - # Otherwise, the standard x86 compiler. We don't use VC/vcvarsall.bat - # for x86 because vcvarsall calls vcvars32, which it can only find if - # VS??COMNTOOLS is set, which isn't guaranteed. - return [JoinPath(self.path, 'Common7', 'Tools', 'vsvars32.bat')] - elif target_arch == 'x64': - arg = 'x86_amd64' - # Use the 64-on-64 compiler if we're not using an express edition and - # we're running on a 64bit OS. - if self.short_name[-1] != 'e' and is_host_arch_x64: - arg = 'amd64' - return [vcvarsall, arg] - - def SetupScript(self, target_arch): - script_data = self._SetupScriptInternal(target_arch) - script_path = script_data[0] - if not os.path.exists(script_path): - raise Exception('%s is missing - make sure VC++ tools are installed.' % - script_path) - return script_data + assert target_arch in ("x86", "x64"), "target_arch not supported" + # If WindowsSDKDir is set and SetEnv.Cmd exists then we are using the + # depot_tools build tools and should run SetEnv.Cmd to set up the + # environment. The check for WindowsSDKDir alone is not sufficient because + # this is set by running vcvarsall.bat. + sdk_dir = os.environ.get("WindowsSDKDir", "") + setup_path = JoinPath(sdk_dir, "Bin", "SetEnv.Cmd") + if self.sdk_based and sdk_dir and os.path.exists(setup_path): + return [setup_path, "/" + target_arch] + + is_host_arch_x64 = ( + os.environ.get("PROCESSOR_ARCHITECTURE") == "AMD64" + or os.environ.get("PROCESSOR_ARCHITEW6432") == "AMD64" + ) + + # For VS2017 (and newer) it's fairly easy + if self.short_name >= "2017": + script_path = JoinPath( + self.path, "VC", "Auxiliary", "Build", "vcvarsall.bat" + ) + + # Always use a native executable, cross-compiling if necessary. + host_arch = "amd64" if is_host_arch_x64 else "x86" + msvc_target_arch = "amd64" if target_arch == "x64" else "x86" + arg = host_arch + if host_arch != msvc_target_arch: + arg += "_" + msvc_target_arch + + return [script_path, arg] + + # We try to find the best version of the env setup batch. + vcvarsall = JoinPath(self.path, "VC", "vcvarsall.bat") + if target_arch == "x86": + if ( + self.short_name >= "2013" + and self.short_name[-1] != "e" + and is_host_arch_x64 + ): + # VS2013 and later, non-Express have a x64-x86 cross that we want + # to prefer. + return [vcvarsall, "amd64_x86"] + else: + # Otherwise, the standard x86 compiler. We don't use VC/vcvarsall.bat + # for x86 because vcvarsall calls vcvars32, which it can only find if + # VS??COMNTOOLS is set, which isn't guaranteed. + return [JoinPath(self.path, "Common7", "Tools", "vsvars32.bat")] + elif target_arch == "x64": + arg = "x86_amd64" + # Use the 64-on-64 compiler if we're not using an express edition and + # we're running on a 64bit OS. + if self.short_name[-1] != "e" and is_host_arch_x64: + arg = "amd64" + return [vcvarsall, arg] + + def SetupScript(self, target_arch): + script_data = self._SetupScriptInternal(target_arch) + script_path = script_data[0] + if not os.path.exists(script_path): + raise Exception( + "%s is missing - make sure VC++ tools are installed." % script_path + ) + return script_data def _RegistryQueryBase(sysdir, key, value): - """Use reg.exe to read a particular key. + """Use reg.exe to read a particular key. While ideally we might use the win32 module, we would like gyp to be python neutral, so for instance cygwin python lacks this module. @@ -153,28 +166,27 @@ def _RegistryQueryBase(sysdir, key, value): Return: stdout from reg.exe, or None for failure. """ - # Skip if not on Windows or Python Win32 setup issue - if sys.platform not in ('win32', 'cygwin'): - return None - # Setup params to pass to and attempt to launch reg.exe - cmd = [os.path.join(os.environ.get('WINDIR', ''), sysdir, 'reg.exe'), - 'query', key] - if value: - cmd.extend(['/v', value]) - p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) - # Obtain the stdout from reg.exe, reading to the end so p.returncode is valid - # Note that the error text may be in [1] in some cases - text = p.communicate()[0] - if PY3: - text = text.decode('utf-8') - # Check return code from reg.exe; officially 0==success and 1==error - if p.returncode: - return None - return text + # Skip if not on Windows or Python Win32 setup issue + if sys.platform not in ("win32", "cygwin"): + return None + # Setup params to pass to and attempt to launch reg.exe + cmd = [os.path.join(os.environ.get("WINDIR", ""), sysdir, "reg.exe"), "query", key] + if value: + cmd.extend(["/v", value]) + p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + # Obtain the stdout from reg.exe, reading to the end so p.returncode is valid + # Note that the error text may be in [1] in some cases + text = p.communicate()[0] + if PY3: + text = text.decode("utf-8") + # Check return code from reg.exe; officially 0==success and 1==error + if p.returncode: + return None + return text def _RegistryQuery(key, value=None): - r"""Use reg.exe to read a particular key through _RegistryQueryBase. + r"""Use reg.exe to read a particular key through _RegistryQueryBase. First tries to launch from %WinDir%\Sysnative to avoid WoW64 redirection. If that fails, it falls back to System32. Sysnative is available on Vista and @@ -190,19 +202,19 @@ def _RegistryQuery(key, value=None): Return: stdout from reg.exe, or None for failure. """ - text = None - try: - text = _RegistryQueryBase('Sysnative', key, value) - except OSError as e: - if e.errno == errno.ENOENT: - text = _RegistryQueryBase('System32', key, value) - else: - raise - return text + text = None + try: + text = _RegistryQueryBase("Sysnative", key, value) + except OSError as e: + if e.errno == errno.ENOENT: + text = _RegistryQueryBase("System32", key, value) + else: + raise + return text def _RegistryGetValueUsingWinReg(key, value): - """Use the _winreg module to obtain the value of a registry key. + """Use the _winreg module to obtain the value of a registry key. Args: key: The registry key. @@ -211,24 +223,24 @@ def _RegistryGetValueUsingWinReg(key, value): contents of the registry key's value, or None on failure. Throws ImportError if _winreg is unavailable. """ - try: - # Python 2 - from _winreg import HKEY_LOCAL_MACHINE, OpenKey, QueryValueEx - except ImportError: - # Python 3 - from winreg import HKEY_LOCAL_MACHINE, OpenKey, QueryValueEx - - try: - root, subkey = key.split('\\', 1) - assert root == 'HKLM' # Only need HKLM for now. - with OpenKey(HKEY_LOCAL_MACHINE, subkey) as hkey: - return QueryValueEx(hkey, value)[0] - except WindowsError: - return None + try: + # Python 2 + from _winreg import HKEY_LOCAL_MACHINE, OpenKey, QueryValueEx + except ImportError: + # Python 3 + from winreg import HKEY_LOCAL_MACHINE, OpenKey, QueryValueEx + + try: + root, subkey = key.split("\\", 1) + assert root == "HKLM" # Only need HKLM for now. + with OpenKey(HKEY_LOCAL_MACHINE, subkey) as hkey: + return QueryValueEx(hkey, value)[0] + except WindowsError: + return None def _RegistryGetValue(key, value): - """Use _winreg or reg.exe to obtain the value of a registry key. + """Use _winreg or reg.exe to obtain the value of a registry key. Using _winreg is preferable because it solves an issue on some corporate environments where access to reg.exe is locked down. However, we still need @@ -241,161 +253,187 @@ def _RegistryGetValue(key, value): Return: contents of the registry key's value, or None on failure. """ - try: - return _RegistryGetValueUsingWinReg(key, value) - except ImportError: - pass - - # Fallback to reg.exe if we fail to import _winreg. - text = _RegistryQuery(key, value) - if not text: - return None - # Extract value. - match = re.search(r'REG_\w+\s+([^\r]+)\r\n', text) - if not match: - return None - return match.group(1) + try: + return _RegistryGetValueUsingWinReg(key, value) + except ImportError: + pass + + # Fallback to reg.exe if we fail to import _winreg. + text = _RegistryQuery(key, value) + if not text: + return None + # Extract value. + match = re.search(r"REG_\w+\s+([^\r]+)\r\n", text) + if not match: + return None + return match.group(1) def _CreateVersion(name, path, sdk_based=False): - """Sets up MSVS project generation. + """Sets up MSVS project generation. Setup is based off the GYP_MSVS_VERSION environment variable or whatever is autodetected if GYP_MSVS_VERSION is not explicitly specified. If a version is passed in that doesn't match a value in versions python will throw a error. """ - if path: - path = os.path.normpath(path) - versions = { - '2019': VisualStudioVersion('2019', - 'Visual Studio 2019', - solution_version='12.00', - project_version='16.0', - flat_sln=False, - uses_vcxproj=True, - path=path, - sdk_based=sdk_based, - default_toolset='v142', - compatible_sdks=['v8.1', 'v10.0']), - '2017': VisualStudioVersion('2017', - 'Visual Studio 2017', - solution_version='12.00', - project_version='15.0', - flat_sln=False, - uses_vcxproj=True, - path=path, - sdk_based=sdk_based, - default_toolset='v141', - compatible_sdks=['v8.1', 'v10.0']), - '2015': VisualStudioVersion('2015', - 'Visual Studio 2015', - solution_version='12.00', - project_version='14.0', - flat_sln=False, - uses_vcxproj=True, - path=path, - sdk_based=sdk_based, - default_toolset='v140'), - '2013': VisualStudioVersion('2013', - 'Visual Studio 2013', - solution_version='13.00', - project_version='12.0', - flat_sln=False, - uses_vcxproj=True, - path=path, - sdk_based=sdk_based, - default_toolset='v120'), - '2013e': VisualStudioVersion('2013e', - 'Visual Studio 2013', - solution_version='13.00', - project_version='12.0', - flat_sln=True, - uses_vcxproj=True, - path=path, - sdk_based=sdk_based, - default_toolset='v120'), - '2012': VisualStudioVersion('2012', - 'Visual Studio 2012', - solution_version='12.00', - project_version='4.0', - flat_sln=False, - uses_vcxproj=True, - path=path, - sdk_based=sdk_based, - default_toolset='v110'), - '2012e': VisualStudioVersion('2012e', - 'Visual Studio 2012', - solution_version='12.00', - project_version='4.0', - flat_sln=True, - uses_vcxproj=True, - path=path, - sdk_based=sdk_based, - default_toolset='v110'), - '2010': VisualStudioVersion('2010', - 'Visual Studio 2010', - solution_version='11.00', - project_version='4.0', - flat_sln=False, - uses_vcxproj=True, - path=path, - sdk_based=sdk_based), - '2010e': VisualStudioVersion('2010e', - 'Visual C++ Express 2010', - solution_version='11.00', - project_version='4.0', - flat_sln=True, - uses_vcxproj=True, - path=path, - sdk_based=sdk_based), - '2008': VisualStudioVersion('2008', - 'Visual Studio 2008', - solution_version='10.00', - project_version='9.00', - flat_sln=False, - uses_vcxproj=False, - path=path, - sdk_based=sdk_based), - '2008e': VisualStudioVersion('2008e', - 'Visual Studio 2008', - solution_version='10.00', - project_version='9.00', - flat_sln=True, - uses_vcxproj=False, - path=path, - sdk_based=sdk_based), - '2005': VisualStudioVersion('2005', - 'Visual Studio 2005', - solution_version='9.00', - project_version='8.00', - flat_sln=False, - uses_vcxproj=False, - path=path, - sdk_based=sdk_based), - '2005e': VisualStudioVersion('2005e', - 'Visual Studio 2005', - solution_version='9.00', - project_version='8.00', - flat_sln=True, - uses_vcxproj=False, - path=path, - sdk_based=sdk_based), - } - return versions[str(name)] + if path: + path = os.path.normpath(path) + versions = { + "2019": VisualStudioVersion( + "2019", + "Visual Studio 2019", + solution_version="12.00", + project_version="16.0", + flat_sln=False, + uses_vcxproj=True, + path=path, + sdk_based=sdk_based, + default_toolset="v142", + compatible_sdks=["v8.1", "v10.0"], + ), + "2017": VisualStudioVersion( + "2017", + "Visual Studio 2017", + solution_version="12.00", + project_version="15.0", + flat_sln=False, + uses_vcxproj=True, + path=path, + sdk_based=sdk_based, + default_toolset="v141", + compatible_sdks=["v8.1", "v10.0"], + ), + "2015": VisualStudioVersion( + "2015", + "Visual Studio 2015", + solution_version="12.00", + project_version="14.0", + flat_sln=False, + uses_vcxproj=True, + path=path, + sdk_based=sdk_based, + default_toolset="v140", + ), + "2013": VisualStudioVersion( + "2013", + "Visual Studio 2013", + solution_version="13.00", + project_version="12.0", + flat_sln=False, + uses_vcxproj=True, + path=path, + sdk_based=sdk_based, + default_toolset="v120", + ), + "2013e": VisualStudioVersion( + "2013e", + "Visual Studio 2013", + solution_version="13.00", + project_version="12.0", + flat_sln=True, + uses_vcxproj=True, + path=path, + sdk_based=sdk_based, + default_toolset="v120", + ), + "2012": VisualStudioVersion( + "2012", + "Visual Studio 2012", + solution_version="12.00", + project_version="4.0", + flat_sln=False, + uses_vcxproj=True, + path=path, + sdk_based=sdk_based, + default_toolset="v110", + ), + "2012e": VisualStudioVersion( + "2012e", + "Visual Studio 2012", + solution_version="12.00", + project_version="4.0", + flat_sln=True, + uses_vcxproj=True, + path=path, + sdk_based=sdk_based, + default_toolset="v110", + ), + "2010": VisualStudioVersion( + "2010", + "Visual Studio 2010", + solution_version="11.00", + project_version="4.0", + flat_sln=False, + uses_vcxproj=True, + path=path, + sdk_based=sdk_based, + ), + "2010e": VisualStudioVersion( + "2010e", + "Visual C++ Express 2010", + solution_version="11.00", + project_version="4.0", + flat_sln=True, + uses_vcxproj=True, + path=path, + sdk_based=sdk_based, + ), + "2008": VisualStudioVersion( + "2008", + "Visual Studio 2008", + solution_version="10.00", + project_version="9.00", + flat_sln=False, + uses_vcxproj=False, + path=path, + sdk_based=sdk_based, + ), + "2008e": VisualStudioVersion( + "2008e", + "Visual Studio 2008", + solution_version="10.00", + project_version="9.00", + flat_sln=True, + uses_vcxproj=False, + path=path, + sdk_based=sdk_based, + ), + "2005": VisualStudioVersion( + "2005", + "Visual Studio 2005", + solution_version="9.00", + project_version="8.00", + flat_sln=False, + uses_vcxproj=False, + path=path, + sdk_based=sdk_based, + ), + "2005e": VisualStudioVersion( + "2005e", + "Visual Studio 2005", + solution_version="9.00", + project_version="8.00", + flat_sln=True, + uses_vcxproj=False, + path=path, + sdk_based=sdk_based, + ), + } + return versions[str(name)] def _ConvertToCygpath(path): - """Convert to cygwin path if we are using cygwin.""" - if sys.platform == 'cygwin': - p = subprocess.Popen(['cygpath', path], stdout=subprocess.PIPE) - path = p.communicate()[0].strip() - if PY3: - path = path.decode('utf-8') - return path + """Convert to cygwin path if we are using cygwin.""" + if sys.platform == "cygwin": + p = subprocess.Popen(["cygpath", path], stdout=subprocess.PIPE) + path = p.communicate()[0].strip() + if PY3: + path = path.decode("utf-8") + return path def _DetectVisualStudioVersions(versions_to_check, force_express): - """Collect the list of installed visual studio versions. + """Collect the list of installed visual studio versions. Returns: A list of visual studio versions installed in descending order of @@ -412,105 +450,122 @@ def _DetectVisualStudioVersions(versions_to_check, force_express): 2019 - Visual Studio 2019 (16) Where (e) is e for express editions of MSVS and blank otherwise. """ - version_to_year = { - '8.0': '2005', - '9.0': '2008', - '10.0': '2010', - '11.0': '2012', - '12.0': '2013', - '14.0': '2015', - '15.0': '2017', - '16.0': '2019', - } - versions = [] - for version in versions_to_check: - # Old method of searching for which VS version is installed - # We don't use the 2010-encouraged-way because we also want to get the - # path to the binaries, which it doesn't offer. - keys = [r'HKLM\Software\Microsoft\VisualStudio\%s' % version, - r'HKLM\Software\Wow6432Node\Microsoft\VisualStudio\%s' % version, - r'HKLM\Software\Microsoft\VCExpress\%s' % version, - r'HKLM\Software\Wow6432Node\Microsoft\VCExpress\%s' % version] - for index in range(len(keys)): - path = _RegistryGetValue(keys[index], 'InstallDir') - if not path: - continue - path = _ConvertToCygpath(path) - # Check for full. - full_path = os.path.join(path, 'devenv.exe') - express_path = os.path.join(path, '*express.exe') - if not force_express and os.path.exists(full_path): - # Add this one. - versions.append(_CreateVersion(version_to_year[version], - os.path.join(path, '..', '..'))) - # Check for express. - elif glob.glob(express_path): - # Add this one. - versions.append(_CreateVersion(version_to_year[version] + 'e', - os.path.join(path, '..', '..'))) - - # The old method above does not work when only SDK is installed. - keys = [r'HKLM\Software\Microsoft\VisualStudio\SxS\VC7', - r'HKLM\Software\Wow6432Node\Microsoft\VisualStudio\SxS\VC7', - r'HKLM\Software\Microsoft\VisualStudio\SxS\VS7', - r'HKLM\Software\Wow6432Node\Microsoft\VisualStudio\SxS\VS7'] - for index in range(len(keys)): - path = _RegistryGetValue(keys[index], version) - if not path: - continue - path = _ConvertToCygpath(path) - if version == '15.0': - if os.path.exists(path): - versions.append(_CreateVersion('2017', path)) - elif version != '14.0': # There is no Express edition for 2015. - versions.append(_CreateVersion(version_to_year[version] + 'e', - os.path.join(path, '..'), sdk_based=True)) - - return versions - - -def SelectVisualStudioVersion(version='auto', allow_fallback=True): - """Select which version of Visual Studio projects to generate. + version_to_year = { + "8.0": "2005", + "9.0": "2008", + "10.0": "2010", + "11.0": "2012", + "12.0": "2013", + "14.0": "2015", + "15.0": "2017", + "16.0": "2019", + } + versions = [] + for version in versions_to_check: + # Old method of searching for which VS version is installed + # We don't use the 2010-encouraged-way because we also want to get the + # path to the binaries, which it doesn't offer. + keys = [ + r"HKLM\Software\Microsoft\VisualStudio\%s" % version, + r"HKLM\Software\Wow6432Node\Microsoft\VisualStudio\%s" % version, + r"HKLM\Software\Microsoft\VCExpress\%s" % version, + r"HKLM\Software\Wow6432Node\Microsoft\VCExpress\%s" % version, + ] + for index in range(len(keys)): + path = _RegistryGetValue(keys[index], "InstallDir") + if not path: + continue + path = _ConvertToCygpath(path) + # Check for full. + full_path = os.path.join(path, "devenv.exe") + express_path = os.path.join(path, "*express.exe") + if not force_express and os.path.exists(full_path): + # Add this one. + versions.append( + _CreateVersion( + version_to_year[version], os.path.join(path, "..", "..") + ) + ) + # Check for express. + elif glob.glob(express_path): + # Add this one. + versions.append( + _CreateVersion( + version_to_year[version] + "e", os.path.join(path, "..", "..") + ) + ) + + # The old method above does not work when only SDK is installed. + keys = [ + r"HKLM\Software\Microsoft\VisualStudio\SxS\VC7", + r"HKLM\Software\Wow6432Node\Microsoft\VisualStudio\SxS\VC7", + r"HKLM\Software\Microsoft\VisualStudio\SxS\VS7", + r"HKLM\Software\Wow6432Node\Microsoft\VisualStudio\SxS\VS7", + ] + for index in range(len(keys)): + path = _RegistryGetValue(keys[index], version) + if not path: + continue + path = _ConvertToCygpath(path) + if version == "15.0": + if os.path.exists(path): + versions.append(_CreateVersion("2017", path)) + elif version != "14.0": # There is no Express edition for 2015. + versions.append( + _CreateVersion( + version_to_year[version] + "e", + os.path.join(path, ".."), + sdk_based=True, + ) + ) + + return versions + + +def SelectVisualStudioVersion(version="auto", allow_fallback=True): + """Select which version of Visual Studio projects to generate. Arguments: version: Hook to allow caller to force a particular version (vs auto). Returns: An object representing a visual studio project format version. """ - # In auto mode, check environment variable for override. - if version == 'auto': - version = os.environ.get('GYP_MSVS_VERSION', 'auto') - version_map = { - 'auto': ('16.0', '15.0', '14.0', '12.0', '10.0', '9.0', '8.0', '11.0'), - '2005': ('8.0',), - '2005e': ('8.0',), - '2008': ('9.0',), - '2008e': ('9.0',), - '2010': ('10.0',), - '2010e': ('10.0',), - '2012': ('11.0',), - '2012e': ('11.0',), - '2013': ('12.0',), - '2013e': ('12.0',), - '2015': ('14.0',), - '2017': ('15.0',), - '2019': ('16.0',), - } - override_path = os.environ.get('GYP_MSVS_OVERRIDE_PATH') - if override_path: - msvs_version = os.environ.get('GYP_MSVS_VERSION') - if not msvs_version: - raise ValueError('GYP_MSVS_OVERRIDE_PATH requires GYP_MSVS_VERSION to be ' - 'set to a particular version (e.g. 2010e).') - return _CreateVersion(msvs_version, override_path, sdk_based=True) - version = str(version) - versions = _DetectVisualStudioVersions(version_map[version], 'e' in version) - if not versions: - if not allow_fallback: - raise ValueError('Could not locate Visual Studio installation.') - if version == 'auto': - # Default to 2005 if we couldn't find anything - return _CreateVersion('2005', None) - else: - return _CreateVersion(version, None) - return versions[0] + # In auto mode, check environment variable for override. + if version == "auto": + version = os.environ.get("GYP_MSVS_VERSION", "auto") + version_map = { + "auto": ("16.0", "15.0", "14.0", "12.0", "10.0", "9.0", "8.0", "11.0"), + "2005": ("8.0",), + "2005e": ("8.0",), + "2008": ("9.0",), + "2008e": ("9.0",), + "2010": ("10.0",), + "2010e": ("10.0",), + "2012": ("11.0",), + "2012e": ("11.0",), + "2013": ("12.0",), + "2013e": ("12.0",), + "2015": ("14.0",), + "2017": ("15.0",), + "2019": ("16.0",), + } + override_path = os.environ.get("GYP_MSVS_OVERRIDE_PATH") + if override_path: + msvs_version = os.environ.get("GYP_MSVS_VERSION") + if not msvs_version: + raise ValueError( + "GYP_MSVS_OVERRIDE_PATH requires GYP_MSVS_VERSION to be " + "set to a particular version (e.g. 2010e)." + ) + return _CreateVersion(msvs_version, override_path, sdk_based=True) + version = str(version) + versions = _DetectVisualStudioVersions(version_map[version], "e" in version) + if not versions: + if not allow_fallback: + raise ValueError("Could not locate Visual Studio installation.") + if version == "auto": + # Default to 2005 if we couldn't find anything + return _CreateVersion("2005", None) + else: + return _CreateVersion(version, None) + return versions[0] diff --git a/tools/gyp/pylib/gyp/__init__.py b/tools/gyp/pylib/gyp/__init__.py index dee834013f4144..f6ea625d40a3c7 100755 --- a/tools/gyp/pylib/gyp/__init__.py +++ b/tools/gyp/pylib/gyp/__init__.py @@ -27,153 +27,178 @@ debug = {} # List of "official" debug modes, but you can use anything you like. -DEBUG_GENERAL = 'general' -DEBUG_VARIABLES = 'variables' -DEBUG_INCLUDES = 'includes' +DEBUG_GENERAL = "general" +DEBUG_VARIABLES = "variables" +DEBUG_INCLUDES = "includes" def DebugOutput(mode, message, *args): - if 'all' in gyp.debug or mode in gyp.debug: - ctx = ('unknown', 0, 'unknown') - try: - f = traceback.extract_stack(limit=2) - if f: - ctx = f[0][:3] - except: - pass - if args: - message %= args - print('%s:%s:%d:%s %s' % (mode.upper(), os.path.basename(ctx[0]), - ctx[1], ctx[2], message)) + if "all" in gyp.debug or mode in gyp.debug: + ctx = ("unknown", 0, "unknown") + try: + f = traceback.extract_stack(limit=2) + if f: + ctx = f[0][:3] + except Exception: + pass + if args: + message %= args + print( + "%s:%s:%d:%s %s" + % (mode.upper(), os.path.basename(ctx[0]), ctx[1], ctx[2], message) + ) + def FindBuildFiles(): - extension = '.gyp' - files = os.listdir(os.getcwd()) - build_files = [] - for file in files: - if file.endswith(extension): - build_files.append(file) - return build_files - - -def Load(build_files, format, default_variables={}, - includes=[], depth='.', params=None, check=False, - circular_check=True, duplicate_basename_check=True): - """ + extension = ".gyp" + files = os.listdir(os.getcwd()) + build_files = [] + for file in files: + if file.endswith(extension): + build_files.append(file) + return build_files + + +def Load( + build_files, + format, + default_variables={}, + includes=[], + depth=".", + params=None, + check=False, + circular_check=True, +): + """ Loads one or more specified build files. default_variables and includes will be copied before use. Returns the generator for the specified format and the data returned by loading the specified build files. """ - if params is None: - params = {} - - if '-' in format: - format, params['flavor'] = format.split('-', 1) - - default_variables = copy.copy(default_variables) - - # Default variables provided by this program and its modules should be - # named WITH_CAPITAL_LETTERS to provide a distinct "best practice" namespace, - # avoiding collisions with user and automatic variables. - default_variables['GENERATOR'] = format - default_variables['GENERATOR_FLAVOR'] = params.get('flavor', '') - - # Format can be a custom python file, or by default the name of a module - # within gyp.generator. - if format.endswith('.py'): - generator_name = os.path.splitext(format)[0] - path, generator_name = os.path.split(generator_name) - - # Make sure the path to the custom generator is in sys.path - # Don't worry about removing it once we are done. Keeping the path - # to each generator that is used in sys.path is likely harmless and - # arguably a good idea. - path = os.path.abspath(path) - if path not in sys.path: - sys.path.insert(0, path) - else: - generator_name = 'gyp.generator.' + format - - # These parameters are passed in order (as opposed to by key) - # because ActivePython cannot handle key parameters to __import__. - generator = __import__(generator_name, globals(), locals(), generator_name) - for (key, val) in generator.generator_default_variables.items(): - default_variables.setdefault(key, val) - - # Give the generator the opportunity to set additional variables based on - # the params it will receive in the output phase. - if getattr(generator, 'CalculateVariables', None): - generator.CalculateVariables(default_variables, params) - - # Give the generator the opportunity to set generator_input_info based on - # the params it will receive in the output phase. - if getattr(generator, 'CalculateGeneratorInputInfo', None): - generator.CalculateGeneratorInputInfo(params) - - # Fetch the generator specific info that gets fed to input, we use getattr - # so we can default things and the generators only have to provide what - # they need. - generator_input_info = { - 'non_configuration_keys': - getattr(generator, 'generator_additional_non_configuration_keys', []), - 'path_sections': - getattr(generator, 'generator_additional_path_sections', []), - 'extra_sources_for_rules': - getattr(generator, 'generator_extra_sources_for_rules', []), - 'generator_supports_multiple_toolsets': - getattr(generator, 'generator_supports_multiple_toolsets', False), - 'generator_wants_static_library_dependencies_adjusted': - getattr(generator, - 'generator_wants_static_library_dependencies_adjusted', True), - 'generator_wants_sorted_dependencies': - getattr(generator, 'generator_wants_sorted_dependencies', False), - 'generator_filelist_paths': - getattr(generator, 'generator_filelist_paths', None), - } - - # Process the input specific to this generator. - result = gyp.input.Load(build_files, default_variables, includes[:], - depth, generator_input_info, check, circular_check, - duplicate_basename_check, - params['parallel'], params['root_targets']) - return [generator] + result + if params is None: + params = {} + + if "-" in format: + format, params["flavor"] = format.split("-", 1) + + default_variables = copy.copy(default_variables) + + # Default variables provided by this program and its modules should be + # named WITH_CAPITAL_LETTERS to provide a distinct "best practice" namespace, + # avoiding collisions with user and automatic variables. + default_variables["GENERATOR"] = format + default_variables["GENERATOR_FLAVOR"] = params.get("flavor", "") + + # Format can be a custom python file, or by default the name of a module + # within gyp.generator. + if format.endswith(".py"): + generator_name = os.path.splitext(format)[0] + path, generator_name = os.path.split(generator_name) + + # Make sure the path to the custom generator is in sys.path + # Don't worry about removing it once we are done. Keeping the path + # to each generator that is used in sys.path is likely harmless and + # arguably a good idea. + path = os.path.abspath(path) + if path not in sys.path: + sys.path.insert(0, path) + else: + generator_name = "gyp.generator." + format + + # These parameters are passed in order (as opposed to by key) + # because ActivePython cannot handle key parameters to __import__. + generator = __import__(generator_name, globals(), locals(), generator_name) + for (key, val) in generator.generator_default_variables.items(): + default_variables.setdefault(key, val) + + # Give the generator the opportunity to set additional variables based on + # the params it will receive in the output phase. + if getattr(generator, "CalculateVariables", None): + generator.CalculateVariables(default_variables, params) + + # Give the generator the opportunity to set generator_input_info based on + # the params it will receive in the output phase. + if getattr(generator, "CalculateGeneratorInputInfo", None): + generator.CalculateGeneratorInputInfo(params) + + # Fetch the generator specific info that gets fed to input, we use getattr + # so we can default things and the generators only have to provide what + # they need. + generator_input_info = { + "non_configuration_keys": getattr( + generator, "generator_additional_non_configuration_keys", [] + ), + "path_sections": getattr(generator, "generator_additional_path_sections", []), + "extra_sources_for_rules": getattr( + generator, "generator_extra_sources_for_rules", [] + ), + "generator_supports_multiple_toolsets": getattr( + generator, "generator_supports_multiple_toolsets", False + ), + "generator_wants_static_library_dependencies_adjusted": getattr( + generator, "generator_wants_static_library_dependencies_adjusted", True + ), + "generator_wants_sorted_dependencies": getattr( + generator, "generator_wants_sorted_dependencies", False + ), + "generator_filelist_paths": getattr( + generator, "generator_filelist_paths", None + ), + } + + # Process the input specific to this generator. + result = gyp.input.Load( + build_files, + default_variables, + includes[:], + depth, + generator_input_info, + check, + circular_check, + params["parallel"], + params["root_targets"], + ) + return [generator] + result + def NameValueListToDict(name_value_list): - """ + """ Takes an array of strings of the form 'NAME=VALUE' and creates a dictionary of the pairs. If a string is simply NAME, then the value in the dictionary is set to True. If VALUE can be converted to an integer, it is. """ - result = { } - for item in name_value_list: - tokens = item.split('=', 1) - if len(tokens) == 2: - # If we can make it an int, use that, otherwise, use the string. - try: - token_value = int(tokens[1]) - except ValueError: - token_value = tokens[1] - # Set the variable to the supplied value. - result[tokens[0]] = token_value - else: - # No value supplied, treat it as a boolean and set it. - result[tokens[0]] = True - return result + result = {} + for item in name_value_list: + tokens = item.split("=", 1) + if len(tokens) == 2: + # If we can make it an int, use that, otherwise, use the string. + try: + token_value = int(tokens[1]) + except ValueError: + token_value = tokens[1] + # Set the variable to the supplied value. + result[tokens[0]] = token_value + else: + # No value supplied, treat it as a boolean and set it. + result[tokens[0]] = True + return result + def ShlexEnv(env_name): - flags = os.environ.get(env_name, []) - if flags: - flags = shlex.split(flags) - return flags + flags = os.environ.get(env_name, []) + if flags: + flags = shlex.split(flags) + return flags + def FormatOpt(opt, value): - if opt.startswith('--'): - return '%s=%s' % (opt, value) - return opt + value + if opt.startswith("--"): + return "%s=%s" % (opt, value) + return opt + value + def RegenerateAppendFlag(flag, values, predicate, env_name, options): - """Regenerate a list of command line flags, for an option of action='append'. + """Regenerate a list of command line flags, for an option of action='append'. The |env_name|, if given, is checked in the environment and used to generate an initial list of options, then the options that were specified on the @@ -182,20 +207,21 @@ def RegenerateAppendFlag(flag, values, predicate, env_name, options): the environment, while not requiring the environment to be set when the flags are used again. """ - flags = [] - if options.use_environment and env_name: - for flag_value in ShlexEnv(env_name): - value = FormatOpt(flag, predicate(flag_value)) - if value in flags: - flags.remove(value) - flags.append(value) - if values: - for flag_value in values: - flags.append(FormatOpt(flag, predicate(flag_value))) - return flags + flags = [] + if options.use_environment and env_name: + for flag_value in ShlexEnv(env_name): + value = FormatOpt(flag, predicate(flag_value)) + if value in flags: + flags.remove(value) + flags.append(value) + if values: + for flag_value in values: + flags.append(FormatOpt(flag, predicate(flag_value))) + return flags + def RegenerateFlags(options): - """Given a parsed options object, and taking the environment variables into + """Given a parsed options object, and taking the environment variables into account, returns a list of flags that should regenerate an equivalent options object (even in the absence of the environment variables.) @@ -204,53 +230,62 @@ def RegenerateFlags(options): The format flag is not included, as it is assumed the calling generator will set that as appropriate. """ - def FixPath(path): - path = gyp.common.FixIfRelativePath(path, options.depth) - if not path: - return os.path.curdir - return path - - def Noop(value): - return value - - # We always want to ignore the environment when regenerating, to avoid - # duplicate or changed flags in the environment at the time of regeneration. - flags = ['--ignore-environment'] - for name, metadata in options._regeneration_metadata.items(): - opt = metadata['opt'] - value = getattr(options, name) - value_predicate = metadata['type'] == 'path' and FixPath or Noop - action = metadata['action'] - env_name = metadata['env_name'] - if action == 'append': - flags.extend(RegenerateAppendFlag(opt, value, value_predicate, - env_name, options)) - elif action in ('store', None): # None is a synonym for 'store'. - if value: - flags.append(FormatOpt(opt, value_predicate(value))) - elif options.use_environment and env_name and os.environ.get(env_name): - flags.append(FormatOpt(opt, value_predicate(os.environ.get(env_name)))) - elif action in ('store_true', 'store_false'): - if ((action == 'store_true' and value) or - (action == 'store_false' and not value)): - flags.append(opt) - elif options.use_environment and env_name: - print('Warning: environment regeneration unimplemented ' - 'for %s flag %r env_name %r' % (action, opt, - env_name), file=sys.stderr) - else: - print('Warning: regeneration unimplemented for action %r ' - 'flag %r' % (action, opt), file=sys.stderr) - return flags + def FixPath(path): + path = gyp.common.FixIfRelativePath(path, options.depth) + if not path: + return os.path.curdir + return path + + def Noop(value): + return value + + # We always want to ignore the environment when regenerating, to avoid + # duplicate or changed flags in the environment at the time of regeneration. + flags = ["--ignore-environment"] + for name, metadata in options._regeneration_metadata.items(): + opt = metadata["opt"] + value = getattr(options, name) + value_predicate = metadata["type"] == "path" and FixPath or Noop + action = metadata["action"] + env_name = metadata["env_name"] + if action == "append": + flags.extend( + RegenerateAppendFlag(opt, value, value_predicate, env_name, options) + ) + elif action in ("store", None): # None is a synonym for 'store'. + if value: + flags.append(FormatOpt(opt, value_predicate(value))) + elif options.use_environment and env_name and os.environ.get(env_name): + flags.append(FormatOpt(opt, value_predicate(os.environ.get(env_name)))) + elif action in ("store_true", "store_false"): + if (action == "store_true" and value) or ( + action == "store_false" and not value + ): + flags.append(opt) + elif options.use_environment and env_name: + print( + "Warning: environment regeneration unimplemented " + "for %s flag %r env_name %r" % (action, opt, env_name), + file=sys.stderr, + ) + else: + print( + "Warning: regeneration unimplemented for action %r " + "flag %r" % (action, opt), + file=sys.stderr, + ) + + return flags + class RegeneratableOptionParser(argparse.ArgumentParser): - def __init__(self, usage): - self.__regeneratable_options = {} - argparse.ArgumentParser.__init__(self, usage=usage) + def __init__(self, usage): + self.__regeneratable_options = {} + argparse.ArgumentParser.__init__(self, usage=usage) - def add_argument(self, *args, **kw): - """Add an option to the parser. + def add_argument(self, *args, **kw): + """Add an option to the parser. This accepts the same arguments as ArgumentParser.add_argument, plus the following: @@ -261,297 +296,379 @@ def add_argument(self, *args, **kw): type: adds type='path', to tell the regenerator that the values of this option need to be made relative to options.depth """ - env_name = kw.pop('env_name', None) - if 'dest' in kw and kw.pop('regenerate', True): - dest = kw['dest'] - - # The path type is needed for regenerating, for optparse we can just treat - # it as a string. - type = kw.get('type') - if type == 'path': - kw['type'] = str - - self.__regeneratable_options[dest] = { - 'action': kw.get('action'), - 'type': type, - 'env_name': env_name, - 'opt': args[0], - } + env_name = kw.pop("env_name", None) + if "dest" in kw and kw.pop("regenerate", True): + dest = kw["dest"] + + # The path type is needed for regenerating, for optparse we can just treat + # it as a string. + type = kw.get("type") + if type == "path": + kw["type"] = str + + self.__regeneratable_options[dest] = { + "action": kw.get("action"), + "type": type, + "env_name": env_name, + "opt": args[0], + } - argparse.ArgumentParser.add_argument(self, *args, **kw) + argparse.ArgumentParser.add_argument(self, *args, **kw) + + def parse_args(self, *args): + values, args = argparse.ArgumentParser.parse_known_args(self, *args) + values._regeneration_metadata = self.__regeneratable_options + return values, args - def parse_args(self, *args): - values, args = argparse.ArgumentParser.parse_known_args(self, *args) - values._regeneration_metadata = self.__regeneratable_options - return values, args def gyp_main(args): - my_name = os.path.basename(sys.argv[0]) - usage = 'usage: %(prog)s [options ...] [build_file ...]' - - - parser = RegeneratableOptionParser(usage=usage.replace('%s', '%(prog)s')) - parser.add_argument('--build', dest='configs', action='append', - help='configuration for build after project generation') - parser.add_argument('--check', dest='check', action='store_true', - help='check format of gyp files') - parser.add_argument('--config-dir', dest='config_dir', action='store', - env_name='GYP_CONFIG_DIR', default=None, - help='The location for configuration files like ' - 'include.gypi.') - parser.add_argument('-d', '--debug', dest='debug', metavar='DEBUGMODE', - action='append', default=[], help='turn on a debugging ' - 'mode for debugging GYP. Supported modes are "variables", ' - '"includes" and "general" or "all" for all of them.') - parser.add_argument('-D', dest='defines', action='append', metavar='VAR=VAL', - env_name='GYP_DEFINES', - help='sets variable VAR to value VAL') - parser.add_argument('--depth', dest='depth', metavar='PATH', type='path', - help='set DEPTH gyp variable to a relative path to PATH') - parser.add_argument('-f', '--format', dest='formats', action='append', - env_name='GYP_GENERATORS', regenerate=False, - help='output formats to generate') - parser.add_argument('-G', dest='generator_flags', action='append', default=[], - metavar='FLAG=VAL', env_name='GYP_GENERATOR_FLAGS', - help='sets generator flag FLAG to VAL') - parser.add_argument('--generator-output', dest='generator_output', - action='store', default=None, metavar='DIR', type='path', - env_name='GYP_GENERATOR_OUTPUT', - help='puts generated build files under DIR') - parser.add_argument('--ignore-environment', dest='use_environment', - action='store_false', default=True, regenerate=False, - help='do not read options from environment variables') - parser.add_argument('-I', '--include', dest='includes', action='append', - metavar='INCLUDE', type='path', - help='files to include in all loaded .gyp files') - # --no-circular-check disables the check for circular relationships between - # .gyp files. These relationships should not exist, but they've only been - # observed to be harmful with the Xcode generator. Chromium's .gyp files - # currently have some circular relationships on non-Mac platforms, so this - # option allows the strict behavior to be used on Macs and the lenient - # behavior to be used elsewhere. - # TODO(mark): Remove this option when http://crbug.com/35878 is fixed. - parser.add_argument('--no-circular-check', dest='circular_check', - action='store_false', default=True, regenerate=False, - help="don't check for circular relationships between files") - # --no-duplicate-basename-check disables the check for duplicate basenames - # in a static_library/shared_library project. Visual C++ 2008 generator - # doesn't support this configuration. Libtool on Mac also generates warnings - # when duplicate basenames are passed into Make generator on Mac. - # TODO(yukawa): Remove this option when these legacy generators are - # deprecated. - parser.add_argument('--no-duplicate-basename-check', - dest='duplicate_basename_check', action='store_false', - default=True, regenerate=False, - help="don't check for duplicate basenames") - parser.add_argument('--no-parallel', action='store_true', default=False, - help='Disable multiprocessing') - parser.add_argument('-S', '--suffix', dest='suffix', default='', - help='suffix to add to generated files') - parser.add_argument('--toplevel-dir', dest='toplevel_dir', action='store', - default=None, metavar='DIR', type='path', - help='directory to use as the root of the source tree') - parser.add_argument('-R', '--root-target', dest='root_targets', - action='append', metavar='TARGET', - help='include only TARGET and its deep dependencies') - - options, build_files_arg = parser.parse_args(args) - build_files = build_files_arg - - # Set up the configuration directory (defaults to ~/.gyp) - if not options.config_dir: - home = None - home_dot_gyp = None + my_name = os.path.basename(sys.argv[0]) + usage = "usage: %(prog)s [options ...] [build_file ...]" + + parser = RegeneratableOptionParser(usage=usage.replace("%s", "%(prog)s")) + parser.add_argument( + "--build", + dest="configs", + action="append", + help="configuration for build after project generation", + ) + parser.add_argument( + "--check", dest="check", action="store_true", help="check format of gyp files" + ) + parser.add_argument( + "--config-dir", + dest="config_dir", + action="store", + env_name="GYP_CONFIG_DIR", + default=None, + help="The location for configuration files like " "include.gypi.", + ) + parser.add_argument( + "-d", + "--debug", + dest="debug", + metavar="DEBUGMODE", + action="append", + default=[], + help="turn on a debugging " + 'mode for debugging GYP. Supported modes are "variables", ' + '"includes" and "general" or "all" for all of them.', + ) + parser.add_argument( + "-D", + dest="defines", + action="append", + metavar="VAR=VAL", + env_name="GYP_DEFINES", + help="sets variable VAR to value VAL", + ) + parser.add_argument( + "--depth", + dest="depth", + metavar="PATH", + type="path", + help="set DEPTH gyp variable to a relative path to PATH", + ) + parser.add_argument( + "-f", + "--format", + dest="formats", + action="append", + env_name="GYP_GENERATORS", + regenerate=False, + help="output formats to generate", + ) + parser.add_argument( + "-G", + dest="generator_flags", + action="append", + default=[], + metavar="FLAG=VAL", + env_name="GYP_GENERATOR_FLAGS", + help="sets generator flag FLAG to VAL", + ) + parser.add_argument( + "--generator-output", + dest="generator_output", + action="store", + default=None, + metavar="DIR", + type="path", + env_name="GYP_GENERATOR_OUTPUT", + help="puts generated build files under DIR", + ) + parser.add_argument( + "--ignore-environment", + dest="use_environment", + action="store_false", + default=True, + regenerate=False, + help="do not read options from environment variables", + ) + parser.add_argument( + "-I", + "--include", + dest="includes", + action="append", + metavar="INCLUDE", + type="path", + help="files to include in all loaded .gyp files", + ) + # --no-circular-check disables the check for circular relationships between + # .gyp files. These relationships should not exist, but they've only been + # observed to be harmful with the Xcode generator. Chromium's .gyp files + # currently have some circular relationships on non-Mac platforms, so this + # option allows the strict behavior to be used on Macs and the lenient + # behavior to be used elsewhere. + # TODO(mark): Remove this option when http://crbug.com/35878 is fixed. + parser.add_argument( + "--no-circular-check", + dest="circular_check", + action="store_false", + default=True, + regenerate=False, + help="don't check for circular relationships between files", + ) + parser.add_argument( + "--no-parallel", + action="store_true", + default=False, + help="Disable multiprocessing", + ) + parser.add_argument( + "-S", + "--suffix", + dest="suffix", + default="", + help="suffix to add to generated files", + ) + parser.add_argument( + "--toplevel-dir", + dest="toplevel_dir", + action="store", + default=None, + metavar="DIR", + type="path", + help="directory to use as the root of the source tree", + ) + parser.add_argument( + "-R", + "--root-target", + dest="root_targets", + action="append", + metavar="TARGET", + help="include only TARGET and its deep dependencies", + ) + + options, build_files_arg = parser.parse_args(args) + build_files = build_files_arg + + # Set up the configuration directory (defaults to ~/.gyp) + if not options.config_dir: + home = None + home_dot_gyp = None + if options.use_environment: + home_dot_gyp = os.environ.get("GYP_CONFIG_DIR", None) + if home_dot_gyp: + home_dot_gyp = os.path.expanduser(home_dot_gyp) + + if not home_dot_gyp: + home_vars = ["HOME"] + if sys.platform in ("cygwin", "win32"): + home_vars.append("USERPROFILE") + for home_var in home_vars: + home = os.getenv(home_var) + if home: + home_dot_gyp = os.path.join(home, ".gyp") + if not os.path.exists(home_dot_gyp): + home_dot_gyp = None + else: + break + else: + home_dot_gyp = os.path.expanduser(options.config_dir) + + if home_dot_gyp and not os.path.exists(home_dot_gyp): + home_dot_gyp = None + + if not options.formats: + # If no format was given on the command line, then check the env variable. + generate_formats = [] + if options.use_environment: + generate_formats = os.environ.get("GYP_GENERATORS", []) + if generate_formats: + generate_formats = re.split(r"[\s,]", generate_formats) + if generate_formats: + options.formats = generate_formats + else: + # Nothing in the variable, default based on platform. + if sys.platform == "darwin": + options.formats = ["xcode"] + elif sys.platform in ("win32", "cygwin"): + options.formats = ["msvs"] + else: + options.formats = ["make"] + + if not options.generator_output and options.use_environment: + g_o = os.environ.get("GYP_GENERATOR_OUTPUT") + if g_o: + options.generator_output = g_o + + options.parallel = not options.no_parallel + + for mode in options.debug: + gyp.debug[mode] = 1 + + # Do an extra check to avoid work when we're not debugging. + if DEBUG_GENERAL in gyp.debug: + DebugOutput(DEBUG_GENERAL, "running with these options:") + for option, value in sorted(options.__dict__.items()): + if option[0] == "_": + continue + if isinstance(value, string_types): + DebugOutput(DEBUG_GENERAL, " %s: '%s'", option, value) + else: + DebugOutput(DEBUG_GENERAL, " %s: %s", option, value) + + if not build_files: + build_files = FindBuildFiles() + if not build_files: + raise GypError((usage + "\n\n%s: error: no build_file") % (my_name, my_name)) + + # TODO(mark): Chromium-specific hack! + # For Chromium, the gyp "depth" variable should always be a relative path + # to Chromium's top-level "src" directory. If no depth variable was set + # on the command line, try to find a "src" directory by looking at the + # absolute path to each build file's directory. The first "src" component + # found will be treated as though it were the path used for --depth. + if not options.depth: + for build_file in build_files: + build_file_dir = os.path.abspath(os.path.dirname(build_file)) + build_file_dir_components = build_file_dir.split(os.path.sep) + components_len = len(build_file_dir_components) + for index in range(components_len - 1, -1, -1): + if build_file_dir_components[index] == "src": + options.depth = os.path.sep.join(build_file_dir_components) + break + del build_file_dir_components[index] + + # If the inner loop found something, break without advancing to another + # build file. + if options.depth: + break + + if not options.depth: + raise GypError( + "Could not automatically locate src directory. This is" + "a temporary Chromium feature that will be removed. Use" + "--depth as a workaround." + ) + + # If toplevel-dir is not set, we assume that depth is the root of our source + # tree. + if not options.toplevel_dir: + options.toplevel_dir = options.depth + + # -D on the command line sets variable defaults - D isn't just for define, + # it's for default. Perhaps there should be a way to force (-F?) a + # variable's value so that it can't be overridden by anything else. + cmdline_default_variables = {} + defines = [] if options.use_environment: - home_dot_gyp = os.environ.get('GYP_CONFIG_DIR', None) - if home_dot_gyp: - home_dot_gyp = os.path.expanduser(home_dot_gyp) - - if not home_dot_gyp: - home_vars = ['HOME'] - if sys.platform in ('cygwin', 'win32'): - home_vars.append('USERPROFILE') - for home_var in home_vars: - home = os.getenv(home_var) - if home != None: - home_dot_gyp = os.path.join(home, '.gyp') - if not os.path.exists(home_dot_gyp): - home_dot_gyp = None - else: - break - else: - home_dot_gyp = os.path.expanduser(options.config_dir) - - if home_dot_gyp and not os.path.exists(home_dot_gyp): - home_dot_gyp = None - - if not options.formats: - # If no format was given on the command line, then check the env variable. - generate_formats = [] + defines += ShlexEnv("GYP_DEFINES") + if options.defines: + defines += options.defines + cmdline_default_variables = NameValueListToDict(defines) + if DEBUG_GENERAL in gyp.debug: + DebugOutput( + DEBUG_GENERAL, "cmdline_default_variables: %s", cmdline_default_variables + ) + + # Set up includes. + includes = [] + + # If ~/.gyp/include.gypi exists, it'll be forcibly included into every + # .gyp file that's loaded, before anything else is included. + if home_dot_gyp: + default_include = os.path.join(home_dot_gyp, "include.gypi") + if os.path.exists(default_include): + print("Using overrides found in " + default_include) + includes.append(default_include) + + # Command-line --include files come after the default include. + if options.includes: + includes.extend(options.includes) + + # Generator flags should be prefixed with the target generator since they + # are global across all generator runs. + gen_flags = [] if options.use_environment: - generate_formats = os.environ.get('GYP_GENERATORS', []) - if generate_formats: - generate_formats = re.split(r'[\s,]', generate_formats) - if generate_formats: - options.formats = generate_formats - else: - # Nothing in the variable, default based on platform. - if sys.platform == 'darwin': - options.formats = ['xcode'] - elif sys.platform in ('win32', 'cygwin'): - options.formats = ['msvs'] - else: - options.formats = ['make'] - - if not options.generator_output and options.use_environment: - g_o = os.environ.get('GYP_GENERATOR_OUTPUT') - if g_o: - options.generator_output = g_o - - options.parallel = not options.no_parallel - - for mode in options.debug: - gyp.debug[mode] = 1 - - # Do an extra check to avoid work when we're not debugging. - if DEBUG_GENERAL in gyp.debug: - DebugOutput(DEBUG_GENERAL, 'running with these options:') - for option, value in sorted(options.__dict__.items()): - if option[0] == '_': - continue - if isinstance(value, string_types): - DebugOutput(DEBUG_GENERAL, " %s: '%s'", option, value) - else: - DebugOutput(DEBUG_GENERAL, " %s: %s", option, value) - - if not build_files: - build_files = FindBuildFiles() - if not build_files: - raise GypError((usage + '\n\n%s: error: no build_file') % - (my_name, my_name)) - - # TODO(mark): Chromium-specific hack! - # For Chromium, the gyp "depth" variable should always be a relative path - # to Chromium's top-level "src" directory. If no depth variable was set - # on the command line, try to find a "src" directory by looking at the - # absolute path to each build file's directory. The first "src" component - # found will be treated as though it were the path used for --depth. - if not options.depth: - for build_file in build_files: - build_file_dir = os.path.abspath(os.path.dirname(build_file)) - build_file_dir_components = build_file_dir.split(os.path.sep) - components_len = len(build_file_dir_components) - for index in range(components_len - 1, -1, -1): - if build_file_dir_components[index] == 'src': - options.depth = os.path.sep.join(build_file_dir_components) - break - del build_file_dir_components[index] - - # If the inner loop found something, break without advancing to another - # build file. - if options.depth: - break + gen_flags += ShlexEnv("GYP_GENERATOR_FLAGS") + if options.generator_flags: + gen_flags += options.generator_flags + generator_flags = NameValueListToDict(gen_flags) + if DEBUG_GENERAL in gyp.debug.keys(): + DebugOutput(DEBUG_GENERAL, "generator_flags: %s", generator_flags) + + # Generate all requested formats (use a set in case we got one format request + # twice) + for format in set(options.formats): + params = { + "options": options, + "build_files": build_files, + "generator_flags": generator_flags, + "cwd": os.getcwd(), + "build_files_arg": build_files_arg, + "gyp_binary": sys.argv[0], + "home_dot_gyp": home_dot_gyp, + "parallel": options.parallel, + "root_targets": options.root_targets, + "target_arch": cmdline_default_variables.get("target_arch", ""), + } - if not options.depth: - raise GypError('Could not automatically locate src directory. This is' - 'a temporary Chromium feature that will be removed. Use' - '--depth as a workaround.') - - # If toplevel-dir is not set, we assume that depth is the root of our source - # tree. - if not options.toplevel_dir: - options.toplevel_dir = options.depth - - # -D on the command line sets variable defaults - D isn't just for define, - # it's for default. Perhaps there should be a way to force (-F?) a - # variable's value so that it can't be overridden by anything else. - cmdline_default_variables = {} - defines = [] - if options.use_environment: - defines += ShlexEnv('GYP_DEFINES') - if options.defines: - defines += options.defines - cmdline_default_variables = NameValueListToDict(defines) - if DEBUG_GENERAL in gyp.debug: - DebugOutput(DEBUG_GENERAL, - "cmdline_default_variables: %s", cmdline_default_variables) - - # Set up includes. - includes = [] - - # If ~/.gyp/include.gypi exists, it'll be forcibly included into every - # .gyp file that's loaded, before anything else is included. - if home_dot_gyp != None: - default_include = os.path.join(home_dot_gyp, 'include.gypi') - if os.path.exists(default_include): - print('Using overrides found in ' + default_include) - includes.append(default_include) - - # Command-line --include files come after the default include. - if options.includes: - includes.extend(options.includes) - - # Generator flags should be prefixed with the target generator since they - # are global across all generator runs. - gen_flags = [] - if options.use_environment: - gen_flags += ShlexEnv('GYP_GENERATOR_FLAGS') - if options.generator_flags: - gen_flags += options.generator_flags - generator_flags = NameValueListToDict(gen_flags) - if DEBUG_GENERAL in gyp.debug.keys(): - DebugOutput(DEBUG_GENERAL, "generator_flags: %s", generator_flags) - - # Generate all requested formats (use a set in case we got one format request - # twice) - for format in set(options.formats): - params = {'options': options, - 'build_files': build_files, - 'generator_flags': generator_flags, - 'cwd': os.getcwd(), - 'build_files_arg': build_files_arg, - 'gyp_binary': sys.argv[0], - 'home_dot_gyp': home_dot_gyp, - 'parallel': options.parallel, - 'root_targets': options.root_targets, - 'target_arch': cmdline_default_variables.get('target_arch', '')} - - # Start with the default variables from the command line. - [generator, flat_list, targets, data] = Load( - build_files, format, cmdline_default_variables, includes, options.depth, - params, options.check, options.circular_check, - options.duplicate_basename_check) - - # TODO(mark): Pass |data| for now because the generator needs a list of - # build files that came in. In the future, maybe it should just accept - # a list, and not the whole data dict. - # NOTE: flat_list is the flattened dependency graph specifying the order - # that targets may be built. Build systems that operate serially or that - # need to have dependencies defined before dependents reference them should - # generate targets in the order specified in flat_list. - generator.GenerateOutput(flat_list, targets, data, params) - - if options.configs: - valid_configs = targets[flat_list[0]]['configurations'].keys() - for conf in options.configs: - if conf not in valid_configs: - raise GypError('Invalid config specified via --build: %s' % conf) - generator.PerformBuild(data, options.configs, params) - - # Done - return 0 + # Start with the default variables from the command line. + [generator, flat_list, targets, data] = Load( + build_files, + format, + cmdline_default_variables, + includes, + options.depth, + params, + options.check, + options.circular_check, + ) + + # TODO(mark): Pass |data| for now because the generator needs a list of + # build files that came in. In the future, maybe it should just accept + # a list, and not the whole data dict. + # NOTE: flat_list is the flattened dependency graph specifying the order + # that targets may be built. Build systems that operate serially or that + # need to have dependencies defined before dependents reference them should + # generate targets in the order specified in flat_list. + generator.GenerateOutput(flat_list, targets, data, params) + + if options.configs: + valid_configs = targets[flat_list[0]]["configurations"] + for conf in options.configs: + if conf not in valid_configs: + raise GypError("Invalid config specified via --build: %s" % conf) + generator.PerformBuild(data, options.configs, params) + + # Done + return 0 def main(args): - try: - return gyp_main(args) - except GypError as e: - sys.stderr.write("gyp: %s\n" % e) - return 1 + try: + return gyp_main(args) + except GypError as e: + sys.stderr.write("gyp: %s\n" % e) + return 1 + # NOTE: setuptools generated console_scripts calls function with no arguments def script_main(): - return main(sys.argv[1:]) + return main(sys.argv[1:]) + -if __name__ == '__main__': - sys.exit(script_main()) +if __name__ == "__main__": + sys.exit(script_main()) diff --git a/tools/gyp/pylib/gyp/common.py b/tools/gyp/pylib/gyp/common.py index aa410e1dfddbab..a915643867293f 100644 --- a/tools/gyp/pylib/gyp/common.py +++ b/tools/gyp/pylib/gyp/common.py @@ -11,9 +11,9 @@ import subprocess try: - from collections.abc import MutableSet + from collections.abc import MutableSet except ImportError: - from collections import MutableSet + from collections import MutableSet PY3 = bytes != str @@ -21,191 +21,197 @@ # A minimal memoizing decorator. It'll blow up if the args aren't immutable, # among other "problems". class memoize(object): - def __init__(self, func): - self.func = func - self.cache = {} - def __call__(self, *args): - try: - return self.cache[args] - except KeyError: - result = self.func(*args) - self.cache[args] = result - return result + def __init__(self, func): + self.func = func + self.cache = {} + + def __call__(self, *args): + try: + return self.cache[args] + except KeyError: + result = self.func(*args) + self.cache[args] = result + return result class GypError(Exception): - """Error class representing an error, which is to be presented + """Error class representing an error, which is to be presented to the user. The main entry point will catch and display this. """ - pass + + pass def ExceptionAppend(e, msg): - """Append a message to the given exception's message.""" - if not e.args: - e.args = (msg,) - elif len(e.args) == 1: - e.args = (str(e.args[0]) + ' ' + msg,) - else: - e.args = (str(e.args[0]) + ' ' + msg,) + e.args[1:] + """Append a message to the given exception's message.""" + if not e.args: + e.args = (msg,) + elif len(e.args) == 1: + e.args = (str(e.args[0]) + " " + msg,) + else: + e.args = (str(e.args[0]) + " " + msg,) + e.args[1:] def FindQualifiedTargets(target, qualified_list): - """ + """ Given a list of qualified targets, return the qualified targets for the specified |target|. """ - return [t for t in qualified_list if ParseQualifiedTarget(t)[1] == target] + return [t for t in qualified_list if ParseQualifiedTarget(t)[1] == target] def ParseQualifiedTarget(target): - # Splits a qualified target into a build file, target name and toolset. + # Splits a qualified target into a build file, target name and toolset. - # NOTE: rsplit is used to disambiguate the Windows drive letter separator. - target_split = target.rsplit(':', 1) - if len(target_split) == 2: - [build_file, target] = target_split - else: - build_file = None + # NOTE: rsplit is used to disambiguate the Windows drive letter separator. + target_split = target.rsplit(":", 1) + if len(target_split) == 2: + [build_file, target] = target_split + else: + build_file = None - target_split = target.rsplit('#', 1) - if len(target_split) == 2: - [target, toolset] = target_split - else: - toolset = None + target_split = target.rsplit("#", 1) + if len(target_split) == 2: + [target, toolset] = target_split + else: + toolset = None - return [build_file, target, toolset] + return [build_file, target, toolset] def ResolveTarget(build_file, target, toolset): - # This function resolves a target into a canonical form: - # - a fully defined build file, either absolute or relative to the current - # directory - # - a target name - # - a toolset - # - # build_file is the file relative to which 'target' is defined. - # target is the qualified target. - # toolset is the default toolset for that target. - [parsed_build_file, target, parsed_toolset] = ParseQualifiedTarget(target) - - if parsed_build_file: - if build_file: - # If a relative path, parsed_build_file is relative to the directory - # containing build_file. If build_file is not in the current directory, - # parsed_build_file is not a usable path as-is. Resolve it by - # interpreting it as relative to build_file. If parsed_build_file is - # absolute, it is usable as a path regardless of the current directory, - # and os.path.join will return it as-is. - build_file = os.path.normpath(os.path.join(os.path.dirname(build_file), - parsed_build_file)) - # Further (to handle cases like ../cwd), make it relative to cwd) - if not os.path.isabs(build_file): - build_file = RelativePath(build_file, '.') - else: - build_file = parsed_build_file + # This function resolves a target into a canonical form: + # - a fully defined build file, either absolute or relative to the current + # directory + # - a target name + # - a toolset + # + # build_file is the file relative to which 'target' is defined. + # target is the qualified target. + # toolset is the default toolset for that target. + [parsed_build_file, target, parsed_toolset] = ParseQualifiedTarget(target) + + if parsed_build_file: + if build_file: + # If a relative path, parsed_build_file is relative to the directory + # containing build_file. If build_file is not in the current directory, + # parsed_build_file is not a usable path as-is. Resolve it by + # interpreting it as relative to build_file. If parsed_build_file is + # absolute, it is usable as a path regardless of the current directory, + # and os.path.join will return it as-is. + build_file = os.path.normpath( + os.path.join(os.path.dirname(build_file), parsed_build_file) + ) + # Further (to handle cases like ../cwd), make it relative to cwd) + if not os.path.isabs(build_file): + build_file = RelativePath(build_file, ".") + else: + build_file = parsed_build_file - if parsed_toolset: - toolset = parsed_toolset + if parsed_toolset: + toolset = parsed_toolset - return [build_file, target, toolset] + return [build_file, target, toolset] def BuildFile(fully_qualified_target): - # Extracts the build file from the fully qualified target. - return ParseQualifiedTarget(fully_qualified_target)[0] + # Extracts the build file from the fully qualified target. + return ParseQualifiedTarget(fully_qualified_target)[0] def GetEnvironFallback(var_list, default): - """Look up a key in the environment, with fallback to secondary keys + """Look up a key in the environment, with fallback to secondary keys and finally falling back to a default value.""" - for var in var_list: - if var in os.environ: - return os.environ[var] - return default + for var in var_list: + if var in os.environ: + return os.environ[var] + return default def QualifiedTarget(build_file, target, toolset): - # "Qualified" means the file that a target was defined in and the target - # name, separated by a colon, suffixed by a # and the toolset name: - # /path/to/file.gyp:target_name#toolset - fully_qualified = build_file + ':' + target - if toolset: - fully_qualified = fully_qualified + '#' + toolset - return fully_qualified + # "Qualified" means the file that a target was defined in and the target + # name, separated by a colon, suffixed by a # and the toolset name: + # /path/to/file.gyp:target_name#toolset + fully_qualified = build_file + ":" + target + if toolset: + fully_qualified = fully_qualified + "#" + toolset + return fully_qualified @memoize def RelativePath(path, relative_to, follow_path_symlink=True): - # Assuming both |path| and |relative_to| are relative to the current - # directory, returns a relative path that identifies path relative to - # relative_to. - # If |follow_symlink_path| is true (default) and |path| is a symlink, then - # this method returns a path to the real file represented by |path|. If it is - # false, this method returns a path to the symlink. If |path| is not a - # symlink, this option has no effect. - - # Convert to normalized (and therefore absolute paths). - if follow_path_symlink: - path = os.path.realpath(path) - else: - path = os.path.abspath(path) - relative_to = os.path.realpath(relative_to) - - # On Windows, we can't create a relative path to a different drive, so just - # use the absolute path. - if sys.platform == 'win32': - if (os.path.splitdrive(path)[0].lower() != - os.path.splitdrive(relative_to)[0].lower()): - return path - - # Split the paths into components. - path_split = path.split(os.path.sep) - relative_to_split = relative_to.split(os.path.sep) - - # Determine how much of the prefix the two paths share. - prefix_len = len(os.path.commonprefix([path_split, relative_to_split])) - - # Put enough ".." components to back up out of relative_to to the common - # prefix, and then append the part of path_split after the common prefix. - relative_split = [os.path.pardir] * (len(relative_to_split) - prefix_len) + \ - path_split[prefix_len:] - - if len(relative_split) == 0: - # The paths were the same. - return '' - - # Turn it back into a string and we're done. - return os.path.join(*relative_split) + # Assuming both |path| and |relative_to| are relative to the current + # directory, returns a relative path that identifies path relative to + # relative_to. + # If |follow_symlink_path| is true (default) and |path| is a symlink, then + # this method returns a path to the real file represented by |path|. If it is + # false, this method returns a path to the symlink. If |path| is not a + # symlink, this option has no effect. + + # Convert to normalized (and therefore absolute paths). + if follow_path_symlink: + path = os.path.realpath(path) + else: + path = os.path.abspath(path) + relative_to = os.path.realpath(relative_to) + + # On Windows, we can't create a relative path to a different drive, so just + # use the absolute path. + if sys.platform == "win32": + if ( + os.path.splitdrive(path)[0].lower() + != os.path.splitdrive(relative_to)[0].lower() + ): + return path + + # Split the paths into components. + path_split = path.split(os.path.sep) + relative_to_split = relative_to.split(os.path.sep) + + # Determine how much of the prefix the two paths share. + prefix_len = len(os.path.commonprefix([path_split, relative_to_split])) + + # Put enough ".." components to back up out of relative_to to the common + # prefix, and then append the part of path_split after the common prefix. + relative_split = [os.path.pardir] * ( + len(relative_to_split) - prefix_len + ) + path_split[prefix_len:] + + if len(relative_split) == 0: + # The paths were the same. + return "" + + # Turn it back into a string and we're done. + return os.path.join(*relative_split) @memoize def InvertRelativePath(path, toplevel_dir=None): - """Given a path like foo/bar that is relative to toplevel_dir, return + """Given a path like foo/bar that is relative to toplevel_dir, return the inverse relative path back to the toplevel_dir. E.g. os.path.normpath(os.path.join(path, InvertRelativePath(path))) should always produce the empty string, unless the path contains symlinks. """ - if not path: - return path - toplevel_dir = '.' if toplevel_dir is None else toplevel_dir - return RelativePath(toplevel_dir, os.path.join(toplevel_dir, path)) + if not path: + return path + toplevel_dir = "." if toplevel_dir is None else toplevel_dir + return RelativePath(toplevel_dir, os.path.join(toplevel_dir, path)) def FixIfRelativePath(path, relative_to): - # Like RelativePath but returns |path| unchanged if it is absolute. - if os.path.isabs(path): - return path - return RelativePath(path, relative_to) + # Like RelativePath but returns |path| unchanged if it is absolute. + if os.path.isabs(path): + return path + return RelativePath(path, relative_to) def UnrelativePath(path, relative_to): - # Assuming that |relative_to| is relative to the current directory, and |path| - # is a path relative to the dirname of |relative_to|, returns a path that - # identifies |path| relative to the current directory. - rel_dir = os.path.dirname(relative_to) - return os.path.normpath(os.path.join(rel_dir, path)) + # Assuming that |relative_to| is relative to the current directory, and |path| + # is a path relative to the dirname of |relative_to|, returns a path that + # identifies |path| relative to the current directory. + rel_dir = os.path.dirname(relative_to) + return os.path.normpath(os.path.join(rel_dir, path)) # re objects used by EncodePOSIXShellArgument. See IEEE 1003.1 XCU.2.2 at @@ -234,7 +240,7 @@ def UnrelativePath(path, relative_to): # This does not match the characters in _escape, because those need to be # backslash-escaped regardless of whether they appear in a double-quoted # string. -_quote = re.compile('[\t\n #$%&\'()*;<=>?[{|}~]|^$') +_quote = re.compile("[\t\n #$%&'()*;<=>?[{|}~]|^$") # _escape is a pattern that should match any character that needs to be # escaped with a backslash, whether or not the argument matched the _quote @@ -262,8 +268,9 @@ def UnrelativePath(path, relative_to): # shells, there is no room for error here by ignoring !. _escape = re.compile(r'(["\\`])') + def EncodePOSIXShellArgument(argument): - """Encodes |argument| suitably for consumption by POSIX shells. + """Encodes |argument| suitably for consumption by POSIX shells. argument may be quoted and escaped as necessary to ensure that POSIX shells treat the returned value as a literal representing the argument passed to @@ -272,67 +279,67 @@ def EncodePOSIXShellArgument(argument): references to variables to be expanded by the shell. """ - if not isinstance(argument, str): - argument = str(argument) + if not isinstance(argument, str): + argument = str(argument) - if _quote.search(argument): - quote = '"' - else: - quote = '' + if _quote.search(argument): + quote = '"' + else: + quote = "" - encoded = quote + re.sub(_escape, r'\\\1', argument) + quote + encoded = quote + re.sub(_escape, r"\\\1", argument) + quote - return encoded + return encoded def EncodePOSIXShellList(list): - """Encodes |list| suitably for consumption by POSIX shells. + """Encodes |list| suitably for consumption by POSIX shells. Returns EncodePOSIXShellArgument for each item in list, and joins them together using the space character as an argument separator. """ - encoded_arguments = [] - for argument in list: - encoded_arguments.append(EncodePOSIXShellArgument(argument)) - return ' '.join(encoded_arguments) + encoded_arguments = [] + for argument in list: + encoded_arguments.append(EncodePOSIXShellArgument(argument)) + return " ".join(encoded_arguments) def DeepDependencyTargets(target_dicts, roots): - """Returns the recursive list of target dependencies.""" - dependencies = set() - pending = set(roots) - while pending: - # Pluck out one. - r = pending.pop() - # Skip if visited already. - if r in dependencies: - continue - # Add it. - dependencies.add(r) - # Add its children. - spec = target_dicts[r] - pending.update(set(spec.get('dependencies', []))) - pending.update(set(spec.get('dependencies_original', []))) - return list(dependencies - set(roots)) + """Returns the recursive list of target dependencies.""" + dependencies = set() + pending = set(roots) + while pending: + # Pluck out one. + r = pending.pop() + # Skip if visited already. + if r in dependencies: + continue + # Add it. + dependencies.add(r) + # Add its children. + spec = target_dicts[r] + pending.update(set(spec.get("dependencies", []))) + pending.update(set(spec.get("dependencies_original", []))) + return list(dependencies - set(roots)) def BuildFileTargets(target_list, build_file): - """From a target_list, returns the subset from the specified build_file. + """From a target_list, returns the subset from the specified build_file. """ - return [p for p in target_list if BuildFile(p) == build_file] + return [p for p in target_list if BuildFile(p) == build_file] def AllTargets(target_list, target_dicts, build_file): - """Returns all targets (direct and dependencies) for the specified build_file. + """Returns all targets (direct and dependencies) for the specified build_file. """ - bftargets = BuildFileTargets(target_list, build_file) - deptargets = DeepDependencyTargets(target_dicts, bftargets) - return bftargets + deptargets + bftargets = BuildFileTargets(target_list, build_file) + deptargets = DeepDependencyTargets(target_dicts, bftargets) + return bftargets + deptargets def WriteOnDiff(filename): - """Write to a file only if the new contents differ. + """Write to a file only if the new contents differ. Arguments: filename: name of the file to potentially write to. @@ -341,148 +348,152 @@ def WriteOnDiff(filename): the target if it differs (on close). """ - class Writer(object): - """Wrapper around file which only covers the target if it differs.""" - def __init__(self): - # On Cygwin remove the "dir" argument because `C:` prefixed paths are treated as relative, - # consequently ending up with current dir "/cygdrive/c/..." being prefixed to those, which was - # obviously a non-existent path, for example: "/cygdrive/c//C:\". - # See https://docs.python.org/2/library/tempfile.html#tempfile.mkstemp for more details - base_temp_dir = "" if IsCygwin() else os.path.dirname(filename) - # Pick temporary file. - tmp_fd, self.tmp_path = tempfile.mkstemp( - suffix='.tmp', - prefix=os.path.split(filename)[1] + '.gyp.', - dir=base_temp_dir) - try: - self.tmp_file = os.fdopen(tmp_fd, 'wb') - except Exception: - # Don't leave turds behind. - os.unlink(self.tmp_path) - raise - - def __getattr__(self, attrname): - # Delegate everything else to self.tmp_file - return getattr(self.tmp_file, attrname) - - def close(self): - try: - # Close tmp file. - self.tmp_file.close() - # Determine if different. - same = False - try: - same = filecmp.cmp(self.tmp_path, filename, False) - except OSError as e: - if e.errno != errno.ENOENT: - raise - - if same: - # The new file is identical to the old one, just get rid of the new - # one. - os.unlink(self.tmp_path) - else: - # The new file is different from the old one, or there is no old one. - # Rename the new file to the permanent name. - # - # tempfile.mkstemp uses an overly restrictive mode, resulting in a - # file that can only be read by the owner, regardless of the umask. - # There's no reason to not respect the umask here, which means that - # an extra hoop is required to fetch it and reset the new file's mode. - # - # No way to get the umask without setting a new one? Set a safe one - # and then set it back to the old value. - umask = os.umask(0o77) - os.umask(umask) - os.chmod(self.tmp_path, 0o666 & ~umask) - if sys.platform == 'win32' and os.path.exists(filename): - # NOTE: on windows (but not cygwin) rename will not replace an - # existing file, so it must be preceded with a remove. Sadly there - # is no way to make the switch atomic. - os.remove(filename) - os.rename(self.tmp_path, filename) - except Exception: - # Don't leave turds behind. - os.unlink(self.tmp_path) - raise - - def write(self, s): - self.tmp_file.write(s.encode('utf-8')) - - return Writer() + class Writer(object): + """Wrapper around file which only covers the target if it differs.""" + + def __init__(self): + # On Cygwin remove the "dir" argument + # `C:` prefixed paths are treated as relative, + # consequently ending up with current dir "/cygdrive/c/..." + # being prefixed to those, which was + # obviously a non-existent path, + # for example: "/cygdrive/c//C:\". + # For more details see: + # https://docs.python.org/2/library/tempfile.html#tempfile.mkstemp + base_temp_dir = "" if IsCygwin() else os.path.dirname(filename) + # Pick temporary file. + tmp_fd, self.tmp_path = tempfile.mkstemp( + suffix=".tmp", + prefix=os.path.split(filename)[1] + ".gyp.", + dir=base_temp_dir, + ) + try: + self.tmp_file = os.fdopen(tmp_fd, "wb") + except Exception: + # Don't leave turds behind. + os.unlink(self.tmp_path) + raise + + def __getattr__(self, attrname): + # Delegate everything else to self.tmp_file + return getattr(self.tmp_file, attrname) + + def close(self): + try: + # Close tmp file. + self.tmp_file.close() + # Determine if different. + same = False + try: + same = filecmp.cmp(self.tmp_path, filename, False) + except OSError as e: + if e.errno != errno.ENOENT: + raise + + if same: + # The new file is identical to the old one, just get rid of the new + # one. + os.unlink(self.tmp_path) + else: + # The new file is different from the old one, + # or there is no old one. + # Rename the new file to the permanent name. + # + # tempfile.mkstemp uses an overly restrictive mode, resulting in a + # file that can only be read by the owner, regardless of the umask. + # There's no reason to not respect the umask here, + # which means that an extra hoop is required + # to fetch it and reset the new file's mode. + # + # No way to get the umask without setting a new one? Set a safe one + # and then set it back to the old value. + umask = os.umask(0o77) + os.umask(umask) + os.chmod(self.tmp_path, 0o666 & ~umask) + if sys.platform == "win32" and os.path.exists(filename): + # NOTE: on windows (but not cygwin) rename will not replace an + # existing file, so it must be preceded with a remove. + # Sadly there is no way to make the switch atomic. + os.remove(filename) + os.rename(self.tmp_path, filename) + except Exception: + # Don't leave turds behind. + os.unlink(self.tmp_path) + raise + + def write(self, s): + self.tmp_file.write(s.encode("utf-8")) + + return Writer() def EnsureDirExists(path): - """Make sure the directory for |path| exists.""" - try: - os.makedirs(os.path.dirname(path)) - except OSError: - pass + """Make sure the directory for |path| exists.""" + try: + os.makedirs(os.path.dirname(path)) + except OSError: + pass def GetFlavor(params): - """Returns |params.flavor| if it's set, the system's default flavor else.""" - flavors = { - 'cygwin': 'win', - 'win32': 'win', - 'darwin': 'mac', - } - - if 'flavor' in params: - return params['flavor'] - if sys.platform in flavors: - return flavors[sys.platform] - if sys.platform.startswith('sunos'): - return 'solaris' - if sys.platform.startswith(('dragonfly', 'freebsd')): - return 'freebsd' - if sys.platform.startswith('openbsd'): - return 'openbsd' - if sys.platform.startswith('netbsd'): - return 'netbsd' - if sys.platform.startswith('aix'): - return 'aix' - if sys.platform.startswith(('os390', 'zos')): - return 'zos' - - return 'linux' + """Returns |params.flavor| if it's set, the system's default flavor else.""" + flavors = { + "cygwin": "win", + "win32": "win", + "darwin": "mac", + } + + if "flavor" in params: + return params["flavor"] + if sys.platform in flavors: + return flavors[sys.platform] + if sys.platform.startswith("sunos"): + return "solaris" + if sys.platform.startswith(("dragonfly", "freebsd")): + return "freebsd" + if sys.platform.startswith("openbsd"): + return "openbsd" + if sys.platform.startswith("netbsd"): + return "netbsd" + if sys.platform.startswith("aix"): + return "aix" + if sys.platform.startswith(("os390", "zos")): + return "zos" + + return "linux" def CopyTool(flavor, out_path, generator_flags={}): - """Finds (flock|mac|win)_tool.gyp in the gyp directory and copies it + """Finds (flock|mac|win)_tool.gyp in the gyp directory and copies it to |out_path|.""" - # aix and solaris just need flock emulation. mac and win use more complicated - # support scripts. - prefix = { - 'aix': 'flock', - 'solaris': 'flock', - 'mac': 'mac', - 'win': 'win' - }.get(flavor, None) - if not prefix: - return - - # Slurp input file. - source_path = os.path.join( - os.path.dirname(os.path.abspath(__file__)), '%s_tool.py' % prefix) - with open(source_path) as source_file: - source = source_file.readlines() - - # Set custom header flags. - header = '# Generated by gyp. Do not edit.\n' - mac_toolchain_dir = generator_flags.get('mac_toolchain_dir', None) - if flavor == 'mac' and mac_toolchain_dir: - header += "import os;\nos.environ['DEVELOPER_DIR']='%s'\n" \ - % mac_toolchain_dir - - # Add header and write it out. - tool_path = os.path.join(out_path, 'gyp-%s-tool' % prefix) - with open(tool_path, 'w') as tool_file: - tool_file.write( - ''.join([source[0], header] + source[1:])) - - # Make file executable. - os.chmod(tool_path, 0o755) + # aix and solaris just need flock emulation. mac and win use more complicated + # support scripts. + prefix = {"aix": "flock", "solaris": "flock", "mac": "mac", "win": "win"}.get( + flavor, None + ) + if not prefix: + return + + # Slurp input file. + source_path = os.path.join( + os.path.dirname(os.path.abspath(__file__)), "%s_tool.py" % prefix + ) + with open(source_path) as source_file: + source = source_file.readlines() + + # Set custom header flags. + header = "# Generated by gyp. Do not edit.\n" + mac_toolchain_dir = generator_flags.get("mac_toolchain_dir", None) + if flavor == "mac" and mac_toolchain_dir: + header += "import os;\nos.environ['DEVELOPER_DIR']='%s'\n" % mac_toolchain_dir + + # Add header and write it out. + tool_path = os.path.join(out_path, "gyp-%s-tool" % prefix) + with open(tool_path, "w") as tool_file: + tool_file.write("".join([source[0], header] + source[1:])) + + # Make file executable. + os.chmod(tool_path, 0o755) # From Alex Martelli, @@ -491,14 +502,14 @@ def CopyTool(flavor, out_path, generator_flags={}): # First comment, dated 2001/10/13. # (Also in the printed Python Cookbook.) -def uniquer(seq, idfun=None): - if idfun is None: - idfun = lambda x: x + +def uniquer(seq, idfun=lambda x: x): seen = {} result = [] for item in seq: marker = idfun(item) - if marker in seen: continue + if marker in seen: + continue seen[marker] = 1 result.append(item) return result @@ -506,80 +517,82 @@ def uniquer(seq, idfun=None): # Based on http://code.activestate.com/recipes/576694/. class OrderedSet(MutableSet): - def __init__(self, iterable=None): - self.end = end = [] - end += [None, end, end] # sentinel node for doubly linked list - self.map = {} # key --> [key, prev, next] - if iterable is not None: - self |= iterable - - def __len__(self): - return len(self.map) - - def __contains__(self, key): - return key in self.map - - def add(self, key): - if key not in self.map: - end = self.end - curr = end[1] - curr[2] = end[1] = self.map[key] = [key, curr, end] - - def discard(self, key): - if key in self.map: - key, prev_item, next_item = self.map.pop(key) - prev_item[2] = next_item - next_item[1] = prev_item - - def __iter__(self): - end = self.end - curr = end[2] - while curr is not end: - yield curr[0] - curr = curr[2] - - def __reversed__(self): - end = self.end - curr = end[1] - while curr is not end: - yield curr[0] - curr = curr[1] - - # The second argument is an addition that causes a pylint warning. - def pop(self, last=True): # pylint: disable=W0221 - if not self: - raise KeyError('set is empty') - key = self.end[1][0] if last else self.end[2][0] - self.discard(key) - return key - - def __repr__(self): - if not self: - return '%s()' % (self.__class__.__name__,) - return '%s(%r)' % (self.__class__.__name__, list(self)) - - def __eq__(self, other): - if isinstance(other, OrderedSet): - return len(self) == len(other) and list(self) == list(other) - return set(self) == set(other) - - # Extensions to the recipe. - def update(self, iterable): - for i in iterable: - if i not in self: - self.add(i) + def __init__(self, iterable=None): + self.end = end = [] + end += [None, end, end] # sentinel node for doubly linked list + self.map = {} # key --> [key, prev, next] + if iterable is not None: + self |= iterable + + def __len__(self): + return len(self.map) + + def __contains__(self, key): + return key in self.map + + def add(self, key): + if key not in self.map: + end = self.end + curr = end[1] + curr[2] = end[1] = self.map[key] = [key, curr, end] + + def discard(self, key): + if key in self.map: + key, prev_item, next_item = self.map.pop(key) + prev_item[2] = next_item + next_item[1] = prev_item + + def __iter__(self): + end = self.end + curr = end[2] + while curr is not end: + yield curr[0] + curr = curr[2] + + def __reversed__(self): + end = self.end + curr = end[1] + while curr is not end: + yield curr[0] + curr = curr[1] + + # The second argument is an addition that causes a pylint warning. + def pop(self, last=True): # pylint: disable=W0221 + if not self: + raise KeyError("set is empty") + key = self.end[1][0] if last else self.end[2][0] + self.discard(key) + return key + + def __repr__(self): + if not self: + return "%s()" % (self.__class__.__name__,) + return "%s(%r)" % (self.__class__.__name__, list(self)) + + def __eq__(self, other): + if isinstance(other, OrderedSet): + return len(self) == len(other) and list(self) == list(other) + return set(self) == set(other) + + # Extensions to the recipe. + def update(self, iterable): + for i in iterable: + if i not in self: + self.add(i) class CycleError(Exception): - """An exception raised when an unexpected cycle is detected.""" - def __init__(self, nodes): - self.nodes = nodes - def __str__(self): - return 'CycleError: cycle involving: ' + str(self.nodes) + """An exception raised when an unexpected cycle is detected.""" + + def __init__(self, nodes): + self.nodes = nodes + + def __str__(self): + return "CycleError: cycle involving: " + str(self.nodes) def TopologicallySorted(graph, get_edges): - r"""Topologically sort based on a user provided edge definition. + r"""Topologically sort based on a user provided edge definition. Args: graph: A list of node names. @@ -599,44 +612,50 @@ def GetEdges(node): ==> ['a', 'c', b'] """ - get_edges = memoize(get_edges) - visited = set() - visiting = set() - ordered_nodes = [] - def Visit(node): - if node in visiting: - raise CycleError(visiting) - if node in visited: - return - visited.add(node) - visiting.add(node) - for neighbor in get_edges(node): - Visit(neighbor) - visiting.remove(node) - ordered_nodes.insert(0, node) - for node in sorted(graph): - Visit(node) - return ordered_nodes + get_edges = memoize(get_edges) + visited = set() + visiting = set() + ordered_nodes = [] + + def Visit(node): + if node in visiting: + raise CycleError(visiting) + if node in visited: + return + visited.add(node) + visiting.add(node) + for neighbor in get_edges(node): + Visit(neighbor) + visiting.remove(node) + ordered_nodes.insert(0, node) + + for node in sorted(graph): + Visit(node) + return ordered_nodes + def CrossCompileRequested(): - # TODO: figure out how to not build extra host objects in the - # non-cross-compile case when this is enabled, and enable unconditionally. - return (os.environ.get('GYP_CROSSCOMPILE') or - os.environ.get('AR_host') or - os.environ.get('CC_host') or - os.environ.get('CXX_host') or - os.environ.get('AR_target') or - os.environ.get('CC_target') or - os.environ.get('CXX_target')) + # TODO: figure out how to not build extra host objects in the + # non-cross-compile case when this is enabled, and enable unconditionally. + return ( + os.environ.get("GYP_CROSSCOMPILE") + or os.environ.get("AR_host") + or os.environ.get("CC_host") + or os.environ.get("CXX_host") + or os.environ.get("AR_target") + or os.environ.get("CC_target") + or os.environ.get("CXX_target") + ) + def IsCygwin(): - try: - out = subprocess.Popen("uname", - stdout=subprocess.PIPE, - stderr=subprocess.STDOUT) - stdout, stderr = out.communicate() - if PY3: - stdout = stdout.decode("utf-8") - return "CYGWIN" in str(stdout) - except Exception: - return False + try: + out = subprocess.Popen( + "uname", stdout=subprocess.PIPE, stderr=subprocess.STDOUT + ) + stdout, stderr = out.communicate() + if PY3: + stdout = stdout.decode("utf-8") + return "CYGWIN" in str(stdout) + except Exception: + return False diff --git a/tools/gyp/pylib/gyp/common_test.py b/tools/gyp/pylib/gyp/common_test.py index b75bbb8412b6f8..0310fb266c4e4f 100755 --- a/tools/gyp/pylib/gyp/common_test.py +++ b/tools/gyp/pylib/gyp/common_test.py @@ -12,61 +12,67 @@ class TestTopologicallySorted(unittest.TestCase): - def test_Valid(self): - """Test that sorting works on a valid graph with one possible order.""" - graph = { - 'a': ['b', 'c'], - 'b': [], - 'c': ['d'], - 'd': ['b'], + def test_Valid(self): + """Test that sorting works on a valid graph with one possible order.""" + graph = { + "a": ["b", "c"], + "b": [], + "c": ["d"], + "d": ["b"], } - def GetEdge(node): - return tuple(graph[node]) - self.assertEqual( - gyp.common.TopologicallySorted(graph.keys(), GetEdge), - ['a', 'c', 'd', 'b']) - - def test_Cycle(self): - """Test that an exception is thrown on a cyclic graph.""" - graph = { - 'a': ['b'], - 'b': ['c'], - 'c': ['d'], - 'd': ['a'], + + def GetEdge(node): + return tuple(graph[node]) + + self.assertEqual( + gyp.common.TopologicallySorted(graph.keys(), GetEdge), ["a", "c", "d", "b"] + ) + + def test_Cycle(self): + """Test that an exception is thrown on a cyclic graph.""" + graph = { + "a": ["b"], + "b": ["c"], + "c": ["d"], + "d": ["a"], } - def GetEdge(node): - return tuple(graph[node]) - self.assertRaises( - gyp.common.CycleError, gyp.common.TopologicallySorted, - graph.keys(), GetEdge) + + def GetEdge(node): + return tuple(graph[node]) + + self.assertRaises( + gyp.common.CycleError, gyp.common.TopologicallySorted, graph.keys(), GetEdge + ) class TestGetFlavor(unittest.TestCase): - """Test that gyp.common.GetFlavor works as intended""" - original_platform = '' + """Test that gyp.common.GetFlavor works as intended""" + + original_platform = "" - def setUp(self): - self.original_platform = sys.platform + def setUp(self): + self.original_platform = sys.platform - def tearDown(self): - sys.platform = self.original_platform + def tearDown(self): + sys.platform = self.original_platform - def assertFlavor(self, expected, argument, param): - sys.platform = argument - self.assertEqual(expected, gyp.common.GetFlavor(param)) + def assertFlavor(self, expected, argument, param): + sys.platform = argument + self.assertEqual(expected, gyp.common.GetFlavor(param)) - def test_platform_default(self): - self.assertFlavor('freebsd', 'freebsd9' , {}) - self.assertFlavor('freebsd', 'freebsd10', {}) - self.assertFlavor('openbsd', 'openbsd5' , {}) - self.assertFlavor('solaris', 'sunos5' , {}) - self.assertFlavor('solaris', 'sunos' , {}) - self.assertFlavor('linux' , 'linux2' , {}) - self.assertFlavor('linux' , 'linux3' , {}) + def test_platform_default(self): + self.assertFlavor("freebsd", "freebsd9", {}) + self.assertFlavor("freebsd", "freebsd10", {}) + self.assertFlavor("openbsd", "openbsd5", {}) + self.assertFlavor("solaris", "sunos5", {}) + self.assertFlavor("solaris", "sunos", {}) + self.assertFlavor("linux", "linux2", {}) + self.assertFlavor("linux", "linux3", {}) + self.assertFlavor("linux", "linux", {}) - def test_param(self): - self.assertFlavor('foobar', 'linux2' , {'flavor': 'foobar'}) + def test_param(self): + self.assertFlavor("foobar", "linux2", {"flavor": "foobar"}) -if __name__ == '__main__': - unittest.main() +if __name__ == "__main__": + unittest.main() diff --git a/tools/gyp/pylib/gyp/easy_xml.py b/tools/gyp/pylib/gyp/easy_xml.py index 86d0ba6c0c57d4..e0628ef4d8310a 100644 --- a/tools/gyp/pylib/gyp/easy_xml.py +++ b/tools/gyp/pylib/gyp/easy_xml.py @@ -8,8 +8,8 @@ from functools import reduce -def XmlToString(content, encoding='utf-8', pretty=False): - """ Writes the XML content to disk, touching the file only if it has changed. +def XmlToString(content, encoding="utf-8", pretty=False): + """ Writes the XML content to disk, touching the file only if it has changed. Visual Studio files have a lot of pre-defined structures. This function makes it easy to represent these structures as Python data structures, instead of @@ -46,18 +46,18 @@ def XmlToString(content, encoding='utf-8', pretty=False): Returns: The XML content as a string. """ - # We create a huge list of all the elements of the file. - xml_parts = ['' % encoding] - if pretty: - xml_parts.append('\n') - _ConstructContentList(xml_parts, content, pretty) + # We create a huge list of all the elements of the file. + xml_parts = ['' % encoding] + if pretty: + xml_parts.append("\n") + _ConstructContentList(xml_parts, content, pretty) - # Convert it to a string - return ''.join(xml_parts) + # Convert it to a string + return "".join(xml_parts) def _ConstructContentList(xml_parts, specification, pretty, level=0): - """ Appends the XML parts corresponding to the specification. + """ Appends the XML parts corresponding to the specification. Args: xml_parts: A list of XML parts to be appended to. @@ -65,48 +65,49 @@ def _ConstructContentList(xml_parts, specification, pretty, level=0): pretty: True if we want pretty printing with indents and new lines. level: Indentation level. """ - # The first item in a specification is the name of the element. - if pretty: - indentation = ' ' * level - new_line = '\n' - else: - indentation = '' - new_line = '' - name = specification[0] - if not isinstance(name, str): - raise Exception('The first item of an EasyXml specification should be ' - 'a string. Specification was ' + str(specification)) - xml_parts.append(indentation + '<' + name) - - # Optionally in second position is a dictionary of the attributes. - rest = specification[1:] - if rest and isinstance(rest[0], dict): - for at, val in sorted(rest[0].items()): - xml_parts.append(' %s="%s"' % (at, _XmlEscape(val, attr=True))) - rest = rest[1:] - if rest: - xml_parts.append('>') - all_strings = reduce(lambda x, y: x and isinstance(y, str), rest, True) - multi_line = not all_strings - if multi_line and new_line: - xml_parts.append(new_line) - for child_spec in rest: - # If it's a string, append a text node. - # Otherwise recurse over that child definition - if isinstance(child_spec, str): - xml_parts.append(_XmlEscape(child_spec)) - else: - _ConstructContentList(xml_parts, child_spec, pretty, level + 1) - if multi_line and indentation: - xml_parts.append(indentation) - xml_parts.append('%s' % (name, new_line)) - else: - xml_parts.append('/>%s' % new_line) - - -def WriteXmlIfChanged(content, path, encoding='utf-8', pretty=False, - win32=False): - """ Writes the XML content to disk, touching the file only if it has changed. + # The first item in a specification is the name of the element. + if pretty: + indentation = " " * level + new_line = "\n" + else: + indentation = "" + new_line = "" + name = specification[0] + if not isinstance(name, str): + raise Exception( + "The first item of an EasyXml specification should be " + "a string. Specification was " + str(specification) + ) + xml_parts.append(indentation + "<" + name) + + # Optionally in second position is a dictionary of the attributes. + rest = specification[1:] + if rest and isinstance(rest[0], dict): + for at, val in sorted(rest[0].items()): + xml_parts.append(' %s="%s"' % (at, _XmlEscape(val, attr=True))) + rest = rest[1:] + if rest: + xml_parts.append(">") + all_strings = reduce(lambda x, y: x and isinstance(y, str), rest, True) + multi_line = not all_strings + if multi_line and new_line: + xml_parts.append(new_line) + for child_spec in rest: + # If it's a string, append a text node. + # Otherwise recurse over that child definition + if isinstance(child_spec, str): + xml_parts.append(_XmlEscape(child_spec)) + else: + _ConstructContentList(xml_parts, child_spec, pretty, level + 1) + if multi_line and indentation: + xml_parts.append(indentation) + xml_parts.append("%s" % (name, new_line)) + else: + xml_parts.append("/>%s" % new_line) + + +def WriteXmlIfChanged(content, path, encoding="utf-8", pretty=False, win32=False): + """ Writes the XML content to disk, touching the file only if it has changed. Args: content: The structured content to be written. @@ -114,50 +115,49 @@ def WriteXmlIfChanged(content, path, encoding='utf-8', pretty=False, encoding: The encoding to report on the first line of the XML file. pretty: True if we want pretty printing with indents and new lines. """ - xml_string = XmlToString(content, encoding, pretty) - if win32 and os.linesep != '\r\n': - xml_string = xml_string.replace('\n', '\r\n') + xml_string = XmlToString(content, encoding, pretty) + if win32 and os.linesep != "\r\n": + xml_string = xml_string.replace("\n", "\r\n") - default_encoding = locale.getdefaultlocale()[1] - if default_encoding and default_encoding.upper() != encoding.upper(): - xml_string = xml_string.encode(encoding) + default_encoding = locale.getdefaultlocale()[1] + if default_encoding and default_encoding.upper() != encoding.upper(): + xml_string = xml_string.encode(encoding) - # Get the old content - try: - f = open(path, 'r') - existing = f.read() - f.close() - except: - existing = None + # Get the old content + try: + with open(path, "r") as file: + existing = file.read() + except IOError: + existing = None - # It has changed, write it - if existing != xml_string: - f = open(path, 'wb') - f.write(xml_string) - f.close() + # It has changed, write it + if existing != xml_string: + with open(path, "wb") as file: + file.write(xml_string) _xml_escape_map = { - '"': '"', - "'": ''', - '<': '<', - '>': '>', - '&': '&', - '\n': ' ', - '\r': ' ', + '"': """, + "'": "'", + "<": "<", + ">": ">", + "&": "&", + "\n": " ", + "\r": " ", } -_xml_escape_re = re.compile( - "(%s)" % "|".join(map(re.escape, _xml_escape_map.keys()))) +_xml_escape_re = re.compile("(%s)" % "|".join(map(re.escape, _xml_escape_map.keys()))) def _XmlEscape(value, attr=False): - """ Escape a string for inclusion in XML.""" - def replace(match): - m = match.string[match.start() : match.end()] - # don't replace single quotes in attrs - if attr and m == "'": - return m - return _xml_escape_map[m] - return _xml_escape_re.sub(replace, value) + """ Escape a string for inclusion in XML.""" + + def replace(match): + m = match.string[match.start() : match.end()] + # don't replace single quotes in attrs + if attr and m == "'": + return m + return _xml_escape_map[m] + + return _xml_escape_re.sub(replace, value) diff --git a/tools/gyp/pylib/gyp/easy_xml_test.py b/tools/gyp/pylib/gyp/easy_xml_test.py index 664b538a58db60..5bc795ddbe441b 100755 --- a/tools/gyp/pylib/gyp/easy_xml_test.py +++ b/tools/gyp/pylib/gyp/easy_xml_test.py @@ -10,98 +10,103 @@ import unittest try: - from StringIO import StringIO # Python 2 + from StringIO import StringIO # Python 2 except ImportError: - from io import StringIO # Python 3 + from io import StringIO # Python 3 class TestSequenceFunctions(unittest.TestCase): - - def setUp(self): - self.stderr = StringIO() - - def test_EasyXml_simple(self): - self.assertEqual( - easy_xml.XmlToString(['test']), - '') - - self.assertEqual( - easy_xml.XmlToString(['test'], encoding='Windows-1252'), - '') - - def test_EasyXml_simple_with_attributes(self): - self.assertEqual( - easy_xml.XmlToString(['test2', {'a': 'value1', 'b': 'value2'}]), - '') - - def test_EasyXml_escaping(self): - original = '\'"\r&\nfoo' - converted = '<test>\'" & foo' - converted_apos = converted.replace("'", ''') - self.assertEqual( - easy_xml.XmlToString(['test3', {'a': original}, original]), - '%s' % - (converted, converted_apos)) - - def test_EasyXml_pretty(self): - self.assertEqual( - easy_xml.XmlToString( - ['test3', - ['GrandParent', - ['Parent1', - ['Child'] - ], - ['Parent2'] + def setUp(self): + self.stderr = StringIO() + + def test_EasyXml_simple(self): + self.assertEqual( + easy_xml.XmlToString(["test"]), + '', + ) + + self.assertEqual( + easy_xml.XmlToString(["test"], encoding="Windows-1252"), + '', + ) + + def test_EasyXml_simple_with_attributes(self): + self.assertEqual( + easy_xml.XmlToString(["test2", {"a": "value1", "b": "value2"}]), + '', + ) + + def test_EasyXml_escaping(self): + original = "'\"\r&\nfoo" + converted = "<test>'" & foo" + converted_apos = converted.replace("'", "'") + self.assertEqual( + easy_xml.XmlToString(["test3", {"a": original}, original]), + '%s' + % (converted, converted_apos), + ) + + def test_EasyXml_pretty(self): + self.assertEqual( + easy_xml.XmlToString( + ["test3", ["GrandParent", ["Parent1", ["Child"]], ["Parent2"]]], + pretty=True, + ), + '\n' + "\n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + "\n", + ) + + def test_EasyXml_complex(self): + # We want to create: + target = ( + '' + "" + '' + "{D2250C20-3A94-4FB9-AF73-11BC5B73884B}" + "Win32Proj" + "automated_ui_tests" + "" + '' + "' + "Application" + "Unicode" + "" + "" + ) + + xml = easy_xml.XmlToString( + [ + "Project", + [ + "PropertyGroup", + {"Label": "Globals"}, + ["ProjectGuid", "{D2250C20-3A94-4FB9-AF73-11BC5B73884B}"], + ["Keyword", "Win32Proj"], + ["RootNamespace", "automated_ui_tests"], + ], + ["Import", {"Project": "$(VCTargetsPath)\\Microsoft.Cpp.props"}], + [ + "PropertyGroup", + { + "Condition": "'$(Configuration)|$(Platform)'=='Debug|Win32'", + "Label": "Configuration", + }, + ["ConfigurationType", "Application"], + ["CharacterSet", "Unicode"], + ], ] - ], - pretty=True), - '\n' - '\n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - '\n') - - - def test_EasyXml_complex(self): - # We want to create: - target = ( - '' - '' - '' - '{D2250C20-3A94-4FB9-AF73-11BC5B73884B}' - 'Win32Proj' - 'automated_ui_tests' - '' - '' - '' - 'Application' - 'Unicode' - '' - '') - - xml = easy_xml.XmlToString( - ['Project', - ['PropertyGroup', {'Label': 'Globals'}, - ['ProjectGuid', '{D2250C20-3A94-4FB9-AF73-11BC5B73884B}'], - ['Keyword', 'Win32Proj'], - ['RootNamespace', 'automated_ui_tests'] - ], - ['Import', {'Project': '$(VCTargetsPath)\\Microsoft.Cpp.props'}], - ['PropertyGroup', - {'Condition': "'$(Configuration)|$(Platform)'=='Debug|Win32'", - 'Label': 'Configuration'}, - ['ConfigurationType', 'Application'], - ['CharacterSet', 'Unicode'] - ] - ]) - self.assertEqual(xml, target) + ) + self.assertEqual(xml, target) -if __name__ == '__main__': - unittest.main() +if __name__ == "__main__": + unittest.main() diff --git a/tools/gyp/pylib/gyp/flock_tool.py b/tools/gyp/pylib/gyp/flock_tool.py index 81fb79d136a0a5..f9f89e520ac684 100755 --- a/tools/gyp/pylib/gyp/flock_tool.py +++ b/tools/gyp/pylib/gyp/flock_tool.py @@ -14,41 +14,42 @@ def main(args): - executor = FlockTool() - executor.Dispatch(args) + executor = FlockTool() + executor.Dispatch(args) class FlockTool(object): - """This class emulates the 'flock' command.""" - def Dispatch(self, args): - """Dispatches a string command to a method.""" - if len(args) < 1: - raise Exception("Not enough arguments") - - method = "Exec%s" % self._CommandifyName(args[0]) - getattr(self, method)(*args[1:]) - - def _CommandifyName(self, name_string): - """Transforms a tool name like copy-info-plist to CopyInfoPlist""" - return name_string.title().replace('-', '') - - def ExecFlock(self, lockfile, *cmd_list): - """Emulates the most basic behavior of Linux's flock(1).""" - # Rely on exception handling to report errors. - # Note that the stock python on SunOS has a bug - # where fcntl.flock(fd, LOCK_EX) always fails - # with EBADF, that's why we use this F_SETLK - # hack instead. - fd = os.open(lockfile, os.O_WRONLY|os.O_NOCTTY|os.O_CREAT, 0o666) - if sys.platform.startswith('aix'): - # Python on AIX is compiled with LARGEFILE support, which changes the - # struct size. - op = struct.pack('hhIllqq', fcntl.F_WRLCK, 0, 0, 0, 0, 0, 0) - else: - op = struct.pack('hhllhhl', fcntl.F_WRLCK, 0, 0, 0, 0, 0, 0) - fcntl.fcntl(fd, fcntl.F_SETLK, op) - return subprocess.call(cmd_list) - - -if __name__ == '__main__': - sys.exit(main(sys.argv[1:])) + """This class emulates the 'flock' command.""" + + def Dispatch(self, args): + """Dispatches a string command to a method.""" + if len(args) < 1: + raise Exception("Not enough arguments") + + method = "Exec%s" % self._CommandifyName(args[0]) + getattr(self, method)(*args[1:]) + + def _CommandifyName(self, name_string): + """Transforms a tool name like copy-info-plist to CopyInfoPlist""" + return name_string.title().replace("-", "") + + def ExecFlock(self, lockfile, *cmd_list): + """Emulates the most basic behavior of Linux's flock(1).""" + # Rely on exception handling to report errors. + # Note that the stock python on SunOS has a bug + # where fcntl.flock(fd, LOCK_EX) always fails + # with EBADF, that's why we use this F_SETLK + # hack instead. + fd = os.open(lockfile, os.O_WRONLY | os.O_NOCTTY | os.O_CREAT, 0o666) + if sys.platform.startswith("aix"): + # Python on AIX is compiled with LARGEFILE support, which changes the + # struct size. + op = struct.pack("hhIllqq", fcntl.F_WRLCK, 0, 0, 0, 0, 0, 0) + else: + op = struct.pack("hhllhhl", fcntl.F_WRLCK, 0, 0, 0, 0, 0, 0) + fcntl.fcntl(fd, fcntl.F_SETLK, op) + return subprocess.call(cmd_list) + + +if __name__ == "__main__": + sys.exit(main(sys.argv[1:])) diff --git a/tools/gyp/pylib/gyp/generator/analyzer.py b/tools/gyp/pylib/gyp/generator/analyzer.py index 59d73dbedbd319..7a393c1f9393da 100644 --- a/tools/gyp/pylib/gyp/generator/analyzer.py +++ b/tools/gyp/pylib/gyp/generator/analyzer.py @@ -65,18 +65,16 @@ from __future__ import print_function import gyp.common -import gyp.ninja_syntax as ninja_syntax import json import os import posixpath -import sys debug = False -found_dependency_string = 'Found dependency' -no_dependency_string = 'No dependencies' +found_dependency_string = "Found dependency" +no_dependency_string = "No dependencies" # Status when it should be assumed that everything has changed. -all_changed_string = 'Found dependency (all)' +all_changed_string = "Found dependency (all)" # MatchStatus is used indicate if and how a target depends upon the supplied # sources. @@ -96,118 +94,130 @@ generator_wants_static_library_dependencies_adjusted = False -generator_default_variables = { -} -for dirname in ['INTERMEDIATE_DIR', 'SHARED_INTERMEDIATE_DIR', 'PRODUCT_DIR', - 'LIB_DIR', 'SHARED_LIB_DIR']: - generator_default_variables[dirname] = '!!!' - -for unused in ['RULE_INPUT_PATH', 'RULE_INPUT_ROOT', 'RULE_INPUT_NAME', - 'RULE_INPUT_DIRNAME', 'RULE_INPUT_EXT', - 'EXECUTABLE_PREFIX', 'EXECUTABLE_SUFFIX', - 'STATIC_LIB_PREFIX', 'STATIC_LIB_SUFFIX', - 'SHARED_LIB_PREFIX', 'SHARED_LIB_SUFFIX', - 'CONFIGURATION_NAME']: - generator_default_variables[unused] = '' +generator_default_variables = {} +for dirname in [ + "INTERMEDIATE_DIR", + "SHARED_INTERMEDIATE_DIR", + "PRODUCT_DIR", + "LIB_DIR", + "SHARED_LIB_DIR", +]: + generator_default_variables[dirname] = "!!!" + +for unused in [ + "RULE_INPUT_PATH", + "RULE_INPUT_ROOT", + "RULE_INPUT_NAME", + "RULE_INPUT_DIRNAME", + "RULE_INPUT_EXT", + "EXECUTABLE_PREFIX", + "EXECUTABLE_SUFFIX", + "STATIC_LIB_PREFIX", + "STATIC_LIB_SUFFIX", + "SHARED_LIB_PREFIX", + "SHARED_LIB_SUFFIX", + "CONFIGURATION_NAME", +]: + generator_default_variables[unused] = "" def _ToGypPath(path): - """Converts a path to the format used by gyp.""" - if os.sep == '\\' and os.altsep == '/': - return path.replace('\\', '/') - return path + """Converts a path to the format used by gyp.""" + if os.sep == "\\" and os.altsep == "/": + return path.replace("\\", "/") + return path def _ResolveParent(path, base_path_components): - """Resolves |path|, which starts with at least one '../'. Returns an empty + """Resolves |path|, which starts with at least one '../'. Returns an empty string if the path shouldn't be considered. See _AddSources() for a description of |base_path_components|.""" - depth = 0 - while path.startswith('../'): - depth += 1 - path = path[3:] - # Relative includes may go outside the source tree. For example, an action may - # have inputs in /usr/include, which are not in the source tree. - if depth > len(base_path_components): - return '' - if depth == len(base_path_components): - return path - return '/'.join(base_path_components[0:len(base_path_components) - depth]) + \ - '/' + path + depth = 0 + while path.startswith("../"): + depth += 1 + path = path[3:] + # Relative includes may go outside the source tree. For example, an action may + # have inputs in /usr/include, which are not in the source tree. + if depth > len(base_path_components): + return "" + if depth == len(base_path_components): + return path + return ( + "/".join(base_path_components[0 : len(base_path_components) - depth]) + + "/" + + path + ) def _AddSources(sources, base_path, base_path_components, result): - """Extracts valid sources from |sources| and adds them to |result|. Each + """Extracts valid sources from |sources| and adds them to |result|. Each source file is relative to |base_path|, but may contain '..'. To make resolving '..' easier |base_path_components| contains each of the directories in |base_path|. Additionally each source may contain variables. Such sources are ignored as it is assumed dependencies on them are expressed and tracked in some other means.""" - # NOTE: gyp paths are always posix style. - for source in sources: - if not len(source) or source.startswith('!!!') or source.startswith('$'): - continue - # variable expansion may lead to //. - org_source = source - source = source[0] + source[1:].replace('//', '/') - if source.startswith('../'): - source = _ResolveParent(source, base_path_components) - if len(source): - result.append(source) - continue - result.append(base_path + source) - if debug: - print('AddSource', org_source, result[len(result) - 1]) - - -def _ExtractSourcesFromAction(action, base_path, base_path_components, - results): - if 'inputs' in action: - _AddSources(action['inputs'], base_path, base_path_components, results) + # NOTE: gyp paths are always posix style. + for source in sources: + if not len(source) or source.startswith("!!!") or source.startswith("$"): + continue + # variable expansion may lead to //. + org_source = source + source = source[0] + source[1:].replace("//", "/") + if source.startswith("../"): + source = _ResolveParent(source, base_path_components) + if len(source): + result.append(source) + continue + result.append(base_path + source) + if debug: + print("AddSource", org_source, result[len(result) - 1]) + + +def _ExtractSourcesFromAction(action, base_path, base_path_components, results): + if "inputs" in action: + _AddSources(action["inputs"], base_path, base_path_components, results) def _ToLocalPath(toplevel_dir, path): - """Converts |path| to a path relative to |toplevel_dir|.""" - if path == toplevel_dir: - return '' - if path.startswith(toplevel_dir + '/'): - return path[len(toplevel_dir) + len('/'):] - return path + """Converts |path| to a path relative to |toplevel_dir|.""" + if path == toplevel_dir: + return "" + if path.startswith(toplevel_dir + "/"): + return path[len(toplevel_dir) + len("/") :] + return path def _ExtractSources(target, target_dict, toplevel_dir): - # |target| is either absolute or relative and in the format of the OS. Gyp - # source paths are always posix. Convert |target| to a posix path relative to - # |toplevel_dir_|. This is done to make it easy to build source paths. - base_path = posixpath.dirname(_ToLocalPath(toplevel_dir, _ToGypPath(target))) - base_path_components = base_path.split('/') - - # Add a trailing '/' so that _AddSources() can easily build paths. - if len(base_path): - base_path += '/' - - if debug: - print('ExtractSources', target, base_path) - - results = [] - if 'sources' in target_dict: - _AddSources(target_dict['sources'], base_path, base_path_components, - results) - # Include the inputs from any actions. Any changes to these affect the - # resulting output. - if 'actions' in target_dict: - for action in target_dict['actions']: - _ExtractSourcesFromAction(action, base_path, base_path_components, - results) - if 'rules' in target_dict: - for rule in target_dict['rules']: - _ExtractSourcesFromAction(rule, base_path, base_path_components, results) - - return results + # |target| is either absolute or relative and in the format of the OS. Gyp + # source paths are always posix. Convert |target| to a posix path relative to + # |toplevel_dir_|. This is done to make it easy to build source paths. + base_path = posixpath.dirname(_ToLocalPath(toplevel_dir, _ToGypPath(target))) + base_path_components = base_path.split("/") + + # Add a trailing '/' so that _AddSources() can easily build paths. + if len(base_path): + base_path += "/" + + if debug: + print("ExtractSources", target, base_path) + + results = [] + if "sources" in target_dict: + _AddSources(target_dict["sources"], base_path, base_path_components, results) + # Include the inputs from any actions. Any changes to these affect the + # resulting output. + if "actions" in target_dict: + for action in target_dict["actions"]: + _ExtractSourcesFromAction(action, base_path, base_path_components, results) + if "rules" in target_dict: + for rule in target_dict["rules"]: + _ExtractSourcesFromAction(rule, base_path, base_path_components, results) + + return results class Target(object): - """Holds information about a particular target: + """Holds information about a particular target: deps: set of Targets this Target depends upon. This is not recursive, only the direct dependent Targets. match_status: one of the MatchStatus values. @@ -225,101 +235,111 @@ class Target(object): is_static_library: true if the type of target is static_library. is_or_has_linked_ancestor: true if the target does a link (eg executable), or if there is a target in back_deps that does a link.""" - def __init__(self, name): - self.deps = set() - self.match_status = MATCH_STATUS_TBD - self.back_deps = set() - self.name = name - # TODO(sky): I don't like hanging this off Target. This state is specific - # to certain functions and should be isolated there. - self.visited = False - self.requires_build = False - self.added_to_compile_targets = False - self.in_roots = False - self.is_executable = False - self.is_static_library = False - self.is_or_has_linked_ancestor = False + + def __init__(self, name): + self.deps = set() + self.match_status = MATCH_STATUS_TBD + self.back_deps = set() + self.name = name + # TODO(sky): I don't like hanging this off Target. This state is specific + # to certain functions and should be isolated there. + self.visited = False + self.requires_build = False + self.added_to_compile_targets = False + self.in_roots = False + self.is_executable = False + self.is_static_library = False + self.is_or_has_linked_ancestor = False class Config(object): - """Details what we're looking for + """Details what we're looking for files: set of files to search for targets: see file description for details.""" - def __init__(self): - self.files = [] - self.targets = set() - self.additional_compile_target_names = set() - self.test_target_names = set() - - def Init(self, params): - """Initializes Config. This is a separate method as it raises an exception + + def __init__(self): + self.files = [] + self.targets = set() + self.additional_compile_target_names = set() + self.test_target_names = set() + + def Init(self, params): + """Initializes Config. This is a separate method as it raises an exception if there is a parse error.""" - generator_flags = params.get('generator_flags', {}) - config_path = generator_flags.get('config_path', None) - if not config_path: - return - try: - f = open(config_path, 'r') - config = json.load(f) - f.close() - except IOError: - raise Exception('Unable to open file ' + config_path) - except ValueError as e: - raise Exception('Unable to parse config file ' + config_path + str(e)) - if not isinstance(config, dict): - raise Exception('config_path must be a JSON file containing a dictionary') - self.files = config.get('files', []) - self.additional_compile_target_names = set( - config.get('additional_compile_targets', [])) - self.test_target_names = set(config.get('test_targets', [])) + generator_flags = params.get("generator_flags", {}) + config_path = generator_flags.get("config_path", None) + if not config_path: + return + try: + f = open(config_path, "r") + config = json.load(f) + f.close() + except IOError: + raise Exception("Unable to open file " + config_path) + except ValueError as e: + raise Exception("Unable to parse config file " + config_path + str(e)) + if not isinstance(config, dict): + raise Exception("config_path must be a JSON file containing a dictionary") + self.files = config.get("files", []) + self.additional_compile_target_names = set( + config.get("additional_compile_targets", []) + ) + self.test_target_names = set(config.get("test_targets", [])) def _WasBuildFileModified(build_file, data, files, toplevel_dir): - """Returns true if the build file |build_file| is either in |files| or + """Returns true if the build file |build_file| is either in |files| or one of the files included by |build_file| is in |files|. |toplevel_dir| is the root of the source tree.""" - if _ToLocalPath(toplevel_dir, _ToGypPath(build_file)) in files: - if debug: - print('gyp file modified', build_file) - return True + if _ToLocalPath(toplevel_dir, _ToGypPath(build_file)) in files: + if debug: + print("gyp file modified", build_file) + return True - # First element of included_files is the file itself. - if len(data[build_file]['included_files']) <= 1: + # First element of included_files is the file itself. + if len(data[build_file]["included_files"]) <= 1: + return False + + for include_file in data[build_file]["included_files"][1:]: + # |included_files| are relative to the directory of the |build_file|. + rel_include_file = _ToGypPath( + gyp.common.UnrelativePath(include_file, build_file) + ) + if _ToLocalPath(toplevel_dir, rel_include_file) in files: + if debug: + print( + "included gyp file modified, gyp_file=", + build_file, + "included file=", + rel_include_file, + ) + return True return False - for include_file in data[build_file]['included_files'][1:]: - # |included_files| are relative to the directory of the |build_file|. - rel_include_file = \ - _ToGypPath(gyp.common.UnrelativePath(include_file, build_file)) - if _ToLocalPath(toplevel_dir, rel_include_file) in files: - if debug: - print('included gyp file modified, gyp_file=', build_file, - 'included file=', rel_include_file) - return True - return False - def _GetOrCreateTargetByName(targets, target_name): - """Creates or returns the Target at targets[target_name]. If there is no + """Creates or returns the Target at targets[target_name]. If there is no Target for |target_name| one is created. Returns a tuple of whether a new Target was created and the Target.""" - if target_name in targets: - return False, targets[target_name] - target = Target(target_name) - targets[target_name] = target - return True, target + if target_name in targets: + return False, targets[target_name] + target = Target(target_name) + targets[target_name] = target + return True, target def _DoesTargetTypeRequireBuild(target_dict): - """Returns true if the target type is such that it needs to be built.""" - # If a 'none' target has rules or actions we assume it requires a build. - return bool(target_dict['type'] != 'none' or - target_dict.get('actions') or target_dict.get('rules')) + """Returns true if the target type is such that it needs to be built.""" + # If a 'none' target has rules or actions we assume it requires a build. + return bool( + target_dict["type"] != "none" + or target_dict.get("actions") + or target_dict.get("rules") + ) -def _GenerateTargets(data, target_list, target_dicts, toplevel_dir, files, - build_files): - """Returns a tuple of the following: +def _GenerateTargets(data, target_list, target_dicts, toplevel_dir, files, build_files): + """Returns a tuple of the following: . A dictionary mapping from fully qualified name to Target. . A list of the targets that have a source file in |files|. . Targets that constitute the 'all' target. See description at top of file @@ -327,417 +347,463 @@ def _GenerateTargets(data, target_list, target_dicts, toplevel_dir, files, This sets the |match_status| of the targets that contain any of the source files in |files| to MATCH_STATUS_MATCHES. |toplevel_dir| is the root of the source tree.""" - # Maps from target name to Target. - name_to_target = {} - - # Targets that matched. - matching_targets = [] - - # Queue of targets to visit. - targets_to_visit = target_list[:] - - # Maps from build file to a boolean indicating whether the build file is in - # |files|. - build_file_in_files = {} - - # Root targets across all files. - roots = set() - - # Set of Targets in |build_files|. - build_file_targets = set() - - while len(targets_to_visit) > 0: - target_name = targets_to_visit.pop() - created_target, target = _GetOrCreateTargetByName(name_to_target, - target_name) - if created_target: - roots.add(target) - elif target.visited: - continue - - target.visited = True - target.requires_build = _DoesTargetTypeRequireBuild( - target_dicts[target_name]) - target_type = target_dicts[target_name]['type'] - target.is_executable = target_type == 'executable' - target.is_static_library = target_type == 'static_library' - target.is_or_has_linked_ancestor = (target_type == 'executable' or - target_type == 'shared_library') - - build_file = gyp.common.ParseQualifiedTarget(target_name)[0] - if not build_file in build_file_in_files: - build_file_in_files[build_file] = \ - _WasBuildFileModified(build_file, data, files, toplevel_dir) - - if build_file in build_files: - build_file_targets.add(target) - - # If a build file (or any of its included files) is modified we assume all - # targets in the file are modified. - if build_file_in_files[build_file]: - print('matching target from modified build file', target_name) - target.match_status = MATCH_STATUS_MATCHES - matching_targets.append(target) - else: - sources = _ExtractSources(target_name, target_dicts[target_name], - toplevel_dir) - for source in sources: - if _ToGypPath(os.path.normpath(source)) in files: - print('target', target_name, 'matches', source) - target.match_status = MATCH_STATUS_MATCHES - matching_targets.append(target) - break - - # Add dependencies to visit as well as updating back pointers for deps. - for dep in target_dicts[target_name].get('dependencies', []): - targets_to_visit.append(dep) - - created_dep_target, dep_target = _GetOrCreateTargetByName(name_to_target, - dep) - if not created_dep_target: - roots.discard(dep_target) - - target.deps.add(dep_target) - dep_target.back_deps.add(target) - - return name_to_target, matching_targets, roots & build_file_targets + # Maps from target name to Target. + name_to_target = {} + + # Targets that matched. + matching_targets = [] + + # Queue of targets to visit. + targets_to_visit = target_list[:] + + # Maps from build file to a boolean indicating whether the build file is in + # |files|. + build_file_in_files = {} + + # Root targets across all files. + roots = set() + + # Set of Targets in |build_files|. + build_file_targets = set() + + while len(targets_to_visit) > 0: + target_name = targets_to_visit.pop() + created_target, target = _GetOrCreateTargetByName(name_to_target, target_name) + if created_target: + roots.add(target) + elif target.visited: + continue + + target.visited = True + target.requires_build = _DoesTargetTypeRequireBuild(target_dicts[target_name]) + target_type = target_dicts[target_name]["type"] + target.is_executable = target_type == "executable" + target.is_static_library = target_type == "static_library" + target.is_or_has_linked_ancestor = ( + target_type == "executable" or target_type == "shared_library" + ) + + build_file = gyp.common.ParseQualifiedTarget(target_name)[0] + if build_file not in build_file_in_files: + build_file_in_files[build_file] = _WasBuildFileModified( + build_file, data, files, toplevel_dir + ) + + if build_file in build_files: + build_file_targets.add(target) + + # If a build file (or any of its included files) is modified we assume all + # targets in the file are modified. + if build_file_in_files[build_file]: + print("matching target from modified build file", target_name) + target.match_status = MATCH_STATUS_MATCHES + matching_targets.append(target) + else: + sources = _ExtractSources( + target_name, target_dicts[target_name], toplevel_dir + ) + for source in sources: + if _ToGypPath(os.path.normpath(source)) in files: + print("target", target_name, "matches", source) + target.match_status = MATCH_STATUS_MATCHES + matching_targets.append(target) + break + + # Add dependencies to visit as well as updating back pointers for deps. + for dep in target_dicts[target_name].get("dependencies", []): + targets_to_visit.append(dep) + + created_dep_target, dep_target = _GetOrCreateTargetByName( + name_to_target, dep + ) + if not created_dep_target: + roots.discard(dep_target) + + target.deps.add(dep_target) + dep_target.back_deps.add(target) + + return name_to_target, matching_targets, roots & build_file_targets def _GetUnqualifiedToTargetMapping(all_targets, to_find): - """Returns a tuple of the following: + """Returns a tuple of the following: . mapping (dictionary) from unqualified name to Target for all the Targets in |to_find|. . any target names not found. If this is empty all targets were found.""" - result = {} - if not to_find: - return {}, [] - to_find = set(to_find) - for target_name in all_targets.keys(): - extracted = gyp.common.ParseQualifiedTarget(target_name) - if len(extracted) > 1 and extracted[1] in to_find: - to_find.remove(extracted[1]) - result[extracted[1]] = all_targets[target_name] - if not to_find: - return result, [] - return result, [x for x in to_find] + result = {} + if not to_find: + return {}, [] + to_find = set(to_find) + for target_name in all_targets.keys(): + extracted = gyp.common.ParseQualifiedTarget(target_name) + if len(extracted) > 1 and extracted[1] in to_find: + to_find.remove(extracted[1]) + result[extracted[1]] = all_targets[target_name] + if not to_find: + return result, [] + return result, [x for x in to_find] def _DoesTargetDependOnMatchingTargets(target): - """Returns true if |target| or any of its dependencies is one of the + """Returns true if |target| or any of its dependencies is one of the targets containing the files supplied as input to analyzer. This updates |matches| of the Targets as it recurses. target: the Target to look for.""" - if target.match_status == MATCH_STATUS_DOESNT_MATCH: + if target.match_status == MATCH_STATUS_DOESNT_MATCH: + return False + if ( + target.match_status == MATCH_STATUS_MATCHES + or target.match_status == MATCH_STATUS_MATCHES_BY_DEPENDENCY + ): + return True + for dep in target.deps: + if _DoesTargetDependOnMatchingTargets(dep): + target.match_status = MATCH_STATUS_MATCHES_BY_DEPENDENCY + print("\t", target.name, "matches by dep", dep.name) + return True + target.match_status = MATCH_STATUS_DOESNT_MATCH return False - if target.match_status == MATCH_STATUS_MATCHES or \ - target.match_status == MATCH_STATUS_MATCHES_BY_DEPENDENCY: - return True - for dep in target.deps: - if _DoesTargetDependOnMatchingTargets(dep): - target.match_status = MATCH_STATUS_MATCHES_BY_DEPENDENCY - print('\t', target.name, 'matches by dep', dep.name) - return True - target.match_status = MATCH_STATUS_DOESNT_MATCH - return False def _GetTargetsDependingOnMatchingTargets(possible_targets): - """Returns the list of Targets in |possible_targets| that depend (either + """Returns the list of Targets in |possible_targets| that depend (either directly on indirectly) on at least one of the targets containing the files supplied as input to analyzer. possible_targets: targets to search from.""" - found = [] - print('Targets that matched by dependency:') - for target in possible_targets: - if _DoesTargetDependOnMatchingTargets(target): - found.append(target) - return found + found = [] + print("Targets that matched by dependency:") + for target in possible_targets: + if _DoesTargetDependOnMatchingTargets(target): + found.append(target) + return found def _AddCompileTargets(target, roots, add_if_no_ancestor, result): - """Recurses through all targets that depend on |target|, adding all targets + """Recurses through all targets that depend on |target|, adding all targets that need to be built (and are in |roots|) to |result|. roots: set of root targets. add_if_no_ancestor: If true and there are no ancestors of |target| then add |target| to |result|. |target| must still be in |roots|. result: targets that need to be built are added here.""" - if target.visited: - return - - target.visited = True - target.in_roots = target in roots - - for back_dep_target in target.back_deps: - _AddCompileTargets(back_dep_target, roots, False, result) - target.added_to_compile_targets |= back_dep_target.added_to_compile_targets - target.in_roots |= back_dep_target.in_roots - target.is_or_has_linked_ancestor |= ( - back_dep_target.is_or_has_linked_ancestor) - - # Always add 'executable' targets. Even though they may be built by other - # targets that depend upon them it makes detection of what is going to be - # built easier. - # And always add static_libraries that have no dependencies on them from - # linkables. This is necessary as the other dependencies on them may be - # static libraries themselves, which are not compile time dependencies. - if target.in_roots and \ - (target.is_executable or - (not target.added_to_compile_targets and - (add_if_no_ancestor or target.requires_build)) or - (target.is_static_library and add_if_no_ancestor and - not target.is_or_has_linked_ancestor)): - print('\t\tadding to compile targets', target.name, 'executable', - target.is_executable, 'added_to_compile_targets', - target.added_to_compile_targets, 'add_if_no_ancestor', - add_if_no_ancestor, 'requires_build', target.requires_build, - 'is_static_library', target.is_static_library, - 'is_or_has_linked_ancestor', target.is_or_has_linked_ancestor) - result.add(target) - target.added_to_compile_targets = True + if target.visited: + return + + target.visited = True + target.in_roots = target in roots + + for back_dep_target in target.back_deps: + _AddCompileTargets(back_dep_target, roots, False, result) + target.added_to_compile_targets |= back_dep_target.added_to_compile_targets + target.in_roots |= back_dep_target.in_roots + target.is_or_has_linked_ancestor |= back_dep_target.is_or_has_linked_ancestor + + # Always add 'executable' targets. Even though they may be built by other + # targets that depend upon them it makes detection of what is going to be + # built easier. + # And always add static_libraries that have no dependencies on them from + # linkables. This is necessary as the other dependencies on them may be + # static libraries themselves, which are not compile time dependencies. + if target.in_roots and ( + target.is_executable + or ( + not target.added_to_compile_targets + and (add_if_no_ancestor or target.requires_build) + ) + or ( + target.is_static_library + and add_if_no_ancestor + and not target.is_or_has_linked_ancestor + ) + ): + print( + "\t\tadding to compile targets", + target.name, + "executable", + target.is_executable, + "added_to_compile_targets", + target.added_to_compile_targets, + "add_if_no_ancestor", + add_if_no_ancestor, + "requires_build", + target.requires_build, + "is_static_library", + target.is_static_library, + "is_or_has_linked_ancestor", + target.is_or_has_linked_ancestor, + ) + result.add(target) + target.added_to_compile_targets = True def _GetCompileTargets(matching_targets, supplied_targets): - """Returns the set of Targets that require a build. + """Returns the set of Targets that require a build. matching_targets: targets that changed and need to be built. supplied_targets: set of targets supplied to analyzer to search from.""" - result = set() - for target in matching_targets: - print('finding compile targets for match', target.name) - _AddCompileTargets(target, supplied_targets, True, result) - return result + result = set() + for target in matching_targets: + print("finding compile targets for match", target.name) + _AddCompileTargets(target, supplied_targets, True, result) + return result def _WriteOutput(params, **values): - """Writes the output, either to stdout or a file is specified.""" - if 'error' in values: - print('Error:', values['error']) - if 'status' in values: - print(values['status']) - if 'targets' in values: - values['targets'].sort() - print('Supplied targets that depend on changed files:') - for target in values['targets']: - print('\t', target) - if 'invalid_targets' in values: - values['invalid_targets'].sort() - print('The following targets were not found:') - for target in values['invalid_targets']: - print('\t', target) - if 'build_targets' in values: - values['build_targets'].sort() - print('Targets that require a build:') - for target in values['build_targets']: - print('\t', target) - if 'compile_targets' in values: - values['compile_targets'].sort() - print('Targets that need to be built:') - for target in values['compile_targets']: - print('\t', target) - if 'test_targets' in values: - values['test_targets'].sort() - print('Test targets:') - for target in values['test_targets']: - print('\t', target) - - output_path = params.get('generator_flags', {}).get( - 'analyzer_output_path', None) - if not output_path: - print(json.dumps(values)) - return - try: - f = open(output_path, 'w') - f.write(json.dumps(values) + '\n') - f.close() - except IOError as e: - print('Error writing to output file', output_path, str(e)) + """Writes the output, either to stdout or a file is specified.""" + if "error" in values: + print("Error:", values["error"]) + if "status" in values: + print(values["status"]) + if "targets" in values: + values["targets"].sort() + print("Supplied targets that depend on changed files:") + for target in values["targets"]: + print("\t", target) + if "invalid_targets" in values: + values["invalid_targets"].sort() + print("The following targets were not found:") + for target in values["invalid_targets"]: + print("\t", target) + if "build_targets" in values: + values["build_targets"].sort() + print("Targets that require a build:") + for target in values["build_targets"]: + print("\t", target) + if "compile_targets" in values: + values["compile_targets"].sort() + print("Targets that need to be built:") + for target in values["compile_targets"]: + print("\t", target) + if "test_targets" in values: + values["test_targets"].sort() + print("Test targets:") + for target in values["test_targets"]: + print("\t", target) + + output_path = params.get("generator_flags", {}).get("analyzer_output_path", None) + if not output_path: + print(json.dumps(values)) + return + try: + f = open(output_path, "w") + f.write(json.dumps(values) + "\n") + f.close() + except IOError as e: + print("Error writing to output file", output_path, str(e)) def _WasGypIncludeFileModified(params, files): - """Returns true if one of the files in |files| is in the set of included + """Returns true if one of the files in |files| is in the set of included files.""" - if params['options'].includes: - for include in params['options'].includes: - if _ToGypPath(os.path.normpath(include)) in files: - print('Include file modified, assuming all changed', include) - return True - return False + if params["options"].includes: + for include in params["options"].includes: + if _ToGypPath(os.path.normpath(include)) in files: + print("Include file modified, assuming all changed", include) + return True + return False def _NamesNotIn(names, mapping): - """Returns a list of the values in |names| that are not in |mapping|.""" - return [name for name in names if name not in mapping] + """Returns a list of the values in |names| that are not in |mapping|.""" + return [name for name in names if name not in mapping] def _LookupTargets(names, mapping): - """Returns a list of the mapping[name] for each value in |names| that is in + """Returns a list of the mapping[name] for each value in |names| that is in |mapping|.""" - return [mapping[name] for name in names if name in mapping] + return [mapping[name] for name in names if name in mapping] def CalculateVariables(default_variables, params): - """Calculate additional variables for use in the build (called by gyp).""" - flavor = gyp.common.GetFlavor(params) - if flavor == 'mac': - default_variables.setdefault('OS', 'mac') - elif flavor == 'win': - default_variables.setdefault('OS', 'win') - # Copy additional generator configuration data from VS, which is shared - # by the Windows Ninja generator. - import gyp.generator.msvs as msvs_generator - generator_additional_non_configuration_keys = getattr(msvs_generator, - 'generator_additional_non_configuration_keys', []) - generator_additional_path_sections = getattr(msvs_generator, - 'generator_additional_path_sections', []) - - gyp.msvs_emulation.CalculateCommonVariables(default_variables, params) - else: - operating_system = flavor - if flavor == 'android': - operating_system = 'linux' # Keep this legacy behavior for now. - default_variables.setdefault('OS', operating_system) + """Calculate additional variables for use in the build (called by gyp).""" + flavor = gyp.common.GetFlavor(params) + if flavor == "mac": + default_variables.setdefault("OS", "mac") + elif flavor == "win": + default_variables.setdefault("OS", "win") + gyp.msvs_emulation.CalculateCommonVariables(default_variables, params) + else: + operating_system = flavor + if flavor == "android": + operating_system = "linux" # Keep this legacy behavior for now. + default_variables.setdefault("OS", operating_system) class TargetCalculator(object): - """Calculates the matching test_targets and matching compile_targets.""" - def __init__(self, files, additional_compile_target_names, test_target_names, - data, target_list, target_dicts, toplevel_dir, build_files): - self._additional_compile_target_names = set(additional_compile_target_names) - self._test_target_names = set(test_target_names) - self._name_to_target, self._changed_targets, self._root_targets = ( - _GenerateTargets(data, target_list, target_dicts, toplevel_dir, - frozenset(files), build_files)) - self._unqualified_mapping, self.invalid_targets = ( - _GetUnqualifiedToTargetMapping(self._name_to_target, - self._supplied_target_names_no_all())) - - def _supplied_target_names(self): - return self._additional_compile_target_names | self._test_target_names - - def _supplied_target_names_no_all(self): - """Returns the supplied test targets without 'all'.""" - result = self._supplied_target_names() - result.discard('all') - return result - - def is_build_impacted(self): - """Returns true if the supplied files impact the build at all.""" - return self._changed_targets - - def find_matching_test_target_names(self): - """Returns the set of output test targets.""" - assert self.is_build_impacted() - # Find the test targets first. 'all' is special cased to mean all the - # root targets. To deal with all the supplied |test_targets| are expanded - # to include the root targets during lookup. If any of the root targets - # match, we remove it and replace it with 'all'. - test_target_names_no_all = set(self._test_target_names) - test_target_names_no_all.discard('all') - test_targets_no_all = _LookupTargets(test_target_names_no_all, - self._unqualified_mapping) - test_target_names_contains_all = 'all' in self._test_target_names - if test_target_names_contains_all: - test_targets = [x for x in (set(test_targets_no_all) | - set(self._root_targets))] - else: - test_targets = [x for x in test_targets_no_all] - print('supplied test_targets') - for target_name in self._test_target_names: - print('\t', target_name) - print('found test_targets') - for target in test_targets: - print('\t', target.name) - print('searching for matching test targets') - matching_test_targets = _GetTargetsDependingOnMatchingTargets(test_targets) - matching_test_targets_contains_all = (test_target_names_contains_all and - set(matching_test_targets) & - set(self._root_targets)) - if matching_test_targets_contains_all: - # Remove any of the targets for all that were not explicitly supplied, - # 'all' is subsequentely added to the matching names below. - matching_test_targets = [x for x in (set(matching_test_targets) & - set(test_targets_no_all))] - print('matched test_targets') - for target in matching_test_targets: - print('\t', target.name) - matching_target_names = [gyp.common.ParseQualifiedTarget(target.name)[1] - for target in matching_test_targets] - if matching_test_targets_contains_all: - matching_target_names.append('all') - print('\tall') - return matching_target_names - - def find_matching_compile_target_names(self): - """Returns the set of output compile targets.""" - assert self.is_build_impacted() - # Compile targets are found by searching up from changed targets. - # Reset the visited status for _GetBuildTargets. - for target in self._name_to_target.values(): - target.visited = False - - supplied_targets = _LookupTargets(self._supplied_target_names_no_all(), - self._unqualified_mapping) - if 'all' in self._supplied_target_names(): - supplied_targets = [x for x in (set(supplied_targets) | - set(self._root_targets))] - print('Supplied test_targets & compile_targets') - for target in supplied_targets: - print('\t', target.name) - print('Finding compile targets') - compile_targets = _GetCompileTargets(self._changed_targets, - supplied_targets) - return [gyp.common.ParseQualifiedTarget(target.name)[1] - for target in compile_targets] + """Calculates the matching test_targets and matching compile_targets.""" + + def __init__( + self, + files, + additional_compile_target_names, + test_target_names, + data, + target_list, + target_dicts, + toplevel_dir, + build_files, + ): + self._additional_compile_target_names = set(additional_compile_target_names) + self._test_target_names = set(test_target_names) + ( + self._name_to_target, + self._changed_targets, + self._root_targets, + ) = _GenerateTargets( + data, target_list, target_dicts, toplevel_dir, frozenset(files), build_files + ) + ( + self._unqualified_mapping, + self.invalid_targets, + ) = _GetUnqualifiedToTargetMapping( + self._name_to_target, self._supplied_target_names_no_all() + ) + + def _supplied_target_names(self): + return self._additional_compile_target_names | self._test_target_names + + def _supplied_target_names_no_all(self): + """Returns the supplied test targets without 'all'.""" + result = self._supplied_target_names() + result.discard("all") + return result + + def is_build_impacted(self): + """Returns true if the supplied files impact the build at all.""" + return self._changed_targets + + def find_matching_test_target_names(self): + """Returns the set of output test targets.""" + assert self.is_build_impacted() + # Find the test targets first. 'all' is special cased to mean all the + # root targets. To deal with all the supplied |test_targets| are expanded + # to include the root targets during lookup. If any of the root targets + # match, we remove it and replace it with 'all'. + test_target_names_no_all = set(self._test_target_names) + test_target_names_no_all.discard("all") + test_targets_no_all = _LookupTargets( + test_target_names_no_all, self._unqualified_mapping + ) + test_target_names_contains_all = "all" in self._test_target_names + if test_target_names_contains_all: + test_targets = [ + x for x in (set(test_targets_no_all) | set(self._root_targets)) + ] + else: + test_targets = [x for x in test_targets_no_all] + print("supplied test_targets") + for target_name in self._test_target_names: + print("\t", target_name) + print("found test_targets") + for target in test_targets: + print("\t", target.name) + print("searching for matching test targets") + matching_test_targets = _GetTargetsDependingOnMatchingTargets(test_targets) + matching_test_targets_contains_all = test_target_names_contains_all and set( + matching_test_targets + ) & set(self._root_targets) + if matching_test_targets_contains_all: + # Remove any of the targets for all that were not explicitly supplied, + # 'all' is subsequentely added to the matching names below. + matching_test_targets = [ + x for x in (set(matching_test_targets) & set(test_targets_no_all)) + ] + print("matched test_targets") + for target in matching_test_targets: + print("\t", target.name) + matching_target_names = [ + gyp.common.ParseQualifiedTarget(target.name)[1] + for target in matching_test_targets + ] + if matching_test_targets_contains_all: + matching_target_names.append("all") + print("\tall") + return matching_target_names + + def find_matching_compile_target_names(self): + """Returns the set of output compile targets.""" + assert self.is_build_impacted() + # Compile targets are found by searching up from changed targets. + # Reset the visited status for _GetBuildTargets. + for target in self._name_to_target.values(): + target.visited = False + + supplied_targets = _LookupTargets( + self._supplied_target_names_no_all(), self._unqualified_mapping + ) + if "all" in self._supplied_target_names(): + supplied_targets = [ + x for x in (set(supplied_targets) | set(self._root_targets)) + ] + print("Supplied test_targets & compile_targets") + for target in supplied_targets: + print("\t", target.name) + print("Finding compile targets") + compile_targets = _GetCompileTargets(self._changed_targets, supplied_targets) + return [ + gyp.common.ParseQualifiedTarget(target.name)[1] + for target in compile_targets + ] def GenerateOutput(target_list, target_dicts, data, params): - """Called by gyp as the final stage. Outputs results.""" - config = Config() - try: - config.Init(params) - - if not config.files: - raise Exception('Must specify files to analyze via config_path generator ' - 'flag') - - toplevel_dir = _ToGypPath(os.path.abspath(params['options'].toplevel_dir)) - if debug: - print('toplevel_dir', toplevel_dir) - - if _WasGypIncludeFileModified(params, config.files): - result_dict = { 'status': all_changed_string, - 'test_targets': list(config.test_target_names), - 'compile_targets': list( - config.additional_compile_target_names | - config.test_target_names) } - _WriteOutput(params, **result_dict) - return - - calculator = TargetCalculator(config.files, - config.additional_compile_target_names, - config.test_target_names, data, - target_list, target_dicts, toplevel_dir, - params['build_files']) - if not calculator.is_build_impacted(): - result_dict = { 'status': no_dependency_string, - 'test_targets': [], - 'compile_targets': [] } - if calculator.invalid_targets: - result_dict['invalid_targets'] = calculator.invalid_targets - _WriteOutput(params, **result_dict) - return - - test_target_names = calculator.find_matching_test_target_names() - compile_target_names = calculator.find_matching_compile_target_names() - found_at_least_one_target = compile_target_names or test_target_names - result_dict = { 'test_targets': test_target_names, - 'status': found_dependency_string if - found_at_least_one_target else no_dependency_string, - 'compile_targets': list( - set(compile_target_names) | - set(test_target_names)) } - if calculator.invalid_targets: - result_dict['invalid_targets'] = calculator.invalid_targets - _WriteOutput(params, **result_dict) - - except Exception as e: - _WriteOutput(params, error=str(e)) + """Called by gyp as the final stage. Outputs results.""" + config = Config() + try: + config.Init(params) + + if not config.files: + raise Exception( + "Must specify files to analyze via config_path generator " "flag" + ) + + toplevel_dir = _ToGypPath(os.path.abspath(params["options"].toplevel_dir)) + if debug: + print("toplevel_dir", toplevel_dir) + + if _WasGypIncludeFileModified(params, config.files): + result_dict = { + "status": all_changed_string, + "test_targets": list(config.test_target_names), + "compile_targets": list( + config.additional_compile_target_names | config.test_target_names + ), + } + _WriteOutput(params, **result_dict) + return + + calculator = TargetCalculator( + config.files, + config.additional_compile_target_names, + config.test_target_names, + data, + target_list, + target_dicts, + toplevel_dir, + params["build_files"], + ) + if not calculator.is_build_impacted(): + result_dict = { + "status": no_dependency_string, + "test_targets": [], + "compile_targets": [], + } + if calculator.invalid_targets: + result_dict["invalid_targets"] = calculator.invalid_targets + _WriteOutput(params, **result_dict) + return + + test_target_names = calculator.find_matching_test_target_names() + compile_target_names = calculator.find_matching_compile_target_names() + found_at_least_one_target = compile_target_names or test_target_names + result_dict = { + "test_targets": test_target_names, + "status": found_dependency_string + if found_at_least_one_target + else no_dependency_string, + "compile_targets": list(set(compile_target_names) | set(test_target_names)), + } + if calculator.invalid_targets: + result_dict["invalid_targets"] = calculator.invalid_targets + _WriteOutput(params, **result_dict) + + except Exception as e: + _WriteOutput(params, error=str(e)) diff --git a/tools/gyp/pylib/gyp/generator/android.py b/tools/gyp/pylib/gyp/generator/android.py index cecb28c3660b5e..16728847c5c9f2 100644 --- a/tools/gyp/pylib/gyp/generator/android.py +++ b/tools/gyp/pylib/gyp/generator/android.py @@ -24,24 +24,24 @@ import subprocess generator_default_variables = { - 'OS': 'android', - 'EXECUTABLE_PREFIX': '', - 'EXECUTABLE_SUFFIX': '', - 'STATIC_LIB_PREFIX': 'lib', - 'SHARED_LIB_PREFIX': 'lib', - 'STATIC_LIB_SUFFIX': '.a', - 'SHARED_LIB_SUFFIX': '.so', - 'INTERMEDIATE_DIR': '$(gyp_intermediate_dir)', - 'SHARED_INTERMEDIATE_DIR': '$(gyp_shared_intermediate_dir)', - 'PRODUCT_DIR': '$(gyp_shared_intermediate_dir)', - 'SHARED_LIB_DIR': '$(builddir)/lib.$(TOOLSET)', - 'LIB_DIR': '$(obj).$(TOOLSET)', - 'RULE_INPUT_ROOT': '%(INPUT_ROOT)s', # This gets expanded by Python. - 'RULE_INPUT_DIRNAME': '%(INPUT_DIRNAME)s', # This gets expanded by Python. - 'RULE_INPUT_PATH': '$(RULE_SOURCES)', - 'RULE_INPUT_EXT': '$(suffix $<)', - 'RULE_INPUT_NAME': '$(notdir $<)', - 'CONFIGURATION_NAME': '$(GYP_CONFIGURATION)', + "OS": "android", + "EXECUTABLE_PREFIX": "", + "EXECUTABLE_SUFFIX": "", + "STATIC_LIB_PREFIX": "lib", + "SHARED_LIB_PREFIX": "lib", + "STATIC_LIB_SUFFIX": ".a", + "SHARED_LIB_SUFFIX": ".so", + "INTERMEDIATE_DIR": "$(gyp_intermediate_dir)", + "SHARED_INTERMEDIATE_DIR": "$(gyp_shared_intermediate_dir)", + "PRODUCT_DIR": "$(gyp_shared_intermediate_dir)", + "SHARED_LIB_DIR": "$(builddir)/lib.$(TOOLSET)", + "LIB_DIR": "$(obj).$(TOOLSET)", + "RULE_INPUT_ROOT": "%(INPUT_ROOT)s", # This gets expanded by Python. + "RULE_INPUT_DIRNAME": "%(INPUT_DIRNAME)s", # This gets expanded by Python. + "RULE_INPUT_PATH": "$(RULE_SOURCES)", + "RULE_INPUT_EXT": "$(suffix $<)", + "RULE_INPUT_NAME": "$(notdir $<)", + "CONFIGURATION_NAME": "$(GYP_CONFIGURATION)", } # Make supports multiple toolsets @@ -51,9 +51,9 @@ # Generator-specific gyp specs. generator_additional_non_configuration_keys = [ # Boolean to declare that this target does not want its name mangled. - 'android_unmangled_name', + "android_unmangled_name", # Map of android build system variables to set. - 'aosp_build_settings', + "aosp_build_settings", ] generator_additional_path_sections = [] generator_extra_sources_for_rules = [] @@ -72,20 +72,20 @@ # Map gyp target types to Android module classes. MODULE_CLASSES = { - 'static_library': 'STATIC_LIBRARIES', - 'shared_library': 'SHARED_LIBRARIES', - 'executable': 'EXECUTABLES', + "static_library": "STATIC_LIBRARIES", + "shared_library": "SHARED_LIBRARIES", + "executable": "EXECUTABLES", } def IsCPPExtension(ext): - return make.COMPILABLE_EXTENSIONS.get(ext) == 'cxx' + return make.COMPILABLE_EXTENSIONS.get(ext) == "cxx" def Sourceify(path): - """Convert a path to its source directory form. The Android backend does not + """Convert a path to its source directory form. The Android backend does not support options.generator_output, so this function is a noop.""" - return path + return path # Map from qualified target to path to output. @@ -101,17 +101,27 @@ def Sourceify(path): class AndroidMkWriter(object): - """AndroidMkWriter packages up the writing of one target-specific Android.mk. + """AndroidMkWriter packages up the writing of one target-specific Android.mk. Its only real entry point is Write(), and is mostly used for namespacing. """ - def __init__(self, android_top_dir): - self.android_top_dir = android_top_dir - - def Write(self, qualified_target, relative_target, base_path, output_filename, - spec, configs, part_of_all, write_alias_target, sdk_version): - """The main entry point: writes a .mk file for a single target. + def __init__(self, android_top_dir): + self.android_top_dir = android_top_dir + + def Write( + self, + qualified_target, + relative_target, + base_path, + output_filename, + spec, + configs, + part_of_all, + write_alias_target, + sdk_version, + ): + """The main entry point: writes a .mk file for a single target. Arguments: qualified_target: target we're generating @@ -125,114 +135,124 @@ def Write(self, qualified_target, relative_target, base_path, output_filename, this target sdk_version: what to emit for LOCAL_SDK_VERSION in output """ - gyp.common.EnsureDirExists(output_filename) - - self.fp = open(output_filename, 'w') - - self.fp.write(header) - - self.qualified_target = qualified_target - self.relative_target = relative_target - self.path = base_path - self.target = spec['target_name'] - self.type = spec['type'] - self.toolset = spec['toolset'] - - deps, link_deps = self.ComputeDeps(spec) - - # Some of the generation below can add extra output, sources, or - # link dependencies. All of the out params of the functions that - # follow use names like extra_foo. - extra_outputs = [] - extra_sources = [] - - self.android_class = MODULE_CLASSES.get(self.type, 'GYP') - self.android_module = self.ComputeAndroidModule(spec) - (self.android_stem, self.android_suffix) = self.ComputeOutputParts(spec) - self.output = self.output_binary = self.ComputeOutput(spec) - - # Standard header. - self.WriteLn('include $(CLEAR_VARS)\n') - - # Module class and name. - self.WriteLn('LOCAL_MODULE_CLASS := ' + self.android_class) - self.WriteLn('LOCAL_MODULE := ' + self.android_module) - # Only emit LOCAL_MODULE_STEM if it's different to LOCAL_MODULE. - # The library module classes fail if the stem is set. ComputeOutputParts - # makes sure that stem == modulename in these cases. - if self.android_stem != self.android_module: - self.WriteLn('LOCAL_MODULE_STEM := ' + self.android_stem) - self.WriteLn('LOCAL_MODULE_SUFFIX := ' + self.android_suffix) - if self.toolset == 'host': - self.WriteLn('LOCAL_IS_HOST_MODULE := true') - self.WriteLn('LOCAL_MULTILIB := $(GYP_HOST_MULTILIB)') - elif sdk_version > 0: - self.WriteLn('LOCAL_MODULE_TARGET_ARCH := ' - '$(TARGET_$(GYP_VAR_PREFIX)ARCH)') - self.WriteLn('LOCAL_SDK_VERSION := %s' % sdk_version) - - # Grab output directories; needed for Actions and Rules. - if self.toolset == 'host': - self.WriteLn('gyp_intermediate_dir := ' - '$(call local-intermediates-dir,,$(GYP_HOST_VAR_PREFIX))') - else: - self.WriteLn('gyp_intermediate_dir := ' - '$(call local-intermediates-dir,,$(GYP_VAR_PREFIX))') - self.WriteLn('gyp_shared_intermediate_dir := ' - '$(call intermediates-dir-for,GYP,shared,,,$(GYP_VAR_PREFIX))') - self.WriteLn() - - # List files this target depends on so that actions/rules/copies/sources - # can depend on the list. - # TODO: doesn't pull in things through transitive link deps; needed? - target_dependencies = [x[1] for x in deps if x[0] == 'path'] - self.WriteLn('# Make sure our deps are built first.') - self.WriteList(target_dependencies, 'GYP_TARGET_DEPENDENCIES', - local_pathify=True) - - # Actions must come first, since they can generate more OBJs for use below. - if 'actions' in spec: - self.WriteActions(spec['actions'], extra_sources, extra_outputs) - - # Rules must be early like actions. - if 'rules' in spec: - self.WriteRules(spec['rules'], extra_sources, extra_outputs) - - if 'copies' in spec: - self.WriteCopies(spec['copies'], extra_outputs) - - # GYP generated outputs. - self.WriteList(extra_outputs, 'GYP_GENERATED_OUTPUTS', local_pathify=True) - - # Set LOCAL_ADDITIONAL_DEPENDENCIES so that Android's build rules depend - # on both our dependency targets and our generated files. - self.WriteLn('# Make sure our deps and generated files are built first.') - self.WriteLn('LOCAL_ADDITIONAL_DEPENDENCIES := $(GYP_TARGET_DEPENDENCIES) ' - '$(GYP_GENERATED_OUTPUTS)') - self.WriteLn() - - # Sources. - if spec.get('sources', []) or extra_sources: - self.WriteSources(spec, configs, extra_sources) - - self.WriteTarget(spec, configs, deps, link_deps, part_of_all, - write_alias_target) - - # Update global list of target outputs, used in dependency tracking. - target_outputs[qualified_target] = ('path', self.output_binary) - - # Update global list of link dependencies. - if self.type == 'static_library': - target_link_deps[qualified_target] = ('static', self.android_module) - elif self.type == 'shared_library': - target_link_deps[qualified_target] = ('shared', self.android_module) - - self.fp.close() - return self.android_module - - - def WriteActions(self, actions, extra_sources, extra_outputs): - """Write Makefile code for any 'actions' from the gyp input. + gyp.common.EnsureDirExists(output_filename) + + self.fp = open(output_filename, "w") + + self.fp.write(header) + + self.qualified_target = qualified_target + self.relative_target = relative_target + self.path = base_path + self.target = spec["target_name"] + self.type = spec["type"] + self.toolset = spec["toolset"] + + deps, link_deps = self.ComputeDeps(spec) + + # Some of the generation below can add extra output, sources, or + # link dependencies. All of the out params of the functions that + # follow use names like extra_foo. + extra_outputs = [] + extra_sources = [] + + self.android_class = MODULE_CLASSES.get(self.type, "GYP") + self.android_module = self.ComputeAndroidModule(spec) + (self.android_stem, self.android_suffix) = self.ComputeOutputParts(spec) + self.output = self.output_binary = self.ComputeOutput(spec) + + # Standard header. + self.WriteLn("include $(CLEAR_VARS)\n") + + # Module class and name. + self.WriteLn("LOCAL_MODULE_CLASS := " + self.android_class) + self.WriteLn("LOCAL_MODULE := " + self.android_module) + # Only emit LOCAL_MODULE_STEM if it's different to LOCAL_MODULE. + # The library module classes fail if the stem is set. ComputeOutputParts + # makes sure that stem == modulename in these cases. + if self.android_stem != self.android_module: + self.WriteLn("LOCAL_MODULE_STEM := " + self.android_stem) + self.WriteLn("LOCAL_MODULE_SUFFIX := " + self.android_suffix) + if self.toolset == "host": + self.WriteLn("LOCAL_IS_HOST_MODULE := true") + self.WriteLn("LOCAL_MULTILIB := $(GYP_HOST_MULTILIB)") + elif sdk_version > 0: + self.WriteLn( + "LOCAL_MODULE_TARGET_ARCH := " "$(TARGET_$(GYP_VAR_PREFIX)ARCH)" + ) + self.WriteLn("LOCAL_SDK_VERSION := %s" % sdk_version) + + # Grab output directories; needed for Actions and Rules. + if self.toolset == "host": + self.WriteLn( + "gyp_intermediate_dir := " + "$(call local-intermediates-dir,,$(GYP_HOST_VAR_PREFIX))" + ) + else: + self.WriteLn( + "gyp_intermediate_dir := " + "$(call local-intermediates-dir,,$(GYP_VAR_PREFIX))" + ) + self.WriteLn( + "gyp_shared_intermediate_dir := " + "$(call intermediates-dir-for,GYP,shared,,,$(GYP_VAR_PREFIX))" + ) + self.WriteLn() + + # List files this target depends on so that actions/rules/copies/sources + # can depend on the list. + # TODO: doesn't pull in things through transitive link deps; needed? + target_dependencies = [x[1] for x in deps if x[0] == "path"] + self.WriteLn("# Make sure our deps are built first.") + self.WriteList( + target_dependencies, "GYP_TARGET_DEPENDENCIES", local_pathify=True + ) + + # Actions must come first, since they can generate more OBJs for use below. + if "actions" in spec: + self.WriteActions(spec["actions"], extra_sources, extra_outputs) + + # Rules must be early like actions. + if "rules" in spec: + self.WriteRules(spec["rules"], extra_sources, extra_outputs) + + if "copies" in spec: + self.WriteCopies(spec["copies"], extra_outputs) + + # GYP generated outputs. + self.WriteList(extra_outputs, "GYP_GENERATED_OUTPUTS", local_pathify=True) + + # Set LOCAL_ADDITIONAL_DEPENDENCIES so that Android's build rules depend + # on both our dependency targets and our generated files. + self.WriteLn("# Make sure our deps and generated files are built first.") + self.WriteLn( + "LOCAL_ADDITIONAL_DEPENDENCIES := $(GYP_TARGET_DEPENDENCIES) " + "$(GYP_GENERATED_OUTPUTS)" + ) + self.WriteLn() + + # Sources. + if spec.get("sources", []) or extra_sources: + self.WriteSources(spec, configs, extra_sources) + + self.WriteTarget( + spec, configs, deps, link_deps, part_of_all, write_alias_target + ) + + # Update global list of target outputs, used in dependency tracking. + target_outputs[qualified_target] = ("path", self.output_binary) + + # Update global list of link dependencies. + if self.type == "static_library": + target_link_deps[qualified_target] = ("static", self.android_module) + elif self.type == "shared_library": + target_link_deps[qualified_target] = ("shared", self.android_module) + + self.fp.close() + return self.android_module + + def WriteActions(self, actions, extra_sources, extra_outputs): + """Write Makefile code for any 'actions' from the gyp input. extra_sources: a list that will be filled in with newly generated source files, if any @@ -240,457 +260,496 @@ def WriteActions(self, actions, extra_sources, extra_outputs): actions (used to make other pieces dependent on these actions) """ - for action in actions: - name = make.StringToMakefileVariable('%s_%s' % (self.relative_target, - action['action_name'])) - self.WriteLn('### Rules for action "%s":' % action['action_name']) - inputs = action['inputs'] - outputs = action['outputs'] - - # Build up a list of outputs. - # Collect the output dirs we'll need. - dirs = set() - for out in outputs: - if not out.startswith('$'): - print('WARNING: Action for target "%s" writes output to local path ' - '"%s".' % (self.target, out)) - dir = os.path.split(out)[0] - if dir: - dirs.add(dir) - if int(action.get('process_outputs_as_sources', False)): - extra_sources += outputs - - # Prepare the actual command. - command = gyp.common.EncodePOSIXShellList(action['action']) - if 'message' in action: - quiet_cmd = 'Gyp action: %s ($@)' % action['message'] - else: - quiet_cmd = 'Gyp action: %s ($@)' % name - if len(dirs) > 0: - command = 'mkdir -p %s' % ' '.join(dirs) + '; ' + command - - cd_action = 'cd $(gyp_local_path)/%s; ' % self.path - command = cd_action + command - - # The makefile rules are all relative to the top dir, but the gyp actions - # are defined relative to their containing dir. This replaces the gyp_* - # variables for the action rule with an absolute version so that the - # output goes in the right place. - # Only write the gyp_* rules for the "primary" output (:1); - # it's superfluous for the "extra outputs", and this avoids accidentally - # writing duplicate dummy rules for those outputs. - main_output = make.QuoteSpaces(self.LocalPathify(outputs[0])) - self.WriteLn('%s: gyp_local_path := $(LOCAL_PATH)' % main_output) - self.WriteLn('%s: gyp_var_prefix := $(GYP_VAR_PREFIX)' % main_output) - self.WriteLn('%s: gyp_intermediate_dir := ' - '$(abspath $(gyp_intermediate_dir))' % main_output) - self.WriteLn('%s: gyp_shared_intermediate_dir := ' - '$(abspath $(gyp_shared_intermediate_dir))' % main_output) - - # Android's envsetup.sh adds a number of directories to the path including - # the built host binary directory. This causes actions/rules invoked by - # gyp to sometimes use these instead of system versions, e.g. bison. - # The built host binaries may not be suitable, and can cause errors. - # So, we remove them from the PATH using the ANDROID_BUILD_PATHS variable - # set by envsetup. - self.WriteLn('%s: export PATH := $(subst $(ANDROID_BUILD_PATHS),,$(PATH))' - % main_output) - - # Don't allow spaces in input/output filenames, but make an exception for - # filenames which start with '$(' since it's okay for there to be spaces - # inside of make function/macro invocations. - for input in inputs: - if not input.startswith('$(') and ' ' in input: - raise gyp.common.GypError( - 'Action input filename "%s" in target %s contains a space' % - (input, self.target)) - for output in outputs: - if not output.startswith('$(') and ' ' in output: - raise gyp.common.GypError( - 'Action output filename "%s" in target %s contains a space' % - (output, self.target)) - - self.WriteLn('%s: %s $(GYP_TARGET_DEPENDENCIES)' % - (main_output, ' '.join(map(self.LocalPathify, inputs)))) - self.WriteLn('\t@echo "%s"' % quiet_cmd) - self.WriteLn('\t$(hide)%s\n' % command) - for output in outputs[1:]: - # Make each output depend on the main output, with an empty command - # to force make to notice that the mtime has changed. - self.WriteLn('%s: %s ;' % (self.LocalPathify(output), main_output)) - - extra_outputs += outputs - self.WriteLn() - - self.WriteLn() - - - def WriteRules(self, rules, extra_sources, extra_outputs): - """Write Makefile code for any 'rules' from the gyp input. + for action in actions: + name = make.StringToMakefileVariable( + "%s_%s" % (self.relative_target, action["action_name"]) + ) + self.WriteLn('### Rules for action "%s":' % action["action_name"]) + inputs = action["inputs"] + outputs = action["outputs"] + + # Build up a list of outputs. + # Collect the output dirs we'll need. + dirs = set() + for out in outputs: + if not out.startswith("$"): + print( + 'WARNING: Action for target "%s" writes output to local path ' + '"%s".' % (self.target, out) + ) + dir = os.path.split(out)[0] + if dir: + dirs.add(dir) + if int(action.get("process_outputs_as_sources", False)): + extra_sources += outputs + + # Prepare the actual command. + command = gyp.common.EncodePOSIXShellList(action["action"]) + if "message" in action: + quiet_cmd = "Gyp action: %s ($@)" % action["message"] + else: + quiet_cmd = "Gyp action: %s ($@)" % name + if len(dirs) > 0: + command = "mkdir -p %s" % " ".join(dirs) + "; " + command + + cd_action = "cd $(gyp_local_path)/%s; " % self.path + command = cd_action + command + + # The makefile rules are all relative to the top dir, but the gyp actions + # are defined relative to their containing dir. This replaces the gyp_* + # variables for the action rule with an absolute version so that the + # output goes in the right place. + # Only write the gyp_* rules for the "primary" output (:1); + # it's superfluous for the "extra outputs", and this avoids accidentally + # writing duplicate dummy rules for those outputs. + main_output = make.QuoteSpaces(self.LocalPathify(outputs[0])) + self.WriteLn("%s: gyp_local_path := $(LOCAL_PATH)" % main_output) + self.WriteLn("%s: gyp_var_prefix := $(GYP_VAR_PREFIX)" % main_output) + self.WriteLn( + "%s: gyp_intermediate_dir := " + "$(abspath $(gyp_intermediate_dir))" % main_output + ) + self.WriteLn( + "%s: gyp_shared_intermediate_dir := " + "$(abspath $(gyp_shared_intermediate_dir))" % main_output + ) + + # Android's envsetup.sh adds a number of directories to the path including + # the built host binary directory. This causes actions/rules invoked by + # gyp to sometimes use these instead of system versions, e.g. bison. + # The built host binaries may not be suitable, and can cause errors. + # So, we remove them from the PATH using the ANDROID_BUILD_PATHS variable + # set by envsetup. + self.WriteLn( + "%s: export PATH := $(subst $(ANDROID_BUILD_PATHS),,$(PATH))" + % main_output + ) + + # Don't allow spaces in input/output filenames, but make an exception for + # filenames which start with '$(' since it's okay for there to be spaces + # inside of make function/macro invocations. + for input in inputs: + if not input.startswith("$(") and " " in input: + raise gyp.common.GypError( + 'Action input filename "%s" in target %s contains a space' + % (input, self.target) + ) + for output in outputs: + if not output.startswith("$(") and " " in output: + raise gyp.common.GypError( + 'Action output filename "%s" in target %s contains a space' + % (output, self.target) + ) + + self.WriteLn( + "%s: %s $(GYP_TARGET_DEPENDENCIES)" + % (main_output, " ".join(map(self.LocalPathify, inputs))) + ) + self.WriteLn('\t@echo "%s"' % quiet_cmd) + self.WriteLn("\t$(hide)%s\n" % command) + for output in outputs[1:]: + # Make each output depend on the main output, with an empty command + # to force make to notice that the mtime has changed. + self.WriteLn("%s: %s ;" % (self.LocalPathify(output), main_output)) + + extra_outputs += outputs + self.WriteLn() + + self.WriteLn() + + def WriteRules(self, rules, extra_sources, extra_outputs): + """Write Makefile code for any 'rules' from the gyp input. extra_sources: a list that will be filled in with newly generated source files, if any extra_outputs: a list that will be filled in with any outputs of these rules (used to make other pieces dependent on these rules) """ - if len(rules) == 0: - return - - for rule in rules: - if len(rule.get('rule_sources', [])) == 0: - continue - name = make.StringToMakefileVariable('%s_%s' % (self.relative_target, - rule['rule_name'])) - self.WriteLn('\n### Generated for rule "%s":' % name) - self.WriteLn('# "%s":' % rule) - - inputs = rule.get('inputs') - for rule_source in rule.get('rule_sources', []): - (rule_source_dirname, rule_source_basename) = os.path.split(rule_source) - (rule_source_root, rule_source_ext) = \ - os.path.splitext(rule_source_basename) - - outputs = [self.ExpandInputRoot(out, rule_source_root, - rule_source_dirname) - for out in rule['outputs']] - - dirs = set() - for out in outputs: - if not out.startswith('$'): - print('WARNING: Rule for target %s writes output to local path %s' - % (self.target, out)) - dir = os.path.dirname(out) - if dir: - dirs.add(dir) - extra_outputs += outputs - if int(rule.get('process_outputs_as_sources', False)): - extra_sources.extend(outputs) - - components = [] - for component in rule['action']: - component = self.ExpandInputRoot(component, rule_source_root, - rule_source_dirname) - if '$(RULE_SOURCES)' in component: - component = component.replace('$(RULE_SOURCES)', - rule_source) - components.append(component) - - command = gyp.common.EncodePOSIXShellList(components) - cd_action = 'cd $(gyp_local_path)/%s; ' % self.path - command = cd_action + command - if dirs: - command = 'mkdir -p %s' % ' '.join(dirs) + '; ' + command - - # We set up a rule to build the first output, and then set up - # a rule for each additional output to depend on the first. - outputs = map(self.LocalPathify, outputs) - main_output = outputs[0] - self.WriteLn('%s: gyp_local_path := $(LOCAL_PATH)' % main_output) - self.WriteLn('%s: gyp_var_prefix := $(GYP_VAR_PREFIX)' % main_output) - self.WriteLn('%s: gyp_intermediate_dir := ' - '$(abspath $(gyp_intermediate_dir))' % main_output) - self.WriteLn('%s: gyp_shared_intermediate_dir := ' - '$(abspath $(gyp_shared_intermediate_dir))' % main_output) - - # See explanation in WriteActions. - self.WriteLn('%s: export PATH := ' - '$(subst $(ANDROID_BUILD_PATHS),,$(PATH))' % main_output) - - main_output_deps = self.LocalPathify(rule_source) - if inputs: - main_output_deps += ' ' - main_output_deps += ' '.join([self.LocalPathify(f) for f in inputs]) - - self.WriteLn('%s: %s $(GYP_TARGET_DEPENDENCIES)' % - (main_output, main_output_deps)) - self.WriteLn('\t%s\n' % command) - for output in outputs[1:]: - # Make each output depend on the main output, with an empty command - # to force make to notice that the mtime has changed. - self.WriteLn('%s: %s ;' % (output, main_output)) - self.WriteLn() - - self.WriteLn() + if len(rules) == 0: + return + + for rule in rules: + if len(rule.get("rule_sources", [])) == 0: + continue + name = make.StringToMakefileVariable( + "%s_%s" % (self.relative_target, rule["rule_name"]) + ) + self.WriteLn('\n### Generated for rule "%s":' % name) + self.WriteLn('# "%s":' % rule) + + inputs = rule.get("inputs") + for rule_source in rule.get("rule_sources", []): + (rule_source_dirname, rule_source_basename) = os.path.split(rule_source) + (rule_source_root, rule_source_ext) = os.path.splitext( + rule_source_basename + ) + + outputs = [ + self.ExpandInputRoot(out, rule_source_root, rule_source_dirname) + for out in rule["outputs"] + ] + + dirs = set() + for out in outputs: + if not out.startswith("$"): + print( + "WARNING: Rule for target %s writes output to local path %s" + % (self.target, out) + ) + dir = os.path.dirname(out) + if dir: + dirs.add(dir) + extra_outputs += outputs + if int(rule.get("process_outputs_as_sources", False)): + extra_sources.extend(outputs) + + components = [] + for component in rule["action"]: + component = self.ExpandInputRoot( + component, rule_source_root, rule_source_dirname + ) + if "$(RULE_SOURCES)" in component: + component = component.replace("$(RULE_SOURCES)", rule_source) + components.append(component) + + command = gyp.common.EncodePOSIXShellList(components) + cd_action = "cd $(gyp_local_path)/%s; " % self.path + command = cd_action + command + if dirs: + command = "mkdir -p %s" % " ".join(dirs) + "; " + command + + # We set up a rule to build the first output, and then set up + # a rule for each additional output to depend on the first. + outputs = map(self.LocalPathify, outputs) + main_output = outputs[0] + self.WriteLn("%s: gyp_local_path := $(LOCAL_PATH)" % main_output) + self.WriteLn("%s: gyp_var_prefix := $(GYP_VAR_PREFIX)" % main_output) + self.WriteLn( + "%s: gyp_intermediate_dir := " + "$(abspath $(gyp_intermediate_dir))" % main_output + ) + self.WriteLn( + "%s: gyp_shared_intermediate_dir := " + "$(abspath $(gyp_shared_intermediate_dir))" % main_output + ) + + # See explanation in WriteActions. + self.WriteLn( + "%s: export PATH := " + "$(subst $(ANDROID_BUILD_PATHS),,$(PATH))" % main_output + ) + + main_output_deps = self.LocalPathify(rule_source) + if inputs: + main_output_deps += " " + main_output_deps += " ".join([self.LocalPathify(f) for f in inputs]) + + self.WriteLn( + "%s: %s $(GYP_TARGET_DEPENDENCIES)" + % (main_output, main_output_deps) + ) + self.WriteLn("\t%s\n" % command) + for output in outputs[1:]: + # Make each output depend on the main output, with an empty command + # to force make to notice that the mtime has changed. + self.WriteLn("%s: %s ;" % (output, main_output)) + self.WriteLn() + self.WriteLn() - def WriteCopies(self, copies, extra_outputs): - """Write Makefile code for any 'copies' from the gyp input. + def WriteCopies(self, copies, extra_outputs): + """Write Makefile code for any 'copies' from the gyp input. extra_outputs: a list that will be filled in with any outputs of this action (used to make other pieces dependent on this action) """ - self.WriteLn('### Generated for copy rule.') - - variable = make.StringToMakefileVariable(self.relative_target + '_copies') - outputs = [] - for copy in copies: - for path in copy['files']: - # The Android build system does not allow generation of files into the - # source tree. The destination should start with a variable, which will - # typically be $(gyp_intermediate_dir) or - # $(gyp_shared_intermediate_dir). Note that we can't use an assertion - # because some of the gyp tests depend on this. - if not copy['destination'].startswith('$'): - print('WARNING: Copy rule for target %s writes output to ' - 'local path %s' % (self.target, copy['destination'])) - - # LocalPathify() calls normpath, stripping trailing slashes. - path = Sourceify(self.LocalPathify(path)) - filename = os.path.split(path)[1] - output = Sourceify(self.LocalPathify(os.path.join(copy['destination'], - filename))) - - self.WriteLn('%s: %s $(GYP_TARGET_DEPENDENCIES) | $(ACP)' % - (output, path)) - self.WriteLn('\t@echo Copying: $@') - self.WriteLn('\t$(hide) mkdir -p $(dir $@)') - self.WriteLn('\t$(hide) $(ACP) -rpf $< $@') + self.WriteLn("### Generated for copy rule.") + + variable = make.StringToMakefileVariable(self.relative_target + "_copies") + outputs = [] + for copy in copies: + for path in copy["files"]: + # The Android build system does not allow generation of files into the + # source tree. The destination should start with a variable, which will + # typically be $(gyp_intermediate_dir) or + # $(gyp_shared_intermediate_dir). Note that we can't use an assertion + # because some of the gyp tests depend on this. + if not copy["destination"].startswith("$"): + print( + "WARNING: Copy rule for target %s writes output to " + "local path %s" % (self.target, copy["destination"]) + ) + + # LocalPathify() calls normpath, stripping trailing slashes. + path = Sourceify(self.LocalPathify(path)) + filename = os.path.split(path)[1] + output = Sourceify( + self.LocalPathify(os.path.join(copy["destination"], filename)) + ) + + self.WriteLn( + "%s: %s $(GYP_TARGET_DEPENDENCIES) | $(ACP)" % (output, path) + ) + self.WriteLn("\t@echo Copying: $@") + self.WriteLn("\t$(hide) mkdir -p $(dir $@)") + self.WriteLn("\t$(hide) $(ACP) -rpf $< $@") + self.WriteLn() + outputs.append(output) + self.WriteLn("%s = %s" % (variable, " ".join(map(make.QuoteSpaces, outputs)))) + extra_outputs.append("$(%s)" % variable) self.WriteLn() - outputs.append(output) - self.WriteLn('%s = %s' % (variable, - ' '.join(map(make.QuoteSpaces, outputs)))) - extra_outputs.append('$(%s)' % variable) - self.WriteLn() - - def WriteSourceFlags(self, spec, configs): - """Write out the flags and include paths used to compile source files for + def WriteSourceFlags(self, spec, configs): + """Write out the flags and include paths used to compile source files for the current target. Args: spec, configs: input from gyp. """ - for configname, config in sorted(configs.items()): - extracted_includes = [] - - self.WriteLn('\n# Flags passed to both C and C++ files.') - cflags, includes_from_cflags = self.ExtractIncludesFromCFlags( - config.get('cflags', []) + config.get('cflags_c', [])) - extracted_includes.extend(includes_from_cflags) - self.WriteList(cflags, 'MY_CFLAGS_%s' % configname) - - self.WriteList(config.get('defines'), 'MY_DEFS_%s' % configname, - prefix='-D', quoter=make.EscapeCppDefine) - - self.WriteLn('\n# Include paths placed before CFLAGS/CPPFLAGS') - includes = list(config.get('include_dirs', [])) - includes.extend(extracted_includes) - includes = map(Sourceify, map(self.LocalPathify, includes)) - includes = self.NormalizeIncludePaths(includes) - self.WriteList(includes, 'LOCAL_C_INCLUDES_%s' % configname) - - self.WriteLn('\n# Flags passed to only C++ (and not C) files.') - self.WriteList(config.get('cflags_cc'), 'LOCAL_CPPFLAGS_%s' % configname) - - self.WriteLn('\nLOCAL_CFLAGS := $(MY_CFLAGS_$(GYP_CONFIGURATION)) ' - '$(MY_DEFS_$(GYP_CONFIGURATION))') - # Undefine ANDROID for host modules - # TODO: the source code should not use macro ANDROID to tell if it's host - # or target module. - if self.toolset == 'host': - self.WriteLn('# Undefine ANDROID for host modules') - self.WriteLn('LOCAL_CFLAGS += -UANDROID') - self.WriteLn('LOCAL_C_INCLUDES := $(GYP_COPIED_SOURCE_ORIGIN_DIRS) ' - '$(LOCAL_C_INCLUDES_$(GYP_CONFIGURATION))') - self.WriteLn('LOCAL_CPPFLAGS := $(LOCAL_CPPFLAGS_$(GYP_CONFIGURATION))') - # Android uses separate flags for assembly file invocations, but gyp expects - # the same CFLAGS to be applied: - self.WriteLn('LOCAL_ASFLAGS := $(LOCAL_CFLAGS)') - - - def WriteSources(self, spec, configs, extra_sources): - """Write Makefile code for any 'sources' from the gyp input. + for configname, config in sorted(configs.items()): + extracted_includes = [] + + self.WriteLn("\n# Flags passed to both C and C++ files.") + cflags, includes_from_cflags = self.ExtractIncludesFromCFlags( + config.get("cflags", []) + config.get("cflags_c", []) + ) + extracted_includes.extend(includes_from_cflags) + self.WriteList(cflags, "MY_CFLAGS_%s" % configname) + + self.WriteList( + config.get("defines"), + "MY_DEFS_%s" % configname, + prefix="-D", + quoter=make.EscapeCppDefine, + ) + + self.WriteLn("\n# Include paths placed before CFLAGS/CPPFLAGS") + includes = list(config.get("include_dirs", [])) + includes.extend(extracted_includes) + includes = map(Sourceify, map(self.LocalPathify, includes)) + includes = self.NormalizeIncludePaths(includes) + self.WriteList(includes, "LOCAL_C_INCLUDES_%s" % configname) + + self.WriteLn("\n# Flags passed to only C++ (and not C) files.") + self.WriteList(config.get("cflags_cc"), "LOCAL_CPPFLAGS_%s" % configname) + + self.WriteLn( + "\nLOCAL_CFLAGS := $(MY_CFLAGS_$(GYP_CONFIGURATION)) " + "$(MY_DEFS_$(GYP_CONFIGURATION))" + ) + # Undefine ANDROID for host modules + # TODO: the source code should not use macro ANDROID to tell if it's host + # or target module. + if self.toolset == "host": + self.WriteLn("# Undefine ANDROID for host modules") + self.WriteLn("LOCAL_CFLAGS += -UANDROID") + self.WriteLn( + "LOCAL_C_INCLUDES := $(GYP_COPIED_SOURCE_ORIGIN_DIRS) " + "$(LOCAL_C_INCLUDES_$(GYP_CONFIGURATION))" + ) + self.WriteLn("LOCAL_CPPFLAGS := $(LOCAL_CPPFLAGS_$(GYP_CONFIGURATION))") + # Android uses separate flags for assembly file invocations, but gyp expects + # the same CFLAGS to be applied: + self.WriteLn("LOCAL_ASFLAGS := $(LOCAL_CFLAGS)") + + def WriteSources(self, spec, configs, extra_sources): + """Write Makefile code for any 'sources' from the gyp input. These are source files necessary to build the current target. We need to handle shared_intermediate directory source files as a special case by copying them to the intermediate directory and - treating them as a genereated sources. Otherwise the Android build + treating them as a generated sources. Otherwise the Android build rules won't pick them up. Args: spec, configs: input from gyp. extra_sources: Sources generated from Actions or Rules. """ - sources = filter(make.Compilable, spec.get('sources', [])) - generated_not_sources = [x for x in extra_sources if not make.Compilable(x)] - extra_sources = filter(make.Compilable, extra_sources) - - # Determine and output the C++ extension used by these sources. - # We simply find the first C++ file and use that extension. - all_sources = sources + extra_sources - local_cpp_extension = '.cpp' - for source in all_sources: - (root, ext) = os.path.splitext(source) - if IsCPPExtension(ext): - local_cpp_extension = ext - break - if local_cpp_extension != '.cpp': - self.WriteLn('LOCAL_CPP_EXTENSION := %s' % local_cpp_extension) - - # We need to move any non-generated sources that are coming from the - # shared intermediate directory out of LOCAL_SRC_FILES and put them - # into LOCAL_GENERATED_SOURCES. We also need to move over any C++ files - # that don't match our local_cpp_extension, since Android will only - # generate Makefile rules for a single LOCAL_CPP_EXTENSION. - local_files = [] - for source in sources: - (root, ext) = os.path.splitext(source) - if '$(gyp_shared_intermediate_dir)' in source: - extra_sources.append(source) - elif '$(gyp_intermediate_dir)' in source: - extra_sources.append(source) - elif IsCPPExtension(ext) and ext != local_cpp_extension: - extra_sources.append(source) - else: - local_files.append(os.path.normpath(os.path.join(self.path, source))) - - # For any generated source, if it is coming from the shared intermediate - # directory then we add a Make rule to copy them to the local intermediate - # directory first. This is because the Android LOCAL_GENERATED_SOURCES - # must be in the local module intermediate directory for the compile rules - # to work properly. If the file has the wrong C++ extension, then we add - # a rule to copy that to intermediates and use the new version. - final_generated_sources = [] - # If a source file gets copied, we still need to add the original source - # directory as header search path, for GCC searches headers in the - # directory that contains the source file by default. - origin_src_dirs = [] - for source in extra_sources: - local_file = source - if not '$(gyp_intermediate_dir)/' in local_file: - basename = os.path.basename(local_file) - local_file = '$(gyp_intermediate_dir)/' + basename - (root, ext) = os.path.splitext(local_file) - if IsCPPExtension(ext) and ext != local_cpp_extension: - local_file = root + local_cpp_extension - if local_file != source: - self.WriteLn('%s: %s' % (local_file, self.LocalPathify(source))) - self.WriteLn('\tmkdir -p $(@D); cp $< $@') - origin_src_dirs.append(os.path.dirname(source)) - final_generated_sources.append(local_file) - - # We add back in all of the non-compilable stuff to make sure that the - # make rules have dependencies on them. - final_generated_sources.extend(generated_not_sources) - self.WriteList(final_generated_sources, 'LOCAL_GENERATED_SOURCES') - - origin_src_dirs = gyp.common.uniquer(origin_src_dirs) - origin_src_dirs = map(Sourceify, map(self.LocalPathify, origin_src_dirs)) - self.WriteList(origin_src_dirs, 'GYP_COPIED_SOURCE_ORIGIN_DIRS') - - self.WriteList(local_files, 'LOCAL_SRC_FILES') - - # Write out the flags used to compile the source; this must be done last - # so that GYP_COPIED_SOURCE_ORIGIN_DIRS can be used as an include path. - self.WriteSourceFlags(spec, configs) - - - def ComputeAndroidModule(self, spec): - """Return the Android module name used for a gyp spec. + sources = filter(make.Compilable, spec.get("sources", [])) + generated_not_sources = [x for x in extra_sources if not make.Compilable(x)] + extra_sources = filter(make.Compilable, extra_sources) + + # Determine and output the C++ extension used by these sources. + # We simply find the first C++ file and use that extension. + all_sources = sources + extra_sources + local_cpp_extension = ".cpp" + for source in all_sources: + (root, ext) = os.path.splitext(source) + if IsCPPExtension(ext): + local_cpp_extension = ext + break + if local_cpp_extension != ".cpp": + self.WriteLn("LOCAL_CPP_EXTENSION := %s" % local_cpp_extension) + + # We need to move any non-generated sources that are coming from the + # shared intermediate directory out of LOCAL_SRC_FILES and put them + # into LOCAL_GENERATED_SOURCES. We also need to move over any C++ files + # that don't match our local_cpp_extension, since Android will only + # generate Makefile rules for a single LOCAL_CPP_EXTENSION. + local_files = [] + for source in sources: + (root, ext) = os.path.splitext(source) + if "$(gyp_shared_intermediate_dir)" in source: + extra_sources.append(source) + elif "$(gyp_intermediate_dir)" in source: + extra_sources.append(source) + elif IsCPPExtension(ext) and ext != local_cpp_extension: + extra_sources.append(source) + else: + local_files.append(os.path.normpath(os.path.join(self.path, source))) + + # For any generated source, if it is coming from the shared intermediate + # directory then we add a Make rule to copy them to the local intermediate + # directory first. This is because the Android LOCAL_GENERATED_SOURCES + # must be in the local module intermediate directory for the compile rules + # to work properly. If the file has the wrong C++ extension, then we add + # a rule to copy that to intermediates and use the new version. + final_generated_sources = [] + # If a source file gets copied, we still need to add the original source + # directory as header search path, for GCC searches headers in the + # directory that contains the source file by default. + origin_src_dirs = [] + for source in extra_sources: + local_file = source + if "$(gyp_intermediate_dir)/" not in local_file: + basename = os.path.basename(local_file) + local_file = "$(gyp_intermediate_dir)/" + basename + (root, ext) = os.path.splitext(local_file) + if IsCPPExtension(ext) and ext != local_cpp_extension: + local_file = root + local_cpp_extension + if local_file != source: + self.WriteLn("%s: %s" % (local_file, self.LocalPathify(source))) + self.WriteLn("\tmkdir -p $(@D); cp $< $@") + origin_src_dirs.append(os.path.dirname(source)) + final_generated_sources.append(local_file) + + # We add back in all of the non-compilable stuff to make sure that the + # make rules have dependencies on them. + final_generated_sources.extend(generated_not_sources) + self.WriteList(final_generated_sources, "LOCAL_GENERATED_SOURCES") + + origin_src_dirs = gyp.common.uniquer(origin_src_dirs) + origin_src_dirs = map(Sourceify, map(self.LocalPathify, origin_src_dirs)) + self.WriteList(origin_src_dirs, "GYP_COPIED_SOURCE_ORIGIN_DIRS") + + self.WriteList(local_files, "LOCAL_SRC_FILES") + + # Write out the flags used to compile the source; this must be done last + # so that GYP_COPIED_SOURCE_ORIGIN_DIRS can be used as an include path. + self.WriteSourceFlags(spec, configs) + + def ComputeAndroidModule(self, spec): + """Return the Android module name used for a gyp spec. We use the complete qualified target name to avoid collisions between duplicate targets in different directories. We also add a suffix to distinguish gyp-generated module names. """ - if int(spec.get('android_unmangled_name', 0)): - assert self.type != 'shared_library' or self.target.startswith('lib') - return self.target - - if self.type == 'shared_library': - # For reasons of convention, the Android build system requires that all - # shared library modules are named 'libfoo' when generating -l flags. - prefix = 'lib_' - else: - prefix = '' + if int(spec.get("android_unmangled_name", 0)): + assert self.type != "shared_library" or self.target.startswith("lib") + return self.target - if spec['toolset'] == 'host': - suffix = '_$(TARGET_$(GYP_VAR_PREFIX)ARCH)_host_gyp' - else: - suffix = '_gyp' + if self.type == "shared_library": + # For reasons of convention, the Android build system requires that all + # shared library modules are named 'libfoo' when generating -l flags. + prefix = "lib_" + else: + prefix = "" - if self.path: - middle = make.StringToMakefileVariable('%s_%s' % (self.path, self.target)) - else: - middle = make.StringToMakefileVariable(self.target) + if spec["toolset"] == "host": + suffix = "_$(TARGET_$(GYP_VAR_PREFIX)ARCH)_host_gyp" + else: + suffix = "_gyp" - return ''.join([prefix, middle, suffix]) + if self.path: + middle = make.StringToMakefileVariable("%s_%s" % (self.path, self.target)) + else: + middle = make.StringToMakefileVariable(self.target) + return "".join([prefix, middle, suffix]) - def ComputeOutputParts(self, spec): - """Return the 'output basename' of a gyp spec, split into filename + ext. + def ComputeOutputParts(self, spec): + """Return the 'output basename' of a gyp spec, split into filename + ext. Android libraries must be named the same thing as their module name, otherwise the linker can't find them, so product_name and so on must be ignored if we are building a library, and the "lib" prepending is not done for Android. """ - assert self.type != 'loadable_module' # TODO: not supported? - - target = spec['target_name'] - target_prefix = '' - target_ext = '' - if self.type == 'static_library': - target = self.ComputeAndroidModule(spec) - target_ext = '.a' - elif self.type == 'shared_library': - target = self.ComputeAndroidModule(spec) - target_ext = '.so' - elif self.type == 'none': - target_ext = '.stamp' - elif self.type != 'executable': - print("ERROR: What output file should be generated?", - "type", self.type, "target", target) - - if self.type != 'static_library' and self.type != 'shared_library': - target_prefix = spec.get('product_prefix', target_prefix) - target = spec.get('product_name', target) - product_ext = spec.get('product_extension') - if product_ext: - target_ext = '.' + product_ext - - target_stem = target_prefix + target - return (target_stem, target_ext) - - - def ComputeOutputBasename(self, spec): - """Return the 'output basename' of a gyp spec. + assert self.type != "loadable_module" # TODO: not supported? + + target = spec["target_name"] + target_prefix = "" + target_ext = "" + if self.type == "static_library": + target = self.ComputeAndroidModule(spec) + target_ext = ".a" + elif self.type == "shared_library": + target = self.ComputeAndroidModule(spec) + target_ext = ".so" + elif self.type == "none": + target_ext = ".stamp" + elif self.type != "executable": + print( + "ERROR: What output file should be generated?", + "type", + self.type, + "target", + target, + ) + + if self.type != "static_library" and self.type != "shared_library": + target_prefix = spec.get("product_prefix", target_prefix) + target = spec.get("product_name", target) + product_ext = spec.get("product_extension") + if product_ext: + target_ext = "." + product_ext + + target_stem = target_prefix + target + return (target_stem, target_ext) + + def ComputeOutputBasename(self, spec): + """Return the 'output basename' of a gyp spec. E.g., the loadable module 'foobar' in directory 'baz' will produce 'libfoobar.so' """ - return ''.join(self.ComputeOutputParts(spec)) - + return "".join(self.ComputeOutputParts(spec)) - def ComputeOutput(self, spec): - """Return the 'output' (full output path) of a gyp spec. + def ComputeOutput(self, spec): + """Return the 'output' (full output path) of a gyp spec. E.g., the loadable module 'foobar' in directory 'baz' will produce '$(obj)/baz/libfoobar.so' """ - if self.type == 'executable': - # We install host executables into shared_intermediate_dir so they can be - # run by gyp rules that refer to PRODUCT_DIR. - path = '$(gyp_shared_intermediate_dir)' - elif self.type == 'shared_library': - if self.toolset == 'host': - path = '$($(GYP_HOST_VAR_PREFIX)HOST_OUT_INTERMEDIATE_LIBRARIES)' - else: - path = '$($(GYP_VAR_PREFIX)TARGET_OUT_INTERMEDIATE_LIBRARIES)' - else: - # Other targets just get built into their intermediate dir. - if self.toolset == 'host': - path = ('$(call intermediates-dir-for,%s,%s,true,,' - '$(GYP_HOST_VAR_PREFIX))' % (self.android_class, - self.android_module)) - else: - path = ('$(call intermediates-dir-for,%s,%s,,,$(GYP_VAR_PREFIX))' - % (self.android_class, self.android_module)) - - assert spec.get('product_dir') is None # TODO: not supported? - return os.path.join(path, self.ComputeOutputBasename(spec)) - - def NormalizeIncludePaths(self, include_paths): - """ Normalize include_paths. + if self.type == "executable": + # We install host executables into shared_intermediate_dir so they can be + # run by gyp rules that refer to PRODUCT_DIR. + path = "$(gyp_shared_intermediate_dir)" + elif self.type == "shared_library": + if self.toolset == "host": + path = "$($(GYP_HOST_VAR_PREFIX)HOST_OUT_INTERMEDIATE_LIBRARIES)" + else: + path = "$($(GYP_VAR_PREFIX)TARGET_OUT_INTERMEDIATE_LIBRARIES)" + else: + # Other targets just get built into their intermediate dir. + if self.toolset == "host": + path = ( + "$(call intermediates-dir-for,%s,%s,true,," + "$(GYP_HOST_VAR_PREFIX))" + % (self.android_class, self.android_module) + ) + else: + path = "$(call intermediates-dir-for,%s,%s,,,$(GYP_VAR_PREFIX))" % ( + self.android_class, + self.android_module, + ) + + assert spec.get("product_dir") is None # TODO: not supported? + return os.path.join(path, self.ComputeOutputBasename(spec)) + + def NormalizeIncludePaths(self, include_paths): + """ Normalize include_paths. Convert absolute paths to relative to the Android top directory. Args: @@ -698,33 +757,33 @@ def NormalizeIncludePaths(self, include_paths): Returns: A list of normalized include paths. """ - normalized = [] - for path in include_paths: - if path[0] == '/': - path = gyp.common.RelativePath(path, self.android_top_dir) - normalized.append(path) - return normalized + normalized = [] + for path in include_paths: + if path[0] == "/": + path = gyp.common.RelativePath(path, self.android_top_dir) + normalized.append(path) + return normalized - def ExtractIncludesFromCFlags(self, cflags): - """Extract includes "-I..." out from cflags + def ExtractIncludesFromCFlags(self, cflags): + """Extract includes "-I..." out from cflags Args: cflags: A list of compiler flags, which may be mixed with "-I.." Returns: A tuple of lists: (clean_clfags, include_paths). "-I.." is trimmed. """ - clean_cflags = [] - include_paths = [] - for flag in cflags: - if flag.startswith('-I'): - include_paths.append(flag[2:]) - else: - clean_cflags.append(flag) + clean_cflags = [] + include_paths = [] + for flag in cflags: + if flag.startswith("-I"): + include_paths.append(flag[2:]) + else: + clean_cflags.append(flag) - return (clean_cflags, include_paths) + return (clean_cflags, include_paths) - def FilterLibraries(self, libraries): - """Filter the 'libraries' key to separate things that shouldn't be ldflags. + def FilterLibraries(self, libraries): + """Filter the 'libraries' key to separate things that shouldn't be ldflags. Library entries that look like filenames should be converted to android module names instead of being passed to the linker as flags. @@ -734,96 +793,103 @@ def FilterLibraries(self, libraries): Returns: A tuple (static_lib_modules, dynamic_lib_modules, ldflags) """ - static_lib_modules = [] - dynamic_lib_modules = [] - ldflags = [] - for libs in libraries: - # Libs can have multiple words. - for lib in libs.split(): - # Filter the system libraries, which are added by default by the Android - # build system. - if (lib == '-lc' or lib == '-lstdc++' or lib == '-lm' or - lib.endswith('libgcc.a')): - continue - match = re.search(r'([^/]+)\.a$', lib) - if match: - static_lib_modules.append(match.group(1)) - continue - match = re.search(r'([^/]+)\.so$', lib) - if match: - dynamic_lib_modules.append(match.group(1)) - continue - if lib.startswith('-l'): - ldflags.append(lib) - return (static_lib_modules, dynamic_lib_modules, ldflags) - - - def ComputeDeps(self, spec): - """Compute the dependencies of a gyp spec. + static_lib_modules = [] + dynamic_lib_modules = [] + ldflags = [] + for libs in libraries: + # Libs can have multiple words. + for lib in libs.split(): + # Filter the system libraries, which are added by default by the Android + # build system. + if ( + lib == "-lc" + or lib == "-lstdc++" + or lib == "-lm" + or lib.endswith("libgcc.a") + ): + continue + match = re.search(r"([^/]+)\.a$", lib) + if match: + static_lib_modules.append(match.group(1)) + continue + match = re.search(r"([^/]+)\.so$", lib) + if match: + dynamic_lib_modules.append(match.group(1)) + continue + if lib.startswith("-l"): + ldflags.append(lib) + return (static_lib_modules, dynamic_lib_modules, ldflags) + + def ComputeDeps(self, spec): + """Compute the dependencies of a gyp spec. Returns a tuple (deps, link_deps), where each is a list of filenames that will need to be put in front of make for either building (deps) or linking (link_deps). """ - deps = [] - link_deps = [] - if 'dependencies' in spec: - deps.extend([target_outputs[dep] for dep in spec['dependencies'] - if target_outputs[dep]]) - for dep in spec['dependencies']: - if dep in target_link_deps: - link_deps.append(target_link_deps[dep]) - deps.extend(link_deps) - return (gyp.common.uniquer(deps), gyp.common.uniquer(link_deps)) - - - def WriteTargetFlags(self, spec, configs, link_deps): - """Write Makefile code to specify the link flags and library dependencies. + deps = [] + link_deps = [] + if "dependencies" in spec: + deps.extend( + [ + target_outputs[dep] + for dep in spec["dependencies"] + if target_outputs[dep] + ] + ) + for dep in spec["dependencies"]: + if dep in target_link_deps: + link_deps.append(target_link_deps[dep]) + deps.extend(link_deps) + return (gyp.common.uniquer(deps), gyp.common.uniquer(link_deps)) + + def WriteTargetFlags(self, spec, configs, link_deps): + """Write Makefile code to specify the link flags and library dependencies. spec, configs: input from gyp. link_deps: link dependency list; see ComputeDeps() """ - # Libraries (i.e. -lfoo) - # These must be included even for static libraries as some of them provide - # implicit include paths through the build system. - libraries = gyp.common.uniquer(spec.get('libraries', [])) - static_libs, dynamic_libs, ldflags_libs = self.FilterLibraries(libraries) - - if self.type != 'static_library': - for configname, config in sorted(configs.items()): - ldflags = list(config.get('ldflags', [])) - self.WriteLn('') - self.WriteList(ldflags, 'LOCAL_LDFLAGS_%s' % configname) - self.WriteList(ldflags_libs, 'LOCAL_GYP_LIBS') - self.WriteLn('LOCAL_LDFLAGS := $(LOCAL_LDFLAGS_$(GYP_CONFIGURATION)) ' - '$(LOCAL_GYP_LIBS)') - - # Link dependencies (i.e. other gyp targets this target depends on) - # These need not be included for static libraries as within the gyp build - # we do not use the implicit include path mechanism. - if self.type != 'static_library': - static_link_deps = [x[1] for x in link_deps if x[0] == 'static'] - shared_link_deps = [x[1] for x in link_deps if x[0] == 'shared'] - else: - static_link_deps = [] - shared_link_deps = [] - - # Only write the lists if they are non-empty. - if static_libs or static_link_deps: - self.WriteLn('') - self.WriteList(static_libs + static_link_deps, - 'LOCAL_STATIC_LIBRARIES') - self.WriteLn('# Enable grouping to fix circular references') - self.WriteLn('LOCAL_GROUP_STATIC_LIBRARIES := true') - if dynamic_libs or shared_link_deps: - self.WriteLn('') - self.WriteList(dynamic_libs + shared_link_deps, - 'LOCAL_SHARED_LIBRARIES') - - - def WriteTarget(self, spec, configs, deps, link_deps, part_of_all, - write_alias_target): - """Write Makefile code to produce the final target of the gyp spec. + # Libraries (i.e. -lfoo) + # These must be included even for static libraries as some of them provide + # implicit include paths through the build system. + libraries = gyp.common.uniquer(spec.get("libraries", [])) + static_libs, dynamic_libs, ldflags_libs = self.FilterLibraries(libraries) + + if self.type != "static_library": + for configname, config in sorted(configs.items()): + ldflags = list(config.get("ldflags", [])) + self.WriteLn("") + self.WriteList(ldflags, "LOCAL_LDFLAGS_%s" % configname) + self.WriteList(ldflags_libs, "LOCAL_GYP_LIBS") + self.WriteLn( + "LOCAL_LDFLAGS := $(LOCAL_LDFLAGS_$(GYP_CONFIGURATION)) " + "$(LOCAL_GYP_LIBS)" + ) + + # Link dependencies (i.e. other gyp targets this target depends on) + # These need not be included for static libraries as within the gyp build + # we do not use the implicit include path mechanism. + if self.type != "static_library": + static_link_deps = [x[1] for x in link_deps if x[0] == "static"] + shared_link_deps = [x[1] for x in link_deps if x[0] == "shared"] + else: + static_link_deps = [] + shared_link_deps = [] + + # Only write the lists if they are non-empty. + if static_libs or static_link_deps: + self.WriteLn("") + self.WriteList(static_libs + static_link_deps, "LOCAL_STATIC_LIBRARIES") + self.WriteLn("# Enable grouping to fix circular references") + self.WriteLn("LOCAL_GROUP_STATIC_LIBRARIES := true") + if dynamic_libs or shared_link_deps: + self.WriteLn("") + self.WriteList(dynamic_libs + shared_link_deps, "LOCAL_SHARED_LIBRARIES") + + def WriteTarget( + self, spec, configs, deps, link_deps, part_of_all, write_alias_target + ): + """Write Makefile code to produce the final target of the gyp spec. spec, configs: input from gyp. deps, link_deps: dependency lists; see ComputeDeps() @@ -831,267 +897,278 @@ def WriteTarget(self, spec, configs, deps, link_deps, part_of_all, write_alias_target: flag indicating whether to create short aliases for this target """ - self.WriteLn('### Rules for final target.') - - if self.type != 'none': - self.WriteTargetFlags(spec, configs, link_deps) - - settings = spec.get('aosp_build_settings', {}) - if settings: - self.WriteLn('### Set directly by aosp_build_settings.') - for k, v in settings.items(): - if isinstance(v, list): - self.WriteList(v, k) + self.WriteLn("### Rules for final target.") + + if self.type != "none": + self.WriteTargetFlags(spec, configs, link_deps) + + settings = spec.get("aosp_build_settings", {}) + if settings: + self.WriteLn("### Set directly by aosp_build_settings.") + for k, v in settings.items(): + if isinstance(v, list): + self.WriteList(v, k) + else: + self.WriteLn("%s := %s" % (k, make.QuoteIfNecessary(v))) + self.WriteLn("") + + # Add to the set of targets which represent the gyp 'all' target. We use the + # name 'gyp_all_modules' as the Android build system doesn't allow the use + # of the Make target 'all' and because 'all_modules' is the equivalent of + # the Make target 'all' on Android. + if part_of_all and write_alias_target: + self.WriteLn('# Add target alias to "gyp_all_modules" target.') + self.WriteLn(".PHONY: gyp_all_modules") + self.WriteLn("gyp_all_modules: %s" % self.android_module) + self.WriteLn("") + + # Add an alias from the gyp target name to the Android module name. This + # simplifies manual builds of the target, and is required by the test + # framework. + if self.target != self.android_module and write_alias_target: + self.WriteLn("# Alias gyp target name.") + self.WriteLn(".PHONY: %s" % self.target) + self.WriteLn("%s: %s" % (self.target, self.android_module)) + self.WriteLn("") + + # Add the command to trigger build of the target type depending + # on the toolset. Ex: BUILD_STATIC_LIBRARY vs. BUILD_HOST_STATIC_LIBRARY + # NOTE: This has to come last! + modifier = "" + if self.toolset == "host": + modifier = "HOST_" + if self.type == "static_library": + self.WriteLn("include $(BUILD_%sSTATIC_LIBRARY)" % modifier) + elif self.type == "shared_library": + self.WriteLn("LOCAL_PRELINK_MODULE := false") + self.WriteLn("include $(BUILD_%sSHARED_LIBRARY)" % modifier) + elif self.type == "executable": + self.WriteLn("LOCAL_CXX_STL := libc++_static") + # Executables are for build and test purposes only, so they're installed + # to a directory that doesn't get included in the system image. + self.WriteLn("LOCAL_MODULE_PATH := $(gyp_shared_intermediate_dir)") + self.WriteLn("include $(BUILD_%sEXECUTABLE)" % modifier) else: - self.WriteLn('%s := %s' % (k, make.QuoteIfNecessary(v))) - self.WriteLn('') - - # Add to the set of targets which represent the gyp 'all' target. We use the - # name 'gyp_all_modules' as the Android build system doesn't allow the use - # of the Make target 'all' and because 'all_modules' is the equivalent of - # the Make target 'all' on Android. - if part_of_all and write_alias_target: - self.WriteLn('# Add target alias to "gyp_all_modules" target.') - self.WriteLn('.PHONY: gyp_all_modules') - self.WriteLn('gyp_all_modules: %s' % self.android_module) - self.WriteLn('') - - # Add an alias from the gyp target name to the Android module name. This - # simplifies manual builds of the target, and is required by the test - # framework. - if self.target != self.android_module and write_alias_target: - self.WriteLn('# Alias gyp target name.') - self.WriteLn('.PHONY: %s' % self.target) - self.WriteLn('%s: %s' % (self.target, self.android_module)) - self.WriteLn('') - - # Add the command to trigger build of the target type depending - # on the toolset. Ex: BUILD_STATIC_LIBRARY vs. BUILD_HOST_STATIC_LIBRARY - # NOTE: This has to come last! - modifier = '' - if self.toolset == 'host': - modifier = 'HOST_' - if self.type == 'static_library': - self.WriteLn('include $(BUILD_%sSTATIC_LIBRARY)' % modifier) - elif self.type == 'shared_library': - self.WriteLn('LOCAL_PRELINK_MODULE := false') - self.WriteLn('include $(BUILD_%sSHARED_LIBRARY)' % modifier) - elif self.type == 'executable': - self.WriteLn('LOCAL_CXX_STL := libc++_static') - # Executables are for build and test purposes only, so they're installed - # to a directory that doesn't get included in the system image. - self.WriteLn('LOCAL_MODULE_PATH := $(gyp_shared_intermediate_dir)') - self.WriteLn('include $(BUILD_%sEXECUTABLE)' % modifier) - else: - self.WriteLn('LOCAL_MODULE_PATH := $(PRODUCT_OUT)/gyp_stamp') - self.WriteLn('LOCAL_UNINSTALLABLE_MODULE := true') - if self.toolset == 'target': - self.WriteLn('LOCAL_2ND_ARCH_VAR_PREFIX := $(GYP_VAR_PREFIX)') - else: - self.WriteLn('LOCAL_2ND_ARCH_VAR_PREFIX := $(GYP_HOST_VAR_PREFIX)') - self.WriteLn() - self.WriteLn('include $(BUILD_SYSTEM)/base_rules.mk') - self.WriteLn() - self.WriteLn('$(LOCAL_BUILT_MODULE): $(LOCAL_ADDITIONAL_DEPENDENCIES)') - self.WriteLn('\t$(hide) echo "Gyp timestamp: $@"') - self.WriteLn('\t$(hide) mkdir -p $(dir $@)') - self.WriteLn('\t$(hide) touch $@') - self.WriteLn() - self.WriteLn('LOCAL_2ND_ARCH_VAR_PREFIX :=') - - - def WriteList(self, value_list, variable=None, prefix='', - quoter=make.QuoteIfNecessary, local_pathify=False): - """Write a variable definition that is a list of values. + self.WriteLn("LOCAL_MODULE_PATH := $(PRODUCT_OUT)/gyp_stamp") + self.WriteLn("LOCAL_UNINSTALLABLE_MODULE := true") + if self.toolset == "target": + self.WriteLn("LOCAL_2ND_ARCH_VAR_PREFIX := $(GYP_VAR_PREFIX)") + else: + self.WriteLn("LOCAL_2ND_ARCH_VAR_PREFIX := $(GYP_HOST_VAR_PREFIX)") + self.WriteLn() + self.WriteLn("include $(BUILD_SYSTEM)/base_rules.mk") + self.WriteLn() + self.WriteLn("$(LOCAL_BUILT_MODULE): $(LOCAL_ADDITIONAL_DEPENDENCIES)") + self.WriteLn('\t$(hide) echo "Gyp timestamp: $@"') + self.WriteLn("\t$(hide) mkdir -p $(dir $@)") + self.WriteLn("\t$(hide) touch $@") + self.WriteLn() + self.WriteLn("LOCAL_2ND_ARCH_VAR_PREFIX :=") + + def WriteList( + self, + value_list, + variable=None, + prefix="", + quoter=make.QuoteIfNecessary, + local_pathify=False, + ): + """Write a variable definition that is a list of values. E.g. WriteList(['a','b'], 'foo', prefix='blah') writes out foo = blaha blahb but in a pretty-printed style. """ - values = '' - if value_list: - value_list = [quoter(prefix + l) for l in value_list] - if local_pathify: - value_list = [self.LocalPathify(l) for l in value_list] - values = ' \\\n\t' + ' \\\n\t'.join(value_list) - self.fp.write('%s :=%s\n\n' % (variable, values)) - - - def WriteLn(self, text=''): - self.fp.write(text + '\n') - - - def LocalPathify(self, path): - """Convert a subdirectory-relative path into a normalized path which starts + values = "" + if value_list: + value_list = [quoter(prefix + value) for value in value_list] + if local_pathify: + value_list = [self.LocalPathify(value) for value in value_list] + values = " \\\n\t" + " \\\n\t".join(value_list) + self.fp.write("%s :=%s\n\n" % (variable, values)) + + def WriteLn(self, text=""): + self.fp.write(text + "\n") + + def LocalPathify(self, path): + """Convert a subdirectory-relative path into a normalized path which starts with the make variable $(LOCAL_PATH) (i.e. the top of the project tree). Absolute paths, or paths that contain variables, are just normalized.""" - if '$(' in path or os.path.isabs(path): - # path is not a file in the project tree in this case, but calling - # normpath is still important for trimming trailing slashes. - return os.path.normpath(path) - local_path = os.path.join('$(LOCAL_PATH)', self.path, path) - local_path = os.path.normpath(local_path) - # Check that normalizing the path didn't ../ itself out of $(LOCAL_PATH) - # - i.e. that the resulting path is still inside the project tree. The - # path may legitimately have ended up containing just $(LOCAL_PATH), though, - # so we don't look for a slash. - assert local_path.startswith('$(LOCAL_PATH)'), ( - 'Path %s attempts to escape from gyp path %s !)' % (path, self.path)) - return local_path - - - def ExpandInputRoot(self, template, expansion, dirname): - if '%(INPUT_ROOT)s' not in template and '%(INPUT_DIRNAME)s' not in template: - return template - path = template % { - 'INPUT_ROOT': expansion, - 'INPUT_DIRNAME': dirname, + if "$(" in path or os.path.isabs(path): + # path is not a file in the project tree in this case, but calling + # normpath is still important for trimming trailing slashes. + return os.path.normpath(path) + local_path = os.path.join("$(LOCAL_PATH)", self.path, path) + local_path = os.path.normpath(local_path) + # Check that normalizing the path didn't ../ itself out of $(LOCAL_PATH) + # - i.e. that the resulting path is still inside the project tree. The + # path may legitimately have ended up containing just $(LOCAL_PATH), though, + # so we don't look for a slash. + assert local_path.startswith( + "$(LOCAL_PATH)" + ), "Path %s attempts to escape from gyp path %s !)" % (path, self.path) + return local_path + + def ExpandInputRoot(self, template, expansion, dirname): + if "%(INPUT_ROOT)s" not in template and "%(INPUT_DIRNAME)s" not in template: + return template + path = template % { + "INPUT_ROOT": expansion, + "INPUT_DIRNAME": dirname, } - return os.path.normpath(path) + return os.path.normpath(path) def PerformBuild(data, configurations, params): - # The android backend only supports the default configuration. - options = params['options'] - makefile = os.path.abspath(os.path.join(options.toplevel_dir, - 'GypAndroid.mk')) - env = dict(os.environ) - env['ONE_SHOT_MAKEFILE'] = makefile - arguments = ['make', '-C', os.environ['ANDROID_BUILD_TOP'], 'gyp_all_modules'] - print('Building: %s' % arguments) - subprocess.check_call(arguments, env=env) + # The android backend only supports the default configuration. + options = params["options"] + makefile = os.path.abspath(os.path.join(options.toplevel_dir, "GypAndroid.mk")) + env = dict(os.environ) + env["ONE_SHOT_MAKEFILE"] = makefile + arguments = ["make", "-C", os.environ["ANDROID_BUILD_TOP"], "gyp_all_modules"] + print("Building: %s" % arguments) + subprocess.check_call(arguments, env=env) def GenerateOutput(target_list, target_dicts, data, params): - options = params['options'] - generator_flags = params.get('generator_flags', {}) - builddir_name = generator_flags.get('output_dir', 'out') - limit_to_target_all = generator_flags.get('limit_to_target_all', False) - write_alias_targets = generator_flags.get('write_alias_targets', True) - sdk_version = generator_flags.get('aosp_sdk_version', 0) - android_top_dir = os.environ.get('ANDROID_BUILD_TOP') - assert android_top_dir, '$ANDROID_BUILD_TOP not set; you need to run lunch.' - - def CalculateMakefilePath(build_file, base_name): - """Determine where to write a Makefile for a given gyp file.""" - # Paths in gyp files are relative to the .gyp file, but we want - # paths relative to the source root for the master makefile. Grab - # the path of the .gyp file as the base to relativize against. - # E.g. "foo/bar" when we're constructing targets for "foo/bar/baz.gyp". - base_path = gyp.common.RelativePath(os.path.dirname(build_file), - options.depth) - # We write the file in the base_path directory. - output_file = os.path.join(options.depth, base_path, base_name) - assert not options.generator_output, ( - 'The Android backend does not support options.generator_output.') - base_path = gyp.common.RelativePath(os.path.dirname(build_file), - options.toplevel_dir) - return base_path, output_file - - # TODO: search for the first non-'Default' target. This can go - # away when we add verification that all targets have the - # necessary configurations. - default_configuration = None - toolsets = set([target_dicts[target]['toolset'] for target in target_list]) - for target in target_list: - spec = target_dicts[target] - if spec['default_configuration'] != 'Default': - default_configuration = spec['default_configuration'] - break - if not default_configuration: - default_configuration = 'Default' - - srcdir = '.' - makefile_name = 'GypAndroid' + options.suffix + '.mk' - makefile_path = os.path.join(options.toplevel_dir, makefile_name) - assert not options.generator_output, ( - 'The Android backend does not support options.generator_output.') - gyp.common.EnsureDirExists(makefile_path) - root_makefile = open(makefile_path, 'w') - - root_makefile.write(header) - - # We set LOCAL_PATH just once, here, to the top of the project tree. This - # allows all the other paths we use to be relative to the Android.mk file, - # as the Android build system expects. - root_makefile.write('\nLOCAL_PATH := $(call my-dir)\n') - - # Find the list of targets that derive from the gyp file(s) being built. - needed_targets = set() - for build_file in params['build_files']: - for target in gyp.common.AllTargets(target_list, target_dicts, build_file): - needed_targets.add(target) - - build_files = set() - include_list = set() - android_modules = {} - for qualified_target in target_list: - build_file, target, toolset = gyp.common.ParseQualifiedTarget( - qualified_target) - relative_build_file = gyp.common.RelativePath(build_file, - options.toplevel_dir) - build_files.add(relative_build_file) - included_files = data[build_file]['included_files'] - for included_file in included_files: - # The included_files entries are relative to the dir of the build file - # that included them, so we have to undo that and then make them relative - # to the root dir. - relative_include_file = gyp.common.RelativePath( - gyp.common.UnrelativePath(included_file, build_file), - options.toplevel_dir) - abs_include_file = os.path.abspath(relative_include_file) - # If the include file is from the ~/.gyp dir, we should use absolute path - # so that relocating the src dir doesn't break the path. - if (params['home_dot_gyp'] and - abs_include_file.startswith(params['home_dot_gyp'])): - build_files.add(abs_include_file) - else: - build_files.add(relative_include_file) - - base_path, output_file = CalculateMakefilePath(build_file, - target + '.' + toolset + options.suffix + '.mk') - - spec = target_dicts[qualified_target] - configs = spec['configurations'] - - part_of_all = qualified_target in needed_targets - if limit_to_target_all and not part_of_all: - continue - - relative_target = gyp.common.QualifiedTarget(relative_build_file, target, - toolset) - writer = AndroidMkWriter(android_top_dir) - android_module = writer.Write(qualified_target, relative_target, base_path, - output_file, spec, configs, - part_of_all=part_of_all, - write_alias_target=write_alias_targets, - sdk_version=sdk_version) - if android_module in android_modules: - print('ERROR: Android module names must be unique. The following ' - 'targets both generate Android module name %s.\n %s\n %s' % - (android_module, android_modules[android_module], - qualified_target)) - return - android_modules[android_module] = qualified_target - - # Our root_makefile lives at the source root. Compute the relative path - # from there to the output_file for including. - mkfile_rel_path = gyp.common.RelativePath(output_file, - os.path.dirname(makefile_path)) - include_list.add(mkfile_rel_path) - - root_makefile.write('GYP_CONFIGURATION ?= %s\n' % default_configuration) - root_makefile.write('GYP_VAR_PREFIX ?=\n') - root_makefile.write('GYP_HOST_VAR_PREFIX ?=\n') - root_makefile.write('GYP_HOST_MULTILIB ?= first\n') - - # Write out the sorted list of includes. - root_makefile.write('\n') - for include_file in sorted(include_list): - root_makefile.write('include $(LOCAL_PATH)/' + include_file + '\n') - root_makefile.write('\n') - - if write_alias_targets: - root_makefile.write(ALL_MODULES_FOOTER) - - root_makefile.close() + options = params["options"] + generator_flags = params.get("generator_flags", {}) + limit_to_target_all = generator_flags.get("limit_to_target_all", False) + write_alias_targets = generator_flags.get("write_alias_targets", True) + sdk_version = generator_flags.get("aosp_sdk_version", 0) + android_top_dir = os.environ.get("ANDROID_BUILD_TOP") + assert android_top_dir, "$ANDROID_BUILD_TOP not set; you need to run lunch." + + def CalculateMakefilePath(build_file, base_name): + """Determine where to write a Makefile for a given gyp file.""" + # Paths in gyp files are relative to the .gyp file, but we want + # paths relative to the source root for the master makefile. Grab + # the path of the .gyp file as the base to relativize against. + # E.g. "foo/bar" when we're constructing targets for "foo/bar/baz.gyp". + base_path = gyp.common.RelativePath(os.path.dirname(build_file), options.depth) + # We write the file in the base_path directory. + output_file = os.path.join(options.depth, base_path, base_name) + assert ( + not options.generator_output + ), "The Android backend does not support options.generator_output." + base_path = gyp.common.RelativePath( + os.path.dirname(build_file), options.toplevel_dir + ) + return base_path, output_file + + # TODO: search for the first non-'Default' target. This can go + # away when we add verification that all targets have the + # necessary configurations. + default_configuration = None + for target in target_list: + spec = target_dicts[target] + if spec["default_configuration"] != "Default": + default_configuration = spec["default_configuration"] + break + if not default_configuration: + default_configuration = "Default" + + makefile_name = "GypAndroid" + options.suffix + ".mk" + makefile_path = os.path.join(options.toplevel_dir, makefile_name) + assert ( + not options.generator_output + ), "The Android backend does not support options.generator_output." + gyp.common.EnsureDirExists(makefile_path) + root_makefile = open(makefile_path, "w") + + root_makefile.write(header) + + # We set LOCAL_PATH just once, here, to the top of the project tree. This + # allows all the other paths we use to be relative to the Android.mk file, + # as the Android build system expects. + root_makefile.write("\nLOCAL_PATH := $(call my-dir)\n") + + # Find the list of targets that derive from the gyp file(s) being built. + needed_targets = set() + for build_file in params["build_files"]: + for target in gyp.common.AllTargets(target_list, target_dicts, build_file): + needed_targets.add(target) + + build_files = set() + include_list = set() + android_modules = {} + for qualified_target in target_list: + build_file, target, toolset = gyp.common.ParseQualifiedTarget(qualified_target) + relative_build_file = gyp.common.RelativePath(build_file, options.toplevel_dir) + build_files.add(relative_build_file) + included_files = data[build_file]["included_files"] + for included_file in included_files: + # The included_files entries are relative to the dir of the build file + # that included them, so we have to undo that and then make them relative + # to the root dir. + relative_include_file = gyp.common.RelativePath( + gyp.common.UnrelativePath(included_file, build_file), + options.toplevel_dir, + ) + abs_include_file = os.path.abspath(relative_include_file) + # If the include file is from the ~/.gyp dir, we should use absolute path + # so that relocating the src dir doesn't break the path. + if params["home_dot_gyp"] and abs_include_file.startswith( + params["home_dot_gyp"] + ): + build_files.add(abs_include_file) + else: + build_files.add(relative_include_file) + + base_path, output_file = CalculateMakefilePath( + build_file, target + "." + toolset + options.suffix + ".mk" + ) + + spec = target_dicts[qualified_target] + configs = spec["configurations"] + + part_of_all = qualified_target in needed_targets + if limit_to_target_all and not part_of_all: + continue + + relative_target = gyp.common.QualifiedTarget( + relative_build_file, target, toolset + ) + writer = AndroidMkWriter(android_top_dir) + android_module = writer.Write( + qualified_target, + relative_target, + base_path, + output_file, + spec, + configs, + part_of_all=part_of_all, + write_alias_target=write_alias_targets, + sdk_version=sdk_version, + ) + if android_module in android_modules: + print( + "ERROR: Android module names must be unique. The following " + "targets both generate Android module name %s.\n %s\n %s" + % (android_module, android_modules[android_module], qualified_target) + ) + return + android_modules[android_module] = qualified_target + + # Our root_makefile lives at the source root. Compute the relative path + # from there to the output_file for including. + mkfile_rel_path = gyp.common.RelativePath( + output_file, os.path.dirname(makefile_path) + ) + include_list.add(mkfile_rel_path) + + root_makefile.write("GYP_CONFIGURATION ?= %s\n" % default_configuration) + root_makefile.write("GYP_VAR_PREFIX ?=\n") + root_makefile.write("GYP_HOST_VAR_PREFIX ?=\n") + root_makefile.write("GYP_HOST_MULTILIB ?= first\n") + + # Write out the sorted list of includes. + root_makefile.write("\n") + for include_file in sorted(include_list): + root_makefile.write("include $(LOCAL_PATH)/" + include_file + "\n") + root_makefile.write("\n") + + if write_alias_targets: + root_makefile.write(ALL_MODULES_FOOTER) + + root_makefile.close() diff --git a/tools/gyp/pylib/gyp/generator/cmake.py b/tools/gyp/pylib/gyp/generator/cmake.py index e966a8f23e1f09..f5ceacfca364ce 100644 --- a/tools/gyp/pylib/gyp/generator/cmake.py +++ b/tools/gyp/pylib/gyp/generator/cmake.py @@ -38,91 +38,98 @@ import gyp.common import gyp.xcode_emulation +try: + # maketrans moved to str in python3. + _maketrans = string.maketrans +except NameError: + _maketrans = str.maketrans + generator_default_variables = { - 'EXECUTABLE_PREFIX': '', - 'EXECUTABLE_SUFFIX': '', - 'STATIC_LIB_PREFIX': 'lib', - 'STATIC_LIB_SUFFIX': '.a', - 'SHARED_LIB_PREFIX': 'lib', - 'SHARED_LIB_SUFFIX': '.so', - 'SHARED_LIB_DIR': '${builddir}/lib.${TOOLSET}', - 'LIB_DIR': '${obj}.${TOOLSET}', - 'INTERMEDIATE_DIR': '${obj}.${TOOLSET}/${TARGET}/geni', - 'SHARED_INTERMEDIATE_DIR': '${obj}/gen', - 'PRODUCT_DIR': '${builddir}', - 'RULE_INPUT_PATH': '${RULE_INPUT_PATH}', - 'RULE_INPUT_DIRNAME': '${RULE_INPUT_DIRNAME}', - 'RULE_INPUT_NAME': '${RULE_INPUT_NAME}', - 'RULE_INPUT_ROOT': '${RULE_INPUT_ROOT}', - 'RULE_INPUT_EXT': '${RULE_INPUT_EXT}', - 'CONFIGURATION_NAME': '${configuration}', + "EXECUTABLE_PREFIX": "", + "EXECUTABLE_SUFFIX": "", + "STATIC_LIB_PREFIX": "lib", + "STATIC_LIB_SUFFIX": ".a", + "SHARED_LIB_PREFIX": "lib", + "SHARED_LIB_SUFFIX": ".so", + "SHARED_LIB_DIR": "${builddir}/lib.${TOOLSET}", + "LIB_DIR": "${obj}.${TOOLSET}", + "INTERMEDIATE_DIR": "${obj}.${TOOLSET}/${TARGET}/geni", + "SHARED_INTERMEDIATE_DIR": "${obj}/gen", + "PRODUCT_DIR": "${builddir}", + "RULE_INPUT_PATH": "${RULE_INPUT_PATH}", + "RULE_INPUT_DIRNAME": "${RULE_INPUT_DIRNAME}", + "RULE_INPUT_NAME": "${RULE_INPUT_NAME}", + "RULE_INPUT_ROOT": "${RULE_INPUT_ROOT}", + "RULE_INPUT_EXT": "${RULE_INPUT_EXT}", + "CONFIGURATION_NAME": "${configuration}", } -FULL_PATH_VARS = ('${CMAKE_CURRENT_LIST_DIR}', '${builddir}', '${obj}') +FULL_PATH_VARS = ("${CMAKE_CURRENT_LIST_DIR}", "${builddir}", "${obj}") generator_supports_multiple_toolsets = True generator_wants_static_library_dependencies_adjusted = True COMPILABLE_EXTENSIONS = { - '.c': 'cc', - '.cc': 'cxx', - '.cpp': 'cxx', - '.cxx': 'cxx', - '.s': 's', # cc - '.S': 's', # cc + ".c": "cc", + ".cc": "cxx", + ".cpp": "cxx", + ".cxx": "cxx", + ".s": "s", # cc + ".S": "s", # cc } def RemovePrefix(a, prefix): - """Returns 'a' without 'prefix' if it starts with 'prefix'.""" - return a[len(prefix):] if a.startswith(prefix) else a + """Returns 'a' without 'prefix' if it starts with 'prefix'.""" + return a[len(prefix) :] if a.startswith(prefix) else a def CalculateVariables(default_variables, params): - """Calculate additional variables for use in the build (called by gyp).""" - default_variables.setdefault('OS', gyp.common.GetFlavor(params)) + """Calculate additional variables for use in the build (called by gyp).""" + default_variables.setdefault("OS", gyp.common.GetFlavor(params)) def Compilable(filename): - """Return true if the file is compilable (should be in OBJS).""" - return any(filename.endswith(e) for e in COMPILABLE_EXTENSIONS) + """Return true if the file is compilable (should be in OBJS).""" + return any(filename.endswith(e) for e in COMPILABLE_EXTENSIONS) def Linkable(filename): - """Return true if the file is linkable (should be on the link line).""" - return filename.endswith('.o') + """Return true if the file is linkable (should be on the link line).""" + return filename.endswith(".o") def NormjoinPathForceCMakeSource(base_path, rel_path): - """Resolves rel_path against base_path and returns the result. + """Resolves rel_path against base_path and returns the result. If rel_path is an absolute path it is returned unchanged. Otherwise it is resolved against base_path and normalized. If the result is a relative path, it is forced to be relative to the CMakeLists.txt. """ - if os.path.isabs(rel_path): - return rel_path - if any([rel_path.startswith(var) for var in FULL_PATH_VARS]): - return rel_path - # TODO: do we need to check base_path for absolute variables as well? - return os.path.join('${CMAKE_CURRENT_LIST_DIR}', - os.path.normpath(os.path.join(base_path, rel_path))) + if os.path.isabs(rel_path): + return rel_path + if any([rel_path.startswith(var) for var in FULL_PATH_VARS]): + return rel_path + # TODO: do we need to check base_path for absolute variables as well? + return os.path.join( + "${CMAKE_CURRENT_LIST_DIR}", os.path.normpath(os.path.join(base_path, rel_path)) + ) def NormjoinPath(base_path, rel_path): - """Resolves rel_path against base_path and returns the result. + """Resolves rel_path against base_path and returns the result. TODO: what is this really used for? If rel_path begins with '$' it is returned unchanged. Otherwise it is resolved against base_path if relative, then normalized. """ - if rel_path.startswith('$') and not rel_path.startswith('${configuration}'): - return rel_path - return os.path.normpath(os.path.join(base_path, rel_path)) + if rel_path.startswith("$") and not rel_path.startswith("${configuration}"): + return rel_path + return os.path.normpath(os.path.join(base_path, rel_path)) def CMakeStringEscape(a): - """Escapes the string 'a' for use inside a CMake string. + """Escapes the string 'a' for use inside a CMake string. This means escaping '\' otherwise it may be seen as modifying the next character @@ -137,118 +144,114 @@ def CMakeStringEscape(a): but text $ should be escaped what is wanted is to know which $ come from generator variables """ - return a.replace('\\', '\\\\').replace(';', '\\;').replace('"', '\\"') + return a.replace("\\", "\\\\").replace(";", "\\;").replace('"', '\\"') def SetFileProperty(output, source_name, property_name, values, sep): - """Given a set of source file, sets the given property on them.""" - output.write('set_source_files_properties(') - output.write(source_name) - output.write(' PROPERTIES ') - output.write(property_name) - output.write(' "') - for value in values: - output.write(CMakeStringEscape(value)) - output.write(sep) - output.write('")\n') + """Given a set of source file, sets the given property on them.""" + output.write("set_source_files_properties(") + output.write(source_name) + output.write(" PROPERTIES ") + output.write(property_name) + output.write(' "') + for value in values: + output.write(CMakeStringEscape(value)) + output.write(sep) + output.write('")\n') def SetFilesProperty(output, variable, property_name, values, sep): - """Given a set of source files, sets the given property on them.""" - output.write('set_source_files_properties(') - WriteVariable(output, variable) - output.write(' PROPERTIES ') - output.write(property_name) - output.write(' "') - for value in values: - output.write(CMakeStringEscape(value)) - output.write(sep) - output.write('")\n') - - -def SetTargetProperty(output, target_name, property_name, values, sep=''): - """Given a target, sets the given property.""" - output.write('set_target_properties(') - output.write(target_name) - output.write(' PROPERTIES ') - output.write(property_name) - output.write(' "') - for value in values: - output.write(CMakeStringEscape(value)) - output.write(sep) - output.write('")\n') + """Given a set of source files, sets the given property on them.""" + output.write("set_source_files_properties(") + WriteVariable(output, variable) + output.write(" PROPERTIES ") + output.write(property_name) + output.write(' "') + for value in values: + output.write(CMakeStringEscape(value)) + output.write(sep) + output.write('")\n') + + +def SetTargetProperty(output, target_name, property_name, values, sep=""): + """Given a target, sets the given property.""" + output.write("set_target_properties(") + output.write(target_name) + output.write(" PROPERTIES ") + output.write(property_name) + output.write(' "') + for value in values: + output.write(CMakeStringEscape(value)) + output.write(sep) + output.write('")\n') def SetVariable(output, variable_name, value): - """Sets a CMake variable.""" - output.write('set(') - output.write(variable_name) - output.write(' "') - output.write(CMakeStringEscape(value)) - output.write('")\n') + """Sets a CMake variable.""" + output.write("set(") + output.write(variable_name) + output.write(' "') + output.write(CMakeStringEscape(value)) + output.write('")\n') def SetVariableList(output, variable_name, values): - """Sets a CMake variable to a list.""" - if not values: - return SetVariable(output, variable_name, "") - if len(values) == 1: - return SetVariable(output, variable_name, values[0]) - output.write('list(APPEND ') - output.write(variable_name) - output.write('\n "') - output.write('"\n "'.join([CMakeStringEscape(value) for value in values])) - output.write('")\n') + """Sets a CMake variable to a list.""" + if not values: + return SetVariable(output, variable_name, "") + if len(values) == 1: + return SetVariable(output, variable_name, values[0]) + output.write("list(APPEND ") + output.write(variable_name) + output.write('\n "') + output.write('"\n "'.join([CMakeStringEscape(value) for value in values])) + output.write('")\n') def UnsetVariable(output, variable_name): - """Unsets a CMake variable.""" - output.write('unset(') - output.write(variable_name) - output.write(')\n') + """Unsets a CMake variable.""" + output.write("unset(") + output.write(variable_name) + output.write(")\n") def WriteVariable(output, variable_name, prepend=None): - if prepend: - output.write(prepend) - output.write('${') - output.write(variable_name) - output.write('}') + if prepend: + output.write(prepend) + output.write("${") + output.write(variable_name) + output.write("}") class CMakeTargetType(object): - def __init__(self, command, modifier, property_modifier): - self.command = command - self.modifier = modifier - self.property_modifier = property_modifier + def __init__(self, command, modifier, property_modifier): + self.command = command + self.modifier = modifier + self.property_modifier = property_modifier cmake_target_type_from_gyp_target_type = { - 'executable': CMakeTargetType('add_executable', None, 'RUNTIME'), - 'static_library': CMakeTargetType('add_library', 'STATIC', 'ARCHIVE'), - 'shared_library': CMakeTargetType('add_library', 'SHARED', 'LIBRARY'), - 'loadable_module': CMakeTargetType('add_library', 'MODULE', 'LIBRARY'), - 'none': CMakeTargetType('add_custom_target', 'SOURCES', None), + "executable": CMakeTargetType("add_executable", None, "RUNTIME"), + "static_library": CMakeTargetType("add_library", "STATIC", "ARCHIVE"), + "shared_library": CMakeTargetType("add_library", "SHARED", "LIBRARY"), + "loadable_module": CMakeTargetType("add_library", "MODULE", "LIBRARY"), + "none": CMakeTargetType("add_custom_target", "SOURCES", None), } def StringToCMakeTargetName(a): - """Converts the given string 'a' to a valid CMake target name. + """Converts the given string 'a' to a valid CMake target name. All invalid characters are replaced by '_'. Invalid for cmake: ' ', '/', '(', ')', '"' Invalid for make: ':' Invalid for unknown reasons but cause failures: '.' """ - try: - return a.translate(str.maketrans(' /():."', '_______')) - except AttributeError: - return a.translate(string.maketrans(' /():."', '_______')) + return a.translate(_maketrans(' /():."', "_______")) -def WriteActions(target_name, actions, extra_sources, extra_deps, - path_to_gyp, output): - """Write CMake for the 'actions' in the target. +def WriteActions(target_name, actions, extra_sources, extra_deps, path_to_gyp, output): + """Write CMake for the 'actions' in the target. Args: target_name: the name of the CMake target being generated. @@ -258,83 +261,86 @@ def WriteActions(target_name, actions, extra_sources, extra_deps, path_to_gyp: relative path from CMakeLists.txt being generated to the Gyp file in which the target being generated is defined. """ - for action in actions: - action_name = StringToCMakeTargetName(action['action_name']) - action_target_name = '%s__%s' % (target_name, action_name) - - inputs = action['inputs'] - inputs_name = action_target_name + '__input' - SetVariableList(output, inputs_name, - [NormjoinPathForceCMakeSource(path_to_gyp, dep) for dep in inputs]) - - outputs = action['outputs'] - cmake_outputs = [NormjoinPathForceCMakeSource(path_to_gyp, out) - for out in outputs] - outputs_name = action_target_name + '__output' - SetVariableList(output, outputs_name, cmake_outputs) - - # Build up a list of outputs. - # Collect the output dirs we'll need. - dirs = set(dir for dir in (os.path.dirname(o) for o in outputs) if dir) - - if int(action.get('process_outputs_as_sources', False)): - extra_sources.extend(zip(cmake_outputs, outputs)) - - # add_custom_command - output.write('add_custom_command(OUTPUT ') - WriteVariable(output, outputs_name) - output.write('\n') - - if len(dirs) > 0: - for directory in dirs: - output.write(' COMMAND ${CMAKE_COMMAND} -E make_directory ') - output.write(directory) - output.write('\n') - - output.write(' COMMAND ') - output.write(gyp.common.EncodePOSIXShellList(action['action'])) - output.write('\n') - - output.write(' DEPENDS ') - WriteVariable(output, inputs_name) - output.write('\n') - - output.write(' WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/') - output.write(path_to_gyp) - output.write('\n') - - output.write(' COMMENT ') - if 'message' in action: - output.write(action['message']) - else: - output.write(action_target_name) - output.write('\n') - - output.write(' VERBATIM\n') - output.write(')\n') - - # add_custom_target - output.write('add_custom_target(') - output.write(action_target_name) - output.write('\n DEPENDS ') - WriteVariable(output, outputs_name) - output.write('\n SOURCES ') - WriteVariable(output, inputs_name) - output.write('\n)\n') - - extra_deps.append(action_target_name) + for action in actions: + action_name = StringToCMakeTargetName(action["action_name"]) + action_target_name = "%s__%s" % (target_name, action_name) + + inputs = action["inputs"] + inputs_name = action_target_name + "__input" + SetVariableList( + output, + inputs_name, + [NormjoinPathForceCMakeSource(path_to_gyp, dep) for dep in inputs], + ) + + outputs = action["outputs"] + cmake_outputs = [ + NormjoinPathForceCMakeSource(path_to_gyp, out) for out in outputs + ] + outputs_name = action_target_name + "__output" + SetVariableList(output, outputs_name, cmake_outputs) + + # Build up a list of outputs. + # Collect the output dirs we'll need. + dirs = set(dir for dir in (os.path.dirname(o) for o in outputs) if dir) + + if int(action.get("process_outputs_as_sources", False)): + extra_sources.extend(zip(cmake_outputs, outputs)) + + # add_custom_command + output.write("add_custom_command(OUTPUT ") + WriteVariable(output, outputs_name) + output.write("\n") + + if len(dirs) > 0: + for directory in dirs: + output.write(" COMMAND ${CMAKE_COMMAND} -E make_directory ") + output.write(directory) + output.write("\n") + + output.write(" COMMAND ") + output.write(gyp.common.EncodePOSIXShellList(action["action"])) + output.write("\n") + + output.write(" DEPENDS ") + WriteVariable(output, inputs_name) + output.write("\n") + + output.write(" WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/") + output.write(path_to_gyp) + output.write("\n") + + output.write(" COMMENT ") + if "message" in action: + output.write(action["message"]) + else: + output.write(action_target_name) + output.write("\n") + + output.write(" VERBATIM\n") + output.write(")\n") + + # add_custom_target + output.write("add_custom_target(") + output.write(action_target_name) + output.write("\n DEPENDS ") + WriteVariable(output, outputs_name) + output.write("\n SOURCES ") + WriteVariable(output, inputs_name) + output.write("\n)\n") + + extra_deps.append(action_target_name) def NormjoinRulePathForceCMakeSource(base_path, rel_path, rule_source): - if rel_path.startswith(("${RULE_INPUT_PATH}","${RULE_INPUT_DIRNAME}")): - if any([rule_source.startswith(var) for var in FULL_PATH_VARS]): - return rel_path - return NormjoinPathForceCMakeSource(base_path, rel_path) + if rel_path.startswith(("${RULE_INPUT_PATH}", "${RULE_INPUT_DIRNAME}")): + if any([rule_source.startswith(var) for var in FULL_PATH_VARS]): + return rel_path + return NormjoinPathForceCMakeSource(base_path, rel_path) -def WriteRules(target_name, rules, extra_sources, extra_deps, - path_to_gyp, output): - """Write CMake for the 'rules' in the target. +def WriteRules(target_name, rules, extra_sources, extra_deps, path_to_gyp, output): + """Write CMake for the 'rules' in the target. Args: target_name: the name of the CMake target being generated. @@ -344,110 +350,115 @@ def WriteRules(target_name, rules, extra_sources, extra_deps, path_to_gyp: relative path from CMakeLists.txt being generated to the Gyp file in which the target being generated is defined. """ - for rule in rules: - rule_name = StringToCMakeTargetName(target_name + '__' + rule['rule_name']) - - inputs = rule.get('inputs', []) - inputs_name = rule_name + '__input' - SetVariableList(output, inputs_name, - [NormjoinPathForceCMakeSource(path_to_gyp, dep) for dep in inputs]) - outputs = rule['outputs'] - var_outputs = [] - - for count, rule_source in enumerate(rule.get('rule_sources', [])): - action_name = rule_name + '_' + str(count) - - rule_source_dirname, rule_source_basename = os.path.split(rule_source) - rule_source_root, rule_source_ext = os.path.splitext(rule_source_basename) - - SetVariable(output, 'RULE_INPUT_PATH', rule_source) - SetVariable(output, 'RULE_INPUT_DIRNAME', rule_source_dirname) - SetVariable(output, 'RULE_INPUT_NAME', rule_source_basename) - SetVariable(output, 'RULE_INPUT_ROOT', rule_source_root) - SetVariable(output, 'RULE_INPUT_EXT', rule_source_ext) - - # Build up a list of outputs. - # Collect the output dirs we'll need. - dirs = set(dir for dir in (os.path.dirname(o) for o in outputs) if dir) - - # Create variables for the output, as 'local' variable will be unset. - these_outputs = [] - for output_index, out in enumerate(outputs): - output_name = action_name + '_' + str(output_index) - SetVariable(output, output_name, - NormjoinRulePathForceCMakeSource(path_to_gyp, out, - rule_source)) - if int(rule.get('process_outputs_as_sources', False)): - extra_sources.append(('${' + output_name + '}', out)) - these_outputs.append('${' + output_name + '}') - var_outputs.append('${' + output_name + '}') - - # add_custom_command - output.write('add_custom_command(OUTPUT\n') - for out in these_outputs: - output.write(' ') - output.write(out) - output.write('\n') - - for directory in dirs: - output.write(' COMMAND ${CMAKE_COMMAND} -E make_directory ') - output.write(directory) - output.write('\n') - - output.write(' COMMAND ') - output.write(gyp.common.EncodePOSIXShellList(rule['action'])) - output.write('\n') - - output.write(' DEPENDS ') - WriteVariable(output, inputs_name) - output.write(' ') - output.write(NormjoinPath(path_to_gyp, rule_source)) - output.write('\n') - - # CMAKE_CURRENT_LIST_DIR is where the CMakeLists.txt lives. - # The cwd is the current build directory. - output.write(' WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/') - output.write(path_to_gyp) - output.write('\n') - - output.write(' COMMENT ') - if 'message' in rule: - output.write(rule['message']) - else: - output.write(action_name) - output.write('\n') - - output.write(' VERBATIM\n') - output.write(')\n') - - UnsetVariable(output, 'RULE_INPUT_PATH') - UnsetVariable(output, 'RULE_INPUT_DIRNAME') - UnsetVariable(output, 'RULE_INPUT_NAME') - UnsetVariable(output, 'RULE_INPUT_ROOT') - UnsetVariable(output, 'RULE_INPUT_EXT') - - # add_custom_target - output.write('add_custom_target(') - output.write(rule_name) - output.write(' DEPENDS\n') - for out in var_outputs: - output.write(' ') - output.write(out) - output.write('\n') - output.write('SOURCES ') - WriteVariable(output, inputs_name) - output.write('\n') - for rule_source in rule.get('rule_sources', []): - output.write(' ') - output.write(NormjoinPath(path_to_gyp, rule_source)) - output.write('\n') - output.write(')\n') - - extra_deps.append(rule_name) + for rule in rules: + rule_name = StringToCMakeTargetName(target_name + "__" + rule["rule_name"]) + + inputs = rule.get("inputs", []) + inputs_name = rule_name + "__input" + SetVariableList( + output, + inputs_name, + [NormjoinPathForceCMakeSource(path_to_gyp, dep) for dep in inputs], + ) + outputs = rule["outputs"] + var_outputs = [] + + for count, rule_source in enumerate(rule.get("rule_sources", [])): + action_name = rule_name + "_" + str(count) + + rule_source_dirname, rule_source_basename = os.path.split(rule_source) + rule_source_root, rule_source_ext = os.path.splitext(rule_source_basename) + + SetVariable(output, "RULE_INPUT_PATH", rule_source) + SetVariable(output, "RULE_INPUT_DIRNAME", rule_source_dirname) + SetVariable(output, "RULE_INPUT_NAME", rule_source_basename) + SetVariable(output, "RULE_INPUT_ROOT", rule_source_root) + SetVariable(output, "RULE_INPUT_EXT", rule_source_ext) + + # Build up a list of outputs. + # Collect the output dirs we'll need. + dirs = set(dir for dir in (os.path.dirname(o) for o in outputs) if dir) + + # Create variables for the output, as 'local' variable will be unset. + these_outputs = [] + for output_index, out in enumerate(outputs): + output_name = action_name + "_" + str(output_index) + SetVariable( + output, + output_name, + NormjoinRulePathForceCMakeSource(path_to_gyp, out, rule_source), + ) + if int(rule.get("process_outputs_as_sources", False)): + extra_sources.append(("${" + output_name + "}", out)) + these_outputs.append("${" + output_name + "}") + var_outputs.append("${" + output_name + "}") + + # add_custom_command + output.write("add_custom_command(OUTPUT\n") + for out in these_outputs: + output.write(" ") + output.write(out) + output.write("\n") + + for directory in dirs: + output.write(" COMMAND ${CMAKE_COMMAND} -E make_directory ") + output.write(directory) + output.write("\n") + + output.write(" COMMAND ") + output.write(gyp.common.EncodePOSIXShellList(rule["action"])) + output.write("\n") + + output.write(" DEPENDS ") + WriteVariable(output, inputs_name) + output.write(" ") + output.write(NormjoinPath(path_to_gyp, rule_source)) + output.write("\n") + + # CMAKE_CURRENT_LIST_DIR is where the CMakeLists.txt lives. + # The cwd is the current build directory. + output.write(" WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/") + output.write(path_to_gyp) + output.write("\n") + + output.write(" COMMENT ") + if "message" in rule: + output.write(rule["message"]) + else: + output.write(action_name) + output.write("\n") + + output.write(" VERBATIM\n") + output.write(")\n") + + UnsetVariable(output, "RULE_INPUT_PATH") + UnsetVariable(output, "RULE_INPUT_DIRNAME") + UnsetVariable(output, "RULE_INPUT_NAME") + UnsetVariable(output, "RULE_INPUT_ROOT") + UnsetVariable(output, "RULE_INPUT_EXT") + + # add_custom_target + output.write("add_custom_target(") + output.write(rule_name) + output.write(" DEPENDS\n") + for out in var_outputs: + output.write(" ") + output.write(out) + output.write("\n") + output.write("SOURCES ") + WriteVariable(output, inputs_name) + output.write("\n") + for rule_source in rule.get("rule_sources", []): + output.write(" ") + output.write(NormjoinPath(path_to_gyp, rule_source)) + output.write("\n") + output.write(")\n") + + extra_deps.append(rule_name) def WriteCopies(target_name, copies, extra_deps, path_to_gyp, output): - """Write CMake for the 'copies' in the target. + """Write CMake for the 'copies' in the target. Args: target_name: the name of the CMake target being generated. @@ -456,126 +467,128 @@ def WriteCopies(target_name, copies, extra_deps, path_to_gyp, output): path_to_gyp: relative path from CMakeLists.txt being generated to the Gyp file in which the target being generated is defined. """ - copy_name = target_name + '__copies' + copy_name = target_name + "__copies" + + # CMake gets upset with custom targets with OUTPUT which specify no output. + have_copies = any(copy["files"] for copy in copies) + if not have_copies: + output.write("add_custom_target(") + output.write(copy_name) + output.write(")\n") + extra_deps.append(copy_name) + return + + class Copy(object): + def __init__(self, ext, command): + self.cmake_inputs = [] + self.cmake_outputs = [] + self.gyp_inputs = [] + self.gyp_outputs = [] + self.ext = ext + self.inputs_name = None + self.outputs_name = None + self.command = command + + file_copy = Copy("", "copy") + dir_copy = Copy("_dirs", "copy_directory") + + for copy in copies: + files = copy["files"] + destination = copy["destination"] + for src in files: + path = os.path.normpath(src) + basename = os.path.split(path)[1] + dst = os.path.join(destination, basename) + + copy = file_copy if os.path.basename(src) else dir_copy + + copy.cmake_inputs.append(NormjoinPathForceCMakeSource(path_to_gyp, src)) + copy.cmake_outputs.append(NormjoinPathForceCMakeSource(path_to_gyp, dst)) + copy.gyp_inputs.append(src) + copy.gyp_outputs.append(dst) + + for copy in (file_copy, dir_copy): + if copy.cmake_inputs: + copy.inputs_name = copy_name + "__input" + copy.ext + SetVariableList(output, copy.inputs_name, copy.cmake_inputs) + + copy.outputs_name = copy_name + "__output" + copy.ext + SetVariableList(output, copy.outputs_name, copy.cmake_outputs) + + # add_custom_command + output.write("add_custom_command(\n") + + output.write("OUTPUT") + for copy in (file_copy, dir_copy): + if copy.outputs_name: + WriteVariable(output, copy.outputs_name, " ") + output.write("\n") + + for copy in (file_copy, dir_copy): + for src, dst in zip(copy.gyp_inputs, copy.gyp_outputs): + # 'cmake -E copy src dst' will create the 'dst' directory if needed. + output.write("COMMAND ${CMAKE_COMMAND} -E %s " % copy.command) + output.write(src) + output.write(" ") + output.write(dst) + output.write("\n") + + output.write("DEPENDS") + for copy in (file_copy, dir_copy): + if copy.inputs_name: + WriteVariable(output, copy.inputs_name, " ") + output.write("\n") + + output.write("WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/") + output.write(path_to_gyp) + output.write("\n") + + output.write("COMMENT Copying for ") + output.write(target_name) + output.write("\n") - # CMake gets upset with custom targets with OUTPUT which specify no output. - have_copies = any(copy['files'] for copy in copies) - if not have_copies: - output.write('add_custom_target(') + output.write("VERBATIM\n") + output.write(")\n") + + # add_custom_target + output.write("add_custom_target(") output.write(copy_name) - output.write(')\n') + output.write("\n DEPENDS") + for copy in (file_copy, dir_copy): + if copy.outputs_name: + WriteVariable(output, copy.outputs_name, " ") + output.write("\n SOURCES") + if file_copy.inputs_name: + WriteVariable(output, file_copy.inputs_name, " ") + output.write("\n)\n") + extra_deps.append(copy_name) - return - - class Copy(object): - def __init__(self, ext, command): - self.cmake_inputs = [] - self.cmake_outputs = [] - self.gyp_inputs = [] - self.gyp_outputs = [] - self.ext = ext - self.inputs_name = None - self.outputs_name = None - self.command = command - - file_copy = Copy('', 'copy') - dir_copy = Copy('_dirs', 'copy_directory') - - for copy in copies: - files = copy['files'] - destination = copy['destination'] - for src in files: - path = os.path.normpath(src) - basename = os.path.split(path)[1] - dst = os.path.join(destination, basename) - - copy = file_copy if os.path.basename(src) else dir_copy - - copy.cmake_inputs.append(NormjoinPathForceCMakeSource(path_to_gyp, src)) - copy.cmake_outputs.append(NormjoinPathForceCMakeSource(path_to_gyp, dst)) - copy.gyp_inputs.append(src) - copy.gyp_outputs.append(dst) - - for copy in (file_copy, dir_copy): - if copy.cmake_inputs: - copy.inputs_name = copy_name + '__input' + copy.ext - SetVariableList(output, copy.inputs_name, copy.cmake_inputs) - - copy.outputs_name = copy_name + '__output' + copy.ext - SetVariableList(output, copy.outputs_name, copy.cmake_outputs) - - # add_custom_command - output.write('add_custom_command(\n') - - output.write('OUTPUT') - for copy in (file_copy, dir_copy): - if copy.outputs_name: - WriteVariable(output, copy.outputs_name, ' ') - output.write('\n') - - for copy in (file_copy, dir_copy): - for src, dst in zip(copy.gyp_inputs, copy.gyp_outputs): - # 'cmake -E copy src dst' will create the 'dst' directory if needed. - output.write('COMMAND ${CMAKE_COMMAND} -E %s ' % copy.command) - output.write(src) - output.write(' ') - output.write(dst) - output.write("\n") - - output.write('DEPENDS') - for copy in (file_copy, dir_copy): - if copy.inputs_name: - WriteVariable(output, copy.inputs_name, ' ') - output.write('\n') - - output.write('WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/') - output.write(path_to_gyp) - output.write('\n') - - output.write('COMMENT Copying for ') - output.write(target_name) - output.write('\n') - - output.write('VERBATIM\n') - output.write(')\n') - - # add_custom_target - output.write('add_custom_target(') - output.write(copy_name) - output.write('\n DEPENDS') - for copy in (file_copy, dir_copy): - if copy.outputs_name: - WriteVariable(output, copy.outputs_name, ' ') - output.write('\n SOURCES') - if file_copy.inputs_name: - WriteVariable(output, file_copy.inputs_name, ' ') - output.write('\n)\n') - - extra_deps.append(copy_name) def CreateCMakeTargetBaseName(qualified_target): - """This is the name we would like the target to have.""" - _, gyp_target_name, gyp_target_toolset = ( - gyp.common.ParseQualifiedTarget(qualified_target)) - cmake_target_base_name = gyp_target_name - if gyp_target_toolset and gyp_target_toolset != 'target': - cmake_target_base_name += '_' + gyp_target_toolset - return StringToCMakeTargetName(cmake_target_base_name) + """This is the name we would like the target to have.""" + _, gyp_target_name, gyp_target_toolset = gyp.common.ParseQualifiedTarget( + qualified_target + ) + cmake_target_base_name = gyp_target_name + if gyp_target_toolset and gyp_target_toolset != "target": + cmake_target_base_name += "_" + gyp_target_toolset + return StringToCMakeTargetName(cmake_target_base_name) def CreateCMakeTargetFullName(qualified_target): - """An unambiguous name for the target.""" - gyp_file, gyp_target_name, gyp_target_toolset = ( - gyp.common.ParseQualifiedTarget(qualified_target)) - cmake_target_full_name = gyp_file + ':' + gyp_target_name - if gyp_target_toolset and gyp_target_toolset != 'target': - cmake_target_full_name += '_' + gyp_target_toolset - return StringToCMakeTargetName(cmake_target_full_name) + """An unambiguous name for the target.""" + gyp_file, gyp_target_name, gyp_target_toolset = gyp.common.ParseQualifiedTarget( + qualified_target + ) + cmake_target_full_name = gyp_file + ":" + gyp_target_name + if gyp_target_toolset and gyp_target_toolset != "target": + cmake_target_full_name += "_" + gyp_target_toolset + return StringToCMakeTargetName(cmake_target_full_name) class CMakeNamer(object): - """Converts Gyp target names into CMake target names. + """Converts Gyp target names into CMake target names. CMake requires that target names be globally unique. One way to ensure this is to fully qualify the names of the targets. Unfortunately, this @@ -594,660 +607,721 @@ class CMakeNamer(object): building. However, it also makes sense for an IDE, as it is possible for defines to be different. """ - def __init__(self, target_list): - self.cmake_target_base_names_conficting = set() - cmake_target_base_names_seen = set() - for qualified_target in target_list: - cmake_target_base_name = CreateCMakeTargetBaseName(qualified_target) - - if cmake_target_base_name not in cmake_target_base_names_seen: - cmake_target_base_names_seen.add(cmake_target_base_name) - else: - self.cmake_target_base_names_conficting.add(cmake_target_base_name) - - def CreateCMakeTargetName(self, qualified_target): - base_name = CreateCMakeTargetBaseName(qualified_target) - if base_name in self.cmake_target_base_names_conficting: - return CreateCMakeTargetFullName(qualified_target) - return base_name - - -def WriteTarget(namer, qualified_target, target_dicts, build_dir, config_to_use, - options, generator_flags, all_qualified_targets, flavor, - output): - # The make generator does this always. - # TODO: It would be nice to be able to tell CMake all dependencies. - circular_libs = generator_flags.get('circular', True) - - if not generator_flags.get('standalone', False): - output.write('\n#') - output.write(qualified_target) - output.write('\n') - - gyp_file, _, _ = gyp.common.ParseQualifiedTarget(qualified_target) - rel_gyp_file = gyp.common.RelativePath(gyp_file, options.toplevel_dir) - rel_gyp_dir = os.path.dirname(rel_gyp_file) - - # Relative path from build dir to top dir. - build_to_top = gyp.common.InvertRelativePath(build_dir, options.toplevel_dir) - # Relative path from build dir to gyp dir. - build_to_gyp = os.path.join(build_to_top, rel_gyp_dir) - - path_from_cmakelists_to_gyp = build_to_gyp - - spec = target_dicts.get(qualified_target, {}) - config = spec.get('configurations', {}).get(config_to_use, {}) - - xcode_settings = None - if flavor == 'mac': - xcode_settings = gyp.xcode_emulation.XcodeSettings(spec) - - target_name = spec.get('target_name', '') - target_type = spec.get('type', '') - target_toolset = spec.get('toolset') - - cmake_target_type = cmake_target_type_from_gyp_target_type.get(target_type) - if cmake_target_type is None: - print('Target %s has unknown target type %s, skipping.' % - ( target_name, target_type )) - return - - SetVariable(output, 'TARGET', target_name) - SetVariable(output, 'TOOLSET', target_toolset) - - cmake_target_name = namer.CreateCMakeTargetName(qualified_target) - - extra_sources = [] - extra_deps = [] - - # Actions must come first, since they can generate more OBJs for use below. - if 'actions' in spec: - WriteActions(cmake_target_name, spec['actions'], extra_sources, extra_deps, - path_from_cmakelists_to_gyp, output) - - # Rules must be early like actions. - if 'rules' in spec: - WriteRules(cmake_target_name, spec['rules'], extra_sources, extra_deps, - path_from_cmakelists_to_gyp, output) - - # Copies - if 'copies' in spec: - WriteCopies(cmake_target_name, spec['copies'], extra_deps, - path_from_cmakelists_to_gyp, output) - - # Target and sources - srcs = spec.get('sources', []) - - # Gyp separates the sheep from the goats based on file extensions. - # A full separation is done here because of flag handing (see below). - s_sources = [] - c_sources = [] - cxx_sources = [] - linkable_sources = [] - other_sources = [] - for src in srcs: - _, ext = os.path.splitext(src) - src_type = COMPILABLE_EXTENSIONS.get(ext, None) - src_norm_path = NormjoinPath(path_from_cmakelists_to_gyp, src) - - if src_type == 's': - s_sources.append(src_norm_path) - elif src_type == 'cc': - c_sources.append(src_norm_path) - elif src_type == 'cxx': - cxx_sources.append(src_norm_path) - elif Linkable(ext): - linkable_sources.append(src_norm_path) - else: - other_sources.append(src_norm_path) - - for extra_source in extra_sources: - src, real_source = extra_source - _, ext = os.path.splitext(real_source) - src_type = COMPILABLE_EXTENSIONS.get(ext, None) - - if src_type == 's': - s_sources.append(src) - elif src_type == 'cc': - c_sources.append(src) - elif src_type == 'cxx': - cxx_sources.append(src) - elif Linkable(ext): - linkable_sources.append(src) - else: - other_sources.append(src) - - s_sources_name = None - if s_sources: - s_sources_name = cmake_target_name + '__asm_srcs' - SetVariableList(output, s_sources_name, s_sources) - - c_sources_name = None - if c_sources: - c_sources_name = cmake_target_name + '__c_srcs' - SetVariableList(output, c_sources_name, c_sources) - - cxx_sources_name = None - if cxx_sources: - cxx_sources_name = cmake_target_name + '__cxx_srcs' - SetVariableList(output, cxx_sources_name, cxx_sources) - - linkable_sources_name = None - if linkable_sources: - linkable_sources_name = cmake_target_name + '__linkable_srcs' - SetVariableList(output, linkable_sources_name, linkable_sources) - - other_sources_name = None - if other_sources: - other_sources_name = cmake_target_name + '__other_srcs' - SetVariableList(output, other_sources_name, other_sources) - - # CMake gets upset when executable targets provide no sources. - # http://www.cmake.org/pipermail/cmake/2010-July/038461.html - dummy_sources_name = None - has_sources = (s_sources_name or - c_sources_name or - cxx_sources_name or - linkable_sources_name or - other_sources_name) - if target_type == 'executable' and not has_sources: - dummy_sources_name = cmake_target_name + '__dummy_srcs' - SetVariable(output, dummy_sources_name, - "${obj}.${TOOLSET}/${TARGET}/genc/dummy.c") - output.write('if(NOT EXISTS "') - WriteVariable(output, dummy_sources_name) - output.write('")\n') - output.write(' file(WRITE "') - WriteVariable(output, dummy_sources_name) - output.write('" "")\n') - output.write("endif()\n") - - - # CMake is opposed to setting linker directories and considers the practice - # of setting linker directories dangerous. Instead, it favors the use of - # find_library and passing absolute paths to target_link_libraries. - # However, CMake does provide the command link_directories, which adds - # link directories to targets defined after it is called. - # As a result, link_directories must come before the target definition. - # CMake unfortunately has no means of removing entries from LINK_DIRECTORIES. - library_dirs = config.get('library_dirs') - if library_dirs is not None: - output.write('link_directories(') - for library_dir in library_dirs: - output.write(' ') - output.write(NormjoinPath(path_from_cmakelists_to_gyp, library_dir)) - output.write('\n') - output.write(')\n') - - output.write(cmake_target_type.command) - output.write('(') - output.write(cmake_target_name) - - if cmake_target_type.modifier is not None: - output.write(' ') - output.write(cmake_target_type.modifier) - - if s_sources_name: - WriteVariable(output, s_sources_name, ' ') - if c_sources_name: - WriteVariable(output, c_sources_name, ' ') - if cxx_sources_name: - WriteVariable(output, cxx_sources_name, ' ') - if linkable_sources_name: - WriteVariable(output, linkable_sources_name, ' ') - if other_sources_name: - WriteVariable(output, other_sources_name, ' ') - if dummy_sources_name: - WriteVariable(output, dummy_sources_name, ' ') - - output.write(')\n') - - # Let CMake know if the 'all' target should depend on this target. - exclude_from_all = ('TRUE' if qualified_target not in all_qualified_targets - else 'FALSE') - SetTargetProperty(output, cmake_target_name, - 'EXCLUDE_FROM_ALL', exclude_from_all) - for extra_target_name in extra_deps: - SetTargetProperty(output, extra_target_name, - 'EXCLUDE_FROM_ALL', exclude_from_all) - - # Output name and location. - if target_type != 'none': - # Link as 'C' if there are no other files - if not c_sources and not cxx_sources: - SetTargetProperty(output, cmake_target_name, 'LINKER_LANGUAGE', ['C']) - - # Mark uncompiled sources as uncompiled. - if other_sources_name: - output.write('set_source_files_properties(') - WriteVariable(output, other_sources_name, '') - output.write(' PROPERTIES HEADER_FILE_ONLY "TRUE")\n') + def __init__(self, target_list): + self.cmake_target_base_names_conficting = set() + + cmake_target_base_names_seen = set() + for qualified_target in target_list: + cmake_target_base_name = CreateCMakeTargetBaseName(qualified_target) + + if cmake_target_base_name not in cmake_target_base_names_seen: + cmake_target_base_names_seen.add(cmake_target_base_name) + else: + self.cmake_target_base_names_conficting.add(cmake_target_base_name) + + def CreateCMakeTargetName(self, qualified_target): + base_name = CreateCMakeTargetBaseName(qualified_target) + if base_name in self.cmake_target_base_names_conficting: + return CreateCMakeTargetFullName(qualified_target) + return base_name + + +def WriteTarget( + namer, + qualified_target, + target_dicts, + build_dir, + config_to_use, + options, + generator_flags, + all_qualified_targets, + flavor, + output, +): + # The make generator does this always. + # TODO: It would be nice to be able to tell CMake all dependencies. + circular_libs = generator_flags.get("circular", True) + + if not generator_flags.get("standalone", False): + output.write("\n#") + output.write(qualified_target) + output.write("\n") + + gyp_file, _, _ = gyp.common.ParseQualifiedTarget(qualified_target) + rel_gyp_file = gyp.common.RelativePath(gyp_file, options.toplevel_dir) + rel_gyp_dir = os.path.dirname(rel_gyp_file) + + # Relative path from build dir to top dir. + build_to_top = gyp.common.InvertRelativePath(build_dir, options.toplevel_dir) + # Relative path from build dir to gyp dir. + build_to_gyp = os.path.join(build_to_top, rel_gyp_dir) + + path_from_cmakelists_to_gyp = build_to_gyp + + spec = target_dicts.get(qualified_target, {}) + config = spec.get("configurations", {}).get(config_to_use, {}) + + xcode_settings = None + if flavor == "mac": + xcode_settings = gyp.xcode_emulation.XcodeSettings(spec) + + target_name = spec.get("target_name", "") + target_type = spec.get("type", "") + target_toolset = spec.get("toolset") + + cmake_target_type = cmake_target_type_from_gyp_target_type.get(target_type) + if cmake_target_type is None: + print( + "Target %s has unknown target type %s, skipping." + % (target_name, target_type) + ) + return + + SetVariable(output, "TARGET", target_name) + SetVariable(output, "TOOLSET", target_toolset) + + cmake_target_name = namer.CreateCMakeTargetName(qualified_target) + + extra_sources = [] + extra_deps = [] + + # Actions must come first, since they can generate more OBJs for use below. + if "actions" in spec: + WriteActions( + cmake_target_name, + spec["actions"], + extra_sources, + extra_deps, + path_from_cmakelists_to_gyp, + output, + ) + + # Rules must be early like actions. + if "rules" in spec: + WriteRules( + cmake_target_name, + spec["rules"], + extra_sources, + extra_deps, + path_from_cmakelists_to_gyp, + output, + ) + + # Copies + if "copies" in spec: + WriteCopies( + cmake_target_name, + spec["copies"], + extra_deps, + path_from_cmakelists_to_gyp, + output, + ) + + # Target and sources + srcs = spec.get("sources", []) + + # Gyp separates the sheep from the goats based on file extensions. + # A full separation is done here because of flag handing (see below). + s_sources = [] + c_sources = [] + cxx_sources = [] + linkable_sources = [] + other_sources = [] + for src in srcs: + _, ext = os.path.splitext(src) + src_type = COMPILABLE_EXTENSIONS.get(ext, None) + src_norm_path = NormjoinPath(path_from_cmakelists_to_gyp, src) + + if src_type == "s": + s_sources.append(src_norm_path) + elif src_type == "cc": + c_sources.append(src_norm_path) + elif src_type == "cxx": + cxx_sources.append(src_norm_path) + elif Linkable(ext): + linkable_sources.append(src_norm_path) + else: + other_sources.append(src_norm_path) + + for extra_source in extra_sources: + src, real_source = extra_source + _, ext = os.path.splitext(real_source) + src_type = COMPILABLE_EXTENSIONS.get(ext, None) + + if src_type == "s": + s_sources.append(src) + elif src_type == "cc": + c_sources.append(src) + elif src_type == "cxx": + cxx_sources.append(src) + elif Linkable(ext): + linkable_sources.append(src) + else: + other_sources.append(src) + + s_sources_name = None + if s_sources: + s_sources_name = cmake_target_name + "__asm_srcs" + SetVariableList(output, s_sources_name, s_sources) + + c_sources_name = None + if c_sources: + c_sources_name = cmake_target_name + "__c_srcs" + SetVariableList(output, c_sources_name, c_sources) + + cxx_sources_name = None + if cxx_sources: + cxx_sources_name = cmake_target_name + "__cxx_srcs" + SetVariableList(output, cxx_sources_name, cxx_sources) + + linkable_sources_name = None + if linkable_sources: + linkable_sources_name = cmake_target_name + "__linkable_srcs" + SetVariableList(output, linkable_sources_name, linkable_sources) + + other_sources_name = None + if other_sources: + other_sources_name = cmake_target_name + "__other_srcs" + SetVariableList(output, other_sources_name, other_sources) + + # CMake gets upset when executable targets provide no sources. + # http://www.cmake.org/pipermail/cmake/2010-July/038461.html + dummy_sources_name = None + has_sources = ( + s_sources_name + or c_sources_name + or cxx_sources_name + or linkable_sources_name + or other_sources_name + ) + if target_type == "executable" and not has_sources: + dummy_sources_name = cmake_target_name + "__dummy_srcs" + SetVariable( + output, dummy_sources_name, "${obj}.${TOOLSET}/${TARGET}/genc/dummy.c" + ) + output.write('if(NOT EXISTS "') + WriteVariable(output, dummy_sources_name) + output.write('")\n') + output.write(' file(WRITE "') + WriteVariable(output, dummy_sources_name) + output.write('" "")\n') + output.write("endif()\n") + + # CMake is opposed to setting linker directories and considers the practice + # of setting linker directories dangerous. Instead, it favors the use of + # find_library and passing absolute paths to target_link_libraries. + # However, CMake does provide the command link_directories, which adds + # link directories to targets defined after it is called. + # As a result, link_directories must come before the target definition. + # CMake unfortunately has no means of removing entries from LINK_DIRECTORIES. + library_dirs = config.get("library_dirs") + if library_dirs is not None: + output.write("link_directories(") + for library_dir in library_dirs: + output.write(" ") + output.write(NormjoinPath(path_from_cmakelists_to_gyp, library_dir)) + output.write("\n") + output.write(")\n") + + output.write(cmake_target_type.command) + output.write("(") + output.write(cmake_target_name) - # Mark object sources as linkable. + if cmake_target_type.modifier is not None: + output.write(" ") + output.write(cmake_target_type.modifier) + + if s_sources_name: + WriteVariable(output, s_sources_name, " ") + if c_sources_name: + WriteVariable(output, c_sources_name, " ") + if cxx_sources_name: + WriteVariable(output, cxx_sources_name, " ") if linkable_sources_name: - output.write('set_source_files_properties(') - WriteVariable(output, other_sources_name, '') - output.write(' PROPERTIES EXTERNAL_OBJECT "TRUE")\n') - - # Output directory - target_output_directory = spec.get('product_dir') - if target_output_directory is None: - if target_type in ('executable', 'loadable_module'): - target_output_directory = generator_default_variables['PRODUCT_DIR'] - elif target_type == 'shared_library': - target_output_directory = '${builddir}/lib.${TOOLSET}' - elif spec.get('standalone_static_library', False): - target_output_directory = generator_default_variables['PRODUCT_DIR'] - else: - base_path = gyp.common.RelativePath(os.path.dirname(gyp_file), - options.toplevel_dir) - target_output_directory = '${obj}.${TOOLSET}' - target_output_directory = ( - os.path.join(target_output_directory, base_path)) - - cmake_target_output_directory = NormjoinPathForceCMakeSource( - path_from_cmakelists_to_gyp, - target_output_directory) - SetTargetProperty(output, - cmake_target_name, - cmake_target_type.property_modifier + '_OUTPUT_DIRECTORY', - cmake_target_output_directory) - - # Output name - default_product_prefix = '' - default_product_name = target_name - default_product_ext = '' - if target_type == 'static_library': - static_library_prefix = generator_default_variables['STATIC_LIB_PREFIX'] - default_product_name = RemovePrefix(default_product_name, - static_library_prefix) - default_product_prefix = static_library_prefix - default_product_ext = generator_default_variables['STATIC_LIB_SUFFIX'] - - elif target_type in ('loadable_module', 'shared_library'): - shared_library_prefix = generator_default_variables['SHARED_LIB_PREFIX'] - default_product_name = RemovePrefix(default_product_name, - shared_library_prefix) - default_product_prefix = shared_library_prefix - default_product_ext = generator_default_variables['SHARED_LIB_SUFFIX'] - - elif target_type != 'executable': - print('ERROR: What output file should be generated?', - 'type', target_type, 'target', target_name) - - product_prefix = spec.get('product_prefix', default_product_prefix) - product_name = spec.get('product_name', default_product_name) - product_ext = spec.get('product_extension') - if product_ext: - product_ext = '.' + product_ext - else: - product_ext = default_product_ext - - SetTargetProperty(output, cmake_target_name, 'PREFIX', product_prefix) - SetTargetProperty(output, cmake_target_name, - cmake_target_type.property_modifier + '_OUTPUT_NAME', - product_name) - SetTargetProperty(output, cmake_target_name, 'SUFFIX', product_ext) - - # Make the output of this target referenceable as a source. - cmake_target_output_basename = product_prefix + product_name + product_ext - cmake_target_output = os.path.join(cmake_target_output_directory, - cmake_target_output_basename) - SetFileProperty(output, cmake_target_output, 'GENERATED', ['TRUE'], '') - - # Includes - includes = config.get('include_dirs') - if includes: - # This (target include directories) is what requires CMake 2.8.8 - includes_name = cmake_target_name + '__include_dirs' - SetVariableList(output, includes_name, - [NormjoinPathForceCMakeSource(path_from_cmakelists_to_gyp, include) - for include in includes]) - output.write('set_property(TARGET ') - output.write(cmake_target_name) - output.write(' APPEND PROPERTY INCLUDE_DIRECTORIES ') - WriteVariable(output, includes_name, '') - output.write(')\n') - - # Defines - defines = config.get('defines') - if defines is not None: - SetTargetProperty(output, - cmake_target_name, - 'COMPILE_DEFINITIONS', - defines, - ';') - - # Compile Flags - http://www.cmake.org/Bug/view.php?id=6493 - # CMake currently does not have target C and CXX flags. - # So, instead of doing... - - # cflags_c = config.get('cflags_c') - # if cflags_c is not None: - # SetTargetProperty(output, cmake_target_name, - # 'C_COMPILE_FLAGS', cflags_c, ' ') - - # cflags_cc = config.get('cflags_cc') - # if cflags_cc is not None: - # SetTargetProperty(output, cmake_target_name, - # 'CXX_COMPILE_FLAGS', cflags_cc, ' ') - - # Instead we must... - cflags = config.get('cflags', []) - cflags_c = config.get('cflags_c', []) - cflags_cxx = config.get('cflags_cc', []) - if xcode_settings: - cflags = xcode_settings.GetCflags(config_to_use) - cflags_c = xcode_settings.GetCflagsC(config_to_use) - cflags_cxx = xcode_settings.GetCflagsCC(config_to_use) - #cflags_objc = xcode_settings.GetCflagsObjC(config_to_use) - #cflags_objcc = xcode_settings.GetCflagsObjCC(config_to_use) - - if (not cflags_c or not c_sources) and (not cflags_cxx or not cxx_sources): - SetTargetProperty(output, cmake_target_name, 'COMPILE_FLAGS', cflags, ' ') - - elif c_sources and not (s_sources or cxx_sources): - flags = [] - flags.extend(cflags) - flags.extend(cflags_c) - SetTargetProperty(output, cmake_target_name, 'COMPILE_FLAGS', flags, ' ') - - elif cxx_sources and not (s_sources or c_sources): - flags = [] - flags.extend(cflags) - flags.extend(cflags_cxx) - SetTargetProperty(output, cmake_target_name, 'COMPILE_FLAGS', flags, ' ') + WriteVariable(output, linkable_sources_name, " ") + if other_sources_name: + WriteVariable(output, other_sources_name, " ") + if dummy_sources_name: + WriteVariable(output, dummy_sources_name, " ") + + output.write(")\n") + + # Let CMake know if the 'all' target should depend on this target. + exclude_from_all = ( + "TRUE" if qualified_target not in all_qualified_targets else "FALSE" + ) + SetTargetProperty(output, cmake_target_name, "EXCLUDE_FROM_ALL", exclude_from_all) + for extra_target_name in extra_deps: + SetTargetProperty( + output, extra_target_name, "EXCLUDE_FROM_ALL", exclude_from_all + ) + + # Output name and location. + if target_type != "none": + # Link as 'C' if there are no other files + if not c_sources and not cxx_sources: + SetTargetProperty(output, cmake_target_name, "LINKER_LANGUAGE", ["C"]) + + # Mark uncompiled sources as uncompiled. + if other_sources_name: + output.write("set_source_files_properties(") + WriteVariable(output, other_sources_name, "") + output.write(' PROPERTIES HEADER_FILE_ONLY "TRUE")\n') + + # Mark object sources as linkable. + if linkable_sources_name: + output.write("set_source_files_properties(") + WriteVariable(output, other_sources_name, "") + output.write(' PROPERTIES EXTERNAL_OBJECT "TRUE")\n') + + # Output directory + target_output_directory = spec.get("product_dir") + if target_output_directory is None: + if target_type in ("executable", "loadable_module"): + target_output_directory = generator_default_variables["PRODUCT_DIR"] + elif target_type == "shared_library": + target_output_directory = "${builddir}/lib.${TOOLSET}" + elif spec.get("standalone_static_library", False): + target_output_directory = generator_default_variables["PRODUCT_DIR"] + else: + base_path = gyp.common.RelativePath( + os.path.dirname(gyp_file), options.toplevel_dir + ) + target_output_directory = "${obj}.${TOOLSET}" + target_output_directory = os.path.join( + target_output_directory, base_path + ) + + cmake_target_output_directory = NormjoinPathForceCMakeSource( + path_from_cmakelists_to_gyp, target_output_directory + ) + SetTargetProperty( + output, + cmake_target_name, + cmake_target_type.property_modifier + "_OUTPUT_DIRECTORY", + cmake_target_output_directory, + ) + + # Output name + default_product_prefix = "" + default_product_name = target_name + default_product_ext = "" + if target_type == "static_library": + static_library_prefix = generator_default_variables["STATIC_LIB_PREFIX"] + default_product_name = RemovePrefix( + default_product_name, static_library_prefix + ) + default_product_prefix = static_library_prefix + default_product_ext = generator_default_variables["STATIC_LIB_SUFFIX"] + + elif target_type in ("loadable_module", "shared_library"): + shared_library_prefix = generator_default_variables["SHARED_LIB_PREFIX"] + default_product_name = RemovePrefix( + default_product_name, shared_library_prefix + ) + default_product_prefix = shared_library_prefix + default_product_ext = generator_default_variables["SHARED_LIB_SUFFIX"] + + elif target_type != "executable": + print( + "ERROR: What output file should be generated?", + "type", + target_type, + "target", + target_name, + ) + + product_prefix = spec.get("product_prefix", default_product_prefix) + product_name = spec.get("product_name", default_product_name) + product_ext = spec.get("product_extension") + if product_ext: + product_ext = "." + product_ext + else: + product_ext = default_product_ext + + SetTargetProperty(output, cmake_target_name, "PREFIX", product_prefix) + SetTargetProperty( + output, + cmake_target_name, + cmake_target_type.property_modifier + "_OUTPUT_NAME", + product_name, + ) + SetTargetProperty(output, cmake_target_name, "SUFFIX", product_ext) + + # Make the output of this target referenceable as a source. + cmake_target_output_basename = product_prefix + product_name + product_ext + cmake_target_output = os.path.join( + cmake_target_output_directory, cmake_target_output_basename + ) + SetFileProperty(output, cmake_target_output, "GENERATED", ["TRUE"], "") + + # Includes + includes = config.get("include_dirs") + if includes: + # This (target include directories) is what requires CMake 2.8.8 + includes_name = cmake_target_name + "__include_dirs" + SetVariableList( + output, + includes_name, + [ + NormjoinPathForceCMakeSource(path_from_cmakelists_to_gyp, include) + for include in includes + ], + ) + output.write("set_property(TARGET ") + output.write(cmake_target_name) + output.write(" APPEND PROPERTY INCLUDE_DIRECTORIES ") + WriteVariable(output, includes_name, "") + output.write(")\n") + + # Defines + defines = config.get("defines") + if defines is not None: + SetTargetProperty( + output, cmake_target_name, "COMPILE_DEFINITIONS", defines, ";" + ) + + # Compile Flags - http://www.cmake.org/Bug/view.php?id=6493 + # CMake currently does not have target C and CXX flags. + # So, instead of doing... + + # cflags_c = config.get('cflags_c') + # if cflags_c is not None: + # SetTargetProperty(output, cmake_target_name, + # 'C_COMPILE_FLAGS', cflags_c, ' ') + + # cflags_cc = config.get('cflags_cc') + # if cflags_cc is not None: + # SetTargetProperty(output, cmake_target_name, + # 'CXX_COMPILE_FLAGS', cflags_cc, ' ') + + # Instead we must... + cflags = config.get("cflags", []) + cflags_c = config.get("cflags_c", []) + cflags_cxx = config.get("cflags_cc", []) + if xcode_settings: + cflags = xcode_settings.GetCflags(config_to_use) + cflags_c = xcode_settings.GetCflagsC(config_to_use) + cflags_cxx = xcode_settings.GetCflagsCC(config_to_use) + # cflags_objc = xcode_settings.GetCflagsObjC(config_to_use) + # cflags_objcc = xcode_settings.GetCflagsObjCC(config_to_use) + + if (not cflags_c or not c_sources) and (not cflags_cxx or not cxx_sources): + SetTargetProperty(output, cmake_target_name, "COMPILE_FLAGS", cflags, " ") + + elif c_sources and not (s_sources or cxx_sources): + flags = [] + flags.extend(cflags) + flags.extend(cflags_c) + SetTargetProperty(output, cmake_target_name, "COMPILE_FLAGS", flags, " ") + + elif cxx_sources and not (s_sources or c_sources): + flags = [] + flags.extend(cflags) + flags.extend(cflags_cxx) + SetTargetProperty(output, cmake_target_name, "COMPILE_FLAGS", flags, " ") + + else: + # TODO: This is broken, one cannot generally set properties on files, + # as other targets may require different properties on the same files. + if s_sources and cflags: + SetFilesProperty(output, s_sources_name, "COMPILE_FLAGS", cflags, " ") + + if c_sources and (cflags or cflags_c): + flags = [] + flags.extend(cflags) + flags.extend(cflags_c) + SetFilesProperty(output, c_sources_name, "COMPILE_FLAGS", flags, " ") + + if cxx_sources and (cflags or cflags_cxx): + flags = [] + flags.extend(cflags) + flags.extend(cflags_cxx) + SetFilesProperty(output, cxx_sources_name, "COMPILE_FLAGS", flags, " ") + + # Linker flags + ldflags = config.get("ldflags") + if ldflags is not None: + SetTargetProperty(output, cmake_target_name, "LINK_FLAGS", ldflags, " ") + + # XCode settings + xcode_settings = config.get("xcode_settings", {}) + for xcode_setting, xcode_value in xcode_settings.viewitems(): + SetTargetProperty( + output, + cmake_target_name, + "XCODE_ATTRIBUTE_%s" % xcode_setting, + xcode_value, + "" if isinstance(xcode_value, str) else " ", + ) + + # Note on Dependencies and Libraries: + # CMake wants to handle link order, resolving the link line up front. + # Gyp does not retain or enforce specifying enough information to do so. + # So do as other gyp generators and use --start-group and --end-group. + # Give CMake as little information as possible so that it doesn't mess it up. + + # Dependencies + rawDeps = spec.get("dependencies", []) + + static_deps = [] + shared_deps = [] + other_deps = [] + for rawDep in rawDeps: + dep_cmake_name = namer.CreateCMakeTargetName(rawDep) + dep_spec = target_dicts.get(rawDep, {}) + dep_target_type = dep_spec.get("type", None) + + if dep_target_type == "static_library": + static_deps.append(dep_cmake_name) + elif dep_target_type == "shared_library": + shared_deps.append(dep_cmake_name) + else: + other_deps.append(dep_cmake_name) + + # ensure all external dependencies are complete before internal dependencies + # extra_deps currently only depend on their own deps, so otherwise run early + if static_deps or shared_deps or other_deps: + for extra_dep in extra_deps: + output.write("add_dependencies(") + output.write(extra_dep) + output.write("\n") + for deps in (static_deps, shared_deps, other_deps): + for dep in gyp.common.uniquer(deps): + output.write(" ") + output.write(dep) + output.write("\n") + output.write(")\n") + + linkable = target_type in ("executable", "loadable_module", "shared_library") + other_deps.extend(extra_deps) + if other_deps or (not linkable and (static_deps or shared_deps)): + output.write("add_dependencies(") + output.write(cmake_target_name) + output.write("\n") + for dep in gyp.common.uniquer(other_deps): + output.write(" ") + output.write(dep) + output.write("\n") + if not linkable: + for deps in (static_deps, shared_deps): + for lib_dep in gyp.common.uniquer(deps): + output.write(" ") + output.write(lib_dep) + output.write("\n") + output.write(")\n") + + # Libraries + if linkable: + external_libs = [lib for lib in spec.get("libraries", []) if len(lib) > 0] + if external_libs or static_deps or shared_deps: + output.write("target_link_libraries(") + output.write(cmake_target_name) + output.write("\n") + if static_deps: + write_group = circular_libs and len(static_deps) > 1 and flavor != "mac" + if write_group: + output.write("-Wl,--start-group\n") + for dep in gyp.common.uniquer(static_deps): + output.write(" ") + output.write(dep) + output.write("\n") + if write_group: + output.write("-Wl,--end-group\n") + if shared_deps: + for dep in gyp.common.uniquer(shared_deps): + output.write(" ") + output.write(dep) + output.write("\n") + if external_libs: + for lib in gyp.common.uniquer(external_libs): + output.write(' "') + output.write(RemovePrefix(lib, "$(SDKROOT)")) + output.write('"\n') + + output.write(")\n") + + UnsetVariable(output, "TOOLSET") + UnsetVariable(output, "TARGET") + + +def GenerateOutputForConfig(target_list, target_dicts, data, params, config_to_use): + options = params["options"] + generator_flags = params["generator_flags"] + flavor = gyp.common.GetFlavor(params) + + # generator_dir: relative path from pwd to where make puts build files. + # Makes migrating from make to cmake easier, cmake doesn't put anything here. + # Each Gyp configuration creates a different CMakeLists.txt file + # to avoid incompatibilities between Gyp and CMake configurations. + generator_dir = os.path.relpath(options.generator_output or ".") + + # output_dir: relative path from generator_dir to the build directory. + output_dir = generator_flags.get("output_dir", "out") - else: - # TODO: This is broken, one cannot generally set properties on files, - # as other targets may require different properties on the same files. - if s_sources and cflags: - SetFilesProperty(output, s_sources_name, 'COMPILE_FLAGS', cflags, ' ') - - if c_sources and (cflags or cflags_c): - flags = [] - flags.extend(cflags) - flags.extend(cflags_c) - SetFilesProperty(output, c_sources_name, 'COMPILE_FLAGS', flags, ' ') - - if cxx_sources and (cflags or cflags_cxx): - flags = [] - flags.extend(cflags) - flags.extend(cflags_cxx) - SetFilesProperty(output, cxx_sources_name, 'COMPILE_FLAGS', flags, ' ') - - # Linker flags - ldflags = config.get('ldflags') - if ldflags is not None: - SetTargetProperty(output, cmake_target_name, 'LINK_FLAGS', ldflags, ' ') - - # XCode settings - xcode_settings = config.get('xcode_settings', {}) - for xcode_setting, xcode_value in xcode_settings.viewitems(): - SetTargetProperty(output, cmake_target_name, - "XCODE_ATTRIBUTE_%s" % xcode_setting, xcode_value, - '' if isinstance(xcode_value, str) else ' ') - - # Note on Dependencies and Libraries: - # CMake wants to handle link order, resolving the link line up front. - # Gyp does not retain or enforce specifying enough information to do so. - # So do as other gyp generators and use --start-group and --end-group. - # Give CMake as little information as possible so that it doesn't mess it up. - - # Dependencies - rawDeps = spec.get('dependencies', []) - - static_deps = [] - shared_deps = [] - other_deps = [] - for rawDep in rawDeps: - dep_cmake_name = namer.CreateCMakeTargetName(rawDep) - dep_spec = target_dicts.get(rawDep, {}) - dep_target_type = dep_spec.get('type', None) - - if dep_target_type == 'static_library': - static_deps.append(dep_cmake_name) - elif dep_target_type == 'shared_library': - shared_deps.append(dep_cmake_name) - else: - other_deps.append(dep_cmake_name) - - # ensure all external dependencies are complete before internal dependencies - # extra_deps currently only depend on their own deps, so otherwise run early - if static_deps or shared_deps or other_deps: - for extra_dep in extra_deps: - output.write('add_dependencies(') - output.write(extra_dep) - output.write('\n') - for deps in (static_deps, shared_deps, other_deps): - for dep in gyp.common.uniquer(deps): - output.write(' ') - output.write(dep) - output.write('\n') - output.write(')\n') - - linkable = target_type in ('executable', 'loadable_module', 'shared_library') - other_deps.extend(extra_deps) - if other_deps or (not linkable and (static_deps or shared_deps)): - output.write('add_dependencies(') - output.write(cmake_target_name) - output.write('\n') - for dep in gyp.common.uniquer(other_deps): - output.write(' ') - output.write(dep) - output.write('\n') - if not linkable: - for deps in (static_deps, shared_deps): - for lib_dep in gyp.common.uniquer(deps): - output.write(' ') - output.write(lib_dep) - output.write('\n') - output.write(')\n') - - # Libraries - if linkable: - external_libs = [lib for lib in spec.get('libraries', []) if len(lib) > 0] - if external_libs or static_deps or shared_deps: - output.write('target_link_libraries(') - output.write(cmake_target_name) - output.write('\n') - if static_deps: - write_group = circular_libs and len(static_deps) > 1 and flavor != 'mac' - if write_group: - output.write('-Wl,--start-group\n') - for dep in gyp.common.uniquer(static_deps): - output.write(' ') - output.write(dep) - output.write('\n') - if write_group: - output.write('-Wl,--end-group\n') - if shared_deps: - for dep in gyp.common.uniquer(shared_deps): - output.write(' ') - output.write(dep) - output.write('\n') - if external_libs: - for lib in gyp.common.uniquer(external_libs): - output.write(' "') - output.write(RemovePrefix(lib, "$(SDKROOT)")) - output.write('"\n') - - output.write(')\n') - - UnsetVariable(output, 'TOOLSET') - UnsetVariable(output, 'TARGET') - - -def GenerateOutputForConfig(target_list, target_dicts, data, - params, config_to_use): - options = params['options'] - generator_flags = params['generator_flags'] - flavor = gyp.common.GetFlavor(params) - - # generator_dir: relative path from pwd to where make puts build files. - # Makes migrating from make to cmake easier, cmake doesn't put anything here. - # Each Gyp configuration creates a different CMakeLists.txt file - # to avoid incompatibilities between Gyp and CMake configurations. - generator_dir = os.path.relpath(options.generator_output or '.') - - # output_dir: relative path from generator_dir to the build directory. - output_dir = generator_flags.get('output_dir', 'out') - - # build_dir: relative path from source root to our output files. - # e.g. "out/Debug" - build_dir = os.path.normpath(os.path.join(generator_dir, - output_dir, - config_to_use)) - - toplevel_build = os.path.join(options.toplevel_dir, build_dir) - - output_file = os.path.join(toplevel_build, 'CMakeLists.txt') - gyp.common.EnsureDirExists(output_file) - - output = open(output_file, 'w') - output.write('cmake_minimum_required(VERSION 2.8.8 FATAL_ERROR)\n') - output.write('cmake_policy(VERSION 2.8.8)\n') - - gyp_file, project_target, _ = gyp.common.ParseQualifiedTarget(target_list[-1]) - output.write('project(') - output.write(project_target) - output.write(')\n') - - SetVariable(output, 'configuration', config_to_use) - - ar = None - cc = None - cxx = None - - make_global_settings = data[gyp_file].get('make_global_settings', []) - build_to_top = gyp.common.InvertRelativePath(build_dir, - options.toplevel_dir) - for key, value in make_global_settings: - if key == 'AR': - ar = os.path.join(build_to_top, value) - if key == 'CC': - cc = os.path.join(build_to_top, value) - if key == 'CXX': - cxx = os.path.join(build_to_top, value) - - ar = gyp.common.GetEnvironFallback(['AR_target', 'AR'], ar) - cc = gyp.common.GetEnvironFallback(['CC_target', 'CC'], cc) - cxx = gyp.common.GetEnvironFallback(['CXX_target', 'CXX'], cxx) - - if ar: - SetVariable(output, 'CMAKE_AR', ar) - if cc: - SetVariable(output, 'CMAKE_C_COMPILER', cc) - if cxx: - SetVariable(output, 'CMAKE_CXX_COMPILER', cxx) - - # The following appears to be as-yet undocumented. - # http://public.kitware.com/Bug/view.php?id=8392 - output.write('enable_language(ASM)\n') - # ASM-ATT does not support .S files. - # output.write('enable_language(ASM-ATT)\n') - - if cc: - SetVariable(output, 'CMAKE_ASM_COMPILER', cc) - - SetVariable(output, 'builddir', '${CMAKE_CURRENT_BINARY_DIR}') - SetVariable(output, 'obj', '${builddir}/obj') - output.write('\n') - - # TODO: Undocumented/unsupported (the CMake Java generator depends on it). - # CMake by default names the object resulting from foo.c to be foo.c.o. - # Gyp traditionally names the object resulting from foo.c foo.o. - # This should be irrelevant, but some targets extract .o files from .a - # and depend on the name of the extracted .o files. - output.write('set(CMAKE_C_OUTPUT_EXTENSION_REPLACE 1)\n') - output.write('set(CMAKE_CXX_OUTPUT_EXTENSION_REPLACE 1)\n') - output.write('\n') - - # Force ninja to use rsp files. Otherwise link and ar lines can get too long, - # resulting in 'Argument list too long' errors. - # However, rsp files don't work correctly on Mac. - if flavor != 'mac': - output.write('set(CMAKE_NINJA_FORCE_RESPONSE_FILE 1)\n') - output.write('\n') - - namer = CMakeNamer(target_list) - - # The list of targets upon which the 'all' target should depend. - # CMake has it's own implicit 'all' target, one is not created explicitly. - all_qualified_targets = set() - for build_file in params['build_files']: - for qualified_target in gyp.common.AllTargets(target_list, - target_dicts, - os.path.normpath(build_file)): - all_qualified_targets.add(qualified_target) - - for qualified_target in target_list: - if flavor == 'mac': - gyp_file, _, _ = gyp.common.ParseQualifiedTarget(qualified_target) - spec = target_dicts[qualified_target] - gyp.xcode_emulation.MergeGlobalXcodeSettingsToSpec(data[gyp_file], spec) - - WriteTarget(namer, qualified_target, target_dicts, build_dir, config_to_use, - options, generator_flags, all_qualified_targets, flavor, output) - - output.close() + # build_dir: relative path from source root to our output files. + # e.g. "out/Debug" + build_dir = os.path.normpath(os.path.join(generator_dir, output_dir, config_to_use)) + + toplevel_build = os.path.join(options.toplevel_dir, build_dir) + + output_file = os.path.join(toplevel_build, "CMakeLists.txt") + gyp.common.EnsureDirExists(output_file) + + output = open(output_file, "w") + output.write("cmake_minimum_required(VERSION 2.8.8 FATAL_ERROR)\n") + output.write("cmake_policy(VERSION 2.8.8)\n") + + gyp_file, project_target, _ = gyp.common.ParseQualifiedTarget(target_list[-1]) + output.write("project(") + output.write(project_target) + output.write(")\n") + + SetVariable(output, "configuration", config_to_use) + + ar = None + cc = None + cxx = None + + make_global_settings = data[gyp_file].get("make_global_settings", []) + build_to_top = gyp.common.InvertRelativePath(build_dir, options.toplevel_dir) + for key, value in make_global_settings: + if key == "AR": + ar = os.path.join(build_to_top, value) + if key == "CC": + cc = os.path.join(build_to_top, value) + if key == "CXX": + cxx = os.path.join(build_to_top, value) + + ar = gyp.common.GetEnvironFallback(["AR_target", "AR"], ar) + cc = gyp.common.GetEnvironFallback(["CC_target", "CC"], cc) + cxx = gyp.common.GetEnvironFallback(["CXX_target", "CXX"], cxx) + + if ar: + SetVariable(output, "CMAKE_AR", ar) + if cc: + SetVariable(output, "CMAKE_C_COMPILER", cc) + if cxx: + SetVariable(output, "CMAKE_CXX_COMPILER", cxx) + + # The following appears to be as-yet undocumented. + # http://public.kitware.com/Bug/view.php?id=8392 + output.write("enable_language(ASM)\n") + # ASM-ATT does not support .S files. + # output.write('enable_language(ASM-ATT)\n') + + if cc: + SetVariable(output, "CMAKE_ASM_COMPILER", cc) + + SetVariable(output, "builddir", "${CMAKE_CURRENT_BINARY_DIR}") + SetVariable(output, "obj", "${builddir}/obj") + output.write("\n") + + # TODO: Undocumented/unsupported (the CMake Java generator depends on it). + # CMake by default names the object resulting from foo.c to be foo.c.o. + # Gyp traditionally names the object resulting from foo.c foo.o. + # This should be irrelevant, but some targets extract .o files from .a + # and depend on the name of the extracted .o files. + output.write("set(CMAKE_C_OUTPUT_EXTENSION_REPLACE 1)\n") + output.write("set(CMAKE_CXX_OUTPUT_EXTENSION_REPLACE 1)\n") + output.write("\n") + + # Force ninja to use rsp files. Otherwise link and ar lines can get too long, + # resulting in 'Argument list too long' errors. + # However, rsp files don't work correctly on Mac. + if flavor != "mac": + output.write("set(CMAKE_NINJA_FORCE_RESPONSE_FILE 1)\n") + output.write("\n") + + namer = CMakeNamer(target_list) + + # The list of targets upon which the 'all' target should depend. + # CMake has it's own implicit 'all' target, one is not created explicitly. + all_qualified_targets = set() + for build_file in params["build_files"]: + for qualified_target in gyp.common.AllTargets( + target_list, target_dicts, os.path.normpath(build_file) + ): + all_qualified_targets.add(qualified_target) + + for qualified_target in target_list: + if flavor == "mac": + gyp_file, _, _ = gyp.common.ParseQualifiedTarget(qualified_target) + spec = target_dicts[qualified_target] + gyp.xcode_emulation.MergeGlobalXcodeSettingsToSpec(data[gyp_file], spec) + + WriteTarget( + namer, + qualified_target, + target_dicts, + build_dir, + config_to_use, + options, + generator_flags, + all_qualified_targets, + flavor, + output, + ) + + output.close() def PerformBuild(data, configurations, params): - options = params['options'] - generator_flags = params['generator_flags'] + options = params["options"] + generator_flags = params["generator_flags"] - # generator_dir: relative path from pwd to where make puts build files. - # Makes migrating from make to cmake easier, cmake doesn't put anything here. - generator_dir = os.path.relpath(options.generator_output or '.') + # generator_dir: relative path from pwd to where make puts build files. + # Makes migrating from make to cmake easier, cmake doesn't put anything here. + generator_dir = os.path.relpath(options.generator_output or ".") - # output_dir: relative path from generator_dir to the build directory. - output_dir = generator_flags.get('output_dir', 'out') + # output_dir: relative path from generator_dir to the build directory. + output_dir = generator_flags.get("output_dir", "out") - for config_name in configurations: - # build_dir: relative path from source root to our output files. - # e.g. "out/Debug" - build_dir = os.path.normpath(os.path.join(generator_dir, - output_dir, - config_name)) - arguments = ['cmake', '-G', 'Ninja'] - print('Generating [%s]: %s' % (config_name, arguments)) - subprocess.check_call(arguments, cwd=build_dir) + for config_name in configurations: + # build_dir: relative path from source root to our output files. + # e.g. "out/Debug" + build_dir = os.path.normpath( + os.path.join(generator_dir, output_dir, config_name) + ) + arguments = ["cmake", "-G", "Ninja"] + print("Generating [%s]: %s" % (config_name, arguments)) + subprocess.check_call(arguments, cwd=build_dir) - arguments = ['ninja', '-C', build_dir] - print('Building [%s]: %s' % (config_name, arguments)) - subprocess.check_call(arguments) + arguments = ["ninja", "-C", build_dir] + print("Building [%s]: %s" % (config_name, arguments)) + subprocess.check_call(arguments) def CallGenerateOutputForConfig(arglist): - # Ignore the interrupt signal so that the parent process catches it and - # kills all multiprocessing children. - signal.signal(signal.SIGINT, signal.SIG_IGN) + # Ignore the interrupt signal so that the parent process catches it and + # kills all multiprocessing children. + signal.signal(signal.SIGINT, signal.SIG_IGN) - target_list, target_dicts, data, params, config_name = arglist - GenerateOutputForConfig(target_list, target_dicts, data, params, config_name) + target_list, target_dicts, data, params, config_name = arglist + GenerateOutputForConfig(target_list, target_dicts, data, params, config_name) def GenerateOutput(target_list, target_dicts, data, params): - user_config = params.get('generator_flags', {}).get('config', None) - if user_config: - GenerateOutputForConfig(target_list, target_dicts, data, - params, user_config) - else: - config_names = target_dicts[target_list[0]]['configurations'].keys() - if params['parallel']: - try: - pool = multiprocessing.Pool(len(config_names)) - arglists = [] - for config_name in config_names: - arglists.append((target_list, target_dicts, data, - params, config_name)) - pool.map(CallGenerateOutputForConfig, arglists) - except KeyboardInterrupt as e: - pool.terminate() - raise e + user_config = params.get("generator_flags", {}).get("config", None) + if user_config: + GenerateOutputForConfig(target_list, target_dicts, data, params, user_config) else: - for config_name in config_names: - GenerateOutputForConfig(target_list, target_dicts, data, - params, config_name) + config_names = target_dicts[target_list[0]]["configurations"] + if params["parallel"]: + try: + pool = multiprocessing.Pool(len(config_names)) + arglists = [] + for config_name in config_names: + arglists.append( + (target_list, target_dicts, data, params, config_name) + ) + pool.map(CallGenerateOutputForConfig, arglists) + except KeyboardInterrupt as e: + pool.terminate() + raise e + else: + for config_name in config_names: + GenerateOutputForConfig( + target_list, target_dicts, data, params, config_name + ) diff --git a/tools/gyp/pylib/gyp/generator/compile_commands_json.py b/tools/gyp/pylib/gyp/generator/compile_commands_json.py index 1b8490451f9793..f330a04dea4c53 100644 --- a/tools/gyp/pylib/gyp/generator/compile_commands_json.py +++ b/tools/gyp/pylib/gyp/generator/compile_commands_json.py @@ -16,100 +16,105 @@ # Lifted from make.py. The actual values don't matter much. generator_default_variables = { - 'CONFIGURATION_NAME': '$(BUILDTYPE)', - 'EXECUTABLE_PREFIX': '', - 'EXECUTABLE_SUFFIX': '', - 'INTERMEDIATE_DIR': '$(obj).$(TOOLSET)/$(TARGET)/geni', - 'PRODUCT_DIR': '$(builddir)', - 'RULE_INPUT_DIRNAME': '%(INPUT_DIRNAME)s', - 'RULE_INPUT_EXT': '$(suffix $<)', - 'RULE_INPUT_NAME': '$(notdir $<)', - 'RULE_INPUT_PATH': '$(abspath $<)', - 'RULE_INPUT_ROOT': '%(INPUT_ROOT)s', - 'SHARED_INTERMEDIATE_DIR': '$(obj)/gen', - 'SHARED_LIB_PREFIX': 'lib', - 'STATIC_LIB_PREFIX': 'lib', - 'STATIC_LIB_SUFFIX': '.a', + "CONFIGURATION_NAME": "$(BUILDTYPE)", + "EXECUTABLE_PREFIX": "", + "EXECUTABLE_SUFFIX": "", + "INTERMEDIATE_DIR": "$(obj).$(TOOLSET)/$(TARGET)/geni", + "PRODUCT_DIR": "$(builddir)", + "RULE_INPUT_DIRNAME": "%(INPUT_DIRNAME)s", + "RULE_INPUT_EXT": "$(suffix $<)", + "RULE_INPUT_NAME": "$(notdir $<)", + "RULE_INPUT_PATH": "$(abspath $<)", + "RULE_INPUT_ROOT": "%(INPUT_ROOT)s", + "SHARED_INTERMEDIATE_DIR": "$(obj)/gen", + "SHARED_LIB_PREFIX": "lib", + "STATIC_LIB_PREFIX": "lib", + "STATIC_LIB_SUFFIX": ".a", } def IsMac(params): - return 'mac' == gyp.common.GetFlavor(params) + return "mac" == gyp.common.GetFlavor(params) def CalculateVariables(default_variables, params): - default_variables.setdefault('OS', gyp.common.GetFlavor(params)) + default_variables.setdefault("OS", gyp.common.GetFlavor(params)) def AddCommandsForTarget(cwd, target, params, per_config_commands): - output_dir = params['generator_flags']['output_dir'] - for configuration_name, configuration in target['configurations'].items(): - builddir_name = os.path.join(output_dir, configuration_name) - - if IsMac(params): - xcode_settings = gyp.xcode_emulation.XcodeSettings(target) - cflags = xcode_settings.GetCflags(configuration_name) - cflags_c = xcode_settings.GetCflagsC(configuration_name) - cflags_cc = xcode_settings.GetCflagsCC(configuration_name) - else: - cflags = configuration.get('cflags', []) - cflags_c = configuration.get('cflags_c', []) - cflags_cc = configuration.get('cflags_cc', []) - - cflags_c = cflags + cflags_c - cflags_cc = cflags + cflags_cc - - defines = configuration.get('defines', []) - defines = ['-D' + s for s in defines] - - # TODO(bnoordhuis) Handle generated source files. - sources = target.get('sources', []) - sources = [s for s in sources if s.endswith('.c') or s.endswith('.cc')] - - def resolve(filename): - return os.path.abspath(os.path.join(cwd, filename)) - - # TODO(bnoordhuis) Handle generated header files. - include_dirs = configuration.get('include_dirs', []) - include_dirs = [s for s in include_dirs if not s.startswith('$(obj)')] - includes = ['-I' + resolve(s) for s in include_dirs] - - defines = gyp.common.EncodePOSIXShellList(defines) - includes = gyp.common.EncodePOSIXShellList(includes) - cflags_c = gyp.common.EncodePOSIXShellList(cflags_c) - cflags_cc = gyp.common.EncodePOSIXShellList(cflags_cc) - - commands = per_config_commands.setdefault(configuration_name, []) - for source in sources: - file = resolve(source) - isc = source.endswith('.c') - cc = 'cc' if isc else 'c++' - cflags = cflags_c if isc else cflags_cc - command = ' '.join((cc, defines, includes, cflags, - '-c', gyp.common.EncodePOSIXShellArgument(file))) - commands.append(dict(command=command, directory=output_dir, file=file)) + output_dir = params["generator_flags"].get("output_dir", "out") + for configuration_name, configuration in target["configurations"].items(): + if IsMac(params): + xcode_settings = gyp.xcode_emulation.XcodeSettings(target) + cflags = xcode_settings.GetCflags(configuration_name) + cflags_c = xcode_settings.GetCflagsC(configuration_name) + cflags_cc = xcode_settings.GetCflagsCC(configuration_name) + else: + cflags = configuration.get("cflags", []) + cflags_c = configuration.get("cflags_c", []) + cflags_cc = configuration.get("cflags_cc", []) + + cflags_c = cflags + cflags_c + cflags_cc = cflags + cflags_cc + + defines = configuration.get("defines", []) + defines = ["-D" + s for s in defines] + + # TODO(bnoordhuis) Handle generated source files. + extensions = (".c", ".cc", ".cpp", ".cxx") + sources = [s for s in target.get("sources", []) if s.endswith(extensions)] + + def resolve(filename): + return os.path.abspath(os.path.join(cwd, filename)) + + # TODO(bnoordhuis) Handle generated header files. + include_dirs = configuration.get("include_dirs", []) + include_dirs = [s for s in include_dirs if not s.startswith("$(obj)")] + includes = ["-I" + resolve(s) for s in include_dirs] + + defines = gyp.common.EncodePOSIXShellList(defines) + includes = gyp.common.EncodePOSIXShellList(includes) + cflags_c = gyp.common.EncodePOSIXShellList(cflags_c) + cflags_cc = gyp.common.EncodePOSIXShellList(cflags_cc) + + commands = per_config_commands.setdefault(configuration_name, []) + for source in sources: + file = resolve(source) + isc = source.endswith(".c") + cc = "cc" if isc else "c++" + cflags = cflags_c if isc else cflags_cc + command = " ".join( + ( + cc, + defines, + includes, + cflags, + "-c", + gyp.common.EncodePOSIXShellArgument(file), + ) + ) + commands.append(dict(command=command, directory=output_dir, file=file)) def GenerateOutput(target_list, target_dicts, data, params): - per_config_commands = {} - for qualified_target, target in target_dicts.items(): - build_file, target_name, toolset = ( - gyp.common.ParseQualifiedTarget(qualified_target)) - if IsMac(params): - settings = data[build_file] - gyp.xcode_emulation.MergeGlobalXcodeSettingsToSpec(settings, target) - cwd = os.path.dirname(build_file) - AddCommandsForTarget(cwd, target, params, per_config_commands) - - output_dir = params['generator_flags']['output_dir'] - for configuration_name, commands in per_config_commands.items(): - filename = os.path.join(output_dir, - configuration_name, - 'compile_commands.json') - gyp.common.EnsureDirExists(filename) - fp = open(filename, 'w') - json.dump(commands, fp=fp, indent=0, check_circular=False) + per_config_commands = {} + for qualified_target, target in target_dicts.items(): + build_file, target_name, toolset = gyp.common.ParseQualifiedTarget( + qualified_target + ) + if IsMac(params): + settings = data[build_file] + gyp.xcode_emulation.MergeGlobalXcodeSettingsToSpec(settings, target) + cwd = os.path.dirname(build_file) + AddCommandsForTarget(cwd, target, params, per_config_commands) + + output_dir = params["generator_flags"].get("output_dir", "out") + for configuration_name, commands in per_config_commands.items(): + filename = os.path.join(output_dir, configuration_name, "compile_commands.json") + gyp.common.EnsureDirExists(filename) + fp = open(filename, "w") + json.dump(commands, fp=fp, indent=0, check_circular=False) def PerformBuild(data, configurations, params): - pass + pass diff --git a/tools/gyp/pylib/gyp/generator/dump_dependency_json.py b/tools/gyp/pylib/gyp/generator/dump_dependency_json.py index 8e4f3168f3e7ef..46f68e038418f3 100644 --- a/tools/gyp/pylib/gyp/generator/dump_dependency_json.py +++ b/tools/gyp/pylib/gyp/generator/dump_dependency_json.py @@ -1,100 +1,104 @@ -from __future__ import print_function # Copyright (c) 2012 Google Inc. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. -import collections +from __future__ import print_function + import os import gyp import gyp.common import gyp.msvs_emulation import json -import sys generator_supports_multiple_toolsets = True generator_wants_static_library_dependencies_adjusted = False -generator_filelist_paths = { -} - -generator_default_variables = { -} -for dirname in ['INTERMEDIATE_DIR', 'SHARED_INTERMEDIATE_DIR', 'PRODUCT_DIR', - 'LIB_DIR', 'SHARED_LIB_DIR']: - # Some gyp steps fail if these are empty(!). - generator_default_variables[dirname] = 'dir' -for unused in ['RULE_INPUT_PATH', 'RULE_INPUT_ROOT', 'RULE_INPUT_NAME', - 'RULE_INPUT_DIRNAME', 'RULE_INPUT_EXT', - 'EXECUTABLE_PREFIX', 'EXECUTABLE_SUFFIX', - 'STATIC_LIB_PREFIX', 'STATIC_LIB_SUFFIX', - 'SHARED_LIB_PREFIX', 'SHARED_LIB_SUFFIX', - 'CONFIGURATION_NAME']: - generator_default_variables[unused] = '' +generator_filelist_paths = {} + +generator_default_variables = {} +for dirname in [ + "INTERMEDIATE_DIR", + "SHARED_INTERMEDIATE_DIR", + "PRODUCT_DIR", + "LIB_DIR", + "SHARED_LIB_DIR", +]: + # Some gyp steps fail if these are empty(!). + generator_default_variables[dirname] = "dir" +for unused in [ + "RULE_INPUT_PATH", + "RULE_INPUT_ROOT", + "RULE_INPUT_NAME", + "RULE_INPUT_DIRNAME", + "RULE_INPUT_EXT", + "EXECUTABLE_PREFIX", + "EXECUTABLE_SUFFIX", + "STATIC_LIB_PREFIX", + "STATIC_LIB_SUFFIX", + "SHARED_LIB_PREFIX", + "SHARED_LIB_SUFFIX", + "CONFIGURATION_NAME", +]: + generator_default_variables[unused] = "" def CalculateVariables(default_variables, params): - generator_flags = params.get('generator_flags', {}) - for key, val in generator_flags.items(): - default_variables.setdefault(key, val) - default_variables.setdefault('OS', gyp.common.GetFlavor(params)) - - flavor = gyp.common.GetFlavor(params) - if flavor =='win': - # Copy additional generator configuration data from VS, which is shared - # by the Windows Ninja generator. - import gyp.generator.msvs as msvs_generator - generator_additional_non_configuration_keys = getattr(msvs_generator, - 'generator_additional_non_configuration_keys', []) - generator_additional_path_sections = getattr(msvs_generator, - 'generator_additional_path_sections', []) + generator_flags = params.get("generator_flags", {}) + for key, val in generator_flags.items(): + default_variables.setdefault(key, val) + default_variables.setdefault("OS", gyp.common.GetFlavor(params)) - gyp.msvs_emulation.CalculateCommonVariables(default_variables, params) + flavor = gyp.common.GetFlavor(params) + if flavor == "win": + gyp.msvs_emulation.CalculateCommonVariables(default_variables, params) def CalculateGeneratorInputInfo(params): - """Calculate the generator specific info that gets fed to input (called by + """Calculate the generator specific info that gets fed to input (called by gyp).""" - generator_flags = params.get('generator_flags', {}) - if generator_flags.get('adjust_static_libraries', False): - global generator_wants_static_library_dependencies_adjusted - generator_wants_static_library_dependencies_adjusted = True - - toplevel = params['options'].toplevel_dir - generator_dir = os.path.relpath(params['options'].generator_output or '.') - # output_dir: relative path from generator_dir to the build directory. - output_dir = generator_flags.get('output_dir', 'out') - qualified_out_dir = os.path.normpath(os.path.join( - toplevel, generator_dir, output_dir, 'gypfiles')) - global generator_filelist_paths - generator_filelist_paths = { - 'toplevel': toplevel, - 'qualified_out_dir': qualified_out_dir, - } + generator_flags = params.get("generator_flags", {}) + if generator_flags.get("adjust_static_libraries", False): + global generator_wants_static_library_dependencies_adjusted + generator_wants_static_library_dependencies_adjusted = True + + toplevel = params["options"].toplevel_dir + generator_dir = os.path.relpath(params["options"].generator_output or ".") + # output_dir: relative path from generator_dir to the build directory. + output_dir = generator_flags.get("output_dir", "out") + qualified_out_dir = os.path.normpath( + os.path.join(toplevel, generator_dir, output_dir, "gypfiles") + ) + global generator_filelist_paths + generator_filelist_paths = { + "toplevel": toplevel, + "qualified_out_dir": qualified_out_dir, + } + def GenerateOutput(target_list, target_dicts, data, params): - # Map of target -> list of targets it depends on. - edges = {} - - # Queue of targets to visit. - targets_to_visit = target_list[:] - - while len(targets_to_visit) > 0: - target = targets_to_visit.pop() - if target in edges: - continue - edges[target] = [] - - for dep in target_dicts[target].get('dependencies', []): - edges[target].append(dep) - targets_to_visit.append(dep) - - try: - filepath = params['generator_flags']['output_dir'] - except KeyError: - filepath = '.' - filename = os.path.join(filepath, 'dump.json') - f = open(filename, 'w') - json.dump(edges, f) - f.close() - print('Wrote json to %s.' % filename) + # Map of target -> list of targets it depends on. + edges = {} + + # Queue of targets to visit. + targets_to_visit = target_list[:] + + while len(targets_to_visit) > 0: + target = targets_to_visit.pop() + if target in edges: + continue + edges[target] = [] + + for dep in target_dicts[target].get("dependencies", []): + edges[target].append(dep) + targets_to_visit.append(dep) + + try: + filepath = params["generator_flags"]["output_dir"] + except KeyError: + filepath = "." + filename = os.path.join(filepath, "dump.json") + f = open(filename, "w") + json.dump(edges, f) + f.close() + print("Wrote json to %s." % filename) diff --git a/tools/gyp/pylib/gyp/generator/eclipse.py b/tools/gyp/pylib/gyp/generator/eclipse.py index 80e5fb6302c5d5..4bd49725dc1e89 100644 --- a/tools/gyp/pylib/gyp/generator/eclipse.py +++ b/tools/gyp/pylib/gyp/generator/eclipse.py @@ -30,402 +30,441 @@ generator_wants_static_library_dependencies_adjusted = False -generator_default_variables = { -} - -for dirname in ['INTERMEDIATE_DIR', 'PRODUCT_DIR', 'LIB_DIR', 'SHARED_LIB_DIR']: - # Some gyp steps fail if these are empty(!), so we convert them to variables - generator_default_variables[dirname] = '$' + dirname - -for unused in ['RULE_INPUT_PATH', 'RULE_INPUT_ROOT', 'RULE_INPUT_NAME', - 'RULE_INPUT_DIRNAME', 'RULE_INPUT_EXT', - 'EXECUTABLE_PREFIX', 'EXECUTABLE_SUFFIX', - 'STATIC_LIB_PREFIX', 'STATIC_LIB_SUFFIX', - 'SHARED_LIB_PREFIX', 'SHARED_LIB_SUFFIX', - 'CONFIGURATION_NAME']: - generator_default_variables[unused] = '' +generator_default_variables = {} + +for dirname in ["INTERMEDIATE_DIR", "PRODUCT_DIR", "LIB_DIR", "SHARED_LIB_DIR"]: + # Some gyp steps fail if these are empty(!), so we convert them to variables + generator_default_variables[dirname] = "$" + dirname + +for unused in [ + "RULE_INPUT_PATH", + "RULE_INPUT_ROOT", + "RULE_INPUT_NAME", + "RULE_INPUT_DIRNAME", + "RULE_INPUT_EXT", + "EXECUTABLE_PREFIX", + "EXECUTABLE_SUFFIX", + "STATIC_LIB_PREFIX", + "STATIC_LIB_SUFFIX", + "SHARED_LIB_PREFIX", + "SHARED_LIB_SUFFIX", + "CONFIGURATION_NAME", +]: + generator_default_variables[unused] = "" # Include dirs will occasionally use the SHARED_INTERMEDIATE_DIR variable as # part of the path when dealing with generated headers. This value will be # replaced dynamically for each configuration. -generator_default_variables['SHARED_INTERMEDIATE_DIR'] = \ - '$SHARED_INTERMEDIATE_DIR' +generator_default_variables["SHARED_INTERMEDIATE_DIR"] = "$SHARED_INTERMEDIATE_DIR" def CalculateVariables(default_variables, params): - generator_flags = params.get('generator_flags', {}) - for key, val in generator_flags.items(): - default_variables.setdefault(key, val) - flavor = gyp.common.GetFlavor(params) - default_variables.setdefault('OS', flavor) - if flavor == 'win': - # Copy additional generator configuration data from VS, which is shared - # by the Eclipse generator. - import gyp.generator.msvs as msvs_generator - generator_additional_non_configuration_keys = getattr(msvs_generator, - 'generator_additional_non_configuration_keys', []) - generator_additional_path_sections = getattr(msvs_generator, - 'generator_additional_path_sections', []) - - gyp.msvs_emulation.CalculateCommonVariables(default_variables, params) + generator_flags = params.get("generator_flags", {}) + for key, val in generator_flags.items(): + default_variables.setdefault(key, val) + flavor = gyp.common.GetFlavor(params) + default_variables.setdefault("OS", flavor) + if flavor == "win": + gyp.msvs_emulation.CalculateCommonVariables(default_variables, params) def CalculateGeneratorInputInfo(params): - """Calculate the generator specific info that gets fed to input (called by + """Calculate the generator specific info that gets fed to input (called by gyp).""" - generator_flags = params.get('generator_flags', {}) - if generator_flags.get('adjust_static_libraries', False): - global generator_wants_static_library_dependencies_adjusted - generator_wants_static_library_dependencies_adjusted = True - - -def GetAllIncludeDirectories(target_list, target_dicts, - shared_intermediate_dirs, config_name, params, - compiler_path): - """Calculate the set of include directories to be used. + generator_flags = params.get("generator_flags", {}) + if generator_flags.get("adjust_static_libraries", False): + global generator_wants_static_library_dependencies_adjusted + generator_wants_static_library_dependencies_adjusted = True + + +def GetAllIncludeDirectories( + target_list, + target_dicts, + shared_intermediate_dirs, + config_name, + params, + compiler_path, +): + """Calculate the set of include directories to be used. Returns: A list including all the include_dir's specified for every target followed by any include directories that were added as cflag compiler options. """ - gyp_includes_set = set() - compiler_includes_list = [] - - # Find compiler's default include dirs. - if compiler_path: - command = shlex.split(compiler_path) - command.extend(['-E', '-xc++', '-v', '-']) - proc = subprocess.Popen(args=command, stdin=subprocess.PIPE, - stdout=subprocess.PIPE, stderr=subprocess.PIPE) - output = proc.communicate()[1] - if PY3: - output = output.decode('utf-8') - # Extract the list of include dirs from the output, which has this format: - # ... - # #include "..." search starts here: - # #include <...> search starts here: - # /usr/include/c++/4.6 - # /usr/local/include - # End of search list. - # ... - in_include_list = False - for line in output.splitlines(): - if line.startswith('#include'): - in_include_list = True - continue - if line.startswith('End of search list.'): - break - if in_include_list: - include_dir = line.strip() - if include_dir not in compiler_includes_list: - compiler_includes_list.append(include_dir) - - flavor = gyp.common.GetFlavor(params) - if flavor == 'win': - generator_flags = params.get('generator_flags', {}) - for target_name in target_list: - target = target_dicts[target_name] - if config_name in target['configurations']: - config = target['configurations'][config_name] - - # Look for any include dirs that were explicitly added via cflags. This - # may be done in gyp files to force certain includes to come at the end. - # TODO(jgreenwald): Change the gyp files to not abuse cflags for this, and - # remove this. - if flavor == 'win': - msvs_settings = gyp.msvs_emulation.MsvsSettings(target, generator_flags) - cflags = msvs_settings.GetCflags(config_name) - else: - cflags = config['cflags'] - for cflag in cflags: - if cflag.startswith('-I'): - include_dir = cflag[2:] - if include_dir not in compiler_includes_list: - compiler_includes_list.append(include_dir) - - # Find standard gyp include dirs. - if 'include_dirs' in config: - include_dirs = config['include_dirs'] - for shared_intermediate_dir in shared_intermediate_dirs: - for include_dir in include_dirs: - include_dir = include_dir.replace('$SHARED_INTERMEDIATE_DIR', - shared_intermediate_dir) - if not os.path.isabs(include_dir): - base_dir = os.path.dirname(target_name) - - include_dir = base_dir + '/' + include_dir - include_dir = os.path.abspath(include_dir) - - gyp_includes_set.add(include_dir) - - # Generate a list that has all the include dirs. - all_includes_list = list(gyp_includes_set) - all_includes_list.sort() - for compiler_include in compiler_includes_list: - if not compiler_include in gyp_includes_set: - all_includes_list.append(compiler_include) - - # All done. - return all_includes_list + gyp_includes_set = set() + compiler_includes_list = [] + + # Find compiler's default include dirs. + if compiler_path: + command = shlex.split(compiler_path) + command.extend(["-E", "-xc++", "-v", "-"]) + proc = subprocess.Popen( + args=command, + stdin=subprocess.PIPE, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + ) + output = proc.communicate()[1] + if PY3: + output = output.decode("utf-8") + # Extract the list of include dirs from the output, which has this format: + # ... + # #include "..." search starts here: + # #include <...> search starts here: + # /usr/include/c++/4.6 + # /usr/local/include + # End of search list. + # ... + in_include_list = False + for line in output.splitlines(): + if line.startswith("#include"): + in_include_list = True + continue + if line.startswith("End of search list."): + break + if in_include_list: + include_dir = line.strip() + if include_dir not in compiler_includes_list: + compiler_includes_list.append(include_dir) + + flavor = gyp.common.GetFlavor(params) + if flavor == "win": + generator_flags = params.get("generator_flags", {}) + for target_name in target_list: + target = target_dicts[target_name] + if config_name in target["configurations"]: + config = target["configurations"][config_name] + + # Look for any include dirs that were explicitly added via cflags. This + # may be done in gyp files to force certain includes to come at the end. + # TODO(jgreenwald): Change the gyp files to not abuse cflags for this, and + # remove this. + if flavor == "win": + msvs_settings = gyp.msvs_emulation.MsvsSettings(target, generator_flags) + cflags = msvs_settings.GetCflags(config_name) + else: + cflags = config["cflags"] + for cflag in cflags: + if cflag.startswith("-I"): + include_dir = cflag[2:] + if include_dir not in compiler_includes_list: + compiler_includes_list.append(include_dir) + + # Find standard gyp include dirs. + if "include_dirs" in config: + include_dirs = config["include_dirs"] + for shared_intermediate_dir in shared_intermediate_dirs: + for include_dir in include_dirs: + include_dir = include_dir.replace( + "$SHARED_INTERMEDIATE_DIR", shared_intermediate_dir + ) + if not os.path.isabs(include_dir): + base_dir = os.path.dirname(target_name) + + include_dir = base_dir + "/" + include_dir + include_dir = os.path.abspath(include_dir) + + gyp_includes_set.add(include_dir) + + # Generate a list that has all the include dirs. + all_includes_list = list(gyp_includes_set) + all_includes_list.sort() + for compiler_include in compiler_includes_list: + if compiler_include not in gyp_includes_set: + all_includes_list.append(compiler_include) + + # All done. + return all_includes_list def GetCompilerPath(target_list, data, options): - """Determine a command that can be used to invoke the compiler. + """Determine a command that can be used to invoke the compiler. Returns: If this is a gyp project that has explicit make settings, try to determine the compiler from that. Otherwise, see if a compiler was specified via the CC_target environment variable. """ - # First, see if the compiler is configured in make's settings. - build_file, _, _ = gyp.common.ParseQualifiedTarget(target_list[0]) - make_global_settings_dict = data[build_file].get('make_global_settings', {}) - for key, value in make_global_settings_dict: - if key in ['CC', 'CXX']: - return os.path.join(options.toplevel_dir, value) + # First, see if the compiler is configured in make's settings. + build_file, _, _ = gyp.common.ParseQualifiedTarget(target_list[0]) + make_global_settings_dict = data[build_file].get("make_global_settings", {}) + for key, value in make_global_settings_dict: + if key in ["CC", "CXX"]: + return os.path.join(options.toplevel_dir, value) - # Check to see if the compiler was specified as an environment variable. - for key in ['CC_target', 'CC', 'CXX']: - compiler = os.environ.get(key) - if compiler: - return compiler + # Check to see if the compiler was specified as an environment variable. + for key in ["CC_target", "CC", "CXX"]: + compiler = os.environ.get(key) + if compiler: + return compiler - return 'gcc' + return "gcc" -def GetAllDefines(target_list, target_dicts, data, config_name, params, - compiler_path): - """Calculate the defines for a project. +def GetAllDefines(target_list, target_dicts, data, config_name, params, compiler_path): + """Calculate the defines for a project. Returns: A dict that includes explicit defines declared in gyp files along with all of the default defines that the compiler uses. """ - # Get defines declared in the gyp files. - all_defines = {} - flavor = gyp.common.GetFlavor(params) - if flavor == 'win': - generator_flags = params.get('generator_flags', {}) - for target_name in target_list: - target = target_dicts[target_name] - - if flavor == 'win': - msvs_settings = gyp.msvs_emulation.MsvsSettings(target, generator_flags) - extra_defines = msvs_settings.GetComputedDefines(config_name) - else: - extra_defines = [] - if config_name in target['configurations']: - config = target['configurations'][config_name] - target_defines = config['defines'] - else: - target_defines = [] - for define in target_defines + extra_defines: - split_define = define.split('=', 1) - if len(split_define) == 1: - split_define.append('1') - if split_define[0].strip() in all_defines: - # Already defined - continue - all_defines[split_define[0].strip()] = split_define[1].strip() - # Get default compiler defines (if possible). - if flavor == 'win': - return all_defines # Default defines already processed in the loop above. - if compiler_path: - command = shlex.split(compiler_path) - command.extend(['-E', '-dM', '-']) - cpp_proc = subprocess.Popen(args=command, cwd='.', - stdin=subprocess.PIPE, stdout=subprocess.PIPE) - cpp_output = cpp_proc.communicate()[0] - if PY3: - cpp_output = cpp_output.decode('utf-8') - cpp_lines = cpp_output.split('\n') - for cpp_line in cpp_lines: - if not cpp_line.strip(): - continue - cpp_line_parts = cpp_line.split(' ', 2) - key = cpp_line_parts[1] - if len(cpp_line_parts) >= 3: - val = cpp_line_parts[2] - else: - val = '1' - all_defines[key] = val - - return all_defines + # Get defines declared in the gyp files. + all_defines = {} + flavor = gyp.common.GetFlavor(params) + if flavor == "win": + generator_flags = params.get("generator_flags", {}) + for target_name in target_list: + target = target_dicts[target_name] + + if flavor == "win": + msvs_settings = gyp.msvs_emulation.MsvsSettings(target, generator_flags) + extra_defines = msvs_settings.GetComputedDefines(config_name) + else: + extra_defines = [] + if config_name in target["configurations"]: + config = target["configurations"][config_name] + target_defines = config["defines"] + else: + target_defines = [] + for define in target_defines + extra_defines: + split_define = define.split("=", 1) + if len(split_define) == 1: + split_define.append("1") + if split_define[0].strip() in all_defines: + # Already defined + continue + all_defines[split_define[0].strip()] = split_define[1].strip() + # Get default compiler defines (if possible). + if flavor == "win": + return all_defines # Default defines already processed in the loop above. + if compiler_path: + command = shlex.split(compiler_path) + command.extend(["-E", "-dM", "-"]) + cpp_proc = subprocess.Popen( + args=command, cwd=".", stdin=subprocess.PIPE, stdout=subprocess.PIPE + ) + cpp_output = cpp_proc.communicate()[0] + if PY3: + cpp_output = cpp_output.decode("utf-8") + cpp_lines = cpp_output.split("\n") + for cpp_line in cpp_lines: + if not cpp_line.strip(): + continue + cpp_line_parts = cpp_line.split(" ", 2) + key = cpp_line_parts[1] + if len(cpp_line_parts) >= 3: + val = cpp_line_parts[2] + else: + val = "1" + all_defines[key] = val + + return all_defines def WriteIncludePaths(out, eclipse_langs, include_dirs): - """Write the includes section of a CDT settings export file.""" - - out.write('
\n') - out.write(' \n') - for lang in eclipse_langs: - out.write(' \n' % lang) - for include_dir in include_dirs: - out.write(' %s\n' % - include_dir) - out.write(' \n') - out.write('
\n') + """Write the includes section of a CDT settings export file.""" + + out.write( + '
\n' + ) + out.write(' \n') + for lang in eclipse_langs: + out.write(' \n' % lang) + for include_dir in include_dirs: + out.write( + ' %s\n' + % include_dir + ) + out.write(" \n") + out.write("
\n") def WriteMacros(out, eclipse_langs, defines): - """Write the macros section of a CDT settings export file.""" - - out.write('
\n') - out.write(' \n') - for lang in eclipse_langs: - out.write(' \n' % lang) - for key in sorted(defines): - out.write(' %s%s\n' % - (escape(key), escape(defines[key]))) - out.write(' \n') - out.write('
\n') - - -def GenerateOutputForConfig(target_list, target_dicts, data, params, - config_name): - options = params['options'] - generator_flags = params.get('generator_flags', {}) - - # build_dir: relative path from source root to our output files. - # e.g. "out/Debug" - build_dir = os.path.join(generator_flags.get('output_dir', 'out'), - config_name) - - toplevel_build = os.path.join(options.toplevel_dir, build_dir) - # Ninja uses out/Debug/gen while make uses out/Debug/obj/gen as the - # SHARED_INTERMEDIATE_DIR. Include both possible locations. - shared_intermediate_dirs = [os.path.join(toplevel_build, 'obj', 'gen'), - os.path.join(toplevel_build, 'gen')] - - GenerateCdtSettingsFile(target_list, - target_dicts, - data, - params, - config_name, - os.path.join(toplevel_build, - 'eclipse-cdt-settings.xml'), - options, - shared_intermediate_dirs) - GenerateClasspathFile(target_list, - target_dicts, - options.toplevel_dir, - toplevel_build, - os.path.join(toplevel_build, - 'eclipse-classpath.xml')) - - -def GenerateCdtSettingsFile(target_list, target_dicts, data, params, - config_name, out_name, options, - shared_intermediate_dirs): - gyp.common.EnsureDirExists(out_name) - with open(out_name, 'w') as out: - out.write('\n') - out.write('\n') - - eclipse_langs = ['C++ Source File', 'C Source File', 'Assembly Source File', - 'GNU C++', 'GNU C', 'Assembly'] - compiler_path = GetCompilerPath(target_list, data, options) - include_dirs = GetAllIncludeDirectories(target_list, target_dicts, - shared_intermediate_dirs, - config_name, params, compiler_path) - WriteIncludePaths(out, eclipse_langs, include_dirs) - defines = GetAllDefines(target_list, target_dicts, data, config_name, - params, compiler_path) - WriteMacros(out, eclipse_langs, defines) - - out.write('\n') - - -def GenerateClasspathFile(target_list, target_dicts, toplevel_dir, - toplevel_build, out_name): - '''Generates a classpath file suitable for symbol navigation and code + """Write the macros section of a CDT settings export file.""" + + out.write( + '
\n' + ) + out.write(' \n') + for lang in eclipse_langs: + out.write(' \n' % lang) + for key in sorted(defines): + out.write( + " %s%s\n" + % (escape(key), escape(defines[key])) + ) + out.write(" \n") + out.write("
\n") + + +def GenerateOutputForConfig(target_list, target_dicts, data, params, config_name): + options = params["options"] + generator_flags = params.get("generator_flags", {}) + + # build_dir: relative path from source root to our output files. + # e.g. "out/Debug" + build_dir = os.path.join(generator_flags.get("output_dir", "out"), config_name) + + toplevel_build = os.path.join(options.toplevel_dir, build_dir) + # Ninja uses out/Debug/gen while make uses out/Debug/obj/gen as the + # SHARED_INTERMEDIATE_DIR. Include both possible locations. + shared_intermediate_dirs = [ + os.path.join(toplevel_build, "obj", "gen"), + os.path.join(toplevel_build, "gen"), + ] + + GenerateCdtSettingsFile( + target_list, + target_dicts, + data, + params, + config_name, + os.path.join(toplevel_build, "eclipse-cdt-settings.xml"), + options, + shared_intermediate_dirs, + ) + GenerateClasspathFile( + target_list, + target_dicts, + options.toplevel_dir, + toplevel_build, + os.path.join(toplevel_build, "eclipse-classpath.xml"), + ) + + +def GenerateCdtSettingsFile( + target_list, + target_dicts, + data, + params, + config_name, + out_name, + options, + shared_intermediate_dirs, +): + gyp.common.EnsureDirExists(out_name) + with open(out_name, "w") as out: + out.write('\n') + out.write("\n") + + eclipse_langs = [ + "C++ Source File", + "C Source File", + "Assembly Source File", + "GNU C++", + "GNU C", + "Assembly", + ] + compiler_path = GetCompilerPath(target_list, data, options) + include_dirs = GetAllIncludeDirectories( + target_list, + target_dicts, + shared_intermediate_dirs, + config_name, + params, + compiler_path, + ) + WriteIncludePaths(out, eclipse_langs, include_dirs) + defines = GetAllDefines( + target_list, target_dicts, data, config_name, params, compiler_path + ) + WriteMacros(out, eclipse_langs, defines) + + out.write("\n") + + +def GenerateClasspathFile( + target_list, target_dicts, toplevel_dir, toplevel_build, out_name +): + """Generates a classpath file suitable for symbol navigation and code completion of Java code (such as in Android projects) by finding all - .java and .jar files used as action inputs.''' - gyp.common.EnsureDirExists(out_name) - result = ET.Element('classpath') - - def AddElements(kind, paths): - # First, we need to normalize the paths so they are all relative to the - # toplevel dir. - rel_paths = set() - for path in paths: - if os.path.isabs(path): - rel_paths.add(os.path.relpath(path, toplevel_dir)) - else: - rel_paths.add(path) - - for path in sorted(rel_paths): - entry_element = ET.SubElement(result, 'classpathentry') - entry_element.set('kind', kind) - entry_element.set('path', path) - - AddElements('lib', GetJavaJars(target_list, target_dicts, toplevel_dir)) - AddElements('src', GetJavaSourceDirs(target_list, target_dicts, toplevel_dir)) - # Include the standard JRE container and a dummy out folder - AddElements('con', ['org.eclipse.jdt.launching.JRE_CONTAINER']) - # Include a dummy out folder so that Eclipse doesn't use the default /bin - # folder in the root of the project. - AddElements('output', [os.path.join(toplevel_build, '.eclipse-java-build')]) - - ET.ElementTree(result).write(out_name) + .java and .jar files used as action inputs.""" + gyp.common.EnsureDirExists(out_name) + result = ET.Element("classpath") + + def AddElements(kind, paths): + # First, we need to normalize the paths so they are all relative to the + # toplevel dir. + rel_paths = set() + for path in paths: + if os.path.isabs(path): + rel_paths.add(os.path.relpath(path, toplevel_dir)) + else: + rel_paths.add(path) + + for path in sorted(rel_paths): + entry_element = ET.SubElement(result, "classpathentry") + entry_element.set("kind", kind) + entry_element.set("path", path) + + AddElements("lib", GetJavaJars(target_list, target_dicts, toplevel_dir)) + AddElements("src", GetJavaSourceDirs(target_list, target_dicts, toplevel_dir)) + # Include the standard JRE container and a dummy out folder + AddElements("con", ["org.eclipse.jdt.launching.JRE_CONTAINER"]) + # Include a dummy out folder so that Eclipse doesn't use the default /bin + # folder in the root of the project. + AddElements("output", [os.path.join(toplevel_build, ".eclipse-java-build")]) + + ET.ElementTree(result).write(out_name) def GetJavaJars(target_list, target_dicts, toplevel_dir): - '''Generates a sequence of all .jars used as inputs.''' - for target_name in target_list: - target = target_dicts[target_name] - for action in target.get('actions', []): - for input_ in action['inputs']: - if os.path.splitext(input_)[1] == '.jar' and not input_.startswith('$'): - if os.path.isabs(input_): - yield input_ - else: - yield os.path.join(os.path.dirname(target_name), input_) + """Generates a sequence of all .jars used as inputs.""" + for target_name in target_list: + target = target_dicts[target_name] + for action in target.get("actions", []): + for input_ in action["inputs"]: + if os.path.splitext(input_)[1] == ".jar" and not input_.startswith("$"): + if os.path.isabs(input_): + yield input_ + else: + yield os.path.join(os.path.dirname(target_name), input_) def GetJavaSourceDirs(target_list, target_dicts, toplevel_dir): - '''Generates a sequence of all likely java package root directories.''' - for target_name in target_list: - target = target_dicts[target_name] - for action in target.get('actions', []): - for input_ in action['inputs']: - if (os.path.splitext(input_)[1] == '.java' and - not input_.startswith('$')): - dir_ = os.path.dirname(os.path.join(os.path.dirname(target_name), - input_)) - # If there is a parent 'src' or 'java' folder, navigate up to it - - # these are canonical package root names in Chromium. This will - # break if 'src' or 'java' exists in the package structure. This - # could be further improved by inspecting the java file for the - # package name if this proves to be too fragile in practice. - parent_search = dir_ - while os.path.basename(parent_search) not in ['src', 'java']: - parent_search, _ = os.path.split(parent_search) - if not parent_search or parent_search == toplevel_dir: - # Didn't find a known root, just return the original path - yield dir_ - break - else: - yield parent_search + """Generates a sequence of all likely java package root directories.""" + for target_name in target_list: + target = target_dicts[target_name] + for action in target.get("actions", []): + for input_ in action["inputs"]: + if os.path.splitext(input_)[1] == ".java" and not input_.startswith( + "$" + ): + dir_ = os.path.dirname( + os.path.join(os.path.dirname(target_name), input_) + ) + # If there is a parent 'src' or 'java' folder, navigate up to it - + # these are canonical package root names in Chromium. This will + # break if 'src' or 'java' exists in the package structure. This + # could be further improved by inspecting the java file for the + # package name if this proves to be too fragile in practice. + parent_search = dir_ + while os.path.basename(parent_search) not in ["src", "java"]: + parent_search, _ = os.path.split(parent_search) + if not parent_search or parent_search == toplevel_dir: + # Didn't find a known root, just return the original path + yield dir_ + break + else: + yield parent_search def GenerateOutput(target_list, target_dicts, data, params): - """Generate an XML settings file that can be imported into a CDT project.""" + """Generate an XML settings file that can be imported into a CDT project.""" - if params['options'].generator_output: - raise NotImplementedError("--generator_output not implemented for eclipse") - - user_config = params.get('generator_flags', {}).get('config', None) - if user_config: - GenerateOutputForConfig(target_list, target_dicts, data, params, - user_config) - else: - config_names = target_dicts[target_list[0]]['configurations'].keys() - for config_name in config_names: - GenerateOutputForConfig(target_list, target_dicts, data, params, - config_name) + if params["options"].generator_output: + raise NotImplementedError("--generator_output not implemented for eclipse") + user_config = params.get("generator_flags", {}).get("config", None) + if user_config: + GenerateOutputForConfig(target_list, target_dicts, data, params, user_config) + else: + config_names = target_dicts[target_list[0]]["configurations"] + for config_name in config_names: + GenerateOutputForConfig( + target_list, target_dicts, data, params, config_name + ) diff --git a/tools/gyp/pylib/gyp/generator/gypd.py b/tools/gyp/pylib/gyp/generator/gypd.py index 78eeaa61b2294a..4171704c47a4b6 100644 --- a/tools/gyp/pylib/gyp/generator/gypd.py +++ b/tools/gyp/pylib/gyp/generator/gypd.py @@ -32,36 +32,33 @@ import gyp.common -import errno -import os import pprint # These variables should just be spit back out as variable references. _generator_identity_variables = [ - 'CONFIGURATION_NAME', - 'EXECUTABLE_PREFIX', - 'EXECUTABLE_SUFFIX', - 'INTERMEDIATE_DIR', - 'LIB_DIR', - 'PRODUCT_DIR', - 'RULE_INPUT_ROOT', - 'RULE_INPUT_DIRNAME', - 'RULE_INPUT_EXT', - 'RULE_INPUT_NAME', - 'RULE_INPUT_PATH', - 'SHARED_INTERMEDIATE_DIR', - 'SHARED_LIB_DIR', - 'SHARED_LIB_PREFIX', - 'SHARED_LIB_SUFFIX', - 'STATIC_LIB_PREFIX', - 'STATIC_LIB_SUFFIX', + "CONFIGURATION_NAME", + "EXECUTABLE_PREFIX", + "EXECUTABLE_SUFFIX", + "INTERMEDIATE_DIR", + "LIB_DIR", + "PRODUCT_DIR", + "RULE_INPUT_ROOT", + "RULE_INPUT_DIRNAME", + "RULE_INPUT_EXT", + "RULE_INPUT_NAME", + "RULE_INPUT_PATH", + "SHARED_INTERMEDIATE_DIR", + "SHARED_LIB_DIR", + "SHARED_LIB_PREFIX", + "SHARED_LIB_SUFFIX", + "STATIC_LIB_PREFIX", + "STATIC_LIB_SUFFIX", ] # gypd doesn't define a default value for OS like many other generator # modules. Specify "-D OS=whatever" on the command line to provide a value. -generator_default_variables = { -} +generator_default_variables = {} # gypd supports multiple toolsets generator_supports_multiple_toolsets = True @@ -71,24 +68,22 @@ # module should use < for the early phase and then switch to > for the late # phase. Bonus points for carrying @ back into the output too. for v in _generator_identity_variables: - generator_default_variables[v] = '<(%s)' % v + generator_default_variables[v] = "<(%s)" % v def GenerateOutput(target_list, target_dicts, data, params): - output_files = {} - for qualified_target in target_list: - [input_file, target] = \ - gyp.common.ParseQualifiedTarget(qualified_target)[0:2] - - if input_file[-4:] != '.gyp': - continue - input_file_stem = input_file[:-4] - output_file = input_file_stem + params['options'].suffix + '.gypd' - - if not output_file in output_files: - output_files[output_file] = input_file - - for output_file, input_file in output_files.items(): - output = open(output_file, 'w') - pprint.pprint(data[input_file], output) - output.close() + output_files = {} + for qualified_target in target_list: + [input_file, target] = gyp.common.ParseQualifiedTarget(qualified_target)[0:2] + + if input_file[-4:] != ".gyp": + continue + input_file_stem = input_file[:-4] + output_file = input_file_stem + params["options"].suffix + ".gypd" + + output_files[output_file] = output_files.get(output_file, input_file) + + for output_file, input_file in output_files.items(): + output = open(output_file, "w") + pprint.pprint(data[input_file], output) + output.close() diff --git a/tools/gyp/pylib/gyp/generator/gypsh.py b/tools/gyp/pylib/gyp/generator/gypsh.py index bd405f43a993d4..2d8aba5d1c19e9 100644 --- a/tools/gyp/pylib/gyp/generator/gypsh.py +++ b/tools/gyp/pylib/gyp/generator/gypsh.py @@ -21,36 +21,38 @@ # All of this stuff about generator variables was lovingly ripped from gypd.py. # That module has a much better description of what's going on and why. _generator_identity_variables = [ - 'EXECUTABLE_PREFIX', - 'EXECUTABLE_SUFFIX', - 'INTERMEDIATE_DIR', - 'PRODUCT_DIR', - 'RULE_INPUT_ROOT', - 'RULE_INPUT_DIRNAME', - 'RULE_INPUT_EXT', - 'RULE_INPUT_NAME', - 'RULE_INPUT_PATH', - 'SHARED_INTERMEDIATE_DIR', + "EXECUTABLE_PREFIX", + "EXECUTABLE_SUFFIX", + "INTERMEDIATE_DIR", + "PRODUCT_DIR", + "RULE_INPUT_ROOT", + "RULE_INPUT_DIRNAME", + "RULE_INPUT_EXT", + "RULE_INPUT_NAME", + "RULE_INPUT_PATH", + "SHARED_INTERMEDIATE_DIR", ] -generator_default_variables = { -} +generator_default_variables = {} for v in _generator_identity_variables: - generator_default_variables[v] = '<(%s)' % v + generator_default_variables[v] = "<(%s)" % v def GenerateOutput(target_list, target_dicts, data, params): - locals = { - 'target_list': target_list, - 'target_dicts': target_dicts, - 'data': data, - } - - # Use a banner that looks like the stock Python one and like what - # code.interact uses by default, but tack on something to indicate what - # locals are available, and identify gypsh. - banner='Python %s on %s\nlocals.keys() = %s\ngypsh' % \ - (sys.version, sys.platform, repr(sorted(locals.keys()))) - - code.interact(banner, local=locals) + locals = { + "target_list": target_list, + "target_dicts": target_dicts, + "data": data, + } + + # Use a banner that looks like the stock Python one and like what + # code.interact uses by default, but tack on something to indicate what + # locals are available, and identify gypsh. + banner = "Python %s on %s\nlocals.keys() = %s\ngypsh" % ( + sys.version, + sys.platform, + repr(sorted(locals.keys())), + ) + + code.interact(banner, local=locals) diff --git a/tools/gyp/pylib/gyp/generator/make.py b/tools/gyp/pylib/gyp/generator/make.py index 848418d3fb6f16..66d0af0f8ea87b 100644 --- a/tools/gyp/pylib/gyp/generator/make.py +++ b/tools/gyp/pylib/gyp/generator/make.py @@ -25,31 +25,29 @@ import os import re -import sys import subprocess import gyp import gyp.common import gyp.xcode_emulation from gyp.common import GetEnvironFallback -from gyp.common import GypError import hashlib generator_default_variables = { - 'EXECUTABLE_PREFIX': '', - 'EXECUTABLE_SUFFIX': '', - 'STATIC_LIB_PREFIX': 'lib', - 'SHARED_LIB_PREFIX': 'lib', - 'STATIC_LIB_SUFFIX': '.a', - 'INTERMEDIATE_DIR': '$(obj).$(TOOLSET)/$(TARGET)/geni', - 'SHARED_INTERMEDIATE_DIR': '$(obj)/gen', - 'PRODUCT_DIR': '$(builddir)', - 'RULE_INPUT_ROOT': '%(INPUT_ROOT)s', # This gets expanded by Python. - 'RULE_INPUT_DIRNAME': '%(INPUT_DIRNAME)s', # This gets expanded by Python. - 'RULE_INPUT_PATH': '$(abspath $<)', - 'RULE_INPUT_EXT': '$(suffix $<)', - 'RULE_INPUT_NAME': '$(notdir $<)', - 'CONFIGURATION_NAME': '$(BUILDTYPE)', + "EXECUTABLE_PREFIX": "", + "EXECUTABLE_SUFFIX": "", + "STATIC_LIB_PREFIX": "lib", + "SHARED_LIB_PREFIX": "lib", + "STATIC_LIB_SUFFIX": ".a", + "INTERMEDIATE_DIR": "$(obj).$(TOOLSET)/$(TARGET)/geni", + "SHARED_INTERMEDIATE_DIR": "$(obj)/gen", + "PRODUCT_DIR": "$(builddir)", + "RULE_INPUT_ROOT": "%(INPUT_ROOT)s", # This gets expanded by Python. + "RULE_INPUT_DIRNAME": "%(INPUT_DIRNAME)s", # This gets expanded by Python. + "RULE_INPUT_PATH": "$(abspath $<)", + "RULE_INPUT_EXT": "$(suffix $<)", + "RULE_INPUT_NAME": "$(notdir $<)", + "CONFIGURATION_NAME": "$(BUILDTYPE)", } # Make supports multiple toolsets @@ -66,63 +64,69 @@ def CalculateVariables(default_variables, params): - """Calculate additional variables for use in the build (called by gyp).""" - flavor = gyp.common.GetFlavor(params) - if flavor == 'mac': - default_variables.setdefault('OS', 'mac') - default_variables.setdefault('SHARED_LIB_SUFFIX', '.dylib') - default_variables.setdefault('SHARED_LIB_DIR', - generator_default_variables['PRODUCT_DIR']) - default_variables.setdefault('LIB_DIR', - generator_default_variables['PRODUCT_DIR']) - - # Copy additional generator configuration data from Xcode, which is shared - # by the Mac Make generator. - import gyp.generator.xcode as xcode_generator - global generator_additional_non_configuration_keys - generator_additional_non_configuration_keys = getattr(xcode_generator, - 'generator_additional_non_configuration_keys', []) - global generator_additional_path_sections - generator_additional_path_sections = getattr(xcode_generator, - 'generator_additional_path_sections', []) - global generator_extra_sources_for_rules - generator_extra_sources_for_rules = getattr(xcode_generator, - 'generator_extra_sources_for_rules', []) - COMPILABLE_EXTENSIONS.update({'.m': 'objc', '.mm' : 'objcxx'}) - else: - operating_system = flavor - if flavor == 'android': - operating_system = 'linux' # Keep this legacy behavior for now. - default_variables.setdefault('OS', operating_system) - if flavor == 'aix': - default_variables.setdefault('SHARED_LIB_SUFFIX', '.a') + """Calculate additional variables for use in the build (called by gyp).""" + flavor = gyp.common.GetFlavor(params) + if flavor == "mac": + default_variables.setdefault("OS", "mac") + default_variables.setdefault("SHARED_LIB_SUFFIX", ".dylib") + default_variables.setdefault( + "SHARED_LIB_DIR", generator_default_variables["PRODUCT_DIR"] + ) + default_variables.setdefault( + "LIB_DIR", generator_default_variables["PRODUCT_DIR"] + ) + + # Copy additional generator configuration data from Xcode, which is shared + # by the Mac Make generator. + import gyp.generator.xcode as xcode_generator + + global generator_additional_non_configuration_keys + generator_additional_non_configuration_keys = getattr( + xcode_generator, "generator_additional_non_configuration_keys", [] + ) + global generator_additional_path_sections + generator_additional_path_sections = getattr( + xcode_generator, "generator_additional_path_sections", [] + ) + global generator_extra_sources_for_rules + generator_extra_sources_for_rules = getattr( + xcode_generator, "generator_extra_sources_for_rules", [] + ) + COMPILABLE_EXTENSIONS.update({".m": "objc", ".mm": "objcxx"}) else: - default_variables.setdefault('SHARED_LIB_SUFFIX', '.so') - default_variables.setdefault('SHARED_LIB_DIR','$(builddir)/lib.$(TOOLSET)') - default_variables.setdefault('LIB_DIR', '$(obj).$(TOOLSET)') + operating_system = flavor + if flavor == "android": + operating_system = "linux" # Keep this legacy behavior for now. + default_variables.setdefault("OS", operating_system) + if flavor == "aix": + default_variables.setdefault("SHARED_LIB_SUFFIX", ".a") + else: + default_variables.setdefault("SHARED_LIB_SUFFIX", ".so") + default_variables.setdefault("SHARED_LIB_DIR", "$(builddir)/lib.$(TOOLSET)") + default_variables.setdefault("LIB_DIR", "$(obj).$(TOOLSET)") def CalculateGeneratorInputInfo(params): - """Calculate the generator specific info that gets fed to input (called by + """Calculate the generator specific info that gets fed to input (called by gyp).""" - generator_flags = params.get('generator_flags', {}) - android_ndk_version = generator_flags.get('android_ndk_version', None) - # Android NDK requires a strict link order. - if android_ndk_version: - global generator_wants_sorted_dependencies - generator_wants_sorted_dependencies = True - - output_dir = params['options'].generator_output or \ - params['options'].toplevel_dir - builddir_name = generator_flags.get('output_dir', 'out') - qualified_out_dir = os.path.normpath(os.path.join( - output_dir, builddir_name, 'gypfiles')) - - global generator_filelist_paths - generator_filelist_paths = { - 'toplevel': params['options'].toplevel_dir, - 'qualified_out_dir': qualified_out_dir, - } + generator_flags = params.get("generator_flags", {}) + android_ndk_version = generator_flags.get("android_ndk_version", None) + # Android NDK requires a strict link order. + if android_ndk_version: + global generator_wants_sorted_dependencies + generator_wants_sorted_dependencies = True + + output_dir = params["options"].generator_output or params["options"].toplevel_dir + builddir_name = generator_flags.get("output_dir", "out") + qualified_out_dir = os.path.normpath( + os.path.join(output_dir, builddir_name, "gypfiles") + ) + + global generator_filelist_paths + generator_filelist_paths = { + "toplevel": params["options"].toplevel_dir, + "qualified_out_dir": qualified_out_dir, + } # The .d checking code below uses these functions: @@ -135,7 +139,7 @@ def CalculateGeneratorInputInfo(params): # is for example # out/Release/.deps/out/Release/Chromium?Framework.framework/foo # This is the replacement character. -SPACE_REPLACEMENT = '?' +SPACE_REPLACEMENT = "?" LINK_COMMANDS_LINUX = """\ @@ -172,7 +176,7 @@ def CalculateGeneratorInputInfo(params): quiet_cmd_solink_module = SOLINK_MODULE($(TOOLSET)) $@ cmd_solink_module = $(LINK.$(TOOLSET)) -o $@ -shared $(GYP_LDFLAGS) $(LDFLAGS.$(TOOLSET)) -Wl,-soname=$(@F) -Wl,--start-group $(filter-out FORCE_DO_CMD, $^) -Wl,--end-group $(LIBS) -""" +""" # noqa: E501 LINK_COMMANDS_MAC = """\ quiet_cmd_alink = LIBTOOL-STATIC $@ @@ -186,7 +190,7 @@ def CalculateGeneratorInputInfo(params): quiet_cmd_solink_module = SOLINK_MODULE($(TOOLSET)) $@ cmd_solink_module = $(LINK.$(TOOLSET)) -bundle $(GYP_LDFLAGS) $(LDFLAGS.$(TOOLSET)) -o $@ $(filter-out FORCE_DO_CMD, $^) $(LIBS) -""" +""" # noqa: E501 LINK_COMMANDS_ANDROID = """\ quiet_cmd_alink = AR($(TOOLSET)) $@ @@ -213,7 +217,7 @@ def CalculateGeneratorInputInfo(params): cmd_solink_module = $(LINK.$(TOOLSET)) -shared $(GYP_LDFLAGS) $(LDFLAGS.$(TOOLSET)) -Wl,-soname=$(@F) -o $@ -Wl,--start-group $(filter-out FORCE_DO_CMD, $^) -Wl,--end-group $(LIBS) quiet_cmd_solink_module_host = SOLINK_MODULE($(TOOLSET)) $@ cmd_solink_module_host = $(LINK.$(TOOLSET)) -shared $(GYP_LDFLAGS) $(LDFLAGS.$(TOOLSET)) -Wl,-soname=$(@F) -o $@ $(filter-out FORCE_DO_CMD, $^) $(LIBS) -""" +""" # noqa: E501 LINK_COMMANDS_AIX = """\ @@ -231,7 +235,7 @@ def CalculateGeneratorInputInfo(params): quiet_cmd_solink_module = SOLINK_MODULE($(TOOLSET)) $@ cmd_solink_module = $(LINK.$(TOOLSET)) -shared $(GYP_LDFLAGS) $(LDFLAGS.$(TOOLSET)) -o $@ $(filter-out FORCE_DO_CMD, $^) $(LIBS) -""" +""" # noqa: E501 LINK_COMMANDS_OS390 = """\ @@ -249,13 +253,13 @@ def CalculateGeneratorInputInfo(params): quiet_cmd_solink_module = SOLINK_MODULE($(TOOLSET)) $@ cmd_solink_module = $(LINK.$(TOOLSET)) $(GYP_LDFLAGS) $(LDFLAGS.$(TOOLSET)) -o $@ $(filter-out FORCE_DO_CMD, $^) $(LIBS) -Wl,DLL - -""" +""" # noqa: E501 # Header of toplevel Makefile. # This should go into the build tree, but it's easier to keep it here for now. -SHARED_HEADER = ("""\ +SHARED_HEADER = ( + """\ # We borrow heavily from the kernel build setup, though we are simpler since # we don't have Kconfig tweaking settings on us. @@ -327,8 +331,12 @@ def CalculateGeneratorInputInfo(params): space := $(empty) $(empty) # http://stackoverflow.com/questions/1189781/using-make-dir-or-notdir-on-a-path-with-spaces -replace_spaces = $(subst $(space),""" + SPACE_REPLACEMENT + """,$1) -unreplace_spaces = $(subst """ + SPACE_REPLACEMENT + """,$(space),$1) +replace_spaces = $(subst $(space),""" + + SPACE_REPLACEMENT + + """,$1) +unreplace_spaces = $(subst """ + + SPACE_REPLACEMENT + + """,$(space),$1) dirx = $(call unreplace_spaces,$(dir $(call replace_spaces,$1))) # Flags to make gcc output dependency info. Note that you need to be @@ -358,7 +366,7 @@ def CalculateGeneratorInputInfo(params): # and dollar signs past make, the shell, and sed at the same time. # Doesn't work with spaces, but that's fine: .d files have spaces in # their names replaced with other characters.""" -r""" + r""" define fixup_dep # The depfile may not exist if the input file didn't have any #includes. touch $(depfile).raw @@ -375,7 +383,7 @@ def CalculateGeneratorInputInfo(params): rm $(depfile).raw endef """ -""" + """ # Command definitions: # - cmd_foo is the actual command to run; # - quiet_cmd_foo is the brief-output summary of the command. @@ -394,9 +402,8 @@ def CalculateGeneratorInputInfo(params): cmd_copy = ln -f "$<" "$@" 2>/dev/null || (rm -rf "$@" && cp %(copy_archive_args)s "$<" "$@") %(link_commands)s -""" - -r""" +""" # noqa: E501 + r""" # Define an escape_quotes function to escape single quotes. # This allows us to handle quotes properly as long as we always use # use single quotes and escape_quotes. @@ -412,7 +419,7 @@ def CalculateGeneratorInputInfo(params): # (e.g., dash, bash). exact_echo = printf '%%s\n' '$(call escape_quotes,$(1))' """ -""" + """ # Helper to compare the command we're about to run against the command # we logged the last time we ran the command. Produces an empty # string (false) when the commands match. @@ -423,8 +430,9 @@ def CalculateGeneratorInputInfo(params): # $(filter-out $(cmd_$@), $(cmd_$(1)))) # We instead substitute each for the empty string into the other, and # say they're equal if both substitutions produce the empty string. -# .d files contain """ + SPACE_REPLACEMENT + \ - """ instead of spaces, take that into account. +# .d files contain """ + + SPACE_REPLACEMENT + + """ instead of spaces, take that into account. command_changed = $(or $(subst $(cmd_$(1)),,$(cmd_$(call replace_spaces,$@))),\\ $(subst $(cmd_$(call replace_spaces,$@)),,$(cmd_$(1)))) @@ -455,10 +463,12 @@ def CalculateGeneratorInputInfo(params): # Should always run for a given target to handle command-line changes. # Second argument, if non-zero, makes it do asm/C/C++ dependency munging. # Third argument, if non-zero, makes it do POSTBUILDS processing. -# Note: We intentionally do NOT call dirx for depfile, since it contains """ + \ - SPACE_REPLACEMENT + """ for -# spaces already and dirx strips the """ + SPACE_REPLACEMENT + \ - """ characters. +# Note: We intentionally do NOT call dirx for depfile, since it contains """ + + SPACE_REPLACEMENT + + """ for +# spaces already and dirx strips the """ + + SPACE_REPLACEMENT + + """ characters. define do_cmd $(if $(or $(command_changed),$(prereq_changed)), @$(call exact_echo, $($(quiet)cmd_$(1))) @@ -491,7 +501,8 @@ def CalculateGeneratorInputInfo(params): .PHONY: FORCE_DO_CMD FORCE_DO_CMD: -""") +""" # noqa: E501 +) SHARED_HEADER_MAC_COMMANDS = """ quiet_cmd_objc = CXX($(TOOLSET)) $@ @@ -521,37 +532,38 @@ def CalculateGeneratorInputInfo(params): quiet_cmd_infoplist = INFOPLIST $@ cmd_infoplist = $(CC.$(TOOLSET)) -E -P -Wno-trigraphs -x c $(INFOPLIST_DEFINES) "$<" -o "$@" -""" +""" # noqa: E501 def WriteRootHeaderSuffixRules(writer): - extensions = sorted(COMPILABLE_EXTENSIONS.keys(), key=str.lower) - - writer.write('# Suffix rules, putting all outputs into $(obj).\n') - for ext in extensions: - writer.write('$(obj).$(TOOLSET)/%%.o: $(srcdir)/%%%s FORCE_DO_CMD\n' % ext) - writer.write('\t@$(call do_cmd,%s,1)\n' % COMPILABLE_EXTENSIONS[ext]) - - writer.write('\n# Try building from generated source, too.\n') - for ext in extensions: - writer.write( - '$(obj).$(TOOLSET)/%%.o: $(obj).$(TOOLSET)/%%%s FORCE_DO_CMD\n' % ext) - writer.write('\t@$(call do_cmd,%s,1)\n' % COMPILABLE_EXTENSIONS[ext]) - writer.write('\n') - for ext in extensions: - writer.write('$(obj).$(TOOLSET)/%%.o: $(obj)/%%%s FORCE_DO_CMD\n' % ext) - writer.write('\t@$(call do_cmd,%s,1)\n' % COMPILABLE_EXTENSIONS[ext]) - writer.write('\n') - - -SHARED_HEADER_SUFFIX_RULES_COMMENT1 = ("""\ + extensions = sorted(COMPILABLE_EXTENSIONS.keys(), key=str.lower) + + writer.write("# Suffix rules, putting all outputs into $(obj).\n") + for ext in extensions: + writer.write("$(obj).$(TOOLSET)/%%.o: $(srcdir)/%%%s FORCE_DO_CMD\n" % ext) + writer.write("\t@$(call do_cmd,%s,1)\n" % COMPILABLE_EXTENSIONS[ext]) + + writer.write("\n# Try building from generated source, too.\n") + for ext in extensions: + writer.write( + "$(obj).$(TOOLSET)/%%.o: $(obj).$(TOOLSET)/%%%s FORCE_DO_CMD\n" % ext + ) + writer.write("\t@$(call do_cmd,%s,1)\n" % COMPILABLE_EXTENSIONS[ext]) + writer.write("\n") + for ext in extensions: + writer.write("$(obj).$(TOOLSET)/%%.o: $(obj)/%%%s FORCE_DO_CMD\n" % ext) + writer.write("\t@$(call do_cmd,%s,1)\n" % COMPILABLE_EXTENSIONS[ext]) + writer.write("\n") + + +SHARED_HEADER_SUFFIX_RULES_COMMENT1 = """\ # Suffix rules, putting all outputs into $(obj). -""") +""" -SHARED_HEADER_SUFFIX_RULES_COMMENT2 = ("""\ +SHARED_HEADER_SUFFIX_RULES_COMMENT2 = """\ # Try building from generated source, too. -""") +""" SHARED_FOOTER = """\ @@ -574,114 +586,88 @@ def WriteRootHeaderSuffixRules(writer): # Maps every compilable file extension to the do_cmd that compiles it. COMPILABLE_EXTENSIONS = { - '.c': 'cc', - '.cc': 'cxx', - '.cpp': 'cxx', - '.cxx': 'cxx', - '.s': 'cc', - '.S': 'cc', + ".c": "cc", + ".cc": "cxx", + ".cpp": "cxx", + ".cxx": "cxx", + ".s": "cc", + ".S": "cc", } + def Compilable(filename): - """Return true if the file is compilable (should be in OBJS).""" - for res in (filename.endswith(e) for e in COMPILABLE_EXTENSIONS): - if res: - return True - return False + """Return true if the file is compilable (should be in OBJS).""" + for res in (filename.endswith(e) for e in COMPILABLE_EXTENSIONS): + if res: + return True + return False def Linkable(filename): - """Return true if the file is linkable (should be on the link line).""" - return filename.endswith('.o') + """Return true if the file is linkable (should be on the link line).""" + return filename.endswith(".o") def Target(filename): - """Translate a compilable filename to its .o target.""" - return os.path.splitext(filename)[0] + '.o' + """Translate a compilable filename to its .o target.""" + return os.path.splitext(filename)[0] + ".o" def EscapeShellArgument(s): - """Quotes an argument so that it will be interpreted literally by a POSIX + """Quotes an argument so that it will be interpreted literally by a POSIX shell. Taken from http://stackoverflow.com/questions/35817/whats-the-best-way-to-escape-ossystem-calls-in-python """ - return "'" + s.replace("'", "'\\''") + "'" + return "'" + s.replace("'", "'\\''") + "'" def EscapeMakeVariableExpansion(s): - """Make has its own variable expansion syntax using $. We must escape it for + """Make has its own variable expansion syntax using $. We must escape it for string to be interpreted literally.""" - return s.replace('$', '$$') + return s.replace("$", "$$") def EscapeCppDefine(s): - """Escapes a CPP define so that it will reach the compiler unaltered.""" - s = EscapeShellArgument(s) - s = EscapeMakeVariableExpansion(s) - # '#' characters must be escaped even embedded in a string, else Make will - # treat it as the start of a comment. - return s.replace('#', r'\#') + """Escapes a CPP define so that it will reach the compiler unaltered.""" + s = EscapeShellArgument(s) + s = EscapeMakeVariableExpansion(s) + # '#' characters must be escaped even embedded in a string, else Make will + # treat it as the start of a comment. + return s.replace("#", r"\#") def QuoteIfNecessary(string): - """TODO: Should this ideally be replaced with one or more of the above + """TODO: Should this ideally be replaced with one or more of the above functions?""" - if '"' in string: - string = '"' + string.replace('"', '\\"') + '"' - return string + if '"' in string: + string = '"' + string.replace('"', '\\"') + '"' + return string def StringToMakefileVariable(string): - """Convert a string to a value that is acceptable as a make variable name.""" - return re.sub('[^a-zA-Z0-9_]', '_', string) + """Convert a string to a value that is acceptable as a make variable name.""" + return re.sub("[^a-zA-Z0-9_]", "_", string) -srcdir_prefix = '' -def Sourceify(path): - """Convert a path to its source directory form.""" - if '$(' in path: - return path - if os.path.isabs(path): - return path - return srcdir_prefix + path +srcdir_prefix = "" + +def Sourceify(path): + """Convert a path to its source directory form.""" + if "$(" in path: + return path + if os.path.isabs(path): + return path + return srcdir_prefix + path -def QuoteSpaces(s, quote=r'\ '): - return s.replace(' ', quote) -def SourceifyAndQuoteSpaces(path): - """Convert a path to its source directory form and quote spaces.""" - return QuoteSpaces(Sourceify(path)) +def QuoteSpaces(s, quote=r"\ "): + return s.replace(" ", quote) -# TODO: Avoid code duplication with _ValidateSourcesForMSVSProject in msvs.py. -def _ValidateSourcesForOSX(spec, all_sources): - """Makes sure if duplicate basenames are not specified in the source list. - Arguments: - spec: The target dictionary containing the properties of the target. - """ - if spec.get('type', None) != 'static_library': - return - - basenames = {} - for source in all_sources: - name, ext = os.path.splitext(source) - is_compiled_file = ext in [ - '.c', '.cc', '.cpp', '.cxx', '.m', '.mm', '.s', '.S'] - if not is_compiled_file: - continue - basename = os.path.basename(name) # Don't include extension. - basenames.setdefault(basename, []).append(source) - - error = '' - for basename, files in basenames.items(): - if len(files) > 1: - error += ' %s: %s\n' % (basename, ' '.join(files)) - - if error: - print(('static library %s has several files with the same basename:\n' % spec['target_name']) - + error + 'libtool on OS X will generate' + ' warnings for them.') - raise GypError('Duplicate basenames in sources section, see list above') +def SourceifyAndQuoteSpaces(path): + """Convert a path to its source directory form and quote spaces.""" + return QuoteSpaces(Sourceify(path)) # Map from qualified target to path to output. @@ -694,41 +680,62 @@ def _ValidateSourcesForOSX(spec, all_sources): class MakefileWriter(object): - """MakefileWriter packages up the writing of one target-specific foobar.mk. + """MakefileWriter packages up the writing of one target-specific foobar.mk. Its only real entry point is Write(), and is mostly used for namespacing. """ - def __init__(self, generator_flags, flavor): - self.generator_flags = generator_flags - self.flavor = flavor - - self.suffix_rules_srcdir = {} - self.suffix_rules_objdir1 = {} - self.suffix_rules_objdir2 = {} - - # Generate suffix rules for all compilable extensions. - for ext in COMPILABLE_EXTENSIONS.keys(): - # Suffix rules for source folder. - self.suffix_rules_srcdir.update({ext: ("""\ + def __init__(self, generator_flags, flavor): + self.generator_flags = generator_flags + self.flavor = flavor + + self.suffix_rules_srcdir = {} + self.suffix_rules_objdir1 = {} + self.suffix_rules_objdir2 = {} + + # Generate suffix rules for all compilable extensions. + for ext in COMPILABLE_EXTENSIONS.keys(): + # Suffix rules for source folder. + self.suffix_rules_srcdir.update( + { + ext: ( + """\ $(obj).$(TOOLSET)/$(TARGET)/%%.o: $(srcdir)/%%%s FORCE_DO_CMD - @$(call do_cmd,%s,1) -""" % (ext, COMPILABLE_EXTENSIONS[ext]))}) - - # Suffix rules for generated source files. - self.suffix_rules_objdir1.update({ext: ("""\ +\t@$(call do_cmd,%s,1) +""" + % (ext, COMPILABLE_EXTENSIONS[ext]) + ) + } + ) + + # Suffix rules for generated source files. + self.suffix_rules_objdir1.update( + { + ext: ( + """\ $(obj).$(TOOLSET)/$(TARGET)/%%.o: $(obj).$(TOOLSET)/%%%s FORCE_DO_CMD - @$(call do_cmd,%s,1) -""" % (ext, COMPILABLE_EXTENSIONS[ext]))}) - self.suffix_rules_objdir2.update({ext: ("""\ +\t@$(call do_cmd,%s,1) +""" + % (ext, COMPILABLE_EXTENSIONS[ext]) + ) + } + ) + self.suffix_rules_objdir2.update( + { + ext: ( + """\ $(obj).$(TOOLSET)/$(TARGET)/%%.o: $(obj)/%%%s FORCE_DO_CMD - @$(call do_cmd,%s,1) -""" % (ext, COMPILABLE_EXTENSIONS[ext]))}) - +\t@$(call do_cmd,%s,1) +""" + % (ext, COMPILABLE_EXTENSIONS[ext]) + ) + } + ) - def Write(self, qualified_target, base_path, output_filename, spec, configs, - part_of_all): - """The main entry point: writes a .mk file for a single target. + def Write( + self, qualified_target, base_path, output_filename, spec, configs, part_of_all + ): + """The main entry point: writes a .mk file for a single target. Arguments: qualified_target: target we're generating @@ -738,129 +745,148 @@ def Write(self, qualified_target, base_path, output_filename, spec, configs, spec, configs: gyp info part_of_all: flag indicating this target is part of 'all' """ - gyp.common.EnsureDirExists(output_filename) - - self.fp = open(output_filename, 'w') - - self.fp.write(header) - - self.qualified_target = qualified_target - self.path = base_path - self.target = spec['target_name'] - self.type = spec['type'] - self.toolset = spec['toolset'] - - self.is_mac_bundle = gyp.xcode_emulation.IsMacBundle(self.flavor, spec) - if self.flavor == 'mac': - self.xcode_settings = gyp.xcode_emulation.XcodeSettings(spec) - else: - self.xcode_settings = None - - deps, link_deps = self.ComputeDeps(spec) - - # Some of the generation below can add extra output, sources, or - # link dependencies. All of the out params of the functions that - # follow use names like extra_foo. - extra_outputs = [] - extra_sources = [] - extra_link_deps = [] - extra_mac_bundle_resources = [] - mac_bundle_deps = [] - - if self.is_mac_bundle: - self.output = self.ComputeMacBundleOutput(spec) - self.output_binary = self.ComputeMacBundleBinaryOutput(spec) - else: - self.output = self.output_binary = self.ComputeOutput(spec) - - self.is_standalone_static_library = bool( - spec.get('standalone_static_library', 0)) - self._INSTALLABLE_TARGETS = ('executable', 'loadable_module', - 'shared_library') - if (self.is_standalone_static_library or - self.type in self._INSTALLABLE_TARGETS): - self.alias = os.path.basename(self.output) - install_path = self._InstallableTargetInstallPath() - else: - self.alias = self.output - install_path = self.output - - self.WriteLn("TOOLSET := " + self.toolset) - self.WriteLn("TARGET := " + self.target) - - # Actions must come first, since they can generate more OBJs for use below. - if 'actions' in spec: - self.WriteActions(spec['actions'], extra_sources, extra_outputs, - extra_mac_bundle_resources, part_of_all) - - # Rules must be early like actions. - if 'rules' in spec: - self.WriteRules(spec['rules'], extra_sources, extra_outputs, - extra_mac_bundle_resources, part_of_all) - - if 'copies' in spec: - self.WriteCopies(spec['copies'], extra_outputs, part_of_all) - - # Bundle resources. - if self.is_mac_bundle: - all_mac_bundle_resources = ( - spec.get('mac_bundle_resources', []) + extra_mac_bundle_resources) - self.WriteMacBundleResources(all_mac_bundle_resources, mac_bundle_deps) - self.WriteMacInfoPlist(mac_bundle_deps) - - # Sources. - all_sources = spec.get('sources', []) + extra_sources - if all_sources: - if self.flavor == 'mac': - # libtool on OS X generates warnings for duplicate basenames in the same - # target. - _ValidateSourcesForOSX(spec, all_sources) - self.WriteSources( - configs, deps, all_sources, extra_outputs, - extra_link_deps, part_of_all, - gyp.xcode_emulation.MacPrefixHeader( - self.xcode_settings, lambda p: Sourceify(self.Absolutify(p)), - self.Pchify)) - sources = list(filter(Compilable, all_sources)) - if sources: - self.WriteLn(SHARED_HEADER_SUFFIX_RULES_COMMENT1) - extensions = set([os.path.splitext(s)[1] for s in sources]) - for ext in extensions: - if ext in self.suffix_rules_srcdir: - self.WriteLn(self.suffix_rules_srcdir[ext]) - self.WriteLn(SHARED_HEADER_SUFFIX_RULES_COMMENT2) - for ext in extensions: - if ext in self.suffix_rules_objdir1: - self.WriteLn(self.suffix_rules_objdir1[ext]) - for ext in extensions: - if ext in self.suffix_rules_objdir2: - self.WriteLn(self.suffix_rules_objdir2[ext]) - self.WriteLn('# End of this set of suffix rules') - - # Add dependency from bundle to bundle binary. - if self.is_mac_bundle: - mac_bundle_deps.append(self.output_binary) + gyp.common.EnsureDirExists(output_filename) - self.WriteTarget(spec, configs, deps, extra_link_deps + link_deps, - mac_bundle_deps, extra_outputs, part_of_all) + self.fp = open(output_filename, "w") - # Update global list of target outputs, used in dependency tracking. - target_outputs[qualified_target] = install_path + self.fp.write(header) - # Update global list of link dependencies. - if self.type in ('static_library', 'shared_library'): - target_link_deps[qualified_target] = self.output_binary + self.qualified_target = qualified_target + self.path = base_path + self.target = spec["target_name"] + self.type = spec["type"] + self.toolset = spec["toolset"] - # Currently any versions have the same effect, but in future the behavior - # could be different. - if self.generator_flags.get('android_ndk_version', None): - self.WriteAndroidNdkModuleRule(self.target, all_sources, link_deps) + self.is_mac_bundle = gyp.xcode_emulation.IsMacBundle(self.flavor, spec) + if self.flavor == "mac": + self.xcode_settings = gyp.xcode_emulation.XcodeSettings(spec) + else: + self.xcode_settings = None - self.fp.close() + deps, link_deps = self.ComputeDeps(spec) + # Some of the generation below can add extra output, sources, or + # link dependencies. All of the out params of the functions that + # follow use names like extra_foo. + extra_outputs = [] + extra_sources = [] + extra_link_deps = [] + extra_mac_bundle_resources = [] + mac_bundle_deps = [] - def WriteSubMake(self, output_filename, makefile_path, targets, build_dir): - """Write a "sub-project" Makefile. + if self.is_mac_bundle: + self.output = self.ComputeMacBundleOutput(spec) + self.output_binary = self.ComputeMacBundleBinaryOutput(spec) + else: + self.output = self.output_binary = self.ComputeOutput(spec) + + self.is_standalone_static_library = bool( + spec.get("standalone_static_library", 0) + ) + self._INSTALLABLE_TARGETS = ("executable", "loadable_module", "shared_library") + if self.is_standalone_static_library or self.type in self._INSTALLABLE_TARGETS: + self.alias = os.path.basename(self.output) + install_path = self._InstallableTargetInstallPath() + else: + self.alias = self.output + install_path = self.output + + self.WriteLn("TOOLSET := " + self.toolset) + self.WriteLn("TARGET := " + self.target) + + # Actions must come first, since they can generate more OBJs for use below. + if "actions" in spec: + self.WriteActions( + spec["actions"], + extra_sources, + extra_outputs, + extra_mac_bundle_resources, + part_of_all, + ) + + # Rules must be early like actions. + if "rules" in spec: + self.WriteRules( + spec["rules"], + extra_sources, + extra_outputs, + extra_mac_bundle_resources, + part_of_all, + ) + + if "copies" in spec: + self.WriteCopies(spec["copies"], extra_outputs, part_of_all) + + # Bundle resources. + if self.is_mac_bundle: + all_mac_bundle_resources = ( + spec.get("mac_bundle_resources", []) + extra_mac_bundle_resources + ) + self.WriteMacBundleResources(all_mac_bundle_resources, mac_bundle_deps) + self.WriteMacInfoPlist(mac_bundle_deps) + + # Sources. + all_sources = spec.get("sources", []) + extra_sources + if all_sources: + self.WriteSources( + configs, + deps, + all_sources, + extra_outputs, + extra_link_deps, + part_of_all, + gyp.xcode_emulation.MacPrefixHeader( + self.xcode_settings, + lambda p: Sourceify(self.Absolutify(p)), + self.Pchify, + ), + ) + sources = [x for x in all_sources if Compilable(x)] + if sources: + self.WriteLn(SHARED_HEADER_SUFFIX_RULES_COMMENT1) + extensions = set([os.path.splitext(s)[1] for s in sources]) + for ext in extensions: + if ext in self.suffix_rules_srcdir: + self.WriteLn(self.suffix_rules_srcdir[ext]) + self.WriteLn(SHARED_HEADER_SUFFIX_RULES_COMMENT2) + for ext in extensions: + if ext in self.suffix_rules_objdir1: + self.WriteLn(self.suffix_rules_objdir1[ext]) + for ext in extensions: + if ext in self.suffix_rules_objdir2: + self.WriteLn(self.suffix_rules_objdir2[ext]) + self.WriteLn("# End of this set of suffix rules") + + # Add dependency from bundle to bundle binary. + if self.is_mac_bundle: + mac_bundle_deps.append(self.output_binary) + + self.WriteTarget( + spec, + configs, + deps, + extra_link_deps + link_deps, + mac_bundle_deps, + extra_outputs, + part_of_all, + ) + + # Update global list of target outputs, used in dependency tracking. + target_outputs[qualified_target] = install_path + + # Update global list of link dependencies. + if self.type in ("static_library", "shared_library"): + target_link_deps[qualified_target] = self.output_binary + + # Currently any versions have the same effect, but in future the behavior + # could be different. + if self.generator_flags.get("android_ndk_version", None): + self.WriteAndroidNdkModuleRule(self.target, all_sources, link_deps) + + self.fp.close() + + def WriteSubMake(self, output_filename, makefile_path, targets, build_dir): + """Write a "sub-project" Makefile. This is a small, wrapper Makefile that calls the top-level Makefile to build the targets from a single gyp file (i.e. a sub-project). @@ -871,24 +897,31 @@ def WriteSubMake(self, output_filename, makefile_path, targets, build_dir): targets: list of "all" targets for this sub-project build_dir: build output directory, relative to the sub-project """ - gyp.common.EnsureDirExists(output_filename) - self.fp = open(output_filename, 'w') - self.fp.write(header) - # For consistency with other builders, put sub-project build output in the - # sub-project dir (see test/subdirectory/gyptest-subdir-all.py). - self.WriteLn('export builddir_name ?= %s' % - os.path.join(os.path.dirname(output_filename), build_dir)) - self.WriteLn('.PHONY: all') - self.WriteLn('all:') - if makefile_path: - makefile_path = ' -C ' + makefile_path - self.WriteLn('\t$(MAKE)%s %s' % (makefile_path, ' '.join(targets))) - self.fp.close() - - - def WriteActions(self, actions, extra_sources, extra_outputs, - extra_mac_bundle_resources, part_of_all): - """Write Makefile code for any 'actions' from the gyp input. + gyp.common.EnsureDirExists(output_filename) + self.fp = open(output_filename, "w") + self.fp.write(header) + # For consistency with other builders, put sub-project build output in the + # sub-project dir (see test/subdirectory/gyptest-subdir-all.py). + self.WriteLn( + "export builddir_name ?= %s" + % os.path.join(os.path.dirname(output_filename), build_dir) + ) + self.WriteLn(".PHONY: all") + self.WriteLn("all:") + if makefile_path: + makefile_path = " -C " + makefile_path + self.WriteLn("\t$(MAKE)%s %s" % (makefile_path, " ".join(targets))) + self.fp.close() + + def WriteActions( + self, + actions, + extra_sources, + extra_outputs, + extra_mac_bundle_resources, + part_of_all, + ): + """Write Makefile code for any 'actions' from the gyp input. extra_sources: a list that will be filled in with newly generated source files, if any @@ -897,98 +930,113 @@ def WriteActions(self, actions, extra_sources, extra_outputs, actions) part_of_all: flag indicating this target is part of 'all' """ - env = self.GetSortedXcodeEnv() - for action in actions: - name = StringToMakefileVariable('%s_%s' % (self.qualified_target, - action['action_name'])) - self.WriteLn('### Rules for action "%s":' % action['action_name']) - inputs = action['inputs'] - outputs = action['outputs'] - - # Build up a list of outputs. - # Collect the output dirs we'll need. - dirs = set() - for out in outputs: - dir = os.path.split(out)[0] - if dir: - dirs.add(dir) - if int(action.get('process_outputs_as_sources', False)): - extra_sources += outputs - if int(action.get('process_outputs_as_mac_bundle_resources', False)): - extra_mac_bundle_resources += outputs - - # Write the actual command. - action_commands = action['action'] - if self.flavor == 'mac': - action_commands = [gyp.xcode_emulation.ExpandEnvVars(command, env) - for command in action_commands] - command = gyp.common.EncodePOSIXShellList(action_commands) - if 'message' in action: - self.WriteLn('quiet_cmd_%s = ACTION %s $@' % (name, action['message'])) - else: - self.WriteLn('quiet_cmd_%s = ACTION %s $@' % (name, name)) - if len(dirs) > 0: - command = 'mkdir -p %s' % ' '.join(dirs) + '; ' + command - - cd_action = 'cd %s; ' % Sourceify(self.path or '.') - - # command and cd_action get written to a toplevel variable called - # cmd_foo. Toplevel variables can't handle things that change per - # makefile like $(TARGET), so hardcode the target. - command = command.replace('$(TARGET)', self.target) - cd_action = cd_action.replace('$(TARGET)', self.target) - - # Set LD_LIBRARY_PATH in case the action runs an executable from this - # build which links to shared libs from this build. - # actions run on the host, so they should in theory only use host - # libraries, but until everything is made cross-compile safe, also use - # target libraries. - # TODO(piman): when everything is cross-compile safe, remove lib.target - self.WriteLn('cmd_%s = LD_LIBRARY_PATH=$(builddir)/lib.host:' - '$(builddir)/lib.target:$$LD_LIBRARY_PATH; ' - 'export LD_LIBRARY_PATH; ' - '%s%s' - % (name, cd_action, command)) - self.WriteLn() - outputs = [self.Absolutify(o) for o in outputs] - # The makefile rules are all relative to the top dir, but the gyp actions - # are defined relative to their containing dir. This replaces the obj - # variable for the action rule with an absolute version so that the output - # goes in the right place. - # Only write the 'obj' and 'builddir' rules for the "primary" output (:1); - # it's superfluous for the "extra outputs", and this avoids accidentally - # writing duplicate dummy rules for those outputs. - # Same for environment. - self.WriteLn("%s: obj := $(abs_obj)" % QuoteSpaces(outputs[0])) - self.WriteLn("%s: builddir := $(abs_builddir)" % QuoteSpaces(outputs[0])) - self.WriteSortedXcodeEnv(outputs[0], self.GetSortedXcodeEnv()) - - for input in inputs: - assert ' ' not in input, ( - "Spaces in action input filenames not supported (%s)" % input) - for output in outputs: - assert ' ' not in output, ( - "Spaces in action output filenames not supported (%s)" % output) - - # See the comment in WriteCopies about expanding env vars. - outputs = [gyp.xcode_emulation.ExpandEnvVars(o, env) for o in outputs] - inputs = [gyp.xcode_emulation.ExpandEnvVars(i, env) for i in inputs] - - self.WriteDoCmd(outputs, [Sourceify(self.Absolutify(i)) for i in inputs], - part_of_all=part_of_all, command=name) - - # Stuff the outputs in a variable so we can refer to them later. - outputs_variable = 'action_%s_outputs' % name - self.WriteLn('%s := %s' % (outputs_variable, ' '.join(outputs))) - extra_outputs.append('$(%s)' % outputs_variable) - self.WriteLn() - - self.WriteLn() - - - def WriteRules(self, rules, extra_sources, extra_outputs, - extra_mac_bundle_resources, part_of_all): - """Write Makefile code for any 'rules' from the gyp input. + env = self.GetSortedXcodeEnv() + for action in actions: + name = StringToMakefileVariable( + "%s_%s" % (self.qualified_target, action["action_name"]) + ) + self.WriteLn('### Rules for action "%s":' % action["action_name"]) + inputs = action["inputs"] + outputs = action["outputs"] + + # Build up a list of outputs. + # Collect the output dirs we'll need. + dirs = set() + for out in outputs: + dir = os.path.split(out)[0] + if dir: + dirs.add(dir) + if int(action.get("process_outputs_as_sources", False)): + extra_sources += outputs + if int(action.get("process_outputs_as_mac_bundle_resources", False)): + extra_mac_bundle_resources += outputs + + # Write the actual command. + action_commands = action["action"] + if self.flavor == "mac": + action_commands = [ + gyp.xcode_emulation.ExpandEnvVars(command, env) + for command in action_commands + ] + command = gyp.common.EncodePOSIXShellList(action_commands) + if "message" in action: + self.WriteLn("quiet_cmd_%s = ACTION %s $@" % (name, action["message"])) + else: + self.WriteLn("quiet_cmd_%s = ACTION %s $@" % (name, name)) + if len(dirs) > 0: + command = "mkdir -p %s" % " ".join(dirs) + "; " + command + + cd_action = "cd %s; " % Sourceify(self.path or ".") + + # command and cd_action get written to a toplevel variable called + # cmd_foo. Toplevel variables can't handle things that change per + # makefile like $(TARGET), so hardcode the target. + command = command.replace("$(TARGET)", self.target) + cd_action = cd_action.replace("$(TARGET)", self.target) + + # Set LD_LIBRARY_PATH in case the action runs an executable from this + # build which links to shared libs from this build. + # actions run on the host, so they should in theory only use host + # libraries, but until everything is made cross-compile safe, also use + # target libraries. + # TODO(piman): when everything is cross-compile safe, remove lib.target + self.WriteLn( + "cmd_%s = LD_LIBRARY_PATH=$(builddir)/lib.host:" + "$(builddir)/lib.target:$$LD_LIBRARY_PATH; " + "export LD_LIBRARY_PATH; " + "%s%s" % (name, cd_action, command) + ) + self.WriteLn() + outputs = [self.Absolutify(o) for o in outputs] + # The makefile rules are all relative to the top dir, but the gyp actions + # are defined relative to their containing dir. This replaces the obj + # variable for the action rule with an absolute version so that the output + # goes in the right place. + # Only write the 'obj' and 'builddir' rules for the "primary" output (:1); + # it's superfluous for the "extra outputs", and this avoids accidentally + # writing duplicate dummy rules for those outputs. + # Same for environment. + self.WriteLn("%s: obj := $(abs_obj)" % QuoteSpaces(outputs[0])) + self.WriteLn("%s: builddir := $(abs_builddir)" % QuoteSpaces(outputs[0])) + self.WriteSortedXcodeEnv(outputs[0], self.GetSortedXcodeEnv()) + + for input in inputs: + assert " " not in input, ( + "Spaces in action input filenames not supported (%s)" % input + ) + for output in outputs: + assert " " not in output, ( + "Spaces in action output filenames not supported (%s)" % output + ) + + # See the comment in WriteCopies about expanding env vars. + outputs = [gyp.xcode_emulation.ExpandEnvVars(o, env) for o in outputs] + inputs = [gyp.xcode_emulation.ExpandEnvVars(i, env) for i in inputs] + + self.WriteDoCmd( + outputs, + [Sourceify(self.Absolutify(i)) for i in inputs], + part_of_all=part_of_all, + command=name, + ) + + # Stuff the outputs in a variable so we can refer to them later. + outputs_variable = "action_%s_outputs" % name + self.WriteLn("%s := %s" % (outputs_variable, " ".join(outputs))) + extra_outputs.append("$(%s)" % outputs_variable) + self.WriteLn() + + self.WriteLn() + + def WriteRules( + self, + rules, + extra_sources, + extra_outputs, + extra_mac_bundle_resources, + part_of_all, + ): + """Write Makefile code for any 'rules' from the gyp input. extra_sources: a list that will be filled in with newly generated source files, if any @@ -996,208 +1044,240 @@ def WriteRules(self, rules, extra_sources, extra_outputs, rules (used to make other pieces dependent on these rules) part_of_all: flag indicating this target is part of 'all' """ - env = self.GetSortedXcodeEnv() - for rule in rules: - name = StringToMakefileVariable('%s_%s' % (self.qualified_target, - rule['rule_name'])) - count = 0 - self.WriteLn('### Generated for rule %s:' % name) - - all_outputs = [] - - for rule_source in rule.get('rule_sources', []): - dirs = set() - (rule_source_dirname, rule_source_basename) = os.path.split(rule_source) - (rule_source_root, rule_source_ext) = \ - os.path.splitext(rule_source_basename) - - outputs = [self.ExpandInputRoot(out, rule_source_root, - rule_source_dirname) - for out in rule['outputs']] - - for out in outputs: - dir = os.path.dirname(out) - if dir: - dirs.add(dir) - if int(rule.get('process_outputs_as_sources', False)): - extra_sources += outputs - if int(rule.get('process_outputs_as_mac_bundle_resources', False)): - extra_mac_bundle_resources += outputs - inputs = [Sourceify(self.Absolutify(i)) for i - in [rule_source] + rule.get('inputs', [])] - actions = ['$(call do_cmd,%s_%d)' % (name, count)] - - if name == 'resources_grit': - # HACK: This is ugly. Grit intentionally doesn't touch the - # timestamp of its output file when the file doesn't change, - # which is fine in hash-based dependency systems like scons - # and forge, but not kosher in the make world. After some - # discussion, hacking around it here seems like the least - # amount of pain. - actions += ['@touch --no-create $@'] - - # See the comment in WriteCopies about expanding env vars. - outputs = [gyp.xcode_emulation.ExpandEnvVars(o, env) for o in outputs] - inputs = [gyp.xcode_emulation.ExpandEnvVars(i, env) for i in inputs] - - outputs = [self.Absolutify(o) for o in outputs] - all_outputs += outputs - # Only write the 'obj' and 'builddir' rules for the "primary" output - # (:1); it's superfluous for the "extra outputs", and this avoids - # accidentally writing duplicate dummy rules for those outputs. - self.WriteLn('%s: obj := $(abs_obj)' % outputs[0]) - self.WriteLn('%s: builddir := $(abs_builddir)' % outputs[0]) - self.WriteMakeRule(outputs, inputs, actions, - command="%s_%d" % (name, count)) - # Spaces in rule filenames are not supported, but rule variables have - # spaces in them (e.g. RULE_INPUT_PATH expands to '$(abspath $<)'). - # The spaces within the variables are valid, so remove the variables - # before checking. - variables_with_spaces = re.compile(r'\$\([^ ]* \$<\)') - for output in outputs: - output = re.sub(variables_with_spaces, '', output) - assert ' ' not in output, ( - "Spaces in rule filenames not yet supported (%s)" % output) - self.WriteLn('all_deps += %s' % ' '.join(outputs)) - - action = [self.ExpandInputRoot(ac, rule_source_root, - rule_source_dirname) - for ac in rule['action']] - mkdirs = '' - if len(dirs) > 0: - mkdirs = 'mkdir -p %s; ' % ' '.join(dirs) - cd_action = 'cd %s; ' % Sourceify(self.path or '.') - - # action, cd_action, and mkdirs get written to a toplevel variable - # called cmd_foo. Toplevel variables can't handle things that change - # per makefile like $(TARGET), so hardcode the target. - if self.flavor == 'mac': - action = [gyp.xcode_emulation.ExpandEnvVars(command, env) - for command in action] - action = gyp.common.EncodePOSIXShellList(action) - action = action.replace('$(TARGET)', self.target) - cd_action = cd_action.replace('$(TARGET)', self.target) - mkdirs = mkdirs.replace('$(TARGET)', self.target) - - # Set LD_LIBRARY_PATH in case the rule runs an executable from this - # build which links to shared libs from this build. - # rules run on the host, so they should in theory only use host - # libraries, but until everything is made cross-compile safe, also use - # target libraries. - # TODO(piman): when everything is cross-compile safe, remove lib.target - self.WriteLn( - "cmd_%(name)s_%(count)d = LD_LIBRARY_PATH=" - "$(builddir)/lib.host:$(builddir)/lib.target:$$LD_LIBRARY_PATH; " - "export LD_LIBRARY_PATH; " - "%(cd_action)s%(mkdirs)s%(action)s" % { - 'action': action, - 'cd_action': cd_action, - 'count': count, - 'mkdirs': mkdirs, - 'name': name, - }) - self.WriteLn( - 'quiet_cmd_%(name)s_%(count)d = RULE %(name)s_%(count)d $@' % { - 'count': count, - 'name': name, - }) - self.WriteLn() - count += 1 - - outputs_variable = 'rule_%s_outputs' % name - self.WriteList(all_outputs, outputs_variable) - extra_outputs.append('$(%s)' % outputs_variable) - - self.WriteLn('### Finished generating for rule: %s' % name) - self.WriteLn() - self.WriteLn('### Finished generating for all rules') - self.WriteLn('') - - - def WriteCopies(self, copies, extra_outputs, part_of_all): - """Write Makefile code for any 'copies' from the gyp input. + env = self.GetSortedXcodeEnv() + for rule in rules: + name = StringToMakefileVariable( + "%s_%s" % (self.qualified_target, rule["rule_name"]) + ) + count = 0 + self.WriteLn("### Generated for rule %s:" % name) + + all_outputs = [] + + for rule_source in rule.get("rule_sources", []): + dirs = set() + (rule_source_dirname, rule_source_basename) = os.path.split(rule_source) + (rule_source_root, rule_source_ext) = os.path.splitext( + rule_source_basename + ) + + outputs = [ + self.ExpandInputRoot(out, rule_source_root, rule_source_dirname) + for out in rule["outputs"] + ] + + for out in outputs: + dir = os.path.dirname(out) + if dir: + dirs.add(dir) + if int(rule.get("process_outputs_as_sources", False)): + extra_sources += outputs + if int(rule.get("process_outputs_as_mac_bundle_resources", False)): + extra_mac_bundle_resources += outputs + inputs = [ + Sourceify(self.Absolutify(i)) + for i in [rule_source] + rule.get("inputs", []) + ] + actions = ["$(call do_cmd,%s_%d)" % (name, count)] + + if name == "resources_grit": + # HACK: This is ugly. Grit intentionally doesn't touch the + # timestamp of its output file when the file doesn't change, + # which is fine in hash-based dependency systems like scons + # and forge, but not kosher in the make world. After some + # discussion, hacking around it here seems like the least + # amount of pain. + actions += ["@touch --no-create $@"] + + # See the comment in WriteCopies about expanding env vars. + outputs = [gyp.xcode_emulation.ExpandEnvVars(o, env) for o in outputs] + inputs = [gyp.xcode_emulation.ExpandEnvVars(i, env) for i in inputs] + + outputs = [self.Absolutify(o) for o in outputs] + all_outputs += outputs + # Only write the 'obj' and 'builddir' rules for the "primary" output + # (:1); it's superfluous for the "extra outputs", and this avoids + # accidentally writing duplicate dummy rules for those outputs. + self.WriteLn("%s: obj := $(abs_obj)" % outputs[0]) + self.WriteLn("%s: builddir := $(abs_builddir)" % outputs[0]) + self.WriteMakeRule( + outputs, inputs, actions, command="%s_%d" % (name, count) + ) + # Spaces in rule filenames are not supported, but rule variables have + # spaces in them (e.g. RULE_INPUT_PATH expands to '$(abspath $<)'). + # The spaces within the variables are valid, so remove the variables + # before checking. + variables_with_spaces = re.compile(r"\$\([^ ]* \$<\)") + for output in outputs: + output = re.sub(variables_with_spaces, "", output) + assert " " not in output, ( + "Spaces in rule filenames not yet supported (%s)" % output + ) + self.WriteLn("all_deps += %s" % " ".join(outputs)) + + action = [ + self.ExpandInputRoot(ac, rule_source_root, rule_source_dirname) + for ac in rule["action"] + ] + mkdirs = "" + if len(dirs) > 0: + mkdirs = "mkdir -p %s; " % " ".join(dirs) + cd_action = "cd %s; " % Sourceify(self.path or ".") + + # action, cd_action, and mkdirs get written to a toplevel variable + # called cmd_foo. Toplevel variables can't handle things that change + # per makefile like $(TARGET), so hardcode the target. + if self.flavor == "mac": + action = [ + gyp.xcode_emulation.ExpandEnvVars(command, env) + for command in action + ] + action = gyp.common.EncodePOSIXShellList(action) + action = action.replace("$(TARGET)", self.target) + cd_action = cd_action.replace("$(TARGET)", self.target) + mkdirs = mkdirs.replace("$(TARGET)", self.target) + + # Set LD_LIBRARY_PATH in case the rule runs an executable from this + # build which links to shared libs from this build. + # rules run on the host, so they should in theory only use host + # libraries, but until everything is made cross-compile safe, also use + # target libraries. + # TODO(piman): when everything is cross-compile safe, remove lib.target + self.WriteLn( + "cmd_%(name)s_%(count)d = LD_LIBRARY_PATH=" + "$(builddir)/lib.host:$(builddir)/lib.target:$$LD_LIBRARY_PATH; " + "export LD_LIBRARY_PATH; " + "%(cd_action)s%(mkdirs)s%(action)s" + % { + "action": action, + "cd_action": cd_action, + "count": count, + "mkdirs": mkdirs, + "name": name, + } + ) + self.WriteLn( + "quiet_cmd_%(name)s_%(count)d = RULE %(name)s_%(count)d $@" + % {"count": count, "name": name} + ) + self.WriteLn() + count += 1 + + outputs_variable = "rule_%s_outputs" % name + self.WriteList(all_outputs, outputs_variable) + extra_outputs.append("$(%s)" % outputs_variable) + + self.WriteLn("### Finished generating for rule: %s" % name) + self.WriteLn() + self.WriteLn("### Finished generating for all rules") + self.WriteLn("") + + def WriteCopies(self, copies, extra_outputs, part_of_all): + """Write Makefile code for any 'copies' from the gyp input. extra_outputs: a list that will be filled in with any outputs of this action (used to make other pieces dependent on this action) part_of_all: flag indicating this target is part of 'all' """ - self.WriteLn('### Generated for copy rule.') - - variable = StringToMakefileVariable(self.qualified_target + '_copies') - outputs = [] - for copy in copies: - for path in copy['files']: - # Absolutify() may call normpath, and will strip trailing slashes. - path = Sourceify(self.Absolutify(path)) - filename = os.path.split(path)[1] - output = Sourceify(self.Absolutify(os.path.join(copy['destination'], - filename))) - - # If the output path has variables in it, which happens in practice for - # 'copies', writing the environment as target-local doesn't work, - # because the variables are already needed for the target name. - # Copying the environment variables into global make variables doesn't - # work either, because then the .d files will potentially contain spaces - # after variable expansion, and .d file handling cannot handle spaces. - # As a workaround, manually expand variables at gyp time. Since 'copies' - # can't run scripts, there's no need to write the env then. - # WriteDoCmd() will escape spaces for .d files. - env = self.GetSortedXcodeEnv() - output = gyp.xcode_emulation.ExpandEnvVars(output, env) - path = gyp.xcode_emulation.ExpandEnvVars(path, env) - self.WriteDoCmd([output], [path], 'copy', part_of_all) - outputs.append(output) - self.WriteLn('%s = %s' % (variable, ' '.join(QuoteSpaces(o) for o in outputs))) - extra_outputs.append('$(%s)' % variable) - self.WriteLn() - - - def WriteMacBundleResources(self, resources, bundle_deps): - """Writes Makefile code for 'mac_bundle_resources'.""" - self.WriteLn('### Generated for mac_bundle_resources') - - for output, res in gyp.xcode_emulation.GetMacBundleResources( - generator_default_variables['PRODUCT_DIR'], self.xcode_settings, - [Sourceify(self.Absolutify(r)) for r in resources]): - _, ext = os.path.splitext(output) - if ext != '.xcassets': - # Make does not supports '.xcassets' emulation. - self.WriteDoCmd([output], [res], 'mac_tool,,,copy-bundle-resource', - part_of_all=True) - bundle_deps.append(output) - - - def WriteMacInfoPlist(self, bundle_deps): - """Write Makefile code for bundle Info.plist files.""" - info_plist, out, defines, extra_env = gyp.xcode_emulation.GetMacInfoPlist( - generator_default_variables['PRODUCT_DIR'], self.xcode_settings, - lambda p: Sourceify(self.Absolutify(p))) - if not info_plist: - return - if defines: - # Create an intermediate file to store preprocessed results. - intermediate_plist = ('$(obj).$(TOOLSET)/$(TARGET)/' + - os.path.basename(info_plist)) - self.WriteList(defines, intermediate_plist + ': INFOPLIST_DEFINES', '-D', - quoter=EscapeCppDefine) - self.WriteMakeRule([intermediate_plist], [info_plist], - ['$(call do_cmd,infoplist)', - # "Convert" the plist so that any weird whitespace changes from the - # preprocessor do not affect the XML parser in mac_tool. - '@plutil -convert xml1 $@ $@']) - info_plist = intermediate_plist - # plists can contain envvars and substitute them into the file. - self.WriteSortedXcodeEnv( - out, self.GetSortedXcodeEnv(additional_settings=extra_env)) - self.WriteDoCmd([out], [info_plist], 'mac_tool,,,copy-info-plist', - part_of_all=True) - bundle_deps.append(out) - - - def WriteSources(self, configs, deps, sources, - extra_outputs, extra_link_deps, - part_of_all, precompiled_header): - """Write Makefile code for any 'sources' from the gyp input. + self.WriteLn("### Generated for copy rule.") + + variable = StringToMakefileVariable(self.qualified_target + "_copies") + outputs = [] + for copy in copies: + for path in copy["files"]: + # Absolutify() may call normpath, and will strip trailing slashes. + path = Sourceify(self.Absolutify(path)) + filename = os.path.split(path)[1] + output = Sourceify( + self.Absolutify(os.path.join(copy["destination"], filename)) + ) + + # If the output path has variables in it, which happens in practice for + # 'copies', writing the environment as target-local doesn't work, + # because the variables are already needed for the target name. + # Copying the environment variables into global make variables doesn't + # work either, because then the .d files will potentially contain spaces + # after variable expansion, and .d file handling cannot handle spaces. + # As a workaround, manually expand variables at gyp time. Since 'copies' + # can't run scripts, there's no need to write the env then. + # WriteDoCmd() will escape spaces for .d files. + env = self.GetSortedXcodeEnv() + output = gyp.xcode_emulation.ExpandEnvVars(output, env) + path = gyp.xcode_emulation.ExpandEnvVars(path, env) + self.WriteDoCmd([output], [path], "copy", part_of_all) + outputs.append(output) + self.WriteLn("%s = %s" % (variable, " ".join(QuoteSpaces(o) for o in outputs))) + extra_outputs.append("$(%s)" % variable) + self.WriteLn() + + def WriteMacBundleResources(self, resources, bundle_deps): + """Writes Makefile code for 'mac_bundle_resources'.""" + self.WriteLn("### Generated for mac_bundle_resources") + + for output, res in gyp.xcode_emulation.GetMacBundleResources( + generator_default_variables["PRODUCT_DIR"], + self.xcode_settings, + [Sourceify(self.Absolutify(r)) for r in resources], + ): + _, ext = os.path.splitext(output) + if ext != ".xcassets": + # Make does not supports '.xcassets' emulation. + self.WriteDoCmd( + [output], [res], "mac_tool,,,copy-bundle-resource", part_of_all=True + ) + bundle_deps.append(output) + + def WriteMacInfoPlist(self, bundle_deps): + """Write Makefile code for bundle Info.plist files.""" + info_plist, out, defines, extra_env = gyp.xcode_emulation.GetMacInfoPlist( + generator_default_variables["PRODUCT_DIR"], + self.xcode_settings, + lambda p: Sourceify(self.Absolutify(p)), + ) + if not info_plist: + return + if defines: + # Create an intermediate file to store preprocessed results. + intermediate_plist = "$(obj).$(TOOLSET)/$(TARGET)/" + os.path.basename( + info_plist + ) + self.WriteList( + defines, + intermediate_plist + ": INFOPLIST_DEFINES", + "-D", + quoter=EscapeCppDefine, + ) + self.WriteMakeRule( + [intermediate_plist], + [info_plist], + [ + "$(call do_cmd,infoplist)", + # "Convert" the plist so that any weird whitespace changes from the + # preprocessor do not affect the XML parser in mac_tool. + "@plutil -convert xml1 $@ $@", + ], + ) + info_plist = intermediate_plist + # plists can contain envvars and substitute them into the file. + self.WriteSortedXcodeEnv( + out, self.GetSortedXcodeEnv(additional_settings=extra_env) + ) + self.WriteDoCmd( + [out], [info_plist], "mac_tool,,,copy-info-plist", part_of_all=True + ) + bundle_deps.append(out) + + def WriteSources( + self, + configs, + deps, + sources, + extra_outputs, + extra_link_deps, + part_of_all, + precompiled_header, + ): + """Write Makefile code for any 'sources' from the gyp input. These are source files necessary to build the current target. configs, deps, sources: input from gyp. @@ -1208,256 +1288,288 @@ def WriteSources(self, configs, deps, sources, part_of_all: flag indicating this target is part of 'all' """ - # Write configuration-specific variables for CFLAGS, etc. - for configname in sorted(configs.keys()): - config = configs[configname] - self.WriteList(config.get('defines'), 'DEFS_%s' % configname, prefix='-D', - quoter=EscapeCppDefine) - - if self.flavor == 'mac': - cflags = self.xcode_settings.GetCflags(configname) - cflags_c = self.xcode_settings.GetCflagsC(configname) - cflags_cc = self.xcode_settings.GetCflagsCC(configname) - cflags_objc = self.xcode_settings.GetCflagsObjC(configname) - cflags_objcc = self.xcode_settings.GetCflagsObjCC(configname) - else: - cflags = config.get('cflags') - cflags_c = config.get('cflags_c') - cflags_cc = config.get('cflags_cc') - - self.WriteLn("# Flags passed to all source files.") - self.WriteList(cflags, 'CFLAGS_%s' % configname) - self.WriteLn("# Flags passed to only C files.") - self.WriteList(cflags_c, 'CFLAGS_C_%s' % configname) - self.WriteLn("# Flags passed to only C++ files.") - self.WriteList(cflags_cc, 'CFLAGS_CC_%s' % configname) - if self.flavor == 'mac': - self.WriteLn("# Flags passed to only ObjC files.") - self.WriteList(cflags_objc, 'CFLAGS_OBJC_%s' % configname) - self.WriteLn("# Flags passed to only ObjC++ files.") - self.WriteList(cflags_objcc, 'CFLAGS_OBJCC_%s' % configname) - includes = config.get('include_dirs') - if includes: - includes = [Sourceify(self.Absolutify(i)) for i in includes] - self.WriteList(includes, 'INCS_%s' % configname, prefix='-I') - - compilable = list(filter(Compilable, sources)) - objs = [self.Objectify(self.Absolutify(Target(c))) for c in compilable] - self.WriteList(objs, 'OBJS') - - for obj in objs: - assert ' ' not in obj, ( - "Spaces in object filenames not supported (%s)" % obj) - self.WriteLn('# Add to the list of files we specially track ' - 'dependencies for.') - self.WriteLn('all_deps += $(OBJS)') - self.WriteLn() - - # Make sure our dependencies are built first. - if deps: - self.WriteMakeRule(['$(OBJS)'], deps, - comment = 'Make sure our dependencies are built ' - 'before any of us.', - order_only = True) - - # Make sure the actions and rules run first. - # If they generate any extra headers etc., the per-.o file dep tracking - # will catch the proper rebuilds, so order only is still ok here. - if extra_outputs: - self.WriteMakeRule(['$(OBJS)'], extra_outputs, - comment = 'Make sure our actions/rules run ' - 'before any of us.', - order_only = True) - - pchdeps = precompiled_header.GetObjDependencies(compilable, objs ) - if pchdeps: - self.WriteLn('# Dependencies from obj files to their precompiled headers') - for source, obj, gch in pchdeps: - self.WriteLn('%s: %s' % (obj, gch)) - self.WriteLn('# End precompiled header dependencies') - - if objs: - extra_link_deps.append('$(OBJS)') - self.WriteLn("""\ + # Write configuration-specific variables for CFLAGS, etc. + for configname in sorted(configs.keys()): + config = configs[configname] + self.WriteList( + config.get("defines"), + "DEFS_%s" % configname, + prefix="-D", + quoter=EscapeCppDefine, + ) + + if self.flavor == "mac": + cflags = self.xcode_settings.GetCflags( + configname, + arch=config.get('xcode_configuration_platform') + ) + cflags_c = self.xcode_settings.GetCflagsC(configname) + cflags_cc = self.xcode_settings.GetCflagsCC(configname) + cflags_objc = self.xcode_settings.GetCflagsObjC(configname) + cflags_objcc = self.xcode_settings.GetCflagsObjCC(configname) + else: + cflags = config.get("cflags") + cflags_c = config.get("cflags_c") + cflags_cc = config.get("cflags_cc") + + self.WriteLn("# Flags passed to all source files.") + self.WriteList(cflags, "CFLAGS_%s" % configname) + self.WriteLn("# Flags passed to only C files.") + self.WriteList(cflags_c, "CFLAGS_C_%s" % configname) + self.WriteLn("# Flags passed to only C++ files.") + self.WriteList(cflags_cc, "CFLAGS_CC_%s" % configname) + if self.flavor == "mac": + self.WriteLn("# Flags passed to only ObjC files.") + self.WriteList(cflags_objc, "CFLAGS_OBJC_%s" % configname) + self.WriteLn("# Flags passed to only ObjC++ files.") + self.WriteList(cflags_objcc, "CFLAGS_OBJCC_%s" % configname) + includes = config.get("include_dirs") + if includes: + includes = [Sourceify(self.Absolutify(i)) for i in includes] + self.WriteList(includes, "INCS_%s" % configname, prefix="-I") + + compilable = list(filter(Compilable, sources)) + objs = [self.Objectify(self.Absolutify(Target(c))) for c in compilable] + self.WriteList(objs, "OBJS") + + for obj in objs: + assert " " not in obj, "Spaces in object filenames not supported (%s)" % obj + self.WriteLn( + "# Add to the list of files we specially track " "dependencies for." + ) + self.WriteLn("all_deps += $(OBJS)") + self.WriteLn() + + # Make sure our dependencies are built first. + if deps: + self.WriteMakeRule( + ["$(OBJS)"], + deps, + comment="Make sure our dependencies are built " "before any of us.", + order_only=True, + ) + + # Make sure the actions and rules run first. + # If they generate any extra headers etc., the per-.o file dep tracking + # will catch the proper rebuilds, so order only is still ok here. + if extra_outputs: + self.WriteMakeRule( + ["$(OBJS)"], + extra_outputs, + comment="Make sure our actions/rules run " "before any of us.", + order_only=True, + ) + + pchdeps = precompiled_header.GetObjDependencies(compilable, objs) + if pchdeps: + self.WriteLn("# Dependencies from obj files to their precompiled headers") + for source, obj, gch in pchdeps: + self.WriteLn("%s: %s" % (obj, gch)) + self.WriteLn("# End precompiled header dependencies") + + if objs: + extra_link_deps.append("$(OBJS)") + self.WriteLn( + """\ # CFLAGS et al overrides must be target-local. -# See "Target-specific Variable Values" in the GNU Make manual.""") - self.WriteLn("$(OBJS): TOOLSET := $(TOOLSET)") - self.WriteLn("$(OBJS): GYP_CFLAGS := " - "$(DEFS_$(BUILDTYPE)) " - "$(INCS_$(BUILDTYPE)) " - "%s " % precompiled_header.GetInclude('c') + - "$(CFLAGS_$(BUILDTYPE)) " - "$(CFLAGS_C_$(BUILDTYPE))") - self.WriteLn("$(OBJS): GYP_CXXFLAGS := " - "$(DEFS_$(BUILDTYPE)) " - "$(INCS_$(BUILDTYPE)) " - "%s " % precompiled_header.GetInclude('cc') + - "$(CFLAGS_$(BUILDTYPE)) " - "$(CFLAGS_CC_$(BUILDTYPE))") - if self.flavor == 'mac': - self.WriteLn("$(OBJS): GYP_OBJCFLAGS := " - "$(DEFS_$(BUILDTYPE)) " - "$(INCS_$(BUILDTYPE)) " - "%s " % precompiled_header.GetInclude('m') + - "$(CFLAGS_$(BUILDTYPE)) " - "$(CFLAGS_C_$(BUILDTYPE)) " - "$(CFLAGS_OBJC_$(BUILDTYPE))") - self.WriteLn("$(OBJS): GYP_OBJCXXFLAGS := " - "$(DEFS_$(BUILDTYPE)) " - "$(INCS_$(BUILDTYPE)) " - "%s " % precompiled_header.GetInclude('mm') + - "$(CFLAGS_$(BUILDTYPE)) " - "$(CFLAGS_CC_$(BUILDTYPE)) " - "$(CFLAGS_OBJCC_$(BUILDTYPE))") - - self.WritePchTargets(precompiled_header.GetPchBuildCommands()) - - # If there are any object files in our input file list, link them into our - # output. - extra_link_deps += list(filter(Linkable, sources)) - - self.WriteLn() - - def WritePchTargets(self, pch_commands): - """Writes make rules to compile prefix headers.""" - if not pch_commands: - return - - for gch, lang_flag, lang, input in pch_commands: - extra_flags = { - 'c': '$(CFLAGS_C_$(BUILDTYPE))', - 'cc': '$(CFLAGS_CC_$(BUILDTYPE))', - 'm': '$(CFLAGS_C_$(BUILDTYPE)) $(CFLAGS_OBJC_$(BUILDTYPE))', - 'mm': '$(CFLAGS_CC_$(BUILDTYPE)) $(CFLAGS_OBJCC_$(BUILDTYPE))', - }[lang] - var_name = { - 'c': 'GYP_PCH_CFLAGS', - 'cc': 'GYP_PCH_CXXFLAGS', - 'm': 'GYP_PCH_OBJCFLAGS', - 'mm': 'GYP_PCH_OBJCXXFLAGS', - }[lang] - self.WriteLn("%s: %s := %s " % (gch, var_name, lang_flag) + - "$(DEFS_$(BUILDTYPE)) " - "$(INCS_$(BUILDTYPE)) " - "$(CFLAGS_$(BUILDTYPE)) " + - extra_flags) - - self.WriteLn('%s: %s FORCE_DO_CMD' % (gch, input)) - self.WriteLn('\t@$(call do_cmd,pch_%s,1)' % lang) - self.WriteLn('') - assert ' ' not in gch, ( - "Spaces in gch filenames not supported (%s)" % gch) - self.WriteLn('all_deps += %s' % gch) - self.WriteLn('') - - - def ComputeOutputBasename(self, spec): - """Return the 'output basename' of a gyp spec. +# See "Target-specific Variable Values" in the GNU Make manual.""" + ) + self.WriteLn("$(OBJS): TOOLSET := $(TOOLSET)") + self.WriteLn( + "$(OBJS): GYP_CFLAGS := " + "$(DEFS_$(BUILDTYPE)) " + "$(INCS_$(BUILDTYPE)) " + "%s " % precompiled_header.GetInclude("c") + "$(CFLAGS_$(BUILDTYPE)) " + "$(CFLAGS_C_$(BUILDTYPE))" + ) + self.WriteLn( + "$(OBJS): GYP_CXXFLAGS := " + "$(DEFS_$(BUILDTYPE)) " + "$(INCS_$(BUILDTYPE)) " + "%s " % precompiled_header.GetInclude("cc") + "$(CFLAGS_$(BUILDTYPE)) " + "$(CFLAGS_CC_$(BUILDTYPE))" + ) + if self.flavor == "mac": + self.WriteLn( + "$(OBJS): GYP_OBJCFLAGS := " + "$(DEFS_$(BUILDTYPE)) " + "$(INCS_$(BUILDTYPE)) " + "%s " % precompiled_header.GetInclude("m") + + "$(CFLAGS_$(BUILDTYPE)) " + "$(CFLAGS_C_$(BUILDTYPE)) " + "$(CFLAGS_OBJC_$(BUILDTYPE))" + ) + self.WriteLn( + "$(OBJS): GYP_OBJCXXFLAGS := " + "$(DEFS_$(BUILDTYPE)) " + "$(INCS_$(BUILDTYPE)) " + "%s " % precompiled_header.GetInclude("mm") + + "$(CFLAGS_$(BUILDTYPE)) " + "$(CFLAGS_CC_$(BUILDTYPE)) " + "$(CFLAGS_OBJCC_$(BUILDTYPE))" + ) + + self.WritePchTargets(precompiled_header.GetPchBuildCommands()) + + # If there are any object files in our input file list, link them into our + # output. + extra_link_deps += [source for source in sources if Linkable(source)] + + self.WriteLn() + + def WritePchTargets(self, pch_commands): + """Writes make rules to compile prefix headers.""" + if not pch_commands: + return + + for gch, lang_flag, lang, input in pch_commands: + extra_flags = { + "c": "$(CFLAGS_C_$(BUILDTYPE))", + "cc": "$(CFLAGS_CC_$(BUILDTYPE))", + "m": "$(CFLAGS_C_$(BUILDTYPE)) $(CFLAGS_OBJC_$(BUILDTYPE))", + "mm": "$(CFLAGS_CC_$(BUILDTYPE)) $(CFLAGS_OBJCC_$(BUILDTYPE))", + }[lang] + var_name = { + "c": "GYP_PCH_CFLAGS", + "cc": "GYP_PCH_CXXFLAGS", + "m": "GYP_PCH_OBJCFLAGS", + "mm": "GYP_PCH_OBJCXXFLAGS", + }[lang] + self.WriteLn( + "%s: %s := %s " % (gch, var_name, lang_flag) + "$(DEFS_$(BUILDTYPE)) " + "$(INCS_$(BUILDTYPE)) " + "$(CFLAGS_$(BUILDTYPE)) " + extra_flags + ) + + self.WriteLn("%s: %s FORCE_DO_CMD" % (gch, input)) + self.WriteLn("\t@$(call do_cmd,pch_%s,1)" % lang) + self.WriteLn("") + assert " " not in gch, "Spaces in gch filenames not supported (%s)" % gch + self.WriteLn("all_deps += %s" % gch) + self.WriteLn("") + + def ComputeOutputBasename(self, spec): + """Return the 'output basename' of a gyp spec. E.g., the loadable module 'foobar' in directory 'baz' will produce 'libfoobar.so' """ - assert not self.is_mac_bundle - - if self.flavor == 'mac' and self.type in ( - 'static_library', 'executable', 'shared_library', 'loadable_module'): - return self.xcode_settings.GetExecutablePath() - - target = spec['target_name'] - target_prefix = '' - target_ext = '' - if self.type == 'static_library': - if target[:3] == 'lib': - target = target[3:] - target_prefix = 'lib' - target_ext = '.a' - elif self.type in ('loadable_module', 'shared_library'): - if target[:3] == 'lib': - target = target[3:] - target_prefix = 'lib' - if self.flavor == 'aix': - target_ext = '.a' - else: - target_ext = '.so' - elif self.type == 'none': - target = '%s.stamp' % target - elif self.type != 'executable': - print("ERROR: What output file should be generated?", - "type", self.type, "target", target) - - target_prefix = spec.get('product_prefix', target_prefix) - target = spec.get('product_name', target) - product_ext = spec.get('product_extension') - if product_ext: - target_ext = '.' + product_ext - - return target_prefix + target + target_ext - - - def _InstallImmediately(self): - return self.toolset == 'target' and self.flavor == 'mac' and self.type in ( - 'static_library', 'executable', 'shared_library', 'loadable_module') - - - def ComputeOutput(self, spec): - """Return the 'output' (full output path) of a gyp spec. + assert not self.is_mac_bundle + + if self.flavor == "mac" and self.type in ( + "static_library", + "executable", + "shared_library", + "loadable_module", + ): + return self.xcode_settings.GetExecutablePath() + + target = spec["target_name"] + target_prefix = "" + target_ext = "" + if self.type == "static_library": + if target[:3] == "lib": + target = target[3:] + target_prefix = "lib" + target_ext = ".a" + elif self.type in ("loadable_module", "shared_library"): + if target[:3] == "lib": + target = target[3:] + target_prefix = "lib" + if self.flavor == "aix": + target_ext = ".a" + else: + target_ext = ".so" + elif self.type == "none": + target = "%s.stamp" % target + elif self.type != "executable": + print( + "ERROR: What output file should be generated?", + "type", + self.type, + "target", + target, + ) + + target_prefix = spec.get("product_prefix", target_prefix) + target = spec.get("product_name", target) + product_ext = spec.get("product_extension") + if product_ext: + target_ext = "." + product_ext + + return target_prefix + target + target_ext + + def _InstallImmediately(self): + return ( + self.toolset == "target" + and self.flavor == "mac" + and self.type + in ("static_library", "executable", "shared_library", "loadable_module") + ) + + def ComputeOutput(self, spec): + """Return the 'output' (full output path) of a gyp spec. E.g., the loadable module 'foobar' in directory 'baz' will produce '$(obj)/baz/libfoobar.so' """ - assert not self.is_mac_bundle - - path = os.path.join('$(obj).' + self.toolset, self.path) - if self.type == 'executable' or self._InstallImmediately(): - path = '$(builddir)' - path = spec.get('product_dir', path) - return os.path.join(path, self.ComputeOutputBasename(spec)) - + assert not self.is_mac_bundle - def ComputeMacBundleOutput(self, spec): - """Return the 'output' (full output path) to a bundle output directory.""" - assert self.is_mac_bundle - path = generator_default_variables['PRODUCT_DIR'] - return os.path.join(path, self.xcode_settings.GetWrapperName()) + path = os.path.join("$(obj)." + self.toolset, self.path) + if self.type == "executable" or self._InstallImmediately(): + path = "$(builddir)" + path = spec.get("product_dir", path) + return os.path.join(path, self.ComputeOutputBasename(spec)) + def ComputeMacBundleOutput(self, spec): + """Return the 'output' (full output path) to a bundle output directory.""" + assert self.is_mac_bundle + path = generator_default_variables["PRODUCT_DIR"] + return os.path.join(path, self.xcode_settings.GetWrapperName()) - def ComputeMacBundleBinaryOutput(self, spec): - """Return the 'output' (full output path) to the binary in a bundle.""" - path = generator_default_variables['PRODUCT_DIR'] - return os.path.join(path, self.xcode_settings.GetExecutablePath()) + def ComputeMacBundleBinaryOutput(self, spec): + """Return the 'output' (full output path) to the binary in a bundle.""" + path = generator_default_variables["PRODUCT_DIR"] + return os.path.join(path, self.xcode_settings.GetExecutablePath()) - - def ComputeDeps(self, spec): - """Compute the dependencies of a gyp spec. + def ComputeDeps(self, spec): + """Compute the dependencies of a gyp spec. Returns a tuple (deps, link_deps), where each is a list of filenames that will need to be put in front of make for either building (deps) or linking (link_deps). """ - deps = [] - link_deps = [] - if 'dependencies' in spec: - deps.extend([target_outputs[dep] for dep in spec['dependencies'] - if target_outputs[dep]]) - for dep in spec['dependencies']: - if dep in target_link_deps: - link_deps.append(target_link_deps[dep]) - deps.extend(link_deps) - # TODO: It seems we need to transitively link in libraries (e.g. -lfoo)? - # This hack makes it work: - # link_deps.extend(spec.get('libraries', [])) - return (gyp.common.uniquer(deps), gyp.common.uniquer(link_deps)) - - - def WriteDependencyOnExtraOutputs(self, target, extra_outputs): - self.WriteMakeRule([self.output_binary], extra_outputs, - comment = 'Build our special outputs first.', - order_only = True) - - - def WriteTarget(self, spec, configs, deps, link_deps, bundle_deps, - extra_outputs, part_of_all): - """Write Makefile code to produce the final target of the gyp spec. + deps = [] + link_deps = [] + if "dependencies" in spec: + deps.extend( + [ + target_outputs[dep] + for dep in spec["dependencies"] + if target_outputs[dep] + ] + ) + for dep in spec["dependencies"]: + if dep in target_link_deps: + link_deps.append(target_link_deps[dep]) + deps.extend(link_deps) + # TODO: It seems we need to transitively link in libraries (e.g. -lfoo)? + # This hack makes it work: + # link_deps.extend(spec.get('libraries', [])) + return (gyp.common.uniquer(deps), gyp.common.uniquer(link_deps)) + + def WriteDependencyOnExtraOutputs(self, target, extra_outputs): + self.WriteMakeRule( + [self.output_binary], + extra_outputs, + comment="Build our special outputs first.", + order_only=True, + ) + + def WriteTarget( + self, spec, configs, deps, link_deps, bundle_deps, extra_outputs, part_of_all + ): + """Write Makefile code to produce the final target of the gyp spec. spec, configs: input from gyp. deps, link_deps: dependency lists; see ComputeDeps() @@ -1465,274 +1577,377 @@ def WriteTarget(self, spec, configs, deps, link_deps, bundle_deps, part_of_all: flag indicating this target is part of 'all' """ - self.WriteLn('### Rules for final target.') - - if extra_outputs: - self.WriteDependencyOnExtraOutputs(self.output_binary, extra_outputs) - self.WriteMakeRule(extra_outputs, deps, - comment=('Preserve order dependency of ' - 'special output on deps.'), - order_only = True) - - target_postbuilds = {} - if self.type != 'none': - for configname in sorted(configs.keys()): - config = configs[configname] - if self.flavor == 'mac': - ldflags = self.xcode_settings.GetLdflags(configname, - generator_default_variables['PRODUCT_DIR'], - lambda p: Sourceify(self.Absolutify(p))) - - # TARGET_POSTBUILDS_$(BUILDTYPE) is added to postbuilds later on. - gyp_to_build = gyp.common.InvertRelativePath(self.path) - target_postbuild = self.xcode_settings.AddImplicitPostbuilds( - configname, - QuoteSpaces(os.path.normpath(os.path.join(gyp_to_build, - self.output))), - QuoteSpaces(os.path.normpath(os.path.join(gyp_to_build, - self.output_binary)))) - if target_postbuild: - target_postbuilds[configname] = target_postbuild + self.WriteLn("### Rules for final target.") + + if extra_outputs: + self.WriteDependencyOnExtraOutputs(self.output_binary, extra_outputs) + self.WriteMakeRule( + extra_outputs, + deps, + comment=("Preserve order dependency of " "special output on deps."), + order_only=True, + ) + + target_postbuilds = {} + if self.type != "none": + for configname in sorted(configs.keys()): + config = configs[configname] + if self.flavor == "mac": + ldflags = self.xcode_settings.GetLdflags( + configname, + generator_default_variables["PRODUCT_DIR"], + lambda p: Sourceify(self.Absolutify(p)), + arch=config.get('xcode_configuration_platform') + ) + + # TARGET_POSTBUILDS_$(BUILDTYPE) is added to postbuilds later on. + gyp_to_build = gyp.common.InvertRelativePath(self.path) + target_postbuild = self.xcode_settings.AddImplicitPostbuilds( + configname, + QuoteSpaces( + os.path.normpath(os.path.join(gyp_to_build, self.output)) + ), + QuoteSpaces( + os.path.normpath( + os.path.join(gyp_to_build, self.output_binary) + ) + ), + ) + if target_postbuild: + target_postbuilds[configname] = target_postbuild + else: + ldflags = config.get("ldflags", []) + # Compute an rpath for this output if needed. + if any(dep.endswith(".so") or ".so." in dep for dep in deps): + # We want to get the literal string "$ORIGIN" + # into the link command, so we need lots of escaping. + ldflags.append(r"-Wl,-rpath=\$$ORIGIN/lib.%s/" % self.toolset) + ldflags.append( + r"-Wl,-rpath-link=\$(builddir)/lib.%s/" % self.toolset + ) + library_dirs = config.get("library_dirs", []) + ldflags += [("-L%s" % library_dir) for library_dir in library_dirs] + self.WriteList(ldflags, "LDFLAGS_%s" % configname) + if self.flavor == "mac": + self.WriteList( + self.xcode_settings.GetLibtoolflags(configname), + "LIBTOOLFLAGS_%s" % configname, + ) + libraries = spec.get("libraries") + if libraries: + # Remove duplicate entries + libraries = gyp.common.uniquer(libraries) + if self.flavor == "mac": + libraries = self.xcode_settings.AdjustLibraries(libraries) + self.WriteList(libraries, "LIBS") + self.WriteLn( + "%s: GYP_LDFLAGS := $(LDFLAGS_$(BUILDTYPE))" + % QuoteSpaces(self.output_binary) + ) + self.WriteLn("%s: LIBS := $(LIBS)" % QuoteSpaces(self.output_binary)) + + if self.flavor == "mac": + self.WriteLn( + "%s: GYP_LIBTOOLFLAGS := $(LIBTOOLFLAGS_$(BUILDTYPE))" + % QuoteSpaces(self.output_binary) + ) + + # Postbuild actions. Like actions, but implicitly depend on the target's + # output. + postbuilds = [] + if self.flavor == "mac": + if target_postbuilds: + postbuilds.append("$(TARGET_POSTBUILDS_$(BUILDTYPE))") + postbuilds.extend(gyp.xcode_emulation.GetSpecPostbuildCommands(spec)) + + if postbuilds: + # Envvars may be referenced by TARGET_POSTBUILDS_$(BUILDTYPE), + # so we must output its definition first, since we declare variables + # using ":=". + self.WriteSortedXcodeEnv(self.output, self.GetSortedXcodePostbuildEnv()) + + for configname in target_postbuilds: + self.WriteLn( + "%s: TARGET_POSTBUILDS_%s := %s" + % ( + QuoteSpaces(self.output), + configname, + gyp.common.EncodePOSIXShellList(target_postbuilds[configname]), + ) + ) + + # Postbuilds expect to be run in the gyp file's directory, so insert an + # implicit postbuild to cd to there. + postbuilds.insert(0, gyp.common.EncodePOSIXShellList(["cd", self.path])) + for i, postbuild in enumerate(postbuilds): + if not postbuild.startswith("$"): + postbuilds[i] = EscapeShellArgument(postbuild) + self.WriteLn("%s: builddir := $(abs_builddir)" % QuoteSpaces(self.output)) + self.WriteLn( + "%s: POSTBUILDS := %s" + % (QuoteSpaces(self.output), " ".join(postbuilds)) + ) + + # A bundle directory depends on its dependencies such as bundle resources + # and bundle binary. When all dependencies have been built, the bundle + # needs to be packaged. + if self.is_mac_bundle: + # If the framework doesn't contain a binary, then nothing depends + # on the actions -- make the framework depend on them directly too. + self.WriteDependencyOnExtraOutputs(self.output, extra_outputs) + + # Bundle dependencies. Note that the code below adds actions to this + # target, so if you move these two lines, move the lines below as well. + self.WriteList([QuoteSpaces(dep) for dep in bundle_deps], "BUNDLE_DEPS") + self.WriteLn("%s: $(BUNDLE_DEPS)" % QuoteSpaces(self.output)) + + # After the framework is built, package it. Needs to happen before + # postbuilds, since postbuilds depend on this. + if self.type in ("shared_library", "loadable_module"): + self.WriteLn( + "\t@$(call do_cmd,mac_package_framework,,,%s)" + % self.xcode_settings.GetFrameworkVersion() + ) + + # Bundle postbuilds can depend on the whole bundle, so run them after + # the bundle is packaged, not already after the bundle binary is done. + if postbuilds: + self.WriteLn("\t@$(call do_postbuilds)") + postbuilds = [] # Don't write postbuilds for target's output. + + # Needed by test/mac/gyptest-rebuild.py. + self.WriteLn("\t@true # No-op, used by tests") + + # Since this target depends on binary and resources which are in + # nested subfolders, the framework directory will be older than + # its dependencies usually. To prevent this rule from executing + # on every build (expensive, especially with postbuilds), expliclity + # update the time on the framework directory. + self.WriteLn("\t@touch -c %s" % QuoteSpaces(self.output)) + + if postbuilds: + assert not self.is_mac_bundle, ( + "Postbuilds for bundles should be done " + "on the bundle, not the binary (target '%s')" % self.target + ) + assert "product_dir" not in spec, ( + "Postbuilds do not work with " "custom product_dir" + ) + + if self.type == "executable": + self.WriteLn( + "%s: LD_INPUTS := %s" + % ( + QuoteSpaces(self.output_binary), + " ".join(QuoteSpaces(dep) for dep in link_deps), + ) + ) + if self.toolset == "host" and self.flavor == "android": + self.WriteDoCmd( + [self.output_binary], + link_deps, + "link_host", + part_of_all, + postbuilds=postbuilds, + ) + else: + self.WriteDoCmd( + [self.output_binary], + link_deps, + "link", + part_of_all, + postbuilds=postbuilds, + ) + + elif self.type == "static_library": + for link_dep in link_deps: + assert " " not in link_dep, ( + "Spaces in alink input filenames not supported (%s)" % link_dep + ) + if ( + self.flavor not in ("mac", "openbsd", "netbsd", "win") + and not self.is_standalone_static_library + ): + self.WriteDoCmd( + [self.output_binary], + link_deps, + "alink_thin", + part_of_all, + postbuilds=postbuilds, + ) + else: + self.WriteDoCmd( + [self.output_binary], + link_deps, + "alink", + part_of_all, + postbuilds=postbuilds, + ) + elif self.type == "shared_library": + self.WriteLn( + "%s: LD_INPUTS := %s" + % ( + QuoteSpaces(self.output_binary), + " ".join(QuoteSpaces(dep) for dep in link_deps), + ) + ) + self.WriteDoCmd( + [self.output_binary], + link_deps, + "solink", + part_of_all, + postbuilds=postbuilds, + ) + elif self.type == "loadable_module": + for link_dep in link_deps: + assert " " not in link_dep, ( + "Spaces in module input filenames not supported (%s)" % link_dep + ) + if self.toolset == "host" and self.flavor == "android": + self.WriteDoCmd( + [self.output_binary], + link_deps, + "solink_module_host", + part_of_all, + postbuilds=postbuilds, + ) + else: + self.WriteDoCmd( + [self.output_binary], + link_deps, + "solink_module", + part_of_all, + postbuilds=postbuilds, + ) + elif self.type == "none": + # Write a stamp line. + self.WriteDoCmd( + [self.output_binary], deps, "touch", part_of_all, postbuilds=postbuilds + ) else: - ldflags = config.get('ldflags', []) - # Compute an rpath for this output if needed. - if any(dep.endswith('.so') or '.so.' in dep for dep in deps): - # We want to get the literal string "$ORIGIN" into the link command, - # so we need lots of escaping. - ldflags.append(r'-Wl,-rpath=\$$ORIGIN/lib.%s/' % self.toolset) - ldflags.append(r'-Wl,-rpath-link=\$(builddir)/lib.%s/' % - self.toolset) - library_dirs = config.get('library_dirs', []) - ldflags += [('-L%s' % library_dir) for library_dir in library_dirs] - self.WriteList(ldflags, 'LDFLAGS_%s' % configname) - if self.flavor == 'mac': - self.WriteList(self.xcode_settings.GetLibtoolflags(configname), - 'LIBTOOLFLAGS_%s' % configname) - libraries = spec.get('libraries') - if libraries: - # Remove duplicate entries - libraries = gyp.common.uniquer(libraries) - if self.flavor == 'mac': - libraries = self.xcode_settings.AdjustLibraries(libraries) - self.WriteList(libraries, 'LIBS') - self.WriteLn('%s: GYP_LDFLAGS := $(LDFLAGS_$(BUILDTYPE))' % - QuoteSpaces(self.output_binary)) - self.WriteLn('%s: LIBS := $(LIBS)' % QuoteSpaces(self.output_binary)) - - if self.flavor == 'mac': - self.WriteLn('%s: GYP_LIBTOOLFLAGS := $(LIBTOOLFLAGS_$(BUILDTYPE))' % - QuoteSpaces(self.output_binary)) - - # Postbuild actions. Like actions, but implicitly depend on the target's - # output. - postbuilds = [] - if self.flavor == 'mac': - if target_postbuilds: - postbuilds.append('$(TARGET_POSTBUILDS_$(BUILDTYPE))') - postbuilds.extend( - gyp.xcode_emulation.GetSpecPostbuildCommands(spec)) - - if postbuilds: - # Envvars may be referenced by TARGET_POSTBUILDS_$(BUILDTYPE), - # so we must output its definition first, since we declare variables - # using ":=". - self.WriteSortedXcodeEnv(self.output, self.GetSortedXcodePostbuildEnv()) - - for configname in target_postbuilds: - self.WriteLn('%s: TARGET_POSTBUILDS_%s := %s' % - (QuoteSpaces(self.output), - configname, - gyp.common.EncodePOSIXShellList(target_postbuilds[configname]))) - - # Postbuilds expect to be run in the gyp file's directory, so insert an - # implicit postbuild to cd to there. - postbuilds.insert(0, gyp.common.EncodePOSIXShellList(['cd', self.path])) - for i in range(len(postbuilds)): - if not postbuilds[i].startswith('$'): - postbuilds[i] = EscapeShellArgument(postbuilds[i]) - self.WriteLn('%s: builddir := $(abs_builddir)' % QuoteSpaces(self.output)) - self.WriteLn('%s: POSTBUILDS := %s' % ( - QuoteSpaces(self.output), ' '.join(postbuilds))) - - # A bundle directory depends on its dependencies such as bundle resources - # and bundle binary. When all dependencies have been built, the bundle - # needs to be packaged. - if self.is_mac_bundle: - # If the framework doesn't contain a binary, then nothing depends - # on the actions -- make the framework depend on them directly too. - self.WriteDependencyOnExtraOutputs(self.output, extra_outputs) - - # Bundle dependencies. Note that the code below adds actions to this - # target, so if you move these two lines, move the lines below as well. - self.WriteList([QuoteSpaces(dep) for dep in bundle_deps], 'BUNDLE_DEPS') - self.WriteLn('%s: $(BUNDLE_DEPS)' % QuoteSpaces(self.output)) - - # After the framework is built, package it. Needs to happen before - # postbuilds, since postbuilds depend on this. - if self.type in ('shared_library', 'loadable_module'): - self.WriteLn('\t@$(call do_cmd,mac_package_framework,,,%s)' % - self.xcode_settings.GetFrameworkVersion()) - - # Bundle postbuilds can depend on the whole bundle, so run them after - # the bundle is packaged, not already after the bundle binary is done. - if postbuilds: - self.WriteLn('\t@$(call do_postbuilds)') - postbuilds = [] # Don't write postbuilds for target's output. - - # Needed by test/mac/gyptest-rebuild.py. - self.WriteLn('\t@true # No-op, used by tests') - - # Since this target depends on binary and resources which are in - # nested subfolders, the framework directory will be older than - # its dependencies usually. To prevent this rule from executing - # on every build (expensive, especially with postbuilds), expliclity - # update the time on the framework directory. - self.WriteLn('\t@touch -c %s' % QuoteSpaces(self.output)) - - if postbuilds: - assert not self.is_mac_bundle, ('Postbuilds for bundles should be done ' - 'on the bundle, not the binary (target \'%s\')' % self.target) - assert 'product_dir' not in spec, ('Postbuilds do not work with ' - 'custom product_dir') - - if self.type == 'executable': - self.WriteLn('%s: LD_INPUTS := %s' % ( - QuoteSpaces(self.output_binary), - ' '.join(QuoteSpaces(dep) for dep in link_deps))) - if self.toolset == 'host' and self.flavor == 'android': - self.WriteDoCmd([self.output_binary], link_deps, 'link_host', - part_of_all, postbuilds=postbuilds) - else: - self.WriteDoCmd([self.output_binary], link_deps, 'link', part_of_all, - postbuilds=postbuilds) - - elif self.type == 'static_library': - for link_dep in link_deps: - assert ' ' not in link_dep, ( - "Spaces in alink input filenames not supported (%s)" % link_dep) - if (self.flavor not in ('mac', 'openbsd', 'netbsd', 'win') and not - self.is_standalone_static_library): - self.WriteDoCmd([self.output_binary], link_deps, 'alink_thin', - part_of_all, postbuilds=postbuilds) - else: - self.WriteDoCmd([self.output_binary], link_deps, 'alink', part_of_all, - postbuilds=postbuilds) - elif self.type == 'shared_library': - self.WriteLn('%s: LD_INPUTS := %s' % ( - QuoteSpaces(self.output_binary), - ' '.join(QuoteSpaces(dep) for dep in link_deps))) - self.WriteDoCmd([self.output_binary], link_deps, 'solink', part_of_all, - postbuilds=postbuilds) - elif self.type == 'loadable_module': - for link_dep in link_deps: - assert ' ' not in link_dep, ( - "Spaces in module input filenames not supported (%s)" % link_dep) - if self.toolset == 'host' and self.flavor == 'android': - self.WriteDoCmd([self.output_binary], link_deps, 'solink_module_host', - part_of_all, postbuilds=postbuilds) - else: - self.WriteDoCmd( - [self.output_binary], link_deps, 'solink_module', part_of_all, - postbuilds=postbuilds) - elif self.type == 'none': - # Write a stamp line. - self.WriteDoCmd([self.output_binary], deps, 'touch', part_of_all, - postbuilds=postbuilds) - else: - print("WARNING: no output for", self.type, self.target) - - # Add an alias for each target (if there are any outputs). - # Installable target aliases are created below. - if ((self.output and self.output != self.target) and - (self.type not in self._INSTALLABLE_TARGETS)): - self.WriteMakeRule([self.target], [self.output], - comment='Add target alias', phony = True) - if part_of_all: - self.WriteMakeRule(['all'], [self.target], - comment = 'Add target alias to "all" target.', - phony = True) - - # Add special-case rules for our installable targets. - # 1) They need to install to the build dir or "product" dir. - # 2) They get shortcuts for building (e.g. "make chrome"). - # 3) They are part of "make all". - if (self.type in self._INSTALLABLE_TARGETS or - self.is_standalone_static_library): - if self.type == 'shared_library': - file_desc = 'shared library' - elif self.type == 'static_library': - file_desc = 'static library' - else: - file_desc = 'executable' - install_path = self._InstallableTargetInstallPath() - installable_deps = [self.output] - if (self.flavor == 'mac' and not 'product_dir' in spec and - self.toolset == 'target'): - # On mac, products are created in install_path immediately. - assert install_path == self.output, '%s != %s' % ( - install_path, self.output) - - # Point the target alias to the final binary output. - self.WriteMakeRule([self.target], [install_path], - comment='Add target alias', phony = True) - if install_path != self.output: - assert not self.is_mac_bundle # See comment a few lines above. - self.WriteDoCmd([install_path], [self.output], 'copy', - comment = 'Copy this to the %s output path.' % - file_desc, part_of_all=part_of_all) - installable_deps.append(install_path) - if self.output != self.alias and self.alias != self.target: - self.WriteMakeRule([self.alias], installable_deps, - comment = 'Short alias for building this %s.' % - file_desc, phony = True) - if part_of_all: - self.WriteMakeRule(['all'], [install_path], - comment = 'Add %s to "all" target.' % file_desc, - phony = True) - - - def WriteList(self, value_list, variable=None, prefix='', - quoter=QuoteIfNecessary): - """Write a variable definition that is a list of values. + print("WARNING: no output for", self.type, self.target) + + # Add an alias for each target (if there are any outputs). + # Installable target aliases are created below. + if (self.output and self.output != self.target) and ( + self.type not in self._INSTALLABLE_TARGETS + ): + self.WriteMakeRule( + [self.target], [self.output], comment="Add target alias", phony=True + ) + if part_of_all: + self.WriteMakeRule( + ["all"], + [self.target], + comment='Add target alias to "all" target.', + phony=True, + ) + + # Add special-case rules for our installable targets. + # 1) They need to install to the build dir or "product" dir. + # 2) They get shortcuts for building (e.g. "make chrome"). + # 3) They are part of "make all". + if self.type in self._INSTALLABLE_TARGETS or self.is_standalone_static_library: + if self.type == "shared_library": + file_desc = "shared library" + elif self.type == "static_library": + file_desc = "static library" + else: + file_desc = "executable" + install_path = self._InstallableTargetInstallPath() + installable_deps = [self.output] + if ( + self.flavor == "mac" + and "product_dir" not in spec + and self.toolset == "target" + ): + # On mac, products are created in install_path immediately. + assert install_path == self.output, "%s != %s" % ( + install_path, + self.output, + ) + + # Point the target alias to the final binary output. + self.WriteMakeRule( + [self.target], [install_path], comment="Add target alias", phony=True + ) + if install_path != self.output: + assert not self.is_mac_bundle # See comment a few lines above. + self.WriteDoCmd( + [install_path], + [self.output], + "copy", + comment="Copy this to the %s output path." % file_desc, + part_of_all=part_of_all, + ) + installable_deps.append(install_path) + if self.output != self.alias and self.alias != self.target: + self.WriteMakeRule( + [self.alias], + installable_deps, + comment="Short alias for building this %s." % file_desc, + phony=True, + ) + if part_of_all: + self.WriteMakeRule( + ["all"], + [install_path], + comment='Add %s to "all" target.' % file_desc, + phony=True, + ) + + def WriteList(self, value_list, variable=None, prefix="", quoter=QuoteIfNecessary): + """Write a variable definition that is a list of values. E.g. WriteList(['a','b'], 'foo', prefix='blah') writes out foo = blaha blahb but in a pretty-printed style. """ - values = '' - if value_list: - value_list = [quoter(prefix + l) for l in value_list] - values = ' \\\n\t' + ' \\\n\t'.join(value_list) - self.fp.write('%s :=%s\n\n' % (variable, values)) - + values = "" + if value_list: + value_list = [quoter(prefix + value) for value in value_list] + values = " \\\n\t" + " \\\n\t".join(value_list) + self.fp.write("%s :=%s\n\n" % (variable, values)) - def WriteDoCmd(self, outputs, inputs, command, part_of_all, comment=None, - postbuilds=False): - """Write a Makefile rule that uses do_cmd. + def WriteDoCmd( + self, outputs, inputs, command, part_of_all, comment=None, postbuilds=False + ): + """Write a Makefile rule that uses do_cmd. This makes the outputs dependent on the command line that was run, as well as support the V= make command line flag. """ - suffix = '' - if postbuilds: - assert ',' not in command - suffix = ',,1' # Tell do_cmd to honor $POSTBUILDS - self.WriteMakeRule(outputs, inputs, - actions = ['$(call do_cmd,%s%s)' % (command, suffix)], - comment = comment, - command = command, - force = True) - # Add our outputs to the list of targets we read depfiles from. - # all_deps is only used for deps file reading, and for deps files we replace - # spaces with ? because escaping doesn't work with make's $(sort) and - # other functions. - outputs = [QuoteSpaces(o, SPACE_REPLACEMENT) for o in outputs] - self.WriteLn('all_deps += %s' % ' '.join(outputs)) - - - def WriteMakeRule(self, outputs, inputs, actions=None, comment=None, - order_only=False, force=False, phony=False, command=None): - """Write a Makefile rule, with some extra tricks. + suffix = "" + if postbuilds: + assert "," not in command + suffix = ",,1" # Tell do_cmd to honor $POSTBUILDS + self.WriteMakeRule( + outputs, + inputs, + actions=["$(call do_cmd,%s%s)" % (command, suffix)], + comment=comment, + command=command, + force=True, + ) + # Add our outputs to the list of targets we read depfiles from. + # all_deps is only used for deps file reading, and for deps files we replace + # spaces with ? because escaping doesn't work with make's $(sort) and + # other functions. + outputs = [QuoteSpaces(o, SPACE_REPLACEMENT) for o in outputs] + self.WriteLn("all_deps += %s" % " ".join(outputs)) + + def WriteMakeRule( + self, + outputs, + inputs, + actions=None, + comment=None, + order_only=False, + force=False, + phony=False, + command=None, + ): + """Write a Makefile rule, with some extra tricks. outputs: a list of outputs for the rule (note: this is not directly supported by make; see comments below) @@ -1746,53 +1961,54 @@ def WriteMakeRule(self, outputs, inputs, actions=None, comment=None, output is just a name to run the rule command: (optional) command name to generate unambiguous labels """ - outputs = [QuoteSpaces(o) for o in outputs] - inputs = [QuoteSpaces(i) for i in inputs] - - if comment: - self.WriteLn('# ' + comment) - if phony: - self.WriteLn('.PHONY: ' + ' '.join(outputs)) - if actions: - self.WriteLn("%s: TOOLSET := $(TOOLSET)" % outputs[0]) - force_append = ' FORCE_DO_CMD' if force else '' - - if order_only: - # Order only rule: Just write a simple rule. - # TODO(evanm): just make order_only a list of deps instead of this hack. - self.WriteLn('%s: | %s%s' % - (' '.join(outputs), ' '.join(inputs), force_append)) - elif len(outputs) == 1: - # Regular rule, one output: Just write a simple rule. - self.WriteLn('%s: %s%s' % (outputs[0], ' '.join(inputs), force_append)) - else: - # Regular rule, more than one output: Multiple outputs are tricky in - # make. We will write three rules: - # - All outputs depend on an intermediate file. - # - Make .INTERMEDIATE depend on the intermediate. - # - The intermediate file depends on the inputs and executes the - # actual command. - # - The intermediate recipe will 'touch' the intermediate file. - # - The multi-output rule will have an do-nothing recipe. - - # Hash the target name to avoid generating overlong filenames. - cmddigest = hashlib.sha1((command or self.target).encode('utf-8')).hexdigest() - intermediate = "%s.intermediate" % cmddigest - self.WriteLn('%s: %s' % (' '.join(outputs), intermediate)) - self.WriteLn('\t%s' % '@:') - self.WriteLn('%s: %s' % ('.INTERMEDIATE', intermediate)) - self.WriteLn('%s: %s%s' % - (intermediate, ' '.join(inputs), force_append)) - actions.insert(0, '$(call do_cmd,touch)') - - if actions: - for action in actions: - self.WriteLn('\t%s' % action) - self.WriteLn() - - - def WriteAndroidNdkModuleRule(self, module_name, all_sources, link_deps): - """Write a set of LOCAL_XXX definitions for Android NDK. + outputs = [QuoteSpaces(o) for o in outputs] + inputs = [QuoteSpaces(i) for i in inputs] + + if comment: + self.WriteLn("# " + comment) + if phony: + self.WriteLn(".PHONY: " + " ".join(outputs)) + if actions: + self.WriteLn("%s: TOOLSET := $(TOOLSET)" % outputs[0]) + force_append = " FORCE_DO_CMD" if force else "" + + if order_only: + # Order only rule: Just write a simple rule. + # TODO(evanm): just make order_only a list of deps instead of this hack. + self.WriteLn( + "%s: | %s%s" % (" ".join(outputs), " ".join(inputs), force_append) + ) + elif len(outputs) == 1: + # Regular rule, one output: Just write a simple rule. + self.WriteLn("%s: %s%s" % (outputs[0], " ".join(inputs), force_append)) + else: + # Regular rule, more than one output: Multiple outputs are tricky in + # make. We will write three rules: + # - All outputs depend on an intermediate file. + # - Make .INTERMEDIATE depend on the intermediate. + # - The intermediate file depends on the inputs and executes the + # actual command. + # - The intermediate recipe will 'touch' the intermediate file. + # - The multi-output rule will have an do-nothing recipe. + + # Hash the target name to avoid generating overlong filenames. + cmddigest = hashlib.sha1( + (command or self.target).encode("utf-8") + ).hexdigest() + intermediate = "%s.intermediate" % cmddigest + self.WriteLn("%s: %s" % (" ".join(outputs), intermediate)) + self.WriteLn("\t%s" % "@:") + self.WriteLn("%s: %s" % (".INTERMEDIATE", intermediate)) + self.WriteLn("%s: %s%s" % (intermediate, " ".join(inputs), force_append)) + actions.insert(0, "$(call do_cmd,touch)") + + if actions: + for action in actions: + self.WriteLn("\t%s" % action) + self.WriteLn() + + def WriteAndroidNdkModuleRule(self, module_name, all_sources, link_deps): + """Write a set of LOCAL_XXX definitions for Android NDK. These variable definitions will be used by Android NDK but do nothing for non-Android applications. @@ -1804,460 +2020,495 @@ def WriteAndroidNdkModuleRule(self, module_name, all_sources, link_deps): link_deps: A list of link dependencies, which must be sorted in the order from dependencies to dependents. """ - if self.type not in ('executable', 'shared_library', 'static_library'): - return - - self.WriteLn('# Variable definitions for Android applications') - self.WriteLn('include $(CLEAR_VARS)') - self.WriteLn('LOCAL_MODULE := ' + module_name) - self.WriteLn('LOCAL_CFLAGS := $(CFLAGS_$(BUILDTYPE)) ' - '$(DEFS_$(BUILDTYPE)) ' - # LOCAL_CFLAGS is applied to both of C and C++. There is - # no way to specify $(CFLAGS_C_$(BUILDTYPE)) only for C - # sources. - '$(CFLAGS_C_$(BUILDTYPE)) ' - # $(INCS_$(BUILDTYPE)) includes the prefix '-I' while - # LOCAL_C_INCLUDES does not expect it. So put it in - # LOCAL_CFLAGS. - '$(INCS_$(BUILDTYPE))') - # LOCAL_CXXFLAGS is obsolete and LOCAL_CPPFLAGS is preferred. - self.WriteLn('LOCAL_CPPFLAGS := $(CFLAGS_CC_$(BUILDTYPE))') - self.WriteLn('LOCAL_C_INCLUDES :=') - self.WriteLn('LOCAL_LDLIBS := $(LDFLAGS_$(BUILDTYPE)) $(LIBS)') - - # Detect the C++ extension. - cpp_ext = {'.cc': 0, '.cpp': 0, '.cxx': 0} - default_cpp_ext = '.cpp' - for filename in all_sources: - ext = os.path.splitext(filename)[1] - if ext in cpp_ext: - cpp_ext[ext] += 1 - if cpp_ext[ext] > cpp_ext[default_cpp_ext]: - default_cpp_ext = ext - self.WriteLn('LOCAL_CPP_EXTENSION := ' + default_cpp_ext) - - self.WriteList(list(map(self.Absolutify, filter(Compilable, all_sources))), - 'LOCAL_SRC_FILES') - - # Filter out those which do not match prefix and suffix and produce - # the resulting list without prefix and suffix. - def DepsToModules(deps, prefix, suffix): - modules = [] - for filepath in deps: - filename = os.path.basename(filepath) - if filename.startswith(prefix) and filename.endswith(suffix): - modules.append(filename[len(prefix):-len(suffix)]) - return modules - - # Retrieve the default value of 'SHARED_LIB_SUFFIX' - params = {'flavor': 'linux'} - default_variables = {} - CalculateVariables(default_variables, params) - - self.WriteList( - DepsToModules(link_deps, - generator_default_variables['SHARED_LIB_PREFIX'], - default_variables['SHARED_LIB_SUFFIX']), - 'LOCAL_SHARED_LIBRARIES') - self.WriteList( - DepsToModules(link_deps, - generator_default_variables['STATIC_LIB_PREFIX'], - generator_default_variables['STATIC_LIB_SUFFIX']), - 'LOCAL_STATIC_LIBRARIES') - - if self.type == 'executable': - self.WriteLn('include $(BUILD_EXECUTABLE)') - elif self.type == 'shared_library': - self.WriteLn('include $(BUILD_SHARED_LIBRARY)') - elif self.type == 'static_library': - self.WriteLn('include $(BUILD_STATIC_LIBRARY)') - self.WriteLn() - - - def WriteLn(self, text=''): - self.fp.write(text + '\n') - - - def GetSortedXcodeEnv(self, additional_settings=None): - return gyp.xcode_emulation.GetSortedXcodeEnv( - self.xcode_settings, "$(abs_builddir)", - os.path.join("$(abs_srcdir)", self.path), "$(BUILDTYPE)", - additional_settings) - - - def GetSortedXcodePostbuildEnv(self): - # CHROMIUM_STRIP_SAVE_FILE is a chromium-specific hack. - # TODO(thakis): It would be nice to have some general mechanism instead. - strip_save_file = self.xcode_settings.GetPerTargetSetting( - 'CHROMIUM_STRIP_SAVE_FILE', '') - # Even if strip_save_file is empty, explicitly write it. Else a postbuild - # might pick up an export from an earlier target. - return self.GetSortedXcodeEnv( - additional_settings={'CHROMIUM_STRIP_SAVE_FILE': strip_save_file}) - - - def WriteSortedXcodeEnv(self, target, env): - for k, v in env: - # For - # foo := a\ b - # the escaped space does the right thing. For - # export foo := a\ b - # it does not -- the backslash is written to the env as literal character. - # So don't escape spaces in |env[k]|. - self.WriteLn('%s: export %s := %s' % (QuoteSpaces(target), k, v)) - - - def Objectify(self, path): - """Convert a path to its output directory form.""" - if '$(' in path: - path = path.replace('$(obj)/', '$(obj).%s/$(TARGET)/' % self.toolset) - if not '$(obj)' in path: - path = '$(obj).%s/$(TARGET)/%s' % (self.toolset, path) - return path - - - def Pchify(self, path, lang): - """Convert a prefix header path to its output directory form.""" - path = self.Absolutify(path) - if '$(' in path: - path = path.replace('$(obj)/', '$(obj).%s/$(TARGET)/pch-%s' % - (self.toolset, lang)) - return path - return '$(obj).%s/$(TARGET)/pch-%s/%s' % (self.toolset, lang, path) - - - def Absolutify(self, path): - """Convert a subdirectory-relative path into a base-relative path. + if self.type not in ("executable", "shared_library", "static_library"): + return + + self.WriteLn("# Variable definitions for Android applications") + self.WriteLn("include $(CLEAR_VARS)") + self.WriteLn("LOCAL_MODULE := " + module_name) + self.WriteLn( + "LOCAL_CFLAGS := $(CFLAGS_$(BUILDTYPE)) " + "$(DEFS_$(BUILDTYPE)) " + # LOCAL_CFLAGS is applied to both of C and C++. There is + # no way to specify $(CFLAGS_C_$(BUILDTYPE)) only for C + # sources. + "$(CFLAGS_C_$(BUILDTYPE)) " + # $(INCS_$(BUILDTYPE)) includes the prefix '-I' while + # LOCAL_C_INCLUDES does not expect it. So put it in + # LOCAL_CFLAGS. + "$(INCS_$(BUILDTYPE))" + ) + # LOCAL_CXXFLAGS is obsolete and LOCAL_CPPFLAGS is preferred. + self.WriteLn("LOCAL_CPPFLAGS := $(CFLAGS_CC_$(BUILDTYPE))") + self.WriteLn("LOCAL_C_INCLUDES :=") + self.WriteLn("LOCAL_LDLIBS := $(LDFLAGS_$(BUILDTYPE)) $(LIBS)") + + # Detect the C++ extension. + cpp_ext = {".cc": 0, ".cpp": 0, ".cxx": 0} + default_cpp_ext = ".cpp" + for filename in all_sources: + ext = os.path.splitext(filename)[1] + if ext in cpp_ext: + cpp_ext[ext] += 1 + if cpp_ext[ext] > cpp_ext[default_cpp_ext]: + default_cpp_ext = ext + self.WriteLn("LOCAL_CPP_EXTENSION := " + default_cpp_ext) + + self.WriteList( + list(map(self.Absolutify, filter(Compilable, all_sources))), + "LOCAL_SRC_FILES", + ) + + # Filter out those which do not match prefix and suffix and produce + # the resulting list without prefix and suffix. + def DepsToModules(deps, prefix, suffix): + modules = [] + for filepath in deps: + filename = os.path.basename(filepath) + if filename.startswith(prefix) and filename.endswith(suffix): + modules.append(filename[len(prefix) : -len(suffix)]) + return modules + + # Retrieve the default value of 'SHARED_LIB_SUFFIX' + params = {"flavor": "linux"} + default_variables = {} + CalculateVariables(default_variables, params) + + self.WriteList( + DepsToModules( + link_deps, + generator_default_variables["SHARED_LIB_PREFIX"], + default_variables["SHARED_LIB_SUFFIX"], + ), + "LOCAL_SHARED_LIBRARIES", + ) + self.WriteList( + DepsToModules( + link_deps, + generator_default_variables["STATIC_LIB_PREFIX"], + generator_default_variables["STATIC_LIB_SUFFIX"], + ), + "LOCAL_STATIC_LIBRARIES", + ) + + if self.type == "executable": + self.WriteLn("include $(BUILD_EXECUTABLE)") + elif self.type == "shared_library": + self.WriteLn("include $(BUILD_SHARED_LIBRARY)") + elif self.type == "static_library": + self.WriteLn("include $(BUILD_STATIC_LIBRARY)") + self.WriteLn() + + def WriteLn(self, text=""): + self.fp.write(text + "\n") + + def GetSortedXcodeEnv(self, additional_settings=None): + return gyp.xcode_emulation.GetSortedXcodeEnv( + self.xcode_settings, + "$(abs_builddir)", + os.path.join("$(abs_srcdir)", self.path), + "$(BUILDTYPE)", + additional_settings, + ) + + def GetSortedXcodePostbuildEnv(self): + # CHROMIUM_STRIP_SAVE_FILE is a chromium-specific hack. + # TODO(thakis): It would be nice to have some general mechanism instead. + strip_save_file = self.xcode_settings.GetPerTargetSetting( + "CHROMIUM_STRIP_SAVE_FILE", "" + ) + # Even if strip_save_file is empty, explicitly write it. Else a postbuild + # might pick up an export from an earlier target. + return self.GetSortedXcodeEnv( + additional_settings={"CHROMIUM_STRIP_SAVE_FILE": strip_save_file} + ) + + def WriteSortedXcodeEnv(self, target, env): + for k, v in env: + # For + # foo := a\ b + # the escaped space does the right thing. For + # export foo := a\ b + # it does not -- the backslash is written to the env as literal character. + # So don't escape spaces in |env[k]|. + self.WriteLn("%s: export %s := %s" % (QuoteSpaces(target), k, v)) + + def Objectify(self, path): + """Convert a path to its output directory form.""" + if "$(" in path: + path = path.replace("$(obj)/", "$(obj).%s/$(TARGET)/" % self.toolset) + if "$(obj)" not in path: + path = "$(obj).%s/$(TARGET)/%s" % (self.toolset, path) + return path + + def Pchify(self, path, lang): + """Convert a prefix header path to its output directory form.""" + path = self.Absolutify(path) + if "$(" in path: + path = path.replace( + "$(obj)/", "$(obj).%s/$(TARGET)/pch-%s" % (self.toolset, lang) + ) + return path + return "$(obj).%s/$(TARGET)/pch-%s/%s" % (self.toolset, lang, path) + + def Absolutify(self, path): + """Convert a subdirectory-relative path into a base-relative path. Skips over paths that contain variables.""" - if '$(' in path: - # Don't call normpath in this case, as it might collapse the - # path too aggressively if it features '..'. However it's still - # important to strip trailing slashes. - return path.rstrip('/') - return os.path.normpath(os.path.join(self.path, path)) - - - def ExpandInputRoot(self, template, expansion, dirname): - if '%(INPUT_ROOT)s' not in template and '%(INPUT_DIRNAME)s' not in template: - return template - path = template % { - 'INPUT_ROOT': expansion, - 'INPUT_DIRNAME': dirname, + if "$(" in path: + # Don't call normpath in this case, as it might collapse the + # path too aggressively if it features '..'. However it's still + # important to strip trailing slashes. + return path.rstrip("/") + return os.path.normpath(os.path.join(self.path, path)) + + def ExpandInputRoot(self, template, expansion, dirname): + if "%(INPUT_ROOT)s" not in template and "%(INPUT_DIRNAME)s" not in template: + return template + path = template % { + "INPUT_ROOT": expansion, + "INPUT_DIRNAME": dirname, + } + return path + + def _InstallableTargetInstallPath(self): + """Returns the location of the final output for an installable target.""" + # Xcode puts shared_library results into PRODUCT_DIR, and some gyp files + # rely on this. Emulate this behavior for mac. + if self.type == "shared_library" and ( + self.flavor != "mac" or self.toolset != "target" + ): + # Install all shared libs into a common directory (per toolset) for + # convenient access with LD_LIBRARY_PATH. + return "$(builddir)/lib.%s/%s" % (self.toolset, self.alias) + return "$(builddir)/" + self.alias + + +def WriteAutoRegenerationRule(params, root_makefile, makefile_name, build_files): + """Write the target to regenerate the Makefile.""" + options = params["options"] + build_files_args = [ + gyp.common.RelativePath(filename, options.toplevel_dir) + for filename in params["build_files_arg"] + ] + + gyp_binary = gyp.common.FixIfRelativePath( + params["gyp_binary"], options.toplevel_dir + ) + if not gyp_binary.startswith(os.sep): + gyp_binary = os.path.join(".", gyp_binary) + + root_makefile.write( + "quiet_cmd_regen_makefile = ACTION Regenerating $@\n" + "cmd_regen_makefile = cd $(srcdir); %(cmd)s\n" + "%(makefile_name)s: %(deps)s\n" + "\t$(call do_cmd,regen_makefile)\n\n" + % { + "makefile_name": makefile_name, + "deps": " ".join(SourceifyAndQuoteSpaces(bf) for bf in build_files), + "cmd": gyp.common.EncodePOSIXShellList( + [gyp_binary, "-fmake"] + gyp.RegenerateFlags(options) + build_files_args + ), } - return path - - - def _InstallableTargetInstallPath(self): - """Returns the location of the final output for an installable target.""" - # Xcode puts shared_library results into PRODUCT_DIR, and some gyp files - # rely on this. Emulate this behavior for mac. - if (self.type == 'shared_library' and - (self.flavor != 'mac' or self.toolset != 'target')): - # Install all shared libs into a common directory (per toolset) for - # convenient access with LD_LIBRARY_PATH. - return '$(builddir)/lib.%s/%s' % (self.toolset, self.alias) - return '$(builddir)/' + self.alias - - -def WriteAutoRegenerationRule(params, root_makefile, makefile_name, - build_files): - """Write the target to regenerate the Makefile.""" - options = params['options'] - build_files_args = [gyp.common.RelativePath(filename, options.toplevel_dir) - for filename in params['build_files_arg']] - - gyp_binary = gyp.common.FixIfRelativePath(params['gyp_binary'], - options.toplevel_dir) - if not gyp_binary.startswith(os.sep): - gyp_binary = os.path.join('.', gyp_binary) - - root_makefile.write( - "quiet_cmd_regen_makefile = ACTION Regenerating $@\n" - "cmd_regen_makefile = cd $(srcdir); %(cmd)s\n" - "%(makefile_name)s: %(deps)s\n" - "\t$(call do_cmd,regen_makefile)\n\n" % { - 'makefile_name': makefile_name, - 'deps': ' '.join(SourceifyAndQuoteSpaces(bf) for bf in build_files), - 'cmd': gyp.common.EncodePOSIXShellList( - [gyp_binary, '-fmake'] + - gyp.RegenerateFlags(options) + - build_files_args)}) + ) def PerformBuild(data, configurations, params): - options = params['options'] - for config in configurations: - arguments = ['make'] - if options.toplevel_dir and options.toplevel_dir != '.': - arguments += '-C', options.toplevel_dir - arguments.append('BUILDTYPE=' + config) - print('Building [%s]: %s' % (config, arguments)) - subprocess.check_call(arguments) + options = params["options"] + for config in configurations: + arguments = ["make"] + if options.toplevel_dir and options.toplevel_dir != ".": + arguments += "-C", options.toplevel_dir + arguments.append("BUILDTYPE=" + config) + print("Building [%s]: %s" % (config, arguments)) + subprocess.check_call(arguments) def GenerateOutput(target_list, target_dicts, data, params): - options = params['options'] - flavor = gyp.common.GetFlavor(params) - generator_flags = params.get('generator_flags', {}) - builddir_name = generator_flags.get('output_dir', 'out') - android_ndk_version = generator_flags.get('android_ndk_version', None) - default_target = generator_flags.get('default_target', 'all') - - def CalculateMakefilePath(build_file, base_name): - """Determine where to write a Makefile for a given gyp file.""" - # Paths in gyp files are relative to the .gyp file, but we want - # paths relative to the source root for the master makefile. Grab - # the path of the .gyp file as the base to relativize against. - # E.g. "foo/bar" when we're constructing targets for "foo/bar/baz.gyp". - base_path = gyp.common.RelativePath(os.path.dirname(build_file), - options.depth) - # We write the file in the base_path directory. - output_file = os.path.join(options.depth, base_path, base_name) + options = params["options"] + flavor = gyp.common.GetFlavor(params) + generator_flags = params.get("generator_flags", {}) + builddir_name = generator_flags.get("output_dir", "out") + android_ndk_version = generator_flags.get("android_ndk_version", None) + default_target = generator_flags.get("default_target", "all") + + def CalculateMakefilePath(build_file, base_name): + """Determine where to write a Makefile for a given gyp file.""" + # Paths in gyp files are relative to the .gyp file, but we want + # paths relative to the source root for the master makefile. Grab + # the path of the .gyp file as the base to relativize against. + # E.g. "foo/bar" when we're constructing targets for "foo/bar/baz.gyp". + base_path = gyp.common.RelativePath(os.path.dirname(build_file), options.depth) + # We write the file in the base_path directory. + output_file = os.path.join(options.depth, base_path, base_name) + if options.generator_output: + output_file = os.path.join( + options.depth, options.generator_output, base_path, base_name + ) + base_path = gyp.common.RelativePath( + os.path.dirname(build_file), options.toplevel_dir + ) + return base_path, output_file + + # TODO: search for the first non-'Default' target. This can go + # away when we add verification that all targets have the + # necessary configurations. + default_configuration = None + toolsets = set([target_dicts[target]["toolset"] for target in target_list]) + for target in target_list: + spec = target_dicts[target] + if spec["default_configuration"] != "Default": + default_configuration = spec["default_configuration"] + break + if not default_configuration: + default_configuration = "Default" + + srcdir = "." + makefile_name = "Makefile" + options.suffix + makefile_path = os.path.join(options.toplevel_dir, makefile_name) if options.generator_output: - output_file = os.path.join( - options.depth, options.generator_output, base_path, base_name) - base_path = gyp.common.RelativePath(os.path.dirname(build_file), - options.toplevel_dir) - return base_path, output_file - - # TODO: search for the first non-'Default' target. This can go - # away when we add verification that all targets have the - # necessary configurations. - default_configuration = None - toolsets = set([target_dicts[target]['toolset'] for target in target_list]) - for target in target_list: - spec = target_dicts[target] - if spec['default_configuration'] != 'Default': - default_configuration = spec['default_configuration'] - break - if not default_configuration: - default_configuration = 'Default' - - srcdir = '.' - makefile_name = 'Makefile' + options.suffix - makefile_path = os.path.join(options.toplevel_dir, makefile_name) - if options.generator_output: - global srcdir_prefix - makefile_path = os.path.join( - options.toplevel_dir, options.generator_output, makefile_name) - srcdir = gyp.common.RelativePath(srcdir, options.generator_output) - srcdir_prefix = '$(srcdir)/' - - flock_command= 'flock' - copy_archive_arguments = '-af' - makedep_arguments = '-MMD' - header_params = { - 'default_target': default_target, - 'builddir': builddir_name, - 'default_configuration': default_configuration, - 'flock': flock_command, - 'flock_index': 1, - 'link_commands': LINK_COMMANDS_LINUX, - 'extra_commands': '', - 'srcdir': srcdir, - 'copy_archive_args': copy_archive_arguments, - 'makedep_args': makedep_arguments, - 'CC.target': GetEnvironFallback(('CC_target', 'CC'), '$(CC)'), - 'AR.target': GetEnvironFallback(('AR_target', 'AR'), '$(AR)'), - 'CXX.target': GetEnvironFallback(('CXX_target', 'CXX'), '$(CXX)'), - 'LINK.target': GetEnvironFallback(('LINK_target', 'LINK'), '$(LINK)'), - 'CC.host': GetEnvironFallback(('CC_host', 'CC'), 'gcc'), - 'AR.host': GetEnvironFallback(('AR_host', 'AR'), 'ar'), - 'CXX.host': GetEnvironFallback(('CXX_host', 'CXX'), 'g++'), - 'LINK.host': GetEnvironFallback(('LINK_host', 'LINK'), '$(CXX.host)'), + global srcdir_prefix + makefile_path = os.path.join( + options.toplevel_dir, options.generator_output, makefile_name + ) + srcdir = gyp.common.RelativePath(srcdir, options.generator_output) + srcdir_prefix = "$(srcdir)/" + + flock_command = "flock" + copy_archive_arguments = "-af" + makedep_arguments = "-MMD" + header_params = { + "default_target": default_target, + "builddir": builddir_name, + "default_configuration": default_configuration, + "flock": flock_command, + "flock_index": 1, + "link_commands": LINK_COMMANDS_LINUX, + "extra_commands": "", + "srcdir": srcdir, + "copy_archive_args": copy_archive_arguments, + "makedep_args": makedep_arguments, + "CC.target": GetEnvironFallback(("CC_target", "CC"), "$(CC)"), + "AR.target": GetEnvironFallback(("AR_target", "AR"), "$(AR)"), + "CXX.target": GetEnvironFallback(("CXX_target", "CXX"), "$(CXX)"), + "LINK.target": GetEnvironFallback(("LINK_target", "LINK"), "$(LINK)"), + "CC.host": GetEnvironFallback(("CC_host", "CC"), "gcc"), + "AR.host": GetEnvironFallback(("AR_host", "AR"), "ar"), + "CXX.host": GetEnvironFallback(("CXX_host", "CXX"), "g++"), + "LINK.host": GetEnvironFallback(("LINK_host", "LINK"), "$(CXX.host)"), } - if flavor == 'mac': - flock_command = './gyp-mac-tool flock' - header_params.update({ - 'flock': flock_command, - 'flock_index': 2, - 'link_commands': LINK_COMMANDS_MAC, - 'extra_commands': SHARED_HEADER_MAC_COMMANDS, - }) - elif flavor == 'android': - header_params.update({ - 'link_commands': LINK_COMMANDS_ANDROID, - }) - elif flavor == 'zos': - copy_archive_arguments = '-fPR' - makedep_arguments = '-qmakedep=gcc' - header_params.update({ - 'copy_archive_args': copy_archive_arguments, - 'makedep_args': makedep_arguments, - 'link_commands': LINK_COMMANDS_OS390, - 'CC.target': GetEnvironFallback(('CC_target', 'CC'), 'njsc'), - 'CXX.target': GetEnvironFallback(('CXX_target', 'CXX'), 'njsc++'), - 'CC.host': GetEnvironFallback(('CC_host', 'CC'), 'njsc'), - 'CXX.host': GetEnvironFallback(('CXX_host', 'CXX'), 'njsc++'), - }) - elif flavor == 'solaris': - header_params.update({ - 'flock': './gyp-flock-tool flock', - 'flock_index': 2, - }) - elif flavor == 'freebsd': - # Note: OpenBSD has sysutils/flock. lockf seems to be FreeBSD specific. - header_params.update({ - 'flock': 'lockf', - }) - elif flavor == 'openbsd': - copy_archive_arguments = '-pPRf' - header_params.update({ - 'copy_archive_args': copy_archive_arguments, - }) - elif flavor == 'aix': - copy_archive_arguments = '-pPRf' - header_params.update({ - 'copy_archive_args': copy_archive_arguments, - 'link_commands': LINK_COMMANDS_AIX, - 'flock': './gyp-flock-tool flock', - 'flock_index': 2, - }) - - build_file, _, _ = gyp.common.ParseQualifiedTarget(target_list[0]) - make_global_settings_array = data[build_file].get('make_global_settings', []) - wrappers = {} - for key, value in make_global_settings_array: - if key.endswith('_wrapper'): - wrappers[key[:-len('_wrapper')]] = '$(abspath %s)' % value - make_global_settings = '' - for key, value in make_global_settings_array: - if re.match('.*_wrapper', key): - continue - if value[0] != '$': - value = '$(abspath %s)' % value - wrapper = wrappers.get(key) - if wrapper: - value = '%s %s' % (wrapper, value) - del wrappers[key] - if key in ('CC', 'CC.host', 'CXX', 'CXX.host'): - make_global_settings += ( - 'ifneq (,$(filter $(origin %s), undefined default))\n' % key) - # Let gyp-time envvars win over global settings. - env_key = key.replace('.', '_') # CC.host -> CC_host - if env_key in os.environ: - value = os.environ[env_key] - make_global_settings += ' %s = %s\n' % (key, value) - make_global_settings += 'endif\n' - else: - make_global_settings += '%s ?= %s\n' % (key, value) - # TODO(ukai): define cmd when only wrapper is specified in - # make_global_settings. - - header_params['make_global_settings'] = make_global_settings - - gyp.common.EnsureDirExists(makefile_path) - root_makefile = open(makefile_path, 'w') - root_makefile.write(SHARED_HEADER % header_params) - # Currently any versions have the same effect, but in future the behavior - # could be different. - if android_ndk_version: - root_makefile.write( - '# Define LOCAL_PATH for build of Android applications.\n' - 'LOCAL_PATH := $(call my-dir)\n' - '\n') - for toolset in toolsets: - root_makefile.write('TOOLSET := %s\n' % toolset) - WriteRootHeaderSuffixRules(root_makefile) - - # Put build-time support tools next to the root Makefile. - dest_path = os.path.dirname(makefile_path) - gyp.common.CopyTool(flavor, dest_path) - - # Find the list of targets that derive from the gyp file(s) being built. - needed_targets = set() - for build_file in params['build_files']: - for target in gyp.common.AllTargets(target_list, target_dicts, build_file): - needed_targets.add(target) - - build_files = set() - include_list = set() - for qualified_target in target_list: - build_file, target, toolset = gyp.common.ParseQualifiedTarget( - qualified_target) - - this_make_global_settings = data[build_file].get('make_global_settings', []) - assert make_global_settings_array == this_make_global_settings, ( - "make_global_settings needs to be the same for all targets. %s vs. %s" % - (this_make_global_settings, make_global_settings)) - - build_files.add(gyp.common.RelativePath(build_file, options.toplevel_dir)) - included_files = data[build_file]['included_files'] - for included_file in included_files: - # The included_files entries are relative to the dir of the build file - # that included them, so we have to undo that and then make them relative - # to the root dir. - relative_include_file = gyp.common.RelativePath( - gyp.common.UnrelativePath(included_file, build_file), - options.toplevel_dir) - abs_include_file = os.path.abspath(relative_include_file) - # If the include file is from the ~/.gyp dir, we should use absolute path - # so that relocating the src dir doesn't break the path. - if (params['home_dot_gyp'] and - abs_include_file.startswith(params['home_dot_gyp'])): - build_files.add(abs_include_file) - else: - build_files.add(relative_include_file) - - base_path, output_file = CalculateMakefilePath(build_file, - target + '.' + toolset + options.suffix + '.mk') - - spec = target_dicts[qualified_target] - configs = spec['configurations'] - - if flavor == 'mac': - gyp.xcode_emulation.MergeGlobalXcodeSettingsToSpec(data[build_file], spec) - - writer = MakefileWriter(generator_flags, flavor) - writer.Write(qualified_target, base_path, output_file, spec, configs, - part_of_all=qualified_target in needed_targets) - - # Our root_makefile lives at the source root. Compute the relative path - # from there to the output_file for including. - mkfile_rel_path = gyp.common.RelativePath(output_file, - os.path.dirname(makefile_path)) - include_list.add(mkfile_rel_path) - - # Write out per-gyp (sub-project) Makefiles. - depth_rel_path = gyp.common.RelativePath(options.depth, os.getcwd()) - for build_file in build_files: - # The paths in build_files were relativized above, so undo that before - # testing against the non-relativized items in target_list and before - # calculating the Makefile path. - build_file = os.path.join(depth_rel_path, build_file) - gyp_targets = [target_dicts[target]['target_name'] for target in target_list - if target.startswith(build_file) and - target in needed_targets] - # Only generate Makefiles for gyp files with targets. - if not gyp_targets: - continue - base_path, output_file = CalculateMakefilePath(build_file, - os.path.splitext(os.path.basename(build_file))[0] + '.Makefile') - makefile_rel_path = gyp.common.RelativePath(os.path.dirname(makefile_path), - os.path.dirname(output_file)) - writer.WriteSubMake(output_file, makefile_rel_path, gyp_targets, - builddir_name) - - - # Write out the sorted list of includes. - root_makefile.write('\n') - for include_file in sorted(include_list): - # We wrap each .mk include in an if statement so users can tell make to - # not load a file by setting NO_LOAD. The below make code says, only - # load the .mk file if the .mk filename doesn't start with a token in - # NO_LOAD. - root_makefile.write( - "ifeq ($(strip $(foreach prefix,$(NO_LOAD),\\\n" - " $(findstring $(join ^,$(prefix)),\\\n" - " $(join ^," + include_file + ")))),)\n") - root_makefile.write(" include " + include_file + "\n") - root_makefile.write("endif\n") - root_makefile.write('\n') - - if (not generator_flags.get('standalone') - and generator_flags.get('auto_regeneration', True)): - WriteAutoRegenerationRule(params, root_makefile, makefile_name, build_files) + if flavor == "mac": + flock_command = "./gyp-mac-tool flock" + header_params.update( + { + "flock": flock_command, + "flock_index": 2, + "link_commands": LINK_COMMANDS_MAC, + "extra_commands": SHARED_HEADER_MAC_COMMANDS, + } + ) + elif flavor == "android": + header_params.update({"link_commands": LINK_COMMANDS_ANDROID}) + elif flavor == "zos": + copy_archive_arguments = "-fPR" + makedep_arguments = "-qmakedep=gcc" + header_params.update( + { + "copy_archive_args": copy_archive_arguments, + "makedep_args": makedep_arguments, + "link_commands": LINK_COMMANDS_OS390, + "CC.target": GetEnvironFallback(("CC_target", "CC"), "njsc"), + "CXX.target": GetEnvironFallback(("CXX_target", "CXX"), "njsc++"), + "CC.host": GetEnvironFallback(("CC_host", "CC"), "njsc"), + "CXX.host": GetEnvironFallback(("CXX_host", "CXX"), "njsc++"), + } + ) + elif flavor == "solaris": + copy_archive_arguments = "-pPRf@" + header_params.update( + { + "copy_archive_args": copy_archive_arguments, + "flock": "./gyp-flock-tool flock", + "flock_index": 2 + } + ) + elif flavor == "freebsd": + # Note: OpenBSD has sysutils/flock. lockf seems to be FreeBSD specific. + header_params.update({"flock": "lockf"}) + elif flavor == "openbsd": + copy_archive_arguments = "-pPRf" + header_params.update({"copy_archive_args": copy_archive_arguments}) + elif flavor == "aix": + copy_archive_arguments = "-pPRf" + header_params.update( + { + "copy_archive_args": copy_archive_arguments, + "link_commands": LINK_COMMANDS_AIX, + "flock": "./gyp-flock-tool flock", + "flock_index": 2, + } + ) + + build_file, _, _ = gyp.common.ParseQualifiedTarget(target_list[0]) + make_global_settings_array = data[build_file].get("make_global_settings", []) + wrappers = {} + for key, value in make_global_settings_array: + if key.endswith("_wrapper"): + wrappers[key[: -len("_wrapper")]] = "$(abspath %s)" % value + make_global_settings = "" + for key, value in make_global_settings_array: + if re.match(".*_wrapper", key): + continue + if value[0] != "$": + value = "$(abspath %s)" % value + wrapper = wrappers.get(key) + if wrapper: + value = "%s %s" % (wrapper, value) + del wrappers[key] + if key in ("CC", "CC.host", "CXX", "CXX.host"): + make_global_settings += ( + "ifneq (,$(filter $(origin %s), undefined default))\n" % key + ) + # Let gyp-time envvars win over global settings. + env_key = key.replace(".", "_") # CC.host -> CC_host + if env_key in os.environ: + value = os.environ[env_key] + make_global_settings += " %s = %s\n" % (key, value) + make_global_settings += "endif\n" + else: + make_global_settings += "%s ?= %s\n" % (key, value) + # TODO(ukai): define cmd when only wrapper is specified in + # make_global_settings. - root_makefile.write(SHARED_FOOTER) + header_params["make_global_settings"] = make_global_settings - root_makefile.close() + gyp.common.EnsureDirExists(makefile_path) + root_makefile = open(makefile_path, "w") + root_makefile.write(SHARED_HEADER % header_params) + # Currently any versions have the same effect, but in future the behavior + # could be different. + if android_ndk_version: + root_makefile.write( + "# Define LOCAL_PATH for build of Android applications.\n" + "LOCAL_PATH := $(call my-dir)\n" + "\n" + ) + for toolset in toolsets: + root_makefile.write("TOOLSET := %s\n" % toolset) + WriteRootHeaderSuffixRules(root_makefile) + + # Put build-time support tools next to the root Makefile. + dest_path = os.path.dirname(makefile_path) + gyp.common.CopyTool(flavor, dest_path) + + # Find the list of targets that derive from the gyp file(s) being built. + needed_targets = set() + for build_file in params["build_files"]: + for target in gyp.common.AllTargets(target_list, target_dicts, build_file): + needed_targets.add(target) + + build_files = set() + include_list = set() + for qualified_target in target_list: + build_file, target, toolset = gyp.common.ParseQualifiedTarget(qualified_target) + + this_make_global_settings = data[build_file].get("make_global_settings", []) + assert make_global_settings_array == this_make_global_settings, ( + "make_global_settings needs to be the same for all targets. %s vs. %s" + % (this_make_global_settings, make_global_settings) + ) + + build_files.add(gyp.common.RelativePath(build_file, options.toplevel_dir)) + included_files = data[build_file]["included_files"] + for included_file in included_files: + # The included_files entries are relative to the dir of the build file + # that included them, so we have to undo that and then make them relative + # to the root dir. + relative_include_file = gyp.common.RelativePath( + gyp.common.UnrelativePath(included_file, build_file), + options.toplevel_dir, + ) + abs_include_file = os.path.abspath(relative_include_file) + # If the include file is from the ~/.gyp dir, we should use absolute path + # so that relocating the src dir doesn't break the path. + if params["home_dot_gyp"] and abs_include_file.startswith( + params["home_dot_gyp"] + ): + build_files.add(abs_include_file) + else: + build_files.add(relative_include_file) + + base_path, output_file = CalculateMakefilePath( + build_file, target + "." + toolset + options.suffix + ".mk" + ) + + spec = target_dicts[qualified_target] + configs = spec["configurations"] + + if flavor == "mac": + gyp.xcode_emulation.MergeGlobalXcodeSettingsToSpec(data[build_file], spec) + + writer = MakefileWriter(generator_flags, flavor) + writer.Write( + qualified_target, + base_path, + output_file, + spec, + configs, + part_of_all=qualified_target in needed_targets, + ) + + # Our root_makefile lives at the source root. Compute the relative path + # from there to the output_file for including. + mkfile_rel_path = gyp.common.RelativePath( + output_file, os.path.dirname(makefile_path) + ) + include_list.add(mkfile_rel_path) + + # Write out per-gyp (sub-project) Makefiles. + depth_rel_path = gyp.common.RelativePath(options.depth, os.getcwd()) + for build_file in build_files: + # The paths in build_files were relativized above, so undo that before + # testing against the non-relativized items in target_list and before + # calculating the Makefile path. + build_file = os.path.join(depth_rel_path, build_file) + gyp_targets = [ + target_dicts[qualified_target]["target_name"] + for qualified_target in target_list + if qualified_target.startswith(build_file) + and qualified_target in needed_targets + ] + # Only generate Makefiles for gyp files with targets. + if not gyp_targets: + continue + base_path, output_file = CalculateMakefilePath( + build_file, os.path.splitext(os.path.basename(build_file))[0] + ".Makefile" + ) + makefile_rel_path = gyp.common.RelativePath( + os.path.dirname(makefile_path), os.path.dirname(output_file) + ) + writer.WriteSubMake(output_file, makefile_rel_path, gyp_targets, builddir_name) + + # Write out the sorted list of includes. + root_makefile.write("\n") + for include_file in sorted(include_list): + # We wrap each .mk include in an if statement so users can tell make to + # not load a file by setting NO_LOAD. The below make code says, only + # load the .mk file if the .mk filename doesn't start with a token in + # NO_LOAD. + root_makefile.write( + "ifeq ($(strip $(foreach prefix,$(NO_LOAD),\\\n" + " $(findstring $(join ^,$(prefix)),\\\n" + " $(join ^," + include_file + ")))),)\n" + ) + root_makefile.write(" include " + include_file + "\n") + root_makefile.write("endif\n") + root_makefile.write("\n") + + if not generator_flags.get("standalone") and generator_flags.get( + "auto_regeneration", True + ): + WriteAutoRegenerationRule(params, root_makefile, makefile_name, build_files) + + root_makefile.write(SHARED_FOOTER) + + root_makefile.close() diff --git a/tools/gyp/pylib/gyp/generator/msvs.py b/tools/gyp/pylib/gyp/generator/msvs.py index 09abadb1bcb658..fadd932f1e958f 100644 --- a/tools/gyp/pylib/gyp/generator/msvs.py +++ b/tools/gyp/pylib/gyp/generator/msvs.py @@ -4,7 +4,6 @@ from __future__ import print_function -import copy import ntpath import os import posixpath @@ -38,65 +37,64 @@ # if IncrediBuild is executed from inside Visual Studio. This regex # validates that the string looks like a GUID with all uppercase hex # letters. -VALID_MSVS_GUID_CHARS = re.compile(r'^[A-F0-9\-]+$') +VALID_MSVS_GUID_CHARS = re.compile(r"^[A-F0-9\-]+$") generator_supports_multiple_toolsets = gyp.common.CrossCompileRequested() generator_default_variables = { - 'DRIVER_PREFIX': '', - 'DRIVER_SUFFIX': '.sys', - 'EXECUTABLE_PREFIX': '', - 'EXECUTABLE_SUFFIX': '.exe', - 'STATIC_LIB_PREFIX': '', - 'SHARED_LIB_PREFIX': '', - 'STATIC_LIB_SUFFIX': '.lib', - 'SHARED_LIB_SUFFIX': '.dll', - 'INTERMEDIATE_DIR': '$(IntDir)', - 'SHARED_INTERMEDIATE_DIR': '$(OutDir)/obj/global_intermediate', - 'OS': 'win', - 'PRODUCT_DIR': '$(OutDir)', - 'LIB_DIR': '$(OutDir)lib', - 'RULE_INPUT_ROOT': '$(InputName)', - 'RULE_INPUT_DIRNAME': '$(InputDir)', - 'RULE_INPUT_EXT': '$(InputExt)', - 'RULE_INPUT_NAME': '$(InputFileName)', - 'RULE_INPUT_PATH': '$(InputPath)', - 'CONFIGURATION_NAME': '$(ConfigurationName)', + "DRIVER_PREFIX": "", + "DRIVER_SUFFIX": ".sys", + "EXECUTABLE_PREFIX": "", + "EXECUTABLE_SUFFIX": ".exe", + "STATIC_LIB_PREFIX": "", + "SHARED_LIB_PREFIX": "", + "STATIC_LIB_SUFFIX": ".lib", + "SHARED_LIB_SUFFIX": ".dll", + "INTERMEDIATE_DIR": "$(IntDir)", + "SHARED_INTERMEDIATE_DIR": "$(OutDir)/obj/global_intermediate", + "OS": "win", + "PRODUCT_DIR": "$(OutDir)", + "LIB_DIR": "$(OutDir)lib", + "RULE_INPUT_ROOT": "$(InputName)", + "RULE_INPUT_DIRNAME": "$(InputDir)", + "RULE_INPUT_EXT": "$(InputExt)", + "RULE_INPUT_NAME": "$(InputFileName)", + "RULE_INPUT_PATH": "$(InputPath)", + "CONFIGURATION_NAME": "$(ConfigurationName)", } # The msvs specific sections that hold paths generator_additional_path_sections = [ - 'msvs_cygwin_dirs', - 'msvs_props', + "msvs_cygwin_dirs", + "msvs_props", ] generator_additional_non_configuration_keys = [ - 'msvs_cygwin_dirs', - 'msvs_cygwin_shell', - 'msvs_large_pdb', - 'msvs_shard', - 'msvs_external_builder', - 'msvs_external_builder_out_dir', - 'msvs_external_builder_build_cmd', - 'msvs_external_builder_clean_cmd', - 'msvs_external_builder_clcompile_cmd', - 'msvs_enable_winrt', - 'msvs_requires_importlibrary', - 'msvs_enable_winphone', - 'msvs_enable_marmasm', - 'msvs_application_type_revision', - 'msvs_target_platform_version', - 'msvs_target_platform_minversion', + "msvs_cygwin_dirs", + "msvs_cygwin_shell", + "msvs_large_pdb", + "msvs_shard", + "msvs_external_builder", + "msvs_external_builder_out_dir", + "msvs_external_builder_build_cmd", + "msvs_external_builder_clean_cmd", + "msvs_external_builder_clcompile_cmd", + "msvs_enable_winrt", + "msvs_requires_importlibrary", + "msvs_enable_winphone", + "msvs_application_type_revision", + "msvs_target_platform_version", + "msvs_target_platform_minversion", ] generator_filelist_paths = None # List of precompiled header related keys. precomp_keys = [ - 'msvs_precompiled_header', - 'msvs_precompiled_source', + "msvs_precompiled_header", + "msvs_precompiled_source", ] @@ -111,36 +109,38 @@ # python version in depot_tools has been updated to work on Vista # 64-bit. def _GetDomainAndUserName(): - if sys.platform not in ('win32', 'cygwin'): - return ('DOMAIN', 'USERNAME') - global cached_username - global cached_domain - if not cached_domain or not cached_username: - domain = os.environ.get('USERDOMAIN') - username = os.environ.get('USERNAME') - if not domain or not username: - call = subprocess.Popen(['net', 'config', 'Workstation'], - stdout=subprocess.PIPE) - config = call.communicate()[0] - if PY3: - config = config.decode('utf-8') - username_re = re.compile(r'^User name\s+(\S+)', re.MULTILINE) - username_match = username_re.search(config) - if username_match: - username = username_match.group(1) - domain_re = re.compile(r'^Logon domain\s+(\S+)', re.MULTILINE) - domain_match = domain_re.search(config) - if domain_match: - domain = domain_match.group(1) - cached_domain = domain - cached_username = username - return (cached_domain, cached_username) + if sys.platform not in ("win32", "cygwin"): + return ("DOMAIN", "USERNAME") + global cached_username + global cached_domain + if not cached_domain or not cached_username: + domain = os.environ.get("USERDOMAIN") + username = os.environ.get("USERNAME") + if not domain or not username: + call = subprocess.Popen( + ["net", "config", "Workstation"], stdout=subprocess.PIPE + ) + config = call.communicate()[0] + if PY3: + config = config.decode("utf-8") + username_re = re.compile(r"^User name\s+(\S+)", re.MULTILINE) + username_match = username_re.search(config) + if username_match: + username = username_match.group(1) + domain_re = re.compile(r"^Logon domain\s+(\S+)", re.MULTILINE) + domain_match = domain_re.search(config) + if domain_match: + domain = domain_match.group(1) + cached_domain = domain + cached_username = username + return (cached_domain, cached_username) + fixpath_prefix = None def _NormalizedSource(source): - """Normalize the path. + """Normalize the path. But not if that gets rid of a variable, as this may expand to something larger than one directory. @@ -151,46 +151,54 @@ def _NormalizedSource(source): Returns: The normalized path. """ - normalized = os.path.normpath(source) - if source.count('$') == normalized.count('$'): - source = normalized - return source + normalized = os.path.normpath(source) + if source.count("$") == normalized.count("$"): + source = normalized + return source def _FixPath(path): - """Convert paths to a form that will make sense in a vcproj file. + """Convert paths to a form that will make sense in a vcproj file. Arguments: path: The path to convert, may contain / etc. Returns: The path with all slashes made into backslashes. """ - if fixpath_prefix and path and not os.path.isabs(path) and not path[0] == '$' and not _IsWindowsAbsPath(path): - path = os.path.join(fixpath_prefix, path) - path = path.replace('/', '\\') - path = _NormalizedSource(path) - if path and path[-1] == '\\': - path = path[:-1] - return path + if ( + fixpath_prefix + and path + and not os.path.isabs(path) + and not path[0] == "$" + and not _IsWindowsAbsPath(path) + ): + path = os.path.join(fixpath_prefix, path) + path = path.replace("/", "\\") + path = _NormalizedSource(path) + if path and path[-1] == "\\": + path = path[:-1] + return path def _IsWindowsAbsPath(path): - """ - On Cygwin systems Python needs a little help determining if a path is an absolute Windows path or not, so that + """ + On Cygwin systems Python needs a little help determining if a path + is an absolute Windows path or not, so that it does not treat those as relative, which results in bad paths like: - '..\C:\\some_source_code_file.cc' + '..\\C:\\\\some_source_code_file.cc' """ - return path.startswith('c:') or path.startswith('C:') + return path.startswith("c:") or path.startswith("C:") def _FixPaths(paths): - """Fix each of the paths of the list.""" - return [_FixPath(i) for i in paths] + """Fix each of the paths of the list.""" + return [_FixPath(i) for i in paths] -def _ConvertSourcesToFilterHierarchy(sources, prefix=None, excluded=None, - list_excluded=True, msvs_version=None): - """Converts a list split source file paths into a vcproj folder hierarchy. +def _ConvertSourcesToFilterHierarchy( + sources, prefix=None, excluded=None, list_excluded=True, msvs_version=None +): + """Converts a list split source file paths into a vcproj folder hierarchy. Arguments: sources: A list of source file paths split. @@ -208,221 +216,248 @@ def _ConvertSourcesToFilterHierarchy(sources, prefix=None, excluded=None, [MSVSProject.Filter('a', contents=['joe\\a\\bob1.c']), MSVSProject.Filter('b', contents=['joe\\b\\bob2.c'])] """ - if not prefix: prefix = [] - result = [] - excluded_result = [] - folders = OrderedDict() - # Gather files into the final result, excluded, or folders. - for s in sources: - if len(s) == 1: - filename = _NormalizedSource('\\'.join(prefix + s)) - if filename in excluded: - excluded_result.append(filename) - else: - result.append(filename) - elif msvs_version and not msvs_version.UsesVcxproj(): - # For MSVS 2008 and earlier, we need to process all files before walking - # the sub folders. - if not folders.get(s[0]): - folders[s[0]] = [] - folders[s[0]].append(s[1:]) - else: - contents = _ConvertSourcesToFilterHierarchy([s[1:]], prefix + [s[0]], - excluded=excluded, - list_excluded=list_excluded, - msvs_version=msvs_version) - contents = MSVSProject.Filter(s[0], contents=contents) - result.append(contents) - # Add a folder for excluded files. - if excluded_result and list_excluded: - excluded_folder = MSVSProject.Filter('_excluded_files', - contents=excluded_result) - result.append(excluded_folder) - - if msvs_version and msvs_version.UsesVcxproj(): + if not prefix: + prefix = [] + result = [] + excluded_result = [] + folders = OrderedDict() + # Gather files into the final result, excluded, or folders. + for s in sources: + if len(s) == 1: + filename = _NormalizedSource("\\".join(prefix + s)) + if filename in excluded: + excluded_result.append(filename) + else: + result.append(filename) + elif msvs_version and not msvs_version.UsesVcxproj(): + # For MSVS 2008 and earlier, we need to process all files before walking + # the sub folders. + if not folders.get(s[0]): + folders[s[0]] = [] + folders[s[0]].append(s[1:]) + else: + contents = _ConvertSourcesToFilterHierarchy( + [s[1:]], + prefix + [s[0]], + excluded=excluded, + list_excluded=list_excluded, + msvs_version=msvs_version, + ) + contents = MSVSProject.Filter(s[0], contents=contents) + result.append(contents) + # Add a folder for excluded files. + if excluded_result and list_excluded: + excluded_folder = MSVSProject.Filter( + "_excluded_files", contents=excluded_result + ) + result.append(excluded_folder) + + if msvs_version and msvs_version.UsesVcxproj(): + return result + + # Populate all the folders. + for f in folders: + contents = _ConvertSourcesToFilterHierarchy( + folders[f], + prefix=prefix + [f], + excluded=excluded, + list_excluded=list_excluded, + msvs_version=msvs_version, + ) + contents = MSVSProject.Filter(f, contents=contents) + result.append(contents) return result - # Populate all the folders. - for f in folders: - contents = _ConvertSourcesToFilterHierarchy(folders[f], prefix=prefix + [f], - excluded=excluded, - list_excluded=list_excluded, - msvs_version=msvs_version) - contents = MSVSProject.Filter(f, contents=contents) - result.append(contents) - return result - def _ToolAppend(tools, tool_name, setting, value, only_if_unset=False): - if not value: return - _ToolSetOrAppend(tools, tool_name, setting, value, only_if_unset) + if not value: + return + _ToolSetOrAppend(tools, tool_name, setting, value, only_if_unset) def _ToolSetOrAppend(tools, tool_name, setting, value, only_if_unset=False): - # TODO(bradnelson): ugly hack, fix this more generally!!! - if 'Directories' in setting or 'Dependencies' in setting: - if type(value) == str: - value = value.replace('/', '\\') - else: - value = [i.replace('/', '\\') for i in value] - if not tools.get(tool_name): - tools[tool_name] = dict() - tool = tools[tool_name] - if 'CompileAsWinRT' == setting: - return - if tool.get(setting): - if only_if_unset: return - if type(tool[setting]) == list and type(value) == list: - tool[setting] += value + # TODO(bradnelson): ugly hack, fix this more generally!!! + if "Directories" in setting or "Dependencies" in setting: + if type(value) == str: + value = value.replace("/", "\\") + else: + value = [i.replace("/", "\\") for i in value] + if not tools.get(tool_name): + tools[tool_name] = dict() + tool = tools[tool_name] + if "CompileAsWinRT" == setting: + return + if tool.get(setting): + if only_if_unset: + return + if type(tool[setting]) == list and type(value) == list: + tool[setting] += value + else: + raise TypeError( + 'Appending "%s" to a non-list setting "%s" for tool "%s" is ' + "not allowed, previous value: %s" + % (value, setting, tool_name, str(tool[setting])) + ) else: - raise TypeError( - 'Appending "%s" to a non-list setting "%s" for tool "%s" is ' - 'not allowed, previous value: %s' % ( - value, setting, tool_name, str(tool[setting]))) - else: - tool[setting] = value + tool[setting] = value def _ConfigTargetVersion(config_data): - return config_data.get('msvs_target_version', 'Windows7') + return config_data.get("msvs_target_version", "Windows7") -def _ConfigPlatform(config_data, spec): - return config_data.get('msvs_configuration_platform', 'Win32') +def _ConfigPlatform(config_data): + return config_data.get("msvs_configuration_platform", "Win32") def _ConfigBaseName(config_name, platform_name): - if config_name.endswith('_' + platform_name): - return config_name[0:-len(platform_name) - 1] - else: - return config_name + if config_name.endswith("_" + platform_name): + return config_name[0 : -len(platform_name) - 1] + else: + return config_name -def _ConfigFullName(config_name, config_data, spec): - platform_name = _ConfigPlatform(config_data, spec) - return '%s|%s' % (_ConfigBaseName(config_name, platform_name), platform_name) +def _ConfigFullName(config_name, config_data): + platform_name = _ConfigPlatform(config_data) + return "%s|%s" % (_ConfigBaseName(config_name, platform_name), platform_name) def _ConfigWindowsTargetPlatformVersion(config_data, version): - target_ver = config_data.get('msvs_windows_target_platform_version') - if target_ver and re.match(r'^\d+', target_ver): - return target_ver - config_ver = config_data.get('msvs_windows_sdk_version') - vers = [config_ver] if config_ver else version.compatible_sdks - for ver in vers: - for key in [ - r'HKLM\Software\Microsoft\Microsoft SDKs\Windows\%s', - r'HKLM\Software\Wow6432Node\Microsoft\Microsoft SDKs\Windows\%s']: - sdk_dir = MSVSVersion._RegistryGetValue(key % ver, 'InstallationFolder') - if not sdk_dir: - continue - version = MSVSVersion._RegistryGetValue(key % ver, 'ProductVersion') or '' - # Find a matching entry in sdk_dir\include. - expected_sdk_dir=r'%s\include' % sdk_dir - names = sorted([x for x in (os.listdir(expected_sdk_dir) - if os.path.isdir(expected_sdk_dir) - else [] - ) - if x.startswith(version)], reverse=True) - if names: - return names[0] - else: - print('Warning: No include files found for detected ' - 'Windows SDK version %s' % (version), file=sys.stdout) - - -def _BuildCommandLineForRuleRaw(spec, cmd, cygwin_shell, has_input_path, - quote_cmd, do_setup_env): - - if [x for x in cmd if '$(InputDir)' in x]: - input_dir_preamble = ( - 'set INPUTDIR=$(InputDir)\n' - 'if NOT DEFINED INPUTDIR set INPUTDIR=.\\\n' - 'set INPUTDIR=%INPUTDIR:~0,-1%\n' - ) - else: - input_dir_preamble = '' - - if cygwin_shell: - # Find path to cygwin. - cygwin_dir = _FixPath(spec.get('msvs_cygwin_dirs', ['.'])[0]) - # Prepare command. - direct_cmd = cmd - direct_cmd = [i.replace('$(IntDir)', - '`cygpath -m "${INTDIR}"`') for i in direct_cmd] - direct_cmd = [i.replace('$(OutDir)', - '`cygpath -m "${OUTDIR}"`') for i in direct_cmd] - direct_cmd = [i.replace('$(InputDir)', - '`cygpath -m "${INPUTDIR}"`') for i in direct_cmd] - if has_input_path: - direct_cmd = [i.replace('$(InputPath)', - '`cygpath -m "${INPUTPATH}"`') - for i in direct_cmd] - direct_cmd = ['\\"%s\\"' % i.replace('"', '\\\\\\"') for i in direct_cmd] - # direct_cmd = gyp.common.EncodePOSIXShellList(direct_cmd) - direct_cmd = ' '.join(direct_cmd) - # TODO(quote): regularize quoting path names throughout the module - cmd = '' - if do_setup_env: - cmd += 'call "$(ProjectDir)%(cygwin_dir)s\\setup_env.bat" && ' - cmd += 'set CYGWIN=nontsec&& ' - if direct_cmd.find('NUMBER_OF_PROCESSORS') >= 0: - cmd += 'set /a NUMBER_OF_PROCESSORS_PLUS_1=%%NUMBER_OF_PROCESSORS%%+1&& ' - if direct_cmd.find('INTDIR') >= 0: - cmd += 'set INTDIR=$(IntDir)&& ' - if direct_cmd.find('OUTDIR') >= 0: - cmd += 'set OUTDIR=$(OutDir)&& ' - if has_input_path and direct_cmd.find('INPUTPATH') >= 0: - cmd += 'set INPUTPATH=$(InputPath) && ' - cmd += 'bash -c "%(cmd)s"' - cmd = cmd % {'cygwin_dir': cygwin_dir, - 'cmd': direct_cmd} - return input_dir_preamble + cmd - else: - # Convert cat --> type to mimic unix. - if cmd[0] == 'cat': - command = ['type'] + target_ver = config_data.get("msvs_windows_target_platform_version") + if target_ver and re.match(r"^\d+", target_ver): + return target_ver + config_ver = config_data.get("msvs_windows_sdk_version") + vers = [config_ver] if config_ver else version.compatible_sdks + for ver in vers: + for key in [ + r"HKLM\Software\Microsoft\Microsoft SDKs\Windows\%s", + r"HKLM\Software\Wow6432Node\Microsoft\Microsoft SDKs\Windows\%s", + ]: + sdk_dir = MSVSVersion._RegistryGetValue(key % ver, "InstallationFolder") + if not sdk_dir: + continue + version = MSVSVersion._RegistryGetValue(key % ver, "ProductVersion") or "" + # Find a matching entry in sdk_dir\include. + expected_sdk_dir = r"%s\include" % sdk_dir + names = sorted( + [ + x + for x in ( + os.listdir(expected_sdk_dir) + if os.path.isdir(expected_sdk_dir) + else [] + ) + if x.startswith(version) + ], + reverse=True, + ) + if names: + return names[0] + else: + print( + "Warning: No include files found for detected " + "Windows SDK version %s" % (version), + file=sys.stdout, + ) + + +def _BuildCommandLineForRuleRaw( + spec, cmd, cygwin_shell, has_input_path, quote_cmd, do_setup_env +): + + if [x for x in cmd if "$(InputDir)" in x]: + input_dir_preamble = ( + "set INPUTDIR=$(InputDir)\n" + "if NOT DEFINED INPUTDIR set INPUTDIR=.\\\n" + "set INPUTDIR=%INPUTDIR:~0,-1%\n" + ) else: - command = [cmd[0].replace('/', '\\')] - # Add call before command to ensure that commands can be tied together one - # after the other without aborting in Incredibuild, since IB makes a bat - # file out of the raw command string, and some commands (like python) are - # actually batch files themselves. - command.insert(0, 'call') - # Fix the paths - # TODO(quote): This is a really ugly heuristic, and will miss path fixing - # for arguments like "--arg=path" or "/opt:path". - # If the argument starts with a slash or dash, it's probably a command line - # switch - arguments = [i if (i[:1] in "/-") else _FixPath(i) for i in cmd[1:]] - arguments = [i.replace('$(InputDir)', '%INPUTDIR%') for i in arguments] - arguments = [MSVSSettings.FixVCMacroSlashes(i) for i in arguments] - if quote_cmd: - # Support a mode for using cmd directly. - # Convert any paths to native form (first element is used directly). - # TODO(quote): regularize quoting path names throughout the module - arguments = ['"%s"' % i for i in arguments] - # Collapse into a single command. - return input_dir_preamble + ' '.join(command + arguments) + input_dir_preamble = "" + + if cygwin_shell: + # Find path to cygwin. + cygwin_dir = _FixPath(spec.get("msvs_cygwin_dirs", ["."])[0]) + # Prepare command. + direct_cmd = cmd + direct_cmd = [ + i.replace("$(IntDir)", '`cygpath -m "${INTDIR}"`') for i in direct_cmd + ] + direct_cmd = [ + i.replace("$(OutDir)", '`cygpath -m "${OUTDIR}"`') for i in direct_cmd + ] + direct_cmd = [ + i.replace("$(InputDir)", '`cygpath -m "${INPUTDIR}"`') for i in direct_cmd + ] + if has_input_path: + direct_cmd = [ + i.replace("$(InputPath)", '`cygpath -m "${INPUTPATH}"`') + for i in direct_cmd + ] + direct_cmd = ['\\"%s\\"' % i.replace('"', '\\\\\\"') for i in direct_cmd] + # direct_cmd = gyp.common.EncodePOSIXShellList(direct_cmd) + direct_cmd = " ".join(direct_cmd) + # TODO(quote): regularize quoting path names throughout the module + cmd = "" + if do_setup_env: + cmd += 'call "$(ProjectDir)%(cygwin_dir)s\\setup_env.bat" && ' + cmd += "set CYGWIN=nontsec&& " + if direct_cmd.find("NUMBER_OF_PROCESSORS") >= 0: + cmd += "set /a NUMBER_OF_PROCESSORS_PLUS_1=%%NUMBER_OF_PROCESSORS%%+1&& " + if direct_cmd.find("INTDIR") >= 0: + cmd += "set INTDIR=$(IntDir)&& " + if direct_cmd.find("OUTDIR") >= 0: + cmd += "set OUTDIR=$(OutDir)&& " + if has_input_path and direct_cmd.find("INPUTPATH") >= 0: + cmd += "set INPUTPATH=$(InputPath) && " + cmd += 'bash -c "%(cmd)s"' + cmd = cmd % {"cygwin_dir": cygwin_dir, "cmd": direct_cmd} + return input_dir_preamble + cmd + else: + # Convert cat --> type to mimic unix. + if cmd[0] == "cat": + command = ["type"] + else: + command = [cmd[0].replace("/", "\\")] + # Add call before command to ensure that commands can be tied together one + # after the other without aborting in Incredibuild, since IB makes a bat + # file out of the raw command string, and some commands (like python) are + # actually batch files themselves. + command.insert(0, "call") + # Fix the paths + # TODO(quote): This is a really ugly heuristic, and will miss path fixing + # for arguments like "--arg=path" or "/opt:path". + # If the argument starts with a slash or dash, it's probably a command line + # switch + arguments = [i if (i[:1] in "/-") else _FixPath(i) for i in cmd[1:]] + arguments = [i.replace("$(InputDir)", "%INPUTDIR%") for i in arguments] + arguments = [MSVSSettings.FixVCMacroSlashes(i) for i in arguments] + if quote_cmd: + # Support a mode for using cmd directly. + # Convert any paths to native form (first element is used directly). + # TODO(quote): regularize quoting path names throughout the module + arguments = ['"%s"' % i for i in arguments] + # Collapse into a single command. + return input_dir_preamble + " ".join(command + arguments) def _BuildCommandLineForRule(spec, rule, has_input_path, do_setup_env): - # Currently this weird argument munging is used to duplicate the way a - # python script would need to be run as part of the chrome tree. - # Eventually we should add some sort of rule_default option to set this - # per project. For now the behavior chrome needs is the default. - mcs = rule.get('msvs_cygwin_shell') - if mcs is None: - mcs = int(spec.get('msvs_cygwin_shell', 1)) - elif isinstance(mcs, str): - mcs = int(mcs) - quote_cmd = int(rule.get('msvs_quote_cmd', 1)) - return _BuildCommandLineForRuleRaw(spec, rule['action'], mcs, has_input_path, - quote_cmd, do_setup_env=do_setup_env) + # Currently this weird argument munging is used to duplicate the way a + # python script would need to be run as part of the chrome tree. + # Eventually we should add some sort of rule_default option to set this + # per project. For now the behavior chrome needs is the default. + mcs = rule.get("msvs_cygwin_shell") + if mcs is None: + mcs = int(spec.get("msvs_cygwin_shell", 1)) + elif isinstance(mcs, str): + mcs = int(mcs) + quote_cmd = int(rule.get("msvs_quote_cmd", 1)) + return _BuildCommandLineForRuleRaw( + spec, rule["action"], mcs, has_input_path, quote_cmd, do_setup_env=do_setup_env + ) def _AddActionStep(actions_dict, inputs, outputs, description, command): - """Merge action into an existing list of actions. + """Merge action into an existing list of actions. Care must be taken so that actions which have overlapping inputs either don't get assigned to the same input, or get collapsed into one. @@ -435,30 +470,31 @@ def _AddActionStep(actions_dict, inputs, outputs, description, command): description: description of the action command: command line to execute """ - # Require there to be at least one input (call sites will ensure this). - assert inputs - - action = { - 'inputs': inputs, - 'outputs': outputs, - 'description': description, - 'command': command, - } + # Require there to be at least one input (call sites will ensure this). + assert inputs + + action = { + "inputs": inputs, + "outputs": outputs, + "description": description, + "command": command, + } - # Pick where to stick this action. - # While less than optimal in terms of build time, attach them to the first - # input for now. - chosen_input = inputs[0] + # Pick where to stick this action. + # While less than optimal in terms of build time, attach them to the first + # input for now. + chosen_input = inputs[0] - # Add it there. - if chosen_input not in actions_dict: - actions_dict[chosen_input] = [] - actions_dict[chosen_input].append(action) + # Add it there. + if chosen_input not in actions_dict: + actions_dict[chosen_input] = [] + actions_dict[chosen_input].append(action) -def _AddCustomBuildToolForMSVS(p, spec, primary_input, - inputs, outputs, description, cmd): - """Add a custom build tool to execute something. +def _AddCustomBuildToolForMSVS( + p, spec, primary_input, inputs, outputs, description, cmd +): + """Add a custom build tool to execute something. Arguments: p: the target project @@ -469,23 +505,26 @@ def _AddCustomBuildToolForMSVS(p, spec, primary_input, description: description of the action cmd: command line to execute """ - inputs = _FixPaths(inputs) - outputs = _FixPaths(outputs) - tool = MSVSProject.Tool( - 'VCCustomBuildTool', - {'Description': description, - 'AdditionalDependencies': ';'.join(inputs), - 'Outputs': ';'.join(outputs), - 'CommandLine': cmd, - }) - # Add to the properties of primary input for each config. - for config_name, c_data in spec['configurations'].items(): - p.AddFileConfig(_FixPath(primary_input), - _ConfigFullName(config_name, c_data), tools=[tool]) + inputs = _FixPaths(inputs) + outputs = _FixPaths(outputs) + tool = MSVSProject.Tool( + "VCCustomBuildTool", + { + "Description": description, + "AdditionalDependencies": ";".join(inputs), + "Outputs": ";".join(outputs), + "CommandLine": cmd, + }, + ) + # Add to the properties of primary input for each config. + for config_name, c_data in spec["configurations"].items(): + p.AddFileConfig( + _FixPath(primary_input), _ConfigFullName(config_name, c_data), tools=[tool] + ) def _AddAccumulatedActionsToMSVS(p, spec, actions_dict): - """Add actions accumulated into an actions_dict, merging as needed. + """Add actions accumulated into an actions_dict, merging as needed. Arguments: p: the target project @@ -493,29 +532,32 @@ def _AddAccumulatedActionsToMSVS(p, spec, actions_dict): actions_dict: dictionary keyed on input name, which maps to a list of dicts describing the actions attached to that input file. """ - for primary_input in actions_dict: - inputs = OrderedSet() - outputs = OrderedSet() - descriptions = [] - commands = [] - for action in actions_dict[primary_input]: - inputs.update(OrderedSet(action['inputs'])) - outputs.update(OrderedSet(action['outputs'])) - descriptions.append(action['description']) - commands.append(action['command']) - # Add the custom build step for one input file. - description = ', and also '.join(descriptions) - command = '\r\n'.join(commands) - _AddCustomBuildToolForMSVS(p, spec, - primary_input=primary_input, - inputs=inputs, - outputs=outputs, - description=description, - cmd=command) + for primary_input in actions_dict: + inputs = OrderedSet() + outputs = OrderedSet() + descriptions = [] + commands = [] + for action in actions_dict[primary_input]: + inputs.update(OrderedSet(action["inputs"])) + outputs.update(OrderedSet(action["outputs"])) + descriptions.append(action["description"]) + commands.append(action["command"]) + # Add the custom build step for one input file. + description = ", and also ".join(descriptions) + command = "\r\n".join(commands) + _AddCustomBuildToolForMSVS( + p, + spec, + primary_input=primary_input, + inputs=inputs, + outputs=outputs, + description=description, + cmd=command, + ) def _RuleExpandPath(path, input_file): - """Given the input file to which a rule applied, string substitute a path. + """Given the input file to which a rule applied, string substitute a path. Arguments: path: a path to string expand @@ -523,18 +565,20 @@ def _RuleExpandPath(path, input_file): Returns: The string substituted path. """ - path = path.replace('$(InputName)', - os.path.splitext(os.path.split(input_file)[1])[0]) - path = path.replace('$(InputDir)', os.path.dirname(input_file)) - path = path.replace('$(InputExt)', - os.path.splitext(os.path.split(input_file)[1])[1]) - path = path.replace('$(InputFileName)', os.path.split(input_file)[1]) - path = path.replace('$(InputPath)', input_file) - return path + path = path.replace( + "$(InputName)", os.path.splitext(os.path.split(input_file)[1])[0] + ) + path = path.replace("$(InputDir)", os.path.dirname(input_file)) + path = path.replace( + "$(InputExt)", os.path.splitext(os.path.split(input_file)[1])[1] + ) + path = path.replace("$(InputFileName)", os.path.split(input_file)[1]) + path = path.replace("$(InputPath)", input_file) + return path def _FindRuleTriggerFiles(rule, sources): - """Find the list of files which a particular rule applies to. + """Find the list of files which a particular rule applies to. Arguments: rule: the rule in question @@ -542,11 +586,11 @@ def _FindRuleTriggerFiles(rule, sources): Returns: The list of sources that trigger a particular rule. """ - return rule.get('rule_sources', []) + return rule.get("rule_sources", []) def _RuleInputsAndOutputs(rule, trigger_file): - """Find the inputs and outputs generated by a rule. + """Find the inputs and outputs generated by a rule. Arguments: rule: the rule in question. @@ -554,20 +598,20 @@ def _RuleInputsAndOutputs(rule, trigger_file): Returns: The pair of (inputs, outputs) involved in this rule. """ - raw_inputs = _FixPaths(rule.get('inputs', [])) - raw_outputs = _FixPaths(rule.get('outputs', [])) - inputs = OrderedSet() - outputs = OrderedSet() - inputs.add(trigger_file) - for i in raw_inputs: - inputs.add(_RuleExpandPath(i, trigger_file)) - for o in raw_outputs: - outputs.add(_RuleExpandPath(o, trigger_file)) - return (inputs, outputs) + raw_inputs = _FixPaths(rule.get("inputs", [])) + raw_outputs = _FixPaths(rule.get("outputs", [])) + inputs = OrderedSet() + outputs = OrderedSet() + inputs.add(trigger_file) + for i in raw_inputs: + inputs.add(_RuleExpandPath(i, trigger_file)) + for o in raw_outputs: + outputs.add(_RuleExpandPath(o, trigger_file)) + return (inputs, outputs) def _GenerateNativeRulesForMSVS(p, rules, output_dir, spec, options): - """Generate a native rules file. + """Generate a native rules file. Arguments: p: the target project @@ -576,43 +620,43 @@ def _GenerateNativeRulesForMSVS(p, rules, output_dir, spec, options): spec: the project dict options: global generator options """ - rules_filename = '%s%s.rules' % (spec['target_name'], - options.suffix) - rules_file = MSVSToolFile.Writer(os.path.join(output_dir, rules_filename), - spec['target_name']) - # Add each rule. - for r in rules: - rule_name = r['rule_name'] - rule_ext = r['extension'] - inputs = _FixPaths(r.get('inputs', [])) - outputs = _FixPaths(r.get('outputs', [])) - # Skip a rule with no action and no inputs. - if 'action' not in r and not r.get('rule_sources', []): - continue - cmd = _BuildCommandLineForRule(spec, r, has_input_path=True, - do_setup_env=True) - rules_file.AddCustomBuildRule(name=rule_name, - description=r.get('message', rule_name), - extensions=[rule_ext], - additional_dependencies=inputs, - outputs=outputs, - cmd=cmd) - # Write out rules file. - rules_file.WriteIfChanged() - - # Add rules file to project. - p.AddToolFile(rules_filename) + rules_filename = "%s%s.rules" % (spec["target_name"], options.suffix) + rules_file = MSVSToolFile.Writer( + os.path.join(output_dir, rules_filename), spec["target_name"] + ) + # Add each rule. + for r in rules: + rule_name = r["rule_name"] + rule_ext = r["extension"] + inputs = _FixPaths(r.get("inputs", [])) + outputs = _FixPaths(r.get("outputs", [])) + # Skip a rule with no action and no inputs. + if "action" not in r and not r.get("rule_sources", []): + continue + cmd = _BuildCommandLineForRule(spec, r, has_input_path=True, do_setup_env=True) + rules_file.AddCustomBuildRule( + name=rule_name, + description=r.get("message", rule_name), + extensions=[rule_ext], + additional_dependencies=inputs, + outputs=outputs, + cmd=cmd, + ) + # Write out rules file. + rules_file.WriteIfChanged() + + # Add rules file to project. + p.AddToolFile(rules_filename) def _Cygwinify(path): - path = path.replace('$(OutDir)', '$(OutDirCygwin)') - path = path.replace('$(IntDir)', '$(IntDirCygwin)') - return path + path = path.replace("$(OutDir)", "$(OutDirCygwin)") + path = path.replace("$(IntDir)", "$(IntDirCygwin)") + return path -def _GenerateExternalRules(rules, output_dir, spec, - sources, options, actions_to_add): - """Generate an external makefile to do a set of rules. +def _GenerateExternalRules(rules, output_dir, spec, sources, options, actions_to_add): + """Generate an external makefile to do a set of rules. Arguments: rules: the list of rules to include @@ -622,77 +666,82 @@ def _GenerateExternalRules(rules, output_dir, spec, options: global generator options actions_to_add: The list of actions we will add to. """ - filename = '%s_rules%s.mk' % (spec['target_name'], options.suffix) - mk_file = gyp.common.WriteOnDiff(os.path.join(output_dir, filename)) - # Find cygwin style versions of some paths. - mk_file.write('OutDirCygwin:=$(shell cygpath -u "$(OutDir)")\n') - mk_file.write('IntDirCygwin:=$(shell cygpath -u "$(IntDir)")\n') - # Gather stuff needed to emit all: target. - all_inputs = OrderedSet() - all_outputs = OrderedSet() - all_output_dirs = OrderedSet() - first_outputs = [] - for rule in rules: - trigger_files = _FindRuleTriggerFiles(rule, sources) - for tf in trigger_files: - inputs, outputs = _RuleInputsAndOutputs(rule, tf) - all_inputs.update(OrderedSet(inputs)) - all_outputs.update(OrderedSet(outputs)) - # Only use one target from each rule as the dependency for - # 'all' so we don't try to build each rule multiple times. - first_outputs.append(list(outputs)[0]) - # Get the unique output directories for this rule. - output_dirs = [os.path.split(i)[0] for i in outputs] - for od in output_dirs: - all_output_dirs.add(od) - first_outputs_cyg = [_Cygwinify(i) for i in first_outputs] - # Write out all: target, including mkdir for each output directory. - mk_file.write('all: %s\n' % ' '.join(first_outputs_cyg)) - for od in all_output_dirs: - if od: - mk_file.write('\tmkdir -p `cygpath -u "%s"`\n' % od) - mk_file.write('\n') - # Define how each output is generated. - for rule in rules: - trigger_files = _FindRuleTriggerFiles(rule, sources) - for tf in trigger_files: - # Get all the inputs and outputs for this rule for this trigger file. - inputs, outputs = _RuleInputsAndOutputs(rule, tf) - inputs = [_Cygwinify(i) for i in inputs] - outputs = [_Cygwinify(i) for i in outputs] - # Prepare the command line for this rule. - cmd = [_RuleExpandPath(c, tf) for c in rule['action']] - cmd = ['"%s"' % i for i in cmd] - cmd = ' '.join(cmd) - # Add it to the makefile. - mk_file.write('%s: %s\n' % (' '.join(outputs), ' '.join(inputs))) - mk_file.write('\t%s\n\n' % cmd) - # Close up the file. - mk_file.close() - - # Add makefile to list of sources. - sources.add(filename) - # Add a build action to call makefile. - cmd = ['make', - 'OutDir=$(OutDir)', - 'IntDir=$(IntDir)', - '-j', '${NUMBER_OF_PROCESSORS_PLUS_1}', - '-f', filename] - cmd = _BuildCommandLineForRuleRaw(spec, cmd, True, False, True, True) - # Insert makefile as 0'th input, so it gets the action attached there, - # as this is easier to understand from in the IDE. - all_inputs = list(all_inputs) - all_inputs.insert(0, filename) - _AddActionStep(actions_to_add, - inputs=_FixPaths(all_inputs), - outputs=_FixPaths(all_outputs), - description='Running external rules for %s' % - spec['target_name'], - command=cmd) + filename = "%s_rules%s.mk" % (spec["target_name"], options.suffix) + mk_file = gyp.common.WriteOnDiff(os.path.join(output_dir, filename)) + # Find cygwin style versions of some paths. + mk_file.write('OutDirCygwin:=$(shell cygpath -u "$(OutDir)")\n') + mk_file.write('IntDirCygwin:=$(shell cygpath -u "$(IntDir)")\n') + # Gather stuff needed to emit all: target. + all_inputs = OrderedSet() + all_outputs = OrderedSet() + all_output_dirs = OrderedSet() + first_outputs = [] + for rule in rules: + trigger_files = _FindRuleTriggerFiles(rule, sources) + for tf in trigger_files: + inputs, outputs = _RuleInputsAndOutputs(rule, tf) + all_inputs.update(OrderedSet(inputs)) + all_outputs.update(OrderedSet(outputs)) + # Only use one target from each rule as the dependency for + # 'all' so we don't try to build each rule multiple times. + first_outputs.append(list(outputs)[0]) + # Get the unique output directories for this rule. + output_dirs = [os.path.split(i)[0] for i in outputs] + for od in output_dirs: + all_output_dirs.add(od) + first_outputs_cyg = [_Cygwinify(i) for i in first_outputs] + # Write out all: target, including mkdir for each output directory. + mk_file.write("all: %s\n" % " ".join(first_outputs_cyg)) + for od in all_output_dirs: + if od: + mk_file.write('\tmkdir -p `cygpath -u "%s"`\n' % od) + mk_file.write("\n") + # Define how each output is generated. + for rule in rules: + trigger_files = _FindRuleTriggerFiles(rule, sources) + for tf in trigger_files: + # Get all the inputs and outputs for this rule for this trigger file. + inputs, outputs = _RuleInputsAndOutputs(rule, tf) + inputs = [_Cygwinify(i) for i in inputs] + outputs = [_Cygwinify(i) for i in outputs] + # Prepare the command line for this rule. + cmd = [_RuleExpandPath(c, tf) for c in rule["action"]] + cmd = ['"%s"' % i for i in cmd] + cmd = " ".join(cmd) + # Add it to the makefile. + mk_file.write("%s: %s\n" % (" ".join(outputs), " ".join(inputs))) + mk_file.write("\t%s\n\n" % cmd) + # Close up the file. + mk_file.close() + + # Add makefile to list of sources. + sources.add(filename) + # Add a build action to call makefile. + cmd = [ + "make", + "OutDir=$(OutDir)", + "IntDir=$(IntDir)", + "-j", + "${NUMBER_OF_PROCESSORS_PLUS_1}", + "-f", + filename, + ] + cmd = _BuildCommandLineForRuleRaw(spec, cmd, True, False, True, True) + # Insert makefile as 0'th input, so it gets the action attached there, + # as this is easier to understand from in the IDE. + all_inputs = list(all_inputs) + all_inputs.insert(0, filename) + _AddActionStep( + actions_to_add, + inputs=_FixPaths(all_inputs), + outputs=_FixPaths(all_outputs), + description="Running external rules for %s" % spec["target_name"], + command=cmd, + ) def _EscapeEnvironmentVariableExpansion(s): - """Escapes % characters. + """Escapes % characters. Escapes any % characters so that Windows-style environment variable expansions will leave them alone. @@ -704,16 +753,16 @@ def _EscapeEnvironmentVariableExpansion(s): Returns: The escaped string. - """ - s = s.replace('%', '%%') - return s + """ # noqa: E731,E123,E501 + s = s.replace("%", "%%") + return s quote_replacer_regex = re.compile(r'(\\*)"') def _EscapeCommandLineArgumentForMSVS(s): - """Escapes a Windows command-line argument. + """Escapes a Windows command-line argument. So that the Win32 CommandLineToArgv function will turn the escaped result back into the original string. @@ -727,24 +776,24 @@ def _EscapeCommandLineArgumentForMSVS(s): the escaped string. """ - def _Replace(match): - # For a literal quote, CommandLineToArgv requires an odd number of - # backslashes preceding it, and it produces half as many literal backslashes - # (rounded down). So we need to produce 2n+1 backslashes. - return 2 * match.group(1) + '\\"' + def _Replace(match): + # For a literal quote, CommandLineToArgv requires an odd number of + # backslashes preceding it, and it produces half as many literal backslashes + # (rounded down). So we need to produce 2n+1 backslashes. + return 2 * match.group(1) + '\\"' - # Escape all quotes so that they are interpreted literally. - s = quote_replacer_regex.sub(_Replace, s) - # Now add unescaped quotes so that any whitespace is interpreted literally. - s = '"' + s + '"' - return s + # Escape all quotes so that they are interpreted literally. + s = quote_replacer_regex.sub(_Replace, s) + # Now add unescaped quotes so that any whitespace is interpreted literally. + s = '"' + s + '"' + return s -delimiters_replacer_regex = re.compile(r'(\\*)([,;]+)') +delimiters_replacer_regex = re.compile(r"(\\*)([,;]+)") def _EscapeVCProjCommandLineArgListItem(s): - """Escapes command line arguments for MSVS. + """Escapes command line arguments for MSVS. The VCProj format stores string lists in a single string using commas and semi-colons as separators, which must be quoted if they are to be @@ -765,85 +814,87 @@ def _EscapeVCProjCommandLineArgListItem(s): the escaped string. """ - def _Replace(match): - # For a non-literal quote, CommandLineToArgv requires an even number of - # backslashes preceding it, and it produces half as many literal - # backslashes. So we need to produce 2n backslashes. - return 2 * match.group(1) + '"' + match.group(2) + '"' - - segments = s.split('"') - # The unquoted segments are at the even-numbered indices. - for i in range(0, len(segments), 2): - segments[i] = delimiters_replacer_regex.sub(_Replace, segments[i]) - # Concatenate back into a single string - s = '"'.join(segments) - if len(segments) % 2 == 0: - # String ends while still quoted according to VCProj's convention. This - # means the delimiter and the next list item that follow this one in the - # .vcproj file will be misinterpreted as part of this item. There is nothing - # we can do about this. Adding an extra quote would correct the problem in - # the VCProj but cause the same problem on the final command-line. Moving - # the item to the end of the list does works, but that's only possible if - # there's only one such item. Let's just warn the user. - print('Warning: MSVS may misinterpret the odd number of ' + - 'quotes in ' + s, file=sys.stderr) - return s + def _Replace(match): + # For a non-literal quote, CommandLineToArgv requires an even number of + # backslashes preceding it, and it produces half as many literal + # backslashes. So we need to produce 2n backslashes. + return 2 * match.group(1) + '"' + match.group(2) + '"' + + segments = s.split('"') + # The unquoted segments are at the even-numbered indices. + for i in range(0, len(segments), 2): + segments[i] = delimiters_replacer_regex.sub(_Replace, segments[i]) + # Concatenate back into a single string + s = '"'.join(segments) + if len(segments) % 2 == 0: + # String ends while still quoted according to VCProj's convention. This + # means the delimiter and the next list item that follow this one in the + # .vcproj file will be misinterpreted as part of this item. There is nothing + # we can do about this. Adding an extra quote would correct the problem in + # the VCProj but cause the same problem on the final command-line. Moving + # the item to the end of the list does works, but that's only possible if + # there's only one such item. Let's just warn the user. + print( + "Warning: MSVS may misinterpret the odd number of " + "quotes in " + s, + file=sys.stderr, + ) + return s def _EscapeCppDefineForMSVS(s): - """Escapes a CPP define so that it will reach the compiler unaltered.""" - s = _EscapeEnvironmentVariableExpansion(s) - s = _EscapeCommandLineArgumentForMSVS(s) - s = _EscapeVCProjCommandLineArgListItem(s) - # cl.exe replaces literal # characters with = in preprocesor definitions for - # some reason. Octal-encode to work around that. - s = s.replace('#', '\\%03o' % ord('#')) - return s + """Escapes a CPP define so that it will reach the compiler unaltered.""" + s = _EscapeEnvironmentVariableExpansion(s) + s = _EscapeCommandLineArgumentForMSVS(s) + s = _EscapeVCProjCommandLineArgListItem(s) + # cl.exe replaces literal # characters with = in preprocessor definitions for + # some reason. Octal-encode to work around that. + s = s.replace("#", "\\%03o" % ord("#")) + return s quote_replacer_regex2 = re.compile(r'(\\+)"') def _EscapeCommandLineArgumentForMSBuild(s): - """Escapes a Windows command-line argument for use by MSBuild.""" + """Escapes a Windows command-line argument for use by MSBuild.""" - def _Replace(match): - return (len(match.group(1)) / 2 * 4) * '\\' + '\\"' + def _Replace(match): + return (len(match.group(1)) / 2 * 4) * "\\" + '\\"' - # Escape all quotes so that they are interpreted literally. - s = quote_replacer_regex2.sub(_Replace, s) - return s + # Escape all quotes so that they are interpreted literally. + s = quote_replacer_regex2.sub(_Replace, s) + return s def _EscapeMSBuildSpecialCharacters(s): - escape_dictionary = { - '%': '%25', - '$': '%24', - '@': '%40', - "'": '%27', - ';': '%3B', - '?': '%3F', - '*': '%2A' - } - result = ''.join([escape_dictionary.get(c, c) for c in s]) - return result + escape_dictionary = { + "%": "%25", + "$": "%24", + "@": "%40", + "'": "%27", + ";": "%3B", + "?": "%3F", + "*": "%2A", + } + result = "".join([escape_dictionary.get(c, c) for c in s]) + return result def _EscapeCppDefineForMSBuild(s): - """Escapes a CPP define so that it will reach the compiler unaltered.""" - s = _EscapeEnvironmentVariableExpansion(s) - s = _EscapeCommandLineArgumentForMSBuild(s) - s = _EscapeMSBuildSpecialCharacters(s) - # cl.exe replaces literal # characters with = in preprocesor definitions for - # some reason. Octal-encode to work around that. - s = s.replace('#', '\\%03o' % ord('#')) - return s + """Escapes a CPP define so that it will reach the compiler unaltered.""" + s = _EscapeEnvironmentVariableExpansion(s) + s = _EscapeCommandLineArgumentForMSBuild(s) + s = _EscapeMSBuildSpecialCharacters(s) + # cl.exe replaces literal # characters with = in preprocessor definitions for + # some reason. Octal-encode to work around that. + s = s.replace("#", "\\%03o" % ord("#")) + return s -def _GenerateRulesForMSVS(p, output_dir, options, spec, - sources, excluded_sources, - actions_to_add): - """Generate all the rules for a particular project. +def _GenerateRulesForMSVS( + p, output_dir, options, spec, sources, excluded_sources, actions_to_add +): + """Generate all the rules for a particular project. Arguments: p: the project @@ -854,45 +905,46 @@ def _GenerateRulesForMSVS(p, output_dir, options, spec, excluded_sources: the set of sources excluded from normal processing actions_to_add: deferred list of actions to add in """ - rules = spec.get('rules', []) - rules_native = [r for r in rules if not int(r.get('msvs_external_rule', 0))] - rules_external = [r for r in rules if int(r.get('msvs_external_rule', 0))] + rules = spec.get("rules", []) + rules_native = [r for r in rules if not int(r.get("msvs_external_rule", 0))] + rules_external = [r for r in rules if int(r.get("msvs_external_rule", 0))] - # Handle rules that use a native rules file. - if rules_native: - _GenerateNativeRulesForMSVS(p, rules_native, output_dir, spec, options) + # Handle rules that use a native rules file. + if rules_native: + _GenerateNativeRulesForMSVS(p, rules_native, output_dir, spec, options) - # Handle external rules (non-native rules). - if rules_external: - _GenerateExternalRules(rules_external, output_dir, spec, - sources, options, actions_to_add) - _AdjustSourcesForRules(rules, sources, excluded_sources, False) + # Handle external rules (non-native rules). + if rules_external: + _GenerateExternalRules( + rules_external, output_dir, spec, sources, options, actions_to_add + ) + _AdjustSourcesForRules(rules, sources, excluded_sources, False) def _AdjustSourcesForRules(rules, sources, excluded_sources, is_msbuild): - # Add outputs generated by each rule (if applicable). - for rule in rules: - # Add in the outputs from this rule. - trigger_files = _FindRuleTriggerFiles(rule, sources) - for trigger_file in trigger_files: - # Remove trigger_file from excluded_sources to let the rule be triggered - # (e.g. rule trigger ax_enums.idl is added to excluded_sources - # because it's also in an action's inputs in the same project) - excluded_sources.discard(_FixPath(trigger_file)) - # Done if not processing outputs as sources. - if int(rule.get('process_outputs_as_sources', False)): - inputs, outputs = _RuleInputsAndOutputs(rule, trigger_file) - inputs = OrderedSet(_FixPaths(inputs)) - outputs = OrderedSet(_FixPaths(outputs)) - inputs.remove(_FixPath(trigger_file)) - sources.update(inputs) - if not is_msbuild: - excluded_sources.update(inputs) - sources.update(outputs) + # Add outputs generated by each rule (if applicable). + for rule in rules: + # Add in the outputs from this rule. + trigger_files = _FindRuleTriggerFiles(rule, sources) + for trigger_file in trigger_files: + # Remove trigger_file from excluded_sources to let the rule be triggered + # (e.g. rule trigger ax_enums.idl is added to excluded_sources + # because it's also in an action's inputs in the same project) + excluded_sources.discard(_FixPath(trigger_file)) + # Done if not processing outputs as sources. + if int(rule.get("process_outputs_as_sources", False)): + inputs, outputs = _RuleInputsAndOutputs(rule, trigger_file) + inputs = OrderedSet(_FixPaths(inputs)) + outputs = OrderedSet(_FixPaths(outputs)) + inputs.remove(_FixPath(trigger_file)) + sources.update(inputs) + if not is_msbuild: + excluded_sources.update(inputs) + sources.update(outputs) def _FilterActionsFromExcluded(excluded_sources, actions_to_add): - """Take inputs with actions attached out of the list of exclusions. + """Take inputs with actions attached out of the list of exclusions. Arguments: excluded_sources: list of source files not to be built. @@ -900,16 +952,16 @@ def _FilterActionsFromExcluded(excluded_sources, actions_to_add): Returns: excluded_sources with files that have actions attached removed. """ - must_keep = OrderedSet(_FixPaths(actions_to_add.keys())) - return [s for s in excluded_sources if s not in must_keep] + must_keep = OrderedSet(_FixPaths(actions_to_add.keys())) + return [s for s in excluded_sources if s not in must_keep] def _GetDefaultConfiguration(spec): - return spec['configurations'][spec['default_configuration']] + return spec["configurations"][spec["default_configuration"]] def _GetGuidOfProject(proj_path, spec): - """Get the guid for the project. + """Get the guid for the project. Arguments: proj_path: Path of the vcproj or vcxproj file to generate. @@ -919,21 +971,23 @@ def _GetGuidOfProject(proj_path, spec): Raises: ValueError: if the specified GUID is invalid. """ - # Pluck out the default configuration. - default_config = _GetDefaultConfiguration(spec) - # Decide the guid of the project. - guid = default_config.get('msvs_guid') - if guid: - if VALID_MSVS_GUID_CHARS.match(guid) is None: - raise ValueError('Invalid MSVS guid: "%s". Must match regex: "%s".' % - (guid, VALID_MSVS_GUID_CHARS.pattern)) - guid = '{%s}' % guid - guid = guid or MSVSNew.MakeGuid(proj_path) - return guid + # Pluck out the default configuration. + default_config = _GetDefaultConfiguration(spec) + # Decide the guid of the project. + guid = default_config.get("msvs_guid") + if guid: + if VALID_MSVS_GUID_CHARS.match(guid) is None: + raise ValueError( + 'Invalid MSVS guid: "%s". Must match regex: "%s".' + % (guid, VALID_MSVS_GUID_CHARS.pattern) + ) + guid = "{%s}" % guid + guid = guid or MSVSNew.MakeGuid(proj_path) + return guid def _GetMsbuildToolsetOfProject(proj_path, spec, version): - """Get the platform toolset for the project. + """Get the platform toolset for the project. Arguments: proj_path: Path of the vcproj or vcxproj file to generate. @@ -942,18 +996,18 @@ def _GetMsbuildToolsetOfProject(proj_path, spec, version): Returns: the platform toolset string or None. """ - # Pluck out the default configuration. - default_config = _GetDefaultConfiguration(spec) - toolset = default_config.get('msbuild_toolset') - if not toolset and version.DefaultToolset(): - toolset = version.DefaultToolset() - if spec['type'] == 'windows_driver': - toolset = 'WindowsKernelModeDriver10.0' - return toolset + # Pluck out the default configuration. + default_config = _GetDefaultConfiguration(spec) + toolset = default_config.get("msbuild_toolset") + if not toolset and version.DefaultToolset(): + toolset = version.DefaultToolset() + if spec["type"] == "windows_driver": + toolset = "WindowsKernelModeDriver10.0" + return toolset def _GenerateProject(project, options, version, generator_flags, spec): - """Generates a vcproj file. + """Generates a vcproj file. Arguments: project: the MSVSProject object. @@ -963,56 +1017,20 @@ def _GenerateProject(project, options, version, generator_flags, spec): Returns: A list of source files that cannot be found on disk. """ - default_config = _GetDefaultConfiguration(project.spec) - - # Skip emitting anything if told to with msvs_existing_vcproj option. - if default_config.get('msvs_existing_vcproj'): - return [] - - if version.UsesVcxproj(): - return _GenerateMSBuildProject(project, options, version, generator_flags, spec) - else: - return _GenerateMSVSProject(project, options, version, generator_flags) + default_config = _GetDefaultConfiguration(project.spec) + # Skip emitting anything if told to with msvs_existing_vcproj option. + if default_config.get("msvs_existing_vcproj"): + return [] -# TODO: Avoid code duplication with _ValidateSourcesForOSX in make.py. -def _ValidateSourcesForMSVSProject(spec, version): - """Makes sure if duplicate basenames are not specified in the source list. - - Arguments: - spec: The target dictionary containing the properties of the target. - version: The VisualStudioVersion object. - """ - # This validation should not be applied to MSVC2010 and later. - assert not version.UsesVcxproj() - - # TODO: Check if MSVC allows this for loadable_module targets. - if spec.get('type', None) not in ('static_library', 'shared_library'): - return - sources = spec.get('sources', []) - basenames = {} - for source in sources: - name, ext = os.path.splitext(source) - is_compiled_file = ext in [ - '.c', '.cc', '.cpp', '.cxx', '.m', '.mm', '.s', '.S'] - if not is_compiled_file: - continue - basename = os.path.basename(name) # Don't include extension. - basenames.setdefault(basename, []).append(source) - - error = '' - for basename, files in basenames.items(): - if len(files) > 1: - error += ' %s: %s\n' % (basename, ' '.join(files)) - - if error: - print('static library %s has several files with the same basename:\n' % spec['target_name'] - + error + 'MSVC08 cannot handle that.') - raise GypError('Duplicate basenames in sources section, see list above') + if version.UsesVcxproj(): + return _GenerateMSBuildProject(project, options, version, generator_flags, spec) + else: + return _GenerateMSVSProject(project, options, version, generator_flags) def _GenerateMSVSProject(project, options, version, generator_flags): - """Generates a .vcproj file. It may create .rules and .user files too. + """Generates a .vcproj file. It may create .rules and .user files too. Arguments: project: The project object we will generate the file for. @@ -1020,85 +1038,77 @@ def _GenerateMSVSProject(project, options, version, generator_flags): version: The VisualStudioVersion object. generator_flags: dict of generator-specific flags. """ - spec = project.spec - gyp.common.EnsureDirExists(project.path) - - platforms = _GetUniquePlatforms(spec) - p = MSVSProject.Writer(project.path, version, spec['target_name'], - project.guid, platforms) - - # Get directory project file is in. - project_dir = os.path.split(project.path)[0] - gyp_path = _NormalizedSource(project.build_file) - relative_path_of_gyp_file = gyp.common.RelativePath(gyp_path, project_dir) - - config_type = _GetMSVSConfigurationType(spec, project.build_file) - for config_name, config in spec['configurations'].items(): - _AddConfigurationToMSVSProject(p, spec, config_type, config_name, config) - - # MSVC08 and prior version cannot handle duplicate basenames in the same - # target. - # TODO: Take excluded sources into consideration if possible. - _ValidateSourcesForMSVSProject(spec, version) - - # Prepare list of sources and excluded sources. - gyp_file = os.path.split(project.build_file)[1] - sources, excluded_sources = _PrepareListOfSources(spec, generator_flags, - gyp_file) - - # Add rules. - actions_to_add = {} - _GenerateRulesForMSVS(p, project_dir, options, spec, - sources, excluded_sources, - actions_to_add) - list_excluded = generator_flags.get('msvs_list_excluded_files', True) - sources, excluded_sources, excluded_idl = ( - _AdjustSourcesAndConvertToFilterHierarchy(spec, options, project_dir, - sources, excluded_sources, - list_excluded, version)) - - # Add in files. - missing_sources = _VerifySourcesExist(sources, project_dir) - p.AddFiles(sources) - - _AddToolFilesToMSVS(p, spec) - _HandlePreCompiledHeaders(p, sources, spec) - _AddActions(actions_to_add, spec, relative_path_of_gyp_file) - _AddCopies(actions_to_add, spec) - _WriteMSVSUserFile(project.path, version, spec) - - # NOTE: this stanza must appear after all actions have been decided. - # Don't excluded sources with actions attached, or they won't run. - excluded_sources = _FilterActionsFromExcluded( - excluded_sources, actions_to_add) - _ExcludeFilesFromBeingBuilt(p, spec, excluded_sources, excluded_idl, - list_excluded) - _AddAccumulatedActionsToMSVS(p, spec, actions_to_add) - - # Write it out. - p.WriteIfChanged() - - return missing_sources + spec = project.spec + gyp.common.EnsureDirExists(project.path) + + platforms = _GetUniquePlatforms(spec) + p = MSVSProject.Writer( + project.path, version, spec["target_name"], project.guid, platforms + ) + + # Get directory project file is in. + project_dir = os.path.split(project.path)[0] + gyp_path = _NormalizedSource(project.build_file) + relative_path_of_gyp_file = gyp.common.RelativePath(gyp_path, project_dir) + + config_type = _GetMSVSConfigurationType(spec, project.build_file) + for config_name, config in spec["configurations"].items(): + _AddConfigurationToMSVSProject(p, spec, config_type, config_name, config) + + # Prepare list of sources and excluded sources. + gyp_file = os.path.split(project.build_file)[1] + sources, excluded_sources = _PrepareListOfSources(spec, generator_flags, gyp_file) + + # Add rules. + actions_to_add = {} + _GenerateRulesForMSVS( + p, project_dir, options, spec, sources, excluded_sources, actions_to_add + ) + list_excluded = generator_flags.get("msvs_list_excluded_files", True) + sources, excluded_sources, excluded_idl = _AdjustSourcesAndConvertToFilterHierarchy( + spec, options, project_dir, sources, excluded_sources, list_excluded, version + ) + + # Add in files. + missing_sources = _VerifySourcesExist(sources, project_dir) + p.AddFiles(sources) + + _AddToolFilesToMSVS(p, spec) + _HandlePreCompiledHeaders(p, sources, spec) + _AddActions(actions_to_add, spec, relative_path_of_gyp_file) + _AddCopies(actions_to_add, spec) + _WriteMSVSUserFile(project.path, version, spec) + + # NOTE: this stanza must appear after all actions have been decided. + # Don't excluded sources with actions attached, or they won't run. + excluded_sources = _FilterActionsFromExcluded(excluded_sources, actions_to_add) + _ExcludeFilesFromBeingBuilt(p, spec, excluded_sources, excluded_idl, list_excluded) + _AddAccumulatedActionsToMSVS(p, spec, actions_to_add) + + # Write it out. + p.WriteIfChanged() + + return missing_sources def _GetUniquePlatforms(spec): - """Returns the list of unique platforms for this spec, e.g ['win32', ...]. + """Returns the list of unique platforms for this spec, e.g ['win32', ...]. Arguments: spec: The target dictionary containing the properties of the target. Returns: The MSVSUserFile object created. """ - # Gather list of unique platforms. - platforms = OrderedSet() - for configuration in spec['configurations']: - platforms.add(_ConfigPlatform(spec['configurations'][configuration], spec)) - platforms = list(platforms) - return platforms + # Gather list of unique platforms. + platforms = OrderedSet() + for configuration in spec["configurations"]: + platforms.add(_ConfigPlatform(spec["configurations"][configuration])) + platforms = list(platforms) + return platforms def _CreateMSVSUserFile(proj_path, version, spec): - """Generates a .user file for the user running this Gyp program. + """Generates a .user file for the user running this Gyp program. Arguments: proj_path: The path of the project file being created. The .user file @@ -1108,15 +1118,14 @@ def _CreateMSVSUserFile(proj_path, version, spec): Returns: The MSVSUserFile object created. """ - (domain, username) = _GetDomainAndUserName() - vcuser_filename = '.'.join([proj_path, domain, username, 'user']) - user_file = MSVSUserFile.Writer(vcuser_filename, version, - spec['target_name']) - return user_file + (domain, username) = _GetDomainAndUserName() + vcuser_filename = ".".join([proj_path, domain, username, "user"]) + user_file = MSVSUserFile.Writer(vcuser_filename, version, spec["target_name"]) + return user_file def _GetMSVSConfigurationType(spec, build_file): - """Returns the configuration type for this project. + """Returns the configuration type for this project. It's a number defined by Microsoft. May raise an exception. @@ -1126,28 +1135,31 @@ def _GetMSVSConfigurationType(spec, build_file): Returns: An integer, the configuration type. """ - try: - config_type = { - 'executable': '1', # .exe - 'shared_library': '2', # .dll - 'loadable_module': '2', # .dll - 'static_library': '4', # .lib - 'windows_driver': '5', # .sys - 'none': '10', # Utility type - }[spec['type']] - except KeyError: - if spec.get('type'): - raise GypError('Target type %s is not a valid target type for ' - 'target %s in %s.' % - (spec['type'], spec['target_name'], build_file)) - else: - raise GypError('Missing type field for target %s in %s.' % - (spec['target_name'], build_file)) - return config_type + try: + config_type = { + "executable": "1", # .exe + "shared_library": "2", # .dll + "loadable_module": "2", # .dll + "static_library": "4", # .lib + "windows_driver": "5", # .sys + "none": "10", # Utility type + }[spec["type"]] + except KeyError: + if spec.get("type"): + raise GypError( + "Target type %s is not a valid target type for " + "target %s in %s." % (spec["type"], spec["target_name"], build_file) + ) + else: + raise GypError( + "Missing type field for target %s in %s." + % (spec["target_name"], build_file) + ) + return config_type def _AddConfigurationToMSVSProject(p, spec, config_type, config_name, config): - """Adds a configuration to the MSVS project. + """Adds a configuration to the MSVS project. Many settings in a vcproj file are specific to a configuration. This function the main part of the vcproj file that's configuration specific. @@ -1160,81 +1172,84 @@ def _AddConfigurationToMSVSProject(p, spec, config_type, config_name, config): config: The dictionary that defines the special processing to be done for this configuration. """ - # Get the information for this configuration - include_dirs, midl_include_dirs, resource_include_dirs = \ - _GetIncludeDirs(config) - libraries = _GetLibraries(spec) - library_dirs = _GetLibraryDirs(config) - out_file, vc_tool, _ = _GetOutputFilePathAndTool(spec, msbuild=False) - defines = _GetDefines(config) - defines = [_EscapeCppDefineForMSVS(d) for d in defines] - disabled_warnings = _GetDisabledWarnings(config) - prebuild = config.get('msvs_prebuild') - postbuild = config.get('msvs_postbuild') - def_file = _GetModuleDefinition(spec) - precompiled_header = config.get('msvs_precompiled_header') - - # Prepare the list of tools as a dictionary. - tools = dict() - # Add in user specified msvs_settings. - msvs_settings = config.get('msvs_settings', {}) - MSVSSettings.ValidateMSVSSettings(msvs_settings) - - # Prevent default library inheritance from the environment. - _ToolAppend(tools, 'VCLinkerTool', 'AdditionalDependencies', ['$(NOINHERIT)']) - - for tool in msvs_settings: - settings = config['msvs_settings'][tool] - for setting in settings: - _ToolAppend(tools, tool, setting, settings[setting]) - # Add the information to the appropriate tool - _ToolAppend(tools, 'VCCLCompilerTool', - 'AdditionalIncludeDirectories', include_dirs) - _ToolAppend(tools, 'VCMIDLTool', - 'AdditionalIncludeDirectories', midl_include_dirs) - _ToolAppend(tools, 'VCResourceCompilerTool', - 'AdditionalIncludeDirectories', resource_include_dirs) - # Add in libraries. - _ToolAppend(tools, 'VCLinkerTool', 'AdditionalDependencies', libraries) - _ToolAppend(tools, 'VCLinkerTool', 'AdditionalLibraryDirectories', - library_dirs) - if out_file: - _ToolAppend(tools, vc_tool, 'OutputFile', out_file, only_if_unset=True) - # Add defines. - _ToolAppend(tools, 'VCCLCompilerTool', 'PreprocessorDefinitions', defines) - _ToolAppend(tools, 'VCResourceCompilerTool', 'PreprocessorDefinitions', - defines) - # Change program database directory to prevent collisions. - _ToolAppend(tools, 'VCCLCompilerTool', 'ProgramDataBaseFileName', - '$(IntDir)$(ProjectName)\\vc80.pdb', only_if_unset=True) - # Add disabled warnings. - _ToolAppend(tools, 'VCCLCompilerTool', - 'DisableSpecificWarnings', disabled_warnings) - # Add Pre-build. - _ToolAppend(tools, 'VCPreBuildEventTool', 'CommandLine', prebuild) - # Add Post-build. - _ToolAppend(tools, 'VCPostBuildEventTool', 'CommandLine', postbuild) - # Turn on precompiled headers if appropriate. - if precompiled_header: - precompiled_header = os.path.split(precompiled_header)[1] - _ToolAppend(tools, 'VCCLCompilerTool', 'UsePrecompiledHeader', '2') - _ToolAppend(tools, 'VCCLCompilerTool', - 'PrecompiledHeaderThrough', precompiled_header) - _ToolAppend(tools, 'VCCLCompilerTool', - 'ForcedIncludeFiles', precompiled_header) - # Loadable modules don't generate import libraries; - # tell dependent projects to not expect one. - if spec['type'] == 'loadable_module': - _ToolAppend(tools, 'VCLinkerTool', 'IgnoreImportLibrary', 'true') - # Set the module definition file if any. - if def_file: - _ToolAppend(tools, 'VCLinkerTool', 'ModuleDefinitionFile', def_file) - - _AddConfigurationToMSVS(p, spec, tools, config, config_type, config_name) + # Get the information for this configuration + include_dirs, midl_include_dirs, resource_include_dirs = _GetIncludeDirs(config) + libraries = _GetLibraries(spec) + library_dirs = _GetLibraryDirs(config) + out_file, vc_tool, _ = _GetOutputFilePathAndTool(spec, msbuild=False) + defines = _GetDefines(config) + defines = [_EscapeCppDefineForMSVS(d) for d in defines] + disabled_warnings = _GetDisabledWarnings(config) + prebuild = config.get("msvs_prebuild") + postbuild = config.get("msvs_postbuild") + def_file = _GetModuleDefinition(spec) + precompiled_header = config.get("msvs_precompiled_header") + + # Prepare the list of tools as a dictionary. + tools = dict() + # Add in user specified msvs_settings. + msvs_settings = config.get("msvs_settings", {}) + MSVSSettings.ValidateMSVSSettings(msvs_settings) + + # Prevent default library inheritance from the environment. + _ToolAppend(tools, "VCLinkerTool", "AdditionalDependencies", ["$(NOINHERIT)"]) + + for tool in msvs_settings: + settings = config["msvs_settings"][tool] + for setting in settings: + _ToolAppend(tools, tool, setting, settings[setting]) + # Add the information to the appropriate tool + _ToolAppend(tools, "VCCLCompilerTool", "AdditionalIncludeDirectories", include_dirs) + _ToolAppend(tools, "VCMIDLTool", "AdditionalIncludeDirectories", midl_include_dirs) + _ToolAppend( + tools, + "VCResourceCompilerTool", + "AdditionalIncludeDirectories", + resource_include_dirs, + ) + # Add in libraries. + _ToolAppend(tools, "VCLinkerTool", "AdditionalDependencies", libraries) + _ToolAppend(tools, "VCLinkerTool", "AdditionalLibraryDirectories", library_dirs) + if out_file: + _ToolAppend(tools, vc_tool, "OutputFile", out_file, only_if_unset=True) + # Add defines. + _ToolAppend(tools, "VCCLCompilerTool", "PreprocessorDefinitions", defines) + _ToolAppend(tools, "VCResourceCompilerTool", "PreprocessorDefinitions", defines) + # Change program database directory to prevent collisions. + _ToolAppend( + tools, + "VCCLCompilerTool", + "ProgramDataBaseFileName", + "$(IntDir)$(ProjectName)\\vc80.pdb", + only_if_unset=True, + ) + # Add disabled warnings. + _ToolAppend(tools, "VCCLCompilerTool", "DisableSpecificWarnings", disabled_warnings) + # Add Pre-build. + _ToolAppend(tools, "VCPreBuildEventTool", "CommandLine", prebuild) + # Add Post-build. + _ToolAppend(tools, "VCPostBuildEventTool", "CommandLine", postbuild) + # Turn on precompiled headers if appropriate. + if precompiled_header: + precompiled_header = os.path.split(precompiled_header)[1] + _ToolAppend(tools, "VCCLCompilerTool", "UsePrecompiledHeader", "2") + _ToolAppend( + tools, "VCCLCompilerTool", "PrecompiledHeaderThrough", precompiled_header + ) + _ToolAppend(tools, "VCCLCompilerTool", "ForcedIncludeFiles", precompiled_header) + # Loadable modules don't generate import libraries; + # tell dependent projects to not expect one. + if spec["type"] == "loadable_module": + _ToolAppend(tools, "VCLinkerTool", "IgnoreImportLibrary", "true") + # Set the module definition file if any. + if def_file: + _ToolAppend(tools, "VCLinkerTool", "ModuleDefinitionFile", def_file) + + _AddConfigurationToMSVS(p, spec, tools, config, config_type, config_name) def _GetIncludeDirs(config): - """Returns the list of directories to be used for #include directives. + """Returns the list of directories to be used for #include directives. Arguments: config: The dictionary that defines the special processing to be done @@ -1242,23 +1257,23 @@ def _GetIncludeDirs(config): Returns: The list of directory paths. """ - # TODO(bradnelson): include_dirs should really be flexible enough not to - # require this sort of thing. - include_dirs = ( - config.get('include_dirs', []) + - config.get('msvs_system_include_dirs', [])) - midl_include_dirs = ( - config.get('midl_include_dirs', []) + - config.get('msvs_system_include_dirs', [])) - resource_include_dirs = config.get('resource_include_dirs', include_dirs) - include_dirs = _FixPaths(include_dirs) - midl_include_dirs = _FixPaths(midl_include_dirs) - resource_include_dirs = _FixPaths(resource_include_dirs) - return include_dirs, midl_include_dirs, resource_include_dirs + # TODO(bradnelson): include_dirs should really be flexible enough not to + # require this sort of thing. + include_dirs = config.get("include_dirs", []) + config.get( + "msvs_system_include_dirs", [] + ) + midl_include_dirs = config.get("midl_include_dirs", []) + config.get( + "msvs_system_include_dirs", [] + ) + resource_include_dirs = config.get("resource_include_dirs", include_dirs) + include_dirs = _FixPaths(include_dirs) + midl_include_dirs = _FixPaths(midl_include_dirs) + resource_include_dirs = _FixPaths(resource_include_dirs) + return include_dirs, midl_include_dirs, resource_include_dirs def _GetLibraryDirs(config): - """Returns the list of directories to be used for library search paths. + """Returns the list of directories to be used for library search paths. Arguments: config: The dictionary that defines the special processing to be done @@ -1267,39 +1282,39 @@ def _GetLibraryDirs(config): The list of directory paths. """ - library_dirs = config.get('library_dirs', []) - library_dirs = _FixPaths(library_dirs) - return library_dirs + library_dirs = config.get("library_dirs", []) + library_dirs = _FixPaths(library_dirs) + return library_dirs def _GetLibraries(spec): - """Returns the list of libraries for this configuration. + """Returns the list of libraries for this configuration. Arguments: spec: The target dictionary containing the properties of the target. Returns: The list of directory paths. """ - libraries = spec.get('libraries', []) - # Strip out -l, as it is not used on windows (but is needed so we can pass - # in libraries that are assumed to be in the default library path). - # Also remove duplicate entries, leaving only the last duplicate, while - # preserving order. - found = OrderedSet() - unique_libraries_list = [] - for entry in reversed(libraries): - library = re.sub(r'^\-l', '', entry) - if not os.path.splitext(library)[1]: - library += '.lib' - if library not in found: - found.add(library) - unique_libraries_list.append(library) - unique_libraries_list.reverse() - return unique_libraries_list + libraries = spec.get("libraries", []) + # Strip out -l, as it is not used on windows (but is needed so we can pass + # in libraries that are assumed to be in the default library path). + # Also remove duplicate entries, leaving only the last duplicate, while + # preserving order. + found = OrderedSet() + unique_libraries_list = [] + for entry in reversed(libraries): + library = re.sub(r"^\-l", "", entry) + if not os.path.splitext(library)[1]: + library += ".lib" + if library not in found: + found.add(library) + unique_libraries_list.append(library) + unique_libraries_list.reverse() + return unique_libraries_list def _GetOutputFilePathAndTool(spec, msbuild): - """Returns the path and tool to use for this target. + """Returns the path and tool to use for this target. Figures out the path of the file this spec will create and the name of the VC tool that will create it. @@ -1309,36 +1324,36 @@ def _GetOutputFilePathAndTool(spec, msbuild): Returns: A triple of (file path, name of the vc tool, name of the msbuild tool) """ - # Select a name for the output file. - out_file = '' - vc_tool = '' - msbuild_tool = '' - output_file_map = { - 'executable': ('VCLinkerTool', 'Link', '$(OutDir)', '.exe'), - 'shared_library': ('VCLinkerTool', 'Link', '$(OutDir)', '.dll'), - 'loadable_module': ('VCLinkerTool', 'Link', '$(OutDir)', '.dll'), - 'windows_driver': ('VCLinkerTool', 'Link', '$(OutDir)', '.sys'), - 'static_library': ('VCLibrarianTool', 'Lib', '$(OutDir)lib\\', '.lib'), - } - output_file_props = output_file_map.get(spec['type']) - if output_file_props and int(spec.get('msvs_auto_output_file', 1)): - vc_tool, msbuild_tool, out_dir, suffix = output_file_props - if spec.get('standalone_static_library', 0): - out_dir = '$(OutDir)' - out_dir = spec.get('product_dir', out_dir) - product_extension = spec.get('product_extension') - if product_extension: - suffix = '.' + product_extension - elif msbuild: - suffix = '$(TargetExt)' - prefix = spec.get('product_prefix', '') - product_name = spec.get('product_name', '$(ProjectName)') - out_file = ntpath.join(out_dir, prefix + product_name + suffix) - return out_file, vc_tool, msbuild_tool + # Select a name for the output file. + out_file = "" + vc_tool = "" + msbuild_tool = "" + output_file_map = { + "executable": ("VCLinkerTool", "Link", "$(OutDir)", ".exe"), + "shared_library": ("VCLinkerTool", "Link", "$(OutDir)", ".dll"), + "loadable_module": ("VCLinkerTool", "Link", "$(OutDir)", ".dll"), + "windows_driver": ("VCLinkerTool", "Link", "$(OutDir)", ".sys"), + "static_library": ("VCLibrarianTool", "Lib", "$(OutDir)lib\\", ".lib"), + } + output_file_props = output_file_map.get(spec["type"]) + if output_file_props and int(spec.get("msvs_auto_output_file", 1)): + vc_tool, msbuild_tool, out_dir, suffix = output_file_props + if spec.get("standalone_static_library", 0): + out_dir = "$(OutDir)" + out_dir = spec.get("product_dir", out_dir) + product_extension = spec.get("product_extension") + if product_extension: + suffix = "." + product_extension + elif msbuild: + suffix = "$(TargetExt)" + prefix = spec.get("product_prefix", "") + product_name = spec.get("product_name", "$(ProjectName)") + out_file = ntpath.join(out_dir, prefix + product_name + suffix) + return out_file, vc_tool, msbuild_tool def _GetOutputTargetExt(spec): - """Returns the extension for this target, including the dot + """Returns the extension for this target, including the dot If product_extension is specified, set target_extension to this to avoid MSB8012, returns None otherwise. Ignores any target_extension settings in @@ -1349,14 +1364,14 @@ def _GetOutputTargetExt(spec): Returns: A string with the extension, or None """ - target_extension = spec.get('product_extension') - if target_extension: - return '.' + target_extension - return None + target_extension = spec.get("product_extension") + if target_extension: + return "." + target_extension + return None def _GetDefines(config): - """Returns the list of preprocessor definitions for this configuation. + """Returns the list of preprocessor definitions for this configuration. Arguments: config: The dictionary that defines the special processing to be done @@ -1364,64 +1379,68 @@ def _GetDefines(config): Returns: The list of preprocessor definitions. """ - defines = [] - for d in config.get('defines', []): - if type(d) == list: - fd = '='.join([str(dpart) for dpart in d]) - else: - fd = str(d) - defines.append(fd) - return defines + defines = [] + for d in config.get("defines", []): + if type(d) == list: + fd = "=".join([str(dpart) for dpart in d]) + else: + fd = str(d) + defines.append(fd) + return defines def _GetDisabledWarnings(config): - return [str(i) for i in config.get('msvs_disabled_warnings', [])] + return [str(i) for i in config.get("msvs_disabled_warnings", [])] def _GetModuleDefinition(spec): - def_file = '' - if spec['type'] in ['shared_library', 'loadable_module', 'executable', - 'windows_driver']: - def_files = [s for s in spec.get('sources', []) if s.endswith('.def')] - if len(def_files) == 1: - def_file = _FixPath(def_files[0]) - elif def_files: - raise ValueError( - 'Multiple module definition files in one target, target %s lists ' - 'multiple .def files: %s' % ( - spec['target_name'], ' '.join(def_files))) - return def_file + def_file = "" + if spec["type"] in [ + "shared_library", + "loadable_module", + "executable", + "windows_driver", + ]: + def_files = [s for s in spec.get("sources", []) if s.endswith(".def")] + if len(def_files) == 1: + def_file = _FixPath(def_files[0]) + elif def_files: + raise ValueError( + "Multiple module definition files in one target, target %s lists " + "multiple .def files: %s" % (spec["target_name"], " ".join(def_files)) + ) + return def_file def _ConvertToolsToExpectedForm(tools): - """Convert tools to a form expected by Visual Studio. + """Convert tools to a form expected by Visual Studio. Arguments: tools: A dictionary of settings; the tool name is the key. Returns: A list of Tool objects. """ - tool_list = [] - for tool, settings in tools.items(): - # Collapse settings with lists. - settings_fixed = {} - for setting, value in settings.items(): - if type(value) == list: - if ((tool == 'VCLinkerTool' and - setting == 'AdditionalDependencies') or - setting == 'AdditionalOptions'): - settings_fixed[setting] = ' '.join(value) - else: - settings_fixed[setting] = ';'.join(value) - else: - settings_fixed[setting] = value - # Add in this tool. - tool_list.append(MSVSProject.Tool(tool, settings_fixed)) - return tool_list + tool_list = [] + for tool, settings in tools.items(): + # Collapse settings with lists. + settings_fixed = {} + for setting, value in settings.items(): + if type(value) == list: + if ( + tool == "VCLinkerTool" and setting == "AdditionalDependencies" + ) or setting == "AdditionalOptions": + settings_fixed[setting] = " ".join(value) + else: + settings_fixed[setting] = ";".join(value) + else: + settings_fixed[setting] = value + # Add in this tool. + tool_list.append(MSVSProject.Tool(tool, settings_fixed)) + return tool_list def _AddConfigurationToMSVS(p, spec, tools, config, config_type, config_name): - """Add to the project file the configuration specified by config. + """Add to the project file the configuration specified by config. Arguments: p: The target project being generated. @@ -1432,45 +1451,45 @@ def _AddConfigurationToMSVS(p, spec, tools, config, config_type, config_name): config_type: The configuration type, a number as defined by Microsoft. config_name: The name of the configuration. """ - attributes = _GetMSVSAttributes(spec, config, config_type) - # Add in this configuration. - tool_list = _ConvertToolsToExpectedForm(tools) - p.AddConfig(_ConfigFullName(config_name, config), - attrs=attributes, tools=tool_list) + attributes = _GetMSVSAttributes(spec, config, config_type) + # Add in this configuration. + tool_list = _ConvertToolsToExpectedForm(tools) + p.AddConfig(_ConfigFullName(config_name, config), attrs=attributes, tools=tool_list) def _GetMSVSAttributes(spec, config, config_type): - # Prepare configuration attributes. - prepared_attrs = {} - source_attrs = config.get('msvs_configuration_attributes', {}) - for a in source_attrs: - prepared_attrs[a] = source_attrs[a] - # Add props files. - vsprops_dirs = config.get('msvs_props', []) - vsprops_dirs = _FixPaths(vsprops_dirs) - if vsprops_dirs: - prepared_attrs['InheritedPropertySheets'] = ';'.join(vsprops_dirs) - # Set configuration type. - prepared_attrs['ConfigurationType'] = config_type - output_dir = prepared_attrs.get('OutputDirectory', - '$(SolutionDir)$(ConfigurationName)') - prepared_attrs['OutputDirectory'] = _FixPath(output_dir) + '\\' - if 'IntermediateDirectory' not in prepared_attrs: - intermediate = '$(ConfigurationName)\\obj\\$(ProjectName)' - prepared_attrs['IntermediateDirectory'] = _FixPath(intermediate) + '\\' - else: - intermediate = _FixPath(prepared_attrs['IntermediateDirectory']) + '\\' - intermediate = MSVSSettings.FixVCMacroSlashes(intermediate) - prepared_attrs['IntermediateDirectory'] = intermediate - return prepared_attrs + # Prepare configuration attributes. + prepared_attrs = {} + source_attrs = config.get("msvs_configuration_attributes", {}) + for a in source_attrs: + prepared_attrs[a] = source_attrs[a] + # Add props files. + vsprops_dirs = config.get("msvs_props", []) + vsprops_dirs = _FixPaths(vsprops_dirs) + if vsprops_dirs: + prepared_attrs["InheritedPropertySheets"] = ";".join(vsprops_dirs) + # Set configuration type. + prepared_attrs["ConfigurationType"] = config_type + output_dir = prepared_attrs.get( + "OutputDirectory", "$(SolutionDir)$(ConfigurationName)" + ) + prepared_attrs["OutputDirectory"] = _FixPath(output_dir) + "\\" + if "IntermediateDirectory" not in prepared_attrs: + intermediate = "$(ConfigurationName)\\obj\\$(ProjectName)" + prepared_attrs["IntermediateDirectory"] = _FixPath(intermediate) + "\\" + else: + intermediate = _FixPath(prepared_attrs["IntermediateDirectory"]) + "\\" + intermediate = MSVSSettings.FixVCMacroSlashes(intermediate) + prepared_attrs["IntermediateDirectory"] = intermediate + return prepared_attrs def _AddNormalizedSources(sources_set, sources_array): - sources_set.update(_NormalizedSource(s) for s in sources_array) + sources_set.update(_NormalizedSource(s) for s in sources_array) def _PrepareListOfSources(spec, generator_flags, gyp_file): - """Prepare list of sources and excluded sources. + """Prepare list of sources and excluded sources. Besides the sources specified directly in the spec, adds the gyp file so that a change to it will cause a re-compile. Also adds appropriate sources @@ -1484,33 +1503,34 @@ def _PrepareListOfSources(spec, generator_flags, gyp_file): A pair of (list of sources, list of excluded sources). The sources will be relative to the gyp file. """ - sources = OrderedSet() - _AddNormalizedSources(sources, spec.get('sources', [])) - excluded_sources = OrderedSet() - # Add in the gyp file. - if not generator_flags.get('standalone'): - sources.add(gyp_file) - - # Add in 'action' inputs and outputs. - for a in spec.get('actions', []): - inputs = a['inputs'] - inputs = [_NormalizedSource(i) for i in inputs] - # Add all inputs to sources and excluded sources. - inputs = OrderedSet(inputs) - sources.update(inputs) - if not spec.get('msvs_external_builder'): - excluded_sources.update(inputs) - if int(a.get('process_outputs_as_sources', False)): - _AddNormalizedSources(sources, a.get('outputs', [])) - # Add in 'copies' inputs and outputs. - for cpy in spec.get('copies', []): - _AddNormalizedSources(sources, cpy.get('files', [])) - return (sources, excluded_sources) + sources = OrderedSet() + _AddNormalizedSources(sources, spec.get("sources", [])) + excluded_sources = OrderedSet() + # Add in the gyp file. + if not generator_flags.get("standalone"): + sources.add(gyp_file) + + # Add in 'action' inputs and outputs. + for a in spec.get("actions", []): + inputs = a["inputs"] + inputs = [_NormalizedSource(i) for i in inputs] + # Add all inputs to sources and excluded sources. + inputs = OrderedSet(inputs) + sources.update(inputs) + if not spec.get("msvs_external_builder"): + excluded_sources.update(inputs) + if int(a.get("process_outputs_as_sources", False)): + _AddNormalizedSources(sources, a.get("outputs", [])) + # Add in 'copies' inputs and outputs. + for cpy in spec.get("copies", []): + _AddNormalizedSources(sources, cpy.get("files", [])) + return (sources, excluded_sources) def _AdjustSourcesAndConvertToFilterHierarchy( - spec, options, gyp_dir, sources, excluded_sources, list_excluded, version): - """Adjusts the list of sources and excluded sources. + spec, options, gyp_dir, sources, excluded_sources, list_excluded, version +): + """Adjusts the list of sources and excluded sources. Also converts the sets to lists. @@ -1525,337 +1545,377 @@ def _AdjustSourcesAndConvertToFilterHierarchy( A trio of (list of sources, list of excluded sources, path of excluded IDL file) """ - # Exclude excluded sources coming into the generator. - excluded_sources.update(OrderedSet(spec.get('sources_excluded', []))) - # Add excluded sources into sources for good measure. - sources.update(excluded_sources) - # Convert to proper windows form. - # NOTE: sources goes from being a set to a list here. - # NOTE: excluded_sources goes from being a set to a list here. - sources = _FixPaths(sources) - # Convert to proper windows form. - excluded_sources = _FixPaths(excluded_sources) - - excluded_idl = _IdlFilesHandledNonNatively(spec, sources) - - precompiled_related = _GetPrecompileRelatedFiles(spec) - # Find the excluded ones, minus the precompiled header related ones. - fully_excluded = [i for i in excluded_sources if i not in precompiled_related] - - # Convert to folders and the right slashes. - sources = [i.split('\\') for i in sources] - sources = _ConvertSourcesToFilterHierarchy(sources, excluded=fully_excluded, - list_excluded=list_excluded, - msvs_version=version) - - # Prune filters with a single child to flatten ugly directory structures - # such as ../../src/modules/module1 etc. - if version.UsesVcxproj(): - while all([isinstance(s, MSVSProject.Filter) for s in sources]) \ - and len(set([s.name for s in sources])) == 1: - assert all([len(s.contents) == 1 for s in sources]) - sources = [s.contents[0] for s in sources] - else: - while len(sources) == 1 and isinstance(sources[0], MSVSProject.Filter): - sources = sources[0].contents - - return sources, excluded_sources, excluded_idl + # Exclude excluded sources coming into the generator. + excluded_sources.update(OrderedSet(spec.get("sources_excluded", []))) + # Add excluded sources into sources for good measure. + sources.update(excluded_sources) + # Convert to proper windows form. + # NOTE: sources goes from being a set to a list here. + # NOTE: excluded_sources goes from being a set to a list here. + sources = _FixPaths(sources) + # Convert to proper windows form. + excluded_sources = _FixPaths(excluded_sources) + + excluded_idl = _IdlFilesHandledNonNatively(spec, sources) + + precompiled_related = _GetPrecompileRelatedFiles(spec) + # Find the excluded ones, minus the precompiled header related ones. + fully_excluded = [i for i in excluded_sources if i not in precompiled_related] + + # Convert to folders and the right slashes. + sources = [i.split("\\") for i in sources] + sources = _ConvertSourcesToFilterHierarchy( + sources, + excluded=fully_excluded, + list_excluded=list_excluded, + msvs_version=version, + ) + + # Prune filters with a single child to flatten ugly directory structures + # such as ../../src/modules/module1 etc. + if version.UsesVcxproj(): + while ( + all([isinstance(s, MSVSProject.Filter) for s in sources]) + and len(set([s.name for s in sources])) == 1 + ): + assert all([len(s.contents) == 1 for s in sources]) + sources = [s.contents[0] for s in sources] + else: + while len(sources) == 1 and isinstance(sources[0], MSVSProject.Filter): + sources = sources[0].contents + + return sources, excluded_sources, excluded_idl def _IdlFilesHandledNonNatively(spec, sources): - # If any non-native rules use 'idl' as an extension exclude idl files. - # Gather a list here to use later. - using_idl = False - for rule in spec.get('rules', []): - if rule['extension'] == 'idl' and int(rule.get('msvs_external_rule', 0)): - using_idl = True - break - if using_idl: - excluded_idl = [i for i in sources if i.endswith('.idl')] - else: - excluded_idl = [] - return excluded_idl + # If any non-native rules use 'idl' as an extension exclude idl files. + # Gather a list here to use later. + using_idl = False + for rule in spec.get("rules", []): + if rule["extension"] == "idl" and int(rule.get("msvs_external_rule", 0)): + using_idl = True + break + if using_idl: + excluded_idl = [i for i in sources if i.endswith(".idl")] + else: + excluded_idl = [] + return excluded_idl def _GetPrecompileRelatedFiles(spec): - # Gather a list of precompiled header related sources. - precompiled_related = [] - for _, config in spec['configurations'].items(): - for k in precomp_keys: - f = config.get(k) - if f: - precompiled_related.append(_FixPath(f)) - return precompiled_related - - -def _ExcludeFilesFromBeingBuilt(p, spec, excluded_sources, excluded_idl, - list_excluded): - exclusions = _GetExcludedFilesFromBuild(spec, excluded_sources, excluded_idl) - for file_name, excluded_configs in exclusions.items(): - if (not list_excluded and - len(excluded_configs) == len(spec['configurations'])): - # If we're not listing excluded files, then they won't appear in the - # project, so don't try to configure them to be excluded. - pass - else: - for config_name, config in excluded_configs: - p.AddFileConfig(file_name, _ConfigFullName(config_name, config), - {'ExcludedFromBuild': 'true'}) + # Gather a list of precompiled header related sources. + precompiled_related = [] + for _, config in spec["configurations"].items(): + for k in precomp_keys: + f = config.get(k) + if f: + precompiled_related.append(_FixPath(f)) + return precompiled_related + + +def _ExcludeFilesFromBeingBuilt(p, spec, excluded_sources, excluded_idl, list_excluded): + exclusions = _GetExcludedFilesFromBuild(spec, excluded_sources, excluded_idl) + for file_name, excluded_configs in exclusions.items(): + if not list_excluded and len(excluded_configs) == len(spec["configurations"]): + # If we're not listing excluded files, then they won't appear in the + # project, so don't try to configure them to be excluded. + pass + else: + for config_name, config in excluded_configs: + p.AddFileConfig( + file_name, + _ConfigFullName(config_name, config), + {"ExcludedFromBuild": "true"}, + ) def _GetExcludedFilesFromBuild(spec, excluded_sources, excluded_idl): - exclusions = {} - # Exclude excluded sources from being built. - for f in excluded_sources: - excluded_configs = [] - for config_name, config in spec['configurations'].items(): - precomped = [_FixPath(config.get(i, '')) for i in precomp_keys] - # Don't do this for ones that are precompiled header related. - if f not in precomped: - excluded_configs.append((config_name, config)) - exclusions[f] = excluded_configs - # If any non-native rules use 'idl' as an extension exclude idl files. - # Exclude them now. - for f in excluded_idl: - excluded_configs = [] - for config_name, config in spec['configurations'].items(): - excluded_configs.append((config_name, config)) - exclusions[f] = excluded_configs - return exclusions + exclusions = {} + # Exclude excluded sources from being built. + for f in excluded_sources: + excluded_configs = [] + for config_name, config in spec["configurations"].items(): + precomped = [_FixPath(config.get(i, "")) for i in precomp_keys] + # Don't do this for ones that are precompiled header related. + if f not in precomped: + excluded_configs.append((config_name, config)) + exclusions[f] = excluded_configs + # If any non-native rules use 'idl' as an extension exclude idl files. + # Exclude them now. + for f in excluded_idl: + excluded_configs = [] + for config_name, config in spec["configurations"].items(): + excluded_configs.append((config_name, config)) + exclusions[f] = excluded_configs + return exclusions def _AddToolFilesToMSVS(p, spec): - # Add in tool files (rules). - tool_files = OrderedSet() - for _, config in spec['configurations'].items(): - for f in config.get('msvs_tool_files', []): - tool_files.add(f) - for f in tool_files: - p.AddToolFile(f) + # Add in tool files (rules). + tool_files = OrderedSet() + for _, config in spec["configurations"].items(): + for f in config.get("msvs_tool_files", []): + tool_files.add(f) + for f in tool_files: + p.AddToolFile(f) def _HandlePreCompiledHeaders(p, sources, spec): - # Pre-compiled header source stubs need a different compiler flag - # (generate precompiled header) and any source file not of the same - # kind (i.e. C vs. C++) as the precompiled header source stub needs - # to have use of precompiled headers disabled. - extensions_excluded_from_precompile = [] - for config_name, config in spec['configurations'].items(): - source = config.get('msvs_precompiled_source') - if source: - source = _FixPath(source) - # UsePrecompiledHeader=1 for if using precompiled headers. - tool = MSVSProject.Tool('VCCLCompilerTool', - {'UsePrecompiledHeader': '1'}) - p.AddFileConfig(source, _ConfigFullName(config_name, config), - {}, tools=[tool]) - basename, extension = os.path.splitext(source) - if extension == '.c': - extensions_excluded_from_precompile = ['.cc', '.cpp', '.cxx'] - else: - extensions_excluded_from_precompile = ['.c'] - def DisableForSourceTree(source_tree): - for source in source_tree: - if isinstance(source, MSVSProject.Filter): - DisableForSourceTree(source.contents) - else: - basename, extension = os.path.splitext(source) - if extension in extensions_excluded_from_precompile: - for config_name, config in spec['configurations'].items(): - tool = MSVSProject.Tool('VCCLCompilerTool', - {'UsePrecompiledHeader': '0', - 'ForcedIncludeFiles': '$(NOINHERIT)'}) - p.AddFileConfig(_FixPath(source), + # Pre-compiled header source stubs need a different compiler flag + # (generate precompiled header) and any source file not of the same + # kind (i.e. C vs. C++) as the precompiled header source stub needs + # to have use of precompiled headers disabled. + extensions_excluded_from_precompile = [] + for config_name, config in spec["configurations"].items(): + source = config.get("msvs_precompiled_source") + if source: + source = _FixPath(source) + # UsePrecompiledHeader=1 for if using precompiled headers. + tool = MSVSProject.Tool("VCCLCompilerTool", {"UsePrecompiledHeader": "1"}) + p.AddFileConfig( + source, _ConfigFullName(config_name, config), {}, tools=[tool] + ) + basename, extension = os.path.splitext(source) + if extension == ".c": + extensions_excluded_from_precompile = [".cc", ".cpp", ".cxx"] + else: + extensions_excluded_from_precompile = [".c"] + + def DisableForSourceTree(source_tree): + for source in source_tree: + if isinstance(source, MSVSProject.Filter): + DisableForSourceTree(source.contents) + else: + basename, extension = os.path.splitext(source) + if extension in extensions_excluded_from_precompile: + for config_name, config in spec["configurations"].items(): + tool = MSVSProject.Tool( + "VCCLCompilerTool", + { + "UsePrecompiledHeader": "0", + "ForcedIncludeFiles": "$(NOINHERIT)", + }, + ) + p.AddFileConfig( + _FixPath(source), _ConfigFullName(config_name, config), - {}, tools=[tool]) - # Do nothing if there was no precompiled source. - if extensions_excluded_from_precompile: - DisableForSourceTree(sources) + {}, + tools=[tool], + ) + + # Do nothing if there was no precompiled source. + if extensions_excluded_from_precompile: + DisableForSourceTree(sources) def _AddActions(actions_to_add, spec, relative_path_of_gyp_file): - # Add actions. - actions = spec.get('actions', []) - # Don't setup_env every time. When all the actions are run together in one - # batch file in VS, the PATH will grow too long. - # Membership in this set means that the cygwin environment has been set up, - # and does not need to be set up again. - have_setup_env = set() - for a in actions: - # Attach actions to the gyp file if nothing else is there. - inputs = a.get('inputs') or [relative_path_of_gyp_file] - attached_to = inputs[0] - need_setup_env = attached_to not in have_setup_env - cmd = _BuildCommandLineForRule(spec, a, has_input_path=False, - do_setup_env=need_setup_env) - have_setup_env.add(attached_to) - # Add the action. - _AddActionStep(actions_to_add, - inputs=inputs, - outputs=a.get('outputs', []), - description=a.get('message', a['action_name']), - command=cmd) + # Add actions. + actions = spec.get("actions", []) + # Don't setup_env every time. When all the actions are run together in one + # batch file in VS, the PATH will grow too long. + # Membership in this set means that the cygwin environment has been set up, + # and does not need to be set up again. + have_setup_env = set() + for a in actions: + # Attach actions to the gyp file if nothing else is there. + inputs = a.get("inputs") or [relative_path_of_gyp_file] + attached_to = inputs[0] + need_setup_env = attached_to not in have_setup_env + cmd = _BuildCommandLineForRule( + spec, a, has_input_path=False, do_setup_env=need_setup_env + ) + have_setup_env.add(attached_to) + # Add the action. + _AddActionStep( + actions_to_add, + inputs=inputs, + outputs=a.get("outputs", []), + description=a.get("message", a["action_name"]), + command=cmd, + ) def _WriteMSVSUserFile(project_path, version, spec): - # Add run_as and test targets. - if 'run_as' in spec: - run_as = spec['run_as'] - action = run_as.get('action', []) - environment = run_as.get('environment', []) - working_directory = run_as.get('working_directory', '.') - elif int(spec.get('test', 0)): - action = ['$(TargetPath)', '--gtest_print_time'] - environment = [] - working_directory = '.' - else: - return # Nothing to add - # Write out the user file. - user_file = _CreateMSVSUserFile(project_path, version, spec) - for config_name, c_data in spec['configurations'].items(): - user_file.AddDebugSettings(_ConfigFullName(config_name, c_data), - action, environment, working_directory) - user_file.WriteIfChanged() + # Add run_as and test targets. + if "run_as" in spec: + run_as = spec["run_as"] + action = run_as.get("action", []) + environment = run_as.get("environment", []) + working_directory = run_as.get("working_directory", ".") + elif int(spec.get("test", 0)): + action = ["$(TargetPath)", "--gtest_print_time"] + environment = [] + working_directory = "." + else: + return # Nothing to add + # Write out the user file. + user_file = _CreateMSVSUserFile(project_path, version, spec) + for config_name, c_data in spec["configurations"].items(): + user_file.AddDebugSettings( + _ConfigFullName(config_name, c_data), action, environment, working_directory + ) + user_file.WriteIfChanged() def _AddCopies(actions_to_add, spec): - copies = _GetCopies(spec) - for inputs, outputs, cmd, description in copies: - _AddActionStep(actions_to_add, inputs=inputs, outputs=outputs, - description=description, command=cmd) + copies = _GetCopies(spec) + for inputs, outputs, cmd, description in copies: + _AddActionStep( + actions_to_add, + inputs=inputs, + outputs=outputs, + description=description, + command=cmd, + ) def _GetCopies(spec): - copies = [] - # Add copies. - for cpy in spec.get('copies', []): - for src in cpy.get('files', []): - dst = os.path.join(cpy['destination'], os.path.basename(src)) - # _AddCustomBuildToolForMSVS() will call _FixPath() on the inputs and - # outputs, so do the same for our generated command line. - if src.endswith('/'): - src_bare = src[:-1] - base_dir = posixpath.split(src_bare)[0] - outer_dir = posixpath.split(src_bare)[1] - fixed_dst = _FixPath(dst) - full_dst = '"%s\\%s\\"' % (fixed_dst, outer_dir) - cmd = 'mkdir %s 2>nul & cd "%s" && xcopy /e /f /y "%s" %s' % ( - full_dst, _FixPath(base_dir), outer_dir, full_dst) - copies.append(([src], ['dummy_copies', dst], cmd, - 'Copying %s to %s' % (src, fixed_dst))) - else: - fix_dst = _FixPath(cpy['destination']) - cmd = 'mkdir "%s" 2>nul & set ERRORLEVEL=0 & copy /Y "%s" "%s"' % ( - fix_dst, _FixPath(src), _FixPath(dst)) - copies.append(([src], [dst], cmd, 'Copying %s to %s' % (src, fix_dst))) - return copies + copies = [] + # Add copies. + for cpy in spec.get("copies", []): + for src in cpy.get("files", []): + dst = os.path.join(cpy["destination"], os.path.basename(src)) + # _AddCustomBuildToolForMSVS() will call _FixPath() on the inputs and + # outputs, so do the same for our generated command line. + if src.endswith("/"): + src_bare = src[:-1] + base_dir = posixpath.split(src_bare)[0] + outer_dir = posixpath.split(src_bare)[1] + fixed_dst = _FixPath(dst) + full_dst = '"%s\\%s\\"' % (fixed_dst, outer_dir) + cmd = 'mkdir %s 2>nul & cd "%s" && xcopy /e /f /y "%s" %s' % ( + full_dst, + _FixPath(base_dir), + outer_dir, + full_dst, + ) + copies.append( + ( + [src], + ["dummy_copies", dst], + cmd, + "Copying %s to %s" % (src, fixed_dst), + ) + ) + else: + fix_dst = _FixPath(cpy["destination"]) + cmd = 'mkdir "%s" 2>nul & set ERRORLEVEL=0 & copy /Y "%s" "%s"' % ( + fix_dst, + _FixPath(src), + _FixPath(dst), + ) + copies.append(([src], [dst], cmd, "Copying %s to %s" % (src, fix_dst))) + return copies def _GetPathDict(root, path): - # |path| will eventually be empty (in the recursive calls) if it was initially - # relative; otherwise it will eventually end up as '\', 'D:\', etc. - if not path or path.endswith(os.sep): - return root - parent, folder = os.path.split(path) - parent_dict = _GetPathDict(root, parent) - if folder not in parent_dict: - parent_dict[folder] = dict() - return parent_dict[folder] + # |path| will eventually be empty (in the recursive calls) if it was initially + # relative; otherwise it will eventually end up as '\', 'D:\', etc. + if not path or path.endswith(os.sep): + return root + parent, folder = os.path.split(path) + parent_dict = _GetPathDict(root, parent) + if folder not in parent_dict: + parent_dict[folder] = dict() + return parent_dict[folder] def _DictsToFolders(base_path, bucket, flat): - # Convert to folders recursively. - children = [] - for folder, contents in bucket.items(): - if type(contents) == dict: - folder_children = _DictsToFolders(os.path.join(base_path, folder), - contents, flat) - if flat: - children += folder_children - else: - folder_children = MSVSNew.MSVSFolder(os.path.join(base_path, folder), - name='(' + folder + ')', - entries=folder_children) - children.append(folder_children) - else: - children.append(contents) - return children + # Convert to folders recursively. + children = [] + for folder, contents in bucket.items(): + if type(contents) == dict: + folder_children = _DictsToFolders( + os.path.join(base_path, folder), contents, flat + ) + if flat: + children += folder_children + else: + folder_children = MSVSNew.MSVSFolder( + os.path.join(base_path, folder), + name="(" + folder + ")", + entries=folder_children, + ) + children.append(folder_children) + else: + children.append(contents) + return children def _CollapseSingles(parent, node): - # Recursively explorer the tree of dicts looking for projects which are - # the sole item in a folder which has the same name as the project. Bring - # such projects up one level. - if (type(node) == dict and - len(node) == 1 and - list(node)[0] == parent + '.vcproj'): - return node[list(node)[0]] - if type(node) != dict: + # Recursively explorer the tree of dicts looking for projects which are + # the sole item in a folder which has the same name as the project. Bring + # such projects up one level. + if type(node) == dict and len(node) == 1 and next(iter(node)) == parent + ".vcproj": + return node[next(iter(node))] + if type(node) != dict: + return node + for child in node: + node[child] = _CollapseSingles(child, node[child]) return node - for child in node: - node[child] = _CollapseSingles(child, node[child]) - return node def _GatherSolutionFolders(sln_projects, project_objects, flat): - root = {} - # Convert into a tree of dicts on path. - for p in sln_projects: - gyp_file, target = gyp.common.ParseQualifiedTarget(p)[0:2] - if p.endswith("#host"): - target += "_host" - gyp_dir = os.path.dirname(gyp_file) - path_dict = _GetPathDict(root, gyp_dir) - path_dict[target + '.vcproj'] = project_objects[p] - # Walk down from the top until we hit a folder that has more than one entry. - # In practice, this strips the top-level "src/" dir from the hierarchy in - # the solution. - while len(root) == 1 and type(root[list(root)[0]]) == dict: - root = root[list(root)[0]] - # Collapse singles. - root = _CollapseSingles('', root) - # Merge buckets until everything is a root entry. - return _DictsToFolders('', root, flat) + root = {} + # Convert into a tree of dicts on path. + for p in sln_projects: + gyp_file, target = gyp.common.ParseQualifiedTarget(p)[0:2] + if p.endswith("#host"): + target += "_host" + gyp_dir = os.path.dirname(gyp_file) + path_dict = _GetPathDict(root, gyp_dir) + path_dict[target + ".vcproj"] = project_objects[p] + # Walk down from the top until we hit a folder that has more than one entry. + # In practice, this strips the top-level "src/" dir from the hierarchy in + # the solution. + while len(root) == 1 and type(root[next(iter(root))]) == dict: + root = root[next(iter(root))] + # Collapse singles. + root = _CollapseSingles("", root) + # Merge buckets until everything is a root entry. + return _DictsToFolders("", root, flat) def _GetPathOfProject(qualified_target, spec, options, msvs_version): - default_config = _GetDefaultConfiguration(spec) - proj_filename = default_config.get('msvs_existing_vcproj') - if not proj_filename: - proj_filename = spec['target_name'] - if spec['toolset'] == 'host': - proj_filename += "_host" - proj_filename = (proj_filename + options.suffix + - msvs_version.ProjectExtension()) - - build_file = gyp.common.BuildFile(qualified_target) - proj_path = os.path.join(os.path.dirname(build_file), proj_filename) - fix_prefix = None - if options.generator_output: - project_dir_path = os.path.dirname(os.path.abspath(proj_path)) - proj_path = os.path.join(options.generator_output, proj_path) - fix_prefix = gyp.common.RelativePath(project_dir_path, - os.path.dirname(proj_path)) - return proj_path, fix_prefix + default_config = _GetDefaultConfiguration(spec) + proj_filename = default_config.get("msvs_existing_vcproj") + if not proj_filename: + proj_filename = spec["target_name"] + if spec["toolset"] == "host": + proj_filename += "_host" + proj_filename = proj_filename + options.suffix + msvs_version.ProjectExtension() + + build_file = gyp.common.BuildFile(qualified_target) + proj_path = os.path.join(os.path.dirname(build_file), proj_filename) + fix_prefix = None + if options.generator_output: + project_dir_path = os.path.dirname(os.path.abspath(proj_path)) + proj_path = os.path.join(options.generator_output, proj_path) + fix_prefix = gyp.common.RelativePath( + project_dir_path, os.path.dirname(proj_path) + ) + return proj_path, fix_prefix def _GetPlatformOverridesOfProject(spec): - # Prepare a dict indicating which project configurations are used for which - # solution configurations for this target. - config_platform_overrides = {} - for config_name, c in spec['configurations'].items(): - config_fullname = _ConfigFullName(config_name, c, spec) - platform = c.get('msvs_target_platform', _ConfigPlatform(c, spec)) - fixed_config_fullname = '%s|%s' % ( - _ConfigBaseName(config_name, _ConfigPlatform(c, spec)), platform) - if spec['toolset'] == 'host' and generator_supports_multiple_toolsets: - fixed_config_fullname = '%s|x64' % (config_name,) - config_platform_overrides[config_fullname] = fixed_config_fullname - return config_platform_overrides + # Prepare a dict indicating which project configurations are used for which + # solution configurations for this target. + config_platform_overrides = {} + for config_name, c in spec["configurations"].items(): + config_fullname = _ConfigFullName(config_name, c) + platform = c.get("msvs_target_platform", _ConfigPlatform(c)) + fixed_config_fullname = "%s|%s" % ( + _ConfigBaseName(config_name, _ConfigPlatform(c)), + platform, + ) + if spec["toolset"] == "host" and generator_supports_multiple_toolsets: + fixed_config_fullname = "%s|x64" % (config_name,) + config_platform_overrides[config_fullname] = fixed_config_fullname + return config_platform_overrides def _CreateProjectObjects(target_list, target_dicts, options, msvs_version): - """Create a MSVSProject object for the targets found in target list. + """Create a MSVSProject object for the targets found in target list. Arguments: target_list: the list of targets to generate project objects for. @@ -1865,45 +1925,48 @@ def _CreateProjectObjects(target_list, target_dicts, options, msvs_version): Returns: A set of created projects, keyed by target. """ - global fixpath_prefix - # Generate each project. - projects = {} - for qualified_target in target_list: - spec = target_dicts[qualified_target] - proj_path, fixpath_prefix = _GetPathOfProject(qualified_target, spec, - options, msvs_version) - guid = _GetGuidOfProject(proj_path, spec) - overrides = _GetPlatformOverridesOfProject(spec) - build_file = gyp.common.BuildFile(qualified_target) - # Create object for this project. - target_name = spec['target_name'] - if spec['toolset'] == 'host': - target_name += '_host' - obj = MSVSNew.MSVSProject( - proj_path, - name=target_name, - guid=guid, - spec=spec, - build_file=build_file, - config_platform_overrides=overrides, - fixpath_prefix=fixpath_prefix) - # Set project toolset if any (MS build only) - if msvs_version.UsesVcxproj(): - obj.set_msbuild_toolset( - _GetMsbuildToolsetOfProject(proj_path, spec, msvs_version)) - projects[qualified_target] = obj - # Set all the dependencies, but not if we are using an external builder like - # ninja - for project in projects.values(): - if not project.spec.get('msvs_external_builder'): - deps = project.spec.get('dependencies', []) - deps = [projects[d] for d in deps] - project.set_dependencies(deps) - return projects + global fixpath_prefix + # Generate each project. + projects = {} + for qualified_target in target_list: + spec = target_dicts[qualified_target] + proj_path, fixpath_prefix = _GetPathOfProject( + qualified_target, spec, options, msvs_version + ) + guid = _GetGuidOfProject(proj_path, spec) + overrides = _GetPlatformOverridesOfProject(spec) + build_file = gyp.common.BuildFile(qualified_target) + # Create object for this project. + target_name = spec["target_name"] + if spec["toolset"] == "host": + target_name += "_host" + obj = MSVSNew.MSVSProject( + proj_path, + name=target_name, + guid=guid, + spec=spec, + build_file=build_file, + config_platform_overrides=overrides, + fixpath_prefix=fixpath_prefix, + ) + # Set project toolset if any (MS build only) + if msvs_version.UsesVcxproj(): + obj.set_msbuild_toolset( + _GetMsbuildToolsetOfProject(proj_path, spec, msvs_version) + ) + projects[qualified_target] = obj + # Set all the dependencies, but not if we are using an external builder like + # ninja + for project in projects.values(): + if not project.spec.get("msvs_external_builder"): + deps = project.spec.get("dependencies", []) + deps = [projects[d] for d in deps] + project.set_dependencies(deps) + return projects def _InitNinjaFlavor(params, target_list, target_dicts): - """Initialize targets for the ninja flavor. + """Initialize targets for the ninja flavor. This sets up the necessary variables in the targets to generate msvs projects that use ninja as an external builder. The variables in the spec are only set @@ -1914,106 +1977,115 @@ def _InitNinjaFlavor(params, target_list, target_dicts): target_list: List of target pairs: 'base/base.gyp:base'. target_dicts: Dict of target properties keyed on target pair. """ - for qualified_target in target_list: - spec = target_dicts[qualified_target] - if spec.get('msvs_external_builder'): - # The spec explicitly defined an external builder, so don't change it. - continue - - path_to_ninja = spec.get('msvs_path_to_ninja', 'ninja.exe') - - spec['msvs_external_builder'] = 'ninja' - if not spec.get('msvs_external_builder_out_dir'): - gyp_file, _, _ = gyp.common.ParseQualifiedTarget(qualified_target) - gyp_dir = os.path.dirname(gyp_file) - configuration = '$(Configuration)' - if params.get('target_arch') == 'x64': - configuration += '_x64' - if params.get('target_arch') == 'arm64': - configuration += '_arm64' - spec['msvs_external_builder_out_dir'] = os.path.join( - gyp.common.RelativePath(params['options'].toplevel_dir, gyp_dir), - ninja_generator.ComputeOutputDir(params), - configuration) - if not spec.get('msvs_external_builder_build_cmd'): - spec['msvs_external_builder_build_cmd'] = [ - path_to_ninja, - '-C', - '$(OutDir)', - '$(ProjectName)', - ] - if not spec.get('msvs_external_builder_clean_cmd'): - spec['msvs_external_builder_clean_cmd'] = [ - path_to_ninja, - '-C', - '$(OutDir)', - '-tclean', - '$(ProjectName)', - ] + for qualified_target in target_list: + spec = target_dicts[qualified_target] + if spec.get("msvs_external_builder"): + # The spec explicitly defined an external builder, so don't change it. + continue + + path_to_ninja = spec.get("msvs_path_to_ninja", "ninja.exe") + + spec["msvs_external_builder"] = "ninja" + if not spec.get("msvs_external_builder_out_dir"): + gyp_file, _, _ = gyp.common.ParseQualifiedTarget(qualified_target) + gyp_dir = os.path.dirname(gyp_file) + configuration = "$(Configuration)" + if params.get("target_arch") == "x64": + configuration += "_x64" + if params.get("target_arch") == "arm64": + configuration += "_arm64" + spec["msvs_external_builder_out_dir"] = os.path.join( + gyp.common.RelativePath(params["options"].toplevel_dir, gyp_dir), + ninja_generator.ComputeOutputDir(params), + configuration, + ) + if not spec.get("msvs_external_builder_build_cmd"): + spec["msvs_external_builder_build_cmd"] = [ + path_to_ninja, + "-C", + "$(OutDir)", + "$(ProjectName)", + ] + if not spec.get("msvs_external_builder_clean_cmd"): + spec["msvs_external_builder_clean_cmd"] = [ + path_to_ninja, + "-C", + "$(OutDir)", + "-tclean", + "$(ProjectName)", + ] def CalculateVariables(default_variables, params): - """Generated variables that require params to be known.""" - - generator_flags = params.get('generator_flags', {}) + """Generated variables that require params to be known.""" + + generator_flags = params.get("generator_flags", {}) + + # Select project file format version (if unset, default to auto detecting). + msvs_version = MSVSVersion.SelectVisualStudioVersion( + generator_flags.get("msvs_version", "auto") + ) + # Stash msvs_version for later (so we don't have to probe the system twice). + params["msvs_version"] = msvs_version + + # Set a variable so conditions can be based on msvs_version. + default_variables["MSVS_VERSION"] = msvs_version.ShortName() + + # To determine processor word size on Windows, in addition to checking + # PROCESSOR_ARCHITECTURE (which reflects the word size of the current + # process), it is also necessary to check PROCESSOR_ARCITEW6432 (which + # contains the actual word size of the system when running thru WOW64). + if ( + os.environ.get("PROCESSOR_ARCHITECTURE", "").find("64") >= 0 + or os.environ.get("PROCESSOR_ARCHITEW6432", "").find("64") >= 0 + ): + default_variables["MSVS_OS_BITS"] = 64 + else: + default_variables["MSVS_OS_BITS"] = 32 - # Select project file format version (if unset, default to auto detecting). - msvs_version = MSVSVersion.SelectVisualStudioVersion( - generator_flags.get('msvs_version', 'auto')) - # Stash msvs_version for later (so we don't have to probe the system twice). - params['msvs_version'] = msvs_version + if gyp.common.GetFlavor(params) == "ninja": + default_variables["SHARED_INTERMEDIATE_DIR"] = "$(OutDir)gen" - # Set a variable so conditions can be based on msvs_version. - default_variables['MSVS_VERSION'] = msvs_version.ShortName() - # To determine processor word size on Windows, in addition to checking - # PROCESSOR_ARCHITECTURE (which reflects the word size of the current - # process), it is also necessary to check PROCESSOR_ARCITEW6432 (which - # contains the actual word size of the system when running thru WOW64). - if (os.environ.get('PROCESSOR_ARCHITECTURE', '').find('64') >= 0 or - os.environ.get('PROCESSOR_ARCHITEW6432', '').find('64') >= 0): - default_variables['MSVS_OS_BITS'] = 64 - else: - default_variables['MSVS_OS_BITS'] = 32 +def PerformBuild(data, configurations, params): + options = params["options"] + msvs_version = params["msvs_version"] + devenv = os.path.join(msvs_version.path, "Common7", "IDE", "devenv.com") - if gyp.common.GetFlavor(params) == 'ninja': - default_variables['SHARED_INTERMEDIATE_DIR'] = '$(OutDir)gen' + for build_file, build_file_dict in data.items(): + (build_file_root, build_file_ext) = os.path.splitext(build_file) + if build_file_ext != ".gyp": + continue + sln_path = build_file_root + options.suffix + ".sln" + if options.generator_output: + sln_path = os.path.join(options.generator_output, sln_path) + for config in configurations: + arguments = [devenv, sln_path, "/Build", config] + print("Building [%s]: %s" % (config, arguments)) + subprocess.check_call(arguments) -def PerformBuild(data, configurations, params): - options = params['options'] - msvs_version = params['msvs_version'] - devenv = os.path.join(msvs_version.path, 'Common7', 'IDE', 'devenv.com') - - for build_file, build_file_dict in data.items(): - (build_file_root, build_file_ext) = os.path.splitext(build_file) - if build_file_ext != '.gyp': - continue - sln_path = build_file_root + options.suffix + '.sln' - if options.generator_output: - sln_path = os.path.join(options.generator_output, sln_path) - for config in configurations: - arguments = [devenv, sln_path, '/Build', config] - print('Building [%s]: %s' % (config, arguments)) - rtn = subprocess.check_call(arguments) +def CalculateGeneratorInputInfo(params): + if params.get("flavor") == "ninja": + toplevel = params["options"].toplevel_dir + qualified_out_dir = os.path.normpath( + os.path.join( + toplevel, + ninja_generator.ComputeOutputDir(params), + "gypfiles-msvs-ninja", + ) + ) + global generator_filelist_paths + generator_filelist_paths = { + "toplevel": toplevel, + "qualified_out_dir": qualified_out_dir, + } -def CalculateGeneratorInputInfo(params): - if params.get('flavor') == 'ninja': - toplevel = params['options'].toplevel_dir - qualified_out_dir = os.path.normpath(os.path.join( - toplevel, ninja_generator.ComputeOutputDir(params), - 'gypfiles-msvs-ninja')) - - global generator_filelist_paths - generator_filelist_paths = { - 'toplevel': toplevel, - 'qualified_out_dir': qualified_out_dir, - } def GenerateOutput(target_list, target_dicts, data, params): - """Generate .sln and .vcproj files. + """Generate .sln and .vcproj files. This is the entry point for this generator. Arguments: @@ -2021,88 +2093,93 @@ def GenerateOutput(target_list, target_dicts, data, params): target_dicts: Dict of target properties keyed on target pair. data: Dictionary containing per .gyp data. """ - global fixpath_prefix - - options = params['options'] - - # Get the project file format version back out of where we stashed it in - # GeneratorCalculatedVariables. - msvs_version = params['msvs_version'] - - generator_flags = params.get('generator_flags', {}) - - # Optionally shard targets marked with 'msvs_shard': SHARD_COUNT. - (target_list, target_dicts) = MSVSUtil.ShardTargets(target_list, target_dicts) - - # Optionally use the large PDB workaround for targets marked with - # 'msvs_large_pdb': 1. - (target_list, target_dicts) = MSVSUtil.InsertLargePdbShims( - target_list, target_dicts, generator_default_variables) - - # Optionally configure each spec to use ninja as the external builder. - if params.get('flavor') == 'ninja': - _InitNinjaFlavor(params, target_list, target_dicts) - - # Prepare the set of configurations. - configs = set() - for qualified_target in target_list: - spec = target_dicts[qualified_target] - for config_name, config in spec['configurations'].items(): - config_name = _ConfigFullName(config_name, config, spec) - configs.add(config_name) - if config_name == 'Release|arm64': - configs.add("Release|x64") - configs = list(configs) - - # Figure out all the projects that will be generated and their guids - project_objects = _CreateProjectObjects(target_list, target_dicts, options, - msvs_version) - - # Generate each project. - missing_sources = [] - for project in project_objects.values(): - fixpath_prefix = project.fixpath_prefix - missing_sources.extend(_GenerateProject(project, options, msvs_version, - generator_flags, spec)) - fixpath_prefix = None - - for build_file in data: - # Validate build_file extension - target_only_configs = configs - if generator_supports_multiple_toolsets: - target_only_configs = [i for i in configs if i.endswith('arm64')] - if not build_file.endswith('.gyp'): - continue - sln_path = os.path.splitext(build_file)[0] + options.suffix + '.sln' - if options.generator_output: - sln_path = os.path.join(options.generator_output, sln_path) - # Get projects in the solution, and their dependents. - sln_projects = gyp.common.BuildFileTargets(target_list, build_file) - sln_projects += gyp.common.DeepDependencyTargets(target_dicts, sln_projects) - # Create folder hierarchy. - root_entries = _GatherSolutionFolders( - sln_projects, project_objects, flat=msvs_version.FlatSolution()) - # Create solution. - sln = MSVSNew.MSVSSolution(sln_path, - entries=root_entries, - variants=target_only_configs, - websiteProperties=False, - version=msvs_version) - sln.Write() - - if missing_sources: - error_message = "Missing input files:\n" + \ - '\n'.join(set(missing_sources)) - if generator_flags.get('msvs_error_on_missing_sources', False): - raise GypError(error_message) - else: - print("Warning: " + error_message, file=sys.stdout) + global fixpath_prefix + + options = params["options"] + + # Get the project file format version back out of where we stashed it in + # GeneratorCalculatedVariables. + msvs_version = params["msvs_version"] + + generator_flags = params.get("generator_flags", {}) + + # Optionally shard targets marked with 'msvs_shard': SHARD_COUNT. + (target_list, target_dicts) = MSVSUtil.ShardTargets(target_list, target_dicts) + + # Optionally use the large PDB workaround for targets marked with + # 'msvs_large_pdb': 1. + (target_list, target_dicts) = MSVSUtil.InsertLargePdbShims( + target_list, target_dicts, generator_default_variables + ) + + # Optionally configure each spec to use ninja as the external builder. + if params.get("flavor") == "ninja": + _InitNinjaFlavor(params, target_list, target_dicts) + + # Prepare the set of configurations. + configs = set() + for qualified_target in target_list: + spec = target_dicts[qualified_target] + for config_name, config in spec["configurations"].items(): + config_name = _ConfigFullName(config_name, config) + configs.add(config_name) + if config_name == "Release|arm64": + configs.add("Release|x64") + configs = list(configs) + + # Figure out all the projects that will be generated and their guids + project_objects = _CreateProjectObjects( + target_list, target_dicts, options, msvs_version + ) + + # Generate each project. + missing_sources = [] + for project in project_objects.values(): + fixpath_prefix = project.fixpath_prefix + missing_sources.extend( + _GenerateProject(project, options, msvs_version, generator_flags, spec) + ) + fixpath_prefix = None + + for build_file in data: + # Validate build_file extension + target_only_configs = configs + if generator_supports_multiple_toolsets: + target_only_configs = [i for i in configs if i.endswith("arm64")] + if not build_file.endswith(".gyp"): + continue + sln_path = os.path.splitext(build_file)[0] + options.suffix + ".sln" + if options.generator_output: + sln_path = os.path.join(options.generator_output, sln_path) + # Get projects in the solution, and their dependents. + sln_projects = gyp.common.BuildFileTargets(target_list, build_file) + sln_projects += gyp.common.DeepDependencyTargets(target_dicts, sln_projects) + # Create folder hierarchy. + root_entries = _GatherSolutionFolders( + sln_projects, project_objects, flat=msvs_version.FlatSolution() + ) + # Create solution. + sln = MSVSNew.MSVSSolution( + sln_path, + entries=root_entries, + variants=target_only_configs, + websiteProperties=False, + version=msvs_version, + ) + sln.Write() + + if missing_sources: + error_message = "Missing input files:\n" + "\n".join(set(missing_sources)) + if generator_flags.get("msvs_error_on_missing_sources", False): + raise GypError(error_message) + else: + print("Warning: " + error_message, file=sys.stdout) -def _GenerateMSBuildFiltersFile(filters_path, source_files, - rule_dependencies, extension_to_rule_name, - platforms): - """Generate the filters file. +def _GenerateMSBuildFiltersFile( + filters_path, source_files, rule_dependencies, extension_to_rule_name, platforms +): + """Generate the filters file. This file is used by Visual Studio to organize the presentation of source files into folders. @@ -2112,29 +2189,43 @@ def _GenerateMSBuildFiltersFile(filters_path, source_files, source_files: The hierarchical structure of all the sources. extension_to_rule_name: A dictionary mapping file extensions to rules. """ - filter_group = [] - source_group = [] - _AppendFiltersForMSBuild('', source_files, rule_dependencies, - extension_to_rule_name, platforms, - filter_group, source_group) - if filter_group: - content = ['Project', - {'ToolsVersion': '4.0', - 'xmlns': 'http://schemas.microsoft.com/developer/msbuild/2003' - }, - ['ItemGroup'] + filter_group, - ['ItemGroup'] + source_group - ] - easy_xml.WriteXmlIfChanged(content, filters_path, pretty=True, win32=True) - elif os.path.exists(filters_path): - # We don't need this filter anymore. Delete the old filter file. - os.unlink(filters_path) - - -def _AppendFiltersForMSBuild(parent_filter_name, sources, rule_dependencies, - extension_to_rule_name, platforms, - filter_group, source_group): - """Creates the list of filters and sources to be added in the filter file. + filter_group = [] + source_group = [] + _AppendFiltersForMSBuild( + "", + source_files, + rule_dependencies, + extension_to_rule_name, + platforms, + filter_group, + source_group, + ) + if filter_group: + content = [ + "Project", + { + "ToolsVersion": "4.0", + "xmlns": "http://schemas.microsoft.com/developer/msbuild/2003", + }, + ["ItemGroup"] + filter_group, + ["ItemGroup"] + source_group, + ] + easy_xml.WriteXmlIfChanged(content, filters_path, pretty=True, win32=True) + elif os.path.exists(filters_path): + # We don't need this filter anymore. Delete the old filter file. + os.unlink(filters_path) + + +def _AppendFiltersForMSBuild( + parent_filter_name, + sources, + rule_dependencies, + extension_to_rule_name, + platforms, + filter_group, + source_group, +): + """Creates the list of filters and sources to be added in the filter file. Args: parent_filter_name: The name of the filter under which the sources are @@ -2142,38 +2233,49 @@ def _AppendFiltersForMSBuild(parent_filter_name, sources, rule_dependencies, sources: The hierarchy of filters and sources to process. extension_to_rule_name: A dictionary mapping file extensions to rules. filter_group: The list to which filter entries will be appended. - source_group: The list to which source entries will be appeneded. + source_group: The list to which source entries will be appended. """ - for source in sources: - if isinstance(source, MSVSProject.Filter): - # We have a sub-filter. Create the name of that sub-filter. - if not parent_filter_name: - filter_name = source.name - else: - filter_name = '%s\\%s' % (parent_filter_name, source.name) - # Add the filter to the group. - filter_group.append( - ['Filter', {'Include': filter_name}, - ['UniqueIdentifier', MSVSNew.MakeGuid(source.name)]]) - # Recurse and add its dependents. - _AppendFiltersForMSBuild(filter_name, source.contents, - rule_dependencies, extension_to_rule_name, - platforms, filter_group, source_group) - else: - # It's a source. Create a source entry. - _, element = _MapFileToMsBuildSourceType(source, rule_dependencies, - extension_to_rule_name, - platforms) - source_entry = [element, {'Include': source}] - # Specify the filter it is part of, if any. - if parent_filter_name: - source_entry.append(['Filter', parent_filter_name]) - source_group.append(source_entry) + for source in sources: + if isinstance(source, MSVSProject.Filter): + # We have a sub-filter. Create the name of that sub-filter. + if not parent_filter_name: + filter_name = source.name + else: + filter_name = "%s\\%s" % (parent_filter_name, source.name) + # Add the filter to the group. + filter_group.append( + [ + "Filter", + {"Include": filter_name}, + ["UniqueIdentifier", MSVSNew.MakeGuid(source.name)], + ] + ) + # Recurse and add its dependents. + _AppendFiltersForMSBuild( + filter_name, + source.contents, + rule_dependencies, + extension_to_rule_name, + platforms, + filter_group, + source_group, + ) + else: + # It's a source. Create a source entry. + _, element = _MapFileToMsBuildSourceType( + source, rule_dependencies, extension_to_rule_name, platforms + ) + source_entry = [element, {"Include": source}] + # Specify the filter it is part of, if any. + if parent_filter_name: + source_entry.append(["Filter", parent_filter_name]) + source_group.append(source_entry) -def _MapFileToMsBuildSourceType(source, rule_dependencies, - extension_to_rule_name, platforms): - """Returns the group and element type of the source file. +def _MapFileToMsBuildSourceType( + source, rule_dependencies, extension_to_rule_name, platforms +): + """Returns the group and element type of the source file. Arguments: source: The source file name. @@ -2182,85 +2284,93 @@ def _MapFileToMsBuildSourceType(source, rule_dependencies, Returns: A pair of (group this file should be part of, the label of element) """ - _, ext = os.path.splitext(source) - ext = ext.lower() - if ext in extension_to_rule_name: - group = 'rule' - element = extension_to_rule_name[ext] - elif ext in ['.cc', '.cpp', '.c', '.cxx', '.mm']: - group = 'compile' - element = 'ClCompile' - elif ext in ['.h', '.hxx']: - group = 'include' - element = 'ClInclude' - elif ext == '.rc': - group = 'resource' - element = 'ResourceCompile' - elif ext in ['.s', '.asm']: - group = 'masm' - element = 'MASM' - for platform in platforms: - if platform.lower() in ['arm', 'arm64']: - element = 'MARMASM' - elif ext == '.idl': - group = 'midl' - element = 'Midl' - elif source in rule_dependencies: - group = 'rule_dependency' - element = 'CustomBuild' - else: - group = 'none' - element = 'None' - return (group, element) - - -def _GenerateRulesForMSBuild(output_dir, options, spec, - sources, excluded_sources, - props_files_of_rules, targets_files_of_rules, - actions_to_add, rule_dependencies, - extension_to_rule_name): - # MSBuild rules are implemented using three files: an XML file, a .targets - # file and a .props file. - # See http://blogs.msdn.com/b/vcblog/archive/2010/04/21/quick-help-on-vs2010-custom-build-rule.aspx - # for more details. - rules = spec.get('rules', []) - rules_native = [r for r in rules if not int(r.get('msvs_external_rule', 0))] - rules_external = [r for r in rules if int(r.get('msvs_external_rule', 0))] - - msbuild_rules = [] - for rule in rules_native: - # Skip a rule with no action and no inputs. - if 'action' not in rule and not rule.get('rule_sources', []): - continue - msbuild_rule = MSBuildRule(rule, spec) - msbuild_rules.append(msbuild_rule) - rule_dependencies.update(msbuild_rule.additional_dependencies.split(';')) - extension_to_rule_name[msbuild_rule.extension] = msbuild_rule.rule_name - if msbuild_rules: - base = spec['target_name'] + options.suffix - props_name = base + '.props' - targets_name = base + '.targets' - xml_name = base + '.xml' - - props_files_of_rules.add(props_name) - targets_files_of_rules.add(targets_name) - - props_path = os.path.join(output_dir, props_name) - targets_path = os.path.join(output_dir, targets_name) - xml_path = os.path.join(output_dir, xml_name) - - _GenerateMSBuildRulePropsFile(props_path, msbuild_rules) - _GenerateMSBuildRuleTargetsFile(targets_path, msbuild_rules) - _GenerateMSBuildRuleXmlFile(xml_path, msbuild_rules) - - if rules_external: - _GenerateExternalRules(rules_external, output_dir, spec, - sources, options, actions_to_add) - _AdjustSourcesForRules(rules, sources, excluded_sources, True) + _, ext = os.path.splitext(source) + ext = ext.lower() + if ext in extension_to_rule_name: + group = "rule" + element = extension_to_rule_name[ext] + elif ext in [".cc", ".cpp", ".c", ".cxx", ".mm"]: + group = "compile" + element = "ClCompile" + elif ext in [".h", ".hxx"]: + group = "include" + element = "ClInclude" + elif ext == ".rc": + group = "resource" + element = "ResourceCompile" + elif ext in [".s", ".asm"]: + group = "masm" + element = "MASM" + for platform in platforms: + if platform.lower() in ["arm", "arm64"]: + element = "MARMASM" + elif ext == ".idl": + group = "midl" + element = "Midl" + elif source in rule_dependencies: + group = "rule_dependency" + element = "CustomBuild" + else: + group = "none" + element = "None" + return (group, element) + + +def _GenerateRulesForMSBuild( + output_dir, + options, + spec, + sources, + excluded_sources, + props_files_of_rules, + targets_files_of_rules, + actions_to_add, + rule_dependencies, + extension_to_rule_name, +): + # MSBuild rules are implemented using three files: an XML file, a .targets + # file and a .props file. + # For more details see: + # https://devblogs.microsoft.com/cppblog/quick-help-on-vs2010-custom-build-rule/ + rules = spec.get("rules", []) + rules_native = [r for r in rules if not int(r.get("msvs_external_rule", 0))] + rules_external = [r for r in rules if int(r.get("msvs_external_rule", 0))] + + msbuild_rules = [] + for rule in rules_native: + # Skip a rule with no action and no inputs. + if "action" not in rule and not rule.get("rule_sources", []): + continue + msbuild_rule = MSBuildRule(rule, spec) + msbuild_rules.append(msbuild_rule) + rule_dependencies.update(msbuild_rule.additional_dependencies.split(";")) + extension_to_rule_name[msbuild_rule.extension] = msbuild_rule.rule_name + if msbuild_rules: + base = spec["target_name"] + options.suffix + props_name = base + ".props" + targets_name = base + ".targets" + xml_name = base + ".xml" + + props_files_of_rules.add(props_name) + targets_files_of_rules.add(targets_name) + + props_path = os.path.join(output_dir, props_name) + targets_path = os.path.join(output_dir, targets_name) + xml_path = os.path.join(output_dir, xml_name) + + _GenerateMSBuildRulePropsFile(props_path, msbuild_rules) + _GenerateMSBuildRuleTargetsFile(targets_path, msbuild_rules) + _GenerateMSBuildRuleXmlFile(xml_path, msbuild_rules) + + if rules_external: + _GenerateExternalRules( + rules_external, output_dir, spec, sources, options, actions_to_add + ) + _AdjustSourcesForRules(rules, sources, excluded_sources, True) class MSBuildRule(object): - """Used to store information used to generate an MSBuild rule. + """Used to store information used to generate an MSBuild rule. Attributes: rule_name: The rule name, sanitized to use in XML. @@ -2279,720 +2389,844 @@ class MSBuildRule(object): command: The command used to run the rule. """ - def __init__(self, rule, spec): - self.display_name = rule['rule_name'] - # Assure that the rule name is only characters and numbers - self.rule_name = re.sub(r'\W', '_', self.display_name) - # Create the various element names, following the example set by the - # Visual Studio 2008 to 2010 conversion. I don't know if VS2010 - # is sensitive to the exact names. - self.target_name = '_' + self.rule_name - self.after_targets = self.rule_name + 'AfterTargets' - self.before_targets = self.rule_name + 'BeforeTargets' - self.depends_on = self.rule_name + 'DependsOn' - self.compute_output = 'Compute%sOutput' % self.rule_name - self.dirs_to_make = self.rule_name + 'DirsToMake' - self.inputs = self.rule_name + '_inputs' - self.tlog = self.rule_name + '_tlog' - self.extension = rule['extension'] - if not self.extension.startswith('.'): - self.extension = '.' + self.extension - - self.description = MSVSSettings.ConvertVCMacrosToMSBuild( - rule.get('message', self.rule_name)) - old_additional_dependencies = _FixPaths(rule.get('inputs', [])) - self.additional_dependencies = ( - ';'.join([MSVSSettings.ConvertVCMacrosToMSBuild(i) - for i in old_additional_dependencies])) - old_outputs = _FixPaths(rule.get('outputs', [])) - self.outputs = ';'.join([MSVSSettings.ConvertVCMacrosToMSBuild(i) - for i in old_outputs]) - old_command = _BuildCommandLineForRule(spec, rule, has_input_path=True, - do_setup_env=True) - self.command = MSVSSettings.ConvertVCMacrosToMSBuild(old_command) + def __init__(self, rule, spec): + self.display_name = rule["rule_name"] + # Assure that the rule name is only characters and numbers + self.rule_name = re.sub(r"\W", "_", self.display_name) + # Create the various element names, following the example set by the + # Visual Studio 2008 to 2010 conversion. I don't know if VS2010 + # is sensitive to the exact names. + self.target_name = "_" + self.rule_name + self.after_targets = self.rule_name + "AfterTargets" + self.before_targets = self.rule_name + "BeforeTargets" + self.depends_on = self.rule_name + "DependsOn" + self.compute_output = "Compute%sOutput" % self.rule_name + self.dirs_to_make = self.rule_name + "DirsToMake" + self.inputs = self.rule_name + "_inputs" + self.tlog = self.rule_name + "_tlog" + self.extension = rule["extension"] + if not self.extension.startswith("."): + self.extension = "." + self.extension + + self.description = MSVSSettings.ConvertVCMacrosToMSBuild( + rule.get("message", self.rule_name) + ) + old_additional_dependencies = _FixPaths(rule.get("inputs", [])) + self.additional_dependencies = ";".join( + [ + MSVSSettings.ConvertVCMacrosToMSBuild(i) + for i in old_additional_dependencies + ] + ) + old_outputs = _FixPaths(rule.get("outputs", [])) + self.outputs = ";".join( + [MSVSSettings.ConvertVCMacrosToMSBuild(i) for i in old_outputs] + ) + old_command = _BuildCommandLineForRule( + spec, rule, has_input_path=True, do_setup_env=True + ) + self.command = MSVSSettings.ConvertVCMacrosToMSBuild(old_command) def _GenerateMSBuildRulePropsFile(props_path, msbuild_rules): - """Generate the .props file.""" - content = ['Project', - {'xmlns': 'http://schemas.microsoft.com/developer/msbuild/2003'}] - for rule in msbuild_rules: - content.extend([ - ['PropertyGroup', - {'Condition': "'$(%s)' == '' and '$(%s)' == '' and " - "'$(ConfigurationType)' != 'Makefile'" % (rule.before_targets, - rule.after_targets) - }, - [rule.before_targets, 'Midl'], - [rule.after_targets, 'CustomBuild'], - ], - ['PropertyGroup', - [rule.depends_on, - {'Condition': "'$(ConfigurationType)' != 'Makefile'"}, - '_SelectedFiles;$(%s)' % rule.depends_on - ], - ], - ['ItemDefinitionGroup', - [rule.rule_name, - ['CommandLineTemplate', rule.command], - ['Outputs', rule.outputs], - ['ExecutionDescription', rule.description], - ['AdditionalDependencies', rule.additional_dependencies], - ], - ] - ]) - easy_xml.WriteXmlIfChanged(content, props_path, pretty=True, win32=True) + """Generate the .props file.""" + content = [ + "Project", + {"xmlns": "http://schemas.microsoft.com/developer/msbuild/2003"}, + ] + for rule in msbuild_rules: + content.extend( + [ + [ + "PropertyGroup", + { + "Condition": "'$(%s)' == '' and '$(%s)' == '' and " + "'$(ConfigurationType)' != 'Makefile'" + % (rule.before_targets, rule.after_targets) + }, + [rule.before_targets, "Midl"], + [rule.after_targets, "CustomBuild"], + ], + [ + "PropertyGroup", + [ + rule.depends_on, + {"Condition": "'$(ConfigurationType)' != 'Makefile'"}, + "_SelectedFiles;$(%s)" % rule.depends_on, + ], + ], + [ + "ItemDefinitionGroup", + [ + rule.rule_name, + ["CommandLineTemplate", rule.command], + ["Outputs", rule.outputs], + ["ExecutionDescription", rule.description], + ["AdditionalDependencies", rule.additional_dependencies], + ], + ], + ] + ) + easy_xml.WriteXmlIfChanged(content, props_path, pretty=True, win32=True) def _GenerateMSBuildRuleTargetsFile(targets_path, msbuild_rules): - """Generate the .targets file.""" - content = ['Project', - {'xmlns': 'http://schemas.microsoft.com/developer/msbuild/2003' - } - ] - item_group = [ - 'ItemGroup', - ['PropertyPageSchema', - {'Include': '$(MSBuildThisFileDirectory)$(MSBuildThisFileName).xml'} - ] - ] - for rule in msbuild_rules: - item_group.append( - ['AvailableItemName', - {'Include': rule.rule_name}, - ['Targets', rule.target_name], - ]) - content.append(item_group) - - for rule in msbuild_rules: - content.append( - ['UsingTask', - {'TaskName': rule.rule_name, - 'TaskFactory': 'XamlTaskFactory', - 'AssemblyName': 'Microsoft.Build.Tasks.v4.0' - }, - ['Task', '$(MSBuildThisFileDirectory)$(MSBuildThisFileName).xml'], - ]) - for rule in msbuild_rules: - rule_name = rule.rule_name - target_outputs = '%%(%s.Outputs)' % rule_name - target_inputs = ('%%(%s.Identity);%%(%s.AdditionalDependencies);' - '$(MSBuildProjectFile)') % (rule_name, rule_name) - rule_inputs = '%%(%s.Identity)' % rule_name - extension_condition = ("'%(Extension)'=='.obj' or " - "'%(Extension)'=='.res' or " - "'%(Extension)'=='.rsc' or " - "'%(Extension)'=='.lib'") - remove_section = [ - 'ItemGroup', - {'Condition': "'@(SelectedFiles)' != ''"}, - [rule_name, - {'Remove': '@(%s)' % rule_name, - 'Condition': "'%(Identity)' != '@(SelectedFiles)'" - } - ] - ] - inputs_section = [ - 'ItemGroup', - [rule.inputs, {'Include': '%%(%s.AdditionalDependencies)' % rule_name}] + """Generate the .targets file.""" + content = [ + "Project", + {"xmlns": "http://schemas.microsoft.com/developer/msbuild/2003"}, ] - logging_section = [ - 'ItemGroup', - [rule.tlog, - {'Include': '%%(%s.Outputs)' % rule_name, - 'Condition': ("'%%(%s.Outputs)' != '' and " - "'%%(%s.ExcludedFromBuild)' != 'true'" % - (rule_name, rule_name)) - }, - ['Source', "@(%s, '|')" % rule_name], - ['Inputs', "@(%s -> '%%(Fullpath)', ';')" % rule.inputs], + item_group = [ + "ItemGroup", + [ + "PropertyPageSchema", + {"Include": "$(MSBuildThisFileDirectory)$(MSBuildThisFileName).xml"}, ], ] - message_section = [ - 'Message', - {'Importance': 'High', - 'Text': '%%(%s.ExecutionDescription)' % rule_name - } - ] - write_tlog_section = [ - 'WriteLinesToFile', - {'Condition': "'@(%s)' != '' and '%%(%s.ExcludedFromBuild)' != " - "'true'" % (rule.tlog, rule.tlog), - 'File': '$(IntDir)$(ProjectName).write.1.tlog', - 'Lines': "^%%(%s.Source);@(%s->'%%(Fullpath)')" % (rule.tlog, - rule.tlog) - } - ] - read_tlog_section = [ - 'WriteLinesToFile', - {'Condition': "'@(%s)' != '' and '%%(%s.ExcludedFromBuild)' != " - "'true'" % (rule.tlog, rule.tlog), - 'File': '$(IntDir)$(ProjectName).read.1.tlog', - 'Lines': "^%%(%s.Source);%%(%s.Inputs)" % (rule.tlog, rule.tlog) - } - ] - command_and_input_section = [ - rule_name, - {'Condition': "'@(%s)' != '' and '%%(%s.ExcludedFromBuild)' != " - "'true'" % (rule_name, rule_name), - 'EchoOff': 'true', - 'StandardOutputImportance': 'High', - 'StandardErrorImportance': 'High', - 'CommandLineTemplate': '%%(%s.CommandLineTemplate)' % rule_name, - 'AdditionalOptions': '%%(%s.AdditionalOptions)' % rule_name, - 'Inputs': rule_inputs - } - ] - content.extend([ - ['Target', - {'Name': rule.target_name, - 'BeforeTargets': '$(%s)' % rule.before_targets, - 'AfterTargets': '$(%s)' % rule.after_targets, - 'Condition': "'@(%s)' != ''" % rule_name, - 'DependsOnTargets': '$(%s);%s' % (rule.depends_on, - rule.compute_output), - 'Outputs': target_outputs, - 'Inputs': target_inputs - }, - remove_section, - inputs_section, - logging_section, - message_section, - write_tlog_section, - read_tlog_section, - command_and_input_section, - ], - ['PropertyGroup', - ['ComputeLinkInputsTargets', - '$(ComputeLinkInputsTargets);', - '%s;' % rule.compute_output - ], - ['ComputeLibInputsTargets', - '$(ComputeLibInputsTargets);', - '%s;' % rule.compute_output - ], - ], - ['Target', - {'Name': rule.compute_output, - 'Condition': "'@(%s)' != ''" % rule_name - }, - ['ItemGroup', - [rule.dirs_to_make, - {'Condition': "'@(%s)' != '' and " - "'%%(%s.ExcludedFromBuild)' != 'true'" % (rule_name, rule_name), - 'Include': '%%(%s.Outputs)' % rule_name - } - ], - ['Link', - {'Include': '%%(%s.Identity)' % rule.dirs_to_make, - 'Condition': extension_condition - } - ], - ['Lib', - {'Include': '%%(%s.Identity)' % rule.dirs_to_make, - 'Condition': extension_condition - } - ], - ['ImpLib', - {'Include': '%%(%s.Identity)' % rule.dirs_to_make, - 'Condition': extension_condition - } - ], - ], - ['MakeDir', - {'Directories': ("@(%s->'%%(RootDir)%%(Directory)')" % - rule.dirs_to_make) - } - ] - ], - ]) - easy_xml.WriteXmlIfChanged(content, targets_path, pretty=True, win32=True) + for rule in msbuild_rules: + item_group.append( + [ + "AvailableItemName", + {"Include": rule.rule_name}, + ["Targets", rule.target_name], + ] + ) + content.append(item_group) + + for rule in msbuild_rules: + content.append( + [ + "UsingTask", + { + "TaskName": rule.rule_name, + "TaskFactory": "XamlTaskFactory", + "AssemblyName": "Microsoft.Build.Tasks.v4.0", + }, + ["Task", "$(MSBuildThisFileDirectory)$(MSBuildThisFileName).xml"], + ] + ) + for rule in msbuild_rules: + rule_name = rule.rule_name + target_outputs = "%%(%s.Outputs)" % rule_name + target_inputs = ( + "%%(%s.Identity);%%(%s.AdditionalDependencies);" "$(MSBuildProjectFile)" + ) % (rule_name, rule_name) + rule_inputs = "%%(%s.Identity)" % rule_name + extension_condition = ( + "'%(Extension)'=='.obj' or " + "'%(Extension)'=='.res' or " + "'%(Extension)'=='.rsc' or " + "'%(Extension)'=='.lib'" + ) + remove_section = [ + "ItemGroup", + {"Condition": "'@(SelectedFiles)' != ''"}, + [ + rule_name, + { + "Remove": "@(%s)" % rule_name, + "Condition": "'%(Identity)' != '@(SelectedFiles)'", + }, + ], + ] + inputs_section = [ + "ItemGroup", + [rule.inputs, {"Include": "%%(%s.AdditionalDependencies)" % rule_name}], + ] + logging_section = [ + "ItemGroup", + [ + rule.tlog, + { + "Include": "%%(%s.Outputs)" % rule_name, + "Condition": ( + "'%%(%s.Outputs)' != '' and " + "'%%(%s.ExcludedFromBuild)' != 'true'" % (rule_name, rule_name) + ), + }, + ["Source", "@(%s, '|')" % rule_name], + ["Inputs", "@(%s -> '%%(Fullpath)', ';')" % rule.inputs], + ], + ] + message_section = [ + "Message", + {"Importance": "High", "Text": "%%(%s.ExecutionDescription)" % rule_name}, + ] + write_tlog_section = [ + "WriteLinesToFile", + { + "Condition": "'@(%s)' != '' and '%%(%s.ExcludedFromBuild)' != " + "'true'" % (rule.tlog, rule.tlog), + "File": "$(IntDir)$(ProjectName).write.1.tlog", + "Lines": "^%%(%s.Source);@(%s->'%%(Fullpath)')" + % (rule.tlog, rule.tlog), + }, + ] + read_tlog_section = [ + "WriteLinesToFile", + { + "Condition": "'@(%s)' != '' and '%%(%s.ExcludedFromBuild)' != " + "'true'" % (rule.tlog, rule.tlog), + "File": "$(IntDir)$(ProjectName).read.1.tlog", + "Lines": "^%%(%s.Source);%%(%s.Inputs)" % (rule.tlog, rule.tlog), + }, + ] + command_and_input_section = [ + rule_name, + { + "Condition": "'@(%s)' != '' and '%%(%s.ExcludedFromBuild)' != " + "'true'" % (rule_name, rule_name), + "EchoOff": "true", + "StandardOutputImportance": "High", + "StandardErrorImportance": "High", + "CommandLineTemplate": "%%(%s.CommandLineTemplate)" % rule_name, + "AdditionalOptions": "%%(%s.AdditionalOptions)" % rule_name, + "Inputs": rule_inputs, + }, + ] + content.extend( + [ + [ + "Target", + { + "Name": rule.target_name, + "BeforeTargets": "$(%s)" % rule.before_targets, + "AfterTargets": "$(%s)" % rule.after_targets, + "Condition": "'@(%s)' != ''" % rule_name, + "DependsOnTargets": "$(%s);%s" + % (rule.depends_on, rule.compute_output), + "Outputs": target_outputs, + "Inputs": target_inputs, + }, + remove_section, + inputs_section, + logging_section, + message_section, + write_tlog_section, + read_tlog_section, + command_and_input_section, + ], + [ + "PropertyGroup", + [ + "ComputeLinkInputsTargets", + "$(ComputeLinkInputsTargets);", + "%s;" % rule.compute_output, + ], + [ + "ComputeLibInputsTargets", + "$(ComputeLibInputsTargets);", + "%s;" % rule.compute_output, + ], + ], + [ + "Target", + { + "Name": rule.compute_output, + "Condition": "'@(%s)' != ''" % rule_name, + }, + [ + "ItemGroup", + [ + rule.dirs_to_make, + { + "Condition": "'@(%s)' != '' and " + "'%%(%s.ExcludedFromBuild)' != 'true'" + % (rule_name, rule_name), + "Include": "%%(%s.Outputs)" % rule_name, + }, + ], + [ + "Link", + { + "Include": "%%(%s.Identity)" % rule.dirs_to_make, + "Condition": extension_condition, + }, + ], + [ + "Lib", + { + "Include": "%%(%s.Identity)" % rule.dirs_to_make, + "Condition": extension_condition, + }, + ], + [ + "ImpLib", + { + "Include": "%%(%s.Identity)" % rule.dirs_to_make, + "Condition": extension_condition, + }, + ], + ], + [ + "MakeDir", + { + "Directories": ( + "@(%s->'%%(RootDir)%%(Directory)')" % rule.dirs_to_make + ) + }, + ], + ], + ] + ) + easy_xml.WriteXmlIfChanged(content, targets_path, pretty=True, win32=True) def _GenerateMSBuildRuleXmlFile(xml_path, msbuild_rules): - # Generate the .xml file - content = [ - 'ProjectSchemaDefinitions', - {'xmlns': ('clr-namespace:Microsoft.Build.Framework.XamlTypes;' - 'assembly=Microsoft.Build.Framework'), - 'xmlns:x': 'http://schemas.microsoft.com/winfx/2006/xaml', - 'xmlns:sys': 'clr-namespace:System;assembly=mscorlib', - 'xmlns:transformCallback': - 'Microsoft.Cpp.Dev10.ConvertPropertyCallback' - } - ] - for rule in msbuild_rules: - content.extend([ - ['Rule', - {'Name': rule.rule_name, - 'PageTemplate': 'tool', - 'DisplayName': rule.display_name, - 'Order': '200' - }, - ['Rule.DataSource', - ['DataSource', - {'Persistence': 'ProjectFile', - 'ItemType': rule.rule_name - } - ] - ], - ['Rule.Categories', - ['Category', - {'Name': 'General'}, - ['Category.DisplayName', - ['sys:String', 'General'], - ], - ], - ['Category', - {'Name': 'Command Line', - 'Subtype': 'CommandLine' - }, - ['Category.DisplayName', - ['sys:String', 'Command Line'], - ], - ], - ], - ['StringListProperty', - {'Name': 'Inputs', - 'Category': 'Command Line', - 'IsRequired': 'true', - 'Switch': ' ' - }, - ['StringListProperty.DataSource', - ['DataSource', - {'Persistence': 'ProjectFile', - 'ItemType': rule.rule_name, - 'SourceType': 'Item' - } - ] - ], - ], - ['StringProperty', - {'Name': 'CommandLineTemplate', - 'DisplayName': 'Command Line', - 'Visible': 'False', - 'IncludeInCommandLine': 'False' - } - ], - ['DynamicEnumProperty', - {'Name': rule.before_targets, - 'Category': 'General', - 'EnumProvider': 'Targets', - 'IncludeInCommandLine': 'False' - }, - ['DynamicEnumProperty.DisplayName', - ['sys:String', 'Execute Before'], - ], - ['DynamicEnumProperty.Description', - ['sys:String', 'Specifies the targets for the build customization' - ' to run before.' - ], - ], - ['DynamicEnumProperty.ProviderSettings', - ['NameValuePair', - {'Name': 'Exclude', - 'Value': '^%s|^Compute' % rule.before_targets - } - ] - ], - ['DynamicEnumProperty.DataSource', - ['DataSource', - {'Persistence': 'ProjectFile', - 'HasConfigurationCondition': 'true' - } - ] - ], - ], - ['DynamicEnumProperty', - {'Name': rule.after_targets, - 'Category': 'General', - 'EnumProvider': 'Targets', - 'IncludeInCommandLine': 'False' - }, - ['DynamicEnumProperty.DisplayName', - ['sys:String', 'Execute After'], - ], - ['DynamicEnumProperty.Description', - ['sys:String', ('Specifies the targets for the build customization' - ' to run after.') - ], - ], - ['DynamicEnumProperty.ProviderSettings', - ['NameValuePair', - {'Name': 'Exclude', - 'Value': '^%s|^Compute' % rule.after_targets - } - ] - ], - ['DynamicEnumProperty.DataSource', - ['DataSource', - {'Persistence': 'ProjectFile', - 'ItemType': '', - 'HasConfigurationCondition': 'true' - } - ] - ], - ], - ['StringListProperty', - {'Name': 'Outputs', - 'DisplayName': 'Outputs', - 'Visible': 'False', - 'IncludeInCommandLine': 'False' - } - ], - ['StringProperty', - {'Name': 'ExecutionDescription', - 'DisplayName': 'Execution Description', - 'Visible': 'False', - 'IncludeInCommandLine': 'False' - } - ], - ['StringListProperty', - {'Name': 'AdditionalDependencies', - 'DisplayName': 'Additional Dependencies', - 'IncludeInCommandLine': 'False', - 'Visible': 'false' - } - ], - ['StringProperty', - {'Subtype': 'AdditionalOptions', - 'Name': 'AdditionalOptions', - 'Category': 'Command Line' - }, - ['StringProperty.DisplayName', - ['sys:String', 'Additional Options'], - ], - ['StringProperty.Description', - ['sys:String', 'Additional Options'], - ], - ], - ], - ['ItemType', - {'Name': rule.rule_name, - 'DisplayName': rule.display_name - } - ], - ['FileExtension', - {'Name': '*' + rule.extension, - 'ContentType': rule.rule_name - } - ], - ['ContentType', - {'Name': rule.rule_name, - 'DisplayName': '', - 'ItemType': rule.rule_name - } - ] - ]) - easy_xml.WriteXmlIfChanged(content, xml_path, pretty=True, win32=True) + # Generate the .xml file + content = [ + "ProjectSchemaDefinitions", + { + "xmlns": ( + "clr-namespace:Microsoft.Build.Framework.XamlTypes;" + "assembly=Microsoft.Build.Framework" + ), + "xmlns:x": "http://schemas.microsoft.com/winfx/2006/xaml", + "xmlns:sys": "clr-namespace:System;assembly=mscorlib", + "xmlns:transformCallback": "Microsoft.Cpp.Dev10.ConvertPropertyCallback", + }, + ] + for rule in msbuild_rules: + content.extend( + [ + [ + "Rule", + { + "Name": rule.rule_name, + "PageTemplate": "tool", + "DisplayName": rule.display_name, + "Order": "200", + }, + [ + "Rule.DataSource", + [ + "DataSource", + {"Persistence": "ProjectFile", "ItemType": rule.rule_name}, + ], + ], + [ + "Rule.Categories", + [ + "Category", + {"Name": "General"}, + ["Category.DisplayName", ["sys:String", "General"]], + ], + [ + "Category", + {"Name": "Command Line", "Subtype": "CommandLine"}, + ["Category.DisplayName", ["sys:String", "Command Line"]], + ], + ], + [ + "StringListProperty", + { + "Name": "Inputs", + "Category": "Command Line", + "IsRequired": "true", + "Switch": " ", + }, + [ + "StringListProperty.DataSource", + [ + "DataSource", + { + "Persistence": "ProjectFile", + "ItemType": rule.rule_name, + "SourceType": "Item", + }, + ], + ], + ], + [ + "StringProperty", + { + "Name": "CommandLineTemplate", + "DisplayName": "Command Line", + "Visible": "False", + "IncludeInCommandLine": "False", + }, + ], + [ + "DynamicEnumProperty", + { + "Name": rule.before_targets, + "Category": "General", + "EnumProvider": "Targets", + "IncludeInCommandLine": "False", + }, + [ + "DynamicEnumProperty.DisplayName", + ["sys:String", "Execute Before"], + ], + [ + "DynamicEnumProperty.Description", + [ + "sys:String", + "Specifies the targets for the build customization" + " to run before.", + ], + ], + [ + "DynamicEnumProperty.ProviderSettings", + [ + "NameValuePair", + { + "Name": "Exclude", + "Value": "^%s|^Compute" % rule.before_targets, + }, + ], + ], + [ + "DynamicEnumProperty.DataSource", + [ + "DataSource", + { + "Persistence": "ProjectFile", + "HasConfigurationCondition": "true", + }, + ], + ], + ], + [ + "DynamicEnumProperty", + { + "Name": rule.after_targets, + "Category": "General", + "EnumProvider": "Targets", + "IncludeInCommandLine": "False", + }, + [ + "DynamicEnumProperty.DisplayName", + ["sys:String", "Execute After"], + ], + [ + "DynamicEnumProperty.Description", + [ + "sys:String", + ( + "Specifies the targets for the build customization" + " to run after." + ), + ], + ], + [ + "DynamicEnumProperty.ProviderSettings", + [ + "NameValuePair", + { + "Name": "Exclude", + "Value": "^%s|^Compute" % rule.after_targets, + }, + ], + ], + [ + "DynamicEnumProperty.DataSource", + [ + "DataSource", + { + "Persistence": "ProjectFile", + "ItemType": "", + "HasConfigurationCondition": "true", + }, + ], + ], + ], + [ + "StringListProperty", + { + "Name": "Outputs", + "DisplayName": "Outputs", + "Visible": "False", + "IncludeInCommandLine": "False", + }, + ], + [ + "StringProperty", + { + "Name": "ExecutionDescription", + "DisplayName": "Execution Description", + "Visible": "False", + "IncludeInCommandLine": "False", + }, + ], + [ + "StringListProperty", + { + "Name": "AdditionalDependencies", + "DisplayName": "Additional Dependencies", + "IncludeInCommandLine": "False", + "Visible": "false", + }, + ], + [ + "StringProperty", + { + "Subtype": "AdditionalOptions", + "Name": "AdditionalOptions", + "Category": "Command Line", + }, + [ + "StringProperty.DisplayName", + ["sys:String", "Additional Options"], + ], + [ + "StringProperty.Description", + ["sys:String", "Additional Options"], + ], + ], + ], + [ + "ItemType", + {"Name": rule.rule_name, "DisplayName": rule.display_name}, + ], + [ + "FileExtension", + {"Name": "*" + rule.extension, "ContentType": rule.rule_name}, + ], + [ + "ContentType", + { + "Name": rule.rule_name, + "DisplayName": "", + "ItemType": rule.rule_name, + }, + ], + ] + ) + easy_xml.WriteXmlIfChanged(content, xml_path, pretty=True, win32=True) def _GetConfigurationAndPlatform(name, settings, spec): - configuration = name.rsplit('_', 1)[0] - platform = settings.get('msvs_configuration_platform', 'Win32') - if spec['toolset'] == 'host' and platform == 'arm64': - platform = 'x64' # Host-only tools are always built for x64 - return (configuration, platform) + configuration = name.rsplit("_", 1)[0] + platform = settings.get("msvs_configuration_platform", "Win32") + if spec["toolset"] == "host" and platform == "arm64": + platform = "x64" # Host-only tools are always built for x64 + return (configuration, platform) def _GetConfigurationCondition(name, settings, spec): - return (r"'$(Configuration)|$(Platform)'=='%s|%s'" % - _GetConfigurationAndPlatform(name, settings, spec)) + return r"'$(Configuration)|$(Platform)'=='%s|%s'" % _GetConfigurationAndPlatform( + name, settings, spec + ) def _GetMSBuildProjectConfigurations(configurations, spec): - group = ['ItemGroup', {'Label': 'ProjectConfigurations'}] - for (name, settings) in sorted(configurations.items()): - configuration, platform = _GetConfigurationAndPlatform(name, settings, spec) - designation = '%s|%s' % (configuration, platform) - group.append( - ['ProjectConfiguration', {'Include': designation}, - ['Configuration', configuration], - ['Platform', platform]]) - return [group] + group = ["ItemGroup", {"Label": "ProjectConfigurations"}] + for (name, settings) in sorted(configurations.items()): + configuration, platform = _GetConfigurationAndPlatform(name, settings, spec) + designation = "%s|%s" % (configuration, platform) + group.append( + [ + "ProjectConfiguration", + {"Include": designation}, + ["Configuration", configuration], + ["Platform", platform], + ] + ) + return [group] def _GetMSBuildGlobalProperties(spec, version, guid, gyp_file_name): - namespace = os.path.splitext(gyp_file_name)[0] - properties = [ - ['PropertyGroup', {'Label': 'Globals'}, - ['ProjectGuid', guid], - ['Keyword', 'Win32Proj'], - ['RootNamespace', namespace], - ['IgnoreWarnCompileDuplicatedFilename', 'true'], - ] + namespace = os.path.splitext(gyp_file_name)[0] + properties = [ + [ + "PropertyGroup", + {"Label": "Globals"}, + ["ProjectGuid", guid], + ["Keyword", "Win32Proj"], + ["RootNamespace", namespace], + ["IgnoreWarnCompileDuplicatedFilename", "true"], + ] ] - if os.environ.get('PROCESSOR_ARCHITECTURE') == 'AMD64' or \ - os.environ.get('PROCESSOR_ARCHITEW6432') == 'AMD64': - properties[0].append(['PreferredToolArchitecture', 'x64']) - - if spec.get('msvs_target_platform_version'): - target_platform_version = spec.get('msvs_target_platform_version') - properties[0].append(['WindowsTargetPlatformVersion', - target_platform_version]) - if spec.get('msvs_target_platform_minversion'): - target_platform_minversion = spec.get('msvs_target_platform_minversion') - properties[0].append(['WindowsTargetPlatformMinVersion', - target_platform_minversion]) - else: - properties[0].append(['WindowsTargetPlatformMinVersion', - target_platform_version]) - - if spec.get('msvs_enable_winrt'): - properties[0].append(['DefaultLanguage', 'en-US']) - properties[0].append(['AppContainerApplication', 'true']) - if spec.get('msvs_application_type_revision'): - app_type_revision = spec.get('msvs_application_type_revision') - properties[0].append(['ApplicationTypeRevision', app_type_revision]) - else: - properties[0].append(['ApplicationTypeRevision', '8.1']) - if spec.get('msvs_enable_winphone'): - properties[0].append(['ApplicationType', 'Windows Phone']) - else: - properties[0].append(['ApplicationType', 'Windows Store']) - - platform_name = None - msvs_windows_sdk_version = None - for configuration in spec['configurations'].values(): - platform_name = platform_name or _ConfigPlatform(configuration, spec) - msvs_windows_sdk_version = (msvs_windows_sdk_version or - _ConfigWindowsTargetPlatformVersion(configuration, version)) - if platform_name and msvs_windows_sdk_version: - break - if msvs_windows_sdk_version: - properties[0].append(['WindowsTargetPlatformVersion', - str(msvs_windows_sdk_version)]) - elif version.compatible_sdks: - raise GypError('%s requires any SDK of %s version, but none were found' % - (version.description, version.compatible_sdks)) - - if platform_name == 'ARM': - properties[0].append(['WindowsSDKDesktopARMSupport', 'true']) + if ( + os.environ.get("PROCESSOR_ARCHITECTURE") == "AMD64" + or os.environ.get("PROCESSOR_ARCHITEW6432") == "AMD64" + ): + properties[0].append(["PreferredToolArchitecture", "x64"]) + + if spec.get("msvs_target_platform_version"): + target_platform_version = spec.get("msvs_target_platform_version") + properties[0].append(["WindowsTargetPlatformVersion", target_platform_version]) + if spec.get("msvs_target_platform_minversion"): + target_platform_minversion = spec.get("msvs_target_platform_minversion") + properties[0].append( + ["WindowsTargetPlatformMinVersion", target_platform_minversion] + ) + else: + properties[0].append( + ["WindowsTargetPlatformMinVersion", target_platform_version] + ) - return properties + if spec.get("msvs_enable_winrt"): + properties[0].append(["DefaultLanguage", "en-US"]) + properties[0].append(["AppContainerApplication", "true"]) + if spec.get("msvs_application_type_revision"): + app_type_revision = spec.get("msvs_application_type_revision") + properties[0].append(["ApplicationTypeRevision", app_type_revision]) + else: + properties[0].append(["ApplicationTypeRevision", "8.1"]) + if spec.get("msvs_enable_winphone"): + properties[0].append(["ApplicationType", "Windows Phone"]) + else: + properties[0].append(["ApplicationType", "Windows Store"]) + + platform_name = None + msvs_windows_sdk_version = None + for configuration in spec["configurations"].values(): + platform_name = platform_name or _ConfigPlatform(configuration) + msvs_windows_sdk_version = ( + msvs_windows_sdk_version + or _ConfigWindowsTargetPlatformVersion(configuration, version) + ) + if platform_name and msvs_windows_sdk_version: + break + if msvs_windows_sdk_version: + properties[0].append( + ["WindowsTargetPlatformVersion", str(msvs_windows_sdk_version)] + ) + elif version.compatible_sdks: + raise GypError( + "%s requires any SDK of %s version, but none were found" + % (version.description, version.compatible_sdks) + ) + + if platform_name == "ARM": + properties[0].append(["WindowsSDKDesktopARMSupport", "true"]) + + return properties def _GetMSBuildConfigurationDetails(spec, build_file): - properties = {} - for name, settings in spec['configurations'].items(): - msbuild_attributes = _GetMSBuildAttributes(spec, settings, build_file) - condition = _GetConfigurationCondition(name, settings, spec) - character_set = msbuild_attributes.get('CharacterSet') - config_type = msbuild_attributes.get('ConfigurationType') - _AddConditionalProperty(properties, condition, 'ConfigurationType', - config_type) - if config_type == 'Driver': - _AddConditionalProperty(properties, condition, 'DriverType', 'WDM') - _AddConditionalProperty(properties, condition, 'TargetVersion', - _ConfigTargetVersion(settings)) - if character_set: - if 'msvs_enable_winrt' not in spec : - _AddConditionalProperty(properties, condition, 'CharacterSet', - character_set) - return _GetMSBuildPropertyGroup(spec, 'Configuration', properties) + properties = {} + for name, settings in spec["configurations"].items(): + msbuild_attributes = _GetMSBuildAttributes(spec, settings, build_file) + condition = _GetConfigurationCondition(name, settings, spec) + character_set = msbuild_attributes.get("CharacterSet") + config_type = msbuild_attributes.get("ConfigurationType") + _AddConditionalProperty(properties, condition, "ConfigurationType", config_type) + if config_type == "Driver": + _AddConditionalProperty(properties, condition, "DriverType", "WDM") + _AddConditionalProperty( + properties, condition, "TargetVersion", _ConfigTargetVersion(settings) + ) + if character_set: + if "msvs_enable_winrt" not in spec: + _AddConditionalProperty( + properties, condition, "CharacterSet", character_set + ) + return _GetMSBuildPropertyGroup(spec, "Configuration", properties) def _GetMSBuildLocalProperties(msbuild_toolset): - # Currently the only local property we support is PlatformToolset - properties = {} - if msbuild_toolset: - properties = [ - ['PropertyGroup', {'Label': 'Locals'}, - ['PlatformToolset', msbuild_toolset], + # Currently the only local property we support is PlatformToolset + properties = {} + if msbuild_toolset: + properties = [ + [ + "PropertyGroup", + {"Label": "Locals"}, + ["PlatformToolset", msbuild_toolset], + ] ] - ] - return properties + return properties def _GetMSBuildPropertySheets(configurations, spec): - user_props = r'$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props' - additional_props = {} - props_specified = False - for name, settings in sorted(configurations.items()): - configuration = _GetConfigurationCondition(name, settings, spec) - if 'msbuild_props' in settings: - additional_props[configuration] = _FixPaths(settings['msbuild_props']) - props_specified = True - else: - additional_props[configuration] = '' - - if not props_specified: - return [ - ['ImportGroup', - {'Label': 'PropertySheets'}, - ['Import', - {'Project': user_props, - 'Condition': "exists('%s')" % user_props, - 'Label': 'LocalAppDataPlatform' - } - ] - ] - ] - else: - sheets = [] - for condition, props in additional_props.items(): - import_group = [ - 'ImportGroup', - {'Label': 'PropertySheets', - 'Condition': condition - }, - ['Import', - {'Project': user_props, - 'Condition': "exists('%s')" % user_props, - 'Label': 'LocalAppDataPlatform' - } + user_props = r"$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" + additional_props = {} + props_specified = False + for name, settings in sorted(configurations.items()): + configuration = _GetConfigurationCondition(name, settings, spec) + if "msbuild_props" in settings: + additional_props[configuration] = _FixPaths(settings["msbuild_props"]) + props_specified = True + else: + additional_props[configuration] = "" + + if not props_specified: + return [ + [ + "ImportGroup", + {"Label": "PropertySheets"}, + [ + "Import", + { + "Project": user_props, + "Condition": "exists('%s')" % user_props, + "Label": "LocalAppDataPlatform", + }, + ], + ] ] - ] - for props_file in props: - import_group.append(['Import', {'Project':props_file}]) - sheets.append(import_group) - return sheets + else: + sheets = [] + for condition, props in additional_props.items(): + import_group = [ + "ImportGroup", + {"Label": "PropertySheets", "Condition": condition}, + [ + "Import", + { + "Project": user_props, + "Condition": "exists('%s')" % user_props, + "Label": "LocalAppDataPlatform", + }, + ], + ] + for props_file in props: + import_group.append(["Import", {"Project": props_file}]) + sheets.append(import_group) + return sheets + def _ConvertMSVSBuildAttributes(spec, config, build_file): - config_type = _GetMSVSConfigurationType(spec, build_file) - msvs_attributes = _GetMSVSAttributes(spec, config, config_type) - msbuild_attributes = {} - for a in msvs_attributes: - if a in ['IntermediateDirectory', 'OutputDirectory']: - directory = MSVSSettings.ConvertVCMacrosToMSBuild(msvs_attributes[a]) - if not directory.endswith('\\'): - directory += '\\' - msbuild_attributes[a] = directory - elif a == 'CharacterSet': - msbuild_attributes[a] = _ConvertMSVSCharacterSet(msvs_attributes[a]) - elif a == 'ConfigurationType': - msbuild_attributes[a] = _ConvertMSVSConfigurationType(msvs_attributes[a]) - else: - print('Warning: Do not know how to convert MSVS attribute ' + a) - return msbuild_attributes + config_type = _GetMSVSConfigurationType(spec, build_file) + msvs_attributes = _GetMSVSAttributes(spec, config, config_type) + msbuild_attributes = {} + for a in msvs_attributes: + if a in ["IntermediateDirectory", "OutputDirectory"]: + directory = MSVSSettings.ConvertVCMacrosToMSBuild(msvs_attributes[a]) + if not directory.endswith("\\"): + directory += "\\" + msbuild_attributes[a] = directory + elif a == "CharacterSet": + msbuild_attributes[a] = _ConvertMSVSCharacterSet(msvs_attributes[a]) + elif a == "ConfigurationType": + msbuild_attributes[a] = _ConvertMSVSConfigurationType(msvs_attributes[a]) + else: + print("Warning: Do not know how to convert MSVS attribute " + a) + return msbuild_attributes def _ConvertMSVSCharacterSet(char_set): - if char_set.isdigit(): - char_set = { - '0': 'MultiByte', - '1': 'Unicode', - '2': 'MultiByte', - }[char_set] - return char_set + if char_set.isdigit(): + char_set = {"0": "MultiByte", "1": "Unicode", "2": "MultiByte"}[char_set] + return char_set def _ConvertMSVSConfigurationType(config_type): - if config_type.isdigit(): - config_type = { - '1': 'Application', - '2': 'DynamicLibrary', - '4': 'StaticLibrary', - '5': 'Driver', - '10': 'Utility' - }[config_type] - return config_type + if config_type.isdigit(): + config_type = { + "1": "Application", + "2": "DynamicLibrary", + "4": "StaticLibrary", + "5": "Driver", + "10": "Utility", + }[config_type] + return config_type def _GetMSBuildAttributes(spec, config, build_file): - if 'msbuild_configuration_attributes' not in config: - msbuild_attributes = _ConvertMSVSBuildAttributes(spec, config, build_file) + if "msbuild_configuration_attributes" not in config: + msbuild_attributes = _ConvertMSVSBuildAttributes(spec, config, build_file) - else: - config_type = _GetMSVSConfigurationType(spec, build_file) - config_type = _ConvertMSVSConfigurationType(config_type) - msbuild_attributes = config.get('msbuild_configuration_attributes', {}) - msbuild_attributes.setdefault('ConfigurationType', config_type) - output_dir = msbuild_attributes.get('OutputDirectory', - '$(SolutionDir)$(Configuration)') - msbuild_attributes['OutputDirectory'] = _FixPath(output_dir) + '\\' - if 'IntermediateDirectory' not in msbuild_attributes: - intermediate = _FixPath('$(Configuration)') + '\\' - msbuild_attributes['IntermediateDirectory'] = intermediate - if 'CharacterSet' in msbuild_attributes: - msbuild_attributes['CharacterSet'] = _ConvertMSVSCharacterSet( - msbuild_attributes['CharacterSet']) - if 'TargetName' not in msbuild_attributes: - prefix = spec.get('product_prefix', '') - product_name = spec.get('product_name', '$(ProjectName)') - target_name = prefix + product_name - msbuild_attributes['TargetName'] = target_name - if 'TargetExt' not in msbuild_attributes and 'product_extension' in spec: - ext = spec.get('product_extension') - msbuild_attributes['TargetExt'] = '.' + ext - - if spec.get('msvs_external_builder'): - external_out_dir = spec.get('msvs_external_builder_out_dir', '.') - msbuild_attributes['OutputDirectory'] = _FixPath(external_out_dir) + '\\' - - # Make sure that 'TargetPath' matches 'Lib.OutputFile' or 'Link.OutputFile' - # (depending on the tool used) to avoid MSB8012 warning. - msbuild_tool_map = { - 'executable': 'Link', - 'shared_library': 'Link', - 'loadable_module': 'Link', - 'windows_driver': 'Link', - 'static_library': 'Lib', - } - msbuild_tool = msbuild_tool_map.get(spec['type']) - if msbuild_tool: - msbuild_settings = config['finalized_msbuild_settings'] - out_file = msbuild_settings[msbuild_tool].get('OutputFile') - if out_file: - msbuild_attributes['TargetPath'] = _FixPath(out_file) - target_ext = msbuild_settings[msbuild_tool].get('TargetExt') - if target_ext: - msbuild_attributes['TargetExt'] = target_ext + else: + config_type = _GetMSVSConfigurationType(spec, build_file) + config_type = _ConvertMSVSConfigurationType(config_type) + msbuild_attributes = config.get("msbuild_configuration_attributes", {}) + msbuild_attributes.setdefault("ConfigurationType", config_type) + output_dir = msbuild_attributes.get( + "OutputDirectory", "$(SolutionDir)$(Configuration)" + ) + msbuild_attributes["OutputDirectory"] = _FixPath(output_dir) + "\\" + if "IntermediateDirectory" not in msbuild_attributes: + intermediate = _FixPath("$(Configuration)") + "\\" + msbuild_attributes["IntermediateDirectory"] = intermediate + if "CharacterSet" in msbuild_attributes: + msbuild_attributes["CharacterSet"] = _ConvertMSVSCharacterSet( + msbuild_attributes["CharacterSet"] + ) + if "TargetName" not in msbuild_attributes: + prefix = spec.get("product_prefix", "") + product_name = spec.get("product_name", "$(ProjectName)") + target_name = prefix + product_name + msbuild_attributes["TargetName"] = target_name + if "TargetExt" not in msbuild_attributes and "product_extension" in spec: + ext = spec.get("product_extension") + msbuild_attributes["TargetExt"] = "." + ext + + if spec.get("msvs_external_builder"): + external_out_dir = spec.get("msvs_external_builder_out_dir", ".") + msbuild_attributes["OutputDirectory"] = _FixPath(external_out_dir) + "\\" + + # Make sure that 'TargetPath' matches 'Lib.OutputFile' or 'Link.OutputFile' + # (depending on the tool used) to avoid MSB8012 warning. + msbuild_tool_map = { + "executable": "Link", + "shared_library": "Link", + "loadable_module": "Link", + "windows_driver": "Link", + "static_library": "Lib", + } + msbuild_tool = msbuild_tool_map.get(spec["type"]) + if msbuild_tool: + msbuild_settings = config["finalized_msbuild_settings"] + out_file = msbuild_settings[msbuild_tool].get("OutputFile") + if out_file: + msbuild_attributes["TargetPath"] = _FixPath(out_file) + target_ext = msbuild_settings[msbuild_tool].get("TargetExt") + if target_ext: + msbuild_attributes["TargetExt"] = target_ext - return msbuild_attributes + return msbuild_attributes def _GetMSBuildConfigurationGlobalProperties(spec, configurations, build_file): - # TODO(jeanluc) We could optimize out the following and do it only if - # there are actions. - # TODO(jeanluc) Handle the equivalent of setting 'CYGWIN=nontsec'. - new_paths = [] - cygwin_dirs = spec.get('msvs_cygwin_dirs', ['.'])[0] - if cygwin_dirs: - cyg_path = '$(MSBuildProjectDirectory)\\%s\\bin\\' % _FixPath(cygwin_dirs) - new_paths.append(cyg_path) - # TODO(jeanluc) Change the convention to have both a cygwin_dir and a - # python_dir. - python_path = cyg_path.replace('cygwin\\bin', 'python_26') - new_paths.append(python_path) - if new_paths: - new_paths = '$(ExecutablePath);' + ';'.join(new_paths) - - properties = {} - for (name, configuration) in sorted(configurations.items()): - condition = _GetConfigurationCondition(name, configuration, spec) - attributes = _GetMSBuildAttributes(spec, configuration, build_file) - msbuild_settings = configuration['finalized_msbuild_settings'] - _AddConditionalProperty(properties, condition, 'IntDir', - attributes['IntermediateDirectory']) - _AddConditionalProperty(properties, condition, 'OutDir', - attributes['OutputDirectory']) - _AddConditionalProperty(properties, condition, 'TargetName', - attributes['TargetName']) - if 'TargetExt' in attributes: - _AddConditionalProperty(properties, condition, 'TargetExt', - attributes['TargetExt']) - - if attributes.get('TargetPath'): - _AddConditionalProperty(properties, condition, 'TargetPath', - attributes['TargetPath']) - if attributes.get('TargetExt'): - _AddConditionalProperty(properties, condition, 'TargetExt', - attributes['TargetExt']) - - if new_paths: - _AddConditionalProperty(properties, condition, 'ExecutablePath', - new_paths) - tool_settings = msbuild_settings.get('', {}) - for name, value in sorted(tool_settings.items()): - formatted_value = _GetValueFormattedForMSBuild('', name, value) - _AddConditionalProperty(properties, condition, name, formatted_value) - return _GetMSBuildPropertyGroup(spec, None, properties) + # TODO(jeanluc) We could optimize out the following and do it only if + # there are actions. + # TODO(jeanluc) Handle the equivalent of setting 'CYGWIN=nontsec'. + new_paths = [] + cygwin_dirs = spec.get("msvs_cygwin_dirs", ["."])[0] + if cygwin_dirs: + cyg_path = "$(MSBuildProjectDirectory)\\%s\\bin\\" % _FixPath(cygwin_dirs) + new_paths.append(cyg_path) + # TODO(jeanluc) Change the convention to have both a cygwin_dir and a + # python_dir. + python_path = cyg_path.replace("cygwin\\bin", "python_26") + new_paths.append(python_path) + if new_paths: + new_paths = "$(ExecutablePath);" + ";".join(new_paths) + + properties = {} + for (name, configuration) in sorted(configurations.items()): + condition = _GetConfigurationCondition(name, configuration, spec) + attributes = _GetMSBuildAttributes(spec, configuration, build_file) + msbuild_settings = configuration["finalized_msbuild_settings"] + _AddConditionalProperty( + properties, condition, "IntDir", attributes["IntermediateDirectory"] + ) + _AddConditionalProperty( + properties, condition, "OutDir", attributes["OutputDirectory"] + ) + _AddConditionalProperty( + properties, condition, "TargetName", attributes["TargetName"] + ) + if "TargetExt" in attributes: + _AddConditionalProperty( + properties, condition, "TargetExt", attributes["TargetExt"] + ) + + if attributes.get("TargetPath"): + _AddConditionalProperty( + properties, condition, "TargetPath", attributes["TargetPath"] + ) + if attributes.get("TargetExt"): + _AddConditionalProperty( + properties, condition, "TargetExt", attributes["TargetExt"] + ) + + if new_paths: + _AddConditionalProperty(properties, condition, "ExecutablePath", new_paths) + tool_settings = msbuild_settings.get("", {}) + for name, value in sorted(tool_settings.items()): + formatted_value = _GetValueFormattedForMSBuild("", name, value) + _AddConditionalProperty(properties, condition, name, formatted_value) + return _GetMSBuildPropertyGroup(spec, None, properties) def _AddConditionalProperty(properties, condition, name, value): - """Adds a property / conditional value pair to a dictionary. + """Adds a property / conditional value pair to a dictionary. Arguments: properties: The dictionary to be modified. The key is the name of the @@ -3002,21 +3236,21 @@ def _AddConditionalProperty(properties, condition, name, value): name: The name of the property. value: The value of the property. """ - if name not in properties: - properties[name] = {} - values = properties[name] - if value not in values: - values[value] = [] - conditions = values[value] - conditions.append(condition) + if name not in properties: + properties[name] = {} + values = properties[name] + if value not in values: + values[value] = [] + conditions = values[value] + conditions.append(condition) # Regex for msvs variable references ( i.e. $(FOO) ). -MSVS_VARIABLE_REFERENCE = re.compile(r'\$\(([a-zA-Z_][a-zA-Z0-9_]*)\)') +MSVS_VARIABLE_REFERENCE = re.compile(r"\$\(([a-zA-Z_][a-zA-Z0-9_]*)\)") def _GetMSBuildPropertyGroup(spec, label, properties): - """Returns a PropertyGroup definition for the specified properties. + """Returns a PropertyGroup definition for the specified properties. Arguments: spec: The target project dict. @@ -3025,193 +3259,215 @@ def _GetMSBuildPropertyGroup(spec, label, properties): property. The value is itself a dictionary; its key is the value and the value a list of condition for which this value is true. """ - group = ['PropertyGroup'] - if label: - group.append({'Label': label}) - num_configurations = len(spec['configurations']) - def GetEdges(node): - # Use a definition of edges such that user_of_variable -> used_varible. - # This happens to be easier in this case, since a variable's - # definition contains all variables it references in a single string. - edges = set() - for value in sorted(properties[node].keys()): - # Add to edges all $(...) references to variables. - # - # Variable references that refer to names not in properties are excluded - # These can exist for instance to refer built in definitions like - # $(SolutionDir). - # - # Self references are ignored. Self reference is used in a few places to - # append to the default value. I.e. PATH=$(PATH);other_path - edges.update(set([v for v in MSVS_VARIABLE_REFERENCE.findall(value) - if v in properties and v != node])) - return edges - properties_ordered = gyp.common.TopologicallySorted( - properties.keys(), GetEdges) - # Walk properties in the reverse of a topological sort on - # user_of_variable -> used_variable as this ensures variables are - # defined before they are used. - # NOTE: reverse(topsort(DAG)) = topsort(reverse_edges(DAG)) - for name in reversed(properties_ordered): - values = properties[name] - for value, conditions in sorted(values.items()): - if len(conditions) == num_configurations: - # If the value is the same all configurations, - # just add one unconditional entry. - group.append([name, value]) - else: - for condition in conditions: - group.append([name, {'Condition': condition}, value]) - return [group] + group = ["PropertyGroup"] + if label: + group.append({"Label": label}) + num_configurations = len(spec["configurations"]) + + def GetEdges(node): + # Use a definition of edges such that user_of_variable -> used_varible. + # This happens to be easier in this case, since a variable's + # definition contains all variables it references in a single string. + edges = set() + for value in sorted(properties[node].keys()): + # Add to edges all $(...) references to variables. + # + # Variable references that refer to names not in properties are excluded + # These can exist for instance to refer built in definitions like + # $(SolutionDir). + # + # Self references are ignored. Self reference is used in a few places to + # append to the default value. I.e. PATH=$(PATH);other_path + edges.update( + set( + [ + v + for v in MSVS_VARIABLE_REFERENCE.findall(value) + if v in properties and v != node + ] + ) + ) + return edges + + properties_ordered = gyp.common.TopologicallySorted(properties.keys(), GetEdges) + # Walk properties in the reverse of a topological sort on + # user_of_variable -> used_variable as this ensures variables are + # defined before they are used. + # NOTE: reverse(topsort(DAG)) = topsort(reverse_edges(DAG)) + for name in reversed(properties_ordered): + values = properties[name] + for value, conditions in sorted(values.items()): + if len(conditions) == num_configurations: + # If the value is the same all configurations, + # just add one unconditional entry. + group.append([name, value]) + else: + for condition in conditions: + group.append([name, {"Condition": condition}, value]) + return [group] def _GetMSBuildToolSettingsSections(spec, configurations): - groups = [] - for (name, configuration) in sorted(configurations.items()): - msbuild_settings = configuration['finalized_msbuild_settings'] - group = ['ItemDefinitionGroup', - {'Condition': - _GetConfigurationCondition(name, configuration, spec) - } - ] - for tool_name, tool_settings in sorted(msbuild_settings.items()): - # Skip the tool named '' which is a holder of global settings handled - # by _GetMSBuildConfigurationGlobalProperties. - if tool_name: - if tool_settings: - tool = [tool_name] - for name, value in sorted(tool_settings.items()): - formatted_value = _GetValueFormattedForMSBuild(tool_name, name, - value) - tool.append([name, formatted_value]) - group.append(tool) - groups.append(group) - return groups + groups = [] + for (name, configuration) in sorted(configurations.items()): + msbuild_settings = configuration["finalized_msbuild_settings"] + group = [ + "ItemDefinitionGroup", + {"Condition": _GetConfigurationCondition(name, configuration, spec)}, + ] + for tool_name, tool_settings in sorted(msbuild_settings.items()): + # Skip the tool named '' which is a holder of global settings handled + # by _GetMSBuildConfigurationGlobalProperties. + if tool_name: + if tool_settings: + tool = [tool_name] + for name, value in sorted(tool_settings.items()): + formatted_value = _GetValueFormattedForMSBuild( + tool_name, name, value + ) + tool.append([name, formatted_value]) + group.append(tool) + groups.append(group) + return groups def _FinalizeMSBuildSettings(spec, configuration): - if 'msbuild_settings' in configuration: - converted = False - msbuild_settings = configuration['msbuild_settings'] - MSVSSettings.ValidateMSBuildSettings(msbuild_settings) - else: - converted = True - msvs_settings = configuration.get('msvs_settings', {}) - msbuild_settings = MSVSSettings.ConvertToMSBuildSettings(msvs_settings) - include_dirs, midl_include_dirs, resource_include_dirs = \ - _GetIncludeDirs(configuration) - libraries = _GetLibraries(spec) - library_dirs = _GetLibraryDirs(configuration) - out_file, _, msbuild_tool = _GetOutputFilePathAndTool(spec, msbuild=True) - target_ext = _GetOutputTargetExt(spec) - defines = _GetDefines(configuration) - if converted: - # Visual Studio 2010 has TR1 - defines = [d for d in defines if d != '_HAS_TR1=0'] - # Warn of ignored settings - ignored_settings = ['msvs_tool_files'] - for ignored_setting in ignored_settings: - value = configuration.get(ignored_setting) - if value: - print('Warning: The automatic conversion to MSBuild does not handle ' - '%s. Ignoring setting of %s' % (ignored_setting, str(value))) - - defines = [_EscapeCppDefineForMSBuild(d) for d in defines] - disabled_warnings = _GetDisabledWarnings(configuration) - prebuild = configuration.get('msvs_prebuild') - postbuild = configuration.get('msvs_postbuild') - def_file = _GetModuleDefinition(spec) - precompiled_header = configuration.get('msvs_precompiled_header') - - # Add the information to the appropriate tool - # TODO(jeanluc) We could optimize and generate these settings only if - # the corresponding files are found, e.g. don't generate ResourceCompile - # if you don't have any resources. - _ToolAppend(msbuild_settings, 'ClCompile', - 'AdditionalIncludeDirectories', include_dirs) - _ToolAppend(msbuild_settings, 'Midl', - 'AdditionalIncludeDirectories', midl_include_dirs) - _ToolAppend(msbuild_settings, 'ResourceCompile', - 'AdditionalIncludeDirectories', resource_include_dirs) - # Add in libraries, note that even for empty libraries, we want this - # set, to prevent inheriting default libraries from the environment. - _ToolSetOrAppend(msbuild_settings, 'Link', 'AdditionalDependencies', - libraries) - _ToolAppend(msbuild_settings, 'Link', 'AdditionalLibraryDirectories', - library_dirs) - if out_file: - _ToolAppend(msbuild_settings, msbuild_tool, 'OutputFile', out_file, - only_if_unset=True) - if target_ext: - _ToolAppend(msbuild_settings, msbuild_tool, 'TargetExt', target_ext, - only_if_unset=True) - # Add defines. - _ToolAppend(msbuild_settings, 'ClCompile', - 'PreprocessorDefinitions', defines) - _ToolAppend(msbuild_settings, 'ResourceCompile', - 'PreprocessorDefinitions', defines) - # Add disabled warnings. - _ToolAppend(msbuild_settings, 'ClCompile', - 'DisableSpecificWarnings', disabled_warnings) - # Turn on precompiled headers if appropriate. - if precompiled_header: - precompiled_header = os.path.split(precompiled_header)[1] - _ToolAppend(msbuild_settings, 'ClCompile', 'PrecompiledHeader', 'Use') - _ToolAppend(msbuild_settings, 'ClCompile', - 'PrecompiledHeaderFile', precompiled_header) - _ToolAppend(msbuild_settings, 'ClCompile', - 'ForcedIncludeFiles', [precompiled_header]) - else: - _ToolAppend(msbuild_settings, 'ClCompile', 'PrecompiledHeader', 'NotUsing') - # Turn off WinRT compilation - _ToolAppend(msbuild_settings, 'ClCompile', 'CompileAsWinRT', 'false') - # Turn on import libraries if appropriate - if spec.get('msvs_requires_importlibrary'): - _ToolAppend(msbuild_settings, '', 'IgnoreImportLibrary', 'false') - # Loadable modules don't generate import libraries; - # tell dependent projects to not expect one. - if spec['type'] == 'loadable_module': - _ToolAppend(msbuild_settings, '', 'IgnoreImportLibrary', 'true') - # Set the module definition file if any. - if def_file: - _ToolAppend(msbuild_settings, 'Link', 'ModuleDefinitionFile', def_file) - configuration['finalized_msbuild_settings'] = msbuild_settings - if prebuild: - _ToolAppend(msbuild_settings, 'PreBuildEvent', 'Command', prebuild) - if postbuild: - _ToolAppend(msbuild_settings, 'PostBuildEvent', 'Command', postbuild) + if "msbuild_settings" in configuration: + converted = False + msbuild_settings = configuration["msbuild_settings"] + MSVSSettings.ValidateMSBuildSettings(msbuild_settings) + else: + converted = True + msvs_settings = configuration.get("msvs_settings", {}) + msbuild_settings = MSVSSettings.ConvertToMSBuildSettings(msvs_settings) + include_dirs, midl_include_dirs, resource_include_dirs = _GetIncludeDirs( + configuration + ) + libraries = _GetLibraries(spec) + library_dirs = _GetLibraryDirs(configuration) + out_file, _, msbuild_tool = _GetOutputFilePathAndTool(spec, msbuild=True) + target_ext = _GetOutputTargetExt(spec) + defines = _GetDefines(configuration) + if converted: + # Visual Studio 2010 has TR1 + defines = [d for d in defines if d != "_HAS_TR1=0"] + # Warn of ignored settings + ignored_settings = ["msvs_tool_files"] + for ignored_setting in ignored_settings: + value = configuration.get(ignored_setting) + if value: + print( + "Warning: The automatic conversion to MSBuild does not handle " + "%s. Ignoring setting of %s" % (ignored_setting, str(value)) + ) + + defines = [_EscapeCppDefineForMSBuild(d) for d in defines] + disabled_warnings = _GetDisabledWarnings(configuration) + prebuild = configuration.get("msvs_prebuild") + postbuild = configuration.get("msvs_postbuild") + def_file = _GetModuleDefinition(spec) + precompiled_header = configuration.get("msvs_precompiled_header") + + # Add the information to the appropriate tool + # TODO(jeanluc) We could optimize and generate these settings only if + # the corresponding files are found, e.g. don't generate ResourceCompile + # if you don't have any resources. + _ToolAppend( + msbuild_settings, "ClCompile", "AdditionalIncludeDirectories", include_dirs + ) + _ToolAppend( + msbuild_settings, "Midl", "AdditionalIncludeDirectories", midl_include_dirs + ) + _ToolAppend( + msbuild_settings, + "ResourceCompile", + "AdditionalIncludeDirectories", + resource_include_dirs, + ) + # Add in libraries, note that even for empty libraries, we want this + # set, to prevent inheriting default libraries from the environment. + _ToolSetOrAppend(msbuild_settings, "Link", "AdditionalDependencies", libraries) + _ToolAppend(msbuild_settings, "Link", "AdditionalLibraryDirectories", library_dirs) + if out_file: + _ToolAppend( + msbuild_settings, msbuild_tool, "OutputFile", out_file, only_if_unset=True + ) + if target_ext: + _ToolAppend( + msbuild_settings, msbuild_tool, "TargetExt", target_ext, only_if_unset=True + ) + # Add defines. + _ToolAppend(msbuild_settings, "ClCompile", "PreprocessorDefinitions", defines) + _ToolAppend(msbuild_settings, "ResourceCompile", "PreprocessorDefinitions", defines) + # Add disabled warnings. + _ToolAppend( + msbuild_settings, "ClCompile", "DisableSpecificWarnings", disabled_warnings + ) + # Turn on precompiled headers if appropriate. + if precompiled_header: + precompiled_header = os.path.split(precompiled_header)[1] + _ToolAppend(msbuild_settings, "ClCompile", "PrecompiledHeader", "Use") + _ToolAppend( + msbuild_settings, "ClCompile", "PrecompiledHeaderFile", precompiled_header + ) + _ToolAppend( + msbuild_settings, "ClCompile", "ForcedIncludeFiles", [precompiled_header] + ) + else: + _ToolAppend(msbuild_settings, "ClCompile", "PrecompiledHeader", "NotUsing") + # Turn off WinRT compilation + _ToolAppend(msbuild_settings, "ClCompile", "CompileAsWinRT", "false") + # Turn on import libraries if appropriate + if spec.get("msvs_requires_importlibrary"): + _ToolAppend(msbuild_settings, "", "IgnoreImportLibrary", "false") + # Loadable modules don't generate import libraries; + # tell dependent projects to not expect one. + if spec["type"] == "loadable_module": + _ToolAppend(msbuild_settings, "", "IgnoreImportLibrary", "true") + # Set the module definition file if any. + if def_file: + _ToolAppend(msbuild_settings, "Link", "ModuleDefinitionFile", def_file) + configuration["finalized_msbuild_settings"] = msbuild_settings + if prebuild: + _ToolAppend(msbuild_settings, "PreBuildEvent", "Command", prebuild) + if postbuild: + _ToolAppend(msbuild_settings, "PostBuildEvent", "Command", postbuild) def _GetValueFormattedForMSBuild(tool_name, name, value): - if type(value) == list: - # For some settings, VS2010 does not automatically extends the settings - # TODO(jeanluc) Is this what we want? - if name in ['AdditionalIncludeDirectories', - 'AdditionalLibraryDirectories', - 'AdditionalOptions', - 'DelayLoadDLLs', - 'DisableSpecificWarnings', - 'PreprocessorDefinitions']: - value.append('%%(%s)' % name) - # For most tools, entries in a list should be separated with ';' but some - # settings use a space. Check for those first. - exceptions = { - 'ClCompile': ['AdditionalOptions'], - 'Link': ['AdditionalOptions'], - 'Lib': ['AdditionalOptions']} - if tool_name in exceptions and name in exceptions[tool_name]: - char = ' ' + if type(value) == list: + # For some settings, VS2010 does not automatically extends the settings + # TODO(jeanluc) Is this what we want? + if name in [ + "AdditionalIncludeDirectories", + "AdditionalLibraryDirectories", + "AdditionalOptions", + "DelayLoadDLLs", + "DisableSpecificWarnings", + "PreprocessorDefinitions", + ]: + value.append("%%(%s)" % name) + # For most tools, entries in a list should be separated with ';' but some + # settings use a space. Check for those first. + exceptions = { + "ClCompile": ["AdditionalOptions"], + "Link": ["AdditionalOptions"], + "Lib": ["AdditionalOptions"], + } + if tool_name in exceptions and name in exceptions[tool_name]: + char = " " + else: + char = ";" + formatted_value = char.join( + [MSVSSettings.ConvertVCMacrosToMSBuild(i) for i in value] + ) else: - char = ';' - formatted_value = char.join( - [MSVSSettings.ConvertVCMacrosToMSBuild(i) for i in value]) - else: - formatted_value = MSVSSettings.ConvertVCMacrosToMSBuild(value) - return formatted_value + formatted_value = MSVSSettings.ConvertVCMacrosToMSBuild(value) + return formatted_value def _VerifySourcesExist(sources, root_dir): - """Verifies that all source files exist on disk. + """Verifies that all source files exist on disk. Checks that all regular source files, i.e. not created at run time, exist on disk. Missing files cause needless recompilation but no otherwise @@ -3223,269 +3479,346 @@ def _VerifySourcesExist(sources, root_dir): Returns: A list of source files that cannot be found on disk. """ - missing_sources = [] - for source in sources: - if isinstance(source, MSVSProject.Filter): - missing_sources.extend(_VerifySourcesExist(source.contents, root_dir)) - else: - if '$' not in source: - full_path = os.path.join(root_dir, source) - if not os.path.exists(full_path): - missing_sources.append(full_path) - return missing_sources - - -def _GetMSBuildSources(spec, sources, exclusions, rule_dependencies, - extension_to_rule_name, actions_spec, - sources_handled_by_action, list_excluded): - groups = ['none', 'masm', 'midl', 'include', 'compile', 'resource', 'rule', - 'rule_dependency'] - grouped_sources = {} - for g in groups: - grouped_sources[g] = [] - - _AddSources2(spec, sources, exclusions, grouped_sources, - rule_dependencies, extension_to_rule_name, - sources_handled_by_action, list_excluded) - sources = [] - for g in groups: - if grouped_sources[g]: - sources.append(['ItemGroup'] + grouped_sources[g]) - if actions_spec: - sources.append(['ItemGroup'] + actions_spec) - return sources - - -def _AddSources2(spec, sources, exclusions, grouped_sources, - rule_dependencies, extension_to_rule_name, - sources_handled_by_action, - list_excluded): - extensions_excluded_from_precompile = [] - for source in sources: - if isinstance(source, MSVSProject.Filter): - _AddSources2(spec, source.contents, exclusions, grouped_sources, - rule_dependencies, extension_to_rule_name, - sources_handled_by_action, - list_excluded) - else: - if not source in sources_handled_by_action: - detail = [] - excluded_configurations = exclusions.get(source, []) - if len(excluded_configurations) == len(spec['configurations']): - detail.append(['ExcludedFromBuild', 'true']) + missing_sources = [] + for source in sources: + if isinstance(source, MSVSProject.Filter): + missing_sources.extend(_VerifySourcesExist(source.contents, root_dir)) else: - for config_name, configuration in sorted(excluded_configurations): - condition = _GetConfigurationCondition(config_name, configuration) - detail.append(['ExcludedFromBuild', - {'Condition': condition}, - 'true']) - # Add precompile if needed - for config_name, configuration in spec['configurations'].items(): - precompiled_source = configuration.get('msvs_precompiled_source', '') - if precompiled_source != '': - precompiled_source = _FixPath(precompiled_source) - if not extensions_excluded_from_precompile: - # If the precompiled header is generated by a C source, we must - # not try to use it for C++ sources, and vice versa. - basename, extension = os.path.splitext(precompiled_source) - if extension == '.c': - extensions_excluded_from_precompile = ['.cc', '.cpp', '.cxx'] - else: - extensions_excluded_from_precompile = ['.c'] - - if precompiled_source == source: - condition = _GetConfigurationCondition( - config_name, configuration, spec + if "$" not in source: + full_path = os.path.join(root_dir, source) + if not os.path.exists(full_path): + missing_sources.append(full_path) + return missing_sources + + +def _GetMSBuildSources( + spec, + sources, + exclusions, + rule_dependencies, + extension_to_rule_name, + actions_spec, + sources_handled_by_action, + list_excluded, +): + groups = [ + "none", + "masm", + "midl", + "include", + "compile", + "resource", + "rule", + "rule_dependency", + ] + grouped_sources = {} + for g in groups: + grouped_sources[g] = [] + + _AddSources2( + spec, + sources, + exclusions, + grouped_sources, + rule_dependencies, + extension_to_rule_name, + sources_handled_by_action, + list_excluded, + ) + sources = [] + for g in groups: + if grouped_sources[g]: + sources.append(["ItemGroup"] + grouped_sources[g]) + if actions_spec: + sources.append(["ItemGroup"] + actions_spec) + return sources + + +def _AddSources2( + spec, + sources, + exclusions, + grouped_sources, + rule_dependencies, + extension_to_rule_name, + sources_handled_by_action, + list_excluded, +): + extensions_excluded_from_precompile = [] + for source in sources: + if isinstance(source, MSVSProject.Filter): + _AddSources2( + spec, + source.contents, + exclusions, + grouped_sources, + rule_dependencies, + extension_to_rule_name, + sources_handled_by_action, + list_excluded, ) - detail.append(['PrecompiledHeader', - {'Condition': condition}, - 'Create' - ]) - else: - # Turn off precompiled header usage for source files of a - # different type than the file that generated the - # precompiled header. - for extension in extensions_excluded_from_precompile: - if source.endswith(extension): - detail.append(['PrecompiledHeader', '']) - detail.append(['ForcedIncludeFiles', '']) - - group, element = _MapFileToMsBuildSourceType(source, rule_dependencies, - extension_to_rule_name, - _GetUniquePlatforms(spec)) - grouped_sources[group].append([element, {'Include': source}] + detail) - -def _GetMSBuildProjectReferences(project, spec): - current_configuration = spec['default_configuration'] - references = [] - if project.dependencies: - group = ['ItemGroup'] - added_dependency_set = set() - for dependency in project.dependencies: - dependency_spec = dependency.spec - should_skip_dep = False - if project.spec["toolset"] == 'target': - if dependency_spec['toolset'] == 'host': - if dependency_spec['type'] == 'static_library': - should_skip_dep = True - if dependency.name.startswith('run_'): - should_skip_dep = False - if should_skip_dep: - continue - - canonical_name = dependency.name.replace('_host', '') - added_dependency_set.add(canonical_name) - guid = dependency.guid - project_dir = os.path.split(project.path)[0] - relative_path = gyp.common.RelativePath(dependency.path, project_dir) - project_ref = ['ProjectReference', - {'Include': relative_path}, - ['Project', guid], - ['ReferenceOutputAssembly', 'false'] - ] - for config in dependency.spec.get('configurations', {}).values(): - if config.get('msvs_use_library_dependency_inputs', 0): - project_ref.append(['UseLibraryDependencyInputs', 'true']) - break - # If it's disabled in any config, turn it off in the reference. - if config.get('msvs_2010_disable_uldi_when_referenced', 0): - project_ref.append(['UseLibraryDependencyInputs', 'false']) - break - group.append(project_ref) - references.append(group) - return references + else: + if source not in sources_handled_by_action: + detail = [] + excluded_configurations = exclusions.get(source, []) + if len(excluded_configurations) == len(spec["configurations"]): + detail.append(["ExcludedFromBuild", "true"]) + else: + for config_name, configuration in sorted(excluded_configurations): + condition = _GetConfigurationCondition( + config_name, configuration + ) + detail.append( + ["ExcludedFromBuild", {"Condition": condition}, "true"] + ) + # Add precompile if needed + for config_name, configuration in spec["configurations"].items(): + precompiled_source = configuration.get( + "msvs_precompiled_source", "" + ) + if precompiled_source != "": + precompiled_source = _FixPath(precompiled_source) + if not extensions_excluded_from_precompile: + # If the precompiled header is generated by a C source, + # we must not try to use it for C++ sources, + # and vice versa. + basename, extension = os.path.splitext(precompiled_source) + if extension == ".c": + extensions_excluded_from_precompile = [ + ".cc", + ".cpp", + ".cxx", + ] + else: + extensions_excluded_from_precompile = [".c"] + + if precompiled_source == source: + condition = _GetConfigurationCondition( + config_name, configuration, spec + ) + detail.append( + ["PrecompiledHeader", {"Condition": condition}, "Create"] + ) + else: + # Turn off precompiled header usage for source files of a + # different type than the file that generated the + # precompiled header. + for extension in extensions_excluded_from_precompile: + if source.endswith(extension): + detail.append(["PrecompiledHeader", ""]) + detail.append(["ForcedIncludeFiles", ""]) + + group, element = _MapFileToMsBuildSourceType( + source, + rule_dependencies, + extension_to_rule_name, + _GetUniquePlatforms(spec), + ) + if group == "compile": + # Always add an value to support duplicate + # source file basenames. + file_name = os.path.splitext(source)[0] + ".obj" + if (file_name.startswith("..\\")): + file_name = re.sub(r"^(\.\.\\)+", "", file_name) + elif (file_name.startswith("$(")): + file_name = re.sub(r"^\$\([^)]+\)\\", "", file_name) + detail.append(["ObjectFileName", "$(IntDir)\\" + file_name]) + grouped_sources[group].append([element, {"Include": source}] + detail) + + +def _GetMSBuildProjectReferences(project): + references = [] + if project.dependencies: + group = ["ItemGroup"] + added_dependency_set = set() + for dependency in project.dependencies: + dependency_spec = dependency.spec + should_skip_dep = False + if project.spec["toolset"] == "target": + if dependency_spec["toolset"] == "host": + if dependency_spec["type"] == "static_library": + should_skip_dep = True + if dependency.name.startswith("run_"): + should_skip_dep = False + if should_skip_dep: + continue + + canonical_name = dependency.name.replace("_host", "") + added_dependency_set.add(canonical_name) + guid = dependency.guid + project_dir = os.path.split(project.path)[0] + relative_path = gyp.common.RelativePath(dependency.path, project_dir) + project_ref = [ + "ProjectReference", + {"Include": relative_path}, + ["Project", guid], + ["ReferenceOutputAssembly", "false"], + ] + for config in dependency.spec.get("configurations", {}).values(): + if config.get("msvs_use_library_dependency_inputs", 0): + project_ref.append(["UseLibraryDependencyInputs", "true"]) + break + # If it's disabled in any config, turn it off in the reference. + if config.get("msvs_2010_disable_uldi_when_referenced", 0): + project_ref.append(["UseLibraryDependencyInputs", "false"]) + break + group.append(project_ref) + references.append(group) + return references def _GenerateMSBuildProject(project, options, version, generator_flags, spec): - spec = project.spec - configurations = spec['configurations'] - project_dir, project_file_name = os.path.split(project.path) - gyp.common.EnsureDirExists(project.path) - # Prepare list of sources and excluded sources. - gyp_path = _NormalizedSource(project.build_file) - relative_path_of_gyp_file = gyp.common.RelativePath(gyp_path, project_dir) - - gyp_file = os.path.split(project.build_file)[1] - sources, excluded_sources = _PrepareListOfSources(spec, generator_flags, - gyp_file) - # Add rules. - actions_to_add = {} - props_files_of_rules = set() - targets_files_of_rules = set() - rule_dependencies = set() - extension_to_rule_name = {} - list_excluded = generator_flags.get('msvs_list_excluded_files', True) - - # Don't generate rules if we are using an external builder like ninja. - if not spec.get('msvs_external_builder'): - _GenerateRulesForMSBuild(project_dir, options, spec, - sources, excluded_sources, - props_files_of_rules, targets_files_of_rules, - actions_to_add, rule_dependencies, - extension_to_rule_name) - else: - rules = spec.get('rules', []) - _AdjustSourcesForRules(rules, sources, excluded_sources, True) - - sources, excluded_sources, excluded_idl = ( - _AdjustSourcesAndConvertToFilterHierarchy(spec, options, - project_dir, sources, - excluded_sources, - list_excluded, version)) - - # Don't add actions if we are using an external builder like ninja. - if not spec.get('msvs_external_builder'): - _AddActions(actions_to_add, spec, project.build_file) - _AddCopies(actions_to_add, spec) + spec = project.spec + configurations = spec["configurations"] + toolset = spec["toolset"] + project_dir, project_file_name = os.path.split(project.path) + gyp.common.EnsureDirExists(project.path) + # Prepare list of sources and excluded sources. + + gyp_file = os.path.split(project.build_file)[1] + sources, excluded_sources = _PrepareListOfSources(spec, generator_flags, gyp_file) + # Add rules. + actions_to_add = {} + props_files_of_rules = set() + targets_files_of_rules = set() + rule_dependencies = set() + extension_to_rule_name = {} + list_excluded = generator_flags.get("msvs_list_excluded_files", True) + platforms = _GetUniquePlatforms(spec) + + # Don't generate rules if we are using an external builder like ninja. + if not spec.get("msvs_external_builder"): + _GenerateRulesForMSBuild( + project_dir, + options, + spec, + sources, + excluded_sources, + props_files_of_rules, + targets_files_of_rules, + actions_to_add, + rule_dependencies, + extension_to_rule_name, + ) + else: + rules = spec.get("rules", []) + _AdjustSourcesForRules(rules, sources, excluded_sources, True) + + sources, excluded_sources, excluded_idl = _AdjustSourcesAndConvertToFilterHierarchy( + spec, options, project_dir, sources, excluded_sources, list_excluded, version + ) + + # Don't add actions if we are using an external builder like ninja. + if not spec.get("msvs_external_builder"): + _AddActions(actions_to_add, spec, project.build_file) + _AddCopies(actions_to_add, spec) + + # NOTE: this stanza must appear after all actions have been decided. + # Don't excluded sources with actions attached, or they won't run. + excluded_sources = _FilterActionsFromExcluded(excluded_sources, actions_to_add) + + exclusions = _GetExcludedFilesFromBuild(spec, excluded_sources, excluded_idl) + actions_spec, sources_handled_by_action = _GenerateActionsForMSBuild( + spec, actions_to_add + ) + + _GenerateMSBuildFiltersFile( + project.path + ".filters", + sources, + rule_dependencies, + extension_to_rule_name, + platforms, + ) + missing_sources = _VerifySourcesExist(sources, project_dir) + + for configuration in configurations.values(): + _FinalizeMSBuildSettings(spec, configuration) + + # Add attributes to root element + + import_default_section = [ + ["Import", {"Project": r"$(VCTargetsPath)\Microsoft.Cpp.Default.props"}] + ] + import_cpp_props_section = [ + ["Import", {"Project": r"$(VCTargetsPath)\Microsoft.Cpp.props"}] + ] + import_cpp_targets_section = [ + ["Import", {"Project": r"$(VCTargetsPath)\Microsoft.Cpp.targets"}] + ] + import_masm_props_section = [ + ["Import", {"Project": r"$(VCTargetsPath)\BuildCustomizations\masm.props"}] + ] + import_masm_targets_section = [ + ["Import", {"Project": r"$(VCTargetsPath)\BuildCustomizations\masm.targets"}] + ] + import_marmasm_props_section = [ + ["Import", {"Project": r"$(VCTargetsPath)\BuildCustomizations\marmasm.props"}] + ] + import_marmasm_targets_section = [ + ["Import", {"Project": r"$(VCTargetsPath)\BuildCustomizations\marmasm.targets"}] + ] + macro_section = [["PropertyGroup", {"Label": "UserMacros"}]] + + content = [ + "Project", + { + "xmlns": "http://schemas.microsoft.com/developer/msbuild/2003", + "ToolsVersion": version.ProjectVersion(), + "DefaultTargets": "Build", + }, + ] - # NOTE: this stanza must appear after all actions have been decided. - # Don't excluded sources with actions attached, or they won't run. - excluded_sources = _FilterActionsFromExcluded( - excluded_sources, actions_to_add) - - exclusions = _GetExcludedFilesFromBuild(spec, excluded_sources, excluded_idl) - actions_spec, sources_handled_by_action = _GenerateActionsForMSBuild( - spec, actions_to_add) - - _GenerateMSBuildFiltersFile(project.path + '.filters', sources, - rule_dependencies, - extension_to_rule_name, _GetUniquePlatforms(spec)) - missing_sources = _VerifySourcesExist(sources, project_dir) - - for configuration in configurations.values(): - _FinalizeMSBuildSettings(spec, configuration) - - # Add attributes to root element - - import_default_section = [ - ['Import', {'Project': r'$(VCTargetsPath)\Microsoft.Cpp.Default.props'}]] - import_cpp_props_section = [ - ['Import', {'Project': r'$(VCTargetsPath)\Microsoft.Cpp.props'}]] - import_cpp_targets_section = [ - ['Import', {'Project': r'$(VCTargetsPath)\Microsoft.Cpp.targets'}]] - import_masm_props_section = [ - ['Import', - {'Project': r'$(VCTargetsPath)\BuildCustomizations\masm.props'}]] - import_masm_targets_section = [ - ['Import', - {'Project': r'$(VCTargetsPath)\BuildCustomizations\masm.targets'}]] - import_marmasm_props_section = [ - ['Import', - {'Project': r'$(VCTargetsPath)\BuildCustomizations\marmasm.props'}]] - import_marmasm_targets_section = [ - ['Import', - {'Project': r'$(VCTargetsPath)\BuildCustomizations\marmasm.targets'}]] - macro_section = [['PropertyGroup', {'Label': 'UserMacros'}]] - - content = [ - 'Project', - {'xmlns': 'http://schemas.microsoft.com/developer/msbuild/2003', - 'ToolsVersion': version.ProjectVersion(), - 'DefaultTargets': 'Build' - }] - - content += _GetMSBuildProjectConfigurations(configurations, spec) - content += _GetMSBuildGlobalProperties(spec, version, project.guid, - project_file_name) - content += import_default_section - content += _GetMSBuildConfigurationDetails(spec, project.build_file) - if spec.get('msvs_enable_winphone'): - content += _GetMSBuildLocalProperties('v120_wp81') - else: - content += _GetMSBuildLocalProperties(project.msbuild_toolset) - content += import_cpp_props_section - content += import_masm_props_section - if spec.get('msvs_enable_marmasm') or True: - content += import_marmasm_props_section - content += _GetMSBuildExtensions(props_files_of_rules) - content += _GetMSBuildPropertySheets(configurations, spec) - content += macro_section - content += _GetMSBuildConfigurationGlobalProperties(spec, configurations, - project.build_file) - content += _GetMSBuildToolSettingsSections(spec, configurations) - content += _GetMSBuildSources( - spec, sources, exclusions, rule_dependencies, extension_to_rule_name, - actions_spec, sources_handled_by_action, list_excluded) - content += _GetMSBuildProjectReferences(project, spec) - content += import_cpp_targets_section - content += import_masm_targets_section - if spec.get('msvs_enable_marmasm'): - content += import_marmasm_targets_section - content += _GetMSBuildExtensionTargets(targets_files_of_rules) - - if spec.get('msvs_external_builder'): - content += _GetMSBuildExternalBuilderTargets(spec) - - # TODO(jeanluc) File a bug to get rid of runas. We had in MSVS: - # has_run_as = _WriteMSVSUserFile(project.path, version, spec) - - easy_xml.WriteXmlIfChanged(content, project.path, pretty=True, win32=True) - - return missing_sources + content += _GetMSBuildProjectConfigurations(configurations, spec) + content += _GetMSBuildGlobalProperties( + spec, version, project.guid, project_file_name + ) + content += import_default_section + content += _GetMSBuildConfigurationDetails(spec, project.build_file) + if spec.get("msvs_enable_winphone"): + content += _GetMSBuildLocalProperties("v120_wp81") + else: + content += _GetMSBuildLocalProperties(project.msbuild_toolset) + content += import_cpp_props_section + content += import_masm_props_section + if "arm64" in platforms and toolset == "target": + content += import_marmasm_props_section + content += _GetMSBuildExtensions(props_files_of_rules) + content += _GetMSBuildPropertySheets(configurations, spec) + content += macro_section + content += _GetMSBuildConfigurationGlobalProperties( + spec, configurations, project.build_file + ) + content += _GetMSBuildToolSettingsSections(spec, configurations) + content += _GetMSBuildSources( + spec, + sources, + exclusions, + rule_dependencies, + extension_to_rule_name, + actions_spec, + sources_handled_by_action, + list_excluded, + ) + content += _GetMSBuildProjectReferences(project) + content += import_cpp_targets_section + content += import_masm_targets_section + if "arm64" in platforms and toolset == "target": + content += import_marmasm_targets_section + content += _GetMSBuildExtensionTargets(targets_files_of_rules) + + if spec.get("msvs_external_builder"): + content += _GetMSBuildExternalBuilderTargets(spec) + + # TODO(jeanluc) File a bug to get rid of runas. We had in MSVS: + # has_run_as = _WriteMSVSUserFile(project.path, version, spec) + + easy_xml.WriteXmlIfChanged(content, project.path, pretty=True, win32=True) + + return missing_sources def _GetMSBuildExternalBuilderTargets(spec): - """Return a list of MSBuild targets for external builders. + """Return a list of MSBuild targets for external builders. The "Build" and "Clean" targets are always generated. If the spec contains 'msvs_external_builder_clcompile_cmd', then the "ClCompile" target will also @@ -3496,47 +3829,52 @@ def _GetMSBuildExternalBuilderTargets(spec): Returns: List of MSBuild 'Target' specs. """ - build_cmd = _BuildCommandLineForRuleRaw( - spec, spec['msvs_external_builder_build_cmd'], - False, False, False, False) - build_target = ['Target', {'Name': 'Build'}] - build_target.append(['Exec', {'Command': build_cmd}]) - - clean_cmd = _BuildCommandLineForRuleRaw( - spec, spec['msvs_external_builder_clean_cmd'], - False, False, False, False) - clean_target = ['Target', {'Name': 'Clean'}] - clean_target.append(['Exec', {'Command': clean_cmd}]) - - targets = [build_target, clean_target] - - if spec.get('msvs_external_builder_clcompile_cmd'): - clcompile_cmd = _BuildCommandLineForRuleRaw( - spec, spec['msvs_external_builder_clcompile_cmd'], - False, False, False, False) - clcompile_target = ['Target', {'Name': 'ClCompile'}] - clcompile_target.append(['Exec', {'Command': clcompile_cmd}]) - targets.append(clcompile_target) - - return targets + build_cmd = _BuildCommandLineForRuleRaw( + spec, spec["msvs_external_builder_build_cmd"], False, False, False, False + ) + build_target = ["Target", {"Name": "Build"}] + build_target.append(["Exec", {"Command": build_cmd}]) + + clean_cmd = _BuildCommandLineForRuleRaw( + spec, spec["msvs_external_builder_clean_cmd"], False, False, False, False + ) + clean_target = ["Target", {"Name": "Clean"}] + clean_target.append(["Exec", {"Command": clean_cmd}]) + + targets = [build_target, clean_target] + + if spec.get("msvs_external_builder_clcompile_cmd"): + clcompile_cmd = _BuildCommandLineForRuleRaw( + spec, + spec["msvs_external_builder_clcompile_cmd"], + False, + False, + False, + False, + ) + clcompile_target = ["Target", {"Name": "ClCompile"}] + clcompile_target.append(["Exec", {"Command": clcompile_cmd}]) + targets.append(clcompile_target) + + return targets def _GetMSBuildExtensions(props_files_of_rules): - extensions = ['ImportGroup', {'Label': 'ExtensionSettings'}] - for props_file in props_files_of_rules: - extensions.append(['Import', {'Project': props_file}]) - return [extensions] + extensions = ["ImportGroup", {"Label": "ExtensionSettings"}] + for props_file in props_files_of_rules: + extensions.append(["Import", {"Project": props_file}]) + return [extensions] def _GetMSBuildExtensionTargets(targets_files_of_rules): - targets_node = ['ImportGroup', {'Label': 'ExtensionTargets'}] - for targets_file in sorted(targets_files_of_rules): - targets_node.append(['Import', {'Project': targets_file}]) - return [targets_node] + targets_node = ["ImportGroup", {"Label": "ExtensionTargets"}] + for targets_file in sorted(targets_files_of_rules): + targets_node.append(["Import", {"Project": targets_file}]) + return [targets_node] def _GenerateActionsForMSBuild(spec, actions_to_add): - """Add actions accumulated into an actions_to_add, merging as needed. + """Add actions accumulated into an actions_to_add, merging as needed. Arguments: spec: the target project dict @@ -3546,73 +3884,87 @@ def _GenerateActionsForMSBuild(spec, actions_to_add): Returns: A pair of (action specification, the sources handled by this action). """ - sources_handled_by_action = OrderedSet() - actions_spec = [] - for primary_input, actions in actions_to_add.items(): - if generator_supports_multiple_toolsets: - primary_input = primary_input.replace(".exe", "_host.exe") - inputs = OrderedSet() - outputs = OrderedSet() - descriptions = [] - commands = [] - for action in actions: - def fixup_host_exe(i): - if "$(OutDir)" in i: - i = i.replace('.exe', '_host.exe') - return i - if generator_supports_multiple_toolsets: - action['inputs'] = [fixup_host_exe(i) for i in action['inputs']] - inputs.update(OrderedSet(action['inputs'])) - outputs.update(OrderedSet(action['outputs'])) - descriptions.append(action['description']) - cmd = action['command'] - if generator_supports_multiple_toolsets: - cmd = cmd.replace('.exe', "_host.exe") - # For most actions, add 'call' so that actions that invoke batch files - # return and continue executing. msbuild_use_call provides a way to - # disable this but I have not seen any adverse effect from doing that - # for everything. - if action.get('msbuild_use_call', True): - cmd = 'call ' + cmd - commands.append(cmd) - # Add the custom build action for one input file. - description = ', and also '.join(descriptions) - - # We can't join the commands simply with && because the command line will - # get too long. See also _AddActions: cygwin's setup_env mustn't be called - # for every invocation or the command that sets the PATH will grow too - # long. - command = '\r\n'.join([c + '\r\nif %errorlevel% neq 0 exit /b %errorlevel%' - for c in commands]) - _AddMSBuildAction(spec, - primary_input, - inputs, - outputs, - command, - description, - sources_handled_by_action, - actions_spec) - return actions_spec, sources_handled_by_action - - -def _AddMSBuildAction(spec, primary_input, inputs, outputs, cmd, description, - sources_handled_by_action, actions_spec): - command = MSVSSettings.ConvertVCMacrosToMSBuild(cmd) - primary_input = _FixPath(primary_input) - inputs_array = _FixPaths(inputs) - outputs_array = _FixPaths(outputs) - additional_inputs = ';'.join([i for i in inputs_array - if i != primary_input]) - outputs = ';'.join(outputs_array) - sources_handled_by_action.add(primary_input) - action_spec = ['CustomBuild', {'Include': primary_input}] - action_spec.extend( - # TODO(jeanluc) 'Document' for all or just if as_sources? - [['FileType', 'Document'], - ['Command', command], - ['Message', description], - ['Outputs', outputs] - ]) - if additional_inputs: - action_spec.append(['AdditionalInputs', additional_inputs]) - actions_spec.append(action_spec) + sources_handled_by_action = OrderedSet() + actions_spec = [] + for primary_input, actions in actions_to_add.items(): + if generator_supports_multiple_toolsets: + primary_input = primary_input.replace(".exe", "_host.exe") + inputs = OrderedSet() + outputs = OrderedSet() + descriptions = [] + commands = [] + for action in actions: + + def fixup_host_exe(i): + if "$(OutDir)" in i: + i = i.replace(".exe", "_host.exe") + return i + + if generator_supports_multiple_toolsets: + action["inputs"] = [fixup_host_exe(i) for i in action["inputs"]] + inputs.update(OrderedSet(action["inputs"])) + outputs.update(OrderedSet(action["outputs"])) + descriptions.append(action["description"]) + cmd = action["command"] + if generator_supports_multiple_toolsets: + cmd = cmd.replace(".exe", "_host.exe") + # For most actions, add 'call' so that actions that invoke batch files + # return and continue executing. msbuild_use_call provides a way to + # disable this but I have not seen any adverse effect from doing that + # for everything. + if action.get("msbuild_use_call", True): + cmd = "call " + cmd + commands.append(cmd) + # Add the custom build action for one input file. + description = ", and also ".join(descriptions) + + # We can't join the commands simply with && because the command line will + # get too long. See also _AddActions: cygwin's setup_env mustn't be called + # for every invocation or the command that sets the PATH will grow too + # long. + command = "\r\n".join( + [c + "\r\nif %errorlevel% neq 0 exit /b %errorlevel%" for c in commands] + ) + _AddMSBuildAction( + spec, + primary_input, + inputs, + outputs, + command, + description, + sources_handled_by_action, + actions_spec, + ) + return actions_spec, sources_handled_by_action + + +def _AddMSBuildAction( + spec, + primary_input, + inputs, + outputs, + cmd, + description, + sources_handled_by_action, + actions_spec, +): + command = MSVSSettings.ConvertVCMacrosToMSBuild(cmd) + primary_input = _FixPath(primary_input) + inputs_array = _FixPaths(inputs) + outputs_array = _FixPaths(outputs) + additional_inputs = ";".join([i for i in inputs_array if i != primary_input]) + outputs = ";".join(outputs_array) + sources_handled_by_action.add(primary_input) + action_spec = ["CustomBuild", {"Include": primary_input}] + action_spec.extend( + # TODO(jeanluc) 'Document' for all or just if as_sources? + [ + ["FileType", "Document"], + ["Command", command], + ["Message", description], + ["Outputs", outputs], + ] + ) + if additional_inputs: + action_spec.append(["AdditionalInputs", additional_inputs]) + actions_spec.append(action_spec) diff --git a/tools/gyp/pylib/gyp/generator/msvs_test.py b/tools/gyp/pylib/gyp/generator/msvs_test.py index 1b0cdd17201d5b..e001f417d53aa5 100755 --- a/tools/gyp/pylib/gyp/generator/msvs_test.py +++ b/tools/gyp/pylib/gyp/generator/msvs_test.py @@ -9,33 +9,39 @@ import unittest try: - from StringIO import StringIO # Python 2 + from StringIO import StringIO # Python 2 except ImportError: - from io import StringIO # Python 3 + from io import StringIO # Python 3 class TestSequenceFunctions(unittest.TestCase): - - def setUp(self): - self.stderr = StringIO() - - def test_GetLibraries(self): - self.assertEqual( - msvs._GetLibraries({}), - []) - self.assertEqual( - msvs._GetLibraries({'libraries': []}), - []) - self.assertEqual( - msvs._GetLibraries({'other':'foo', 'libraries': ['a.lib']}), - ['a.lib']) - self.assertEqual( - msvs._GetLibraries({'libraries': ['-la']}), - ['a.lib']) - self.assertEqual( - msvs._GetLibraries({'libraries': ['a.lib', 'b.lib', 'c.lib', '-lb.lib', - '-lb.lib', 'd.lib', 'a.lib']}), - ['c.lib', 'b.lib', 'd.lib', 'a.lib']) - -if __name__ == '__main__': - unittest.main() + def setUp(self): + self.stderr = StringIO() + + def test_GetLibraries(self): + self.assertEqual(msvs._GetLibraries({}), []) + self.assertEqual(msvs._GetLibraries({"libraries": []}), []) + self.assertEqual( + msvs._GetLibraries({"other": "foo", "libraries": ["a.lib"]}), ["a.lib"] + ) + self.assertEqual(msvs._GetLibraries({"libraries": ["-la"]}), ["a.lib"]) + self.assertEqual( + msvs._GetLibraries( + { + "libraries": [ + "a.lib", + "b.lib", + "c.lib", + "-lb.lib", + "-lb.lib", + "d.lib", + "a.lib", + ] + } + ), + ["c.lib", "b.lib", "d.lib", "a.lib"], + ) + + +if __name__ == "__main__": + unittest.main() diff --git a/tools/gyp/pylib/gyp/generator/ninja.py b/tools/gyp/pylib/gyp/generator/ninja.py index d5006bf84a0b2a..e064bad7ed8404 100644 --- a/tools/gyp/pylib/gyp/generator/ninja.py +++ b/tools/gyp/pylib/gyp/generator/ninja.py @@ -16,49 +16,47 @@ import sys import gyp import gyp.common -from gyp.common import OrderedSet import gyp.msvs_emulation import gyp.MSVSUtil as MSVSUtil import gyp.xcode_emulation + try: - from cStringIO import StringIO + from cStringIO import StringIO except ImportError: - from io import StringIO + from io import StringIO from gyp.common import GetEnvironFallback import gyp.ninja_syntax as ninja_syntax generator_default_variables = { - 'EXECUTABLE_PREFIX': '', - 'EXECUTABLE_SUFFIX': '', - 'STATIC_LIB_PREFIX': 'lib', - 'STATIC_LIB_SUFFIX': '.a', - 'SHARED_LIB_PREFIX': 'lib', - - # Gyp expects the following variables to be expandable by the build - # system to the appropriate locations. Ninja prefers paths to be - # known at gyp time. To resolve this, introduce special - # variables starting with $! and $| (which begin with a $ so gyp knows it - # should be treated specially, but is otherwise an invalid - # ninja/shell variable) that are passed to gyp here but expanded - # before writing out into the target .ninja files; see - # ExpandSpecial. - # $! is used for variables that represent a path and that can only appear at - # the start of a string, while $| is used for variables that can appear - # anywhere in a string. - 'INTERMEDIATE_DIR': '$!INTERMEDIATE_DIR', - 'SHARED_INTERMEDIATE_DIR': '$!PRODUCT_DIR/gen', - 'PRODUCT_DIR': '$!PRODUCT_DIR', - 'CONFIGURATION_NAME': '$|CONFIGURATION_NAME', - - # Special variables that may be used by gyp 'rule' targets. - # We generate definitions for these variables on the fly when processing a - # rule. - 'RULE_INPUT_ROOT': '${root}', - 'RULE_INPUT_DIRNAME': '${dirname}', - 'RULE_INPUT_PATH': '${source}', - 'RULE_INPUT_EXT': '${ext}', - 'RULE_INPUT_NAME': '${name}', + "EXECUTABLE_PREFIX": "", + "EXECUTABLE_SUFFIX": "", + "STATIC_LIB_PREFIX": "lib", + "STATIC_LIB_SUFFIX": ".a", + "SHARED_LIB_PREFIX": "lib", + # Gyp expects the following variables to be expandable by the build + # system to the appropriate locations. Ninja prefers paths to be + # known at gyp time. To resolve this, introduce special + # variables starting with $! and $| (which begin with a $ so gyp knows it + # should be treated specially, but is otherwise an invalid + # ninja/shell variable) that are passed to gyp here but expanded + # before writing out into the target .ninja files; see + # ExpandSpecial. + # $! is used for variables that represent a path and that can only appear at + # the start of a string, while $| is used for variables that can appear + # anywhere in a string. + "INTERMEDIATE_DIR": "$!INTERMEDIATE_DIR", + "SHARED_INTERMEDIATE_DIR": "$!PRODUCT_DIR/gen", + "PRODUCT_DIR": "$!PRODUCT_DIR", + "CONFIGURATION_NAME": "$|CONFIGURATION_NAME", + # Special variables that may be used by gyp 'rule' targets. + # We generate definitions for these variables on the fly when processing a + # rule. + "RULE_INPUT_ROOT": "${root}", + "RULE_INPUT_DIRNAME": "${dirname}", + "RULE_INPUT_PATH": "${source}", + "RULE_INPUT_EXT": "${ext}", + "RULE_INPUT_NAME": "${name}", } # Placates pylint. @@ -69,42 +67,43 @@ generator_supports_multiple_toolsets = gyp.common.CrossCompileRequested() + def StripPrefix(arg, prefix): - if arg.startswith(prefix): - return arg[len(prefix):] - return arg + if arg.startswith(prefix): + return arg[len(prefix) :] + return arg def QuoteShellArgument(arg, flavor): - """Quote a string such that it will be interpreted as a single argument + """Quote a string such that it will be interpreted as a single argument by the shell.""" - # Rather than attempting to enumerate the bad shell characters, just - # whitelist common OK ones and quote anything else. - if re.match(r'^[a-zA-Z0-9_=.\\/-]+$', arg): - return arg # No quoting necessary. - if flavor == 'win': - return gyp.msvs_emulation.QuoteForRspFile(arg) - return "'" + arg.replace("'", "'" + '"\'"' + "'") + "'" + # Rather than attempting to enumerate the bad shell characters, just + # allow common OK ones and quote anything else. + if re.match(r"^[a-zA-Z0-9_=.\\/-]+$", arg): + return arg # No quoting necessary. + if flavor == "win": + return gyp.msvs_emulation.QuoteForRspFile(arg) + return "'" + arg.replace("'", "'" + '"\'"' + "'") + "'" def Define(d, flavor): - """Takes a preprocessor define and returns a -D parameter that's ninja- and + """Takes a preprocessor define and returns a -D parameter that's ninja- and shell-escaped.""" - if flavor == 'win': - # cl.exe replaces literal # characters with = in preprocesor definitions for - # some reason. Octal-encode to work around that. - d = d.replace('#', '\\%03o' % ord('#')) - return QuoteShellArgument(ninja_syntax.escape('-D' + d), flavor) + if flavor == "win": + # cl.exe replaces literal # characters with = in preprocessor definitions for + # some reason. Octal-encode to work around that. + d = d.replace("#", "\\%03o" % ord("#")) + return QuoteShellArgument(ninja_syntax.escape("-D" + d), flavor) def AddArch(output, arch): - """Adds an arch string to an output path.""" - output, extension = os.path.splitext(output) - return '%s.%s%s' % (output, arch, extension) + """Adds an arch string to an output path.""" + output, extension = os.path.splitext(output) + return "%s.%s%s" % (output, arch, extension) class Target(object): - """Target represents the paths used within a single gyp target. + """Target represents the paths used within a single gyp target. Conceptually, building a single target A is a series of steps: @@ -126,67 +125,68 @@ class Target(object): variables only store concrete paths to single files, while methods compute derived values like "the last output of the target". """ - def __init__(self, type): - # Gyp type ("static_library", etc.) of this target. - self.type = type - # File representing whether any input dependencies necessary for - # dependent actions have completed. - self.preaction_stamp = None - # File representing whether any input dependencies necessary for - # dependent compiles have completed. - self.precompile_stamp = None - # File representing the completion of actions/rules/copies, if any. - self.actions_stamp = None - # Path to the output of the link step, if any. - self.binary = None - # Path to the file representing the completion of building the bundle, - # if any. - self.bundle = None - # On Windows, incremental linking requires linking against all the .objs - # that compose a .lib (rather than the .lib itself). That list is stored - # here. In this case, we also need to save the compile_deps for the target, - # so that the target that directly depends on the .objs can also depend - # on those. - self.component_objs = None - self.compile_deps = None - # Windows only. The import .lib is the output of a build step, but - # because dependents only link against the lib (not both the lib and the - # dll) we keep track of the import library here. - self.import_lib = None - # Track if this target contains any C++ files, to decide if gcc or g++ - # should be used for linking. - self.uses_cpp = False - - def Linkable(self): - """Return true if this is a target that can be linked against.""" - return self.type in ('static_library', 'shared_library') - - def UsesToc(self, flavor): - """Return true if the target should produce a restat rule based on a TOC + + def __init__(self, type): + # Gyp type ("static_library", etc.) of this target. + self.type = type + # File representing whether any input dependencies necessary for + # dependent actions have completed. + self.preaction_stamp = None + # File representing whether any input dependencies necessary for + # dependent compiles have completed. + self.precompile_stamp = None + # File representing the completion of actions/rules/copies, if any. + self.actions_stamp = None + # Path to the output of the link step, if any. + self.binary = None + # Path to the file representing the completion of building the bundle, + # if any. + self.bundle = None + # On Windows, incremental linking requires linking against all the .objs + # that compose a .lib (rather than the .lib itself). That list is stored + # here. In this case, we also need to save the compile_deps for the target, + # so that the target that directly depends on the .objs can also depend + # on those. + self.component_objs = None + self.compile_deps = None + # Windows only. The import .lib is the output of a build step, but + # because dependents only link against the lib (not both the lib and the + # dll) we keep track of the import library here. + self.import_lib = None + # Track if this target contains any C++ files, to decide if gcc or g++ + # should be used for linking. + self.uses_cpp = False + + def Linkable(self): + """Return true if this is a target that can be linked against.""" + return self.type in ("static_library", "shared_library") + + def UsesToc(self, flavor): + """Return true if the target should produce a restat rule based on a TOC file.""" - # For bundles, the .TOC should be produced for the binary, not for - # FinalOutput(). But the naive approach would put the TOC file into the - # bundle, so don't do this for bundles for now. - if flavor == 'win' or self.bundle: - return False - return self.type in ('shared_library', 'loadable_module') - - def PreActionInput(self, flavor): - """Return the path, if any, that should be used as a dependency of + # For bundles, the .TOC should be produced for the binary, not for + # FinalOutput(). But the naive approach would put the TOC file into the + # bundle, so don't do this for bundles for now. + if flavor == "win" or self.bundle: + return False + return self.type in ("shared_library", "loadable_module") + + def PreActionInput(self, flavor): + """Return the path, if any, that should be used as a dependency of any dependent action step.""" - if self.UsesToc(flavor): - return self.FinalOutput() + '.TOC' - return self.FinalOutput() or self.preaction_stamp + if self.UsesToc(flavor): + return self.FinalOutput() + ".TOC" + return self.FinalOutput() or self.preaction_stamp - def PreCompileInput(self): - """Return the path, if any, that should be used as a dependency of + def PreCompileInput(self): + """Return the path, if any, that should be used as a dependency of any dependent compile step.""" - return self.actions_stamp or self.precompile_stamp + return self.actions_stamp or self.precompile_stamp - def FinalOutput(self): - """Return the last output of the target, which depends on all prior + def FinalOutput(self): + """Return the last output of the target, which depends on all prior steps.""" - return self.bundle or self.binary or self.actions_stamp + return self.bundle or self.binary or self.actions_stamp # A small discourse on paths as used within the Ninja build: @@ -213,108 +213,116 @@ def FinalOutput(self): # an output file; the result can be namespaced such that it is unique # to the input file name as well as the output target name. + class NinjaWriter(object): - def __init__(self, hash_for_rules, target_outputs, base_dir, build_dir, - output_file, toplevel_build, output_file_name, flavor, - toplevel_dir=None): - """ + def __init__( + self, + hash_for_rules, + target_outputs, + base_dir, + build_dir, + output_file, + toplevel_build, + output_file_name, + flavor, + toplevel_dir=None, + ): + """ base_dir: path from source root to directory containing this gyp file, by gyp semantics, all input paths are relative to this build_dir: path from source root to build output toplevel_dir: path to the toplevel directory """ - self.hash_for_rules = hash_for_rules - self.target_outputs = target_outputs - self.base_dir = base_dir - self.build_dir = build_dir - self.ninja = ninja_syntax.Writer(output_file) - self.toplevel_build = toplevel_build - self.output_file_name = output_file_name - - self.flavor = flavor - self.abs_build_dir = None - if toplevel_dir is not None: - self.abs_build_dir = os.path.abspath(os.path.join(toplevel_dir, - build_dir)) - self.obj_ext = '.obj' if flavor == 'win' else '.o' - if flavor == 'win': - # See docstring of msvs_emulation.GenerateEnvironmentFiles(). - self.win_env = {} - for arch in ('x86', 'x64'): - self.win_env[arch] = 'environment.' + arch - - # Relative path from build output dir to base dir. - build_to_top = gyp.common.InvertRelativePath(build_dir, toplevel_dir) - self.build_to_base = os.path.join(build_to_top, base_dir) - # Relative path from base dir to build dir. - base_to_top = gyp.common.InvertRelativePath(base_dir, toplevel_dir) - self.base_to_build = os.path.join(base_to_top, build_dir) - - def ExpandSpecial(self, path, product_dir=None): - """Expand specials like $!PRODUCT_DIR in |path|. + self.hash_for_rules = hash_for_rules + self.target_outputs = target_outputs + self.base_dir = base_dir + self.build_dir = build_dir + self.ninja = ninja_syntax.Writer(output_file) + self.toplevel_build = toplevel_build + self.output_file_name = output_file_name + + self.flavor = flavor + self.abs_build_dir = None + if toplevel_dir is not None: + self.abs_build_dir = os.path.abspath(os.path.join(toplevel_dir, build_dir)) + self.obj_ext = ".obj" if flavor == "win" else ".o" + if flavor == "win": + # See docstring of msvs_emulation.GenerateEnvironmentFiles(). + self.win_env = {} + for arch in ("x86", "x64"): + self.win_env[arch] = "environment." + arch + + # Relative path from build output dir to base dir. + build_to_top = gyp.common.InvertRelativePath(build_dir, toplevel_dir) + self.build_to_base = os.path.join(build_to_top, base_dir) + # Relative path from base dir to build dir. + base_to_top = gyp.common.InvertRelativePath(base_dir, toplevel_dir) + self.base_to_build = os.path.join(base_to_top, build_dir) + + def ExpandSpecial(self, path, product_dir=None): + """Expand specials like $!PRODUCT_DIR in |path|. If |product_dir| is None, assumes the cwd is already the product dir. Otherwise, |product_dir| is the relative path to the product dir. """ - PRODUCT_DIR = '$!PRODUCT_DIR' - if PRODUCT_DIR in path: - if product_dir: - path = path.replace(PRODUCT_DIR, product_dir) - else: - path = path.replace(PRODUCT_DIR + '/', '') - path = path.replace(PRODUCT_DIR + '\\', '') - path = path.replace(PRODUCT_DIR, '.') - - INTERMEDIATE_DIR = '$!INTERMEDIATE_DIR' - if INTERMEDIATE_DIR in path: - int_dir = self.GypPathToUniqueOutput('gen') - # GypPathToUniqueOutput generates a path relative to the product dir, - # so insert product_dir in front if it is provided. - path = path.replace(INTERMEDIATE_DIR, - os.path.join(product_dir or '', int_dir)) - - CONFIGURATION_NAME = '$|CONFIGURATION_NAME' - path = path.replace(CONFIGURATION_NAME, self.config_name) - - return path - - def ExpandRuleVariables(self, path, root, dirname, source, ext, name): - if self.flavor == 'win': - path = self.msvs_settings.ConvertVSMacros( - path, config=self.config_name) - path = path.replace(generator_default_variables['RULE_INPUT_ROOT'], root) - path = path.replace(generator_default_variables['RULE_INPUT_DIRNAME'], - dirname) - path = path.replace(generator_default_variables['RULE_INPUT_PATH'], source) - path = path.replace(generator_default_variables['RULE_INPUT_EXT'], ext) - path = path.replace(generator_default_variables['RULE_INPUT_NAME'], name) - return path - - def GypPathToNinja(self, path, env=None): - """Translate a gyp path to a ninja path, optionally expanding environment + PRODUCT_DIR = "$!PRODUCT_DIR" + if PRODUCT_DIR in path: + if product_dir: + path = path.replace(PRODUCT_DIR, product_dir) + else: + path = path.replace(PRODUCT_DIR + "/", "") + path = path.replace(PRODUCT_DIR + "\\", "") + path = path.replace(PRODUCT_DIR, ".") + + INTERMEDIATE_DIR = "$!INTERMEDIATE_DIR" + if INTERMEDIATE_DIR in path: + int_dir = self.GypPathToUniqueOutput("gen") + # GypPathToUniqueOutput generates a path relative to the product dir, + # so insert product_dir in front if it is provided. + path = path.replace( + INTERMEDIATE_DIR, os.path.join(product_dir or "", int_dir) + ) + + CONFIGURATION_NAME = "$|CONFIGURATION_NAME" + path = path.replace(CONFIGURATION_NAME, self.config_name) + + return path + + def ExpandRuleVariables(self, path, root, dirname, source, ext, name): + if self.flavor == "win": + path = self.msvs_settings.ConvertVSMacros(path, config=self.config_name) + path = path.replace(generator_default_variables["RULE_INPUT_ROOT"], root) + path = path.replace(generator_default_variables["RULE_INPUT_DIRNAME"], dirname) + path = path.replace(generator_default_variables["RULE_INPUT_PATH"], source) + path = path.replace(generator_default_variables["RULE_INPUT_EXT"], ext) + path = path.replace(generator_default_variables["RULE_INPUT_NAME"], name) + return path + + def GypPathToNinja(self, path, env=None): + """Translate a gyp path to a ninja path, optionally expanding environment variable references in |path| with |env|. See the above discourse on path conversions.""" - if env: - if self.flavor == 'mac': - path = gyp.xcode_emulation.ExpandEnvVars(path, env) - elif self.flavor == 'win': - path = gyp.msvs_emulation.ExpandMacros(path, env) - if path.startswith('$!'): - expanded = self.ExpandSpecial(path) - if self.flavor == 'win': - expanded = os.path.normpath(expanded) - return expanded - if '$|' in path: - path = self.ExpandSpecial(path) - assert '$' not in path, path - return os.path.normpath(os.path.join(self.build_to_base, path)) - - def GypPathToUniqueOutput(self, path, qualified=True): - """Translate a gyp path to a ninja path for writing output. + if env: + if self.flavor == "mac": + path = gyp.xcode_emulation.ExpandEnvVars(path, env) + elif self.flavor == "win": + path = gyp.msvs_emulation.ExpandMacros(path, env) + if path.startswith("$!"): + expanded = self.ExpandSpecial(path) + if self.flavor == "win": + expanded = os.path.normpath(expanded) + return expanded + if "$|" in path: + path = self.ExpandSpecial(path) + assert "$" not in path, path + return os.path.normpath(os.path.join(self.build_to_base, path)) + + def GypPathToUniqueOutput(self, path, qualified=True): + """Translate a gyp path to a ninja path for writing output. If qualified is True, qualify the resulting filename with the name of the target. This is necessary when e.g. compiling the same @@ -322,2180 +330,2611 @@ def GypPathToUniqueOutput(self, path, qualified=True): See the above discourse on path conversions.""" - path = self.ExpandSpecial(path) - assert not path.startswith('$'), path - - # Translate the path following this scheme: - # Input: foo/bar.gyp, target targ, references baz/out.o - # Output: obj/foo/baz/targ.out.o (if qualified) - # obj/foo/baz/out.o (otherwise) - # (and obj.host instead of obj for cross-compiles) - # - # Why this scheme and not some other one? - # 1) for a given input, you can compute all derived outputs by matching - # its path, even if the input is brought via a gyp file with '..'. - # 2) simple files like libraries and stamps have a simple filename. - - obj = 'obj' - if self.toolset != 'target': - obj += '.' + self.toolset - - path_dir, path_basename = os.path.split(path) - assert not os.path.isabs(path_dir), ( - "'%s' can not be absolute path (see crbug.com/462153)." % path_dir) - - if qualified: - path_basename = self.name + '.' + path_basename - return os.path.normpath(os.path.join(obj, self.base_dir, path_dir, - path_basename)) - - def WriteCollapsedDependencies(self, name, targets, order_only=None): - """Given a list of targets, return a path for a single file + path = self.ExpandSpecial(path) + assert not path.startswith("$"), path + + # Translate the path following this scheme: + # Input: foo/bar.gyp, target targ, references baz/out.o + # Output: obj/foo/baz/targ.out.o (if qualified) + # obj/foo/baz/out.o (otherwise) + # (and obj.host instead of obj for cross-compiles) + # + # Why this scheme and not some other one? + # 1) for a given input, you can compute all derived outputs by matching + # its path, even if the input is brought via a gyp file with '..'. + # 2) simple files like libraries and stamps have a simple filename. + + obj = "obj" + if self.toolset != "target": + obj += "." + self.toolset + + path_dir, path_basename = os.path.split(path) + assert not os.path.isabs(path_dir), ( + "'%s' can not be absolute path (see crbug.com/462153)." % path_dir + ) + + if qualified: + path_basename = self.name + "." + path_basename + return os.path.normpath( + os.path.join(obj, self.base_dir, path_dir, path_basename) + ) + + def WriteCollapsedDependencies(self, name, targets, order_only=None): + """Given a list of targets, return a path for a single file representing the result of building all the targets or None. Uses a stamp file if necessary.""" - assert targets == [item for item in targets if item], targets - if len(targets) == 0: - assert not order_only - return None - if len(targets) > 1 or order_only: - stamp = self.GypPathToUniqueOutput(name + '.stamp') - targets = self.ninja.build(stamp, 'stamp', targets, order_only=order_only) - self.ninja.newline() - return targets[0] + assert targets == [item for item in targets if item], targets + if len(targets) == 0: + assert not order_only + return None + if len(targets) > 1 or order_only: + stamp = self.GypPathToUniqueOutput(name + ".stamp") + targets = self.ninja.build(stamp, "stamp", targets, order_only=order_only) + self.ninja.newline() + return targets[0] - def _SubninjaNameForArch(self, arch): - output_file_base = os.path.splitext(self.output_file_name)[0] - return '%s.%s.ninja' % (output_file_base, arch) + def _SubninjaNameForArch(self, arch): + output_file_base = os.path.splitext(self.output_file_name)[0] + return "%s.%s.ninja" % (output_file_base, arch) - def WriteSpec(self, spec, config_name, generator_flags): - """The main entry point for NinjaWriter: write the build rules for a spec. + def WriteSpec(self, spec, config_name, generator_flags): + """The main entry point for NinjaWriter: write the build rules for a spec. Returns a Target object, which represents the output paths for this spec. Returns None if there are no outputs (e.g. a settings-only 'none' type target).""" - self.config_name = config_name - self.name = spec['target_name'] - self.toolset = spec['toolset'] - config = spec['configurations'][config_name] - self.target = Target(spec['type']) - self.is_standalone_static_library = bool( - spec.get('standalone_static_library', 0)) - - self.target_rpath = generator_flags.get('target_rpath', r'\$$ORIGIN/lib/') - - self.is_mac_bundle = gyp.xcode_emulation.IsMacBundle(self.flavor, spec) - self.xcode_settings = self.msvs_settings = None - if self.flavor == 'mac': - self.xcode_settings = gyp.xcode_emulation.XcodeSettings(spec) - mac_toolchain_dir = generator_flags.get('mac_toolchain_dir', None) - if mac_toolchain_dir: - self.xcode_settings.mac_toolchain_dir = mac_toolchain_dir - - if self.flavor == 'win': - self.msvs_settings = gyp.msvs_emulation.MsvsSettings(spec, - generator_flags) - arch = self.msvs_settings.GetArch(config_name) - self.ninja.variable('arch', self.win_env[arch]) - self.ninja.variable('cc', '$cl_' + arch) - self.ninja.variable('cxx', '$cl_' + arch) - self.ninja.variable('cc_host', '$cl_' + arch) - self.ninja.variable('cxx_host', '$cl_' + arch) - self.ninja.variable('asm', '$ml_' + arch) - - if self.flavor == 'mac': - self.archs = self.xcode_settings.GetActiveArchs(config_name) - if len(self.archs) > 1: - self.arch_subninjas = dict( - (arch, ninja_syntax.Writer( - OpenOutput(os.path.join(self.toplevel_build, - self._SubninjaNameForArch(arch)), - 'w'))) - for arch in self.archs) - - # Compute predepends for all rules. - # actions_depends is the dependencies this target depends on before running - # any of its action/rule/copy steps. - # compile_depends is the dependencies this target depends on before running - # any of its compile steps. - actions_depends = [] - compile_depends = [] - # TODO(evan): it is rather confusing which things are lists and which - # are strings. Fix these. - if 'dependencies' in spec: - for dep in spec['dependencies']: - if dep in self.target_outputs: - target = self.target_outputs[dep] - actions_depends.append(target.PreActionInput(self.flavor)) - compile_depends.append(target.PreCompileInput()) - if target.uses_cpp: - self.target.uses_cpp = True - actions_depends = [item for item in actions_depends if item] - compile_depends = [item for item in compile_depends if item] - actions_depends = self.WriteCollapsedDependencies('actions_depends', - actions_depends) - compile_depends = self.WriteCollapsedDependencies('compile_depends', - compile_depends) - self.target.preaction_stamp = actions_depends - self.target.precompile_stamp = compile_depends - - # Write out actions, rules, and copies. These must happen before we - # compile any sources, so compute a list of predependencies for sources - # while we do it. - extra_sources = [] - mac_bundle_depends = [] - self.target.actions_stamp = self.WriteActionsRulesCopies( - spec, extra_sources, actions_depends, mac_bundle_depends) - - # If we have actions/rules/copies, we depend directly on those, but - # otherwise we depend on dependent target's actions/rules/copies etc. - # We never need to explicitly depend on previous target's link steps, - # because no compile ever depends on them. - compile_depends_stamp = (self.target.actions_stamp or compile_depends) - - # Write out the compilation steps, if any. - link_deps = [] - try: - sources = extra_sources + spec.get('sources', []) - except TypeError: - print('extra_sources: ', str(extra_sources)) - print('spec.get("sources"): ', str(spec.get('sources'))) - raise - if sources: - if self.flavor == 'mac' and len(self.archs) > 1: - # Write subninja file containing compile and link commands scoped to - # a single arch if a fat binary is being built. - for arch in self.archs: - self.ninja.subninja(self._SubninjaNameForArch(arch)) - - pch = None - if self.flavor == 'win': - gyp.msvs_emulation.VerifyMissingSources( - sources, self.abs_build_dir, generator_flags, self.GypPathToNinja) - pch = gyp.msvs_emulation.PrecompiledHeader( - self.msvs_settings, config_name, self.GypPathToNinja, - self.GypPathToUniqueOutput, self.obj_ext) - else: - pch = gyp.xcode_emulation.MacPrefixHeader( - self.xcode_settings, self.GypPathToNinja, - lambda path, lang: self.GypPathToUniqueOutput(path + '-' + lang)) - link_deps = self.WriteSources( - self.ninja, config_name, config, sources, compile_depends_stamp, pch, - spec) - # Some actions/rules output 'sources' that are already object files. - obj_outputs = [f for f in sources if f.endswith(self.obj_ext)] - if obj_outputs: - if self.flavor != 'mac' or len(self.archs) == 1: - link_deps += [self.GypPathToNinja(o) for o in obj_outputs] - else: - print("Warning: Actions/rules writing object files don't work with " - "multiarch targets, dropping. (target %s)" % spec['target_name']) - elif self.flavor == 'mac' and len(self.archs) > 1: - link_deps = collections.defaultdict(list) - - compile_deps = self.target.actions_stamp or actions_depends - if self.flavor == 'win' and self.target.type == 'static_library': - self.target.component_objs = link_deps - self.target.compile_deps = compile_deps - - # Write out a link step, if needed. - output = None - is_empty_bundle = not link_deps and not mac_bundle_depends - if link_deps or self.target.actions_stamp or actions_depends: - output = self.WriteTarget(spec, config_name, config, link_deps, - compile_deps) - if self.is_mac_bundle: - mac_bundle_depends.append(output) - - # Bundle all of the above together, if needed. - if self.is_mac_bundle: - output = self.WriteMacBundle(spec, mac_bundle_depends, is_empty_bundle) - - if not output: - return None - - assert self.target.FinalOutput(), output - return self.target - - def _WinIdlRule(self, source, prebuild, outputs): - """Handle the implicit VS .idl rule for one source file. Fills |outputs| - with files that are generated.""" - outdir, output, vars, flags = self.msvs_settings.GetIdlBuildData( - source, self.config_name) - outdir = self.GypPathToNinja(outdir) - def fix_path(path, rel=None): - path = os.path.join(outdir, path) - dirname, basename = os.path.split(source) - root, ext = os.path.splitext(basename) - path = self.ExpandRuleVariables( - path, root, dirname, source, ext, basename) - if rel: - path = os.path.relpath(path, rel) - return path - vars = [(name, fix_path(value, outdir)) for name, value in vars] - output = [fix_path(p) for p in output] - vars.append(('outdir', outdir)) - vars.append(('idlflags', flags)) - input = self.GypPathToNinja(source) - self.ninja.build(output, 'idl', input, - variables=vars, order_only=prebuild) - outputs.extend(output) - - def WriteWinIdlFiles(self, spec, prebuild): - """Writes rules to match MSVS's implicit idl handling.""" - assert self.flavor == 'win' - if self.msvs_settings.HasExplicitIdlRulesOrActions(spec): - return [] - outputs = [] - for source in filter(lambda x: x.endswith('.idl'), spec['sources']): - self._WinIdlRule(source, prebuild, outputs) - return outputs - - def WriteActionsRulesCopies(self, spec, extra_sources, prebuild, - mac_bundle_depends): - """Write out the Actions, Rules, and Copies steps. Return a path - representing the outputs of these steps.""" - outputs = [] - if self.is_mac_bundle: - mac_bundle_resources = spec.get('mac_bundle_resources', [])[:] - else: - mac_bundle_resources = [] - extra_mac_bundle_resources = [] - - if 'actions' in spec: - outputs += self.WriteActions(spec['actions'], extra_sources, prebuild, - extra_mac_bundle_resources) - if 'rules' in spec: - outputs += self.WriteRules(spec['rules'], extra_sources, prebuild, - mac_bundle_resources, - extra_mac_bundle_resources) - if 'copies' in spec: - outputs += self.WriteCopies(spec['copies'], prebuild, mac_bundle_depends) + self.config_name = config_name + self.name = spec["target_name"] + self.toolset = spec["toolset"] + config = spec["configurations"][config_name] + self.target = Target(spec["type"]) + self.is_standalone_static_library = bool( + spec.get("standalone_static_library", 0) + ) + + self.target_rpath = generator_flags.get("target_rpath", r"\$$ORIGIN/lib/") + + self.is_mac_bundle = gyp.xcode_emulation.IsMacBundle(self.flavor, spec) + self.xcode_settings = self.msvs_settings = None + if self.flavor == "mac": + self.xcode_settings = gyp.xcode_emulation.XcodeSettings(spec) + mac_toolchain_dir = generator_flags.get("mac_toolchain_dir", None) + if mac_toolchain_dir: + self.xcode_settings.mac_toolchain_dir = mac_toolchain_dir + + if self.flavor == "win": + self.msvs_settings = gyp.msvs_emulation.MsvsSettings(spec, generator_flags) + arch = self.msvs_settings.GetArch(config_name) + self.ninja.variable("arch", self.win_env[arch]) + self.ninja.variable("cc", "$cl_" + arch) + self.ninja.variable("cxx", "$cl_" + arch) + self.ninja.variable("cc_host", "$cl_" + arch) + self.ninja.variable("cxx_host", "$cl_" + arch) + self.ninja.variable("asm", "$ml_" + arch) + + if self.flavor == "mac": + self.archs = self.xcode_settings.GetActiveArchs(config_name) + if len(self.archs) > 1: + self.arch_subninjas = dict( + ( + arch, + ninja_syntax.Writer( + OpenOutput( + os.path.join( + self.toplevel_build, self._SubninjaNameForArch(arch) + ), + "w", + ) + ), + ) + for arch in self.archs + ) + + # Compute predepends for all rules. + # actions_depends is the dependencies this target depends on before running + # any of its action/rule/copy steps. + # compile_depends is the dependencies this target depends on before running + # any of its compile steps. + actions_depends = [] + compile_depends = [] + # TODO(evan): it is rather confusing which things are lists and which + # are strings. Fix these. + if "dependencies" in spec: + for dep in spec["dependencies"]: + if dep in self.target_outputs: + target = self.target_outputs[dep] + actions_depends.append(target.PreActionInput(self.flavor)) + compile_depends.append(target.PreCompileInput()) + if target.uses_cpp: + self.target.uses_cpp = True + actions_depends = [item for item in actions_depends if item] + compile_depends = [item for item in compile_depends if item] + actions_depends = self.WriteCollapsedDependencies( + "actions_depends", actions_depends + ) + compile_depends = self.WriteCollapsedDependencies( + "compile_depends", compile_depends + ) + self.target.preaction_stamp = actions_depends + self.target.precompile_stamp = compile_depends + + # Write out actions, rules, and copies. These must happen before we + # compile any sources, so compute a list of predependencies for sources + # while we do it. + extra_sources = [] + mac_bundle_depends = [] + self.target.actions_stamp = self.WriteActionsRulesCopies( + spec, extra_sources, actions_depends, mac_bundle_depends + ) + + # If we have actions/rules/copies, we depend directly on those, but + # otherwise we depend on dependent target's actions/rules/copies etc. + # We never need to explicitly depend on previous target's link steps, + # because no compile ever depends on them. + compile_depends_stamp = self.target.actions_stamp or compile_depends + + # Write out the compilation steps, if any. + link_deps = [] + try: + sources = extra_sources + spec.get("sources", []) + except TypeError: + print("extra_sources: ", str(extra_sources)) + print('spec.get("sources"): ', str(spec.get("sources"))) + raise + if sources: + if self.flavor == "mac" and len(self.archs) > 1: + # Write subninja file containing compile and link commands scoped to + # a single arch if a fat binary is being built. + for arch in self.archs: + self.ninja.subninja(self._SubninjaNameForArch(arch)) + + pch = None + if self.flavor == "win": + gyp.msvs_emulation.VerifyMissingSources( + sources, self.abs_build_dir, generator_flags, self.GypPathToNinja + ) + pch = gyp.msvs_emulation.PrecompiledHeader( + self.msvs_settings, + config_name, + self.GypPathToNinja, + self.GypPathToUniqueOutput, + self.obj_ext, + ) + else: + pch = gyp.xcode_emulation.MacPrefixHeader( + self.xcode_settings, + self.GypPathToNinja, + lambda path, lang: self.GypPathToUniqueOutput(path + "-" + lang), + ) + link_deps = self.WriteSources( + self.ninja, + config_name, + config, + sources, + compile_depends_stamp, + pch, + spec, + ) + # Some actions/rules output 'sources' that are already object files. + obj_outputs = [f for f in sources if f.endswith(self.obj_ext)] + if obj_outputs: + if self.flavor != "mac" or len(self.archs) == 1: + link_deps += [self.GypPathToNinja(o) for o in obj_outputs] + else: + print( + "Warning: Actions/rules writing object files don't work with " + "multiarch targets, dropping. (target %s)" % spec["target_name"] + ) + elif self.flavor == "mac" and len(self.archs) > 1: + link_deps = collections.defaultdict(list) + + compile_deps = self.target.actions_stamp or actions_depends + if self.flavor == "win" and self.target.type == "static_library": + self.target.component_objs = link_deps + self.target.compile_deps = compile_deps + + # Write out a link step, if needed. + output = None + is_empty_bundle = not link_deps and not mac_bundle_depends + if link_deps or self.target.actions_stamp or actions_depends: + output = self.WriteTarget( + spec, config_name, config, link_deps, compile_deps + ) + if self.is_mac_bundle: + mac_bundle_depends.append(output) + + # Bundle all of the above together, if needed. + if self.is_mac_bundle: + output = self.WriteMacBundle(spec, mac_bundle_depends, is_empty_bundle) - if 'sources' in spec and self.flavor == 'win': - outputs += self.WriteWinIdlFiles(spec, prebuild) + if not output: + return None - if self.xcode_settings and self.xcode_settings.IsIosFramework(): - self.WriteiOSFrameworkHeaders(spec, outputs, prebuild) + assert self.target.FinalOutput(), output + return self.target - stamp = self.WriteCollapsedDependencies('actions_rules_copies', outputs) + def _WinIdlRule(self, source, prebuild, outputs): + """Handle the implicit VS .idl rule for one source file. Fills |outputs| + with files that are generated.""" + outdir, output, vars, flags = self.msvs_settings.GetIdlBuildData( + source, self.config_name + ) + outdir = self.GypPathToNinja(outdir) + + def fix_path(path, rel=None): + path = os.path.join(outdir, path) + dirname, basename = os.path.split(source) + root, ext = os.path.splitext(basename) + path = self.ExpandRuleVariables(path, root, dirname, source, ext, basename) + if rel: + path = os.path.relpath(path, rel) + return path + + vars = [(name, fix_path(value, outdir)) for name, value in vars] + output = [fix_path(p) for p in output] + vars.append(("outdir", outdir)) + vars.append(("idlflags", flags)) + input = self.GypPathToNinja(source) + self.ninja.build(output, "idl", input, variables=vars, order_only=prebuild) + outputs.extend(output) + + def WriteWinIdlFiles(self, spec, prebuild): + """Writes rules to match MSVS's implicit idl handling.""" + assert self.flavor == "win" + if self.msvs_settings.HasExplicitIdlRulesOrActions(spec): + return [] + outputs = [] + for source in filter(lambda x: x.endswith(".idl"), spec["sources"]): + self._WinIdlRule(source, prebuild, outputs) + return outputs + + def WriteActionsRulesCopies( + self, spec, extra_sources, prebuild, mac_bundle_depends + ): + """Write out the Actions, Rules, and Copies steps. Return a path + representing the outputs of these steps.""" + outputs = [] + if self.is_mac_bundle: + mac_bundle_resources = spec.get("mac_bundle_resources", [])[:] + else: + mac_bundle_resources = [] + extra_mac_bundle_resources = [] + + if "actions" in spec: + outputs += self.WriteActions( + spec["actions"], extra_sources, prebuild, extra_mac_bundle_resources + ) + if "rules" in spec: + outputs += self.WriteRules( + spec["rules"], + extra_sources, + prebuild, + mac_bundle_resources, + extra_mac_bundle_resources, + ) + if "copies" in spec: + outputs += self.WriteCopies(spec["copies"], prebuild, mac_bundle_depends) + + if "sources" in spec and self.flavor == "win": + outputs += self.WriteWinIdlFiles(spec, prebuild) + + if self.xcode_settings and self.xcode_settings.IsIosFramework(): + self.WriteiOSFrameworkHeaders(spec, outputs, prebuild) + + stamp = self.WriteCollapsedDependencies("actions_rules_copies", outputs) - if self.is_mac_bundle: - xcassets = self.WriteMacBundleResources( - extra_mac_bundle_resources + mac_bundle_resources, mac_bundle_depends) - partial_info_plist = self.WriteMacXCassets(xcassets, mac_bundle_depends) - self.WriteMacInfoPlist(partial_info_plist, mac_bundle_depends) + if self.is_mac_bundle: + xcassets = self.WriteMacBundleResources( + extra_mac_bundle_resources + mac_bundle_resources, mac_bundle_depends + ) + partial_info_plist = self.WriteMacXCassets(xcassets, mac_bundle_depends) + self.WriteMacInfoPlist(partial_info_plist, mac_bundle_depends) - return stamp + return stamp - def GenerateDescription(self, verb, message, fallback): - """Generate and return a description of a build step. + def GenerateDescription(self, verb, message, fallback): + """Generate and return a description of a build step. |verb| is the short summary, e.g. ACTION or RULE. |message| is a hand-written description, or None if not available. |fallback| is the gyp-level name of the step, usable as a fallback. """ - if self.toolset != 'target': - verb += '(%s)' % self.toolset - if message: - return '%s %s' % (verb, self.ExpandSpecial(message)) - else: - return '%s %s: %s' % (verb, self.name, fallback) - - def WriteActions(self, actions, extra_sources, prebuild, - extra_mac_bundle_resources): - # Actions cd into the base directory. - env = self.GetToolchainEnv() - all_outputs = [] - for action in actions: - # First write out a rule for the action. - name = '%s_%s' % (action['action_name'], self.hash_for_rules) - description = self.GenerateDescription('ACTION', - action.get('message', None), - name) - is_cygwin = (self.msvs_settings.IsRuleRunUnderCygwin(action) - if self.flavor == 'win' else False) - args = action['action'] - depfile = action.get('depfile', None) - if depfile: - depfile = self.ExpandSpecial(depfile, self.base_to_build) - pool = 'console' if int(action.get('ninja_use_console', 0)) else None - rule_name, _ = self.WriteNewNinjaRule(name, args, description, - is_cygwin, env, pool, - depfile=depfile) - - inputs = [self.GypPathToNinja(i, env) for i in action['inputs']] - if int(action.get('process_outputs_as_sources', False)): - extra_sources += action['outputs'] - if int(action.get('process_outputs_as_mac_bundle_resources', False)): - extra_mac_bundle_resources += action['outputs'] - outputs = [self.GypPathToNinja(o, env) for o in action['outputs']] - - # Then write out an edge using the rule. - self.ninja.build(outputs, rule_name, inputs, - order_only=prebuild) - all_outputs += outputs - - self.ninja.newline() - - return all_outputs - - def WriteRules(self, rules, extra_sources, prebuild, - mac_bundle_resources, extra_mac_bundle_resources): - env = self.GetToolchainEnv() - all_outputs = [] - for rule in rules: - # Skip a rule with no action and no inputs. - if 'action' not in rule and not rule.get('rule_sources', []): - continue - - # First write out a rule for the rule action. - name = '%s_%s' % (rule['rule_name'], self.hash_for_rules) - - args = rule['action'] - description = self.GenerateDescription( - 'RULE', - rule.get('message', None), - ('%s ' + generator_default_variables['RULE_INPUT_PATH']) % name) - is_cygwin = (self.msvs_settings.IsRuleRunUnderCygwin(rule) - if self.flavor == 'win' else False) - pool = 'console' if int(rule.get('ninja_use_console', 0)) else None - rule_name, args = self.WriteNewNinjaRule( - name, args, description, is_cygwin, env, pool) - - # TODO: if the command references the outputs directly, we should - # simplify it to just use $out. - - # Rules can potentially make use of some special variables which - # must vary per source file. - # Compute the list of variables we'll need to provide. - special_locals = ('source', 'root', 'dirname', 'ext', 'name') - needed_variables = set(['source']) - for argument in args: - for var in special_locals: - if '${%s}' % var in argument: - needed_variables.add(var) - needed_variables = sorted(needed_variables) - - def cygwin_munge(path): - # pylint: disable=cell-var-from-loop - if is_cygwin: - return path.replace('\\', '/') - return path - - inputs = [self.GypPathToNinja(i, env) for i in rule.get('inputs', [])] - - # If there are n source files matching the rule, and m additional rule - # inputs, then adding 'inputs' to each build edge written below will - # write m * n inputs. Collapsing reduces this to m + n. - sources = rule.get('rule_sources', []) - num_inputs = len(inputs) - if prebuild: - num_inputs += 1 - if num_inputs > 2 and len(sources) > 2: - inputs = [self.WriteCollapsedDependencies( - rule['rule_name'], inputs, order_only=prebuild)] - prebuild = [] - - # For each source file, write an edge that generates all the outputs. - for source in sources: - source = os.path.normpath(source) - dirname, basename = os.path.split(source) - root, ext = os.path.splitext(basename) - - # Gather the list of inputs and outputs, expanding $vars if possible. - outputs = [self.ExpandRuleVariables(o, root, dirname, - source, ext, basename) - for o in rule['outputs']] - - if int(rule.get('process_outputs_as_sources', False)): - extra_sources += outputs - - was_mac_bundle_resource = source in mac_bundle_resources - if was_mac_bundle_resource or \ - int(rule.get('process_outputs_as_mac_bundle_resources', False)): - extra_mac_bundle_resources += outputs - # Note: This is n_resources * n_outputs_in_rule. Put to-be-removed - # items in a set and remove them all in a single pass if this becomes - # a performance issue. - if was_mac_bundle_resource: - mac_bundle_resources.remove(source) - - extra_bindings = [] - for var in needed_variables: - if var == 'root': - extra_bindings.append(('root', cygwin_munge(root))) - elif var == 'dirname': - # '$dirname' is a parameter to the rule action, which means - # it shouldn't be converted to a Ninja path. But we don't - # want $!PRODUCT_DIR in there either. - dirname_expanded = self.ExpandSpecial(dirname, self.base_to_build) - extra_bindings.append(('dirname', cygwin_munge(dirname_expanded))) - elif var == 'source': - # '$source' is a parameter to the rule action, which means - # it shouldn't be converted to a Ninja path. But we don't - # want $!PRODUCT_DIR in there either. - source_expanded = self.ExpandSpecial(source, self.base_to_build) - extra_bindings.append(('source', cygwin_munge(source_expanded))) - elif var == 'ext': - extra_bindings.append(('ext', ext)) - elif var == 'name': - extra_bindings.append(('name', cygwin_munge(basename))) - else: - assert var is None, repr(var) - - outputs = [self.GypPathToNinja(o, env) for o in outputs] - if self.flavor == 'win': - # WriteNewNinjaRule uses unique_name for creating an rsp file on win. - extra_bindings.append(('unique_name', - hashlib.md5(outputs[0]).hexdigest())) - - self.ninja.build(outputs, rule_name, self.GypPathToNinja(source), - implicit=inputs, - order_only=prebuild, - variables=extra_bindings) - - all_outputs.extend(outputs) - - return all_outputs - - def WriteCopies(self, copies, prebuild, mac_bundle_depends): - outputs = [] - if self.xcode_settings: - extra_env = self.xcode_settings.GetPerTargetSettings() - env = self.GetToolchainEnv(additional_settings=extra_env) - else: - env = self.GetToolchainEnv() - for copy in copies: - for path in copy['files']: - # Normalize the path so trailing slashes don't confuse us. - path = os.path.normpath(path) - basename = os.path.split(path)[1] - src = self.GypPathToNinja(path, env) - dst = self.GypPathToNinja(os.path.join(copy['destination'], basename), - env) - outputs += self.ninja.build(dst, 'copy', src, order_only=prebuild) - if self.is_mac_bundle: - # gyp has mac_bundle_resources to copy things into a bundle's - # Resources folder, but there's no built-in way to copy files to other - # places in the bundle. Hence, some targets use copies for this. Check - # if this file is copied into the current bundle, and if so add it to - # the bundle depends so that dependent targets get rebuilt if the copy - # input changes. - if dst.startswith(self.xcode_settings.GetBundleContentsFolderPath()): - mac_bundle_depends.append(dst) - - return outputs - - def WriteiOSFrameworkHeaders(self, spec, outputs, prebuild): - """Prebuild steps to generate hmap files and copy headers to destination.""" - framework = self.ComputeMacBundleOutput() - all_sources = spec['sources'] - copy_headers = spec['mac_framework_headers'] - output = self.GypPathToUniqueOutput('headers.hmap') - self.xcode_settings.header_map_path = output - all_headers = map(self.GypPathToNinja, - filter(lambda x:x.endswith(('.h')), all_sources)) - variables = [('framework', framework), - ('copy_headers', map(self.GypPathToNinja, copy_headers))] - outputs.extend(self.ninja.build( - output, 'compile_ios_framework_headers', all_headers, - variables=variables, order_only=prebuild)) - - def WriteMacBundleResources(self, resources, bundle_depends): - """Writes ninja edges for 'mac_bundle_resources'.""" - xcassets = [] - - extra_env = self.xcode_settings.GetPerTargetSettings() - env = self.GetSortedXcodeEnv(additional_settings=extra_env) - env = self.ComputeExportEnvString(env) - isBinary = self.xcode_settings.IsBinaryOutputFormat(self.config_name) - - for output, res in gyp.xcode_emulation.GetMacBundleResources( - generator_default_variables['PRODUCT_DIR'], - self.xcode_settings, map(self.GypPathToNinja, resources)): - output = self.ExpandSpecial(output) - if os.path.splitext(output)[-1] != '.xcassets': - self.ninja.build(output, 'mac_tool', res, - variables=[('mactool_cmd', 'copy-bundle-resource'), \ - ('env', env), ('binary', isBinary)]) - bundle_depends.append(output) - else: - xcassets.append(res) - return xcassets - - def WriteMacXCassets(self, xcassets, bundle_depends): - """Writes ninja edges for 'mac_bundle_resources' .xcassets files. + if self.toolset != "target": + verb += "(%s)" % self.toolset + if message: + return "%s %s" % (verb, self.ExpandSpecial(message)) + else: + return "%s %s: %s" % (verb, self.name, fallback) + + def WriteActions( + self, actions, extra_sources, prebuild, extra_mac_bundle_resources + ): + # Actions cd into the base directory. + env = self.GetToolchainEnv() + all_outputs = [] + for action in actions: + # First write out a rule for the action. + name = "%s_%s" % (action["action_name"], self.hash_for_rules) + description = self.GenerateDescription( + "ACTION", action.get("message", None), name + ) + is_cygwin = ( + self.msvs_settings.IsRuleRunUnderCygwin(action) + if self.flavor == "win" + else False + ) + args = action["action"] + depfile = action.get("depfile", None) + if depfile: + depfile = self.ExpandSpecial(depfile, self.base_to_build) + pool = "console" if int(action.get("ninja_use_console", 0)) else None + rule_name, _ = self.WriteNewNinjaRule( + name, args, description, is_cygwin, env, pool, depfile=depfile + ) + + inputs = [self.GypPathToNinja(i, env) for i in action["inputs"]] + if int(action.get("process_outputs_as_sources", False)): + extra_sources += action["outputs"] + if int(action.get("process_outputs_as_mac_bundle_resources", False)): + extra_mac_bundle_resources += action["outputs"] + outputs = [self.GypPathToNinja(o, env) for o in action["outputs"]] + + # Then write out an edge using the rule. + self.ninja.build(outputs, rule_name, inputs, order_only=prebuild) + all_outputs += outputs + + self.ninja.newline() + + return all_outputs + + def WriteRules( + self, + rules, + extra_sources, + prebuild, + mac_bundle_resources, + extra_mac_bundle_resources, + ): + env = self.GetToolchainEnv() + all_outputs = [] + for rule in rules: + # Skip a rule with no action and no inputs. + if "action" not in rule and not rule.get("rule_sources", []): + continue + + # First write out a rule for the rule action. + name = "%s_%s" % (rule["rule_name"], self.hash_for_rules) + + args = rule["action"] + description = self.GenerateDescription( + "RULE", + rule.get("message", None), + ("%s " + generator_default_variables["RULE_INPUT_PATH"]) % name, + ) + is_cygwin = ( + self.msvs_settings.IsRuleRunUnderCygwin(rule) + if self.flavor == "win" + else False + ) + pool = "console" if int(rule.get("ninja_use_console", 0)) else None + rule_name, args = self.WriteNewNinjaRule( + name, args, description, is_cygwin, env, pool + ) + + # TODO: if the command references the outputs directly, we should + # simplify it to just use $out. + + # Rules can potentially make use of some special variables which + # must vary per source file. + # Compute the list of variables we'll need to provide. + special_locals = ("source", "root", "dirname", "ext", "name") + needed_variables = set(["source"]) + for argument in args: + for var in special_locals: + if "${%s}" % var in argument: + needed_variables.add(var) + needed_variables = sorted(needed_variables) + + def cygwin_munge(path): + # pylint: disable=cell-var-from-loop + if is_cygwin: + return path.replace("\\", "/") + return path + + inputs = [self.GypPathToNinja(i, env) for i in rule.get("inputs", [])] + + # If there are n source files matching the rule, and m additional rule + # inputs, then adding 'inputs' to each build edge written below will + # write m * n inputs. Collapsing reduces this to m + n. + sources = rule.get("rule_sources", []) + num_inputs = len(inputs) + if prebuild: + num_inputs += 1 + if num_inputs > 2 and len(sources) > 2: + inputs = [ + self.WriteCollapsedDependencies( + rule["rule_name"], inputs, order_only=prebuild + ) + ] + prebuild = [] + + # For each source file, write an edge that generates all the outputs. + for source in sources: + source = os.path.normpath(source) + dirname, basename = os.path.split(source) + root, ext = os.path.splitext(basename) + + # Gather the list of inputs and outputs, expanding $vars if possible. + outputs = [ + self.ExpandRuleVariables(o, root, dirname, source, ext, basename) + for o in rule["outputs"] + ] + + if int(rule.get("process_outputs_as_sources", False)): + extra_sources += outputs + + was_mac_bundle_resource = source in mac_bundle_resources + if was_mac_bundle_resource or int( + rule.get("process_outputs_as_mac_bundle_resources", False) + ): + extra_mac_bundle_resources += outputs + # Note: This is n_resources * n_outputs_in_rule. + # Put to-be-removed items in a set and + # remove them all in a single pass + # if this becomes a performance issue. + if was_mac_bundle_resource: + mac_bundle_resources.remove(source) + + extra_bindings = [] + for var in needed_variables: + if var == "root": + extra_bindings.append(("root", cygwin_munge(root))) + elif var == "dirname": + # '$dirname' is a parameter to the rule action, which means + # it shouldn't be converted to a Ninja path. But we don't + # want $!PRODUCT_DIR in there either. + dirname_expanded = self.ExpandSpecial( + dirname, self.base_to_build + ) + extra_bindings.append( + ("dirname", cygwin_munge(dirname_expanded)) + ) + elif var == "source": + # '$source' is a parameter to the rule action, which means + # it shouldn't be converted to a Ninja path. But we don't + # want $!PRODUCT_DIR in there either. + source_expanded = self.ExpandSpecial(source, self.base_to_build) + extra_bindings.append(("source", cygwin_munge(source_expanded))) + elif var == "ext": + extra_bindings.append(("ext", ext)) + elif var == "name": + extra_bindings.append(("name", cygwin_munge(basename))) + else: + assert var is None, repr(var) + + outputs = [self.GypPathToNinja(o, env) for o in outputs] + if self.flavor == "win": + # WriteNewNinjaRule uses unique_name to create a rsp file on win. + extra_bindings.append( + ("unique_name", hashlib.md5(outputs[0]).hexdigest()) + ) + + self.ninja.build( + outputs, + rule_name, + self.GypPathToNinja(source), + implicit=inputs, + order_only=prebuild, + variables=extra_bindings, + ) + + all_outputs.extend(outputs) + + return all_outputs + + def WriteCopies(self, copies, prebuild, mac_bundle_depends): + outputs = [] + if self.xcode_settings: + extra_env = self.xcode_settings.GetPerTargetSettings() + env = self.GetToolchainEnv(additional_settings=extra_env) + else: + env = self.GetToolchainEnv() + for to_copy in copies: + for path in to_copy["files"]: + # Normalize the path so trailing slashes don't confuse us. + path = os.path.normpath(path) + basename = os.path.split(path)[1] + src = self.GypPathToNinja(path, env) + dst = self.GypPathToNinja( + os.path.join(to_copy["destination"], basename), env + ) + outputs += self.ninja.build(dst, "copy", src, order_only=prebuild) + if self.is_mac_bundle: + # gyp has mac_bundle_resources to copy things into a bundle's + # Resources folder, but there's no built-in way to copy files + # to other places in the bundle. + # Hence, some targets use copies for this. + # Check if this file is copied into the current bundle, + # and if so add it to the bundle depends so + # that dependent targets get rebuilt if the copy input changes. + if dst.startswith( + self.xcode_settings.GetBundleContentsFolderPath() + ): + mac_bundle_depends.append(dst) + + return outputs + + def WriteiOSFrameworkHeaders(self, spec, outputs, prebuild): + """Prebuild steps to generate hmap files and copy headers to destination.""" + framework = self.ComputeMacBundleOutput() + all_sources = spec["sources"] + copy_headers = spec["mac_framework_headers"] + output = self.GypPathToUniqueOutput("headers.hmap") + self.xcode_settings.header_map_path = output + all_headers = map( + self.GypPathToNinja, filter(lambda x: x.endswith((".h")), all_sources) + ) + variables = [ + ("framework", framework), + ("copy_headers", map(self.GypPathToNinja, copy_headers)), + ] + outputs.extend( + self.ninja.build( + output, + "compile_ios_framework_headers", + all_headers, + variables=variables, + order_only=prebuild, + ) + ) + + def WriteMacBundleResources(self, resources, bundle_depends): + """Writes ninja edges for 'mac_bundle_resources'.""" + xcassets = [] + + extra_env = self.xcode_settings.GetPerTargetSettings() + env = self.GetSortedXcodeEnv(additional_settings=extra_env) + env = self.ComputeExportEnvString(env) + isBinary = self.xcode_settings.IsBinaryOutputFormat(self.config_name) + + for output, res in gyp.xcode_emulation.GetMacBundleResources( + generator_default_variables["PRODUCT_DIR"], + self.xcode_settings, + map(self.GypPathToNinja, resources), + ): + output = self.ExpandSpecial(output) + if os.path.splitext(output)[-1] != ".xcassets": + self.ninja.build( + output, + "mac_tool", + res, + variables=[ + ("mactool_cmd", "copy-bundle-resource"), + ("env", env), + ("binary", isBinary), + ], + ) + bundle_depends.append(output) + else: + xcassets.append(res) + return xcassets + + def WriteMacXCassets(self, xcassets, bundle_depends): + """Writes ninja edges for 'mac_bundle_resources' .xcassets files. This add an invocation of 'actool' via the 'mac_tool.py' helper script. It assumes that the assets catalogs define at least one imageset and thus an Assets.car file will be generated in the application resources directory. If this is not the case, then the build will probably be done at each invocation of ninja.""" - if not xcassets: - return - - extra_arguments = {} - settings_to_arg = { - 'XCASSETS_APP_ICON': 'app-icon', - 'XCASSETS_LAUNCH_IMAGE': 'launch-image', - } - settings = self.xcode_settings.xcode_settings[self.config_name] - for settings_key, arg_name in settings_to_arg.items(): - value = settings.get(settings_key) - if value: - extra_arguments[arg_name] = value - - partial_info_plist = None - if extra_arguments: - partial_info_plist = self.GypPathToUniqueOutput( - 'assetcatalog_generated_info.plist') - extra_arguments['output-partial-info-plist'] = partial_info_plist - - outputs = [] - outputs.append( - os.path.join( - self.xcode_settings.GetBundleResourceFolder(), - 'Assets.car')) - if partial_info_plist: - outputs.append(partial_info_plist) - - keys = QuoteShellArgument(json.dumps(extra_arguments), self.flavor) - extra_env = self.xcode_settings.GetPerTargetSettings() - env = self.GetSortedXcodeEnv(additional_settings=extra_env) - env = self.ComputeExportEnvString(env) - - bundle_depends.extend(self.ninja.build( - outputs, 'compile_xcassets', xcassets, - variables=[('env', env), ('keys', keys)])) - return partial_info_plist - - def WriteMacInfoPlist(self, partial_info_plist, bundle_depends): - """Write build rules for bundle Info.plist files.""" - info_plist, out, defines, extra_env = gyp.xcode_emulation.GetMacInfoPlist( - generator_default_variables['PRODUCT_DIR'], - self.xcode_settings, self.GypPathToNinja) - if not info_plist: - return - out = self.ExpandSpecial(out) - if defines: - # Create an intermediate file to store preprocessed results. - intermediate_plist = self.GypPathToUniqueOutput( - os.path.basename(info_plist)) - defines = ' '.join([Define(d, self.flavor) for d in defines]) - info_plist = self.ninja.build( - intermediate_plist, 'preprocess_infoplist', info_plist, - variables=[('defines',defines)]) - - env = self.GetSortedXcodeEnv(additional_settings=extra_env) - env = self.ComputeExportEnvString(env) - - if partial_info_plist: - intermediate_plist = self.GypPathToUniqueOutput('merged_info.plist') - info_plist = self.ninja.build( - intermediate_plist, 'merge_infoplist', - [partial_info_plist, info_plist]) - - keys = self.xcode_settings.GetExtraPlistItems(self.config_name) - keys = QuoteShellArgument(json.dumps(keys), self.flavor) - isBinary = self.xcode_settings.IsBinaryOutputFormat(self.config_name) - self.ninja.build(out, 'copy_infoplist', info_plist, - variables=[('env', env), ('keys', keys), - ('binary', isBinary)]) - bundle_depends.append(out) - - def WriteSources(self, ninja_file, config_name, config, sources, predepends, - precompiled_header, spec): - """Write build rules to compile all of |sources|.""" - if self.toolset == 'host': - self.ninja.variable('ar', '$ar_host') - self.ninja.variable('cc', '$cc_host') - self.ninja.variable('cxx', '$cxx_host') - self.ninja.variable('ld', '$ld_host') - self.ninja.variable('ldxx', '$ldxx_host') - self.ninja.variable('nm', '$nm_host') - self.ninja.variable('readelf', '$readelf_host') - - if self.flavor != 'mac' or len(self.archs) == 1: - return self.WriteSourcesForArch( - self.ninja, config_name, config, sources, predepends, - precompiled_header, spec) - else: - return dict((arch, self.WriteSourcesForArch( - self.arch_subninjas[arch], config_name, config, sources, predepends, - precompiled_header, spec, arch=arch)) - for arch in self.archs) - - def WriteSourcesForArch(self, ninja_file, config_name, config, sources, - predepends, precompiled_header, spec, arch=None): - """Write build rules to compile all of |sources|.""" - - extra_defines = [] - if self.flavor == 'mac': - cflags = self.xcode_settings.GetCflags(config_name, arch=arch) - cflags_c = self.xcode_settings.GetCflagsC(config_name) - cflags_cc = self.xcode_settings.GetCflagsCC(config_name) - cflags_objc = ['$cflags_c'] + \ - self.xcode_settings.GetCflagsObjC(config_name) - cflags_objcc = ['$cflags_cc'] + \ - self.xcode_settings.GetCflagsObjCC(config_name) - elif self.flavor == 'win': - asmflags = self.msvs_settings.GetAsmflags(config_name) - cflags = self.msvs_settings.GetCflags(config_name) - cflags_c = self.msvs_settings.GetCflagsC(config_name) - cflags_cc = self.msvs_settings.GetCflagsCC(config_name) - extra_defines = self.msvs_settings.GetComputedDefines(config_name) - # See comment at cc_command for why there's two .pdb files. - pdbpath_c = pdbpath_cc = self.msvs_settings.GetCompilerPdbName( - config_name, self.ExpandSpecial) - if not pdbpath_c: - obj = 'obj' - if self.toolset != 'target': - obj += '.' + self.toolset - pdbpath = os.path.normpath(os.path.join(obj, self.base_dir, self.name)) - pdbpath_c = pdbpath + '.c.pdb' - pdbpath_cc = pdbpath + '.cc.pdb' - self.WriteVariableList(ninja_file, 'pdbname_c', [pdbpath_c]) - self.WriteVariableList(ninja_file, 'pdbname_cc', [pdbpath_cc]) - self.WriteVariableList(ninja_file, 'pchprefix', [self.name]) - else: - cflags = config.get('cflags', []) - cflags_c = config.get('cflags_c', []) - cflags_cc = config.get('cflags_cc', []) - - # Respect environment variables related to build, but target-specific - # flags can still override them. - if self.toolset == 'target': - cflags_c = (os.environ.get('CPPFLAGS', '').split() + - os.environ.get('CFLAGS', '').split() + cflags_c) - cflags_cc = (os.environ.get('CPPFLAGS', '').split() + - os.environ.get('CXXFLAGS', '').split() + cflags_cc) - elif self.toolset == 'host': - cflags_c = (os.environ.get('CPPFLAGS_host', '').split() + - os.environ.get('CFLAGS_host', '').split() + cflags_c) - cflags_cc = (os.environ.get('CPPFLAGS_host', '').split() + - os.environ.get('CXXFLAGS_host', '').split() + cflags_cc) - - defines = config.get('defines', []) + extra_defines - self.WriteVariableList(ninja_file, 'defines', - [Define(d, self.flavor) for d in defines]) - if self.flavor == 'win': - self.WriteVariableList(ninja_file, 'asmflags', - map(self.ExpandSpecial, asmflags)) - self.WriteVariableList(ninja_file, 'rcflags', - [QuoteShellArgument(self.ExpandSpecial(f), self.flavor) - for f in self.msvs_settings.GetRcflags(config_name, - self.GypPathToNinja)]) - - include_dirs = config.get('include_dirs', []) - - env = self.GetToolchainEnv() - if self.flavor == 'win': - include_dirs = self.msvs_settings.AdjustIncludeDirs(include_dirs, - config_name) - self.WriteVariableList(ninja_file, 'includes', - [QuoteShellArgument('-I' + self.GypPathToNinja(i, env), self.flavor) - for i in include_dirs]) - - if self.flavor == 'win': - midl_include_dirs = config.get('midl_include_dirs', []) - midl_include_dirs = self.msvs_settings.AdjustMidlIncludeDirs( - midl_include_dirs, config_name) - self.WriteVariableList(ninja_file, 'midl_includes', - [QuoteShellArgument('-I' + self.GypPathToNinja(i, env), self.flavor) - for i in midl_include_dirs]) - - pch_commands = precompiled_header.GetPchBuildCommands(arch) - if self.flavor == 'mac': - # Most targets use no precompiled headers, so only write these if needed. - for ext, var in [('c', 'cflags_pch_c'), ('cc', 'cflags_pch_cc'), - ('m', 'cflags_pch_objc'), ('mm', 'cflags_pch_objcc')]: - include = precompiled_header.GetInclude(ext, arch) - if include: ninja_file.variable(var, include) - - arflags = config.get('arflags', []) - - self.WriteVariableList(ninja_file, 'cflags', - map(self.ExpandSpecial, cflags)) - self.WriteVariableList(ninja_file, 'cflags_c', - map(self.ExpandSpecial, cflags_c)) - self.WriteVariableList(ninja_file, 'cflags_cc', - map(self.ExpandSpecial, cflags_cc)) - if self.flavor == 'mac': - self.WriteVariableList(ninja_file, 'cflags_objc', - map(self.ExpandSpecial, cflags_objc)) - self.WriteVariableList(ninja_file, 'cflags_objcc', - map(self.ExpandSpecial, cflags_objcc)) - self.WriteVariableList(ninja_file, 'arflags', - map(self.ExpandSpecial, arflags)) - ninja_file.newline() - outputs = [] - has_rc_source = False - for source in sources: - filename, ext = os.path.splitext(source) - ext = ext[1:] - obj_ext = self.obj_ext - if ext in ('cc', 'cpp', 'cxx'): - command = 'cxx' - self.target.uses_cpp = True - elif ext == 'c' or (ext == 'S' and self.flavor != 'win'): - command = 'cc' - elif ext == 's' and self.flavor != 'win': # Doesn't generate .o.d files. - command = 'cc_s' - elif (self.flavor == 'win' and ext == 'asm' and - not self.msvs_settings.HasExplicitAsmRules(spec)): - command = 'asm' - # Add the _asm suffix as msvs is capable of handling .cc and - # .asm files of the same name without collision. - obj_ext = '_asm.obj' - elif self.flavor == 'mac' and ext == 'm': - command = 'objc' - elif self.flavor == 'mac' and ext == 'mm': - command = 'objcxx' - self.target.uses_cpp = True - elif self.flavor == 'win' and ext == 'rc': - command = 'rc' - obj_ext = '.res' - has_rc_source = True - else: - # Ignore unhandled extensions. - continue - input = self.GypPathToNinja(source) - output = self.GypPathToUniqueOutput(filename + obj_ext) - if arch is not None: - output = AddArch(output, arch) - implicit = precompiled_header.GetObjDependencies([input], [output], arch) - variables = [] - if self.flavor == 'win': - variables, output, implicit = precompiled_header.GetFlagsModifications( - input, output, implicit, command, cflags_c, cflags_cc, - self.ExpandSpecial) - ninja_file.build(output, command, input, - implicit=[gch for _, _, gch in implicit], - order_only=predepends, variables=variables) - outputs.append(output) - - if has_rc_source: - resource_include_dirs = config.get('resource_include_dirs', include_dirs) - self.WriteVariableList(ninja_file, 'resource_includes', - [QuoteShellArgument('-I' + self.GypPathToNinja(i, env), self.flavor) - for i in resource_include_dirs]) - - self.WritePchTargets(ninja_file, pch_commands) - - ninja_file.newline() - return outputs - - def WritePchTargets(self, ninja_file, pch_commands): - """Writes ninja rules to compile prefix headers.""" - if not pch_commands: - return - - for gch, lang_flag, lang, input in pch_commands: - var_name = { - 'c': 'cflags_pch_c', - 'cc': 'cflags_pch_cc', - 'm': 'cflags_pch_objc', - 'mm': 'cflags_pch_objcc', - }[lang] - - map = { 'c': 'cc', 'cc': 'cxx', 'm': 'objc', 'mm': 'objcxx', } - cmd = map.get(lang) - ninja_file.build(gch, cmd, input, variables=[(var_name, lang_flag)]) - - def WriteLink(self, spec, config_name, config, link_deps, compile_deps): - """Write out a link step. Fills out target.binary. """ - if self.flavor != 'mac' or len(self.archs) == 1: - return self.WriteLinkForArch( - self.ninja, spec, config_name, config, link_deps, compile_deps) - else: - output = self.ComputeOutput(spec) - inputs = [self.WriteLinkForArch(self.arch_subninjas[arch], spec, - config_name, config, link_deps[arch], - compile_deps, arch=arch) - for arch in self.archs] - extra_bindings = [] - build_output = output - if not self.is_mac_bundle: - self.AppendPostbuildVariable(extra_bindings, spec, output, output) - - # TODO(yyanagisawa): more work needed to fix: - # https://code.google.com/p/gyp/issues/detail?id=411 - if (spec['type'] in ('shared_library', 'loadable_module') and - not self.is_mac_bundle): - extra_bindings.append(('lib', output)) - self.ninja.build([output, output + '.TOC'], 'solipo', inputs, - variables=extra_bindings) - else: - self.ninja.build(build_output, 'lipo', inputs, variables=extra_bindings) - return output - - def WriteLinkForArch(self, ninja_file, spec, config_name, config, - link_deps, compile_deps, arch=None): - """Write out a link step. Fills out target.binary. """ - command = { - 'executable': 'link', - 'loadable_module': 'solink_module', - 'shared_library': 'solink', - }[spec['type']] - command_suffix = '' - - implicit_deps = set() - solibs = set() - order_deps = set() - - if compile_deps: - # Normally, the compiles of the target already depend on compile_deps, - # but a shared_library target might have no sources and only link together - # a few static_library deps, so the link step also needs to depend - # on compile_deps to make sure actions in the shared_library target - # get run before the link. - order_deps.add(compile_deps) - - if 'dependencies' in spec: - # Two kinds of dependencies: - # - Linkable dependencies (like a .a or a .so): add them to the link line. - # - Non-linkable dependencies (like a rule that generates a file - # and writes a stamp file): add them to implicit_deps - extra_link_deps = set() - for dep in spec['dependencies']: - target = self.target_outputs.get(dep) - if not target: - continue - linkable = target.Linkable() - if linkable: - new_deps = [] - if (self.flavor == 'win' and - target.component_objs and - self.msvs_settings.IsUseLibraryDependencyInputs(config_name)): - new_deps = target.component_objs - if target.compile_deps: - order_deps.add(target.compile_deps) - elif self.flavor == 'win' and target.import_lib: - new_deps = [target.import_lib] - elif target.UsesToc(self.flavor): - solibs.add(target.binary) - implicit_deps.add(target.binary + '.TOC') - else: - new_deps = [target.binary] - for new_dep in new_deps: - if new_dep not in extra_link_deps: - extra_link_deps.add(new_dep) - link_deps.append(new_dep) - - final_output = target.FinalOutput() - if not linkable or final_output != target.binary: - implicit_deps.add(final_output) - - extra_bindings = [] - if self.target.uses_cpp and self.flavor != 'win': - extra_bindings.append(('ld', '$ldxx')) - - output = self.ComputeOutput(spec, arch) - if arch is None and not self.is_mac_bundle: - self.AppendPostbuildVariable(extra_bindings, spec, output, output) - - is_executable = spec['type'] == 'executable' - # The ldflags config key is not used on mac or win. On those platforms - # linker flags are set via xcode_settings and msvs_settings, respectively. - env_ldflags = os.environ.get('LDFLAGS', '').split() - if self.flavor == 'mac': - ldflags = self.xcode_settings.GetLdflags(config_name, - self.ExpandSpecial(generator_default_variables['PRODUCT_DIR']), - self.GypPathToNinja, arch) - ldflags = env_ldflags + ldflags - elif self.flavor == 'win': - manifest_base_name = self.GypPathToUniqueOutput( - self.ComputeOutputFileName(spec)) - ldflags, intermediate_manifest, manifest_files = \ - self.msvs_settings.GetLdflags(config_name, self.GypPathToNinja, - self.ExpandSpecial, manifest_base_name, - output, is_executable, - self.toplevel_build) - ldflags = env_ldflags + ldflags - self.WriteVariableList(ninja_file, 'manifests', manifest_files) - implicit_deps = implicit_deps.union(manifest_files) - if intermediate_manifest: + if not xcassets: + return + + extra_arguments = {} + settings_to_arg = { + "XCASSETS_APP_ICON": "app-icon", + "XCASSETS_LAUNCH_IMAGE": "launch-image", + } + settings = self.xcode_settings.xcode_settings[self.config_name] + for settings_key, arg_name in settings_to_arg.items(): + value = settings.get(settings_key) + if value: + extra_arguments[arg_name] = value + + partial_info_plist = None + if extra_arguments: + partial_info_plist = self.GypPathToUniqueOutput( + "assetcatalog_generated_info.plist" + ) + extra_arguments["output-partial-info-plist"] = partial_info_plist + + outputs = [] + outputs.append( + os.path.join(self.xcode_settings.GetBundleResourceFolder(), "Assets.car") + ) + if partial_info_plist: + outputs.append(partial_info_plist) + + keys = QuoteShellArgument(json.dumps(extra_arguments), self.flavor) + extra_env = self.xcode_settings.GetPerTargetSettings() + env = self.GetSortedXcodeEnv(additional_settings=extra_env) + env = self.ComputeExportEnvString(env) + + bundle_depends.extend( + self.ninja.build( + outputs, + "compile_xcassets", + xcassets, + variables=[("env", env), ("keys", keys)], + ) + ) + return partial_info_plist + + def WriteMacInfoPlist(self, partial_info_plist, bundle_depends): + """Write build rules for bundle Info.plist files.""" + info_plist, out, defines, extra_env = gyp.xcode_emulation.GetMacInfoPlist( + generator_default_variables["PRODUCT_DIR"], + self.xcode_settings, + self.GypPathToNinja, + ) + if not info_plist: + return + out = self.ExpandSpecial(out) + if defines: + # Create an intermediate file to store preprocessed results. + intermediate_plist = self.GypPathToUniqueOutput( + os.path.basename(info_plist) + ) + defines = " ".join([Define(d, self.flavor) for d in defines]) + info_plist = self.ninja.build( + intermediate_plist, + "preprocess_infoplist", + info_plist, + variables=[("defines", defines)], + ) + + env = self.GetSortedXcodeEnv(additional_settings=extra_env) + env = self.ComputeExportEnvString(env) + + if partial_info_plist: + intermediate_plist = self.GypPathToUniqueOutput("merged_info.plist") + info_plist = self.ninja.build( + intermediate_plist, "merge_infoplist", [partial_info_plist, info_plist] + ) + + keys = self.xcode_settings.GetExtraPlistItems(self.config_name) + keys = QuoteShellArgument(json.dumps(keys), self.flavor) + isBinary = self.xcode_settings.IsBinaryOutputFormat(self.config_name) + self.ninja.build( + out, + "copy_infoplist", + info_plist, + variables=[("env", env), ("keys", keys), ("binary", isBinary)], + ) + bundle_depends.append(out) + + def WriteSources( + self, + ninja_file, + config_name, + config, + sources, + predepends, + precompiled_header, + spec, + ): + """Write build rules to compile all of |sources|.""" + if self.toolset == "host": + self.ninja.variable("ar", "$ar_host") + self.ninja.variable("cc", "$cc_host") + self.ninja.variable("cxx", "$cxx_host") + self.ninja.variable("ld", "$ld_host") + self.ninja.variable("ldxx", "$ldxx_host") + self.ninja.variable("nm", "$nm_host") + self.ninja.variable("readelf", "$readelf_host") + + if self.flavor != "mac" or len(self.archs) == 1: + return self.WriteSourcesForArch( + self.ninja, + config_name, + config, + sources, + predepends, + precompiled_header, + spec, + ) + else: + return dict( + ( + arch, + self.WriteSourcesForArch( + self.arch_subninjas[arch], + config_name, + config, + sources, + predepends, + precompiled_header, + spec, + arch=arch, + ), + ) + for arch in self.archs + ) + + def WriteSourcesForArch( + self, + ninja_file, + config_name, + config, + sources, + predepends, + precompiled_header, + spec, + arch=None, + ): + """Write build rules to compile all of |sources|.""" + + extra_defines = [] + if self.flavor == "mac": + cflags = self.xcode_settings.GetCflags(config_name, arch=arch) + cflags_c = self.xcode_settings.GetCflagsC(config_name) + cflags_cc = self.xcode_settings.GetCflagsCC(config_name) + cflags_objc = ["$cflags_c"] + self.xcode_settings.GetCflagsObjC(config_name) + cflags_objcc = ["$cflags_cc"] + self.xcode_settings.GetCflagsObjCC( + config_name + ) + elif self.flavor == "win": + asmflags = self.msvs_settings.GetAsmflags(config_name) + cflags = self.msvs_settings.GetCflags(config_name) + cflags_c = self.msvs_settings.GetCflagsC(config_name) + cflags_cc = self.msvs_settings.GetCflagsCC(config_name) + extra_defines = self.msvs_settings.GetComputedDefines(config_name) + # See comment at cc_command for why there's two .pdb files. + pdbpath_c = pdbpath_cc = self.msvs_settings.GetCompilerPdbName( + config_name, self.ExpandSpecial + ) + if not pdbpath_c: + obj = "obj" + if self.toolset != "target": + obj += "." + self.toolset + pdbpath = os.path.normpath(os.path.join(obj, self.base_dir, self.name)) + pdbpath_c = pdbpath + ".c.pdb" + pdbpath_cc = pdbpath + ".cc.pdb" + self.WriteVariableList(ninja_file, "pdbname_c", [pdbpath_c]) + self.WriteVariableList(ninja_file, "pdbname_cc", [pdbpath_cc]) + self.WriteVariableList(ninja_file, "pchprefix", [self.name]) + else: + cflags = config.get("cflags", []) + cflags_c = config.get("cflags_c", []) + cflags_cc = config.get("cflags_cc", []) + + # Respect environment variables related to build, but target-specific + # flags can still override them. + if self.toolset == "target": + cflags_c = ( + os.environ.get("CPPFLAGS", "").split() + + os.environ.get("CFLAGS", "").split() + + cflags_c + ) + cflags_cc = ( + os.environ.get("CPPFLAGS", "").split() + + os.environ.get("CXXFLAGS", "").split() + + cflags_cc + ) + elif self.toolset == "host": + cflags_c = ( + os.environ.get("CPPFLAGS_host", "").split() + + os.environ.get("CFLAGS_host", "").split() + + cflags_c + ) + cflags_cc = ( + os.environ.get("CPPFLAGS_host", "").split() + + os.environ.get("CXXFLAGS_host", "").split() + + cflags_cc + ) + + defines = config.get("defines", []) + extra_defines self.WriteVariableList( - ninja_file, 'intermediatemanifest', [intermediate_manifest]) - command_suffix = _GetWinLinkRuleNameSuffix( - self.msvs_settings.IsEmbedManifest(config_name)) - def_file = self.msvs_settings.GetDefFile(self.GypPathToNinja) - if def_file: - implicit_deps.add(def_file) - else: - # Respect environment variables related to build, but target-specific - # flags can still override them. - ldflags = env_ldflags + config.get('ldflags', []) - if is_executable and len(solibs): - rpath = 'lib/' - if self.toolset != 'target': - rpath += self.toolset - ldflags.append(r'-Wl,-rpath=\$$ORIGIN/%s' % rpath) + ninja_file, "defines", [Define(d, self.flavor) for d in defines] + ) + if self.flavor == "win": + self.WriteVariableList( + ninja_file, "asmflags", map(self.ExpandSpecial, asmflags) + ) + self.WriteVariableList( + ninja_file, + "rcflags", + [ + QuoteShellArgument(self.ExpandSpecial(f), self.flavor) + for f in self.msvs_settings.GetRcflags( + config_name, self.GypPathToNinja + ) + ], + ) + + include_dirs = config.get("include_dirs", []) + + env = self.GetToolchainEnv() + if self.flavor == "win": + include_dirs = self.msvs_settings.AdjustIncludeDirs( + include_dirs, config_name + ) + self.WriteVariableList( + ninja_file, + "includes", + [ + QuoteShellArgument("-I" + self.GypPathToNinja(i, env), self.flavor) + for i in include_dirs + ], + ) + + if self.flavor == "win": + midl_include_dirs = config.get("midl_include_dirs", []) + midl_include_dirs = self.msvs_settings.AdjustMidlIncludeDirs( + midl_include_dirs, config_name + ) + self.WriteVariableList( + ninja_file, + "midl_includes", + [ + QuoteShellArgument("-I" + self.GypPathToNinja(i, env), self.flavor) + for i in midl_include_dirs + ], + ) + + pch_commands = precompiled_header.GetPchBuildCommands(arch) + if self.flavor == "mac": + # Most targets use no precompiled headers, so only write these if needed. + for ext, var in [ + ("c", "cflags_pch_c"), + ("cc", "cflags_pch_cc"), + ("m", "cflags_pch_objc"), + ("mm", "cflags_pch_objcc"), + ]: + include = precompiled_header.GetInclude(ext, arch) + if include: + ninja_file.variable(var, include) + + arflags = config.get("arflags", []) + + self.WriteVariableList(ninja_file, "cflags", map(self.ExpandSpecial, cflags)) + self.WriteVariableList( + ninja_file, "cflags_c", map(self.ExpandSpecial, cflags_c) + ) + self.WriteVariableList( + ninja_file, "cflags_cc", map(self.ExpandSpecial, cflags_cc) + ) + if self.flavor == "mac": + self.WriteVariableList( + ninja_file, "cflags_objc", map(self.ExpandSpecial, cflags_objc) + ) + self.WriteVariableList( + ninja_file, "cflags_objcc", map(self.ExpandSpecial, cflags_objcc) + ) + self.WriteVariableList(ninja_file, "arflags", map(self.ExpandSpecial, arflags)) + ninja_file.newline() + outputs = [] + has_rc_source = False + for source in sources: + filename, ext = os.path.splitext(source) + ext = ext[1:] + obj_ext = self.obj_ext + if ext in ("cc", "cpp", "cxx"): + command = "cxx" + self.target.uses_cpp = True + elif ext == "c" or (ext == "S" and self.flavor != "win"): + command = "cc" + elif ext == "s" and self.flavor != "win": # Doesn't generate .o.d files. + command = "cc_s" + elif ( + self.flavor == "win" + and ext == "asm" + and not self.msvs_settings.HasExplicitAsmRules(spec) + ): + command = "asm" + # Add the _asm suffix as msvs is capable of handling .cc and + # .asm files of the same name without collision. + obj_ext = "_asm.obj" + elif self.flavor == "mac" and ext == "m": + command = "objc" + elif self.flavor == "mac" and ext == "mm": + command = "objcxx" + self.target.uses_cpp = True + elif self.flavor == "win" and ext == "rc": + command = "rc" + obj_ext = ".res" + has_rc_source = True + else: + # Ignore unhandled extensions. + continue + input = self.GypPathToNinja(source) + output = self.GypPathToUniqueOutput(filename + obj_ext) + if arch is not None: + output = AddArch(output, arch) + implicit = precompiled_header.GetObjDependencies([input], [output], arch) + variables = [] + if self.flavor == "win": + variables, output, implicit = precompiled_header.GetFlagsModifications( + input, + output, + implicit, + command, + cflags_c, + cflags_cc, + self.ExpandSpecial, + ) + ninja_file.build( + output, + command, + input, + implicit=[gch for _, _, gch in implicit], + order_only=predepends, + variables=variables, + ) + outputs.append(output) + + if has_rc_source: + resource_include_dirs = config.get("resource_include_dirs", include_dirs) + self.WriteVariableList( + ninja_file, + "resource_includes", + [ + QuoteShellArgument("-I" + self.GypPathToNinja(i, env), self.flavor) + for i in resource_include_dirs + ], + ) + + self.WritePchTargets(ninja_file, pch_commands) + + ninja_file.newline() + return outputs + + def WritePchTargets(self, ninja_file, pch_commands): + """Writes ninja rules to compile prefix headers.""" + if not pch_commands: + return + + for gch, lang_flag, lang, input in pch_commands: + var_name = { + "c": "cflags_pch_c", + "cc": "cflags_pch_cc", + "m": "cflags_pch_objc", + "mm": "cflags_pch_objcc", + }[lang] + + map = { + "c": "cc", + "cc": "cxx", + "m": "objc", + "mm": "objcxx", + } + cmd = map.get(lang) + ninja_file.build(gch, cmd, input, variables=[(var_name, lang_flag)]) + + def WriteLink(self, spec, config_name, config, link_deps, compile_deps): + """Write out a link step. Fills out target.binary. """ + if self.flavor != "mac" or len(self.archs) == 1: + return self.WriteLinkForArch( + self.ninja, spec, config_name, config, link_deps, compile_deps + ) else: - ldflags.append('-Wl,-rpath=%s' % self.target_rpath) - ldflags.append('-Wl,-rpath-link=%s' % rpath) - self.WriteVariableList(ninja_file, 'ldflags', - map(self.ExpandSpecial, ldflags)) - - library_dirs = config.get('library_dirs', []) - if self.flavor == 'win': - library_dirs = [self.msvs_settings.ConvertVSMacros(l, config_name) - for l in library_dirs] - library_dirs = ['/LIBPATH:' + QuoteShellArgument(self.GypPathToNinja(l), - self.flavor) - for l in library_dirs] - else: - library_dirs = [QuoteShellArgument('-L' + self.GypPathToNinja(l), - self.flavor) - for l in library_dirs] - - libraries = gyp.common.uniquer(map(self.ExpandSpecial, - spec.get('libraries', []))) - if self.flavor == 'mac': - libraries = self.xcode_settings.AdjustLibraries(libraries, config_name) - elif self.flavor == 'win': - libraries = self.msvs_settings.AdjustLibraries(libraries) - - self.WriteVariableList(ninja_file, 'libs', library_dirs + libraries) - - linked_binary = output - - if command in ('solink', 'solink_module'): - extra_bindings.append(('soname', os.path.split(output)[1])) - extra_bindings.append(('lib', - gyp.common.EncodePOSIXShellArgument(output))) - if self.flavor != 'win': - link_file_list = output - if self.is_mac_bundle: - # 'Dependency Framework.framework/Versions/A/Dependency Framework' -> - # 'Dependency Framework.framework.rsp' - link_file_list = self.xcode_settings.GetWrapperName() - if arch: - link_file_list += '.' + arch - link_file_list += '.rsp' - # If an rspfile contains spaces, ninja surrounds the filename with - # quotes around it and then passes it to open(), creating a file with - # quotes in its name (and when looking for the rsp file, the name - # makes it through bash which strips the quotes) :-/ - link_file_list = link_file_list.replace(' ', '_') - extra_bindings.append( - ('link_file_list', - gyp.common.EncodePOSIXShellArgument(link_file_list))) - if self.flavor == 'win': - extra_bindings.append(('binary', output)) - if ('/NOENTRY' not in ldflags and - not self.msvs_settings.GetNoImportLibrary(config_name)): - self.target.import_lib = output + '.lib' - extra_bindings.append(('implibflag', - '/IMPLIB:%s' % self.target.import_lib)) - pdbname = self.msvs_settings.GetPDBName( - config_name, self.ExpandSpecial, output + '.pdb') - output = [output, self.target.import_lib] - if pdbname: - output.append(pdbname) - elif not self.is_mac_bundle: - output = [output, output + '.TOC'] - else: - command = command + '_notoc' - elif self.flavor == 'win': - extra_bindings.append(('binary', output)) - pdbname = self.msvs_settings.GetPDBName( - config_name, self.ExpandSpecial, output + '.pdb') - if pdbname: - output = [output, pdbname] - - - if len(solibs): - extra_bindings.append(('solibs', - gyp.common.EncodePOSIXShellList(sorted(solibs)))) - - ninja_file.build(output, command + command_suffix, link_deps, - implicit=sorted(implicit_deps), - order_only=list(order_deps), - variables=extra_bindings) - return linked_binary - - def WriteTarget(self, spec, config_name, config, link_deps, compile_deps): - extra_link_deps = any(self.target_outputs.get(dep).Linkable() - for dep in spec.get('dependencies', []) - if dep in self.target_outputs) - if spec['type'] == 'none' or (not link_deps and not extra_link_deps): - # TODO(evan): don't call this function for 'none' target types, as - # it doesn't do anything, and we fake out a 'binary' with a stamp file. - self.target.binary = compile_deps - self.target.type = 'none' - elif spec['type'] == 'static_library': - self.target.binary = self.ComputeOutput(spec) - if (self.flavor not in ('mac', 'openbsd', 'netbsd', 'win') and not - self.is_standalone_static_library): - self.ninja.build(self.target.binary, 'alink_thin', link_deps, - order_only=compile_deps) - else: + output = self.ComputeOutput(spec) + inputs = [ + self.WriteLinkForArch( + self.arch_subninjas[arch], + spec, + config_name, + config, + link_deps[arch], + compile_deps, + arch=arch, + ) + for arch in self.archs + ] + extra_bindings = [] + build_output = output + if not self.is_mac_bundle: + self.AppendPostbuildVariable(extra_bindings, spec, output, output) + + # TODO(yyanagisawa): more work needed to fix: + # https://code.google.com/p/gyp/issues/detail?id=411 + if ( + spec["type"] in ("shared_library", "loadable_module") + and not self.is_mac_bundle + ): + extra_bindings.append(("lib", output)) + self.ninja.build( + [output, output + ".TOC"], + "solipo", + inputs, + variables=extra_bindings, + ) + else: + self.ninja.build(build_output, "lipo", inputs, variables=extra_bindings) + return output + + def WriteLinkForArch( + self, ninja_file, spec, config_name, config, link_deps, compile_deps, arch=None + ): + """Write out a link step. Fills out target.binary. """ + command = { + "executable": "link", + "loadable_module": "solink_module", + "shared_library": "solink", + }[spec["type"]] + command_suffix = "" + + implicit_deps = set() + solibs = set() + order_deps = set() + + if compile_deps: + # Normally, the compiles of the target already depend on compile_deps, + # but a shared_library target might have no sources and only link together + # a few static_library deps, so the link step also needs to depend + # on compile_deps to make sure actions in the shared_library target + # get run before the link. + order_deps.add(compile_deps) + + if "dependencies" in spec: + # Two kinds of dependencies: + # - Linkable dependencies (like a .a or a .so): add them to the link line. + # - Non-linkable dependencies (like a rule that generates a file + # and writes a stamp file): add them to implicit_deps + extra_link_deps = set() + for dep in spec["dependencies"]: + target = self.target_outputs.get(dep) + if not target: + continue + linkable = target.Linkable() + if linkable: + new_deps = [] + if ( + self.flavor == "win" + and target.component_objs + and self.msvs_settings.IsUseLibraryDependencyInputs(config_name) + ): + new_deps = target.component_objs + if target.compile_deps: + order_deps.add(target.compile_deps) + elif self.flavor == "win" and target.import_lib: + new_deps = [target.import_lib] + elif target.UsesToc(self.flavor): + solibs.add(target.binary) + implicit_deps.add(target.binary + ".TOC") + else: + new_deps = [target.binary] + for new_dep in new_deps: + if new_dep not in extra_link_deps: + extra_link_deps.add(new_dep) + link_deps.append(new_dep) + + final_output = target.FinalOutput() + if not linkable or final_output != target.binary: + implicit_deps.add(final_output) + + extra_bindings = [] + if self.target.uses_cpp and self.flavor != "win": + extra_bindings.append(("ld", "$ldxx")) + + output = self.ComputeOutput(spec, arch) + if arch is None and not self.is_mac_bundle: + self.AppendPostbuildVariable(extra_bindings, spec, output, output) + + is_executable = spec["type"] == "executable" + # The ldflags config key is not used on mac or win. On those platforms + # linker flags are set via xcode_settings and msvs_settings, respectively. + env_ldflags = os.environ.get("LDFLAGS", "").split() + if self.flavor == "mac": + ldflags = self.xcode_settings.GetLdflags( + config_name, + self.ExpandSpecial(generator_default_variables["PRODUCT_DIR"]), + self.GypPathToNinja, + arch, + ) + ldflags = env_ldflags + ldflags + elif self.flavor == "win": + manifest_base_name = self.GypPathToUniqueOutput( + self.ComputeOutputFileName(spec) + ) + ( + ldflags, + intermediate_manifest, + manifest_files, + ) = self.msvs_settings.GetLdflags( + config_name, + self.GypPathToNinja, + self.ExpandSpecial, + manifest_base_name, + output, + is_executable, + self.toplevel_build, + ) + ldflags = env_ldflags + ldflags + self.WriteVariableList(ninja_file, "manifests", manifest_files) + implicit_deps = implicit_deps.union(manifest_files) + if intermediate_manifest: + self.WriteVariableList( + ninja_file, "intermediatemanifest", [intermediate_manifest] + ) + command_suffix = _GetWinLinkRuleNameSuffix( + self.msvs_settings.IsEmbedManifest(config_name) + ) + def_file = self.msvs_settings.GetDefFile(self.GypPathToNinja) + if def_file: + implicit_deps.add(def_file) + else: + # Respect environment variables related to build, but target-specific + # flags can still override them. + ldflags = env_ldflags + config.get("ldflags", []) + if is_executable and len(solibs): + rpath = "lib/" + if self.toolset != "target": + rpath += self.toolset + ldflags.append(r"-Wl,-rpath=\$$ORIGIN/%s" % rpath) + else: + ldflags.append("-Wl,-rpath=%s" % self.target_rpath) + ldflags.append("-Wl,-rpath-link=%s" % rpath) + self.WriteVariableList(ninja_file, "ldflags", map(self.ExpandSpecial, ldflags)) + + library_dirs = config.get("library_dirs", []) + if self.flavor == "win": + library_dirs = [ + self.msvs_settings.ConvertVSMacros(library_dir, config_name) + for library_dir in library_dirs + ] + library_dirs = [ + "/LIBPATH:" + + QuoteShellArgument(self.GypPathToNinja(library_dir), self.flavor) + for library_dir in library_dirs + ] + else: + library_dirs = [ + QuoteShellArgument("-L" + self.GypPathToNinja(library_dir), self.flavor) + for library_dir in library_dirs + ] + + libraries = gyp.common.uniquer( + map(self.ExpandSpecial, spec.get("libraries", [])) + ) + if self.flavor == "mac": + libraries = self.xcode_settings.AdjustLibraries(libraries, config_name) + elif self.flavor == "win": + libraries = self.msvs_settings.AdjustLibraries(libraries) + + self.WriteVariableList(ninja_file, "libs", library_dirs + libraries) + + linked_binary = output + + if command in ("solink", "solink_module"): + extra_bindings.append(("soname", os.path.split(output)[1])) + extra_bindings.append(("lib", gyp.common.EncodePOSIXShellArgument(output))) + if self.flavor != "win": + link_file_list = output + if self.is_mac_bundle: + # 'Dependency Framework.framework/Versions/A/Dependency Framework' + # -> 'Dependency Framework.framework.rsp' + link_file_list = self.xcode_settings.GetWrapperName() + if arch: + link_file_list += "." + arch + link_file_list += ".rsp" + # If an rspfile contains spaces, ninja surrounds the filename with + # quotes around it and then passes it to open(), creating a file with + # quotes in its name (and when looking for the rsp file, the name + # makes it through bash which strips the quotes) :-/ + link_file_list = link_file_list.replace(" ", "_") + extra_bindings.append( + ( + "link_file_list", + gyp.common.EncodePOSIXShellArgument(link_file_list), + ) + ) + if self.flavor == "win": + extra_bindings.append(("binary", output)) + if ( + "/NOENTRY" not in ldflags + and not self.msvs_settings.GetNoImportLibrary(config_name) + ): + self.target.import_lib = output + ".lib" + extra_bindings.append( + ("implibflag", "/IMPLIB:%s" % self.target.import_lib) + ) + pdbname = self.msvs_settings.GetPDBName( + config_name, self.ExpandSpecial, output + ".pdb" + ) + output = [output, self.target.import_lib] + if pdbname: + output.append(pdbname) + elif not self.is_mac_bundle: + output = [output, output + ".TOC"] + else: + command = command + "_notoc" + elif self.flavor == "win": + extra_bindings.append(("binary", output)) + pdbname = self.msvs_settings.GetPDBName( + config_name, self.ExpandSpecial, output + ".pdb" + ) + if pdbname: + output = [output, pdbname] + + if len(solibs): + extra_bindings.append( + ("solibs", gyp.common.EncodePOSIXShellList(sorted(solibs))) + ) + + ninja_file.build( + output, + command + command_suffix, + link_deps, + implicit=sorted(implicit_deps), + order_only=list(order_deps), + variables=extra_bindings, + ) + return linked_binary + + def WriteTarget(self, spec, config_name, config, link_deps, compile_deps): + extra_link_deps = any( + self.target_outputs.get(dep).Linkable() + for dep in spec.get("dependencies", []) + if dep in self.target_outputs + ) + if spec["type"] == "none" or (not link_deps and not extra_link_deps): + # TODO(evan): don't call this function for 'none' target types, as + # it doesn't do anything, and we fake out a 'binary' with a stamp file. + self.target.binary = compile_deps + self.target.type = "none" + elif spec["type"] == "static_library": + self.target.binary = self.ComputeOutput(spec) + if ( + self.flavor not in ("mac", "openbsd", "netbsd", "win") + and not self.is_standalone_static_library + ): + self.ninja.build( + self.target.binary, "alink_thin", link_deps, order_only=compile_deps + ) + else: + variables = [] + if self.xcode_settings: + libtool_flags = self.xcode_settings.GetLibtoolflags(config_name) + if libtool_flags: + variables.append(("libtool_flags", libtool_flags)) + if self.msvs_settings: + libflags = self.msvs_settings.GetLibFlags( + config_name, self.GypPathToNinja + ) + variables.append(("libflags", libflags)) + + if self.flavor != "mac" or len(self.archs) == 1: + self.AppendPostbuildVariable( + variables, spec, self.target.binary, self.target.binary + ) + self.ninja.build( + self.target.binary, + "alink", + link_deps, + order_only=compile_deps, + variables=variables, + ) + else: + inputs = [] + for arch in self.archs: + output = self.ComputeOutput(spec, arch) + self.arch_subninjas[arch].build( + output, + "alink", + link_deps[arch], + order_only=compile_deps, + variables=variables, + ) + inputs.append(output) + # TODO: It's not clear if + # libtool_flags should be passed to the alink + # call that combines single-arch .a files into a fat .a file. + self.AppendPostbuildVariable( + variables, spec, self.target.binary, self.target.binary + ) + self.ninja.build( + self.target.binary, + "alink", + inputs, + # FIXME: test proving order_only=compile_deps isn't + # needed. + variables=variables, + ) + else: + self.target.binary = self.WriteLink( + spec, config_name, config, link_deps, compile_deps + ) + return self.target.binary + + def WriteMacBundle(self, spec, mac_bundle_depends, is_empty): + assert self.is_mac_bundle + package_framework = spec["type"] in ("shared_library", "loadable_module") + output = self.ComputeMacBundleOutput() + if is_empty: + output += ".stamp" variables = [] - if self.xcode_settings: - libtool_flags = self.xcode_settings.GetLibtoolflags(config_name) - if libtool_flags: - variables.append(('libtool_flags', libtool_flags)) - if self.msvs_settings: - libflags = self.msvs_settings.GetLibFlags(config_name, - self.GypPathToNinja) - variables.append(('libflags', libflags)) - - if self.flavor != 'mac' or len(self.archs) == 1: - self.AppendPostbuildVariable(variables, spec, - self.target.binary, self.target.binary) - self.ninja.build(self.target.binary, 'alink', link_deps, - order_only=compile_deps, variables=variables) + self.AppendPostbuildVariable( + variables, + spec, + output, + self.target.binary, + is_command_start=not package_framework, + ) + if package_framework and not is_empty: + if spec["type"] == "shared_library" and self.xcode_settings.isIOS: + self.ninja.build( + output, + "package_ios_framework", + mac_bundle_depends, + variables=variables, + ) + else: + variables.append(("version", self.xcode_settings.GetFrameworkVersion())) + self.ninja.build( + output, "package_framework", mac_bundle_depends, variables=variables + ) else: - inputs = [] - for arch in self.archs: - output = self.ComputeOutput(spec, arch) - self.arch_subninjas[arch].build(output, 'alink', link_deps[arch], - order_only=compile_deps, - variables=variables) - inputs.append(output) - # TODO: It's not clear if libtool_flags should be passed to the alink - # call that combines single-arch .a files into a fat .a file. - self.AppendPostbuildVariable(variables, spec, - self.target.binary, self.target.binary) - self.ninja.build(self.target.binary, 'alink', inputs, - # FIXME: test proving order_only=compile_deps isn't - # needed. - variables=variables) - else: - self.target.binary = self.WriteLink(spec, config_name, config, link_deps, - compile_deps) - return self.target.binary - - def WriteMacBundle(self, spec, mac_bundle_depends, is_empty): - assert self.is_mac_bundle - package_framework = spec['type'] in ('shared_library', 'loadable_module') - output = self.ComputeMacBundleOutput() - if is_empty: - output += '.stamp' - variables = [] - self.AppendPostbuildVariable(variables, spec, output, self.target.binary, - is_command_start=not package_framework) - if package_framework and not is_empty: - if spec['type'] == 'shared_library' and self.xcode_settings.isIOS: - self.ninja.build(output, 'package_ios_framework', mac_bundle_depends, - variables=variables) - else: - variables.append(('version', self.xcode_settings.GetFrameworkVersion())) - self.ninja.build(output, 'package_framework', mac_bundle_depends, - variables=variables) - else: - self.ninja.build(output, 'stamp', mac_bundle_depends, - variables=variables) - self.target.bundle = output - return output - - def GetToolchainEnv(self, additional_settings=None): - """Returns the variables toolchain would set for build steps.""" - env = self.GetSortedXcodeEnv(additional_settings=additional_settings) - if self.flavor == 'win': - env = self.GetMsvsToolchainEnv( - additional_settings=additional_settings) - return env - - def GetMsvsToolchainEnv(self, additional_settings=None): - """Returns the variables Visual Studio would set for build steps.""" - return self.msvs_settings.GetVSMacroEnv('$!PRODUCT_DIR', - config=self.config_name) - - def GetSortedXcodeEnv(self, additional_settings=None): - """Returns the variables Xcode would set for build steps.""" - assert self.abs_build_dir - abs_build_dir = self.abs_build_dir - return gyp.xcode_emulation.GetSortedXcodeEnv( - self.xcode_settings, abs_build_dir, - os.path.join(abs_build_dir, self.build_to_base), self.config_name, - additional_settings) - - def GetSortedXcodePostbuildEnv(self): - """Returns the variables Xcode would set for postbuild steps.""" - postbuild_settings = {} - # CHROMIUM_STRIP_SAVE_FILE is a chromium-specific hack. - # TODO(thakis): It would be nice to have some general mechanism instead. - strip_save_file = self.xcode_settings.GetPerTargetSetting( - 'CHROMIUM_STRIP_SAVE_FILE') - if strip_save_file: - postbuild_settings['CHROMIUM_STRIP_SAVE_FILE'] = strip_save_file - return self.GetSortedXcodeEnv(additional_settings=postbuild_settings) - - def AppendPostbuildVariable(self, variables, spec, output, binary, - is_command_start=False): - """Adds a 'postbuild' variable if there is a postbuild for |output|.""" - postbuild = self.GetPostbuildCommand(spec, output, binary, is_command_start) - if postbuild: - variables.append(('postbuilds', postbuild)) - - def GetPostbuildCommand(self, spec, output, output_binary, is_command_start): - """Returns a shell command that runs all the postbuilds, and removes + self.ninja.build(output, "stamp", mac_bundle_depends, variables=variables) + self.target.bundle = output + return output + + def GetToolchainEnv(self, additional_settings=None): + """Returns the variables toolchain would set for build steps.""" + env = self.GetSortedXcodeEnv(additional_settings=additional_settings) + if self.flavor == "win": + env = self.GetMsvsToolchainEnv(additional_settings=additional_settings) + return env + + def GetMsvsToolchainEnv(self, additional_settings=None): + """Returns the variables Visual Studio would set for build steps.""" + return self.msvs_settings.GetVSMacroEnv( + "$!PRODUCT_DIR", config=self.config_name + ) + + def GetSortedXcodeEnv(self, additional_settings=None): + """Returns the variables Xcode would set for build steps.""" + assert self.abs_build_dir + abs_build_dir = self.abs_build_dir + return gyp.xcode_emulation.GetSortedXcodeEnv( + self.xcode_settings, + abs_build_dir, + os.path.join(abs_build_dir, self.build_to_base), + self.config_name, + additional_settings, + ) + + def GetSortedXcodePostbuildEnv(self): + """Returns the variables Xcode would set for postbuild steps.""" + postbuild_settings = {} + # CHROMIUM_STRIP_SAVE_FILE is a chromium-specific hack. + # TODO(thakis): It would be nice to have some general mechanism instead. + strip_save_file = self.xcode_settings.GetPerTargetSetting( + "CHROMIUM_STRIP_SAVE_FILE" + ) + if strip_save_file: + postbuild_settings["CHROMIUM_STRIP_SAVE_FILE"] = strip_save_file + return self.GetSortedXcodeEnv(additional_settings=postbuild_settings) + + def AppendPostbuildVariable( + self, variables, spec, output, binary, is_command_start=False + ): + """Adds a 'postbuild' variable if there is a postbuild for |output|.""" + postbuild = self.GetPostbuildCommand(spec, output, binary, is_command_start) + if postbuild: + variables.append(("postbuilds", postbuild)) + + def GetPostbuildCommand(self, spec, output, output_binary, is_command_start): + """Returns a shell command that runs all the postbuilds, and removes |output| if any of them fails. If |is_command_start| is False, then the returned string will start with ' && '.""" - if not self.xcode_settings or spec['type'] == 'none' or not output: - return '' - output = QuoteShellArgument(output, self.flavor) - postbuilds = gyp.xcode_emulation.GetSpecPostbuildCommands(spec, quiet=True) - if output_binary is not None: - postbuilds = self.xcode_settings.AddImplicitPostbuilds( - self.config_name, - os.path.normpath(os.path.join(self.base_to_build, output)), - QuoteShellArgument( - os.path.normpath(os.path.join(self.base_to_build, output_binary)), - self.flavor), - postbuilds, quiet=True) - - if not postbuilds: - return '' - # Postbuilds expect to be run in the gyp file's directory, so insert an - # implicit postbuild to cd to there. - postbuilds.insert(0, gyp.common.EncodePOSIXShellList( - ['cd', self.build_to_base])) - env = self.ComputeExportEnvString(self.GetSortedXcodePostbuildEnv()) - # G will be non-null if any postbuild fails. Run all postbuilds in a - # subshell. - commands = env + ' (' + \ - ' && '.join([ninja_syntax.escape(command) for command in postbuilds]) - command_string = (commands + '); G=$$?; ' - # Remove the final output if any postbuild failed. - '((exit $$G) || rm -rf %s) ' % output + '&& exit $$G)') - if is_command_start: - return '(' + command_string + ' && ' - else: - return '$ && (' + command_string + if not self.xcode_settings or spec["type"] == "none" or not output: + return "" + output = QuoteShellArgument(output, self.flavor) + postbuilds = gyp.xcode_emulation.GetSpecPostbuildCommands(spec, quiet=True) + if output_binary is not None: + postbuilds = self.xcode_settings.AddImplicitPostbuilds( + self.config_name, + os.path.normpath(os.path.join(self.base_to_build, output)), + QuoteShellArgument( + os.path.normpath(os.path.join(self.base_to_build, output_binary)), + self.flavor, + ), + postbuilds, + quiet=True, + ) + + if not postbuilds: + return "" + # Postbuilds expect to be run in the gyp file's directory, so insert an + # implicit postbuild to cd to there. + postbuilds.insert( + 0, gyp.common.EncodePOSIXShellList(["cd", self.build_to_base]) + ) + env = self.ComputeExportEnvString(self.GetSortedXcodePostbuildEnv()) + # G will be non-null if any postbuild fails. Run all postbuilds in a + # subshell. + commands = ( + env + + " (" + + " && ".join([ninja_syntax.escape(command) for command in postbuilds]) + ) + command_string = ( + commands + + "); G=$$?; " + # Remove the final output if any postbuild failed. + "((exit $$G) || rm -rf %s) " % output + + "&& exit $$G)" + ) + if is_command_start: + return "(" + command_string + " && " + else: + return "$ && (" + command_string - def ComputeExportEnvString(self, env): - """Given an environment, returns a string looking like + def ComputeExportEnvString(self, env): + """Given an environment, returns a string looking like 'export FOO=foo; export BAR="${FOO} bar;' that exports |env| to the shell.""" - export_str = [] - for k, v in env: - export_str.append('export %s=%s;' % - (k, ninja_syntax.escape(gyp.common.EncodePOSIXShellArgument(v)))) - return ' '.join(export_str) - - def ComputeMacBundleOutput(self): - """Return the 'output' (full output path) to a bundle output directory.""" - assert self.is_mac_bundle - path = generator_default_variables['PRODUCT_DIR'] - return self.ExpandSpecial( - os.path.join(path, self.xcode_settings.GetWrapperName())) - - def ComputeOutputFileName(self, spec, type=None): - """Compute the filename of the final output for the current target.""" - if not type: - type = spec['type'] - - default_variables = copy.copy(generator_default_variables) - CalculateVariables(default_variables, {'flavor': self.flavor}) - - # Compute filename prefix: the product prefix, or a default for - # the product type. - DEFAULT_PREFIX = { - 'loadable_module': default_variables['SHARED_LIB_PREFIX'], - 'shared_library': default_variables['SHARED_LIB_PREFIX'], - 'static_library': default_variables['STATIC_LIB_PREFIX'], - 'executable': default_variables['EXECUTABLE_PREFIX'], - } - prefix = spec.get('product_prefix', DEFAULT_PREFIX.get(type, '')) - - # Compute filename extension: the product extension, or a default - # for the product type. - DEFAULT_EXTENSION = { - 'loadable_module': default_variables['SHARED_LIB_SUFFIX'], - 'shared_library': default_variables['SHARED_LIB_SUFFIX'], - 'static_library': default_variables['STATIC_LIB_SUFFIX'], - 'executable': default_variables['EXECUTABLE_SUFFIX'], - } - extension = spec.get('product_extension') - if extension: - extension = '.' + extension - else: - extension = DEFAULT_EXTENSION.get(type, '') - - if 'product_name' in spec: - # If we were given an explicit name, use that. - target = spec['product_name'] - else: - # Otherwise, derive a name from the target name. - target = spec['target_name'] - if prefix == 'lib': - # Snip out an extra 'lib' from libs if appropriate. - target = StripPrefix(target, 'lib') - - if type in ('static_library', 'loadable_module', 'shared_library', - 'executable'): - return '%s%s%s' % (prefix, target, extension) - elif type == 'none': - return '%s.stamp' % target - else: - raise Exception('Unhandled output type %s' % type) - - def ComputeOutput(self, spec, arch=None): - """Compute the path for the final output of the spec.""" - type = spec['type'] - - if self.flavor == 'win': - override = self.msvs_settings.GetOutputName(self.config_name, - self.ExpandSpecial) - if override: - return override + export_str = [] + for k, v in env: + export_str.append( + "export %s=%s;" + % (k, ninja_syntax.escape(gyp.common.EncodePOSIXShellArgument(v))) + ) + return " ".join(export_str) + + def ComputeMacBundleOutput(self): + """Return the 'output' (full output path) to a bundle output directory.""" + assert self.is_mac_bundle + path = generator_default_variables["PRODUCT_DIR"] + return self.ExpandSpecial( + os.path.join(path, self.xcode_settings.GetWrapperName()) + ) + + def ComputeOutputFileName(self, spec, type=None): + """Compute the filename of the final output for the current target.""" + if not type: + type = spec["type"] + + default_variables = copy.copy(generator_default_variables) + CalculateVariables(default_variables, {"flavor": self.flavor}) + + # Compute filename prefix: the product prefix, or a default for + # the product type. + DEFAULT_PREFIX = { + "loadable_module": default_variables["SHARED_LIB_PREFIX"], + "shared_library": default_variables["SHARED_LIB_PREFIX"], + "static_library": default_variables["STATIC_LIB_PREFIX"], + "executable": default_variables["EXECUTABLE_PREFIX"], + } + prefix = spec.get("product_prefix", DEFAULT_PREFIX.get(type, "")) + + # Compute filename extension: the product extension, or a default + # for the product type. + DEFAULT_EXTENSION = { + "loadable_module": default_variables["SHARED_LIB_SUFFIX"], + "shared_library": default_variables["SHARED_LIB_SUFFIX"], + "static_library": default_variables["STATIC_LIB_SUFFIX"], + "executable": default_variables["EXECUTABLE_SUFFIX"], + } + extension = spec.get("product_extension") + if extension: + extension = "." + extension + else: + extension = DEFAULT_EXTENSION.get(type, "") - if arch is None and self.flavor == 'mac' and type in ( - 'static_library', 'executable', 'shared_library', 'loadable_module'): - filename = self.xcode_settings.GetExecutablePath() - else: - filename = self.ComputeOutputFileName(spec, type) - - if arch is None and 'product_dir' in spec: - path = os.path.join(spec['product_dir'], filename) - return self.ExpandSpecial(path) - - # Some products go into the output root, libraries go into shared library - # dir, and everything else goes into the normal place. - type_in_output_root = ['executable', 'loadable_module'] - if self.flavor == 'mac' and self.toolset == 'target': - type_in_output_root += ['shared_library', 'static_library'] - elif self.flavor == 'win' and self.toolset == 'target': - type_in_output_root += ['shared_library'] - - if arch is not None: - # Make sure partial executables don't end up in a bundle or the regular - # output directory. - archdir = 'arch' - if self.toolset != 'target': - archdir = os.path.join('arch', '%s' % self.toolset) - return os.path.join(archdir, AddArch(filename, arch)) - elif type in type_in_output_root or self.is_standalone_static_library: - return filename - elif type == 'shared_library': - libdir = 'lib' - if self.toolset != 'target': - libdir = os.path.join('lib', '%s' % self.toolset) - return os.path.join(libdir, filename) - else: - return self.GypPathToUniqueOutput(filename, qualified=False) + if "product_name" in spec: + # If we were given an explicit name, use that. + target = spec["product_name"] + else: + # Otherwise, derive a name from the target name. + target = spec["target_name"] + if prefix == "lib": + # Snip out an extra 'lib' from libs if appropriate. + target = StripPrefix(target, "lib") + + if type in ( + "static_library", + "loadable_module", + "shared_library", + "executable", + ): + return "%s%s%s" % (prefix, target, extension) + elif type == "none": + return "%s.stamp" % target + else: + raise Exception("Unhandled output type %s" % type) + + def ComputeOutput(self, spec, arch=None): + """Compute the path for the final output of the spec.""" + type = spec["type"] + + if self.flavor == "win": + override = self.msvs_settings.GetOutputName( + self.config_name, self.ExpandSpecial + ) + if override: + return override + + if ( + arch is None + and self.flavor == "mac" + and type + in ("static_library", "executable", "shared_library", "loadable_module") + ): + filename = self.xcode_settings.GetExecutablePath() + else: + filename = self.ComputeOutputFileName(spec, type) + + if arch is None and "product_dir" in spec: + path = os.path.join(spec["product_dir"], filename) + return self.ExpandSpecial(path) + + # Some products go into the output root, libraries go into shared library + # dir, and everything else goes into the normal place. + type_in_output_root = ["executable", "loadable_module"] + if self.flavor == "mac" and self.toolset == "target": + type_in_output_root += ["shared_library", "static_library"] + elif self.flavor == "win" and self.toolset == "target": + type_in_output_root += ["shared_library"] + + if arch is not None: + # Make sure partial executables don't end up in a bundle or the regular + # output directory. + archdir = "arch" + if self.toolset != "target": + archdir = os.path.join("arch", "%s" % self.toolset) + return os.path.join(archdir, AddArch(filename, arch)) + elif type in type_in_output_root or self.is_standalone_static_library: + return filename + elif type == "shared_library": + libdir = "lib" + if self.toolset != "target": + libdir = os.path.join("lib", "%s" % self.toolset) + return os.path.join(libdir, filename) + else: + return self.GypPathToUniqueOutput(filename, qualified=False) - def WriteVariableList(self, ninja_file, var, values): - assert not isinstance(values, str) - if values is None: - values = [] - ninja_file.variable(var, ' '.join(values)) + def WriteVariableList(self, ninja_file, var, values): + assert not isinstance(values, str) + if values is None: + values = [] + ninja_file.variable(var, " ".join(values)) - def WriteNewNinjaRule(self, name, args, description, is_cygwin, env, pool, - depfile=None): - """Write out a new ninja "rule" statement for a given command. + def WriteNewNinjaRule( + self, name, args, description, is_cygwin, env, pool, depfile=None + ): + """Write out a new ninja "rule" statement for a given command. Returns the name of the new rule, and a copy of |args| with variables expanded.""" - if self.flavor == 'win': - args = [self.msvs_settings.ConvertVSMacros( - arg, self.base_to_build, config=self.config_name) - for arg in args] - description = self.msvs_settings.ConvertVSMacros( - description, config=self.config_name) - elif self.flavor == 'mac': - # |env| is an empty list on non-mac. - args = [gyp.xcode_emulation.ExpandEnvVars(arg, env) for arg in args] - description = gyp.xcode_emulation.ExpandEnvVars(description, env) - - # TODO: we shouldn't need to qualify names; we do it because - # currently the ninja rule namespace is global, but it really - # should be scoped to the subninja. - rule_name = self.name - if self.toolset == 'target': - rule_name += '.' + self.toolset - rule_name += '.' + name - rule_name = re.sub('[^a-zA-Z0-9_]', '_', rule_name) - - # Remove variable references, but not if they refer to the magic rule - # variables. This is not quite right, as it also protects these for - # actions, not just for rules where they are valid. Good enough. - protect = [ '${root}', '${dirname}', '${source}', '${ext}', '${name}' ] - protect = '(?!' + '|'.join(map(re.escape, protect)) + ')' - description = re.sub(protect + r'\$', '_', description) - - # gyp dictates that commands are run from the base directory. - # cd into the directory before running, and adjust paths in - # the arguments to point to the proper locations. - rspfile = None - rspfile_content = None - args = [self.ExpandSpecial(arg, self.base_to_build) for arg in args] - if self.flavor == 'win': - rspfile = rule_name + '.$unique_name.rsp' - # The cygwin case handles this inside the bash sub-shell. - run_in = '' if is_cygwin else ' ' + self.build_to_base - if is_cygwin: - rspfile_content = self.msvs_settings.BuildCygwinBashCommandLine( - args, self.build_to_base) - else: - rspfile_content = gyp.msvs_emulation.EncodeRspFileList(args) - command = ('%s gyp-win-tool action-wrapper $arch ' % sys.executable + - rspfile + run_in) - else: - env = self.ComputeExportEnvString(env) - command = gyp.common.EncodePOSIXShellList(args) - command = 'cd %s; ' % self.build_to_base + env + command - - # GYP rules/actions express being no-ops by not touching their outputs. - # Avoid executing downstream dependencies in this case by specifying - # restat=1 to ninja. - self.ninja.rule(rule_name, command, description, depfile=depfile, - restat=True, pool=pool, - rspfile=rspfile, rspfile_content=rspfile_content) - self.ninja.newline() - - return rule_name, args + if self.flavor == "win": + args = [ + self.msvs_settings.ConvertVSMacros( + arg, self.base_to_build, config=self.config_name + ) + for arg in args + ] + description = self.msvs_settings.ConvertVSMacros( + description, config=self.config_name + ) + elif self.flavor == "mac": + # |env| is an empty list on non-mac. + args = [gyp.xcode_emulation.ExpandEnvVars(arg, env) for arg in args] + description = gyp.xcode_emulation.ExpandEnvVars(description, env) + + # TODO: we shouldn't need to qualify names; we do it because + # currently the ninja rule namespace is global, but it really + # should be scoped to the subninja. + rule_name = self.name + if self.toolset == "target": + rule_name += "." + self.toolset + rule_name += "." + name + rule_name = re.sub("[^a-zA-Z0-9_]", "_", rule_name) + + # Remove variable references, but not if they refer to the magic rule + # variables. This is not quite right, as it also protects these for + # actions, not just for rules where they are valid. Good enough. + protect = ["${root}", "${dirname}", "${source}", "${ext}", "${name}"] + protect = "(?!" + "|".join(map(re.escape, protect)) + ")" + description = re.sub(protect + r"\$", "_", description) + + # gyp dictates that commands are run from the base directory. + # cd into the directory before running, and adjust paths in + # the arguments to point to the proper locations. + rspfile = None + rspfile_content = None + args = [self.ExpandSpecial(arg, self.base_to_build) for arg in args] + if self.flavor == "win": + rspfile = rule_name + ".$unique_name.rsp" + # The cygwin case handles this inside the bash sub-shell. + run_in = "" if is_cygwin else " " + self.build_to_base + if is_cygwin: + rspfile_content = self.msvs_settings.BuildCygwinBashCommandLine( + args, self.build_to_base + ) + else: + rspfile_content = gyp.msvs_emulation.EncodeRspFileList(args) + command = ( + "%s gyp-win-tool action-wrapper $arch " % sys.executable + + rspfile + + run_in + ) + else: + env = self.ComputeExportEnvString(env) + command = gyp.common.EncodePOSIXShellList(args) + command = "cd %s; " % self.build_to_base + env + command + + # GYP rules/actions express being no-ops by not touching their outputs. + # Avoid executing downstream dependencies in this case by specifying + # restat=1 to ninja. + self.ninja.rule( + rule_name, + command, + description, + depfile=depfile, + restat=True, + pool=pool, + rspfile=rspfile, + rspfile_content=rspfile_content, + ) + self.ninja.newline() + + return rule_name, args def CalculateVariables(default_variables, params): - """Calculate additional variables for use in the build (called by gyp).""" - global generator_additional_non_configuration_keys - global generator_additional_path_sections - flavor = gyp.common.GetFlavor(params) - if flavor == 'mac': - default_variables.setdefault('OS', 'mac') - default_variables.setdefault('SHARED_LIB_SUFFIX', '.dylib') - default_variables.setdefault('SHARED_LIB_DIR', - generator_default_variables['PRODUCT_DIR']) - default_variables.setdefault('LIB_DIR', - generator_default_variables['PRODUCT_DIR']) - - # Copy additional generator configuration data from Xcode, which is shared - # by the Mac Ninja generator. - import gyp.generator.xcode as xcode_generator - generator_additional_non_configuration_keys = getattr(xcode_generator, - 'generator_additional_non_configuration_keys', []) - generator_additional_path_sections = getattr(xcode_generator, - 'generator_additional_path_sections', []) - global generator_extra_sources_for_rules - generator_extra_sources_for_rules = getattr(xcode_generator, - 'generator_extra_sources_for_rules', []) - elif flavor == 'win': - exts = gyp.MSVSUtil.TARGET_TYPE_EXT - default_variables.setdefault('OS', 'win') - default_variables['EXECUTABLE_SUFFIX'] = '.' + exts['executable'] - default_variables['STATIC_LIB_PREFIX'] = '' - default_variables['STATIC_LIB_SUFFIX'] = '.' + exts['static_library'] - default_variables['SHARED_LIB_PREFIX'] = '' - default_variables['SHARED_LIB_SUFFIX'] = '.' + exts['shared_library'] - - # Copy additional generator configuration data from VS, which is shared - # by the Windows Ninja generator. - import gyp.generator.msvs as msvs_generator - generator_additional_non_configuration_keys = getattr(msvs_generator, - 'generator_additional_non_configuration_keys', []) - generator_additional_path_sections = getattr(msvs_generator, - 'generator_additional_path_sections', []) - - gyp.msvs_emulation.CalculateCommonVariables(default_variables, params) - else: - operating_system = flavor - if flavor == 'android': - operating_system = 'linux' # Keep this legacy behavior for now. - default_variables.setdefault('OS', operating_system) - default_variables.setdefault('SHARED_LIB_SUFFIX', '.so') - default_variables.setdefault('SHARED_LIB_DIR', - os.path.join('$!PRODUCT_DIR', 'lib')) - default_variables.setdefault('LIB_DIR', - os.path.join('$!PRODUCT_DIR', 'obj')) + """Calculate additional variables for use in the build (called by gyp).""" + global generator_additional_non_configuration_keys + global generator_additional_path_sections + flavor = gyp.common.GetFlavor(params) + if flavor == "mac": + default_variables.setdefault("OS", "mac") + default_variables.setdefault("SHARED_LIB_SUFFIX", ".dylib") + default_variables.setdefault( + "SHARED_LIB_DIR", generator_default_variables["PRODUCT_DIR"] + ) + default_variables.setdefault( + "LIB_DIR", generator_default_variables["PRODUCT_DIR"] + ) + + # Copy additional generator configuration data from Xcode, which is shared + # by the Mac Ninja generator. + import gyp.generator.xcode as xcode_generator + + generator_additional_non_configuration_keys = getattr( + xcode_generator, "generator_additional_non_configuration_keys", [] + ) + generator_additional_path_sections = getattr( + xcode_generator, "generator_additional_path_sections", [] + ) + global generator_extra_sources_for_rules + generator_extra_sources_for_rules = getattr( + xcode_generator, "generator_extra_sources_for_rules", [] + ) + elif flavor == "win": + exts = gyp.MSVSUtil.TARGET_TYPE_EXT + default_variables.setdefault("OS", "win") + default_variables["EXECUTABLE_SUFFIX"] = "." + exts["executable"] + default_variables["STATIC_LIB_PREFIX"] = "" + default_variables["STATIC_LIB_SUFFIX"] = "." + exts["static_library"] + default_variables["SHARED_LIB_PREFIX"] = "" + default_variables["SHARED_LIB_SUFFIX"] = "." + exts["shared_library"] + + # Copy additional generator configuration data from VS, which is shared + # by the Windows Ninja generator. + import gyp.generator.msvs as msvs_generator + + generator_additional_non_configuration_keys = getattr( + msvs_generator, "generator_additional_non_configuration_keys", [] + ) + generator_additional_path_sections = getattr( + msvs_generator, "generator_additional_path_sections", [] + ) + + gyp.msvs_emulation.CalculateCommonVariables(default_variables, params) + else: + operating_system = flavor + if flavor == "android": + operating_system = "linux" # Keep this legacy behavior for now. + default_variables.setdefault("OS", operating_system) + default_variables.setdefault("SHARED_LIB_SUFFIX", ".so") + default_variables.setdefault( + "SHARED_LIB_DIR", os.path.join("$!PRODUCT_DIR", "lib") + ) + default_variables.setdefault("LIB_DIR", os.path.join("$!PRODUCT_DIR", "obj")) + def ComputeOutputDir(params): - """Returns the path from the toplevel_dir to the build output directory.""" - # generator_dir: relative path from pwd to where make puts build files. - # Makes migrating from make to ninja easier, ninja doesn't put anything here. - generator_dir = os.path.relpath(params['options'].generator_output or '.') + """Returns the path from the toplevel_dir to the build output directory.""" + # generator_dir: relative path from pwd to where make puts build files. + # Makes migrating from make to ninja easier, ninja doesn't put anything here. + generator_dir = os.path.relpath(params["options"].generator_output or ".") - # output_dir: relative path from generator_dir to the build directory. - output_dir = params.get('generator_flags', {}).get('output_dir', 'out') + # output_dir: relative path from generator_dir to the build directory. + output_dir = params.get("generator_flags", {}).get("output_dir", "out") - # Relative path from source root to our output files. e.g. "out" - return os.path.normpath(os.path.join(generator_dir, output_dir)) + # Relative path from source root to our output files. e.g. "out" + return os.path.normpath(os.path.join(generator_dir, output_dir)) def CalculateGeneratorInputInfo(params): - """Called by __init__ to initialize generator values based on params.""" - # E.g. "out/gypfiles" - toplevel = params['options'].toplevel_dir - qualified_out_dir = os.path.normpath(os.path.join( - toplevel, ComputeOutputDir(params), 'gypfiles')) - - global generator_filelist_paths - generator_filelist_paths = { - 'toplevel': toplevel, - 'qualified_out_dir': qualified_out_dir, - } + """Called by __init__ to initialize generator values based on params.""" + # E.g. "out/gypfiles" + toplevel = params["options"].toplevel_dir + qualified_out_dir = os.path.normpath( + os.path.join(toplevel, ComputeOutputDir(params), "gypfiles") + ) + + global generator_filelist_paths + generator_filelist_paths = { + "toplevel": toplevel, + "qualified_out_dir": qualified_out_dir, + } -def OpenOutput(path, mode='w'): - """Open |path| for writing, creating directories if necessary.""" - gyp.common.EnsureDirExists(path) - return open(path, mode) +def OpenOutput(path, mode="w"): + """Open |path| for writing, creating directories if necessary.""" + gyp.common.EnsureDirExists(path) + return open(path, mode) def CommandWithWrapper(cmd, wrappers, prog): - wrapper = wrappers.get(cmd, '') - if wrapper: - return wrapper + ' ' + prog - return prog + wrapper = wrappers.get(cmd, "") + if wrapper: + return wrapper + " " + prog + return prog def GetDefaultConcurrentLinks(): - """Returns a best-guess for a number of concurrent links.""" - pool_size = int(os.environ.get('GYP_LINK_CONCURRENCY', 0)) - if pool_size: - return pool_size - - if sys.platform in ('win32', 'cygwin'): - import ctypes - - class MEMORYSTATUSEX(ctypes.Structure): - _fields_ = [ - ("dwLength", ctypes.c_ulong), - ("dwMemoryLoad", ctypes.c_ulong), - ("ullTotalPhys", ctypes.c_ulonglong), - ("ullAvailPhys", ctypes.c_ulonglong), - ("ullTotalPageFile", ctypes.c_ulonglong), - ("ullAvailPageFile", ctypes.c_ulonglong), - ("ullTotalVirtual", ctypes.c_ulonglong), - ("ullAvailVirtual", ctypes.c_ulonglong), - ("sullAvailExtendedVirtual", ctypes.c_ulonglong), - ] - - stat = MEMORYSTATUSEX() - stat.dwLength = ctypes.sizeof(stat) - ctypes.windll.kernel32.GlobalMemoryStatusEx(ctypes.byref(stat)) - - # VS 2015 uses 20% more working set than VS 2013 and can consume all RAM - # on a 64 GB machine. - mem_limit = max(1, stat.ullTotalPhys / (5 * (2 ** 30))) # total / 5GB - hard_cap = max(1, int(os.environ.get('GYP_LINK_CONCURRENCY_MAX', 2**32))) - return min(mem_limit, hard_cap) - elif sys.platform.startswith('linux'): - if os.path.exists("/proc/meminfo"): - with open("/proc/meminfo") as meminfo: - memtotal_re = re.compile(r'^MemTotal:\s*(\d*)\s*kB') - for line in meminfo: - match = memtotal_re.match(line) - if not match: - continue - # Allow 8Gb per link on Linux because Gold is quite memory hungry - return max(1, int(match.group(1)) / (8 * (2 ** 20))) - return 1 - elif sys.platform == 'darwin': - try: - avail_bytes = int(subprocess.check_output(['sysctl', '-n', 'hw.memsize'])) - # A static library debug build of Chromium's unit_tests takes ~2.7GB, so - # 4GB per ld process allows for some more bloat. - return max(1, avail_bytes / (4 * (2 ** 30))) # total / 4GB - except: - return 1 - else: - # TODO(scottmg): Implement this for other platforms. - return 1 + """Returns a best-guess for a number of concurrent links.""" + pool_size = int(os.environ.get("GYP_LINK_CONCURRENCY", 0)) + if pool_size: + return pool_size + + if sys.platform in ("win32", "cygwin"): + import ctypes + + class MEMORYSTATUSEX(ctypes.Structure): + _fields_ = [ + ("dwLength", ctypes.c_ulong), + ("dwMemoryLoad", ctypes.c_ulong), + ("ullTotalPhys", ctypes.c_ulonglong), + ("ullAvailPhys", ctypes.c_ulonglong), + ("ullTotalPageFile", ctypes.c_ulonglong), + ("ullAvailPageFile", ctypes.c_ulonglong), + ("ullTotalVirtual", ctypes.c_ulonglong), + ("ullAvailVirtual", ctypes.c_ulonglong), + ("sullAvailExtendedVirtual", ctypes.c_ulonglong), + ] + + stat = MEMORYSTATUSEX() + stat.dwLength = ctypes.sizeof(stat) + ctypes.windll.kernel32.GlobalMemoryStatusEx(ctypes.byref(stat)) + + # VS 2015 uses 20% more working set than VS 2013 and can consume all RAM + # on a 64 GB machine. + mem_limit = max(1, stat.ullTotalPhys // (5 * (2 ** 30))) # total / 5GB + hard_cap = max(1, int(os.environ.get("GYP_LINK_CONCURRENCY_MAX", 2 ** 32))) + return min(mem_limit, hard_cap) + elif sys.platform.startswith("linux"): + if os.path.exists("/proc/meminfo"): + with open("/proc/meminfo") as meminfo: + memtotal_re = re.compile(r"^MemTotal:\s*(\d*)\s*kB") + for line in meminfo: + match = memtotal_re.match(line) + if not match: + continue + # Allow 8Gb per link on Linux because Gold is quite memory hungry + return max(1, int(match.group(1)) // (8 * (2 ** 20))) + return 1 + elif sys.platform == "darwin": + try: + avail_bytes = int(subprocess.check_output(["sysctl", "-n", "hw.memsize"])) + # A static library debug build of Chromium's unit_tests takes ~2.7GB, so + # 4GB per ld process allows for some more bloat. + return max(1, avail_bytes // (4 * (2 ** 30))) # total / 4GB + except subprocess.CalledProcessError: + return 1 + else: + # TODO(scottmg): Implement this for other platforms. + return 1 def _GetWinLinkRuleNameSuffix(embed_manifest): - """Returns the suffix used to select an appropriate linking rule depending on + """Returns the suffix used to select an appropriate linking rule depending on whether the manifest embedding is enabled.""" - return '_embed' if embed_manifest else '' + return "_embed" if embed_manifest else "" def _AddWinLinkRules(master_ninja, embed_manifest): - """Adds link rules for Windows platform to |master_ninja|.""" - def FullLinkCommand(ldcmd, out, binary_type): - resource_name = { - 'exe': '1', - 'dll': '2', - }[binary_type] - return '%(python)s gyp-win-tool link-with-manifests $arch %(embed)s ' \ - '%(out)s "%(ldcmd)s" %(resname)s $mt $rc "$intermediatemanifest" ' \ - '$manifests' % { - 'python': sys.executable, - 'out': out, - 'ldcmd': ldcmd, - 'resname': resource_name, - 'embed': embed_manifest } - rule_name_suffix = _GetWinLinkRuleNameSuffix(embed_manifest) - use_separate_mspdbsrv = ( - int(os.environ.get('GYP_USE_SEPARATE_MSPDBSRV', '0')) != 0) - dlldesc = 'LINK%s(DLL) $binary' % rule_name_suffix.upper() - dllcmd = ('%s gyp-win-tool link-wrapper $arch %s ' - '$ld /nologo $implibflag /DLL /OUT:$binary ' - '@$binary.rsp' % (sys.executable, use_separate_mspdbsrv)) - dllcmd = FullLinkCommand(dllcmd, '$binary', 'dll') - master_ninja.rule('solink' + rule_name_suffix, - description=dlldesc, command=dllcmd, - rspfile='$binary.rsp', - rspfile_content='$libs $in_newline $ldflags', - restat=True, - pool='link_pool') - master_ninja.rule('solink_module' + rule_name_suffix, - description=dlldesc, command=dllcmd, - rspfile='$binary.rsp', - rspfile_content='$libs $in_newline $ldflags', - restat=True, - pool='link_pool') - # Note that ldflags goes at the end so that it has the option of - # overriding default settings earlier in the command line. - exe_cmd = ('%s gyp-win-tool link-wrapper $arch %s ' - '$ld /nologo /OUT:$binary @$binary.rsp' % - (sys.executable, use_separate_mspdbsrv)) - exe_cmd = FullLinkCommand(exe_cmd, '$binary', 'exe') - master_ninja.rule('link' + rule_name_suffix, - description='LINK%s $binary' % rule_name_suffix.upper(), - command=exe_cmd, - rspfile='$binary.rsp', - rspfile_content='$in_newline $libs $ldflags', - pool='link_pool') - - -def GenerateOutputForConfig(target_list, target_dicts, data, params, - config_name): - options = params['options'] - flavor = gyp.common.GetFlavor(params) - generator_flags = params.get('generator_flags', {}) - - # build_dir: relative path from source root to our output files. - # e.g. "out/Debug" - build_dir = os.path.normpath( - os.path.join(ComputeOutputDir(params), config_name)) - - toplevel_build = os.path.join(options.toplevel_dir, build_dir) - - master_ninja_file = OpenOutput(os.path.join(toplevel_build, 'build.ninja')) - master_ninja = ninja_syntax.Writer(master_ninja_file, width=120) - - # Put build-time support tools in out/{config_name}. - gyp.common.CopyTool(flavor, toplevel_build, generator_flags) - - # Grab make settings for CC/CXX. - # The rules are - # - The priority from low to high is gcc/g++, the 'make_global_settings' in - # gyp, the environment variable. - # - If there is no 'make_global_settings' for CC.host/CXX.host or - # 'CC_host'/'CXX_host' environment variable, cc_host/cxx_host should be set - # to cc/cxx. - if flavor == 'win': - ar = 'lib.exe' - # cc and cxx must be set to the correct architecture by overriding with one - # of cl_x86 or cl_x64 below. - cc = 'UNSET' - cxx = 'UNSET' - ld = 'link.exe' - ld_host = '$ld' - else: - ar = 'ar' - cc = 'cc' - cxx = 'c++' - ld = '$cc' - ldxx = '$cxx' - ld_host = '$cc_host' - ldxx_host = '$cxx_host' - - ar_host = ar - cc_host = None - cxx_host = None - cc_host_global_setting = None - cxx_host_global_setting = None - clang_cl = None - nm = 'nm' - nm_host = 'nm' - readelf = 'readelf' - readelf_host = 'readelf' - - build_file, _, _ = gyp.common.ParseQualifiedTarget(target_list[0]) - make_global_settings = data[build_file].get('make_global_settings', []) - build_to_root = gyp.common.InvertRelativePath(build_dir, - options.toplevel_dir) - wrappers = {} - for key, value in make_global_settings: - if key == 'AR': - ar = os.path.join(build_to_root, value) - if key == 'AR.host': - ar_host = os.path.join(build_to_root, value) - if key == 'CC': - cc = os.path.join(build_to_root, value) - if cc.endswith('clang-cl'): - clang_cl = cc - if key == 'CXX': - cxx = os.path.join(build_to_root, value) - if key == 'CC.host': - cc_host = os.path.join(build_to_root, value) - cc_host_global_setting = value - if key == 'CXX.host': - cxx_host = os.path.join(build_to_root, value) - cxx_host_global_setting = value - if key == 'LD': - ld = os.path.join(build_to_root, value) - if key == 'LD.host': - ld_host = os.path.join(build_to_root, value) - if key == 'LDXX': - ldxx = os.path.join(build_to_root, value) - if key == 'LDXX.host': - ldxx_host = os.path.join(build_to_root, value) - if key == 'NM': - nm = os.path.join(build_to_root, value) - if key == 'NM.host': - nm_host = os.path.join(build_to_root, value) - if key == 'READELF': - readelf = os.path.join(build_to_root, value) - if key == 'READELF.host': - readelf_host = os.path.join(build_to_root, value) - if key.endswith('_wrapper'): - wrappers[key[:-len('_wrapper')]] = os.path.join(build_to_root, value) - - # Support wrappers from environment variables too. - for key, value in os.environ.items(): - if key.lower().endswith('_wrapper'): - key_prefix = key[:-len('_wrapper')] - key_prefix = re.sub(r'\.HOST$', '.host', key_prefix) - wrappers[key_prefix] = os.path.join(build_to_root, value) - - mac_toolchain_dir = generator_flags.get('mac_toolchain_dir', None) - if mac_toolchain_dir: - wrappers['LINK'] = "export DEVELOPER_DIR='%s' &&" % mac_toolchain_dir - - if flavor == 'win': - configs = [target_dicts[qualified_target]['configurations'][config_name] - for qualified_target in target_list] - shared_system_includes = None - if not generator_flags.get('ninja_use_custom_environment_files', 0): - shared_system_includes = \ - gyp.msvs_emulation.ExtractSharedMSVSSystemIncludes( - configs, generator_flags) - cl_paths = gyp.msvs_emulation.GenerateEnvironmentFiles( - toplevel_build, generator_flags, shared_system_includes, OpenOutput) - for arch, path in sorted(cl_paths.items()): - if clang_cl: - # If we have selected clang-cl, use that instead. - path = clang_cl - command = CommandWithWrapper('CC', wrappers, - QuoteShellArgument(path, 'win')) - if clang_cl: - # Use clang-cl to cross-compile for x86 or x86_64. - command += (' -m32' if arch == 'x86' else ' -m64') - master_ninja.variable('cl_' + arch, command) - - cc = GetEnvironFallback(['CC_target', 'CC'], cc) - master_ninja.variable('cc', CommandWithWrapper('CC', wrappers, cc)) - cxx = GetEnvironFallback(['CXX_target', 'CXX'], cxx) - master_ninja.variable('cxx', CommandWithWrapper('CXX', wrappers, cxx)) - - if flavor == 'win': - master_ninja.variable('ld', ld) - master_ninja.variable('idl', 'midl.exe') - master_ninja.variable('ar', ar) - master_ninja.variable('rc', 'rc.exe') - master_ninja.variable('ml_x86', 'ml.exe') - master_ninja.variable('ml_x64', 'ml64.exe') - master_ninja.variable('mt', 'mt.exe') - else: - master_ninja.variable('ld', CommandWithWrapper('LINK', wrappers, ld)) - master_ninja.variable('ldxx', CommandWithWrapper('LINK', wrappers, ldxx)) - master_ninja.variable('ar', GetEnvironFallback(['AR_target', 'AR'], ar)) - if flavor != 'mac': - # Mac does not use readelf/nm for .TOC generation, so avoiding polluting - # the master ninja with extra unused variables. - master_ninja.variable( - 'nm', GetEnvironFallback(['NM_target', 'NM'], nm)) - master_ninja.variable( - 'readelf', GetEnvironFallback(['READELF_target', 'READELF'], readelf)) - - if generator_supports_multiple_toolsets: - if not cc_host: - cc_host = cc - if not cxx_host: - cxx_host = cxx - - master_ninja.variable('ar_host', GetEnvironFallback(['AR_host'], ar_host)) - master_ninja.variable('nm_host', GetEnvironFallback(['NM_host'], nm_host)) - master_ninja.variable('readelf_host', - GetEnvironFallback(['READELF_host'], readelf_host)) - cc_host = GetEnvironFallback(['CC_host'], cc_host) - cxx_host = GetEnvironFallback(['CXX_host'], cxx_host) - - # The environment variable could be used in 'make_global_settings', like - # ['CC.host', '$(CC)'] or ['CXX.host', '$(CXX)'], transform them here. - if '$(CC)' in cc_host and cc_host_global_setting: - cc_host = cc_host_global_setting.replace('$(CC)', cc) - if '$(CXX)' in cxx_host and cxx_host_global_setting: - cxx_host = cxx_host_global_setting.replace('$(CXX)', cxx) - master_ninja.variable('cc_host', - CommandWithWrapper('CC.host', wrappers, cc_host)) - master_ninja.variable('cxx_host', - CommandWithWrapper('CXX.host', wrappers, cxx_host)) - if flavor == 'win': - master_ninja.variable('ld_host', ld_host) - master_ninja.variable('ldxx_host', ldxx_host) - else: - master_ninja.variable('ld_host', CommandWithWrapper( - 'LINK', wrappers, ld_host)) - master_ninja.variable('ldxx_host', CommandWithWrapper( - 'LINK', wrappers, ldxx_host)) - - master_ninja.newline() - - master_ninja.pool('link_pool', depth=GetDefaultConcurrentLinks()) - master_ninja.newline() - - deps = 'msvc' if flavor == 'win' else 'gcc' - - if flavor != 'win': - master_ninja.rule( - 'cc', - description='CC $out', - command=('$cc -MMD -MF $out.d $defines $includes $cflags $cflags_c ' - '$cflags_pch_c -c $in -o $out'), - depfile='$out.d', - deps=deps) - master_ninja.rule( - 'cc_s', - description='CC $out', - command=('$cc $defines $includes $cflags $cflags_c ' - '$cflags_pch_c -c $in -o $out')) - master_ninja.rule( - 'cxx', - description='CXX $out', - command=('$cxx -MMD -MF $out.d $defines $includes $cflags $cflags_cc ' - '$cflags_pch_cc -c $in -o $out'), - depfile='$out.d', - deps=deps) - else: - # TODO(scottmg) Separate pdb names is a test to see if it works around - # http://crbug.com/142362. It seems there's a race between the creation of - # the .pdb by the precompiled header step for .cc and the compilation of - # .c files. This should be handled by mspdbsrv, but rarely errors out with - # c1xx : fatal error C1033: cannot open program database - # By making the rules target separate pdb files this might be avoided. - cc_command = ('ninja -t msvc -e $arch ' + - '-- ' - '$cc /nologo /showIncludes /FC ' - '@$out.rsp /c $in /Fo$out /Fd$pdbname_c ') - cxx_command = ('ninja -t msvc -e $arch ' + - '-- ' - '$cxx /nologo /showIncludes /FC ' - '@$out.rsp /c $in /Fo$out /Fd$pdbname_cc ') - master_ninja.rule( - 'cc', - description='CC $out', - command=cc_command, - rspfile='$out.rsp', - rspfile_content='$defines $includes $cflags $cflags_c', - deps=deps) - master_ninja.rule( - 'cxx', - description='CXX $out', - command=cxx_command, - rspfile='$out.rsp', - rspfile_content='$defines $includes $cflags $cflags_cc', - deps=deps) - master_ninja.rule( - 'idl', - description='IDL $in', - command=('%s gyp-win-tool midl-wrapper $arch $outdir ' - '$tlb $h $dlldata $iid $proxy $in ' - '$midl_includes $idlflags' % sys.executable)) - master_ninja.rule( - 'rc', - description='RC $in', - # Note: $in must be last otherwise rc.exe complains. - command=('%s gyp-win-tool rc-wrapper ' - '$arch $rc $defines $resource_includes $rcflags /fo$out $in' % - sys.executable)) - master_ninja.rule( - 'asm', - description='ASM $out', - command=('%s gyp-win-tool asm-wrapper ' - '$arch $asm $defines $includes $asmflags /c /Fo $out $in' % - sys.executable)) - - if flavor != 'mac' and flavor != 'win': - master_ninja.rule( - 'alink', - description='AR $out', - command='rm -f $out && $ar rcs $arflags $out $in') - master_ninja.rule( - 'alink_thin', - description='AR $out', - command='rm -f $out && $ar rcsT $arflags $out $in') - - # This allows targets that only need to depend on $lib's API to declare an - # order-only dependency on $lib.TOC and avoid relinking such downstream - # dependencies when $lib changes only in non-public ways. - # The resulting string leaves an uninterpolated %{suffix} which - # is used in the final substitution below. - mtime_preserving_solink_base = ( - 'if [ ! -e $lib -o ! -e $lib.TOC ]; then ' - '%(solink)s && %(extract_toc)s > $lib.TOC; else ' - '%(solink)s && %(extract_toc)s > $lib.tmp && ' - 'if ! cmp -s $lib.tmp $lib.TOC; then mv $lib.tmp $lib.TOC ; ' - 'fi; fi' - % { 'solink': - '$ld -shared $ldflags -o $lib -Wl,-soname=$soname %(suffix)s', - 'extract_toc': - ('{ $readelf -d $lib | grep SONAME ; ' - '$nm -gD -f p $lib | cut -f1-2 -d\' \'; }')}) - - master_ninja.rule( - 'solink', - description='SOLINK $lib', - restat=True, - command=mtime_preserving_solink_base % {'suffix': '@$link_file_list'}, - rspfile='$link_file_list', - rspfile_content= - '-Wl,--whole-archive $in $solibs -Wl,--no-whole-archive $libs', - pool='link_pool') - master_ninja.rule( - 'solink_module', - description='SOLINK(module) $lib', - restat=True, - command=mtime_preserving_solink_base % {'suffix': '@$link_file_list'}, - rspfile='$link_file_list', - rspfile_content='-Wl,--start-group $in $solibs $libs -Wl,--end-group', - pool='link_pool') - master_ninja.rule( - 'link', - description='LINK $out', - command=('$ld $ldflags -o $out ' - '-Wl,--start-group $in $solibs $libs -Wl,--end-group'), - pool='link_pool') - elif flavor == 'win': + """Adds link rules for Windows platform to |master_ninja|.""" + + def FullLinkCommand(ldcmd, out, binary_type): + resource_name = {"exe": "1", "dll": "2"}[binary_type] + return ( + "%(python)s gyp-win-tool link-with-manifests $arch %(embed)s " + '%(out)s "%(ldcmd)s" %(resname)s $mt $rc "$intermediatemanifest" ' + "$manifests" + % { + "python": sys.executable, + "out": out, + "ldcmd": ldcmd, + "resname": resource_name, + "embed": embed_manifest, + } + ) + + rule_name_suffix = _GetWinLinkRuleNameSuffix(embed_manifest) + use_separate_mspdbsrv = int(os.environ.get("GYP_USE_SEPARATE_MSPDBSRV", "0")) != 0 + dlldesc = "LINK%s(DLL) $binary" % rule_name_suffix.upper() + dllcmd = ( + "%s gyp-win-tool link-wrapper $arch %s " + "$ld /nologo $implibflag /DLL /OUT:$binary " + "@$binary.rsp" % (sys.executable, use_separate_mspdbsrv) + ) + dllcmd = FullLinkCommand(dllcmd, "$binary", "dll") master_ninja.rule( - 'alink', - description='LIB $out', - command=('%s gyp-win-tool link-wrapper $arch False ' - '$ar /nologo /ignore:4221 /OUT:$out @$out.rsp' % - sys.executable), - rspfile='$out.rsp', - rspfile_content='$in_newline $libflags') - _AddWinLinkRules(master_ninja, embed_manifest=True) - _AddWinLinkRules(master_ninja, embed_manifest=False) - else: + "solink" + rule_name_suffix, + description=dlldesc, + command=dllcmd, + rspfile="$binary.rsp", + rspfile_content="$libs $in_newline $ldflags", + restat=True, + pool="link_pool", + ) master_ninja.rule( - 'objc', - description='OBJC $out', - command=('$cc -MMD -MF $out.d $defines $includes $cflags $cflags_objc ' - '$cflags_pch_objc -c $in -o $out'), - depfile='$out.d', - deps=deps) + "solink_module" + rule_name_suffix, + description=dlldesc, + command=dllcmd, + rspfile="$binary.rsp", + rspfile_content="$libs $in_newline $ldflags", + restat=True, + pool="link_pool", + ) + # Note that ldflags goes at the end so that it has the option of + # overriding default settings earlier in the command line. + exe_cmd = ( + "%s gyp-win-tool link-wrapper $arch %s " + "$ld /nologo /OUT:$binary @$binary.rsp" + % (sys.executable, use_separate_mspdbsrv) + ) + exe_cmd = FullLinkCommand(exe_cmd, "$binary", "exe") master_ninja.rule( - 'objcxx', - description='OBJCXX $out', - command=('$cxx -MMD -MF $out.d $defines $includes $cflags $cflags_objcc ' - '$cflags_pch_objcc -c $in -o $out'), - depfile='$out.d', - deps=deps) - master_ninja.rule( - 'alink', - description='LIBTOOL-STATIC $out, POSTBUILDS', - command='rm -f $out && ' - './gyp-mac-tool filter-libtool libtool $libtool_flags ' - '-static -o $out $in' - '$postbuilds') - master_ninja.rule( - 'lipo', - description='LIPO $out, POSTBUILDS', - command='rm -f $out && lipo -create $in -output $out$postbuilds') - master_ninja.rule( - 'solipo', - description='SOLIPO $out, POSTBUILDS', - command=( - 'rm -f $lib $lib.TOC && lipo -create $in -output $lib$postbuilds &&' - '%(extract_toc)s > $lib.TOC' - % { 'extract_toc': - '{ otool -l $lib | grep LC_ID_DYLIB -A 5; ' - 'nm -gP $lib | cut -f1-2 -d\' \' | grep -v U$$; true; }'})) - - - # Record the public interface of $lib in $lib.TOC. See the corresponding - # comment in the posix section above for details. - solink_base = '$ld %(type)s $ldflags -o $lib %(suffix)s' - mtime_preserving_solink_base = ( - 'if [ ! -e $lib -o ! -e $lib.TOC ] || ' - # Always force dependent targets to relink if this library - # reexports something. Handling this correctly would require - # recursive TOC dumping but this is rare in practice, so punt. - 'otool -l $lib | grep -q LC_REEXPORT_DYLIB ; then ' - '%(solink)s && %(extract_toc)s > $lib.TOC; ' - 'else ' - '%(solink)s && %(extract_toc)s > $lib.tmp && ' - 'if ! cmp -s $lib.tmp $lib.TOC; then ' - 'mv $lib.tmp $lib.TOC ; ' - 'fi; ' - 'fi' - % { 'solink': solink_base, - 'extract_toc': - '{ otool -l $lib | grep LC_ID_DYLIB -A 5; ' - 'nm -gP $lib | cut -f1-2 -d\' \' | grep -v U$$; true; }'}) - - - solink_suffix = '@$link_file_list$postbuilds' - master_ninja.rule( - 'solink', - description='SOLINK $lib, POSTBUILDS', - restat=True, - command=mtime_preserving_solink_base % {'suffix': solink_suffix, - 'type': '-shared'}, - rspfile='$link_file_list', - rspfile_content='$in $solibs $libs', - pool='link_pool') - master_ninja.rule( - 'solink_notoc', - description='SOLINK $lib, POSTBUILDS', - restat=True, - command=solink_base % {'suffix':solink_suffix, 'type': '-shared'}, - rspfile='$link_file_list', - rspfile_content='$in $solibs $libs', - pool='link_pool') - - master_ninja.rule( - 'solink_module', - description='SOLINK(module) $lib, POSTBUILDS', - restat=True, - command=mtime_preserving_solink_base % {'suffix': solink_suffix, - 'type': '-bundle'}, - rspfile='$link_file_list', - rspfile_content='$in $solibs $libs', - pool='link_pool') - master_ninja.rule( - 'solink_module_notoc', - description='SOLINK(module) $lib, POSTBUILDS', - restat=True, - command=solink_base % {'suffix': solink_suffix, 'type': '-bundle'}, - rspfile='$link_file_list', - rspfile_content='$in $solibs $libs', - pool='link_pool') - - master_ninja.rule( - 'link', - description='LINK $out, POSTBUILDS', - command=('$ld $ldflags -o $out ' - '$in $solibs $libs$postbuilds'), - pool='link_pool') - master_ninja.rule( - 'preprocess_infoplist', - description='PREPROCESS INFOPLIST $out', - command=('$cc -E -P -Wno-trigraphs -x c $defines $in -o $out && ' - 'plutil -convert xml1 $out $out')) - master_ninja.rule( - 'copy_infoplist', - description='COPY INFOPLIST $in', - command='$env ./gyp-mac-tool copy-info-plist $in $out $binary $keys') - master_ninja.rule( - 'merge_infoplist', - description='MERGE INFOPLISTS $in', - command='$env ./gyp-mac-tool merge-info-plist $out $in') - master_ninja.rule( - 'compile_xcassets', - description='COMPILE XCASSETS $in', - command='$env ./gyp-mac-tool compile-xcassets $keys $in') - master_ninja.rule( - 'compile_ios_framework_headers', - description='COMPILE HEADER MAPS AND COPY FRAMEWORK HEADERS $in', - command='$env ./gyp-mac-tool compile-ios-framework-header-map $out ' - '$framework $in && $env ./gyp-mac-tool ' - 'copy-ios-framework-headers $framework $copy_headers') - master_ninja.rule( - 'mac_tool', - description='MACTOOL $mactool_cmd $in', - command='$env ./gyp-mac-tool $mactool_cmd $in $out $binary') - master_ninja.rule( - 'package_framework', - description='PACKAGE FRAMEWORK $out, POSTBUILDS', - command='./gyp-mac-tool package-framework $out $version$postbuilds ' - '&& touch $out') - master_ninja.rule( - 'package_ios_framework', - description='PACKAGE IOS FRAMEWORK $out, POSTBUILDS', - command='./gyp-mac-tool package-ios-framework $out $postbuilds ' - '&& touch $out') - if flavor == 'win': - master_ninja.rule( - 'stamp', - description='STAMP $out', - command='%s gyp-win-tool stamp $out' % sys.executable) - else: - master_ninja.rule( - 'stamp', - description='STAMP $out', - command='${postbuilds}touch $out') - if flavor == 'win': - master_ninja.rule( - 'copy', - description='COPY $in $out', - command='%s gyp-win-tool recursive-mirror $in $out' % sys.executable) - elif flavor == 'zos': - master_ninja.rule( - 'copy', - description='COPY $in $out', - command='rm -rf $out && cp -fRP $in $out') - else: - master_ninja.rule( - 'copy', - description='COPY $in $out', - command='ln -f $in $out 2>/dev/null || (rm -rf $out && cp -af $in $out)') - master_ninja.newline() - - all_targets = set() - for build_file in params['build_files']: - for target in gyp.common.AllTargets(target_list, - target_dicts, - os.path.normpath(build_file)): - all_targets.add(target) - all_outputs = set() - - # target_outputs is a map from qualified target name to a Target object. - target_outputs = {} - # target_short_names is a map from target short name to a list of Target - # objects. - target_short_names = {} - - # short name of targets that were skipped because they didn't contain anything - # interesting. - # NOTE: there may be overlap between this an non_empty_target_names. - empty_target_names = set() - - # Set of non-empty short target names. - # NOTE: there may be overlap between this an empty_target_names. - non_empty_target_names = set() - - for qualified_target in target_list: - # qualified_target is like: third_party/icu/icu.gyp:icui18n#target - build_file, name, toolset = \ - gyp.common.ParseQualifiedTarget(qualified_target) - - this_make_global_settings = data[build_file].get('make_global_settings', []) - assert make_global_settings == this_make_global_settings, ( - "make_global_settings needs to be the same for all targets. %s vs. %s" % - (this_make_global_settings, make_global_settings)) - - spec = target_dicts[qualified_target] - if flavor == 'mac': - gyp.xcode_emulation.MergeGlobalXcodeSettingsToSpec(data[build_file], spec) - - # If build_file is a symlink, we must not follow it because there's a chance - # it could point to a path above toplevel_dir, and we cannot correctly deal - # with that case at the moment. - build_file = gyp.common.RelativePath(build_file, options.toplevel_dir, - False) - - qualified_target_for_hash = gyp.common.QualifiedTarget(build_file, name, - toolset) - qualified_target_for_hash = qualified_target_for_hash.encode('utf-8') - hash_for_rules = hashlib.md5(qualified_target_for_hash).hexdigest() - - base_path = os.path.dirname(build_file) - obj = 'obj' - if toolset != 'target': - obj += '.' + toolset - output_file = os.path.join(obj, base_path, name + '.ninja') - - ninja_output = StringIO() - writer = NinjaWriter(hash_for_rules, target_outputs, base_path, build_dir, - ninja_output, - toplevel_build, output_file, - flavor, toplevel_dir=options.toplevel_dir) - - target = writer.WriteSpec(spec, config_name, generator_flags) - - if ninja_output.tell() > 0: - # Only create files for ninja files that actually have contents. - with OpenOutput(os.path.join(toplevel_build, output_file)) as ninja_file: - ninja_file.write(ninja_output.getvalue()) - ninja_output.close() - master_ninja.subninja(output_file) - - if target: - if name != target.FinalOutput() and spec['toolset'] == 'target': - target_short_names.setdefault(name, []).append(target) - target_outputs[qualified_target] = target - if qualified_target in all_targets: - all_outputs.add(target.FinalOutput()) - non_empty_target_names.add(name) + "link" + rule_name_suffix, + description="LINK%s $binary" % rule_name_suffix.upper(), + command=exe_cmd, + rspfile="$binary.rsp", + rspfile_content="$in_newline $libs $ldflags", + pool="link_pool", + ) + + +def GenerateOutputForConfig(target_list, target_dicts, data, params, config_name): + options = params["options"] + flavor = gyp.common.GetFlavor(params) + generator_flags = params.get("generator_flags", {}) + + # build_dir: relative path from source root to our output files. + # e.g. "out/Debug" + build_dir = os.path.normpath(os.path.join(ComputeOutputDir(params), config_name)) + + toplevel_build = os.path.join(options.toplevel_dir, build_dir) + + master_ninja_file = OpenOutput(os.path.join(toplevel_build, "build.ninja")) + master_ninja = ninja_syntax.Writer(master_ninja_file, width=120) + + # Put build-time support tools in out/{config_name}. + gyp.common.CopyTool(flavor, toplevel_build, generator_flags) + + # Grab make settings for CC/CXX. + # The rules are + # - The priority from low to high is gcc/g++, the 'make_global_settings' in + # gyp, the environment variable. + # - If there is no 'make_global_settings' for CC.host/CXX.host or + # 'CC_host'/'CXX_host' environment variable, cc_host/cxx_host should be set + # to cc/cxx. + if flavor == "win": + ar = "lib.exe" + # cc and cxx must be set to the correct architecture by overriding with one + # of cl_x86 or cl_x64 below. + cc = "UNSET" + cxx = "UNSET" + ld = "link.exe" + ld_host = "$ld" + else: + ar = "ar" + cc = "cc" + cxx = "c++" + ld = "$cc" + ldxx = "$cxx" + ld_host = "$cc_host" + ldxx_host = "$cxx_host" + + ar_host = ar + cc_host = None + cxx_host = None + cc_host_global_setting = None + cxx_host_global_setting = None + clang_cl = None + nm = "nm" + nm_host = "nm" + readelf = "readelf" + readelf_host = "readelf" + + build_file, _, _ = gyp.common.ParseQualifiedTarget(target_list[0]) + make_global_settings = data[build_file].get("make_global_settings", []) + build_to_root = gyp.common.InvertRelativePath(build_dir, options.toplevel_dir) + wrappers = {} + for key, value in make_global_settings: + if key == "AR": + ar = os.path.join(build_to_root, value) + if key == "AR.host": + ar_host = os.path.join(build_to_root, value) + if key == "CC": + cc = os.path.join(build_to_root, value) + if cc.endswith("clang-cl"): + clang_cl = cc + if key == "CXX": + cxx = os.path.join(build_to_root, value) + if key == "CC.host": + cc_host = os.path.join(build_to_root, value) + cc_host_global_setting = value + if key == "CXX.host": + cxx_host = os.path.join(build_to_root, value) + cxx_host_global_setting = value + if key == "LD": + ld = os.path.join(build_to_root, value) + if key == "LD.host": + ld_host = os.path.join(build_to_root, value) + if key == "LDXX": + ldxx = os.path.join(build_to_root, value) + if key == "LDXX.host": + ldxx_host = os.path.join(build_to_root, value) + if key == "NM": + nm = os.path.join(build_to_root, value) + if key == "NM.host": + nm_host = os.path.join(build_to_root, value) + if key == "READELF": + readelf = os.path.join(build_to_root, value) + if key == "READELF.host": + readelf_host = os.path.join(build_to_root, value) + if key.endswith("_wrapper"): + wrappers[key[: -len("_wrapper")]] = os.path.join(build_to_root, value) + + # Support wrappers from environment variables too. + for key, value in os.environ.items(): + if key.lower().endswith("_wrapper"): + key_prefix = key[: -len("_wrapper")] + key_prefix = re.sub(r"\.HOST$", ".host", key_prefix) + wrappers[key_prefix] = os.path.join(build_to_root, value) + + mac_toolchain_dir = generator_flags.get("mac_toolchain_dir", None) + if mac_toolchain_dir: + wrappers["LINK"] = "export DEVELOPER_DIR='%s' &&" % mac_toolchain_dir + + if flavor == "win": + configs = [ + target_dicts[qualified_target]["configurations"][config_name] + for qualified_target in target_list + ] + shared_system_includes = None + if not generator_flags.get("ninja_use_custom_environment_files", 0): + shared_system_includes = gyp.msvs_emulation.ExtractSharedMSVSSystemIncludes( + configs, generator_flags + ) + cl_paths = gyp.msvs_emulation.GenerateEnvironmentFiles( + toplevel_build, generator_flags, shared_system_includes, OpenOutput + ) + for arch, path in sorted(cl_paths.items()): + if clang_cl: + # If we have selected clang-cl, use that instead. + path = clang_cl + command = CommandWithWrapper( + "CC", wrappers, QuoteShellArgument(path, "win") + ) + if clang_cl: + # Use clang-cl to cross-compile for x86 or x86_64. + command += " -m32" if arch == "x86" else " -m64" + master_ninja.variable("cl_" + arch, command) + + cc = GetEnvironFallback(["CC_target", "CC"], cc) + master_ninja.variable("cc", CommandWithWrapper("CC", wrappers, cc)) + cxx = GetEnvironFallback(["CXX_target", "CXX"], cxx) + master_ninja.variable("cxx", CommandWithWrapper("CXX", wrappers, cxx)) + + if flavor == "win": + master_ninja.variable("ld", ld) + master_ninja.variable("idl", "midl.exe") + master_ninja.variable("ar", ar) + master_ninja.variable("rc", "rc.exe") + master_ninja.variable("ml_x86", "ml.exe") + master_ninja.variable("ml_x64", "ml64.exe") + master_ninja.variable("mt", "mt.exe") else: - empty_target_names.add(name) + master_ninja.variable("ld", CommandWithWrapper("LINK", wrappers, ld)) + master_ninja.variable("ldxx", CommandWithWrapper("LINK", wrappers, ldxx)) + master_ninja.variable("ar", GetEnvironFallback(["AR_target", "AR"], ar)) + if flavor != "mac": + # Mac does not use readelf/nm for .TOC generation, so avoiding polluting + # the master ninja with extra unused variables. + master_ninja.variable("nm", GetEnvironFallback(["NM_target", "NM"], nm)) + master_ninja.variable( + "readelf", GetEnvironFallback(["READELF_target", "READELF"], readelf) + ) + + if generator_supports_multiple_toolsets: + if not cc_host: + cc_host = cc + if not cxx_host: + cxx_host = cxx + + master_ninja.variable("ar_host", GetEnvironFallback(["AR_host"], ar_host)) + master_ninja.variable("nm_host", GetEnvironFallback(["NM_host"], nm_host)) + master_ninja.variable( + "readelf_host", GetEnvironFallback(["READELF_host"], readelf_host) + ) + cc_host = GetEnvironFallback(["CC_host"], cc_host) + cxx_host = GetEnvironFallback(["CXX_host"], cxx_host) + + # The environment variable could be used in 'make_global_settings', like + # ['CC.host', '$(CC)'] or ['CXX.host', '$(CXX)'], transform them here. + if "$(CC)" in cc_host and cc_host_global_setting: + cc_host = cc_host_global_setting.replace("$(CC)", cc) + if "$(CXX)" in cxx_host and cxx_host_global_setting: + cxx_host = cxx_host_global_setting.replace("$(CXX)", cxx) + master_ninja.variable( + "cc_host", CommandWithWrapper("CC.host", wrappers, cc_host) + ) + master_ninja.variable( + "cxx_host", CommandWithWrapper("CXX.host", wrappers, cxx_host) + ) + if flavor == "win": + master_ninja.variable("ld_host", ld_host) + master_ninja.variable("ldxx_host", ldxx_host) + else: + master_ninja.variable( + "ld_host", CommandWithWrapper("LINK", wrappers, ld_host) + ) + master_ninja.variable( + "ldxx_host", CommandWithWrapper("LINK", wrappers, ldxx_host) + ) - if target_short_names: - # Write a short name to build this target. This benefits both the - # "build chrome" case as well as the gyp tests, which expect to be - # able to run actions and build libraries by their short name. master_ninja.newline() - master_ninja.comment('Short names for targets.') - for short_name in sorted(target_short_names): - master_ninja.build(short_name, 'phony', [x.FinalOutput() for x in - target_short_names[short_name]]) - - # Write phony targets for any empty targets that weren't written yet. As - # short names are not necessarily unique only do this for short names that - # haven't already been output for another target. - empty_target_names = empty_target_names - non_empty_target_names - if empty_target_names: + + master_ninja.pool("link_pool", depth=GetDefaultConcurrentLinks()) master_ninja.newline() - master_ninja.comment('Empty targets (output for completeness).') - for name in sorted(empty_target_names): - master_ninja.build(name, 'phony') - if all_outputs: + deps = "msvc" if flavor == "win" else "gcc" + + if flavor != "win": + master_ninja.rule( + "cc", + description="CC $out", + command=( + "$cc -MMD -MF $out.d $defines $includes $cflags $cflags_c " + "$cflags_pch_c -c $in -o $out" + ), + depfile="$out.d", + deps=deps, + ) + master_ninja.rule( + "cc_s", + description="CC $out", + command=( + "$cc $defines $includes $cflags $cflags_c " + "$cflags_pch_c -c $in -o $out" + ), + ) + master_ninja.rule( + "cxx", + description="CXX $out", + command=( + "$cxx -MMD -MF $out.d $defines $includes $cflags $cflags_cc " + "$cflags_pch_cc -c $in -o $out" + ), + depfile="$out.d", + deps=deps, + ) + else: + # TODO(scottmg) Separate pdb names is a test to see if it works around + # http://crbug.com/142362. It seems there's a race between the creation of + # the .pdb by the precompiled header step for .cc and the compilation of + # .c files. This should be handled by mspdbsrv, but rarely errors out with + # c1xx : fatal error C1033: cannot open program database + # By making the rules target separate pdb files this might be avoided. + cc_command = ( + "ninja -t msvc -e $arch " + "-- " + "$cc /nologo /showIncludes /FC " + "@$out.rsp /c $in /Fo$out /Fd$pdbname_c " + ) + cxx_command = ( + "ninja -t msvc -e $arch " + "-- " + "$cxx /nologo /showIncludes /FC " + "@$out.rsp /c $in /Fo$out /Fd$pdbname_cc " + ) + master_ninja.rule( + "cc", + description="CC $out", + command=cc_command, + rspfile="$out.rsp", + rspfile_content="$defines $includes $cflags $cflags_c", + deps=deps, + ) + master_ninja.rule( + "cxx", + description="CXX $out", + command=cxx_command, + rspfile="$out.rsp", + rspfile_content="$defines $includes $cflags $cflags_cc", + deps=deps, + ) + master_ninja.rule( + "idl", + description="IDL $in", + command=( + "%s gyp-win-tool midl-wrapper $arch $outdir " + "$tlb $h $dlldata $iid $proxy $in " + "$midl_includes $idlflags" % sys.executable + ), + ) + master_ninja.rule( + "rc", + description="RC $in", + # Note: $in must be last otherwise rc.exe complains. + command=( + "%s gyp-win-tool rc-wrapper " + "$arch $rc $defines $resource_includes $rcflags /fo$out $in" + % sys.executable + ), + ) + master_ninja.rule( + "asm", + description="ASM $out", + command=( + "%s gyp-win-tool asm-wrapper " + "$arch $asm $defines $includes $asmflags /c /Fo $out $in" + % sys.executable + ), + ) + + if flavor != "mac" and flavor != "win": + master_ninja.rule( + "alink", + description="AR $out", + command="rm -f $out && $ar rcs $arflags $out $in", + ) + master_ninja.rule( + "alink_thin", + description="AR $out", + command="rm -f $out && $ar rcsT $arflags $out $in", + ) + + # This allows targets that only need to depend on $lib's API to declare an + # order-only dependency on $lib.TOC and avoid relinking such downstream + # dependencies when $lib changes only in non-public ways. + # The resulting string leaves an uninterpolated %{suffix} which + # is used in the final substitution below. + mtime_preserving_solink_base = ( + "if [ ! -e $lib -o ! -e $lib.TOC ]; then " + "%(solink)s && %(extract_toc)s > $lib.TOC; else " + "%(solink)s && %(extract_toc)s > $lib.tmp && " + "if ! cmp -s $lib.tmp $lib.TOC; then mv $lib.tmp $lib.TOC ; " + "fi; fi" + % { + "solink": "$ld -shared $ldflags -o $lib -Wl,-soname=$soname %(suffix)s", + "extract_toc": ( + "{ $readelf -d $lib | grep SONAME ; " + "$nm -gD -f p $lib | cut -f1-2 -d' '; }" + ), + } + ) + + master_ninja.rule( + "solink", + description="SOLINK $lib", + restat=True, + command=mtime_preserving_solink_base % {"suffix": "@$link_file_list"}, # noqa: E501 + rspfile="$link_file_list", + rspfile_content=("-Wl,--whole-archive $in $solibs -Wl," + "--no-whole-archive $libs"), + pool="link_pool", + ) + master_ninja.rule( + "solink_module", + description="SOLINK(module) $lib", + restat=True, + command=mtime_preserving_solink_base % {"suffix": "@$link_file_list"}, + rspfile="$link_file_list", + rspfile_content="-Wl,--start-group $in $solibs $libs -Wl,--end-group", + pool="link_pool", + ) + master_ninja.rule( + "link", + description="LINK $out", + command=( + "$ld $ldflags -o $out " + "-Wl,--start-group $in $solibs $libs -Wl,--end-group" + ), + pool="link_pool", + ) + elif flavor == "win": + master_ninja.rule( + "alink", + description="LIB $out", + command=( + "%s gyp-win-tool link-wrapper $arch False " + "$ar /nologo /ignore:4221 /OUT:$out @$out.rsp" % sys.executable + ), + rspfile="$out.rsp", + rspfile_content="$in_newline $libflags", + ) + _AddWinLinkRules(master_ninja, embed_manifest=True) + _AddWinLinkRules(master_ninja, embed_manifest=False) + else: + master_ninja.rule( + "objc", + description="OBJC $out", + command=( + "$cc -MMD -MF $out.d $defines $includes $cflags $cflags_objc " + "$cflags_pch_objc -c $in -o $out" + ), + depfile="$out.d", + deps=deps, + ) + master_ninja.rule( + "objcxx", + description="OBJCXX $out", + command=( + "$cxx -MMD -MF $out.d $defines $includes $cflags $cflags_objcc " + "$cflags_pch_objcc -c $in -o $out" + ), + depfile="$out.d", + deps=deps, + ) + master_ninja.rule( + "alink", + description="LIBTOOL-STATIC $out, POSTBUILDS", + command="rm -f $out && " + "./gyp-mac-tool filter-libtool libtool $libtool_flags " + "-static -o $out $in" + "$postbuilds", + ) + master_ninja.rule( + "lipo", + description="LIPO $out, POSTBUILDS", + command="rm -f $out && lipo -create $in -output $out$postbuilds", + ) + master_ninja.rule( + "solipo", + description="SOLIPO $out, POSTBUILDS", + command=( + "rm -f $lib $lib.TOC && lipo -create $in -output $lib$postbuilds &&" + "%(extract_toc)s > $lib.TOC" + % { + "extract_toc": "{ otool -l $lib | grep LC_ID_DYLIB -A 5; " + "nm -gP $lib | cut -f1-2 -d' ' | grep -v U$$; true; }" + } + ), + ) + + # Record the public interface of $lib in $lib.TOC. See the corresponding + # comment in the posix section above for details. + solink_base = "$ld %(type)s $ldflags -o $lib %(suffix)s" + mtime_preserving_solink_base = ( + "if [ ! -e $lib -o ! -e $lib.TOC ] || " + # Always force dependent targets to relink if this library + # reexports something. Handling this correctly would require + # recursive TOC dumping but this is rare in practice, so punt. + "otool -l $lib | grep -q LC_REEXPORT_DYLIB ; then " + "%(solink)s && %(extract_toc)s > $lib.TOC; " + "else " + "%(solink)s && %(extract_toc)s > $lib.tmp && " + "if ! cmp -s $lib.tmp $lib.TOC; then " + "mv $lib.tmp $lib.TOC ; " + "fi; " + "fi" + % { + "solink": solink_base, + "extract_toc": "{ otool -l $lib | grep LC_ID_DYLIB -A 5; " + "nm -gP $lib | cut -f1-2 -d' ' | grep -v U$$; true; }", + } + ) + + solink_suffix = "@$link_file_list$postbuilds" + master_ninja.rule( + "solink", + description="SOLINK $lib, POSTBUILDS", + restat=True, + command=mtime_preserving_solink_base + % {"suffix": solink_suffix, "type": "-shared"}, + rspfile="$link_file_list", + rspfile_content="$in $solibs $libs", + pool="link_pool", + ) + master_ninja.rule( + "solink_notoc", + description="SOLINK $lib, POSTBUILDS", + restat=True, + command=solink_base % {"suffix": solink_suffix, "type": "-shared"}, + rspfile="$link_file_list", + rspfile_content="$in $solibs $libs", + pool="link_pool", + ) + + master_ninja.rule( + "solink_module", + description="SOLINK(module) $lib, POSTBUILDS", + restat=True, + command=mtime_preserving_solink_base + % {"suffix": solink_suffix, "type": "-bundle"}, + rspfile="$link_file_list", + rspfile_content="$in $solibs $libs", + pool="link_pool", + ) + master_ninja.rule( + "solink_module_notoc", + description="SOLINK(module) $lib, POSTBUILDS", + restat=True, + command=solink_base % {"suffix": solink_suffix, "type": "-bundle"}, + rspfile="$link_file_list", + rspfile_content="$in $solibs $libs", + pool="link_pool", + ) + + master_ninja.rule( + "link", + description="LINK $out, POSTBUILDS", + command=("$ld $ldflags -o $out " "$in $solibs $libs$postbuilds"), + pool="link_pool", + ) + master_ninja.rule( + "preprocess_infoplist", + description="PREPROCESS INFOPLIST $out", + command=( + "$cc -E -P -Wno-trigraphs -x c $defines $in -o $out && " + "plutil -convert xml1 $out $out" + ), + ) + master_ninja.rule( + "copy_infoplist", + description="COPY INFOPLIST $in", + command="$env ./gyp-mac-tool copy-info-plist $in $out $binary $keys", + ) + master_ninja.rule( + "merge_infoplist", + description="MERGE INFOPLISTS $in", + command="$env ./gyp-mac-tool merge-info-plist $out $in", + ) + master_ninja.rule( + "compile_xcassets", + description="COMPILE XCASSETS $in", + command="$env ./gyp-mac-tool compile-xcassets $keys $in", + ) + master_ninja.rule( + "compile_ios_framework_headers", + description="COMPILE HEADER MAPS AND COPY FRAMEWORK HEADERS $in", + command="$env ./gyp-mac-tool compile-ios-framework-header-map $out " + "$framework $in && $env ./gyp-mac-tool " + "copy-ios-framework-headers $framework $copy_headers", + ) + master_ninja.rule( + "mac_tool", + description="MACTOOL $mactool_cmd $in", + command="$env ./gyp-mac-tool $mactool_cmd $in $out $binary", + ) + master_ninja.rule( + "package_framework", + description="PACKAGE FRAMEWORK $out, POSTBUILDS", + command="./gyp-mac-tool package-framework $out $version$postbuilds " + "&& touch $out", + ) + master_ninja.rule( + "package_ios_framework", + description="PACKAGE IOS FRAMEWORK $out, POSTBUILDS", + command="./gyp-mac-tool package-ios-framework $out $postbuilds " + "&& touch $out", + ) + if flavor == "win": + master_ninja.rule( + "stamp", + description="STAMP $out", + command="%s gyp-win-tool stamp $out" % sys.executable, + ) + else: + master_ninja.rule( + "stamp", description="STAMP $out", command="${postbuilds}touch $out" + ) + if flavor == "win": + master_ninja.rule( + "copy", + description="COPY $in $out", + command="%s gyp-win-tool recursive-mirror $in $out" % sys.executable, + ) + elif flavor == "zos": + master_ninja.rule( + "copy", + description="COPY $in $out", + command="rm -rf $out && cp -fRP $in $out", + ) + else: + master_ninja.rule( + "copy", + description="COPY $in $out", + command="ln -f $in $out 2>/dev/null || (rm -rf $out && cp -af $in $out)", + ) master_ninja.newline() - master_ninja.build('all', 'phony', sorted(all_outputs)) - master_ninja.default(generator_flags.get('default_target', 'all')) - master_ninja_file.close() + all_targets = set() + for build_file in params["build_files"]: + for target in gyp.common.AllTargets( + target_list, target_dicts, os.path.normpath(build_file) + ): + all_targets.add(target) + all_outputs = set() + + # target_outputs is a map from qualified target name to a Target object. + target_outputs = {} + # target_short_names is a map from target short name to a list of Target + # objects. + target_short_names = {} + + # short name of targets that were skipped because they didn't contain anything + # interesting. + # NOTE: there may be overlap between this an non_empty_target_names. + empty_target_names = set() + + # Set of non-empty short target names. + # NOTE: there may be overlap between this an empty_target_names. + non_empty_target_names = set() + + for qualified_target in target_list: + # qualified_target is like: third_party/icu/icu.gyp:icui18n#target + build_file, name, toolset = gyp.common.ParseQualifiedTarget(qualified_target) + + this_make_global_settings = data[build_file].get("make_global_settings", []) + assert make_global_settings == this_make_global_settings, ( + "make_global_settings needs to be the same for all targets. %s vs. %s" + % (this_make_global_settings, make_global_settings) + ) + + spec = target_dicts[qualified_target] + if flavor == "mac": + gyp.xcode_emulation.MergeGlobalXcodeSettingsToSpec(data[build_file], spec) + + # If build_file is a symlink, we must not follow it because there's a chance + # it could point to a path above toplevel_dir, and we cannot correctly deal + # with that case at the moment. + build_file = gyp.common.RelativePath(build_file, options.toplevel_dir, False) + + qualified_target_for_hash = gyp.common.QualifiedTarget( + build_file, name, toolset + ) + qualified_target_for_hash = qualified_target_for_hash.encode("utf-8") + hash_for_rules = hashlib.md5(qualified_target_for_hash).hexdigest() + + base_path = os.path.dirname(build_file) + obj = "obj" + if toolset != "target": + obj += "." + toolset + output_file = os.path.join(obj, base_path, name + ".ninja") + + ninja_output = StringIO() + writer = NinjaWriter( + hash_for_rules, + target_outputs, + base_path, + build_dir, + ninja_output, + toplevel_build, + output_file, + flavor, + toplevel_dir=options.toplevel_dir, + ) + + target = writer.WriteSpec(spec, config_name, generator_flags) + + if ninja_output.tell() > 0: + # Only create files for ninja files that actually have contents. + with OpenOutput(os.path.join(toplevel_build, output_file)) as ninja_file: + ninja_file.write(ninja_output.getvalue()) + ninja_output.close() + master_ninja.subninja(output_file) + + if target: + if name != target.FinalOutput() and spec["toolset"] == "target": + target_short_names.setdefault(name, []).append(target) + target_outputs[qualified_target] = target + if qualified_target in all_targets: + all_outputs.add(target.FinalOutput()) + non_empty_target_names.add(name) + else: + empty_target_names.add(name) + + if target_short_names: + # Write a short name to build this target. This benefits both the + # "build chrome" case as well as the gyp tests, which expect to be + # able to run actions and build libraries by their short name. + master_ninja.newline() + master_ninja.comment("Short names for targets.") + for short_name in sorted(target_short_names): + master_ninja.build( + short_name, + "phony", + [x.FinalOutput() for x in target_short_names[short_name]], + ) + + # Write phony targets for any empty targets that weren't written yet. As + # short names are not necessarily unique only do this for short names that + # haven't already been output for another target. + empty_target_names = empty_target_names - non_empty_target_names + if empty_target_names: + master_ninja.newline() + master_ninja.comment("Empty targets (output for completeness).") + for name in sorted(empty_target_names): + master_ninja.build(name, "phony") + + if all_outputs: + master_ninja.newline() + master_ninja.build("all", "phony", sorted(all_outputs)) + master_ninja.default(generator_flags.get("default_target", "all")) + + master_ninja_file.close() def PerformBuild(data, configurations, params): - options = params['options'] - for config in configurations: - builddir = os.path.join(options.toplevel_dir, 'out', config) - arguments = ['ninja', '-C', builddir] - print('Building [%s]: %s' % (config, arguments)) - subprocess.check_call(arguments) + options = params["options"] + for config in configurations: + builddir = os.path.join(options.toplevel_dir, "out", config) + arguments = ["ninja", "-C", builddir] + print("Building [%s]: %s" % (config, arguments)) + subprocess.check_call(arguments) def CallGenerateOutputForConfig(arglist): - # Ignore the interrupt signal so that the parent process catches it and - # kills all multiprocessing children. - signal.signal(signal.SIGINT, signal.SIG_IGN) + # Ignore the interrupt signal so that the parent process catches it and + # kills all multiprocessing children. + signal.signal(signal.SIGINT, signal.SIG_IGN) - (target_list, target_dicts, data, params, config_name) = arglist - GenerateOutputForConfig(target_list, target_dicts, data, params, config_name) + (target_list, target_dicts, data, params, config_name) = arglist + GenerateOutputForConfig(target_list, target_dicts, data, params, config_name) def GenerateOutput(target_list, target_dicts, data, params): - # Update target_dicts for iOS device builds. - target_dicts = gyp.xcode_emulation.CloneConfigurationForDeviceAndEmulator( - target_dicts) - - user_config = params.get('generator_flags', {}).get('config', None) - if gyp.common.GetFlavor(params) == 'win': - target_list, target_dicts = MSVSUtil.ShardTargets(target_list, target_dicts) - target_list, target_dicts = MSVSUtil.InsertLargePdbShims( - target_list, target_dicts, generator_default_variables) - - if user_config: - GenerateOutputForConfig(target_list, target_dicts, data, params, - user_config) - else: - config_names = target_dicts[target_list[0]]['configurations'].keys() - if params['parallel']: - try: - pool = multiprocessing.Pool(len(config_names)) - arglists = [] - for config_name in config_names: - arglists.append( - (target_list, target_dicts, data, params, config_name)) - pool.map(CallGenerateOutputForConfig, arglists) - except KeyboardInterrupt as e: - pool.terminate() - raise e + # Update target_dicts for iOS device builds. + target_dicts = gyp.xcode_emulation.CloneConfigurationForDeviceAndEmulator( + target_dicts + ) + + user_config = params.get("generator_flags", {}).get("config", None) + if gyp.common.GetFlavor(params) == "win": + target_list, target_dicts = MSVSUtil.ShardTargets(target_list, target_dicts) + target_list, target_dicts = MSVSUtil.InsertLargePdbShims( + target_list, target_dicts, generator_default_variables + ) + + if user_config: + GenerateOutputForConfig(target_list, target_dicts, data, params, user_config) else: - for config_name in config_names: - GenerateOutputForConfig(target_list, target_dicts, data, params, - config_name) + config_names = target_dicts[target_list[0]]["configurations"] + if params["parallel"]: + try: + pool = multiprocessing.Pool(len(config_names)) + arglists = [] + for config_name in config_names: + arglists.append( + (target_list, target_dicts, data, params, config_name) + ) + pool.map(CallGenerateOutputForConfig, arglists) + except KeyboardInterrupt as e: + pool.terminate() + raise e + else: + for config_name in config_names: + GenerateOutputForConfig( + target_list, target_dicts, data, params, config_name + ) diff --git a/tools/gyp/pylib/gyp/generator/ninja_test.py b/tools/gyp/pylib/gyp/generator/ninja_test.py index c8adc251c97d50..abadcd9828e8b2 100644 --- a/tools/gyp/pylib/gyp/generator/ninja_test.py +++ b/tools/gyp/pylib/gyp/generator/ninja_test.py @@ -13,34 +13,43 @@ class TestPrefixesAndSuffixes(unittest.TestCase): - def test_BinaryNamesWindows(self): - # These cannot run on non-Windows as they require a VS installation to - # correctly handle variable expansion. - if sys.platform.startswith('win'): - writer = ninja.NinjaWriter('foo', 'wee', '.', '.', 'build.ninja', '.', - 'build.ninja', 'win') - spec = { 'target_name': 'wee' } - self.assertTrue(writer.ComputeOutputFileName(spec, 'executable'). - endswith('.exe')) - self.assertTrue(writer.ComputeOutputFileName(spec, 'shared_library'). - endswith('.dll')) - self.assertTrue(writer.ComputeOutputFileName(spec, 'static_library'). - endswith('.lib')) - - def test_BinaryNamesLinux(self): - writer = ninja.NinjaWriter('foo', 'wee', '.', '.', 'build.ninja', '.', - 'build.ninja', 'linux') - spec = { 'target_name': 'wee' } - self.assertTrue('.' not in writer.ComputeOutputFileName(spec, - 'executable')) - self.assertTrue(writer.ComputeOutputFileName(spec, 'shared_library'). - startswith('lib')) - self.assertTrue(writer.ComputeOutputFileName(spec, 'static_library'). - startswith('lib')) - self.assertTrue(writer.ComputeOutputFileName(spec, 'shared_library'). - endswith('.so')) - self.assertTrue(writer.ComputeOutputFileName(spec, 'static_library'). - endswith('.a')) - -if __name__ == '__main__': - unittest.main() + def test_BinaryNamesWindows(self): + # These cannot run on non-Windows as they require a VS installation to + # correctly handle variable expansion. + if sys.platform.startswith("win"): + writer = ninja.NinjaWriter( + "foo", "wee", ".", ".", "build.ninja", ".", "build.ninja", "win" + ) + spec = {"target_name": "wee"} + self.assertTrue( + writer.ComputeOutputFileName(spec, "executable").endswith(".exe") + ) + self.assertTrue( + writer.ComputeOutputFileName(spec, "shared_library").endswith(".dll") + ) + self.assertTrue( + writer.ComputeOutputFileName(spec, "static_library").endswith(".lib") + ) + + def test_BinaryNamesLinux(self): + writer = ninja.NinjaWriter( + "foo", "wee", ".", ".", "build.ninja", ".", "build.ninja", "linux" + ) + spec = {"target_name": "wee"} + self.assertTrue("." not in writer.ComputeOutputFileName(spec, "executable")) + self.assertTrue( + writer.ComputeOutputFileName(spec, "shared_library").startswith("lib") + ) + self.assertTrue( + writer.ComputeOutputFileName(spec, "static_library").startswith("lib") + ) + self.assertTrue( + writer.ComputeOutputFileName(spec, "shared_library").endswith(".so") + ) + self.assertTrue( + writer.ComputeOutputFileName(spec, "static_library").endswith(".a") + ) + + +if __name__ == "__main__": + unittest.main() diff --git a/tools/gyp/pylib/gyp/generator/xcode.py b/tools/gyp/pylib/gyp/generator/xcode.py index 4917ba77b9d577..9e7e99e9e13d2d 100644 --- a/tools/gyp/pylib/gyp/generator/xcode.py +++ b/tools/gyp/pylib/gyp/generator/xcode.py @@ -27,511 +27,543 @@ # PROJECT_DERIVED_FILE_DIR, is unsuitable because while it is project-specific, # it is not configuration-specific. INTERMEDIATE_DIR is defined as # $(PROJECT_DERIVED_FILE_DIR)/$(CONFIGURATION). -_intermediate_var = 'INTERMEDIATE_DIR' +_intermediate_var = "INTERMEDIATE_DIR" # SHARED_INTERMEDIATE_DIR is the same, except that it is shared among all # targets that share the same BUILT_PRODUCTS_DIR. -_shared_intermediate_var = 'SHARED_INTERMEDIATE_DIR' +_shared_intermediate_var = "SHARED_INTERMEDIATE_DIR" -_library_search_paths_var = 'LIBRARY_SEARCH_PATHS' +_library_search_paths_var = "LIBRARY_SEARCH_PATHS" generator_default_variables = { - 'EXECUTABLE_PREFIX': '', - 'EXECUTABLE_SUFFIX': '', - 'STATIC_LIB_PREFIX': 'lib', - 'SHARED_LIB_PREFIX': 'lib', - 'STATIC_LIB_SUFFIX': '.a', - 'SHARED_LIB_SUFFIX': '.dylib', - # INTERMEDIATE_DIR is a place for targets to build up intermediate products. - # It is specific to each build environment. It is only guaranteed to exist - # and be constant within the context of a project, corresponding to a single - # input file. Some build environments may allow their intermediate directory - # to be shared on a wider scale, but this is not guaranteed. - 'INTERMEDIATE_DIR': '$(%s)' % _intermediate_var, - 'OS': 'mac', - 'PRODUCT_DIR': '$(BUILT_PRODUCTS_DIR)', - 'LIB_DIR': '$(BUILT_PRODUCTS_DIR)', - 'RULE_INPUT_ROOT': '$(INPUT_FILE_BASE)', - 'RULE_INPUT_EXT': '$(INPUT_FILE_SUFFIX)', - 'RULE_INPUT_NAME': '$(INPUT_FILE_NAME)', - 'RULE_INPUT_PATH': '$(INPUT_FILE_PATH)', - 'RULE_INPUT_DIRNAME': '$(INPUT_FILE_DIRNAME)', - 'SHARED_INTERMEDIATE_DIR': '$(%s)' % _shared_intermediate_var, - 'CONFIGURATION_NAME': '$(CONFIGURATION)', + "EXECUTABLE_PREFIX": "", + "EXECUTABLE_SUFFIX": "", + "STATIC_LIB_PREFIX": "lib", + "SHARED_LIB_PREFIX": "lib", + "STATIC_LIB_SUFFIX": ".a", + "SHARED_LIB_SUFFIX": ".dylib", + # INTERMEDIATE_DIR is a place for targets to build up intermediate products. + # It is specific to each build environment. It is only guaranteed to exist + # and be constant within the context of a project, corresponding to a single + # input file. Some build environments may allow their intermediate directory + # to be shared on a wider scale, but this is not guaranteed. + "INTERMEDIATE_DIR": "$(%s)" % _intermediate_var, + "OS": "mac", + "PRODUCT_DIR": "$(BUILT_PRODUCTS_DIR)", + "LIB_DIR": "$(BUILT_PRODUCTS_DIR)", + "RULE_INPUT_ROOT": "$(INPUT_FILE_BASE)", + "RULE_INPUT_EXT": "$(INPUT_FILE_SUFFIX)", + "RULE_INPUT_NAME": "$(INPUT_FILE_NAME)", + "RULE_INPUT_PATH": "$(INPUT_FILE_PATH)", + "RULE_INPUT_DIRNAME": "$(INPUT_FILE_DIRNAME)", + "SHARED_INTERMEDIATE_DIR": "$(%s)" % _shared_intermediate_var, + "CONFIGURATION_NAME": "$(CONFIGURATION)", } # The Xcode-specific sections that hold paths. generator_additional_path_sections = [ - 'mac_bundle_resources', - 'mac_framework_headers', - 'mac_framework_private_headers', - # 'mac_framework_dirs', input already handles _dirs endings. + "mac_bundle_resources", + "mac_framework_headers", + "mac_framework_private_headers", + # 'mac_framework_dirs', input already handles _dirs endings. ] # The Xcode-specific keys that exist on targets and aren't moved down to # configurations. generator_additional_non_configuration_keys = [ - 'ios_app_extension', - 'ios_watch_app', - 'ios_watchkit_extension', - 'mac_bundle', - 'mac_bundle_resources', - 'mac_framework_headers', - 'mac_framework_private_headers', - 'mac_xctest_bundle', - 'mac_xcuitest_bundle', - 'xcode_create_dependents_test_runner', + "ios_app_extension", + "ios_watch_app", + "ios_watchkit_extension", + "mac_bundle", + "mac_bundle_resources", + "mac_framework_headers", + "mac_framework_private_headers", + "mac_xctest_bundle", + "mac_xcuitest_bundle", + "xcode_create_dependents_test_runner", ] # We want to let any rules apply to files that are resources also. generator_extra_sources_for_rules = [ - 'mac_bundle_resources', - 'mac_framework_headers', - 'mac_framework_private_headers', + "mac_bundle_resources", + "mac_framework_headers", + "mac_framework_private_headers", ] generator_filelist_paths = None # Xcode's standard set of library directories, which don't need to be duplicated # in LIBRARY_SEARCH_PATHS. This list is not exhaustive, but that's okay. -xcode_standard_library_dirs = frozenset([ - '$(SDKROOT)/usr/lib', - '$(SDKROOT)/usr/local/lib', -]) +xcode_standard_library_dirs = frozenset( + ["$(SDKROOT)/usr/lib", "$(SDKROOT)/usr/local/lib"] +) + def CreateXCConfigurationList(configuration_names): - xccl = gyp.xcodeproj_file.XCConfigurationList({'buildConfigurations': []}) - if len(configuration_names) == 0: - configuration_names = ['Default'] - for configuration_name in configuration_names: - xcbc = gyp.xcodeproj_file.XCBuildConfiguration({ - 'name': configuration_name}) - xccl.AppendProperty('buildConfigurations', xcbc) - xccl.SetProperty('defaultConfigurationName', configuration_names[0]) - return xccl + xccl = gyp.xcodeproj_file.XCConfigurationList({"buildConfigurations": []}) + if len(configuration_names) == 0: + configuration_names = ["Default"] + for configuration_name in configuration_names: + xcbc = gyp.xcodeproj_file.XCBuildConfiguration({"name": configuration_name}) + xccl.AppendProperty("buildConfigurations", xcbc) + xccl.SetProperty("defaultConfigurationName", configuration_names[0]) + return xccl class XcodeProject(object): - def __init__(self, gyp_path, path, build_file_dict): - self.gyp_path = gyp_path - self.path = path - self.project = gyp.xcodeproj_file.PBXProject(path=path) - projectDirPath = gyp.common.RelativePath( - os.path.dirname(os.path.abspath(self.gyp_path)), - os.path.dirname(path) or '.') - self.project.SetProperty('projectDirPath', projectDirPath) - self.project_file = \ - gyp.xcodeproj_file.XCProjectFile({'rootObject': self.project}) - self.build_file_dict = build_file_dict - - # TODO(mark): add destructor that cleans up self.path if created_dir is - # True and things didn't complete successfully. Or do something even - # better with "try"? - self.created_dir = False - try: - os.makedirs(self.path) - self.created_dir = True - except OSError as e: - if e.errno != errno.EEXIST: - raise - - def Finalize1(self, xcode_targets, serialize_all_tests): - # Collect a list of all of the build configuration names used by the - # various targets in the file. It is very heavily advised to keep each - # target in an entire project (even across multiple project files) using - # the same set of configuration names. - configurations = [] - for xct in self.project.GetProperty('targets'): - xccl = xct.GetProperty('buildConfigurationList') - xcbcs = xccl.GetProperty('buildConfigurations') - for xcbc in xcbcs: - name = xcbc.GetProperty('name') - if name not in configurations: - configurations.append(name) - - # Replace the XCConfigurationList attached to the PBXProject object with - # a new one specifying all of the configuration names used by the various - # targets. - try: - xccl = CreateXCConfigurationList(configurations) - self.project.SetProperty('buildConfigurationList', xccl) - except: - sys.stderr.write("Problem with gyp file %s\n" % self.gyp_path) - raise - - # The need for this setting is explained above where _intermediate_var is - # defined. The comments below about wanting to avoid project-wide build - # settings apply here too, but this needs to be set on a project-wide basis - # so that files relative to the _intermediate_var setting can be displayed - # properly in the Xcode UI. - # - # Note that for configuration-relative files such as anything relative to - # _intermediate_var, for the purposes of UI tree view display, Xcode will - # only resolve the configuration name once, when the project file is - # opened. If the active build configuration is changed, the project file - # must be closed and reopened if it is desired for the tree view to update. - # This is filed as Apple radar 6588391. - xccl.SetBuildSetting(_intermediate_var, - '$(PROJECT_DERIVED_FILE_DIR)/$(CONFIGURATION)') - xccl.SetBuildSetting(_shared_intermediate_var, - '$(SYMROOT)/DerivedSources/$(CONFIGURATION)') - - # Set user-specified project-wide build settings and config files. This - # is intended to be used very sparingly. Really, almost everything should - # go into target-specific build settings sections. The project-wide - # settings are only intended to be used in cases where Xcode attempts to - # resolve variable references in a project context as opposed to a target - # context, such as when resolving sourceTree references while building up - # the tree tree view for UI display. - # Any values set globally are applied to all configurations, then any - # per-configuration values are applied. - for xck, xcv in self.build_file_dict.get('xcode_settings', {}).items(): - xccl.SetBuildSetting(xck, xcv) - if 'xcode_config_file' in self.build_file_dict: - config_ref = self.project.AddOrGetFileInRootGroup( - self.build_file_dict['xcode_config_file']) - xccl.SetBaseConfiguration(config_ref) - build_file_configurations = self.build_file_dict.get('configurations', {}) - if build_file_configurations: - for config_name in configurations: - build_file_configuration_named = \ - build_file_configurations.get(config_name, {}) - if build_file_configuration_named: - xcc = xccl.ConfigurationNamed(config_name) - for xck, xcv in build_file_configuration_named.get('xcode_settings', - {}).items(): - xcc.SetBuildSetting(xck, xcv) - if 'xcode_config_file' in build_file_configuration_named: + def __init__(self, gyp_path, path, build_file_dict): + self.gyp_path = gyp_path + self.path = path + self.project = gyp.xcodeproj_file.PBXProject(path=path) + projectDirPath = gyp.common.RelativePath( + os.path.dirname(os.path.abspath(self.gyp_path)), + os.path.dirname(path) or ".", + ) + self.project.SetProperty("projectDirPath", projectDirPath) + self.project_file = gyp.xcodeproj_file.XCProjectFile( + {"rootObject": self.project} + ) + self.build_file_dict = build_file_dict + + # TODO(mark): add destructor that cleans up self.path if created_dir is + # True and things didn't complete successfully. Or do something even + # better with "try"? + self.created_dir = False + try: + os.makedirs(self.path) + self.created_dir = True + except OSError as e: + if e.errno != errno.EEXIST: + raise + + def Finalize1(self, xcode_targets, serialize_all_tests): + # Collect a list of all of the build configuration names used by the + # various targets in the file. It is very heavily advised to keep each + # target in an entire project (even across multiple project files) using + # the same set of configuration names. + configurations = [] + for xct in self.project.GetProperty("targets"): + xccl = xct.GetProperty("buildConfigurationList") + xcbcs = xccl.GetProperty("buildConfigurations") + for xcbc in xcbcs: + name = xcbc.GetProperty("name") + if name not in configurations: + configurations.append(name) + + # Replace the XCConfigurationList attached to the PBXProject object with + # a new one specifying all of the configuration names used by the various + # targets. + try: + xccl = CreateXCConfigurationList(configurations) + self.project.SetProperty("buildConfigurationList", xccl) + except Exception: + sys.stderr.write("Problem with gyp file %s\n" % self.gyp_path) + raise + + # The need for this setting is explained above where _intermediate_var is + # defined. The comments below about wanting to avoid project-wide build + # settings apply here too, but this needs to be set on a project-wide basis + # so that files relative to the _intermediate_var setting can be displayed + # properly in the Xcode UI. + # + # Note that for configuration-relative files such as anything relative to + # _intermediate_var, for the purposes of UI tree view display, Xcode will + # only resolve the configuration name once, when the project file is + # opened. If the active build configuration is changed, the project file + # must be closed and reopened if it is desired for the tree view to update. + # This is filed as Apple radar 6588391. + xccl.SetBuildSetting( + _intermediate_var, "$(PROJECT_DERIVED_FILE_DIR)/$(CONFIGURATION)" + ) + xccl.SetBuildSetting( + _shared_intermediate_var, "$(SYMROOT)/DerivedSources/$(CONFIGURATION)" + ) + + # Set user-specified project-wide build settings and config files. This + # is intended to be used very sparingly. Really, almost everything should + # go into target-specific build settings sections. The project-wide + # settings are only intended to be used in cases where Xcode attempts to + # resolve variable references in a project context as opposed to a target + # context, such as when resolving sourceTree references while building up + # the tree tree view for UI display. + # Any values set globally are applied to all configurations, then any + # per-configuration values are applied. + for xck, xcv in self.build_file_dict.get("xcode_settings", {}).items(): + xccl.SetBuildSetting(xck, xcv) + if "xcode_config_file" in self.build_file_dict: config_ref = self.project.AddOrGetFileInRootGroup( - build_file_configurations[config_name]['xcode_config_file']) - xcc.SetBaseConfiguration(config_ref) - - # Sort the targets based on how they appeared in the input. - # TODO(mark): Like a lot of other things here, this assumes internal - # knowledge of PBXProject - in this case, of its "targets" property. - - # ordinary_targets are ordinary targets that are already in the project - # file. run_test_targets are the targets that run unittests and should be - # used for the Run All Tests target. support_targets are the action/rule - # targets used by GYP file targets, just kept for the assert check. - ordinary_targets = [] - run_test_targets = [] - support_targets = [] - - # targets is full list of targets in the project. - targets = [] - - # does the it define it's own "all"? - has_custom_all = False - - # targets_for_all is the list of ordinary_targets that should be listed - # in this project's "All" target. It includes each non_runtest_target - # that does not have suppress_wildcard set. - targets_for_all = [] - - for target in self.build_file_dict['targets']: - target_name = target['target_name'] - toolset = target['toolset'] - qualified_target = gyp.common.QualifiedTarget(self.gyp_path, target_name, - toolset) - xcode_target = xcode_targets[qualified_target] - # Make sure that the target being added to the sorted list is already in - # the unsorted list. - assert xcode_target in self.project._properties['targets'] - targets.append(xcode_target) - ordinary_targets.append(xcode_target) - if xcode_target.support_target: - support_targets.append(xcode_target.support_target) - targets.append(xcode_target.support_target) - - if not int(target.get('suppress_wildcard', False)): - targets_for_all.append(xcode_target) - - if target_name.lower() == 'all': - has_custom_all = True - - # If this target has a 'run_as' attribute, add its target to the - # targets, and add it to the test targets. - if target.get('run_as'): - # Make a target to run something. It should have one - # dependency, the parent xcode target. - xccl = CreateXCConfigurationList(configurations) - run_target = gyp.xcodeproj_file.PBXAggregateTarget({ - 'name': 'Run ' + target_name, - 'productName': xcode_target.GetProperty('productName'), - 'buildConfigurationList': xccl, - }, - parent=self.project) - run_target.AddDependency(xcode_target) - - command = target['run_as'] - script = '' - if command.get('working_directory'): - script = script + 'cd "%s"\n' % \ - gyp.xcodeproj_file.ConvertVariablesToShellSyntax( - command.get('working_directory')) - - if command.get('environment'): - script = script + "\n".join( - ['export %s="%s"' % - (key, gyp.xcodeproj_file.ConvertVariablesToShellSyntax(val)) - for (key, val) in command.get('environment').items()]) + "\n" - - # Some test end up using sockets, files on disk, etc. and can get - # confused if more then one test runs at a time. The generator - # flag 'xcode_serialize_all_test_runs' controls the forcing of all - # tests serially. It defaults to True. To get serial runs this - # little bit of python does the same as the linux flock utility to - # make sure only one runs at a time. - command_prefix = '' - if serialize_all_tests: - command_prefix = \ -"""python -c "import fcntl, subprocess, sys + self.build_file_dict["xcode_config_file"] + ) + xccl.SetBaseConfiguration(config_ref) + build_file_configurations = self.build_file_dict.get("configurations", {}) + if build_file_configurations: + for config_name in configurations: + build_file_configuration_named = build_file_configurations.get( + config_name, {} + ) + if build_file_configuration_named: + xcc = xccl.ConfigurationNamed(config_name) + for xck, xcv in build_file_configuration_named.get( + "xcode_settings", {} + ).items(): + xcc.SetBuildSetting(xck, xcv) + if "xcode_config_file" in build_file_configuration_named: + config_ref = self.project.AddOrGetFileInRootGroup( + build_file_configurations[config_name]["xcode_config_file"] + ) + xcc.SetBaseConfiguration(config_ref) + + # Sort the targets based on how they appeared in the input. + # TODO(mark): Like a lot of other things here, this assumes internal + # knowledge of PBXProject - in this case, of its "targets" property. + + # ordinary_targets are ordinary targets that are already in the project + # file. run_test_targets are the targets that run unittests and should be + # used for the Run All Tests target. support_targets are the action/rule + # targets used by GYP file targets, just kept for the assert check. + ordinary_targets = [] + run_test_targets = [] + support_targets = [] + + # targets is full list of targets in the project. + targets = [] + + # does the it define it's own "all"? + has_custom_all = False + + # targets_for_all is the list of ordinary_targets that should be listed + # in this project's "All" target. It includes each non_runtest_target + # that does not have suppress_wildcard set. + targets_for_all = [] + + for target in self.build_file_dict["targets"]: + target_name = target["target_name"] + toolset = target["toolset"] + qualified_target = gyp.common.QualifiedTarget( + self.gyp_path, target_name, toolset + ) + xcode_target = xcode_targets[qualified_target] + # Make sure that the target being added to the sorted list is already in + # the unsorted list. + assert xcode_target in self.project._properties["targets"] + targets.append(xcode_target) + ordinary_targets.append(xcode_target) + if xcode_target.support_target: + support_targets.append(xcode_target.support_target) + targets.append(xcode_target.support_target) + + if not int(target.get("suppress_wildcard", False)): + targets_for_all.append(xcode_target) + + if target_name.lower() == "all": + has_custom_all = True + + # If this target has a 'run_as' attribute, add its target to the + # targets, and add it to the test targets. + if target.get("run_as"): + # Make a target to run something. It should have one + # dependency, the parent xcode target. + xccl = CreateXCConfigurationList(configurations) + run_target = gyp.xcodeproj_file.PBXAggregateTarget( + { + "name": "Run " + target_name, + "productName": xcode_target.GetProperty("productName"), + "buildConfigurationList": xccl, + }, + parent=self.project, + ) + run_target.AddDependency(xcode_target) + + command = target["run_as"] + script = "" + if command.get("working_directory"): + script = ( + script + + 'cd "%s"\n' + % gyp.xcodeproj_file.ConvertVariablesToShellSyntax( + command.get("working_directory") + ) + ) + + if command.get("environment"): + script = ( + script + + "\n".join( + [ + 'export %s="%s"' + % ( + key, + gyp.xcodeproj_file.ConvertVariablesToShellSyntax( + val + ), + ) + for (key, val) in command.get("environment").items() + ] + ) + + "\n" + ) + + # Some test end up using sockets, files on disk, etc. and can get + # confused if more then one test runs at a time. The generator + # flag 'xcode_serialize_all_test_runs' controls the forcing of all + # tests serially. It defaults to True. To get serial runs this + # little bit of python does the same as the linux flock utility to + # make sure only one runs at a time. + command_prefix = "" + if serialize_all_tests: + command_prefix = """python -c "import fcntl, subprocess, sys file = open('$TMPDIR/GYP_serialize_test_runs', 'a') fcntl.flock(file.fileno(), fcntl.LOCK_EX) sys.exit(subprocess.call(sys.argv[1:]))" """ - # If we were unable to exec for some reason, we want to exit - # with an error, and fixup variable references to be shell - # syntax instead of xcode syntax. - script = script + 'exec ' + command_prefix + '%s\nexit 1\n' % \ - gyp.xcodeproj_file.ConvertVariablesToShellSyntax( - gyp.common.EncodePOSIXShellList(command.get('action'))) - - ssbp = gyp.xcodeproj_file.PBXShellScriptBuildPhase({ - 'shellScript': script, - 'showEnvVarsInLog': 0, - }) - run_target.AppendProperty('buildPhases', ssbp) - - # Add the run target to the project file. - targets.append(run_target) - run_test_targets.append(run_target) - xcode_target.test_runner = run_target - - - # Make sure that the list of targets being replaced is the same length as - # the one replacing it, but allow for the added test runner targets. - assert len(self.project._properties['targets']) == \ - len(ordinary_targets) + len(support_targets) - - self.project._properties['targets'] = targets - - # Get rid of unnecessary levels of depth in groups like the Source group. - self.project.RootGroupsTakeOverOnlyChildren(True) - - # Sort the groups nicely. Do this after sorting the targets, because the - # Products group is sorted based on the order of the targets. - self.project.SortGroups() - - # Create an "All" target if there's more than one target in this project - # file and the project didn't define its own "All" target. Put a generated - # "All" target first so that people opening up the project for the first - # time will build everything by default. - if len(targets_for_all) > 1 and not has_custom_all: - xccl = CreateXCConfigurationList(configurations) - all_target = gyp.xcodeproj_file.PBXAggregateTarget( - { - 'buildConfigurationList': xccl, - 'name': 'All', - }, - parent=self.project) - - for target in targets_for_all: - all_target.AddDependency(target) - - # TODO(mark): This is evil because it relies on internal knowledge of - # PBXProject._properties. It's important to get the "All" target first, - # though. - self.project._properties['targets'].insert(0, all_target) - - # The same, but for run_test_targets. - if len(run_test_targets) > 1: - xccl = CreateXCConfigurationList(configurations) - run_all_tests_target = gyp.xcodeproj_file.PBXAggregateTarget( - { - 'buildConfigurationList': xccl, - 'name': 'Run All Tests', - }, - parent=self.project) - for run_test_target in run_test_targets: - run_all_tests_target.AddDependency(run_test_target) - - # Insert after the "All" target, which must exist if there is more than - # one run_test_target. - self.project._properties['targets'].insert(1, run_all_tests_target) - - def Finalize2(self, xcode_targets, xcode_target_to_target_dict): - # Finalize2 needs to happen in a separate step because the process of - # updating references to other projects depends on the ordering of targets - # within remote project files. Finalize1 is responsible for sorting duty, - # and once all project files are sorted, Finalize2 can come in and update - # these references. - - # To support making a "test runner" target that will run all the tests - # that are direct dependents of any given target, we look for - # xcode_create_dependents_test_runner being set on an Aggregate target, - # and generate a second target that will run the tests runners found under - # the marked target. - for bf_tgt in self.build_file_dict['targets']: - if int(bf_tgt.get('xcode_create_dependents_test_runner', 0)): - tgt_name = bf_tgt['target_name'] - toolset = bf_tgt['toolset'] - qualified_target = gyp.common.QualifiedTarget(self.gyp_path, - tgt_name, toolset) - xcode_target = xcode_targets[qualified_target] - if isinstance(xcode_target, gyp.xcodeproj_file.PBXAggregateTarget): - # Collect all the run test targets. - all_run_tests = [] - pbxtds = xcode_target.GetProperty('dependencies') - for pbxtd in pbxtds: - pbxcip = pbxtd.GetProperty('targetProxy') - dependency_xct = pbxcip.GetProperty('remoteGlobalIDString') - if hasattr(dependency_xct, 'test_runner'): - all_run_tests.append(dependency_xct.test_runner) - - # Directly depend on all the runners as they depend on the target - # that builds them. - if len(all_run_tests) > 0: - run_all_target = gyp.xcodeproj_file.PBXAggregateTarget({ - 'name': 'Run %s Tests' % tgt_name, - 'productName': tgt_name, - }, - parent=self.project) - for run_test_target in all_run_tests: - run_all_target.AddDependency(run_test_target) - - # Insert the test runner after the related target. - idx = self.project._properties['targets'].index(xcode_target) - self.project._properties['targets'].insert(idx + 1, run_all_target) - - # Update all references to other projects, to make sure that the lists of - # remote products are complete. Otherwise, Xcode will fill them in when - # it opens the project file, which will result in unnecessary diffs. - # TODO(mark): This is evil because it relies on internal knowledge of - # PBXProject._other_pbxprojects. - for other_pbxproject in self.project._other_pbxprojects.keys(): - self.project.AddOrGetProjectReference(other_pbxproject) - - self.project.SortRemoteProductReferences() - - # Give everything an ID. - self.project_file.ComputeIDs() - - # Make sure that no two objects in the project file have the same ID. If - # multiple objects wind up with the same ID, upon loading the file, Xcode - # will only recognize one object (the last one in the file?) and the - # results are unpredictable. - self.project_file.EnsureNoIDCollisions() - - def Write(self): - # Write the project file to a temporary location first. Xcode watches for - # changes to the project file and presents a UI sheet offering to reload - # the project when it does change. However, in some cases, especially when - # multiple projects are open or when Xcode is busy, things don't work so - # seamlessly. Sometimes, Xcode is able to detect that a project file has - # changed but can't unload it because something else is referencing it. - # To mitigate this problem, and to avoid even having Xcode present the UI - # sheet when an open project is rewritten for inconsequential changes, the - # project file is written to a temporary file in the xcodeproj directory - # first. The new temporary file is then compared to the existing project - # file, if any. If they differ, the new file replaces the old; otherwise, - # the new project file is simply deleted. Xcode properly detects a file - # being renamed over an open project file as a change and so it remains - # able to present the "project file changed" sheet under this system. - # Writing to a temporary file first also avoids the possible problem of - # Xcode rereading an incomplete project file. - (output_fd, new_pbxproj_path) = \ - tempfile.mkstemp(suffix='.tmp', prefix='project.pbxproj.gyp.', - dir=self.path) - - try: - output_file = os.fdopen(output_fd, 'wb') - - self.project_file.Print(output_file) - output_file.close() - - pbxproj_path = os.path.join(self.path, 'project.pbxproj') - - same = False - try: - same = filecmp.cmp(pbxproj_path, new_pbxproj_path, False) - except OSError as e: - if e.errno != errno.ENOENT: - raise - - if same: - # The new file is identical to the old one, just get rid of the new - # one. - os.unlink(new_pbxproj_path) - else: - # The new file is different from the old one, or there is no old one. - # Rename the new file to the permanent name. - # - # tempfile.mkstemp uses an overly restrictive mode, resulting in a - # file that can only be read by the owner, regardless of the umask. - # There's no reason to not respect the umask here, which means that - # an extra hoop is required to fetch it and reset the new file's mode. - # - # No way to get the umask without setting a new one? Set a safe one - # and then set it back to the old value. - umask = os.umask(0o77) - os.umask(umask) - - os.chmod(new_pbxproj_path, 0o666 & ~umask) - os.rename(new_pbxproj_path, pbxproj_path) - - except Exception: - # Don't leave turds behind. In fact, if this code was responsible for - # creating the xcodeproj directory, get rid of that too. - os.unlink(new_pbxproj_path) - if self.created_dir: - shutil.rmtree(self.path, True) - raise + # If we were unable to exec for some reason, we want to exit + # with an error, and fixup variable references to be shell + # syntax instead of xcode syntax. + script = ( + script + + "exec " + + command_prefix + + "%s\nexit 1\n" + % gyp.xcodeproj_file.ConvertVariablesToShellSyntax( + gyp.common.EncodePOSIXShellList(command.get("action")) + ) + ) + + ssbp = gyp.xcodeproj_file.PBXShellScriptBuildPhase( + {"shellScript": script, "showEnvVarsInLog": 0} + ) + run_target.AppendProperty("buildPhases", ssbp) + + # Add the run target to the project file. + targets.append(run_target) + run_test_targets.append(run_target) + xcode_target.test_runner = run_target + + # Make sure that the list of targets being replaced is the same length as + # the one replacing it, but allow for the added test runner targets. + assert len(self.project._properties["targets"]) == len(ordinary_targets) + len( + support_targets + ) + + self.project._properties["targets"] = targets + + # Get rid of unnecessary levels of depth in groups like the Source group. + self.project.RootGroupsTakeOverOnlyChildren(True) + + # Sort the groups nicely. Do this after sorting the targets, because the + # Products group is sorted based on the order of the targets. + self.project.SortGroups() + + # Create an "All" target if there's more than one target in this project + # file and the project didn't define its own "All" target. Put a generated + # "All" target first so that people opening up the project for the first + # time will build everything by default. + if len(targets_for_all) > 1 and not has_custom_all: + xccl = CreateXCConfigurationList(configurations) + all_target = gyp.xcodeproj_file.PBXAggregateTarget( + {"buildConfigurationList": xccl, "name": "All"}, parent=self.project + ) + + for target in targets_for_all: + all_target.AddDependency(target) + + # TODO(mark): This is evil because it relies on internal knowledge of + # PBXProject._properties. It's important to get the "All" target first, + # though. + self.project._properties["targets"].insert(0, all_target) + + # The same, but for run_test_targets. + if len(run_test_targets) > 1: + xccl = CreateXCConfigurationList(configurations) + run_all_tests_target = gyp.xcodeproj_file.PBXAggregateTarget( + {"buildConfigurationList": xccl, "name": "Run All Tests"}, + parent=self.project, + ) + for run_test_target in run_test_targets: + run_all_tests_target.AddDependency(run_test_target) + + # Insert after the "All" target, which must exist if there is more than + # one run_test_target. + self.project._properties["targets"].insert(1, run_all_tests_target) + + def Finalize2(self, xcode_targets, xcode_target_to_target_dict): + # Finalize2 needs to happen in a separate step because the process of + # updating references to other projects depends on the ordering of targets + # within remote project files. Finalize1 is responsible for sorting duty, + # and once all project files are sorted, Finalize2 can come in and update + # these references. + + # To support making a "test runner" target that will run all the tests + # that are direct dependents of any given target, we look for + # xcode_create_dependents_test_runner being set on an Aggregate target, + # and generate a second target that will run the tests runners found under + # the marked target. + for bf_tgt in self.build_file_dict["targets"]: + if int(bf_tgt.get("xcode_create_dependents_test_runner", 0)): + tgt_name = bf_tgt["target_name"] + toolset = bf_tgt["toolset"] + qualified_target = gyp.common.QualifiedTarget( + self.gyp_path, tgt_name, toolset + ) + xcode_target = xcode_targets[qualified_target] + if isinstance(xcode_target, gyp.xcodeproj_file.PBXAggregateTarget): + # Collect all the run test targets. + all_run_tests = [] + pbxtds = xcode_target.GetProperty("dependencies") + for pbxtd in pbxtds: + pbxcip = pbxtd.GetProperty("targetProxy") + dependency_xct = pbxcip.GetProperty("remoteGlobalIDString") + if hasattr(dependency_xct, "test_runner"): + all_run_tests.append(dependency_xct.test_runner) + + # Directly depend on all the runners as they depend on the target + # that builds them. + if len(all_run_tests) > 0: + run_all_target = gyp.xcodeproj_file.PBXAggregateTarget( + { + "name": "Run %s Tests" % tgt_name, + "productName": tgt_name, + }, + parent=self.project, + ) + for run_test_target in all_run_tests: + run_all_target.AddDependency(run_test_target) + + # Insert the test runner after the related target. + idx = self.project._properties["targets"].index(xcode_target) + self.project._properties["targets"].insert( + idx + 1, run_all_target + ) + + # Update all references to other projects, to make sure that the lists of + # remote products are complete. Otherwise, Xcode will fill them in when + # it opens the project file, which will result in unnecessary diffs. + # TODO(mark): This is evil because it relies on internal knowledge of + # PBXProject._other_pbxprojects. + for other_pbxproject in self.project._other_pbxprojects.keys(): + self.project.AddOrGetProjectReference(other_pbxproject) + + self.project.SortRemoteProductReferences() + + # Give everything an ID. + self.project_file.ComputeIDs() + + # Make sure that no two objects in the project file have the same ID. If + # multiple objects wind up with the same ID, upon loading the file, Xcode + # will only recognize one object (the last one in the file?) and the + # results are unpredictable. + self.project_file.EnsureNoIDCollisions() + + def Write(self): + # Write the project file to a temporary location first. Xcode watches for + # changes to the project file and presents a UI sheet offering to reload + # the project when it does change. However, in some cases, especially when + # multiple projects are open or when Xcode is busy, things don't work so + # seamlessly. Sometimes, Xcode is able to detect that a project file has + # changed but can't unload it because something else is referencing it. + # To mitigate this problem, and to avoid even having Xcode present the UI + # sheet when an open project is rewritten for inconsequential changes, the + # project file is written to a temporary file in the xcodeproj directory + # first. The new temporary file is then compared to the existing project + # file, if any. If they differ, the new file replaces the old; otherwise, + # the new project file is simply deleted. Xcode properly detects a file + # being renamed over an open project file as a change and so it remains + # able to present the "project file changed" sheet under this system. + # Writing to a temporary file first also avoids the possible problem of + # Xcode rereading an incomplete project file. + (output_fd, new_pbxproj_path) = tempfile.mkstemp( + suffix=".tmp", prefix="project.pbxproj.gyp.", dir=self.path + ) + + try: + output_file = os.fdopen(output_fd, "w") + + self.project_file.Print(output_file) + output_file.close() + + pbxproj_path = os.path.join(self.path, "project.pbxproj") + + same = False + try: + same = filecmp.cmp(pbxproj_path, new_pbxproj_path, False) + except OSError as e: + if e.errno != errno.ENOENT: + raise + + if same: + # The new file is identical to the old one, just get rid of the new + # one. + os.unlink(new_pbxproj_path) + else: + # The new file is different from the old one, or there is no old one. + # Rename the new file to the permanent name. + # + # tempfile.mkstemp uses an overly restrictive mode, resulting in a + # file that can only be read by the owner, regardless of the umask. + # There's no reason to not respect the umask here, which means that + # an extra hoop is required to fetch it and reset the new file's mode. + # + # No way to get the umask without setting a new one? Set a safe one + # and then set it back to the old value. + umask = os.umask(0o77) + os.umask(umask) + + os.chmod(new_pbxproj_path, 0o666 & ~umask) + os.rename(new_pbxproj_path, pbxproj_path) + + except Exception: + # Don't leave turds behind. In fact, if this code was responsible for + # creating the xcodeproj directory, get rid of that too. + os.unlink(new_pbxproj_path) + if self.created_dir: + shutil.rmtree(self.path, True) + raise def AddSourceToTarget(source, type, pbxp, xct): - # TODO(mark): Perhaps source_extensions and library_extensions can be made a - # little bit fancier. - source_extensions = ['c', 'cc', 'cpp', 'cxx', 'm', 'mm', 's', 'swift'] - - # .o is conceptually more of a "source" than a "library," but Xcode thinks - # of "sources" as things to compile and "libraries" (or "frameworks") as - # things to link with. Adding an object file to an Xcode target's frameworks - # phase works properly. - library_extensions = ['a', 'dylib', 'framework', 'o'] - - basename = posixpath.basename(source) - (root, ext) = posixpath.splitext(basename) - if ext: - ext = ext[1:].lower() - - if ext in source_extensions and type != 'none': - xct.SourcesPhase().AddFile(source) - elif ext in library_extensions and type != 'none': - xct.FrameworksPhase().AddFile(source) - else: - # Files that aren't added to a sources or frameworks build phase can still - # go into the project file, just not as part of a build phase. - pbxp.AddOrGetFileInRootGroup(source) + # TODO(mark): Perhaps source_extensions and library_extensions can be made a + # little bit fancier. + source_extensions = ["c", "cc", "cpp", "cxx", "m", "mm", "s", "swift"] + + # .o is conceptually more of a "source" than a "library," but Xcode thinks + # of "sources" as things to compile and "libraries" (or "frameworks") as + # things to link with. Adding an object file to an Xcode target's frameworks + # phase works properly. + library_extensions = ["a", "dylib", "framework", "o"] + + basename = posixpath.basename(source) + (root, ext) = posixpath.splitext(basename) + if ext: + ext = ext[1:].lower() + + if ext in source_extensions and type != "none": + xct.SourcesPhase().AddFile(source) + elif ext in library_extensions and type != "none": + xct.FrameworksPhase().AddFile(source) + else: + # Files that aren't added to a sources or frameworks build phase can still + # go into the project file, just not as part of a build phase. + pbxp.AddOrGetFileInRootGroup(source) def AddResourceToTarget(resource, pbxp, xct): - # TODO(mark): Combine with AddSourceToTarget above? Or just inline this call - # where it's used. - xct.ResourcesPhase().AddFile(resource) + # TODO(mark): Combine with AddSourceToTarget above? Or just inline this call + # where it's used. + xct.ResourcesPhase().AddFile(resource) def AddHeaderToTarget(header, pbxp, xct, is_public): - # TODO(mark): Combine with AddSourceToTarget above? Or just inline this call - # where it's used. - settings = '{ATTRIBUTES = (%s, ); }' % ('Private', 'Public')[is_public] - xct.HeadersPhase().AddFile(header, settings) + # TODO(mark): Combine with AddSourceToTarget above? Or just inline this call + # where it's used. + settings = "{ATTRIBUTES = (%s, ); }" % ("Private", "Public")[is_public] + xct.HeadersPhase().AddFile(header, settings) + + +_xcode_variable_re = re.compile(r"(\$\((.*?)\))") -_xcode_variable_re = re.compile(r'(\$\((.*?)\))') def ExpandXcodeVariables(string, expansions): - """Expands Xcode-style $(VARIABLES) in string per the expansions dict. + """Expands Xcode-style $(VARIABLES) in string per the expansions dict. In some rare cases, it is appropriate to expand Xcode variables when a project file is generated. For any substring $(VAR) in string, if VAR is a @@ -540,774 +572,824 @@ def ExpandXcodeVariables(string, expansions): dict will remain in the returned string. """ - matches = _xcode_variable_re.findall(string) - if matches is None: - return string + matches = _xcode_variable_re.findall(string) + if matches is None: + return string - matches.reverse() - for match in matches: - (to_replace, variable) = match - if not variable in expansions: - continue + matches.reverse() + for match in matches: + (to_replace, variable) = match + if variable not in expansions: + continue - replacement = expansions[variable] - string = re.sub(re.escape(to_replace), replacement, string) + replacement = expansions[variable] + string = re.sub(re.escape(to_replace), replacement, string) + + return string - return string + +_xcode_define_re = re.compile(r"([\\\"\' ])") -_xcode_define_re = re.compile(r'([\\\"\' ])') def EscapeXcodeDefine(s): - """We must escape the defines that we give to XCode so that it knows not to + """We must escape the defines that we give to XCode so that it knows not to split on spaces and to respect backslash and quote literals. However, we - must not quote the define, or Xcode will incorrectly intepret variables + must not quote the define, or Xcode will incorrectly interpret variables especially $(inherited).""" - return re.sub(_xcode_define_re, r'\\\1', s) + return re.sub(_xcode_define_re, r"\\\1", s) def PerformBuild(data, configurations, params): - options = params['options'] + options = params["options"] - for build_file, build_file_dict in data.items(): - (build_file_root, build_file_ext) = os.path.splitext(build_file) - if build_file_ext != '.gyp': - continue - xcodeproj_path = build_file_root + options.suffix + '.xcodeproj' - if options.generator_output: - xcodeproj_path = os.path.join(options.generator_output, xcodeproj_path) + for build_file, build_file_dict in data.items(): + (build_file_root, build_file_ext) = os.path.splitext(build_file) + if build_file_ext != ".gyp": + continue + xcodeproj_path = build_file_root + options.suffix + ".xcodeproj" + if options.generator_output: + xcodeproj_path = os.path.join(options.generator_output, xcodeproj_path) - for config in configurations: - arguments = ['xcodebuild', '-project', xcodeproj_path] - arguments += ['-configuration', config] - print("Building [%s]: %s" % (config, arguments)) - subprocess.check_call(arguments) + for config in configurations: + arguments = ["xcodebuild", "-project", xcodeproj_path] + arguments += ["-configuration", config] + print("Building [%s]: %s" % (config, arguments)) + subprocess.check_call(arguments) def CalculateGeneratorInputInfo(params): - toplevel = params['options'].toplevel_dir - if params.get('flavor') == 'ninja': - generator_dir = os.path.relpath(params['options'].generator_output or '.') - output_dir = params.get('generator_flags', {}).get('output_dir', 'out') - output_dir = os.path.normpath(os.path.join(generator_dir, output_dir)) - qualified_out_dir = os.path.normpath(os.path.join( - toplevel, output_dir, 'gypfiles-xcode-ninja')) - else: - output_dir = os.path.normpath(os.path.join(toplevel, 'xcodebuild')) - qualified_out_dir = os.path.normpath(os.path.join( - toplevel, output_dir, 'gypfiles')) - - global generator_filelist_paths - generator_filelist_paths = { - 'toplevel': toplevel, - 'qualified_out_dir': qualified_out_dir, - } + toplevel = params["options"].toplevel_dir + if params.get("flavor") == "ninja": + generator_dir = os.path.relpath(params["options"].generator_output or ".") + output_dir = params.get("generator_flags", {}).get("output_dir", "out") + output_dir = os.path.normpath(os.path.join(generator_dir, output_dir)) + qualified_out_dir = os.path.normpath( + os.path.join(toplevel, output_dir, "gypfiles-xcode-ninja") + ) + else: + output_dir = os.path.normpath(os.path.join(toplevel, "xcodebuild")) + qualified_out_dir = os.path.normpath( + os.path.join(toplevel, output_dir, "gypfiles") + ) + + global generator_filelist_paths + generator_filelist_paths = { + "toplevel": toplevel, + "qualified_out_dir": qualified_out_dir, + } def GenerateOutput(target_list, target_dicts, data, params): - # Optionally configure each spec to use ninja as the external builder. - ninja_wrapper = params.get('flavor') == 'ninja' - if ninja_wrapper: - (target_list, target_dicts, data) = \ - gyp.xcode_ninja.CreateWrapper(target_list, target_dicts, data, params) - - options = params['options'] - generator_flags = params.get('generator_flags', {}) - parallel_builds = generator_flags.get('xcode_parallel_builds', True) - serialize_all_tests = \ - generator_flags.get('xcode_serialize_all_test_runs', True) - upgrade_check_project_version = \ - generator_flags.get('xcode_upgrade_check_project_version', None) - - # Format upgrade_check_project_version with leading zeros as needed. - if upgrade_check_project_version: - upgrade_check_project_version = str(upgrade_check_project_version) - while len(upgrade_check_project_version) < 4: - upgrade_check_project_version = '0' + upgrade_check_project_version - - skip_excluded_files = \ - not generator_flags.get('xcode_list_excluded_files', True) - xcode_projects = {} - for build_file, build_file_dict in data.items(): - (build_file_root, build_file_ext) = os.path.splitext(build_file) - if build_file_ext != '.gyp': - continue - xcodeproj_path = build_file_root + options.suffix + '.xcodeproj' - if options.generator_output: - xcodeproj_path = os.path.join(options.generator_output, xcodeproj_path) - xcp = XcodeProject(build_file, xcodeproj_path, build_file_dict) - xcode_projects[build_file] = xcp - pbxp = xcp.project - - # Set project-level attributes from multiple options - project_attributes = {} - if parallel_builds: - project_attributes['BuildIndependentTargetsInParallel'] = 'YES' + # Optionally configure each spec to use ninja as the external builder. + ninja_wrapper = params.get("flavor") == "ninja" + if ninja_wrapper: + (target_list, target_dicts, data) = gyp.xcode_ninja.CreateWrapper( + target_list, target_dicts, data, params + ) + + options = params["options"] + generator_flags = params.get("generator_flags", {}) + parallel_builds = generator_flags.get("xcode_parallel_builds", True) + serialize_all_tests = generator_flags.get("xcode_serialize_all_test_runs", True) + upgrade_check_project_version = generator_flags.get( + "xcode_upgrade_check_project_version", None + ) + + # Format upgrade_check_project_version with leading zeros as needed. if upgrade_check_project_version: - project_attributes['LastUpgradeCheck'] = upgrade_check_project_version - project_attributes['LastTestingUpgradeCheck'] = \ - upgrade_check_project_version - project_attributes['LastSwiftUpdateCheck'] = \ - upgrade_check_project_version - pbxp.SetProperty('attributes', project_attributes) - - # Add gyp/gypi files to project - if not generator_flags.get('standalone'): - main_group = pbxp.GetProperty('mainGroup') - build_group = gyp.xcodeproj_file.PBXGroup({'name': 'Build'}) - main_group.AppendChild(build_group) - for included_file in build_file_dict['included_files']: - build_group.AddOrGetFileByPath(included_file, False) - - xcode_targets = {} - xcode_target_to_target_dict = {} - for qualified_target in target_list: - [build_file, target_name, toolset] = \ - gyp.common.ParseQualifiedTarget(qualified_target) - - spec = target_dicts[qualified_target] - if spec['toolset'] != 'target': - raise Exception( - 'Multiple toolsets not supported in xcode build (target %s)' % - qualified_target) - configuration_names = [spec['default_configuration']] - for configuration_name in sorted(spec['configurations'].keys()): - if configuration_name not in configuration_names: - configuration_names.append(configuration_name) - xcp = xcode_projects[build_file] - pbxp = xcp.project - - # Set up the configurations for the target according to the list of names - # supplied. - xccl = CreateXCConfigurationList(configuration_names) - - # Create an XCTarget subclass object for the target. The type with - # "+bundle" appended will be used if the target has "mac_bundle" set. - # loadable_modules not in a mac_bundle are mapped to - # com.googlecode.gyp.xcode.bundle, a pseudo-type that xcode.py interprets - # to create a single-file mh_bundle. - _types = { - 'executable': 'com.apple.product-type.tool', - 'loadable_module': 'com.googlecode.gyp.xcode.bundle', - 'shared_library': 'com.apple.product-type.library.dynamic', - 'static_library': 'com.apple.product-type.library.static', - 'mac_kernel_extension': 'com.apple.product-type.kernel-extension', - 'executable+bundle': 'com.apple.product-type.application', - 'loadable_module+bundle': 'com.apple.product-type.bundle', - 'loadable_module+xctest': 'com.apple.product-type.bundle.unit-test', - 'loadable_module+xcuitest': 'com.apple.product-type.bundle.ui-testing', - 'shared_library+bundle': 'com.apple.product-type.framework', - 'executable+extension+bundle': 'com.apple.product-type.app-extension', - 'executable+watch+extension+bundle': - 'com.apple.product-type.watchkit-extension', - 'executable+watch+bundle': - 'com.apple.product-type.application.watchapp', - 'mac_kernel_extension+bundle': 'com.apple.product-type.kernel-extension', - } - - target_properties = { - 'buildConfigurationList': xccl, - 'name': target_name, - } + upgrade_check_project_version = str(upgrade_check_project_version) + while len(upgrade_check_project_version) < 4: + upgrade_check_project_version = "0" + upgrade_check_project_version + + skip_excluded_files = not generator_flags.get("xcode_list_excluded_files", True) + xcode_projects = {} + for build_file, build_file_dict in data.items(): + (build_file_root, build_file_ext) = os.path.splitext(build_file) + if build_file_ext != ".gyp": + continue + xcodeproj_path = build_file_root + options.suffix + ".xcodeproj" + if options.generator_output: + xcodeproj_path = os.path.join(options.generator_output, xcodeproj_path) + xcp = XcodeProject(build_file, xcodeproj_path, build_file_dict) + xcode_projects[build_file] = xcp + pbxp = xcp.project + + # Set project-level attributes from multiple options + project_attributes = {} + if parallel_builds: + project_attributes["BuildIndependentTargetsInParallel"] = "YES" + if upgrade_check_project_version: + project_attributes["LastUpgradeCheck"] = upgrade_check_project_version + project_attributes[ + "LastTestingUpgradeCheck" + ] = upgrade_check_project_version + project_attributes["LastSwiftUpdateCheck"] = upgrade_check_project_version + pbxp.SetProperty("attributes", project_attributes) + + # Add gyp/gypi files to project + if not generator_flags.get("standalone"): + main_group = pbxp.GetProperty("mainGroup") + build_group = gyp.xcodeproj_file.PBXGroup({"name": "Build"}) + main_group.AppendChild(build_group) + for included_file in build_file_dict["included_files"]: + build_group.AddOrGetFileByPath(included_file, False) + + xcode_targets = {} + xcode_target_to_target_dict = {} + for qualified_target in target_list: + [build_file, target_name, toolset] = gyp.common.ParseQualifiedTarget( + qualified_target + ) + + spec = target_dicts[qualified_target] + if spec["toolset"] != "target": + raise Exception( + "Multiple toolsets not supported in xcode build (target %s)" + % qualified_target + ) + configuration_names = [spec["default_configuration"]] + for configuration_name in sorted(spec["configurations"].keys()): + if configuration_name not in configuration_names: + configuration_names.append(configuration_name) + xcp = xcode_projects[build_file] + pbxp = xcp.project + + # Set up the configurations for the target according to the list of names + # supplied. + xccl = CreateXCConfigurationList(configuration_names) + + # Create an XCTarget subclass object for the target. The type with + # "+bundle" appended will be used if the target has "mac_bundle" set. + # loadable_modules not in a mac_bundle are mapped to + # com.googlecode.gyp.xcode.bundle, a pseudo-type that xcode.py interprets + # to create a single-file mh_bundle. + _types = { + "executable": "com.apple.product-type.tool", + "loadable_module": "com.googlecode.gyp.xcode.bundle", + "shared_library": "com.apple.product-type.library.dynamic", + "static_library": "com.apple.product-type.library.static", + "mac_kernel_extension": "com.apple.product-type.kernel-extension", + "executable+bundle": "com.apple.product-type.application", + "loadable_module+bundle": "com.apple.product-type.bundle", + "loadable_module+xctest": "com.apple.product-type.bundle.unit-test", + "loadable_module+xcuitest": "com.apple.product-type.bundle.ui-testing", + "shared_library+bundle": "com.apple.product-type.framework", + "executable+extension+bundle": "com.apple.product-type.app-extension", + "executable+watch+extension+bundle": + "com.apple.product-type.watchkit-extension", + "executable+watch+bundle": "com.apple.product-type.application.watchapp", + "mac_kernel_extension+bundle": "com.apple.product-type.kernel-extension", + } - type = spec['type'] - is_xctest = int(spec.get('mac_xctest_bundle', 0)) - is_xcuitest = int(spec.get('mac_xcuitest_bundle', 0)) - is_bundle = int(spec.get('mac_bundle', 0)) or is_xctest - is_app_extension = int(spec.get('ios_app_extension', 0)) - is_watchkit_extension = int(spec.get('ios_watchkit_extension', 0)) - is_watch_app = int(spec.get('ios_watch_app', 0)) - if type != 'none': - type_bundle_key = type - if is_xcuitest: - type_bundle_key += '+xcuitest' - assert type == 'loadable_module', ( - 'mac_xcuitest_bundle targets must have type loadable_module ' - '(target %s)' % target_name) - elif is_xctest: - type_bundle_key += '+xctest' - assert type == 'loadable_module', ( - 'mac_xctest_bundle targets must have type loadable_module ' - '(target %s)' % target_name) - elif is_app_extension: - assert is_bundle, ('ios_app_extension flag requires mac_bundle ' - '(target %s)' % target_name) - type_bundle_key += '+extension+bundle' - elif is_watchkit_extension: - assert is_bundle, ('ios_watchkit_extension flag requires mac_bundle ' - '(target %s)' % target_name) - type_bundle_key += '+watch+extension+bundle' - elif is_watch_app: - assert is_bundle, ('ios_watch_app flag requires mac_bundle ' - '(target %s)' % target_name) - type_bundle_key += '+watch+bundle' - elif is_bundle: - type_bundle_key += '+bundle' - - xctarget_type = gyp.xcodeproj_file.PBXNativeTarget - try: - target_properties['productType'] = _types[type_bundle_key] - except KeyError as e: - gyp.common.ExceptionAppend(e, "-- unknown product type while " - "writing target %s" % target_name) - raise - else: - xctarget_type = gyp.xcodeproj_file.PBXAggregateTarget - assert not is_bundle, ( - 'mac_bundle targets cannot have type none (target "%s")' % - target_name) - assert not is_xcuitest, ( - 'mac_xcuitest_bundle targets cannot have type none (target "%s")' % - target_name) - assert not is_xctest, ( - 'mac_xctest_bundle targets cannot have type none (target "%s")' % - target_name) - - target_product_name = spec.get('product_name') - if target_product_name is not None: - target_properties['productName'] = target_product_name - - xct = xctarget_type(target_properties, parent=pbxp, - force_outdir=spec.get('product_dir'), - force_prefix=spec.get('product_prefix'), - force_extension=spec.get('product_extension')) - pbxp.AppendProperty('targets', xct) - xcode_targets[qualified_target] = xct - xcode_target_to_target_dict[xct] = spec - - spec_actions = spec.get('actions', []) - spec_rules = spec.get('rules', []) - - # Xcode has some "issues" with checking dependencies for the "Compile - # sources" step with any source files/headers generated by actions/rules. - # To work around this, if a target is building anything directly (not - # type "none"), then a second target is used to run the GYP actions/rules - # and is made a dependency of this target. This way the work is done - # before the dependency checks for what should be recompiled. - support_xct = None - # The Xcode "issues" don't affect xcode-ninja builds, since the dependency - # logic all happens in ninja. Don't bother creating the extra targets in - # that case. - if type != 'none' and (spec_actions or spec_rules) and not ninja_wrapper: - support_xccl = CreateXCConfigurationList(configuration_names) - support_target_suffix = generator_flags.get( - 'support_target_suffix', ' Support') - support_target_properties = { - 'buildConfigurationList': support_xccl, - 'name': target_name + support_target_suffix, - } - if target_product_name: - support_target_properties['productName'] = \ - target_product_name + ' Support' - support_xct = \ - gyp.xcodeproj_file.PBXAggregateTarget(support_target_properties, - parent=pbxp) - pbxp.AppendProperty('targets', support_xct) - xct.AddDependency(support_xct) - # Hang the support target off the main target so it can be tested/found - # by the generator during Finalize. - xct.support_target = support_xct - - prebuild_index = 0 - - # Add custom shell script phases for "actions" sections. - for action in spec_actions: - # There's no need to write anything into the script to ensure that the - # output directories already exist, because Xcode will look at the - # declared outputs and automatically ensure that they exist for us. - - # Do we have a message to print when this action runs? - message = action.get('message') - if message: - message = 'echo note: ' + gyp.common.EncodePOSIXShellArgument(message) - else: - message = '' - - # Turn the list into a string that can be passed to a shell. - action_string = gyp.common.EncodePOSIXShellList(action['action']) - - # Convert Xcode-type variable references to sh-compatible environment - # variable references. - message_sh = gyp.xcodeproj_file.ConvertVariablesToShellSyntax(message) - action_string_sh = gyp.xcodeproj_file.ConvertVariablesToShellSyntax( - action_string) - - script = '' - # Include the optional message - if message_sh: - script += message_sh + '\n' - # Be sure the script runs in exec, and that if exec fails, the script - # exits signalling an error. - script += 'exec ' + action_string_sh + '\nexit 1\n' - ssbp = gyp.xcodeproj_file.PBXShellScriptBuildPhase({ - 'inputPaths': action['inputs'], - 'name': 'Action "' + action['action_name'] + '"', - 'outputPaths': action['outputs'], - 'shellScript': script, - 'showEnvVarsInLog': 0, - }) - - if support_xct: - support_xct.AppendProperty('buildPhases', ssbp) - else: - # TODO(mark): this assumes too much knowledge of the internals of - # xcodeproj_file; some of these smarts should move into xcodeproj_file - # itself. - xct._properties['buildPhases'].insert(prebuild_index, ssbp) - prebuild_index = prebuild_index + 1 - - # TODO(mark): Should verify that at most one of these is specified. - if int(action.get('process_outputs_as_sources', False)): - for output in action['outputs']: - AddSourceToTarget(output, type, pbxp, xct) - - if int(action.get('process_outputs_as_mac_bundle_resources', False)): - for output in action['outputs']: - AddResourceToTarget(output, pbxp, xct) - - # tgt_mac_bundle_resources holds the list of bundle resources so - # the rule processing can check against it. - if is_bundle: - tgt_mac_bundle_resources = spec.get('mac_bundle_resources', []) - else: - tgt_mac_bundle_resources = [] - - # Add custom shell script phases driving "make" for "rules" sections. - # - # Xcode's built-in rule support is almost powerful enough to use directly, - # but there are a few significant deficiencies that render them unusable. - # There are workarounds for some of its inadequacies, but in aggregate, - # the workarounds added complexity to the generator, and some workarounds - # actually require input files to be crafted more carefully than I'd like. - # Consequently, until Xcode rules are made more capable, "rules" input - # sections will be handled in Xcode output by shell script build phases - # performed prior to the compilation phase. - # - # The following problems with Xcode rules were found. The numbers are - # Apple radar IDs. I hope that these shortcomings are addressed, I really - # liked having the rules handled directly in Xcode during the period that - # I was prototyping this. - # - # 6588600 Xcode compiles custom script rule outputs too soon, compilation - # fails. This occurs when rule outputs from distinct inputs are - # interdependent. The only workaround is to put rules and their - # inputs in a separate target from the one that compiles the rule - # outputs. This requires input file cooperation and it means that - # process_outputs_as_sources is unusable. - # 6584932 Need to declare that custom rule outputs should be excluded from - # compilation. A possible workaround is to lie to Xcode about a - # rule's output, giving it a dummy file it doesn't know how to - # compile. The rule action script would need to touch the dummy. - # 6584839 I need a way to declare additional inputs to a custom rule. - # A possible workaround is a shell script phase prior to - # compilation that touches a rule's primary input files if any - # would-be additional inputs are newer than the output. Modifying - # the source tree - even just modification times - feels dirty. - # 6564240 Xcode "custom script" build rules always dump all environment - # variables. This is a low-prioroty problem and is not a - # show-stopper. - rules_by_ext = {} - for rule in spec_rules: - rules_by_ext[rule['extension']] = rule - - # First, some definitions: - # - # A "rule source" is a file that was listed in a target's "sources" - # list and will have a rule applied to it on the basis of matching the - # rule's "extensions" attribute. Rule sources are direct inputs to - # rules. - # - # Rule definitions may specify additional inputs in their "inputs" - # attribute. These additional inputs are used for dependency tracking - # purposes. - # - # A "concrete output" is a rule output with input-dependent variables - # resolved. For example, given a rule with: - # 'extension': 'ext', 'outputs': ['$(INPUT_FILE_BASE).cc'], - # if the target's "sources" list contained "one.ext" and "two.ext", - # the "concrete output" for rule input "two.ext" would be "two.cc". If - # a rule specifies multiple outputs, each input file that the rule is - # applied to will have the same number of concrete outputs. - # - # If any concrete outputs are outdated or missing relative to their - # corresponding rule_source or to any specified additional input, the - # rule action must be performed to generate the concrete outputs. - - # concrete_outputs_by_rule_source will have an item at the same index - # as the rule['rule_sources'] that it corresponds to. Each item is a - # list of all of the concrete outputs for the rule_source. - concrete_outputs_by_rule_source = [] - - # concrete_outputs_all is a flat list of all concrete outputs that this - # rule is able to produce, given the known set of input files - # (rule_sources) that apply to it. - concrete_outputs_all = [] - - # messages & actions are keyed by the same indices as rule['rule_sources'] - # and concrete_outputs_by_rule_source. They contain the message and - # action to perform after resolving input-dependent variables. The - # message is optional, in which case None is stored for each rule source. - messages = [] - actions = [] - - for rule_source in rule.get('rule_sources', []): - rule_source_dirname, rule_source_basename = \ - posixpath.split(rule_source) - (rule_source_root, rule_source_ext) = \ - posixpath.splitext(rule_source_basename) - - # These are the same variable names that Xcode uses for its own native - # rule support. Because Xcode's rule engine is not being used, they - # need to be expanded as they are written to the makefile. - rule_input_dict = { - 'INPUT_FILE_BASE': rule_source_root, - 'INPUT_FILE_SUFFIX': rule_source_ext, - 'INPUT_FILE_NAME': rule_source_basename, - 'INPUT_FILE_PATH': rule_source, - 'INPUT_FILE_DIRNAME': rule_source_dirname, + target_properties = { + "buildConfigurationList": xccl, + "name": target_name, } - concrete_outputs_for_this_rule_source = [] - for output in rule.get('outputs', []): - # Fortunately, Xcode and make both use $(VAR) format for their - # variables, so the expansion is the only transformation necessary. - # Any remaning $(VAR)-type variables in the string can be given - # directly to make, which will pick up the correct settings from - # what Xcode puts into the environment. - concrete_output = ExpandXcodeVariables(output, rule_input_dict) - concrete_outputs_for_this_rule_source.append(concrete_output) - - # Add all concrete outputs to the project. - pbxp.AddOrGetFileInRootGroup(concrete_output) - - concrete_outputs_by_rule_source.append( \ - concrete_outputs_for_this_rule_source) - concrete_outputs_all.extend(concrete_outputs_for_this_rule_source) - - # TODO(mark): Should verify that at most one of these is specified. - if int(rule.get('process_outputs_as_sources', False)): - for output in concrete_outputs_for_this_rule_source: - AddSourceToTarget(output, type, pbxp, xct) - - # If the file came from the mac_bundle_resources list or if the rule - # is marked to process outputs as bundle resource, do so. - was_mac_bundle_resource = rule_source in tgt_mac_bundle_resources - if was_mac_bundle_resource or \ - int(rule.get('process_outputs_as_mac_bundle_resources', False)): - for output in concrete_outputs_for_this_rule_source: - AddResourceToTarget(output, pbxp, xct) - - # Do we have a message to print when this rule runs? - message = rule.get('message') - if message: - message = gyp.common.EncodePOSIXShellArgument(message) - message = ExpandXcodeVariables(message, rule_input_dict) - messages.append(message) - - # Turn the list into a string that can be passed to a shell. - action_string = gyp.common.EncodePOSIXShellList(rule['action']) - - action = ExpandXcodeVariables(action_string, rule_input_dict) - actions.append(action) - - if len(concrete_outputs_all) > 0: - # TODO(mark): There's a possibility for collision here. Consider - # target "t" rule "A_r" and target "t_A" rule "r". - makefile_name = '%s.make' % re.sub( - '[^a-zA-Z0-9_]', '_' , '%s_%s' % (target_name, rule['rule_name'])) - makefile_path = os.path.join(xcode_projects[build_file].path, - makefile_name) - # TODO(mark): try/close? Write to a temporary file and swap it only - # if it's got changes? - makefile = open(makefile_path, 'wb') - - # make will build the first target in the makefile by default. By - # convention, it's called "all". List all (or at least one) - # concrete output for each rule source as a prerequisite of the "all" - # target. - makefile.write('all: \\\n') - for concrete_output_index in \ - range(0, len(concrete_outputs_by_rule_source)): - # Only list the first (index [0]) concrete output of each input - # in the "all" target. Otherwise, a parallel make (-j > 1) would - # attempt to process each input multiple times simultaneously. - # Otherwise, "all" could just contain the entire list of - # concrete_outputs_all. - concrete_output = \ - concrete_outputs_by_rule_source[concrete_output_index][0] - if concrete_output_index == len(concrete_outputs_by_rule_source) - 1: - eol = '' - else: - eol = ' \\' - makefile.write(' %s%s\n' % (concrete_output, eol)) - - for (rule_source, concrete_outputs, message, action) in \ - zip(rule['rule_sources'], concrete_outputs_by_rule_source, - messages, actions): - makefile.write('\n') - - # Add a rule that declares it can build each concrete output of a - # rule source. Collect the names of the directories that are - # required. - concrete_output_dirs = [] - for concrete_output_index in range(0, len(concrete_outputs)): - concrete_output = concrete_outputs[concrete_output_index] - if concrete_output_index == 0: - bol = '' + type = spec["type"] + is_xctest = int(spec.get("mac_xctest_bundle", 0)) + is_xcuitest = int(spec.get("mac_xcuitest_bundle", 0)) + is_bundle = int(spec.get("mac_bundle", 0)) or is_xctest + is_app_extension = int(spec.get("ios_app_extension", 0)) + is_watchkit_extension = int(spec.get("ios_watchkit_extension", 0)) + is_watch_app = int(spec.get("ios_watch_app", 0)) + if type != "none": + type_bundle_key = type + if is_xcuitest: + type_bundle_key += "+xcuitest" + assert type == "loadable_module", ( + "mac_xcuitest_bundle targets must have type loadable_module " + "(target %s)" % target_name + ) + elif is_xctest: + type_bundle_key += "+xctest" + assert type == "loadable_module", ( + "mac_xctest_bundle targets must have type loadable_module " + "(target %s)" % target_name + ) + elif is_app_extension: + assert is_bundle, ( + "ios_app_extension flag requires mac_bundle " + "(target %s)" % target_name + ) + type_bundle_key += "+extension+bundle" + elif is_watchkit_extension: + assert is_bundle, ( + "ios_watchkit_extension flag requires mac_bundle " + "(target %s)" % target_name + ) + type_bundle_key += "+watch+extension+bundle" + elif is_watch_app: + assert is_bundle, ( + "ios_watch_app flag requires mac_bundle " + "(target %s)" % target_name + ) + type_bundle_key += "+watch+bundle" + elif is_bundle: + type_bundle_key += "+bundle" + + xctarget_type = gyp.xcodeproj_file.PBXNativeTarget + try: + target_properties["productType"] = _types[type_bundle_key] + except KeyError as e: + gyp.common.ExceptionAppend( + e, + "-- unknown product type while " "writing target %s" % target_name, + ) + raise + else: + xctarget_type = gyp.xcodeproj_file.PBXAggregateTarget + assert not is_bundle, ( + 'mac_bundle targets cannot have type none (target "%s")' % target_name + ) + assert not is_xcuitest, ( + 'mac_xcuitest_bundle targets cannot have type none (target "%s")' + % target_name + ) + assert not is_xctest, ( + 'mac_xctest_bundle targets cannot have type none (target "%s")' + % target_name + ) + + target_product_name = spec.get("product_name") + if target_product_name is not None: + target_properties["productName"] = target_product_name + + xct = xctarget_type( + target_properties, + parent=pbxp, + force_outdir=spec.get("product_dir"), + force_prefix=spec.get("product_prefix"), + force_extension=spec.get("product_extension"), + ) + pbxp.AppendProperty("targets", xct) + xcode_targets[qualified_target] = xct + xcode_target_to_target_dict[xct] = spec + + spec_actions = spec.get("actions", []) + spec_rules = spec.get("rules", []) + + # Xcode has some "issues" with checking dependencies for the "Compile + # sources" step with any source files/headers generated by actions/rules. + # To work around this, if a target is building anything directly (not + # type "none"), then a second target is used to run the GYP actions/rules + # and is made a dependency of this target. This way the work is done + # before the dependency checks for what should be recompiled. + support_xct = None + # The Xcode "issues" don't affect xcode-ninja builds, since the dependency + # logic all happens in ninja. Don't bother creating the extra targets in + # that case. + if type != "none" and (spec_actions or spec_rules) and not ninja_wrapper: + support_xccl = CreateXCConfigurationList(configuration_names) + support_target_suffix = generator_flags.get( + "support_target_suffix", " Support" + ) + support_target_properties = { + "buildConfigurationList": support_xccl, + "name": target_name + support_target_suffix, + } + if target_product_name: + support_target_properties["productName"] = ( + target_product_name + " Support" + ) + support_xct = gyp.xcodeproj_file.PBXAggregateTarget( + support_target_properties, parent=pbxp + ) + pbxp.AppendProperty("targets", support_xct) + xct.AddDependency(support_xct) + # Hang the support target off the main target so it can be tested/found + # by the generator during Finalize. + xct.support_target = support_xct + + prebuild_index = 0 + + # Add custom shell script phases for "actions" sections. + for action in spec_actions: + # There's no need to write anything into the script to ensure that the + # output directories already exist, because Xcode will look at the + # declared outputs and automatically ensure that they exist for us. + + # Do we have a message to print when this action runs? + message = action.get("message") + if message: + message = "echo note: " + gyp.common.EncodePOSIXShellArgument(message) else: - bol = ' ' - makefile.write('%s%s \\\n' % (bol, concrete_output)) - - concrete_output_dir = posixpath.dirname(concrete_output) - if (concrete_output_dir and - concrete_output_dir not in concrete_output_dirs): - concrete_output_dirs.append(concrete_output_dir) - - makefile.write(' : \\\n') - - # The prerequisites for this rule are the rule source itself and - # the set of additional rule inputs, if any. - prerequisites = [rule_source] - prerequisites.extend(rule.get('inputs', [])) - for prerequisite_index in range(0, len(prerequisites)): - prerequisite = prerequisites[prerequisite_index] - if prerequisite_index == len(prerequisites) - 1: - eol = '' + message = "" + + # Turn the list into a string that can be passed to a shell. + action_string = gyp.common.EncodePOSIXShellList(action["action"]) + + # Convert Xcode-type variable references to sh-compatible environment + # variable references. + message_sh = gyp.xcodeproj_file.ConvertVariablesToShellSyntax(message) + action_string_sh = gyp.xcodeproj_file.ConvertVariablesToShellSyntax( + action_string + ) + + script = "" + # Include the optional message + if message_sh: + script += message_sh + "\n" + # Be sure the script runs in exec, and that if exec fails, the script + # exits signalling an error. + script += "exec " + action_string_sh + "\nexit 1\n" + ssbp = gyp.xcodeproj_file.PBXShellScriptBuildPhase( + { + "inputPaths": action["inputs"], + "name": 'Action "' + action["action_name"] + '"', + "outputPaths": action["outputs"], + "shellScript": script, + "showEnvVarsInLog": 0, + } + ) + + if support_xct: + support_xct.AppendProperty("buildPhases", ssbp) else: - eol = ' \\' - makefile.write(' %s%s\n' % (prerequisite, eol)) - - # Make sure that output directories exist before executing the rule - # action. - if len(concrete_output_dirs) > 0: - makefile.write('\t@mkdir -p "%s"\n' % - '" "'.join(concrete_output_dirs)) - - # The rule message and action have already had the necessary variable - # substitutions performed. - if message: - # Mark it with note: so Xcode picks it up in build output. - makefile.write('\t@echo note: %s\n' % message) - makefile.write('\t%s\n' % action) - - makefile.close() - - # It might be nice to ensure that needed output directories exist - # here rather than in each target in the Makefile, but that wouldn't - # work if there ever was a concrete output that had an input-dependent - # variable anywhere other than in the leaf position. - - # Don't declare any inputPaths or outputPaths. If they're present, - # Xcode will provide a slight optimization by only running the script - # phase if any output is missing or outdated relative to any input. - # Unfortunately, it will also assume that all outputs are touched by - # the script, and if the outputs serve as files in a compilation - # phase, they will be unconditionally rebuilt. Since make might not - # rebuild everything that could be declared here as an output, this - # extra compilation activity is unnecessary. With inputPaths and - # outputPaths not supplied, make will always be called, but it knows - # enough to not do anything when everything is up-to-date. - - # To help speed things up, pass -j COUNT to make so it does some work - # in parallel. Don't use ncpus because Xcode will build ncpus targets - # in parallel and if each target happens to have a rules step, there - # would be ncpus^2 things going. With a machine that has 2 quad-core - # Xeons, a build can quickly run out of processes based on - # scheduling/other tasks, and randomly failing builds are no good. - script = \ -"""JOB_COUNT="$(/usr/sbin/sysctl -n hw.ncpu)" + # TODO(mark): this assumes too much knowledge of the internals of + # xcodeproj_file; some of these smarts should move into xcodeproj_file + # itself. + xct._properties["buildPhases"].insert(prebuild_index, ssbp) + prebuild_index = prebuild_index + 1 + + # TODO(mark): Should verify that at most one of these is specified. + if int(action.get("process_outputs_as_sources", False)): + for output in action["outputs"]: + AddSourceToTarget(output, type, pbxp, xct) + + if int(action.get("process_outputs_as_mac_bundle_resources", False)): + for output in action["outputs"]: + AddResourceToTarget(output, pbxp, xct) + + # tgt_mac_bundle_resources holds the list of bundle resources so + # the rule processing can check against it. + if is_bundle: + tgt_mac_bundle_resources = spec.get("mac_bundle_resources", []) + else: + tgt_mac_bundle_resources = [] + + # Add custom shell script phases driving "make" for "rules" sections. + # + # Xcode's built-in rule support is almost powerful enough to use directly, + # but there are a few significant deficiencies that render them unusable. + # There are workarounds for some of its inadequacies, but in aggregate, + # the workarounds added complexity to the generator, and some workarounds + # actually require input files to be crafted more carefully than I'd like. + # Consequently, until Xcode rules are made more capable, "rules" input + # sections will be handled in Xcode output by shell script build phases + # performed prior to the compilation phase. + # + # The following problems with Xcode rules were found. The numbers are + # Apple radar IDs. I hope that these shortcomings are addressed, I really + # liked having the rules handled directly in Xcode during the period that + # I was prototyping this. + # + # 6588600 Xcode compiles custom script rule outputs too soon, compilation + # fails. This occurs when rule outputs from distinct inputs are + # interdependent. The only workaround is to put rules and their + # inputs in a separate target from the one that compiles the rule + # outputs. This requires input file cooperation and it means that + # process_outputs_as_sources is unusable. + # 6584932 Need to declare that custom rule outputs should be excluded from + # compilation. A possible workaround is to lie to Xcode about a + # rule's output, giving it a dummy file it doesn't know how to + # compile. The rule action script would need to touch the dummy. + # 6584839 I need a way to declare additional inputs to a custom rule. + # A possible workaround is a shell script phase prior to + # compilation that touches a rule's primary input files if any + # would-be additional inputs are newer than the output. Modifying + # the source tree - even just modification times - feels dirty. + # 6564240 Xcode "custom script" build rules always dump all environment + # variables. This is a low-prioroty problem and is not a + # show-stopper. + rules_by_ext = {} + for rule in spec_rules: + rules_by_ext[rule["extension"]] = rule + + # First, some definitions: + # + # A "rule source" is a file that was listed in a target's "sources" + # list and will have a rule applied to it on the basis of matching the + # rule's "extensions" attribute. Rule sources are direct inputs to + # rules. + # + # Rule definitions may specify additional inputs in their "inputs" + # attribute. These additional inputs are used for dependency tracking + # purposes. + # + # A "concrete output" is a rule output with input-dependent variables + # resolved. For example, given a rule with: + # 'extension': 'ext', 'outputs': ['$(INPUT_FILE_BASE).cc'], + # if the target's "sources" list contained "one.ext" and "two.ext", + # the "concrete output" for rule input "two.ext" would be "two.cc". If + # a rule specifies multiple outputs, each input file that the rule is + # applied to will have the same number of concrete outputs. + # + # If any concrete outputs are outdated or missing relative to their + # corresponding rule_source or to any specified additional input, the + # rule action must be performed to generate the concrete outputs. + + # concrete_outputs_by_rule_source will have an item at the same index + # as the rule['rule_sources'] that it corresponds to. Each item is a + # list of all of the concrete outputs for the rule_source. + concrete_outputs_by_rule_source = [] + + # concrete_outputs_all is a flat list of all concrete outputs that this + # rule is able to produce, given the known set of input files + # (rule_sources) that apply to it. + concrete_outputs_all = [] + + # messages & actions are keyed by the same indices as rule['rule_sources'] + # and concrete_outputs_by_rule_source. They contain the message and + # action to perform after resolving input-dependent variables. The + # message is optional, in which case None is stored for each rule source. + messages = [] + actions = [] + + for rule_source in rule.get("rule_sources", []): + rule_source_dirname, rule_source_basename = posixpath.split(rule_source) + (rule_source_root, rule_source_ext) = posixpath.splitext( + rule_source_basename + ) + + # These are the same variable names that Xcode uses for its own native + # rule support. Because Xcode's rule engine is not being used, they + # need to be expanded as they are written to the makefile. + rule_input_dict = { + "INPUT_FILE_BASE": rule_source_root, + "INPUT_FILE_SUFFIX": rule_source_ext, + "INPUT_FILE_NAME": rule_source_basename, + "INPUT_FILE_PATH": rule_source, + "INPUT_FILE_DIRNAME": rule_source_dirname, + } + + concrete_outputs_for_this_rule_source = [] + for output in rule.get("outputs", []): + # Fortunately, Xcode and make both use $(VAR) format for their + # variables, so the expansion is the only transformation necessary. + # Any remaining $(VAR)-type variables in the string can be given + # directly to make, which will pick up the correct settings from + # what Xcode puts into the environment. + concrete_output = ExpandXcodeVariables(output, rule_input_dict) + concrete_outputs_for_this_rule_source.append(concrete_output) + + # Add all concrete outputs to the project. + pbxp.AddOrGetFileInRootGroup(concrete_output) + + concrete_outputs_by_rule_source.append( + concrete_outputs_for_this_rule_source + ) + concrete_outputs_all.extend(concrete_outputs_for_this_rule_source) + + # TODO(mark): Should verify that at most one of these is specified. + if int(rule.get("process_outputs_as_sources", False)): + for output in concrete_outputs_for_this_rule_source: + AddSourceToTarget(output, type, pbxp, xct) + + # If the file came from the mac_bundle_resources list or if the rule + # is marked to process outputs as bundle resource, do so. + was_mac_bundle_resource = rule_source in tgt_mac_bundle_resources + if was_mac_bundle_resource or int( + rule.get("process_outputs_as_mac_bundle_resources", False) + ): + for output in concrete_outputs_for_this_rule_source: + AddResourceToTarget(output, pbxp, xct) + + # Do we have a message to print when this rule runs? + message = rule.get("message") + if message: + message = gyp.common.EncodePOSIXShellArgument(message) + message = ExpandXcodeVariables(message, rule_input_dict) + messages.append(message) + + # Turn the list into a string that can be passed to a shell. + action_string = gyp.common.EncodePOSIXShellList(rule["action"]) + + action = ExpandXcodeVariables(action_string, rule_input_dict) + actions.append(action) + + if len(concrete_outputs_all) > 0: + # TODO(mark): There's a possibility for collision here. Consider + # target "t" rule "A_r" and target "t_A" rule "r". + makefile_name = "%s.make" % re.sub( + "[^a-zA-Z0-9_]", "_", "%s_%s" % (target_name, rule["rule_name"]) + ) + makefile_path = os.path.join( + xcode_projects[build_file].path, makefile_name + ) + # TODO(mark): try/close? Write to a temporary file and swap it only + # if it's got changes? + makefile = open(makefile_path, "w") + + # make will build the first target in the makefile by default. By + # convention, it's called "all". List all (or at least one) + # concrete output for each rule source as a prerequisite of the "all" + # target. + makefile.write("all: \\\n") + for concrete_output_index, concrete_output_by_rule_source in enumerate( + concrete_outputs_by_rule_source + ): + # Only list the first (index [0]) concrete output of each input + # in the "all" target. Otherwise, a parallel make (-j > 1) would + # attempt to process each input multiple times simultaneously. + # Otherwise, "all" could just contain the entire list of + # concrete_outputs_all. + concrete_output = concrete_output_by_rule_source[0] + if ( + concrete_output_index + == len(concrete_outputs_by_rule_source) - 1 + ): + eol = "" + else: + eol = " \\" + makefile.write(" %s%s\n" % (concrete_output, eol)) + + for (rule_source, concrete_outputs, message, action) in zip( + rule["rule_sources"], + concrete_outputs_by_rule_source, + messages, + actions, + ): + makefile.write("\n") + + # Add a rule that declares it can build each concrete output of a + # rule source. Collect the names of the directories that are + # required. + concrete_output_dirs = [] + for concrete_output_index, concrete_output in enumerate( + concrete_outputs + ): + if concrete_output_index == 0: + bol = "" + else: + bol = " " + makefile.write("%s%s \\\n" % (bol, concrete_output)) + + concrete_output_dir = posixpath.dirname(concrete_output) + if ( + concrete_output_dir + and concrete_output_dir not in concrete_output_dirs + ): + concrete_output_dirs.append(concrete_output_dir) + + makefile.write(" : \\\n") + + # The prerequisites for this rule are the rule source itself and + # the set of additional rule inputs, if any. + prerequisites = [rule_source] + prerequisites.extend(rule.get("inputs", [])) + for prerequisite_index, prerequisite in enumerate(prerequisites): + if prerequisite_index == len(prerequisites) - 1: + eol = "" + else: + eol = " \\" + makefile.write(" %s%s\n" % (prerequisite, eol)) + + # Make sure that output directories exist before executing the rule + # action. + if len(concrete_output_dirs) > 0: + makefile.write( + '\t@mkdir -p "%s"\n' % '" "'.join(concrete_output_dirs) + ) + + # The rule message and action have already had + # the necessary variable substitutions performed. + if message: + # Mark it with note: so Xcode picks it up in build output. + makefile.write("\t@echo note: %s\n" % message) + makefile.write("\t%s\n" % action) + + makefile.close() + + # It might be nice to ensure that needed output directories exist + # here rather than in each target in the Makefile, but that wouldn't + # work if there ever was a concrete output that had an input-dependent + # variable anywhere other than in the leaf position. + + # Don't declare any inputPaths or outputPaths. If they're present, + # Xcode will provide a slight optimization by only running the script + # phase if any output is missing or outdated relative to any input. + # Unfortunately, it will also assume that all outputs are touched by + # the script, and if the outputs serve as files in a compilation + # phase, they will be unconditionally rebuilt. Since make might not + # rebuild everything that could be declared here as an output, this + # extra compilation activity is unnecessary. With inputPaths and + # outputPaths not supplied, make will always be called, but it knows + # enough to not do anything when everything is up-to-date. + + # To help speed things up, pass -j COUNT to make so it does some work + # in parallel. Don't use ncpus because Xcode will build ncpus targets + # in parallel and if each target happens to have a rules step, there + # would be ncpus^2 things going. With a machine that has 2 quad-core + # Xeons, a build can quickly run out of processes based on + # scheduling/other tasks, and randomly failing builds are no good. + script = ( + """JOB_COUNT="$(/usr/sbin/sysctl -n hw.ncpu)" if [ "${JOB_COUNT}" -gt 4 ]; then JOB_COUNT=4 fi exec xcrun make -f "${PROJECT_FILE_PATH}/%s" -j "${JOB_COUNT}" exit 1 -""" % makefile_name - ssbp = gyp.xcodeproj_file.PBXShellScriptBuildPhase({ - 'name': 'Rule "' + rule['rule_name'] + '"', - 'shellScript': script, - 'showEnvVarsInLog': 0, - }) - - if support_xct: - support_xct.AppendProperty('buildPhases', ssbp) - else: - # TODO(mark): this assumes too much knowledge of the internals of - # xcodeproj_file; some of these smarts should move into xcodeproj_file - # itself. - xct._properties['buildPhases'].insert(prebuild_index, ssbp) - prebuild_index = prebuild_index + 1 - - # Extra rule inputs also go into the project file. Concrete outputs were - # already added when they were computed. - groups = ['inputs', 'inputs_excluded'] - if skip_excluded_files: - groups = [x for x in groups if not x.endswith('_excluded')] - for group in groups: - for item in rule.get(group, []): - pbxp.AddOrGetFileInRootGroup(item) - - # Add "sources". - for source in spec.get('sources', []): - (source_root, source_extension) = posixpath.splitext(source) - if source_extension[1:] not in rules_by_ext: - # AddSourceToTarget will add the file to a root group if it's not - # already there. - AddSourceToTarget(source, type, pbxp, xct) - else: - pbxp.AddOrGetFileInRootGroup(source) - - # Add "mac_bundle_resources" and "mac_framework_private_headers" if - # it's a bundle of any type. - if is_bundle: - for resource in tgt_mac_bundle_resources: - (resource_root, resource_extension) = posixpath.splitext(resource) - if resource_extension[1:] not in rules_by_ext: - AddResourceToTarget(resource, pbxp, xct) - else: - pbxp.AddOrGetFileInRootGroup(resource) - - for header in spec.get('mac_framework_private_headers', []): - AddHeaderToTarget(header, pbxp, xct, False) - - # Add "mac_framework_headers". These can be valid for both frameworks - # and static libraries. - if is_bundle or type == 'static_library': - for header in spec.get('mac_framework_headers', []): - AddHeaderToTarget(header, pbxp, xct, True) - - # Add "copies". - pbxcp_dict = {} - for copy_group in spec.get('copies', []): - dest = copy_group['destination'] - if dest[0] not in ('/', '$'): - # Relative paths are relative to $(SRCROOT). - dest = '$(SRCROOT)/' + dest - - code_sign = int(copy_group.get('xcode_code_sign', 0)) - settings = (None, '{ATTRIBUTES = (CodeSignOnCopy, ); }')[code_sign] - - # Coalesce multiple "copies" sections in the same target with the same - # "destination" property into the same PBXCopyFilesBuildPhase, otherwise - # they'll wind up with ID collisions. - pbxcp = pbxcp_dict.get(dest, None) - if pbxcp is None: - pbxcp = gyp.xcodeproj_file.PBXCopyFilesBuildPhase({ - 'name': 'Copy to ' + copy_group['destination'] - }, - parent=xct) - pbxcp.SetDestination(dest) - - # TODO(mark): The usual comment about this knowing too much about - # gyp.xcodeproj_file internals applies. - xct._properties['buildPhases'].insert(prebuild_index, pbxcp) - - pbxcp_dict[dest] = pbxcp - - for file in copy_group['files']: - pbxcp.AddFile(file, settings) - - # Excluded files can also go into the project file. - if not skip_excluded_files: - for key in ['sources', 'mac_bundle_resources', 'mac_framework_headers', - 'mac_framework_private_headers']: - excluded_key = key + '_excluded' - for item in spec.get(excluded_key, []): - pbxp.AddOrGetFileInRootGroup(item) - - # So can "inputs" and "outputs" sections of "actions" groups. - groups = ['inputs', 'inputs_excluded', 'outputs', 'outputs_excluded'] - if skip_excluded_files: - groups = [x for x in groups if not x.endswith('_excluded')] - for action in spec.get('actions', []): - for group in groups: - for item in action.get(group, []): - # Exclude anything in BUILT_PRODUCTS_DIR. They're products, not - # sources. - if not item.startswith('$(BUILT_PRODUCTS_DIR)/'): - pbxp.AddOrGetFileInRootGroup(item) - - for postbuild in spec.get('postbuilds', []): - action_string_sh = gyp.common.EncodePOSIXShellList(postbuild['action']) - script = 'exec ' + action_string_sh + '\nexit 1\n' - - # Make the postbuild step depend on the output of ld or ar from this - # target. Apparently putting the script step after the link step isn't - # sufficient to ensure proper ordering in all cases. With an input - # declared but no outputs, the script step should run every time, as - # desired. - ssbp = gyp.xcodeproj_file.PBXShellScriptBuildPhase({ - 'inputPaths': ['$(BUILT_PRODUCTS_DIR)/$(EXECUTABLE_PATH)'], - 'name': 'Postbuild "' + postbuild['postbuild_name'] + '"', - 'shellScript': script, - 'showEnvVarsInLog': 0, - }) - xct.AppendProperty('buildPhases', ssbp) - - # Add dependencies before libraries, because adding a dependency may imply - # adding a library. It's preferable to keep dependencies listed first - # during a link phase so that they can override symbols that would - # otherwise be provided by libraries, which will usually include system - # libraries. On some systems, ld is finicky and even requires the - # libraries to be ordered in such a way that unresolved symbols in - # earlier-listed libraries may only be resolved by later-listed libraries. - # The Mac linker doesn't work that way, but other platforms do, and so - # their linker invocations need to be constructed in this way. There's - # no compelling reason for Xcode's linker invocations to differ. - - if 'dependencies' in spec: - for dependency in spec['dependencies']: - xct.AddDependency(xcode_targets[dependency]) - # The support project also gets the dependencies (in case they are - # needed for the actions/rules to work). - if support_xct: - support_xct.AddDependency(xcode_targets[dependency]) - - if 'libraries' in spec: - for library in spec['libraries']: - xct.FrameworksPhase().AddFile(library) - # Add the library's directory to LIBRARY_SEARCH_PATHS if necessary. - # I wish Xcode handled this automatically. - library_dir = posixpath.dirname(library) - if library_dir not in xcode_standard_library_dirs and ( - not xct.HasBuildSetting(_library_search_paths_var) or - library_dir not in xct.GetBuildSetting(_library_search_paths_var)): - xct.AppendBuildSetting(_library_search_paths_var, library_dir) - - for configuration_name in configuration_names: - configuration = spec['configurations'][configuration_name] - xcbc = xct.ConfigurationNamed(configuration_name) - for include_dir in configuration.get('mac_framework_dirs', []): - xcbc.AppendBuildSetting('FRAMEWORK_SEARCH_PATHS', include_dir) - for include_dir in configuration.get('include_dirs', []): - xcbc.AppendBuildSetting('HEADER_SEARCH_PATHS', include_dir) - for library_dir in configuration.get('library_dirs', []): - if library_dir not in xcode_standard_library_dirs and ( - not xcbc.HasBuildSetting(_library_search_paths_var) or - library_dir not in xcbc.GetBuildSetting(_library_search_paths_var)): - xcbc.AppendBuildSetting(_library_search_paths_var, library_dir) - - if 'defines' in configuration: - for define in configuration['defines']: - set_define = EscapeXcodeDefine(define) - xcbc.AppendBuildSetting('GCC_PREPROCESSOR_DEFINITIONS', set_define) - if 'xcode_settings' in configuration: - for xck, xcv in configuration['xcode_settings'].items(): - xcbc.SetBuildSetting(xck, xcv) - if 'xcode_config_file' in configuration: - config_ref = pbxp.AddOrGetFileInRootGroup( - configuration['xcode_config_file']) - xcbc.SetBaseConfiguration(config_ref) - - build_files = [] - for build_file, build_file_dict in data.items(): - if build_file.endswith('.gyp'): - build_files.append(build_file) - - for build_file in build_files: - xcode_projects[build_file].Finalize1(xcode_targets, serialize_all_tests) - - for build_file in build_files: - xcode_projects[build_file].Finalize2(xcode_targets, - xcode_target_to_target_dict) - - for build_file in build_files: - xcode_projects[build_file].Write() +""" + % makefile_name + ) + ssbp = gyp.xcodeproj_file.PBXShellScriptBuildPhase( + { + "name": 'Rule "' + rule["rule_name"] + '"', + "shellScript": script, + "showEnvVarsInLog": 0, + } + ) + + if support_xct: + support_xct.AppendProperty("buildPhases", ssbp) + else: + # TODO(mark): this assumes too much knowledge of the internals of + # xcodeproj_file; some of these smarts should move + # into xcodeproj_file itself. + xct._properties["buildPhases"].insert(prebuild_index, ssbp) + prebuild_index = prebuild_index + 1 + + # Extra rule inputs also go into the project file. Concrete outputs were + # already added when they were computed. + groups = ["inputs", "inputs_excluded"] + if skip_excluded_files: + groups = [x for x in groups if not x.endswith("_excluded")] + for group in groups: + for item in rule.get(group, []): + pbxp.AddOrGetFileInRootGroup(item) + + # Add "sources". + for source in spec.get("sources", []): + (source_root, source_extension) = posixpath.splitext(source) + if source_extension[1:] not in rules_by_ext: + # AddSourceToTarget will add the file to a root group if it's not + # already there. + AddSourceToTarget(source, type, pbxp, xct) + else: + pbxp.AddOrGetFileInRootGroup(source) + + # Add "mac_bundle_resources" and "mac_framework_private_headers" if + # it's a bundle of any type. + if is_bundle: + for resource in tgt_mac_bundle_resources: + (resource_root, resource_extension) = posixpath.splitext(resource) + if resource_extension[1:] not in rules_by_ext: + AddResourceToTarget(resource, pbxp, xct) + else: + pbxp.AddOrGetFileInRootGroup(resource) + + for header in spec.get("mac_framework_private_headers", []): + AddHeaderToTarget(header, pbxp, xct, False) + + # Add "mac_framework_headers". These can be valid for both frameworks + # and static libraries. + if is_bundle or type == "static_library": + for header in spec.get("mac_framework_headers", []): + AddHeaderToTarget(header, pbxp, xct, True) + + # Add "copies". + pbxcp_dict = {} + for copy_group in spec.get("copies", []): + dest = copy_group["destination"] + if dest[0] not in ("/", "$"): + # Relative paths are relative to $(SRCROOT). + dest = "$(SRCROOT)/" + dest + + code_sign = int(copy_group.get("xcode_code_sign", 0)) + settings = (None, "{ATTRIBUTES = (CodeSignOnCopy, ); }")[code_sign] + + # Coalesce multiple "copies" sections in the same target with the same + # "destination" property into the same PBXCopyFilesBuildPhase, otherwise + # they'll wind up with ID collisions. + pbxcp = pbxcp_dict.get(dest, None) + if pbxcp is None: + pbxcp = gyp.xcodeproj_file.PBXCopyFilesBuildPhase( + {"name": "Copy to " + copy_group["destination"]}, parent=xct + ) + pbxcp.SetDestination(dest) + + # TODO(mark): The usual comment about this knowing too much about + # gyp.xcodeproj_file internals applies. + xct._properties["buildPhases"].insert(prebuild_index, pbxcp) + + pbxcp_dict[dest] = pbxcp + + for file in copy_group["files"]: + pbxcp.AddFile(file, settings) + + # Excluded files can also go into the project file. + if not skip_excluded_files: + for key in [ + "sources", + "mac_bundle_resources", + "mac_framework_headers", + "mac_framework_private_headers", + ]: + excluded_key = key + "_excluded" + for item in spec.get(excluded_key, []): + pbxp.AddOrGetFileInRootGroup(item) + + # So can "inputs" and "outputs" sections of "actions" groups. + groups = ["inputs", "inputs_excluded", "outputs", "outputs_excluded"] + if skip_excluded_files: + groups = [x for x in groups if not x.endswith("_excluded")] + for action in spec.get("actions", []): + for group in groups: + for item in action.get(group, []): + # Exclude anything in BUILT_PRODUCTS_DIR. They're products, not + # sources. + if not item.startswith("$(BUILT_PRODUCTS_DIR)/"): + pbxp.AddOrGetFileInRootGroup(item) + + for postbuild in spec.get("postbuilds", []): + action_string_sh = gyp.common.EncodePOSIXShellList(postbuild["action"]) + script = "exec " + action_string_sh + "\nexit 1\n" + + # Make the postbuild step depend on the output of ld or ar from this + # target. Apparently putting the script step after the link step isn't + # sufficient to ensure proper ordering in all cases. With an input + # declared but no outputs, the script step should run every time, as + # desired. + ssbp = gyp.xcodeproj_file.PBXShellScriptBuildPhase( + { + "inputPaths": ["$(BUILT_PRODUCTS_DIR)/$(EXECUTABLE_PATH)"], + "name": 'Postbuild "' + postbuild["postbuild_name"] + '"', + "shellScript": script, + "showEnvVarsInLog": 0, + } + ) + xct.AppendProperty("buildPhases", ssbp) + + # Add dependencies before libraries, because adding a dependency may imply + # adding a library. It's preferable to keep dependencies listed first + # during a link phase so that they can override symbols that would + # otherwise be provided by libraries, which will usually include system + # libraries. On some systems, ld is finicky and even requires the + # libraries to be ordered in such a way that unresolved symbols in + # earlier-listed libraries may only be resolved by later-listed libraries. + # The Mac linker doesn't work that way, but other platforms do, and so + # their linker invocations need to be constructed in this way. There's + # no compelling reason for Xcode's linker invocations to differ. + + if "dependencies" in spec: + for dependency in spec["dependencies"]: + xct.AddDependency(xcode_targets[dependency]) + # The support project also gets the dependencies (in case they are + # needed for the actions/rules to work). + if support_xct: + support_xct.AddDependency(xcode_targets[dependency]) + + if "libraries" in spec: + for library in spec["libraries"]: + xct.FrameworksPhase().AddFile(library) + # Add the library's directory to LIBRARY_SEARCH_PATHS if necessary. + # I wish Xcode handled this automatically. + library_dir = posixpath.dirname(library) + if library_dir not in xcode_standard_library_dirs and ( + not xct.HasBuildSetting(_library_search_paths_var) + or library_dir not in xct.GetBuildSetting(_library_search_paths_var) + ): + xct.AppendBuildSetting(_library_search_paths_var, library_dir) + + for configuration_name in configuration_names: + configuration = spec["configurations"][configuration_name] + xcbc = xct.ConfigurationNamed(configuration_name) + for include_dir in configuration.get("mac_framework_dirs", []): + xcbc.AppendBuildSetting("FRAMEWORK_SEARCH_PATHS", include_dir) + for include_dir in configuration.get("include_dirs", []): + xcbc.AppendBuildSetting("HEADER_SEARCH_PATHS", include_dir) + for library_dir in configuration.get("library_dirs", []): + if library_dir not in xcode_standard_library_dirs and ( + not xcbc.HasBuildSetting(_library_search_paths_var) + or library_dir + not in xcbc.GetBuildSetting(_library_search_paths_var) + ): + xcbc.AppendBuildSetting(_library_search_paths_var, library_dir) + + if "defines" in configuration: + for define in configuration["defines"]: + set_define = EscapeXcodeDefine(define) + xcbc.AppendBuildSetting("GCC_PREPROCESSOR_DEFINITIONS", set_define) + if "xcode_settings" in configuration: + for xck, xcv in configuration["xcode_settings"].items(): + xcbc.SetBuildSetting(xck, xcv) + if "xcode_config_file" in configuration: + config_ref = pbxp.AddOrGetFileInRootGroup( + configuration["xcode_config_file"] + ) + xcbc.SetBaseConfiguration(config_ref) + + build_files = [] + for build_file, build_file_dict in data.items(): + if build_file.endswith(".gyp"): + build_files.append(build_file) + + for build_file in build_files: + xcode_projects[build_file].Finalize1(xcode_targets, serialize_all_tests) + + for build_file in build_files: + xcode_projects[build_file].Finalize2(xcode_targets, xcode_target_to_target_dict) + + for build_file in build_files: + xcode_projects[build_file].Write() diff --git a/tools/gyp/pylib/gyp/generator/xcode_test.py b/tools/gyp/pylib/gyp/generator/xcode_test.py index 260324a43f3e23..51fbca6a2a28f0 100644 --- a/tools/gyp/pylib/gyp/generator/xcode_test.py +++ b/tools/gyp/pylib/gyp/generator/xcode_test.py @@ -12,12 +12,14 @@ class TestEscapeXcodeDefine(unittest.TestCase): - if sys.platform == 'darwin': - def test_InheritedRemainsUnescaped(self): - self.assertEqual(xcode.EscapeXcodeDefine('$(inherited)'), '$(inherited)') + if sys.platform == "darwin": - def test_Escaping(self): - self.assertEqual(xcode.EscapeXcodeDefine('a b"c\\'), 'a\\ b\\"c\\\\') + def test_InheritedRemainsUnescaped(self): + self.assertEqual(xcode.EscapeXcodeDefine("$(inherited)"), "$(inherited)") -if __name__ == '__main__': - unittest.main() + def test_Escaping(self): + self.assertEqual(xcode.EscapeXcodeDefine('a b"c\\'), 'a\\ b\\"c\\\\') + + +if __name__ == "__main__": + unittest.main() diff --git a/tools/gyp/pylib/gyp/input.py b/tools/gyp/pylib/gyp/input.py index 1f40abb06951bb..5504390c0bb33f 100644 --- a/tools/gyp/pylib/gyp/input.py +++ b/tools/gyp/pylib/gyp/input.py @@ -9,7 +9,6 @@ import gyp.common import gyp.simple_copy import multiprocessing -import optparse import os.path import re import shlex @@ -17,7 +16,6 @@ import subprocess import sys import threading -import time import traceback from distutils.version import StrictVersion from gyp.common import GypError @@ -27,28 +25,28 @@ # A list of types that are treated as linkable. linkable_types = [ - 'executable', - 'shared_library', - 'loadable_module', - 'mac_kernel_extension', - 'windows_driver', + "executable", + "shared_library", + "loadable_module", + "mac_kernel_extension", + "windows_driver", ] # A list of sections that contain links to other targets. -dependency_sections = ['dependencies', 'export_dependent_settings'] +dependency_sections = ["dependencies", "export_dependent_settings"] # base_path_sections is a list of sections defined by GYP that contain # pathnames. The generators can provide more keys, the two lists are merged # into path_sections, but you should call IsPathSection instead of using either # list directly. base_path_sections = [ - 'destination', - 'files', - 'include_dirs', - 'inputs', - 'libraries', - 'outputs', - 'sources', + "destination", + "files", + "include_dirs", + "inputs", + "libraries", + "outputs", + "sources", ] path_sections = set() @@ -57,77 +55,78 @@ per_process_data = {} per_process_aux_data = {} + def IsPathSection(section): - # If section ends in one of the '=+?!' characters, it's applied to a section - # without the trailing characters. '/' is notably absent from this list, - # because there's no way for a regular expression to be treated as a path. - while section and section[-1:] in '=+?!': - section = section[:-1] - - if section in path_sections: - return True - - # Sections mathing the regexp '_(dir|file|path)s?$' are also - # considered PathSections. Using manual string matching since that - # is much faster than the regexp and this can be called hundreds of - # thousands of times so micro performance matters. - if "_" in section: - tail = section[-6:] - if tail[-1] == 's': - tail = tail[:-1] - if tail[-5:] in ('_file', '_path'): - return True - return tail[-4:] == '_dir' - - return False + # If section ends in one of the '=+?!' characters, it's applied to a section + # without the trailing characters. '/' is notably absent from this list, + # because there's no way for a regular expression to be treated as a path. + while section and section[-1:] in "=+?!": + section = section[:-1] + + if section in path_sections: + return True + + # Sections matching the regexp '_(dir|file|path)s?$' are also + # considered PathSections. Using manual string matching since that + # is much faster than the regexp and this can be called hundreds of + # thousands of times so micro performance matters. + if "_" in section: + tail = section[-6:] + if tail[-1] == "s": + tail = tail[:-1] + if tail[-5:] in ("_file", "_path"): + return True + return tail[-4:] == "_dir" + + return False + # base_non_configuration_keys is a list of key names that belong in the target # itself and should not be propagated into its configurations. It is merged # with a list that can come from the generator to # create non_configuration_keys. base_non_configuration_keys = [ - # Sections that must exist inside targets and not configurations. - 'actions', - 'configurations', - 'copies', - 'default_configuration', - 'dependencies', - 'dependencies_original', - 'libraries', - 'postbuilds', - 'product_dir', - 'product_extension', - 'product_name', - 'product_prefix', - 'rules', - 'run_as', - 'sources', - 'standalone_static_library', - 'suppress_wildcard', - 'target_name', - 'toolset', - 'toolsets', - 'type', - - # Sections that can be found inside targets or configurations, but that - # should not be propagated from targets into their configurations. - 'variables', + # Sections that must exist inside targets and not configurations. + "actions", + "configurations", + "copies", + "default_configuration", + "dependencies", + "dependencies_original", + "libraries", + "postbuilds", + "product_dir", + "product_extension", + "product_name", + "product_prefix", + "rules", + "run_as", + "sources", + "standalone_static_library", + "suppress_wildcard", + "target_name", + "toolset", + "toolsets", + "type", + # Sections that can be found inside targets or configurations, but that + # should not be propagated from targets into their configurations. + "variables", ] non_configuration_keys = [] # Keys that do not belong inside a configuration dictionary. invalid_configuration_keys = [ - 'actions', - 'all_dependent_settings', - 'configurations', - 'dependencies', - 'direct_dependent_settings', - 'libraries', - 'link_settings', - 'sources', - 'standalone_static_library', - 'target_name', - 'type', + "actions", + "all_dependent_settings", + "configurations", + "dependencies", + "direct_dependent_settings", + "libraries", + "link_settings", + "sources", + "standalone_static_library", + "target_name", + "type", ] # Controls whether or not the generator supports multiple toolsets. @@ -139,8 +138,9 @@ def IsPathSection(section): # } generator_filelist_paths = None + def GetIncludedBuildFiles(build_file_path, aux_data, included=None): - """Return a list of all build files included into build_file_path. + """Return a list of all build files included into build_file_path. The returned list will contain build_file_path as well as all other files that it included, either directly or indirectly. Note that the list may @@ -158,535 +158,595 @@ def GetIncludedBuildFiles(build_file_path, aux_data, included=None): in the list will be relative to the current directory. """ - if included is None: - included = [] + if included is None: + included = [] - if build_file_path in included: - return included + if build_file_path in included: + return included - included.append(build_file_path) + included.append(build_file_path) - for included_build_file in aux_data[build_file_path].get('included', []): - GetIncludedBuildFiles(included_build_file, aux_data, included) + for included_build_file in aux_data[build_file_path].get("included", []): + GetIncludedBuildFiles(included_build_file, aux_data, included) - return included + return included def CheckedEval(file_contents): - """Return the eval of a gyp file. + """Return the eval of a gyp file. The gyp file is restricted to dictionaries and lists only, and repeated keys are not allowed. Note that this is slower than eval() is. """ - syntax_tree = ast.parse(file_contents) - assert isinstance(syntax_tree, ast.Module) - c1 = syntax_tree.body - assert len(c1) == 1 - c2 = c1[0] - assert isinstance(c2, ast.Expr) - return CheckNode(c2.value, []) + syntax_tree = ast.parse(file_contents) + assert isinstance(syntax_tree, ast.Module) + c1 = syntax_tree.body + assert len(c1) == 1 + c2 = c1[0] + assert isinstance(c2, ast.Expr) + return CheckNode(c2.value, []) def CheckNode(node, keypath): - if isinstance(node, ast.Dict): - c = node.getChildren() - dict = {} - for key, value in zip(node.keys, node.values): - assert isinstance(key, ast.Str) - key = key.s - if key in dict: - raise GypError("Key '" + key + "' repeated at level " + - repr(len(keypath) + 1) + " with key path '" + - '.'.join(keypath) + "'") - kp = list(keypath) # Make a copy of the list for descending this node. - kp.append(key) - dict[key] = CheckNode(value, kp) - return dict - elif isinstance(node, ast.List): - children = [] - for index, child in enumerate(node.elts): - kp = list(keypath) # Copy list. - kp.append(repr(index)) - children.append(CheckNode(child, kp)) - return children - elif isinstance(node, ast.Str): - return node.s - else: - raise TypeError("Unknown AST node at key path '" + '.'.join(keypath) + - "': " + repr(node)) - - -def LoadOneBuildFile(build_file_path, data, aux_data, includes, - is_target, check): - if build_file_path in data: - return data[build_file_path] - - if os.path.exists(build_file_path): - # Open the build file for read ('r') with universal-newlines mode ('U') - # to make sure platform specific newlines ('\r\n' or '\r') are converted to '\n' - # which otherwise will fail eval() - if sys.platform == 'zos': - # On z/OS, universal-newlines mode treats the file as an ascii file. But since - # node-gyp produces ebcdic files, do not use that mode. - build_file_contents = open(build_file_path, 'r').read() + if isinstance(node, ast.Dict): + dict = {} + for key, value in zip(node.keys, node.values): + assert isinstance(key, ast.Str) + key = key.s + if key in dict: + raise GypError( + "Key '" + + key + + "' repeated at level " + + repr(len(keypath) + 1) + + " with key path '" + + ".".join(keypath) + + "'" + ) + kp = list(keypath) # Make a copy of the list for descending this node. + kp.append(key) + dict[key] = CheckNode(value, kp) + return dict + elif isinstance(node, ast.List): + children = [] + for index, child in enumerate(node.elts): + kp = list(keypath) # Copy list. + kp.append(repr(index)) + children.append(CheckNode(child, kp)) + return children + elif isinstance(node, ast.Str): + return node.s else: - build_file_contents = open(build_file_path, 'rU').read() - else: - raise GypError("%s not found (cwd: %s)" % (build_file_path, os.getcwd())) - - build_file_data = None - try: - if check: - build_file_data = CheckedEval(build_file_contents) + raise TypeError( + "Unknown AST node at key path '" + ".".join(keypath) + "': " + repr(node) + ) + + +def LoadOneBuildFile(build_file_path, data, aux_data, includes, is_target, check): + if build_file_path in data: + return data[build_file_path] + + if os.path.exists(build_file_path): + # Open the build file for read ('r') with universal-newlines mode ('U') + # to make sure platform specific newlines ('\r\n' or '\r') are converted to '\n' + # which otherwise will fail eval() + if sys.platform == "zos": + # On z/OS, universal-newlines mode treats the file as an ascii file. + # But since node-gyp produces ebcdic files, do not use that mode. + build_file_contents = open(build_file_path, "r").read() + else: + build_file_contents = open(build_file_path, "rU").read() else: - build_file_data = eval(build_file_contents, {'__builtins__': {}}, - None) - except SyntaxError as e: - e.filename = build_file_path - raise - except Exception as e: - gyp.common.ExceptionAppend(e, 'while reading ' + build_file_path) - raise - - if type(build_file_data) is not dict: - raise GypError("%s does not evaluate to a dictionary." % build_file_path) - - data[build_file_path] = build_file_data - aux_data[build_file_path] = {} - - # Scan for includes and merge them in. - if ('skip_includes' not in build_file_data or - not build_file_data['skip_includes']): + raise GypError("%s not found (cwd: %s)" % (build_file_path, os.getcwd())) + + build_file_data = None try: - if is_target: - LoadBuildFileIncludesIntoDict(build_file_data, build_file_path, data, - aux_data, includes, check) - else: - LoadBuildFileIncludesIntoDict(build_file_data, build_file_path, data, - aux_data, None, check) + if check: + build_file_data = CheckedEval(build_file_contents) + else: + build_file_data = eval(build_file_contents, {"__builtins__": {}}, None) + except SyntaxError as e: + e.filename = build_file_path + raise except Exception as e: - gyp.common.ExceptionAppend(e, - 'while reading includes of ' + build_file_path) - raise - - return build_file_data - - -def LoadBuildFileIncludesIntoDict(subdict, subdict_path, data, aux_data, - includes, check): - includes_list = [] - if includes != None: - includes_list.extend(includes) - if 'includes' in subdict: - for include in subdict['includes']: - # "include" is specified relative to subdict_path, so compute the real - # path to include by appending the provided "include" to the directory - # in which subdict_path resides. - relative_include = \ - os.path.normpath(os.path.join(os.path.dirname(subdict_path), include)) - includes_list.append(relative_include) - # Unhook the includes list, it's no longer needed. - del subdict['includes'] - - # Merge in the included files. - for include in includes_list: - if not 'included' in aux_data[subdict_path]: - aux_data[subdict_path]['included'] = [] - aux_data[subdict_path]['included'].append(include) - - gyp.DebugOutput(gyp.DEBUG_INCLUDES, "Loading Included File: '%s'", include) - - MergeDicts(subdict, - LoadOneBuildFile(include, data, aux_data, None, False, check), - subdict_path, include) - - # Recurse into subdictionaries. - for k, v in subdict.items(): - if type(v) is dict: - LoadBuildFileIncludesIntoDict(v, subdict_path, data, aux_data, - None, check) - elif type(v) is list: - LoadBuildFileIncludesIntoList(v, subdict_path, data, aux_data, - check) + gyp.common.ExceptionAppend(e, "while reading " + build_file_path) + raise + + if type(build_file_data) is not dict: + raise GypError("%s does not evaluate to a dictionary." % build_file_path) + + data[build_file_path] = build_file_data + aux_data[build_file_path] = {} + + # Scan for includes and merge them in. + if "skip_includes" not in build_file_data or not build_file_data["skip_includes"]: + try: + if is_target: + LoadBuildFileIncludesIntoDict( + build_file_data, build_file_path, data, aux_data, includes, check + ) + else: + LoadBuildFileIncludesIntoDict( + build_file_data, build_file_path, data, aux_data, None, check + ) + except Exception as e: + gyp.common.ExceptionAppend( + e, "while reading includes of " + build_file_path + ) + raise + + return build_file_data + + +def LoadBuildFileIncludesIntoDict( + subdict, subdict_path, data, aux_data, includes, check +): + includes_list = [] + if includes is not None: + includes_list.extend(includes) + if "includes" in subdict: + for include in subdict["includes"]: + # "include" is specified relative to subdict_path, so compute the real + # path to include by appending the provided "include" to the directory + # in which subdict_path resides. + relative_include = os.path.normpath( + os.path.join(os.path.dirname(subdict_path), include) + ) + includes_list.append(relative_include) + # Unhook the includes list, it's no longer needed. + del subdict["includes"] + + # Merge in the included files. + for include in includes_list: + if "included" not in aux_data[subdict_path]: + aux_data[subdict_path]["included"] = [] + aux_data[subdict_path]["included"].append(include) + + gyp.DebugOutput(gyp.DEBUG_INCLUDES, "Loading Included File: '%s'", include) + + MergeDicts( + subdict, + LoadOneBuildFile(include, data, aux_data, None, False, check), + subdict_path, + include, + ) + + # Recurse into subdictionaries. + for k, v in subdict.items(): + if type(v) is dict: + LoadBuildFileIncludesIntoDict(v, subdict_path, data, aux_data, None, check) + elif type(v) is list: + LoadBuildFileIncludesIntoList(v, subdict_path, data, aux_data, check) # This recurses into lists so that it can look for dicts. def LoadBuildFileIncludesIntoList(sublist, sublist_path, data, aux_data, check): - for item in sublist: - if type(item) is dict: - LoadBuildFileIncludesIntoDict(item, sublist_path, data, aux_data, - None, check) - elif type(item) is list: - LoadBuildFileIncludesIntoList(item, sublist_path, data, aux_data, check) + for item in sublist: + if type(item) is dict: + LoadBuildFileIncludesIntoDict( + item, sublist_path, data, aux_data, None, check + ) + elif type(item) is list: + LoadBuildFileIncludesIntoList(item, sublist_path, data, aux_data, check) + # Processes toolsets in all the targets. This recurses into condition entries # since they can contain toolsets as well. def ProcessToolsetsInDict(data): - if 'targets' in data: - target_list = data['targets'] - new_target_list = [] - for target in target_list: - # If this target already has an explicit 'toolset', and no 'toolsets' - # list, don't modify it further. - if 'toolset' in target and 'toolsets' not in target: - new_target_list.append(target) - continue - if multiple_toolsets: - toolsets = target.get('toolsets', ['target']) - else: - toolsets = ['target'] - # Make sure this 'toolsets' definition is only processed once. - if 'toolsets' in target: - del target['toolsets'] - if len(toolsets) > 0: - # Optimization: only do copies if more than one toolset is specified. - for build in toolsets[1:]: - new_target = gyp.simple_copy.deepcopy(target) - new_target['toolset'] = build - new_target_list.append(new_target) - target['toolset'] = toolsets[0] - new_target_list.append(target) - data['targets'] = new_target_list - if 'conditions' in data: - for condition in data['conditions']: - if type(condition) is list: - for condition_dict in condition[1:]: - if type(condition_dict) is dict: - ProcessToolsetsInDict(condition_dict) + if "targets" in data: + target_list = data["targets"] + new_target_list = [] + for target in target_list: + # If this target already has an explicit 'toolset', and no 'toolsets' + # list, don't modify it further. + if "toolset" in target and "toolsets" not in target: + new_target_list.append(target) + continue + if multiple_toolsets: + toolsets = target.get("toolsets", ["target"]) + else: + toolsets = ["target"] + # Make sure this 'toolsets' definition is only processed once. + if "toolsets" in target: + del target["toolsets"] + if len(toolsets) > 0: + # Optimization: only do copies if more than one toolset is specified. + for build in toolsets[1:]: + new_target = gyp.simple_copy.deepcopy(target) + new_target["toolset"] = build + new_target_list.append(new_target) + target["toolset"] = toolsets[0] + new_target_list.append(target) + data["targets"] = new_target_list + if "conditions" in data: + for condition in data["conditions"]: + if type(condition) is list: + for condition_dict in condition[1:]: + if type(condition_dict) is dict: + ProcessToolsetsInDict(condition_dict) # TODO(mark): I don't love this name. It just means that it's going to load # a build file that contains targets and is expected to provide a targets dict # that contains the targets... -def LoadTargetBuildFile(build_file_path, data, aux_data, variables, includes, - depth, check, load_dependencies): - # If depth is set, predefine the DEPTH variable to be a relative path from - # this build file's directory to the directory identified by depth. - if depth: - # TODO(dglazkov) The backslash/forward-slash replacement at the end is a - # temporary measure. This should really be addressed by keeping all paths - # in POSIX until actual project generation. - d = gyp.common.RelativePath(depth, os.path.dirname(build_file_path)) - if d == '': - variables['DEPTH'] = '.' +def LoadTargetBuildFile( + build_file_path, + data, + aux_data, + variables, + includes, + depth, + check, + load_dependencies, +): + # If depth is set, predefine the DEPTH variable to be a relative path from + # this build file's directory to the directory identified by depth. + if depth: + # TODO(dglazkov) The backslash/forward-slash replacement at the end is a + # temporary measure. This should really be addressed by keeping all paths + # in POSIX until actual project generation. + d = gyp.common.RelativePath(depth, os.path.dirname(build_file_path)) + if d == "": + variables["DEPTH"] = "." + else: + variables["DEPTH"] = d.replace("\\", "/") + + # The 'target_build_files' key is only set when loading target build files in + # the non-parallel code path, where LoadTargetBuildFile is called + # recursively. In the parallel code path, we don't need to check whether the + # |build_file_path| has already been loaded, because the 'scheduled' set in + # ParallelState guarantees that we never load the same |build_file_path| + # twice. + if "target_build_files" in data: + if build_file_path in data["target_build_files"]: + # Already loaded. + return False + data["target_build_files"].add(build_file_path) + + gyp.DebugOutput( + gyp.DEBUG_INCLUDES, "Loading Target Build File '%s'", build_file_path + ) + + build_file_data = LoadOneBuildFile( + build_file_path, data, aux_data, includes, True, check + ) + + # Store DEPTH for later use in generators. + build_file_data["_DEPTH"] = depth + + # Set up the included_files key indicating which .gyp files contributed to + # this target dict. + if "included_files" in build_file_data: + raise GypError(build_file_path + " must not contain included_files key") + + included = GetIncludedBuildFiles(build_file_path, aux_data) + build_file_data["included_files"] = [] + for included_file in included: + # included_file is relative to the current directory, but it needs to + # be made relative to build_file_path's directory. + included_relative = gyp.common.RelativePath( + included_file, os.path.dirname(build_file_path) + ) + build_file_data["included_files"].append(included_relative) + + # Do a first round of toolsets expansion so that conditions can be defined + # per toolset. + ProcessToolsetsInDict(build_file_data) + + # Apply "pre"/"early" variable expansions and condition evaluations. + ProcessVariablesAndConditionsInDict( + build_file_data, PHASE_EARLY, variables, build_file_path + ) + + # Since some toolsets might have been defined conditionally, perform + # a second round of toolsets expansion now. + ProcessToolsetsInDict(build_file_data) + + # Look at each project's target_defaults dict, and merge settings into + # targets. + if "target_defaults" in build_file_data: + if "targets" not in build_file_data: + raise GypError("Unable to find targets in build file %s" % build_file_path) + + index = 0 + while index < len(build_file_data["targets"]): + # This procedure needs to give the impression that target_defaults is + # used as defaults, and the individual targets inherit from that. + # The individual targets need to be merged into the defaults. Make + # a deep copy of the defaults for each target, merge the target dict + # as found in the input file into that copy, and then hook up the + # copy with the target-specific data merged into it as the replacement + # target dict. + old_target_dict = build_file_data["targets"][index] + new_target_dict = gyp.simple_copy.deepcopy( + build_file_data["target_defaults"] + ) + MergeDicts( + new_target_dict, old_target_dict, build_file_path, build_file_path + ) + build_file_data["targets"][index] = new_target_dict + index += 1 + + # No longer needed. + del build_file_data["target_defaults"] + + # Look for dependencies. This means that dependency resolution occurs + # after "pre" conditionals and variable expansion, but before "post" - + # in other words, you can't put a "dependencies" section inside a "post" + # conditional within a target. + + dependencies = [] + if "targets" in build_file_data: + for target_dict in build_file_data["targets"]: + if "dependencies" not in target_dict: + continue + for dependency in target_dict["dependencies"]: + dependencies.append( + gyp.common.ResolveTarget(build_file_path, dependency, None)[0] + ) + + if load_dependencies: + for dependency in dependencies: + try: + LoadTargetBuildFile( + dependency, + data, + aux_data, + variables, + includes, + depth, + check, + load_dependencies, + ) + except Exception as e: + gyp.common.ExceptionAppend( + e, "while loading dependencies of %s" % build_file_path + ) + raise else: - variables['DEPTH'] = d.replace('\\', '/') - - # The 'target_build_files' key is only set when loading target build files in - # the non-parallel code path, where LoadTargetBuildFile is called - # recursively. In the parallel code path, we don't need to check whether the - # |build_file_path| has already been loaded, because the 'scheduled' set in - # ParallelState guarantees that we never load the same |build_file_path| - # twice. - if 'target_build_files' in data: - if build_file_path in data['target_build_files']: - # Already loaded. - return False - data['target_build_files'].add(build_file_path) - - gyp.DebugOutput(gyp.DEBUG_INCLUDES, - "Loading Target Build File '%s'", build_file_path) - - build_file_data = LoadOneBuildFile(build_file_path, data, aux_data, - includes, True, check) - - # Store DEPTH for later use in generators. - build_file_data['_DEPTH'] = depth - - # Set up the included_files key indicating which .gyp files contributed to - # this target dict. - if 'included_files' in build_file_data: - raise GypError(build_file_path + ' must not contain included_files key') - - included = GetIncludedBuildFiles(build_file_path, aux_data) - build_file_data['included_files'] = [] - for included_file in included: - # included_file is relative to the current directory, but it needs to - # be made relative to build_file_path's directory. - included_relative = \ - gyp.common.RelativePath(included_file, - os.path.dirname(build_file_path)) - build_file_data['included_files'].append(included_relative) - - # Do a first round of toolsets expansion so that conditions can be defined - # per toolset. - ProcessToolsetsInDict(build_file_data) - - # Apply "pre"/"early" variable expansions and condition evaluations. - ProcessVariablesAndConditionsInDict( - build_file_data, PHASE_EARLY, variables, build_file_path) - - # Since some toolsets might have been defined conditionally, perform - # a second round of toolsets expansion now. - ProcessToolsetsInDict(build_file_data) - - # Look at each project's target_defaults dict, and merge settings into - # targets. - if 'target_defaults' in build_file_data: - if 'targets' not in build_file_data: - raise GypError("Unable to find targets in build file %s" % - build_file_path) + return (build_file_path, dependencies) - index = 0 - while index < len(build_file_data['targets']): - # This procedure needs to give the impression that target_defaults is - # used as defaults, and the individual targets inherit from that. - # The individual targets need to be merged into the defaults. Make - # a deep copy of the defaults for each target, merge the target dict - # as found in the input file into that copy, and then hook up the - # copy with the target-specific data merged into it as the replacement - # target dict. - old_target_dict = build_file_data['targets'][index] - new_target_dict = gyp.simple_copy.deepcopy( - build_file_data['target_defaults']) - MergeDicts(new_target_dict, old_target_dict, - build_file_path, build_file_path) - build_file_data['targets'][index] = new_target_dict - index += 1 - - # No longer needed. - del build_file_data['target_defaults'] - - # Look for dependencies. This means that dependency resolution occurs - # after "pre" conditionals and variable expansion, but before "post" - - # in other words, you can't put a "dependencies" section inside a "post" - # conditional within a target. - - dependencies = [] - if 'targets' in build_file_data: - for target_dict in build_file_data['targets']: - if 'dependencies' not in target_dict: - continue - for dependency in target_dict['dependencies']: - dependencies.append( - gyp.common.ResolveTarget(build_file_path, dependency, None)[0]) - - if load_dependencies: - for dependency in dependencies: - try: - LoadTargetBuildFile(dependency, data, aux_data, variables, - includes, depth, check, load_dependencies) - except Exception as e: - gyp.common.ExceptionAppend( - e, 'while loading dependencies of %s' % build_file_path) - raise - else: - return (build_file_path, dependencies) -def CallLoadTargetBuildFile(global_flags, - build_file_path, variables, - includes, depth, check, - generator_input_info): - """Wrapper around LoadTargetBuildFile for parallel processing. +def CallLoadTargetBuildFile( + global_flags, + build_file_path, + variables, + includes, + depth, + check, + generator_input_info, +): + """Wrapper around LoadTargetBuildFile for parallel processing. This wrapper is used when LoadTargetBuildFile is executed in a worker process. """ - try: - signal.signal(signal.SIGINT, signal.SIG_IGN) - - # Apply globals so that the worker process behaves the same. - for key, value in global_flags.items(): - globals()[key] = value - - SetGeneratorGlobals(generator_input_info) - result = LoadTargetBuildFile(build_file_path, per_process_data, - per_process_aux_data, variables, - includes, depth, check, False) - if not result: - return result - - (build_file_path, dependencies) = result - - # We can safely pop the build_file_data from per_process_data because it - # will never be referenced by this process again, so we don't need to keep - # it in the cache. - build_file_data = per_process_data.pop(build_file_path) - - # This gets serialized and sent back to the main process via a pipe. - # It's handled in LoadTargetBuildFileCallback. - return (build_file_path, - build_file_data, - dependencies) - except GypError as e: - sys.stderr.write("gyp: %s\n" % e) - return None - except Exception as e: - print('Exception:', e, file=sys.stderr) - print(traceback.format_exc(), file=sys.stderr) - return None + try: + signal.signal(signal.SIGINT, signal.SIG_IGN) + + # Apply globals so that the worker process behaves the same. + for key, value in global_flags.items(): + globals()[key] = value + + SetGeneratorGlobals(generator_input_info) + result = LoadTargetBuildFile( + build_file_path, + per_process_data, + per_process_aux_data, + variables, + includes, + depth, + check, + False, + ) + if not result: + return result + + (build_file_path, dependencies) = result + + # We can safely pop the build_file_data from per_process_data because it + # will never be referenced by this process again, so we don't need to keep + # it in the cache. + build_file_data = per_process_data.pop(build_file_path) + + # This gets serialized and sent back to the main process via a pipe. + # It's handled in LoadTargetBuildFileCallback. + return (build_file_path, build_file_data, dependencies) + except GypError as e: + sys.stderr.write("gyp: %s\n" % e) + return None + except Exception as e: + print("Exception:", e, file=sys.stderr) + print(traceback.format_exc(), file=sys.stderr) + return None class ParallelProcessingError(Exception): - pass + pass class ParallelState(object): - """Class to keep track of state when processing input files in parallel. + """Class to keep track of state when processing input files in parallel. If build files are loaded in parallel, use this to keep track of state during farming out and processing parallel jobs. It's stored in a global so that the callback function can have access to it. """ - def __init__(self): - # The multiprocessing pool. - self.pool = None - # The condition variable used to protect this object and notify - # the main loop when there might be more data to process. - self.condition = None - # The "data" dict that was passed to LoadTargetBuildFileParallel - self.data = None - # The number of parallel calls outstanding; decremented when a response - # was received. - self.pending = 0 - # The set of all build files that have been scheduled, so we don't - # schedule the same one twice. - self.scheduled = set() - # A list of dependency build file paths that haven't been scheduled yet. - self.dependencies = [] - # Flag to indicate if there was an error in a child process. - self.error = False - - def LoadTargetBuildFileCallback(self, result): - """Handle the results of running LoadTargetBuildFile in another process. + def __init__(self): + # The multiprocessing pool. + self.pool = None + # The condition variable used to protect this object and notify + # the main loop when there might be more data to process. + self.condition = None + # The "data" dict that was passed to LoadTargetBuildFileParallel + self.data = None + # The number of parallel calls outstanding; decremented when a response + # was received. + self.pending = 0 + # The set of all build files that have been scheduled, so we don't + # schedule the same one twice. + self.scheduled = set() + # A list of dependency build file paths that haven't been scheduled yet. + self.dependencies = [] + # Flag to indicate if there was an error in a child process. + self.error = False + + def LoadTargetBuildFileCallback(self, result): + """Handle the results of running LoadTargetBuildFile in another process. """ - self.condition.acquire() - if not result: - self.error = True - self.condition.notify() - self.condition.release() - return - (build_file_path0, build_file_data0, dependencies0) = result - self.data[build_file_path0] = build_file_data0 - self.data['target_build_files'].add(build_file_path0) - for new_dependency in dependencies0: - if new_dependency not in self.scheduled: - self.scheduled.add(new_dependency) - self.dependencies.append(new_dependency) - self.pending -= 1 - self.condition.notify() - self.condition.release() - - -def LoadTargetBuildFilesParallel(build_files, data, variables, includes, depth, - check, generator_input_info): - parallel_state = ParallelState() - parallel_state.condition = threading.Condition() - # Make copies of the build_files argument that we can modify while working. - parallel_state.dependencies = list(build_files) - parallel_state.scheduled = set(build_files) - parallel_state.pending = 0 - parallel_state.data = data - - try: - parallel_state.condition.acquire() - while parallel_state.dependencies or parallel_state.pending: - if parallel_state.error: - break - if not parallel_state.dependencies: - parallel_state.condition.wait() - continue - - dependency = parallel_state.dependencies.pop() - - parallel_state.pending += 1 - global_flags = { - 'path_sections': globals()['path_sections'], - 'non_configuration_keys': globals()['non_configuration_keys'], - 'multiple_toolsets': globals()['multiple_toolsets']} - - if not parallel_state.pool: - parallel_state.pool = multiprocessing.Pool(multiprocessing.cpu_count()) - parallel_state.pool.apply_async( - CallLoadTargetBuildFile, - args = (global_flags, dependency, - variables, includes, depth, check, generator_input_info), - callback = parallel_state.LoadTargetBuildFileCallback) - except KeyboardInterrupt as e: - parallel_state.pool.terminate() - raise e - - parallel_state.condition.release() - - parallel_state.pool.close() - parallel_state.pool.join() - parallel_state.pool = None - - if parallel_state.error: - sys.exit(1) + self.condition.acquire() + if not result: + self.error = True + self.condition.notify() + self.condition.release() + return + (build_file_path0, build_file_data0, dependencies0) = result + self.data[build_file_path0] = build_file_data0 + self.data["target_build_files"].add(build_file_path0) + for new_dependency in dependencies0: + if new_dependency not in self.scheduled: + self.scheduled.add(new_dependency) + self.dependencies.append(new_dependency) + self.pending -= 1 + self.condition.notify() + self.condition.release() + + +def LoadTargetBuildFilesParallel( + build_files, data, variables, includes, depth, check, generator_input_info +): + parallel_state = ParallelState() + parallel_state.condition = threading.Condition() + # Make copies of the build_files argument that we can modify while working. + parallel_state.dependencies = list(build_files) + parallel_state.scheduled = set(build_files) + parallel_state.pending = 0 + parallel_state.data = data + + try: + parallel_state.condition.acquire() + while parallel_state.dependencies or parallel_state.pending: + if parallel_state.error: + break + if not parallel_state.dependencies: + parallel_state.condition.wait() + continue + + dependency = parallel_state.dependencies.pop() + + parallel_state.pending += 1 + global_flags = { + "path_sections": globals()["path_sections"], + "non_configuration_keys": globals()["non_configuration_keys"], + "multiple_toolsets": globals()["multiple_toolsets"], + } + + if not parallel_state.pool: + parallel_state.pool = multiprocessing.Pool(multiprocessing.cpu_count()) + parallel_state.pool.apply_async( + CallLoadTargetBuildFile, + args=( + global_flags, + dependency, + variables, + includes, + depth, + check, + generator_input_info, + ), + callback=parallel_state.LoadTargetBuildFileCallback, + ) + except KeyboardInterrupt as e: + parallel_state.pool.terminate() + raise e + + parallel_state.condition.release() + + parallel_state.pool.close() + parallel_state.pool.join() + parallel_state.pool = None + + if parallel_state.error: + sys.exit(1) + # Look for the bracket that matches the first bracket seen in a # string, and return the start and end as a tuple. For example, if # the input is something like "<(foo <(bar)) blah", then it would # return (1, 13), indicating the entire string except for the leading # "<" and trailing " blah". -LBRACKETS= set('{[(') -BRACKETS = {'}': '{', ']': '[', ')': '('} +LBRACKETS = set("{[(") +BRACKETS = {"}": "{", "]": "[", ")": "("} + + def FindEnclosingBracketGroup(input_str): - stack = [] - start = -1 - for index, char in enumerate(input_str): - if char in LBRACKETS: - stack.append(char) - if start == -1: - start = index - elif char in BRACKETS: - if not stack: - return (-1, -1) - if stack.pop() != BRACKETS[char]: - return (-1, -1) - if not stack: - return (start, index + 1) - return (-1, -1) + stack = [] + start = -1 + for index, char in enumerate(input_str): + if char in LBRACKETS: + stack.append(char) + if start == -1: + start = index + elif char in BRACKETS: + if not stack: + return (-1, -1) + if stack.pop() != BRACKETS[char]: + return (-1, -1) + if not stack: + return (start, index + 1) + return (-1, -1) def IsStrCanonicalInt(string): - """Returns True if |string| is in its canonical integer form. + """Returns True if |string| is in its canonical integer form. The canonical form is such that str(int(string)) == string. """ - if type(string) is str: - # This function is called a lot so for maximum performance, avoid - # involving regexps which would otherwise make the code much - # shorter. Regexps would need twice the time of this function. - if string: - if string == "0": - return True - if string[0] == "-": - string = string[1:] - if not string: - return False - if '1' <= string[0] <= '9': - return string.isdigit() - - return False + if type(string) is str: + # This function is called a lot so for maximum performance, avoid + # involving regexps which would otherwise make the code much + # shorter. Regexps would need twice the time of this function. + if string: + if string == "0": + return True + if string[0] == "-": + string = string[1:] + if not string: + return False + if "1" <= string[0] <= "9": + return string.isdigit() + + return False # This matches things like "<(asdf)", "(?P<(?:(?:!?@?)|\|)?)' - r'(?P[-a-zA-Z0-9_.]+)?' - r'\((?P\s*\[?)' - r'(?P.*?)(\]?)\))') + r"(?P(?P<(?:(?:!?@?)|\|)?)" + r"(?P[-a-zA-Z0-9_.]+)?" + r"\((?P\s*\[?)" + r"(?P.*?)(\]?)\))" +) # This matches the same as early_variable_re, but with '>' instead of '<'. late_variable_re = re.compile( - r'(?P(?P>(?:(?:!?@?)|\|)?)' - r'(?P[-a-zA-Z0-9_.]+)?' - r'\((?P\s*\[?)' - r'(?P.*?)(\]?)\))') + r"(?P(?P>(?:(?:!?@?)|\|)?)" + r"(?P[-a-zA-Z0-9_.]+)?" + r"\((?P\s*\[?)" + r"(?P.*?)(\]?)\))" +) # This matches the same as early_variable_re, but with '^' instead of '<'. latelate_variable_re = re.compile( - r'(?P(?P[\^](?:(?:!?@?)|\|)?)' - r'(?P[-a-zA-Z0-9_.]+)?' - r'\((?P\s*\[?)' - r'(?P.*?)(\]?)\))') + r"(?P(?P[\^](?:(?:!?@?)|\|)?)" + r"(?P[-a-zA-Z0-9_.]+)?" + r"\((?P\s*\[?)" + r"(?P.*?)(\]?)\))" +) # Global cache of results from running commands so they don't have to be run # more then once. @@ -694,12 +754,12 @@ def IsStrCanonicalInt(string): def FixupPlatformCommand(cmd): - if sys.platform == 'win32': - if type(cmd) is list: - cmd = [re.sub('^cat ', 'type ', cmd[0])] + cmd[1:] - else: - cmd = re.sub('^cat ', 'type ', cmd) - return cmd + if sys.platform == "win32": + if type(cmd) is list: + cmd = [re.sub("^cat ", "type ", cmd[0])] + cmd[1:] + else: + cmd = re.sub("^cat ", "type ", cmd) + return cmd PHASE_EARLY = 0 @@ -708,640 +768,702 @@ def FixupPlatformCommand(cmd): def ExpandVariables(input, phase, variables, build_file): - # Look for the pattern that gets expanded into variables - if phase == PHASE_EARLY: - variable_re = early_variable_re - expansion_symbol = '<' - elif phase == PHASE_LATE: - variable_re = late_variable_re - expansion_symbol = '>' - elif phase == PHASE_LATELATE: - variable_re = latelate_variable_re - expansion_symbol = '^' - else: - assert False - - input_str = str(input) - if IsStrCanonicalInt(input_str): - return int(input_str) - - # Do a quick scan to determine if an expensive regex search is warranted. - if expansion_symbol not in input_str: - return input_str - - # Get the entire list of matches as a list of MatchObject instances. - # (using findall here would return strings instead of MatchObjects). - matches = list(variable_re.finditer(input_str)) - if not matches: - return input_str - - output = input_str - # Reverse the list of matches so that replacements are done right-to-left. - # That ensures that earlier replacements won't mess up the string in a - # way that causes later calls to find the earlier substituted text instead - # of what's intended for replacement. - matches.reverse() - for match_group in matches: - match = match_group.groupdict() - gyp.DebugOutput(gyp.DEBUG_VARIABLES, "Matches: %r", match) - # match['replace'] is the substring to look for, match['type'] - # is the character code for the replacement type (< > ! <| >| <@ - # >@ !@), match['is_array'] contains a '[' for command - # arrays, and match['content'] is the name of the variable (< >) - # or command to run (!). match['command_string'] is an optional - # command string. Currently, only 'pymod_do_main' is supported. - - # run_command is true if a ! variant is used. - run_command = '!' in match['type'] - command_string = match['command_string'] - - # file_list is true if a | variant is used. - file_list = '|' in match['type'] - - # Capture these now so we can adjust them later. - replace_start = match_group.start('replace') - replace_end = match_group.end('replace') - - # Find the ending paren, and re-evaluate the contained string. - (c_start, c_end) = FindEnclosingBracketGroup(input_str[replace_start:]) - - # Adjust the replacement range to match the entire command - # found by FindEnclosingBracketGroup (since the variable_re - # probably doesn't match the entire command if it contained - # nested variables). - replace_end = replace_start + c_end - - # Find the "real" replacement, matching the appropriate closing - # paren, and adjust the replacement start and end. - replacement = input_str[replace_start:replace_end] - - # Figure out what the contents of the variable parens are. - contents_start = replace_start + c_start + 1 - contents_end = replace_end - 1 - contents = input_str[contents_start:contents_end] - - # Do filter substitution now for <|(). - # Admittedly, this is different than the evaluation order in other - # contexts. However, since filtration has no chance to run on <|(), - # this seems like the only obvious way to give them access to filters. - if file_list: - processed_variables = gyp.simple_copy.deepcopy(variables) - ProcessListFiltersInDict(contents, processed_variables) - # Recurse to expand variables in the contents - contents = ExpandVariables(contents, phase, - processed_variables, build_file) + # Look for the pattern that gets expanded into variables + if phase == PHASE_EARLY: + variable_re = early_variable_re + expansion_symbol = "<" + elif phase == PHASE_LATE: + variable_re = late_variable_re + expansion_symbol = ">" + elif phase == PHASE_LATELATE: + variable_re = latelate_variable_re + expansion_symbol = "^" else: - # Recurse to expand variables in the contents - contents = ExpandVariables(contents, phase, variables, build_file) - - # Strip off leading/trailing whitespace so that variable matches are - # simpler below (and because they are rarely needed). - contents = contents.strip() - - # expand_to_list is true if an @ variant is used. In that case, - # the expansion should result in a list. Note that the caller - # is to be expecting a list in return, and not all callers do - # because not all are working in list context. Also, for list - # expansions, there can be no other text besides the variable - # expansion in the input string. - expand_to_list = '@' in match['type'] and input_str == replacement - - if run_command or file_list: - # Find the build file's directory, so commands can be run or file lists - # generated relative to it. - build_file_dir = os.path.dirname(build_file) - if build_file_dir == '' and not file_list: - # If build_file is just a leaf filename indicating a file in the - # current directory, build_file_dir might be an empty string. Set - # it to None to signal to subprocess.Popen that it should run the - # command in the current directory. - build_file_dir = None - - # Support <|(listfile.txt ...) which generates a file - # containing items from a gyp list, generated at gyp time. - # This works around actions/rules which have more inputs than will - # fit on the command line. - if file_list: - if type(contents) is list: - contents_list = contents - else: - contents_list = contents.split(' ') - replacement = contents_list[0] - if os.path.isabs(replacement): - raise GypError('| cannot handle absolute paths, got "%s"' % replacement) - - if not generator_filelist_paths: - path = os.path.join(build_file_dir, replacement) - else: - if os.path.isabs(build_file_dir): - toplevel = generator_filelist_paths['toplevel'] - rel_build_file_dir = gyp.common.RelativePath(build_file_dir, toplevel) + assert False + + input_str = str(input) + if IsStrCanonicalInt(input_str): + return int(input_str) + + # Do a quick scan to determine if an expensive regex search is warranted. + if expansion_symbol not in input_str: + return input_str + + # Get the entire list of matches as a list of MatchObject instances. + # (using findall here would return strings instead of MatchObjects). + matches = list(variable_re.finditer(input_str)) + if not matches: + return input_str + + output = input_str + # Reverse the list of matches so that replacements are done right-to-left. + # That ensures that earlier replacements won't mess up the string in a + # way that causes later calls to find the earlier substituted text instead + # of what's intended for replacement. + matches.reverse() + for match_group in matches: + match = match_group.groupdict() + gyp.DebugOutput(gyp.DEBUG_VARIABLES, "Matches: %r", match) + # match['replace'] is the substring to look for, match['type'] + # is the character code for the replacement type (< > ! <| >| <@ + # >@ !@), match['is_array'] contains a '[' for command + # arrays, and match['content'] is the name of the variable (< >) + # or command to run (!). match['command_string'] is an optional + # command string. Currently, only 'pymod_do_main' is supported. + + # run_command is true if a ! variant is used. + run_command = "!" in match["type"] + command_string = match["command_string"] + + # file_list is true if a | variant is used. + file_list = "|" in match["type"] + + # Capture these now so we can adjust them later. + replace_start = match_group.start("replace") + replace_end = match_group.end("replace") + + # Find the ending paren, and re-evaluate the contained string. + (c_start, c_end) = FindEnclosingBracketGroup(input_str[replace_start:]) + + # Adjust the replacement range to match the entire command + # found by FindEnclosingBracketGroup (since the variable_re + # probably doesn't match the entire command if it contained + # nested variables). + replace_end = replace_start + c_end + + # Find the "real" replacement, matching the appropriate closing + # paren, and adjust the replacement start and end. + replacement = input_str[replace_start:replace_end] + + # Figure out what the contents of the variable parens are. + contents_start = replace_start + c_start + 1 + contents_end = replace_end - 1 + contents = input_str[contents_start:contents_end] + + # Do filter substitution now for <|(). + # Admittedly, this is different than the evaluation order in other + # contexts. However, since filtration has no chance to run on <|(), + # this seems like the only obvious way to give them access to filters. + if file_list: + processed_variables = gyp.simple_copy.deepcopy(variables) + ProcessListFiltersInDict(contents, processed_variables) + # Recurse to expand variables in the contents + contents = ExpandVariables(contents, phase, processed_variables, build_file) else: - rel_build_file_dir = build_file_dir - qualified_out_dir = generator_filelist_paths['qualified_out_dir'] - path = os.path.join(qualified_out_dir, rel_build_file_dir, replacement) - gyp.common.EnsureDirExists(path) - - replacement = gyp.common.RelativePath(path, build_file_dir) - f = gyp.common.WriteOnDiff(path) - for i in contents_list[1:]: - f.write('%s\n' % i) - f.close() - - elif run_command: - use_shell = True - if match['is_array']: - contents = eval(contents) - use_shell = False - - # Check for a cached value to avoid executing commands, or generating - # file lists more than once. The cache key contains the command to be - # run as well as the directory to run it from, to account for commands - # that depend on their current directory. - # TODO(http://code.google.com/p/gyp/issues/detail?id=111): In theory, - # someone could author a set of GYP files where each time the command - # is invoked it produces different output by design. When the need - # arises, the syntax should be extended to support no caching off a - # command's output so it is run every time. - cache_key = (str(contents), build_file_dir) - cached_value = cached_command_results.get(cache_key, None) - if cached_value is None: - gyp.DebugOutput(gyp.DEBUG_VARIABLES, - "Executing command '%s' in directory '%s'", - contents, build_file_dir) - - replacement = '' - - if command_string == 'pymod_do_main': - # (sources/) etc. to resolve to - # and empty list if undefined. This allows actions to: - # 'action!': [ - # '>@(_sources!)', - # ], - # 'action/': [ - # '>@(_sources/)', - # ], - replacement = [] else: - raise GypError('Undefined variable ' + contents + - ' in ' + build_file) - else: - replacement = variables[contents] - - if isinstance(replacement, bytes) and not isinstance(replacement, str): - replacement = replacement.decode("utf-8") # done on Python 3 only - if type(replacement) is list: - for item in replacement: - if isinstance(item, bytes) and not isinstance(item, str): - item = item.decode("utf-8") # done on Python 3 only - if not contents[-1] == '/' and type(item) not in (str, int): - raise GypError('Variable ' + contents + - ' must expand to a string or list of strings; ' + - 'list contains a ' + - item.__class__.__name__) - # Run through the list and handle variable expansions in it. Since - # the list is guaranteed not to contain dicts, this won't do anything - # with conditions sections. - ProcessVariablesAndConditionsInList(replacement, phase, variables, - build_file) - elif type(replacement) not in (str, int): - raise GypError('Variable ' + contents + - ' must expand to a string or list of strings; ' + - 'found a ' + replacement.__class__.__name__) - - if expand_to_list: - # Expanding in list context. It's guaranteed that there's only one - # replacement to do in |input_str| and that it's this replacement. See - # above. - if type(replacement) is list: - # If it's already a list, make a copy. - output = replacement[:] - else: - # Split it the same way sh would split arguments. - output = shlex.split(str(replacement)) + if contents not in variables: + if contents[-1] in ["!", "/"]: + # In order to allow cross-compiles (nacl) to happen more naturally, + # we will allow references to >(sources/) etc. to resolve to + # and empty list if undefined. This allows actions to: + # 'action!': [ + # '>@(_sources!)', + # ], + # 'action/': [ + # '>@(_sources/)', + # ], + replacement = [] + else: + raise GypError( + "Undefined variable " + contents + " in " + build_file + ) + else: + replacement = variables[contents] + + if isinstance(replacement, bytes) and not isinstance(replacement, str): + replacement = replacement.decode("utf-8") # done on Python 3 only + if type(replacement) is list: + for item in replacement: + if isinstance(item, bytes) and not isinstance(item, str): + item = item.decode("utf-8") # done on Python 3 only + if not contents[-1] == "/" and type(item) not in (str, int): + raise GypError( + "Variable " + + contents + + " must expand to a string or list of strings; " + + "list contains a " + + item.__class__.__name__ + ) + # Run through the list and handle variable expansions in it. Since + # the list is guaranteed not to contain dicts, this won't do anything + # with conditions sections. + ProcessVariablesAndConditionsInList( + replacement, phase, variables, build_file + ) + elif type(replacement) not in (str, int): + raise GypError( + "Variable " + + contents + + " must expand to a string or list of strings; " + + "found a " + + replacement.__class__.__name__ + ) + + if expand_to_list: + # Expanding in list context. It's guaranteed that there's only one + # replacement to do in |input_str| and that it's this replacement. See + # above. + if type(replacement) is list: + # If it's already a list, make a copy. + output = replacement[:] + else: + # Split it the same way sh would split arguments. + output = shlex.split(str(replacement)) + else: + # Expanding in string context. + encoded_replacement = "" + if type(replacement) is list: + # When expanding a list into string context, turn the list items + # into a string in a way that will work with a subprocess call. + # + # TODO(mark): This isn't completely correct. This should + # call a generator-provided function that observes the + # proper list-to-argument quoting rules on a specific + # platform instead of just calling the POSIX encoding + # routine. + encoded_replacement = gyp.common.EncodePOSIXShellList(replacement) + else: + encoded_replacement = replacement + + output = ( + output[:replace_start] + str(encoded_replacement) + output[replace_end:] + ) + # Prepare for the next match iteration. + input_str = output + + if output == input: + gyp.DebugOutput( + gyp.DEBUG_VARIABLES, + "Found only identity matches on %r, avoiding infinite " "recursion.", + output, + ) else: - # Expanding in string context. - encoded_replacement = '' - if type(replacement) is list: - # When expanding a list into string context, turn the list items - # into a string in a way that will work with a subprocess call. - # - # TODO(mark): This isn't completely correct. This should - # call a generator-provided function that observes the - # proper list-to-argument quoting rules on a specific - # platform instead of just calling the POSIX encoding - # routine. - encoded_replacement = gyp.common.EncodePOSIXShellList(replacement) - else: - encoded_replacement = replacement - - output = output[:replace_start] + str(encoded_replacement) + \ - output[replace_end:] - # Prepare for the next match iteration. - input_str = output - - if output == input: - gyp.DebugOutput(gyp.DEBUG_VARIABLES, - "Found only identity matches on %r, avoiding infinite " - "recursion.", - output) - else: - # Look for more matches now that we've replaced some, to deal with - # expanding local variables (variables defined in the same - # variables block as this one). - gyp.DebugOutput(gyp.DEBUG_VARIABLES, "Found output %r, recursing.", output) + # Look for more matches now that we've replaced some, to deal with + # expanding local variables (variables defined in the same + # variables block as this one). + gyp.DebugOutput(gyp.DEBUG_VARIABLES, "Found output %r, recursing.", output) + if type(output) is list: + if output and type(output[0]) is list: + # Leave output alone if it's a list of lists. + # We don't want such lists to be stringified. + pass + else: + new_output = [] + for item in output: + new_output.append( + ExpandVariables(item, phase, variables, build_file) + ) + output = new_output + else: + output = ExpandVariables(output, phase, variables, build_file) + + # Convert all strings that are canonically-represented integers into integers. if type(output) is list: - if output and type(output[0]) is list: - # Leave output alone if it's a list of lists. - # We don't want such lists to be stringified. - pass - else: - new_output = [] - for item in output: - new_output.append( - ExpandVariables(item, phase, variables, build_file)) - output = new_output - else: - output = ExpandVariables(output, phase, variables, build_file) + for index, outstr in enumerate(output): + if IsStrCanonicalInt(outstr): + output[index] = int(outstr) + elif IsStrCanonicalInt(output): + output = int(output) - # Convert all strings that are canonically-represented integers into integers. - if type(output) is list: - for index in range(0, len(output)): - if IsStrCanonicalInt(output[index]): - output[index] = int(output[index]) - elif IsStrCanonicalInt(output): - output = int(output) + return output - return output # The same condition is often evaluated over and over again so it # makes sense to cache as much as possible between evaluations. cached_conditions_asts = {} + def EvalCondition(condition, conditions_key, phase, variables, build_file): - """Returns the dict that should be used or None if the result was + """Returns the dict that should be used or None if the result was that nothing should be used.""" - if type(condition) is not list: - raise GypError(conditions_key + ' must be a list') - if len(condition) < 2: - # It's possible that condition[0] won't work in which case this - # attempt will raise its own IndexError. That's probably fine. - raise GypError(conditions_key + ' ' + condition[0] + - ' must be at least length 2, not ' + str(len(condition))) - - i = 0 - result = None - while i < len(condition): - cond_expr = condition[i] - true_dict = condition[i + 1] - if type(true_dict) is not dict: - raise GypError('{} {} must be followed by a dictionary, not {}'.format( - conditions_key, cond_expr, type(true_dict))) - if len(condition) > i + 2 and type(condition[i + 2]) is dict: - false_dict = condition[i + 2] - i = i + 3 - if i != len(condition): - raise GypError('{} {} has {} unexpected trailing items'.format( - conditions_key, cond_expr, len(condition) - i)) - else: - false_dict = None - i = i + 2 - if result is None: - result = EvalSingleCondition( - cond_expr, true_dict, false_dict, phase, variables, build_file) + if type(condition) is not list: + raise GypError(conditions_key + " must be a list") + if len(condition) < 2: + # It's possible that condition[0] won't work in which case this + # attempt will raise its own IndexError. That's probably fine. + raise GypError( + conditions_key + + " " + + condition[0] + + " must be at least length 2, not " + + str(len(condition)) + ) + + i = 0 + result = None + while i < len(condition): + cond_expr = condition[i] + true_dict = condition[i + 1] + if type(true_dict) is not dict: + raise GypError( + "{} {} must be followed by a dictionary, not {}".format( + conditions_key, cond_expr, type(true_dict) + ) + ) + if len(condition) > i + 2 and type(condition[i + 2]) is dict: + false_dict = condition[i + 2] + i = i + 3 + if i != len(condition): + raise GypError( + "{} {} has {} unexpected trailing items".format( + conditions_key, cond_expr, len(condition) - i + ) + ) + else: + false_dict = None + i = i + 2 + if result is None: + result = EvalSingleCondition( + cond_expr, true_dict, false_dict, phase, variables, build_file + ) - return result + return result -def EvalSingleCondition( - cond_expr, true_dict, false_dict, phase, variables, build_file): - """Returns true_dict if cond_expr evaluates to true, and false_dict +def EvalSingleCondition(cond_expr, true_dict, false_dict, phase, variables, build_file): + """Returns true_dict if cond_expr evaluates to true, and false_dict otherwise.""" - # Do expansions on the condition itself. Since the condition can naturally - # contain variable references without needing to resort to GYP expansion - # syntax, this is of dubious value for variables, but someone might want to - # use a command expansion directly inside a condition. - cond_expr_expanded = ExpandVariables(cond_expr, phase, variables, - build_file) - if type(cond_expr_expanded) not in (str, int): - raise ValueError( - 'Variable expansion in this context permits str and int ' + \ - 'only, found ' + cond_expr_expanded.__class__.__name__) - - try: - if cond_expr_expanded in cached_conditions_asts: - ast_code = cached_conditions_asts[cond_expr_expanded] - else: - ast_code = compile(cond_expr_expanded, '', 'eval') - cached_conditions_asts[cond_expr_expanded] = ast_code - env = {'__builtins__': {}, 'v': StrictVersion} - if eval(ast_code, env, variables): - return true_dict - return false_dict - except SyntaxError as e: - syntax_error = SyntaxError('%s while evaluating condition \'%s\' in %s ' - 'at character %d.' % - (str(e.args[0]), e.text, build_file, e.offset), - e.filename, e.lineno, e.offset, e.text) - raise syntax_error - except NameError as e: - gyp.common.ExceptionAppend(e, 'while evaluating condition \'%s\' in %s' % - (cond_expr_expanded, build_file)) - raise GypError(e) + # Do expansions on the condition itself. Since the condition can naturally + # contain variable references without needing to resort to GYP expansion + # syntax, this is of dubious value for variables, but someone might want to + # use a command expansion directly inside a condition. + cond_expr_expanded = ExpandVariables(cond_expr, phase, variables, build_file) + if type(cond_expr_expanded) not in (str, int): + raise ValueError( + "Variable expansion in this context permits str and int " + + "only, found " + + cond_expr_expanded.__class__.__name__ + ) + + try: + if cond_expr_expanded in cached_conditions_asts: + ast_code = cached_conditions_asts[cond_expr_expanded] + else: + ast_code = compile(cond_expr_expanded, "", "eval") + cached_conditions_asts[cond_expr_expanded] = ast_code + env = {"__builtins__": {}, "v": StrictVersion} + if eval(ast_code, env, variables): + return true_dict + return false_dict + except SyntaxError as e: + syntax_error = SyntaxError( + "%s while evaluating condition '%s' in %s " + "at character %d." % (str(e.args[0]), e.text, build_file, e.offset), + e.filename, + e.lineno, + e.offset, + e.text, + ) + raise syntax_error + except NameError as e: + gyp.common.ExceptionAppend( + e, + "while evaluating condition '%s' in %s" % (cond_expr_expanded, build_file), + ) + raise GypError(e) def ProcessConditionsInDict(the_dict, phase, variables, build_file): - # Process a 'conditions' or 'target_conditions' section in the_dict, - # depending on phase. - # early -> conditions - # late -> target_conditions - # latelate -> no conditions - # - # Each item in a conditions list consists of cond_expr, a string expression - # evaluated as the condition, and true_dict, a dict that will be merged into - # the_dict if cond_expr evaluates to true. Optionally, a third item, - # false_dict, may be present. false_dict is merged into the_dict if - # cond_expr evaluates to false. - # - # Any dict merged into the_dict will be recursively processed for nested - # conditionals and other expansions, also according to phase, immediately - # prior to being merged. - - if phase == PHASE_EARLY: - conditions_key = 'conditions' - elif phase == PHASE_LATE: - conditions_key = 'target_conditions' - elif phase == PHASE_LATELATE: - return - else: - assert False - - if not conditions_key in the_dict: - return - - conditions_list = the_dict[conditions_key] - # Unhook the conditions list, it's no longer needed. - del the_dict[conditions_key] - - for condition in conditions_list: - merge_dict = EvalCondition(condition, conditions_key, phase, variables, - build_file) - - if merge_dict != None: - # Expand variables and nested conditinals in the merge_dict before - # merging it. - ProcessVariablesAndConditionsInDict(merge_dict, phase, - variables, build_file) - - MergeDicts(the_dict, merge_dict, build_file, build_file) + # Process a 'conditions' or 'target_conditions' section in the_dict, + # depending on phase. + # early -> conditions + # late -> target_conditions + # latelate -> no conditions + # + # Each item in a conditions list consists of cond_expr, a string expression + # evaluated as the condition, and true_dict, a dict that will be merged into + # the_dict if cond_expr evaluates to true. Optionally, a third item, + # false_dict, may be present. false_dict is merged into the_dict if + # cond_expr evaluates to false. + # + # Any dict merged into the_dict will be recursively processed for nested + # conditionals and other expansions, also according to phase, immediately + # prior to being merged. + + if phase == PHASE_EARLY: + conditions_key = "conditions" + elif phase == PHASE_LATE: + conditions_key = "target_conditions" + elif phase == PHASE_LATELATE: + return + else: + assert False + + if conditions_key not in the_dict: + return + + conditions_list = the_dict[conditions_key] + # Unhook the conditions list, it's no longer needed. + del the_dict[conditions_key] + + for condition in conditions_list: + merge_dict = EvalCondition( + condition, conditions_key, phase, variables, build_file + ) + + if merge_dict is not None: + # Expand variables and nested conditinals in the merge_dict before + # merging it. + ProcessVariablesAndConditionsInDict( + merge_dict, phase, variables, build_file + ) + + MergeDicts(the_dict, merge_dict, build_file, build_file) def LoadAutomaticVariablesFromDict(variables, the_dict): - # Any keys with plain string values in the_dict become automatic variables. - # The variable name is the key name with a "_" character prepended. - for key, value in the_dict.items(): - if type(value) in (str, int, list): - variables['_' + key] = value + # Any keys with plain string values in the_dict become automatic variables. + # The variable name is the key name with a "_" character prepended. + for key, value in the_dict.items(): + if type(value) in (str, int, list): + variables["_" + key] = value def LoadVariablesFromVariablesDict(variables, the_dict, the_dict_key): - # Any keys in the_dict's "variables" dict, if it has one, becomes a - # variable. The variable name is the key name in the "variables" dict. - # Variables that end with the % character are set only if they are unset in - # the variables dict. the_dict_key is the name of the key that accesses - # the_dict in the_dict's parent dict. If the_dict's parent is not a dict - # (it could be a list or it could be parentless because it is a root dict), - # the_dict_key will be None. - for key, value in the_dict.get('variables', {}).items(): - if type(value) not in (str, int, list): - continue - - if key.endswith('%'): - variable_name = key[:-1] - if variable_name in variables: - # If the variable is already set, don't set it. - continue - if the_dict_key == 'variables' and variable_name in the_dict: - # If the variable is set without a % in the_dict, and the_dict is a - # variables dict (making |variables| a variables sub-dict of a - # variables dict), use the_dict's definition. - value = the_dict[variable_name] - else: - variable_name = key + # Any keys in the_dict's "variables" dict, if it has one, becomes a + # variable. The variable name is the key name in the "variables" dict. + # Variables that end with the % character are set only if they are unset in + # the variables dict. the_dict_key is the name of the key that accesses + # the_dict in the_dict's parent dict. If the_dict's parent is not a dict + # (it could be a list or it could be parentless because it is a root dict), + # the_dict_key will be None. + for key, value in the_dict.get("variables", {}).items(): + if type(value) not in (str, int, list): + continue + + if key.endswith("%"): + variable_name = key[:-1] + if variable_name in variables: + # If the variable is already set, don't set it. + continue + if the_dict_key == "variables" and variable_name in the_dict: + # If the variable is set without a % in the_dict, and the_dict is a + # variables dict (making |variables| a variables sub-dict of a + # variables dict), use the_dict's definition. + value = the_dict[variable_name] + else: + variable_name = key - variables[variable_name] = value + variables[variable_name] = value -def ProcessVariablesAndConditionsInDict(the_dict, phase, variables_in, - build_file, the_dict_key=None): - """Handle all variable and command expansion and conditional evaluation. +def ProcessVariablesAndConditionsInDict( + the_dict, phase, variables_in, build_file, the_dict_key=None +): + """Handle all variable and command expansion and conditional evaluation. This function is the public entry point for all variable expansions and conditional evaluations. The variables_in dictionary will not be modified by this function. """ - # Make a copy of the variables_in dict that can be modified during the - # loading of automatics and the loading of the variables dict. - variables = variables_in.copy() - LoadAutomaticVariablesFromDict(variables, the_dict) - - if 'variables' in the_dict: - # Make sure all the local variables are added to the variables - # list before we process them so that you can reference one - # variable from another. They will be fully expanded by recursion - # in ExpandVariables. - for key, value in the_dict['variables'].items(): - variables[key] = value - - # Handle the associated variables dict first, so that any variable - # references within can be resolved prior to using them as variables. - # Pass a copy of the variables dict to avoid having it be tainted. - # Otherwise, it would have extra automatics added for everything that - # should just be an ordinary variable in this scope. - ProcessVariablesAndConditionsInDict(the_dict['variables'], phase, - variables, build_file, 'variables') - - LoadVariablesFromVariablesDict(variables, the_dict, the_dict_key) - - for key, value in the_dict.items(): - # Skip "variables", which was already processed if present. - if key != 'variables' and type(value) is str: - expanded = ExpandVariables(value, phase, variables, build_file) - if type(expanded) not in (str, int): - raise ValueError( - 'Variable expansion in this context permits str and int ' + \ - 'only, found ' + expanded.__class__.__name__ + ' for ' + key) - the_dict[key] = expanded - - # Variable expansion may have resulted in changes to automatics. Reload. - # TODO(mark): Optimization: only reload if no changes were made. - variables = variables_in.copy() - LoadAutomaticVariablesFromDict(variables, the_dict) - LoadVariablesFromVariablesDict(variables, the_dict, the_dict_key) - - # Process conditions in this dict. This is done after variable expansion - # so that conditions may take advantage of expanded variables. For example, - # if the_dict contains: - # {'type': '<(library_type)', - # 'conditions': [['_type=="static_library"', { ... }]]}, - # _type, as used in the condition, will only be set to the value of - # library_type if variable expansion is performed before condition - # processing. However, condition processing should occur prior to recursion - # so that variables (both automatic and "variables" dict type) may be - # adjusted by conditions sections, merged into the_dict, and have the - # intended impact on contained dicts. - # - # This arrangement means that a "conditions" section containing a "variables" - # section will only have those variables effective in subdicts, not in - # the_dict. The workaround is to put a "conditions" section within a - # "variables" section. For example: - # {'conditions': [['os=="mac"', {'variables': {'define': 'IS_MAC'}}]], - # 'defines': ['<(define)'], - # 'my_subdict': {'defines': ['<(define)']}}, - # will not result in "IS_MAC" being appended to the "defines" list in the - # current scope but would result in it being appended to the "defines" list - # within "my_subdict". By comparison: - # {'variables': {'conditions': [['os=="mac"', {'define': 'IS_MAC'}]]}, - # 'defines': ['<(define)'], - # 'my_subdict': {'defines': ['<(define)']}}, - # will append "IS_MAC" to both "defines" lists. - - # Evaluate conditions sections, allowing variable expansions within them - # as well as nested conditionals. This will process a 'conditions' or - # 'target_conditions' section, perform appropriate merging and recursive - # conditional and variable processing, and then remove the conditions section - # from the_dict if it is present. - ProcessConditionsInDict(the_dict, phase, variables, build_file) - - # Conditional processing may have resulted in changes to automatics or the - # variables dict. Reload. - variables = variables_in.copy() - LoadAutomaticVariablesFromDict(variables, the_dict) - LoadVariablesFromVariablesDict(variables, the_dict, the_dict_key) - - # Recurse into child dicts, or process child lists which may result in - # further recursion into descendant dicts. - for key, value in the_dict.items(): - # Skip "variables" and string values, which were already processed if - # present. - if key == 'variables' or type(value) is str: - continue - if type(value) is dict: - # Pass a copy of the variables dict so that subdicts can't influence - # parents. - ProcessVariablesAndConditionsInDict(value, phase, variables, - build_file, key) - elif type(value) is list: - # The list itself can't influence the variables dict, and - # ProcessVariablesAndConditionsInList will make copies of the variables - # dict if it needs to pass it to something that can influence it. No - # copy is necessary here. - ProcessVariablesAndConditionsInList(value, phase, variables, - build_file) - elif type(value) is not int: - raise TypeError('Unknown type ' + value.__class__.__name__ + \ - ' for ' + key) - - -def ProcessVariablesAndConditionsInList(the_list, phase, variables, - build_file): - # Iterate using an index so that new values can be assigned into the_list. - index = 0 - while index < len(the_list): - item = the_list[index] - if type(item) is dict: - # Make a copy of the variables dict so that it won't influence anything - # outside of its own scope. - ProcessVariablesAndConditionsInDict(item, phase, variables, build_file) - elif type(item) is list: - ProcessVariablesAndConditionsInList(item, phase, variables, build_file) - elif type(item) is str: - expanded = ExpandVariables(item, phase, variables, build_file) - if type(expanded) in (str, int): - the_list[index] = expanded - elif type(expanded) is list: - the_list[index:index+1] = expanded - index += len(expanded) - - # index now identifies the next item to examine. Continue right now - # without falling into the index increment below. - continue - else: - raise ValueError( - 'Variable expansion in this context permits strings and ' + \ - 'lists only, found ' + expanded.__class__.__name__ + ' at ' + \ - index) - elif type(item) is not int: - raise TypeError('Unknown type ' + item.__class__.__name__ + \ - ' at index ' + index) - index = index + 1 + # Make a copy of the variables_in dict that can be modified during the + # loading of automatics and the loading of the variables dict. + variables = variables_in.copy() + LoadAutomaticVariablesFromDict(variables, the_dict) + + if "variables" in the_dict: + # Make sure all the local variables are added to the variables + # list before we process them so that you can reference one + # variable from another. They will be fully expanded by recursion + # in ExpandVariables. + for key, value in the_dict["variables"].items(): + variables[key] = value + + # Handle the associated variables dict first, so that any variable + # references within can be resolved prior to using them as variables. + # Pass a copy of the variables dict to avoid having it be tainted. + # Otherwise, it would have extra automatics added for everything that + # should just be an ordinary variable in this scope. + ProcessVariablesAndConditionsInDict( + the_dict["variables"], phase, variables, build_file, "variables" + ) + + LoadVariablesFromVariablesDict(variables, the_dict, the_dict_key) + + for key, value in the_dict.items(): + # Skip "variables", which was already processed if present. + if key != "variables" and type(value) is str: + expanded = ExpandVariables(value, phase, variables, build_file) + if type(expanded) not in (str, int): + raise ValueError( + "Variable expansion in this context permits str and int " + + "only, found " + + expanded.__class__.__name__ + + " for " + + key + ) + the_dict[key] = expanded + + # Variable expansion may have resulted in changes to automatics. Reload. + # TODO(mark): Optimization: only reload if no changes were made. + variables = variables_in.copy() + LoadAutomaticVariablesFromDict(variables, the_dict) + LoadVariablesFromVariablesDict(variables, the_dict, the_dict_key) + + # Process conditions in this dict. This is done after variable expansion + # so that conditions may take advantage of expanded variables. For example, + # if the_dict contains: + # {'type': '<(library_type)', + # 'conditions': [['_type=="static_library"', { ... }]]}, + # _type, as used in the condition, will only be set to the value of + # library_type if variable expansion is performed before condition + # processing. However, condition processing should occur prior to recursion + # so that variables (both automatic and "variables" dict type) may be + # adjusted by conditions sections, merged into the_dict, and have the + # intended impact on contained dicts. + # + # This arrangement means that a "conditions" section containing a "variables" + # section will only have those variables effective in subdicts, not in + # the_dict. The workaround is to put a "conditions" section within a + # "variables" section. For example: + # {'conditions': [['os=="mac"', {'variables': {'define': 'IS_MAC'}}]], + # 'defines': ['<(define)'], + # 'my_subdict': {'defines': ['<(define)']}}, + # will not result in "IS_MAC" being appended to the "defines" list in the + # current scope but would result in it being appended to the "defines" list + # within "my_subdict". By comparison: + # {'variables': {'conditions': [['os=="mac"', {'define': 'IS_MAC'}]]}, + # 'defines': ['<(define)'], + # 'my_subdict': {'defines': ['<(define)']}}, + # will append "IS_MAC" to both "defines" lists. + + # Evaluate conditions sections, allowing variable expansions within them + # as well as nested conditionals. This will process a 'conditions' or + # 'target_conditions' section, perform appropriate merging and recursive + # conditional and variable processing, and then remove the conditions section + # from the_dict if it is present. + ProcessConditionsInDict(the_dict, phase, variables, build_file) + + # Conditional processing may have resulted in changes to automatics or the + # variables dict. Reload. + variables = variables_in.copy() + LoadAutomaticVariablesFromDict(variables, the_dict) + LoadVariablesFromVariablesDict(variables, the_dict, the_dict_key) + + # Recurse into child dicts, or process child lists which may result in + # further recursion into descendant dicts. + for key, value in the_dict.items(): + # Skip "variables" and string values, which were already processed if + # present. + if key == "variables" or type(value) is str: + continue + if type(value) is dict: + # Pass a copy of the variables dict so that subdicts can't influence + # parents. + ProcessVariablesAndConditionsInDict( + value, phase, variables, build_file, key + ) + elif type(value) is list: + # The list itself can't influence the variables dict, and + # ProcessVariablesAndConditionsInList will make copies of the variables + # dict if it needs to pass it to something that can influence it. No + # copy is necessary here. + ProcessVariablesAndConditionsInList(value, phase, variables, build_file) + elif type(value) is not int: + raise TypeError("Unknown type " + value.__class__.__name__ + " for " + key) + + +def ProcessVariablesAndConditionsInList(the_list, phase, variables, build_file): + # Iterate using an index so that new values can be assigned into the_list. + index = 0 + while index < len(the_list): + item = the_list[index] + if type(item) is dict: + # Make a copy of the variables dict so that it won't influence anything + # outside of its own scope. + ProcessVariablesAndConditionsInDict(item, phase, variables, build_file) + elif type(item) is list: + ProcessVariablesAndConditionsInList(item, phase, variables, build_file) + elif type(item) is str: + expanded = ExpandVariables(item, phase, variables, build_file) + if type(expanded) in (str, int): + the_list[index] = expanded + elif type(expanded) is list: + the_list[index : index + 1] = expanded + index += len(expanded) + + # index now identifies the next item to examine. Continue right now + # without falling into the index increment below. + continue + else: + raise ValueError( + "Variable expansion in this context permits strings and " + + "lists only, found " + + expanded.__class__.__name__ + + " at " + + index + ) + elif type(item) is not int: + raise TypeError( + "Unknown type " + item.__class__.__name__ + " at index " + index + ) + index = index + 1 def BuildTargetsDict(data): - """Builds a dict mapping fully-qualified target names to their target dicts. + """Builds a dict mapping fully-qualified target names to their target dicts. |data| is a dict mapping loaded build files by pathname relative to the current directory. Values in |data| are build file contents. For each @@ -1353,21 +1475,21 @@ def BuildTargetsDict(data): the dicts in the "targets" lists. """ - targets = {} - for build_file in data['target_build_files']: - for target in data[build_file].get('targets', []): - target_name = gyp.common.QualifiedTarget(build_file, - target['target_name'], - target['toolset']) - if target_name in targets: - raise GypError('Duplicate target definitions for ' + target_name) - targets[target_name] = target + targets = {} + for build_file in data["target_build_files"]: + for target in data[build_file].get("targets", []): + target_name = gyp.common.QualifiedTarget( + build_file, target["target_name"], target["toolset"] + ) + if target_name in targets: + raise GypError("Duplicate target definitions for " + target_name) + targets[target_name] = target - return targets + return targets def QualifyDependencies(targets): - """Make dependency links fully-qualified relative to the current directory. + """Make dependency links fully-qualified relative to the current directory. |targets| is a dict mapping fully-qualified target names to their target dicts. For each target in this dict, keys known to contain dependency @@ -1377,36 +1499,46 @@ def QualifyDependencies(targets): similar dict. """ - all_dependency_sections = [dep + op - for dep in dependency_sections - for op in ('', '!', '/')] - - for target, target_dict in targets.items(): - target_build_file = gyp.common.BuildFile(target) - toolset = target_dict['toolset'] - for dependency_key in all_dependency_sections: - dependencies = target_dict.get(dependency_key, []) - for index in range(0, len(dependencies)): - dep_file, dep_target, dep_toolset = gyp.common.ResolveTarget( - target_build_file, dependencies[index], toolset) - if not multiple_toolsets: - # Ignore toolset specification in the dependency if it is specified. - dep_toolset = toolset - dependency = gyp.common.QualifiedTarget(dep_file, - dep_target, - dep_toolset) - dependencies[index] = dependency - - # Make sure anything appearing in a list other than "dependencies" also - # appears in the "dependencies" list. - if dependency_key != 'dependencies' and \ - dependency not in target_dict['dependencies']: - raise GypError('Found ' + dependency + ' in ' + dependency_key + - ' of ' + target + ', but not in dependencies') + all_dependency_sections = [ + dep + op for dep in dependency_sections for op in ("", "!", "/") + ] + + for target, target_dict in targets.items(): + target_build_file = gyp.common.BuildFile(target) + toolset = target_dict["toolset"] + for dependency_key in all_dependency_sections: + dependencies = target_dict.get(dependency_key, []) + for index, dep in enumerate(dependencies): + dep_file, dep_target, dep_toolset = gyp.common.ResolveTarget( + target_build_file, dep, toolset + ) + if not multiple_toolsets: + # Ignore toolset specification in the dependency if it is specified. + dep_toolset = toolset + dependency = gyp.common.QualifiedTarget( + dep_file, dep_target, dep_toolset + ) + dependencies[index] = dependency + + # Make sure anything appearing in a list other than "dependencies" also + # appears in the "dependencies" list. + if ( + dependency_key != "dependencies" + and dependency not in target_dict["dependencies"] + ): + raise GypError( + "Found " + + dependency + + " in " + + dependency_key + + " of " + + target + + ", but not in dependencies" + ) def ExpandWildcardDependencies(targets, data): - """Expands dependencies specified as build_file:*. + """Expands dependencies specified as build_file:*. For each target in |targets|, examines sections containing links to other targets. If any such section contains a link of the form build_file:*, it @@ -1421,110 +1553,130 @@ def ExpandWildcardDependencies(targets, data): dependency list, must be qualified when this function is called. """ - for target, target_dict in targets.items(): - toolset = target_dict['toolset'] - target_build_file = gyp.common.BuildFile(target) - for dependency_key in dependency_sections: - dependencies = target_dict.get(dependency_key, []) - - # Loop this way instead of "for dependency in" or "for index in range" - # because the dependencies list will be modified within the loop body. - index = 0 - while index < len(dependencies): - (dependency_build_file, dependency_target, dependency_toolset) = \ - gyp.common.ParseQualifiedTarget(dependencies[index]) - if dependency_target != '*' and dependency_toolset != '*': - # Not a wildcard. Keep it moving. - index = index + 1 - continue - - if dependency_build_file == target_build_file: - # It's an error for a target to depend on all other targets in - # the same file, because a target cannot depend on itself. - raise GypError('Found wildcard in ' + dependency_key + ' of ' + - target + ' referring to same build file') - - # Take the wildcard out and adjust the index so that the next - # dependency in the list will be processed the next time through the - # loop. - del dependencies[index] - index = index - 1 - - # Loop through the targets in the other build file, adding them to - # this target's list of dependencies in place of the removed - # wildcard. - dependency_target_dicts = data[dependency_build_file]['targets'] - for dependency_target_dict in dependency_target_dicts: - if int(dependency_target_dict.get('suppress_wildcard', False)): - continue - dependency_target_name = dependency_target_dict['target_name'] - if (dependency_target != '*' and - dependency_target != dependency_target_name): - continue - dependency_target_toolset = dependency_target_dict['toolset'] - if (dependency_toolset != '*' and - dependency_toolset != dependency_target_toolset): - continue - dependency = gyp.common.QualifiedTarget(dependency_build_file, - dependency_target_name, - dependency_target_toolset) - index = index + 1 - dependencies.insert(index, dependency) - - index = index + 1 - - -def Unify(l): - """Removes duplicate elements from l, keeping the first element.""" - seen = {} - return [seen.setdefault(e, e) for e in l if e not in seen] + for target, target_dict in targets.items(): + target_build_file = gyp.common.BuildFile(target) + for dependency_key in dependency_sections: + dependencies = target_dict.get(dependency_key, []) + + # Loop this way instead of "for dependency in" or "for index in range" + # because the dependencies list will be modified within the loop body. + index = 0 + while index < len(dependencies): + ( + dependency_build_file, + dependency_target, + dependency_toolset, + ) = gyp.common.ParseQualifiedTarget(dependencies[index]) + if dependency_target != "*" and dependency_toolset != "*": + # Not a wildcard. Keep it moving. + index = index + 1 + continue + + if dependency_build_file == target_build_file: + # It's an error for a target to depend on all other targets in + # the same file, because a target cannot depend on itself. + raise GypError( + "Found wildcard in " + + dependency_key + + " of " + + target + + " referring to same build file" + ) + + # Take the wildcard out and adjust the index so that the next + # dependency in the list will be processed the next time through the + # loop. + del dependencies[index] + index = index - 1 + + # Loop through the targets in the other build file, adding them to + # this target's list of dependencies in place of the removed + # wildcard. + dependency_target_dicts = data[dependency_build_file]["targets"] + for dependency_target_dict in dependency_target_dicts: + if int(dependency_target_dict.get("suppress_wildcard", False)): + continue + dependency_target_name = dependency_target_dict["target_name"] + if ( + dependency_target != "*" + and dependency_target != dependency_target_name + ): + continue + dependency_target_toolset = dependency_target_dict["toolset"] + if ( + dependency_toolset != "*" + and dependency_toolset != dependency_target_toolset + ): + continue + dependency = gyp.common.QualifiedTarget( + dependency_build_file, + dependency_target_name, + dependency_target_toolset, + ) + index = index + 1 + dependencies.insert(index, dependency) + + index = index + 1 + + +def Unify(items): + """Removes duplicate elements from items, keeping the first element.""" + seen = {} + return [seen.setdefault(e, e) for e in items if e not in seen] def RemoveDuplicateDependencies(targets): - """Makes sure every dependency appears only once in all targets's dependency + """Makes sure every dependency appears only once in all targets's dependency lists.""" - for target_name, target_dict in targets.items(): - for dependency_key in dependency_sections: - dependencies = target_dict.get(dependency_key, []) - if dependencies: - target_dict[dependency_key] = Unify(dependencies) + for target_name, target_dict in targets.items(): + for dependency_key in dependency_sections: + dependencies = target_dict.get(dependency_key, []) + if dependencies: + target_dict[dependency_key] = Unify(dependencies) -def Filter(l, item): - """Removes item from l.""" - res = {} - return [res.setdefault(e, e) for e in l if e != item] +def Filter(items, item): + """Removes item from items.""" + res = {} + return [res.setdefault(e, e) for e in items if e != item] def RemoveSelfDependencies(targets): - """Remove self dependencies from targets that have the prune_self_dependency + """Remove self dependencies from targets that have the prune_self_dependency variable set.""" - for target_name, target_dict in targets.items(): - for dependency_key in dependency_sections: - dependencies = target_dict.get(dependency_key, []) - if dependencies: - for t in dependencies: - if t == target_name: - if targets[t].get('variables', {}).get('prune_self_dependency', 0): - target_dict[dependency_key] = Filter(dependencies, target_name) + for target_name, target_dict in targets.items(): + for dependency_key in dependency_sections: + dependencies = target_dict.get(dependency_key, []) + if dependencies: + for t in dependencies: + if t == target_name: + if ( + targets[t] + .get("variables", {}) + .get("prune_self_dependency", 0) + ): + target_dict[dependency_key] = Filter( + dependencies, target_name + ) def RemoveLinkDependenciesFromNoneTargets(targets): - """Remove dependencies having the 'link_dependency' attribute from the 'none' + """Remove dependencies having the 'link_dependency' attribute from the 'none' targets.""" - for target_name, target_dict in targets.items(): - for dependency_key in dependency_sections: - dependencies = target_dict.get(dependency_key, []) - if dependencies: - for t in dependencies: - if target_dict.get('type', None) == 'none': - if targets[t].get('variables', {}).get('link_dependency', 0): - target_dict[dependency_key] = \ - Filter(target_dict[dependency_key], t) + for target_name, target_dict in targets.items(): + for dependency_key in dependency_sections: + dependencies = target_dict.get(dependency_key, []) + if dependencies: + for t in dependencies: + if target_dict.get("type", None) == "none": + if targets[t].get("variables", {}).get("link_dependency", 0): + target_dict[dependency_key] = Filter( + target_dict[dependency_key], t + ) class DependencyGraphNode(object): - """ + """ Attributes: ref: A reference to an object that this DependencyGraphNode represents. @@ -1532,101 +1684,102 @@ class DependencyGraphNode(object): dependents: List of DependencyGraphNodes that depend on this one. """ - class CircularException(GypError): - pass + class CircularException(GypError): + pass - def __init__(self, ref): - self.ref = ref - self.dependencies = [] - self.dependents = [] - - def __repr__(self): - return '' % self.ref - - def FlattenToList(self): - # flat_list is the sorted list of dependencies - actually, the list items - # are the "ref" attributes of DependencyGraphNodes. Every target will - # appear in flat_list after all of its dependencies, and before all of its - # dependents. - flat_list = OrderedSet() - - def ExtractNodeRef(node): - """Extracts the object that the node represents from the given node.""" - return node.ref - - # in_degree_zeros is the list of DependencyGraphNodes that have no - # dependencies not in flat_list. Initially, it is a copy of the children - # of this node, because when the graph was built, nodes with no - # dependencies were made implicit dependents of the root node. - in_degree_zeros = sorted(self.dependents[:], key=ExtractNodeRef) - - while in_degree_zeros: - # Nodes in in_degree_zeros have no dependencies not in flat_list, so they - # can be appended to flat_list. Take these nodes out of in_degree_zeros - # as work progresses, so that the next node to process from the list can - # always be accessed at a consistent position. - node = in_degree_zeros.pop() - flat_list.add(node.ref) - - # Look at dependents of the node just added to flat_list. Some of them - # may now belong in in_degree_zeros. - for node_dependent in sorted(node.dependents, key=ExtractNodeRef): - is_in_degree_zero = True - # TODO: We want to check through the - # node_dependent.dependencies list but if it's long and we - # always start at the beginning, then we get O(n^2) behaviour. - for node_dependent_dependency in (sorted(node_dependent.dependencies, - key=ExtractNodeRef)): - if not node_dependent_dependency.ref in flat_list: - # The dependent one or more dependencies not in flat_list. There - # will be more chances to add it to flat_list when examining - # it again as a dependent of those other dependencies, provided - # that there are no cycles. - is_in_degree_zero = False - break - - if is_in_degree_zero: - # All of the dependent's dependencies are already in flat_list. Add - # it to in_degree_zeros where it will be processed in a future - # iteration of the outer loop. - in_degree_zeros += [node_dependent] - - return list(flat_list) - - def FindCycles(self): - """ + def __init__(self, ref): + self.ref = ref + self.dependencies = [] + self.dependents = [] + + def __repr__(self): + return "" % self.ref + + def FlattenToList(self): + # flat_list is the sorted list of dependencies - actually, the list items + # are the "ref" attributes of DependencyGraphNodes. Every target will + # appear in flat_list after all of its dependencies, and before all of its + # dependents. + flat_list = OrderedSet() + + def ExtractNodeRef(node): + """Extracts the object that the node represents from the given node.""" + return node.ref + + # in_degree_zeros is the list of DependencyGraphNodes that have no + # dependencies not in flat_list. Initially, it is a copy of the children + # of this node, because when the graph was built, nodes with no + # dependencies were made implicit dependents of the root node. + in_degree_zeros = sorted(self.dependents[:], key=ExtractNodeRef) + + while in_degree_zeros: + # Nodes in in_degree_zeros have no dependencies not in flat_list, so they + # can be appended to flat_list. Take these nodes out of in_degree_zeros + # as work progresses, so that the next node to process from the list can + # always be accessed at a consistent position. + node = in_degree_zeros.pop() + flat_list.add(node.ref) + + # Look at dependents of the node just added to flat_list. Some of them + # may now belong in in_degree_zeros. + for node_dependent in sorted(node.dependents, key=ExtractNodeRef): + is_in_degree_zero = True + # TODO: We want to check through the + # node_dependent.dependencies list but if it's long and we + # always start at the beginning, then we get O(n^2) behaviour. + for node_dependent_dependency in sorted( + node_dependent.dependencies, key=ExtractNodeRef + ): + if node_dependent_dependency.ref not in flat_list: + # The dependent one or more dependencies not in flat_list. + # There will be more chances to add it to flat_list + # when examining it again as a dependent of those other + # dependencies, provided that there are no cycles. + is_in_degree_zero = False + break + + if is_in_degree_zero: + # All of the dependent's dependencies are already in flat_list. Add + # it to in_degree_zeros where it will be processed in a future + # iteration of the outer loop. + in_degree_zeros += [node_dependent] + + return list(flat_list) + + def FindCycles(self): + """ Returns a list of cycles in the graph, where each cycle is its own list. """ - results = [] - visited = set() + results = [] + visited = set() - def Visit(node, path): - for child in node.dependents: - if child in path: - results.append([child] + path[:path.index(child) + 1]) - elif not child in visited: - visited.add(child) - Visit(child, [child] + path) + def Visit(node, path): + for child in node.dependents: + if child in path: + results.append([child] + path[: path.index(child) + 1]) + elif child not in visited: + visited.add(child) + Visit(child, [child] + path) - visited.add(self) - Visit(self, [self]) + visited.add(self) + Visit(self, [self]) - return results + return results - def DirectDependencies(self, dependencies=None): - """Returns a list of just direct dependencies.""" - if dependencies is None: - dependencies = [] + def DirectDependencies(self, dependencies=None): + """Returns a list of just direct dependencies.""" + if dependencies is None: + dependencies = [] - for dependency in self.dependencies: - # Check for None, corresponding to the root node. - if dependency.ref != None and dependency.ref not in dependencies: - dependencies.append(dependency.ref) + for dependency in self.dependencies: + # Check for None, corresponding to the root node. + if dependency.ref and dependency.ref not in dependencies: + dependencies.append(dependency.ref) - return dependencies + return dependencies - def _AddImportedDependencies(self, targets, dependencies=None): - """Given a list of direct dependencies, adds indirect dependencies that + def _AddImportedDependencies(self, targets, dependencies=None): + """Given a list of direct dependencies, adds indirect dependencies that other dependencies have declared to export their settings. This method does not operate on self. Rather, it operates on the list @@ -1643,58 +1796,60 @@ def _AddImportedDependencies(self, targets, dependencies=None): public entry point. """ - if dependencies is None: - dependencies = [] - - index = 0 - while index < len(dependencies): - dependency = dependencies[index] - dependency_dict = targets[dependency] - # Add any dependencies whose settings should be imported to the list - # if not already present. Newly-added items will be checked for - # their own imports when the list iteration reaches them. - # Rather than simply appending new items, insert them after the - # dependency that exported them. This is done to more closely match - # the depth-first method used by DeepDependencies. - add_index = 1 - for imported_dependency in \ - dependency_dict.get('export_dependent_settings', []): - if imported_dependency not in dependencies: - dependencies.insert(index + add_index, imported_dependency) - add_index = add_index + 1 - index = index + 1 - - return dependencies - - def DirectAndImportedDependencies(self, targets, dependencies=None): - """Returns a list of a target's direct dependencies and all indirect + if dependencies is None: + dependencies = [] + + index = 0 + while index < len(dependencies): + dependency = dependencies[index] + dependency_dict = targets[dependency] + # Add any dependencies whose settings should be imported to the list + # if not already present. Newly-added items will be checked for + # their own imports when the list iteration reaches them. + # Rather than simply appending new items, insert them after the + # dependency that exported them. This is done to more closely match + # the depth-first method used by DeepDependencies. + add_index = 1 + for imported_dependency in dependency_dict.get( + "export_dependent_settings", [] + ): + if imported_dependency not in dependencies: + dependencies.insert(index + add_index, imported_dependency) + add_index = add_index + 1 + index = index + 1 + + return dependencies + + def DirectAndImportedDependencies(self, targets, dependencies=None): + """Returns a list of a target's direct dependencies and all indirect dependencies that a dependency has advertised settings should be exported through the dependency for. """ - dependencies = self.DirectDependencies(dependencies) - return self._AddImportedDependencies(targets, dependencies) - - def DeepDependencies(self, dependencies=None): - """Returns an OrderedSet of all of a target's dependencies, recursively.""" - if dependencies is None: - # Using a list to get ordered output and a set to do fast "is it - # already added" checks. - dependencies = OrderedSet() - - for dependency in self.dependencies: - # Check for None, corresponding to the root node. - if dependency.ref is None: - continue - if dependency.ref not in dependencies: - dependency.DeepDependencies(dependencies) - dependencies.add(dependency.ref) - - return dependencies - - def _LinkDependenciesInternal(self, targets, include_shared_libraries, - dependencies=None, initial=True): - """Returns an OrderedSet of dependency targets that are linked + dependencies = self.DirectDependencies(dependencies) + return self._AddImportedDependencies(targets, dependencies) + + def DeepDependencies(self, dependencies=None): + """Returns an OrderedSet of all of a target's dependencies, recursively.""" + if dependencies is None: + # Using a list to get ordered output and a set to do fast "is it + # already added" checks. + dependencies = OrderedSet() + + for dependency in self.dependencies: + # Check for None, corresponding to the root node. + if dependency.ref is None: + continue + if dependency.ref not in dependencies: + dependency.DeepDependencies(dependencies) + dependencies.add(dependency.ref) + + return dependencies + + def _LinkDependenciesInternal( + self, targets, include_shared_libraries, dependencies=None, initial=True + ): + """Returns an OrderedSet of dependency targets that are linked into this target. This function has a split personality, depending on the setting of @@ -1708,625 +1863,683 @@ def _LinkDependenciesInternal(self, targets, include_shared_libraries, If |include_shared_libraries| is False, the resulting dependencies will not include shared_library targets that are linked into this target. """ - if dependencies is None: - # Using a list to get ordered output and a set to do fast "is it - # already added" checks. - dependencies = OrderedSet() - - # Check for None, corresponding to the root node. - if self.ref is None: - return dependencies - - # It's kind of sucky that |targets| has to be passed into this function, - # but that's presently the easiest way to access the target dicts so that - # this function can find target types. - - if 'target_name' not in targets[self.ref]: - raise GypError("Missing 'target_name' field in target.") - - if 'type' not in targets[self.ref]: - raise GypError("Missing 'type' field in target %s" % - targets[self.ref]['target_name']) - - target_type = targets[self.ref]['type'] - - is_linkable = target_type in linkable_types - - if initial and not is_linkable: - # If this is the first target being examined and it's not linkable, - # return an empty list of link dependencies, because the link - # dependencies are intended to apply to the target itself (initial is - # True) and this target won't be linked. - return dependencies - - # Don't traverse 'none' targets if explicitly excluded. - if (target_type == 'none' and - not targets[self.ref].get('dependencies_traverse', True)): - dependencies.add(self.ref) - return dependencies - - # Executables, mac kernel extensions, windows drivers and loadable modules - # are already fully and finally linked. Nothing else can be a link - # dependency of them, there can only be dependencies in the sense that a - # dependent target might run an executable or load the loadable_module. - if not initial and target_type in ('executable', 'loadable_module', - 'mac_kernel_extension', - 'windows_driver'): - return dependencies - - # Shared libraries are already fully linked. They should only be included - # in |dependencies| when adjusting static library dependencies (in order to - # link against the shared_library's import lib), but should not be included - # in |dependencies| when propagating link_settings. - # The |include_shared_libraries| flag controls which of these two cases we - # are handling. - if (not initial and target_type == 'shared_library' and - not include_shared_libraries): - return dependencies - - # The target is linkable, add it to the list of link dependencies. - if self.ref not in dependencies: - dependencies.add(self.ref) - if initial or not is_linkable: - # If this is a subsequent target and it's linkable, don't look any - # further for linkable dependencies, as they'll already be linked into - # this target linkable. Always look at dependencies of the initial - # target, and always look at dependencies of non-linkables. - for dependency in self.dependencies: - dependency._LinkDependenciesInternal(targets, - include_shared_libraries, - dependencies, False) - - return dependencies - - def DependenciesForLinkSettings(self, targets): - """ + if dependencies is None: + # Using a list to get ordered output and a set to do fast "is it + # already added" checks. + dependencies = OrderedSet() + + # Check for None, corresponding to the root node. + if self.ref is None: + return dependencies + + # It's kind of sucky that |targets| has to be passed into this function, + # but that's presently the easiest way to access the target dicts so that + # this function can find target types. + + if "target_name" not in targets[self.ref]: + raise GypError("Missing 'target_name' field in target.") + + if "type" not in targets[self.ref]: + raise GypError( + "Missing 'type' field in target %s" % targets[self.ref]["target_name"] + ) + + target_type = targets[self.ref]["type"] + + is_linkable = target_type in linkable_types + + if initial and not is_linkable: + # If this is the first target being examined and it's not linkable, + # return an empty list of link dependencies, because the link + # dependencies are intended to apply to the target itself (initial is + # True) and this target won't be linked. + return dependencies + + # Don't traverse 'none' targets if explicitly excluded. + if target_type == "none" and not targets[self.ref].get( + "dependencies_traverse", True + ): + dependencies.add(self.ref) + return dependencies + + # Executables, mac kernel extensions, windows drivers and loadable modules + # are already fully and finally linked. Nothing else can be a link + # dependency of them, there can only be dependencies in the sense that a + # dependent target might run an executable or load the loadable_module. + if not initial and target_type in ( + "executable", + "loadable_module", + "mac_kernel_extension", + "windows_driver", + ): + return dependencies + + # Shared libraries are already fully linked. They should only be included + # in |dependencies| when adjusting static library dependencies (in order to + # link against the shared_library's import lib), but should not be included + # in |dependencies| when propagating link_settings. + # The |include_shared_libraries| flag controls which of these two cases we + # are handling. + if ( + not initial + and target_type == "shared_library" + and not include_shared_libraries + ): + return dependencies + + # The target is linkable, add it to the list of link dependencies. + if self.ref not in dependencies: + dependencies.add(self.ref) + if initial or not is_linkable: + # If this is a subsequent target and it's linkable, don't look any + # further for linkable dependencies, as they'll already be linked into + # this target linkable. Always look at dependencies of the initial + # target, and always look at dependencies of non-linkables. + for dependency in self.dependencies: + dependency._LinkDependenciesInternal( + targets, include_shared_libraries, dependencies, False + ) + + return dependencies + + def DependenciesForLinkSettings(self, targets): + """ Returns a list of dependency targets whose link_settings should be merged into this target. """ - # TODO(sbaig) Currently, chrome depends on the bug that shared libraries' - # link_settings are propagated. So for now, we will allow it, unless the - # 'allow_sharedlib_linksettings_propagation' flag is explicitly set to - # False. Once chrome is fixed, we can remove this flag. - include_shared_libraries = \ - targets[self.ref].get('allow_sharedlib_linksettings_propagation', True) - return self._LinkDependenciesInternal(targets, include_shared_libraries) - - def DependenciesToLinkAgainst(self, targets): - """ + # TODO(sbaig) Currently, chrome depends on the bug that shared libraries' + # link_settings are propagated. So for now, we will allow it, unless the + # 'allow_sharedlib_linksettings_propagation' flag is explicitly set to + # False. Once chrome is fixed, we can remove this flag. + include_shared_libraries = targets[self.ref].get( + "allow_sharedlib_linksettings_propagation", True + ) + return self._LinkDependenciesInternal(targets, include_shared_libraries) + + def DependenciesToLinkAgainst(self, targets): + """ Returns a list of dependency targets that are linked into this target. """ - return self._LinkDependenciesInternal(targets, True) + return self._LinkDependenciesInternal(targets, True) def BuildDependencyList(targets): - # Create a DependencyGraphNode for each target. Put it into a dict for easy - # access. - dependency_nodes = {} - for target, spec in targets.items(): - if target not in dependency_nodes: - dependency_nodes[target] = DependencyGraphNode(target) - - # Set up the dependency links. Targets that have no dependencies are treated - # as dependent on root_node. - root_node = DependencyGraphNode(None) - for target, spec in targets.items(): - target_node = dependency_nodes[target] - target_build_file = gyp.common.BuildFile(target) - dependencies = spec.get('dependencies') - if not dependencies: - target_node.dependencies = [root_node] - root_node.dependents.append(target_node) - else: - for dependency in dependencies: - dependency_node = dependency_nodes.get(dependency) - if not dependency_node: - raise GypError("Dependency '%s' not found while " - "trying to load target %s" % (dependency, target)) - target_node.dependencies.append(dependency_node) - dependency_node.dependents.append(target_node) - - flat_list = root_node.FlattenToList() - - # If there's anything left unvisited, there must be a circular dependency - # (cycle). - if len(flat_list) != len(targets): - if not root_node.dependents: - # If all targets have dependencies, add the first target as a dependent - # of root_node so that the cycle can be discovered from root_node. - target = targets.keys()[0] - target_node = dependency_nodes[target] - target_node.dependencies.append(root_node) - root_node.dependents.append(target_node) - - cycles = [] - for cycle in root_node.FindCycles(): - paths = [node.ref for node in cycle] - cycles.append('Cycle: %s' % ' -> '.join(paths)) - raise DependencyGraphNode.CircularException( - 'Cycles in dependency graph detected:\n' + '\n'.join(cycles)) - - return [dependency_nodes, flat_list] + # Create a DependencyGraphNode for each target. Put it into a dict for easy + # access. + dependency_nodes = {} + for target, spec in targets.items(): + if target not in dependency_nodes: + dependency_nodes[target] = DependencyGraphNode(target) + + # Set up the dependency links. Targets that have no dependencies are treated + # as dependent on root_node. + root_node = DependencyGraphNode(None) + for target, spec in targets.items(): + target_node = dependency_nodes[target] + dependencies = spec.get("dependencies") + if not dependencies: + target_node.dependencies = [root_node] + root_node.dependents.append(target_node) + else: + for dependency in dependencies: + dependency_node = dependency_nodes.get(dependency) + if not dependency_node: + raise GypError( + "Dependency '%s' not found while " + "trying to load target %s" % (dependency, target) + ) + target_node.dependencies.append(dependency_node) + dependency_node.dependents.append(target_node) + + flat_list = root_node.FlattenToList() + + # If there's anything left unvisited, there must be a circular dependency + # (cycle). + if len(flat_list) != len(targets): + if not root_node.dependents: + # If all targets have dependencies, add the first target as a dependent + # of root_node so that the cycle can be discovered from root_node. + target = next(iter(targets)) + target_node = dependency_nodes[target] + target_node.dependencies.append(root_node) + root_node.dependents.append(target_node) + + cycles = [] + for cycle in root_node.FindCycles(): + paths = [node.ref for node in cycle] + cycles.append("Cycle: %s" % " -> ".join(paths)) + raise DependencyGraphNode.CircularException( + "Cycles in dependency graph detected:\n" + "\n".join(cycles) + ) + + return [dependency_nodes, flat_list] def VerifyNoGYPFileCircularDependencies(targets): - # Create a DependencyGraphNode for each gyp file containing a target. Put - # it into a dict for easy access. - dependency_nodes = {} - for target in targets: - build_file = gyp.common.BuildFile(target) - if not build_file in dependency_nodes: - dependency_nodes[build_file] = DependencyGraphNode(build_file) - - # Set up the dependency links. - for target, spec in targets.items(): - build_file = gyp.common.BuildFile(target) - build_file_node = dependency_nodes[build_file] - target_dependencies = spec.get('dependencies', []) - for dependency in target_dependencies: - try: - dependency_build_file = gyp.common.BuildFile(dependency) - except GypError as e: - gyp.common.ExceptionAppend( - e, 'while computing dependencies of .gyp file %s' % build_file) - raise - - if dependency_build_file == build_file: - # A .gyp file is allowed to refer back to itself. - continue - dependency_node = dependency_nodes.get(dependency_build_file) - if not dependency_node: - raise GypError("Dependency '%s' not found" % dependency_build_file) - if dependency_node not in build_file_node.dependencies: - build_file_node.dependencies.append(dependency_node) - dependency_node.dependents.append(build_file_node) - - - # Files that have no dependencies are treated as dependent on root_node. - root_node = DependencyGraphNode(None) - for build_file_node in dependency_nodes.values(): - if len(build_file_node.dependencies) == 0: - build_file_node.dependencies.append(root_node) - root_node.dependents.append(build_file_node) - - flat_list = root_node.FlattenToList() - - # If there's anything left unvisited, there must be a circular dependency - # (cycle). - if len(flat_list) != len(dependency_nodes): - if not root_node.dependents: - # If all files have dependencies, add the first file as a dependent - # of root_node so that the cycle can be discovered from root_node. - file_node = dependency_nodes.values()[0] - file_node.dependencies.append(root_node) - root_node.dependents.append(file_node) - cycles = [] - for cycle in root_node.FindCycles(): - paths = [node.ref for node in cycle] - cycles.append('Cycle: %s' % ' -> '.join(paths)) - raise DependencyGraphNode.CircularException( - 'Cycles in .gyp file dependency graph detected:\n' + '\n'.join(cycles)) + # Create a DependencyGraphNode for each gyp file containing a target. Put + # it into a dict for easy access. + dependency_nodes = {} + for target in targets: + build_file = gyp.common.BuildFile(target) + if build_file not in dependency_nodes: + dependency_nodes[build_file] = DependencyGraphNode(build_file) + + # Set up the dependency links. + for target, spec in targets.items(): + build_file = gyp.common.BuildFile(target) + build_file_node = dependency_nodes[build_file] + target_dependencies = spec.get("dependencies", []) + for dependency in target_dependencies: + try: + dependency_build_file = gyp.common.BuildFile(dependency) + except GypError as e: + gyp.common.ExceptionAppend( + e, "while computing dependencies of .gyp file %s" % build_file + ) + raise + + if dependency_build_file == build_file: + # A .gyp file is allowed to refer back to itself. + continue + dependency_node = dependency_nodes.get(dependency_build_file) + if not dependency_node: + raise GypError("Dependency '%s' not found" % dependency_build_file) + if dependency_node not in build_file_node.dependencies: + build_file_node.dependencies.append(dependency_node) + dependency_node.dependents.append(build_file_node) + + # Files that have no dependencies are treated as dependent on root_node. + root_node = DependencyGraphNode(None) + for build_file_node in dependency_nodes.values(): + if len(build_file_node.dependencies) == 0: + build_file_node.dependencies.append(root_node) + root_node.dependents.append(build_file_node) + + flat_list = root_node.FlattenToList() + + # If there's anything left unvisited, there must be a circular dependency + # (cycle). + if len(flat_list) != len(dependency_nodes): + if not root_node.dependents: + # If all files have dependencies, add the first file as a dependent + # of root_node so that the cycle can be discovered from root_node. + file_node = next(iter(dependency_nodes.values())) + file_node.dependencies.append(root_node) + root_node.dependents.append(file_node) + cycles = [] + for cycle in root_node.FindCycles(): + paths = [node.ref for node in cycle] + cycles.append("Cycle: %s" % " -> ".join(paths)) + raise DependencyGraphNode.CircularException( + "Cycles in .gyp file dependency graph detected:\n" + "\n".join(cycles) + ) def DoDependentSettings(key, flat_list, targets, dependency_nodes): - # key should be one of all_dependent_settings, direct_dependent_settings, - # or link_settings. - - for target in flat_list: - target_dict = targets[target] - build_file = gyp.common.BuildFile(target) + # key should be one of all_dependent_settings, direct_dependent_settings, + # or link_settings. - if key == 'all_dependent_settings': - dependencies = dependency_nodes[target].DeepDependencies() - elif key == 'direct_dependent_settings': - dependencies = \ - dependency_nodes[target].DirectAndImportedDependencies(targets) - elif key == 'link_settings': - dependencies = \ - dependency_nodes[target].DependenciesForLinkSettings(targets) - else: - raise GypError("DoDependentSettings doesn't know how to determine " - 'dependencies for ' + key) - - for dependency in dependencies: - dependency_dict = targets[dependency] - if not key in dependency_dict: - continue - dependency_build_file = gyp.common.BuildFile(dependency) - MergeDicts(target_dict, dependency_dict[key], - build_file, dependency_build_file) - - -def AdjustStaticLibraryDependencies(flat_list, targets, dependency_nodes, - sort_dependencies): - # Recompute target "dependencies" properties. For each static library - # target, remove "dependencies" entries referring to other static libraries, - # unless the dependency has the "hard_dependency" attribute set. For each - # linkable target, add a "dependencies" entry referring to all of the - # target's computed list of link dependencies (including static libraries - # if no such entry is already present. - for target in flat_list: - target_dict = targets[target] - target_type = target_dict['type'] - - if target_type == 'static_library': - if not 'dependencies' in target_dict: - continue - - target_dict['dependencies_original'] = target_dict.get( - 'dependencies', [])[:] - - # A static library should not depend on another static library unless - # the dependency relationship is "hard," which should only be done when - # a dependent relies on some side effect other than just the build - # product, like a rule or action output. Further, if a target has a - # non-hard dependency, but that dependency exports a hard dependency, - # the non-hard dependency can safely be removed, but the exported hard - # dependency must be added to the target to keep the same dependency - # ordering. - dependencies = \ - dependency_nodes[target].DirectAndImportedDependencies(targets) - index = 0 - while index < len(dependencies): - dependency = dependencies[index] - dependency_dict = targets[dependency] - - # Remove every non-hard static library dependency and remove every - # non-static library dependency that isn't a direct dependency. - if (dependency_dict['type'] == 'static_library' and \ - not dependency_dict.get('hard_dependency', False)) or \ - (dependency_dict['type'] != 'static_library' and \ - not dependency in target_dict['dependencies']): - # Take the dependency out of the list, and don't increment index - # because the next dependency to analyze will shift into the index - # formerly occupied by the one being removed. - del dependencies[index] + for target in flat_list: + target_dict = targets[target] + build_file = gyp.common.BuildFile(target) + + if key == "all_dependent_settings": + dependencies = dependency_nodes[target].DeepDependencies() + elif key == "direct_dependent_settings": + dependencies = dependency_nodes[target].DirectAndImportedDependencies( + targets + ) + elif key == "link_settings": + dependencies = dependency_nodes[target].DependenciesForLinkSettings(targets) else: - index = index + 1 - - # Update the dependencies. If the dependencies list is empty, it's not - # needed, so unhook it. - if len(dependencies) > 0: - target_dict['dependencies'] = dependencies - else: - del target_dict['dependencies'] - - elif target_type in linkable_types: - # Get a list of dependency targets that should be linked into this - # target. Add them to the dependencies list if they're not already - # present. - - link_dependencies = \ - dependency_nodes[target].DependenciesToLinkAgainst(targets) - for dependency in link_dependencies: - if dependency == target: - continue - if not 'dependencies' in target_dict: - target_dict['dependencies'] = [] - if not dependency in target_dict['dependencies']: - target_dict['dependencies'].append(dependency) - # Sort the dependencies list in the order from dependents to dependencies. - # e.g. If A and B depend on C and C depends on D, sort them in A, B, C, D. - # Note: flat_list is already sorted in the order from dependencies to - # dependents. - if sort_dependencies and 'dependencies' in target_dict: - target_dict['dependencies'] = [dep for dep in reversed(flat_list) - if dep in target_dict['dependencies']] + raise GypError( + "DoDependentSettings doesn't know how to determine " + "dependencies for " + key + ) + + for dependency in dependencies: + dependency_dict = targets[dependency] + if key not in dependency_dict: + continue + dependency_build_file = gyp.common.BuildFile(dependency) + MergeDicts( + target_dict, dependency_dict[key], build_file, dependency_build_file + ) + + +def AdjustStaticLibraryDependencies( + flat_list, targets, dependency_nodes, sort_dependencies +): + # Recompute target "dependencies" properties. For each static library + # target, remove "dependencies" entries referring to other static libraries, + # unless the dependency has the "hard_dependency" attribute set. For each + # linkable target, add a "dependencies" entry referring to all of the + # target's computed list of link dependencies (including static libraries + # if no such entry is already present. + for target in flat_list: + target_dict = targets[target] + target_type = target_dict["type"] + + if target_type == "static_library": + if "dependencies" not in target_dict: + continue + + target_dict["dependencies_original"] = target_dict.get("dependencies", [])[ + : + ] + + # A static library should not depend on another static library unless + # the dependency relationship is "hard," which should only be done when + # a dependent relies on some side effect other than just the build + # product, like a rule or action output. Further, if a target has a + # non-hard dependency, but that dependency exports a hard dependency, + # the non-hard dependency can safely be removed, but the exported hard + # dependency must be added to the target to keep the same dependency + # ordering. + dependencies = dependency_nodes[target].DirectAndImportedDependencies( + targets + ) + index = 0 + while index < len(dependencies): + dependency = dependencies[index] + dependency_dict = targets[dependency] + + # Remove every non-hard static library dependency and remove every + # non-static library dependency that isn't a direct dependency. + if ( + dependency_dict["type"] == "static_library" + and not dependency_dict.get("hard_dependency", False) + ) or ( + dependency_dict["type"] != "static_library" + and dependency not in target_dict["dependencies"] + ): + # Take the dependency out of the list, and don't increment index + # because the next dependency to analyze will shift into the index + # formerly occupied by the one being removed. + del dependencies[index] + else: + index = index + 1 + + # Update the dependencies. If the dependencies list is empty, it's not + # needed, so unhook it. + if len(dependencies) > 0: + target_dict["dependencies"] = dependencies + else: + del target_dict["dependencies"] + + elif target_type in linkable_types: + # Get a list of dependency targets that should be linked into this + # target. Add them to the dependencies list if they're not already + # present. + + link_dependencies = dependency_nodes[target].DependenciesToLinkAgainst( + targets + ) + for dependency in link_dependencies: + if dependency == target: + continue + if "dependencies" not in target_dict: + target_dict["dependencies"] = [] + if dependency not in target_dict["dependencies"]: + target_dict["dependencies"].append(dependency) + # Sort the dependencies list in the order from dependents to dependencies. + # e.g. If A and B depend on C and C depends on D, sort them in A, B, C, D. + # Note: flat_list is already sorted in the order from dependencies to + # dependents. + if sort_dependencies and "dependencies" in target_dict: + target_dict["dependencies"] = [ + dep + for dep in reversed(flat_list) + if dep in target_dict["dependencies"] + ] # Initialize this here to speed up MakePathRelative. -exception_re = re.compile(r'''["']?[-/$<>^]''') +exception_re = re.compile(r"""["']?[-/$<>^]""") def MakePathRelative(to_file, fro_file, item): - # If item is a relative path, it's relative to the build file dict that it's - # coming from. Fix it up to make it relative to the build file dict that - # it's going into. - # Exception: any |item| that begins with these special characters is - # returned without modification. - # / Used when a path is already absolute (shortcut optimization; - # such paths would be returned as absolute anyway) - # $ Used for build environment variables - # - Used for some build environment flags (such as -lapr-1 in a - # "libraries" section) - # < Used for our own variable and command expansions (see ExpandVariables) - # > Used for our own variable and command expansions (see ExpandVariables) - # ^ Used for our own variable and command expansions (see ExpandVariables) - # - # "/' Used when a value is quoted. If these are present, then we - # check the second character instead. - # - if to_file == fro_file or exception_re.match(item): - return item - else: - # TODO(dglazkov) The backslash/forward-slash replacement at the end is a - # temporary measure. This should really be addressed by keeping all paths - # in POSIX until actual project generation. - ret = os.path.normpath(os.path.join( - gyp.common.RelativePath(os.path.dirname(fro_file), - os.path.dirname(to_file)), - item)).replace('\\', '/') - if item.endswith('/'): - ret += '/' - return ret - -def MergeLists(to, fro, to_file, fro_file, is_paths=False, append=True): - # Python documentation recommends objects which do not support hash - # set this value to None. Python library objects follow this rule. - is_hashable = lambda val: val.__hash__ - - # If x is hashable, returns whether x is in s. Else returns whether x is in l. - def is_in_set_or_list(x, s, l): - if is_hashable(x): - return x in s - return x in l - - prepend_index = 0 - - # Make membership testing of hashables in |to| (in particular, strings) - # faster. - hashable_to_set = set(x for x in to if is_hashable(x)) - for item in fro: - singleton = False - if type(item) in (str, int): - # The cheap and easy case. - if is_paths: - to_item = MakePathRelative(to_file, fro_file, item) - else: - to_item = item - - if not (type(item) is str and item.startswith('-')): - # Any string that doesn't begin with a "-" is a singleton - it can - # only appear once in a list, to be enforced by the list merge append - # or prepend. - singleton = True - elif type(item) is dict: - # Make a copy of the dictionary, continuing to look for paths to fix. - # The other intelligent aspects of merge processing won't apply because - # item is being merged into an empty dict. - to_item = {} - MergeDicts(to_item, item, to_file, fro_file) - elif type(item) is list: - # Recurse, making a copy of the list. If the list contains any - # descendant dicts, path fixing will occur. Note that here, custom - # values for is_paths and append are dropped; those are only to be - # applied to |to| and |fro|, not sublists of |fro|. append shouldn't - # matter anyway because the new |to_item| list is empty. - to_item = [] - MergeLists(to_item, item, to_file, fro_file) - else: - raise TypeError( - 'Attempt to merge list item of unsupported type ' + \ - item.__class__.__name__) - - if append: - # If appending a singleton that's already in the list, don't append. - # This ensures that the earliest occurrence of the item will stay put. - if not singleton or not is_in_set_or_list(to_item, hashable_to_set, to): - to.append(to_item) - if is_hashable(to_item): - hashable_to_set.add(to_item) - else: - # If prepending a singleton that's already in the list, remove the - # existing instance and proceed with the prepend. This ensures that the - # item appears at the earliest possible position in the list. - while singleton and to_item in to: - to.remove(to_item) - - # Don't just insert everything at index 0. That would prepend the new - # items to the list in reverse order, which would be an unwelcome - # surprise. - to.insert(prepend_index, to_item) - if is_hashable(to_item): - hashable_to_set.add(to_item) - prepend_index = prepend_index + 1 - - -def MergeDicts(to, fro, to_file, fro_file): - # I wanted to name the parameter "from" but it's a Python keyword... - for k, v in fro.items(): - # It would be nice to do "if not k in to: to[k] = v" but that wouldn't give - # copy semantics. Something else may want to merge from the |fro| dict - # later, and having the same dict ref pointed to twice in the tree isn't - # what anyone wants considering that the dicts may subsequently be - # modified. - if k in to: - bad_merge = False - if type(v) in (str, int): - if type(to[k]) not in (str, int): - bad_merge = True - elif type(v) is not type(to[k]): - bad_merge = True - - if bad_merge: - raise TypeError( - 'Attempt to merge dict value of type ' + v.__class__.__name__ + \ - ' into incompatible type ' + to[k].__class__.__name__ + \ - ' for key ' + k) - if type(v) in (str, int): - # Overwrite the existing value, if any. Cheap and easy. - is_path = IsPathSection(k) - if is_path: - to[k] = MakePathRelative(to_file, fro_file, v) - else: - to[k] = v - elif type(v) is dict: - # Recurse, guaranteeing copies will be made of objects that require it. - if not k in to: - to[k] = {} - MergeDicts(to[k], v, to_file, fro_file) - elif type(v) is list: - # Lists in dicts can be merged with different policies, depending on - # how the key in the "from" dict (k, the from-key) is written. - # - # If the from-key has ...the to-list will have this action - # this character appended:... applied when receiving the from-list: - # = replace - # + prepend - # ? set, only if to-list does not yet exist - # (none) append - # - # This logic is list-specific, but since it relies on the associated - # dict key, it's checked in this dict-oriented function. - ext = k[-1] - append = True - if ext == '=': - list_base = k[:-1] - lists_incompatible = [list_base, list_base + '?'] - to[list_base] = [] - elif ext == '+': - list_base = k[:-1] - lists_incompatible = [list_base + '=', list_base + '?'] - append = False - elif ext == '?': - list_base = k[:-1] - lists_incompatible = [list_base, list_base + '=', list_base + '+'] - else: - list_base = k - lists_incompatible = [list_base + '=', list_base + '?'] - - # Some combinations of merge policies appearing together are meaningless. - # It's stupid to replace and append simultaneously, for example. Append - # and prepend are the only policies that can coexist. - for list_incompatible in lists_incompatible: - if list_incompatible in fro: - raise GypError('Incompatible list policies ' + k + ' and ' + - list_incompatible) - - if list_base in to: - if ext == '?': - # If the key ends in "?", the list will only be merged if it doesn't - # already exist. - continue - elif type(to[list_base]) is not list: - # This may not have been checked above if merging in a list with an - # extension character. - raise TypeError( - 'Attempt to merge dict value of type ' + v.__class__.__name__ + \ - ' into incompatible type ' + to[list_base].__class__.__name__ + \ - ' for key ' + list_base + '(' + k + ')') - else: - to[list_base] = [] - - # Call MergeLists, which will make copies of objects that require it. - # MergeLists can recurse back into MergeDicts, although this will be - # to make copies of dicts (with paths fixed), there will be no - # subsequent dict "merging" once entering a list because lists are - # always replaced, appended to, or prepended to. - is_paths = IsPathSection(list_base) - MergeLists(to[list_base], v, to_file, fro_file, is_paths, append) + # If item is a relative path, it's relative to the build file dict that it's + # coming from. Fix it up to make it relative to the build file dict that + # it's going into. + # Exception: any |item| that begins with these special characters is + # returned without modification. + # / Used when a path is already absolute (shortcut optimization; + # such paths would be returned as absolute anyway) + # $ Used for build environment variables + # - Used for some build environment flags (such as -lapr-1 in a + # "libraries" section) + # < Used for our own variable and command expansions (see ExpandVariables) + # > Used for our own variable and command expansions (see ExpandVariables) + # ^ Used for our own variable and command expansions (see ExpandVariables) + # + # "/' Used when a value is quoted. If these are present, then we + # check the second character instead. + # + if to_file == fro_file or exception_re.match(item): + return item else: - raise TypeError( - 'Attempt to merge dict value of unsupported type ' + \ - v.__class__.__name__ + ' for key ' + k) + # TODO(dglazkov) The backslash/forward-slash replacement at the end is a + # temporary measure. This should really be addressed by keeping all paths + # in POSIX until actual project generation. + ret = os.path.normpath( + os.path.join( + gyp.common.RelativePath( + os.path.dirname(fro_file), os.path.dirname(to_file) + ), + item, + ) + ).replace("\\", "/") + if item.endswith("/"): + ret += "/" + return ret -def MergeConfigWithInheritance(new_configuration_dict, build_file, - target_dict, configuration, visited): - # Skip if previously visted. - if configuration in visited: - return - - # Look at this configuration. - configuration_dict = target_dict['configurations'][configuration] +def MergeLists(to, fro, to_file, fro_file, is_paths=False, append=True): + # Python documentation recommends objects which do not support hash + # set this value to None. Python library objects follow this rule. + def is_hashable(val): + return val.__hash__ + + # If x is hashable, returns whether x is in s. Else returns whether x is in items. + def is_in_set_or_list(x, s, items): + if is_hashable(x): + return x in s + return x in items + + prepend_index = 0 + + # Make membership testing of hashables in |to| (in particular, strings) + # faster. + hashable_to_set = set(x for x in to if is_hashable(x)) + for item in fro: + singleton = False + if type(item) in (str, int): + # The cheap and easy case. + if is_paths: + to_item = MakePathRelative(to_file, fro_file, item) + else: + to_item = item + + if not (type(item) is str and item.startswith("-")): + # Any string that doesn't begin with a "-" is a singleton - it can + # only appear once in a list, to be enforced by the list merge append + # or prepend. + singleton = True + elif type(item) is dict: + # Make a copy of the dictionary, continuing to look for paths to fix. + # The other intelligent aspects of merge processing won't apply because + # item is being merged into an empty dict. + to_item = {} + MergeDicts(to_item, item, to_file, fro_file) + elif type(item) is list: + # Recurse, making a copy of the list. If the list contains any + # descendant dicts, path fixing will occur. Note that here, custom + # values for is_paths and append are dropped; those are only to be + # applied to |to| and |fro|, not sublists of |fro|. append shouldn't + # matter anyway because the new |to_item| list is empty. + to_item = [] + MergeLists(to_item, item, to_file, fro_file) + else: + raise TypeError( + "Attempt to merge list item of unsupported type " + + item.__class__.__name__ + ) + + if append: + # If appending a singleton that's already in the list, don't append. + # This ensures that the earliest occurrence of the item will stay put. + if not singleton or not is_in_set_or_list(to_item, hashable_to_set, to): + to.append(to_item) + if is_hashable(to_item): + hashable_to_set.add(to_item) + else: + # If prepending a singleton that's already in the list, remove the + # existing instance and proceed with the prepend. This ensures that the + # item appears at the earliest possible position in the list. + while singleton and to_item in to: + to.remove(to_item) - # Merge in parents. - for parent in configuration_dict.get('inherit_from', []): - MergeConfigWithInheritance(new_configuration_dict, build_file, - target_dict, parent, visited + [configuration]) + # Don't just insert everything at index 0. That would prepend the new + # items to the list in reverse order, which would be an unwelcome + # surprise. + to.insert(prepend_index, to_item) + if is_hashable(to_item): + hashable_to_set.add(to_item) + prepend_index = prepend_index + 1 - # Merge it into the new config. - MergeDicts(new_configuration_dict, configuration_dict, - build_file, build_file) - # Drop abstract. - if 'abstract' in new_configuration_dict: - del new_configuration_dict['abstract'] +def MergeDicts(to, fro, to_file, fro_file): + # I wanted to name the parameter "from" but it's a Python keyword... + for k, v in fro.items(): + # It would be nice to do "if not k in to: to[k] = v" but that wouldn't give + # copy semantics. Something else may want to merge from the |fro| dict + # later, and having the same dict ref pointed to twice in the tree isn't + # what anyone wants considering that the dicts may subsequently be + # modified. + if k in to: + bad_merge = False + if type(v) in (str, int): + if type(to[k]) not in (str, int): + bad_merge = True + elif not isinstance(v, type(to[k])): + bad_merge = True + + if bad_merge: + raise TypeError( + "Attempt to merge dict value of type " + + v.__class__.__name__ + + " into incompatible type " + + to[k].__class__.__name__ + + " for key " + + k + ) + if type(v) in (str, int): + # Overwrite the existing value, if any. Cheap and easy. + is_path = IsPathSection(k) + if is_path: + to[k] = MakePathRelative(to_file, fro_file, v) + else: + to[k] = v + elif type(v) is dict: + # Recurse, guaranteeing copies will be made of objects that require it. + if k not in to: + to[k] = {} + MergeDicts(to[k], v, to_file, fro_file) + elif type(v) is list: + # Lists in dicts can be merged with different policies, depending on + # how the key in the "from" dict (k, the from-key) is written. + # + # If the from-key has ...the to-list will have this action + # this character appended:... applied when receiving the from-list: + # = replace + # + prepend + # ? set, only if to-list does not yet exist + # (none) append + # + # This logic is list-specific, but since it relies on the associated + # dict key, it's checked in this dict-oriented function. + ext = k[-1] + append = True + if ext == "=": + list_base = k[:-1] + lists_incompatible = [list_base, list_base + "?"] + to[list_base] = [] + elif ext == "+": + list_base = k[:-1] + lists_incompatible = [list_base + "=", list_base + "?"] + append = False + elif ext == "?": + list_base = k[:-1] + lists_incompatible = [list_base, list_base + "=", list_base + "+"] + else: + list_base = k + lists_incompatible = [list_base + "=", list_base + "?"] + + # Some combinations of merge policies appearing together are meaningless. + # It's stupid to replace and append simultaneously, for example. Append + # and prepend are the only policies that can coexist. + for list_incompatible in lists_incompatible: + if list_incompatible in fro: + raise GypError( + "Incompatible list policies " + k + " and " + list_incompatible + ) + + if list_base in to: + if ext == "?": + # If the key ends in "?", the list will only be merged if it doesn't + # already exist. + continue + elif type(to[list_base]) is not list: + # This may not have been checked above if merging in a list with an + # extension character. + raise TypeError( + "Attempt to merge dict value of type " + + v.__class__.__name__ + + " into incompatible type " + + to[list_base].__class__.__name__ + + " for key " + + list_base + + "(" + + k + + ")" + ) + else: + to[list_base] = [] + + # Call MergeLists, which will make copies of objects that require it. + # MergeLists can recurse back into MergeDicts, although this will be + # to make copies of dicts (with paths fixed), there will be no + # subsequent dict "merging" once entering a list because lists are + # always replaced, appended to, or prepended to. + is_paths = IsPathSection(list_base) + MergeLists(to[list_base], v, to_file, fro_file, is_paths, append) + else: + raise TypeError( + "Attempt to merge dict value of unsupported type " + + v.__class__.__name__ + + " for key " + + k + ) + + +def MergeConfigWithInheritance( + new_configuration_dict, build_file, target_dict, configuration, visited +): + # Skip if previously visited. + if configuration in visited: + return + + # Look at this configuration. + configuration_dict = target_dict["configurations"][configuration] + + # Merge in parents. + for parent in configuration_dict.get("inherit_from", []): + MergeConfigWithInheritance( + new_configuration_dict, + build_file, + target_dict, + parent, + visited + [configuration], + ) + + # Merge it into the new config. + MergeDicts(new_configuration_dict, configuration_dict, build_file, build_file) + + # Drop abstract. + if "abstract" in new_configuration_dict: + del new_configuration_dict["abstract"] def SetUpConfigurations(target, target_dict): - # key_suffixes is a list of key suffixes that might appear on key names. - # These suffixes are handled in conditional evaluations (for =, +, and ?) - # and rules/exclude processing (for ! and /). Keys with these suffixes - # should be treated the same as keys without. - key_suffixes = ['=', '+', '?', '!', '/'] - - build_file = gyp.common.BuildFile(target) - - # Provide a single configuration by default if none exists. - # TODO(mark): Signal an error if default_configurations exists but - # configurations does not. - if not 'configurations' in target_dict: - target_dict['configurations'] = {'Default': {}} - if not 'default_configuration' in target_dict: - concrete = [i for (i, config) in target_dict['configurations'].items() - if not config.get('abstract')] - target_dict['default_configuration'] = sorted(concrete)[0] - - merged_configurations = {} - configs = target_dict['configurations'] - for (configuration, old_configuration_dict) in configs.items(): - # Skip abstract configurations (saves work only). - if old_configuration_dict.get('abstract'): - continue - # Configurations inherit (most) settings from the enclosing target scope. - # Get the inheritance relationship right by making a copy of the target - # dict. - new_configuration_dict = {} - for (key, target_val) in target_dict.items(): - key_ext = key[-1:] - if key_ext in key_suffixes: - key_base = key[:-1] - else: - key_base = key - if not key_base in non_configuration_keys: - new_configuration_dict[key] = gyp.simple_copy.deepcopy(target_val) - - # Merge in configuration (with all its parents first). - MergeConfigWithInheritance(new_configuration_dict, build_file, - target_dict, configuration, []) - - merged_configurations[configuration] = new_configuration_dict - - # Put the new configurations back into the target dict as a configuration. - for configuration in merged_configurations.keys(): - target_dict['configurations'][configuration] = ( - merged_configurations[configuration]) - - # Now drop all the abstract ones. - for configuration in list(target_dict['configurations']): - old_configuration_dict = target_dict['configurations'][configuration] - if old_configuration_dict.get('abstract'): - del target_dict['configurations'][configuration] - - # Now that all of the target's configurations have been built, go through - # the target dict's keys and remove everything that's been moved into a - # "configurations" section. - delete_keys = [] - for key in target_dict: - key_ext = key[-1:] - if key_ext in key_suffixes: - key_base = key[:-1] - else: - key_base = key - if not key_base in non_configuration_keys: - delete_keys.append(key) - for key in delete_keys: - del target_dict[key] + # key_suffixes is a list of key suffixes that might appear on key names. + # These suffixes are handled in conditional evaluations (for =, +, and ?) + # and rules/exclude processing (for ! and /). Keys with these suffixes + # should be treated the same as keys without. + key_suffixes = ["=", "+", "?", "!", "/"] - # Check the configurations to see if they contain invalid keys. - for configuration in target_dict['configurations'].keys(): - configuration_dict = target_dict['configurations'][configuration] - for key in configuration_dict.keys(): - if key in invalid_configuration_keys: - raise GypError('%s not allowed in the %s configuration, found in ' - 'target %s' % (key, configuration, target)) + build_file = gyp.common.BuildFile(target) + # Provide a single configuration by default if none exists. + # TODO(mark): Signal an error if default_configurations exists but + # configurations does not. + if "configurations" not in target_dict: + target_dict["configurations"] = {"Default": {}} + if "default_configuration" not in target_dict: + concrete = [ + i + for (i, config) in target_dict["configurations"].items() + if not config.get("abstract") + ] + target_dict["default_configuration"] = sorted(concrete)[0] + + merged_configurations = {} + configs = target_dict["configurations"] + for (configuration, old_configuration_dict) in configs.items(): + # Skip abstract configurations (saves work only). + if old_configuration_dict.get("abstract"): + continue + # Configurations inherit (most) settings from the enclosing target scope. + # Get the inheritance relationship right by making a copy of the target + # dict. + new_configuration_dict = {} + for (key, target_val) in target_dict.items(): + key_ext = key[-1:] + if key_ext in key_suffixes: + key_base = key[:-1] + else: + key_base = key + if key_base not in non_configuration_keys: + new_configuration_dict[key] = gyp.simple_copy.deepcopy(target_val) + + # Merge in configuration (with all its parents first). + MergeConfigWithInheritance( + new_configuration_dict, build_file, target_dict, configuration, [] + ) + + merged_configurations[configuration] = new_configuration_dict + + # Put the new configurations back into the target dict as a configuration. + for configuration in merged_configurations.keys(): + target_dict["configurations"][configuration] = merged_configurations[ + configuration + ] + + # Now drop all the abstract ones. + configs = target_dict["configurations"] + target_dict["configurations"] = { + k: v for k, v in configs.items() if not v.get("abstract") + } + + # Now that all of the target's configurations have been built, go through + # the target dict's keys and remove everything that's been moved into a + # "configurations" section. + delete_keys = [] + for key in target_dict: + key_ext = key[-1:] + if key_ext in key_suffixes: + key_base = key[:-1] + else: + key_base = key + if key_base not in non_configuration_keys: + delete_keys.append(key) + for key in delete_keys: + del target_dict[key] + + # Check the configurations to see if they contain invalid keys. + for configuration in target_dict["configurations"].keys(): + configuration_dict = target_dict["configurations"][configuration] + for key in configuration_dict.keys(): + if key in invalid_configuration_keys: + raise GypError( + "%s not allowed in the %s configuration, found in " + "target %s" % (key, configuration, target) + ) def ProcessListFiltersInDict(name, the_dict): - """Process regular expression and exclusion-based filters on lists. + """Process regular expression and exclusion-based filters on lists. An exclusion list is in a dict key named with a trailing "!", like "sources!". Every item in such a list is removed from the associated @@ -2348,151 +2561,163 @@ def ProcessListFiltersInDict(name, the_dict): patterns can still cause items to be excluded after matching an "include". """ - # Look through the dictionary for any lists whose keys end in "!" or "/". - # These are lists that will be treated as exclude lists and regular - # expression-based exclude/include lists. Collect the lists that are - # needed first, looking for the lists that they operate on, and assemble - # then into |lists|. This is done in a separate loop up front, because - # the _included and _excluded keys need to be added to the_dict, and that - # can't be done while iterating through it. - - lists = [] - del_lists = [] - for key, value in the_dict.items(): - operation = key[-1] - if operation != '!' and operation != '/': - continue - - if type(value) is not list: - raise ValueError(name + ' key ' + key + ' must be list, not ' + \ - value.__class__.__name__) - - list_key = key[:-1] - if list_key not in the_dict: - # This happens when there's a list like "sources!" but no corresponding - # "sources" list. Since there's nothing for it to operate on, queue up - # the "sources!" list for deletion now. - del_lists.append(key) - continue - - if type(the_dict[list_key]) is not list: - value = the_dict[list_key] - raise ValueError(name + ' key ' + list_key + \ - ' must be list, not ' + \ - value.__class__.__name__ + ' when applying ' + \ - {'!': 'exclusion', '/': 'regex'}[operation]) - - if not list_key in lists: - lists.append(list_key) - - # Delete the lists that are known to be unneeded at this point. - for del_list in del_lists: - del the_dict[del_list] - - for list_key in lists: - the_list = the_dict[list_key] - - # Initialize the list_actions list, which is parallel to the_list. Each - # item in list_actions identifies whether the corresponding item in - # the_list should be excluded, unconditionally preserved (included), or - # whether no exclusion or inclusion has been applied. Items for which - # no exclusion or inclusion has been applied (yet) have value -1, items - # excluded have value 0, and items included have value 1. Includes and - # excludes override previous actions. All items in list_actions are - # initialized to -1 because no excludes or includes have been processed - # yet. - list_actions = list((-1,) * len(the_list)) - - exclude_key = list_key + '!' - if exclude_key in the_dict: - for exclude_item in the_dict[exclude_key]: - for index in range(0, len(the_list)): - if exclude_item == the_list[index]: - # This item matches the exclude_item, so set its action to 0 - # (exclude). - list_actions[index] = 0 - - # The "whatever!" list is no longer needed, dump it. - del the_dict[exclude_key] - - regex_key = list_key + '/' - if regex_key in the_dict: - for regex_item in the_dict[regex_key]: - [action, pattern] = regex_item - pattern_re = re.compile(pattern) - - if action == 'exclude': - # This item matches an exclude regex, so set its value to 0 (exclude). - action_value = 0 - elif action == 'include': - # This item matches an include regex, so set its value to 1 (include). - action_value = 1 - else: - # This is an action that doesn't make any sense. - raise ValueError('Unrecognized action ' + action + ' in ' + name + \ - ' key ' + regex_key) - - for index in range(0, len(the_list)): - list_item = the_list[index] - if list_actions[index] == action_value: - # Even if the regex matches, nothing will change so continue (regex - # searches are expensive). + # Look through the dictionary for any lists whose keys end in "!" or "/". + # These are lists that will be treated as exclude lists and regular + # expression-based exclude/include lists. Collect the lists that are + # needed first, looking for the lists that they operate on, and assemble + # then into |lists|. This is done in a separate loop up front, because + # the _included and _excluded keys need to be added to the_dict, and that + # can't be done while iterating through it. + + lists = [] + del_lists = [] + for key, value in the_dict.items(): + operation = key[-1] + if operation != "!" and operation != "/": continue - if pattern_re.search(list_item): - # Regular expression match. - list_actions[index] = action_value - # The "whatever/" list is no longer needed, dump it. - del the_dict[regex_key] + if type(value) is not list: + raise ValueError( + name + " key " + key + " must be list, not " + value.__class__.__name__ + ) + + list_key = key[:-1] + if list_key not in the_dict: + # This happens when there's a list like "sources!" but no corresponding + # "sources" list. Since there's nothing for it to operate on, queue up + # the "sources!" list for deletion now. + del_lists.append(key) + continue - # Add excluded items to the excluded list. - # - # Note that exclude_key ("sources!") is different from excluded_key - # ("sources_excluded"). The exclude_key list is input and it was already - # processed and deleted; the excluded_key list is output and it's about - # to be created. - excluded_key = list_key + '_excluded' - if excluded_key in the_dict: - raise GypError(name + ' key ' + excluded_key + - ' must not be present prior ' - ' to applying exclusion/regex filters for ' + list_key) - - excluded_list = [] - - # Go backwards through the list_actions list so that as items are deleted, - # the indices of items that haven't been seen yet don't shift. That means - # that things need to be prepended to excluded_list to maintain them in the - # same order that they existed in the_list. - for index in range(len(list_actions) - 1, -1, -1): - if list_actions[index] == 0: - # Dump anything with action 0 (exclude). Keep anything with action 1 - # (include) or -1 (no include or exclude seen for the item). - excluded_list.insert(0, the_list[index]) - del the_list[index] - - # If anything was excluded, put the excluded list into the_dict at - # excluded_key. - if len(excluded_list) > 0: - the_dict[excluded_key] = excluded_list - - # Now recurse into subdicts and lists that may contain dicts. - for key, value in the_dict.items(): - if type(value) is dict: - ProcessListFiltersInDict(key, value) - elif type(value) is list: - ProcessListFiltersInList(key, value) + if type(the_dict[list_key]) is not list: + value = the_dict[list_key] + raise ValueError( + name + + " key " + + list_key + + " must be list, not " + + value.__class__.__name__ + + " when applying " + + {"!": "exclusion", "/": "regex"}[operation] + ) + + if list_key not in lists: + lists.append(list_key) + + # Delete the lists that are known to be unneeded at this point. + for del_list in del_lists: + del the_dict[del_list] + + for list_key in lists: + the_list = the_dict[list_key] + + # Initialize the list_actions list, which is parallel to the_list. Each + # item in list_actions identifies whether the corresponding item in + # the_list should be excluded, unconditionally preserved (included), or + # whether no exclusion or inclusion has been applied. Items for which + # no exclusion or inclusion has been applied (yet) have value -1, items + # excluded have value 0, and items included have value 1. Includes and + # excludes override previous actions. All items in list_actions are + # initialized to -1 because no excludes or includes have been processed + # yet. + list_actions = list((-1,) * len(the_list)) + + exclude_key = list_key + "!" + if exclude_key in the_dict: + for exclude_item in the_dict[exclude_key]: + for index, list_item in enumerate(the_list): + if exclude_item == list_item: + # This item matches the exclude_item, so set its action to 0 + # (exclude). + list_actions[index] = 0 + + # The "whatever!" list is no longer needed, dump it. + del the_dict[exclude_key] + + regex_key = list_key + "/" + if regex_key in the_dict: + for regex_item in the_dict[regex_key]: + [action, pattern] = regex_item + pattern_re = re.compile(pattern) + + if action == "exclude": + # This item matches an exclude regex, set its value to 0 (exclude). + action_value = 0 + elif action == "include": + # This item matches an include regex, set its value to 1 (include). + action_value = 1 + else: + # This is an action that doesn't make any sense. + raise ValueError( + "Unrecognized action " + + action + + " in " + + name + + " key " + + regex_key + ) + + for index, list_item in enumerate(the_list): + if list_actions[index] == action_value: + # Even if the regex matches, nothing will change so continue + # (regex searches are expensive). + continue + if pattern_re.search(list_item): + # Regular expression match. + list_actions[index] = action_value + + # The "whatever/" list is no longer needed, dump it. + del the_dict[regex_key] + + # Add excluded items to the excluded list. + # + # Note that exclude_key ("sources!") is different from excluded_key + # ("sources_excluded"). The exclude_key list is input and it was already + # processed and deleted; the excluded_key list is output and it's about + # to be created. + excluded_key = list_key + "_excluded" + if excluded_key in the_dict: + raise GypError( + name + " key " + excluded_key + " must not be present prior " + " to applying exclusion/regex filters for " + list_key + ) + + excluded_list = [] + + # Go backwards through the list_actions list so that as items are deleted, + # the indices of items that haven't been seen yet don't shift. That means + # that things need to be prepended to excluded_list to maintain them in the + # same order that they existed in the_list. + for index in range(len(list_actions) - 1, -1, -1): + if list_actions[index] == 0: + # Dump anything with action 0 (exclude). Keep anything with action 1 + # (include) or -1 (no include or exclude seen for the item). + excluded_list.insert(0, the_list[index]) + del the_list[index] + + # If anything was excluded, put the excluded list into the_dict at + # excluded_key. + if len(excluded_list) > 0: + the_dict[excluded_key] = excluded_list + + # Now recurse into subdicts and lists that may contain dicts. + for key, value in the_dict.items(): + if type(value) is dict: + ProcessListFiltersInDict(key, value) + elif type(value) is list: + ProcessListFiltersInList(key, value) def ProcessListFiltersInList(name, the_list): - for item in the_list: - if type(item) is dict: - ProcessListFiltersInDict(name, item) - elif type(item) is list: - ProcessListFiltersInList(name, item) + for item in the_list: + if type(item) is dict: + ProcessListFiltersInDict(name, item) + elif type(item) is list: + ProcessListFiltersInList(name, item) def ValidateTargetType(target, target_dict): - """Ensures the 'type' field on the target is one of the known types. + """Ensures the 'type' field on the target is one of the known types. Arguments: target: string, name of target. @@ -2500,52 +2725,33 @@ def ValidateTargetType(target, target_dict): Raises an exception on error. """ - VALID_TARGET_TYPES = ('executable', 'loadable_module', - 'static_library', 'shared_library', - 'mac_kernel_extension', 'none', 'windows_driver') - target_type = target_dict.get('type', None) - if target_type not in VALID_TARGET_TYPES: - raise GypError("Target %s has an invalid target type '%s'. " - "Must be one of %s." % - (target, target_type, '/'.join(VALID_TARGET_TYPES))) - if (target_dict.get('standalone_static_library', 0) and - not target_type == 'static_library'): - raise GypError('Target %s has type %s but standalone_static_library flag is' - ' only valid for static_library type.' % (target, - target_type)) - - -def ValidateSourcesInTarget(target, target_dict, build_file, - duplicate_basename_check): - if not duplicate_basename_check: - return - if target_dict.get('type', None) != 'static_library': - return - sources = target_dict.get('sources', []) - basenames = {} - for source in sources: - name, ext = os.path.splitext(source) - is_compiled_file = ext in [ - '.c', '.cc', '.cpp', '.cxx', '.m', '.mm', '.s', '.S'] - if not is_compiled_file: - continue - basename = os.path.basename(name) # Don't include extension. - basenames.setdefault(basename, []).append(source) - - error = '' - for basename, files in basenames.items(): - if len(files) > 1: - error += ' %s: %s\n' % (basename, ' '.join(files)) - - if error: - print('static library %s has several files with the same basename:\n' % target - + error + 'libtool on Mac cannot handle that. Use ' - '--no-duplicate-basename-check to disable this validation.') - raise GypError('Duplicate basenames in sources section, see list above') + VALID_TARGET_TYPES = ( + "executable", + "loadable_module", + "static_library", + "shared_library", + "mac_kernel_extension", + "none", + "windows_driver", + ) + target_type = target_dict.get("type", None) + if target_type not in VALID_TARGET_TYPES: + raise GypError( + "Target %s has an invalid target type '%s'. " + "Must be one of %s." % (target, target_type, "/".join(VALID_TARGET_TYPES)) + ) + if ( + target_dict.get("standalone_static_library", 0) + and not target_type == "static_library" + ): + raise GypError( + "Target %s has type %s but standalone_static_library flag is" + " only valid for static_library type." % (target, target_type) + ) def ValidateRulesInTarget(target, target_dict, extra_sources_for_rules): - """Ensures that the rules sections in target_dict are valid and consistent, + """Ensures that the rules sections in target_dict are valid and consistent, and determines which sources they apply to. Arguments: @@ -2555,357 +2761,389 @@ def ValidateRulesInTarget(target, target_dict, extra_sources_for_rules): addition to 'sources'. """ - # Dicts to map between values found in rules' 'rule_name' and 'extension' - # keys and the rule dicts themselves. - rule_names = {} - rule_extensions = {} - - rules = target_dict.get('rules', []) - for rule in rules: - # Make sure that there's no conflict among rule names and extensions. - rule_name = rule['rule_name'] - if rule_name in rule_names: - raise GypError('rule %s exists in duplicate, target %s' % - (rule_name, target)) - rule_names[rule_name] = rule - - rule_extension = rule['extension'] - if rule_extension.startswith('.'): - rule_extension = rule_extension[1:] - if rule_extension in rule_extensions: - raise GypError(('extension %s associated with multiple rules, ' + - 'target %s rules %s and %s') % - (rule_extension, target, - rule_extensions[rule_extension]['rule_name'], - rule_name)) - rule_extensions[rule_extension] = rule - - # Make sure rule_sources isn't already there. It's going to be - # created below if needed. - if 'rule_sources' in rule: - raise GypError( - 'rule_sources must not exist in input, target %s rule %s' % - (target, rule_name)) - - rule_sources = [] - source_keys = ['sources'] - source_keys.extend(extra_sources_for_rules) - for source_key in source_keys: - for source in target_dict.get(source_key, []): - (source_root, source_extension) = os.path.splitext(source) - if source_extension.startswith('.'): - source_extension = source_extension[1:] - if source_extension == rule_extension: - rule_sources.append(source) - - if len(rule_sources) > 0: - rule['rule_sources'] = rule_sources + # Dicts to map between values found in rules' 'rule_name' and 'extension' + # keys and the rule dicts themselves. + rule_names = {} + rule_extensions = {} + + rules = target_dict.get("rules", []) + for rule in rules: + # Make sure that there's no conflict among rule names and extensions. + rule_name = rule["rule_name"] + if rule_name in rule_names: + raise GypError( + "rule %s exists in duplicate, target %s" % (rule_name, target) + ) + rule_names[rule_name] = rule + + rule_extension = rule["extension"] + if rule_extension.startswith("."): + rule_extension = rule_extension[1:] + if rule_extension in rule_extensions: + raise GypError( + ( + "extension %s associated with multiple rules, " + + "target %s rules %s and %s" + ) + % ( + rule_extension, + target, + rule_extensions[rule_extension]["rule_name"], + rule_name, + ) + ) + rule_extensions[rule_extension] = rule + + # Make sure rule_sources isn't already there. It's going to be + # created below if needed. + if "rule_sources" in rule: + raise GypError( + "rule_sources must not exist in input, target %s rule %s" + % (target, rule_name) + ) + + rule_sources = [] + source_keys = ["sources"] + source_keys.extend(extra_sources_for_rules) + for source_key in source_keys: + for source in target_dict.get(source_key, []): + (source_root, source_extension) = os.path.splitext(source) + if source_extension.startswith("."): + source_extension = source_extension[1:] + if source_extension == rule_extension: + rule_sources.append(source) + + if len(rule_sources) > 0: + rule["rule_sources"] = rule_sources def ValidateRunAsInTarget(target, target_dict, build_file): - target_name = target_dict.get('target_name') - run_as = target_dict.get('run_as') - if not run_as: - return - if type(run_as) is not dict: - raise GypError("The 'run_as' in target %s from file %s should be a " - "dictionary." % - (target_name, build_file)) - action = run_as.get('action') - if not action: - raise GypError("The 'run_as' in target %s from file %s must have an " - "'action' section." % - (target_name, build_file)) - if type(action) is not list: - raise GypError("The 'action' for 'run_as' in target %s from file %s " - "must be a list." % - (target_name, build_file)) - working_directory = run_as.get('working_directory') - if working_directory and type(working_directory) is not str: - raise GypError("The 'working_directory' for 'run_as' in target %s " - "in file %s should be a string." % - (target_name, build_file)) - environment = run_as.get('environment') - if environment and type(environment) is not dict: - raise GypError("The 'environment' for 'run_as' in target %s " - "in file %s should be a dictionary." % - (target_name, build_file)) + target_name = target_dict.get("target_name") + run_as = target_dict.get("run_as") + if not run_as: + return + if type(run_as) is not dict: + raise GypError( + "The 'run_as' in target %s from file %s should be a " + "dictionary." % (target_name, build_file) + ) + action = run_as.get("action") + if not action: + raise GypError( + "The 'run_as' in target %s from file %s must have an " + "'action' section." % (target_name, build_file) + ) + if type(action) is not list: + raise GypError( + "The 'action' for 'run_as' in target %s from file %s " + "must be a list." % (target_name, build_file) + ) + working_directory = run_as.get("working_directory") + if working_directory and type(working_directory) is not str: + raise GypError( + "The 'working_directory' for 'run_as' in target %s " + "in file %s should be a string." % (target_name, build_file) + ) + environment = run_as.get("environment") + if environment and type(environment) is not dict: + raise GypError( + "The 'environment' for 'run_as' in target %s " + "in file %s should be a dictionary." % (target_name, build_file) + ) def ValidateActionsInTarget(target, target_dict, build_file): - '''Validates the inputs to the actions in a target.''' - target_name = target_dict.get('target_name') - actions = target_dict.get('actions', []) - for action in actions: - action_name = action.get('action_name') - if not action_name: - raise GypError("Anonymous action in target %s. " - "An action must have an 'action_name' field." % - target_name) - inputs = action.get('inputs', None) - if inputs is None: - raise GypError('Action in target %s has no inputs.' % target_name) - action_command = action.get('action') - if action_command and not action_command[0]: - raise GypError("Empty action as command in target %s." % target_name) + """Validates the inputs to the actions in a target.""" + target_name = target_dict.get("target_name") + actions = target_dict.get("actions", []) + for action in actions: + action_name = action.get("action_name") + if not action_name: + raise GypError( + "Anonymous action in target %s. " + "An action must have an 'action_name' field." % target_name + ) + inputs = action.get("inputs", None) + if inputs is None: + raise GypError("Action in target %s has no inputs." % target_name) + action_command = action.get("action") + if action_command and not action_command[0]: + raise GypError("Empty action as command in target %s." % target_name) def TurnIntIntoStrInDict(the_dict): - """Given dict the_dict, recursively converts all integers into strings. + """Given dict the_dict, recursively converts all integers into strings. """ - # Use items instead of iteritems because there's no need to try to look at - # reinserted keys and their associated values. - for k, v in the_dict.items(): - if type(v) is int: - v = str(v) - the_dict[k] = v - elif type(v) is dict: - TurnIntIntoStrInDict(v) - elif type(v) is list: - TurnIntIntoStrInList(v) - - if type(k) is int: - del the_dict[k] - the_dict[str(k)] = v + # Use items instead of iteritems because there's no need to try to look at + # reinserted keys and their associated values. + for k, v in the_dict.items(): + if type(v) is int: + v = str(v) + the_dict[k] = v + elif type(v) is dict: + TurnIntIntoStrInDict(v) + elif type(v) is list: + TurnIntIntoStrInList(v) + + if type(k) is int: + del the_dict[k] + the_dict[str(k)] = v def TurnIntIntoStrInList(the_list): - """Given list the_list, recursively converts all integers into strings. + """Given list the_list, recursively converts all integers into strings. """ - for index in range(0, len(the_list)): - item = the_list[index] - if type(item) is int: - the_list[index] = str(item) - elif type(item) is dict: - TurnIntIntoStrInDict(item) - elif type(item) is list: - TurnIntIntoStrInList(item) - - -def PruneUnwantedTargets(targets, flat_list, dependency_nodes, root_targets, - data): - """Return only the targets that are deep dependencies of |root_targets|.""" - qualified_root_targets = [] - for target in root_targets: - target = target.strip() - qualified_targets = gyp.common.FindQualifiedTargets(target, flat_list) - if not qualified_targets: - raise GypError("Could not find target %s" % target) - qualified_root_targets.extend(qualified_targets) - - wanted_targets = {} - for target in qualified_root_targets: - wanted_targets[target] = targets[target] - for dependency in dependency_nodes[target].DeepDependencies(): - wanted_targets[dependency] = targets[dependency] - - wanted_flat_list = [t for t in flat_list if t in wanted_targets] - - # Prune unwanted targets from each build_file's data dict. - for build_file in data['target_build_files']: - if not 'targets' in data[build_file]: - continue - new_targets = [] - for target in data[build_file]['targets']: - qualified_name = gyp.common.QualifiedTarget(build_file, - target['target_name'], - target['toolset']) - if qualified_name in wanted_targets: - new_targets.append(target) - data[build_file]['targets'] = new_targets - - return wanted_targets, wanted_flat_list + for index, item in enumerate(the_list): + if type(item) is int: + the_list[index] = str(item) + elif type(item) is dict: + TurnIntIntoStrInDict(item) + elif type(item) is list: + TurnIntIntoStrInList(item) + + +def PruneUnwantedTargets(targets, flat_list, dependency_nodes, root_targets, data): + """Return only the targets that are deep dependencies of |root_targets|.""" + qualified_root_targets = [] + for target in root_targets: + target = target.strip() + qualified_targets = gyp.common.FindQualifiedTargets(target, flat_list) + if not qualified_targets: + raise GypError("Could not find target %s" % target) + qualified_root_targets.extend(qualified_targets) + + wanted_targets = {} + for target in qualified_root_targets: + wanted_targets[target] = targets[target] + for dependency in dependency_nodes[target].DeepDependencies(): + wanted_targets[dependency] = targets[dependency] + + wanted_flat_list = [t for t in flat_list if t in wanted_targets] + + # Prune unwanted targets from each build_file's data dict. + for build_file in data["target_build_files"]: + if "targets" not in data[build_file]: + continue + new_targets = [] + for target in data[build_file]["targets"]: + qualified_name = gyp.common.QualifiedTarget( + build_file, target["target_name"], target["toolset"] + ) + if qualified_name in wanted_targets: + new_targets.append(target) + data[build_file]["targets"] = new_targets + + return wanted_targets, wanted_flat_list def VerifyNoCollidingTargets(targets): - """Verify that no two targets in the same directory share the same name. + """Verify that no two targets in the same directory share the same name. Arguments: targets: A list of targets in the form 'path/to/file.gyp:target_name'. """ - # Keep a dict going from 'subdirectory:target_name' to 'foo.gyp'. - used = {} - for target in targets: - # Separate out 'path/to/file.gyp, 'target_name' from - # 'path/to/file.gyp:target_name'. - path, name = target.rsplit(':', 1) - # Separate out 'path/to', 'file.gyp' from 'path/to/file.gyp'. - subdir, gyp = os.path.split(path) - # Use '.' for the current directory '', so that the error messages make - # more sense. - if not subdir: - subdir = '.' - # Prepare a key like 'path/to:target_name'. - key = subdir + ':' + name - if key in used: - # Complain if this target is already used. - raise GypError('Duplicate target name "%s" in directory "%s" used both ' - 'in "%s" and "%s".' % (name, subdir, gyp, used[key])) - used[key] = gyp + # Keep a dict going from 'subdirectory:target_name' to 'foo.gyp'. + used = {} + for target in targets: + # Separate out 'path/to/file.gyp, 'target_name' from + # 'path/to/file.gyp:target_name'. + path, name = target.rsplit(":", 1) + # Separate out 'path/to', 'file.gyp' from 'path/to/file.gyp'. + subdir, gyp = os.path.split(path) + # Use '.' for the current directory '', so that the error messages make + # more sense. + if not subdir: + subdir = "." + # Prepare a key like 'path/to:target_name'. + key = subdir + ":" + name + if key in used: + # Complain if this target is already used. + raise GypError( + 'Duplicate target name "%s" in directory "%s" used both ' + 'in "%s" and "%s".' % (name, subdir, gyp, used[key]) + ) + used[key] = gyp def SetGeneratorGlobals(generator_input_info): - # Set up path_sections and non_configuration_keys with the default data plus - # the generator-specific data. - global path_sections - path_sections = set(base_path_sections) - path_sections.update(generator_input_info['path_sections']) - - global non_configuration_keys - non_configuration_keys = base_non_configuration_keys[:] - non_configuration_keys.extend(generator_input_info['non_configuration_keys']) - - global multiple_toolsets - multiple_toolsets = generator_input_info[ - 'generator_supports_multiple_toolsets'] - - global generator_filelist_paths - generator_filelist_paths = generator_input_info['generator_filelist_paths'] - - -def Load(build_files, variables, includes, depth, generator_input_info, check, - circular_check, duplicate_basename_check, parallel, root_targets): - SetGeneratorGlobals(generator_input_info) - # A generator can have other lists (in addition to sources) be processed - # for rules. - extra_sources_for_rules = generator_input_info['extra_sources_for_rules'] - - # Load build files. This loads every target-containing build file into - # the |data| dictionary such that the keys to |data| are build file names, - # and the values are the entire build file contents after "early" or "pre" - # processing has been done and includes have been resolved. - # NOTE: data contains both "target" files (.gyp) and "includes" (.gypi), as - # well as meta-data (e.g. 'included_files' key). 'target_build_files' keeps - # track of the keys corresponding to "target" files. - data = {'target_build_files': set()} - # Normalize paths everywhere. This is important because paths will be - # used as keys to the data dict and for references between input files. - build_files = set(map(os.path.normpath, build_files)) - if parallel: - LoadTargetBuildFilesParallel(build_files, data, variables, includes, depth, - check, generator_input_info) - else: - aux_data = {} - for build_file in build_files: - try: - LoadTargetBuildFile(build_file, data, aux_data, - variables, includes, depth, check, True) - except Exception as e: - gyp.common.ExceptionAppend(e, 'while trying to load %s' % build_file) - raise - - # Build a dict to access each target's subdict by qualified name. - targets = BuildTargetsDict(data) - - # Fully qualify all dependency links. - QualifyDependencies(targets) - - # Remove self-dependencies from targets that have 'prune_self_dependencies' - # set to 1. - RemoveSelfDependencies(targets) - - # Expand dependencies specified as build_file:*. - ExpandWildcardDependencies(targets, data) - - # Remove all dependencies marked as 'link_dependency' from the targets of - # type 'none'. - RemoveLinkDependenciesFromNoneTargets(targets) - - # Apply exclude (!) and regex (/) list filters only for dependency_sections. - for target_name, target_dict in targets.items(): - tmp_dict = {} - for key_base in dependency_sections: - for op in ('', '!', '/'): - key = key_base + op - if key in target_dict: - tmp_dict[key] = target_dict[key] - del target_dict[key] - ProcessListFiltersInDict(target_name, tmp_dict) - # Write the results back to |target_dict|. - for key in tmp_dict: - target_dict[key] = tmp_dict[key] - - # Make sure every dependency appears at most once. - RemoveDuplicateDependencies(targets) - - if circular_check: - # Make sure that any targets in a.gyp don't contain dependencies in other - # .gyp files that further depend on a.gyp. - VerifyNoGYPFileCircularDependencies(targets) - - [dependency_nodes, flat_list] = BuildDependencyList(targets) - - if root_targets: - # Remove, from |targets| and |flat_list|, the targets that are not deep - # dependencies of the targets specified in |root_targets|. - targets, flat_list = PruneUnwantedTargets( - targets, flat_list, dependency_nodes, root_targets, data) - - # Check that no two targets in the same directory have the same name. - VerifyNoCollidingTargets(flat_list) - - # Handle dependent settings of various types. - for settings_type in ['all_dependent_settings', - 'direct_dependent_settings', - 'link_settings']: - DoDependentSettings(settings_type, flat_list, targets, dependency_nodes) - - # Take out the dependent settings now that they've been published to all - # of the targets that require them. + # Set up path_sections and non_configuration_keys with the default data plus + # the generator-specific data. + global path_sections + path_sections = set(base_path_sections) + path_sections.update(generator_input_info["path_sections"]) + + global non_configuration_keys + non_configuration_keys = base_non_configuration_keys[:] + non_configuration_keys.extend(generator_input_info["non_configuration_keys"]) + + global multiple_toolsets + multiple_toolsets = generator_input_info["generator_supports_multiple_toolsets"] + + global generator_filelist_paths + generator_filelist_paths = generator_input_info["generator_filelist_paths"] + + +def Load( + build_files, + variables, + includes, + depth, + generator_input_info, + check, + circular_check, + parallel, + root_targets, +): + SetGeneratorGlobals(generator_input_info) + # A generator can have other lists (in addition to sources) be processed + # for rules. + extra_sources_for_rules = generator_input_info["extra_sources_for_rules"] + + # Load build files. This loads every target-containing build file into + # the |data| dictionary such that the keys to |data| are build file names, + # and the values are the entire build file contents after "early" or "pre" + # processing has been done and includes have been resolved. + # NOTE: data contains both "target" files (.gyp) and "includes" (.gypi), as + # well as meta-data (e.g. 'included_files' key). 'target_build_files' keeps + # track of the keys corresponding to "target" files. + data = {"target_build_files": set()} + # Normalize paths everywhere. This is important because paths will be + # used as keys to the data dict and for references between input files. + build_files = set(map(os.path.normpath, build_files)) + if parallel: + LoadTargetBuildFilesParallel( + build_files, data, variables, includes, depth, check, generator_input_info + ) + else: + aux_data = {} + for build_file in build_files: + try: + LoadTargetBuildFile( + build_file, data, aux_data, variables, includes, depth, check, True + ) + except Exception as e: + gyp.common.ExceptionAppend(e, "while trying to load %s" % build_file) + raise + + # Build a dict to access each target's subdict by qualified name. + targets = BuildTargetsDict(data) + + # Fully qualify all dependency links. + QualifyDependencies(targets) + + # Remove self-dependencies from targets that have 'prune_self_dependencies' + # set to 1. + RemoveSelfDependencies(targets) + + # Expand dependencies specified as build_file:*. + ExpandWildcardDependencies(targets, data) + + # Remove all dependencies marked as 'link_dependency' from the targets of + # type 'none'. + RemoveLinkDependenciesFromNoneTargets(targets) + + # Apply exclude (!) and regex (/) list filters only for dependency_sections. + for target_name, target_dict in targets.items(): + tmp_dict = {} + for key_base in dependency_sections: + for op in ("", "!", "/"): + key = key_base + op + if key in target_dict: + tmp_dict[key] = target_dict[key] + del target_dict[key] + ProcessListFiltersInDict(target_name, tmp_dict) + # Write the results back to |target_dict|. + for key in tmp_dict: + target_dict[key] = tmp_dict[key] + + # Make sure every dependency appears at most once. + RemoveDuplicateDependencies(targets) + + if circular_check: + # Make sure that any targets in a.gyp don't contain dependencies in other + # .gyp files that further depend on a.gyp. + VerifyNoGYPFileCircularDependencies(targets) + + [dependency_nodes, flat_list] = BuildDependencyList(targets) + + if root_targets: + # Remove, from |targets| and |flat_list|, the targets that are not deep + # dependencies of the targets specified in |root_targets|. + targets, flat_list = PruneUnwantedTargets( + targets, flat_list, dependency_nodes, root_targets, data + ) + + # Check that no two targets in the same directory have the same name. + VerifyNoCollidingTargets(flat_list) + + # Handle dependent settings of various types. + for settings_type in [ + "all_dependent_settings", + "direct_dependent_settings", + "link_settings", + ]: + DoDependentSettings(settings_type, flat_list, targets, dependency_nodes) + + # Take out the dependent settings now that they've been published to all + # of the targets that require them. + for target in flat_list: + if settings_type in targets[target]: + del targets[target][settings_type] + + # Make sure static libraries don't declare dependencies on other static + # libraries, but that linkables depend on all unlinked static libraries + # that they need so that their link steps will be correct. + gii = generator_input_info + if gii["generator_wants_static_library_dependencies_adjusted"]: + AdjustStaticLibraryDependencies( + flat_list, + targets, + dependency_nodes, + gii["generator_wants_sorted_dependencies"], + ) + + # Apply "post"/"late"/"target" variable expansions and condition evaluations. for target in flat_list: - if settings_type in targets[target]: - del targets[target][settings_type] - - # Make sure static libraries don't declare dependencies on other static - # libraries, but that linkables depend on all unlinked static libraries - # that they need so that their link steps will be correct. - gii = generator_input_info - if gii['generator_wants_static_library_dependencies_adjusted']: - AdjustStaticLibraryDependencies(flat_list, targets, dependency_nodes, - gii['generator_wants_sorted_dependencies']) - - # Apply "post"/"late"/"target" variable expansions and condition evaluations. - for target in flat_list: - target_dict = targets[target] - build_file = gyp.common.BuildFile(target) - ProcessVariablesAndConditionsInDict( - target_dict, PHASE_LATE, variables, build_file) + target_dict = targets[target] + build_file = gyp.common.BuildFile(target) + ProcessVariablesAndConditionsInDict( + target_dict, PHASE_LATE, variables, build_file + ) - # Move everything that can go into a "configurations" section into one. - for target in flat_list: - target_dict = targets[target] - SetUpConfigurations(target, target_dict) + # Move everything that can go into a "configurations" section into one. + for target in flat_list: + target_dict = targets[target] + SetUpConfigurations(target, target_dict) - # Apply exclude (!) and regex (/) list filters. - for target in flat_list: - target_dict = targets[target] - ProcessListFiltersInDict(target, target_dict) + # Apply exclude (!) and regex (/) list filters. + for target in flat_list: + target_dict = targets[target] + ProcessListFiltersInDict(target, target_dict) - # Apply "latelate" variable expansions and condition evaluations. - for target in flat_list: - target_dict = targets[target] - build_file = gyp.common.BuildFile(target) - ProcessVariablesAndConditionsInDict( - target_dict, PHASE_LATELATE, variables, build_file) - - # Make sure that the rules make sense, and build up rule_sources lists as - # needed. Not all generators will need to use the rule_sources lists, but - # some may, and it seems best to build the list in a common spot. - # Also validate actions and run_as elements in targets. - for target in flat_list: - target_dict = targets[target] - build_file = gyp.common.BuildFile(target) - ValidateTargetType(target, target_dict) - ValidateSourcesInTarget(target, target_dict, build_file, - duplicate_basename_check) - ValidateRulesInTarget(target, target_dict, extra_sources_for_rules) - ValidateRunAsInTarget(target, target_dict, build_file) - ValidateActionsInTarget(target, target_dict, build_file) - - # Generators might not expect ints. Turn them into strs. - TurnIntIntoStrInDict(data) - - # TODO(mark): Return |data| for now because the generator needs a list of - # build files that came in. In the future, maybe it should just accept - # a list, and not the whole data dict. - return [flat_list, targets, data] + # Apply "latelate" variable expansions and condition evaluations. + for target in flat_list: + target_dict = targets[target] + build_file = gyp.common.BuildFile(target) + ProcessVariablesAndConditionsInDict( + target_dict, PHASE_LATELATE, variables, build_file + ) + + # Make sure that the rules make sense, and build up rule_sources lists as + # needed. Not all generators will need to use the rule_sources lists, but + # some may, and it seems best to build the list in a common spot. + # Also validate actions and run_as elements in targets. + for target in flat_list: + target_dict = targets[target] + build_file = gyp.common.BuildFile(target) + ValidateTargetType(target, target_dict) + ValidateRulesInTarget(target, target_dict, extra_sources_for_rules) + ValidateRunAsInTarget(target, target_dict, build_file) + ValidateActionsInTarget(target, target_dict, build_file) + + # Generators might not expect ints. Turn them into strs. + TurnIntIntoStrInDict(data) + + # TODO(mark): Return |data| for now because the generator needs a list of + # build files that came in. In the future, maybe it should just accept + # a list, and not the whole data dict. + return [flat_list, targets, data] diff --git a/tools/gyp/pylib/gyp/input_test.py b/tools/gyp/pylib/gyp/input_test.py index 1bc5e3d3081ea6..6672ddc014b150 100755 --- a/tools/gyp/pylib/gyp/input_test.py +++ b/tools/gyp/pylib/gyp/input_test.py @@ -8,83 +8,91 @@ import gyp.input import unittest -import sys class TestFindCycles(unittest.TestCase): - def setUp(self): - self.nodes = {} - for x in ('a', 'b', 'c', 'd', 'e'): - self.nodes[x] = gyp.input.DependencyGraphNode(x) - - def _create_dependency(self, dependent, dependency): - dependent.dependencies.append(dependency) - dependency.dependents.append(dependent) - - def test_no_cycle_empty_graph(self): - for label, node in self.nodes.items(): - self.assertEqual([], node.FindCycles()) - - def test_no_cycle_line(self): - self._create_dependency(self.nodes['a'], self.nodes['b']) - self._create_dependency(self.nodes['b'], self.nodes['c']) - self._create_dependency(self.nodes['c'], self.nodes['d']) - - for label, node in self.nodes.items(): - self.assertEqual([], node.FindCycles()) - - def test_no_cycle_dag(self): - self._create_dependency(self.nodes['a'], self.nodes['b']) - self._create_dependency(self.nodes['a'], self.nodes['c']) - self._create_dependency(self.nodes['b'], self.nodes['c']) - - for label, node in self.nodes.items(): - self.assertEqual([], node.FindCycles()) - - def test_cycle_self_reference(self): - self._create_dependency(self.nodes['a'], self.nodes['a']) - - self.assertEqual([[self.nodes['a'], self.nodes['a']]], - self.nodes['a'].FindCycles()) - - def test_cycle_two_nodes(self): - self._create_dependency(self.nodes['a'], self.nodes['b']) - self._create_dependency(self.nodes['b'], self.nodes['a']) - - self.assertEqual([[self.nodes['a'], self.nodes['b'], self.nodes['a']]], - self.nodes['a'].FindCycles()) - self.assertEqual([[self.nodes['b'], self.nodes['a'], self.nodes['b']]], - self.nodes['b'].FindCycles()) - - def test_two_cycles(self): - self._create_dependency(self.nodes['a'], self.nodes['b']) - self._create_dependency(self.nodes['b'], self.nodes['a']) - - self._create_dependency(self.nodes['b'], self.nodes['c']) - self._create_dependency(self.nodes['c'], self.nodes['b']) - - cycles = self.nodes['a'].FindCycles() - self.assertTrue( - [self.nodes['a'], self.nodes['b'], self.nodes['a']] in cycles) - self.assertTrue( - [self.nodes['b'], self.nodes['c'], self.nodes['b']] in cycles) - self.assertEqual(2, len(cycles)) - - def test_big_cycle(self): - self._create_dependency(self.nodes['a'], self.nodes['b']) - self._create_dependency(self.nodes['b'], self.nodes['c']) - self._create_dependency(self.nodes['c'], self.nodes['d']) - self._create_dependency(self.nodes['d'], self.nodes['e']) - self._create_dependency(self.nodes['e'], self.nodes['a']) - - self.assertEqual([[self.nodes['a'], - self.nodes['b'], - self.nodes['c'], - self.nodes['d'], - self.nodes['e'], - self.nodes['a']]], - self.nodes['a'].FindCycles()) - - -if __name__ == '__main__': - unittest.main() + def setUp(self): + self.nodes = {} + for x in ("a", "b", "c", "d", "e"): + self.nodes[x] = gyp.input.DependencyGraphNode(x) + + def _create_dependency(self, dependent, dependency): + dependent.dependencies.append(dependency) + dependency.dependents.append(dependent) + + def test_no_cycle_empty_graph(self): + for label, node in self.nodes.items(): + self.assertEqual([], node.FindCycles()) + + def test_no_cycle_line(self): + self._create_dependency(self.nodes["a"], self.nodes["b"]) + self._create_dependency(self.nodes["b"], self.nodes["c"]) + self._create_dependency(self.nodes["c"], self.nodes["d"]) + + for label, node in self.nodes.items(): + self.assertEqual([], node.FindCycles()) + + def test_no_cycle_dag(self): + self._create_dependency(self.nodes["a"], self.nodes["b"]) + self._create_dependency(self.nodes["a"], self.nodes["c"]) + self._create_dependency(self.nodes["b"], self.nodes["c"]) + + for label, node in self.nodes.items(): + self.assertEqual([], node.FindCycles()) + + def test_cycle_self_reference(self): + self._create_dependency(self.nodes["a"], self.nodes["a"]) + + self.assertEqual( + [[self.nodes["a"], self.nodes["a"]]], self.nodes["a"].FindCycles() + ) + + def test_cycle_two_nodes(self): + self._create_dependency(self.nodes["a"], self.nodes["b"]) + self._create_dependency(self.nodes["b"], self.nodes["a"]) + + self.assertEqual( + [[self.nodes["a"], self.nodes["b"], self.nodes["a"]]], + self.nodes["a"].FindCycles(), + ) + self.assertEqual( + [[self.nodes["b"], self.nodes["a"], self.nodes["b"]]], + self.nodes["b"].FindCycles(), + ) + + def test_two_cycles(self): + self._create_dependency(self.nodes["a"], self.nodes["b"]) + self._create_dependency(self.nodes["b"], self.nodes["a"]) + + self._create_dependency(self.nodes["b"], self.nodes["c"]) + self._create_dependency(self.nodes["c"], self.nodes["b"]) + + cycles = self.nodes["a"].FindCycles() + self.assertTrue([self.nodes["a"], self.nodes["b"], self.nodes["a"]] in cycles) + self.assertTrue([self.nodes["b"], self.nodes["c"], self.nodes["b"]] in cycles) + self.assertEqual(2, len(cycles)) + + def test_big_cycle(self): + self._create_dependency(self.nodes["a"], self.nodes["b"]) + self._create_dependency(self.nodes["b"], self.nodes["c"]) + self._create_dependency(self.nodes["c"], self.nodes["d"]) + self._create_dependency(self.nodes["d"], self.nodes["e"]) + self._create_dependency(self.nodes["e"], self.nodes["a"]) + + self.assertEqual( + [ + [ + self.nodes["a"], + self.nodes["b"], + self.nodes["c"], + self.nodes["d"], + self.nodes["e"], + self.nodes["a"], + ] + ], + self.nodes["a"].FindCycles(), + ) + + +if __name__ == "__main__": + unittest.main() diff --git a/tools/gyp/pylib/gyp/mac_tool.py b/tools/gyp/pylib/gyp/mac_tool.py index 781a8633bc2c7f..07412578d19a24 100755 --- a/tools/gyp/pylib/gyp/mac_tool.py +++ b/tools/gyp/pylib/gyp/mac_tool.py @@ -18,7 +18,6 @@ import plistlib import re import shutil -import string import struct import subprocess import sys @@ -28,328 +27,345 @@ def main(args): - executor = MacTool() - exit_code = executor.Dispatch(args) - if exit_code is not None: - sys.exit(exit_code) + executor = MacTool() + exit_code = executor.Dispatch(args) + if exit_code is not None: + sys.exit(exit_code) class MacTool(object): - """This class performs all the Mac tooling steps. The methods can either be + """This class performs all the Mac tooling steps. The methods can either be executed directly, or dispatched from an argument list.""" - def Dispatch(self, args): - """Dispatches a string command to a method.""" - if len(args) < 1: - raise Exception("Not enough arguments") + def Dispatch(self, args): + """Dispatches a string command to a method.""" + if len(args) < 1: + raise Exception("Not enough arguments") - method = "Exec%s" % self._CommandifyName(args[0]) - return getattr(self, method)(*args[1:]) + method = "Exec%s" % self._CommandifyName(args[0]) + return getattr(self, method)(*args[1:]) - def _CommandifyName(self, name_string): - """Transforms a tool name like copy-info-plist to CopyInfoPlist""" - return name_string.title().replace('-', '') + def _CommandifyName(self, name_string): + """Transforms a tool name like copy-info-plist to CopyInfoPlist""" + return name_string.title().replace("-", "") - def ExecCopyBundleResource(self, source, dest, convert_to_binary): - """Copies a resource file to the bundle/Resources directory, performing any + def ExecCopyBundleResource(self, source, dest, convert_to_binary): + """Copies a resource file to the bundle/Resources directory, performing any necessary compilation on each resource.""" - convert_to_binary = convert_to_binary == 'True' - extension = os.path.splitext(source)[1].lower() - if os.path.isdir(source): - # Copy tree. - # TODO(thakis): This copies file attributes like mtime, while the - # single-file branch below doesn't. This should probably be changed to - # be consistent with the single-file branch. - if os.path.exists(dest): - shutil.rmtree(dest) - shutil.copytree(source, dest) - elif extension == '.xib': - return self._CopyXIBFile(source, dest) - elif extension == '.storyboard': - return self._CopyXIBFile(source, dest) - elif extension == '.strings' and not convert_to_binary: - self._CopyStringsFile(source, dest) - else: - if os.path.exists(dest): - os.unlink(dest) - shutil.copy(source, dest) - - if convert_to_binary and extension in ('.plist', '.strings'): - self._ConvertToBinary(dest) - - def _CopyXIBFile(self, source, dest): - """Compiles a XIB file with ibtool into a binary plist in the bundle.""" - - # ibtool sometimes crashes with relative paths. See crbug.com/314728. - base = os.path.dirname(os.path.realpath(__file__)) - if os.path.relpath(source): - source = os.path.join(base, source) - if os.path.relpath(dest): - dest = os.path.join(base, dest) - - args = ['xcrun', 'ibtool', '--errors', '--warnings', '--notices'] - - if os.environ['XCODE_VERSION_ACTUAL'] > '0700': - args.extend(['--auto-activate-custom-fonts']) - if 'IPHONEOS_DEPLOYMENT_TARGET' in os.environ: - args.extend([ - '--target-device', 'iphone', '--target-device', 'ipad', - '--minimum-deployment-target', - os.environ['IPHONEOS_DEPLOYMENT_TARGET'], - ]) - else: - args.extend([ - '--target-device', 'mac', - '--minimum-deployment-target', - os.environ['MACOSX_DEPLOYMENT_TARGET'], - ]) - - args.extend(['--output-format', 'human-readable-text', '--compile', dest, - source]) - - ibtool_section_re = re.compile(r'/\*.*\*/') - ibtool_re = re.compile(r'.*note:.*is clipping its content') - try: - stdout = subprocess.check_output(args) - except subprocess.CalledProcessError as e: - print(e.output) - raise - current_section_header = None - for line in stdout.splitlines(): - if ibtool_section_re.match(line): - current_section_header = line - elif not ibtool_re.match(line): - if current_section_header: - print(current_section_header) - current_section_header = None - print(line) - return 0 - - def _ConvertToBinary(self, dest): - subprocess.check_call([ - 'xcrun', 'plutil', '-convert', 'binary1', '-o', dest, dest]) - - def _CopyStringsFile(self, source, dest): - """Copies a .strings file using iconv to reconvert the input into UTF-16.""" - input_code = self._DetectInputEncoding(source) or "UTF-8" - - # Xcode's CpyCopyStringsFile / builtin-copyStrings seems to call - # CFPropertyListCreateFromXMLData() behind the scenes; at least it prints - # CFPropertyListCreateFromXMLData(): Old-style plist parser: missing - # semicolon in dictionary. - # on invalid files. Do the same kind of validation. - import CoreFoundation - with open(source, 'rb') as in_file: - s = in_file.read() - d = CoreFoundation.CFDataCreate(None, s, len(s)) - _, error = CoreFoundation.CFPropertyListCreateFromXMLData(None, d, 0, None) - if error: - return - - with open(dest, 'wb') as fp: - fp.write(s.decode(input_code).encode('UTF-16')) - - def _DetectInputEncoding(self, file_name): - """Reads the first few bytes from file_name and tries to guess the text + convert_to_binary = convert_to_binary == "True" + extension = os.path.splitext(source)[1].lower() + if os.path.isdir(source): + # Copy tree. + # TODO(thakis): This copies file attributes like mtime, while the + # single-file branch below doesn't. This should probably be changed to + # be consistent with the single-file branch. + if os.path.exists(dest): + shutil.rmtree(dest) + shutil.copytree(source, dest) + elif extension == ".xib": + return self._CopyXIBFile(source, dest) + elif extension == ".storyboard": + return self._CopyXIBFile(source, dest) + elif extension == ".strings" and not convert_to_binary: + self._CopyStringsFile(source, dest) + else: + if os.path.exists(dest): + os.unlink(dest) + shutil.copy(source, dest) + + if convert_to_binary and extension in (".plist", ".strings"): + self._ConvertToBinary(dest) + + def _CopyXIBFile(self, source, dest): + """Compiles a XIB file with ibtool into a binary plist in the bundle.""" + + # ibtool sometimes crashes with relative paths. See crbug.com/314728. + base = os.path.dirname(os.path.realpath(__file__)) + if os.path.relpath(source): + source = os.path.join(base, source) + if os.path.relpath(dest): + dest = os.path.join(base, dest) + + args = ["xcrun", "ibtool", "--errors", "--warnings", "--notices"] + + if os.environ["XCODE_VERSION_ACTUAL"] > "0700": + args.extend(["--auto-activate-custom-fonts"]) + if "IPHONEOS_DEPLOYMENT_TARGET" in os.environ: + args.extend( + [ + "--target-device", + "iphone", + "--target-device", + "ipad", + "--minimum-deployment-target", + os.environ["IPHONEOS_DEPLOYMENT_TARGET"], + ] + ) + else: + args.extend( + [ + "--target-device", + "mac", + "--minimum-deployment-target", + os.environ["MACOSX_DEPLOYMENT_TARGET"], + ] + ) + + args.extend( + ["--output-format", "human-readable-text", "--compile", dest, source] + ) + + ibtool_section_re = re.compile(r"/\*.*\*/") + ibtool_re = re.compile(r".*note:.*is clipping its content") + try: + stdout = subprocess.check_output(args) + except subprocess.CalledProcessError as e: + print(e.output) + raise + current_section_header = None + for line in stdout.splitlines(): + if ibtool_section_re.match(line): + current_section_header = line + elif not ibtool_re.match(line): + if current_section_header: + print(current_section_header) + current_section_header = None + print(line) + return 0 + + def _ConvertToBinary(self, dest): + subprocess.check_call( + ["xcrun", "plutil", "-convert", "binary1", "-o", dest, dest] + ) + + def _CopyStringsFile(self, source, dest): + """Copies a .strings file using iconv to reconvert the input into UTF-16.""" + input_code = self._DetectInputEncoding(source) or "UTF-8" + + # Xcode's CpyCopyStringsFile / builtin-copyStrings seems to call + # CFPropertyListCreateFromXMLData() behind the scenes; at least it prints + # CFPropertyListCreateFromXMLData(): Old-style plist parser: missing + # semicolon in dictionary. + # on invalid files. Do the same kind of validation. + import CoreFoundation + + with open(source, "rb") as in_file: + s = in_file.read() + d = CoreFoundation.CFDataCreate(None, s, len(s)) + _, error = CoreFoundation.CFPropertyListCreateFromXMLData(None, d, 0, None) + if error: + return + + with open(dest, "wb") as fp: + fp.write(s.decode(input_code).encode("UTF-16")) + + def _DetectInputEncoding(self, file_name): + """Reads the first few bytes from file_name and tries to guess the text encoding. Returns None as a guess if it can't detect it.""" - with open(file_name, 'rb') as fp: - try: - header = fp.read(3) - except Exception: - return None - if header.startswith(("\xFE\xFF", "\xFF\xFE")): - return "UTF-16" - elif header.startswith("\xEF\xBB\xBF"): - return "UTF-8" - else: - return None - - def ExecCopyInfoPlist(self, source, dest, convert_to_binary, *keys): - """Copies the |source| Info.plist to the destination directory |dest|.""" - # Read the source Info.plist into memory. - with open(source, 'r') as fd: - lines = fd.read() - - # Insert synthesized key/value pairs (e.g. BuildMachineOSBuild). - plist = plistlib.readPlistFromString(lines) - if keys: - plist = dict(plist.items() + json.loads(keys[0]).items()) - lines = plistlib.writePlistToString(plist) - - # Go through all the environment variables and replace them as variables in - # the file. - IDENT_RE = re.compile(r'[_/\s]') - for key in os.environ: - if key.startswith('_'): - continue - evar = '${%s}' % key - evalue = os.environ[key] - lines = string.replace(lines, evar, evalue) - - # Xcode supports various suffices on environment variables, which are - # all undocumented. :rfc1034identifier is used in the standard project - # template these days, and :identifier was used earlier. They are used to - # convert non-url characters into things that look like valid urls -- - # except that the replacement character for :identifier, '_' isn't valid - # in a URL either -- oops, hence :rfc1034identifier was born. - evar = '${%s:identifier}' % key - evalue = IDENT_RE.sub('_', os.environ[key]) - lines = string.replace(lines, evar, evalue) - - evar = '${%s:rfc1034identifier}' % key - evalue = IDENT_RE.sub('-', os.environ[key]) - lines = string.replace(lines, evar, evalue) - - # Remove any keys with values that haven't been replaced. - lines = lines.splitlines() - for i in range(len(lines)): - if lines[i].strip().startswith("${"): - lines[i] = None - lines[i - 1] = None - lines = '\n'.join(line for line in lines if line is not None) - - # Write out the file with variables replaced. - with open(dest, 'w') as fd: - fd.write(lines) - - # Now write out PkgInfo file now that the Info.plist file has been - # "compiled". - self._WritePkgInfo(dest) - - if convert_to_binary == 'True': - self._ConvertToBinary(dest) - - def _WritePkgInfo(self, info_plist): - """This writes the PkgInfo file from the data stored in Info.plist.""" - plist = plistlib.readPlist(info_plist) - if not plist: - return - - # Only create PkgInfo for executable types. - package_type = plist['CFBundlePackageType'] - if package_type != 'APPL': - return - - # The format of PkgInfo is eight characters, representing the bundle type - # and bundle signature, each four characters. If that is missing, four - # '?' characters are used instead. - signature_code = plist.get('CFBundleSignature', '????') - if len(signature_code) != 4: # Wrong length resets everything, too. - signature_code = '?' * 4 - - dest = os.path.join(os.path.dirname(info_plist), 'PkgInfo') - with open(dest, 'w') as fp: - fp.write('%s%s' % (package_type, signature_code)) - - def ExecFlock(self, lockfile, *cmd_list): - """Emulates the most basic behavior of Linux's flock(1).""" - # Rely on exception handling to report errors. - fd = os.open(lockfile, os.O_RDONLY|os.O_NOCTTY|os.O_CREAT, 0o666) - fcntl.flock(fd, fcntl.LOCK_EX) - return subprocess.call(cmd_list) - - def ExecFilterLibtool(self, *cmd_list): - """Calls libtool and filters out '/path/to/libtool: file: foo.o has no + with open(file_name, "rb") as fp: + try: + header = fp.read(3) + except Exception: + return None + if header.startswith(b"\xFE\xFF"): + return "UTF-16" + elif header.startswith(b"\xFF\xFE"): + return "UTF-16" + elif header.startswith(b"\xEF\xBB\xBF"): + return "UTF-8" + else: + return None + + def ExecCopyInfoPlist(self, source, dest, convert_to_binary, *keys): + """Copies the |source| Info.plist to the destination directory |dest|.""" + # Read the source Info.plist into memory. + with open(source, "r") as fd: + lines = fd.read() + + # Insert synthesized key/value pairs (e.g. BuildMachineOSBuild). + plist = plistlib.readPlistFromString(lines) + if keys: + plist.update(json.loads(keys[0])) + lines = plistlib.writePlistToString(plist) + + # Go through all the environment variables and replace them as variables in + # the file. + IDENT_RE = re.compile(r"[_/\s]") + for key in os.environ: + if key.startswith("_"): + continue + evar = "${%s}" % key + evalue = os.environ[key] + lines = lines.replace(lines, evar, evalue) + + # Xcode supports various suffices on environment variables, which are + # all undocumented. :rfc1034identifier is used in the standard project + # template these days, and :identifier was used earlier. They are used to + # convert non-url characters into things that look like valid urls -- + # except that the replacement character for :identifier, '_' isn't valid + # in a URL either -- oops, hence :rfc1034identifier was born. + evar = "${%s:identifier}" % key + evalue = IDENT_RE.sub("_", os.environ[key]) + lines = lines.replace(lines, evar, evalue) + + evar = "${%s:rfc1034identifier}" % key + evalue = IDENT_RE.sub("-", os.environ[key]) + lines = lines.replace(lines, evar, evalue) + + # Remove any keys with values that haven't been replaced. + lines = lines.splitlines() + for i in range(len(lines)): + if lines[i].strip().startswith("${"): + lines[i] = None + lines[i - 1] = None + lines = "\n".join(line for line in lines if line is not None) + + # Write out the file with variables replaced. + with open(dest, "w") as fd: + fd.write(lines) + + # Now write out PkgInfo file now that the Info.plist file has been + # "compiled". + self._WritePkgInfo(dest) + + if convert_to_binary == "True": + self._ConvertToBinary(dest) + + def _WritePkgInfo(self, info_plist): + """This writes the PkgInfo file from the data stored in Info.plist.""" + plist = plistlib.readPlist(info_plist) + if not plist: + return + + # Only create PkgInfo for executable types. + package_type = plist["CFBundlePackageType"] + if package_type != "APPL": + return + + # The format of PkgInfo is eight characters, representing the bundle type + # and bundle signature, each four characters. If that is missing, four + # '?' characters are used instead. + signature_code = plist.get("CFBundleSignature", "????") + if len(signature_code) != 4: # Wrong length resets everything, too. + signature_code = "?" * 4 + + dest = os.path.join(os.path.dirname(info_plist), "PkgInfo") + with open(dest, "w") as fp: + fp.write("%s%s" % (package_type, signature_code)) + + def ExecFlock(self, lockfile, *cmd_list): + """Emulates the most basic behavior of Linux's flock(1).""" + # Rely on exception handling to report errors. + fd = os.open(lockfile, os.O_RDONLY | os.O_NOCTTY | os.O_CREAT, 0o666) + fcntl.flock(fd, fcntl.LOCK_EX) + return subprocess.call(cmd_list) + + def ExecFilterLibtool(self, *cmd_list): + """Calls libtool and filters out '/path/to/libtool: file: foo.o has no symbols'.""" - libtool_re = re.compile(r'^.*libtool: (?:for architecture: \S* )?' - r'file: .* has no symbols$') - libtool_re5 = re.compile( - r'^.*libtool: warning for library: ' + - r'.* the table of contents is empty ' + - r'\(no object file members in the library define global symbols\)$') - env = os.environ.copy() - # Ref: - # http://www.opensource.apple.com/source/cctools/cctools-809/misc/libtool.c - # The problem with this flag is that it resets the file mtime on the file to - # epoch=0, e.g. 1970-1-1 or 1969-12-31 depending on timezone. - env['ZERO_AR_DATE'] = '1' - libtoolout = subprocess.Popen(cmd_list, stderr=subprocess.PIPE, env=env) - _, err = libtoolout.communicate() - if PY3: - err = err.decode('utf-8') - for line in err.splitlines(): - if not libtool_re.match(line) and not libtool_re5.match(line): - print(line, file=sys.stderr) - # Unconditionally touch the output .a file on the command line if present - # and the command succeeded. A bit hacky. - if not libtoolout.returncode: - for i in range(len(cmd_list) - 1): - if cmd_list[i] == "-o" and cmd_list[i+1].endswith('.a'): - os.utime(cmd_list[i+1], None) - break - return libtoolout.returncode - - def ExecPackageIosFramework(self, framework): - # Find the name of the binary based on the part before the ".framework". - binary = os.path.basename(framework).split('.')[0] - module_path = os.path.join(framework, 'Modules') - if not os.path.exists(module_path): - os.mkdir(module_path) - module_template = 'framework module %s {\n' \ - ' umbrella header "%s.h"\n' \ - '\n' \ - ' export *\n' \ - ' module * { export * }\n' \ - '}\n' % (binary, binary) - - with open(os.path.join(module_path, 'module.modulemap'), "w") as module_file: - module_file.write(module_template) - - def ExecPackageFramework(self, framework, version): - """Takes a path to Something.framework and the Current version of that and + libtool_re = re.compile( + r"^.*libtool: (?:for architecture: \S* )?" r"file: .* has no symbols$" + ) + libtool_re5 = re.compile( + r"^.*libtool: warning for library: " + + r".* the table of contents is empty " + + r"\(no object file members in the library define global symbols\)$" + ) + env = os.environ.copy() + # Ref: + # http://www.opensource.apple.com/source/cctools/cctools-809/misc/libtool.c + # The problem with this flag is that it resets the file mtime on the file to + # epoch=0, e.g. 1970-1-1 or 1969-12-31 depending on timezone. + env["ZERO_AR_DATE"] = "1" + libtoolout = subprocess.Popen(cmd_list, stderr=subprocess.PIPE, env=env) + _, err = libtoolout.communicate() + if PY3: + err = err.decode("utf-8") + for line in err.splitlines(): + if not libtool_re.match(line) and not libtool_re5.match(line): + print(line, file=sys.stderr) + # Unconditionally touch the output .a file on the command line if present + # and the command succeeded. A bit hacky. + if not libtoolout.returncode: + for i in range(len(cmd_list) - 1): + if cmd_list[i] == "-o" and cmd_list[i + 1].endswith(".a"): + os.utime(cmd_list[i + 1], None) + break + return libtoolout.returncode + + def ExecPackageIosFramework(self, framework): + # Find the name of the binary based on the part before the ".framework". + binary = os.path.basename(framework).split(".")[0] + module_path = os.path.join(framework, "Modules") + if not os.path.exists(module_path): + os.mkdir(module_path) + module_template = ( + "framework module %s {\n" + ' umbrella header "%s.h"\n' + "\n" + " export *\n" + " module * { export * }\n" + "}\n" % (binary, binary) + ) + + with open(os.path.join(module_path, "module.modulemap"), "w") as module_file: + module_file.write(module_template) + + def ExecPackageFramework(self, framework, version): + """Takes a path to Something.framework and the Current version of that and sets up all the symlinks.""" - # Find the name of the binary based on the part before the ".framework". - binary = os.path.basename(framework).split('.')[0] + # Find the name of the binary based on the part before the ".framework". + binary = os.path.basename(framework).split(".")[0] - CURRENT = 'Current' - RESOURCES = 'Resources' - VERSIONS = 'Versions' + CURRENT = "Current" + RESOURCES = "Resources" + VERSIONS = "Versions" - if not os.path.exists(os.path.join(framework, VERSIONS, version, binary)): - # Binary-less frameworks don't seem to contain symlinks (see e.g. - # chromium's out/Debug/org.chromium.Chromium.manifest/ bundle). - return + if not os.path.exists(os.path.join(framework, VERSIONS, version, binary)): + # Binary-less frameworks don't seem to contain symlinks (see e.g. + # chromium's out/Debug/org.chromium.Chromium.manifest/ bundle). + return - # Move into the framework directory to set the symlinks correctly. - pwd = os.getcwd() - os.chdir(framework) + # Move into the framework directory to set the symlinks correctly. + pwd = os.getcwd() + os.chdir(framework) - # Set up the Current version. - self._Relink(version, os.path.join(VERSIONS, CURRENT)) + # Set up the Current version. + self._Relink(version, os.path.join(VERSIONS, CURRENT)) - # Set up the root symlinks. - self._Relink(os.path.join(VERSIONS, CURRENT, binary), binary) - self._Relink(os.path.join(VERSIONS, CURRENT, RESOURCES), RESOURCES) + # Set up the root symlinks. + self._Relink(os.path.join(VERSIONS, CURRENT, binary), binary) + self._Relink(os.path.join(VERSIONS, CURRENT, RESOURCES), RESOURCES) - # Back to where we were before! - os.chdir(pwd) + # Back to where we were before! + os.chdir(pwd) - def _Relink(self, dest, link): - """Creates a symlink to |dest| named |link|. If |link| already exists, + def _Relink(self, dest, link): + """Creates a symlink to |dest| named |link|. If |link| already exists, it is overwritten.""" - if os.path.lexists(link): - os.remove(link) - os.symlink(dest, link) - - def ExecCompileIosFrameworkHeaderMap(self, out, framework, *all_headers): - framework_name = os.path.basename(framework).split('.')[0] - all_headers = [os.path.abspath(header) for header in all_headers] - filelist = {} - for header in all_headers: - filename = os.path.basename(header) - filelist[filename] = header - filelist[os.path.join(framework_name, filename)] = header - WriteHmap(out, filelist) - - def ExecCopyIosFrameworkHeaders(self, framework, *copy_headers): - header_path = os.path.join(framework, 'Headers') - if not os.path.exists(header_path): - os.makedirs(header_path) - for header in copy_headers: - shutil.copy(header, os.path.join(header_path, os.path.basename(header))) - - def ExecCompileXcassets(self, keys, *inputs): - """Compiles multiple .xcassets files into a single .car file. + if os.path.lexists(link): + os.remove(link) + os.symlink(dest, link) + + def ExecCompileIosFrameworkHeaderMap(self, out, framework, *all_headers): + framework_name = os.path.basename(framework).split(".")[0] + all_headers = [os.path.abspath(header) for header in all_headers] + filelist = {} + for header in all_headers: + filename = os.path.basename(header) + filelist[filename] = header + filelist[os.path.join(framework_name, filename)] = header + WriteHmap(out, filelist) + + def ExecCopyIosFrameworkHeaders(self, framework, *copy_headers): + header_path = os.path.join(framework, "Headers") + if not os.path.exists(header_path): + os.makedirs(header_path) + for header in copy_headers: + shutil.copy(header, os.path.join(header_path, os.path.basename(header))) + + def ExecCompileXcassets(self, keys, *inputs): + """Compiles multiple .xcassets files into a single .car file. This invokes 'actool' to compile all the inputs .xcassets files. The |keys| arguments is a json-encoded dictionary of extra arguments to @@ -359,57 +375,77 @@ def ExecCompileXcassets(self, keys, *inputs): Note that 'actool' does not create the Assets.car file if the asset catalogs does not contains imageset. """ - command_line = [ - 'xcrun', 'actool', '--output-format', 'human-readable-text', - '--compress-pngs', '--notices', '--warnings', '--errors', - ] - is_iphone_target = 'IPHONEOS_DEPLOYMENT_TARGET' in os.environ - if is_iphone_target: - platform = os.environ['CONFIGURATION'].split('-')[-1] - if platform not in ('iphoneos', 'iphonesimulator'): - platform = 'iphonesimulator' - command_line.extend([ - '--platform', platform, '--target-device', 'iphone', - '--target-device', 'ipad', '--minimum-deployment-target', - os.environ['IPHONEOS_DEPLOYMENT_TARGET'], '--compile', - os.path.abspath(os.environ['CONTENTS_FOLDER_PATH']), - ]) - else: - command_line.extend([ - '--platform', 'macosx', '--target-device', 'mac', - '--minimum-deployment-target', os.environ['MACOSX_DEPLOYMENT_TARGET'], - '--compile', - os.path.abspath(os.environ['UNLOCALIZED_RESOURCES_FOLDER_PATH']), - ]) - if keys: - keys = json.loads(keys) - for key, value in keys.items(): - arg_name = '--' + key - if isinstance(value, bool): - if value: - command_line.append(arg_name) - elif isinstance(value, list): - for v in value: - command_line.append(arg_name) - command_line.append(str(v)) + command_line = [ + "xcrun", + "actool", + "--output-format", + "human-readable-text", + "--compress-pngs", + "--notices", + "--warnings", + "--errors", + ] + is_iphone_target = "IPHONEOS_DEPLOYMENT_TARGET" in os.environ + if is_iphone_target: + platform = os.environ["CONFIGURATION"].split("-")[-1] + if platform not in ("iphoneos", "iphonesimulator"): + platform = "iphonesimulator" + command_line.extend( + [ + "--platform", + platform, + "--target-device", + "iphone", + "--target-device", + "ipad", + "--minimum-deployment-target", + os.environ["IPHONEOS_DEPLOYMENT_TARGET"], + "--compile", + os.path.abspath(os.environ["CONTENTS_FOLDER_PATH"]), + ] + ) else: - command_line.append(arg_name) - command_line.append(str(value)) - # Note: actool crashes if inputs path are relative, so use os.path.abspath - # to get absolute path name for inputs. - command_line.extend(map(os.path.abspath, inputs)) - subprocess.check_call(command_line) - - def ExecMergeInfoPlist(self, output, *inputs): - """Merge multiple .plist files into a single .plist file.""" - merged_plist = {} - for path in inputs: - plist = self._LoadPlistMaybeBinary(path) - self._MergePlist(merged_plist, plist) - plistlib.writePlist(merged_plist, output) - - def ExecCodeSignBundle(self, key, entitlements, provisioning, path, preserve): - """Code sign a bundle. + command_line.extend( + [ + "--platform", + "macosx", + "--target-device", + "mac", + "--minimum-deployment-target", + os.environ["MACOSX_DEPLOYMENT_TARGET"], + "--compile", + os.path.abspath(os.environ["UNLOCALIZED_RESOURCES_FOLDER_PATH"]), + ] + ) + if keys: + keys = json.loads(keys) + for key, value in keys.items(): + arg_name = "--" + key + if isinstance(value, bool): + if value: + command_line.append(arg_name) + elif isinstance(value, list): + for v in value: + command_line.append(arg_name) + command_line.append(str(v)) + else: + command_line.append(arg_name) + command_line.append(str(value)) + # Note: actool crashes if inputs path are relative, so use os.path.abspath + # to get absolute path name for inputs. + command_line.extend(map(os.path.abspath, inputs)) + subprocess.check_call(command_line) + + def ExecMergeInfoPlist(self, output, *inputs): + """Merge multiple .plist files into a single .plist file.""" + merged_plist = {} + for path in inputs: + plist = self._LoadPlistMaybeBinary(path) + self._MergePlist(merged_plist, plist) + plistlib.writePlist(merged_plist, output) + + def ExecCodeSignBundle(self, key, entitlements, provisioning, path, preserve): + """Code sign a bundle. This function tries to code sign an iOS bundle, following the same algorithm as Xcode: @@ -418,21 +454,23 @@ def ExecCodeSignBundle(self, key, entitlements, provisioning, path, preserve): 2. copy Entitlements.plist from user or SDK next to the bundle, 3. code sign the bundle. """ - substitutions, overrides = self._InstallProvisioningProfile( - provisioning, self._GetCFBundleIdentifier()) - entitlements_path = self._InstallEntitlements( - entitlements, substitutions, overrides) - - args = ['codesign', '--force', '--sign', key] - if preserve == 'True': - args.extend(['--deep', '--preserve-metadata=identifier,entitlements']) - else: - args.extend(['--entitlements', entitlements_path]) - args.extend(['--timestamp=none', path]) - subprocess.check_call(args) - - def _InstallProvisioningProfile(self, profile, bundle_identifier): - """Installs embedded.mobileprovision into the bundle. + substitutions, overrides = self._InstallProvisioningProfile( + provisioning, self._GetCFBundleIdentifier() + ) + entitlements_path = self._InstallEntitlements( + entitlements, substitutions, overrides + ) + + args = ["codesign", "--force", "--sign", key] + if preserve == "True": + args.extend(["--deep", "--preserve-metadata=identifier,entitlements"]) + else: + args.extend(["--entitlements", entitlements_path]) + args.extend(["--timestamp=none", path]) + subprocess.check_call(args) + + def _InstallProvisioningProfile(self, profile, bundle_identifier): + """Installs embedded.mobileprovision into the bundle. Args: profile: string, optional, short name of the .mobileprovision file @@ -444,18 +482,20 @@ def _InstallProvisioningProfile(self, profile, bundle_identifier): A tuple containing two dictionary: variables substitutions and values to overrides when generating the entitlements file. """ - source_path, provisioning_data, team_id = self._FindProvisioningProfile( - profile, bundle_identifier) - target_path = os.path.join( - os.environ['BUILT_PRODUCTS_DIR'], - os.environ['CONTENTS_FOLDER_PATH'], - 'embedded.mobileprovision') - shutil.copy2(source_path, target_path) - substitutions = self._GetSubstitutions(bundle_identifier, team_id + '.') - return substitutions, provisioning_data['Entitlements'] - - def _FindProvisioningProfile(self, profile, bundle_identifier): - """Finds the .mobileprovision file to use for signing the bundle. + source_path, provisioning_data, team_id = self._FindProvisioningProfile( + profile, bundle_identifier + ) + target_path = os.path.join( + os.environ["BUILT_PRODUCTS_DIR"], + os.environ["CONTENTS_FOLDER_PATH"], + "embedded.mobileprovision", + ) + shutil.copy2(source_path, target_path) + substitutions = self._GetSubstitutions(bundle_identifier, team_id + ".") + return substitutions, provisioning_data["Entitlements"] + + def _FindProvisioningProfile(self, profile, bundle_identifier): + """Finds the .mobileprovision file to use for signing the bundle. Checks all the installed provisioning profiles (or if the user specified the PROVISIONING_PROFILE variable, only consult it) and select the most @@ -475,40 +515,52 @@ def _FindProvisioningProfile(self, profile, bundle_identifier): Raises: SystemExit: if no .mobileprovision can be used to sign the bundle. """ - profiles_dir = os.path.join( - os.environ['HOME'], 'Library', 'MobileDevice', 'Provisioning Profiles') - if not os.path.isdir(profiles_dir): - print('cannot find mobile provisioning for %s' % (bundle_identifier), file=sys.stderr) - sys.exit(1) - provisioning_profiles = None - if profile: - profile_path = os.path.join(profiles_dir, profile + '.mobileprovision') - if os.path.exists(profile_path): - provisioning_profiles = [profile_path] - if not provisioning_profiles: - provisioning_profiles = glob.glob( - os.path.join(profiles_dir, '*.mobileprovision')) - valid_provisioning_profiles = {} - for profile_path in provisioning_profiles: - profile_data = self._LoadProvisioningProfile(profile_path) - app_id_pattern = profile_data.get( - 'Entitlements', {}).get('application-identifier', '') - for team_identifier in profile_data.get('TeamIdentifier', []): - app_id = '%s.%s' % (team_identifier, bundle_identifier) - if fnmatch.fnmatch(app_id, app_id_pattern): - valid_provisioning_profiles[app_id_pattern] = ( - profile_path, profile_data, team_identifier) - if not valid_provisioning_profiles: - print('cannot find mobile provisioning for %s' % (bundle_identifier), file=sys.stderr) - sys.exit(1) - # If the user has multiple provisioning profiles installed that can be - # used for ${bundle_identifier}, pick the most specific one (ie. the - # provisioning profile whose pattern is the longest). - selected_key = max(valid_provisioning_profiles, key=lambda v: len(v)) - return valid_provisioning_profiles[selected_key] - - def _LoadProvisioningProfile(self, profile_path): - """Extracts the plist embedded in a provisioning profile. + profiles_dir = os.path.join( + os.environ["HOME"], "Library", "MobileDevice", "Provisioning Profiles" + ) + if not os.path.isdir(profiles_dir): + print( + "cannot find mobile provisioning for %s" % (bundle_identifier), + file=sys.stderr, + ) + sys.exit(1) + provisioning_profiles = None + if profile: + profile_path = os.path.join(profiles_dir, profile + ".mobileprovision") + if os.path.exists(profile_path): + provisioning_profiles = [profile_path] + if not provisioning_profiles: + provisioning_profiles = glob.glob( + os.path.join(profiles_dir, "*.mobileprovision") + ) + valid_provisioning_profiles = {} + for profile_path in provisioning_profiles: + profile_data = self._LoadProvisioningProfile(profile_path) + app_id_pattern = profile_data.get("Entitlements", {}).get( + "application-identifier", "" + ) + for team_identifier in profile_data.get("TeamIdentifier", []): + app_id = "%s.%s" % (team_identifier, bundle_identifier) + if fnmatch.fnmatch(app_id, app_id_pattern): + valid_provisioning_profiles[app_id_pattern] = ( + profile_path, + profile_data, + team_identifier, + ) + if not valid_provisioning_profiles: + print( + "cannot find mobile provisioning for %s" % (bundle_identifier), + file=sys.stderr, + ) + sys.exit(1) + # If the user has multiple provisioning profiles installed that can be + # used for ${bundle_identifier}, pick the most specific one (ie. the + # provisioning profile whose pattern is the longest). + selected_key = max(valid_provisioning_profiles, key=lambda v: len(v)) + return valid_provisioning_profiles[selected_key] + + def _LoadProvisioningProfile(self, profile_path): + """Extracts the plist embedded in a provisioning profile. Args: profile_path: string, path to the .mobileprovision file @@ -516,26 +568,27 @@ def _LoadProvisioningProfile(self, profile_path): Returns: Content of the plist embedded in the provisioning profile as a dictionary. """ - with tempfile.NamedTemporaryFile() as temp: - subprocess.check_call([ - 'security', 'cms', '-D', '-i', profile_path, '-o', temp.name]) - return self._LoadPlistMaybeBinary(temp.name) - - def _MergePlist(self, merged_plist, plist): - """Merge |plist| into |merged_plist|.""" - for key, value in plist.items(): - if isinstance(value, dict): - merged_value = merged_plist.get(key, {}) - if isinstance(merged_value, dict): - self._MergePlist(merged_value, value) - merged_plist[key] = merged_value - else: - merged_plist[key] = value - else: - merged_plist[key] = value - - def _LoadPlistMaybeBinary(self, plist_path): - """Loads into a memory a plist possibly encoded in binary format. + with tempfile.NamedTemporaryFile() as temp: + subprocess.check_call( + ["security", "cms", "-D", "-i", profile_path, "-o", temp.name] + ) + return self._LoadPlistMaybeBinary(temp.name) + + def _MergePlist(self, merged_plist, plist): + """Merge |plist| into |merged_plist|.""" + for key, value in plist.items(): + if isinstance(value, dict): + merged_value = merged_plist.get(key, {}) + if isinstance(merged_value, dict): + self._MergePlist(merged_value, value) + merged_plist[key] = merged_value + else: + merged_plist[key] = value + else: + merged_plist[key] = value + + def _LoadPlistMaybeBinary(self, plist_path): + """Loads into a memory a plist possibly encoded in binary format. This is a wrapper around plistlib.readPlist that tries to convert the plist to the XML format if it can't be parsed (assuming that it is in @@ -547,20 +600,20 @@ def _LoadPlistMaybeBinary(self, plist_path): Returns: Content of the plist as a dictionary. """ - try: - # First, try to read the file using plistlib that only supports XML, - # and if an exception is raised, convert a temporary copy to XML and - # load that copy. - return plistlib.readPlist(plist_path) - except: - pass - with tempfile.NamedTemporaryFile() as temp: - shutil.copy2(plist_path, temp.name) - subprocess.check_call(['plutil', '-convert', 'xml1', temp.name]) - return plistlib.readPlist(temp.name) - - def _GetSubstitutions(self, bundle_identifier, app_identifier_prefix): - """Constructs a dictionary of variable substitutions for Entitlements.plist. + try: + # First, try to read the file using plistlib that only supports XML, + # and if an exception is raised, convert a temporary copy to XML and + # load that copy. + return plistlib.readPlist(plist_path) + except Exception: + pass + with tempfile.NamedTemporaryFile() as temp: + shutil.copy2(plist_path, temp.name) + subprocess.check_call(["plutil", "-convert", "xml1", temp.name]) + return plistlib.readPlist(temp.name) + + def _GetSubstitutions(self, bundle_identifier, app_identifier_prefix): + """Constructs a dictionary of variable substitutions for Entitlements.plist. Args: bundle_identifier: string, value of CFBundleIdentifier from Info.plist @@ -569,25 +622,25 @@ def _GetSubstitutions(self, bundle_identifier, app_identifier_prefix): Returns: Dictionary of substitutions to apply when generating Entitlements.plist. """ - return { - 'CFBundleIdentifier': bundle_identifier, - 'AppIdentifierPrefix': app_identifier_prefix, - } + return { + "CFBundleIdentifier": bundle_identifier, + "AppIdentifierPrefix": app_identifier_prefix, + } - def _GetCFBundleIdentifier(self): - """Extracts CFBundleIdentifier value from Info.plist in the bundle. + def _GetCFBundleIdentifier(self): + """Extracts CFBundleIdentifier value from Info.plist in the bundle. Returns: Value of CFBundleIdentifier in the Info.plist located in the bundle. """ - info_plist_path = os.path.join( - os.environ['TARGET_BUILD_DIR'], - os.environ['INFOPLIST_PATH']) - info_plist_data = self._LoadPlistMaybeBinary(info_plist_path) - return info_plist_data['CFBundleIdentifier'] + info_plist_path = os.path.join( + os.environ["TARGET_BUILD_DIR"], os.environ["INFOPLIST_PATH"] + ) + info_plist_data = self._LoadPlistMaybeBinary(info_plist_path) + return info_plist_data["CFBundleIdentifier"] - def _InstallEntitlements(self, entitlements, substitutions, overrides): - """Generates and install the ${BundleName}.xcent entitlements file. + def _InstallEntitlements(self, entitlements, substitutions, overrides): + """Generates and install the ${BundleName}.xcent entitlements file. Expands variables "$(variable)" pattern in the source entitlements file, add extra entitlements defined in the .mobileprovision file and the copy @@ -602,26 +655,24 @@ def _InstallEntitlements(self, entitlements, substitutions, overrides): Returns: Path to the generated entitlements file. """ - source_path = entitlements - target_path = os.path.join( - os.environ['BUILT_PRODUCTS_DIR'], - os.environ['PRODUCT_NAME'] + '.xcent') - if not source_path: - source_path = os.path.join( - os.environ['SDKROOT'], - 'Entitlements.plist') - shutil.copy2(source_path, target_path) - data = self._LoadPlistMaybeBinary(target_path) - data = self._ExpandVariables(data, substitutions) - if overrides: - for key in overrides: - if key not in data: - data[key] = overrides[key] - plistlib.writePlist(data, target_path) - return target_path - - def _ExpandVariables(self, data, substitutions): - """Expands variables "$(variable)" in data. + source_path = entitlements + target_path = os.path.join( + os.environ["BUILT_PRODUCTS_DIR"], os.environ["PRODUCT_NAME"] + ".xcent" + ) + if not source_path: + source_path = os.path.join(os.environ["SDKROOT"], "Entitlements.plist") + shutil.copy2(source_path, target_path) + data = self._LoadPlistMaybeBinary(target_path) + data = self._ExpandVariables(data, substitutions) + if overrides: + for key in overrides: + if key not in data: + data[key] = overrides[key] + plistlib.writePlist(data, target_path) + return target_path + + def _ExpandVariables(self, data, substitutions): + """Expands variables "$(variable)" in data. Args: data: object, can be either string, list or dictionary @@ -632,21 +683,23 @@ def _ExpandVariables(self, data, substitutions): by the corresponding value found in substitutions, or left intact if the key was not found. """ - if isinstance(data, str): - for key, value in substitutions.items(): - data = data.replace('$(%s)' % key, value) - return data - if isinstance(data, list): - return [self._ExpandVariables(v, substitutions) for v in data] - if isinstance(data, dict): - return {k: self._ExpandVariables(data[k], substitutions) for k in data} - return data + if isinstance(data, str): + for key, value in substitutions.items(): + data = data.replace("$(%s)" % key, value) + return data + if isinstance(data, list): + return [self._ExpandVariables(v, substitutions) for v in data] + if isinstance(data, dict): + return {k: self._ExpandVariables(data[k], substitutions) for k in data} + return data + def NextGreaterPowerOf2(x): - return 2**(x).bit_length() + return 2 ** (x).bit_length() + def WriteHmap(output_name, filelist): - """Generates a header map based on |filelist|. + """Generates a header map based on |filelist|. Per Mark Mentovai: A header map is structured essentially as a hash table, keyed by names used @@ -657,56 +710,67 @@ def WriteHmap(output_name, filelist): while also looking at the implementation in clang in: https://llvm.org/svn/llvm-project/cfe/trunk/lib/Lex/HeaderMap.cpp """ - magic = 1751998832 - version = 1 - _reserved = 0 - count = len(filelist) - capacity = NextGreaterPowerOf2(count) - strings_offset = 24 + (12 * capacity) - max_value_length = max(len(value) for value in filelist.values()) - - out = open(output_name, "wb") - out.write(struct.pack(' 0 or arg.count('/') > 1: - arg = os.path.normpath(arg) + # Use a heuristic to try to find args that are paths, and normalize them + if arg.find("/") > 0 or arg.count("/") > 1: + arg = os.path.normpath(arg) - # For a literal quote, CommandLineToArgvW requires 2n+1 backslashes - # preceding it, and results in n backslashes + the quote. So we substitute - # in 2* what we match, +1 more, plus the quote. - arg = windows_quoter_regex.sub(lambda mo: 2 * mo.group(1) + '\\"', arg) + # For a literal quote, CommandLineToArgvW requires 2n+1 backslashes + # preceding it, and results in n backslashes + the quote. So we substitute + # in 2* what we match, +1 more, plus the quote. + arg = windows_quoter_regex.sub(lambda mo: 2 * mo.group(1) + '\\"', arg) - # %'s also need to be doubled otherwise they're interpreted as batch - # positional arguments. Also make sure to escape the % so that they're - # passed literally through escaping so they can be singled to just the - # original %. Otherwise, trying to pass the literal representation that - # looks like an environment variable to the shell (e.g. %PATH%) would fail. - arg = arg.replace('%', '%%') + # %'s also need to be doubled otherwise they're interpreted as batch + # positional arguments. Also make sure to escape the % so that they're + # passed literally through escaping so they can be singled to just the + # original %. Otherwise, trying to pass the literal representation that + # looks like an environment variable to the shell (e.g. %PATH%) would fail. + arg = arg.replace("%", "%%") - # These commands are used in rsp files, so no escaping for the shell (via ^) - # is necessary. + # These commands are used in rsp files, so no escaping for the shell (via ^) + # is necessary. - # Finally, wrap the whole thing in quotes so that the above quote rule - # applies and whitespace isn't a word break. - return '"' + arg + '"' + # Finally, wrap the whole thing in quotes so that the above quote rule + # applies and whitespace isn't a word break. + return '"' + arg + '"' def EncodeRspFileList(args): - """Process a list of arguments using QuoteCmdExeArgument.""" - # Note that the first argument is assumed to be the command. Don't add - # quotes around it because then built-ins like 'echo', etc. won't work. - # Take care to normpath only the path in the case of 'call ../x.bat' because - # otherwise the whole thing is incorrectly interpreted as a path and not - # normalized correctly. - if not args: return '' - if args[0].startswith('call '): - call, program = args[0].split(' ', 1) - program = call + ' ' + os.path.normpath(program) - else: - program = os.path.normpath(args[0]) - return program + ' ' + ' '.join(QuoteForRspFile(arg) for arg in args[1:]) + """Process a list of arguments using QuoteCmdExeArgument.""" + # Note that the first argument is assumed to be the command. Don't add + # quotes around it because then built-ins like 'echo', etc. won't work. + # Take care to normpath only the path in the case of 'call ../x.bat' because + # otherwise the whole thing is incorrectly interpreted as a path and not + # normalized correctly. + if not args: + return "" + if args[0].startswith("call "): + call, program = args[0].split(" ", 1) + program = call + " " + os.path.normpath(program) + else: + program = os.path.normpath(args[0]) + return program + " " + " ".join(QuoteForRspFile(arg) for arg in args[1:]) def _GenericRetrieve(root, default, path): - """Given a list of dictionary keys |path| and a tree of dicts |root|, find + """Given a list of dictionary keys |path| and a tree of dicts |root|, find value at path, or return |default| if any of the path doesn't exist.""" - if not root: - return default - if not path: - return root - return _GenericRetrieve(root.get(path[0]), default, path[1:]) + if not root: + return default + if not path: + return root + return _GenericRetrieve(root.get(path[0]), default, path[1:]) def _AddPrefix(element, prefix): - """Add |prefix| to |element| or each subelement if element is iterable.""" - if element is None: - return element - # Note, not Iterable because we don't want to handle strings like that. - if isinstance(element, list) or isinstance(element, tuple): - return [prefix + e for e in element] - else: - return prefix + element + """Add |prefix| to |element| or each subelement if element is iterable.""" + if element is None: + return element + # Note, not Iterable because we don't want to handle strings like that. + if isinstance(element, list) or isinstance(element, tuple): + return [prefix + e for e in element] + else: + return prefix + element def _DoRemapping(element, map): - """If |element| then remap it through |map|. If |element| is iterable then + """If |element| then remap it through |map|. If |element| is iterable then each item will be remapped. Any elements not found will be removed.""" - if map is not None and element is not None: - if not callable(map): - map = map.get # Assume it's a dict, otherwise a callable to do the remap. - if isinstance(element, list) or isinstance(element, tuple): - element = filter(None, [map(elem) for elem in element]) - else: - element = map(element) - return element + if map is not None and element is not None: + if not callable(map): + map = map.get # Assume it's a dict, otherwise a callable to do the remap. + if isinstance(element, list) or isinstance(element, tuple): + element = filter(None, [map(elem) for elem in element]) + else: + element = map(element) + return element def _AppendOrReturn(append, element): - """If |append| is None, simply return |element|. If |append| is not None, + """If |append| is None, simply return |element|. If |append| is not None, then add |element| to it, adding each item in |element| if it's a list or tuple.""" - if append is not None and element is not None: - if isinstance(element, list) or isinstance(element, tuple): - append.extend(element) + if append is not None and element is not None: + if isinstance(element, list) or isinstance(element, tuple): + append.extend(element) + else: + append.append(element) else: - append.append(element) - else: - return element + return element def _FindDirectXInstallation(): - """Try to find an installation location for the DirectX SDK. Check for the + """Try to find an installation location for the DirectX SDK. Check for the standard environment variable, and if that doesn't exist, try to find via the registry. May return None if not found in either location.""" - # Return previously calculated value, if there is one - if hasattr(_FindDirectXInstallation, 'dxsdk_dir'): - return _FindDirectXInstallation.dxsdk_dir - - dxsdk_dir = os.environ.get('DXSDK_DIR') - if not dxsdk_dir: - # Setup params to pass to and attempt to launch reg.exe. - cmd = ['reg.exe', 'query', r'HKLM\Software\Microsoft\DirectX', '/s'] - p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) - stdout = p.communicate()[0] - if PY3: - stdout = stdout.decode('utf-8') - for line in stdout.splitlines(): - if 'InstallPath' in line: - dxsdk_dir = line.split(' ')[3] + "\\" - - # Cache return value - _FindDirectXInstallation.dxsdk_dir = dxsdk_dir - return dxsdk_dir + # Return previously calculated value, if there is one + if hasattr(_FindDirectXInstallation, "dxsdk_dir"): + return _FindDirectXInstallation.dxsdk_dir + + dxsdk_dir = os.environ.get("DXSDK_DIR") + if not dxsdk_dir: + # Setup params to pass to and attempt to launch reg.exe. + cmd = ["reg.exe", "query", r"HKLM\Software\Microsoft\DirectX", "/s"] + p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + stdout = p.communicate()[0] + if PY3: + stdout = stdout.decode("utf-8") + for line in stdout.splitlines(): + if "InstallPath" in line: + dxsdk_dir = line.split(" ")[3] + "\\" + + # Cache return value + _FindDirectXInstallation.dxsdk_dir = dxsdk_dir + return dxsdk_dir def GetGlobalVSMacroEnv(vs_version): - """Get a dict of variables mapping internal VS macro names to their gyp + """Get a dict of variables mapping internal VS macro names to their gyp equivalents. Returns all variables that are independent of the target.""" - env = {} - # '$(VSInstallDir)' and '$(VCInstallDir)' are available when and only when - # Visual Studio is actually installed. - if vs_version.Path(): - env['$(VSInstallDir)'] = vs_version.Path() - env['$(VCInstallDir)'] = os.path.join(vs_version.Path(), 'VC') + '\\' - # Chromium uses DXSDK_DIR in include/lib paths, but it may or may not be - # set. This happens when the SDK is sync'd via src-internal, rather than - # by typical end-user installation of the SDK. If it's not set, we don't - # want to leave the unexpanded variable in the path, so simply strip it. - dxsdk_dir = _FindDirectXInstallation() - env['$(DXSDK_DIR)'] = dxsdk_dir if dxsdk_dir else '' - # Try to find an installation location for the Windows DDK by checking - # the WDK_DIR environment variable, may be None. - env['$(WDK_DIR)'] = os.environ.get('WDK_DIR', '') - return env + env = {} + # '$(VSInstallDir)' and '$(VCInstallDir)' are available when and only when + # Visual Studio is actually installed. + if vs_version.Path(): + env["$(VSInstallDir)"] = vs_version.Path() + env["$(VCInstallDir)"] = os.path.join(vs_version.Path(), "VC") + "\\" + # Chromium uses DXSDK_DIR in include/lib paths, but it may or may not be + # set. This happens when the SDK is sync'd via src-internal, rather than + # by typical end-user installation of the SDK. If it's not set, we don't + # want to leave the unexpanded variable in the path, so simply strip it. + dxsdk_dir = _FindDirectXInstallation() + env["$(DXSDK_DIR)"] = dxsdk_dir if dxsdk_dir else "" + # Try to find an installation location for the Windows DDK by checking + # the WDK_DIR environment variable, may be None. + env["$(WDK_DIR)"] = os.environ.get("WDK_DIR", "") + return env + def ExtractSharedMSVSSystemIncludes(configs, generator_flags): - """Finds msvs_system_include_dirs that are common to all targets, removes + """Finds msvs_system_include_dirs that are common to all targets, removes them from all targets, and returns an OrderedSet containing them.""" - all_system_includes = OrderedSet( - configs[0].get('msvs_system_include_dirs', [])) - for config in configs[1:]: - system_includes = config.get('msvs_system_include_dirs', []) - all_system_includes = all_system_includes & OrderedSet(system_includes) - if not all_system_includes: - return None - # Expand macros in all_system_includes. - env = GetGlobalVSMacroEnv(GetVSVersion(generator_flags)) - expanded_system_includes = OrderedSet([ExpandMacros(include, env) - for include in all_system_includes]) - if any(['$' in include for include in expanded_system_includes]): - # Some path relies on target-specific variables, bail. - return None - - # Remove system includes shared by all targets from the targets. - for config in configs: - includes = config.get('msvs_system_include_dirs', []) - if includes: # Don't insert a msvs_system_include_dirs key if not needed. - # This must check the unexpanded includes list: - new_includes = [i for i in includes if i not in all_system_includes] - config['msvs_system_include_dirs'] = new_includes - return expanded_system_includes + all_system_includes = OrderedSet(configs[0].get("msvs_system_include_dirs", [])) + for config in configs[1:]: + system_includes = config.get("msvs_system_include_dirs", []) + all_system_includes = all_system_includes & OrderedSet(system_includes) + if not all_system_includes: + return None + # Expand macros in all_system_includes. + env = GetGlobalVSMacroEnv(GetVSVersion(generator_flags)) + expanded_system_includes = OrderedSet( + [ExpandMacros(include, env) for include in all_system_includes] + ) + if any(["$" in include for include in expanded_system_includes]): + # Some path relies on target-specific variables, bail. + return None + + # Remove system includes shared by all targets from the targets. + for config in configs: + includes = config.get("msvs_system_include_dirs", []) + if includes: # Don't insert a msvs_system_include_dirs key if not needed. + # This must check the unexpanded includes list: + new_includes = [i for i in includes if i not in all_system_includes] + config["msvs_system_include_dirs"] = new_includes + return expanded_system_includes class MsvsSettings(object): - """A class that understands the gyp 'msvs_...' values (especially the + """A class that understands the gyp 'msvs_...' values (especially the msvs_settings field). They largely correpond to the VS2008 IDE DOM. This class helps map those settings to command line options.""" - def __init__(self, spec, generator_flags): - self.spec = spec - self.vs_version = GetVSVersion(generator_flags) - - supported_fields = [ - ('msvs_configuration_attributes', dict), - ('msvs_settings', dict), - ('msvs_system_include_dirs', list), - ('msvs_disabled_warnings', list), - ('msvs_precompiled_header', str), - ('msvs_precompiled_source', str), - ('msvs_configuration_platform', str), - ('msvs_target_platform', str), + def __init__(self, spec, generator_flags): + self.spec = spec + self.vs_version = GetVSVersion(generator_flags) + + supported_fields = [ + ("msvs_configuration_attributes", dict), + ("msvs_settings", dict), + ("msvs_system_include_dirs", list), + ("msvs_disabled_warnings", list), + ("msvs_precompiled_header", str), + ("msvs_precompiled_source", str), + ("msvs_configuration_platform", str), + ("msvs_target_platform", str), + ] + configs = spec["configurations"] + for field, default in supported_fields: + setattr(self, field, {}) + for configname, config in configs.items(): + getattr(self, field)[configname] = config.get(field, default()) + + self.msvs_cygwin_dirs = spec.get("msvs_cygwin_dirs", ["."]) + + unsupported_fields = [ + "msvs_prebuild", + "msvs_postbuild", ] - configs = spec['configurations'] - for field, default in supported_fields: - setattr(self, field, {}) - for configname, config in configs.items(): - getattr(self, field)[configname] = config.get(field, default()) - - self.msvs_cygwin_dirs = spec.get('msvs_cygwin_dirs', ['.']) - - unsupported_fields = [ - 'msvs_prebuild', - 'msvs_postbuild', - ] - unsupported = [] - for field in unsupported_fields: - for config in configs.values(): - if field in config: - unsupported += ["%s not supported (target %s)." % - (field, spec['target_name'])] - if unsupported: - raise Exception('\n'.join(unsupported)) - - def GetExtension(self): - """Returns the extension for the target, with no leading dot. + unsupported = [] + for field in unsupported_fields: + for config in configs.values(): + if field in config: + unsupported += [ + "%s not supported (target %s)." % (field, spec["target_name"]) + ] + if unsupported: + raise Exception("\n".join(unsupported)) + + def GetExtension(self): + """Returns the extension for the target, with no leading dot. Uses 'product_extension' if specified, otherwise uses MSVS defaults based on the target type. """ - ext = self.spec.get('product_extension', None) - if ext: - return ext - return gyp.MSVSUtil.TARGET_TYPE_EXT.get(self.spec['type'], '') + ext = self.spec.get("product_extension", None) + if ext: + return ext + return gyp.MSVSUtil.TARGET_TYPE_EXT.get(self.spec["type"], "") - def GetVSMacroEnv(self, base_to_build=None, config=None): - """Get a dict of variables mapping internal VS macro names to their gyp + def GetVSMacroEnv(self, base_to_build=None, config=None): + """Get a dict of variables mapping internal VS macro names to their gyp equivalents.""" - target_arch = self.GetArch(config) - if target_arch == 'x86': - target_platform = 'Win32' - else: - target_platform = target_arch - target_name = self.spec.get('product_prefix', '') + \ - self.spec.get('product_name', self.spec['target_name']) - target_dir = base_to_build + '\\' if base_to_build else '' - target_ext = '.' + self.GetExtension() - target_file_name = target_name + target_ext - - replacements = { - '$(InputName)': '${root}', - '$(InputPath)': '${source}', - '$(IntDir)': '$!INTERMEDIATE_DIR', - '$(OutDir)\\': target_dir, - '$(PlatformName)': target_platform, - '$(ProjectDir)\\': '', - '$(ProjectName)': self.spec['target_name'], - '$(TargetDir)\\': target_dir, - '$(TargetExt)': target_ext, - '$(TargetFileName)': target_file_name, - '$(TargetName)': target_name, - '$(TargetPath)': os.path.join(target_dir, target_file_name), - } - replacements.update(GetGlobalVSMacroEnv(self.vs_version)) - return replacements - - def ConvertVSMacros(self, s, base_to_build=None, config=None): - """Convert from VS macro names to something equivalent.""" - env = self.GetVSMacroEnv(base_to_build, config=config) - return ExpandMacros(s, env) - - def AdjustLibraries(self, libraries): - """Strip -l from library if it's specified with that.""" - libs = [lib[2:] if lib.startswith('-l') else lib for lib in libraries] - return [lib + '.lib' if not lib.lower().endswith('.lib') \ - and not lib.lower().endswith('.obj') else lib for lib in libs] - - def _GetAndMunge(self, field, path, default, prefix, append, map): - """Retrieve a value from |field| at |path| or return |default|. If + target_arch = self.GetArch(config) + if target_arch == "x86": + target_platform = "Win32" + else: + target_platform = target_arch + target_name = self.spec.get("product_prefix", "") + self.spec.get( + "product_name", self.spec["target_name"] + ) + target_dir = base_to_build + "\\" if base_to_build else "" + target_ext = "." + self.GetExtension() + target_file_name = target_name + target_ext + + replacements = { + "$(InputName)": "${root}", + "$(InputPath)": "${source}", + "$(IntDir)": "$!INTERMEDIATE_DIR", + "$(OutDir)\\": target_dir, + "$(PlatformName)": target_platform, + "$(ProjectDir)\\": "", + "$(ProjectName)": self.spec["target_name"], + "$(TargetDir)\\": target_dir, + "$(TargetExt)": target_ext, + "$(TargetFileName)": target_file_name, + "$(TargetName)": target_name, + "$(TargetPath)": os.path.join(target_dir, target_file_name), + } + replacements.update(GetGlobalVSMacroEnv(self.vs_version)) + return replacements + + def ConvertVSMacros(self, s, base_to_build=None, config=None): + """Convert from VS macro names to something equivalent.""" + env = self.GetVSMacroEnv(base_to_build, config=config) + return ExpandMacros(s, env) + + def AdjustLibraries(self, libraries): + """Strip -l from library if it's specified with that.""" + libs = [lib[2:] if lib.startswith("-l") else lib for lib in libraries] + return [ + lib + ".lib" + if not lib.lower().endswith(".lib") and not lib.lower().endswith(".obj") + else lib + for lib in libs + ] + + def _GetAndMunge(self, field, path, default, prefix, append, map): + """Retrieve a value from |field| at |path| or return |default|. If |append| is specified, and the item is found, it will be appended to that object instead of returned. If |map| is specified, results will be remapped through |map| before being returned or appended.""" - result = _GenericRetrieve(field, default, path) - result = _DoRemapping(result, map) - result = _AddPrefix(result, prefix) - return _AppendOrReturn(append, result) - - class _GetWrapper(object): - def __init__(self, parent, field, base_path, append=None): - self.parent = parent - self.field = field - self.base_path = [base_path] - self.append = append - def __call__(self, name, map=None, prefix='', default=None): - return self.parent._GetAndMunge(self.field, self.base_path + [name], - default=default, prefix=prefix, append=self.append, map=map) - - def GetArch(self, config): - """Get architecture based on msvs_configuration_platform and + result = _GenericRetrieve(field, default, path) + result = _DoRemapping(result, map) + result = _AddPrefix(result, prefix) + return _AppendOrReturn(append, result) + + class _GetWrapper(object): + def __init__(self, parent, field, base_path, append=None): + self.parent = parent + self.field = field + self.base_path = [base_path] + self.append = append + + def __call__(self, name, map=None, prefix="", default=None): + return self.parent._GetAndMunge( + self.field, + self.base_path + [name], + default=default, + prefix=prefix, + append=self.append, + map=map, + ) + + def GetArch(self, config): + """Get architecture based on msvs_configuration_platform and msvs_target_platform. Returns either 'x86' or 'x64'.""" - configuration_platform = self.msvs_configuration_platform.get(config, '') - platform = self.msvs_target_platform.get(config, '') - if not platform: # If no specific override, use the configuration's. - platform = configuration_platform - # Map from platform to architecture. - return {'Win32': 'x86', 'x64': 'x64', 'ARM64': 'arm64'}.get(platform, 'x86') - - def _TargetConfig(self, config): - """Returns the target-specific configuration.""" - # There's two levels of architecture/platform specification in VS. The - # first level is globally for the configuration (this is what we consider - # "the" config at the gyp level, which will be something like 'Debug' or - # 'Release'), VS2015 and later only use this level - if self.vs_version.short_name >= 2015: - return config - # and a second target-specific configuration, which is an - # override for the global one. |config| is remapped here to take into - # account the local target-specific overrides to the global configuration. - arch = self.GetArch(config) - if arch == 'x64' and not config.endswith('_x64'): - config += '_x64' - if arch == 'x86' and config.endswith('_x64'): - config = config.rsplit('_', 1)[0] - return config - - def _Setting(self, path, config, - default=None, prefix='', append=None, map=None): - """_GetAndMunge for msvs_settings.""" - return self._GetAndMunge( - self.msvs_settings[config], path, default, prefix, append, map) - - def _ConfigAttrib(self, path, config, - default=None, prefix='', append=None, map=None): - """_GetAndMunge for msvs_configuration_attributes.""" - return self._GetAndMunge( - self.msvs_configuration_attributes[config], - path, default, prefix, append, map) - - def AdjustIncludeDirs(self, include_dirs, config): - """Updates include_dirs to expand VS specific paths, and adds the system + configuration_platform = self.msvs_configuration_platform.get(config, "") + platform = self.msvs_target_platform.get(config, "") + if not platform: # If no specific override, use the configuration's. + platform = configuration_platform + # Map from platform to architecture. + return {"Win32": "x86", "x64": "x64", "ARM64": "arm64"}.get(platform, "x86") + + def _TargetConfig(self, config): + """Returns the target-specific configuration.""" + # There's two levels of architecture/platform specification in VS. The + # first level is globally for the configuration (this is what we consider + # "the" config at the gyp level, which will be something like 'Debug' or + # 'Release'), VS2015 and later only use this level + if self.vs_version.short_name >= 2015: + return config + # and a second target-specific configuration, which is an + # override for the global one. |config| is remapped here to take into + # account the local target-specific overrides to the global configuration. + arch = self.GetArch(config) + if arch == "x64" and not config.endswith("_x64"): + config += "_x64" + if arch == "x86" and config.endswith("_x64"): + config = config.rsplit("_", 1)[0] + return config + + def _Setting(self, path, config, default=None, prefix="", append=None, map=None): + """_GetAndMunge for msvs_settings.""" + return self._GetAndMunge( + self.msvs_settings[config], path, default, prefix, append, map + ) + + def _ConfigAttrib( + self, path, config, default=None, prefix="", append=None, map=None + ): + """_GetAndMunge for msvs_configuration_attributes.""" + return self._GetAndMunge( + self.msvs_configuration_attributes[config], + path, + default, + prefix, + append, + map, + ) + + def AdjustIncludeDirs(self, include_dirs, config): + """Updates include_dirs to expand VS specific paths, and adds the system include dirs used for platform SDK and similar.""" - config = self._TargetConfig(config) - includes = include_dirs + self.msvs_system_include_dirs[config] - includes.extend(self._Setting( - ('VCCLCompilerTool', 'AdditionalIncludeDirectories'), config, default=[])) - return [self.ConvertVSMacros(p, config=config) for p in includes] - - def AdjustMidlIncludeDirs(self, midl_include_dirs, config): - """Updates midl_include_dirs to expand VS specific paths, and adds the + config = self._TargetConfig(config) + includes = include_dirs + self.msvs_system_include_dirs[config] + includes.extend( + self._Setting( + ("VCCLCompilerTool", "AdditionalIncludeDirectories"), config, default=[] + ) + ) + return [self.ConvertVSMacros(p, config=config) for p in includes] + + def AdjustMidlIncludeDirs(self, midl_include_dirs, config): + """Updates midl_include_dirs to expand VS specific paths, and adds the system include dirs used for platform SDK and similar.""" - config = self._TargetConfig(config) - includes = midl_include_dirs + self.msvs_system_include_dirs[config] - includes.extend(self._Setting( - ('VCMIDLTool', 'AdditionalIncludeDirectories'), config, default=[])) - return [self.ConvertVSMacros(p, config=config) for p in includes] - - def GetComputedDefines(self, config): - """Returns the set of defines that are injected to the defines list based + config = self._TargetConfig(config) + includes = midl_include_dirs + self.msvs_system_include_dirs[config] + includes.extend( + self._Setting( + ("VCMIDLTool", "AdditionalIncludeDirectories"), config, default=[] + ) + ) + return [self.ConvertVSMacros(p, config=config) for p in includes] + + def GetComputedDefines(self, config): + """Returns the set of defines that are injected to the defines list based on other VS settings.""" - config = self._TargetConfig(config) - defines = [] - if self._ConfigAttrib(['CharacterSet'], config) == '1': - defines.extend(('_UNICODE', 'UNICODE')) - if self._ConfigAttrib(['CharacterSet'], config) == '2': - defines.append('_MBCS') - defines.extend(self._Setting( - ('VCCLCompilerTool', 'PreprocessorDefinitions'), config, default=[])) - return defines - - def GetCompilerPdbName(self, config, expand_special): - """Get the pdb file name that should be used for compiler invocations, or + config = self._TargetConfig(config) + defines = [] + if self._ConfigAttrib(["CharacterSet"], config) == "1": + defines.extend(("_UNICODE", "UNICODE")) + if self._ConfigAttrib(["CharacterSet"], config) == "2": + defines.append("_MBCS") + defines.extend( + self._Setting( + ("VCCLCompilerTool", "PreprocessorDefinitions"), config, default=[] + ) + ) + return defines + + def GetCompilerPdbName(self, config, expand_special): + """Get the pdb file name that should be used for compiler invocations, or None if there's no explicit name specified.""" - config = self._TargetConfig(config) - pdbname = self._Setting( - ('VCCLCompilerTool', 'ProgramDataBaseFileName'), config) - if pdbname: - pdbname = expand_special(self.ConvertVSMacros(pdbname)) - return pdbname - - def GetMapFileName(self, config, expand_special): - """Gets the explicitly overridden map file name for a target or returns None + config = self._TargetConfig(config) + pdbname = self._Setting(("VCCLCompilerTool", "ProgramDataBaseFileName"), config) + if pdbname: + pdbname = expand_special(self.ConvertVSMacros(pdbname)) + return pdbname + + def GetMapFileName(self, config, expand_special): + """Gets the explicitly overridden map file name for a target or returns None if it's not set.""" - config = self._TargetConfig(config) - map_file = self._Setting(('VCLinkerTool', 'MapFileName'), config) - if map_file: - map_file = expand_special(self.ConvertVSMacros(map_file, config=config)) - return map_file - - def GetOutputName(self, config, expand_special): - """Gets the explicitly overridden output name for a target or returns None + config = self._TargetConfig(config) + map_file = self._Setting(("VCLinkerTool", "MapFileName"), config) + if map_file: + map_file = expand_special(self.ConvertVSMacros(map_file, config=config)) + return map_file + + def GetOutputName(self, config, expand_special): + """Gets the explicitly overridden output name for a target or returns None if it's not overridden.""" - config = self._TargetConfig(config) - type = self.spec['type'] - root = 'VCLibrarianTool' if type == 'static_library' else 'VCLinkerTool' - # TODO(scottmg): Handle OutputDirectory without OutputFile. - output_file = self._Setting((root, 'OutputFile'), config) - if output_file: - output_file = expand_special(self.ConvertVSMacros( - output_file, config=config)) - return output_file - - def GetPDBName(self, config, expand_special, default): - """Gets the explicitly overridden pdb name for a target or returns + config = self._TargetConfig(config) + type = self.spec["type"] + root = "VCLibrarianTool" if type == "static_library" else "VCLinkerTool" + # TODO(scottmg): Handle OutputDirectory without OutputFile. + output_file = self._Setting((root, "OutputFile"), config) + if output_file: + output_file = expand_special( + self.ConvertVSMacros(output_file, config=config) + ) + return output_file + + def GetPDBName(self, config, expand_special, default): + """Gets the explicitly overridden pdb name for a target or returns default if it's not overridden, or if no pdb will be generated.""" - config = self._TargetConfig(config) - output_file = self._Setting(('VCLinkerTool', 'ProgramDatabaseFile'), config) - generate_debug_info = self._Setting( - ('VCLinkerTool', 'GenerateDebugInformation'), config) - if generate_debug_info == 'true': - if output_file: - return expand_special(self.ConvertVSMacros(output_file, config=config)) - else: - return default - else: - return None - - def GetNoImportLibrary(self, config): - """If NoImportLibrary: true, ninja will not expect the output to include + config = self._TargetConfig(config) + output_file = self._Setting(("VCLinkerTool", "ProgramDatabaseFile"), config) + generate_debug_info = self._Setting( + ("VCLinkerTool", "GenerateDebugInformation"), config + ) + if generate_debug_info == "true": + if output_file: + return expand_special(self.ConvertVSMacros(output_file, config=config)) + else: + return default + else: + return None + + def GetNoImportLibrary(self, config): + """If NoImportLibrary: true, ninja will not expect the output to include an import library.""" - config = self._TargetConfig(config) - noimplib = self._Setting(('NoImportLibrary',), config) - return noimplib == 'true' - - def GetAsmflags(self, config): - """Returns the flags that need to be added to ml invocations.""" - config = self._TargetConfig(config) - asmflags = [] - safeseh = self._Setting(('MASM', 'UseSafeExceptionHandlers'), config) - if safeseh == 'true': - asmflags.append('/safeseh') - return asmflags - - def GetCflags(self, config): - """Returns the flags that need to be added to .c and .cc compilations.""" - config = self._TargetConfig(config) - cflags = [] - cflags.extend(['/wd' + w for w in self.msvs_disabled_warnings[config]]) - cl = self._GetWrapper(self, self.msvs_settings[config], - 'VCCLCompilerTool', append=cflags) - cl('Optimization', - map={'0': 'd', '1': '1', '2': '2', '3': 'x'}, prefix='/O', default='2') - cl('InlineFunctionExpansion', prefix='/Ob') - cl('DisableSpecificWarnings', prefix='/wd') - cl('StringPooling', map={'true': '/GF'}) - cl('EnableFiberSafeOptimizations', map={'true': '/GT'}) - cl('OmitFramePointers', map={'false': '-', 'true': ''}, prefix='/Oy') - cl('EnableIntrinsicFunctions', map={'false': '-', 'true': ''}, prefix='/Oi') - cl('FavorSizeOrSpeed', map={'1': 't', '2': 's'}, prefix='/O') - cl('FloatingPointModel', - map={'0': 'precise', '1': 'strict', '2': 'fast'}, prefix='/fp:', - default='0') - cl('CompileAsManaged', map={'false': '', 'true': '/clr'}) - cl('WholeProgramOptimization', map={'true': '/GL'}) - cl('WarningLevel', prefix='/W') - cl('WarnAsError', map={'true': '/WX'}) - cl('CallingConvention', - map={'0': 'd', '1': 'r', '2': 'z', '3': 'v'}, prefix='/G') - cl('DebugInformationFormat', - map={'1': '7', '3': 'i', '4': 'I'}, prefix='/Z') - cl('RuntimeTypeInfo', map={'true': '/GR', 'false': '/GR-'}) - cl('EnableFunctionLevelLinking', map={'true': '/Gy', 'false': '/Gy-'}) - cl('MinimalRebuild', map={'true': '/Gm'}) - cl('BufferSecurityCheck', map={'true': '/GS', 'false': '/GS-'}) - cl('BasicRuntimeChecks', map={'1': 's', '2': 'u', '3': '1'}, prefix='/RTC') - cl('RuntimeLibrary', - map={'0': 'T', '1': 'Td', '2': 'D', '3': 'Dd'}, prefix='/M') - cl('ExceptionHandling', map={'1': 'sc','2': 'a'}, prefix='/EH') - cl('DefaultCharIsUnsigned', map={'true': '/J'}) - cl('TreatWChar_tAsBuiltInType', - map={'false': '-', 'true': ''}, prefix='/Zc:wchar_t') - cl('EnablePREfast', map={'true': '/analyze'}) - cl('AdditionalOptions', prefix='') - cl('EnableEnhancedInstructionSet', - map={'1': 'SSE', '2': 'SSE2', '3': 'AVX', '4': 'IA32', '5': 'AVX2'}, - prefix='/arch:') - cflags.extend(['/FI' + f for f in self._Setting( - ('VCCLCompilerTool', 'ForcedIncludeFiles'), config, default=[])]) - if self.vs_version.project_version >= 12.0: - # New flag introduced in VS2013 (project version 12.0) Forces writes to - # the program database (PDB) to be serialized through MSPDBSRV.EXE. - # https://msdn.microsoft.com/en-us/library/dn502518.aspx - cflags.append('/FS') - # ninja handles parallelism by itself, don't have the compiler do it too. - cflags = filter(lambda x: not x.startswith('/MP'), cflags) - return cflags - - def _GetPchFlags(self, config, extension): - """Get the flags to be added to the cflags for precompiled header support. + config = self._TargetConfig(config) + noimplib = self._Setting(("NoImportLibrary",), config) + return noimplib == "true" + + def GetAsmflags(self, config): + """Returns the flags that need to be added to ml invocations.""" + config = self._TargetConfig(config) + asmflags = [] + safeseh = self._Setting(("MASM", "UseSafeExceptionHandlers"), config) + if safeseh == "true": + asmflags.append("/safeseh") + return asmflags + + def GetCflags(self, config): + """Returns the flags that need to be added to .c and .cc compilations.""" + config = self._TargetConfig(config) + cflags = [] + cflags.extend(["/wd" + w for w in self.msvs_disabled_warnings[config]]) + cl = self._GetWrapper( + self, self.msvs_settings[config], "VCCLCompilerTool", append=cflags + ) + cl( + "Optimization", + map={"0": "d", "1": "1", "2": "2", "3": "x"}, + prefix="/O", + default="2", + ) + cl("InlineFunctionExpansion", prefix="/Ob") + cl("DisableSpecificWarnings", prefix="/wd") + cl("StringPooling", map={"true": "/GF"}) + cl("EnableFiberSafeOptimizations", map={"true": "/GT"}) + cl("OmitFramePointers", map={"false": "-", "true": ""}, prefix="/Oy") + cl("EnableIntrinsicFunctions", map={"false": "-", "true": ""}, prefix="/Oi") + cl("FavorSizeOrSpeed", map={"1": "t", "2": "s"}, prefix="/O") + cl( + "FloatingPointModel", + map={"0": "precise", "1": "strict", "2": "fast"}, + prefix="/fp:", + default="0", + ) + cl("CompileAsManaged", map={"false": "", "true": "/clr"}) + cl("WholeProgramOptimization", map={"true": "/GL"}) + cl("WarningLevel", prefix="/W") + cl("WarnAsError", map={"true": "/WX"}) + cl( + "CallingConvention", + map={"0": "d", "1": "r", "2": "z", "3": "v"}, + prefix="/G", + ) + cl("DebugInformationFormat", map={"1": "7", "3": "i", "4": "I"}, prefix="/Z") + cl("RuntimeTypeInfo", map={"true": "/GR", "false": "/GR-"}) + cl("EnableFunctionLevelLinking", map={"true": "/Gy", "false": "/Gy-"}) + cl("MinimalRebuild", map={"true": "/Gm"}) + cl("BufferSecurityCheck", map={"true": "/GS", "false": "/GS-"}) + cl("BasicRuntimeChecks", map={"1": "s", "2": "u", "3": "1"}, prefix="/RTC") + cl( + "RuntimeLibrary", + map={"0": "T", "1": "Td", "2": "D", "3": "Dd"}, + prefix="/M", + ) + cl("ExceptionHandling", map={"1": "sc", "2": "a"}, prefix="/EH") + cl("DefaultCharIsUnsigned", map={"true": "/J"}) + cl( + "TreatWChar_tAsBuiltInType", + map={"false": "-", "true": ""}, + prefix="/Zc:wchar_t", + ) + cl("EnablePREfast", map={"true": "/analyze"}) + cl("AdditionalOptions", prefix="") + cl( + "EnableEnhancedInstructionSet", + map={"1": "SSE", "2": "SSE2", "3": "AVX", "4": "IA32", "5": "AVX2"}, + prefix="/arch:", + ) + cflags.extend( + [ + "/FI" + f + for f in self._Setting( + ("VCCLCompilerTool", "ForcedIncludeFiles"), config, default=[] + ) + ] + ) + if self.vs_version.project_version >= 12.0: + # New flag introduced in VS2013 (project version 12.0) Forces writes to + # the program database (PDB) to be serialized through MSPDBSRV.EXE. + # https://msdn.microsoft.com/en-us/library/dn502518.aspx + cflags.append("/FS") + # ninja handles parallelism by itself, don't have the compiler do it too. + cflags = [x for x in cflags if not x.startswith("/MP")] + return cflags + + def _GetPchFlags(self, config, extension): + """Get the flags to be added to the cflags for precompiled header support. """ - config = self._TargetConfig(config) - # The PCH is only built once by a particular source file. Usage of PCH must - # only be for the same language (i.e. C vs. C++), so only include the pch - # flags when the language matches. - if self.msvs_precompiled_header[config]: - source_ext = os.path.splitext(self.msvs_precompiled_source[config])[1] - if _LanguageMatchesForPch(source_ext, extension): - pch = self.msvs_precompiled_header[config] - pchbase = os.path.split(pch)[1] - return ['/Yu' + pch, '/FI' + pch, '/Fp${pchprefix}.' + pchbase + '.pch'] - return [] - - def GetCflagsC(self, config): - """Returns the flags that need to be added to .c compilations.""" - config = self._TargetConfig(config) - return self._GetPchFlags(config, '.c') - - def GetCflagsCC(self, config): - """Returns the flags that need to be added to .cc compilations.""" - config = self._TargetConfig(config) - return ['/TP'] + self._GetPchFlags(config, '.cc') - - def _GetAdditionalLibraryDirectories(self, root, config, gyp_to_build_path): - """Get and normalize the list of paths in AdditionalLibraryDirectories + config = self._TargetConfig(config) + # The PCH is only built once by a particular source file. Usage of PCH must + # only be for the same language (i.e. C vs. C++), so only include the pch + # flags when the language matches. + if self.msvs_precompiled_header[config]: + source_ext = os.path.splitext(self.msvs_precompiled_source[config])[1] + if _LanguageMatchesForPch(source_ext, extension): + pch = self.msvs_precompiled_header[config] + pchbase = os.path.split(pch)[1] + return ["/Yu" + pch, "/FI" + pch, "/Fp${pchprefix}." + pchbase + ".pch"] + return [] + + def GetCflagsC(self, config): + """Returns the flags that need to be added to .c compilations.""" + config = self._TargetConfig(config) + return self._GetPchFlags(config, ".c") + + def GetCflagsCC(self, config): + """Returns the flags that need to be added to .cc compilations.""" + config = self._TargetConfig(config) + return ["/TP"] + self._GetPchFlags(config, ".cc") + + def _GetAdditionalLibraryDirectories(self, root, config, gyp_to_build_path): + """Get and normalize the list of paths in AdditionalLibraryDirectories setting.""" - config = self._TargetConfig(config) - libpaths = self._Setting((root, 'AdditionalLibraryDirectories'), - config, default=[]) - libpaths = [os.path.normpath( - gyp_to_build_path(self.ConvertVSMacros(p, config=config))) - for p in libpaths] - return ['/LIBPATH:"' + p + '"' for p in libpaths] - - def GetLibFlags(self, config, gyp_to_build_path): - """Returns the flags that need to be added to lib commands.""" - config = self._TargetConfig(config) - libflags = [] - lib = self._GetWrapper(self, self.msvs_settings[config], - 'VCLibrarianTool', append=libflags) - libflags.extend(self._GetAdditionalLibraryDirectories( - 'VCLibrarianTool', config, gyp_to_build_path)) - lib('LinkTimeCodeGeneration', map={'true': '/LTCG'}) - lib('TargetMachine', map={'1': 'X86', '17': 'X64', '3': 'ARM'}, - prefix='/MACHINE:') - lib('AdditionalOptions') - return libflags - - def GetDefFile(self, gyp_to_build_path): - """Returns the .def file from sources, if any. Otherwise returns None.""" - spec = self.spec - if spec['type'] in ('shared_library', 'loadable_module', 'executable'): - def_files = [s for s in spec.get('sources', []) - if s.lower().endswith('.def')] - if len(def_files) == 1: - return gyp_to_build_path(def_files[0]) - elif len(def_files) > 1: - raise Exception("Multiple .def files") - return None - - def _GetDefFileAsLdflags(self, ldflags, gyp_to_build_path): - """.def files get implicitly converted to a ModuleDefinitionFile for the + config = self._TargetConfig(config) + libpaths = self._Setting( + (root, "AdditionalLibraryDirectories"), config, default=[] + ) + libpaths = [ + os.path.normpath(gyp_to_build_path(self.ConvertVSMacros(p, config=config))) + for p in libpaths + ] + return ['/LIBPATH:"' + p + '"' for p in libpaths] + + def GetLibFlags(self, config, gyp_to_build_path): + """Returns the flags that need to be added to lib commands.""" + config = self._TargetConfig(config) + libflags = [] + lib = self._GetWrapper( + self, self.msvs_settings[config], "VCLibrarianTool", append=libflags + ) + libflags.extend( + self._GetAdditionalLibraryDirectories( + "VCLibrarianTool", config, gyp_to_build_path + ) + ) + lib("LinkTimeCodeGeneration", map={"true": "/LTCG"}) + lib( + "TargetMachine", + map={"1": "X86", "17": "X64", "3": "ARM"}, + prefix="/MACHINE:", + ) + lib("AdditionalOptions") + return libflags + + def GetDefFile(self, gyp_to_build_path): + """Returns the .def file from sources, if any. Otherwise returns None.""" + spec = self.spec + if spec["type"] in ("shared_library", "loadable_module", "executable"): + def_files = [ + s for s in spec.get("sources", []) if s.lower().endswith(".def") + ] + if len(def_files) == 1: + return gyp_to_build_path(def_files[0]) + elif len(def_files) > 1: + raise Exception("Multiple .def files") + return None + + def _GetDefFileAsLdflags(self, ldflags, gyp_to_build_path): + """.def files get implicitly converted to a ModuleDefinitionFile for the linker in the VS generator. Emulate that behaviour here.""" - def_file = self.GetDefFile(gyp_to_build_path) - if def_file: - ldflags.append('/DEF:"%s"' % def_file) + def_file = self.GetDefFile(gyp_to_build_path) + if def_file: + ldflags.append('/DEF:"%s"' % def_file) - def GetPGDName(self, config, expand_special): - """Gets the explicitly overridden pgd name for a target or returns None + def GetPGDName(self, config, expand_special): + """Gets the explicitly overridden pgd name for a target or returns None if it's not overridden.""" - config = self._TargetConfig(config) - output_file = self._Setting( - ('VCLinkerTool', 'ProfileGuidedDatabase'), config) - if output_file: - output_file = expand_special(self.ConvertVSMacros( - output_file, config=config)) - return output_file - - def GetLdflags(self, config, gyp_to_build_path, expand_special, - manifest_base_name, output_name, is_executable, build_dir): - """Returns the flags that need to be added to link commands, and the + config = self._TargetConfig(config) + output_file = self._Setting(("VCLinkerTool", "ProfileGuidedDatabase"), config) + if output_file: + output_file = expand_special( + self.ConvertVSMacros(output_file, config=config) + ) + return output_file + + def GetLdflags( + self, + config, + gyp_to_build_path, + expand_special, + manifest_base_name, + output_name, + is_executable, + build_dir, + ): + """Returns the flags that need to be added to link commands, and the manifest files.""" - config = self._TargetConfig(config) - ldflags = [] - ld = self._GetWrapper(self, self.msvs_settings[config], - 'VCLinkerTool', append=ldflags) - self._GetDefFileAsLdflags(ldflags, gyp_to_build_path) - ld('GenerateDebugInformation', map={'true': '/DEBUG'}) - # TODO: These 'map' values come from machineTypeOption enum, - # and does not have an official value for ARM64 in VS2017 (yet). - # It needs to verify the ARM64 value when machineTypeOption is updated. - ld('TargetMachine', map={'1': 'X86', '17': 'X64', '3': 'ARM', '18': 'ARM64'}, - prefix='/MACHINE:') - ldflags.extend(self._GetAdditionalLibraryDirectories( - 'VCLinkerTool', config, gyp_to_build_path)) - ld('DelayLoadDLLs', prefix='/DELAYLOAD:') - ld('TreatLinkerWarningAsErrors', prefix='/WX', - map={'true': '', 'false': ':NO'}) - out = self.GetOutputName(config, expand_special) - if out: - ldflags.append('/OUT:' + out) - pdb = self.GetPDBName(config, expand_special, output_name + '.pdb') - if pdb: - ldflags.append('/PDB:' + pdb) - pgd = self.GetPGDName(config, expand_special) - if pgd: - ldflags.append('/PGD:' + pgd) - map_file = self.GetMapFileName(config, expand_special) - ld('GenerateMapFile', map={'true': '/MAP:' + map_file if map_file - else '/MAP'}) - ld('MapExports', map={'true': '/MAPINFO:EXPORTS'}) - ld('AdditionalOptions', prefix='') - - minimum_required_version = self._Setting( - ('VCLinkerTool', 'MinimumRequiredVersion'), config, default='') - if minimum_required_version: - minimum_required_version = ',' + minimum_required_version - ld('SubSystem', - map={'1': 'CONSOLE%s' % minimum_required_version, - '2': 'WINDOWS%s' % minimum_required_version}, - prefix='/SUBSYSTEM:') - - stack_reserve_size = self._Setting( - ('VCLinkerTool', 'StackReserveSize'), config, default='') - if stack_reserve_size: - stack_commit_size = self._Setting( - ('VCLinkerTool', 'StackCommitSize'), config, default='') - if stack_commit_size: - stack_commit_size = ',' + stack_commit_size - ldflags.append('/STACK:%s%s' % (stack_reserve_size, stack_commit_size)) - - ld('TerminalServerAware', map={'1': ':NO', '2': ''}, prefix='/TSAWARE') - ld('LinkIncremental', map={'1': ':NO', '2': ''}, prefix='/INCREMENTAL') - ld('BaseAddress', prefix='/BASE:') - ld('FixedBaseAddress', map={'1': ':NO', '2': ''}, prefix='/FIXED') - ld('RandomizedBaseAddress', - map={'1': ':NO', '2': ''}, prefix='/DYNAMICBASE') - ld('DataExecutionPrevention', - map={'1': ':NO', '2': ''}, prefix='/NXCOMPAT') - ld('OptimizeReferences', map={'1': 'NOREF', '2': 'REF'}, prefix='/OPT:') - ld('ForceSymbolReferences', prefix='/INCLUDE:') - ld('EnableCOMDATFolding', map={'1': 'NOICF', '2': 'ICF'}, prefix='/OPT:') - ld('LinkTimeCodeGeneration', - map={'1': '', '2': ':PGINSTRUMENT', '3': ':PGOPTIMIZE', - '4': ':PGUPDATE'}, - prefix='/LTCG') - ld('IgnoreDefaultLibraryNames', prefix='/NODEFAULTLIB:') - ld('ResourceOnlyDLL', map={'true': '/NOENTRY'}) - ld('EntryPointSymbol', prefix='/ENTRY:') - ld('Profile', map={'true': '/PROFILE'}) - ld('LargeAddressAware', - map={'1': ':NO', '2': ''}, prefix='/LARGEADDRESSAWARE') - # TODO(scottmg): This should sort of be somewhere else (not really a flag). - ld('AdditionalDependencies', prefix='') - - if self.GetArch(config) == 'x86': - safeseh_default = 'true' - else: - safeseh_default = None - ld('ImageHasSafeExceptionHandlers', - map={'false': ':NO', 'true': ''}, prefix='/SAFESEH', - default=safeseh_default) - - # If the base address is not specifically controlled, DYNAMICBASE should - # be on by default. - base_flags = filter(lambda x: 'DYNAMICBASE' in x or x == '/FIXED', - ldflags) - if not base_flags: - ldflags.append('/DYNAMICBASE') - - # If the NXCOMPAT flag has not been specified, default to on. Despite the - # documentation that says this only defaults to on when the subsystem is - # Vista or greater (which applies to the linker), the IDE defaults it on - # unless it's explicitly off. - if not filter(lambda x: 'NXCOMPAT' in x, ldflags): - ldflags.append('/NXCOMPAT') - - have_def_file = filter(lambda x: x.startswith('/DEF:'), ldflags) - manifest_flags, intermediate_manifest, manifest_files = \ - self._GetLdManifestFlags(config, manifest_base_name, gyp_to_build_path, - is_executable and not have_def_file, build_dir) - ldflags.extend(manifest_flags) - return ldflags, intermediate_manifest, manifest_files - - def _GetLdManifestFlags(self, config, name, gyp_to_build_path, - allow_isolation, build_dir): - """Returns a 3-tuple: + config = self._TargetConfig(config) + ldflags = [] + ld = self._GetWrapper( + self, self.msvs_settings[config], "VCLinkerTool", append=ldflags + ) + self._GetDefFileAsLdflags(ldflags, gyp_to_build_path) + ld("GenerateDebugInformation", map={"true": "/DEBUG"}) + # TODO: These 'map' values come from machineTypeOption enum, + # and does not have an official value for ARM64 in VS2017 (yet). + # It needs to verify the ARM64 value when machineTypeOption is updated. + ld( + "TargetMachine", + map={"1": "X86", "17": "X64", "3": "ARM", "18": "ARM64"}, + prefix="/MACHINE:", + ) + ldflags.extend( + self._GetAdditionalLibraryDirectories( + "VCLinkerTool", config, gyp_to_build_path + ) + ) + ld("DelayLoadDLLs", prefix="/DELAYLOAD:") + ld("TreatLinkerWarningAsErrors", prefix="/WX", map={"true": "", "false": ":NO"}) + out = self.GetOutputName(config, expand_special) + if out: + ldflags.append("/OUT:" + out) + pdb = self.GetPDBName(config, expand_special, output_name + ".pdb") + if pdb: + ldflags.append("/PDB:" + pdb) + pgd = self.GetPGDName(config, expand_special) + if pgd: + ldflags.append("/PGD:" + pgd) + map_file = self.GetMapFileName(config, expand_special) + ld("GenerateMapFile", map={"true": "/MAP:" + map_file if map_file else "/MAP"}) + ld("MapExports", map={"true": "/MAPINFO:EXPORTS"}) + ld("AdditionalOptions", prefix="") + + minimum_required_version = self._Setting( + ("VCLinkerTool", "MinimumRequiredVersion"), config, default="" + ) + if minimum_required_version: + minimum_required_version = "," + minimum_required_version + ld( + "SubSystem", + map={ + "1": "CONSOLE%s" % minimum_required_version, + "2": "WINDOWS%s" % minimum_required_version, + }, + prefix="/SUBSYSTEM:", + ) + + stack_reserve_size = self._Setting( + ("VCLinkerTool", "StackReserveSize"), config, default="" + ) + if stack_reserve_size: + stack_commit_size = self._Setting( + ("VCLinkerTool", "StackCommitSize"), config, default="" + ) + if stack_commit_size: + stack_commit_size = "," + stack_commit_size + ldflags.append("/STACK:%s%s" % (stack_reserve_size, stack_commit_size)) + + ld("TerminalServerAware", map={"1": ":NO", "2": ""}, prefix="/TSAWARE") + ld("LinkIncremental", map={"1": ":NO", "2": ""}, prefix="/INCREMENTAL") + ld("BaseAddress", prefix="/BASE:") + ld("FixedBaseAddress", map={"1": ":NO", "2": ""}, prefix="/FIXED") + ld("RandomizedBaseAddress", map={"1": ":NO", "2": ""}, prefix="/DYNAMICBASE") + ld("DataExecutionPrevention", map={"1": ":NO", "2": ""}, prefix="/NXCOMPAT") + ld("OptimizeReferences", map={"1": "NOREF", "2": "REF"}, prefix="/OPT:") + ld("ForceSymbolReferences", prefix="/INCLUDE:") + ld("EnableCOMDATFolding", map={"1": "NOICF", "2": "ICF"}, prefix="/OPT:") + ld( + "LinkTimeCodeGeneration", + map={"1": "", "2": ":PGINSTRUMENT", "3": ":PGOPTIMIZE", "4": ":PGUPDATE"}, + prefix="/LTCG", + ) + ld("IgnoreDefaultLibraryNames", prefix="/NODEFAULTLIB:") + ld("ResourceOnlyDLL", map={"true": "/NOENTRY"}) + ld("EntryPointSymbol", prefix="/ENTRY:") + ld("Profile", map={"true": "/PROFILE"}) + ld("LargeAddressAware", map={"1": ":NO", "2": ""}, prefix="/LARGEADDRESSAWARE") + # TODO(scottmg): This should sort of be somewhere else (not really a flag). + ld("AdditionalDependencies", prefix="") + + if self.GetArch(config) == "x86": + safeseh_default = "true" + else: + safeseh_default = None + ld( + "ImageHasSafeExceptionHandlers", + map={"false": ":NO", "true": ""}, + prefix="/SAFESEH", + default=safeseh_default, + ) + + # If the base address is not specifically controlled, DYNAMICBASE should + # be on by default. + if not any("DYNAMICBASE" in flag or flag == "/FIXED" for flag in ldflags): + ldflags.append("/DYNAMICBASE") + + # If the NXCOMPAT flag has not been specified, default to on. Despite the + # documentation that says this only defaults to on when the subsystem is + # Vista or greater (which applies to the linker), the IDE defaults it on + # unless it's explicitly off. + if not any("NXCOMPAT" in flag for flag in ldflags): + ldflags.append("/NXCOMPAT") + + have_def_file = any(flag.startswith("/DEF:") for flag in ldflags) + ( + manifest_flags, + intermediate_manifest, + manifest_files, + ) = self._GetLdManifestFlags( + config, + manifest_base_name, + gyp_to_build_path, + is_executable and not have_def_file, + build_dir, + ) + ldflags.extend(manifest_flags) + return ldflags, intermediate_manifest, manifest_files + + def _GetLdManifestFlags( + self, config, name, gyp_to_build_path, allow_isolation, build_dir + ): + """Returns a 3-tuple: - the set of flags that need to be added to the link to generate a default manifest - the intermediate manifest that the linker will generate that should be used to assert it doesn't add anything to the merged one. - the list of all the manifest files to be merged by the manifest tool and included into the link.""" - generate_manifest = self._Setting(('VCLinkerTool', 'GenerateManifest'), - config, - default='true') - if generate_manifest != 'true': - # This means not only that the linker should not generate the intermediate - # manifest but also that the manifest tool should do nothing even when - # additional manifests are specified. - return ['/MANIFEST:NO'], [], [] - - output_name = name + '.intermediate.manifest' - flags = [ - '/MANIFEST', - '/ManifestFile:' + output_name, - ] - - # Instead of using the MANIFESTUAC flags, we generate a .manifest to - # include into the list of manifests. This allows us to avoid the need to - # do two passes during linking. The /MANIFEST flag and /ManifestFile are - # still used, and the intermediate manifest is used to assert that the - # final manifest we get from merging all the additional manifest files - # (plus the one we generate here) isn't modified by merging the - # intermediate into it. - - # Always NO, because we generate a manifest file that has what we want. - flags.append('/MANIFESTUAC:NO') - - config = self._TargetConfig(config) - enable_uac = self._Setting(('VCLinkerTool', 'EnableUAC'), config, - default='true') - manifest_files = [] - generated_manifest_outer = \ -"" \ -"%s" \ -"" - if enable_uac == 'true': - execution_level = self._Setting(('VCLinkerTool', 'UACExecutionLevel'), - config, default='0') - execution_level_map = { - '0': 'asInvoker', - '1': 'highestAvailable', - '2': 'requireAdministrator' - } - - ui_access = self._Setting(('VCLinkerTool', 'UACUIAccess'), config, - default='false') - - inner = ''' + generate_manifest = self._Setting( + ("VCLinkerTool", "GenerateManifest"), config, default="true" + ) + if generate_manifest != "true": + # This means not only that the linker should not generate the intermediate + # manifest but also that the manifest tool should do nothing even when + # additional manifests are specified. + return ["/MANIFEST:NO"], [], [] + + output_name = name + ".intermediate.manifest" + flags = [ + "/MANIFEST", + "/ManifestFile:" + output_name, + ] + + # Instead of using the MANIFESTUAC flags, we generate a .manifest to + # include into the list of manifests. This allows us to avoid the need to + # do two passes during linking. The /MANIFEST flag and /ManifestFile are + # still used, and the intermediate manifest is used to assert that the + # final manifest we get from merging all the additional manifest files + # (plus the one we generate here) isn't modified by merging the + # intermediate into it. + + # Always NO, because we generate a manifest file that has what we want. + flags.append("/MANIFESTUAC:NO") + + config = self._TargetConfig(config) + enable_uac = self._Setting( + ("VCLinkerTool", "EnableUAC"), config, default="true" + ) + manifest_files = [] + generated_manifest_outer = ( + "" + "" + "%s" + ) + if enable_uac == "true": + execution_level = self._Setting( + ("VCLinkerTool", "UACExecutionLevel"), config, default="0" + ) + execution_level_map = { + "0": "asInvoker", + "1": "highestAvailable", + "2": "requireAdministrator", + } + + ui_access = self._Setting( + ("VCLinkerTool", "UACUIAccess"), config, default="false" + ) + + inner = """ -''' % (execution_level_map[execution_level], ui_access) - else: - inner = '' - - generated_manifest_contents = generated_manifest_outer % inner - generated_name = name + '.generated.manifest' - # Need to join with the build_dir here as we're writing it during - # generation time, but we return the un-joined version because the build - # will occur in that directory. We only write the file if the contents - # have changed so that simply regenerating the project files doesn't - # cause a relink. - build_dir_generated_name = os.path.join(build_dir, generated_name) - gyp.common.EnsureDirExists(build_dir_generated_name) - f = gyp.common.WriteOnDiff(build_dir_generated_name) - f.write(generated_manifest_contents) - f.close() - manifest_files = [generated_name] - - if allow_isolation: - flags.append('/ALLOWISOLATION') - - manifest_files += self._GetAdditionalManifestFiles(config, - gyp_to_build_path) - return flags, output_name, manifest_files - - def _GetAdditionalManifestFiles(self, config, gyp_to_build_path): - """Gets additional manifest files that are added to the default one +""" % ( + execution_level_map[execution_level], + ui_access, + ) + else: + inner = "" + + generated_manifest_contents = generated_manifest_outer % inner + generated_name = name + ".generated.manifest" + # Need to join with the build_dir here as we're writing it during + # generation time, but we return the un-joined version because the build + # will occur in that directory. We only write the file if the contents + # have changed so that simply regenerating the project files doesn't + # cause a relink. + build_dir_generated_name = os.path.join(build_dir, generated_name) + gyp.common.EnsureDirExists(build_dir_generated_name) + f = gyp.common.WriteOnDiff(build_dir_generated_name) + f.write(generated_manifest_contents) + f.close() + manifest_files = [generated_name] + + if allow_isolation: + flags.append("/ALLOWISOLATION") + + manifest_files += self._GetAdditionalManifestFiles(config, gyp_to_build_path) + return flags, output_name, manifest_files + + def _GetAdditionalManifestFiles(self, config, gyp_to_build_path): + """Gets additional manifest files that are added to the default one generated by the linker.""" - files = self._Setting(('VCManifestTool', 'AdditionalManifestFiles'), config, - default=[]) - if isinstance(files, str): - files = files.split(';') - return [os.path.normpath( - gyp_to_build_path(self.ConvertVSMacros(f, config=config))) - for f in files] - - def IsUseLibraryDependencyInputs(self, config): - """Returns whether the target should be linked via Use Library Dependency + files = self._Setting( + ("VCManifestTool", "AdditionalManifestFiles"), config, default=[] + ) + if isinstance(files, str): + files = files.split(";") + return [ + os.path.normpath(gyp_to_build_path(self.ConvertVSMacros(f, config=config))) + for f in files + ] + + def IsUseLibraryDependencyInputs(self, config): + """Returns whether the target should be linked via Use Library Dependency Inputs (using component .objs of a given .lib).""" - config = self._TargetConfig(config) - uldi = self._Setting(('VCLinkerTool', 'UseLibraryDependencyInputs'), config) - return uldi == 'true' - - def IsEmbedManifest(self, config): - """Returns whether manifest should be linked into binary.""" - config = self._TargetConfig(config) - embed = self._Setting(('VCManifestTool', 'EmbedManifest'), config, - default='true') - return embed == 'true' - - def IsLinkIncremental(self, config): - """Returns whether the target should be linked incrementally.""" - config = self._TargetConfig(config) - link_inc = self._Setting(('VCLinkerTool', 'LinkIncremental'), config) - return link_inc != '1' - - def GetRcflags(self, config, gyp_to_ninja_path): - """Returns the flags that need to be added to invocations of the resource + config = self._TargetConfig(config) + uldi = self._Setting(("VCLinkerTool", "UseLibraryDependencyInputs"), config) + return uldi == "true" + + def IsEmbedManifest(self, config): + """Returns whether manifest should be linked into binary.""" + config = self._TargetConfig(config) + embed = self._Setting( + ("VCManifestTool", "EmbedManifest"), config, default="true" + ) + return embed == "true" + + def IsLinkIncremental(self, config): + """Returns whether the target should be linked incrementally.""" + config = self._TargetConfig(config) + link_inc = self._Setting(("VCLinkerTool", "LinkIncremental"), config) + return link_inc != "1" + + def GetRcflags(self, config, gyp_to_ninja_path): + """Returns the flags that need to be added to invocations of the resource compiler.""" - config = self._TargetConfig(config) - rcflags = [] - rc = self._GetWrapper(self, self.msvs_settings[config], - 'VCResourceCompilerTool', append=rcflags) - rc('AdditionalIncludeDirectories', map=gyp_to_ninja_path, prefix='/I') - rcflags.append('/I' + gyp_to_ninja_path('.')) - rc('PreprocessorDefinitions', prefix='/d') - # /l arg must be in hex without leading '0x' - rc('Culture', prefix='/l', map=lambda x: hex(int(x))[2:]) - return rcflags - - def BuildCygwinBashCommandLine(self, args, path_to_base): - """Build a command line that runs args via cygwin bash. We assume that all + config = self._TargetConfig(config) + rcflags = [] + rc = self._GetWrapper( + self, self.msvs_settings[config], "VCResourceCompilerTool", append=rcflags + ) + rc("AdditionalIncludeDirectories", map=gyp_to_ninja_path, prefix="/I") + rcflags.append("/I" + gyp_to_ninja_path(".")) + rc("PreprocessorDefinitions", prefix="/d") + # /l arg must be in hex without leading '0x' + rc("Culture", prefix="/l", map=lambda x: hex(int(x))[2:]) + return rcflags + + def BuildCygwinBashCommandLine(self, args, path_to_base): + """Build a command line that runs args via cygwin bash. We assume that all incoming paths are in Windows normpath'd form, so they need to be converted to posix style for the part of the command line that's passed to bash. We also have to do some Visual Studio macro emulation here because @@ -820,216 +923,246 @@ def BuildCygwinBashCommandLine(self, args, path_to_base): contain ninja variables cannot be fixed here (for example ${source}), so the outer generator needs to make sure that the paths that are written out are in posix style, if the command line will be used here.""" - cygwin_dir = os.path.normpath( - os.path.join(path_to_base, self.msvs_cygwin_dirs[0])) - cd = ('cd %s' % path_to_base).replace('\\', '/') - args = [a.replace('\\', '/').replace('"', '\\"') for a in args] - args = ["'%s'" % a.replace("'", "'\\''") for a in args] - bash_cmd = ' '.join(args) - cmd = ( - 'call "%s\\setup_env.bat" && set CYGWIN=nontsec && ' % cygwin_dir + - 'bash -c "%s ; %s"' % (cd, bash_cmd)) - return cmd - - def IsRuleRunUnderCygwin(self, rule): - """Determine if an action should be run under cygwin. If the variable is + cygwin_dir = os.path.normpath( + os.path.join(path_to_base, self.msvs_cygwin_dirs[0]) + ) + cd = ("cd %s" % path_to_base).replace("\\", "/") + args = [a.replace("\\", "/").replace('"', '\\"') for a in args] + args = ["'%s'" % a.replace("'", "'\\''") for a in args] + bash_cmd = " ".join(args) + cmd = ( + 'call "%s\\setup_env.bat" && set CYGWIN=nontsec && ' % cygwin_dir + + 'bash -c "%s ; %s"' % (cd, bash_cmd) + ) + return cmd + + def IsRuleRunUnderCygwin(self, rule): + """Determine if an action should be run under cygwin. If the variable is unset, or set to 1 we use cygwin.""" - return int(rule.get('msvs_cygwin_shell', - self.spec.get('msvs_cygwin_shell', 1))) != 0 - - def _HasExplicitRuleForExtension(self, spec, extension): - """Determine if there's an explicit rule for a particular extension.""" - for rule in spec.get('rules', []): - if rule['extension'] == extension: - return True - return False - - def _HasExplicitIdlActions(self, spec): - """Determine if an action should not run midl for .idl files.""" - return any([action.get('explicit_idl_action', 0) - for action in spec.get('actions', [])]) - - def HasExplicitIdlRulesOrActions(self, spec): - """Determine if there's an explicit rule or action for idl files. When + return ( + int(rule.get("msvs_cygwin_shell", self.spec.get("msvs_cygwin_shell", 1))) + != 0 + ) + + def _HasExplicitRuleForExtension(self, spec, extension): + """Determine if there's an explicit rule for a particular extension.""" + for rule in spec.get("rules", []): + if rule["extension"] == extension: + return True + return False + + def _HasExplicitIdlActions(self, spec): + """Determine if an action should not run midl for .idl files.""" + return any( + [action.get("explicit_idl_action", 0) for action in spec.get("actions", [])] + ) + + def HasExplicitIdlRulesOrActions(self, spec): + """Determine if there's an explicit rule or action for idl files. When there isn't we need to generate implicit rules to build MIDL .idl files.""" - return (self._HasExplicitRuleForExtension(spec, 'idl') or - self._HasExplicitIdlActions(spec)) + return self._HasExplicitRuleForExtension( + spec, "idl" + ) or self._HasExplicitIdlActions(spec) - def HasExplicitAsmRules(self, spec): - """Determine if there's an explicit rule for asm files. When there isn't we + def HasExplicitAsmRules(self, spec): + """Determine if there's an explicit rule for asm files. When there isn't we need to generate implicit rules to assemble .asm files.""" - return self._HasExplicitRuleForExtension(spec, 'asm') + return self._HasExplicitRuleForExtension(spec, "asm") - def GetIdlBuildData(self, source, config): - """Determine the implicit outputs for an idl file. Returns output + def GetIdlBuildData(self, source, config): + """Determine the implicit outputs for an idl file. Returns output directory, outputs, and variables and flags that are required.""" - config = self._TargetConfig(config) - midl_get = self._GetWrapper(self, self.msvs_settings[config], 'VCMIDLTool') - def midl(name, default=None): - return self.ConvertVSMacros(midl_get(name, default=default), - config=config) - tlb = midl('TypeLibraryName', default='${root}.tlb') - header = midl('HeaderFileName', default='${root}.h') - dlldata = midl('DLLDataFileName', default='dlldata.c') - iid = midl('InterfaceIdentifierFileName', default='${root}_i.c') - proxy = midl('ProxyFileName', default='${root}_p.c') - # Note that .tlb is not included in the outputs as it is not always - # generated depending on the content of the input idl file. - outdir = midl('OutputDirectory', default='') - output = [header, dlldata, iid, proxy] - variables = [('tlb', tlb), - ('h', header), - ('dlldata', dlldata), - ('iid', iid), - ('proxy', proxy)] - # TODO(scottmg): Are there configuration settings to set these flags? - target_platform = self.GetArch(config) - if target_platform == 'x86': - target_platform = 'win32' - flags = ['/char', 'signed', '/env', target_platform, '/Oicf'] - return outdir, output, variables, flags + config = self._TargetConfig(config) + midl_get = self._GetWrapper(self, self.msvs_settings[config], "VCMIDLTool") + + def midl(name, default=None): + return self.ConvertVSMacros(midl_get(name, default=default), config=config) + + tlb = midl("TypeLibraryName", default="${root}.tlb") + header = midl("HeaderFileName", default="${root}.h") + dlldata = midl("DLLDataFileName", default="dlldata.c") + iid = midl("InterfaceIdentifierFileName", default="${root}_i.c") + proxy = midl("ProxyFileName", default="${root}_p.c") + # Note that .tlb is not included in the outputs as it is not always + # generated depending on the content of the input idl file. + outdir = midl("OutputDirectory", default="") + output = [header, dlldata, iid, proxy] + variables = [ + ("tlb", tlb), + ("h", header), + ("dlldata", dlldata), + ("iid", iid), + ("proxy", proxy), + ] + # TODO(scottmg): Are there configuration settings to set these flags? + target_platform = self.GetArch(config) + if target_platform == "x86": + target_platform = "win32" + flags = ["/char", "signed", "/env", target_platform, "/Oicf"] + return outdir, output, variables, flags def _LanguageMatchesForPch(source_ext, pch_source_ext): - c_exts = ('.c',) - cc_exts = ('.cc', '.cxx', '.cpp') - return ((source_ext in c_exts and pch_source_ext in c_exts) or - (source_ext in cc_exts and pch_source_ext in cc_exts)) + c_exts = (".c",) + cc_exts = (".cc", ".cxx", ".cpp") + return (source_ext in c_exts and pch_source_ext in c_exts) or ( + source_ext in cc_exts and pch_source_ext in cc_exts + ) class PrecompiledHeader(object): - """Helper to generate dependencies and build rules to handle generation of + """Helper to generate dependencies and build rules to handle generation of precompiled headers. Interface matches the GCH handler in xcode_emulation.py. """ - def __init__( - self, settings, config, gyp_to_build_path, gyp_to_unique_output, obj_ext): - self.settings = settings - self.config = config - pch_source = self.settings.msvs_precompiled_source[self.config] - self.pch_source = gyp_to_build_path(pch_source) - filename, _ = os.path.splitext(pch_source) - self.output_obj = gyp_to_unique_output(filename + obj_ext).lower() - - def _PchHeader(self): - """Get the header that will appear in an #include line for all source + + def __init__( + self, settings, config, gyp_to_build_path, gyp_to_unique_output, obj_ext + ): + self.settings = settings + self.config = config + pch_source = self.settings.msvs_precompiled_source[self.config] + self.pch_source = gyp_to_build_path(pch_source) + filename, _ = os.path.splitext(pch_source) + self.output_obj = gyp_to_unique_output(filename + obj_ext).lower() + + def _PchHeader(self): + """Get the header that will appear in an #include line for all source files.""" - return self.settings.msvs_precompiled_header[self.config] + return self.settings.msvs_precompiled_header[self.config] - def GetObjDependencies(self, sources, objs, arch): - """Given a list of sources files and the corresponding object files, + def GetObjDependencies(self, sources, objs, arch): + """Given a list of sources files and the corresponding object files, returns a list of the pch files that should be depended upon. The additional wrapping in the return value is for interface compatibility with make.py on Mac, and xcode_emulation.py.""" - assert arch is None - if not self._PchHeader(): - return [] - pch_ext = os.path.splitext(self.pch_source)[1] - for source in sources: - if _LanguageMatchesForPch(os.path.splitext(source)[1], pch_ext): - return [(None, None, self.output_obj)] - return [] - - def GetPchBuildCommands(self, arch): - """Not used on Windows as there are no additional build steps required + assert arch is None + if not self._PchHeader(): + return [] + pch_ext = os.path.splitext(self.pch_source)[1] + for source in sources: + if _LanguageMatchesForPch(os.path.splitext(source)[1], pch_ext): + return [(None, None, self.output_obj)] + return [] + + def GetPchBuildCommands(self, arch): + """Not used on Windows as there are no additional build steps required (instead, existing steps are modified in GetFlagsModifications below).""" - return [] + return [] - def GetFlagsModifications(self, input, output, implicit, command, - cflags_c, cflags_cc, expand_special): - """Get the modified cflags and implicit dependencies that should be used + def GetFlagsModifications( + self, input, output, implicit, command, cflags_c, cflags_cc, expand_special + ): + """Get the modified cflags and implicit dependencies that should be used for the pch compilation step.""" - if input == self.pch_source: - pch_output = ['/Yc' + self._PchHeader()] - if command == 'cxx': - return ([('cflags_cc', map(expand_special, cflags_cc + pch_output))], - self.output_obj, []) - elif command == 'cc': - return ([('cflags_c', map(expand_special, cflags_c + pch_output))], - self.output_obj, []) - return [], output, implicit + if input == self.pch_source: + pch_output = ["/Yc" + self._PchHeader()] + if command == "cxx": + return ( + [("cflags_cc", map(expand_special, cflags_cc + pch_output))], + self.output_obj, + [], + ) + elif command == "cc": + return ( + [("cflags_c", map(expand_special, cflags_c + pch_output))], + self.output_obj, + [], + ) + return [], output, implicit vs_version = None + + def GetVSVersion(generator_flags): - global vs_version - if not vs_version: - vs_version = gyp.MSVSVersion.SelectVisualStudioVersion( - generator_flags.get('msvs_version', 'auto'), - allow_fallback=False) - return vs_version + global vs_version + if not vs_version: + vs_version = gyp.MSVSVersion.SelectVisualStudioVersion( + generator_flags.get("msvs_version", "auto"), allow_fallback=False + ) + return vs_version + def _GetVsvarsSetupArgs(generator_flags, arch): - vs = GetVSVersion(generator_flags) - return vs.SetupScript() + vs = GetVSVersion(generator_flags) + return vs.SetupScript() + def ExpandMacros(string, expansions): - """Expand $(Variable) per expansions dict. See MsvsSettings.GetVSMacroEnv + """Expand $(Variable) per expansions dict. See MsvsSettings.GetVSMacroEnv for the canonical way to retrieve a suitable dict.""" - if '$' in string: - for old, new in expansions.items(): - assert '$(' not in new, new - string = string.replace(old, new) - return string + if "$" in string: + for old, new in expansions.items(): + assert "$(" not in new, new + string = string.replace(old, new) + return string + def _ExtractImportantEnvironment(output_of_set): - """Extracts environment variables required for the toolchain to run from + """Extracts environment variables required for the toolchain to run from a textual dump output by the cmd.exe 'set' command.""" - envvars_to_save = ( - 'goma_.*', # TODO(scottmg): This is ugly, but needed for goma. - 'include', - 'lib', - 'libpath', - 'path', - 'pathext', - 'systemroot', - 'temp', - 'tmp', - ) - env = {} - # This occasionally happens and leads to misleading SYSTEMROOT error messages - # if not caught here. - if output_of_set.count('=') == 0: - raise Exception('Invalid output_of_set. Value is:\n%s' % output_of_set) - for line in output_of_set.splitlines(): - for envvar in envvars_to_save: - if re.match(envvar + '=', line.lower()): - var, setting = line.split('=', 1) - if envvar == 'path': - # Our own rules (for running gyp-win-tool) and other actions in - # Chromium rely on python being in the path. Add the path to this - # python here so that if it's not in the path when ninja is run - # later, python will still be found. - setting = os.path.dirname(sys.executable) + os.pathsep + setting - env[var.upper()] = setting - break - for required in ('SYSTEMROOT', 'TEMP', 'TMP'): - if required not in env: - raise Exception('Environment variable "%s" ' - 'required to be set to valid path' % required) - return env + envvars_to_save = ( + "goma_.*", # TODO(scottmg): This is ugly, but needed for goma. + "include", + "lib", + "libpath", + "path", + "pathext", + "systemroot", + "temp", + "tmp", + ) + env = {} + # This occasionally happens and leads to misleading SYSTEMROOT error messages + # if not caught here. + if output_of_set.count("=") == 0: + raise Exception("Invalid output_of_set. Value is:\n%s" % output_of_set) + for line in output_of_set.splitlines(): + for envvar in envvars_to_save: + if re.match(envvar + "=", line.lower()): + var, setting = line.split("=", 1) + if envvar == "path": + # Our own rules (for running gyp-win-tool) and other actions in + # Chromium rely on python being in the path. Add the path to this + # python here so that if it's not in the path when ninja is run + # later, python will still be found. + setting = os.path.dirname(sys.executable) + os.pathsep + setting + env[var.upper()] = setting + break + for required in ("SYSTEMROOT", "TEMP", "TMP"): + if required not in env: + raise Exception( + 'Environment variable "%s" ' + "required to be set to valid path" % required + ) + return env + def _FormatAsEnvironmentBlock(envvar_dict): - """Format as an 'environment block' directly suitable for CreateProcess. + """Format as an 'environment block' directly suitable for CreateProcess. Briefly this is a list of key=value\0, terminated by an additional \0. See CreateProcess documentation for more details.""" - block = '' - nul = '\0' - for key, value in envvar_dict.items(): - block += key + '=' + value + nul - block += nul - return block + block = "" + nul = "\0" + for key, value in envvar_dict.items(): + block += key + "=" + value + nul + block += nul + return block + def _ExtractCLPath(output_of_where): - """Gets the path to cl.exe based on the output of calling the environment + """Gets the path to cl.exe based on the output of calling the environment setup batch file, followed by the equivalent of `where`.""" - # Take the first line, as that's the first found in the PATH. - for line in output_of_where.strip().splitlines(): - if line.startswith('LOC:'): - return line[len('LOC:'):].strip() - -def GenerateEnvironmentFiles(toplevel_build_dir, generator_flags, - system_includes, open_out): - """It's not sufficient to have the absolute path to the compiler, linker, + # Take the first line, as that's the first found in the PATH. + for line in output_of_where.strip().splitlines(): + if line.startswith("LOC:"): + return line[len("LOC:") :].strip() + + +def GenerateEnvironmentFiles( + toplevel_build_dir, generator_flags, system_includes, open_out +): + """It's not sufficient to have the absolute path to the compiler, linker, etc. on Windows, as those tools rely on .dlls being in the PATH. We also need to support both x86 and x64 compilers within the same build (to support msvs_target_platform hackery). Different architectures require a different @@ -1043,80 +1176,86 @@ def GenerateEnvironmentFiles(toplevel_build_dir, generator_flags, meet your requirement (e.g. for custom toolchains), you can pass "-G ninja_use_custom_environment_files" to the gyp to suppress file generation and use custom environment files prepared by yourself.""" - archs = ('x86', 'x64') - if generator_flags.get('ninja_use_custom_environment_files', 0): + archs = ("x86", "x64") + if generator_flags.get("ninja_use_custom_environment_files", 0): + cl_paths = {} + for arch in archs: + cl_paths[arch] = "cl.exe" + return cl_paths + vs = GetVSVersion(generator_flags) cl_paths = {} for arch in archs: - cl_paths[arch] = 'cl.exe' + # Extract environment variables for subprocesses. + args = vs.SetupScript(arch) + args.extend(("&&", "set")) + popen = subprocess.Popen( + args, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT + ) + variables, _ = popen.communicate() + if PY3: + variables = variables.decode("utf-8") + if popen.returncode != 0: + raise Exception('"%s" failed with error %d' % (args, popen.returncode)) + env = _ExtractImportantEnvironment(variables) + + # Inject system includes from gyp files into INCLUDE. + if system_includes: + system_includes = system_includes | OrderedSet( + env.get("INCLUDE", "").split(";") + ) + env["INCLUDE"] = ";".join(system_includes) + + env_block = _FormatAsEnvironmentBlock(env) + f = open_out(os.path.join(toplevel_build_dir, "environment." + arch), "w") + f.write(env_block) + f.close() + + # Find cl.exe location for this architecture. + args = vs.SetupScript(arch) + args.extend( + ("&&", "for", "%i", "in", "(cl.exe)", "do", "@echo", "LOC:%~$PATH:i") + ) + popen = subprocess.Popen(args, shell=True, stdout=subprocess.PIPE) + output, _ = popen.communicate() + if PY3: + output = output.decode("utf-8") + cl_paths[arch] = _ExtractCLPath(output) return cl_paths - vs = GetVSVersion(generator_flags) - cl_paths = {} - for arch in archs: - # Extract environment variables for subprocesses. - args = vs.SetupScript(arch) - args.extend(('&&', 'set')) - popen = subprocess.Popen( - args, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) - variables, _ = popen.communicate() - if PY3: - variables = variables.decode('utf-8') - if popen.returncode != 0: - raise Exception('"%s" failed with error %d' % (args, popen.returncode)) - env = _ExtractImportantEnvironment(variables) - - # Inject system includes from gyp files into INCLUDE. - if system_includes: - system_includes = system_includes | OrderedSet( - env.get('INCLUDE', '').split(';')) - env['INCLUDE'] = ';'.join(system_includes) - - env_block = _FormatAsEnvironmentBlock(env) - f = open_out(os.path.join(toplevel_build_dir, 'environment.' + arch), 'wb') - f.write(env_block) - f.close() - - # Find cl.exe location for this architecture. - args = vs.SetupScript(arch) - args.extend(('&&', - 'for', '%i', 'in', '(cl.exe)', 'do', '@echo', 'LOC:%~$PATH:i')) - popen = subprocess.Popen(args, shell=True, stdout=subprocess.PIPE) - output, _ = popen.communicate() - if PY3: - output = output.decode('utf-8') - cl_paths[arch] = _ExtractCLPath(output) - return cl_paths + def VerifyMissingSources(sources, build_dir, generator_flags, gyp_to_ninja): - """Emulate behavior of msvs_error_on_missing_sources present in the msvs + """Emulate behavior of msvs_error_on_missing_sources present in the msvs generator: Check that all regular source files, i.e. not created at run time, exist on disk. Missing files cause needless recompilation when building via VS, and we want this check to match for people/bots that build using ninja, so they're not surprised when the VS build fails.""" - if int(generator_flags.get('msvs_error_on_missing_sources', 0)): - no_specials = filter(lambda x: '$' not in x, sources) - relative = [os.path.join(build_dir, gyp_to_ninja(s)) for s in no_specials] - missing = filter(lambda x: not os.path.exists(x), relative) - if missing: - # They'll look like out\Release\..\..\stuff\things.cc, so normalize the - # path for a slightly less crazy looking output. - cleaned_up = [os.path.normpath(x) for x in missing] - raise Exception('Missing input files:\n%s' % '\n'.join(cleaned_up)) + if int(generator_flags.get("msvs_error_on_missing_sources", 0)): + no_specials = filter(lambda x: "$" not in x, sources) + relative = [os.path.join(build_dir, gyp_to_ninja(s)) for s in no_specials] + missing = [x for x in relative if not os.path.exists(x)] + if missing: + # They'll look like out\Release\..\..\stuff\things.cc, so normalize the + # path for a slightly less crazy looking output. + cleaned_up = [os.path.normpath(x) for x in missing] + raise Exception("Missing input files:\n%s" % "\n".join(cleaned_up)) + # Sets some values in default_variables, which are required for many # generators, run on Windows. def CalculateCommonVariables(default_variables, params): - generator_flags = params.get('generator_flags', {}) - - # Set a variable so conditions can be based on msvs_version. - msvs_version = gyp.msvs_emulation.GetVSVersion(generator_flags) - default_variables['MSVS_VERSION'] = msvs_version.ShortName() - - # To determine processor word size on Windows, in addition to checking - # PROCESSOR_ARCHITECTURE (which reflects the word size of the current - # process), it is also necessary to check PROCESSOR_ARCHITEW6432 (which - # contains the actual word size of the system when running thru WOW64). - if ('64' in os.environ.get('PROCESSOR_ARCHITECTURE', '') or - '64' in os.environ.get('PROCESSOR_ARCHITEW6432', '')): - default_variables['MSVS_OS_BITS'] = 64 - else: - default_variables['MSVS_OS_BITS'] = 32 + generator_flags = params.get("generator_flags", {}) + + # Set a variable so conditions can be based on msvs_version. + msvs_version = gyp.msvs_emulation.GetVSVersion(generator_flags) + default_variables["MSVS_VERSION"] = msvs_version.ShortName() + + # To determine processor word size on Windows, in addition to checking + # PROCESSOR_ARCHITECTURE (which reflects the word size of the current + # process), it is also necessary to check PROCESSOR_ARCHITEW6432 (which + # contains the actual word size of the system when running thru WOW64). + if "64" in os.environ.get("PROCESSOR_ARCHITECTURE", "") or "64" in os.environ.get( + "PROCESSOR_ARCHITEW6432", "" + ): + default_variables["MSVS_OS_BITS"] = 64 + else: + default_variables["MSVS_OS_BITS"] = 32 diff --git a/tools/gyp/pylib/gyp/ninja_syntax.py b/tools/gyp/pylib/gyp/ninja_syntax.py index d2948f06c08e36..14212358082a62 100644 --- a/tools/gyp/pylib/gyp/ninja_syntax.py +++ b/tools/gyp/pylib/gyp/ninja_syntax.py @@ -10,10 +10,11 @@ """ import textwrap -import re + def escape_path(word): - return word.replace('$ ','$$ ').replace(' ','$ ').replace(':', '$:') + return word.replace("$ ", "$$ ").replace(" ", "$ ").replace(":", "$:") + class Writer(object): def __init__(self, output, width=78): @@ -21,47 +22,58 @@ def __init__(self, output, width=78): self.width = width def newline(self): - self.output.write('\n') + self.output.write("\n") def comment(self, text): for line in textwrap.wrap(text, self.width - 2): - self.output.write('# ' + line + '\n') + self.output.write("# " + line + "\n") def variable(self, key, value, indent=0): if value is None: return if isinstance(value, list): - value = ' '.join(filter(None, value)) # Filter out empty strings. - self._line('%s = %s' % (key, value), indent) + value = " ".join(filter(None, value)) # Filter out empty strings. + self._line("%s = %s" % (key, value), indent) def pool(self, name, depth): - self._line('pool %s' % name) - self.variable('depth', depth, indent=1) - - def rule(self, name, command, description=None, depfile=None, - generator=False, pool=None, restat=False, rspfile=None, - rspfile_content=None, deps=None): - self._line('rule %s' % name) - self.variable('command', command, indent=1) + self._line("pool %s" % name) + self.variable("depth", depth, indent=1) + + def rule( + self, + name, + command, + description=None, + depfile=None, + generator=False, + pool=None, + restat=False, + rspfile=None, + rspfile_content=None, + deps=None, + ): + self._line("rule %s" % name) + self.variable("command", command, indent=1) if description: - self.variable('description', description, indent=1) + self.variable("description", description, indent=1) if depfile: - self.variable('depfile', depfile, indent=1) + self.variable("depfile", depfile, indent=1) if generator: - self.variable('generator', '1', indent=1) + self.variable("generator", "1", indent=1) if pool: - self.variable('pool', pool, indent=1) + self.variable("pool", pool, indent=1) if restat: - self.variable('restat', '1', indent=1) + self.variable("restat", "1", indent=1) if rspfile: - self.variable('rspfile', rspfile, indent=1) + self.variable("rspfile", rspfile, indent=1) if rspfile_content: - self.variable('rspfile_content', rspfile_content, indent=1) + self.variable("rspfile_content", rspfile_content, indent=1) if deps: - self.variable('deps', deps, indent=1) + self.variable("deps", deps, indent=1) - def build(self, outputs, rule, inputs=None, implicit=None, order_only=None, - variables=None): + def build( + self, outputs, rule, inputs=None, implicit=None, order_only=None, variables=None + ): outputs = self._as_list(outputs) all_inputs = self._as_list(inputs)[:] out_outputs = list(map(escape_path, outputs)) @@ -69,15 +81,16 @@ def build(self, outputs, rule, inputs=None, implicit=None, order_only=None, if implicit: implicit = map(escape_path, self._as_list(implicit)) - all_inputs.append('|') + all_inputs.append("|") all_inputs.extend(implicit) if order_only: order_only = map(escape_path, self._as_list(order_only)) - all_inputs.append('||') + all_inputs.append("||") all_inputs.extend(order_only) - self._line('build %s: %s' % (' '.join(out_outputs), - ' '.join([rule] + all_inputs))) + self._line( + "build %s: %s" % (" ".join(out_outputs), " ".join([rule] + all_inputs)) + ) if variables: if isinstance(variables, dict): @@ -91,58 +104,59 @@ def build(self, outputs, rule, inputs=None, implicit=None, order_only=None, return outputs def include(self, path): - self._line('include %s' % path) + self._line("include %s" % path) def subninja(self, path): - self._line('subninja %s' % path) + self._line("subninja %s" % path) def default(self, paths): - self._line('default %s' % ' '.join(self._as_list(paths))) + self._line("default %s" % " ".join(self._as_list(paths))) def _count_dollars_before_index(self, s, i): - """Returns the number of '$' characters right in front of s[i].""" - dollar_count = 0 - dollar_index = i - 1 - while dollar_index > 0 and s[dollar_index] == '$': - dollar_count += 1 - dollar_index -= 1 - return dollar_count + """Returns the number of '$' characters right in front of s[i].""" + dollar_count = 0 + dollar_index = i - 1 + while dollar_index > 0 and s[dollar_index] == "$": + dollar_count += 1 + dollar_index -= 1 + return dollar_count def _line(self, text, indent=0): """Write 'text' word-wrapped at self.width characters.""" - leading_space = ' ' * indent + leading_space = " " * indent while len(leading_space) + len(text) > self.width: # The text is too wide; wrap if possible. # Find the rightmost space that would obey our width constraint and # that's not an escaped space. - available_space = self.width - len(leading_space) - len(' $') + available_space = self.width - len(leading_space) - len(" $") space = available_space while True: - space = text.rfind(' ', 0, space) - if space < 0 or \ - self._count_dollars_before_index(text, space) % 2 == 0: - break + space = text.rfind(" ", 0, space) + if space < 0 or self._count_dollars_before_index(text, space) % 2 == 0: + break if space < 0: # No such space; just use the first unescaped space we can find. space = available_space - 1 while True: - space = text.find(' ', space + 1) - if space < 0 or \ - self._count_dollars_before_index(text, space) % 2 == 0: - break + space = text.find(" ", space + 1) + if ( + space < 0 + or self._count_dollars_before_index(text, space) % 2 == 0 + ): + break if space < 0: # Give up on breaking. break - self.output.write(leading_space + text[0:space] + ' $\n') - text = text[space+1:] + self.output.write(leading_space + text[0:space] + " $\n") + text = text[space + 1 :] # Subsequent lines are continuations, so indent them. - leading_space = ' ' * (indent+2) + leading_space = " " * (indent + 2) - self.output.write(leading_space + text + '\n') + self.output.write(leading_space + text + "\n") def _as_list(self, input): if input is None: @@ -155,6 +169,6 @@ def _as_list(self, input): def escape(string): """Escape a string such that it can be embedded into a Ninja file without further interpretation.""" - assert '\n' not in string, 'Ninja syntax does not allow newlines' + assert "\n" not in string, "Ninja syntax does not allow newlines" # We only have one special metacharacter: '$'. - return string.replace('$', '$$') + return string.replace("$", "$$") diff --git a/tools/gyp/pylib/gyp/simple_copy.py b/tools/gyp/pylib/gyp/simple_copy.py index 94a6f17dab5d1c..e01106f9c4821a 100644 --- a/tools/gyp/pylib/gyp/simple_copy.py +++ b/tools/gyp/pylib/gyp/simple_copy.py @@ -7,44 +7,58 @@ because gyp copies so large structure that small copy overhead ends up taking seconds in a project the size of Chromium.""" + class Error(Exception): - pass + pass + __all__ = ["Error", "deepcopy"] + def deepcopy(x): - """Deep copy operation on gyp objects such as strings, ints, dicts + """Deep copy operation on gyp objects such as strings, ints, dicts and lists. More than twice as fast as copy.deepcopy but much less generic.""" - try: - return _deepcopy_dispatch[type(x)](x) - except KeyError: - raise Error('Unsupported type %s for deepcopy. Use copy.deepcopy ' + - 'or expand simple_copy support.' % type(x)) + try: + return _deepcopy_dispatch[type(x)](x) + except KeyError: + raise Error( + "Unsupported type %s for deepcopy. Use copy.deepcopy " + + "or expand simple_copy support." % type(x) + ) + _deepcopy_dispatch = d = {} + def _deepcopy_atomic(x): - return x + return x + try: - types = bool, float, int, str, type, type(None), long, unicode + types = bool, float, int, str, type, type(None), long, unicode except NameError: # Python 3 - types = bool, float, int, str, type, type(None) + types = bool, float, int, str, type, type(None) for x in types: - d[x] = _deepcopy_atomic + d[x] = _deepcopy_atomic + def _deepcopy_list(x): - return [deepcopy(a) for a in x] + return [deepcopy(a) for a in x] + + d[list] = _deepcopy_list + def _deepcopy_dict(x): - y = {} - for key, value in x.items(): - y[deepcopy(key)] = deepcopy(value) - return y + y = {} + for key, value in x.items(): + y[deepcopy(key)] = deepcopy(value) + return y + + d[dict] = _deepcopy_dict del d diff --git a/tools/gyp/pylib/gyp/win_tool.py b/tools/gyp/pylib/gyp/win_tool.py index cfdacb0d7ccd10..758e9f5c456221 100755 --- a/tools/gyp/pylib/gyp/win_tool.py +++ b/tools/gyp/pylib/gyp/win_tool.py @@ -24,312 +24,362 @@ # A regex matching an argument corresponding to the output filename passed to # link.exe. -_LINK_EXE_OUT_ARG = re.compile('/OUT:(?P.+)$', re.IGNORECASE) +_LINK_EXE_OUT_ARG = re.compile("/OUT:(?P.+)$", re.IGNORECASE) + def main(args): - executor = WinTool() - exit_code = executor.Dispatch(args) - if exit_code is not None: - sys.exit(exit_code) + executor = WinTool() + exit_code = executor.Dispatch(args) + if exit_code is not None: + sys.exit(exit_code) class WinTool(object): - """This class performs all the Windows tooling steps. The methods can either + """This class performs all the Windows tooling steps. The methods can either be executed directly, or dispatched from an argument list.""" - def _UseSeparateMspdbsrv(self, env, args): - """Allows to use a unique instance of mspdbsrv.exe per linker instead of a + def _UseSeparateMspdbsrv(self, env, args): + """Allows to use a unique instance of mspdbsrv.exe per linker instead of a shared one.""" - if len(args) < 1: - raise Exception("Not enough arguments") - - if args[0] != 'link.exe': - return - - # Use the output filename passed to the linker to generate an endpoint name - # for mspdbsrv.exe. - endpoint_name = None - for arg in args: - m = _LINK_EXE_OUT_ARG.match(arg) - if m: - endpoint_name = re.sub(r'\W+', '', - '%s_%d' % (m.group('out'), os.getpid())) - break - - if endpoint_name is None: - return - - # Adds the appropriate environment variable. This will be read by link.exe - # to know which instance of mspdbsrv.exe it should connect to (if it's - # not set then the default endpoint is used). - env['_MSPDBSRV_ENDPOINT_'] = endpoint_name - - def Dispatch(self, args): - """Dispatches a string command to a method.""" - if len(args) < 1: - raise Exception("Not enough arguments") - - method = "Exec%s" % self._CommandifyName(args[0]) - return getattr(self, method)(*args[1:]) - - def _CommandifyName(self, name_string): - """Transforms a tool name like recursive-mirror to RecursiveMirror.""" - return name_string.title().replace('-', '') - - def _GetEnv(self, arch): - """Gets the saved environment from a file for a given architecture.""" - # The environment is saved as an "environment block" (see CreateProcess - # and msvs_emulation for details). We convert to a dict here. - # Drop last 2 NULs, one for list terminator, one for trailing vs. separator. - pairs = open(arch).read()[:-2].split('\0') - kvs = [item.split('=', 1) for item in pairs] - return dict(kvs) - - def ExecStamp(self, path): - """Simple stamp command.""" - open(path, 'w').close() - - def ExecRecursiveMirror(self, source, dest): - """Emulation of rm -rf out && cp -af in out.""" - if os.path.exists(dest): - if os.path.isdir(dest): - def _on_error(fn, path, excinfo): - # The operation failed, possibly because the file is set to - # read-only. If that's why, make it writable and try the op again. - if not os.access(path, os.W_OK): - os.chmod(path, stat.S_IWRITE) - fn(path) - shutil.rmtree(dest, onerror=_on_error) - else: - if not os.access(dest, os.W_OK): - # Attempt to make the file writable before deleting it. - os.chmod(dest, stat.S_IWRITE) - os.unlink(dest) - - if os.path.isdir(source): - shutil.copytree(source, dest) - else: - shutil.copy2(source, dest) - - def ExecLinkWrapper(self, arch, use_separate_mspdbsrv, *args): - """Filter diagnostic output from link that looks like: + if len(args) < 1: + raise Exception("Not enough arguments") + + if args[0] != "link.exe": + return + + # Use the output filename passed to the linker to generate an endpoint name + # for mspdbsrv.exe. + endpoint_name = None + for arg in args: + m = _LINK_EXE_OUT_ARG.match(arg) + if m: + endpoint_name = re.sub( + r"\W+", "", "%s_%d" % (m.group("out"), os.getpid()) + ) + break + + if endpoint_name is None: + return + + # Adds the appropriate environment variable. This will be read by link.exe + # to know which instance of mspdbsrv.exe it should connect to (if it's + # not set then the default endpoint is used). + env["_MSPDBSRV_ENDPOINT_"] = endpoint_name + + def Dispatch(self, args): + """Dispatches a string command to a method.""" + if len(args) < 1: + raise Exception("Not enough arguments") + + method = "Exec%s" % self._CommandifyName(args[0]) + return getattr(self, method)(*args[1:]) + + def _CommandifyName(self, name_string): + """Transforms a tool name like recursive-mirror to RecursiveMirror.""" + return name_string.title().replace("-", "") + + def _GetEnv(self, arch): + """Gets the saved environment from a file for a given architecture.""" + # The environment is saved as an "environment block" (see CreateProcess + # and msvs_emulation for details). We convert to a dict here. + # Drop last 2 NULs, one for list terminator, one for trailing vs. separator. + pairs = open(arch).read()[:-2].split("\0") + kvs = [item.split("=", 1) for item in pairs] + return dict(kvs) + + def ExecStamp(self, path): + """Simple stamp command.""" + open(path, "w").close() + + def ExecRecursiveMirror(self, source, dest): + """Emulation of rm -rf out && cp -af in out.""" + if os.path.exists(dest): + if os.path.isdir(dest): + + def _on_error(fn, path, excinfo): + # The operation failed, possibly because the file is set to + # read-only. If that's why, make it writable and try the op again. + if not os.access(path, os.W_OK): + os.chmod(path, stat.S_IWRITE) + fn(path) + + shutil.rmtree(dest, onerror=_on_error) + else: + if not os.access(dest, os.W_OK): + # Attempt to make the file writable before deleting it. + os.chmod(dest, stat.S_IWRITE) + os.unlink(dest) + + if os.path.isdir(source): + shutil.copytree(source, dest) + else: + shutil.copy2(source, dest) + + def ExecLinkWrapper(self, arch, use_separate_mspdbsrv, *args): + """Filter diagnostic output from link that looks like: ' Creating library ui.dll.lib and object ui.dll.exp' This happens when there are exports from the dll or exe. """ - env = self._GetEnv(arch) - if use_separate_mspdbsrv == 'True': - self._UseSeparateMspdbsrv(env, args) - if sys.platform == 'win32': - args = list(args) # *args is a tuple by default, which is read-only. - args[0] = args[0].replace('/', '\\') - # https://docs.python.org/2/library/subprocess.html: - # "On Unix with shell=True [...] if args is a sequence, the first item - # specifies the command string, and any additional items will be treated as - # additional arguments to the shell itself. That is to say, Popen does the - # equivalent of: - # Popen(['/bin/sh', '-c', args[0], args[1], ...])" - # For that reason, since going through the shell doesn't seem necessary on - # non-Windows don't do that there. - link = subprocess.Popen(args, shell=sys.platform == 'win32', env=env, - stdout=subprocess.PIPE, stderr=subprocess.STDOUT) - out, _ = link.communicate() - if PY3: - out = out.decode('utf-8') - for line in out.splitlines(): - if (not line.startswith(' Creating library ') and - not line.startswith('Generating code') and - not line.startswith('Finished generating code')): - print(line) - return link.returncode - - def ExecLinkWithManifests(self, arch, embed_manifest, out, ldcmd, resname, - mt, rc, intermediate_manifest, *manifests): - """A wrapper for handling creating a manifest resource and then executing + env = self._GetEnv(arch) + if use_separate_mspdbsrv == "True": + self._UseSeparateMspdbsrv(env, args) + if sys.platform == "win32": + args = list(args) # *args is a tuple by default, which is read-only. + args[0] = args[0].replace("/", "\\") + # https://docs.python.org/2/library/subprocess.html: + # "On Unix with shell=True [...] if args is a sequence, the first item + # specifies the command string, and any additional items will be treated as + # additional arguments to the shell itself. That is to say, Popen does the + # equivalent of: + # Popen(['/bin/sh', '-c', args[0], args[1], ...])" + # For that reason, since going through the shell doesn't seem necessary on + # non-Windows don't do that there. + link = subprocess.Popen( + args, + shell=sys.platform == "win32", + env=env, + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, + ) + out, _ = link.communicate() + if PY3: + out = out.decode("utf-8") + for line in out.splitlines(): + if ( + not line.startswith(" Creating library ") + and not line.startswith("Generating code") + and not line.startswith("Finished generating code") + ): + print(line) + return link.returncode + + def ExecLinkWithManifests( + self, + arch, + embed_manifest, + out, + ldcmd, + resname, + mt, + rc, + intermediate_manifest, + *manifests + ): + """A wrapper for handling creating a manifest resource and then executing a link command.""" - # The 'normal' way to do manifests is to have link generate a manifest - # based on gathering dependencies from the object files, then merge that - # manifest with other manifests supplied as sources, convert the merged - # manifest to a resource, and then *relink*, including the compiled - # version of the manifest resource. This breaks incremental linking, and - # is generally overly complicated. Instead, we merge all the manifests - # provided (along with one that includes what would normally be in the - # linker-generated one, see msvs_emulation.py), and include that into the - # first and only link. We still tell link to generate a manifest, but we - # only use that to assert that our simpler process did not miss anything. - variables = { - 'python': sys.executable, - 'arch': arch, - 'out': out, - 'ldcmd': ldcmd, - 'resname': resname, - 'mt': mt, - 'rc': rc, - 'intermediate_manifest': intermediate_manifest, - 'manifests': ' '.join(manifests), - } - add_to_ld = '' - if manifests: - subprocess.check_call( - '%(python)s gyp-win-tool manifest-wrapper %(arch)s %(mt)s -nologo ' - '-manifest %(manifests)s -out:%(out)s.manifest' % variables) - if embed_manifest == 'True': - subprocess.check_call( - '%(python)s gyp-win-tool manifest-to-rc %(arch)s %(out)s.manifest' - ' %(out)s.manifest.rc %(resname)s' % variables) - subprocess.check_call( - '%(python)s gyp-win-tool rc-wrapper %(arch)s %(rc)s ' - '%(out)s.manifest.rc' % variables) - add_to_ld = ' %(out)s.manifest.res' % variables - subprocess.check_call(ldcmd + add_to_ld) - - # Run mt.exe on the theoretically complete manifest we generated, merging - # it with the one the linker generated to confirm that the linker - # generated one does not add anything. This is strictly unnecessary for - # correctness, it's only to verify that e.g. /MANIFESTDEPENDENCY was not - # used in a #pragma comment. - if manifests: - # Merge the intermediate one with ours to .assert.manifest, then check - # that .assert.manifest is identical to ours. - subprocess.check_call( - '%(python)s gyp-win-tool manifest-wrapper %(arch)s %(mt)s -nologo ' - '-manifest %(out)s.manifest %(intermediate_manifest)s ' - '-out:%(out)s.assert.manifest' % variables) - assert_manifest = '%(out)s.assert.manifest' % variables - our_manifest = '%(out)s.manifest' % variables - # Load and normalize the manifests. mt.exe sometimes removes whitespace, - # and sometimes doesn't unfortunately. - with open(our_manifest, 'rb') as our_f: - with open(assert_manifest, 'rb') as assert_f: - our_data = our_f.read().translate(None, string.whitespace) - assert_data = assert_f.read().translate(None, string.whitespace) - if our_data != assert_data: - os.unlink(out) - def dump(filename): - sys.stderr.write('%s\n-----\n' % filename) - with open(filename, 'rb') as f: - sys.stderr.write(f.read() + '\n-----\n') - dump(intermediate_manifest) - dump(our_manifest) - dump(assert_manifest) - sys.stderr.write( - 'Linker generated manifest "%s" added to final manifest "%s" ' - '(result in "%s"). ' - 'Were /MANIFEST switches used in #pragma statements? ' % ( - intermediate_manifest, our_manifest, assert_manifest)) - return 1 - - def ExecManifestWrapper(self, arch, *args): - """Run manifest tool with environment set. Strip out undesirable warning + # The 'normal' way to do manifests is to have link generate a manifest + # based on gathering dependencies from the object files, then merge that + # manifest with other manifests supplied as sources, convert the merged + # manifest to a resource, and then *relink*, including the compiled + # version of the manifest resource. This breaks incremental linking, and + # is generally overly complicated. Instead, we merge all the manifests + # provided (along with one that includes what would normally be in the + # linker-generated one, see msvs_emulation.py), and include that into the + # first and only link. We still tell link to generate a manifest, but we + # only use that to assert that our simpler process did not miss anything. + variables = { + "python": sys.executable, + "arch": arch, + "out": out, + "ldcmd": ldcmd, + "resname": resname, + "mt": mt, + "rc": rc, + "intermediate_manifest": intermediate_manifest, + "manifests": " ".join(manifests), + } + add_to_ld = "" + if manifests: + subprocess.check_call( + "%(python)s gyp-win-tool manifest-wrapper %(arch)s %(mt)s -nologo " + "-manifest %(manifests)s -out:%(out)s.manifest" % variables + ) + if embed_manifest == "True": + subprocess.check_call( + "%(python)s gyp-win-tool manifest-to-rc %(arch)s %(out)s.manifest" + " %(out)s.manifest.rc %(resname)s" % variables + ) + subprocess.check_call( + "%(python)s gyp-win-tool rc-wrapper %(arch)s %(rc)s " + "%(out)s.manifest.rc" % variables + ) + add_to_ld = " %(out)s.manifest.res" % variables + subprocess.check_call(ldcmd + add_to_ld) + + # Run mt.exe on the theoretically complete manifest we generated, merging + # it with the one the linker generated to confirm that the linker + # generated one does not add anything. This is strictly unnecessary for + # correctness, it's only to verify that e.g. /MANIFESTDEPENDENCY was not + # used in a #pragma comment. + if manifests: + # Merge the intermediate one with ours to .assert.manifest, then check + # that .assert.manifest is identical to ours. + subprocess.check_call( + "%(python)s gyp-win-tool manifest-wrapper %(arch)s %(mt)s -nologo " + "-manifest %(out)s.manifest %(intermediate_manifest)s " + "-out:%(out)s.assert.manifest" % variables + ) + assert_manifest = "%(out)s.assert.manifest" % variables + our_manifest = "%(out)s.manifest" % variables + # Load and normalize the manifests. mt.exe sometimes removes whitespace, + # and sometimes doesn't unfortunately. + with open(our_manifest, "r") as our_f: + with open(assert_manifest, "r") as assert_f: + our_data = our_f.read().translate(None, string.whitespace) + assert_data = assert_f.read().translate(None, string.whitespace) + if our_data != assert_data: + os.unlink(out) + + def dump(filename): + print(filename, file=sys.stderr) + print("-----", file=sys.stderr) + with open(filename, "r") as f: + print(f.read(), file=sys.stderr) + print("-----", file=sys.stderr) + + dump(intermediate_manifest) + dump(our_manifest) + dump(assert_manifest) + sys.stderr.write( + 'Linker generated manifest "%s" added to final manifest "%s" ' + '(result in "%s"). ' + "Were /MANIFEST switches used in #pragma statements? " + % (intermediate_manifest, our_manifest, assert_manifest) + ) + return 1 + + def ExecManifestWrapper(self, arch, *args): + """Run manifest tool with environment set. Strip out undesirable warning (some XML blocks are recognized by the OS loader, but not the manifest tool).""" - env = self._GetEnv(arch) - popen = subprocess.Popen(args, shell=True, env=env, - stdout=subprocess.PIPE, stderr=subprocess.STDOUT) - out, _ = popen.communicate() - if PY3: - out = out.decode('utf-8') - for line in out.splitlines(): - if line and 'manifest authoring warning 81010002' not in line: - print(line) - return popen.returncode - - def ExecManifestToRc(self, arch, *args): - """Creates a resource file pointing a SxS assembly manifest. + env = self._GetEnv(arch) + popen = subprocess.Popen( + args, shell=True, env=env, stdout=subprocess.PIPE, stderr=subprocess.STDOUT + ) + out, _ = popen.communicate() + if PY3: + out = out.decode("utf-8") + for line in out.splitlines(): + if line and "manifest authoring warning 81010002" not in line: + print(line) + return popen.returncode + + def ExecManifestToRc(self, arch, *args): + """Creates a resource file pointing a SxS assembly manifest. |args| is tuple containing path to resource file, path to manifest file and resource name which can be "1" (for executables) or "2" (for DLLs).""" - manifest_path, resource_path, resource_name = args - with open(resource_path, 'wb') as output: - output.write('#include \n%s RT_MANIFEST "%s"' % ( - resource_name, - os.path.abspath(manifest_path).replace('\\', '/'))) - - def ExecMidlWrapper(self, arch, outdir, tlb, h, dlldata, iid, proxy, idl, - *flags): - """Filter noisy filenames output from MIDL compile step that isn't + manifest_path, resource_path, resource_name = args + with open(resource_path, "w") as output: + output.write( + '#include \n%s RT_MANIFEST "%s"' + % (resource_name, os.path.abspath(manifest_path).replace("\\", "/")) + ) + + def ExecMidlWrapper(self, arch, outdir, tlb, h, dlldata, iid, proxy, idl, *flags): + """Filter noisy filenames output from MIDL compile step that isn't quietable via command line flags. """ - args = ['midl', '/nologo'] + list(flags) + [ - '/out', outdir, - '/tlb', tlb, - '/h', h, - '/dlldata', dlldata, - '/iid', iid, - '/proxy', proxy, - idl] - env = self._GetEnv(arch) - popen = subprocess.Popen(args, shell=True, env=env, - stdout=subprocess.PIPE, stderr=subprocess.STDOUT) - out, _ = popen.communicate() - if PY3: - out = out.decode('utf-8') - # Filter junk out of stdout, and write filtered versions. Output we want - # to filter is pairs of lines that look like this: - # Processing C:\Program Files (x86)\Microsoft SDKs\...\include\objidl.idl - # objidl.idl - lines = out.splitlines() - prefixes = ('Processing ', '64 bit Processing ') - processing = set(os.path.basename(x) - for x in lines if x.startswith(prefixes)) - for line in lines: - if not line.startswith(prefixes) and line not in processing: - print(line) - return popen.returncode - - def ExecAsmWrapper(self, arch, *args): - """Filter logo banner from invocations of asm.exe.""" - env = self._GetEnv(arch) - popen = subprocess.Popen(args, shell=True, env=env, - stdout=subprocess.PIPE, stderr=subprocess.STDOUT) - out, _ = popen.communicate() - if PY3: - out = out.decode('utf-8') - for line in out.splitlines(): - if (not line.startswith('Copyright (C) Microsoft Corporation') and - not line.startswith('Microsoft (R) Macro Assembler') and - not line.startswith(' Assembling: ') and - line): - print(line) - return popen.returncode - - def ExecRcWrapper(self, arch, *args): - """Filter logo banner from invocations of rc.exe. Older versions of RC + args = ( + ["midl", "/nologo"] + + list(flags) + + [ + "/out", + outdir, + "/tlb", + tlb, + "/h", + h, + "/dlldata", + dlldata, + "/iid", + iid, + "/proxy", + proxy, + idl, + ] + ) + env = self._GetEnv(arch) + popen = subprocess.Popen( + args, shell=True, env=env, stdout=subprocess.PIPE, stderr=subprocess.STDOUT + ) + out, _ = popen.communicate() + if PY3: + out = out.decode("utf-8") + # Filter junk out of stdout, and write filtered versions. Output we want + # to filter is pairs of lines that look like this: + # Processing C:\Program Files (x86)\Microsoft SDKs\...\include\objidl.idl + # objidl.idl + lines = out.splitlines() + prefixes = ("Processing ", "64 bit Processing ") + processing = set(os.path.basename(x) for x in lines if x.startswith(prefixes)) + for line in lines: + if not line.startswith(prefixes) and line not in processing: + print(line) + return popen.returncode + + def ExecAsmWrapper(self, arch, *args): + """Filter logo banner from invocations of asm.exe.""" + env = self._GetEnv(arch) + popen = subprocess.Popen( + args, shell=True, env=env, stdout=subprocess.PIPE, stderr=subprocess.STDOUT + ) + out, _ = popen.communicate() + if PY3: + out = out.decode("utf-8") + for line in out.splitlines(): + if ( + not line.startswith("Copyright (C) Microsoft Corporation") + and not line.startswith("Microsoft (R) Macro Assembler") + and not line.startswith(" Assembling: ") + and line + ): + print(line) + return popen.returncode + + def ExecRcWrapper(self, arch, *args): + """Filter logo banner from invocations of rc.exe. Older versions of RC don't support the /nologo flag.""" - env = self._GetEnv(arch) - popen = subprocess.Popen(args, shell=True, env=env, - stdout=subprocess.PIPE, stderr=subprocess.STDOUT) - out, _ = popen.communicate() - if PY3: - out = out.decode('utf-8') - for line in out.splitlines(): - if (not line.startswith('Microsoft (R) Windows (R) Resource Compiler') and - not line.startswith('Copyright (C) Microsoft Corporation') and - line): - print(line) - return popen.returncode - - def ExecActionWrapper(self, arch, rspfile, *dir): - """Runs an action command line from a response file using the environment + env = self._GetEnv(arch) + popen = subprocess.Popen( + args, shell=True, env=env, stdout=subprocess.PIPE, stderr=subprocess.STDOUT + ) + out, _ = popen.communicate() + if PY3: + out = out.decode("utf-8") + for line in out.splitlines(): + if ( + not line.startswith("Microsoft (R) Windows (R) Resource Compiler") + and not line.startswith("Copyright (C) Microsoft Corporation") + and line + ): + print(line) + return popen.returncode + + def ExecActionWrapper(self, arch, rspfile, *dir): + """Runs an action command line from a response file using the environment for |arch|. If |dir| is supplied, use that as the working directory.""" - env = self._GetEnv(arch) - # TODO(scottmg): This is a temporary hack to get some specific variables - # through to actions that are set after gyp-time. http://crbug.com/333738. - for k, v in os.environ.items(): - if k not in env: - env[k] = v - args = open(rspfile).read() - dir = dir[0] if dir else None - return subprocess.call(args, shell=True, env=env, cwd=dir) - - def ExecClCompile(self, project_dir, selected_files): - """Executed by msvs-ninja projects when the 'ClCompile' target is used to + env = self._GetEnv(arch) + # TODO(scottmg): This is a temporary hack to get some specific variables + # through to actions that are set after gyp-time. http://crbug.com/333738. + for k, v in os.environ.items(): + if k not in env: + env[k] = v + args = open(rspfile).read() + dir = dir[0] if dir else None + return subprocess.call(args, shell=True, env=env, cwd=dir) + + def ExecClCompile(self, project_dir, selected_files): + """Executed by msvs-ninja projects when the 'ClCompile' target is used to build selected C/C++ files.""" - project_dir = os.path.relpath(project_dir, BASE_DIR) - selected_files = selected_files.split(';') - ninja_targets = [os.path.join(project_dir, filename) + '^^' - for filename in selected_files] - cmd = ['ninja.exe'] - cmd.extend(ninja_targets) - return subprocess.call(cmd, shell=True, cwd=BASE_DIR) - -if __name__ == '__main__': - sys.exit(main(sys.argv[1:])) + project_dir = os.path.relpath(project_dir, BASE_DIR) + selected_files = selected_files.split(";") + ninja_targets = [ + os.path.join(project_dir, filename) + "^^" for filename in selected_files + ] + cmd = ["ninja.exe"] + cmd.extend(ninja_targets) + return subprocess.call(cmd, shell=True, cwd=BASE_DIR) + + +if __name__ == "__main__": + sys.exit(main(sys.argv[1:])) diff --git a/tools/gyp/pylib/gyp/xcode_emulation.py b/tools/gyp/pylib/gyp/xcode_emulation.py index c3daba5fb82e1a..8af2b39f9a1479 100644 --- a/tools/gyp/pylib/gyp/xcode_emulation.py +++ b/tools/gyp/pylib/gyp/xcode_emulation.py @@ -17,7 +17,6 @@ import shlex import subprocess import sys -import tempfile from gyp.common import GypError PY3 = bytes != str @@ -33,71 +32,72 @@ def XcodeArchsVariableMapping(archs, archs_including_64_bit=None): - """Constructs a dictionary with expansion for $(ARCHS_STANDARD) variable, + """Constructs a dictionary with expansion for $(ARCHS_STANDARD) variable, and optionally for $(ARCHS_STANDARD_INCLUDING_64_BIT).""" - mapping = {'$(ARCHS_STANDARD)': archs} - if archs_including_64_bit: - mapping['$(ARCHS_STANDARD_INCLUDING_64_BIT)'] = archs_including_64_bit - return mapping + mapping = {"$(ARCHS_STANDARD)": archs} + if archs_including_64_bit: + mapping["$(ARCHS_STANDARD_INCLUDING_64_BIT)"] = archs_including_64_bit + return mapping + class XcodeArchsDefault(object): - """A class to resolve ARCHS variable from xcode_settings, resolving Xcode + """A class to resolve ARCHS variable from xcode_settings, resolving Xcode macros and implementing filtering by VALID_ARCHS. The expansion of macros depends on the SDKROOT used ("macosx", "iphoneos", "iphonesimulator") and on the version of Xcode. """ - # Match variable like $(ARCHS_STANDARD). - variable_pattern = re.compile(r'\$\([a-zA-Z_][a-zA-Z0-9_]*\)$') + # Match variable like $(ARCHS_STANDARD). + variable_pattern = re.compile(r"\$\([a-zA-Z_][a-zA-Z0-9_]*\)$") - def __init__(self, default, mac, iphonesimulator, iphoneos): - self._default = (default,) - self._archs = {'mac': mac, 'ios': iphoneos, 'iossim': iphonesimulator} + def __init__(self, default, mac, iphonesimulator, iphoneos): + self._default = (default,) + self._archs = {"mac": mac, "ios": iphoneos, "iossim": iphonesimulator} - def _VariableMapping(self, sdkroot): - """Returns the dictionary of variable mapping depending on the SDKROOT.""" - sdkroot = sdkroot.lower() - if 'iphoneos' in sdkroot: - return self._archs['ios'] - elif 'iphonesimulator' in sdkroot: - return self._archs['iossim'] - else: - return self._archs['mac'] - - def _ExpandArchs(self, archs, sdkroot): - """Expands variables references in ARCHS, and remove duplicates.""" - variable_mapping = self._VariableMapping(sdkroot) - expanded_archs = [] - for arch in archs: - if self.variable_pattern.match(arch): - variable = arch - try: - variable_expansion = variable_mapping[variable] - for arch in variable_expansion: - if arch not in expanded_archs: - expanded_archs.append(arch) - except KeyError as e: - print('Warning: Ignoring unsupported variable "%s".' % variable) - elif arch not in expanded_archs: - expanded_archs.append(arch) - return expanded_archs - - def ActiveArchs(self, archs, valid_archs, sdkroot): - """Expands variables references in ARCHS, and filter by VALID_ARCHS if it + def _VariableMapping(self, sdkroot): + """Returns the dictionary of variable mapping depending on the SDKROOT.""" + sdkroot = sdkroot.lower() + if "iphoneos" in sdkroot: + return self._archs["ios"] + elif "iphonesimulator" in sdkroot: + return self._archs["iossim"] + else: + return self._archs["mac"] + + def _ExpandArchs(self, archs, sdkroot): + """Expands variables references in ARCHS, and remove duplicates.""" + variable_mapping = self._VariableMapping(sdkroot) + expanded_archs = [] + for arch in archs: + if self.variable_pattern.match(arch): + variable = arch + try: + variable_expansion = variable_mapping[variable] + for arch in variable_expansion: + if arch not in expanded_archs: + expanded_archs.append(arch) + except KeyError: + print('Warning: Ignoring unsupported variable "%s".' % variable) + elif arch not in expanded_archs: + expanded_archs.append(arch) + return expanded_archs + + def ActiveArchs(self, archs, valid_archs, sdkroot): + """Expands variables references in ARCHS, and filter by VALID_ARCHS if it is defined (if not set, Xcode accept any value in ARCHS, otherwise, only values present in VALID_ARCHS are kept).""" - expanded_archs = self._ExpandArchs(archs or self._default, sdkroot or '') - if valid_archs: - filtered_archs = [] - for arch in expanded_archs: - if arch in valid_archs: - filtered_archs.append(arch) - expanded_archs = filtered_archs - return expanded_archs + expanded_archs = self._ExpandArchs(archs or self._default, sdkroot or "") + if valid_archs: + filtered_archs = [] + for arch in expanded_archs: + if arch in valid_archs: + filtered_archs.append(arch) + expanded_archs = filtered_archs + return expanded_archs def GetXcodeArchsDefault(): - """Returns the |XcodeArchsDefault| object to use to expand ARCHS for the + """Returns the |XcodeArchsDefault| object to use to expand ARCHS for the installed version of Xcode. The default values used by Xcode for ARCHS and the expansion of the variables depends on the version of Xcode used. @@ -113,741 +113,789 @@ def GetXcodeArchsDefault(): of $(ARCHS_STANDARD_INCLUDING_64_BIT) from Xcode 5.0. From Xcode 5.1, they are also part of $(ARCHS_STANDARD). - All thoses rules are coded in the construction of the |XcodeArchsDefault| + All these rules are coded in the construction of the |XcodeArchsDefault| object to use depending on the version of Xcode detected. The object is for performance reason.""" - global XCODE_ARCHS_DEFAULT_CACHE - if XCODE_ARCHS_DEFAULT_CACHE: + global XCODE_ARCHS_DEFAULT_CACHE + if XCODE_ARCHS_DEFAULT_CACHE: + return XCODE_ARCHS_DEFAULT_CACHE + xcode_version, _ = XcodeVersion() + if xcode_version < "0500": + XCODE_ARCHS_DEFAULT_CACHE = XcodeArchsDefault( + "$(ARCHS_STANDARD)", + XcodeArchsVariableMapping(["i386"]), + XcodeArchsVariableMapping(["i386"]), + XcodeArchsVariableMapping(["armv7"]), + ) + elif xcode_version < "0510": + XCODE_ARCHS_DEFAULT_CACHE = XcodeArchsDefault( + "$(ARCHS_STANDARD_INCLUDING_64_BIT)", + XcodeArchsVariableMapping(["x86_64"], ["x86_64"]), + XcodeArchsVariableMapping(["i386"], ["i386", "x86_64"]), + XcodeArchsVariableMapping( + ["armv7", "armv7s"], ["armv7", "armv7s", "arm64"] + ), + ) + else: + XCODE_ARCHS_DEFAULT_CACHE = XcodeArchsDefault( + "$(ARCHS_STANDARD)", + XcodeArchsVariableMapping(["x86_64"], ["x86_64"]), + XcodeArchsVariableMapping(["i386", "x86_64"], ["i386", "x86_64"]), + XcodeArchsVariableMapping( + ["armv7", "armv7s", "arm64"], ["armv7", "armv7s", "arm64"] + ), + ) return XCODE_ARCHS_DEFAULT_CACHE - xcode_version, _ = XcodeVersion() - if xcode_version < '0500': - XCODE_ARCHS_DEFAULT_CACHE = XcodeArchsDefault( - '$(ARCHS_STANDARD)', - XcodeArchsVariableMapping(['i386']), - XcodeArchsVariableMapping(['i386']), - XcodeArchsVariableMapping(['armv7'])) - elif xcode_version < '0510': - XCODE_ARCHS_DEFAULT_CACHE = XcodeArchsDefault( - '$(ARCHS_STANDARD_INCLUDING_64_BIT)', - XcodeArchsVariableMapping(['x86_64'], ['x86_64']), - XcodeArchsVariableMapping(['i386'], ['i386', 'x86_64']), - XcodeArchsVariableMapping( - ['armv7', 'armv7s'], - ['armv7', 'armv7s', 'arm64'])) - else: - XCODE_ARCHS_DEFAULT_CACHE = XcodeArchsDefault( - '$(ARCHS_STANDARD)', - XcodeArchsVariableMapping(['x86_64'], ['x86_64']), - XcodeArchsVariableMapping(['i386', 'x86_64'], ['i386', 'x86_64']), - XcodeArchsVariableMapping( - ['armv7', 'armv7s', 'arm64'], - ['armv7', 'armv7s', 'arm64'])) - return XCODE_ARCHS_DEFAULT_CACHE class XcodeSettings(object): - """A class that understands the gyp 'xcode_settings' object.""" - - # Populated lazily by _SdkPath(). Shared by all XcodeSettings, so cached - # at class-level for efficiency. - _sdk_path_cache = {} - _platform_path_cache = {} - _sdk_root_cache = {} - - # Populated lazily by GetExtraPlistItems(). Shared by all XcodeSettings, so - # cached at class-level for efficiency. - _plist_cache = {} - - # Populated lazily by GetIOSPostbuilds. Shared by all XcodeSettings, so - # cached at class-level for efficiency. - _codesigning_key_cache = {} - - def __init__(self, spec): - self.spec = spec - - self.isIOS = False - self.mac_toolchain_dir = None - self.header_map_path = None - - # Per-target 'xcode_settings' are pushed down into configs earlier by gyp. - # This means self.xcode_settings[config] always contains all settings - # for that config -- the per-target settings as well. Settings that are - # the same for all configs are implicitly per-target settings. - self.xcode_settings = {} - configs = spec['configurations'] - for configname, config in configs.items(): - self.xcode_settings[configname] = config.get('xcode_settings', {}) - self._ConvertConditionalKeys(configname) - if self.xcode_settings[configname].get('IPHONEOS_DEPLOYMENT_TARGET', - None): - self.isIOS = True - - # This is only non-None temporarily during the execution of some methods. - self.configname = None - - # Used by _AdjustLibrary to match .a and .dylib entries in libraries. - self.library_re = re.compile(r'^lib([^/]+)\.(a|dylib)$') - - def _ConvertConditionalKeys(self, configname): - """Converts or warns on conditional keys. Xcode supports conditional keys, + """A class that understands the gyp 'xcode_settings' object.""" + + # Populated lazily by _SdkPath(). Shared by all XcodeSettings, so cached + # at class-level for efficiency. + _sdk_path_cache = {} + _platform_path_cache = {} + _sdk_root_cache = {} + + # Populated lazily by GetExtraPlistItems(). Shared by all XcodeSettings, so + # cached at class-level for efficiency. + _plist_cache = {} + + # Populated lazily by GetIOSPostbuilds. Shared by all XcodeSettings, so + # cached at class-level for efficiency. + _codesigning_key_cache = {} + + def __init__(self, spec): + self.spec = spec + + self.isIOS = False + self.mac_toolchain_dir = None + self.header_map_path = None + + # Per-target 'xcode_settings' are pushed down into configs earlier by gyp. + # This means self.xcode_settings[config] always contains all settings + # for that config -- the per-target settings as well. Settings that are + # the same for all configs are implicitly per-target settings. + self.xcode_settings = {} + configs = spec["configurations"] + for configname, config in configs.items(): + self.xcode_settings[configname] = config.get("xcode_settings", {}) + self._ConvertConditionalKeys(configname) + if self.xcode_settings[configname].get("IPHONEOS_DEPLOYMENT_TARGET", None): + self.isIOS = True + + # This is only non-None temporarily during the execution of some methods. + self.configname = None + + # Used by _AdjustLibrary to match .a and .dylib entries in libraries. + self.library_re = re.compile(r"^lib([^/]+)\.(a|dylib)$") + + def _ConvertConditionalKeys(self, configname): + """Converts or warns on conditional keys. Xcode supports conditional keys, such as CODE_SIGN_IDENTITY[sdk=iphoneos*]. This is a partial implementation with some keys converted while the rest force a warning.""" - settings = self.xcode_settings[configname] - conditional_keys = [key for key in settings if key.endswith(']')] - for key in conditional_keys: - # If you need more, speak up at http://crbug.com/122592 - if key.endswith("[sdk=iphoneos*]"): - if configname.endswith("iphoneos"): - new_key = key.split("[")[0] - settings[new_key] = settings[key] - else: - print('Warning: Conditional keys not implemented, ignoring:', - ' '.join(conditional_keys)) - del settings[key] - - def _Settings(self): - assert self.configname - return self.xcode_settings[self.configname] - - def _Test(self, test_key, cond_key, default): - return self._Settings().get(test_key, default) == cond_key - - def _Appendf(self, lst, test_key, format_str, default=None): - if test_key in self._Settings(): - lst.append(format_str % str(self._Settings()[test_key])) - elif default: - lst.append(format_str % str(default)) - - def _WarnUnimplemented(self, test_key): - if test_key in self._Settings(): - print('Warning: Ignoring not yet implemented key "%s".' % test_key) - - def IsBinaryOutputFormat(self, configname): - default = "binary" if self.isIOS else "xml" - format = self.xcode_settings[configname].get('INFOPLIST_OUTPUT_FORMAT', - default) - return format == "binary" - - def IsIosFramework(self): - return self.spec['type'] == 'shared_library' and self._IsBundle() and \ - self.isIOS - - def _IsBundle(self): - return int(self.spec.get('mac_bundle', 0)) != 0 or self._IsXCTest() or \ - self._IsXCUiTest() - - def _IsXCTest(self): - return int(self.spec.get('mac_xctest_bundle', 0)) != 0 - - def _IsXCUiTest(self): - return int(self.spec.get('mac_xcuitest_bundle', 0)) != 0 - - def _IsIosAppExtension(self): - return int(self.spec.get('ios_app_extension', 0)) != 0 - - def _IsIosWatchKitExtension(self): - return int(self.spec.get('ios_watchkit_extension', 0)) != 0 - - def _IsIosWatchApp(self): - return int(self.spec.get('ios_watch_app', 0)) != 0 - - def GetFrameworkVersion(self): - """Returns the framework version of the current target. Only valid for + settings = self.xcode_settings[configname] + conditional_keys = [key for key in settings if key.endswith("]")] + for key in conditional_keys: + # If you need more, speak up at http://crbug.com/122592 + if key.endswith("[sdk=iphoneos*]"): + if configname.endswith("iphoneos"): + new_key = key.split("[")[0] + settings[new_key] = settings[key] + else: + print( + "Warning: Conditional keys not implemented, ignoring:", + " ".join(conditional_keys), + ) + del settings[key] + + def _Settings(self): + assert self.configname + return self.xcode_settings[self.configname] + + def _Test(self, test_key, cond_key, default): + return self._Settings().get(test_key, default) == cond_key + + def _Appendf(self, lst, test_key, format_str, default=None): + if test_key in self._Settings(): + lst.append(format_str % str(self._Settings()[test_key])) + elif default: + lst.append(format_str % str(default)) + + def _WarnUnimplemented(self, test_key): + if test_key in self._Settings(): + print('Warning: Ignoring not yet implemented key "%s".' % test_key) + + def IsBinaryOutputFormat(self, configname): + default = "binary" if self.isIOS else "xml" + format = self.xcode_settings[configname].get("INFOPLIST_OUTPUT_FORMAT", default) + return format == "binary" + + def IsIosFramework(self): + return self.spec["type"] == "shared_library" and self._IsBundle() and self.isIOS + + def _IsBundle(self): + return ( + int(self.spec.get("mac_bundle", 0)) != 0 + or self._IsXCTest() + or self._IsXCUiTest() + ) + + def _IsXCTest(self): + return int(self.spec.get("mac_xctest_bundle", 0)) != 0 + + def _IsXCUiTest(self): + return int(self.spec.get("mac_xcuitest_bundle", 0)) != 0 + + def _IsIosAppExtension(self): + return int(self.spec.get("ios_app_extension", 0)) != 0 + + def _IsIosWatchKitExtension(self): + return int(self.spec.get("ios_watchkit_extension", 0)) != 0 + + def _IsIosWatchApp(self): + return int(self.spec.get("ios_watch_app", 0)) != 0 + + def GetFrameworkVersion(self): + """Returns the framework version of the current target. Only valid for bundles.""" - assert self._IsBundle() - return self.GetPerTargetSetting('FRAMEWORK_VERSION', default='A') + assert self._IsBundle() + return self.GetPerTargetSetting("FRAMEWORK_VERSION", default="A") - def GetWrapperExtension(self): - """Returns the bundle extension (.app, .framework, .plugin, etc). Only + def GetWrapperExtension(self): + """Returns the bundle extension (.app, .framework, .plugin, etc). Only valid for bundles.""" - assert self._IsBundle() - if self.spec['type'] in ('loadable_module', 'shared_library'): - default_wrapper_extension = { - 'loadable_module': 'bundle', - 'shared_library': 'framework', - }[self.spec['type']] - wrapper_extension = self.GetPerTargetSetting( - 'WRAPPER_EXTENSION', default=default_wrapper_extension) - return '.' + self.spec.get('product_extension', wrapper_extension) - elif self.spec['type'] == 'executable': - if self._IsIosAppExtension() or self._IsIosWatchKitExtension(): - return '.' + self.spec.get('product_extension', 'appex') - else: - return '.' + self.spec.get('product_extension', 'app') - else: - assert False, "Don't know extension for '%s', target '%s'" % ( - self.spec['type'], self.spec['target_name']) - - def GetProductName(self): - """Returns PRODUCT_NAME.""" - return self.spec.get('product_name', self.spec['target_name']) - - def GetFullProductName(self): - """Returns FULL_PRODUCT_NAME.""" - if self._IsBundle(): - return self.GetWrapperName() - else: - return self._GetStandaloneBinaryPath() + assert self._IsBundle() + if self.spec["type"] in ("loadable_module", "shared_library"): + default_wrapper_extension = { + "loadable_module": "bundle", + "shared_library": "framework", + }[self.spec["type"]] + wrapper_extension = self.GetPerTargetSetting( + "WRAPPER_EXTENSION", default=default_wrapper_extension + ) + return "." + self.spec.get("product_extension", wrapper_extension) + elif self.spec["type"] == "executable": + if self._IsIosAppExtension() or self._IsIosWatchKitExtension(): + return "." + self.spec.get("product_extension", "appex") + else: + return "." + self.spec.get("product_extension", "app") + else: + assert False, "Don't know extension for '%s', target '%s'" % ( + self.spec["type"], + self.spec["target_name"], + ) + + def GetProductName(self): + """Returns PRODUCT_NAME.""" + return self.spec.get("product_name", self.spec["target_name"]) + + def GetFullProductName(self): + """Returns FULL_PRODUCT_NAME.""" + if self._IsBundle(): + return self.GetWrapperName() + else: + return self._GetStandaloneBinaryPath() - def GetWrapperName(self): - """Returns the directory name of the bundle represented by this target. + def GetWrapperName(self): + """Returns the directory name of the bundle represented by this target. Only valid for bundles.""" - assert self._IsBundle() - return self.GetProductName() + self.GetWrapperExtension() + assert self._IsBundle() + return self.GetProductName() + self.GetWrapperExtension() - def GetBundleContentsFolderPath(self): - """Returns the qualified path to the bundle's contents folder. E.g. + def GetBundleContentsFolderPath(self): + """Returns the qualified path to the bundle's contents folder. E.g. Chromium.app/Contents or Foo.bundle/Versions/A. Only valid for bundles.""" - if self.isIOS: - return self.GetWrapperName() - assert self._IsBundle() - if self.spec['type'] == 'shared_library': - return os.path.join( - self.GetWrapperName(), 'Versions', self.GetFrameworkVersion()) - else: - # loadable_modules have a 'Contents' folder like executables. - return os.path.join(self.GetWrapperName(), 'Contents') + if self.isIOS: + return self.GetWrapperName() + assert self._IsBundle() + if self.spec["type"] == "shared_library": + return os.path.join( + self.GetWrapperName(), "Versions", self.GetFrameworkVersion() + ) + else: + # loadable_modules have a 'Contents' folder like executables. + return os.path.join(self.GetWrapperName(), "Contents") - def GetBundleResourceFolder(self): - """Returns the qualified path to the bundle's resource folder. E.g. + def GetBundleResourceFolder(self): + """Returns the qualified path to the bundle's resource folder. E.g. Chromium.app/Contents/Resources. Only valid for bundles.""" - assert self._IsBundle() - if self.isIOS: - return self.GetBundleContentsFolderPath() - return os.path.join(self.GetBundleContentsFolderPath(), 'Resources') + assert self._IsBundle() + if self.isIOS: + return self.GetBundleContentsFolderPath() + return os.path.join(self.GetBundleContentsFolderPath(), "Resources") - def GetBundleExecutableFolderPath(self): - """Returns the qualified path to the bundle's executables folder. E.g. + def GetBundleExecutableFolderPath(self): + """Returns the qualified path to the bundle's executables folder. E.g. Chromium.app/Contents/MacOS. Only valid for bundles.""" - assert self._IsBundle() - if self.spec['type'] in ('shared_library') or self.isIOS: - return self.GetBundleContentsFolderPath() - elif self.spec['type'] in ('executable', 'loadable_module'): - return os.path.join(self.GetBundleContentsFolderPath(), 'MacOS') - - def GetBundleJavaFolderPath(self): - """Returns the qualified path to the bundle's Java resource folder. + assert self._IsBundle() + if self.spec["type"] in ("shared_library") or self.isIOS: + return self.GetBundleContentsFolderPath() + elif self.spec["type"] in ("executable", "loadable_module"): + return os.path.join(self.GetBundleContentsFolderPath(), "MacOS") + + def GetBundleJavaFolderPath(self): + """Returns the qualified path to the bundle's Java resource folder. E.g. Chromium.app/Contents/Resources/Java. Only valid for bundles.""" - assert self._IsBundle() - return os.path.join(self.GetBundleResourceFolder(), 'Java') + assert self._IsBundle() + return os.path.join(self.GetBundleResourceFolder(), "Java") - def GetBundleFrameworksFolderPath(self): - """Returns the qualified path to the bundle's frameworks folder. E.g, + def GetBundleFrameworksFolderPath(self): + """Returns the qualified path to the bundle's frameworks folder. E.g, Chromium.app/Contents/Frameworks. Only valid for bundles.""" - assert self._IsBundle() - return os.path.join(self.GetBundleContentsFolderPath(), 'Frameworks') + assert self._IsBundle() + return os.path.join(self.GetBundleContentsFolderPath(), "Frameworks") - def GetBundleSharedFrameworksFolderPath(self): - """Returns the qualified path to the bundle's frameworks folder. E.g, + def GetBundleSharedFrameworksFolderPath(self): + """Returns the qualified path to the bundle's frameworks folder. E.g, Chromium.app/Contents/SharedFrameworks. Only valid for bundles.""" - assert self._IsBundle() - return os.path.join(self.GetBundleContentsFolderPath(), - 'SharedFrameworks') + assert self._IsBundle() + return os.path.join(self.GetBundleContentsFolderPath(), "SharedFrameworks") - def GetBundleSharedSupportFolderPath(self): - """Returns the qualified path to the bundle's shared support folder. E.g, + def GetBundleSharedSupportFolderPath(self): + """Returns the qualified path to the bundle's shared support folder. E.g, Chromium.app/Contents/SharedSupport. Only valid for bundles.""" - assert self._IsBundle() - if self.spec['type'] == 'shared_library': - return self.GetBundleResourceFolder() - else: - return os.path.join(self.GetBundleContentsFolderPath(), - 'SharedSupport') + assert self._IsBundle() + if self.spec["type"] == "shared_library": + return self.GetBundleResourceFolder() + else: + return os.path.join(self.GetBundleContentsFolderPath(), "SharedSupport") - def GetBundlePlugInsFolderPath(self): - """Returns the qualified path to the bundle's plugins folder. E.g, + def GetBundlePlugInsFolderPath(self): + """Returns the qualified path to the bundle's plugins folder. E.g, Chromium.app/Contents/PlugIns. Only valid for bundles.""" - assert self._IsBundle() - return os.path.join(self.GetBundleContentsFolderPath(), 'PlugIns') + assert self._IsBundle() + return os.path.join(self.GetBundleContentsFolderPath(), "PlugIns") - def GetBundleXPCServicesFolderPath(self): - """Returns the qualified path to the bundle's XPC services folder. E.g, + def GetBundleXPCServicesFolderPath(self): + """Returns the qualified path to the bundle's XPC services folder. E.g, Chromium.app/Contents/XPCServices. Only valid for bundles.""" - assert self._IsBundle() - return os.path.join(self.GetBundleContentsFolderPath(), 'XPCServices') + assert self._IsBundle() + return os.path.join(self.GetBundleContentsFolderPath(), "XPCServices") - def GetBundlePlistPath(self): - """Returns the qualified path to the bundle's plist file. E.g. + def GetBundlePlistPath(self): + """Returns the qualified path to the bundle's plist file. E.g. Chromium.app/Contents/Info.plist. Only valid for bundles.""" - assert self._IsBundle() - if self.spec['type'] in ('executable', 'loadable_module') or \ - self.IsIosFramework(): - return os.path.join(self.GetBundleContentsFolderPath(), 'Info.plist') - else: - return os.path.join(self.GetBundleContentsFolderPath(), - 'Resources', 'Info.plist') - - def GetProductType(self): - """Returns the PRODUCT_TYPE of this target.""" - if self._IsIosAppExtension(): - assert self._IsBundle(), ('ios_app_extension flag requires mac_bundle ' - '(target %s)' % self.spec['target_name']) - return 'com.apple.product-type.app-extension' - if self._IsIosWatchKitExtension(): - assert self._IsBundle(), ('ios_watchkit_extension flag requires ' - 'mac_bundle (target %s)' % self.spec['target_name']) - return 'com.apple.product-type.watchkit-extension' - if self._IsIosWatchApp(): - assert self._IsBundle(), ('ios_watch_app flag requires mac_bundle ' - '(target %s)' % self.spec['target_name']) - return 'com.apple.product-type.application.watchapp' - if self._IsXCUiTest(): - assert self._IsBundle(), ('mac_xcuitest_bundle flag requires mac_bundle ' - '(target %s)' % self.spec['target_name']) - return 'com.apple.product-type.bundle.ui-testing' - if self._IsBundle(): - return { - 'executable': 'com.apple.product-type.application', - 'loadable_module': 'com.apple.product-type.bundle', - 'shared_library': 'com.apple.product-type.framework', - }[self.spec['type']] - else: - return { - 'executable': 'com.apple.product-type.tool', - 'loadable_module': 'com.apple.product-type.library.dynamic', - 'shared_library': 'com.apple.product-type.library.dynamic', - 'static_library': 'com.apple.product-type.library.static', - }[self.spec['type']] - - def GetMachOType(self): - """Returns the MACH_O_TYPE of this target.""" - # Weird, but matches Xcode. - if not self._IsBundle() and self.spec['type'] == 'executable': - return '' - return { - 'executable': 'mh_execute', - 'static_library': 'staticlib', - 'shared_library': 'mh_dylib', - 'loadable_module': 'mh_bundle', - }[self.spec['type']] - - def _GetBundleBinaryPath(self): - """Returns the name of the bundle binary of by this target. + assert self._IsBundle() + if ( + self.spec["type"] in ("executable", "loadable_module") + or self.IsIosFramework() + ): + return os.path.join(self.GetBundleContentsFolderPath(), "Info.plist") + else: + return os.path.join( + self.GetBundleContentsFolderPath(), "Resources", "Info.plist" + ) + + def GetProductType(self): + """Returns the PRODUCT_TYPE of this target.""" + if self._IsIosAppExtension(): + assert self._IsBundle(), ( + "ios_app_extension flag requires mac_bundle " + "(target %s)" % self.spec["target_name"] + ) + return "com.apple.product-type.app-extension" + if self._IsIosWatchKitExtension(): + assert self._IsBundle(), ( + "ios_watchkit_extension flag requires " + "mac_bundle (target %s)" % self.spec["target_name"] + ) + return "com.apple.product-type.watchkit-extension" + if self._IsIosWatchApp(): + assert self._IsBundle(), ( + "ios_watch_app flag requires mac_bundle " + "(target %s)" % self.spec["target_name"] + ) + return "com.apple.product-type.application.watchapp" + if self._IsXCUiTest(): + assert self._IsBundle(), ( + "mac_xcuitest_bundle flag requires mac_bundle " + "(target %s)" % self.spec["target_name"] + ) + return "com.apple.product-type.bundle.ui-testing" + if self._IsBundle(): + return { + "executable": "com.apple.product-type.application", + "loadable_module": "com.apple.product-type.bundle", + "shared_library": "com.apple.product-type.framework", + }[self.spec["type"]] + else: + return { + "executable": "com.apple.product-type.tool", + "loadable_module": "com.apple.product-type.library.dynamic", + "shared_library": "com.apple.product-type.library.dynamic", + "static_library": "com.apple.product-type.library.static", + }[self.spec["type"]] + + def GetMachOType(self): + """Returns the MACH_O_TYPE of this target.""" + # Weird, but matches Xcode. + if not self._IsBundle() and self.spec["type"] == "executable": + return "" + return { + "executable": "mh_execute", + "static_library": "staticlib", + "shared_library": "mh_dylib", + "loadable_module": "mh_bundle", + }[self.spec["type"]] + + def _GetBundleBinaryPath(self): + """Returns the name of the bundle binary of by this target. E.g. Chromium.app/Contents/MacOS/Chromium. Only valid for bundles.""" - assert self._IsBundle() - return os.path.join(self.GetBundleExecutableFolderPath(), \ - self.GetExecutableName()) - - def _GetStandaloneExecutableSuffix(self): - if 'product_extension' in self.spec: - return '.' + self.spec['product_extension'] - return { - 'executable': '', - 'static_library': '.a', - 'shared_library': '.dylib', - 'loadable_module': '.so', - }[self.spec['type']] - - def _GetStandaloneExecutablePrefix(self): - return self.spec.get('product_prefix', { - 'executable': '', - 'static_library': 'lib', - 'shared_library': 'lib', - # Non-bundled loadable_modules are called foo.so for some reason - # (that is, .so and no prefix) with the xcode build -- match that. - 'loadable_module': '', - }[self.spec['type']]) - - def _GetStandaloneBinaryPath(self): - """Returns the name of the non-bundle binary represented by this target. + assert self._IsBundle() + return os.path.join( + self.GetBundleExecutableFolderPath(), self.GetExecutableName() + ) + + def _GetStandaloneExecutableSuffix(self): + if "product_extension" in self.spec: + return "." + self.spec["product_extension"] + return { + "executable": "", + "static_library": ".a", + "shared_library": ".dylib", + "loadable_module": ".so", + }[self.spec["type"]] + + def _GetStandaloneExecutablePrefix(self): + return self.spec.get( + "product_prefix", + { + "executable": "", + "static_library": "lib", + "shared_library": "lib", + # Non-bundled loadable_modules are called foo.so for some reason + # (that is, .so and no prefix) with the xcode build -- match that. + "loadable_module": "", + }[self.spec["type"]], + ) + + def _GetStandaloneBinaryPath(self): + """Returns the name of the non-bundle binary represented by this target. E.g. hello_world. Only valid for non-bundles.""" - assert not self._IsBundle() - assert self.spec['type'] in ( - 'executable', 'shared_library', 'static_library', 'loadable_module'), ( - 'Unexpected type %s' % self.spec['type']) - target = self.spec['target_name'] - if self.spec['type'] == 'static_library': - if target[:3] == 'lib': - target = target[3:] - elif self.spec['type'] in ('loadable_module', 'shared_library'): - if target[:3] == 'lib': - target = target[3:] - - target_prefix = self._GetStandaloneExecutablePrefix() - target = self.spec.get('product_name', target) - target_ext = self._GetStandaloneExecutableSuffix() - return target_prefix + target + target_ext - - def GetExecutableName(self): - """Returns the executable name of the bundle represented by this target. + assert not self._IsBundle() + assert self.spec["type"] in ( + "executable", + "shared_library", + "static_library", + "loadable_module", + ), ("Unexpected type %s" % self.spec["type"]) + target = self.spec["target_name"] + if self.spec["type"] == "static_library": + if target[:3] == "lib": + target = target[3:] + elif self.spec["type"] in ("loadable_module", "shared_library"): + if target[:3] == "lib": + target = target[3:] + + target_prefix = self._GetStandaloneExecutablePrefix() + target = self.spec.get("product_name", target) + target_ext = self._GetStandaloneExecutableSuffix() + return target_prefix + target + target_ext + + def GetExecutableName(self): + """Returns the executable name of the bundle represented by this target. E.g. Chromium.""" - if self._IsBundle(): - return self.spec.get('product_name', self.spec['target_name']) - else: - return self._GetStandaloneBinaryPath() + if self._IsBundle(): + return self.spec.get("product_name", self.spec["target_name"]) + else: + return self._GetStandaloneBinaryPath() - def GetExecutablePath(self): - """Returns the qualified path to the primary executable of the bundle + def GetExecutablePath(self): + """Returns the qualified path to the primary executable of the bundle represented by this target. E.g. Chromium.app/Contents/MacOS/Chromium.""" - if self._IsBundle(): - return self._GetBundleBinaryPath() - else: - return self._GetStandaloneBinaryPath() - - def GetActiveArchs(self, configname): - """Returns the architectures this target should be built for.""" - config_settings = self.xcode_settings[configname] - xcode_archs_default = GetXcodeArchsDefault() - return xcode_archs_default.ActiveArchs( - config_settings.get('ARCHS'), - config_settings.get('VALID_ARCHS'), - config_settings.get('SDKROOT')) - - def _GetSdkVersionInfoItem(self, sdk, infoitem): - # xcodebuild requires Xcode and can't run on Command Line Tools-only - # systems from 10.7 onward. - # Since the CLT has no SDK paths anyway, returning None is the - # most sensible route and should still do the right thing. - try: - return GetStdoutQuiet(['xcrun', '--sdk', sdk, infoitem]) - except GypError: - pass - - def _SdkRoot(self, configname): - if configname is None: - configname = self.configname - return self.GetPerConfigSetting('SDKROOT', configname, default='') - - def _XcodePlatformPath(self, configname=None): - sdk_root = self._SdkRoot(configname) - if sdk_root not in XcodeSettings._platform_path_cache: - platform_path = self._GetSdkVersionInfoItem(sdk_root, - '--show-sdk-platform-path') - XcodeSettings._platform_path_cache[sdk_root] = platform_path - return XcodeSettings._platform_path_cache[sdk_root] - - def _SdkPath(self, configname=None): - sdk_root = self._SdkRoot(configname) - if sdk_root.startswith('/'): - return sdk_root - return self._XcodeSdkPath(sdk_root) - - def _XcodeSdkPath(self, sdk_root): - if sdk_root not in XcodeSettings._sdk_path_cache: - sdk_path = self._GetSdkVersionInfoItem(sdk_root, '--show-sdk-path') - XcodeSettings._sdk_path_cache[sdk_root] = sdk_path - if sdk_root: - XcodeSettings._sdk_root_cache[sdk_path] = sdk_root - return XcodeSettings._sdk_path_cache[sdk_root] - - def _AppendPlatformVersionMinFlags(self, lst): - self._Appendf(lst, 'MACOSX_DEPLOYMENT_TARGET', '-mmacosx-version-min=%s') - if 'IPHONEOS_DEPLOYMENT_TARGET' in self._Settings(): - # TODO: Implement this better? - sdk_path_basename = os.path.basename(self._SdkPath()) - if sdk_path_basename.lower().startswith('iphonesimulator'): - self._Appendf(lst, 'IPHONEOS_DEPLOYMENT_TARGET', - '-mios-simulator-version-min=%s') - else: - self._Appendf(lst, 'IPHONEOS_DEPLOYMENT_TARGET', - '-miphoneos-version-min=%s') - - def GetCflags(self, configname, arch=None): - """Returns flags that need to be added to .c, .cc, .m, and .mm + if self._IsBundle(): + return self._GetBundleBinaryPath() + else: + return self._GetStandaloneBinaryPath() + + def GetActiveArchs(self, configname): + """Returns the architectures this target should be built for.""" + config_settings = self.xcode_settings[configname] + xcode_archs_default = GetXcodeArchsDefault() + return xcode_archs_default.ActiveArchs( + config_settings.get("ARCHS"), + config_settings.get("VALID_ARCHS"), + config_settings.get("SDKROOT"), + ) + + def _GetSdkVersionInfoItem(self, sdk, infoitem): + # xcodebuild requires Xcode and can't run on Command Line Tools-only + # systems from 10.7 onward. + # Since the CLT has no SDK paths anyway, returning None is the + # most sensible route and should still do the right thing. + try: + return GetStdoutQuiet(["xcrun", "--sdk", sdk, infoitem]) + except GypError: + pass + + def _SdkRoot(self, configname): + if configname is None: + configname = self.configname + return self.GetPerConfigSetting("SDKROOT", configname, default="") + + def _XcodePlatformPath(self, configname=None): + sdk_root = self._SdkRoot(configname) + if sdk_root not in XcodeSettings._platform_path_cache: + platform_path = self._GetSdkVersionInfoItem( + sdk_root, "--show-sdk-platform-path" + ) + XcodeSettings._platform_path_cache[sdk_root] = platform_path + return XcodeSettings._platform_path_cache[sdk_root] + + def _SdkPath(self, configname=None): + sdk_root = self._SdkRoot(configname) + if sdk_root.startswith("/"): + return sdk_root + return self._XcodeSdkPath(sdk_root) + + def _XcodeSdkPath(self, sdk_root): + if sdk_root not in XcodeSettings._sdk_path_cache: + sdk_path = self._GetSdkVersionInfoItem(sdk_root, "--show-sdk-path") + XcodeSettings._sdk_path_cache[sdk_root] = sdk_path + if sdk_root: + XcodeSettings._sdk_root_cache[sdk_path] = sdk_root + return XcodeSettings._sdk_path_cache[sdk_root] + + def _AppendPlatformVersionMinFlags(self, lst): + self._Appendf(lst, "MACOSX_DEPLOYMENT_TARGET", "-mmacosx-version-min=%s") + if "IPHONEOS_DEPLOYMENT_TARGET" in self._Settings(): + # TODO: Implement this better? + sdk_path_basename = os.path.basename(self._SdkPath()) + if sdk_path_basename.lower().startswith("iphonesimulator"): + self._Appendf( + lst, "IPHONEOS_DEPLOYMENT_TARGET", "-mios-simulator-version-min=%s" + ) + else: + self._Appendf( + lst, "IPHONEOS_DEPLOYMENT_TARGET", "-miphoneos-version-min=%s" + ) + + def GetCflags(self, configname, arch=None): + """Returns flags that need to be added to .c, .cc, .m, and .mm compilations.""" - # This functions (and the similar ones below) do not offer complete - # emulation of all xcode_settings keys. They're implemented on demand. + # This functions (and the similar ones below) do not offer complete + # emulation of all xcode_settings keys. They're implemented on demand. - self.configname = configname - cflags = [] + self.configname = configname + cflags = [] - sdk_root = self._SdkPath() - if 'SDKROOT' in self._Settings() and sdk_root: - cflags.append('-isysroot %s' % sdk_root) + sdk_root = self._SdkPath() + if "SDKROOT" in self._Settings() and sdk_root: + cflags.append("-isysroot %s" % sdk_root) - if self.header_map_path: - cflags.append('-I%s' % self.header_map_path) + if self.header_map_path: + cflags.append("-I%s" % self.header_map_path) - if self._Test('CLANG_WARN_CONSTANT_CONVERSION', 'YES', default='NO'): - cflags.append('-Wconstant-conversion') + if self._Test("CLANG_WARN_CONSTANT_CONVERSION", "YES", default="NO"): + cflags.append("-Wconstant-conversion") - if self._Test('GCC_CHAR_IS_UNSIGNED_CHAR', 'YES', default='NO'): - cflags.append('-funsigned-char') + if self._Test("GCC_CHAR_IS_UNSIGNED_CHAR", "YES", default="NO"): + cflags.append("-funsigned-char") - if self._Test('GCC_CW_ASM_SYNTAX', 'YES', default='YES'): - cflags.append('-fasm-blocks') + if self._Test("GCC_CW_ASM_SYNTAX", "YES", default="YES"): + cflags.append("-fasm-blocks") - if 'GCC_DYNAMIC_NO_PIC' in self._Settings(): - if self._Settings()['GCC_DYNAMIC_NO_PIC'] == 'YES': - cflags.append('-mdynamic-no-pic') - else: - pass - # TODO: In this case, it depends on the target. xcode passes - # mdynamic-no-pic by default for executable and possibly static lib - # according to mento - - if self._Test('GCC_ENABLE_PASCAL_STRINGS', 'YES', default='YES'): - cflags.append('-mpascal-strings') - - self._Appendf(cflags, 'GCC_OPTIMIZATION_LEVEL', '-O%s', default='s') - - if self._Test('GCC_GENERATE_DEBUGGING_SYMBOLS', 'YES', default='YES'): - dbg_format = self._Settings().get('DEBUG_INFORMATION_FORMAT', 'dwarf') - if dbg_format == 'dwarf': - cflags.append('-gdwarf-2') - elif dbg_format == 'stabs': - raise NotImplementedError('stabs debug format is not supported yet.') - elif dbg_format == 'dwarf-with-dsym': - cflags.append('-gdwarf-2') - else: - raise NotImplementedError('Unknown debug format %s' % dbg_format) - - if self._Settings().get('GCC_STRICT_ALIASING') == 'YES': - cflags.append('-fstrict-aliasing') - elif self._Settings().get('GCC_STRICT_ALIASING') == 'NO': - cflags.append('-fno-strict-aliasing') - - if self._Test('GCC_SYMBOLS_PRIVATE_EXTERN', 'YES', default='NO'): - cflags.append('-fvisibility=hidden') - - if self._Test('GCC_TREAT_WARNINGS_AS_ERRORS', 'YES', default='NO'): - cflags.append('-Werror') - - if self._Test('GCC_WARN_ABOUT_MISSING_NEWLINE', 'YES', default='NO'): - cflags.append('-Wnewline-eof') - - # In Xcode, this is only activated when GCC_COMPILER_VERSION is clang or - # llvm-gcc. It also requires a fairly recent libtool, and - # if the system clang isn't used, DYLD_LIBRARY_PATH needs to contain the - # path to the libLTO.dylib that matches the used clang. - if self._Test('LLVM_LTO', 'YES', default='NO'): - cflags.append('-flto') - - self._AppendPlatformVersionMinFlags(cflags) - - # TODO: - if self._Test('COPY_PHASE_STRIP', 'YES', default='NO'): - self._WarnUnimplemented('COPY_PHASE_STRIP') - self._WarnUnimplemented('GCC_DEBUGGING_SYMBOLS') - self._WarnUnimplemented('GCC_ENABLE_OBJC_EXCEPTIONS') - - # TODO: This is exported correctly, but assigning to it is not supported. - self._WarnUnimplemented('MACH_O_TYPE') - self._WarnUnimplemented('PRODUCT_TYPE') - - if arch is not None: - archs = [arch] - else: - assert self.configname - archs = self.GetActiveArchs(self.configname) - if len(archs) != 1: - # TODO: Supporting fat binaries will be annoying. - self._WarnUnimplemented('ARCHS') - archs = ['i386'] - cflags.append('-arch ' + archs[0]) - - if archs[0] in ('i386', 'x86_64'): - if self._Test('GCC_ENABLE_SSE3_EXTENSIONS', 'YES', default='NO'): - cflags.append('-msse3') - if self._Test('GCC_ENABLE_SUPPLEMENTAL_SSE3_INSTRUCTIONS', 'YES', - default='NO'): - cflags.append('-mssse3') # Note 3rd 's'. - if self._Test('GCC_ENABLE_SSE41_EXTENSIONS', 'YES', default='NO'): - cflags.append('-msse4.1') - if self._Test('GCC_ENABLE_SSE42_EXTENSIONS', 'YES', default='NO'): - cflags.append('-msse4.2') - - cflags += self._Settings().get('WARNING_CFLAGS', []) - - if self._IsXCTest(): - platform_root = self._XcodePlatformPath(configname) - if platform_root: - cflags.append('-F' + platform_root + '/Developer/Library/Frameworks/') - - if sdk_root: - framework_root = sdk_root - else: - framework_root = '' - config = self.spec['configurations'][self.configname] - framework_dirs = config.get('mac_framework_dirs', []) - for directory in framework_dirs: - cflags.append('-F' + directory.replace('$(SDKROOT)', framework_root)) - - self.configname = None - return cflags - - def GetCflagsC(self, configname): - """Returns flags that need to be added to .c, and .m compilations.""" - self.configname = configname - cflags_c = [] - if self._Settings().get('GCC_C_LANGUAGE_STANDARD', '') == 'ansi': - cflags_c.append('-ansi') - else: - self._Appendf(cflags_c, 'GCC_C_LANGUAGE_STANDARD', '-std=%s') - cflags_c += self._Settings().get('OTHER_CFLAGS', []) - self.configname = None - return cflags_c - - def GetCflagsCC(self, configname): - """Returns flags that need to be added to .cc, and .mm compilations.""" - self.configname = configname - cflags_cc = [] - - clang_cxx_language_standard = self._Settings().get( - 'CLANG_CXX_LANGUAGE_STANDARD') - # Note: Don't make c++0x to c++11 so that c++0x can be used with older - # clangs that don't understand c++11 yet (like Xcode 4.2's). - if clang_cxx_language_standard: - cflags_cc.append('-std=%s' % clang_cxx_language_standard) - - self._Appendf(cflags_cc, 'CLANG_CXX_LIBRARY', '-stdlib=%s') - - if self._Test('GCC_ENABLE_CPP_RTTI', 'NO', default='YES'): - cflags_cc.append('-fno-rtti') - if self._Test('GCC_ENABLE_CPP_EXCEPTIONS', 'NO', default='YES'): - cflags_cc.append('-fno-exceptions') - if self._Test('GCC_INLINES_ARE_PRIVATE_EXTERN', 'YES', default='NO'): - cflags_cc.append('-fvisibility-inlines-hidden') - if self._Test('GCC_THREADSAFE_STATICS', 'NO', default='YES'): - cflags_cc.append('-fno-threadsafe-statics') - # Note: This flag is a no-op for clang, it only has an effect for gcc. - if self._Test('GCC_WARN_ABOUT_INVALID_OFFSETOF_MACRO', 'NO', default='YES'): - cflags_cc.append('-Wno-invalid-offsetof') - - other_ccflags = [] - - for flag in self._Settings().get('OTHER_CPLUSPLUSFLAGS', ['$(inherited)']): - # TODO: More general variable expansion. Missing in many other places too. - if flag in ('$inherited', '$(inherited)', '${inherited}'): - flag = '$OTHER_CFLAGS' - if flag in ('$OTHER_CFLAGS', '$(OTHER_CFLAGS)', '${OTHER_CFLAGS}'): - other_ccflags += self._Settings().get('OTHER_CFLAGS', []) - else: - other_ccflags.append(flag) - cflags_cc += other_ccflags - - self.configname = None - return cflags_cc - - def _AddObjectiveCGarbageCollectionFlags(self, flags): - gc_policy = self._Settings().get('GCC_ENABLE_OBJC_GC', 'unsupported') - if gc_policy == 'supported': - flags.append('-fobjc-gc') - elif gc_policy == 'required': - flags.append('-fobjc-gc-only') - - def _AddObjectiveCARCFlags(self, flags): - if self._Test('CLANG_ENABLE_OBJC_ARC', 'YES', default='NO'): - flags.append('-fobjc-arc') - - def _AddObjectiveCMissingPropertySynthesisFlags(self, flags): - if self._Test('CLANG_WARN_OBJC_MISSING_PROPERTY_SYNTHESIS', - 'YES', default='NO'): - flags.append('-Wobjc-missing-property-synthesis') - - def GetCflagsObjC(self, configname): - """Returns flags that need to be added to .m compilations.""" - self.configname = configname - cflags_objc = [] - self._AddObjectiveCGarbageCollectionFlags(cflags_objc) - self._AddObjectiveCARCFlags(cflags_objc) - self._AddObjectiveCMissingPropertySynthesisFlags(cflags_objc) - self.configname = None - return cflags_objc - - def GetCflagsObjCC(self, configname): - """Returns flags that need to be added to .mm compilations.""" - self.configname = configname - cflags_objcc = [] - self._AddObjectiveCGarbageCollectionFlags(cflags_objcc) - self._AddObjectiveCARCFlags(cflags_objcc) - self._AddObjectiveCMissingPropertySynthesisFlags(cflags_objcc) - if self._Test('GCC_OBJC_CALL_CXX_CDTORS', 'YES', default='NO'): - cflags_objcc.append('-fobjc-call-cxx-cdtors') - self.configname = None - return cflags_objcc - - def GetInstallNameBase(self): - """Return DYLIB_INSTALL_NAME_BASE for this target.""" - # Xcode sets this for shared_libraries, and for nonbundled loadable_modules. - if (self.spec['type'] != 'shared_library' and - (self.spec['type'] != 'loadable_module' or self._IsBundle())): - return None - install_base = self.GetPerTargetSetting( - 'DYLIB_INSTALL_NAME_BASE', - default='/Library/Frameworks' if self._IsBundle() else '/usr/local/lib') - return install_base - - def _StandardizePath(self, path): - """Do :standardizepath processing for path.""" - # I'm not quite sure what :standardizepath does. Just call normpath(), - # but don't let @executable_path/../foo collapse to foo. - if '/' in path: - prefix, rest = '', path - if path.startswith('@'): - prefix, rest = path.split('/', 1) - rest = os.path.normpath(rest) # :standardizepath - path = os.path.join(prefix, rest) - return path - - def GetInstallName(self): - """Return LD_DYLIB_INSTALL_NAME for this target.""" - # Xcode sets this for shared_libraries, and for nonbundled loadable_modules. - if (self.spec['type'] != 'shared_library' and - (self.spec['type'] != 'loadable_module' or self._IsBundle())): - return None - - default_install_name = \ - '$(DYLIB_INSTALL_NAME_BASE:standardizepath)/$(EXECUTABLE_PATH)' - install_name = self.GetPerTargetSetting( - 'LD_DYLIB_INSTALL_NAME', default=default_install_name) - - # Hardcode support for the variables used in chromium for now, to - # unblock people using the make build. - if '$' in install_name: - assert install_name in ('$(DYLIB_INSTALL_NAME_BASE:standardizepath)/' - '$(WRAPPER_NAME)/$(PRODUCT_NAME)', default_install_name), ( - 'Variables in LD_DYLIB_INSTALL_NAME are not generally supported ' - 'yet in target \'%s\' (got \'%s\')' % - (self.spec['target_name'], install_name)) - - install_name = install_name.replace( - '$(DYLIB_INSTALL_NAME_BASE:standardizepath)', - self._StandardizePath(self.GetInstallNameBase())) - if self._IsBundle(): - # These are only valid for bundles, hence the |if|. - install_name = install_name.replace( - '$(WRAPPER_NAME)', self.GetWrapperName()) - install_name = install_name.replace( - '$(PRODUCT_NAME)', self.GetProductName()) - else: - assert '$(WRAPPER_NAME)' not in install_name - assert '$(PRODUCT_NAME)' not in install_name - - install_name = install_name.replace( - '$(EXECUTABLE_PATH)', self.GetExecutablePath()) - return install_name - - def _MapLinkerFlagFilename(self, ldflag, gyp_to_build_path): - """Checks if ldflag contains a filename and if so remaps it from + if "GCC_DYNAMIC_NO_PIC" in self._Settings(): + if self._Settings()["GCC_DYNAMIC_NO_PIC"] == "YES": + cflags.append("-mdynamic-no-pic") + else: + pass + # TODO: In this case, it depends on the target. xcode passes + # mdynamic-no-pic by default for executable and possibly static lib + # according to mento + + if self._Test("GCC_ENABLE_PASCAL_STRINGS", "YES", default="YES"): + cflags.append("-mpascal-strings") + + self._Appendf(cflags, "GCC_OPTIMIZATION_LEVEL", "-O%s", default="s") + + if self._Test("GCC_GENERATE_DEBUGGING_SYMBOLS", "YES", default="YES"): + dbg_format = self._Settings().get("DEBUG_INFORMATION_FORMAT", "dwarf") + if dbg_format == "dwarf": + cflags.append("-gdwarf-2") + elif dbg_format == "stabs": + raise NotImplementedError("stabs debug format is not supported yet.") + elif dbg_format == "dwarf-with-dsym": + cflags.append("-gdwarf-2") + else: + raise NotImplementedError("Unknown debug format %s" % dbg_format) + + if self._Settings().get("GCC_STRICT_ALIASING") == "YES": + cflags.append("-fstrict-aliasing") + elif self._Settings().get("GCC_STRICT_ALIASING") == "NO": + cflags.append("-fno-strict-aliasing") + + if self._Test("GCC_SYMBOLS_PRIVATE_EXTERN", "YES", default="NO"): + cflags.append("-fvisibility=hidden") + + if self._Test("GCC_TREAT_WARNINGS_AS_ERRORS", "YES", default="NO"): + cflags.append("-Werror") + + if self._Test("GCC_WARN_ABOUT_MISSING_NEWLINE", "YES", default="NO"): + cflags.append("-Wnewline-eof") + + # In Xcode, this is only activated when GCC_COMPILER_VERSION is clang or + # llvm-gcc. It also requires a fairly recent libtool, and + # if the system clang isn't used, DYLD_LIBRARY_PATH needs to contain the + # path to the libLTO.dylib that matches the used clang. + if self._Test("LLVM_LTO", "YES", default="NO"): + cflags.append("-flto") + + self._AppendPlatformVersionMinFlags(cflags) + + # TODO: + if self._Test("COPY_PHASE_STRIP", "YES", default="NO"): + self._WarnUnimplemented("COPY_PHASE_STRIP") + self._WarnUnimplemented("GCC_DEBUGGING_SYMBOLS") + self._WarnUnimplemented("GCC_ENABLE_OBJC_EXCEPTIONS") + + # TODO: This is exported correctly, but assigning to it is not supported. + self._WarnUnimplemented("MACH_O_TYPE") + self._WarnUnimplemented("PRODUCT_TYPE") + + if arch is not None: + archs = [arch] + else: + assert self.configname + archs = self.GetActiveArchs(self.configname) + if len(archs) != 1: + # TODO: Supporting fat binaries will be annoying. + self._WarnUnimplemented("ARCHS") + archs = ["i386"] + cflags.append("-arch " + archs[0]) + + if archs[0] in ("i386", "x86_64"): + if self._Test("GCC_ENABLE_SSE3_EXTENSIONS", "YES", default="NO"): + cflags.append("-msse3") + if self._Test( + "GCC_ENABLE_SUPPLEMENTAL_SSE3_INSTRUCTIONS", "YES", default="NO" + ): + cflags.append("-mssse3") # Note 3rd 's'. + if self._Test("GCC_ENABLE_SSE41_EXTENSIONS", "YES", default="NO"): + cflags.append("-msse4.1") + if self._Test("GCC_ENABLE_SSE42_EXTENSIONS", "YES", default="NO"): + cflags.append("-msse4.2") + + cflags += self._Settings().get("WARNING_CFLAGS", []) + + if self._IsXCTest(): + platform_root = self._XcodePlatformPath(configname) + if platform_root: + cflags.append("-F" + platform_root + "/Developer/Library/Frameworks/") + + if sdk_root: + framework_root = sdk_root + else: + framework_root = "" + config = self.spec["configurations"][self.configname] + framework_dirs = config.get("mac_framework_dirs", []) + for directory in framework_dirs: + cflags.append("-F" + directory.replace("$(SDKROOT)", framework_root)) + + self.configname = None + return cflags + + def GetCflagsC(self, configname): + """Returns flags that need to be added to .c, and .m compilations.""" + self.configname = configname + cflags_c = [] + if self._Settings().get("GCC_C_LANGUAGE_STANDARD", "") == "ansi": + cflags_c.append("-ansi") + else: + self._Appendf(cflags_c, "GCC_C_LANGUAGE_STANDARD", "-std=%s") + cflags_c += self._Settings().get("OTHER_CFLAGS", []) + self.configname = None + return cflags_c + + def GetCflagsCC(self, configname): + """Returns flags that need to be added to .cc, and .mm compilations.""" + self.configname = configname + cflags_cc = [] + + clang_cxx_language_standard = self._Settings().get( + "CLANG_CXX_LANGUAGE_STANDARD" + ) + # Note: Don't make c++0x to c++11 so that c++0x can be used with older + # clangs that don't understand c++11 yet (like Xcode 4.2's). + if clang_cxx_language_standard: + cflags_cc.append("-std=%s" % clang_cxx_language_standard) + + self._Appendf(cflags_cc, "CLANG_CXX_LIBRARY", "-stdlib=%s") + + if self._Test("GCC_ENABLE_CPP_RTTI", "NO", default="YES"): + cflags_cc.append("-fno-rtti") + if self._Test("GCC_ENABLE_CPP_EXCEPTIONS", "NO", default="YES"): + cflags_cc.append("-fno-exceptions") + if self._Test("GCC_INLINES_ARE_PRIVATE_EXTERN", "YES", default="NO"): + cflags_cc.append("-fvisibility-inlines-hidden") + if self._Test("GCC_THREADSAFE_STATICS", "NO", default="YES"): + cflags_cc.append("-fno-threadsafe-statics") + # Note: This flag is a no-op for clang, it only has an effect for gcc. + if self._Test("GCC_WARN_ABOUT_INVALID_OFFSETOF_MACRO", "NO", default="YES"): + cflags_cc.append("-Wno-invalid-offsetof") + + other_ccflags = [] + + for flag in self._Settings().get("OTHER_CPLUSPLUSFLAGS", ["$(inherited)"]): + # TODO: More general variable expansion. Missing in many other places too. + if flag in ("$inherited", "$(inherited)", "${inherited}"): + flag = "$OTHER_CFLAGS" + if flag in ("$OTHER_CFLAGS", "$(OTHER_CFLAGS)", "${OTHER_CFLAGS}"): + other_ccflags += self._Settings().get("OTHER_CFLAGS", []) + else: + other_ccflags.append(flag) + cflags_cc += other_ccflags + + self.configname = None + return cflags_cc + + def _AddObjectiveCGarbageCollectionFlags(self, flags): + gc_policy = self._Settings().get("GCC_ENABLE_OBJC_GC", "unsupported") + if gc_policy == "supported": + flags.append("-fobjc-gc") + elif gc_policy == "required": + flags.append("-fobjc-gc-only") + + def _AddObjectiveCARCFlags(self, flags): + if self._Test("CLANG_ENABLE_OBJC_ARC", "YES", default="NO"): + flags.append("-fobjc-arc") + + def _AddObjectiveCMissingPropertySynthesisFlags(self, flags): + if self._Test( + "CLANG_WARN_OBJC_MISSING_PROPERTY_SYNTHESIS", "YES", default="NO" + ): + flags.append("-Wobjc-missing-property-synthesis") + + def GetCflagsObjC(self, configname): + """Returns flags that need to be added to .m compilations.""" + self.configname = configname + cflags_objc = [] + self._AddObjectiveCGarbageCollectionFlags(cflags_objc) + self._AddObjectiveCARCFlags(cflags_objc) + self._AddObjectiveCMissingPropertySynthesisFlags(cflags_objc) + self.configname = None + return cflags_objc + + def GetCflagsObjCC(self, configname): + """Returns flags that need to be added to .mm compilations.""" + self.configname = configname + cflags_objcc = [] + self._AddObjectiveCGarbageCollectionFlags(cflags_objcc) + self._AddObjectiveCARCFlags(cflags_objcc) + self._AddObjectiveCMissingPropertySynthesisFlags(cflags_objcc) + if self._Test("GCC_OBJC_CALL_CXX_CDTORS", "YES", default="NO"): + cflags_objcc.append("-fobjc-call-cxx-cdtors") + self.configname = None + return cflags_objcc + + def GetInstallNameBase(self): + """Return DYLIB_INSTALL_NAME_BASE for this target.""" + # Xcode sets this for shared_libraries, and for nonbundled loadable_modules. + if self.spec["type"] != "shared_library" and ( + self.spec["type"] != "loadable_module" or self._IsBundle() + ): + return None + install_base = self.GetPerTargetSetting( + "DYLIB_INSTALL_NAME_BASE", + default="/Library/Frameworks" if self._IsBundle() else "/usr/local/lib", + ) + return install_base + + def _StandardizePath(self, path): + """Do :standardizepath processing for path.""" + # I'm not quite sure what :standardizepath does. Just call normpath(), + # but don't let @executable_path/../foo collapse to foo. + if "/" in path: + prefix, rest = "", path + if path.startswith("@"): + prefix, rest = path.split("/", 1) + rest = os.path.normpath(rest) # :standardizepath + path = os.path.join(prefix, rest) + return path + + def GetInstallName(self): + """Return LD_DYLIB_INSTALL_NAME for this target.""" + # Xcode sets this for shared_libraries, and for nonbundled loadable_modules. + if self.spec["type"] != "shared_library" and ( + self.spec["type"] != "loadable_module" or self._IsBundle() + ): + return None + + default_install_name = ( + "$(DYLIB_INSTALL_NAME_BASE:standardizepath)/$(EXECUTABLE_PATH)" + ) + install_name = self.GetPerTargetSetting( + "LD_DYLIB_INSTALL_NAME", default=default_install_name + ) + + # Hardcode support for the variables used in chromium for now, to + # unblock people using the make build. + if "$" in install_name: + assert install_name in ( + "$(DYLIB_INSTALL_NAME_BASE:standardizepath)/" + "$(WRAPPER_NAME)/$(PRODUCT_NAME)", + default_install_name, + ), ( + "Variables in LD_DYLIB_INSTALL_NAME are not generally supported " + "yet in target '%s' (got '%s')" + % (self.spec["target_name"], install_name) + ) + + install_name = install_name.replace( + "$(DYLIB_INSTALL_NAME_BASE:standardizepath)", + self._StandardizePath(self.GetInstallNameBase()), + ) + if self._IsBundle(): + # These are only valid for bundles, hence the |if|. + install_name = install_name.replace( + "$(WRAPPER_NAME)", self.GetWrapperName() + ) + install_name = install_name.replace( + "$(PRODUCT_NAME)", self.GetProductName() + ) + else: + assert "$(WRAPPER_NAME)" not in install_name + assert "$(PRODUCT_NAME)" not in install_name + + install_name = install_name.replace( + "$(EXECUTABLE_PATH)", self.GetExecutablePath() + ) + return install_name + + def _MapLinkerFlagFilename(self, ldflag, gyp_to_build_path): + """Checks if ldflag contains a filename and if so remaps it from gyp-directory-relative to build-directory-relative.""" - # This list is expanded on demand. - # They get matched as: - # -exported_symbols_list file - # -Wl,exported_symbols_list file - # -Wl,exported_symbols_list,file - LINKER_FILE = r'(\S+)' - WORD = r'\S+' - linker_flags = [ - ['-exported_symbols_list', LINKER_FILE], # Needed for NaCl. - ['-unexported_symbols_list', LINKER_FILE], - ['-reexported_symbols_list', LINKER_FILE], - ['-sectcreate', WORD, WORD, LINKER_FILE], # Needed for remoting. - ] - for flag_pattern in linker_flags: - regex = re.compile('(?:-Wl,)?' + '[ ,]'.join(flag_pattern)) - m = regex.match(ldflag) - if m: - ldflag = ldflag[:m.start(1)] + gyp_to_build_path(m.group(1)) + \ - ldflag[m.end(1):] - # Required for ffmpeg (no idea why they don't use LIBRARY_SEARCH_PATHS, - # TODO(thakis): Update ffmpeg.gyp): - if ldflag.startswith('-L'): - ldflag = '-L' + gyp_to_build_path(ldflag[len('-L'):]) - return ldflag - - def GetLdflags(self, configname, product_dir, gyp_to_build_path, arch=None): - """Returns flags that need to be passed to the linker. + # This list is expanded on demand. + # They get matched as: + # -exported_symbols_list file + # -Wl,exported_symbols_list file + # -Wl,exported_symbols_list,file + LINKER_FILE = r"(\S+)" + WORD = r"\S+" + linker_flags = [ + ["-exported_symbols_list", LINKER_FILE], # Needed for NaCl. + ["-unexported_symbols_list", LINKER_FILE], + ["-reexported_symbols_list", LINKER_FILE], + ["-sectcreate", WORD, WORD, LINKER_FILE], # Needed for remoting. + ] + for flag_pattern in linker_flags: + regex = re.compile("(?:-Wl,)?" + "[ ,]".join(flag_pattern)) + m = regex.match(ldflag) + if m: + ldflag = ( + ldflag[: m.start(1)] + + gyp_to_build_path(m.group(1)) + + ldflag[m.end(1) :] + ) + # Required for ffmpeg (no idea why they don't use LIBRARY_SEARCH_PATHS, + # TODO(thakis): Update ffmpeg.gyp): + if ldflag.startswith("-L"): + ldflag = "-L" + gyp_to_build_path(ldflag[len("-L") :]) + return ldflag + + def GetLdflags(self, configname, product_dir, gyp_to_build_path, arch=None): + """Returns flags that need to be passed to the linker. Args: configname: The name of the configuration to get ld flags for. @@ -856,431 +904,470 @@ def GetLdflags(self, configname, product_dir, gyp_to_build_path, arch=None): gyp_to_build_path: A function that converts paths relative to the current gyp file to paths relative to the build directory. """ - self.configname = configname - ldflags = [] + self.configname = configname + ldflags = [] - # The xcode build is relative to a gyp file's directory, and OTHER_LDFLAGS - # can contain entries that depend on this. Explicitly absolutify these. - for ldflag in self._Settings().get('OTHER_LDFLAGS', []): - ldflags.append(self._MapLinkerFlagFilename(ldflag, gyp_to_build_path)) + # The xcode build is relative to a gyp file's directory, and OTHER_LDFLAGS + # can contain entries that depend on this. Explicitly absolutify these. + for ldflag in self._Settings().get("OTHER_LDFLAGS", []): + ldflags.append(self._MapLinkerFlagFilename(ldflag, gyp_to_build_path)) - if self._Test('DEAD_CODE_STRIPPING', 'YES', default='NO'): - ldflags.append('-Wl,-dead_strip') + if self._Test("DEAD_CODE_STRIPPING", "YES", default="NO"): + ldflags.append("-Wl,-dead_strip") - if self._Test('PREBINDING', 'YES', default='NO'): - ldflags.append('-Wl,-prebind') + if self._Test("PREBINDING", "YES", default="NO"): + ldflags.append("-Wl,-prebind") - self._Appendf( - ldflags, 'DYLIB_COMPATIBILITY_VERSION', '-compatibility_version %s') - self._Appendf( - ldflags, 'DYLIB_CURRENT_VERSION', '-current_version %s') + self._Appendf( + ldflags, "DYLIB_COMPATIBILITY_VERSION", "-compatibility_version %s" + ) + self._Appendf(ldflags, "DYLIB_CURRENT_VERSION", "-current_version %s") - self._AppendPlatformVersionMinFlags(ldflags) + self._AppendPlatformVersionMinFlags(ldflags) - if 'SDKROOT' in self._Settings() and self._SdkPath(): - ldflags.append('-isysroot ' + self._SdkPath()) + if "SDKROOT" in self._Settings() and self._SdkPath(): + ldflags.append("-isysroot " + self._SdkPath()) - for library_path in self._Settings().get('LIBRARY_SEARCH_PATHS', []): - ldflags.append('-L' + gyp_to_build_path(library_path)) + for library_path in self._Settings().get("LIBRARY_SEARCH_PATHS", []): + ldflags.append("-L" + gyp_to_build_path(library_path)) - if 'ORDER_FILE' in self._Settings(): - ldflags.append('-Wl,-order_file ' + - '-Wl,' + gyp_to_build_path( - self._Settings()['ORDER_FILE'])) + if "ORDER_FILE" in self._Settings(): + ldflags.append( + "-Wl,-order_file " + + "-Wl," + + gyp_to_build_path(self._Settings()["ORDER_FILE"]) + ) - if arch is not None: - archs = [arch] - else: - assert self.configname - archs = self.GetActiveArchs(self.configname) - if len(archs) != 1: - # TODO: Supporting fat binaries will be annoying. - self._WarnUnimplemented('ARCHS') - archs = ['i386'] - ldflags.append('-arch ' + archs[0]) - - # Xcode adds the product directory by default. - # Rewrite -L. to -L./ to work around http://www.openradar.me/25313838 - ldflags.append('-L' + (product_dir if product_dir != '.' else './')) - - install_name = self.GetInstallName() - if install_name and self.spec['type'] != 'loadable_module': - ldflags.append('-install_name ' + install_name.replace(' ', r'\ ')) - - for rpath in self._Settings().get('LD_RUNPATH_SEARCH_PATHS', []): - ldflags.append('-Wl,-rpath,' + rpath) - - sdk_root = self._SdkPath() - if not sdk_root: - sdk_root = '' - config = self.spec['configurations'][self.configname] - framework_dirs = config.get('mac_framework_dirs', []) - for directory in framework_dirs: - ldflags.append('-F' + directory.replace('$(SDKROOT)', sdk_root)) - - if self._IsXCTest(): - platform_root = self._XcodePlatformPath(configname) - if sdk_root and platform_root: - ldflags.append('-F' + platform_root + '/Developer/Library/Frameworks/') - ldflags.append('-framework XCTest') - - is_extension = self._IsIosAppExtension() or self._IsIosWatchKitExtension() - if sdk_root and is_extension: - # Adds the link flags for extensions. These flags are common for all - # extensions and provide loader and main function. - # These flags reflect the compilation options used by xcode to compile - # extensions. - xcode_version, _ = XcodeVersion() - if xcode_version < '0900': - ldflags.append('-lpkstart') - ldflags.append(sdk_root + - '/System/Library/PrivateFrameworks/PlugInKit.framework/PlugInKit') - else: - ldflags.append('-e _NSExtensionMain') - ldflags.append('-fapplication-extension') - - self._Appendf(ldflags, 'CLANG_CXX_LIBRARY', '-stdlib=%s') - - self.configname = None - return ldflags - - def GetLibtoolflags(self, configname): - """Returns flags that need to be passed to the static linker. + if arch is not None: + archs = [arch] + else: + assert self.configname + archs = self.GetActiveArchs(self.configname) + if len(archs) != 1: + # TODO: Supporting fat binaries will be annoying. + self._WarnUnimplemented("ARCHS") + archs = ["i386"] + ldflags.append("-arch " + archs[0]) + + # Xcode adds the product directory by default. + # Rewrite -L. to -L./ to work around http://www.openradar.me/25313838 + ldflags.append("-L" + (product_dir if product_dir != "." else "./")) + + install_name = self.GetInstallName() + if install_name and self.spec["type"] != "loadable_module": + ldflags.append("-install_name " + install_name.replace(" ", r"\ ")) + + for rpath in self._Settings().get("LD_RUNPATH_SEARCH_PATHS", []): + ldflags.append("-Wl,-rpath," + rpath) + + sdk_root = self._SdkPath() + if not sdk_root: + sdk_root = "" + config = self.spec["configurations"][self.configname] + framework_dirs = config.get("mac_framework_dirs", []) + for directory in framework_dirs: + ldflags.append("-F" + directory.replace("$(SDKROOT)", sdk_root)) + + if self._IsXCTest(): + platform_root = self._XcodePlatformPath(configname) + if sdk_root and platform_root: + ldflags.append("-F" + platform_root + "/Developer/Library/Frameworks/") + ldflags.append("-framework XCTest") + + is_extension = self._IsIosAppExtension() or self._IsIosWatchKitExtension() + if sdk_root and is_extension: + # Adds the link flags for extensions. These flags are common for all + # extensions and provide loader and main function. + # These flags reflect the compilation options used by xcode to compile + # extensions. + xcode_version, _ = XcodeVersion() + if xcode_version < "0900": + ldflags.append("-lpkstart") + ldflags.append( + sdk_root + + "/System/Library/PrivateFrameworks/PlugInKit.framework/PlugInKit" + ) + else: + ldflags.append("-e _NSExtensionMain") + ldflags.append("-fapplication-extension") + + self._Appendf(ldflags, "CLANG_CXX_LIBRARY", "-stdlib=%s") + + self.configname = None + return ldflags + + def GetLibtoolflags(self, configname): + """Returns flags that need to be passed to the static linker. Args: configname: The name of the configuration to get ld flags for. """ - self.configname = configname - libtoolflags = [] + self.configname = configname + libtoolflags = [] - for libtoolflag in self._Settings().get('OTHER_LDFLAGS', []): - libtoolflags.append(libtoolflag) - # TODO(thakis): ARCHS? + for libtoolflag in self._Settings().get("OTHER_LDFLAGS", []): + libtoolflags.append(libtoolflag) + # TODO(thakis): ARCHS? - self.configname = None - return libtoolflags + self.configname = None + return libtoolflags - def GetPerTargetSettings(self): - """Gets a list of all the per-target settings. This will only fetch keys + def GetPerTargetSettings(self): + """Gets a list of all the per-target settings. This will only fetch keys whose values are the same across all configurations.""" - first_pass = True - result = {} - for configname in sorted(self.xcode_settings.keys()): - if first_pass: - result = dict(self.xcode_settings[configname]) - first_pass = False - else: - for key, value in self.xcode_settings[configname].items(): - if key not in result: - continue - elif result[key] != value: - del result[key] - return result - - def GetPerConfigSetting(self, setting, configname, default=None): - if configname in self.xcode_settings: - return self.xcode_settings[configname].get(setting, default) - else: - return self.GetPerTargetSetting(setting, default) + first_pass = True + result = {} + for configname in sorted(self.xcode_settings.keys()): + if first_pass: + result = dict(self.xcode_settings[configname]) + first_pass = False + else: + for key, value in self.xcode_settings[configname].items(): + if key not in result: + continue + elif result[key] != value: + del result[key] + return result + + def GetPerConfigSetting(self, setting, configname, default=None): + if configname in self.xcode_settings: + return self.xcode_settings[configname].get(setting, default) + else: + return self.GetPerTargetSetting(setting, default) - def GetPerTargetSetting(self, setting, default=None): - """Tries to get xcode_settings.setting from spec. Assumes that the setting + def GetPerTargetSetting(self, setting, default=None): + """Tries to get xcode_settings.setting from spec. Assumes that the setting has the same value in all configurations and throws otherwise.""" - is_first_pass = True - result = None - for configname in sorted(self.xcode_settings.keys()): - if is_first_pass: - result = self.xcode_settings[configname].get(setting, None) - is_first_pass = False - else: - assert result == self.xcode_settings[configname].get(setting, None), ( - "Expected per-target setting for '%s', got per-config setting " - "(target %s)" % (setting, self.spec['target_name'])) - if result is None: - return default - return result - - def _GetStripPostbuilds(self, configname, output_binary, quiet): - """Returns a list of shell commands that contain the shell commands + is_first_pass = True + result = None + for configname in sorted(self.xcode_settings.keys()): + if is_first_pass: + result = self.xcode_settings[configname].get(setting, None) + is_first_pass = False + else: + assert result == self.xcode_settings[configname].get(setting, None), ( + "Expected per-target setting for '%s', got per-config setting " + "(target %s)" % (setting, self.spec["target_name"]) + ) + if result is None: + return default + return result + + def _GetStripPostbuilds(self, configname, output_binary, quiet): + """Returns a list of shell commands that contain the shell commands necessary to strip this target's binary. These should be run as postbuilds before the actual postbuilds run.""" - self.configname = configname - - result = [] - if (self._Test('DEPLOYMENT_POSTPROCESSING', 'YES', default='NO') and - self._Test('STRIP_INSTALLED_PRODUCT', 'YES', default='NO')): - - default_strip_style = 'debugging' - if ((self.spec['type'] == 'loadable_module' or self._IsIosAppExtension()) - and self._IsBundle()): - default_strip_style = 'non-global' - elif self.spec['type'] == 'executable': - default_strip_style = 'all' - - strip_style = self._Settings().get('STRIP_STYLE', default_strip_style) - strip_flags = { - 'all': '', - 'non-global': '-x', - 'debugging': '-S', - }[strip_style] - - explicit_strip_flags = self._Settings().get('STRIPFLAGS', '') - if explicit_strip_flags: - strip_flags += ' ' + _NormalizeEnvVarReferences(explicit_strip_flags) - - if not quiet: - result.append('echo STRIP\\(%s\\)' % self.spec['target_name']) - result.append('strip %s %s' % (strip_flags, output_binary)) - - self.configname = None - return result - - def _GetDebugInfoPostbuilds(self, configname, output, output_binary, quiet): - """Returns a list of shell commands that contain the shell commands + self.configname = configname + + result = [] + if self._Test("DEPLOYMENT_POSTPROCESSING", "YES", default="NO") and self._Test( + "STRIP_INSTALLED_PRODUCT", "YES", default="NO" + ): + + default_strip_style = "debugging" + if ( + self.spec["type"] == "loadable_module" or self._IsIosAppExtension() + ) and self._IsBundle(): + default_strip_style = "non-global" + elif self.spec["type"] == "executable": + default_strip_style = "all" + + strip_style = self._Settings().get("STRIP_STYLE", default_strip_style) + strip_flags = {"all": "", "non-global": "-x", "debugging": "-S"}[ + strip_style + ] + + explicit_strip_flags = self._Settings().get("STRIPFLAGS", "") + if explicit_strip_flags: + strip_flags += " " + _NormalizeEnvVarReferences(explicit_strip_flags) + + if not quiet: + result.append("echo STRIP\\(%s\\)" % self.spec["target_name"]) + result.append("strip %s %s" % (strip_flags, output_binary)) + + self.configname = None + return result + + def _GetDebugInfoPostbuilds(self, configname, output, output_binary, quiet): + """Returns a list of shell commands that contain the shell commands necessary to massage this target's debug information. These should be run as postbuilds before the actual postbuilds run.""" - self.configname = configname - - # For static libraries, no dSYMs are created. - result = [] - if (self._Test('GCC_GENERATE_DEBUGGING_SYMBOLS', 'YES', default='YES') and - self._Test( - 'DEBUG_INFORMATION_FORMAT', 'dwarf-with-dsym', default='dwarf') and - self.spec['type'] != 'static_library'): - if not quiet: - result.append('echo DSYMUTIL\\(%s\\)' % self.spec['target_name']) - result.append('dsymutil %s -o %s' % (output_binary, output + '.dSYM')) - - self.configname = None - return result - - def _GetTargetPostbuilds(self, configname, output, output_binary, - quiet=False): - """Returns a list of shell commands that contain the shell commands + self.configname = configname + + # For static libraries, no dSYMs are created. + result = [] + if ( + self._Test("GCC_GENERATE_DEBUGGING_SYMBOLS", "YES", default="YES") + and self._Test( + "DEBUG_INFORMATION_FORMAT", "dwarf-with-dsym", default="dwarf" + ) + and self.spec["type"] != "static_library" + ): + if not quiet: + result.append("echo DSYMUTIL\\(%s\\)" % self.spec["target_name"]) + result.append("dsymutil %s -o %s" % (output_binary, output + ".dSYM")) + + self.configname = None + return result + + def _GetTargetPostbuilds(self, configname, output, output_binary, quiet=False): + """Returns a list of shell commands that contain the shell commands to run as postbuilds for this target, before the actual postbuilds.""" - # dSYMs need to build before stripping happens. - return ( - self._GetDebugInfoPostbuilds(configname, output, output_binary, quiet) + - self._GetStripPostbuilds(configname, output_binary, quiet)) + # dSYMs need to build before stripping happens. + return self._GetDebugInfoPostbuilds( + configname, output, output_binary, quiet + ) + self._GetStripPostbuilds(configname, output_binary, quiet) - def _GetIOSPostbuilds(self, configname, output_binary): - """Return a shell command to codesign the iOS output binary so it can + def _GetIOSPostbuilds(self, configname, output_binary): + """Return a shell command to codesign the iOS output binary so it can be deployed to a device. This should be run as the very last step of the build.""" - if not (self.isIOS and - (self.spec['type'] == 'executable' or self._IsXCTest()) or - self.IsIosFramework()): - return [] - - postbuilds = [] - product_name = self.GetFullProductName() - settings = self.xcode_settings[configname] - - # Xcode expects XCTests to be copied into the TEST_HOST dir. - if self._IsXCTest(): - source = os.path.join("${BUILT_PRODUCTS_DIR}", product_name) - test_host = os.path.dirname(settings.get('TEST_HOST')) - xctest_destination = os.path.join(test_host, 'PlugIns', product_name) - postbuilds.extend(['ditto %s %s' % (source, xctest_destination)]) - - key = self._GetIOSCodeSignIdentityKey(settings) - if not key: - return postbuilds - - # Warn for any unimplemented signing xcode keys. - unimpl = ['OTHER_CODE_SIGN_FLAGS'] - unimpl = set(unimpl) & set(self.xcode_settings[configname].keys()) - if unimpl: - print('Warning: Some codesign keys not implemented, ignoring: %s' % - ', '.join(sorted(unimpl))) - - if self._IsXCTest(): - # For device xctests, Xcode copies two extra frameworks into $TEST_HOST. - test_host = os.path.dirname(settings.get('TEST_HOST')) - frameworks_dir = os.path.join(test_host, 'Frameworks') - platform_root = self._XcodePlatformPath(configname) - frameworks = \ - ['Developer/Library/PrivateFrameworks/IDEBundleInjection.framework', - 'Developer/Library/Frameworks/XCTest.framework'] - for framework in frameworks: - source = os.path.join(platform_root, framework) - destination = os.path.join(frameworks_dir, os.path.basename(framework)) - postbuilds.extend(['ditto %s %s' % (source, destination)]) - - # Then re-sign everything with 'preserve=True' - postbuilds.extend(['%s code-sign-bundle "%s" "%s" "%s" "%s" %s' % ( - os.path.join('${TARGET_BUILD_DIR}', 'gyp-mac-tool'), key, - settings.get('CODE_SIGN_ENTITLEMENTS', ''), - settings.get('PROVISIONING_PROFILE', ''), destination, True) - ]) - plugin_dir = os.path.join(test_host, 'PlugIns') - targets = [os.path.join(plugin_dir, product_name), test_host] - for target in targets: - postbuilds.extend(['%s code-sign-bundle "%s" "%s" "%s" "%s" %s' % ( - os.path.join('${TARGET_BUILD_DIR}', 'gyp-mac-tool'), key, - settings.get('CODE_SIGN_ENTITLEMENTS', ''), - settings.get('PROVISIONING_PROFILE', ''), target, True) - ]) - - postbuilds.extend(['%s code-sign-bundle "%s" "%s" "%s" "%s" %s' % ( - os.path.join('${TARGET_BUILD_DIR}', 'gyp-mac-tool'), key, - settings.get('CODE_SIGN_ENTITLEMENTS', ''), - settings.get('PROVISIONING_PROFILE', ''), - os.path.join("${BUILT_PRODUCTS_DIR}", product_name), False) - ]) - return postbuilds - - def _GetIOSCodeSignIdentityKey(self, settings): - identity = settings.get('CODE_SIGN_IDENTITY') - if not identity: - return None - if identity not in XcodeSettings._codesigning_key_cache: - output = subprocess.check_output( - ['security', 'find-identity', '-p', 'codesigning', '-v']) - for line in output.splitlines(): - if identity in line: - fingerprint = line.split()[1] - cache = XcodeSettings._codesigning_key_cache - assert identity not in cache or fingerprint == cache[identity], ( - "Multiple codesigning fingerprints for identity: %s" % identity) - XcodeSettings._codesigning_key_cache[identity] = fingerprint - return XcodeSettings._codesigning_key_cache.get(identity, '') - - def AddImplicitPostbuilds(self, configname, output, output_binary, - postbuilds=[], quiet=False): - """Returns a list of shell commands that should run before and after + if not ( + self.isIOS + and (self.spec["type"] == "executable" or self._IsXCTest()) + or self.IsIosFramework() + ): + return [] + + postbuilds = [] + product_name = self.GetFullProductName() + settings = self.xcode_settings[configname] + + # Xcode expects XCTests to be copied into the TEST_HOST dir. + if self._IsXCTest(): + source = os.path.join("${BUILT_PRODUCTS_DIR}", product_name) + test_host = os.path.dirname(settings.get("TEST_HOST")) + xctest_destination = os.path.join(test_host, "PlugIns", product_name) + postbuilds.extend(["ditto %s %s" % (source, xctest_destination)]) + + key = self._GetIOSCodeSignIdentityKey(settings) + if not key: + return postbuilds + + # Warn for any unimplemented signing xcode keys. + unimpl = ["OTHER_CODE_SIGN_FLAGS"] + unimpl = set(unimpl) & set(self.xcode_settings[configname].keys()) + if unimpl: + print( + "Warning: Some codesign keys not implemented, ignoring: %s" + % ", ".join(sorted(unimpl)) + ) + + if self._IsXCTest(): + # For device xctests, Xcode copies two extra frameworks into $TEST_HOST. + test_host = os.path.dirname(settings.get("TEST_HOST")) + frameworks_dir = os.path.join(test_host, "Frameworks") + platform_root = self._XcodePlatformPath(configname) + frameworks = [ + "Developer/Library/PrivateFrameworks/IDEBundleInjection.framework", + "Developer/Library/Frameworks/XCTest.framework", + ] + for framework in frameworks: + source = os.path.join(platform_root, framework) + destination = os.path.join(frameworks_dir, os.path.basename(framework)) + postbuilds.extend(["ditto %s %s" % (source, destination)]) + + # Then re-sign everything with 'preserve=True' + postbuilds.extend( + [ + '%s code-sign-bundle "%s" "%s" "%s" "%s" %s' + % ( + os.path.join("${TARGET_BUILD_DIR}", "gyp-mac-tool"), + key, + settings.get("CODE_SIGN_ENTITLEMENTS", ""), + settings.get("PROVISIONING_PROFILE", ""), + destination, + True, + ) + ] + ) + plugin_dir = os.path.join(test_host, "PlugIns") + targets = [os.path.join(plugin_dir, product_name), test_host] + for target in targets: + postbuilds.extend( + [ + '%s code-sign-bundle "%s" "%s" "%s" "%s" %s' + % ( + os.path.join("${TARGET_BUILD_DIR}", "gyp-mac-tool"), + key, + settings.get("CODE_SIGN_ENTITLEMENTS", ""), + settings.get("PROVISIONING_PROFILE", ""), + target, + True, + ) + ] + ) + + postbuilds.extend( + [ + '%s code-sign-bundle "%s" "%s" "%s" "%s" %s' + % ( + os.path.join("${TARGET_BUILD_DIR}", "gyp-mac-tool"), + key, + settings.get("CODE_SIGN_ENTITLEMENTS", ""), + settings.get("PROVISIONING_PROFILE", ""), + os.path.join("${BUILT_PRODUCTS_DIR}", product_name), + False, + ) + ] + ) + return postbuilds + + def _GetIOSCodeSignIdentityKey(self, settings): + identity = settings.get("CODE_SIGN_IDENTITY") + if not identity: + return None + if identity not in XcodeSettings._codesigning_key_cache: + output = subprocess.check_output( + ["security", "find-identity", "-p", "codesigning", "-v"] + ) + for line in output.splitlines(): + if identity in line: + fingerprint = line.split()[1] + cache = XcodeSettings._codesigning_key_cache + assert identity not in cache or fingerprint == cache[identity], ( + "Multiple codesigning fingerprints for identity: %s" % identity + ) + XcodeSettings._codesigning_key_cache[identity] = fingerprint + return XcodeSettings._codesigning_key_cache.get(identity, "") + + def AddImplicitPostbuilds( + self, configname, output, output_binary, postbuilds=[], quiet=False + ): + """Returns a list of shell commands that should run before and after |postbuilds|.""" - assert output_binary is not None - pre = self._GetTargetPostbuilds(configname, output, output_binary, quiet) - post = self._GetIOSPostbuilds(configname, output_binary) - return pre + postbuilds + post - - def _AdjustLibrary(self, library, config_name=None): - if library.endswith('.framework'): - l = '-framework ' + os.path.splitext(os.path.basename(library))[0] - else: - m = self.library_re.match(library) - if m: - l = '-l' + m.group(1) - else: - l = library - - sdk_root = self._SdkPath(config_name) - if not sdk_root: - sdk_root = '' - # Xcode 7 started shipping with ".tbd" (text based stubs) files instead of - # ".dylib" without providing a real support for them. What it does, for - # "/usr/lib" libraries, is do "-L/usr/lib -lname" which is dependent on the - # library order and cause collision when building Chrome. - # - # Instead substitute ".tbd" to ".dylib" in the generated project when the - # following conditions are both true: - # - library is referenced in the gyp file as "$(SDKROOT)/**/*.dylib", - # - the ".dylib" file does not exists but a ".tbd" file do. - library = l.replace('$(SDKROOT)', sdk_root) - if l.startswith('$(SDKROOT)'): - basename, ext = os.path.splitext(library) - if ext == '.dylib' and not os.path.exists(library): - tbd_library = basename + '.tbd' - if os.path.exists(tbd_library): - library = tbd_library - return library - - def AdjustLibraries(self, libraries, config_name=None): - """Transforms entries like 'Cocoa.framework' in libraries into entries like + assert output_binary is not None + pre = self._GetTargetPostbuilds(configname, output, output_binary, quiet) + post = self._GetIOSPostbuilds(configname, output_binary) + return pre + postbuilds + post + + def _AdjustLibrary(self, library, config_name=None): + if library.endswith(".framework"): + l_flag = "-framework " + os.path.splitext(os.path.basename(library))[0] + else: + m = self.library_re.match(library) + if m: + l_flag = "-l" + m.group(1) + else: + l_flag = library + + sdk_root = self._SdkPath(config_name) + if not sdk_root: + sdk_root = "" + # Xcode 7 started shipping with ".tbd" (text based stubs) files instead of + # ".dylib" without providing a real support for them. What it does, for + # "/usr/lib" libraries, is do "-L/usr/lib -lname" which is dependent on the + # library order and cause collision when building Chrome. + # + # Instead substitute ".tbd" to ".dylib" in the generated project when the + # following conditions are both true: + # - library is referenced in the gyp file as "$(SDKROOT)/**/*.dylib", + # - the ".dylib" file does not exists but a ".tbd" file do. + library = l_flag.replace("$(SDKROOT)", sdk_root) + if l_flag.startswith("$(SDKROOT)"): + basename, ext = os.path.splitext(library) + if ext == ".dylib" and not os.path.exists(library): + tbd_library = basename + ".tbd" + if os.path.exists(tbd_library): + library = tbd_library + return library + + def AdjustLibraries(self, libraries, config_name=None): + """Transforms entries like 'Cocoa.framework' in libraries into entries like '-framework Cocoa', 'libcrypto.dylib' into '-lcrypto', etc. """ - libraries = [self._AdjustLibrary(library, config_name) - for library in libraries] - return libraries - - def _BuildMachineOSBuild(self): - return GetStdout(['sw_vers', '-buildVersion']) - - def _XcodeIOSDeviceFamily(self, configname): - family = self.xcode_settings[configname].get('TARGETED_DEVICE_FAMILY', '1') - return [int(x) for x in family.split(',')] - - def GetExtraPlistItems(self, configname=None): - """Returns a dictionary with extra items to insert into Info.plist.""" - if configname not in XcodeSettings._plist_cache: - cache = {} - cache['BuildMachineOSBuild'] = self._BuildMachineOSBuild() - - xcode_version, xcode_build = XcodeVersion() - cache['DTXcode'] = xcode_version - cache['DTXcodeBuild'] = xcode_build - compiler = self.xcode_settings[configname].get('GCC_VERSION') - if compiler is not None: - cache['DTCompiler'] = compiler - - sdk_root = self._SdkRoot(configname) - if not sdk_root: - sdk_root = self._DefaultSdkRoot() - sdk_version = self._GetSdkVersionInfoItem(sdk_root, '--show-sdk-version') - cache['DTSDKName'] = sdk_root + (sdk_version or '') - if xcode_version >= '0720': - cache['DTSDKBuild'] = self._GetSdkVersionInfoItem( - sdk_root, '--show-sdk-build-version') - elif xcode_version >= '0430': - cache['DTSDKBuild'] = sdk_version - else: - cache['DTSDKBuild'] = cache['BuildMachineOSBuild'] - - if self.isIOS: - cache['MinimumOSVersion'] = self.xcode_settings[configname].get( - 'IPHONEOS_DEPLOYMENT_TARGET') - cache['DTPlatformName'] = sdk_root - cache['DTPlatformVersion'] = sdk_version - - if configname.endswith("iphoneos"): - cache['CFBundleSupportedPlatforms'] = ['iPhoneOS'] - cache['DTPlatformBuild'] = cache['DTSDKBuild'] - else: - cache['CFBundleSupportedPlatforms'] = ['iPhoneSimulator'] - # This is weird, but Xcode sets DTPlatformBuild to an empty field - # for simulator builds. - cache['DTPlatformBuild'] = "" - XcodeSettings._plist_cache[configname] = cache - - # Include extra plist items that are per-target, not per global - # XcodeSettings. - items = dict(XcodeSettings._plist_cache[configname]) - if self.isIOS: - items['UIDeviceFamily'] = self._XcodeIOSDeviceFamily(configname) - return items - - def _DefaultSdkRoot(self): - """Returns the default SDKROOT to use. + libraries = [self._AdjustLibrary(library, config_name) for library in libraries] + return libraries + + def _BuildMachineOSBuild(self): + return GetStdout(["sw_vers", "-buildVersion"]) + + def _XcodeIOSDeviceFamily(self, configname): + family = self.xcode_settings[configname].get("TARGETED_DEVICE_FAMILY", "1") + return [int(x) for x in family.split(",")] + + def GetExtraPlistItems(self, configname=None): + """Returns a dictionary with extra items to insert into Info.plist.""" + if configname not in XcodeSettings._plist_cache: + cache = {} + cache["BuildMachineOSBuild"] = self._BuildMachineOSBuild() + + xcode_version, xcode_build = XcodeVersion() + cache["DTXcode"] = xcode_version + cache["DTXcodeBuild"] = xcode_build + compiler = self.xcode_settings[configname].get("GCC_VERSION") + if compiler is not None: + cache["DTCompiler"] = compiler + + sdk_root = self._SdkRoot(configname) + if not sdk_root: + sdk_root = self._DefaultSdkRoot() + sdk_version = self._GetSdkVersionInfoItem(sdk_root, "--show-sdk-version") + cache["DTSDKName"] = sdk_root + (sdk_version or "") + if xcode_version >= "0720": + cache["DTSDKBuild"] = self._GetSdkVersionInfoItem( + sdk_root, "--show-sdk-build-version" + ) + elif xcode_version >= "0430": + cache["DTSDKBuild"] = sdk_version + else: + cache["DTSDKBuild"] = cache["BuildMachineOSBuild"] + + if self.isIOS: + cache["MinimumOSVersion"] = self.xcode_settings[configname].get( + "IPHONEOS_DEPLOYMENT_TARGET" + ) + cache["DTPlatformName"] = sdk_root + cache["DTPlatformVersion"] = sdk_version + + if configname.endswith("iphoneos"): + cache["CFBundleSupportedPlatforms"] = ["iPhoneOS"] + cache["DTPlatformBuild"] = cache["DTSDKBuild"] + else: + cache["CFBundleSupportedPlatforms"] = ["iPhoneSimulator"] + # This is weird, but Xcode sets DTPlatformBuild to an empty field + # for simulator builds. + cache["DTPlatformBuild"] = "" + XcodeSettings._plist_cache[configname] = cache + + # Include extra plist items that are per-target, not per global + # XcodeSettings. + items = dict(XcodeSettings._plist_cache[configname]) + if self.isIOS: + items["UIDeviceFamily"] = self._XcodeIOSDeviceFamily(configname) + return items + + def _DefaultSdkRoot(self): + """Returns the default SDKROOT to use. Prior to version 5.0.0, if SDKROOT was not explicitly set in the Xcode project, then the environment variable was empty. Starting with this version, Xcode uses the name of the newest SDK installed. """ - xcode_version, _ = XcodeVersion() - if xcode_version < '0500': - return '' - default_sdk_path = self._XcodeSdkPath('') - default_sdk_root = XcodeSettings._sdk_root_cache.get(default_sdk_path) - if default_sdk_root: - return default_sdk_root - try: - all_sdks = GetStdout(['xcodebuild', '-showsdks']) - except GypError: - # If xcodebuild fails, there will be no valid SDKs - return '' - for line in all_sdks.splitlines(): - items = line.split() - if len(items) >= 3 and items[-2] == '-sdk': - sdk_root = items[-1] - sdk_path = self._XcodeSdkPath(sdk_root) - if sdk_path == default_sdk_path: - return sdk_root - return '' + xcode_version, _ = XcodeVersion() + if xcode_version < "0500": + return "" + default_sdk_path = self._XcodeSdkPath("") + default_sdk_root = XcodeSettings._sdk_root_cache.get(default_sdk_path) + if default_sdk_root: + return default_sdk_root + try: + all_sdks = GetStdout(["xcodebuild", "-showsdks"]) + except GypError: + # If xcodebuild fails, there will be no valid SDKs + return "" + for line in all_sdks.splitlines(): + items = line.split() + if len(items) >= 3 and items[-2] == "-sdk": + sdk_root = items[-1] + sdk_path = self._XcodeSdkPath(sdk_root) + if sdk_path == default_sdk_path: + return sdk_root + return "" class MacPrefixHeader(object): - """A class that helps with emulating Xcode's GCC_PREFIX_HEADER feature. + """A class that helps with emulating Xcode's GCC_PREFIX_HEADER feature. This feature consists of several pieces: * If GCC_PREFIX_HEADER is present, all compilations in that project get an @@ -1301,9 +1388,11 @@ class MacPrefixHeader(object): system for writing dependencies to the gch files, for writing build commands for the gch files, and for figuring out the location of the gch files. """ - def __init__(self, xcode_settings, - gyp_path_to_build_path, gyp_path_to_build_output): - """If xcode_settings is None, all methods on this class are no-ops. + + def __init__( + self, xcode_settings, gyp_path_to_build_path, gyp_path_to_build_output + ): + """If xcode_settings is None, all methods on this class are no-ops. Args: gyp_path_to_build_path: A function that takes a gyp-relative path, @@ -1313,201 +1402,219 @@ def __init__(self, xcode_settings, to where the output of precompiling that path for that language should be placed (without the trailing '.gch'). """ - # This doesn't support per-configuration prefix headers. Good enough - # for now. - self.header = None - self.compile_headers = False - if xcode_settings: - self.header = xcode_settings.GetPerTargetSetting('GCC_PREFIX_HEADER') - self.compile_headers = xcode_settings.GetPerTargetSetting( - 'GCC_PRECOMPILE_PREFIX_HEADER', default='NO') != 'NO' - self.compiled_headers = {} - if self.header: - if self.compile_headers: - for lang in ['c', 'cc', 'm', 'mm']: - self.compiled_headers[lang] = gyp_path_to_build_output( - self.header, lang) - self.header = gyp_path_to_build_path(self.header) - - def _CompiledHeader(self, lang, arch): - assert self.compile_headers - h = self.compiled_headers[lang] - if arch: - h += '.' + arch - return h - - def GetInclude(self, lang, arch=None): - """Gets the cflags to include the prefix header for language |lang|.""" - if self.compile_headers and lang in self.compiled_headers: - return '-include %s' % self._CompiledHeader(lang, arch) - elif self.header: - return '-include %s' % self.header - else: - return '' + # This doesn't support per-configuration prefix headers. Good enough + # for now. + self.header = None + self.compile_headers = False + if xcode_settings: + self.header = xcode_settings.GetPerTargetSetting("GCC_PREFIX_HEADER") + self.compile_headers = ( + xcode_settings.GetPerTargetSetting( + "GCC_PRECOMPILE_PREFIX_HEADER", default="NO" + ) + != "NO" + ) + self.compiled_headers = {} + if self.header: + if self.compile_headers: + for lang in ["c", "cc", "m", "mm"]: + self.compiled_headers[lang] = gyp_path_to_build_output( + self.header, lang + ) + self.header = gyp_path_to_build_path(self.header) + + def _CompiledHeader(self, lang, arch): + assert self.compile_headers + h = self.compiled_headers[lang] + if arch: + h += "." + arch + return h + + def GetInclude(self, lang, arch=None): + """Gets the cflags to include the prefix header for language |lang|.""" + if self.compile_headers and lang in self.compiled_headers: + return "-include %s" % self._CompiledHeader(lang, arch) + elif self.header: + return "-include %s" % self.header + else: + return "" - def _Gch(self, lang, arch): - """Returns the actual file name of the prefix header for language |lang|.""" - assert self.compile_headers - return self._CompiledHeader(lang, arch) + '.gch' + def _Gch(self, lang, arch): + """Returns the actual file name of the prefix header for language |lang|.""" + assert self.compile_headers + return self._CompiledHeader(lang, arch) + ".gch" - def GetObjDependencies(self, sources, objs, arch=None): - """Given a list of source files and the corresponding object files, returns + def GetObjDependencies(self, sources, objs, arch=None): + """Given a list of source files and the corresponding object files, returns a list of (source, object, gch) tuples, where |gch| is the build-directory relative path to the gch file each object file depends on. |compilable[i]| has to be the source file belonging to |objs[i]|.""" - if not self.header or not self.compile_headers: - return [] - - result = [] - for source, obj in zip(sources, objs): - ext = os.path.splitext(source)[1] - lang = { - '.c': 'c', - '.cpp': 'cc', '.cc': 'cc', '.cxx': 'cc', - '.m': 'm', - '.mm': 'mm', - }.get(ext, None) - if lang: - result.append((source, obj, self._Gch(lang, arch))) - return result - - def GetPchBuildCommands(self, arch=None): - """Returns [(path_to_gch, language_flag, language, header)]. + if not self.header or not self.compile_headers: + return [] + + result = [] + for source, obj in zip(sources, objs): + ext = os.path.splitext(source)[1] + lang = { + ".c": "c", + ".cpp": "cc", + ".cc": "cc", + ".cxx": "cc", + ".m": "m", + ".mm": "mm", + }.get(ext, None) + if lang: + result.append((source, obj, self._Gch(lang, arch))) + return result + + def GetPchBuildCommands(self, arch=None): + """Returns [(path_to_gch, language_flag, language, header)]. |path_to_gch| and |header| are relative to the build directory. """ - if not self.header or not self.compile_headers: - return [] - return [ - (self._Gch('c', arch), '-x c-header', 'c', self.header), - (self._Gch('cc', arch), '-x c++-header', 'cc', self.header), - (self._Gch('m', arch), '-x objective-c-header', 'm', self.header), - (self._Gch('mm', arch), '-x objective-c++-header', 'mm', self.header), - ] + if not self.header or not self.compile_headers: + return [] + return [ + (self._Gch("c", arch), "-x c-header", "c", self.header), + (self._Gch("cc", arch), "-x c++-header", "cc", self.header), + (self._Gch("m", arch), "-x objective-c-header", "m", self.header), + (self._Gch("mm", arch), "-x objective-c++-header", "mm", self.header), + ] def XcodeVersion(): - """Returns a tuple of version and build version of installed Xcode.""" - # `xcodebuild -version` output looks like - # Xcode 4.6.3 - # Build version 4H1503 - # or like - # Xcode 3.2.6 - # Component versions: DevToolsCore-1809.0; DevToolsSupport-1806.0 - # BuildVersion: 10M2518 - # Convert that to ('0463', '4H1503') or ('0326', '10M2518'). - global XCODE_VERSION_CACHE - if XCODE_VERSION_CACHE: + """Returns a tuple of version and build version of installed Xcode.""" + # `xcodebuild -version` output looks like + # Xcode 4.6.3 + # Build version 4H1503 + # or like + # Xcode 3.2.6 + # Component versions: DevToolsCore-1809.0; DevToolsSupport-1806.0 + # BuildVersion: 10M2518 + # Convert that to ('0463', '4H1503') or ('0326', '10M2518'). + global XCODE_VERSION_CACHE + if XCODE_VERSION_CACHE: + return XCODE_VERSION_CACHE + version = "" + build = "" + try: + version_list = GetStdoutQuiet(["xcodebuild", "-version"]).splitlines() + # In some circumstances xcodebuild exits 0 but doesn't return + # the right results; for example, a user on 10.7 or 10.8 with + # a bogus path set via xcode-select + # In that case this may be a CLT-only install so fall back to + # checking that version. + if len(version_list) < 2: + raise GypError("xcodebuild returned unexpected results") + version = version_list[0].split()[-1] # Last word on first line + build = version_list[-1].split()[-1] # Last word on last line + except GypError: # Xcode not installed so look for XCode Command Line Tools + version = CLTVersion() # macOS Catalina returns 11.0.0.0.1.1567737322 + if not version: + raise GypError("No Xcode or CLT version detected!") + # Be careful to convert "4.2.3" to "0423" and "11.0.0" to "1100": + version = version.split(".")[:3] # Just major, minor, micro + version[0] = version[0].zfill(2) # Add a leading zero if major is one digit + version = ("".join(version) + "00")[:4] # Limit to exactly four characters + XCODE_VERSION_CACHE = (version, build) return XCODE_VERSION_CACHE - version = "" - build = "" - try: - version_list = GetStdoutQuiet(['xcodebuild', '-version']).splitlines() - # In some circumstances xcodebuild exits 0 but doesn't return - # the right results; for example, a user on 10.7 or 10.8 with - # a bogus path set via xcode-select - # In that case this may be a CLT-only install so fall back to - # checking that version. - if len(version_list) < 2: - raise GypError("xcodebuild returned unexpected results") - version = version_list[0].split()[-1] # Last word on first line - build = version_list[-1].split()[-1] # Last word on last line - except GypError: # Xcode not installed so look for XCode Command Line Tools - version = CLTVersion() # macOS Catalina returns 11.0.0.0.1.1567737322 - if not version: - raise GypError("No Xcode or CLT version detected!") - # Be careful to convert "4.2.3" to "0423" and "11.0.0" to "1100": - version = version.split(".")[:3] # Just major, minor, micro - version[0] = version[0].zfill(2) # Add a leading zero if major is one digit - version = ("".join(version) + "00")[:4] # Limit to exactly four characters - XCODE_VERSION_CACHE = (version, build) - return XCODE_VERSION_CACHE # This function ported from the logic in Homebrew's CLT version check def CLTVersion(): - """Returns the version of command-line tools from pkgutil.""" - # pkgutil output looks like - # package-id: com.apple.pkg.CLTools_Executables - # version: 5.0.1.0.1.1382131676 - # volume: / - # location: / - # install-time: 1382544035 - # groups: com.apple.FindSystemFiles.pkg-group com.apple.DevToolsBoth.pkg-group com.apple.DevToolsNonRelocatableShared.pkg-group - STANDALONE_PKG_ID = "com.apple.pkg.DeveloperToolsCLILeo" - FROM_XCODE_PKG_ID = "com.apple.pkg.DeveloperToolsCLI" - MAVERICKS_PKG_ID = "com.apple.pkg.CLTools_Executables" - - regex = re.compile('version: (?P.+)') - for key in [MAVERICKS_PKG_ID, STANDALONE_PKG_ID, FROM_XCODE_PKG_ID]: + """Returns the version of command-line tools from pkgutil.""" + # pkgutil output looks like + # package-id: com.apple.pkg.CLTools_Executables + # version: 5.0.1.0.1.1382131676 + # volume: / + # location: / + # install-time: 1382544035 + # groups: com.apple.FindSystemFiles.pkg-group + # com.apple.DevToolsBoth.pkg-group + # com.apple.DevToolsNonRelocatableShared.pkg-group + STANDALONE_PKG_ID = "com.apple.pkg.DeveloperToolsCLILeo" + FROM_XCODE_PKG_ID = "com.apple.pkg.DeveloperToolsCLI" + MAVERICKS_PKG_ID = "com.apple.pkg.CLTools_Executables" + + regex = re.compile("version: (?P.+)") + for key in [MAVERICKS_PKG_ID, STANDALONE_PKG_ID, FROM_XCODE_PKG_ID]: + try: + output = GetStdout(["/usr/sbin/pkgutil", "--pkg-info", key]) + return re.search(regex, output).groupdict()["version"] + except GypError: + continue + + regex = re.compile(r'Command Line Tools for Xcode\s+(?P\S+)') try: - output = GetStdout(['/usr/sbin/pkgutil', '--pkg-info', key]) - return re.search(regex, output).groupdict()['version'] + output = GetStdout(["/usr/sbin/softwareupdate", "--history"]) + return re.search(regex, output).groupdict()["version"] except GypError: - continue + return None def GetStdoutQuiet(cmdlist): - """Returns the content of standard output returned by invoking |cmdlist|. + """Returns the content of standard output returned by invoking |cmdlist|. Ignores the stderr. Raises |GypError| if the command return with a non-zero return code.""" - job = subprocess.Popen(cmdlist, stdout=subprocess.PIPE, - stderr=subprocess.PIPE) - out = job.communicate()[0] - if PY3: - out = out.decode("utf-8") - if job.returncode != 0: - raise GypError('Error %d running %s' % (job.returncode, cmdlist[0])) - return out.rstrip('\n') + job = subprocess.Popen(cmdlist, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + out = job.communicate()[0] + if PY3: + out = out.decode("utf-8") + if job.returncode != 0: + raise GypError("Error %d running %s" % (job.returncode, cmdlist[0])) + return out.rstrip("\n") def GetStdout(cmdlist): - """Returns the content of standard output returned by invoking |cmdlist|. + """Returns the content of standard output returned by invoking |cmdlist|. Raises |GypError| if the command return with a non-zero return code.""" - job = subprocess.Popen(cmdlist, stdout=subprocess.PIPE) - out = job.communicate()[0] - if PY3: - out = out.decode("utf-8") - if job.returncode != 0: - sys.stderr.write(out + '\n') - raise GypError('Error %d running %s' % (job.returncode, cmdlist[0])) - return out.rstrip('\n') + job = subprocess.Popen(cmdlist, stdout=subprocess.PIPE) + out = job.communicate()[0] + if PY3: + out = out.decode("utf-8") + if job.returncode != 0: + sys.stderr.write(out + "\n") + raise GypError("Error %d running %s" % (job.returncode, cmdlist[0])) + return out.rstrip("\n") def MergeGlobalXcodeSettingsToSpec(global_dict, spec): - """Merges the global xcode_settings dictionary into each configuration of the + """Merges the global xcode_settings dictionary into each configuration of the target represented by spec. For keys that are both in the global and the local xcode_settings dict, the local key gets precedence. """ - # The xcode generator special-cases global xcode_settings and does something - # that amounts to merging in the global xcode_settings into each local - # xcode_settings dict. - global_xcode_settings = global_dict.get('xcode_settings', {}) - for config in spec['configurations'].values(): - if 'xcode_settings' in config: - new_settings = global_xcode_settings.copy() - new_settings.update(config['xcode_settings']) - config['xcode_settings'] = new_settings + # The xcode generator special-cases global xcode_settings and does something + # that amounts to merging in the global xcode_settings into each local + # xcode_settings dict. + global_xcode_settings = global_dict.get("xcode_settings", {}) + for config in spec["configurations"].values(): + if "xcode_settings" in config: + new_settings = global_xcode_settings.copy() + new_settings.update(config["xcode_settings"]) + config["xcode_settings"] = new_settings def IsMacBundle(flavor, spec): - """Returns if |spec| should be treated as a bundle. + """Returns if |spec| should be treated as a bundle. Bundles are directories with a certain subdirectory structure, instead of just a single file. Bundle rules do not produce a binary but also package resources into that directory.""" - is_mac_bundle = int(spec.get('mac_xctest_bundle', 0)) != 0 or \ - int(spec.get('mac_xcuitest_bundle', 0)) != 0 or \ - (int(spec.get('mac_bundle', 0)) != 0 and flavor == 'mac') + is_mac_bundle = ( + int(spec.get("mac_xctest_bundle", 0)) != 0 + or int(spec.get("mac_xcuitest_bundle", 0)) != 0 + or (int(spec.get("mac_bundle", 0)) != 0 and flavor == "mac") + ) - if is_mac_bundle: - assert spec['type'] != 'none', ( - 'mac_bundle targets cannot have type none (target "%s")' % - spec['target_name']) - return is_mac_bundle + if is_mac_bundle: + assert spec["type"] != "none", ( + 'mac_bundle targets cannot have type none (target "%s")' + % spec["target_name"] + ) + return is_mac_bundle def GetMacBundleResources(product_dir, xcode_settings, resources): - """Yields (output, resource) pairs for every resource in |resources|. + """Yields (output, resource) pairs for every resource in |resources|. Only call this for mac bundle targets. Args: @@ -1516,38 +1623,36 @@ def GetMacBundleResources(product_dir, xcode_settings, resources): xcode_settings: The XcodeSettings of the current target. resources: A list of bundle resources, relative to the build directory. """ - dest = os.path.join(product_dir, - xcode_settings.GetBundleResourceFolder()) - for res in resources: - output = dest + dest = os.path.join(product_dir, xcode_settings.GetBundleResourceFolder()) + for res in resources: + output = dest - # The make generator doesn't support it, so forbid it everywhere - # to keep the generators more interchangeable. - assert ' ' not in res, ( - "Spaces in resource filenames not supported (%s)" % res) + # The make generator doesn't support it, so forbid it everywhere + # to keep the generators more interchangeable. + assert " " not in res, "Spaces in resource filenames not supported (%s)" % res - # Split into (path,file). - res_parts = os.path.split(res) + # Split into (path,file). + res_parts = os.path.split(res) - # Now split the path into (prefix,maybe.lproj). - lproj_parts = os.path.split(res_parts[0]) - # If the resource lives in a .lproj bundle, add that to the destination. - if lproj_parts[1].endswith('.lproj'): - output = os.path.join(output, lproj_parts[1]) + # Now split the path into (prefix,maybe.lproj). + lproj_parts = os.path.split(res_parts[0]) + # If the resource lives in a .lproj bundle, add that to the destination. + if lproj_parts[1].endswith(".lproj"): + output = os.path.join(output, lproj_parts[1]) - output = os.path.join(output, res_parts[1]) - # Compiled XIB files are referred to by .nib. - if output.endswith('.xib'): - output = os.path.splitext(output)[0] + '.nib' - # Compiled storyboard files are referred to by .storyboardc. - if output.endswith('.storyboard'): - output = os.path.splitext(output)[0] + '.storyboardc' + output = os.path.join(output, res_parts[1]) + # Compiled XIB files are referred to by .nib. + if output.endswith(".xib"): + output = os.path.splitext(output)[0] + ".nib" + # Compiled storyboard files are referred to by .storyboardc. + if output.endswith(".storyboard"): + output = os.path.splitext(output)[0] + ".storyboardc" - yield output, res + yield output, res def GetMacInfoPlist(product_dir, xcode_settings, gyp_path_to_build_path): - """Returns (info_plist, dest_plist, defines, extra_env), where: + """Returns (info_plist, dest_plist, defines, extra_env), where: * |info_plist| is the source plist path, relative to the build directory, * |dest_plist| is the destination plist path, relative to the @@ -1566,36 +1671,43 @@ def GetMacInfoPlist(product_dir, xcode_settings, gyp_path_to_build_path): gyp_to_build_path: A function that converts paths relative to the current gyp file to paths relative to the build directory. """ - info_plist = xcode_settings.GetPerTargetSetting('INFOPLIST_FILE') - if not info_plist: - return None, None, [], {} - - # The make generator doesn't support it, so forbid it everywhere - # to keep the generators more interchangeable. - assert ' ' not in info_plist, ( - "Spaces in Info.plist filenames not supported (%s)" % info_plist) + info_plist = xcode_settings.GetPerTargetSetting("INFOPLIST_FILE") + if not info_plist: + return None, None, [], {} - info_plist = gyp_path_to_build_path(info_plist) - - # If explicitly set to preprocess the plist, invoke the C preprocessor and - # specify any defines as -D flags. - if xcode_settings.GetPerTargetSetting( - 'INFOPLIST_PREPROCESS', default='NO') == 'YES': - # Create an intermediate file based on the path. - defines = shlex.split(xcode_settings.GetPerTargetSetting( - 'INFOPLIST_PREPROCESSOR_DEFINITIONS', default='')) - else: - defines = [] + # The make generator doesn't support it, so forbid it everywhere + # to keep the generators more interchangeable. + assert " " not in info_plist, ( + "Spaces in Info.plist filenames not supported (%s)" % info_plist + ) + + info_plist = gyp_path_to_build_path(info_plist) + + # If explicitly set to preprocess the plist, invoke the C preprocessor and + # specify any defines as -D flags. + if ( + xcode_settings.GetPerTargetSetting("INFOPLIST_PREPROCESS", default="NO") + == "YES" + ): + # Create an intermediate file based on the path. + defines = shlex.split( + xcode_settings.GetPerTargetSetting( + "INFOPLIST_PREPROCESSOR_DEFINITIONS", default="" + ) + ) + else: + defines = [] - dest_plist = os.path.join(product_dir, xcode_settings.GetBundlePlistPath()) - extra_env = xcode_settings.GetPerTargetSettings() + dest_plist = os.path.join(product_dir, xcode_settings.GetBundlePlistPath()) + extra_env = xcode_settings.GetPerTargetSettings() - return info_plist, dest_plist, defines, extra_env + return info_plist, dest_plist, defines, extra_env -def _GetXcodeEnv(xcode_settings, built_products_dir, srcroot, configuration, - additional_settings=None): - """Return the environment variables that Xcode would set. See +def _GetXcodeEnv( + xcode_settings, built_products_dir, srcroot, configuration, additional_settings=None +): + """Return the environment variables that Xcode would set. See http://developer.apple.com/library/mac/#documentation/DeveloperTools/Reference/XcodeBuildSettingRef/1-Build_Setting_Reference/build_setting_ref.html#//apple_ref/doc/uid/TP40003931-CH3-SW153 for a full list. @@ -1609,209 +1721,221 @@ def _GetXcodeEnv(xcode_settings, built_products_dir, srcroot, configuration, result. """ - if not xcode_settings: return {} - - # This function is considered a friend of XcodeSettings, so let it reach into - # its implementation details. - spec = xcode_settings.spec - - # These are filled in on an as-needed basis. - env = { - 'BUILT_FRAMEWORKS_DIR' : built_products_dir, - 'BUILT_PRODUCTS_DIR' : built_products_dir, - 'CONFIGURATION' : configuration, - 'PRODUCT_NAME' : xcode_settings.GetProductName(), - # See /Developer/Platforms/MacOSX.platform/Developer/Library/Xcode/Specifications/MacOSX\ Product\ Types.xcspec for FULL_PRODUCT_NAME - 'SRCROOT' : srcroot, - 'SOURCE_ROOT': '${SRCROOT}', - # This is not true for static libraries, but currently the env is only - # written for bundles: - 'TARGET_BUILD_DIR' : built_products_dir, - 'TEMP_DIR' : '${TMPDIR}', - 'XCODE_VERSION_ACTUAL' : XcodeVersion()[0], - } - if xcode_settings.GetPerConfigSetting('SDKROOT', configuration): - env['SDKROOT'] = xcode_settings._SdkPath(configuration) - else: - env['SDKROOT'] = '' - - if xcode_settings.mac_toolchain_dir: - env['DEVELOPER_DIR'] = xcode_settings.mac_toolchain_dir - - if spec['type'] in ( - 'executable', 'static_library', 'shared_library', 'loadable_module'): - env['EXECUTABLE_NAME'] = xcode_settings.GetExecutableName() - env['EXECUTABLE_PATH'] = xcode_settings.GetExecutablePath() - env['FULL_PRODUCT_NAME'] = xcode_settings.GetFullProductName() - mach_o_type = xcode_settings.GetMachOType() - if mach_o_type: - env['MACH_O_TYPE'] = mach_o_type - env['PRODUCT_TYPE'] = xcode_settings.GetProductType() - if xcode_settings._IsBundle(): - # xcodeproj_file.py sets the same Xcode subfolder value for this as for - # FRAMEWORKS_FOLDER_PATH so Xcode builds will actually use FFP's value. - env['BUILT_FRAMEWORKS_DIR'] = \ - os.path.join(built_products_dir + os.sep \ - + xcode_settings.GetBundleFrameworksFolderPath()) - env['CONTENTS_FOLDER_PATH'] = \ - xcode_settings.GetBundleContentsFolderPath() - env['EXECUTABLE_FOLDER_PATH'] = \ - xcode_settings.GetBundleExecutableFolderPath() - env['UNLOCALIZED_RESOURCES_FOLDER_PATH'] = \ - xcode_settings.GetBundleResourceFolder() - env['JAVA_FOLDER_PATH'] = xcode_settings.GetBundleJavaFolderPath() - env['FRAMEWORKS_FOLDER_PATH'] = \ - xcode_settings.GetBundleFrameworksFolderPath() - env['SHARED_FRAMEWORKS_FOLDER_PATH'] = \ - xcode_settings.GetBundleSharedFrameworksFolderPath() - env['SHARED_SUPPORT_FOLDER_PATH'] = \ - xcode_settings.GetBundleSharedSupportFolderPath() - env['PLUGINS_FOLDER_PATH'] = xcode_settings.GetBundlePlugInsFolderPath() - env['XPCSERVICES_FOLDER_PATH'] = \ - xcode_settings.GetBundleXPCServicesFolderPath() - env['INFOPLIST_PATH'] = xcode_settings.GetBundlePlistPath() - env['WRAPPER_NAME'] = xcode_settings.GetWrapperName() - - install_name = xcode_settings.GetInstallName() - if install_name: - env['LD_DYLIB_INSTALL_NAME'] = install_name - install_name_base = xcode_settings.GetInstallNameBase() - if install_name_base: - env['DYLIB_INSTALL_NAME_BASE'] = install_name_base - xcode_version, _ = XcodeVersion() - if xcode_version >= '0500' and not env.get('SDKROOT'): - sdk_root = xcode_settings._SdkRoot(configuration) - if not sdk_root: - sdk_root = xcode_settings._XcodeSdkPath('') - if sdk_root is None: - sdk_root = '' - env['SDKROOT'] = sdk_root - - if not additional_settings: - additional_settings = {} - else: - # Flatten lists to strings. - for k in additional_settings: - if not isinstance(additional_settings[k], str): - additional_settings[k] = ' '.join(additional_settings[k]) - additional_settings.update(env) + if not xcode_settings: + return {} + + # This function is considered a friend of XcodeSettings, so let it reach into + # its implementation details. + spec = xcode_settings.spec + + # These are filled in on an as-needed basis. + env = { + "BUILT_FRAMEWORKS_DIR": built_products_dir, + "BUILT_PRODUCTS_DIR": built_products_dir, + "CONFIGURATION": configuration, + "PRODUCT_NAME": xcode_settings.GetProductName(), + # For FULL_PRODUCT_NAME see: + # /Developer/Platforms/MacOSX.platform/Developer/Library/Xcode/Specifications/MacOSX\ Product\ Types.xcspec # noqa: E501 + "SRCROOT": srcroot, + "SOURCE_ROOT": "${SRCROOT}", + # This is not true for static libraries, but currently the env is only + # written for bundles: + "TARGET_BUILD_DIR": built_products_dir, + "TEMP_DIR": "${TMPDIR}", + "XCODE_VERSION_ACTUAL": XcodeVersion()[0], + } + if xcode_settings.GetPerConfigSetting("SDKROOT", configuration): + env["SDKROOT"] = xcode_settings._SdkPath(configuration) + else: + env["SDKROOT"] = "" + + if xcode_settings.mac_toolchain_dir: + env["DEVELOPER_DIR"] = xcode_settings.mac_toolchain_dir + + if spec["type"] in ( + "executable", + "static_library", + "shared_library", + "loadable_module", + ): + env["EXECUTABLE_NAME"] = xcode_settings.GetExecutableName() + env["EXECUTABLE_PATH"] = xcode_settings.GetExecutablePath() + env["FULL_PRODUCT_NAME"] = xcode_settings.GetFullProductName() + mach_o_type = xcode_settings.GetMachOType() + if mach_o_type: + env["MACH_O_TYPE"] = mach_o_type + env["PRODUCT_TYPE"] = xcode_settings.GetProductType() + if xcode_settings._IsBundle(): + # xcodeproj_file.py sets the same Xcode subfolder value for this as for + # FRAMEWORKS_FOLDER_PATH so Xcode builds will actually use FFP's value. + env["BUILT_FRAMEWORKS_DIR"] = os.path.join( + built_products_dir + os.sep + xcode_settings.GetBundleFrameworksFolderPath() + ) + env["CONTENTS_FOLDER_PATH"] = xcode_settings.GetBundleContentsFolderPath() + env["EXECUTABLE_FOLDER_PATH"] = xcode_settings.GetBundleExecutableFolderPath() + env[ + "UNLOCALIZED_RESOURCES_FOLDER_PATH" + ] = xcode_settings.GetBundleResourceFolder() + env["JAVA_FOLDER_PATH"] = xcode_settings.GetBundleJavaFolderPath() + env["FRAMEWORKS_FOLDER_PATH"] = xcode_settings.GetBundleFrameworksFolderPath() + env[ + "SHARED_FRAMEWORKS_FOLDER_PATH" + ] = xcode_settings.GetBundleSharedFrameworksFolderPath() + env[ + "SHARED_SUPPORT_FOLDER_PATH" + ] = xcode_settings.GetBundleSharedSupportFolderPath() + env["PLUGINS_FOLDER_PATH"] = xcode_settings.GetBundlePlugInsFolderPath() + env["XPCSERVICES_FOLDER_PATH"] = xcode_settings.GetBundleXPCServicesFolderPath() + env["INFOPLIST_PATH"] = xcode_settings.GetBundlePlistPath() + env["WRAPPER_NAME"] = xcode_settings.GetWrapperName() + + install_name = xcode_settings.GetInstallName() + if install_name: + env["LD_DYLIB_INSTALL_NAME"] = install_name + install_name_base = xcode_settings.GetInstallNameBase() + if install_name_base: + env["DYLIB_INSTALL_NAME_BASE"] = install_name_base + xcode_version, _ = XcodeVersion() + if xcode_version >= "0500" and not env.get("SDKROOT"): + sdk_root = xcode_settings._SdkRoot(configuration) + if not sdk_root: + sdk_root = xcode_settings._XcodeSdkPath("") + if sdk_root is None: + sdk_root = "" + env["SDKROOT"] = sdk_root + + if not additional_settings: + additional_settings = {} + else: + # Flatten lists to strings. + for k in additional_settings: + if not isinstance(additional_settings[k], str): + additional_settings[k] = " ".join(additional_settings[k]) + additional_settings.update(env) - for k in additional_settings: - additional_settings[k] = _NormalizeEnvVarReferences(additional_settings[k]) + for k in additional_settings: + additional_settings[k] = _NormalizeEnvVarReferences(additional_settings[k]) - return additional_settings + return additional_settings def _NormalizeEnvVarReferences(str): - """Takes a string containing variable references in the form ${FOO}, $(FOO), + """Takes a string containing variable references in the form ${FOO}, $(FOO), or $FOO, and returns a string with all variable references in the form ${FOO}. """ - # $FOO -> ${FOO} - str = re.sub(r'\$([a-zA-Z_][a-zA-Z0-9_]*)', r'${\1}', str) + # $FOO -> ${FOO} + str = re.sub(r"\$([a-zA-Z_][a-zA-Z0-9_]*)", r"${\1}", str) - # $(FOO) -> ${FOO} - matches = re.findall(r'(\$\(([a-zA-Z0-9\-_]+)\))', str) - for match in matches: - to_replace, variable = match - assert '$(' not in match, '$($(FOO)) variables not supported: ' + match - str = str.replace(to_replace, '${' + variable + '}') + # $(FOO) -> ${FOO} + matches = re.findall(r"(\$\(([a-zA-Z0-9\-_]+)\))", str) + for match in matches: + to_replace, variable = match + assert "$(" not in match, "$($(FOO)) variables not supported: " + match + str = str.replace(to_replace, "${" + variable + "}") - return str + return str def ExpandEnvVars(string, expansions): - """Expands ${VARIABLES}, $(VARIABLES), and $VARIABLES in string per the + """Expands ${VARIABLES}, $(VARIABLES), and $VARIABLES in string per the expansions list. If the variable expands to something that references another variable, this variable is expanded as well if it's in env -- until no variables present in env are left.""" - for k, v in reversed(expansions): - string = string.replace('${' + k + '}', v) - string = string.replace('$(' + k + ')', v) - string = string.replace('$' + k, v) - return string + for k, v in reversed(expansions): + string = string.replace("${" + k + "}", v) + string = string.replace("$(" + k + ")", v) + string = string.replace("$" + k, v) + return string def _TopologicallySortedEnvVarKeys(env): - """Takes a dict |env| whose values are strings that can refer to other keys, + """Takes a dict |env| whose values are strings that can refer to other keys, for example env['foo'] = '$(bar) and $(baz)'. Returns a list L of all keys of env such that key2 is after key1 in L if env[key2] refers to env[key1]. Throws an Exception in case of dependency cycles. """ - # Since environment variables can refer to other variables, the evaluation - # order is important. Below is the logic to compute the dependency graph - # and sort it. - regex = re.compile(r'\$\{([a-zA-Z0-9\-_]+)\}') - def GetEdges(node): - # Use a definition of edges such that user_of_variable -> used_varible. - # This happens to be easier in this case, since a variable's - # definition contains all variables it references in a single string. - # We can then reverse the result of the topological sort at the end. - # Since: reverse(topsort(DAG)) = topsort(reverse_edges(DAG)) - matches = set([v for v in regex.findall(env[node]) if v in env]) - for dependee in matches: - assert '${' not in dependee, 'Nested variables not supported: ' + dependee - return matches - - try: - # Topologically sort, and then reverse, because we used an edge definition - # that's inverted from the expected result of this function (see comment - # above). - order = gyp.common.TopologicallySorted(env.keys(), GetEdges) - order.reverse() - return order - except gyp.common.CycleError as e: - raise GypError( - 'Xcode environment variables are cyclically dependent: ' + str(e.nodes)) - - -def GetSortedXcodeEnv(xcode_settings, built_products_dir, srcroot, - configuration, additional_settings=None): - env = _GetXcodeEnv(xcode_settings, built_products_dir, srcroot, configuration, - additional_settings) - return [(key, env[key]) for key in _TopologicallySortedEnvVarKeys(env)] + # Since environment variables can refer to other variables, the evaluation + # order is important. Below is the logic to compute the dependency graph + # and sort it. + regex = re.compile(r"\$\{([a-zA-Z0-9\-_]+)\}") + + def GetEdges(node): + # Use a definition of edges such that user_of_variable -> used_varible. + # This happens to be easier in this case, since a variable's + # definition contains all variables it references in a single string. + # We can then reverse the result of the topological sort at the end. + # Since: reverse(topsort(DAG)) = topsort(reverse_edges(DAG)) + matches = set([v for v in regex.findall(env[node]) if v in env]) + for dependee in matches: + assert "${" not in dependee, "Nested variables not supported: " + dependee + return matches + + try: + # Topologically sort, and then reverse, because we used an edge definition + # that's inverted from the expected result of this function (see comment + # above). + order = gyp.common.TopologicallySorted(env.keys(), GetEdges) + order.reverse() + return order + except gyp.common.CycleError as e: + raise GypError( + "Xcode environment variables are cyclically dependent: " + str(e.nodes) + ) + + +def GetSortedXcodeEnv( + xcode_settings, built_products_dir, srcroot, configuration, additional_settings=None +): + env = _GetXcodeEnv( + xcode_settings, built_products_dir, srcroot, configuration, additional_settings + ) + return [(key, env[key]) for key in _TopologicallySortedEnvVarKeys(env)] def GetSpecPostbuildCommands(spec, quiet=False): - """Returns the list of postbuilds explicitly defined on |spec|, in a form + """Returns the list of postbuilds explicitly defined on |spec|, in a form executable by a shell.""" - postbuilds = [] - for postbuild in spec.get('postbuilds', []): - if not quiet: - postbuilds.append('echo POSTBUILD\\(%s\\) %s' % ( - spec['target_name'], postbuild['postbuild_name'])) - postbuilds.append(gyp.common.EncodePOSIXShellList(postbuild['action'])) - return postbuilds + postbuilds = [] + for postbuild in spec.get("postbuilds", []): + if not quiet: + postbuilds.append( + "echo POSTBUILD\\(%s\\) %s" + % (spec["target_name"], postbuild["postbuild_name"]) + ) + postbuilds.append(gyp.common.EncodePOSIXShellList(postbuild["action"])) + return postbuilds def _HasIOSTarget(targets): - """Returns true if any target contains the iOS specific key + """Returns true if any target contains the iOS specific key IPHONEOS_DEPLOYMENT_TARGET.""" - for target_dict in targets.values(): - for config in target_dict['configurations'].values(): - if config.get('xcode_settings', {}).get('IPHONEOS_DEPLOYMENT_TARGET'): - return True - return False + for target_dict in targets.values(): + for config in target_dict["configurations"].values(): + if config.get("xcode_settings", {}).get("IPHONEOS_DEPLOYMENT_TARGET"): + return True + return False def _AddIOSDeviceConfigurations(targets): - """Clone all targets and append -iphoneos to the name. Configure these targets + """Clone all targets and append -iphoneos to the name. Configure these targets to build for iOS devices and use correct architectures for those builds.""" - for target_dict in targets.values(): - toolset = target_dict['toolset'] - configs = target_dict['configurations'] - for config_name, simulator_config_dict in dict(configs).items(): - iphoneos_config_dict = copy.deepcopy(simulator_config_dict) - configs[config_name + '-iphoneos'] = iphoneos_config_dict - configs[config_name + '-iphonesimulator'] = simulator_config_dict - if toolset == 'target': - simulator_config_dict['xcode_settings']['SDKROOT'] = 'iphonesimulator' - iphoneos_config_dict['xcode_settings']['SDKROOT'] = 'iphoneos' - return targets + for target_dict in targets.values(): + toolset = target_dict["toolset"] + configs = target_dict["configurations"] + for config_name, simulator_config_dict in dict(configs).items(): + iphoneos_config_dict = copy.deepcopy(simulator_config_dict) + configs[config_name + "-iphoneos"] = iphoneos_config_dict + configs[config_name + "-iphonesimulator"] = simulator_config_dict + if toolset == "target": + simulator_config_dict["xcode_settings"]["SDKROOT"] = "iphonesimulator" + iphoneos_config_dict["xcode_settings"]["SDKROOT"] = "iphoneos" + return targets + def CloneConfigurationForDeviceAndEmulator(target_dicts): - """If |target_dicts| contains any iOS targets, automatically create -iphoneos + """If |target_dicts| contains any iOS targets, automatically create -iphoneos targets for iOS device builds.""" - if _HasIOSTarget(target_dicts): - return _AddIOSDeviceConfigurations(target_dicts) - return target_dicts + if _HasIOSTarget(target_dicts): + return _AddIOSDeviceConfigurations(target_dicts) + return target_dicts diff --git a/tools/gyp/pylib/gyp/xcode_ninja.py b/tools/gyp/pylib/gyp/xcode_ninja.py index 2bc2143340229b..10ddcbccd0310b 100644 --- a/tools/gyp/pylib/gyp/xcode_ninja.py +++ b/tools/gyp/pylib/gyp/xcode_ninja.py @@ -20,116 +20,122 @@ def _WriteWorkspace(main_gyp, sources_gyp, params): - """ Create a workspace to wrap main and sources gyp paths. """ - (build_file_root, build_file_ext) = os.path.splitext(main_gyp) - workspace_path = build_file_root + '.xcworkspace' - options = params['options'] - if options.generator_output: - workspace_path = os.path.join(options.generator_output, workspace_path) - try: - os.makedirs(workspace_path) - except OSError as e: - if e.errno != errno.EEXIST: - raise - output_string = '\n' + \ - '\n' - for gyp_name in [main_gyp, sources_gyp]: - name = os.path.splitext(os.path.basename(gyp_name))[0] + '.xcodeproj' - name = xml.sax.saxutils.quoteattr("group:" + name) - output_string += ' \n' % name - output_string += '\n' - - workspace_file = os.path.join(workspace_path, "contents.xcworkspacedata") - - try: - with open(workspace_file, 'r') as input_file: - input_string = input_file.read() - if input_string == output_string: - return - except IOError: - # Ignore errors if the file doesn't exist. - pass - - with open(workspace_file, 'w') as output_file: - output_file.write(output_string) + """ Create a workspace to wrap main and sources gyp paths. """ + (build_file_root, build_file_ext) = os.path.splitext(main_gyp) + workspace_path = build_file_root + ".xcworkspace" + options = params["options"] + if options.generator_output: + workspace_path = os.path.join(options.generator_output, workspace_path) + try: + os.makedirs(workspace_path) + except OSError as e: + if e.errno != errno.EEXIST: + raise + output_string = ( + '\n' + '\n' + ) + for gyp_name in [main_gyp, sources_gyp]: + name = os.path.splitext(os.path.basename(gyp_name))[0] + ".xcodeproj" + name = xml.sax.saxutils.quoteattr("group:" + name) + output_string += " \n" % name + output_string += "\n" + + workspace_file = os.path.join(workspace_path, "contents.xcworkspacedata") + + try: + with open(workspace_file, "r") as input_file: + input_string = input_file.read() + if input_string == output_string: + return + except IOError: + # Ignore errors if the file doesn't exist. + pass + + with open(workspace_file, "w") as output_file: + output_file.write(output_string) + def _TargetFromSpec(old_spec, params): - """ Create fake target for xcode-ninja wrapper. """ - # Determine ninja top level build dir (e.g. /path/to/out). - ninja_toplevel = None - jobs = 0 - if params: - options = params['options'] - ninja_toplevel = \ - os.path.join(options.toplevel_dir, - gyp.generator.ninja.ComputeOutputDir(params)) - jobs = params.get('generator_flags', {}).get('xcode_ninja_jobs', 0) - - target_name = old_spec.get('target_name') - product_name = old_spec.get('product_name', target_name) - product_extension = old_spec.get('product_extension') - - ninja_target = {} - ninja_target['target_name'] = target_name - ninja_target['product_name'] = product_name - if product_extension: - ninja_target['product_extension'] = product_extension - ninja_target['toolset'] = old_spec.get('toolset') - ninja_target['default_configuration'] = old_spec.get('default_configuration') - ninja_target['configurations'] = {} - - # Tell Xcode to look in |ninja_toplevel| for build products. - new_xcode_settings = {} - if ninja_toplevel: - new_xcode_settings['CONFIGURATION_BUILD_DIR'] = \ - "%s/$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)" % ninja_toplevel - - if 'configurations' in old_spec: - for config in old_spec['configurations']: - old_xcode_settings = \ - old_spec['configurations'][config].get('xcode_settings', {}) - if 'IPHONEOS_DEPLOYMENT_TARGET' in old_xcode_settings: - new_xcode_settings['CODE_SIGNING_REQUIRED'] = "NO" - new_xcode_settings['IPHONEOS_DEPLOYMENT_TARGET'] = \ - old_xcode_settings['IPHONEOS_DEPLOYMENT_TARGET'] - for key in ['BUNDLE_LOADER', 'TEST_HOST']: - if key in old_xcode_settings: - new_xcode_settings[key] = old_xcode_settings[key] - - ninja_target['configurations'][config] = {} - ninja_target['configurations'][config]['xcode_settings'] = \ - new_xcode_settings - - ninja_target['mac_bundle'] = old_spec.get('mac_bundle', 0) - ninja_target['mac_xctest_bundle'] = old_spec.get('mac_xctest_bundle', 0) - ninja_target['ios_app_extension'] = old_spec.get('ios_app_extension', 0) - ninja_target['ios_watchkit_extension'] = \ - old_spec.get('ios_watchkit_extension', 0) - ninja_target['ios_watchkit_app'] = old_spec.get('ios_watchkit_app', 0) - ninja_target['type'] = old_spec['type'] - if ninja_toplevel: - ninja_target['actions'] = [ - { - 'action_name': 'Compile and copy %s via ninja' % target_name, - 'inputs': [], - 'outputs': [], - 'action': [ - 'env', - 'PATH=%s' % os.environ['PATH'], - 'ninja', - '-C', - new_xcode_settings['CONFIGURATION_BUILD_DIR'], - target_name, - ], - 'message': 'Compile and copy %s via ninja' % target_name, - }, - ] - if jobs > 0: - ninja_target['actions'][0]['action'].extend(('-j', jobs)) - return ninja_target + """ Create fake target for xcode-ninja wrapper. """ + # Determine ninja top level build dir (e.g. /path/to/out). + ninja_toplevel = None + jobs = 0 + if params: + options = params["options"] + ninja_toplevel = os.path.join( + options.toplevel_dir, gyp.generator.ninja.ComputeOutputDir(params) + ) + jobs = params.get("generator_flags", {}).get("xcode_ninja_jobs", 0) + + target_name = old_spec.get("target_name") + product_name = old_spec.get("product_name", target_name) + product_extension = old_spec.get("product_extension") + + ninja_target = {} + ninja_target["target_name"] = target_name + ninja_target["product_name"] = product_name + if product_extension: + ninja_target["product_extension"] = product_extension + ninja_target["toolset"] = old_spec.get("toolset") + ninja_target["default_configuration"] = old_spec.get("default_configuration") + ninja_target["configurations"] = {} + + # Tell Xcode to look in |ninja_toplevel| for build products. + new_xcode_settings = {} + if ninja_toplevel: + new_xcode_settings["CONFIGURATION_BUILD_DIR"] = ( + "%s/$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)" % ninja_toplevel + ) + + if "configurations" in old_spec: + for config in old_spec["configurations"]: + old_xcode_settings = old_spec["configurations"][config].get( + "xcode_settings", {} + ) + if "IPHONEOS_DEPLOYMENT_TARGET" in old_xcode_settings: + new_xcode_settings["CODE_SIGNING_REQUIRED"] = "NO" + new_xcode_settings["IPHONEOS_DEPLOYMENT_TARGET"] = old_xcode_settings[ + "IPHONEOS_DEPLOYMENT_TARGET" + ] + for key in ["BUNDLE_LOADER", "TEST_HOST"]: + if key in old_xcode_settings: + new_xcode_settings[key] = old_xcode_settings[key] + + ninja_target["configurations"][config] = {} + ninja_target["configurations"][config][ + "xcode_settings" + ] = new_xcode_settings + + ninja_target["mac_bundle"] = old_spec.get("mac_bundle", 0) + ninja_target["mac_xctest_bundle"] = old_spec.get("mac_xctest_bundle", 0) + ninja_target["ios_app_extension"] = old_spec.get("ios_app_extension", 0) + ninja_target["ios_watchkit_extension"] = old_spec.get("ios_watchkit_extension", 0) + ninja_target["ios_watchkit_app"] = old_spec.get("ios_watchkit_app", 0) + ninja_target["type"] = old_spec["type"] + if ninja_toplevel: + ninja_target["actions"] = [ + { + "action_name": "Compile and copy %s via ninja" % target_name, + "inputs": [], + "outputs": [], + "action": [ + "env", + "PATH=%s" % os.environ["PATH"], + "ninja", + "-C", + new_xcode_settings["CONFIGURATION_BUILD_DIR"], + target_name, + ], + "message": "Compile and copy %s via ninja" % target_name, + }, + ] + if jobs > 0: + ninja_target["actions"][0]["action"].extend(("-j", jobs)) + return ninja_target + def IsValidTargetForWrapper(target_extras, executable_target_pattern, spec): - """Limit targets for Xcode wrapper. + """Limit targets for Xcode wrapper. Xcode sometimes performs poorly with too many targets, so only include proper executable targets, with filters to customize. @@ -138,25 +144,27 @@ def IsValidTargetForWrapper(target_extras, executable_target_pattern, spec): executable_target_pattern: Regular expression limiting executable targets. spec: Specifications for target. """ - target_name = spec.get('target_name') - # Always include targets matching target_extras. - if target_extras is not None and re.search(target_extras, target_name): - return True - - # Otherwise just show executable targets and xc_tests. - if (int(spec.get('mac_xctest_bundle', 0)) != 0 or - (spec.get('type', '') == 'executable' and - spec.get('product_extension', '') != 'bundle')): - - # If there is a filter and the target does not match, exclude the target. - if executable_target_pattern is not None: - if not re.search(executable_target_pattern, target_name): - return False - return True - return False + target_name = spec.get("target_name") + # Always include targets matching target_extras. + if target_extras is not None and re.search(target_extras, target_name): + return True + + # Otherwise just show executable targets and xc_tests. + if int(spec.get("mac_xctest_bundle", 0)) != 0 or ( + spec.get("type", "") == "executable" + and spec.get("product_extension", "") != "bundle" + ): + + # If there is a filter and the target does not match, exclude the target. + if executable_target_pattern is not None: + if not re.search(executable_target_pattern, target_name): + return False + return True + return False + def CreateWrapper(target_list, target_dicts, data, params): - """Initialize targets for the ninja wrapper. + """Initialize targets for the ninja wrapper. This sets up the necessary variables in the targets to generate Xcode projects that use ninja as an external builder. @@ -166,124 +174,129 @@ def CreateWrapper(target_list, target_dicts, data, params): data: Dict of flattened build files keyed on gyp path. params: Dict of global options for gyp. """ - orig_gyp = params['build_files'][0] - for gyp_name, gyp_dict in data.items(): - if gyp_name == orig_gyp: - depth = gyp_dict['_DEPTH'] - - # Check for custom main gyp name, otherwise use the default CHROMIUM_GYP_FILE - # and prepend .ninja before the .gyp extension. - generator_flags = params.get('generator_flags', {}) - main_gyp = generator_flags.get('xcode_ninja_main_gyp', None) - if main_gyp is None: - (build_file_root, build_file_ext) = os.path.splitext(orig_gyp) - main_gyp = build_file_root + ".ninja" + build_file_ext - - # Create new |target_list|, |target_dicts| and |data| data structures. - new_target_list = [] - new_target_dicts = {} - new_data = {} - - # Set base keys needed for |data|. - new_data[main_gyp] = {} - new_data[main_gyp]['included_files'] = [] - new_data[main_gyp]['targets'] = [] - new_data[main_gyp]['xcode_settings'] = \ - data[orig_gyp].get('xcode_settings', {}) - - # Normally the xcode-ninja generator includes only valid executable targets. - # If |xcode_ninja_executable_target_pattern| is set, that list is reduced to - # executable targets that match the pattern. (Default all) - executable_target_pattern = \ - generator_flags.get('xcode_ninja_executable_target_pattern', None) - - # For including other non-executable targets, add the matching target name - # to the |xcode_ninja_target_pattern| regular expression. (Default none) - target_extras = generator_flags.get('xcode_ninja_target_pattern', None) - - for old_qualified_target in target_list: - spec = target_dicts[old_qualified_target] - if IsValidTargetForWrapper(target_extras, executable_target_pattern, spec): - # Add to new_target_list. - target_name = spec.get('target_name') - new_target_name = '%s:%s#target' % (main_gyp, target_name) - new_target_list.append(new_target_name) - - # Add to new_target_dicts. - new_target_dicts[new_target_name] = _TargetFromSpec(spec, params) - - # Add to new_data. - for old_target in data[old_qualified_target.split(':')[0]]['targets']: - if old_target['target_name'] == target_name: - new_data_target = {} - new_data_target['target_name'] = old_target['target_name'] - new_data_target['toolset'] = old_target['toolset'] - new_data[main_gyp]['targets'].append(new_data_target) - - # Create sources target. - sources_target_name = 'sources_for_indexing' - sources_target = _TargetFromSpec( - { 'target_name' : sources_target_name, - 'toolset': 'target', - 'default_configuration': 'Default', - 'mac_bundle': '0', - 'type': 'executable' - }, None) - - # Tell Xcode to look everywhere for headers. - sources_target['configurations'] = {'Default': { 'include_dirs': [ depth ] } } - - # Put excluded files into the sources target so they can be opened in Xcode. - skip_excluded_files = \ - not generator_flags.get('xcode_ninja_list_excluded_files', True) - - sources = [] - for target, target_dict in target_dicts.items(): - base = os.path.dirname(target) - files = target_dict.get('sources', []) + \ - target_dict.get('mac_bundle_resources', []) - - if not skip_excluded_files: - files.extend(target_dict.get('sources_excluded', []) + - target_dict.get('mac_bundle_resources_excluded', [])) - - for action in target_dict.get('actions', []): - files.extend(action.get('inputs', [])) - - if not skip_excluded_files: - files.extend(action.get('inputs_excluded', [])) - - # Remove files starting with $. These are mostly intermediate files for the - # build system. - files = [ file for file in files if not file.startswith('$')] - - # Make sources relative to root build file. - relative_path = os.path.dirname(main_gyp) - sources += [ os.path.relpath(os.path.join(base, file), relative_path) - for file in files ] - - sources_target['sources'] = sorted(set(sources)) - - # Put sources_to_index in it's own gyp. - sources_gyp = \ - os.path.join(os.path.dirname(main_gyp), sources_target_name + ".gyp") - fully_qualified_target_name = \ - '%s:%s#target' % (sources_gyp, sources_target_name) - - # Add to new_target_list, new_target_dicts and new_data. - new_target_list.append(fully_qualified_target_name) - new_target_dicts[fully_qualified_target_name] = sources_target - new_data_target = {} - new_data_target['target_name'] = sources_target['target_name'] - new_data_target['_DEPTH'] = depth - new_data_target['toolset'] = "target" - new_data[sources_gyp] = {} - new_data[sources_gyp]['targets'] = [] - new_data[sources_gyp]['included_files'] = [] - new_data[sources_gyp]['xcode_settings'] = \ - data[orig_gyp].get('xcode_settings', {}) - new_data[sources_gyp]['targets'].append(new_data_target) - - # Write workspace to file. - _WriteWorkspace(main_gyp, sources_gyp, params) - return (new_target_list, new_target_dicts, new_data) + orig_gyp = params["build_files"][0] + for gyp_name, gyp_dict in data.items(): + if gyp_name == orig_gyp: + depth = gyp_dict["_DEPTH"] + + # Check for custom main gyp name, otherwise use the default CHROMIUM_GYP_FILE + # and prepend .ninja before the .gyp extension. + generator_flags = params.get("generator_flags", {}) + main_gyp = generator_flags.get("xcode_ninja_main_gyp", None) + if main_gyp is None: + (build_file_root, build_file_ext) = os.path.splitext(orig_gyp) + main_gyp = build_file_root + ".ninja" + build_file_ext + + # Create new |target_list|, |target_dicts| and |data| data structures. + new_target_list = [] + new_target_dicts = {} + new_data = {} + + # Set base keys needed for |data|. + new_data[main_gyp] = {} + new_data[main_gyp]["included_files"] = [] + new_data[main_gyp]["targets"] = [] + new_data[main_gyp]["xcode_settings"] = data[orig_gyp].get("xcode_settings", {}) + + # Normally the xcode-ninja generator includes only valid executable targets. + # If |xcode_ninja_executable_target_pattern| is set, that list is reduced to + # executable targets that match the pattern. (Default all) + executable_target_pattern = generator_flags.get( + "xcode_ninja_executable_target_pattern", None + ) + + # For including other non-executable targets, add the matching target name + # to the |xcode_ninja_target_pattern| regular expression. (Default none) + target_extras = generator_flags.get("xcode_ninja_target_pattern", None) + + for old_qualified_target in target_list: + spec = target_dicts[old_qualified_target] + if IsValidTargetForWrapper(target_extras, executable_target_pattern, spec): + # Add to new_target_list. + target_name = spec.get("target_name") + new_target_name = "%s:%s#target" % (main_gyp, target_name) + new_target_list.append(new_target_name) + + # Add to new_target_dicts. + new_target_dicts[new_target_name] = _TargetFromSpec(spec, params) + + # Add to new_data. + for old_target in data[old_qualified_target.split(":")[0]]["targets"]: + if old_target["target_name"] == target_name: + new_data_target = {} + new_data_target["target_name"] = old_target["target_name"] + new_data_target["toolset"] = old_target["toolset"] + new_data[main_gyp]["targets"].append(new_data_target) + + # Create sources target. + sources_target_name = "sources_for_indexing" + sources_target = _TargetFromSpec( + { + "target_name": sources_target_name, + "toolset": "target", + "default_configuration": "Default", + "mac_bundle": "0", + "type": "executable", + }, + None, + ) + + # Tell Xcode to look everywhere for headers. + sources_target["configurations"] = {"Default": {"include_dirs": [depth]}} + + # Put excluded files into the sources target so they can be opened in Xcode. + skip_excluded_files = not generator_flags.get( + "xcode_ninja_list_excluded_files", True + ) + + sources = [] + for target, target_dict in target_dicts.items(): + base = os.path.dirname(target) + files = target_dict.get("sources", []) + target_dict.get( + "mac_bundle_resources", [] + ) + + if not skip_excluded_files: + files.extend( + target_dict.get("sources_excluded", []) + + target_dict.get("mac_bundle_resources_excluded", []) + ) + + for action in target_dict.get("actions", []): + files.extend(action.get("inputs", [])) + + if not skip_excluded_files: + files.extend(action.get("inputs_excluded", [])) + + # Remove files starting with $. These are mostly intermediate files for the + # build system. + files = [file for file in files if not file.startswith("$")] + + # Make sources relative to root build file. + relative_path = os.path.dirname(main_gyp) + sources += [ + os.path.relpath(os.path.join(base, file), relative_path) for file in files + ] + + sources_target["sources"] = sorted(set(sources)) + + # Put sources_to_index in it's own gyp. + sources_gyp = os.path.join(os.path.dirname(main_gyp), sources_target_name + ".gyp") + fully_qualified_target_name = "%s:%s#target" % (sources_gyp, sources_target_name) + + # Add to new_target_list, new_target_dicts and new_data. + new_target_list.append(fully_qualified_target_name) + new_target_dicts[fully_qualified_target_name] = sources_target + new_data_target = {} + new_data_target["target_name"] = sources_target["target_name"] + new_data_target["_DEPTH"] = depth + new_data_target["toolset"] = "target" + new_data[sources_gyp] = {} + new_data[sources_gyp]["targets"] = [] + new_data[sources_gyp]["included_files"] = [] + new_data[sources_gyp]["xcode_settings"] = data[orig_gyp].get("xcode_settings", {}) + new_data[sources_gyp]["targets"].append(new_data_target) + + # Write workspace to file. + _WriteWorkspace(main_gyp, sources_gyp, params) + return (new_target_list, new_target_dicts, new_data) diff --git a/tools/gyp/pylib/gyp/xcodeproj_file.py b/tools/gyp/pylib/gyp/xcodeproj_file.py index 1e950dce8f0a05..d90dd99dccecf9 100644 --- a/tools/gyp/pylib/gyp/xcodeproj_file.py +++ b/tools/gyp/pylib/gyp/xcodeproj_file.py @@ -145,11 +145,12 @@ import sys try: - basestring, cmp, unicode + basestring, cmp, unicode except NameError: # Python 3 - basestring = unicode = str - def cmp(x, y): - return (x > y) - (x < y) + basestring = unicode = str + + def cmp(x, y): + return (x > y) - (x < y) # See XCObject._EncodeString. This pattern is used to determine when a string @@ -158,11 +159,11 @@ def cmp(x, y): # transformed to be properly encoded. Note that this expression matches the # characters listed with "+", for 1 or more occurrences: if a string is empty, # it must not match this pattern, because it needs to be encoded as "". -_unquoted = re.compile('^[A-Za-z0-9$./_]+$') +_unquoted = re.compile("^[A-Za-z0-9$./_]+$") # Strings that match this pattern are quoted regardless of what _unquoted says. # Oddly, Xcode will quote any string with a run of three or more underscores. -_quoted = re.compile('___') +_quoted = re.compile("___") # This pattern should match any character that needs to be escaped by # XCObject._EncodeString. See that function. @@ -170,10 +171,11 @@ def cmp(x, y): # Used by SourceTreeAndPathFromPath -_path_leading_variable = re.compile(r'^\$\((.*?)\)(/(.*))?$') +_path_leading_variable = re.compile(r"^\$\((.*?)\)(/(.*))?$") + def SourceTreeAndPathFromPath(input_path): - """Given input_path, returns a tuple with sourceTree and path values. + """Given input_path, returns a tuple with sourceTree and path values. Examples: input_path (source_tree, output_path) @@ -182,21 +184,23 @@ def SourceTreeAndPathFromPath(input_path): 'path' (None, 'path') """ - source_group_match = _path_leading_variable.match(input_path) - if source_group_match: - source_tree = source_group_match.group(1) - output_path = source_group_match.group(3) # This may be None. - else: - source_tree = None - output_path = input_path + source_group_match = _path_leading_variable.match(input_path) + if source_group_match: + source_tree = source_group_match.group(1) + output_path = source_group_match.group(3) # This may be None. + else: + source_tree = None + output_path = input_path + + return (source_tree, output_path) - return (source_tree, output_path) def ConvertVariablesToShellSyntax(input_string): - return re.sub(r'\$\((.*?)\)', '${\\1}', input_string) + return re.sub(r"\$\((.*?)\)", "${\\1}", input_string) + class XCObject(object): - """The abstract base of all class types used in Xcode project files. + """The abstract base of all class types used in Xcode project files. Class variables: _schema: A dictionary defining the properties of this class. The keys to @@ -263,45 +267,45 @@ class XCObject(object): described by its class' _schema variable. """ - _schema = {} - _should_print_single_line = False - - # See _EncodeString. - _encode_transforms = [] - i = 0 - while i < ord(' '): - _encode_transforms.append('\\U%04x' % i) - i = i + 1 - _encode_transforms[7] = '\\a' - _encode_transforms[8] = '\\b' - _encode_transforms[9] = '\\t' - _encode_transforms[10] = '\\n' - _encode_transforms[11] = '\\v' - _encode_transforms[12] = '\\f' - _encode_transforms[13] = '\\n' - - _alternate_encode_transforms = list(_encode_transforms) - _alternate_encode_transforms[9] = chr(9) - _alternate_encode_transforms[10] = chr(10) - _alternate_encode_transforms[11] = chr(11) - - def __init__(self, properties=None, id=None, parent=None): - self.id = id - self.parent = parent - self._properties = {} - self._hashables = [] - self._SetDefaultsFromSchema() - self.UpdateProperties(properties) - - def __repr__(self): - try: - name = self.Name() - except NotImplementedError: - return '<%s at 0x%x>' % (self.__class__.__name__, id(self)) - return '<%s %r at 0x%x>' % (self.__class__.__name__, name, id(self)) - - def Copy(self): - """Make a copy of this object. + _schema = {} + _should_print_single_line = False + + # See _EncodeString. + _encode_transforms = [] + i = 0 + while i < ord(" "): + _encode_transforms.append("\\U%04x" % i) + i = i + 1 + _encode_transforms[7] = "\\a" + _encode_transforms[8] = "\\b" + _encode_transforms[9] = "\\t" + _encode_transforms[10] = "\\n" + _encode_transforms[11] = "\\v" + _encode_transforms[12] = "\\f" + _encode_transforms[13] = "\\n" + + _alternate_encode_transforms = list(_encode_transforms) + _alternate_encode_transforms[9] = chr(9) + _alternate_encode_transforms[10] = chr(10) + _alternate_encode_transforms[11] = chr(11) + + def __init__(self, properties=None, id=None, parent=None): + self.id = id + self.parent = parent + self._properties = {} + self._hashables = [] + self._SetDefaultsFromSchema() + self.UpdateProperties(properties) + + def __repr__(self): + try: + name = self.Name() + except NotImplementedError: + return "<%s at 0x%x>" % (self.__class__.__name__, id(self)) + return "<%s %r at 0x%x>" % (self.__class__.__name__, name, id(self)) + + def Copy(self): + """Make a copy of this object. The new object will have its own copy of lists and dicts. Any XCObject objects owned by this object (marked "strong") will be copied in the @@ -310,62 +314,70 @@ def Copy(self): object without making a copy. """ - that = self.__class__(id=self.id, parent=self.parent) - for key, value in self._properties.items(): - is_strong = self._schema[key][2] - - if isinstance(value, XCObject): - if is_strong: - new_value = value.Copy() - new_value.parent = that - that._properties[key] = new_value - else: - that._properties[key] = value - elif isinstance(value, (basestring, int)): - that._properties[key] = value - elif isinstance(value, list): - if is_strong: - # If is_strong is True, each element is an XCObject, so it's safe to - # call Copy. - that._properties[key] = [] - for item in value: - new_item = item.Copy() - new_item.parent = that - that._properties[key].append(new_item) - else: - that._properties[key] = value[:] - elif isinstance(value, dict): - # dicts are never strong. - if is_strong: - raise TypeError('Strong dict for key ' + key + ' in ' + \ - self.__class__.__name__) - else: - that._properties[key] = value.copy() - else: - raise TypeError('Unexpected type ' + value.__class__.__name__ + \ - ' for key ' + key + ' in ' + self.__class__.__name__) - - return that - - def Name(self): - """Return the name corresponding to an object. + that = self.__class__(id=self.id, parent=self.parent) + for key, value in self._properties.items(): + is_strong = self._schema[key][2] + + if isinstance(value, XCObject): + if is_strong: + new_value = value.Copy() + new_value.parent = that + that._properties[key] = new_value + else: + that._properties[key] = value + elif isinstance(value, (basestring, int)): + that._properties[key] = value + elif isinstance(value, list): + if is_strong: + # If is_strong is True, each element is an XCObject, so it's safe to + # call Copy. + that._properties[key] = [] + for item in value: + new_item = item.Copy() + new_item.parent = that + that._properties[key].append(new_item) + else: + that._properties[key] = value[:] + elif isinstance(value, dict): + # dicts are never strong. + if is_strong: + raise TypeError( + "Strong dict for key " + key + " in " + self.__class__.__name__ + ) + else: + that._properties[key] = value.copy() + else: + raise TypeError( + "Unexpected type " + + value.__class__.__name__ + + " for key " + + key + + " in " + + self.__class__.__name__ + ) + + return that + + def Name(self): + """Return the name corresponding to an object. Not all objects necessarily need to be nameable, and not all that do have a "name" property. Override as needed. """ - # If the schema indicates that "name" is required, try to access the - # property even if it doesn't exist. This will result in a KeyError - # being raised for the property that should be present, which seems more - # appropriate than NotImplementedError in this case. - if 'name' in self._properties or \ - ('name' in self._schema and self._schema['name'][3]): - return self._properties['name'] + # If the schema indicates that "name" is required, try to access the + # property even if it doesn't exist. This will result in a KeyError + # being raised for the property that should be present, which seems more + # appropriate than NotImplementedError in this case. + if "name" in self._properties or ( + "name" in self._schema and self._schema["name"][3] + ): + return self._properties["name"] - raise NotImplementedError(self.__class__.__name__ + ' must implement Name') + raise NotImplementedError(self.__class__.__name__ + " must implement Name") - def Comment(self): - """Return a comment string for the object. + def Comment(self): + """Return a comment string for the object. Most objects just use their name as the comment, but PBXProject uses different values. @@ -374,24 +386,24 @@ def Comment(self): strings applied to it. """ - return self.Name() + return self.Name() - def Hashables(self): - hashables = [self.__class__.__name__] + def Hashables(self): + hashables = [self.__class__.__name__] - name = self.Name() - if name != None: - hashables.append(name) + name = self.Name() + if name is not None: + hashables.append(name) - hashables.extend(self._hashables) + hashables.extend(self._hashables) - return hashables + return hashables - def HashablesForChild(self): - return None + def HashablesForChild(self): + return None - def ComputeIDs(self, recursive=True, overwrite=True, seed_hash=None): - """Set "id" properties deterministically. + def ComputeIDs(self, recursive=True, overwrite=True, seed_hash=None): + """Set "id" properties deterministically. An object's "id" property is set based on a hash of its class type and name, as well as the class type and name of all ancestor objects. As @@ -405,8 +417,8 @@ def ComputeIDs(self, recursive=True, overwrite=True, seed_hash=None): replaced. """ - def _HashUpdate(hash, data): - """Update hash with data's length and contents. + def _HashUpdate(hash, data): + """Update hash with data's length and contents. If the hash were updated only with the value of data, it would be possible for clowns to induce collisions by manipulating the names of @@ -414,162 +426,167 @@ def _HashUpdate(hash, data): ID collisions will be encountered, intentionally or not. """ - hash.update(struct.pack('>i', len(data))) - hash.update(data) - - if seed_hash is None: - seed_hash = hashlib.sha1() - - hash = seed_hash.copy() - - hashables = self.Hashables() - assert len(hashables) > 0 - for hashable in hashables: - _HashUpdate(hash, hashable) - - if recursive: - hashables_for_child = self.HashablesForChild() - if hashables_for_child is None: - child_hash = hash - else: - assert len(hashables_for_child) > 0 - child_hash = seed_hash.copy() - for hashable in hashables_for_child: - _HashUpdate(child_hash, hashable) - - for child in self.Children(): - child.ComputeIDs(recursive, overwrite, child_hash) - - if overwrite or self.id is None: - # Xcode IDs are only 96 bits (24 hex characters), but a SHA-1 digest is - # is 160 bits. Instead of throwing out 64 bits of the digest, xor them - # into the portion that gets used. - assert hash.digest_size % 4 == 0 - digest_int_count = hash.digest_size / 4 - digest_ints = struct.unpack('>' + 'I' * digest_int_count, hash.digest()) - id_ints = [0, 0, 0] - for index in range(0, digest_int_count): - id_ints[index % 3] ^= digest_ints[index] - self.id = '%08X%08X%08X' % tuple(id_ints) - - def EnsureNoIDCollisions(self): - """Verifies that no two objects have the same ID. Checks all descendants. + hash.update(struct.pack(">i", len(data))) + hash.update(data) + + if seed_hash is None: + seed_hash = hashlib.sha1() + + hash = seed_hash.copy() + + hashables = self.Hashables() + assert len(hashables) > 0 + for hashable in hashables: + _HashUpdate(hash, hashable) + + if recursive: + hashables_for_child = self.HashablesForChild() + if hashables_for_child is None: + child_hash = hash + else: + assert len(hashables_for_child) > 0 + child_hash = seed_hash.copy() + for hashable in hashables_for_child: + _HashUpdate(child_hash, hashable) + + for child in self.Children(): + child.ComputeIDs(recursive, overwrite, child_hash) + + if overwrite or self.id is None: + # Xcode IDs are only 96 bits (24 hex characters), but a SHA-1 digest is + # is 160 bits. Instead of throwing out 64 bits of the digest, xor them + # into the portion that gets used. + assert hash.digest_size % 4 == 0 + digest_int_count = hash.digest_size // 4 + digest_ints = struct.unpack(">" + "I" * digest_int_count, hash.digest()) + id_ints = [0, 0, 0] + for index in range(0, digest_int_count): + id_ints[index % 3] ^= digest_ints[index] + self.id = "%08X%08X%08X" % tuple(id_ints) + + def EnsureNoIDCollisions(self): + """Verifies that no two objects have the same ID. Checks all descendants. """ - ids = {} - descendants = self.Descendants() - for descendant in descendants: - if descendant.id in ids: - other = ids[descendant.id] - raise KeyError( - 'Duplicate ID %s, objects "%s" and "%s" in "%s"' % \ - (descendant.id, str(descendant._properties), - str(other._properties), self._properties['rootObject'].Name())) - ids[descendant.id] = descendant - - def Children(self): - """Returns a list of all of this object's owned (strong) children.""" - - children = [] - for property, attributes in self._schema.items(): - (is_list, property_type, is_strong) = attributes[0:3] - if is_strong and property in self._properties: - if not is_list: - children.append(self._properties[property]) - else: - children.extend(self._properties[property]) - return children - - def Descendants(self): - """Returns a list of all of this object's descendants, including this + ids = {} + descendants = self.Descendants() + for descendant in descendants: + if descendant.id in ids: + other = ids[descendant.id] + raise KeyError( + 'Duplicate ID %s, objects "%s" and "%s" in "%s"' + % ( + descendant.id, + str(descendant._properties), + str(other._properties), + self._properties["rootObject"].Name(), + ) + ) + ids[descendant.id] = descendant + + def Children(self): + """Returns a list of all of this object's owned (strong) children.""" + + children = [] + for property, attributes in self._schema.items(): + (is_list, property_type, is_strong) = attributes[0:3] + if is_strong and property in self._properties: + if not is_list: + children.append(self._properties[property]) + else: + children.extend(self._properties[property]) + return children + + def Descendants(self): + """Returns a list of all of this object's descendants, including this object. """ - children = self.Children() - descendants = [self] - for child in children: - descendants.extend(child.Descendants()) - return descendants + children = self.Children() + descendants = [self] + for child in children: + descendants.extend(child.Descendants()) + return descendants - def PBXProjectAncestor(self): - # The base case for recursion is defined at PBXProject.PBXProjectAncestor. - if self.parent: - return self.parent.PBXProjectAncestor() - return None + def PBXProjectAncestor(self): + # The base case for recursion is defined at PBXProject.PBXProjectAncestor. + if self.parent: + return self.parent.PBXProjectAncestor() + return None - def _EncodeComment(self, comment): - """Encodes a comment to be placed in the project file output, mimicing + def _EncodeComment(self, comment): + """Encodes a comment to be placed in the project file output, mimicking Xcode behavior. """ - # This mimics Xcode behavior by wrapping the comment in "/*" and "*/". If - # the string already contains a "*/", it is turned into "(*)/". This keeps - # the file writer from outputting something that would be treated as the - # end of a comment in the middle of something intended to be entirely a - # comment. - - return '/* ' + comment.replace('*/', '(*)/') + ' */' - - def _EncodeTransform(self, match): - # This function works closely with _EncodeString. It will only be called - # by re.sub with match.group(0) containing a character matched by the - # the _escaped expression. - char = match.group(0) - - # Backslashes (\) and quotation marks (") are always replaced with a - # backslash-escaped version of the same. Everything else gets its - # replacement from the class' _encode_transforms array. - if char == '\\': - return '\\\\' - if char == '"': - return '\\"' - return self._encode_transforms[ord(char)] - - def _EncodeString(self, value): - """Encodes a string to be placed in the project file output, mimicing + # This mimics Xcode behavior by wrapping the comment in "/*" and "*/". If + # the string already contains a "*/", it is turned into "(*)/". This keeps + # the file writer from outputting something that would be treated as the + # end of a comment in the middle of something intended to be entirely a + # comment. + + return "/* " + comment.replace("*/", "(*)/") + " */" + + def _EncodeTransform(self, match): + # This function works closely with _EncodeString. It will only be called + # by re.sub with match.group(0) containing a character matched by the + # the _escaped expression. + char = match.group(0) + + # Backslashes (\) and quotation marks (") are always replaced with a + # backslash-escaped version of the same. Everything else gets its + # replacement from the class' _encode_transforms array. + if char == "\\": + return "\\\\" + if char == '"': + return '\\"' + return self._encode_transforms[ord(char)] + + def _EncodeString(self, value): + """Encodes a string to be placed in the project file output, mimicking Xcode behavior. """ - # Use quotation marks when any character outside of the range A-Z, a-z, 0-9, - # $ (dollar sign), . (period), and _ (underscore) is present. Also use - # quotation marks to represent empty strings. - # - # Escape " (double-quote) and \ (backslash) by preceding them with a - # backslash. - # - # Some characters below the printable ASCII range are encoded specially: - # 7 ^G BEL is encoded as "\a" - # 8 ^H BS is encoded as "\b" - # 11 ^K VT is encoded as "\v" - # 12 ^L NP is encoded as "\f" - # 127 ^? DEL is passed through as-is without escaping - # - In PBXFileReference and PBXBuildFile objects: - # 9 ^I HT is passed through as-is without escaping - # 10 ^J NL is passed through as-is without escaping - # 13 ^M CR is passed through as-is without escaping - # - In other objects: - # 9 ^I HT is encoded as "\t" - # 10 ^J NL is encoded as "\n" - # 13 ^M CR is encoded as "\n" rendering it indistinguishable from - # 10 ^J NL - # All other characters within the ASCII control character range (0 through - # 31 inclusive) are encoded as "\U001f" referring to the Unicode code point - # in hexadecimal. For example, character 14 (^N SO) is encoded as "\U000e". - # Characters above the ASCII range are passed through to the output encoded - # as UTF-8 without any escaping. These mappings are contained in the - # class' _encode_transforms list. - - if _unquoted.search(value) and not _quoted.search(value): - return value - - return '"' + _escaped.sub(self._EncodeTransform, value) + '"' - - def _XCPrint(self, file, tabs, line): - file.write('\t' * tabs + line) - - def _XCPrintableValue(self, tabs, value, flatten_list=False): - """Returns a representation of value that may be printed in a project file, - mimicing Xcode's behavior. + # Use quotation marks when any character outside of the range A-Z, a-z, 0-9, + # $ (dollar sign), . (period), and _ (underscore) is present. Also use + # quotation marks to represent empty strings. + # + # Escape " (double-quote) and \ (backslash) by preceding them with a + # backslash. + # + # Some characters below the printable ASCII range are encoded specially: + # 7 ^G BEL is encoded as "\a" + # 8 ^H BS is encoded as "\b" + # 11 ^K VT is encoded as "\v" + # 12 ^L NP is encoded as "\f" + # 127 ^? DEL is passed through as-is without escaping + # - In PBXFileReference and PBXBuildFile objects: + # 9 ^I HT is passed through as-is without escaping + # 10 ^J NL is passed through as-is without escaping + # 13 ^M CR is passed through as-is without escaping + # - In other objects: + # 9 ^I HT is encoded as "\t" + # 10 ^J NL is encoded as "\n" + # 13 ^M CR is encoded as "\n" rendering it indistinguishable from + # 10 ^J NL + # All other characters within the ASCII control character range (0 through + # 31 inclusive) are encoded as "\U001f" referring to the Unicode code point + # in hexadecimal. For example, character 14 (^N SO) is encoded as "\U000e". + # Characters above the ASCII range are passed through to the output encoded + # as UTF-8 without any escaping. These mappings are contained in the + # class' _encode_transforms list. + + if _unquoted.search(value) and not _quoted.search(value): + return value + + return '"' + _escaped.sub(self._EncodeTransform, value) + '"' + + def _XCPrint(self, file, tabs, line): + file.write("\t" * tabs + line) + + def _XCPrintableValue(self, tabs, value, flatten_list=False): + """Returns a representation of value that may be printed in a project file, + mimicking Xcode's behavior. _XCPrintableValue can handle str and int values, XCObjects (which are made printable by returning their id property), and list and dict objects @@ -582,58 +599,65 @@ def _XCPrintableValue(self, tabs, value, flatten_list=False): strings. """ - printable = '' - comment = None + printable = "" + comment = None - if self._should_print_single_line: - sep = ' ' - element_tabs = '' - end_tabs = '' - else: - sep = '\n' - element_tabs = '\t' * (tabs + 1) - end_tabs = '\t' * tabs - - if isinstance(value, XCObject): - printable += value.id - comment = value.Comment() - elif isinstance(value, str): - printable += self._EncodeString(value) - elif isinstance(value, unicode): - printable += self._EncodeString(value.encode('utf-8')) - elif isinstance(value, int): - printable += str(value) - elif isinstance(value, list): - if flatten_list and len(value) <= 1: - if len(value) == 0: - printable += self._EncodeString('') + if self._should_print_single_line: + sep = " " + element_tabs = "" + end_tabs = "" else: - printable += self._EncodeString(value[0]) - else: - printable = '(' + sep - for item in value: - printable += element_tabs + \ - self._XCPrintableValue(tabs + 1, item, flatten_list) + \ - ',' + sep - printable += end_tabs + ')' - elif isinstance(value, dict): - printable = '{' + sep - for item_key, item_value in sorted(value.items()): - printable += element_tabs + \ - self._XCPrintableValue(tabs + 1, item_key, flatten_list) + ' = ' + \ - self._XCPrintableValue(tabs + 1, item_value, flatten_list) + ';' + \ - sep - printable += end_tabs + '}' - else: - raise TypeError("Can't make " + value.__class__.__name__ + ' printable') + sep = "\n" + element_tabs = "\t" * (tabs + 1) + end_tabs = "\t" * tabs + + if isinstance(value, XCObject): + printable += value.id + comment = value.Comment() + elif isinstance(value, str): + printable += self._EncodeString(value) + elif isinstance(value, basestring): + printable += self._EncodeString(value.encode("utf-8")) + elif isinstance(value, int): + printable += str(value) + elif isinstance(value, list): + if flatten_list and len(value) <= 1: + if len(value) == 0: + printable += self._EncodeString("") + else: + printable += self._EncodeString(value[0]) + else: + printable = "(" + sep + for item in value: + printable += ( + element_tabs + + self._XCPrintableValue(tabs + 1, item, flatten_list) + + "," + + sep + ) + printable += end_tabs + ")" + elif isinstance(value, dict): + printable = "{" + sep + for item_key, item_value in sorted(value.items()): + printable += ( + element_tabs + + self._XCPrintableValue(tabs + 1, item_key, flatten_list) + + " = " + + self._XCPrintableValue(tabs + 1, item_value, flatten_list) + + ";" + + sep + ) + printable += end_tabs + "}" + else: + raise TypeError("Can't make " + value.__class__.__name__ + " printable") - if comment != None: - printable += ' ' + self._EncodeComment(comment) + if comment: + printable += " " + self._EncodeComment(comment) - return printable + return printable - def _XCKVPrint(self, file, tabs, key, value): - """Prints a key and value, members of an XCObject's _properties dictionary, + def _XCKVPrint(self, file, tabs, key, value): + """Prints a key and value, members of an XCObject's _properties dictionary, to file. tabs is an int identifying the indentation level. If the class' @@ -641,99 +665,100 @@ def _XCKVPrint(self, file, tabs, key, value): key-value pair will be followed by a space insead of a newline. """ - if self._should_print_single_line: - printable = '' - after_kv = ' ' - else: - printable = '\t' * tabs - after_kv = '\n' - - # Xcode usually prints remoteGlobalIDString values in PBXContainerItemProxy - # objects without comments. Sometimes it prints them with comments, but - # the majority of the time, it doesn't. To avoid unnecessary changes to - # the project file after Xcode opens it, don't write comments for - # remoteGlobalIDString. This is a sucky hack and it would certainly be - # cleaner to extend the schema to indicate whether or not a comment should - # be printed, but since this is the only case where the problem occurs and - # Xcode itself can't seem to make up its mind, the hack will suffice. - # - # Also see PBXContainerItemProxy._schema['remoteGlobalIDString']. - if key == 'remoteGlobalIDString' and isinstance(self, - PBXContainerItemProxy): - value_to_print = value.id - else: - value_to_print = value + if self._should_print_single_line: + printable = "" + after_kv = " " + else: + printable = "\t" * tabs + after_kv = "\n" + + # Xcode usually prints remoteGlobalIDString values in PBXContainerItemProxy + # objects without comments. Sometimes it prints them with comments, but + # the majority of the time, it doesn't. To avoid unnecessary changes to + # the project file after Xcode opens it, don't write comments for + # remoteGlobalIDString. This is a sucky hack and it would certainly be + # cleaner to extend the schema to indicate whether or not a comment should + # be printed, but since this is the only case where the problem occurs and + # Xcode itself can't seem to make up its mind, the hack will suffice. + # + # Also see PBXContainerItemProxy._schema['remoteGlobalIDString']. + if key == "remoteGlobalIDString" and isinstance(self, PBXContainerItemProxy): + value_to_print = value.id + else: + value_to_print = value - # PBXBuildFile's settings property is represented in the output as a dict, - # but a hack here has it represented as a string. Arrange to strip off the - # quotes so that it shows up in the output as expected. - if key == 'settings' and isinstance(self, PBXBuildFile): - strip_value_quotes = True - else: - strip_value_quotes = False + # PBXBuildFile's settings property is represented in the output as a dict, + # but a hack here has it represented as a string. Arrange to strip off the + # quotes so that it shows up in the output as expected. + if key == "settings" and isinstance(self, PBXBuildFile): + strip_value_quotes = True + else: + strip_value_quotes = False - # In another one-off, let's set flatten_list on buildSettings properties - # of XCBuildConfiguration objects, because that's how Xcode treats them. - if key == 'buildSettings' and isinstance(self, XCBuildConfiguration): - flatten_list = True - else: - flatten_list = False - - try: - printable_key = self._XCPrintableValue(tabs, key, flatten_list) - printable_value = self._XCPrintableValue(tabs, value_to_print, - flatten_list) - if strip_value_quotes and len(printable_value) > 1 and \ - printable_value[0] == '"' and printable_value[-1] == '"': - printable_value = printable_value[1:-1] - printable += printable_key + ' = ' + printable_value + ';' + after_kv - except TypeError as e: - gyp.common.ExceptionAppend(e, - 'while printing key "%s"' % key) - raise - - self._XCPrint(file, 0, printable) - - def Print(self, file=sys.stdout): - """Prints a reprentation of this object to file, adhering to Xcode output + # In another one-off, let's set flatten_list on buildSettings properties + # of XCBuildConfiguration objects, because that's how Xcode treats them. + if key == "buildSettings" and isinstance(self, XCBuildConfiguration): + flatten_list = True + else: + flatten_list = False + + try: + printable_key = self._XCPrintableValue(tabs, key, flatten_list) + printable_value = self._XCPrintableValue(tabs, value_to_print, flatten_list) + if ( + strip_value_quotes + and len(printable_value) > 1 + and printable_value[0] == '"' + and printable_value[-1] == '"' + ): + printable_value = printable_value[1:-1] + printable += printable_key + " = " + printable_value + ";" + after_kv + except TypeError as e: + gyp.common.ExceptionAppend(e, 'while printing key "%s"' % key) + raise + + self._XCPrint(file, 0, printable) + + def Print(self, file=sys.stdout): + """Prints a reprentation of this object to file, adhering to Xcode output formatting. """ - self.VerifyHasRequiredProperties() - - if self._should_print_single_line: - # When printing an object in a single line, Xcode doesn't put any space - # between the beginning of a dictionary (or presumably a list) and the - # first contained item, so you wind up with snippets like - # ...CDEF = {isa = PBXFileReference; fileRef = 0123... - # If it were me, I would have put a space in there after the opening - # curly, but I guess this is just another one of those inconsistencies - # between how Xcode prints PBXFileReference and PBXBuildFile objects as - # compared to other objects. Mimic Xcode's behavior here by using an - # empty string for sep. - sep = '' - end_tabs = 0 - else: - sep = '\n' - end_tabs = 2 + self.VerifyHasRequiredProperties() + + if self._should_print_single_line: + # When printing an object in a single line, Xcode doesn't put any space + # between the beginning of a dictionary (or presumably a list) and the + # first contained item, so you wind up with snippets like + # ...CDEF = {isa = PBXFileReference; fileRef = 0123... + # If it were me, I would have put a space in there after the opening + # curly, but I guess this is just another one of those inconsistencies + # between how Xcode prints PBXFileReference and PBXBuildFile objects as + # compared to other objects. Mimic Xcode's behavior here by using an + # empty string for sep. + sep = "" + end_tabs = 0 + else: + sep = "\n" + end_tabs = 2 - # Start the object. For example, '\t\tPBXProject = {\n'. - self._XCPrint(file, 2, self._XCPrintableValue(2, self) + ' = {' + sep) + # Start the object. For example, '\t\tPBXProject = {\n'. + self._XCPrint(file, 2, self._XCPrintableValue(2, self) + " = {" + sep) - # "isa" isn't in the _properties dictionary, it's an intrinsic property - # of the class which the object belongs to. Xcode always outputs "isa" - # as the first element of an object dictionary. - self._XCKVPrint(file, 3, 'isa', self.__class__.__name__) + # "isa" isn't in the _properties dictionary, it's an intrinsic property + # of the class which the object belongs to. Xcode always outputs "isa" + # as the first element of an object dictionary. + self._XCKVPrint(file, 3, "isa", self.__class__.__name__) - # The remaining elements of an object dictionary are sorted alphabetically. - for property, value in sorted(self._properties.items()): - self._XCKVPrint(file, 3, property, value) + # The remaining elements of an object dictionary are sorted alphabetically. + for property, value in sorted(self._properties.items()): + self._XCKVPrint(file, 3, property, value) - # End the object. - self._XCPrint(file, end_tabs, '};\n') + # End the object. + self._XCPrint(file, end_tabs, "};\n") - def UpdateProperties(self, properties, do_copy=False): - """Merge the supplied properties into the _properties dictionary. + def UpdateProperties(self, properties, do_copy=False): + """Merge the supplied properties into the _properties dictionary. The input properties must adhere to the class schema or a KeyError or TypeError exception will be raised. If adding an object of an XCObject @@ -745,206 +770,244 @@ def UpdateProperties(self, properties, do_copy=False): references added. """ - if properties is None: - return - - for property, value in properties.items(): - # Make sure the property is in the schema. - if not property in self._schema: - raise KeyError(property + ' not in ' + self.__class__.__name__) - - # Make sure the property conforms to the schema. - (is_list, property_type, is_strong) = self._schema[property][0:3] - if is_list: - if value.__class__ != list: - raise TypeError( - property + ' of ' + self.__class__.__name__ + \ - ' must be list, not ' + value.__class__.__name__) - for item in value: - if not isinstance(item, property_type) and \ - not (item.__class__ == unicode and property_type == str): - # Accept unicode where str is specified. str is treated as - # UTF-8-encoded. - raise TypeError( - 'item of ' + property + ' of ' + self.__class__.__name__ + \ - ' must be ' + property_type.__name__ + ', not ' + \ - item.__class__.__name__) - elif not isinstance(value, property_type) and \ - not (value.__class__ == unicode and property_type == str): - # Accept unicode where str is specified. str is treated as - # UTF-8-encoded. - raise TypeError( - property + ' of ' + self.__class__.__name__ + ' must be ' + \ - property_type.__name__ + ', not ' + value.__class__.__name__) - - # Checks passed, perform the assignment. - if do_copy: - if isinstance(value, XCObject): - if is_strong: - self._properties[property] = value.Copy() - else: - self._properties[property] = value - elif isinstance(value, (basestring, int)): - self._properties[property] = value - elif isinstance(value, list): - if is_strong: - # If is_strong is True, each element is an XCObject, so it's safe - # to call Copy. - self._properties[property] = [] - for item in value: - self._properties[property].append(item.Copy()) - else: - self._properties[property] = value[:] - elif isinstance(value, dict): - self._properties[property] = value.copy() - else: - raise TypeError("Don't know how to copy a " + \ - value.__class__.__name__ + ' object for ' + \ - property + ' in ' + self.__class__.__name__) - else: - self._properties[property] = value - - # Set up the child's back-reference to this object. Don't use |value| - # any more because it may not be right if do_copy is true. - if is_strong: + if properties is None: + return + + for property, value in properties.items(): + # Make sure the property is in the schema. + if property not in self._schema: + raise KeyError(property + " not in " + self.__class__.__name__) + + # Make sure the property conforms to the schema. + (is_list, property_type, is_strong) = self._schema[property][0:3] + if is_list: + if value.__class__ != list: + raise TypeError( + property + + " of " + + self.__class__.__name__ + + " must be list, not " + + value.__class__.__name__ + ) + for item in value: + if not isinstance(item, property_type) and not ( + isinstance(item, basestring) and property_type == str + ): + # Accept unicode where str is specified. str is treated as + # UTF-8-encoded. + raise TypeError( + "item of " + + property + + " of " + + self.__class__.__name__ + + " must be " + + property_type.__name__ + + ", not " + + item.__class__.__name__ + ) + elif not isinstance(value, property_type) and not ( + isinstance(value, basestring) and property_type == str + ): + # Accept unicode where str is specified. str is treated as + # UTF-8-encoded. + raise TypeError( + property + + " of " + + self.__class__.__name__ + + " must be " + + property_type.__name__ + + ", not " + + value.__class__.__name__ + ) + + # Checks passed, perform the assignment. + if do_copy: + if isinstance(value, XCObject): + if is_strong: + self._properties[property] = value.Copy() + else: + self._properties[property] = value + elif isinstance(value, (basestring, int)): + self._properties[property] = value + elif isinstance(value, list): + if is_strong: + # If is_strong is True, each element is an XCObject, + # so it's safe to call Copy. + self._properties[property] = [] + for item in value: + self._properties[property].append(item.Copy()) + else: + self._properties[property] = value[:] + elif isinstance(value, dict): + self._properties[property] = value.copy() + else: + raise TypeError( + "Don't know how to copy a " + + value.__class__.__name__ + + " object for " + + property + + " in " + + self.__class__.__name__ + ) + else: + self._properties[property] = value + + # Set up the child's back-reference to this object. Don't use |value| + # any more because it may not be right if do_copy is true. + if is_strong: + if not is_list: + self._properties[property].parent = self + else: + for item in self._properties[property]: + item.parent = self + + def HasProperty(self, key): + return key in self._properties + + def GetProperty(self, key): + return self._properties[key] + + def SetProperty(self, key, value): + self.UpdateProperties({key: value}) + + def DelProperty(self, key): + if key in self._properties: + del self._properties[key] + + def AppendProperty(self, key, value): + # TODO(mark): Support ExtendProperty too (and make this call that)? + + # Schema validation. + if key not in self._schema: + raise KeyError(key + " not in " + self.__class__.__name__) + + (is_list, property_type, is_strong) = self._schema[key][0:3] if not is_list: - self._properties[property].parent = self - else: - for item in self._properties[property]: - item.parent = self - - def HasProperty(self, key): - return key in self._properties - - def GetProperty(self, key): - return self._properties[key] - - def SetProperty(self, key, value): - self.UpdateProperties({key: value}) - - def DelProperty(self, key): - if key in self._properties: - del self._properties[key] - - def AppendProperty(self, key, value): - # TODO(mark): Support ExtendProperty too (and make this call that)? - - # Schema validation. - if not key in self._schema: - raise KeyError(key + ' not in ' + self.__class__.__name__) - - (is_list, property_type, is_strong) = self._schema[key][0:3] - if not is_list: - raise TypeError(key + ' of ' + self.__class__.__name__ + ' must be list') - if not isinstance(value, property_type): - raise TypeError('item of ' + key + ' of ' + self.__class__.__name__ + \ - ' must be ' + property_type.__name__ + ', not ' + \ - value.__class__.__name__) - - # If the property doesn't exist yet, create a new empty list to receive the - # item. - if not key in self._properties: - self._properties[key] = [] - - # Set up the ownership link. - if is_strong: - value.parent = self + raise TypeError(key + " of " + self.__class__.__name__ + " must be list") + if not isinstance(value, property_type): + raise TypeError( + "item of " + + key + + " of " + + self.__class__.__name__ + + " must be " + + property_type.__name__ + + ", not " + + value.__class__.__name__ + ) + + # If the property doesn't exist yet, create a new empty list to receive the + # item. + self._properties[key] = self._properties.get(key, []) + + # Set up the ownership link. + if is_strong: + value.parent = self - # Store the item. - self._properties[key].append(value) + # Store the item. + self._properties[key].append(value) - def VerifyHasRequiredProperties(self): - """Ensure that all properties identified as required by the schema are + def VerifyHasRequiredProperties(self): + """Ensure that all properties identified as required by the schema are set. """ - # TODO(mark): A stronger verification mechanism is needed. Some - # subclasses need to perform validation beyond what the schema can enforce. - for property, attributes in self._schema.items(): - (is_list, property_type, is_strong, is_required) = attributes[0:4] - if is_required and not property in self._properties: - raise KeyError(self.__class__.__name__ + ' requires ' + property) + # TODO(mark): A stronger verification mechanism is needed. Some + # subclasses need to perform validation beyond what the schema can enforce. + for property, attributes in self._schema.items(): + (is_list, property_type, is_strong, is_required) = attributes[0:4] + if is_required and property not in self._properties: + raise KeyError(self.__class__.__name__ + " requires " + property) - def _SetDefaultsFromSchema(self): - """Assign object default values according to the schema. This will not + def _SetDefaultsFromSchema(self): + """Assign object default values according to the schema. This will not overwrite properties that have already been set.""" - defaults = {} - for property, attributes in self._schema.items(): - (is_list, property_type, is_strong, is_required) = attributes[0:4] - if is_required and len(attributes) >= 5 and \ - not property in self._properties: - default = attributes[4] + defaults = {} + for property, attributes in self._schema.items(): + (is_list, property_type, is_strong, is_required) = attributes[0:4] + if ( + is_required + and len(attributes) >= 5 + and property not in self._properties + ): + default = attributes[4] - defaults[property] = default + defaults[property] = default - if len(defaults) > 0: - # Use do_copy=True so that each new object gets its own copy of strong - # objects, lists, and dicts. - self.UpdateProperties(defaults, do_copy=True) + if len(defaults) > 0: + # Use do_copy=True so that each new object gets its own copy of strong + # objects, lists, and dicts. + self.UpdateProperties(defaults, do_copy=True) class XCHierarchicalElement(XCObject): - """Abstract base for PBXGroup and PBXFileReference. Not represented in a + """Abstract base for PBXGroup and PBXFileReference. Not represented in a project file.""" - # TODO(mark): Do name and path belong here? Probably so. - # If path is set and name is not, name may have a default value. Name will - # be set to the basename of path, if the basename of path is different from - # the full value of path. If path is already just a leaf name, name will - # not be set. - _schema = XCObject._schema.copy() - _schema.update({ - 'comments': [0, str, 0, 0], - 'fileEncoding': [0, str, 0, 0], - 'includeInIndex': [0, int, 0, 0], - 'indentWidth': [0, int, 0, 0], - 'lineEnding': [0, int, 0, 0], - 'sourceTree': [0, str, 0, 1, ''], - 'tabWidth': [0, int, 0, 0], - 'usesTabs': [0, int, 0, 0], - 'wrapsLines': [0, int, 0, 0], - }) - - def __init__(self, properties=None, id=None, parent=None): - # super - XCObject.__init__(self, properties, id, parent) - if 'path' in self._properties and not 'name' in self._properties: - path = self._properties['path'] - name = posixpath.basename(path) - if name != '' and path != name: - self.SetProperty('name', name) - - if 'path' in self._properties and \ - (not 'sourceTree' in self._properties or \ - self._properties['sourceTree'] == ''): - # If the pathname begins with an Xcode variable like "$(SDKROOT)/", take - # the variable out and make the path be relative to that variable by - # assigning the variable name as the sourceTree. - (source_tree, path) = SourceTreeAndPathFromPath(self._properties['path']) - if source_tree != None: - self._properties['sourceTree'] = source_tree - if path != None: - self._properties['path'] = path - if source_tree != None and path is None and \ - not 'name' in self._properties: - # The path was of the form "$(SDKROOT)" with no path following it. - # This object is now relative to that variable, so it has no path - # attribute of its own. It does, however, keep a name. - del self._properties['path'] - self._properties['name'] = source_tree - - def Name(self): - if 'name' in self._properties: - return self._properties['name'] - elif 'path' in self._properties: - return self._properties['path'] - else: - # This happens in the case of the root PBXGroup. - return None + # TODO(mark): Do name and path belong here? Probably so. + # If path is set and name is not, name may have a default value. Name will + # be set to the basename of path, if the basename of path is different from + # the full value of path. If path is already just a leaf name, name will + # not be set. + _schema = XCObject._schema.copy() + _schema.update( + { + "comments": [0, str, 0, 0], + "fileEncoding": [0, str, 0, 0], + "includeInIndex": [0, int, 0, 0], + "indentWidth": [0, int, 0, 0], + "lineEnding": [0, int, 0, 0], + "sourceTree": [0, str, 0, 1, ""], + "tabWidth": [0, int, 0, 0], + "usesTabs": [0, int, 0, 0], + "wrapsLines": [0, int, 0, 0], + } + ) + + def __init__(self, properties=None, id=None, parent=None): + # super + XCObject.__init__(self, properties, id, parent) + if "path" in self._properties and "name" not in self._properties: + path = self._properties["path"] + name = posixpath.basename(path) + if name != "" and path != name: + self.SetProperty("name", name) + + if "path" in self._properties and ( + "sourceTree" not in self._properties + or self._properties["sourceTree"] == "" + ): + # If the pathname begins with an Xcode variable like "$(SDKROOT)/", take + # the variable out and make the path be relative to that variable by + # assigning the variable name as the sourceTree. + (source_tree, path) = SourceTreeAndPathFromPath(self._properties["path"]) + if source_tree is not None: + self._properties["sourceTree"] = source_tree + if path is not None: + self._properties["path"] = path + if ( + source_tree is not None + and path is None + and "name" not in self._properties + ): + # The path was of the form "$(SDKROOT)" with no path following it. + # This object is now relative to that variable, so it has no path + # attribute of its own. It does, however, keep a name. + del self._properties["path"] + self._properties["name"] = source_tree + + def Name(self): + if "name" in self._properties: + return self._properties["name"] + elif "path" in self._properties: + return self._properties["path"] + else: + # This happens in the case of the root PBXGroup. + return None - def Hashables(self): - """Custom hashables for XCHierarchicalElements. + def Hashables(self): + """Custom hashables for XCHierarchicalElements. XCHierarchicalElements are special. Generally, their hashes shouldn't change if the paths don't change. The normal XCObject implementation of @@ -968,128 +1031,134 @@ def Hashables(self): is not considered a problem because there can be only one main group. """ - if self == self.PBXProjectAncestor()._properties['mainGroup']: - # super - return XCObject.Hashables(self) - - hashables = [] - - # Put the name in first, ensuring that if TakeOverOnlyChild collapses - # children into a top-level group like "Source", the name always goes - # into the list of hashables without interfering with path components. - if 'name' in self._properties: - # Make it less likely for people to manipulate hashes by following the - # pattern of always pushing an object type value onto the list first. - hashables.append(self.__class__.__name__ + '.name') - hashables.append(self._properties['name']) - - # NOTE: This still has the problem that if an absolute path is encountered, - # including paths with a sourceTree, they'll still inherit their parents' - # hashables, even though the paths aren't relative to their parents. This - # is not expected to be much of a problem in practice. - path = self.PathFromSourceTreeAndPath() - if path != None: - components = path.split(posixpath.sep) - for component in components: - hashables.append(self.__class__.__name__ + '.path') - hashables.append(component) - - hashables.extend(self._hashables) - - return hashables - - def Compare(self, other): - # Allow comparison of these types. PBXGroup has the highest sort rank; - # PBXVariantGroup is treated as equal to PBXFileReference. - valid_class_types = { - PBXFileReference: 'file', - PBXGroup: 'group', - PBXVariantGroup: 'file', - } - self_type = valid_class_types[self.__class__] - other_type = valid_class_types[other.__class__] - - if self_type == other_type: - # If the two objects are of the same sort rank, compare their names. - return cmp(self.Name(), other.Name()) - - # Otherwise, sort groups before everything else. - if self_type == 'group': - return -1 - return 1 - - def CompareRootGroup(self, other): - # This function should be used only to compare direct children of the - # containing PBXProject's mainGroup. These groups should appear in the - # listed order. - # TODO(mark): "Build" is used by gyp.generator.xcode, perhaps the - # generator should have a way of influencing this list rather than having - # to hardcode for the generator here. - order = ['Source', 'Intermediates', 'Projects', 'Frameworks', 'Products', - 'Build'] - - # If the groups aren't in the listed order, do a name comparison. - # Otherwise, groups in the listed order should come before those that - # aren't. - self_name = self.Name() - other_name = other.Name() - self_in = isinstance(self, PBXGroup) and self_name in order - other_in = isinstance(self, PBXGroup) and other_name in order - if not self_in and not other_in: - return self.Compare(other) - if self_name in order and not other_name in order: - return -1 - if other_name in order and not self_name in order: - return 1 - - # If both groups are in the listed order, go by the defined order. - self_index = order.index(self_name) - other_index = order.index(other_name) - if self_index < other_index: - return -1 - if self_index > other_index: - return 1 - return 0 - - def PathFromSourceTreeAndPath(self): - # Turn the object's sourceTree and path properties into a single flat - # string of a form comparable to the path parameter. If there's a - # sourceTree property other than "", wrap it in $(...) for the - # comparison. - components = [] - if self._properties['sourceTree'] != '': - components.append('$(' + self._properties['sourceTree'] + ')') - if 'path' in self._properties: - components.append(self._properties['path']) - - if len(components) > 0: - return posixpath.join(*components) - - return None - - def FullPath(self): - # Returns a full path to self relative to the project file, or relative - # to some other source tree. Start with self, and walk up the chain of - # parents prepending their paths, if any, until no more parents are - # available (project-relative path) or until a path relative to some - # source tree is found. - xche = self - path = None - while isinstance(xche, XCHierarchicalElement) and \ - (path is None or \ - (not path.startswith('/') and not path.startswith('$'))): - this_path = xche.PathFromSourceTreeAndPath() - if this_path != None and path != None: - path = posixpath.join(this_path, path) - elif this_path != None: - path = this_path - xche = xche.parent - - return path + if self == self.PBXProjectAncestor()._properties["mainGroup"]: + # super + return XCObject.Hashables(self) + + hashables = [] + + # Put the name in first, ensuring that if TakeOverOnlyChild collapses + # children into a top-level group like "Source", the name always goes + # into the list of hashables without interfering with path components. + if "name" in self._properties: + # Make it less likely for people to manipulate hashes by following the + # pattern of always pushing an object type value onto the list first. + hashables.append(self.__class__.__name__ + ".name") + hashables.append(self._properties["name"]) + + # NOTE: This still has the problem that if an absolute path is encountered, + # including paths with a sourceTree, they'll still inherit their parents' + # hashables, even though the paths aren't relative to their parents. This + # is not expected to be much of a problem in practice. + path = self.PathFromSourceTreeAndPath() + if path is not None: + components = path.split(posixpath.sep) + for component in components: + hashables.append(self.__class__.__name__ + ".path") + hashables.append(component) + + hashables.extend(self._hashables) + + return hashables + + def Compare(self, other): + # Allow comparison of these types. PBXGroup has the highest sort rank; + # PBXVariantGroup is treated as equal to PBXFileReference. + valid_class_types = { + PBXFileReference: "file", + PBXGroup: "group", + PBXVariantGroup: "file", + } + self_type = valid_class_types[self.__class__] + other_type = valid_class_types[other.__class__] + + if self_type == other_type: + # If the two objects are of the same sort rank, compare their names. + return cmp(self.Name(), other.Name()) + + # Otherwise, sort groups before everything else. + if self_type == "group": + return -1 + return 1 + + def CompareRootGroup(self, other): + # This function should be used only to compare direct children of the + # containing PBXProject's mainGroup. These groups should appear in the + # listed order. + # TODO(mark): "Build" is used by gyp.generator.xcode, perhaps the + # generator should have a way of influencing this list rather than having + # to hardcode for the generator here. + order = [ + "Source", + "Intermediates", + "Projects", + "Frameworks", + "Products", + "Build", + ] + + # If the groups aren't in the listed order, do a name comparison. + # Otherwise, groups in the listed order should come before those that + # aren't. + self_name = self.Name() + other_name = other.Name() + self_in = isinstance(self, PBXGroup) and self_name in order + other_in = isinstance(self, PBXGroup) and other_name in order + if not self_in and not other_in: + return self.Compare(other) + if self_name in order and other_name not in order: + return -1 + if other_name in order and self_name not in order: + return 1 + + # If both groups are in the listed order, go by the defined order. + self_index = order.index(self_name) + other_index = order.index(other_name) + if self_index < other_index: + return -1 + if self_index > other_index: + return 1 + return 0 + + def PathFromSourceTreeAndPath(self): + # Turn the object's sourceTree and path properties into a single flat + # string of a form comparable to the path parameter. If there's a + # sourceTree property other than "", wrap it in $(...) for the + # comparison. + components = [] + if self._properties["sourceTree"] != "": + components.append("$(" + self._properties["sourceTree"] + ")") + if "path" in self._properties: + components.append(self._properties["path"]) + + if len(components) > 0: + return posixpath.join(*components) + + return None + + def FullPath(self): + # Returns a full path to self relative to the project file, or relative + # to some other source tree. Start with self, and walk up the chain of + # parents prepending their paths, if any, until no more parents are + # available (project-relative path) or until a path relative to some + # source tree is found. + xche = self + path = None + while isinstance(xche, XCHierarchicalElement) and ( + path is None or (not path.startswith("/") and not path.startswith("$")) + ): + this_path = xche.PathFromSourceTreeAndPath() + if this_path is not None and path is not None: + path = posixpath.join(this_path, path) + elif this_path is not None: + path = this_path + xche = xche.parent + + return path class PBXGroup(XCHierarchicalElement): - """ + """ Attributes: _children_by_path: Maps pathnames of children of this PBXGroup to the actual child XCHierarchicalElement objects. @@ -1097,116 +1166,122 @@ class PBXGroup(XCHierarchicalElement): PBXVariantGroup children to the actual child PBXVariantGroup objects. """ - _schema = XCHierarchicalElement._schema.copy() - _schema.update({ - 'children': [1, XCHierarchicalElement, 1, 1, []], - 'name': [0, str, 0, 0], - 'path': [0, str, 0, 0], - }) - - def __init__(self, properties=None, id=None, parent=None): - # super - XCHierarchicalElement.__init__(self, properties, id, parent) - self._children_by_path = {} - self._variant_children_by_name_and_path = {} - for child in self._properties.get('children', []): - self._AddChildToDicts(child) - - def Hashables(self): - # super - hashables = XCHierarchicalElement.Hashables(self) - - # It is not sufficient to just rely on name and parent to build a unique - # hashable : a node could have two child PBXGroup sharing a common name. - # To add entropy the hashable is enhanced with the names of all its - # children. - for child in self._properties.get('children', []): - child_name = child.Name() - if child_name != None: - hashables.append(child_name) - - return hashables - - def HashablesForChild(self): - # To avoid a circular reference the hashables used to compute a child id do - # not include the child names. - return XCHierarchicalElement.Hashables(self) - - def _AddChildToDicts(self, child): - # Sets up this PBXGroup object's dicts to reference the child properly. - child_path = child.PathFromSourceTreeAndPath() - if child_path: - if child_path in self._children_by_path: - raise ValueError('Found multiple children with path ' + child_path) - self._children_by_path[child_path] = child - - if isinstance(child, PBXVariantGroup): - child_name = child._properties.get('name', None) - key = (child_name, child_path) - if key in self._variant_children_by_name_and_path: - raise ValueError('Found multiple PBXVariantGroup children with ' + \ - 'name ' + str(child_name) + ' and path ' + \ - str(child_path)) - self._variant_children_by_name_and_path[key] = child - - def AppendChild(self, child): - # Callers should use this instead of calling - # AppendProperty('children', child) directly because this function - # maintains the group's dicts. - self.AppendProperty('children', child) - self._AddChildToDicts(child) - - def GetChildByName(self, name): - # This is not currently optimized with a dict as GetChildByPath is because - # it has few callers. Most callers probably want GetChildByPath. This - # function is only useful to get children that have names but no paths, - # which is rare. The children of the main group ("Source", "Products", - # etc.) is pretty much the only case where this likely to come up. - # - # TODO(mark): Maybe this should raise an error if more than one child is - # present with the same name. - if not 'children' in self._properties: - return None + _schema = XCHierarchicalElement._schema.copy() + _schema.update( + { + "children": [1, XCHierarchicalElement, 1, 1, []], + "name": [0, str, 0, 0], + "path": [0, str, 0, 0], + } + ) + + def __init__(self, properties=None, id=None, parent=None): + # super + XCHierarchicalElement.__init__(self, properties, id, parent) + self._children_by_path = {} + self._variant_children_by_name_and_path = {} + for child in self._properties.get("children", []): + self._AddChildToDicts(child) + + def Hashables(self): + # super + hashables = XCHierarchicalElement.Hashables(self) + + # It is not sufficient to just rely on name and parent to build a unique + # hashable : a node could have two child PBXGroup sharing a common name. + # To add entropy the hashable is enhanced with the names of all its + # children. + for child in self._properties.get("children", []): + child_name = child.Name() + if child_name is not None: + hashables.append(child_name) + + return hashables + + def HashablesForChild(self): + # To avoid a circular reference the hashables used to compute a child id do + # not include the child names. + return XCHierarchicalElement.Hashables(self) + + def _AddChildToDicts(self, child): + # Sets up this PBXGroup object's dicts to reference the child properly. + child_path = child.PathFromSourceTreeAndPath() + if child_path: + if child_path in self._children_by_path: + raise ValueError("Found multiple children with path " + child_path) + self._children_by_path[child_path] = child + + if isinstance(child, PBXVariantGroup): + child_name = child._properties.get("name", None) + key = (child_name, child_path) + if key in self._variant_children_by_name_and_path: + raise ValueError( + "Found multiple PBXVariantGroup children with " + + "name " + + str(child_name) + + " and path " + + str(child_path) + ) + self._variant_children_by_name_and_path[key] = child + + def AppendChild(self, child): + # Callers should use this instead of calling + # AppendProperty('children', child) directly because this function + # maintains the group's dicts. + self.AppendProperty("children", child) + self._AddChildToDicts(child) + + def GetChildByName(self, name): + # This is not currently optimized with a dict as GetChildByPath is because + # it has few callers. Most callers probably want GetChildByPath. This + # function is only useful to get children that have names but no paths, + # which is rare. The children of the main group ("Source", "Products", + # etc.) is pretty much the only case where this likely to come up. + # + # TODO(mark): Maybe this should raise an error if more than one child is + # present with the same name. + if "children" not in self._properties: + return None - for child in self._properties['children']: - if child.Name() == name: - return child + for child in self._properties["children"]: + if child.Name() == name: + return child - return None + return None - def GetChildByPath(self, path): - if not path: - return None + def GetChildByPath(self, path): + if not path: + return None - if path in self._children_by_path: - return self._children_by_path[path] + if path in self._children_by_path: + return self._children_by_path[path] - return None + return None - def GetChildByRemoteObject(self, remote_object): - # This method is a little bit esoteric. Given a remote_object, which - # should be a PBXFileReference in another project file, this method will - # return this group's PBXReferenceProxy object serving as a local proxy - # for the remote PBXFileReference. - # - # This function might benefit from a dict optimization as GetChildByPath - # for some workloads, but profiling shows that it's not currently a - # problem. - if not 'children' in self._properties: - return None + def GetChildByRemoteObject(self, remote_object): + # This method is a little bit esoteric. Given a remote_object, which + # should be a PBXFileReference in another project file, this method will + # return this group's PBXReferenceProxy object serving as a local proxy + # for the remote PBXFileReference. + # + # This function might benefit from a dict optimization as GetChildByPath + # for some workloads, but profiling shows that it's not currently a + # problem. + if "children" not in self._properties: + return None - for child in self._properties['children']: - if not isinstance(child, PBXReferenceProxy): - continue + for child in self._properties["children"]: + if not isinstance(child, PBXReferenceProxy): + continue - container_proxy = child._properties['remoteRef'] - if container_proxy._properties['remoteGlobalIDString'] == remote_object: - return child + container_proxy = child._properties["remoteRef"] + if container_proxy._properties["remoteGlobalIDString"] == remote_object: + return child - return None + return None - def AddOrGetFileByPath(self, path, hierarchical): - """Returns an existing or new file reference corresponding to path. + def AddOrGetFileByPath(self, path, hierarchical): + """Returns an existing or new file reference corresponding to path. If hierarchical is True, this method will create or use the necessary hierarchical group structure corresponding to path. Otherwise, it will @@ -1223,83 +1298,88 @@ def AddOrGetFileByPath(self, path, hierarchical): all other paths, a "normal" PBXFileReference will be returned. """ - # Adding or getting a directory? Directories end with a trailing slash. - is_dir = False - if path.endswith('/'): - is_dir = True - path = posixpath.normpath(path) - if is_dir: - path = path + '/' - - # Adding or getting a variant? Variants are files inside directories - # with an ".lproj" extension. Xcode uses variants for localization. For - # a variant path/to/Language.lproj/MainMenu.nib, put a variant group named - # MainMenu.nib inside path/to, and give it a variant named Language. In - # this example, grandparent would be set to path/to and parent_root would - # be set to Language. - variant_name = None - parent = posixpath.dirname(path) - grandparent = posixpath.dirname(parent) - parent_basename = posixpath.basename(parent) - (parent_root, parent_ext) = posixpath.splitext(parent_basename) - if parent_ext == '.lproj': - variant_name = parent_root - if grandparent == '': - grandparent = None - - # Putting a directory inside a variant group is not currently supported. - assert not is_dir or variant_name is None - - path_split = path.split(posixpath.sep) - if len(path_split) == 1 or \ - ((is_dir or variant_name != None) and len(path_split) == 2) or \ - not hierarchical: - # The PBXFileReference or PBXVariantGroup will be added to or gotten from - # this PBXGroup, no recursion necessary. - if variant_name is None: - # Add or get a PBXFileReference. - file_ref = self.GetChildByPath(path) - if file_ref != None: - assert file_ref.__class__ == PBXFileReference + # Adding or getting a directory? Directories end with a trailing slash. + is_dir = False + if path.endswith("/"): + is_dir = True + path = posixpath.normpath(path) + if is_dir: + path = path + "/" + + # Adding or getting a variant? Variants are files inside directories + # with an ".lproj" extension. Xcode uses variants for localization. For + # a variant path/to/Language.lproj/MainMenu.nib, put a variant group named + # MainMenu.nib inside path/to, and give it a variant named Language. In + # this example, grandparent would be set to path/to and parent_root would + # be set to Language. + variant_name = None + parent = posixpath.dirname(path) + grandparent = posixpath.dirname(parent) + parent_basename = posixpath.basename(parent) + (parent_root, parent_ext) = posixpath.splitext(parent_basename) + if parent_ext == ".lproj": + variant_name = parent_root + if grandparent == "": + grandparent = None + + # Putting a directory inside a variant group is not currently supported. + assert not is_dir or variant_name is None + + path_split = path.split(posixpath.sep) + if ( + len(path_split) == 1 + or ((is_dir or variant_name is not None) and len(path_split) == 2) + or not hierarchical + ): + # The PBXFileReference or PBXVariantGroup will be added to or gotten from + # this PBXGroup, no recursion necessary. + if variant_name is None: + # Add or get a PBXFileReference. + file_ref = self.GetChildByPath(path) + if file_ref is not None: + assert file_ref.__class__ == PBXFileReference + else: + file_ref = PBXFileReference({"path": path}) + self.AppendChild(file_ref) + else: + # Add or get a PBXVariantGroup. The variant group name is the same + # as the basename (MainMenu.nib in the example above). grandparent + # specifies the path to the variant group itself, and path_split[-2:] + # is the path of the specific variant relative to its group. + variant_group_name = posixpath.basename(path) + variant_group_ref = self.AddOrGetVariantGroupByNameAndPath( + variant_group_name, grandparent + ) + variant_path = posixpath.sep.join(path_split[-2:]) + variant_ref = variant_group_ref.GetChildByPath(variant_path) + if variant_ref is not None: + assert variant_ref.__class__ == PBXFileReference + else: + variant_ref = PBXFileReference( + {"name": variant_name, "path": variant_path} + ) + variant_group_ref.AppendChild(variant_ref) + # The caller is interested in the variant group, not the specific + # variant file. + file_ref = variant_group_ref + return file_ref else: - file_ref = PBXFileReference({'path': path}) - self.AppendChild(file_ref) - else: - # Add or get a PBXVariantGroup. The variant group name is the same - # as the basename (MainMenu.nib in the example above). grandparent - # specifies the path to the variant group itself, and path_split[-2:] - # is the path of the specific variant relative to its group. - variant_group_name = posixpath.basename(path) - variant_group_ref = self.AddOrGetVariantGroupByNameAndPath( - variant_group_name, grandparent) - variant_path = posixpath.sep.join(path_split[-2:]) - variant_ref = variant_group_ref.GetChildByPath(variant_path) - if variant_ref != None: - assert variant_ref.__class__ == PBXFileReference - else: - variant_ref = PBXFileReference({'name': variant_name, - 'path': variant_path}) - variant_group_ref.AppendChild(variant_ref) - # The caller is interested in the variant group, not the specific - # variant file. - file_ref = variant_group_ref - return file_ref - else: - # Hierarchical recursion. Add or get a PBXGroup corresponding to the - # outermost path component, and then recurse into it, chopping off that - # path component. - next_dir = path_split[0] - group_ref = self.GetChildByPath(next_dir) - if group_ref != None: - assert group_ref.__class__ == PBXGroup - else: - group_ref = PBXGroup({'path': next_dir}) - self.AppendChild(group_ref) - return group_ref.AddOrGetFileByPath(posixpath.sep.join(path_split[1:]), - hierarchical) - - def AddOrGetVariantGroupByNameAndPath(self, name, path): - """Returns an existing or new PBXVariantGroup for name and path. + # Hierarchical recursion. Add or get a PBXGroup corresponding to the + # outermost path component, and then recurse into it, chopping off that + # path component. + next_dir = path_split[0] + group_ref = self.GetChildByPath(next_dir) + if group_ref is not None: + assert group_ref.__class__ == PBXGroup + else: + group_ref = PBXGroup({"path": next_dir}) + self.AppendChild(group_ref) + return group_ref.AddOrGetFileByPath( + posixpath.sep.join(path_split[1:]), hierarchical + ) + + def AddOrGetVariantGroupByNameAndPath(self, name, path): + """Returns an existing or new PBXVariantGroup for name and path. If a PBXVariantGroup identified by the name and path arguments is already present as a child of this object, it is returned. Otherwise, a new @@ -1311,22 +1391,22 @@ def AddOrGetVariantGroupByNameAndPath(self, name, path): passed to it. """ - key = (name, path) - if key in self._variant_children_by_name_and_path: - variant_group_ref = self._variant_children_by_name_and_path[key] - assert variant_group_ref.__class__ == PBXVariantGroup - return variant_group_ref + key = (name, path) + if key in self._variant_children_by_name_and_path: + variant_group_ref = self._variant_children_by_name_and_path[key] + assert variant_group_ref.__class__ == PBXVariantGroup + return variant_group_ref - variant_group_properties = {'name': name} - if path != None: - variant_group_properties['path'] = path - variant_group_ref = PBXVariantGroup(variant_group_properties) - self.AppendChild(variant_group_ref) + variant_group_properties = {"name": name} + if path is not None: + variant_group_properties["path"] = path + variant_group_ref = PBXVariantGroup(variant_group_properties) + self.AppendChild(variant_group_ref) - return variant_group_ref + return variant_group_ref - def TakeOverOnlyChild(self, recurse=False): - """If this PBXGroup has only one child and it's also a PBXGroup, take + def TakeOverOnlyChild(self, recurse=False): + """If this PBXGroup has only one child and it's also a PBXGroup, take it over by making all of its children this object's children. This function will continue to take over only children when those children @@ -1341,210 +1421,226 @@ def TakeOverOnlyChild(self, recurse=False): a group for a/b/c containing a group for d3/e. """ - # At this stage, check that child class types are PBXGroup exactly, - # instead of using isinstance. The only subclass of PBXGroup, - # PBXVariantGroup, should not participate in reparenting in the same way: - # reparenting by merging different object types would be wrong. - while len(self._properties['children']) == 1 and \ - self._properties['children'][0].__class__ == PBXGroup: - # Loop to take over the innermost only-child group possible. - - child = self._properties['children'][0] - - # Assume the child's properties, including its children. Save a copy - # of this object's old properties, because they'll still be needed. - # This object retains its existing id and parent attributes. - old_properties = self._properties - self._properties = child._properties - self._children_by_path = child._children_by_path - - if not 'sourceTree' in self._properties or \ - self._properties['sourceTree'] == '': - # The child was relative to its parent. Fix up the path. Note that - # children with a sourceTree other than "" are not relative to - # their parents, so no path fix-up is needed in that case. - if 'path' in old_properties: - if 'path' in self._properties: - # Both the original parent and child have paths set. - self._properties['path'] = posixpath.join(old_properties['path'], - self._properties['path']) - else: - # Only the original parent has a path, use it. - self._properties['path'] = old_properties['path'] - if 'sourceTree' in old_properties: - # The original parent had a sourceTree set, use it. - self._properties['sourceTree'] = old_properties['sourceTree'] - - # If the original parent had a name set, keep using it. If the original - # parent didn't have a name but the child did, let the child's name - # live on. If the name attribute seems unnecessary now, get rid of it. - if 'name' in old_properties and old_properties['name'] != None and \ - old_properties['name'] != self.Name(): - self._properties['name'] = old_properties['name'] - if 'name' in self._properties and 'path' in self._properties and \ - self._properties['name'] == self._properties['path']: - del self._properties['name'] - - # Notify all children of their new parent. - for child in self._properties['children']: - child.parent = self - - # If asked to recurse, recurse. - if recurse: - for child in self._properties['children']: - if child.__class__ == PBXGroup: - child.TakeOverOnlyChild(recurse) - - def SortGroup(self): - self._properties['children'] = \ - sorted(self._properties['children'], cmp=lambda x,y: x.Compare(y)) - - # Recurse. - for child in self._properties['children']: - if isinstance(child, PBXGroup): - child.SortGroup() + # At this stage, check that child class types are PBXGroup exactly, + # instead of using isinstance. The only subclass of PBXGroup, + # PBXVariantGroup, should not participate in reparenting in the same way: + # reparenting by merging different object types would be wrong. + while ( + len(self._properties["children"]) == 1 + and self._properties["children"][0].__class__ == PBXGroup + ): + # Loop to take over the innermost only-child group possible. + + child = self._properties["children"][0] + + # Assume the child's properties, including its children. Save a copy + # of this object's old properties, because they'll still be needed. + # This object retains its existing id and parent attributes. + old_properties = self._properties + self._properties = child._properties + self._children_by_path = child._children_by_path + + if ( + "sourceTree" not in self._properties + or self._properties["sourceTree"] == "" + ): + # The child was relative to its parent. Fix up the path. Note that + # children with a sourceTree other than "" are not relative to + # their parents, so no path fix-up is needed in that case. + if "path" in old_properties: + if "path" in self._properties: + # Both the original parent and child have paths set. + self._properties["path"] = posixpath.join( + old_properties["path"], self._properties["path"] + ) + else: + # Only the original parent has a path, use it. + self._properties["path"] = old_properties["path"] + if "sourceTree" in old_properties: + # The original parent had a sourceTree set, use it. + self._properties["sourceTree"] = old_properties["sourceTree"] + + # If the original parent had a name set, keep using it. If the original + # parent didn't have a name but the child did, let the child's name + # live on. If the name attribute seems unnecessary now, get rid of it. + if "name" in old_properties and old_properties["name"] not in ( + None, + self.Name(), + ): + self._properties["name"] = old_properties["name"] + if ( + "name" in self._properties + and "path" in self._properties + and self._properties["name"] == self._properties["path"] + ): + del self._properties["name"] + + # Notify all children of their new parent. + for child in self._properties["children"]: + child.parent = self + + # If asked to recurse, recurse. + if recurse: + for child in self._properties["children"]: + if child.__class__ == PBXGroup: + child.TakeOverOnlyChild(recurse) + + def SortGroup(self): + self._properties["children"] = sorted( + self._properties["children"], cmp=lambda x, y: x.Compare(y) + ) + + # Recurse. + for child in self._properties["children"]: + if isinstance(child, PBXGroup): + child.SortGroup() class XCFileLikeElement(XCHierarchicalElement): - # Abstract base for objects that can be used as the fileRef property of - # PBXBuildFile. - - def PathHashables(self): - # A PBXBuildFile that refers to this object will call this method to - # obtain additional hashables specific to this XCFileLikeElement. Don't - # just use this object's hashables, they're not specific and unique enough - # on their own (without access to the parent hashables.) Instead, provide - # hashables that identify this object by path by getting its hashables as - # well as the hashables of ancestor XCHierarchicalElement objects. - - hashables = [] - xche = self - while xche != None and isinstance(xche, XCHierarchicalElement): - xche_hashables = xche.Hashables() - for index in range(0, len(xche_hashables)): - hashables.insert(index, xche_hashables[index]) - xche = xche.parent - return hashables + # Abstract base for objects that can be used as the fileRef property of + # PBXBuildFile. + + def PathHashables(self): + # A PBXBuildFile that refers to this object will call this method to + # obtain additional hashables specific to this XCFileLikeElement. Don't + # just use this object's hashables, they're not specific and unique enough + # on their own (without access to the parent hashables.) Instead, provide + # hashables that identify this object by path by getting its hashables as + # well as the hashables of ancestor XCHierarchicalElement objects. + + hashables = [] + xche = self + while isinstance(xche, XCHierarchicalElement): + xche_hashables = xche.Hashables() + for index, xche_hashable in enumerate(xche_hashables): + hashables.insert(index, xche_hashable) + xche = xche.parent + return hashables class XCContainerPortal(XCObject): - # Abstract base for objects that can be used as the containerPortal property - # of PBXContainerItemProxy. - pass + # Abstract base for objects that can be used as the containerPortal property + # of PBXContainerItemProxy. + pass class XCRemoteObject(XCObject): - # Abstract base for objects that can be used as the remoteGlobalIDString - # property of PBXContainerItemProxy. - pass + # Abstract base for objects that can be used as the remoteGlobalIDString + # property of PBXContainerItemProxy. + pass class PBXFileReference(XCFileLikeElement, XCContainerPortal, XCRemoteObject): - _schema = XCFileLikeElement._schema.copy() - _schema.update({ - 'explicitFileType': [0, str, 0, 0], - 'lastKnownFileType': [0, str, 0, 0], - 'name': [0, str, 0, 0], - 'path': [0, str, 0, 1], - }) - - # Weird output rules for PBXFileReference. - _should_print_single_line = True - # super - _encode_transforms = XCFileLikeElement._alternate_encode_transforms - - def __init__(self, properties=None, id=None, parent=None): + _schema = XCFileLikeElement._schema.copy() + _schema.update( + { + "explicitFileType": [0, str, 0, 0], + "lastKnownFileType": [0, str, 0, 0], + "name": [0, str, 0, 0], + "path": [0, str, 0, 1], + } + ) + + # Weird output rules for PBXFileReference. + _should_print_single_line = True # super - XCFileLikeElement.__init__(self, properties, id, parent) - if 'path' in self._properties and self._properties['path'].endswith('/'): - self._properties['path'] = self._properties['path'][:-1] - is_dir = True - else: - is_dir = False - - if 'path' in self._properties and \ - not 'lastKnownFileType' in self._properties and \ - not 'explicitFileType' in self._properties: - # TODO(mark): This is the replacement for a replacement for a quick hack. - # It is no longer incredibly sucky, but this list needs to be extended. - extension_map = { - 'a': 'archive.ar', - 'app': 'wrapper.application', - 'bdic': 'file', - 'bundle': 'wrapper.cfbundle', - 'c': 'sourcecode.c.c', - 'cc': 'sourcecode.cpp.cpp', - 'cpp': 'sourcecode.cpp.cpp', - 'css': 'text.css', - 'cxx': 'sourcecode.cpp.cpp', - 'dart': 'sourcecode', - 'dylib': 'compiled.mach-o.dylib', - 'framework': 'wrapper.framework', - 'gyp': 'sourcecode', - 'gypi': 'sourcecode', - 'h': 'sourcecode.c.h', - 'hxx': 'sourcecode.cpp.h', - 'icns': 'image.icns', - 'java': 'sourcecode.java', - 'js': 'sourcecode.javascript', - 'kext': 'wrapper.kext', - 'm': 'sourcecode.c.objc', - 'mm': 'sourcecode.cpp.objcpp', - 'nib': 'wrapper.nib', - 'o': 'compiled.mach-o.objfile', - 'pdf': 'image.pdf', - 'pl': 'text.script.perl', - 'plist': 'text.plist.xml', - 'pm': 'text.script.perl', - 'png': 'image.png', - 'py': 'text.script.python', - 'r': 'sourcecode.rez', - 'rez': 'sourcecode.rez', - 's': 'sourcecode.asm', - 'storyboard': 'file.storyboard', - 'strings': 'text.plist.strings', - 'swift': 'sourcecode.swift', - 'ttf': 'file', - 'xcassets': 'folder.assetcatalog', - 'xcconfig': 'text.xcconfig', - 'xcdatamodel': 'wrapper.xcdatamodel', - 'xcdatamodeld':'wrapper.xcdatamodeld', - 'xib': 'file.xib', - 'y': 'sourcecode.yacc', - } - - prop_map = { - 'dart': 'explicitFileType', - 'gyp': 'explicitFileType', - 'gypi': 'explicitFileType', - } - - if is_dir: - file_type = 'folder' - prop_name = 'lastKnownFileType' - else: - basename = posixpath.basename(self._properties['path']) - (root, ext) = posixpath.splitext(basename) - # Check the map using a lowercase extension. - # TODO(mark): Maybe it should try with the original case first and fall - # back to lowercase, in case there are any instances where case - # matters. There currently aren't. - if ext != '': - ext = ext[1:].lower() - - # TODO(mark): "text" is the default value, but "file" is appropriate - # for unrecognized files not containing text. Xcode seems to choose - # based on content. - file_type = extension_map.get(ext, 'text') - prop_name = prop_map.get(ext, 'lastKnownFileType') - - self._properties[prop_name] = file_type + _encode_transforms = XCFileLikeElement._alternate_encode_transforms + + def __init__(self, properties=None, id=None, parent=None): + # super + XCFileLikeElement.__init__(self, properties, id, parent) + if "path" in self._properties and self._properties["path"].endswith("/"): + self._properties["path"] = self._properties["path"][:-1] + is_dir = True + else: + is_dir = False + + if ( + "path" in self._properties + and "lastKnownFileType" not in self._properties + and "explicitFileType" not in self._properties + ): + # TODO(mark): This is the replacement for a replacement for a quick hack. + # It is no longer incredibly sucky, but this list needs to be extended. + extension_map = { + "a": "archive.ar", + "app": "wrapper.application", + "bdic": "file", + "bundle": "wrapper.cfbundle", + "c": "sourcecode.c.c", + "cc": "sourcecode.cpp.cpp", + "cpp": "sourcecode.cpp.cpp", + "css": "text.css", + "cxx": "sourcecode.cpp.cpp", + "dart": "sourcecode", + "dylib": "compiled.mach-o.dylib", + "framework": "wrapper.framework", + "gyp": "sourcecode", + "gypi": "sourcecode", + "h": "sourcecode.c.h", + "hxx": "sourcecode.cpp.h", + "icns": "image.icns", + "java": "sourcecode.java", + "js": "sourcecode.javascript", + "kext": "wrapper.kext", + "m": "sourcecode.c.objc", + "mm": "sourcecode.cpp.objcpp", + "nib": "wrapper.nib", + "o": "compiled.mach-o.objfile", + "pdf": "image.pdf", + "pl": "text.script.perl", + "plist": "text.plist.xml", + "pm": "text.script.perl", + "png": "image.png", + "py": "text.script.python", + "r": "sourcecode.rez", + "rez": "sourcecode.rez", + "s": "sourcecode.asm", + "storyboard": "file.storyboard", + "strings": "text.plist.strings", + "swift": "sourcecode.swift", + "ttf": "file", + "xcassets": "folder.assetcatalog", + "xcconfig": "text.xcconfig", + "xcdatamodel": "wrapper.xcdatamodel", + "xcdatamodeld": "wrapper.xcdatamodeld", + "xib": "file.xib", + "y": "sourcecode.yacc", + } + + prop_map = { + "dart": "explicitFileType", + "gyp": "explicitFileType", + "gypi": "explicitFileType", + } + + if is_dir: + file_type = "folder" + prop_name = "lastKnownFileType" + else: + basename = posixpath.basename(self._properties["path"]) + (root, ext) = posixpath.splitext(basename) + # Check the map using a lowercase extension. + # TODO(mark): Maybe it should try with the original case first and fall + # back to lowercase, in case there are any instances where case + # matters. There currently aren't. + if ext != "": + ext = ext[1:].lower() + + # TODO(mark): "text" is the default value, but "file" is appropriate + # for unrecognized files not containing text. Xcode seems to choose + # based on content. + file_type = extension_map.get(ext, "text") + prop_name = prop_map.get(ext, "lastKnownFileType") + + self._properties[prop_name] = file_type class PBXVariantGroup(PBXGroup, XCFileLikeElement): - """PBXVariantGroup is used by Xcode to represent localizations.""" - # No additions to the schema relative to PBXGroup. - pass + """PBXVariantGroup is used by Xcode to represent localizations.""" + + # No additions to the schema relative to PBXGroup. + pass # PBXReferenceProxy is also an XCFileLikeElement subclass. It is defined below @@ -1552,65 +1648,77 @@ class PBXVariantGroup(PBXGroup, XCFileLikeElement): class XCBuildConfiguration(XCObject): - _schema = XCObject._schema.copy() - _schema.update({ - 'baseConfigurationReference': [0, PBXFileReference, 0, 0], - 'buildSettings': [0, dict, 0, 1, {}], - 'name': [0, str, 0, 1], - }) + _schema = XCObject._schema.copy() + _schema.update( + { + "baseConfigurationReference": [0, PBXFileReference, 0, 0], + "buildSettings": [0, dict, 0, 1, {}], + "name": [0, str, 0, 1], + } + ) + + def HasBuildSetting(self, key): + return key in self._properties["buildSettings"] - def HasBuildSetting(self, key): - return key in self._properties['buildSettings'] + def GetBuildSetting(self, key): + return self._properties["buildSettings"][key] - def GetBuildSetting(self, key): - return self._properties['buildSettings'][key] + def SetBuildSetting(self, key, value): + # TODO(mark): If a list, copy? + self._properties["buildSettings"][key] = value - def SetBuildSetting(self, key, value): - # TODO(mark): If a list, copy? - self._properties['buildSettings'][key] = value + def AppendBuildSetting(self, key, value): + if key not in self._properties["buildSettings"]: + self._properties["buildSettings"][key] = [] + self._properties["buildSettings"][key].append(value) - def AppendBuildSetting(self, key, value): - if not key in self._properties['buildSettings']: - self._properties['buildSettings'][key] = [] - self._properties['buildSettings'][key].append(value) + def DelBuildSetting(self, key): + if key in self._properties["buildSettings"]: + del self._properties["buildSettings"][key] - def DelBuildSetting(self, key): - if key in self._properties['buildSettings']: - del self._properties['buildSettings'][key] + def SetBaseConfiguration(self, value): + self._properties["baseConfigurationReference"] = value - def SetBaseConfiguration(self, value): - self._properties['baseConfigurationReference'] = value class XCConfigurationList(XCObject): - # _configs is the default list of configurations. - _configs = [ XCBuildConfiguration({'name': 'Debug'}), - XCBuildConfiguration({'name': 'Release'}) ] - - _schema = XCObject._schema.copy() - _schema.update({ - 'buildConfigurations': [1, XCBuildConfiguration, 1, 1, _configs], - 'defaultConfigurationIsVisible': [0, int, 0, 1, 1], - 'defaultConfigurationName': [0, str, 0, 1, 'Release'], - }) - - def Name(self): - return 'Build configuration list for ' + \ - self.parent.__class__.__name__ + ' "' + self.parent.Name() + '"' - - def ConfigurationNamed(self, name): - """Convenience accessor to obtain an XCBuildConfiguration by name.""" - for configuration in self._properties['buildConfigurations']: - if configuration._properties['name'] == name: - return configuration - - raise KeyError(name) - - def DefaultConfiguration(self): - """Convenience accessor to obtain the default XCBuildConfiguration.""" - return self.ConfigurationNamed(self._properties['defaultConfigurationName']) - - def HasBuildSetting(self, key): - """Determines the state of a build setting in all XCBuildConfiguration + # _configs is the default list of configurations. + _configs = [ + XCBuildConfiguration({"name": "Debug"}), + XCBuildConfiguration({"name": "Release"}), + ] + + _schema = XCObject._schema.copy() + _schema.update( + { + "buildConfigurations": [1, XCBuildConfiguration, 1, 1, _configs], + "defaultConfigurationIsVisible": [0, int, 0, 1, 1], + "defaultConfigurationName": [0, str, 0, 1, "Release"], + } + ) + + def Name(self): + return ( + "Build configuration list for " + + self.parent.__class__.__name__ + + ' "' + + self.parent.Name() + + '"' + ) + + def ConfigurationNamed(self, name): + """Convenience accessor to obtain an XCBuildConfiguration by name.""" + for configuration in self._properties["buildConfigurations"]: + if configuration._properties["name"] == name: + return configuration + + raise KeyError(name) + + def DefaultConfiguration(self): + """Convenience accessor to obtain the default XCBuildConfiguration.""" + return self.ConfigurationNamed(self._properties["defaultConfigurationName"]) + + def HasBuildSetting(self, key): + """Determines the state of a build setting in all XCBuildConfiguration child objects. If all child objects have key in their build settings, and the value is the @@ -1622,112 +1730,114 @@ def HasBuildSetting(self, key): or if any children have different values for the key, returns -1. """ - has = None - value = None - for configuration in self._properties['buildConfigurations']: - configuration_has = configuration.HasBuildSetting(key) - if has is None: - has = configuration_has - elif has != configuration_has: - return -1 + has = None + value = None + for configuration in self._properties["buildConfigurations"]: + configuration_has = configuration.HasBuildSetting(key) + if has is None: + has = configuration_has + elif has != configuration_has: + return -1 - if configuration_has: - configuration_value = configuration.GetBuildSetting(key) - if value is None: - value = configuration_value - elif value != configuration_value: - return -1 + if configuration_has: + configuration_value = configuration.GetBuildSetting(key) + if value is None: + value = configuration_value + elif value != configuration_value: + return -1 - if not has: - return 0 + if not has: + return 0 - return 1 + return 1 - def GetBuildSetting(self, key): - """Gets the build setting for key. + def GetBuildSetting(self, key): + """Gets the build setting for key. All child XCConfiguration objects must have the same value set for the setting, or a ValueError will be raised. """ - # TODO(mark): This is wrong for build settings that are lists. The list - # contents should be compared (and a list copy returned?) + # TODO(mark): This is wrong for build settings that are lists. The list + # contents should be compared (and a list copy returned?) - value = None - for configuration in self._properties['buildConfigurations']: - configuration_value = configuration.GetBuildSetting(key) - if value is None: - value = configuration_value - else: - if value != configuration_value: - raise ValueError('Variant values for ' + key) + value = None + for configuration in self._properties["buildConfigurations"]: + configuration_value = configuration.GetBuildSetting(key) + if value is None: + value = configuration_value + else: + if value != configuration_value: + raise ValueError("Variant values for " + key) - return value + return value - def SetBuildSetting(self, key, value): - """Sets the build setting for key to value in all child + def SetBuildSetting(self, key, value): + """Sets the build setting for key to value in all child XCBuildConfiguration objects. """ - for configuration in self._properties['buildConfigurations']: - configuration.SetBuildSetting(key, value) + for configuration in self._properties["buildConfigurations"]: + configuration.SetBuildSetting(key, value) - def AppendBuildSetting(self, key, value): - """Appends value to the build setting for key, which is treated as a list, + def AppendBuildSetting(self, key, value): + """Appends value to the build setting for key, which is treated as a list, in all child XCBuildConfiguration objects. """ - for configuration in self._properties['buildConfigurations']: - configuration.AppendBuildSetting(key, value) + for configuration in self._properties["buildConfigurations"]: + configuration.AppendBuildSetting(key, value) - def DelBuildSetting(self, key): - """Deletes the build setting key from all child XCBuildConfiguration + def DelBuildSetting(self, key): + """Deletes the build setting key from all child XCBuildConfiguration objects. """ - for configuration in self._properties['buildConfigurations']: - configuration.DelBuildSetting(key) + for configuration in self._properties["buildConfigurations"]: + configuration.DelBuildSetting(key) - def SetBaseConfiguration(self, value): - """Sets the build configuration in all child XCBuildConfiguration objects. + def SetBaseConfiguration(self, value): + """Sets the build configuration in all child XCBuildConfiguration objects. """ - for configuration in self._properties['buildConfigurations']: - configuration.SetBaseConfiguration(value) + for configuration in self._properties["buildConfigurations"]: + configuration.SetBaseConfiguration(value) class PBXBuildFile(XCObject): - _schema = XCObject._schema.copy() - _schema.update({ - 'fileRef': [0, XCFileLikeElement, 0, 1], - 'settings': [0, str, 0, 0], # hack, it's a dict - }) + _schema = XCObject._schema.copy() + _schema.update( + { + "fileRef": [0, XCFileLikeElement, 0, 1], + "settings": [0, str, 0, 0], # hack, it's a dict + } + ) - # Weird output rules for PBXBuildFile. - _should_print_single_line = True - _encode_transforms = XCObject._alternate_encode_transforms + # Weird output rules for PBXBuildFile. + _should_print_single_line = True + _encode_transforms = XCObject._alternate_encode_transforms - def Name(self): - # Example: "main.cc in Sources" - return self._properties['fileRef'].Name() + ' in ' + self.parent.Name() + def Name(self): + # Example: "main.cc in Sources" + return self._properties["fileRef"].Name() + " in " + self.parent.Name() - def Hashables(self): - # super - hashables = XCObject.Hashables(self) + def Hashables(self): + # super + hashables = XCObject.Hashables(self) - # It is not sufficient to just rely on Name() to get the - # XCFileLikeElement's name, because that is not a complete pathname. - # PathHashables returns hashables unique enough that no two - # PBXBuildFiles should wind up with the same set of hashables, unless - # someone adds the same file multiple times to the same target. That - # would be considered invalid anyway. - hashables.extend(self._properties['fileRef'].PathHashables()) + # It is not sufficient to just rely on Name() to get the + # XCFileLikeElement's name, because that is not a complete pathname. + # PathHashables returns hashables unique enough that no two + # PBXBuildFiles should wind up with the same set of hashables, unless + # someone adds the same file multiple times to the same target. That + # would be considered invalid anyway. + hashables.extend(self._properties["fileRef"].PathHashables()) - return hashables + return hashables class XCBuildPhase(XCObject): - """Abstract base for build phase classes. Not represented in a project + """Abstract base for build phase classes. Not represented in a project file. Attributes: @@ -1737,51 +1847,52 @@ class XCBuildPhase(XCObject): to the corresponding PBXBuildFile children (values). """ - # TODO(mark): Some build phase types, like PBXShellScriptBuildPhase, don't - # actually have a "files" list. XCBuildPhase should not have "files" but - # another abstract subclass of it should provide this, and concrete build - # phase types that do have "files" lists should be derived from that new - # abstract subclass. XCBuildPhase should only provide buildActionMask and - # runOnlyForDeploymentPostprocessing, and not files or the various - # file-related methods and attributes. - - _schema = XCObject._schema.copy() - _schema.update({ - 'buildActionMask': [0, int, 0, 1, 0x7fffffff], - 'files': [1, PBXBuildFile, 1, 1, []], - 'runOnlyForDeploymentPostprocessing': [0, int, 0, 1, 0], - }) - - def __init__(self, properties=None, id=None, parent=None): - # super - XCObject.__init__(self, properties, id, parent) + # TODO(mark): Some build phase types, like PBXShellScriptBuildPhase, don't + # actually have a "files" list. XCBuildPhase should not have "files" but + # another abstract subclass of it should provide this, and concrete build + # phase types that do have "files" lists should be derived from that new + # abstract subclass. XCBuildPhase should only provide buildActionMask and + # runOnlyForDeploymentPostprocessing, and not files or the various + # file-related methods and attributes. + + _schema = XCObject._schema.copy() + _schema.update( + { + "buildActionMask": [0, int, 0, 1, 0x7FFFFFFF], + "files": [1, PBXBuildFile, 1, 1, []], + "runOnlyForDeploymentPostprocessing": [0, int, 0, 1, 0], + } + ) - self._files_by_path = {} - self._files_by_xcfilelikeelement = {} - for pbxbuildfile in self._properties.get('files', []): - self._AddBuildFileToDicts(pbxbuildfile) + def __init__(self, properties=None, id=None, parent=None): + # super + XCObject.__init__(self, properties, id, parent) - def FileGroup(self, path): - # Subclasses must override this by returning a two-element tuple. The - # first item in the tuple should be the PBXGroup to which "path" should be - # added, either as a child or deeper descendant. The second item should - # be a boolean indicating whether files should be added into hierarchical - # groups or one single flat group. - raise NotImplementedError( - self.__class__.__name__ + ' must implement FileGroup') + self._files_by_path = {} + self._files_by_xcfilelikeelement = {} + for pbxbuildfile in self._properties.get("files", []): + self._AddBuildFileToDicts(pbxbuildfile) - def _AddPathToDict(self, pbxbuildfile, path): - """Adds path to the dict tracking paths belonging to this build phase. + def FileGroup(self, path): + # Subclasses must override this by returning a two-element tuple. The + # first item in the tuple should be the PBXGroup to which "path" should be + # added, either as a child or deeper descendant. The second item should + # be a boolean indicating whether files should be added into hierarchical + # groups or one single flat group. + raise NotImplementedError(self.__class__.__name__ + " must implement FileGroup") + + def _AddPathToDict(self, pbxbuildfile, path): + """Adds path to the dict tracking paths belonging to this build phase. If the path is already a member of this build phase, raises an exception. """ - if path in self._files_by_path: - raise ValueError('Found multiple build files with path ' + path) - self._files_by_path[path] = pbxbuildfile + if path in self._files_by_path: + raise ValueError("Found multiple build files with path " + path) + self._files_by_path[path] = pbxbuildfile - def _AddBuildFileToDicts(self, pbxbuildfile, path=None): - """Maintains the _files_by_path and _files_by_xcfilelikeelement dicts. + def _AddBuildFileToDicts(self, pbxbuildfile, path=None): + """Maintains the _files_by_path and _files_by_xcfilelikeelement dicts. If path is specified, then it is the path that is being added to the phase, and pbxbuildfile must contain either a PBXFileReference directly @@ -1806,751 +1917,825 @@ def _AddBuildFileToDicts(self, pbxbuildfile, path=None): the PBXBuildFile if it is already present in the list of children. """ - xcfilelikeelement = pbxbuildfile._properties['fileRef'] + xcfilelikeelement = pbxbuildfile._properties["fileRef"] - paths = [] - if path != None: - # It's best when the caller provides the path. - if isinstance(xcfilelikeelement, PBXVariantGroup): - paths.append(path) - else: - # If the caller didn't provide a path, there can be either multiple - # paths (PBXVariantGroup) or one. - if isinstance(xcfilelikeelement, PBXVariantGroup): - for variant in xcfilelikeelement._properties['children']: - paths.append(variant.FullPath()) - else: - paths.append(xcfilelikeelement.FullPath()) - - # Add the paths first, because if something's going to raise, the - # messages provided by _AddPathToDict are more useful owing to its - # having access to a real pathname and not just an object's Name(). - for a_path in paths: - self._AddPathToDict(pbxbuildfile, a_path) - - # If another PBXBuildFile references this XCFileLikeElement, there's a - # problem. - if xcfilelikeelement in self._files_by_xcfilelikeelement and \ - self._files_by_xcfilelikeelement[xcfilelikeelement] != pbxbuildfile: - raise ValueError('Found multiple build files for ' + \ - xcfilelikeelement.Name()) - self._files_by_xcfilelikeelement[xcfilelikeelement] = pbxbuildfile - - def AppendBuildFile(self, pbxbuildfile, path=None): - # Callers should use this instead of calling - # AppendProperty('files', pbxbuildfile) directly because this function - # maintains the object's dicts. Better yet, callers can just call AddFile - # with a pathname and not worry about building their own PBXBuildFile - # objects. - self.AppendProperty('files', pbxbuildfile) - self._AddBuildFileToDicts(pbxbuildfile, path) - - def AddFile(self, path, settings=None): - (file_group, hierarchical) = self.FileGroup(path) - file_ref = file_group.AddOrGetFileByPath(path, hierarchical) - - if file_ref in self._files_by_xcfilelikeelement and \ - isinstance(file_ref, PBXVariantGroup): - # There's already a PBXBuildFile in this phase corresponding to the - # PBXVariantGroup. path just provides a new variant that belongs to - # the group. Add the path to the dict. - pbxbuildfile = self._files_by_xcfilelikeelement[file_ref] - self._AddBuildFileToDicts(pbxbuildfile, path) - else: - # Add a new PBXBuildFile to get file_ref into the phase. - if settings is None: - pbxbuildfile = PBXBuildFile({'fileRef': file_ref}) - else: - pbxbuildfile = PBXBuildFile({'fileRef': file_ref, 'settings': settings}) - self.AppendBuildFile(pbxbuildfile, path) + paths = [] + if path is not None: + # It's best when the caller provides the path. + if isinstance(xcfilelikeelement, PBXVariantGroup): + paths.append(path) + else: + # If the caller didn't provide a path, there can be either multiple + # paths (PBXVariantGroup) or one. + if isinstance(xcfilelikeelement, PBXVariantGroup): + for variant in xcfilelikeelement._properties["children"]: + paths.append(variant.FullPath()) + else: + paths.append(xcfilelikeelement.FullPath()) + + # Add the paths first, because if something's going to raise, the + # messages provided by _AddPathToDict are more useful owing to its + # having access to a real pathname and not just an object's Name(). + for a_path in paths: + self._AddPathToDict(pbxbuildfile, a_path) + + # If another PBXBuildFile references this XCFileLikeElement, there's a + # problem. + if ( + xcfilelikeelement in self._files_by_xcfilelikeelement + and self._files_by_xcfilelikeelement[xcfilelikeelement] != pbxbuildfile + ): + raise ValueError( + "Found multiple build files for " + xcfilelikeelement.Name() + ) + self._files_by_xcfilelikeelement[xcfilelikeelement] = pbxbuildfile + + def AppendBuildFile(self, pbxbuildfile, path=None): + # Callers should use this instead of calling + # AppendProperty('files', pbxbuildfile) directly because this function + # maintains the object's dicts. Better yet, callers can just call AddFile + # with a pathname and not worry about building their own PBXBuildFile + # objects. + self.AppendProperty("files", pbxbuildfile) + self._AddBuildFileToDicts(pbxbuildfile, path) + + def AddFile(self, path, settings=None): + (file_group, hierarchical) = self.FileGroup(path) + file_ref = file_group.AddOrGetFileByPath(path, hierarchical) + + if file_ref in self._files_by_xcfilelikeelement and isinstance( + file_ref, PBXVariantGroup + ): + # There's already a PBXBuildFile in this phase corresponding to the + # PBXVariantGroup. path just provides a new variant that belongs to + # the group. Add the path to the dict. + pbxbuildfile = self._files_by_xcfilelikeelement[file_ref] + self._AddBuildFileToDicts(pbxbuildfile, path) + else: + # Add a new PBXBuildFile to get file_ref into the phase. + if settings is None: + pbxbuildfile = PBXBuildFile({"fileRef": file_ref}) + else: + pbxbuildfile = PBXBuildFile({"fileRef": file_ref, "settings": settings}) + self.AppendBuildFile(pbxbuildfile, path) class PBXHeadersBuildPhase(XCBuildPhase): - # No additions to the schema relative to XCBuildPhase. + # No additions to the schema relative to XCBuildPhase. - def Name(self): - return 'Headers' + def Name(self): + return "Headers" - def FileGroup(self, path): - return self.PBXProjectAncestor().RootGroupForPath(path) + def FileGroup(self, path): + return self.PBXProjectAncestor().RootGroupForPath(path) class PBXResourcesBuildPhase(XCBuildPhase): - # No additions to the schema relative to XCBuildPhase. + # No additions to the schema relative to XCBuildPhase. - def Name(self): - return 'Resources' + def Name(self): + return "Resources" - def FileGroup(self, path): - return self.PBXProjectAncestor().RootGroupForPath(path) + def FileGroup(self, path): + return self.PBXProjectAncestor().RootGroupForPath(path) class PBXSourcesBuildPhase(XCBuildPhase): - # No additions to the schema relative to XCBuildPhase. + # No additions to the schema relative to XCBuildPhase. - def Name(self): - return 'Sources' + def Name(self): + return "Sources" - def FileGroup(self, path): - return self.PBXProjectAncestor().RootGroupForPath(path) + def FileGroup(self, path): + return self.PBXProjectAncestor().RootGroupForPath(path) class PBXFrameworksBuildPhase(XCBuildPhase): - # No additions to the schema relative to XCBuildPhase. - - def Name(self): - return 'Frameworks' - - def FileGroup(self, path): - (root, ext) = posixpath.splitext(path) - if ext != '': - ext = ext[1:].lower() - if ext == 'o': - # .o files are added to Xcode Frameworks phases, but conceptually aren't - # frameworks, they're more like sources or intermediates. Redirect them - # to show up in one of those other groups. - return self.PBXProjectAncestor().RootGroupForPath(path) - else: - return (self.PBXProjectAncestor().FrameworksGroup(), False) + # No additions to the schema relative to XCBuildPhase. + + def Name(self): + return "Frameworks" + + def FileGroup(self, path): + (root, ext) = posixpath.splitext(path) + if ext != "": + ext = ext[1:].lower() + if ext == "o": + # .o files are added to Xcode Frameworks phases, but conceptually aren't + # frameworks, they're more like sources or intermediates. Redirect them + # to show up in one of those other groups. + return self.PBXProjectAncestor().RootGroupForPath(path) + else: + return (self.PBXProjectAncestor().FrameworksGroup(), False) class PBXShellScriptBuildPhase(XCBuildPhase): - _schema = XCBuildPhase._schema.copy() - _schema.update({ - 'inputPaths': [1, str, 0, 1, []], - 'name': [0, str, 0, 0], - 'outputPaths': [1, str, 0, 1, []], - 'shellPath': [0, str, 0, 1, '/bin/sh'], - 'shellScript': [0, str, 0, 1], - 'showEnvVarsInLog': [0, int, 0, 0], - }) + _schema = XCBuildPhase._schema.copy() + _schema.update( + { + "inputPaths": [1, str, 0, 1, []], + "name": [0, str, 0, 0], + "outputPaths": [1, str, 0, 1, []], + "shellPath": [0, str, 0, 1, "/bin/sh"], + "shellScript": [0, str, 0, 1], + "showEnvVarsInLog": [0, int, 0, 0], + } + ) - def Name(self): - if 'name' in self._properties: - return self._properties['name'] + def Name(self): + if "name" in self._properties: + return self._properties["name"] - return 'ShellScript' + return "ShellScript" class PBXCopyFilesBuildPhase(XCBuildPhase): - _schema = XCBuildPhase._schema.copy() - _schema.update({ - 'dstPath': [0, str, 0, 1], - 'dstSubfolderSpec': [0, int, 0, 1], - 'name': [0, str, 0, 0], - }) - - # path_tree_re matches "$(DIR)/path", "$(DIR)/$(DIR2)/path" or just "$(DIR)". - # Match group 1 is "DIR", group 3 is "path" or "$(DIR2") or "$(DIR2)/path" - # or None. If group 3 is "path", group 4 will be None otherwise group 4 is - # "DIR2" and group 6 is "path". - path_tree_re = re.compile(r'^\$\((.*?)\)(/(\$\((.*?)\)(/(.*)|)|(.*)|)|)$') - - # path_tree_{first,second}_to_subfolder map names of Xcode variables to the - # associated dstSubfolderSpec property value used in a PBXCopyFilesBuildPhase - # object. - path_tree_first_to_subfolder = { - # Types that can be chosen via the Xcode UI. - 'BUILT_PRODUCTS_DIR': 16, # Products Directory - 'BUILT_FRAMEWORKS_DIR': 10, # Not an official Xcode macro. - # Existed before support for the - # names below was added. Maps to - # "Frameworks". - } - - path_tree_second_to_subfolder = { - 'WRAPPER_NAME': 1, # Wrapper - # Although Xcode's friendly name is "Executables", the destination - # is demonstrably the value of the build setting - # EXECUTABLE_FOLDER_PATH not EXECUTABLES_FOLDER_PATH. - 'EXECUTABLE_FOLDER_PATH': 6, # Executables. - 'UNLOCALIZED_RESOURCES_FOLDER_PATH': 7, # Resources - 'JAVA_FOLDER_PATH': 15, # Java Resources - 'FRAMEWORKS_FOLDER_PATH': 10, # Frameworks - 'SHARED_FRAMEWORKS_FOLDER_PATH': 11, # Shared Frameworks - 'SHARED_SUPPORT_FOLDER_PATH': 12, # Shared Support - 'PLUGINS_FOLDER_PATH': 13, # PlugIns - # For XPC Services, Xcode sets both dstPath and dstSubfolderSpec. - # Note that it re-uses the BUILT_PRODUCTS_DIR value for - # dstSubfolderSpec. dstPath is set below. - 'XPCSERVICES_FOLDER_PATH': 16, # XPC Services. - } - - def Name(self): - if 'name' in self._properties: - return self._properties['name'] - - return 'CopyFiles' - - def FileGroup(self, path): - return self.PBXProjectAncestor().RootGroupForPath(path) - - def SetDestination(self, path): - """Set the dstSubfolderSpec and dstPath properties from path. + _schema = XCBuildPhase._schema.copy() + _schema.update( + { + "dstPath": [0, str, 0, 1], + "dstSubfolderSpec": [0, int, 0, 1], + "name": [0, str, 0, 0], + } + ) + + # path_tree_re matches "$(DIR)/path", "$(DIR)/$(DIR2)/path" or just "$(DIR)". + # Match group 1 is "DIR", group 3 is "path" or "$(DIR2") or "$(DIR2)/path" + # or None. If group 3 is "path", group 4 will be None otherwise group 4 is + # "DIR2" and group 6 is "path". + path_tree_re = re.compile(r"^\$\((.*?)\)(/(\$\((.*?)\)(/(.*)|)|(.*)|)|)$") + + # path_tree_{first,second}_to_subfolder map names of Xcode variables to the + # associated dstSubfolderSpec property value used in a PBXCopyFilesBuildPhase + # object. + path_tree_first_to_subfolder = { + # Types that can be chosen via the Xcode UI. + "BUILT_PRODUCTS_DIR": 16, # Products Directory + "BUILT_FRAMEWORKS_DIR": 10, # Not an official Xcode macro. + # Existed before support for the + # names below was added. Maps to + # "Frameworks". + } + + path_tree_second_to_subfolder = { + "WRAPPER_NAME": 1, # Wrapper + # Although Xcode's friendly name is "Executables", the destination + # is demonstrably the value of the build setting + # EXECUTABLE_FOLDER_PATH not EXECUTABLES_FOLDER_PATH. + "EXECUTABLE_FOLDER_PATH": 6, # Executables. + "UNLOCALIZED_RESOURCES_FOLDER_PATH": 7, # Resources + "JAVA_FOLDER_PATH": 15, # Java Resources + "FRAMEWORKS_FOLDER_PATH": 10, # Frameworks + "SHARED_FRAMEWORKS_FOLDER_PATH": 11, # Shared Frameworks + "SHARED_SUPPORT_FOLDER_PATH": 12, # Shared Support + "PLUGINS_FOLDER_PATH": 13, # PlugIns + # For XPC Services, Xcode sets both dstPath and dstSubfolderSpec. + # Note that it re-uses the BUILT_PRODUCTS_DIR value for + # dstSubfolderSpec. dstPath is set below. + "XPCSERVICES_FOLDER_PATH": 16, # XPC Services. + } + + def Name(self): + if "name" in self._properties: + return self._properties["name"] + + return "CopyFiles" + + def FileGroup(self, path): + return self.PBXProjectAncestor().RootGroupForPath(path) + + def SetDestination(self, path): + """Set the dstSubfolderSpec and dstPath properties from path. path may be specified in the same notation used for XCHierarchicalElements, specifically, "$(DIR)/path". """ - path_tree_match = self.path_tree_re.search(path) - if path_tree_match: - path_tree = path_tree_match.group(1) - if path_tree in self.path_tree_first_to_subfolder: - subfolder = self.path_tree_first_to_subfolder[path_tree] - relative_path = path_tree_match.group(3) - if relative_path is None: - relative_path = '' - - if subfolder == 16 and path_tree_match.group(4) is not None: - # BUILT_PRODUCTS_DIR (16) is the first element in a path whose - # second element is possibly one of the variable names in - # path_tree_second_to_subfolder. Xcode sets the values of all these - # variables to relative paths so .gyp files must prefix them with - # BUILT_PRODUCTS_DIR, e.g. - # $(BUILT_PRODUCTS_DIR)/$(PLUGINS_FOLDER_PATH). Then - # xcode_emulation.py can export these variables with the same values - # as Xcode yet make & ninja files can determine the absolute path - # to the target. Xcode uses the dstSubfolderSpec value set here - # to determine the full path. - # - # An alternative of xcode_emulation.py setting the values to absolute - # paths when exporting these variables has been ruled out because - # then the values would be different depending on the build tool. - # - # Another alternative is to invent new names for the variables used - # to match to the subfolder indices in the second table. .gyp files - # then will not need to prepend $(BUILT_PRODUCTS_DIR) because - # xcode_emulation.py can set the values of those variables to - # the absolute paths when exporting. This is possibly the thinking - # behind BUILT_FRAMEWORKS_DIR which is used in exactly this manner. - # - # Requiring prepending BUILT_PRODUCTS_DIR has been chosen because - # this same way could be used to specify destinations in .gyp files - # that pre-date this addition to GYP. However they would only work - # with the Xcode generator. The previous version of xcode_emulation.py - # does not export these variables. Such files will get the benefit - # of the Xcode UI showing the proper destination name simply by - # regenerating the projects with this version of GYP. - path_tree = path_tree_match.group(4) - relative_path = path_tree_match.group(6) - separator = '/' - - if path_tree in self.path_tree_second_to_subfolder: - subfolder = self.path_tree_second_to_subfolder[path_tree] - if relative_path is None: - relative_path = '' - separator = '' - if path_tree == 'XPCSERVICES_FOLDER_PATH': - relative_path = '$(CONTENTS_FOLDER_PATH)/XPCServices' \ - + separator + relative_path - else: - # subfolder = 16 from above - # The second element of the path is an unrecognized variable. - # Include it and any remaining elements in relative_path. - relative_path = path_tree_match.group(3) - - else: - # The path starts with an unrecognized Xcode variable - # name like $(SRCROOT). Xcode will still handle this - # as an "absolute path" that starts with the variable. - subfolder = 0 - relative_path = path - elif path.startswith('/'): - # Special case. Absolute paths are in dstSubfolderSpec 0. - subfolder = 0 - relative_path = path[1:] - else: - raise ValueError('Can\'t use path %s in a %s' % \ - (path, self.__class__.__name__)) + path_tree_match = self.path_tree_re.search(path) + if path_tree_match: + path_tree = path_tree_match.group(1) + if path_tree in self.path_tree_first_to_subfolder: + subfolder = self.path_tree_first_to_subfolder[path_tree] + relative_path = path_tree_match.group(3) + if relative_path is None: + relative_path = "" + + if subfolder == 16 and path_tree_match.group(4) is not None: + # BUILT_PRODUCTS_DIR (16) is the first element in a path whose + # second element is possibly one of the variable names in + # path_tree_second_to_subfolder. Xcode sets the values of all these + # variables to relative paths so .gyp files must prefix them with + # BUILT_PRODUCTS_DIR, e.g. + # $(BUILT_PRODUCTS_DIR)/$(PLUGINS_FOLDER_PATH). Then + # xcode_emulation.py can export these variables with the same values + # as Xcode yet make & ninja files can determine the absolute path + # to the target. Xcode uses the dstSubfolderSpec value set here + # to determine the full path. + # + # An alternative of xcode_emulation.py setting the values to + # absolute paths when exporting these variables has been + # ruled out because then the values would be different + # depending on the build tool. + # + # Another alternative is to invent new names for the variables used + # to match to the subfolder indices in the second table. .gyp files + # then will not need to prepend $(BUILT_PRODUCTS_DIR) because + # xcode_emulation.py can set the values of those variables to + # the absolute paths when exporting. This is possibly the thinking + # behind BUILT_FRAMEWORKS_DIR which is used in exactly this manner. + # + # Requiring prepending BUILT_PRODUCTS_DIR has been chosen because + # this same way could be used to specify destinations in .gyp files + # that pre-date this addition to GYP. However they would only work + # with the Xcode generator. + # The previous version of xcode_emulation.py + # does not export these variables. Such files will get the benefit + # of the Xcode UI showing the proper destination name simply by + # regenerating the projects with this version of GYP. + path_tree = path_tree_match.group(4) + relative_path = path_tree_match.group(6) + separator = "/" + + if path_tree in self.path_tree_second_to_subfolder: + subfolder = self.path_tree_second_to_subfolder[path_tree] + if relative_path is None: + relative_path = "" + separator = "" + if path_tree == "XPCSERVICES_FOLDER_PATH": + relative_path = ( + "$(CONTENTS_FOLDER_PATH)/XPCServices" + + separator + + relative_path + ) + else: + # subfolder = 16 from above + # The second element of the path is an unrecognized variable. + # Include it and any remaining elements in relative_path. + relative_path = path_tree_match.group(3) + + else: + # The path starts with an unrecognized Xcode variable + # name like $(SRCROOT). Xcode will still handle this + # as an "absolute path" that starts with the variable. + subfolder = 0 + relative_path = path + elif path.startswith("/"): + # Special case. Absolute paths are in dstSubfolderSpec 0. + subfolder = 0 + relative_path = path[1:] + else: + raise ValueError( + "Can't use path %s in a %s" % (path, self.__class__.__name__) + ) - self._properties['dstPath'] = relative_path - self._properties['dstSubfolderSpec'] = subfolder + self._properties["dstPath"] = relative_path + self._properties["dstSubfolderSpec"] = subfolder class PBXBuildRule(XCObject): - _schema = XCObject._schema.copy() - _schema.update({ - 'compilerSpec': [0, str, 0, 1], - 'filePatterns': [0, str, 0, 0], - 'fileType': [0, str, 0, 1], - 'isEditable': [0, int, 0, 1, 1], - 'outputFiles': [1, str, 0, 1, []], - 'script': [0, str, 0, 0], - }) - - def Name(self): - # Not very inspired, but it's what Xcode uses. - return self.__class__.__name__ - - def Hashables(self): - # super - hashables = XCObject.Hashables(self) + _schema = XCObject._schema.copy() + _schema.update( + { + "compilerSpec": [0, str, 0, 1], + "filePatterns": [0, str, 0, 0], + "fileType": [0, str, 0, 1], + "isEditable": [0, int, 0, 1, 1], + "outputFiles": [1, str, 0, 1, []], + "script": [0, str, 0, 0], + } + ) - # Use the hashables of the weak objects that this object refers to. - hashables.append(self._properties['fileType']) - if 'filePatterns' in self._properties: - hashables.append(self._properties['filePatterns']) - return hashables + def Name(self): + # Not very inspired, but it's what Xcode uses. + return self.__class__.__name__ + + def Hashables(self): + # super + hashables = XCObject.Hashables(self) + + # Use the hashables of the weak objects that this object refers to. + hashables.append(self._properties["fileType"]) + if "filePatterns" in self._properties: + hashables.append(self._properties["filePatterns"]) + return hashables class PBXContainerItemProxy(XCObject): - # When referencing an item in this project file, containerPortal is the - # PBXProject root object of this project file. When referencing an item in - # another project file, containerPortal is a PBXFileReference identifying - # the other project file. - # - # When serving as a proxy to an XCTarget (in this project file or another), - # proxyType is 1. When serving as a proxy to a PBXFileReference (in another - # project file), proxyType is 2. Type 2 is used for references to the - # producs of the other project file's targets. - # - # Xcode is weird about remoteGlobalIDString. Usually, it's printed without - # a comment, indicating that it's tracked internally simply as a string, but - # sometimes it's printed with a comment (usually when the object is initially - # created), indicating that it's tracked as a project file object at least - # sometimes. This module always tracks it as an object, but contains a hack - # to prevent it from printing the comment in the project file output. See - # _XCKVPrint. - _schema = XCObject._schema.copy() - _schema.update({ - 'containerPortal': [0, XCContainerPortal, 0, 1], - 'proxyType': [0, int, 0, 1], - 'remoteGlobalIDString': [0, XCRemoteObject, 0, 1], - 'remoteInfo': [0, str, 0, 1], - }) - - def __repr__(self): - props = self._properties - name = '%s.gyp:%s' % (props['containerPortal'].Name(), props['remoteInfo']) - return '<%s %r at 0x%x>' % (self.__class__.__name__, name, id(self)) - - def Name(self): - # Admittedly not the best name, but it's what Xcode uses. - return self.__class__.__name__ - - def Hashables(self): - # super - hashables = XCObject.Hashables(self) + # When referencing an item in this project file, containerPortal is the + # PBXProject root object of this project file. When referencing an item in + # another project file, containerPortal is a PBXFileReference identifying + # the other project file. + # + # When serving as a proxy to an XCTarget (in this project file or another), + # proxyType is 1. When serving as a proxy to a PBXFileReference (in another + # project file), proxyType is 2. Type 2 is used for references to the + # producs of the other project file's targets. + # + # Xcode is weird about remoteGlobalIDString. Usually, it's printed without + # a comment, indicating that it's tracked internally simply as a string, but + # sometimes it's printed with a comment (usually when the object is initially + # created), indicating that it's tracked as a project file object at least + # sometimes. This module always tracks it as an object, but contains a hack + # to prevent it from printing the comment in the project file output. See + # _XCKVPrint. + _schema = XCObject._schema.copy() + _schema.update( + { + "containerPortal": [0, XCContainerPortal, 0, 1], + "proxyType": [0, int, 0, 1], + "remoteGlobalIDString": [0, XCRemoteObject, 0, 1], + "remoteInfo": [0, str, 0, 1], + } + ) + + def __repr__(self): + props = self._properties + name = "%s.gyp:%s" % (props["containerPortal"].Name(), props["remoteInfo"]) + return "<%s %r at 0x%x>" % (self.__class__.__name__, name, id(self)) + + def Name(self): + # Admittedly not the best name, but it's what Xcode uses. + return self.__class__.__name__ + + def Hashables(self): + # super + hashables = XCObject.Hashables(self) - # Use the hashables of the weak objects that this object refers to. - hashables.extend(self._properties['containerPortal'].Hashables()) - hashables.extend(self._properties['remoteGlobalIDString'].Hashables()) - return hashables + # Use the hashables of the weak objects that this object refers to. + hashables.extend(self._properties["containerPortal"].Hashables()) + hashables.extend(self._properties["remoteGlobalIDString"].Hashables()) + return hashables class PBXTargetDependency(XCObject): - # The "target" property accepts an XCTarget object, and obviously not - # NoneType. But XCTarget is defined below, so it can't be put into the - # schema yet. The definition of PBXTargetDependency can't be moved below - # XCTarget because XCTarget's own schema references PBXTargetDependency. - # Python doesn't deal well with this circular relationship, and doesn't have - # a real way to do forward declarations. To work around, the type of - # the "target" property is reset below, after XCTarget is defined. - # - # At least one of "name" and "target" is required. - _schema = XCObject._schema.copy() - _schema.update({ - 'name': [0, str, 0, 0], - 'target': [0, None.__class__, 0, 0], - 'targetProxy': [0, PBXContainerItemProxy, 1, 1], - }) - - def __repr__(self): - name = self._properties.get('name') or self._properties['target'].Name() - return '<%s %r at 0x%x>' % (self.__class__.__name__, name, id(self)) - - def Name(self): - # Admittedly not the best name, but it's what Xcode uses. - return self.__class__.__name__ - - def Hashables(self): - # super - hashables = XCObject.Hashables(self) + # The "target" property accepts an XCTarget object, and obviously not + # NoneType. But XCTarget is defined below, so it can't be put into the + # schema yet. The definition of PBXTargetDependency can't be moved below + # XCTarget because XCTarget's own schema references PBXTargetDependency. + # Python doesn't deal well with this circular relationship, and doesn't have + # a real way to do forward declarations. To work around, the type of + # the "target" property is reset below, after XCTarget is defined. + # + # At least one of "name" and "target" is required. + _schema = XCObject._schema.copy() + _schema.update( + { + "name": [0, str, 0, 0], + "target": [0, None.__class__, 0, 0], + "targetProxy": [0, PBXContainerItemProxy, 1, 1], + } + ) + + def __repr__(self): + name = self._properties.get("name") or self._properties["target"].Name() + return "<%s %r at 0x%x>" % (self.__class__.__name__, name, id(self)) + + def Name(self): + # Admittedly not the best name, but it's what Xcode uses. + return self.__class__.__name__ - # Use the hashables of the weak objects that this object refers to. - hashables.extend(self._properties['targetProxy'].Hashables()) - return hashables + def Hashables(self): + # super + hashables = XCObject.Hashables(self) + + # Use the hashables of the weak objects that this object refers to. + hashables.extend(self._properties["targetProxy"].Hashables()) + return hashables class PBXReferenceProxy(XCFileLikeElement): - _schema = XCFileLikeElement._schema.copy() - _schema.update({ - 'fileType': [0, str, 0, 1], - 'path': [0, str, 0, 1], - 'remoteRef': [0, PBXContainerItemProxy, 1, 1], - }) + _schema = XCFileLikeElement._schema.copy() + _schema.update( + { + "fileType": [0, str, 0, 1], + "path": [0, str, 0, 1], + "remoteRef": [0, PBXContainerItemProxy, 1, 1], + } + ) class XCTarget(XCRemoteObject): - # An XCTarget is really just an XCObject, the XCRemoteObject thing is just - # to allow PBXProject to be used in the remoteGlobalIDString property of - # PBXContainerItemProxy. - # - # Setting a "name" property at instantiation may also affect "productName", - # which may in turn affect the "PRODUCT_NAME" build setting in children of - # "buildConfigurationList". See __init__ below. - _schema = XCRemoteObject._schema.copy() - _schema.update({ - 'buildConfigurationList': [0, XCConfigurationList, 1, 1, - XCConfigurationList()], - 'buildPhases': [1, XCBuildPhase, 1, 1, []], - 'dependencies': [1, PBXTargetDependency, 1, 1, []], - 'name': [0, str, 0, 1], - 'productName': [0, str, 0, 1], - }) - - def __init__(self, properties=None, id=None, parent=None, - force_outdir=None, force_prefix=None, force_extension=None): - # super - XCRemoteObject.__init__(self, properties, id, parent) - - # Set up additional defaults not expressed in the schema. If a "name" - # property was supplied, set "productName" if it is not present. Also set - # the "PRODUCT_NAME" build setting in each configuration, but only if - # the setting is not present in any build configuration. - if 'name' in self._properties: - if not 'productName' in self._properties: - self.SetProperty('productName', self._properties['name']) - - if 'productName' in self._properties: - if 'buildConfigurationList' in self._properties: - configs = self._properties['buildConfigurationList'] - if configs.HasBuildSetting('PRODUCT_NAME') == 0: - configs.SetBuildSetting('PRODUCT_NAME', - self._properties['productName']) - - def AddDependency(self, other): - pbxproject = self.PBXProjectAncestor() - other_pbxproject = other.PBXProjectAncestor() - if pbxproject == other_pbxproject: - # Add a dependency to another target in the same project file. - container = PBXContainerItemProxy({'containerPortal': pbxproject, - 'proxyType': 1, - 'remoteGlobalIDString': other, - 'remoteInfo': other.Name()}) - dependency = PBXTargetDependency({'target': other, - 'targetProxy': container}) - self.AppendProperty('dependencies', dependency) - else: - # Add a dependency to a target in a different project file. - other_project_ref = \ - pbxproject.AddOrGetProjectReference(other_pbxproject)[1] - container = PBXContainerItemProxy({ - 'containerPortal': other_project_ref, - 'proxyType': 1, - 'remoteGlobalIDString': other, - 'remoteInfo': other.Name(), - }) - dependency = PBXTargetDependency({'name': other.Name(), - 'targetProxy': container}) - self.AppendProperty('dependencies', dependency) + # An XCTarget is really just an XCObject, the XCRemoteObject thing is just + # to allow PBXProject to be used in the remoteGlobalIDString property of + # PBXContainerItemProxy. + # + # Setting a "name" property at instantiation may also affect "productName", + # which may in turn affect the "PRODUCT_NAME" build setting in children of + # "buildConfigurationList". See __init__ below. + _schema = XCRemoteObject._schema.copy() + _schema.update( + { + "buildConfigurationList": [ + 0, + XCConfigurationList, + 1, + 1, + XCConfigurationList(), + ], + "buildPhases": [1, XCBuildPhase, 1, 1, []], + "dependencies": [1, PBXTargetDependency, 1, 1, []], + "name": [0, str, 0, 1], + "productName": [0, str, 0, 1], + } + ) + + def __init__( + self, + properties=None, + id=None, + parent=None, + force_outdir=None, + force_prefix=None, + force_extension=None, + ): + # super + XCRemoteObject.__init__(self, properties, id, parent) + + # Set up additional defaults not expressed in the schema. If a "name" + # property was supplied, set "productName" if it is not present. Also set + # the "PRODUCT_NAME" build setting in each configuration, but only if + # the setting is not present in any build configuration. + if "name" in self._properties: + if "productName" not in self._properties: + self.SetProperty("productName", self._properties["name"]) + + if "productName" in self._properties: + if "buildConfigurationList" in self._properties: + configs = self._properties["buildConfigurationList"] + if configs.HasBuildSetting("PRODUCT_NAME") == 0: + configs.SetBuildSetting( + "PRODUCT_NAME", self._properties["productName"] + ) + + def AddDependency(self, other): + pbxproject = self.PBXProjectAncestor() + other_pbxproject = other.PBXProjectAncestor() + if pbxproject == other_pbxproject: + # Add a dependency to another target in the same project file. + container = PBXContainerItemProxy( + { + "containerPortal": pbxproject, + "proxyType": 1, + "remoteGlobalIDString": other, + "remoteInfo": other.Name(), + } + ) + dependency = PBXTargetDependency( + {"target": other, "targetProxy": container} + ) + self.AppendProperty("dependencies", dependency) + else: + # Add a dependency to a target in a different project file. + other_project_ref = pbxproject.AddOrGetProjectReference(other_pbxproject)[1] + container = PBXContainerItemProxy( + { + "containerPortal": other_project_ref, + "proxyType": 1, + "remoteGlobalIDString": other, + "remoteInfo": other.Name(), + } + ) + dependency = PBXTargetDependency( + {"name": other.Name(), "targetProxy": container} + ) + self.AppendProperty("dependencies", dependency) - # Proxy all of these through to the build configuration list. + # Proxy all of these through to the build configuration list. - def ConfigurationNamed(self, name): - return self._properties['buildConfigurationList'].ConfigurationNamed(name) + def ConfigurationNamed(self, name): + return self._properties["buildConfigurationList"].ConfigurationNamed(name) - def DefaultConfiguration(self): - return self._properties['buildConfigurationList'].DefaultConfiguration() + def DefaultConfiguration(self): + return self._properties["buildConfigurationList"].DefaultConfiguration() - def HasBuildSetting(self, key): - return self._properties['buildConfigurationList'].HasBuildSetting(key) + def HasBuildSetting(self, key): + return self._properties["buildConfigurationList"].HasBuildSetting(key) - def GetBuildSetting(self, key): - return self._properties['buildConfigurationList'].GetBuildSetting(key) + def GetBuildSetting(self, key): + return self._properties["buildConfigurationList"].GetBuildSetting(key) - def SetBuildSetting(self, key, value): - return self._properties['buildConfigurationList'].SetBuildSetting(key, \ - value) + def SetBuildSetting(self, key, value): + return self._properties["buildConfigurationList"].SetBuildSetting(key, value) - def AppendBuildSetting(self, key, value): - return self._properties['buildConfigurationList'].AppendBuildSetting(key, \ - value) + def AppendBuildSetting(self, key, value): + return self._properties["buildConfigurationList"].AppendBuildSetting(key, value) - def DelBuildSetting(self, key): - return self._properties['buildConfigurationList'].DelBuildSetting(key) + def DelBuildSetting(self, key): + return self._properties["buildConfigurationList"].DelBuildSetting(key) # Redefine the type of the "target" property. See PBXTargetDependency._schema # above. -PBXTargetDependency._schema['target'][1] = XCTarget +PBXTargetDependency._schema["target"][1] = XCTarget class PBXNativeTarget(XCTarget): - # buildPhases is overridden in the schema to be able to set defaults. - # - # NOTE: Contrary to most objects, it is advisable to set parent when - # constructing PBXNativeTarget. A parent of an XCTarget must be a PBXProject - # object. A parent reference is required for a PBXNativeTarget during - # construction to be able to set up the target defaults for productReference, - # because a PBXBuildFile object must be created for the target and it must - # be added to the PBXProject's mainGroup hierarchy. - _schema = XCTarget._schema.copy() - _schema.update({ - 'buildPhases': [1, XCBuildPhase, 1, 1, - [PBXSourcesBuildPhase(), PBXFrameworksBuildPhase()]], - 'buildRules': [1, PBXBuildRule, 1, 1, []], - 'productReference': [0, PBXFileReference, 0, 1], - 'productType': [0, str, 0, 1], - }) - - # Mapping from Xcode product-types to settings. The settings are: - # filetype : used for explicitFileType in the project file - # prefix : the prefix for the file name - # suffix : the suffix for the file name - _product_filetypes = { - 'com.apple.product-type.application': ['wrapper.application', - '', '.app'], - 'com.apple.product-type.application.watchapp': ['wrapper.application', - '', '.app'], - 'com.apple.product-type.watchkit-extension': ['wrapper.app-extension', - '', '.appex'], - 'com.apple.product-type.app-extension': ['wrapper.app-extension', - '', '.appex'], - 'com.apple.product-type.bundle': ['wrapper.cfbundle', - '', '.bundle'], - 'com.apple.product-type.framework': ['wrapper.framework', - '', '.framework'], - 'com.apple.product-type.library.dynamic': ['compiled.mach-o.dylib', - 'lib', '.dylib'], - 'com.apple.product-type.library.static': ['archive.ar', - 'lib', '.a'], - 'com.apple.product-type.tool': ['compiled.mach-o.executable', - '', ''], - 'com.apple.product-type.bundle.unit-test': ['wrapper.cfbundle', - '', '.xctest'], - 'com.apple.product-type.bundle.ui-testing': ['wrapper.cfbundle', - '', '.xctest'], - 'com.googlecode.gyp.xcode.bundle': ['compiled.mach-o.dylib', - '', '.so'], - 'com.apple.product-type.kernel-extension': ['wrapper.kext', - '', '.kext'], - } - - def __init__(self, properties=None, id=None, parent=None, - force_outdir=None, force_prefix=None, force_extension=None): - # super - XCTarget.__init__(self, properties, id, parent) - - if 'productName' in self._properties and \ - 'productType' in self._properties and \ - not 'productReference' in self._properties and \ - self._properties['productType'] in self._product_filetypes: - products_group = None - pbxproject = self.PBXProjectAncestor() - if pbxproject != None: - products_group = pbxproject.ProductsGroup() - - if products_group != None: - (filetype, prefix, suffix) = \ - self._product_filetypes[self._properties['productType']] - # Xcode does not have a distinct type for loadable modules that are - # pure BSD targets (not in a bundle wrapper). GYP allows such modules - # to be specified by setting a target type to loadable_module without - # having mac_bundle set. These are mapped to the pseudo-product type - # com.googlecode.gyp.xcode.bundle. - # - # By picking up this special type and converting it to a dynamic - # library (com.apple.product-type.library.dynamic) with fix-ups, - # single-file loadable modules can be produced. - # - # MACH_O_TYPE is changed to mh_bundle to produce the proper file type - # (as opposed to mh_dylib). In order for linking to succeed, - # DYLIB_CURRENT_VERSION and DYLIB_COMPATIBILITY_VERSION must be - # cleared. They are meaningless for type mh_bundle. - # - # Finally, the .so extension is forcibly applied over the default - # (.dylib), unless another forced extension is already selected. - # .dylib is plainly wrong, and .bundle is used by loadable_modules in - # bundle wrappers (com.apple.product-type.bundle). .so seems an odd - # choice because it's used as the extension on many other systems that - # don't distinguish between linkable shared libraries and non-linkable - # loadable modules, but there's precedent: Python loadable modules on - # Mac OS X use an .so extension. - if self._properties['productType'] == 'com.googlecode.gyp.xcode.bundle': - self._properties['productType'] = \ - 'com.apple.product-type.library.dynamic' - self.SetBuildSetting('MACH_O_TYPE', 'mh_bundle') - self.SetBuildSetting('DYLIB_CURRENT_VERSION', '') - self.SetBuildSetting('DYLIB_COMPATIBILITY_VERSION', '') - if force_extension is None: - force_extension = suffix[1:] - - if self._properties['productType'] == \ - 'com.apple.product-type-bundle.unit.test' or \ - self._properties['productType'] == \ - 'com.apple.product-type-bundle.ui-testing': - if force_extension is None: - force_extension = suffix[1:] - - if force_extension is not None: - # If it's a wrapper (bundle), set WRAPPER_EXTENSION. - # Extension override. - suffix = '.' + force_extension - if filetype.startswith('wrapper.'): - self.SetBuildSetting('WRAPPER_EXTENSION', force_extension) - else: - self.SetBuildSetting('EXECUTABLE_EXTENSION', force_extension) - - if filetype.startswith('compiled.mach-o.executable'): - product_name = self._properties['productName'] - product_name += suffix - suffix = '' - self.SetProperty('productName', product_name) - self.SetBuildSetting('PRODUCT_NAME', product_name) - - # Xcode handles most prefixes based on the target type, however there - # are exceptions. If a "BSD Dynamic Library" target is added in the - # Xcode UI, Xcode sets EXECUTABLE_PREFIX. This check duplicates that - # behavior. - if force_prefix is not None: - prefix = force_prefix - if filetype.startswith('wrapper.'): - self.SetBuildSetting('WRAPPER_PREFIX', prefix) - else: - self.SetBuildSetting('EXECUTABLE_PREFIX', prefix) - - if force_outdir is not None: - self.SetBuildSetting('TARGET_BUILD_DIR', force_outdir) - - # TODO(tvl): Remove the below hack. - # http://code.google.com/p/gyp/issues/detail?id=122 - - # Some targets include the prefix in the target_name. These targets - # really should just add a product_name setting that doesn't include - # the prefix. For example: - # target_name = 'libevent', product_name = 'event' - # This check cleans up for them. - product_name = self._properties['productName'] - prefix_len = len(prefix) - if prefix_len and (product_name[:prefix_len] == prefix): - product_name = product_name[prefix_len:] - self.SetProperty('productName', product_name) - self.SetBuildSetting('PRODUCT_NAME', product_name) - - ref_props = { - 'explicitFileType': filetype, - 'includeInIndex': 0, - 'path': prefix + product_name + suffix, - 'sourceTree': 'BUILT_PRODUCTS_DIR', + # buildPhases is overridden in the schema to be able to set defaults. + # + # NOTE: Contrary to most objects, it is advisable to set parent when + # constructing PBXNativeTarget. A parent of an XCTarget must be a PBXProject + # object. A parent reference is required for a PBXNativeTarget during + # construction to be able to set up the target defaults for productReference, + # because a PBXBuildFile object must be created for the target and it must + # be added to the PBXProject's mainGroup hierarchy. + _schema = XCTarget._schema.copy() + _schema.update( + { + "buildPhases": [ + 1, + XCBuildPhase, + 1, + 1, + [PBXSourcesBuildPhase(), PBXFrameworksBuildPhase()], + ], + "buildRules": [1, PBXBuildRule, 1, 1, []], + "productReference": [0, PBXFileReference, 0, 1], + "productType": [0, str, 0, 1], } - file_ref = PBXFileReference(ref_props) - products_group.AppendChild(file_ref) - self.SetProperty('productReference', file_ref) - - def GetBuildPhaseByType(self, type): - if not 'buildPhases' in self._properties: - return None - - the_phase = None - for phase in self._properties['buildPhases']: - if isinstance(phase, type): - # Some phases may be present in multiples in a well-formed project file, - # but phases like PBXSourcesBuildPhase may only be present singly, and - # this function is intended as an aid to GetBuildPhaseByType. Loop - # over the entire list of phases and assert if more than one of the - # desired type is found. - assert the_phase is None - the_phase = phase - - return the_phase - - def HeadersPhase(self): - headers_phase = self.GetBuildPhaseByType(PBXHeadersBuildPhase) - if headers_phase is None: - headers_phase = PBXHeadersBuildPhase() - - # The headers phase should come before the resources, sources, and - # frameworks phases, if any. - insert_at = len(self._properties['buildPhases']) - for index in range(0, len(self._properties['buildPhases'])): - phase = self._properties['buildPhases'][index] - if isinstance(phase, PBXResourcesBuildPhase) or \ - isinstance(phase, PBXSourcesBuildPhase) or \ - isinstance(phase, PBXFrameworksBuildPhase): - insert_at = index - break - - self._properties['buildPhases'].insert(insert_at, headers_phase) - headers_phase.parent = self - - return headers_phase - - def ResourcesPhase(self): - resources_phase = self.GetBuildPhaseByType(PBXResourcesBuildPhase) - if resources_phase is None: - resources_phase = PBXResourcesBuildPhase() - - # The resources phase should come before the sources and frameworks - # phases, if any. - insert_at = len(self._properties['buildPhases']) - for index in range(0, len(self._properties['buildPhases'])): - phase = self._properties['buildPhases'][index] - if isinstance(phase, PBXSourcesBuildPhase) or \ - isinstance(phase, PBXFrameworksBuildPhase): - insert_at = index - break - - self._properties['buildPhases'].insert(insert_at, resources_phase) - resources_phase.parent = self - - return resources_phase - - def SourcesPhase(self): - sources_phase = self.GetBuildPhaseByType(PBXSourcesBuildPhase) - if sources_phase is None: - sources_phase = PBXSourcesBuildPhase() - self.AppendProperty('buildPhases', sources_phase) - - return sources_phase - - def FrameworksPhase(self): - frameworks_phase = self.GetBuildPhaseByType(PBXFrameworksBuildPhase) - if frameworks_phase is None: - frameworks_phase = PBXFrameworksBuildPhase() - self.AppendProperty('buildPhases', frameworks_phase) - - return frameworks_phase - - def AddDependency(self, other): - # super - XCTarget.AddDependency(self, other) - - static_library_type = 'com.apple.product-type.library.static' - shared_library_type = 'com.apple.product-type.library.dynamic' - framework_type = 'com.apple.product-type.framework' - if isinstance(other, PBXNativeTarget) and \ - 'productType' in self._properties and \ - self._properties['productType'] != static_library_type and \ - 'productType' in other._properties and \ - (other._properties['productType'] == static_library_type or \ - ((other._properties['productType'] == shared_library_type or \ - other._properties['productType'] == framework_type) and \ - ((not other.HasBuildSetting('MACH_O_TYPE')) or - other.GetBuildSetting('MACH_O_TYPE') != 'mh_bundle'))): - - file_ref = other.GetProperty('productReference') - - pbxproject = self.PBXProjectAncestor() - other_pbxproject = other.PBXProjectAncestor() - if pbxproject != other_pbxproject: - other_project_product_group = \ - pbxproject.AddOrGetProjectReference(other_pbxproject)[0] - file_ref = other_project_product_group.GetChildByRemoteObject(file_ref) - - self.FrameworksPhase().AppendProperty('files', - PBXBuildFile({'fileRef': file_ref})) + ) + + # Mapping from Xcode product-types to settings. The settings are: + # filetype : used for explicitFileType in the project file + # prefix : the prefix for the file name + # suffix : the suffix for the file name + _product_filetypes = { + "com.apple.product-type.application": ["wrapper.application", "", ".app"], + "com.apple.product-type.application.watchapp": [ + "wrapper.application", + "", + ".app", + ], + "com.apple.product-type.watchkit-extension": [ + "wrapper.app-extension", + "", + ".appex", + ], + "com.apple.product-type.app-extension": ["wrapper.app-extension", "", ".appex"], + "com.apple.product-type.bundle": ["wrapper.cfbundle", "", ".bundle"], + "com.apple.product-type.framework": ["wrapper.framework", "", ".framework"], + "com.apple.product-type.library.dynamic": [ + "compiled.mach-o.dylib", + "lib", + ".dylib", + ], + "com.apple.product-type.library.static": ["archive.ar", "lib", ".a"], + "com.apple.product-type.tool": ["compiled.mach-o.executable", "", ""], + "com.apple.product-type.bundle.unit-test": ["wrapper.cfbundle", "", ".xctest"], + "com.apple.product-type.bundle.ui-testing": ["wrapper.cfbundle", "", ".xctest"], + "com.googlecode.gyp.xcode.bundle": ["compiled.mach-o.dylib", "", ".so"], + "com.apple.product-type.kernel-extension": ["wrapper.kext", "", ".kext"], + } + + def __init__( + self, + properties=None, + id=None, + parent=None, + force_outdir=None, + force_prefix=None, + force_extension=None, + ): + # super + XCTarget.__init__(self, properties, id, parent) + + if ( + "productName" in self._properties + and "productType" in self._properties + and "productReference" not in self._properties + and self._properties["productType"] in self._product_filetypes + ): + products_group = None + pbxproject = self.PBXProjectAncestor() + if pbxproject is not None: + products_group = pbxproject.ProductsGroup() + + if products_group is not None: + (filetype, prefix, suffix) = self._product_filetypes[ + self._properties["productType"] + ] + # Xcode does not have a distinct type for loadable modules that are + # pure BSD targets (not in a bundle wrapper). GYP allows such modules + # to be specified by setting a target type to loadable_module without + # having mac_bundle set. These are mapped to the pseudo-product type + # com.googlecode.gyp.xcode.bundle. + # + # By picking up this special type and converting it to a dynamic + # library (com.apple.product-type.library.dynamic) with fix-ups, + # single-file loadable modules can be produced. + # + # MACH_O_TYPE is changed to mh_bundle to produce the proper file type + # (as opposed to mh_dylib). In order for linking to succeed, + # DYLIB_CURRENT_VERSION and DYLIB_COMPATIBILITY_VERSION must be + # cleared. They are meaningless for type mh_bundle. + # + # Finally, the .so extension is forcibly applied over the default + # (.dylib), unless another forced extension is already selected. + # .dylib is plainly wrong, and .bundle is used by loadable_modules in + # bundle wrappers (com.apple.product-type.bundle). .so seems an odd + # choice because it's used as the extension on many other systems that + # don't distinguish between linkable shared libraries and non-linkable + # loadable modules, but there's precedent: Python loadable modules on + # Mac OS X use an .so extension. + if self._properties["productType"] == "com.googlecode.gyp.xcode.bundle": + self._properties[ + "productType" + ] = "com.apple.product-type.library.dynamic" + self.SetBuildSetting("MACH_O_TYPE", "mh_bundle") + self.SetBuildSetting("DYLIB_CURRENT_VERSION", "") + self.SetBuildSetting("DYLIB_COMPATIBILITY_VERSION", "") + if force_extension is None: + force_extension = suffix[1:] + + if ( + self._properties["productType"] + == "com.apple.product-type-bundle.unit.test" + or self._properties["productType"] + == "com.apple.product-type-bundle.ui-testing" + ): + if force_extension is None: + force_extension = suffix[1:] + + if force_extension is not None: + # If it's a wrapper (bundle), set WRAPPER_EXTENSION. + # Extension override. + suffix = "." + force_extension + if filetype.startswith("wrapper."): + self.SetBuildSetting("WRAPPER_EXTENSION", force_extension) + else: + self.SetBuildSetting("EXECUTABLE_EXTENSION", force_extension) + + if filetype.startswith("compiled.mach-o.executable"): + product_name = self._properties["productName"] + product_name += suffix + suffix = "" + self.SetProperty("productName", product_name) + self.SetBuildSetting("PRODUCT_NAME", product_name) + + # Xcode handles most prefixes based on the target type, however there + # are exceptions. If a "BSD Dynamic Library" target is added in the + # Xcode UI, Xcode sets EXECUTABLE_PREFIX. This check duplicates that + # behavior. + if force_prefix is not None: + prefix = force_prefix + if filetype.startswith("wrapper."): + self.SetBuildSetting("WRAPPER_PREFIX", prefix) + else: + self.SetBuildSetting("EXECUTABLE_PREFIX", prefix) + + if force_outdir is not None: + self.SetBuildSetting("TARGET_BUILD_DIR", force_outdir) + + # TODO(tvl): Remove the below hack. + # http://code.google.com/p/gyp/issues/detail?id=122 + + # Some targets include the prefix in the target_name. These targets + # really should just add a product_name setting that doesn't include + # the prefix. For example: + # target_name = 'libevent', product_name = 'event' + # This check cleans up for them. + product_name = self._properties["productName"] + prefix_len = len(prefix) + if prefix_len and (product_name[:prefix_len] == prefix): + product_name = product_name[prefix_len:] + self.SetProperty("productName", product_name) + self.SetBuildSetting("PRODUCT_NAME", product_name) + + ref_props = { + "explicitFileType": filetype, + "includeInIndex": 0, + "path": prefix + product_name + suffix, + "sourceTree": "BUILT_PRODUCTS_DIR", + } + file_ref = PBXFileReference(ref_props) + products_group.AppendChild(file_ref) + self.SetProperty("productReference", file_ref) + + def GetBuildPhaseByType(self, type): + if "buildPhases" not in self._properties: + return None + + the_phase = None + for phase in self._properties["buildPhases"]: + if isinstance(phase, type): + # Some phases may be present in multiples in a well-formed project file, + # but phases like PBXSourcesBuildPhase may only be present singly, and + # this function is intended as an aid to GetBuildPhaseByType. Loop + # over the entire list of phases and assert if more than one of the + # desired type is found. + assert the_phase is None + the_phase = phase + + return the_phase + + def HeadersPhase(self): + headers_phase = self.GetBuildPhaseByType(PBXHeadersBuildPhase) + if headers_phase is None: + headers_phase = PBXHeadersBuildPhase() + + # The headers phase should come before the resources, sources, and + # frameworks phases, if any. + insert_at = len(self._properties["buildPhases"]) + for index, phase in enumerate(self._properties["buildPhases"]): + if ( + isinstance(phase, PBXResourcesBuildPhase) + or isinstance(phase, PBXSourcesBuildPhase) + or isinstance(phase, PBXFrameworksBuildPhase) + ): + insert_at = index + break + + self._properties["buildPhases"].insert(insert_at, headers_phase) + headers_phase.parent = self + + return headers_phase + + def ResourcesPhase(self): + resources_phase = self.GetBuildPhaseByType(PBXResourcesBuildPhase) + if resources_phase is None: + resources_phase = PBXResourcesBuildPhase() + + # The resources phase should come before the sources and frameworks + # phases, if any. + insert_at = len(self._properties["buildPhases"]) + for index, phase in enumerate(self._properties["buildPhases"]): + if isinstance(phase, PBXSourcesBuildPhase) or isinstance( + phase, PBXFrameworksBuildPhase + ): + insert_at = index + break + + self._properties["buildPhases"].insert(insert_at, resources_phase) + resources_phase.parent = self + + return resources_phase + + def SourcesPhase(self): + sources_phase = self.GetBuildPhaseByType(PBXSourcesBuildPhase) + if sources_phase is None: + sources_phase = PBXSourcesBuildPhase() + self.AppendProperty("buildPhases", sources_phase) + + return sources_phase + + def FrameworksPhase(self): + frameworks_phase = self.GetBuildPhaseByType(PBXFrameworksBuildPhase) + if frameworks_phase is None: + frameworks_phase = PBXFrameworksBuildPhase() + self.AppendProperty("buildPhases", frameworks_phase) + + return frameworks_phase + + def AddDependency(self, other): + # super + XCTarget.AddDependency(self, other) + + static_library_type = "com.apple.product-type.library.static" + shared_library_type = "com.apple.product-type.library.dynamic" + framework_type = "com.apple.product-type.framework" + if ( + isinstance(other, PBXNativeTarget) + and "productType" in self._properties + and self._properties["productType"] != static_library_type + and "productType" in other._properties + and ( + other._properties["productType"] == static_library_type + or ( + ( + other._properties["productType"] == shared_library_type + or other._properties["productType"] == framework_type + ) + and ( + (not other.HasBuildSetting("MACH_O_TYPE")) + or other.GetBuildSetting("MACH_O_TYPE") != "mh_bundle" + ) + ) + ) + ): + + file_ref = other.GetProperty("productReference") + + pbxproject = self.PBXProjectAncestor() + other_pbxproject = other.PBXProjectAncestor() + if pbxproject != other_pbxproject: + other_project_product_group = pbxproject.AddOrGetProjectReference( + other_pbxproject + )[0] + file_ref = other_project_product_group.GetChildByRemoteObject(file_ref) + + self.FrameworksPhase().AppendProperty( + "files", PBXBuildFile({"fileRef": file_ref}) + ) class PBXAggregateTarget(XCTarget): - pass + pass class PBXProject(XCContainerPortal): - # A PBXProject is really just an XCObject, the XCContainerPortal thing is - # just to allow PBXProject to be used in the containerPortal property of - # PBXContainerItemProxy. - """ + # A PBXProject is really just an XCObject, the XCContainerPortal thing is + # just to allow PBXProject to be used in the containerPortal property of + # PBXContainerItemProxy. + """ Attributes: path: "sample.xcodeproj". TODO(mark) Document me! @@ -2560,91 +2745,98 @@ class PBXProject(XCContainerPortal): PBXProject. """ - _schema = XCContainerPortal._schema.copy() - _schema.update({ - 'attributes': [0, dict, 0, 0], - 'buildConfigurationList': [0, XCConfigurationList, 1, 1, - XCConfigurationList()], - 'compatibilityVersion': [0, str, 0, 1, 'Xcode 3.2'], - 'hasScannedForEncodings': [0, int, 0, 1, 1], - 'mainGroup': [0, PBXGroup, 1, 1, PBXGroup()], - 'projectDirPath': [0, str, 0, 1, ''], - 'projectReferences': [1, dict, 0, 0], - 'projectRoot': [0, str, 0, 1, ''], - 'targets': [1, XCTarget, 1, 1, []], - }) - - def __init__(self, properties=None, id=None, parent=None, path=None): - self.path = path - self._other_pbxprojects = {} - # super - return XCContainerPortal.__init__(self, properties, id, parent) + _schema = XCContainerPortal._schema.copy() + _schema.update( + { + "attributes": [0, dict, 0, 0], + "buildConfigurationList": [ + 0, + XCConfigurationList, + 1, + 1, + XCConfigurationList(), + ], + "compatibilityVersion": [0, str, 0, 1, "Xcode 3.2"], + "hasScannedForEncodings": [0, int, 0, 1, 1], + "mainGroup": [0, PBXGroup, 1, 1, PBXGroup()], + "projectDirPath": [0, str, 0, 1, ""], + "projectReferences": [1, dict, 0, 0], + "projectRoot": [0, str, 0, 1, ""], + "targets": [1, XCTarget, 1, 1, []], + } + ) - def Name(self): - name = self.path - if name[-10:] == '.xcodeproj': - name = name[:-10] - return posixpath.basename(name) + def __init__(self, properties=None, id=None, parent=None, path=None): + self.path = path + self._other_pbxprojects = {} + # super + return XCContainerPortal.__init__(self, properties, id, parent) - def Path(self): - return self.path + def Name(self): + name = self.path + if name[-10:] == ".xcodeproj": + name = name[:-10] + return posixpath.basename(name) - def Comment(self): - return 'Project object' + def Path(self): + return self.path - def Children(self): - # super - children = XCContainerPortal.Children(self) + def Comment(self): + return "Project object" + + def Children(self): + # super + children = XCContainerPortal.Children(self) - # Add children that the schema doesn't know about. Maybe there's a more - # elegant way around this, but this is the only case where we need to own - # objects in a dictionary (that is itself in a list), and three lines for - # a one-off isn't that big a deal. - if 'projectReferences' in self._properties: - for reference in self._properties['projectReferences']: - children.append(reference['ProductGroup']) + # Add children that the schema doesn't know about. Maybe there's a more + # elegant way around this, but this is the only case where we need to own + # objects in a dictionary (that is itself in a list), and three lines for + # a one-off isn't that big a deal. + if "projectReferences" in self._properties: + for reference in self._properties["projectReferences"]: + children.append(reference["ProductGroup"]) - return children + return children - def PBXProjectAncestor(self): - return self + def PBXProjectAncestor(self): + return self - def _GroupByName(self, name): - if not 'mainGroup' in self._properties: - self.SetProperty('mainGroup', PBXGroup()) + def _GroupByName(self, name): + if "mainGroup" not in self._properties: + self.SetProperty("mainGroup", PBXGroup()) - main_group = self._properties['mainGroup'] - group = main_group.GetChildByName(name) - if group is None: - group = PBXGroup({'name': name}) - main_group.AppendChild(group) + main_group = self._properties["mainGroup"] + group = main_group.GetChildByName(name) + if group is None: + group = PBXGroup({"name": name}) + main_group.AppendChild(group) - return group + return group - # SourceGroup and ProductsGroup are created by default in Xcode's own - # templates. - def SourceGroup(self): - return self._GroupByName('Source') + # SourceGroup and ProductsGroup are created by default in Xcode's own + # templates. + def SourceGroup(self): + return self._GroupByName("Source") - def ProductsGroup(self): - return self._GroupByName('Products') + def ProductsGroup(self): + return self._GroupByName("Products") - # IntermediatesGroup is used to collect source-like files that are generated - # by rules or script phases and are placed in intermediate directories such - # as DerivedSources. - def IntermediatesGroup(self): - return self._GroupByName('Intermediates') + # IntermediatesGroup is used to collect source-like files that are generated + # by rules or script phases and are placed in intermediate directories such + # as DerivedSources. + def IntermediatesGroup(self): + return self._GroupByName("Intermediates") - # FrameworksGroup and ProjectsGroup are top-level groups used to collect - # frameworks and projects. - def FrameworksGroup(self): - return self._GroupByName('Frameworks') + # FrameworksGroup and ProjectsGroup are top-level groups used to collect + # frameworks and projects. + def FrameworksGroup(self): + return self._GroupByName("Frameworks") - def ProjectsGroup(self): - return self._GroupByName('Projects') + def ProjectsGroup(self): + return self._GroupByName("Projects") - def RootGroupForPath(self, path): - """Returns a PBXGroup child of this object to which path should be added. + def RootGroupForPath(self, path): + """Returns a PBXGroup child of this object to which path should be added. This method is intended to choose between SourceGroup and IntermediatesGroup on the basis of whether path is present in a source @@ -2658,83 +2850,84 @@ def RootGroupForPath(self, path): organized hierarchically (True) or as a single flat list (False). """ - # TODO(mark): make this a class variable and bind to self on call? - # Also, this list is nowhere near exhaustive. - # INTERMEDIATE_DIR and SHARED_INTERMEDIATE_DIR are used by - # gyp.generator.xcode. There should probably be some way for that module - # to push the names in, rather than having to hard-code them here. - source_tree_groups = { - 'DERIVED_FILE_DIR': (self.IntermediatesGroup, True), - 'INTERMEDIATE_DIR': (self.IntermediatesGroup, True), - 'PROJECT_DERIVED_FILE_DIR': (self.IntermediatesGroup, True), - 'SHARED_INTERMEDIATE_DIR': (self.IntermediatesGroup, True), - } + # TODO(mark): make this a class variable and bind to self on call? + # Also, this list is nowhere near exhaustive. + # INTERMEDIATE_DIR and SHARED_INTERMEDIATE_DIR are used by + # gyp.generator.xcode. There should probably be some way for that module + # to push the names in, rather than having to hard-code them here. + source_tree_groups = { + "DERIVED_FILE_DIR": (self.IntermediatesGroup, True), + "INTERMEDIATE_DIR": (self.IntermediatesGroup, True), + "PROJECT_DERIVED_FILE_DIR": (self.IntermediatesGroup, True), + "SHARED_INTERMEDIATE_DIR": (self.IntermediatesGroup, True), + } - (source_tree, path) = SourceTreeAndPathFromPath(path) - if source_tree != None and source_tree in source_tree_groups: - (group_func, hierarchical) = source_tree_groups[source_tree] - group = group_func() - return (group, hierarchical) + (source_tree, path) = SourceTreeAndPathFromPath(path) + if source_tree is not None and source_tree in source_tree_groups: + (group_func, hierarchical) = source_tree_groups[source_tree] + group = group_func() + return (group, hierarchical) - # TODO(mark): make additional choices based on file extension. + # TODO(mark): make additional choices based on file extension. - return (self.SourceGroup(), True) + return (self.SourceGroup(), True) - def AddOrGetFileInRootGroup(self, path): - """Returns a PBXFileReference corresponding to path in the correct group + def AddOrGetFileInRootGroup(self, path): + """Returns a PBXFileReference corresponding to path in the correct group according to RootGroupForPath's heuristics. If an existing PBXFileReference for path exists, it will be returned. Otherwise, one will be created and returned. """ - (group, hierarchical) = self.RootGroupForPath(path) - return group.AddOrGetFileByPath(path, hierarchical) - - def RootGroupsTakeOverOnlyChildren(self, recurse=False): - """Calls TakeOverOnlyChild for all groups in the main group.""" - - for group in self._properties['mainGroup']._properties['children']: - if isinstance(group, PBXGroup): - group.TakeOverOnlyChild(recurse) - - def SortGroups(self): - # Sort the children of the mainGroup (like "Source" and "Products") - # according to their defined order. - self._properties['mainGroup']._properties['children'] = \ - sorted(self._properties['mainGroup']._properties['children'], - cmp=lambda x,y: x.CompareRootGroup(y)) - - # Sort everything else by putting group before files, and going - # alphabetically by name within sections of groups and files. SortGroup - # is recursive. - for group in self._properties['mainGroup']._properties['children']: - if not isinstance(group, PBXGroup): - continue - - if group.Name() == 'Products': - # The Products group is a special case. Instead of sorting - # alphabetically, sort things in the order of the targets that - # produce the products. To do this, just build up a new list of - # products based on the targets. - products = [] - for target in self._properties['targets']: - if not isinstance(target, PBXNativeTarget): - continue - product = target._properties['productReference'] - # Make sure that the product is already in the products group. - assert product in group._properties['children'] - products.append(product) - - # Make sure that this process doesn't miss anything that was already - # in the products group. - assert len(products) == len(group._properties['children']) - group._properties['children'] = products - else: - group.SortGroup() - - def AddOrGetProjectReference(self, other_pbxproject): - """Add a reference to another project file (via PBXProject object) to this + (group, hierarchical) = self.RootGroupForPath(path) + return group.AddOrGetFileByPath(path, hierarchical) + + def RootGroupsTakeOverOnlyChildren(self, recurse=False): + """Calls TakeOverOnlyChild for all groups in the main group.""" + + for group in self._properties["mainGroup"]._properties["children"]: + if isinstance(group, PBXGroup): + group.TakeOverOnlyChild(recurse) + + def SortGroups(self): + # Sort the children of the mainGroup (like "Source" and "Products") + # according to their defined order. + self._properties["mainGroup"]._properties["children"] = sorted( + self._properties["mainGroup"]._properties["children"], + cmp=lambda x, y: x.CompareRootGroup(y), + ) + + # Sort everything else by putting group before files, and going + # alphabetically by name within sections of groups and files. SortGroup + # is recursive. + for group in self._properties["mainGroup"]._properties["children"]: + if not isinstance(group, PBXGroup): + continue + + if group.Name() == "Products": + # The Products group is a special case. Instead of sorting + # alphabetically, sort things in the order of the targets that + # produce the products. To do this, just build up a new list of + # products based on the targets. + products = [] + for target in self._properties["targets"]: + if not isinstance(target, PBXNativeTarget): + continue + product = target._properties["productReference"] + # Make sure that the product is already in the products group. + assert product in group._properties["children"] + products.append(product) + + # Make sure that this process doesn't miss anything that was already + # in the products group. + assert len(products) == len(group._properties["children"]) + group._properties["children"] = products + else: + group.SortGroup() + + def AddOrGetProjectReference(self, other_pbxproject): + """Add a reference to another project file (via PBXProject object) to this one. Returns [ProductGroup, ProjectRef]. ProductGroup is a PBXGroup object in @@ -2747,243 +2940,259 @@ def AddOrGetProjectReference(self, other_pbxproject): still be updated if necessary. """ - if not 'projectReferences' in self._properties: - self._properties['projectReferences'] = [] - - product_group = None - project_ref = None - - if not other_pbxproject in self._other_pbxprojects: - # This project file isn't yet linked to the other one. Establish the - # link. - product_group = PBXGroup({'name': 'Products'}) - - # ProductGroup is strong. - product_group.parent = self - - # There's nothing unique about this PBXGroup, and if left alone, it will - # wind up with the same set of hashables as all other PBXGroup objects - # owned by the projectReferences list. Add the hashables of the - # remote PBXProject that it's related to. - product_group._hashables.extend(other_pbxproject.Hashables()) - - # The other project reports its path as relative to the same directory - # that this project's path is relative to. The other project's path - # is not necessarily already relative to this project. Figure out the - # pathname that this project needs to use to refer to the other one. - this_path = posixpath.dirname(self.Path()) - projectDirPath = self.GetProperty('projectDirPath') - if projectDirPath: - if posixpath.isabs(projectDirPath[0]): - this_path = projectDirPath + if "projectReferences" not in self._properties: + self._properties["projectReferences"] = [] + + product_group = None + project_ref = None + + if other_pbxproject not in self._other_pbxprojects: + # This project file isn't yet linked to the other one. Establish the + # link. + product_group = PBXGroup({"name": "Products"}) + + # ProductGroup is strong. + product_group.parent = self + + # There's nothing unique about this PBXGroup, and if left alone, it will + # wind up with the same set of hashables as all other PBXGroup objects + # owned by the projectReferences list. Add the hashables of the + # remote PBXProject that it's related to. + product_group._hashables.extend(other_pbxproject.Hashables()) + + # The other project reports its path as relative to the same directory + # that this project's path is relative to. The other project's path + # is not necessarily already relative to this project. Figure out the + # pathname that this project needs to use to refer to the other one. + this_path = posixpath.dirname(self.Path()) + projectDirPath = self.GetProperty("projectDirPath") + if projectDirPath: + if posixpath.isabs(projectDirPath[0]): + this_path = projectDirPath + else: + this_path = posixpath.join(this_path, projectDirPath) + other_path = gyp.common.RelativePath(other_pbxproject.Path(), this_path) + + # ProjectRef is weak (it's owned by the mainGroup hierarchy). + project_ref = PBXFileReference( + { + "lastKnownFileType": "wrapper.pb-project", + "path": other_path, + "sourceTree": "SOURCE_ROOT", + } + ) + self.ProjectsGroup().AppendChild(project_ref) + + ref_dict = {"ProductGroup": product_group, "ProjectRef": project_ref} + self._other_pbxprojects[other_pbxproject] = ref_dict + self.AppendProperty("projectReferences", ref_dict) + + # Xcode seems to sort this list case-insensitively + self._properties["projectReferences"] = sorted( + self._properties["projectReferences"], + cmp=lambda x, y: cmp( + x["ProjectRef"].Name().lower(), y["ProjectRef"].Name().lower() + ), + ) else: - this_path = posixpath.join(this_path, projectDirPath) - other_path = gyp.common.RelativePath(other_pbxproject.Path(), this_path) - - # ProjectRef is weak (it's owned by the mainGroup hierarchy). - project_ref = PBXFileReference({ - 'lastKnownFileType': 'wrapper.pb-project', - 'path': other_path, - 'sourceTree': 'SOURCE_ROOT', - }) - self.ProjectsGroup().AppendChild(project_ref) - - ref_dict = {'ProductGroup': product_group, 'ProjectRef': project_ref} - self._other_pbxprojects[other_pbxproject] = ref_dict - self.AppendProperty('projectReferences', ref_dict) - - # Xcode seems to sort this list case-insensitively - self._properties['projectReferences'] = \ - sorted(self._properties['projectReferences'], cmp=lambda x,y: - cmp(x['ProjectRef'].Name().lower(), - y['ProjectRef'].Name().lower())) - else: - # The link already exists. Pull out the relevnt data. - project_ref_dict = self._other_pbxprojects[other_pbxproject] - product_group = project_ref_dict['ProductGroup'] - project_ref = project_ref_dict['ProjectRef'] - - self._SetUpProductReferences(other_pbxproject, product_group, project_ref) - - inherit_unique_symroot = self._AllSymrootsUnique(other_pbxproject, False) - targets = other_pbxproject.GetProperty('targets') - if all(self._AllSymrootsUnique(t, inherit_unique_symroot) for t in targets): - dir_path = project_ref._properties['path'] - product_group._hashables.extend(dir_path) - - return [product_group, project_ref] - - def _AllSymrootsUnique(self, target, inherit_unique_symroot): - # Returns True if all configurations have a unique 'SYMROOT' attribute. - # The value of inherit_unique_symroot decides, if a configuration is assumed - # to inherit a unique 'SYMROOT' attribute from its parent, if it doesn't - # define an explicit value for 'SYMROOT'. - symroots = self._DefinedSymroots(target) - for s in self._DefinedSymroots(target): - if (s is not None and not self._IsUniqueSymrootForTarget(s) or - s is None and not inherit_unique_symroot): + # The link already exists. Pull out the relevnt data. + project_ref_dict = self._other_pbxprojects[other_pbxproject] + product_group = project_ref_dict["ProductGroup"] + project_ref = project_ref_dict["ProjectRef"] + + self._SetUpProductReferences(other_pbxproject, product_group, project_ref) + + inherit_unique_symroot = self._AllSymrootsUnique(other_pbxproject, False) + targets = other_pbxproject.GetProperty("targets") + if all(self._AllSymrootsUnique(t, inherit_unique_symroot) for t in targets): + dir_path = project_ref._properties["path"] + product_group._hashables.extend(dir_path) + + return [product_group, project_ref] + + def _AllSymrootsUnique(self, target, inherit_unique_symroot): + # Returns True if all configurations have a unique 'SYMROOT' attribute. + # The value of inherit_unique_symroot decides, if a configuration is assumed + # to inherit a unique 'SYMROOT' attribute from its parent, if it doesn't + # define an explicit value for 'SYMROOT'. + symroots = self._DefinedSymroots(target) + for s in self._DefinedSymroots(target): + if ( + s is not None + and not self._IsUniqueSymrootForTarget(s) + or s is None + and not inherit_unique_symroot + ): + return False + return True if symroots else inherit_unique_symroot + + def _DefinedSymroots(self, target): + # Returns all values for the 'SYMROOT' attribute defined in all + # configurations for this target. If any configuration doesn't define the + # 'SYMROOT' attribute, None is added to the returned set. If all + # configurations don't define the 'SYMROOT' attribute, an empty set is + # returned. + config_list = target.GetProperty("buildConfigurationList") + symroots = set() + for config in config_list.GetProperty("buildConfigurations"): + setting = config.GetProperty("buildSettings") + if "SYMROOT" in setting: + symroots.add(setting["SYMROOT"]) + else: + symroots.add(None) + if len(symroots) == 1 and None in symroots: + return set() + return symroots + + def _IsUniqueSymrootForTarget(self, symroot): + # This method returns True if all configurations in target contain a + # 'SYMROOT' attribute that is unique for the given target. A value is + # unique, if the Xcode macro '$SRCROOT' appears in it in any form. + uniquifier = ["$SRCROOT", "$(SRCROOT)"] + if any(x in symroot for x in uniquifier): + return True return False - return True if symroots else inherit_unique_symroot - - def _DefinedSymroots(self, target): - # Returns all values for the 'SYMROOT' attribute defined in all - # configurations for this target. If any configuration doesn't define the - # 'SYMROOT' attribute, None is added to the returned set. If all - # configurations don't define the 'SYMROOT' attribute, an empty set is - # returned. - config_list = target.GetProperty('buildConfigurationList') - symroots = set() - for config in config_list.GetProperty('buildConfigurations'): - setting = config.GetProperty('buildSettings') - if 'SYMROOT' in setting: - symroots.add(setting['SYMROOT']) - else: - symroots.add(None) - if len(symroots) == 1 and None in symroots: - return set() - return symroots - - def _IsUniqueSymrootForTarget(self, symroot): - # This method returns True if all configurations in target contain a - # 'SYMROOT' attribute that is unique for the given target. A value is - # unique, if the Xcode macro '$SRCROOT' appears in it in any form. - uniquifier = ['$SRCROOT', '$(SRCROOT)'] - if any(x in symroot for x in uniquifier): - return True - return False - - def _SetUpProductReferences(self, other_pbxproject, product_group, - project_ref): - # TODO(mark): This only adds references to products in other_pbxproject - # when they don't exist in this pbxproject. Perhaps it should also - # remove references from this pbxproject that are no longer present in - # other_pbxproject. Perhaps it should update various properties if they - # change. - for target in other_pbxproject._properties['targets']: - if not isinstance(target, PBXNativeTarget): - continue - - other_fileref = target._properties['productReference'] - if product_group.GetChildByRemoteObject(other_fileref) is None: - # Xcode sets remoteInfo to the name of the target and not the name - # of its product, despite this proxy being a reference to the product. - container_item = PBXContainerItemProxy({ - 'containerPortal': project_ref, - 'proxyType': 2, - 'remoteGlobalIDString': other_fileref, - 'remoteInfo': target.Name() - }) - # TODO(mark): Does sourceTree get copied straight over from the other - # project? Can the other project ever have lastKnownFileType here - # instead of explicitFileType? (Use it if so?) Can path ever be - # unset? (I don't think so.) Can other_fileref have name set, and - # does it impact the PBXReferenceProxy if so? These are the questions - # that perhaps will be answered one day. - reference_proxy = PBXReferenceProxy({ - 'fileType': other_fileref._properties['explicitFileType'], - 'path': other_fileref._properties['path'], - 'sourceTree': other_fileref._properties['sourceTree'], - 'remoteRef': container_item, - }) - - product_group.AppendChild(reference_proxy) - - def SortRemoteProductReferences(self): - # For each remote project file, sort the associated ProductGroup in the - # same order that the targets are sorted in the remote project file. This - # is the sort order used by Xcode. - - def CompareProducts(x, y, remote_products): - # x and y are PBXReferenceProxy objects. Go through their associated - # PBXContainerItem to get the remote PBXFileReference, which will be - # present in the remote_products list. - x_remote = x._properties['remoteRef']._properties['remoteGlobalIDString'] - y_remote = y._properties['remoteRef']._properties['remoteGlobalIDString'] - x_index = remote_products.index(x_remote) - y_index = remote_products.index(y_remote) - - # Use the order of each remote PBXFileReference in remote_products to - # determine the sort order. - return cmp(x_index, y_index) - - for other_pbxproject, ref_dict in self._other_pbxprojects.items(): - # Build up a list of products in the remote project file, ordered the - # same as the targets that produce them. - remote_products = [] - for target in other_pbxproject._properties['targets']: - if not isinstance(target, PBXNativeTarget): - continue - remote_products.append(target._properties['productReference']) - - # Sort the PBXReferenceProxy children according to the list of remote - # products. - product_group = ref_dict['ProductGroup'] - product_group._properties['children'] = sorted( - product_group._properties['children'], - cmp=lambda x, y, rp=remote_products: CompareProducts(x, y, rp)) + + def _SetUpProductReferences(self, other_pbxproject, product_group, project_ref): + # TODO(mark): This only adds references to products in other_pbxproject + # when they don't exist in this pbxproject. Perhaps it should also + # remove references from this pbxproject that are no longer present in + # other_pbxproject. Perhaps it should update various properties if they + # change. + for target in other_pbxproject._properties["targets"]: + if not isinstance(target, PBXNativeTarget): + continue + + other_fileref = target._properties["productReference"] + if product_group.GetChildByRemoteObject(other_fileref) is None: + # Xcode sets remoteInfo to the name of the target and not the name + # of its product, despite this proxy being a reference to the product. + container_item = PBXContainerItemProxy( + { + "containerPortal": project_ref, + "proxyType": 2, + "remoteGlobalIDString": other_fileref, + "remoteInfo": target.Name(), + } + ) + # TODO(mark): Does sourceTree get copied straight over from the other + # project? Can the other project ever have lastKnownFileType here + # instead of explicitFileType? (Use it if so?) Can path ever be + # unset? (I don't think so.) Can other_fileref have name set, and + # does it impact the PBXReferenceProxy if so? These are the questions + # that perhaps will be answered one day. + reference_proxy = PBXReferenceProxy( + { + "fileType": other_fileref._properties["explicitFileType"], + "path": other_fileref._properties["path"], + "sourceTree": other_fileref._properties["sourceTree"], + "remoteRef": container_item, + } + ) + + product_group.AppendChild(reference_proxy) + + def SortRemoteProductReferences(self): + # For each remote project file, sort the associated ProductGroup in the + # same order that the targets are sorted in the remote project file. This + # is the sort order used by Xcode. + + def CompareProducts(x, y, remote_products): + # x and y are PBXReferenceProxy objects. Go through their associated + # PBXContainerItem to get the remote PBXFileReference, which will be + # present in the remote_products list. + x_remote = x._properties["remoteRef"]._properties["remoteGlobalIDString"] + y_remote = y._properties["remoteRef"]._properties["remoteGlobalIDString"] + x_index = remote_products.index(x_remote) + y_index = remote_products.index(y_remote) + + # Use the order of each remote PBXFileReference in remote_products to + # determine the sort order. + return cmp(x_index, y_index) + + for other_pbxproject, ref_dict in self._other_pbxprojects.items(): + # Build up a list of products in the remote project file, ordered the + # same as the targets that produce them. + remote_products = [] + for target in other_pbxproject._properties["targets"]: + if not isinstance(target, PBXNativeTarget): + continue + remote_products.append(target._properties["productReference"]) + + # Sort the PBXReferenceProxy children according to the list of remote + # products. + product_group = ref_dict["ProductGroup"] + product_group._properties["children"] = sorted( + product_group._properties["children"], + cmp=lambda x, y, rp=remote_products: CompareProducts(x, y, rp), + ) class XCProjectFile(XCObject): - _schema = XCObject._schema.copy() - _schema.update({ - 'archiveVersion': [0, int, 0, 1, 1], - 'classes': [0, dict, 0, 1, {}], - 'objectVersion': [0, int, 0, 1, 46], - 'rootObject': [0, PBXProject, 1, 1], - }) - - def ComputeIDs(self, recursive=True, overwrite=True, hash=None): - # Although XCProjectFile is implemented here as an XCObject, it's not a - # proper object in the Xcode sense, and it certainly doesn't have its own - # ID. Pass through an attempt to update IDs to the real root object. - if recursive: - self._properties['rootObject'].ComputeIDs(recursive, overwrite, hash) - - def Print(self, file=sys.stdout): - self.VerifyHasRequiredProperties() - - # Add the special "objects" property, which will be caught and handled - # separately during printing. This structure allows a fairly standard - # loop do the normal printing. - self._properties['objects'] = {} - self._XCPrint(file, 0, '// !$*UTF8*$!\n') - if self._should_print_single_line: - self._XCPrint(file, 0, '{ ') - else: - self._XCPrint(file, 0, '{\n') - for property, value in sorted(self._properties.items(), - cmp=lambda x, y: cmp(x, y)): - if property == 'objects': - self._PrintObjects(file) - else: - self._XCKVPrint(file, 1, property, value) - self._XCPrint(file, 0, '}\n') - del self._properties['objects'] - - def _PrintObjects(self, file): - if self._should_print_single_line: - self._XCPrint(file, 0, 'objects = {') - else: - self._XCPrint(file, 1, 'objects = {\n') - - objects_by_class = {} - for object in self.Descendants(): - if object == self: - continue - class_name = object.__class__.__name__ - if not class_name in objects_by_class: - objects_by_class[class_name] = [] - objects_by_class[class_name].append(object) - - for class_name in sorted(objects_by_class): - self._XCPrint(file, 0, '\n') - self._XCPrint(file, 0, '/* Begin ' + class_name + ' section */\n') - for object in sorted(objects_by_class[class_name], - cmp=lambda x, y: cmp(x.id, y.id)): - object.Print(file) - self._XCPrint(file, 0, '/* End ' + class_name + ' section */\n') - - if self._should_print_single_line: - self._XCPrint(file, 0, '}; ') - else: - self._XCPrint(file, 1, '};\n') + _schema = XCObject._schema.copy() + _schema.update( + { + "archiveVersion": [0, int, 0, 1, 1], + "classes": [0, dict, 0, 1, {}], + "objectVersion": [0, int, 0, 1, 46], + "rootObject": [0, PBXProject, 1, 1], + } + ) + + def ComputeIDs(self, recursive=True, overwrite=True, hash=None): + # Although XCProjectFile is implemented here as an XCObject, it's not a + # proper object in the Xcode sense, and it certainly doesn't have its own + # ID. Pass through an attempt to update IDs to the real root object. + if recursive: + self._properties["rootObject"].ComputeIDs(recursive, overwrite, hash) + + def Print(self, file=sys.stdout): + self.VerifyHasRequiredProperties() + + # Add the special "objects" property, which will be caught and handled + # separately during printing. This structure allows a fairly standard + # loop do the normal printing. + self._properties["objects"] = {} + self._XCPrint(file, 0, "// !$*UTF8*$!\n") + if self._should_print_single_line: + self._XCPrint(file, 0, "{ ") + else: + self._XCPrint(file, 0, "{\n") + for property, value in sorted( + self._properties.items(), cmp=lambda x, y: cmp(x, y) + ): + if property == "objects": + self._PrintObjects(file) + else: + self._XCKVPrint(file, 1, property, value) + self._XCPrint(file, 0, "}\n") + del self._properties["objects"] + + def _PrintObjects(self, file): + if self._should_print_single_line: + self._XCPrint(file, 0, "objects = {") + else: + self._XCPrint(file, 1, "objects = {\n") + + objects_by_class = {} + for object in self.Descendants(): + if object == self: + continue + class_name = object.__class__.__name__ + if class_name not in objects_by_class: + objects_by_class[class_name] = [] + objects_by_class[class_name].append(object) + + for class_name in sorted(objects_by_class): + self._XCPrint(file, 0, "\n") + self._XCPrint(file, 0, "/* Begin " + class_name + " section */\n") + for object in sorted( + objects_by_class[class_name], cmp=lambda x, y: cmp(x.id, y.id) + ): + object.Print(file) + self._XCPrint(file, 0, "/* End " + class_name + " section */\n") + + if self._should_print_single_line: + self._XCPrint(file, 0, "}; ") + else: + self._XCPrint(file, 1, "};\n") diff --git a/tools/gyp/pylib/gyp/xml_fix.py b/tools/gyp/pylib/gyp/xml_fix.py index 5de848158d2899..0a945322b4587a 100644 --- a/tools/gyp/pylib/gyp/xml_fix.py +++ b/tools/gyp/pylib/gyp/xml_fix.py @@ -14,56 +14,52 @@ def _Replacement_write_data(writer, data, is_attrib=False): - """Writes datachars to writer.""" - data = data.replace("&", "&").replace("<", "<") - data = data.replace("\"", """).replace(">", ">") - if is_attrib: - data = data.replace( - "\r", " ").replace( - "\n", " ").replace( - "\t", " ") - writer.write(data) + """Writes datachars to writer.""" + data = data.replace("&", "&").replace("<", "<") + data = data.replace('"', """).replace(">", ">") + if is_attrib: + data = data.replace("\r", " ").replace("\n", " ").replace("\t", " ") + writer.write(data) def _Replacement_writexml(self, writer, indent="", addindent="", newl=""): - # indent = current indentation - # addindent = indentation to add to higher levels - # newl = newline string - writer.write(indent+"<" + self.tagName) + # indent = current indentation + # addindent = indentation to add to higher levels + # newl = newline string + writer.write(indent + "<" + self.tagName) - attrs = self._get_attributes() - a_names = attrs.keys() - a_names.sort() + attrs = self._get_attributes() + a_names = sorted(attrs.keys()) - for a_name in a_names: - writer.write(" %s=\"" % a_name) - _Replacement_write_data(writer, attrs[a_name].value, is_attrib=True) - writer.write("\"") - if self.childNodes: - writer.write(">%s" % newl) - for node in self.childNodes: - node.writexml(writer, indent + addindent, addindent, newl) - writer.write("%s%s" % (indent, self.tagName, newl)) - else: - writer.write("/>%s" % newl) + for a_name in a_names: + writer.write(' %s="' % a_name) + _Replacement_write_data(writer, attrs[a_name].value, is_attrib=True) + writer.write('"') + if self.childNodes: + writer.write(">%s" % newl) + for node in self.childNodes: + node.writexml(writer, indent + addindent, addindent, newl) + writer.write("%s%s" % (indent, self.tagName, newl)) + else: + writer.write("/>%s" % newl) class XmlFix(object): - """Object to manage temporary patching of xml.dom.minidom.""" + """Object to manage temporary patching of xml.dom.minidom.""" - def __init__(self): - # Preserve current xml.dom.minidom functions. - self.write_data = xml.dom.minidom._write_data - self.writexml = xml.dom.minidom.Element.writexml - # Inject replacement versions of a function and a method. - xml.dom.minidom._write_data = _Replacement_write_data - xml.dom.minidom.Element.writexml = _Replacement_writexml + def __init__(self): + # Preserve current xml.dom.minidom functions. + self.write_data = xml.dom.minidom._write_data + self.writexml = xml.dom.minidom.Element.writexml + # Inject replacement versions of a function and a method. + xml.dom.minidom._write_data = _Replacement_write_data + xml.dom.minidom.Element.writexml = _Replacement_writexml - def Cleanup(self): - if self.write_data: - xml.dom.minidom._write_data = self.write_data - xml.dom.minidom.Element.writexml = self.writexml - self.write_data = None + def Cleanup(self): + if self.write_data: + xml.dom.minidom._write_data = self.write_data + xml.dom.minidom.Element.writexml = self.writexml + self.write_data = None - def __del__(self): - self.Cleanup() + def __del__(self): + self.Cleanup() diff --git a/tools/gyp/requirements_dev.txt b/tools/gyp/requirements_dev.txt new file mode 100644 index 00000000000000..28ecacab602938 --- /dev/null +++ b/tools/gyp/requirements_dev.txt @@ -0,0 +1,2 @@ +flake8 +pytest diff --git a/tools/gyp/samples/samples b/tools/gyp/samples/samples deleted file mode 100755 index 804b618998747d..00000000000000 --- a/tools/gyp/samples/samples +++ /dev/null @@ -1,81 +0,0 @@ -#!/usr/bin/python - -# Copyright (c) 2009 Google Inc. All rights reserved. -# Use of this source code is governed by a BSD-style license that can be -# found in the LICENSE file. - -import os.path -import shutil -import sys - - -gyps = [ - 'app/app.gyp', - 'base/base.gyp', - 'build/temp_gyp/googleurl.gyp', - 'build/all.gyp', - 'build/common.gypi', - 'build/external_code.gypi', - 'chrome/test/security_tests/security_tests.gyp', - 'chrome/third_party/hunspell/hunspell.gyp', - 'chrome/chrome.gyp', - 'media/media.gyp', - 'net/net.gyp', - 'printing/printing.gyp', - 'sdch/sdch.gyp', - 'skia/skia.gyp', - 'testing/gmock.gyp', - 'testing/gtest.gyp', - 'third_party/bzip2/bzip2.gyp', - 'third_party/icu38/icu38.gyp', - 'third_party/libevent/libevent.gyp', - 'third_party/libjpeg/libjpeg.gyp', - 'third_party/libpng/libpng.gyp', - 'third_party/libxml/libxml.gyp', - 'third_party/libxslt/libxslt.gyp', - 'third_party/lzma_sdk/lzma_sdk.gyp', - 'third_party/modp_b64/modp_b64.gyp', - 'third_party/npapi/npapi.gyp', - 'third_party/sqlite/sqlite.gyp', - 'third_party/zlib/zlib.gyp', - 'v8/tools/gyp/v8.gyp', - 'webkit/activex_shim/activex_shim.gyp', - 'webkit/activex_shim_dll/activex_shim_dll.gyp', - 'webkit/build/action_csspropertynames.py', - 'webkit/build/action_cssvaluekeywords.py', - 'webkit/build/action_jsconfig.py', - 'webkit/build/action_makenames.py', - 'webkit/build/action_maketokenizer.py', - 'webkit/build/action_useragentstylesheets.py', - 'webkit/build/rule_binding.py', - 'webkit/build/rule_bison.py', - 'webkit/build/rule_gperf.py', - 'webkit/tools/test_shell/test_shell.gyp', - 'webkit/webkit.gyp', -] - - -def Main(argv): - if len(argv) != 3 or argv[1] not in ['push', 'pull']: - print 'Usage: %s push/pull PATH_TO_CHROME' % argv[0] - return 1 - - path_to_chrome = argv[2] - - for g in gyps: - chrome_file = os.path.join(path_to_chrome, g) - local_file = os.path.join(os.path.dirname(argv[0]), os.path.split(g)[1]) - if argv[1] == 'push': - print 'Copying %s to %s' % (local_file, chrome_file) - shutil.copyfile(local_file, chrome_file) - elif argv[1] == 'pull': - print 'Copying %s to %s' % (chrome_file, local_file) - shutil.copyfile(chrome_file, local_file) - else: - assert False - - return 0 - - -if __name__ == '__main__': - sys.exit(Main(sys.argv)) diff --git a/tools/gyp/samples/samples.bat b/tools/gyp/samples/samples.bat deleted file mode 100644 index 778d9c90f06066..00000000000000 --- a/tools/gyp/samples/samples.bat +++ /dev/null @@ -1,5 +0,0 @@ -@rem Copyright (c) 2009 Google Inc. All rights reserved. -@rem Use of this source code is governed by a BSD-style license that can be -@rem found in the LICENSE file. - -@python %~dp0/samples %* diff --git a/tools/gyp/setup.py b/tools/gyp/setup.py index 75a42558d8026b..656f912b83b7bb 100755 --- a/tools/gyp/setup.py +++ b/tools/gyp/setup.py @@ -4,16 +4,41 @@ # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. +from os import path + from setuptools import setup +here = path.abspath(path.dirname(__file__)) +# Get the long description from the README file +with open(path.join(here, "README.md")) as in_file: + long_description = in_file.read() + setup( - name='gyp', - version='0.1', - description='Generate Your Projects', - author='Chromium Authors', - author_email='chromium-dev@googlegroups.com', - url='http://code.google.com/p/gyp', - package_dir = {'': 'pylib'}, - packages=['gyp', 'gyp.generator'], - entry_points = {'console_scripts': ['gyp=gyp:script_main'] } + name="gyp-next", + version="0.5.0", + description="A fork of the GYP build system for use in the Node.js projects", + long_description=long_description, + long_description_content_type="text/markdown", + author="Node.js contributors", + author_email="ryzokuken@disroot.org", + url="https://github.com/nodejs/gyp-next", + package_dir={"": "pylib"}, + packages=["gyp", "gyp.generator"], + entry_points={"console_scripts": ["gyp=gyp:script_main"]}, + python_requires=">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*", + classifiers=[ + "Development Status :: 3 - Alpha", + "Environment :: Console", + "Intended Audience :: Developers", + "License :: OSI Approved :: BSD License", + "Natural Language :: English", + "Programming Language :: Python", + "Programming Language :: Python :: 2", + "Programming Language :: Python :: 2.7", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.5", + "Programming Language :: Python :: 3.6", + "Programming Language :: Python :: 3.7", + "Programming Language :: Python :: 3.8", + ], ) diff --git a/tools/gyp/test_gyp.py b/tools/gyp/test_gyp.py new file mode 100755 index 00000000000000..382e75272d8b6b --- /dev/null +++ b/tools/gyp/test_gyp.py @@ -0,0 +1,268 @@ +#!/usr/bin/env python +# Copyright (c) 2012 Google Inc. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +"""gyptest.py -- test runner for GYP tests.""" + +from __future__ import print_function + +import argparse +import os +import platform +import subprocess +import sys +import time + + +def is_test_name(f): + return f.startswith("gyptest") and f.endswith(".py") + + +def find_all_gyptest_files(directory): + result = [] + for root, dirs, files in os.walk(directory): + result.extend([os.path.join(root, f) for f in files if is_test_name(f)]) + result.sort() + return result + + +def main(argv=None): + if argv is None: + argv = sys.argv + + parser = argparse.ArgumentParser() + parser.add_argument("-a", "--all", action="store_true", help="run all tests") + parser.add_argument("-C", "--chdir", action="store", help="change to directory") + parser.add_argument( + "-f", + "--format", + action="store", + default="", + help="run tests with the specified formats", + ) + parser.add_argument( + "-G", + "--gyp_option", + action="append", + default=[], + help="Add -G options to the gyp command line", + ) + parser.add_argument( + "-l", "--list", action="store_true", help="list available tests and exit" + ) + parser.add_argument( + "-n", + "--no-exec", + action="store_true", + help="no execute, just print the command line", + ) + parser.add_argument( + "--path", action="append", default=[], help="additional $PATH directory" + ) + parser.add_argument( + "-q", + "--quiet", + action="store_true", + help="quiet, don't print anything unless there are failures", + ) + parser.add_argument( + "-v", + "--verbose", + action="store_true", + help="print configuration info and test results.", + ) + parser.add_argument("tests", nargs="*") + args = parser.parse_args(argv[1:]) + + if args.chdir: + os.chdir(args.chdir) + + if args.path: + extra_path = [os.path.abspath(p) for p in args.path] + extra_path = os.pathsep.join(extra_path) + os.environ["PATH"] = extra_path + os.pathsep + os.environ["PATH"] + + if not args.tests: + if not args.all: + sys.stderr.write("Specify -a to get all tests.\n") + return 1 + args.tests = ["test"] + + tests = [] + for arg in args.tests: + if os.path.isdir(arg): + tests.extend(find_all_gyptest_files(os.path.normpath(arg))) + else: + if not is_test_name(os.path.basename(arg)): + print(arg, "is not a valid gyp test name.", file=sys.stderr) + sys.exit(1) + tests.append(arg) + + if args.list: + for test in tests: + print(test) + sys.exit(0) + + os.environ["PYTHONPATH"] = os.path.abspath("test/lib") + + if args.verbose: + print_configuration_info() + + if args.gyp_option and not args.quiet: + print("Extra Gyp options: %s\n" % args.gyp_option) + + if args.format: + format_list = args.format.split(",") + else: + format_list = { + "aix5": ["make"], + "freebsd7": ["make"], + "freebsd8": ["make"], + "openbsd5": ["make"], + "cygwin": ["msvs"], + "win32": ["msvs", "ninja"], + "linux": ["make", "ninja"], + "linux2": ["make", "ninja"], + "linux3": ["make", "ninja"], + # TODO: Re-enable xcode-ninja. + # https://bugs.chromium.org/p/gyp/issues/detail?id=530 + # 'darwin': ['make', 'ninja', 'xcode', 'xcode-ninja'], + "darwin": ["make", "ninja", "xcode"], + }[sys.platform] + + gyp_options = [] + for option in args.gyp_option: + gyp_options += ["-G", option] + + runner = Runner(format_list, tests, gyp_options, args.verbose) + runner.run() + + if not args.quiet: + runner.print_results() + + if runner.failures: + return 1 + else: + return 0 + + +def print_configuration_info(): + print("Test configuration:") + if sys.platform == "darwin": + sys.path.append(os.path.abspath("test/lib")) + import TestMac + + print(" Mac %s %s" % (platform.mac_ver()[0], platform.mac_ver()[2])) + print(" Xcode %s" % TestMac.Xcode.Version()) + elif sys.platform == "win32": + sys.path.append(os.path.abspath("pylib")) + import gyp.MSVSVersion + + print(" Win %s %s\n" % platform.win32_ver()[0:2]) + print(" MSVS %s" % gyp.MSVSVersion.SelectVisualStudioVersion().Description()) + elif sys.platform in ("linux", "linux2"): + print(" Linux %s" % " ".join(platform.linux_distribution())) + print(" Python %s" % platform.python_version()) + print(" PYTHONPATH=%s" % os.environ["PYTHONPATH"]) + print() + + +class Runner(object): + def __init__(self, formats, tests, gyp_options, verbose): + self.formats = formats + self.tests = tests + self.verbose = verbose + self.gyp_options = gyp_options + self.failures = [] + self.num_tests = len(formats) * len(tests) + num_digits = len(str(self.num_tests)) + self.fmt_str = "[%%%dd/%%%dd] (%%s) %%s" % (num_digits, num_digits) + self.isatty = sys.stdout.isatty() and not self.verbose + self.env = os.environ.copy() + self.hpos = 0 + + def run(self): + run_start = time.time() + + i = 1 + for fmt in self.formats: + for test in self.tests: + self.run_test(test, fmt, i) + i += 1 + + if self.isatty: + self.erase_current_line() + + self.took = time.time() - run_start + + def run_test(self, test, fmt, i): + if self.isatty: + self.erase_current_line() + + msg = self.fmt_str % (i, self.num_tests, fmt, test) + self.print_(msg) + + start = time.time() + cmd = [sys.executable, test] + self.gyp_options + self.env["TESTGYP_FORMAT"] = fmt + proc = subprocess.Popen( + cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=self.env + ) + proc.wait() + took = time.time() - start + + stdout = proc.stdout.read().decode("utf8") + if proc.returncode == 2: + res = "skipped" + elif proc.returncode: + res = "failed" + self.failures.append("(%s) %s" % (test, fmt)) + else: + res = "passed" + res_msg = " %s %.3fs" % (res, took) + self.print_(res_msg) + + if ( + stdout + and not stdout.endswith("PASSED\n") + and not (stdout.endswith("NO RESULT\n")) + ): + print() + print("\n".join(" %s" % line for line in stdout.splitlines())) + elif not self.isatty: + print() + + def print_(self, msg): + print(msg, end="") + index = msg.rfind("\n") + if index == -1: + self.hpos += len(msg) + else: + self.hpos = len(msg) - index + sys.stdout.flush() + + def erase_current_line(self): + print("\b" * self.hpos + " " * self.hpos + "\b" * self.hpos, end="") + sys.stdout.flush() + self.hpos = 0 + + def print_results(self): + num_failures = len(self.failures) + if num_failures: + print() + if num_failures == 1: + print("Failed the following test:") + else: + print("Failed the following %d tests:" % num_failures) + print("\t" + "\n\t".join(sorted(self.failures))) + print() + print( + "Ran %d tests in %.3fs, %d failed." + % (self.num_tests, self.took, num_failures) + ) + print() + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/tools/gyp/tools/graphviz.py b/tools/gyp/tools/graphviz.py index 538b059da4a7e6..1f3acf37fccd8a 100755 --- a/tools/gyp/tools/graphviz.py +++ b/tools/gyp/tools/graphviz.py @@ -16,87 +16,88 @@ def ParseTarget(target): - target, _, suffix = target.partition('#') - filename, _, target = target.partition(':') - return filename, target, suffix + target, _, suffix = target.partition("#") + filename, _, target = target.partition(":") + return filename, target, suffix def LoadEdges(filename, targets): - """Load the edges map from the dump file, and filter it to only + """Load the edges map from the dump file, and filter it to only show targets in |targets| and their depedendents.""" - file = open('dump.json') - edges = json.load(file) - file.close() + file = open("dump.json") + edges = json.load(file) + file.close() - # Copy out only the edges we're interested in from the full edge list. - target_edges = {} - to_visit = targets[:] - while to_visit: - src = to_visit.pop() - if src in target_edges: - continue - target_edges[src] = edges[src] - to_visit.extend(edges[src]) + # Copy out only the edges we're interested in from the full edge list. + target_edges = {} + to_visit = targets[:] + while to_visit: + src = to_visit.pop() + if src in target_edges: + continue + target_edges[src] = edges[src] + to_visit.extend(edges[src]) - return target_edges + return target_edges def WriteGraph(edges): - """Print a graphviz graph to stdout. + """Print a graphviz graph to stdout. |edges| is a map of target to a list of other targets it depends on.""" - # Bucket targets by file. - files = collections.defaultdict(list) - for src, dst in edges.items(): - build_file, target_name, toolset = ParseTarget(src) - files[build_file].append(src) - - print('digraph D {') - print(' fontsize=8') # Used by subgraphs. - print(' node [fontsize=8]') - - # Output nodes by file. We must first write out each node within - # its file grouping before writing out any edges that may refer - # to those nodes. - for filename, targets in files.items(): - if len(targets) == 1: - # If there's only one node for this file, simplify - # the display by making it a box without an internal node. - target = targets[0] - build_file, target_name, toolset = ParseTarget(target) - print(' "%s" [shape=box, label="%s\\n%s"]' % (target, filename, - target_name)) - else: - # Group multiple nodes together in a subgraph. - print(' subgraph "cluster_%s" {' % filename) - print(' label = "%s"' % filename) - for target in targets: - build_file, target_name, toolset = ParseTarget(target) - print(' "%s" [label="%s"]' % (target, target_name)) - print(' }') - - # Now that we've placed all the nodes within subgraphs, output all - # the edges between nodes. - for src, dsts in edges.items(): - for dst in dsts: - print(' "%s" -> "%s"' % (src, dst)) - - print('}') + # Bucket targets by file. + files = collections.defaultdict(list) + for src, dst in edges.items(): + build_file, target_name, toolset = ParseTarget(src) + files[build_file].append(src) + + print("digraph D {") + print(" fontsize=8") # Used by subgraphs. + print(" node [fontsize=8]") + + # Output nodes by file. We must first write out each node within + # its file grouping before writing out any edges that may refer + # to those nodes. + for filename, targets in files.items(): + if len(targets) == 1: + # If there's only one node for this file, simplify + # the display by making it a box without an internal node. + target = targets[0] + build_file, target_name, toolset = ParseTarget(target) + print( + ' "%s" [shape=box, label="%s\\n%s"]' % (target, filename, target_name) + ) + else: + # Group multiple nodes together in a subgraph. + print(' subgraph "cluster_%s" {' % filename) + print(' label = "%s"' % filename) + for target in targets: + build_file, target_name, toolset = ParseTarget(target) + print(' "%s" [label="%s"]' % (target, target_name)) + print(" }") + + # Now that we've placed all the nodes within subgraphs, output all + # the edges between nodes. + for src, dsts in edges.items(): + for dst in dsts: + print(' "%s" -> "%s"' % (src, dst)) + + print("}") def main(): - if len(sys.argv) < 2: - print(__doc__, file=sys.stderr) - print(file=sys.stderr) - print('usage: %s target1 target2...' % (sys.argv[0]), file=sys.stderr) - return 1 + if len(sys.argv) < 2: + print(__doc__, file=sys.stderr) + print(file=sys.stderr) + print("usage: %s target1 target2..." % (sys.argv[0]), file=sys.stderr) + return 1 - edges = LoadEdges('dump.json', sys.argv[1:]) + edges = LoadEdges("dump.json", sys.argv[1:]) - WriteGraph(edges) - return 0 + WriteGraph(edges) + return 0 -if __name__ == '__main__': - sys.exit(main()) +if __name__ == "__main__": + sys.exit(main()) diff --git a/tools/gyp/tools/pretty_gyp.py b/tools/gyp/tools/pretty_gyp.py index 633048a59ad28c..7313b4fe1b6710 100755 --- a/tools/gyp/tools/pretty_gyp.py +++ b/tools/gyp/tools/pretty_gyp.py @@ -13,7 +13,7 @@ # Regex to remove comments when we're counting braces. -COMMENT_RE = re.compile(r'\s*#.*') +COMMENT_RE = re.compile(r"\s*#.*") # Regex to remove quoted strings when we're counting braces. # It takes into account quoted quotes, and makes sure that the quotes match. @@ -24,45 +24,47 @@ def comment_replace(matchobj): - return matchobj.group(1) + matchobj.group(2) + '#' * len(matchobj.group(3)) + return matchobj.group(1) + matchobj.group(2) + "#" * len(matchobj.group(3)) def mask_comments(input): - """Mask the quoted strings so we skip braces inside quoted strings.""" - search_re = re.compile(r'(.*?)(#)(.*)') - return [search_re.sub(comment_replace, line) for line in input] + """Mask the quoted strings so we skip braces inside quoted strings.""" + search_re = re.compile(r"(.*?)(#)(.*)") + return [search_re.sub(comment_replace, line) for line in input] def quote_replace(matchobj): - return "%s%s%s%s" % (matchobj.group(1), - matchobj.group(2), - 'x'*len(matchobj.group(3)), - matchobj.group(2)) + return "%s%s%s%s" % ( + matchobj.group(1), + matchobj.group(2), + "x" * len(matchobj.group(3)), + matchobj.group(2), + ) def mask_quotes(input): - """Mask the quoted strings so we skip braces inside quoted strings.""" - search_re = re.compile(r'(.*?)' + QUOTE_RE_STR) - return [search_re.sub(quote_replace, line) for line in input] + """Mask the quoted strings so we skip braces inside quoted strings.""" + search_re = re.compile(r"(.*?)" + QUOTE_RE_STR) + return [search_re.sub(quote_replace, line) for line in input] def do_split(input, masked_input, search_re): - output = [] - mask_output = [] - for (line, masked_line) in zip(input, masked_input): - m = search_re.match(masked_line) - while m: - split = len(m.group(1)) - line = line[:split] + r'\n' + line[split:] - masked_line = masked_line[:split] + r'\n' + masked_line[split:] - m = search_re.match(masked_line) - output.extend(line.split(r'\n')) - mask_output.extend(masked_line.split(r'\n')) - return (output, mask_output) + output = [] + mask_output = [] + for (line, masked_line) in zip(input, masked_input): + m = search_re.match(masked_line) + while m: + split = len(m.group(1)) + line = line[:split] + r"\n" + line[split:] + masked_line = masked_line[:split] + r"\n" + masked_line[split:] + m = search_re.match(masked_line) + output.extend(line.split(r"\n")) + mask_output.extend(masked_line.split(r"\n")) + return (output, mask_output) def split_double_braces(input): - """Masks out the quotes and comments, and then splits appropriate + """Masks out the quotes and comments, and then splits appropriate lines (lines that matche the double_*_brace re's above) before indenting them below. @@ -70,88 +72,86 @@ def split_double_braces(input): that the indentation looks prettier when all laid out (e.g. closing braces make a nice diagonal line). """ - double_open_brace_re = re.compile(r'(.*?[\[\{\(,])(\s*)([\[\{\(])') - double_close_brace_re = re.compile(r'(.*?[\]\}\)],?)(\s*)([\]\}\)])') + double_open_brace_re = re.compile(r"(.*?[\[\{\(,])(\s*)([\[\{\(])") + double_close_brace_re = re.compile(r"(.*?[\]\}\)],?)(\s*)([\]\}\)])") - masked_input = mask_quotes(input) - masked_input = mask_comments(masked_input) + masked_input = mask_quotes(input) + masked_input = mask_comments(masked_input) - (output, mask_output) = do_split(input, masked_input, double_open_brace_re) - (output, mask_output) = do_split(output, mask_output, double_close_brace_re) + (output, mask_output) = do_split(input, masked_input, double_open_brace_re) + (output, mask_output) = do_split(output, mask_output, double_close_brace_re) - return output + return output def count_braces(line): - """keeps track of the number of braces on a given line and returns the result. + """keeps track of the number of braces on a given line and returns the result. It starts at zero and subtracts for closed braces, and adds for open braces. """ - open_braces = ['[', '(', '{'] - close_braces = [']', ')', '}'] - closing_prefix_re = re.compile(r'(.*?[^\s\]\}\)]+.*?)([\]\}\)],?)\s*$') - cnt = 0 - stripline = COMMENT_RE.sub(r'', line) - stripline = QUOTE_RE.sub(r"''", stripline) - for char in stripline: - for brace in open_braces: - if char == brace: - cnt += 1 - for brace in close_braces: - if char == brace: - cnt -= 1 - - after = False - if cnt > 0: - after = True - - # This catches the special case of a closing brace having something - # other than just whitespace ahead of it -- we don't want to - # unindent that until after this line is printed so it stays with - # the previous indentation level. - if cnt < 0 and closing_prefix_re.match(stripline): - after = True - return (cnt, after) + open_braces = ["[", "(", "{"] + close_braces = ["]", ")", "}"] + closing_prefix_re = re.compile(r"(.*?[^\s\]\}\)]+.*?)([\]\}\)],?)\s*$") + cnt = 0 + stripline = COMMENT_RE.sub(r"", line) + stripline = QUOTE_RE.sub(r"''", stripline) + for char in stripline: + for brace in open_braces: + if char == brace: + cnt += 1 + for brace in close_braces: + if char == brace: + cnt -= 1 + + after = False + if cnt > 0: + after = True + + # This catches the special case of a closing brace having something + # other than just whitespace ahead of it -- we don't want to + # unindent that until after this line is printed so it stays with + # the previous indentation level. + if cnt < 0 and closing_prefix_re.match(stripline): + after = True + return (cnt, after) def prettyprint_input(lines): - """Does the main work of indenting the input based on the brace counts.""" - indent = 0 - basic_offset = 2 - last_line = "" - for line in lines: - if COMMENT_RE.match(line): - print(line) - else: - line = line.strip('\r\n\t ') # Otherwise doesn't strip \r on Unix. - if len(line) > 0: - (brace_diff, after) = count_braces(line) - if brace_diff != 0: - if after: - print(" " * (basic_offset * indent) + line) - indent += brace_diff - else: - indent += brace_diff - print(" " * (basic_offset * indent) + line) + """Does the main work of indenting the input based on the brace counts.""" + indent = 0 + basic_offset = 2 + for line in lines: + if COMMENT_RE.match(line): + print(line) else: - print(" " * (basic_offset * indent) + line) - else: - print("") - last_line = line + line = line.strip("\r\n\t ") # Otherwise doesn't strip \r on Unix. + if len(line) > 0: + (brace_diff, after) = count_braces(line) + if brace_diff != 0: + if after: + print(" " * (basic_offset * indent) + line) + indent += brace_diff + else: + indent += brace_diff + print(" " * (basic_offset * indent) + line) + else: + print(" " * (basic_offset * indent) + line) + else: + print("") def main(): - if len(sys.argv) > 1: - data = open(sys.argv[1]).read().splitlines() - else: - data = sys.stdin.read().splitlines() - # Split up the double braces. - lines = split_double_braces(data) + if len(sys.argv) > 1: + data = open(sys.argv[1]).read().splitlines() + else: + data = sys.stdin.read().splitlines() + # Split up the double braces. + lines = split_double_braces(data) - # Indent and print the output. - prettyprint_input(lines) - return 0 + # Indent and print the output. + prettyprint_input(lines) + return 0 -if __name__ == '__main__': - sys.exit(main()) +if __name__ == "__main__": + sys.exit(main()) diff --git a/tools/gyp/tools/pretty_sln.py b/tools/gyp/tools/pretty_sln.py index 196566fb9e4470..2b1cb1de747577 100755 --- a/tools/gyp/tools/pretty_sln.py +++ b/tools/gyp/tools/pretty_sln.py @@ -19,153 +19,164 @@ import sys import pretty_vcproj -__author__ = 'nsylvain (Nicolas Sylvain)' +__author__ = "nsylvain (Nicolas Sylvain)" + def BuildProject(project, built, projects, deps): - # if all dependencies are done, we can build it, otherwise we try to build the - # dependency. - # This is not infinite-recursion proof. - for dep in deps[project]: - if dep not in built: - BuildProject(dep, built, projects, deps) - print(project) - built.append(project) + # if all dependencies are done, we can build it, otherwise we try to build the + # dependency. + # This is not infinite-recursion proof. + for dep in deps[project]: + if dep not in built: + BuildProject(dep, built, projects, deps) + print(project) + built.append(project) + def ParseSolution(solution_file): - # All projects, their clsid and paths. - projects = dict() - - # A list of dependencies associated with a project. - dependencies = dict() - - # Regular expressions that matches the SLN format. - # The first line of a project definition. - begin_project = re.compile(r'^Project\("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942' - r'}"\) = "(.*)", "(.*)", "(.*)"$') - # The last line of a project definition. - end_project = re.compile('^EndProject$') - # The first line of a dependency list. - begin_dep = re.compile( - r'ProjectSection\(ProjectDependencies\) = postProject$') - # The last line of a dependency list. - end_dep = re.compile('EndProjectSection$') - # A line describing a dependency. - dep_line = re.compile(' *({.*}) = ({.*})$') - - in_deps = False - solution = open(solution_file) - for line in solution: - results = begin_project.search(line) - if results: - # Hack to remove icu because the diff is too different. - if results.group(1).find('icu') != -1: - continue - # We remove "_gyp" from the names because it helps to diff them. - current_project = results.group(1).replace('_gyp', '') - projects[current_project] = [results.group(2).replace('_gyp', ''), - results.group(3), - results.group(2)] - dependencies[current_project] = [] - continue - - results = end_project.search(line) - if results: - current_project = None - continue - - results = begin_dep.search(line) - if results: - in_deps = True - continue - - results = end_dep.search(line) - if results: - in_deps = False - continue - - results = dep_line.search(line) - if results and in_deps and current_project: - dependencies[current_project].append(results.group(1)) - continue - - # Change all dependencies clsid to name instead. - for project in dependencies: - # For each dependencies in this project - new_dep_array = [] - for dep in dependencies[project]: - # Look for the project name matching this cldis - for project_info in projects: - if projects[project_info][1] == dep: - new_dep_array.append(project_info) - dependencies[project] = sorted(new_dep_array) - - return (projects, dependencies) + # All projects, their clsid and paths. + projects = dict() + + # A list of dependencies associated with a project. + dependencies = dict() + + # Regular expressions that matches the SLN format. + # The first line of a project definition. + begin_project = re.compile( + r'^Project\("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942' + r'}"\) = "(.*)", "(.*)", "(.*)"$' + ) + # The last line of a project definition. + end_project = re.compile("^EndProject$") + # The first line of a dependency list. + begin_dep = re.compile(r"ProjectSection\(ProjectDependencies\) = postProject$") + # The last line of a dependency list. + end_dep = re.compile("EndProjectSection$") + # A line describing a dependency. + dep_line = re.compile(" *({.*}) = ({.*})$") + + in_deps = False + solution = open(solution_file) + for line in solution: + results = begin_project.search(line) + if results: + # Hack to remove icu because the diff is too different. + if results.group(1).find("icu") != -1: + continue + # We remove "_gyp" from the names because it helps to diff them. + current_project = results.group(1).replace("_gyp", "") + projects[current_project] = [ + results.group(2).replace("_gyp", ""), + results.group(3), + results.group(2), + ] + dependencies[current_project] = [] + continue + + results = end_project.search(line) + if results: + current_project = None + continue + + results = begin_dep.search(line) + if results: + in_deps = True + continue + + results = end_dep.search(line) + if results: + in_deps = False + continue + + results = dep_line.search(line) + if results and in_deps and current_project: + dependencies[current_project].append(results.group(1)) + continue + + # Change all dependencies clsid to name instead. + for project in dependencies: + # For each dependencies in this project + new_dep_array = [] + for dep in dependencies[project]: + # Look for the project name matching this cldis + for project_info in projects: + if projects[project_info][1] == dep: + new_dep_array.append(project_info) + dependencies[project] = sorted(new_dep_array) + + return (projects, dependencies) + def PrintDependencies(projects, deps): - print("---------------------------------------") - print("Dependencies for all projects") - print("---------------------------------------") - print("-- --") + print("---------------------------------------") + print("Dependencies for all projects") + print("---------------------------------------") + print("-- --") + + for (project, dep_list) in sorted(deps.items()): + print("Project : %s" % project) + print("Path : %s" % projects[project][0]) + if dep_list: + for dep in dep_list: + print(" - %s" % dep) + print("") - for (project, dep_list) in sorted(deps.items()): - print("Project : %s" % project) - print("Path : %s" % projects[project][0]) - if dep_list: - for dep in dep_list: - print(" - %s" % dep) - print("") + print("-- --") - print("-- --") def PrintBuildOrder(projects, deps): - print("---------------------------------------") - print("Build order ") - print("---------------------------------------") - print("-- --") + print("---------------------------------------") + print("Build order ") + print("---------------------------------------") + print("-- --") - built = [] - for (project, _) in sorted(deps.items()): - if project not in built: - BuildProject(project, built, projects, deps) + built = [] + for (project, _) in sorted(deps.items()): + if project not in built: + BuildProject(project, built, projects, deps) - print("-- --") + print("-- --") -def PrintVCProj(projects): - for project in projects: - print("-------------------------------------") - print("-------------------------------------") - print(project) - print(project) - print(project) - print("-------------------------------------") - print("-------------------------------------") +def PrintVCProj(projects): - project_path = os.path.abspath(os.path.join(os.path.dirname(sys.argv[1]), - projects[project][2])) + for project in projects: + print("-------------------------------------") + print("-------------------------------------") + print(project) + print(project) + print(project) + print("-------------------------------------") + print("-------------------------------------") + + project_path = os.path.abspath( + os.path.join(os.path.dirname(sys.argv[1]), projects[project][2]) + ) + + pretty = pretty_vcproj + argv = [ + "", + project_path, + "$(SolutionDir)=%s\\" % os.path.dirname(sys.argv[1]), + ] + argv.extend(sys.argv[3:]) + pretty.main(argv) - pretty = pretty_vcproj - argv = [ '', - project_path, - '$(SolutionDir)=%s\\' % os.path.dirname(sys.argv[1]), - ] - argv.extend(sys.argv[3:]) - pretty.main(argv) def main(): - # check if we have exactly 1 parameter. - if len(sys.argv) < 2: - print('Usage: %s "c:\\path\\to\\project.sln"' % sys.argv[0]) - return 1 + # check if we have exactly 1 parameter. + if len(sys.argv) < 2: + print('Usage: %s "c:\\path\\to\\project.sln"' % sys.argv[0]) + return 1 - (projects, deps) = ParseSolution(sys.argv[1]) - PrintDependencies(projects, deps) - PrintBuildOrder(projects, deps) + (projects, deps) = ParseSolution(sys.argv[1]) + PrintDependencies(projects, deps) + PrintBuildOrder(projects, deps) - if '--recursive' in sys.argv: - PrintVCProj(projects) - return 0 + if "--recursive" in sys.argv: + PrintVCProj(projects) + return 0 -if __name__ == '__main__': - sys.exit(main()) +if __name__ == "__main__": + sys.exit(main()) diff --git a/tools/gyp/tools/pretty_vcproj.py b/tools/gyp/tools/pretty_vcproj.py index 3212626004778e..b171fae6cf4442 100755 --- a/tools/gyp/tools/pretty_vcproj.py +++ b/tools/gyp/tools/pretty_vcproj.py @@ -20,318 +20,326 @@ from xml.dom.minidom import parse from xml.dom.minidom import Node -__author__ = 'nsylvain (Nicolas Sylvain)' +__author__ = "nsylvain (Nicolas Sylvain)" try: - cmp + cmp except NameError: - def cmp(x, y): - return (x > y) - (x < y) + + def cmp(x, y): + return (x > y) - (x < y) + REPLACEMENTS = dict() ARGUMENTS = None class CmpTuple(object): - """Compare function between 2 tuple.""" - def __call__(self, x, y): - return cmp(x[0], y[0]) + """Compare function between 2 tuple.""" + + def __call__(self, x, y): + return cmp(x[0], y[0]) class CmpNode(object): - """Compare function between 2 xml nodes.""" + """Compare function between 2 xml nodes.""" - def __call__(self, x, y): - def get_string(node): - node_string = "node" - node_string += node.nodeName - if node.nodeValue: - node_string += node.nodeValue + def __call__(self, x, y): + def get_string(node): + node_string = "node" + node_string += node.nodeName + if node.nodeValue: + node_string += node.nodeValue - if node.attributes: - # We first sort by name, if present. - node_string += node.getAttribute("Name") + if node.attributes: + # We first sort by name, if present. + node_string += node.getAttribute("Name") - all_nodes = [] - for (name, value) in node.attributes.items(): - all_nodes.append((name, value)) + all_nodes = [] + for (name, value) in node.attributes.items(): + all_nodes.append((name, value)) - all_nodes.sort(CmpTuple()) - for (name, value) in all_nodes: - node_string += name - node_string += value + all_nodes.sort(CmpTuple()) + for (name, value) in all_nodes: + node_string += name + node_string += value - return node_string + return node_string - return cmp(get_string(x), get_string(y)) + return cmp(get_string(x), get_string(y)) def PrettyPrintNode(node, indent=0): - if node.nodeType == Node.TEXT_NODE: - if node.data.strip(): - print('%s%s' % (' '*indent, node.data.strip())) - return - - if node.childNodes: - node.normalize() - # Get the number of attributes - attr_count = 0 - if node.attributes: - attr_count = node.attributes.length - - # Print the main tag - if attr_count == 0: - print('%s<%s>' % (' '*indent, node.nodeName)) - else: - print('%s<%s' % (' '*indent, node.nodeName)) - - all_attributes = [] - for (name, value) in node.attributes.items(): - all_attributes.append((name, value)) - all_attributes.sort(CmpTuple()) - for (name, value) in all_attributes: - print('%s %s="%s"' % (' '*indent, name, value)) - print('%s>' % (' '*indent)) - if node.nodeValue: - print('%s %s' % (' '*indent, node.nodeValue)) - - for sub_node in node.childNodes: - PrettyPrintNode(sub_node, indent=indent+2) - print('%s' % (' '*indent, node.nodeName)) + if node.nodeType == Node.TEXT_NODE: + if node.data.strip(): + print("%s%s" % (" " * indent, node.data.strip())) + return + + if node.childNodes: + node.normalize() + # Get the number of attributes + attr_count = 0 + if node.attributes: + attr_count = node.attributes.length + + # Print the main tag + if attr_count == 0: + print("%s<%s>" % (" " * indent, node.nodeName)) + else: + print("%s<%s" % (" " * indent, node.nodeName)) + + all_attributes = [] + for (name, value) in node.attributes.items(): + all_attributes.append((name, value)) + all_attributes.sort(CmpTuple()) + for (name, value) in all_attributes: + print('%s %s="%s"' % (" " * indent, name, value)) + print("%s>" % (" " * indent)) + if node.nodeValue: + print("%s %s" % (" " * indent, node.nodeValue)) + + for sub_node in node.childNodes: + PrettyPrintNode(sub_node, indent=indent + 2) + print("%s" % (" " * indent, node.nodeName)) def FlattenFilter(node): - """Returns a list of all the node and sub nodes.""" - node_list = [] + """Returns a list of all the node and sub nodes.""" + node_list = [] - if (node.attributes and - node.getAttribute('Name') == '_excluded_files'): - # We don't add the "_excluded_files" filter. - return [] + if node.attributes and node.getAttribute("Name") == "_excluded_files": + # We don't add the "_excluded_files" filter. + return [] - for current in node.childNodes: - if current.nodeName == 'Filter': - node_list.extend(FlattenFilter(current)) - else: - node_list.append(current) + for current in node.childNodes: + if current.nodeName == "Filter": + node_list.extend(FlattenFilter(current)) + else: + node_list.append(current) - return node_list + return node_list def FixFilenames(filenames, current_directory): - new_list = [] - for filename in filenames: - if filename: - for key in REPLACEMENTS: - filename = filename.replace(key, REPLACEMENTS[key]) - os.chdir(current_directory) - filename = filename.strip('"\' ') - if filename.startswith('$'): - new_list.append(filename) - else: - new_list.append(os.path.abspath(filename)) - return new_list + new_list = [] + for filename in filenames: + if filename: + for key in REPLACEMENTS: + filename = filename.replace(key, REPLACEMENTS[key]) + os.chdir(current_directory) + filename = filename.strip("\"' ") + if filename.startswith("$"): + new_list.append(filename) + else: + new_list.append(os.path.abspath(filename)) + return new_list def AbsoluteNode(node): - """Makes all the properties we know about in this node absolute.""" - if node.attributes: - for (name, value) in node.attributes.items(): - if name in ['InheritedPropertySheets', 'RelativePath', - 'AdditionalIncludeDirectories', - 'IntermediateDirectory', 'OutputDirectory', - 'AdditionalLibraryDirectories']: - # We want to fix up these paths - path_list = value.split(';') - new_list = FixFilenames(path_list, os.path.dirname(ARGUMENTS[1])) - node.setAttribute(name, ';'.join(new_list)) - if not value: - node.removeAttribute(name) + """Makes all the properties we know about in this node absolute.""" + if node.attributes: + for (name, value) in node.attributes.items(): + if name in [ + "InheritedPropertySheets", + "RelativePath", + "AdditionalIncludeDirectories", + "IntermediateDirectory", + "OutputDirectory", + "AdditionalLibraryDirectories", + ]: + # We want to fix up these paths + path_list = value.split(";") + new_list = FixFilenames(path_list, os.path.dirname(ARGUMENTS[1])) + node.setAttribute(name, ";".join(new_list)) + if not value: + node.removeAttribute(name) def CleanupVcproj(node): - """For each sub node, we call recursively this function.""" - for sub_node in node.childNodes: - AbsoluteNode(sub_node) - CleanupVcproj(sub_node) - - # Normalize the node, and remove all extranous whitespaces. - for sub_node in node.childNodes: - if sub_node.nodeType == Node.TEXT_NODE: - sub_node.data = sub_node.data.replace("\r", "") - sub_node.data = sub_node.data.replace("\n", "") - sub_node.data = sub_node.data.rstrip() - - # Fix all the semicolon separated attributes to be sorted, and we also - # remove the dups. - if node.attributes: - for (name, value) in node.attributes.items(): - sorted_list = sorted(value.split(';')) - unique_list = [] - for i in sorted_list: - if not unique_list.count(i): - unique_list.append(i) - node.setAttribute(name, ';'.join(unique_list)) - if not value: - node.removeAttribute(name) - - if node.childNodes: - node.normalize() - - # For each node, take a copy, and remove it from the list. - node_array = [] - while node.childNodes and node.childNodes[0]: - # Take a copy of the node and remove it from the list. - current = node.childNodes[0] - node.removeChild(current) - - # If the child is a filter, we want to append all its children - # to this same list. - if current.nodeName == 'Filter': - node_array.extend(FlattenFilter(current)) - else: - node_array.append(current) - - - # Sort the list. - node_array.sort(CmpNode()) - - # Insert the nodes in the correct order. - for new_node in node_array: - # But don't append empty tool node. - if new_node.nodeName == 'Tool': - if new_node.attributes and new_node.attributes.length == 1: - # This one was empty. - continue - if new_node.nodeName == 'UserMacro': - continue - node.appendChild(new_node) + """For each sub node, we call recursively this function.""" + for sub_node in node.childNodes: + AbsoluteNode(sub_node) + CleanupVcproj(sub_node) + + # Normalize the node, and remove all extraneous whitespaces. + for sub_node in node.childNodes: + if sub_node.nodeType == Node.TEXT_NODE: + sub_node.data = sub_node.data.replace("\r", "") + sub_node.data = sub_node.data.replace("\n", "") + sub_node.data = sub_node.data.rstrip() + + # Fix all the semicolon separated attributes to be sorted, and we also + # remove the dups. + if node.attributes: + for (name, value) in node.attributes.items(): + sorted_list = sorted(value.split(";")) + unique_list = [] + for i in sorted_list: + if not unique_list.count(i): + unique_list.append(i) + node.setAttribute(name, ";".join(unique_list)) + if not value: + node.removeAttribute(name) + + if node.childNodes: + node.normalize() + + # For each node, take a copy, and remove it from the list. + node_array = [] + while node.childNodes and node.childNodes[0]: + # Take a copy of the node and remove it from the list. + current = node.childNodes[0] + node.removeChild(current) + + # If the child is a filter, we want to append all its children + # to this same list. + if current.nodeName == "Filter": + node_array.extend(FlattenFilter(current)) + else: + node_array.append(current) + + # Sort the list. + node_array.sort(CmpNode()) + + # Insert the nodes in the correct order. + for new_node in node_array: + # But don't append empty tool node. + if new_node.nodeName == "Tool": + if new_node.attributes and new_node.attributes.length == 1: + # This one was empty. + continue + if new_node.nodeName == "UserMacro": + continue + node.appendChild(new_node) def GetConfiguationNodes(vcproj): - #TODO(nsylvain): Find a better way to navigate the xml. - nodes = [] - for node in vcproj.childNodes: - if node.nodeName == "Configurations": - for sub_node in node.childNodes: - if sub_node.nodeName == "Configuration": - nodes.append(sub_node) + # TODO(nsylvain): Find a better way to navigate the xml. + nodes = [] + for node in vcproj.childNodes: + if node.nodeName == "Configurations": + for sub_node in node.childNodes: + if sub_node.nodeName == "Configuration": + nodes.append(sub_node) - return nodes + return nodes def GetChildrenVsprops(filename): - dom = parse(filename) - if dom.documentElement.attributes: - vsprops = dom.documentElement.getAttribute('InheritedPropertySheets') - return FixFilenames(vsprops.split(';'), os.path.dirname(filename)) - return [] + dom = parse(filename) + if dom.documentElement.attributes: + vsprops = dom.documentElement.getAttribute("InheritedPropertySheets") + return FixFilenames(vsprops.split(";"), os.path.dirname(filename)) + return [] -def SeekToNode(node1, child2): - # A text node does not have properties. - if child2.nodeType == Node.TEXT_NODE: - return None - # Get the name of the current node. - current_name = child2.getAttribute("Name") - if not current_name: - # There is no name. We don't know how to merge. +def SeekToNode(node1, child2): + # A text node does not have properties. + if child2.nodeType == Node.TEXT_NODE: + return None + + # Get the name of the current node. + current_name = child2.getAttribute("Name") + if not current_name: + # There is no name. We don't know how to merge. + return None + + # Look through all the nodes to find a match. + for sub_node in node1.childNodes: + if sub_node.nodeName == child2.nodeName: + name = sub_node.getAttribute("Name") + if name == current_name: + return sub_node + + # No match. We give up. return None - # Look through all the nodes to find a match. - for sub_node in node1.childNodes: - if sub_node.nodeName == child2.nodeName: - name = sub_node.getAttribute("Name") - if name == current_name: - return sub_node - - # No match. We give up. - return None - def MergeAttributes(node1, node2): - # No attributes to merge? - if not node2.attributes: - return - - for (name, value2) in node2.attributes.items(): - # Don't merge the 'Name' attribute. - if name == 'Name': - continue - value1 = node1.getAttribute(name) - if value1: - # The attribute exist in the main node. If it's equal, we leave it - # untouched, otherwise we concatenate it. - if value1 != value2: - node1.setAttribute(name, ';'.join([value1, value2])) - else: - # The attribute does not exist in the main node. We append this one. - node1.setAttribute(name, value2) - - # If the attribute was a property sheet attributes, we remove it, since - # they are useless. - if name == 'InheritedPropertySheets': - node1.removeAttribute(name) + # No attributes to merge? + if not node2.attributes: + return + + for (name, value2) in node2.attributes.items(): + # Don't merge the 'Name' attribute. + if name == "Name": + continue + value1 = node1.getAttribute(name) + if value1: + # The attribute exist in the main node. If it's equal, we leave it + # untouched, otherwise we concatenate it. + if value1 != value2: + node1.setAttribute(name, ";".join([value1, value2])) + else: + # The attribute does not exist in the main node. We append this one. + node1.setAttribute(name, value2) + + # If the attribute was a property sheet attributes, we remove it, since + # they are useless. + if name == "InheritedPropertySheets": + node1.removeAttribute(name) def MergeProperties(node1, node2): - MergeAttributes(node1, node2) - for child2 in node2.childNodes: - child1 = SeekToNode(node1, child2) - if child1: - MergeProperties(child1, child2) - else: - node1.appendChild(child2.cloneNode(True)) + MergeAttributes(node1, node2) + for child2 in node2.childNodes: + child1 = SeekToNode(node1, child2) + if child1: + MergeProperties(child1, child2) + else: + node1.appendChild(child2.cloneNode(True)) def main(argv): - """Main function of this vcproj prettifier.""" - global ARGUMENTS - ARGUMENTS = argv - - # check if we have exactly 1 parameter. - if len(argv) < 2: - print('Usage: %s "c:\\path\\to\\vcproj.vcproj" [key1=value1] ' - '[key2=value2]' % argv[0]) - return 1 - - # Parse the keys - for i in range(2, len(argv)): - (key, value) = argv[i].split('=') - REPLACEMENTS[key] = value - - # Open the vcproj and parse the xml. - dom = parse(argv[1]) - - # First thing we need to do is find the Configuration Node and merge them - # with the vsprops they include. - for configuration_node in GetConfiguationNodes(dom.documentElement): - # Get the property sheets associated with this configuration. - vsprops = configuration_node.getAttribute('InheritedPropertySheets') - - # Fix the filenames to be absolute. - vsprops_list = FixFilenames(vsprops.strip().split(';'), - os.path.dirname(argv[1])) - - # Extend the list of vsprops with all vsprops contained in the current - # vsprops. - for current_vsprops in vsprops_list: - vsprops_list.extend(GetChildrenVsprops(current_vsprops)) - - # Now that we have all the vsprops, we need to merge them. - for current_vsprops in vsprops_list: - MergeProperties(configuration_node, - parse(current_vsprops).documentElement) - - # Now that everything is merged, we need to cleanup the xml. - CleanupVcproj(dom.documentElement) - - # Finally, we use the prett xml function to print the vcproj back to the - # user. - #print dom.toprettyxml(newl="\n") - PrettyPrintNode(dom.documentElement) - return 0 - - -if __name__ == '__main__': - sys.exit(main(sys.argv)) + """Main function of this vcproj prettifier.""" + global ARGUMENTS + ARGUMENTS = argv + + # check if we have exactly 1 parameter. + if len(argv) < 2: + print( + 'Usage: %s "c:\\path\\to\\vcproj.vcproj" [key1=value1] ' + "[key2=value2]" % argv[0] + ) + return 1 + + # Parse the keys + for i in range(2, len(argv)): + (key, value) = argv[i].split("=") + REPLACEMENTS[key] = value + + # Open the vcproj and parse the xml. + dom = parse(argv[1]) + + # First thing we need to do is find the Configuration Node and merge them + # with the vsprops they include. + for configuration_node in GetConfiguationNodes(dom.documentElement): + # Get the property sheets associated with this configuration. + vsprops = configuration_node.getAttribute("InheritedPropertySheets") + + # Fix the filenames to be absolute. + vsprops_list = FixFilenames( + vsprops.strip().split(";"), os.path.dirname(argv[1]) + ) + + # Extend the list of vsprops with all vsprops contained in the current + # vsprops. + for current_vsprops in vsprops_list: + vsprops_list.extend(GetChildrenVsprops(current_vsprops)) + + # Now that we have all the vsprops, we need to merge them. + for current_vsprops in vsprops_list: + MergeProperties(configuration_node, parse(current_vsprops).documentElement) + + # Now that everything is merged, we need to cleanup the xml. + CleanupVcproj(dom.documentElement) + + # Finally, we use the prett xml function to print the vcproj back to the + # user. + # print dom.toprettyxml(newl="\n") + PrettyPrintNode(dom.documentElement) + return 0 + + +if __name__ == "__main__": + sys.exit(main(sys.argv)) diff --git a/tools/snapshot/node_mksnapshot.cc b/tools/snapshot/node_mksnapshot.cc index 29f9e1cbcbb096..edf5cb72a80b84 100644 --- a/tools/snapshot/node_mksnapshot.cc +++ b/tools/snapshot/node_mksnapshot.cc @@ -17,6 +17,7 @@ int wmain(int argc, wchar_t* argv[]) { #else // UNIX int main(int argc, char* argv[]) { + argv = uv_setup_args(argc, argv); #endif // _WIN32 v8::V8::SetFlagsFromString("--random_seed=42");