Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1 | // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | 'use strict'; |
| 5 | |
| 6 | const fs = require('fs'); |
| 7 | const path = require('path'); |
| 8 | |
| 9 | const FRONTEND_PATH = path.resolve(__dirname, '..', 'front_end'); |
| 10 | |
| 11 | const manifestModules = []; |
Tim van der Lippe | 7b347ca | 2021-04-09 15:59:21 | [diff] [blame] | 12 | for (const config of ['inspector', 'devtools_app', 'js_app', 'node_app', 'shell', 'worker_app']) { |
| 13 | manifestModules.push(...require(path.resolve(FRONTEND_PATH, 'entrypoints', config, `${config}.json`)).modules); |
Tim van der Lippe | ba26b2b | 2020-03-11 14:40:00 | [diff] [blame] | 14 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 15 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 16 | const gnPath = path.resolve(__dirname, '..', 'BUILD.gn'); |
| 17 | const gnFile = fs.readFileSync(gnPath, 'utf-8'); |
| 18 | const gnLines = gnFile.split('\n'); |
| 19 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 20 | /** |
| 21 | * Ensures that generated module files are in the right list in BUILD.gn. |
| 22 | * This is primarily to avoid remote modules from accidentally getting |
| 23 | * bundled with the main Chrome binary. |
| 24 | */ |
| 25 | function checkNonAutostartNonRemoteModules() { |
| 26 | const errors = []; |
Tim van der Lippe | 30103ef | 2021-02-03 13:31:03 | [diff] [blame] | 27 | const gnVariable = 'non_autostart_non_remote_modules'; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 28 | const lines = selectGNLines(`${gnVariable} = [`, ']'); |
| 29 | if (!lines.length) { |
| 30 | return [ |
| 31 | 'Could not identify non-autostart non-remote modules in gn file', |
| 32 | 'Please look at: ' + __filename, |
| 33 | ]; |
| 34 | } |
| 35 | const text = lines.join('\n'); |
Tim van der Lippe | f5feb1f | 2020-09-21 11:37:55 | [diff] [blame] | 36 | const modules = manifestModules.filter(m => m.type !== 'autostart').map(m => m.name); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 37 | |
Tim van der Lippe | 4fea78d | 2021-03-12 17:44:45 | [diff] [blame] | 38 | const missingModules = modules.filter(m => !text.includes(`${m}/${path.basename(m)}_module.js`)); |
Tim van der Lippe | ba26b2b | 2020-03-11 14:40:00 | [diff] [blame] | 39 | if (missingModules.length) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 40 | errors.push(`Check that you've included [${missingModules.join(', ')}] modules in: ` + gnVariable); |
Tim van der Lippe | ba26b2b | 2020-03-11 14:40:00 | [diff] [blame] | 41 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 42 | |
Tim van der Lippe | 8b17931 | 2021-02-05 15:13:54 | [diff] [blame] | 43 | // e.g. "lighthouse/lighthouse_module.js" => "lighthouse" |
Tim van der Lippe | 4fea78d | 2021-03-12 17:44:45 | [diff] [blame] | 44 | const mapLineToModuleName = line => path.dirname(line.substring(1)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 45 | |
Tim van der Lippe | d25ae40 | 2020-03-26 16:04:51 | [diff] [blame] | 46 | const extraneousModules = lines.map(mapLineToModuleName).filter(module => !modules.includes(module)); |
Tim van der Lippe | ba26b2b | 2020-03-11 14:40:00 | [diff] [blame] | 47 | if (extraneousModules.length) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 48 | errors.push(`Found extraneous modules [${extraneousModules.join(', ')}] in: ` + gnVariable); |
Tim van der Lippe | ba26b2b | 2020-03-11 14:40:00 | [diff] [blame] | 49 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 50 | |
| 51 | return errors; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Ensures that all source files (according to the various module.json files) are |
| 56 | * listed in BUILD.gn. |
| 57 | */ |
| 58 | function checkAllDevToolsFiles() { |
Jan Scheffler | f3277b2 | 2020-07-28 10:51:44 | [diff] [blame] | 59 | return checkGNVariable('all_devtools_files', 'all_devtools_files', moduleJSON => { |
Tim van der Lippe | ac961dd | 2019-12-16 13:32:46 | [diff] [blame] | 60 | const resources = moduleJSON.resources || []; |
| 61 | return [ |
| 62 | 'module.json', |
Tim van der Lippe | ac961dd | 2019-12-16 13:32:46 | [diff] [blame] | 63 | ...resources, |
| 64 | ]; |
Tim van der Lippe | 334be38 | 2020-07-13 14:35:58 | [diff] [blame] | 65 | }); |
Tim van der Lippe | ac961dd | 2019-12-16 13:32:46 | [diff] [blame] | 66 | } |
| 67 | |
Jan Scheffler | f3277b2 | 2020-07-28 10:51:44 | [diff] [blame] | 68 | function checkGNVariable(fileName, gnVariable, obtainFiles, obtainRelativePath) { |
Tim van der Lippe | 732be58 | 2021-04-09 15:58:55 | [diff] [blame] | 69 | const filePath = path.resolve(__dirname, '..', 'config', 'gni', `${fileName}.gni`); |
Tim van der Lippe | 334be38 | 2020-07-13 14:35:58 | [diff] [blame] | 70 | const fileContent = fs.readFileSync(filePath, 'utf-8'); |
| 71 | const linesToCheck = fileContent.split('\n'); |
| 72 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 73 | const errors = []; |
vidorteg | 1fd76f8 | 2020-08-26 18:09:20 | [diff] [blame] | 74 | const excludedFiles = |
Tim van der Lippe | 7256e5e | 2021-04-14 15:00:44 | [diff] [blame] | 75 | ['axe.js', 'entrypoints/formatter_worker/', 'third_party/lighthouse/', 'third_party/i18n/'].map(path.normalize); |
Tim van der Lippe | 5822c1a | 2020-07-13 14:00:51 | [diff] [blame] | 76 | const lines = selectGNLines(`${gnVariable} = [`, ']', linesToCheck).map(path.normalize); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 77 | if (!lines.length) { |
| 78 | return [ |
Tim van der Lippe | ac961dd | 2019-12-16 13:32:46 | [diff] [blame] | 79 | `Could not identify ${gnVariable} list in gn file`, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 80 | 'Please look at: ' + __filename, |
| 81 | ]; |
| 82 | } |
| 83 | const gnFiles = new Set(lines); |
Tim van der Lippe | ba26b2b | 2020-03-11 14:40:00 | [diff] [blame] | 84 | let moduleFiles = []; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 85 | |
Tim van der Lippe | 8bdbc7a | 2020-03-09 10:42:09 | [diff] [blame] | 86 | function addModuleFilesForDirectory(moduleJSONPath, buildGNPath, folderName) { |
Tim van der Lippe | 383de76 | 2020-01-13 16:53:06 | [diff] [blame] | 87 | const moduleJSON = require(moduleJSONPath); |
Tim van der Lippe | 8bdbc7a | 2020-03-09 10:42:09 | [diff] [blame] | 88 | const files = obtainFiles(moduleJSON, folderName) |
Tim van der Lippe | 383de76 | 2020-01-13 16:53:06 | [diff] [blame] | 89 | .map(obtainRelativePath && obtainRelativePath(buildGNPath) || relativePathFromBuildGN) |
| 90 | .filter(file => excludedFiles.every(excludedFile => !file.includes(excludedFile))); |
| 91 | moduleFiles = moduleFiles.concat(files); |
| 92 | |
| 93 | function relativePathFromBuildGN(filename) { |
| 94 | const relativePath = path.normalize(`front_end/${buildGNPath}/${filename}`); |
| 95 | return `"${relativePath}",`; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 96 | } |
Tim van der Lippe | 383de76 | 2020-01-13 16:53:06 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | function traverseDirectoriesForModuleJSONFiles(folderName, buildGNPath) { |
| 100 | if (!fs.lstatSync(folderName).isDirectory()) { |
| 101 | return; |
| 102 | } |
| 103 | const moduleJSONPath = path.join(folderName, 'module.json'); |
Tim van der Lippe | d25ae40 | 2020-03-26 16:04:51 | [diff] [blame] | 104 | if (fs.existsSync(moduleJSONPath)) { |
Tim van der Lippe | 8bdbc7a | 2020-03-09 10:42:09 | [diff] [blame] | 105 | addModuleFilesForDirectory(moduleJSONPath, buildGNPath, path.basename(folderName)); |
Tim van der Lippe | 383de76 | 2020-01-13 16:53:06 | [diff] [blame] | 106 | } |
| 107 | |
Tim van der Lippe | ba26b2b | 2020-03-11 14:40:00 | [diff] [blame] | 108 | fs.readdirSync(folderName).forEach(nestedModuleName => { |
Tim van der Lippe | 383de76 | 2020-01-13 16:53:06 | [diff] [blame] | 109 | traverseDirectoriesForModuleJSONFiles( |
| 110 | path.join(folderName, nestedModuleName), `${buildGNPath}/${nestedModuleName}`); |
| 111 | }); |
| 112 | } |
| 113 | |
Tim van der Lippe | ba26b2b | 2020-03-11 14:40:00 | [diff] [blame] | 114 | fs.readdirSync(FRONTEND_PATH).forEach(moduleName => { |
Tim van der Lippe | 383de76 | 2020-01-13 16:53:06 | [diff] [blame] | 115 | traverseDirectoriesForModuleJSONFiles(path.join(FRONTEND_PATH, moduleName), moduleName); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 116 | }); |
Tim van der Lippe | 383de76 | 2020-01-13 16:53:06 | [diff] [blame] | 117 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 118 | for (const file of moduleFiles) { |
Tim van der Lippe | ba26b2b | 2020-03-11 14:40:00 | [diff] [blame] | 119 | if (!gnFiles.has(file)) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 120 | errors.push(`Missing file in BUILD.gn for ${gnVariable}: ` + file); |
Tim van der Lippe | ba26b2b | 2020-03-11 14:40:00 | [diff] [blame] | 121 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 122 | } |
Tim van der Lippe | 383de76 | 2020-01-13 16:53:06 | [diff] [blame] | 123 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 124 | return errors; |
| 125 | } |
| 126 | |
Tim van der Lippe | 5822c1a | 2020-07-13 14:00:51 | [diff] [blame] | 127 | function selectGNLines(startLine, endLine, linesToCheck = gnLines) { |
| 128 | const lines = linesToCheck.map(line => line.trim()); |
Tim van der Lippe | ba26b2b | 2020-03-11 14:40:00 | [diff] [blame] | 129 | const startIndex = lines.indexOf(startLine); |
| 130 | if (startIndex === -1) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 131 | return []; |
Tim van der Lippe | ba26b2b | 2020-03-11 14:40:00 | [diff] [blame] | 132 | } |
| 133 | const endIndex = lines.indexOf(endLine, startIndex); |
| 134 | if (endIndex === -1) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 135 | return []; |
Tim van der Lippe | ba26b2b | 2020-03-11 14:40:00 | [diff] [blame] | 136 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 137 | return lines.slice(startIndex + 1, endIndex); |
| 138 | } |
Tim van der Lippe | 3c97657 | 2020-07-10 12:15:33 | [diff] [blame] | 139 | |
| 140 | function main() { |
| 141 | const errors = [ |
| 142 | ...checkNonAutostartNonRemoteModules(), |
| 143 | ...checkAllDevToolsFiles(), |
Tim van der Lippe | 3c97657 | 2020-07-10 12:15:33 | [diff] [blame] | 144 | ]; |
| 145 | if (errors.length) { |
| 146 | console.log('DevTools BUILD.gn checker detected errors!'); |
| 147 | console.log(`There's an issue with: ${gnPath}`); |
| 148 | console.log(errors.join('\n')); |
| 149 | process.exit(1); |
| 150 | } |
| 151 | console.log('DevTools BUILD.gn checker passed'); |
| 152 | } |
| 153 | |
| 154 | main(); |