blob: 53ee8659e0839b78f235050e0d9e1af6039884c4 [file] [log] [blame]
Blink Reformat4c46d092018-04-07 15:32:371// 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
6const fs = require('fs');
7const path = require('path');
8
9const FRONTEND_PATH = path.resolve(__dirname, '..', 'front_end');
10
11const manifestModules = [];
Tim van der Lippe7b347ca2021-04-09 15:59:2112for (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 Lippeba26b2b2020-03-11 14:40:0014}
Blink Reformat4c46d092018-04-07 15:32:3715
Blink Reformat4c46d092018-04-07 15:32:3716const gnPath = path.resolve(__dirname, '..', 'BUILD.gn');
17const gnFile = fs.readFileSync(gnPath, 'utf-8');
18const gnLines = gnFile.split('\n');
19
Blink Reformat4c46d092018-04-07 15:32:3720/**
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 */
25function checkNonAutostartNonRemoteModules() {
26 const errors = [];
Tim van der Lippe30103ef2021-02-03 13:31:0327 const gnVariable = 'non_autostart_non_remote_modules';
Blink Reformat4c46d092018-04-07 15:32:3728 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 Lippef5feb1f2020-09-21 11:37:5536 const modules = manifestModules.filter(m => m.type !== 'autostart').map(m => m.name);
Blink Reformat4c46d092018-04-07 15:32:3737
Tim van der Lippe4fea78d2021-03-12 17:44:4538 const missingModules = modules.filter(m => !text.includes(`${m}/${path.basename(m)}_module.js`));
Tim van der Lippeba26b2b2020-03-11 14:40:0039 if (missingModules.length) {
Blink Reformat4c46d092018-04-07 15:32:3740 errors.push(`Check that you've included [${missingModules.join(', ')}] modules in: ` + gnVariable);
Tim van der Lippeba26b2b2020-03-11 14:40:0041 }
Blink Reformat4c46d092018-04-07 15:32:3742
Tim van der Lippe8b179312021-02-05 15:13:5443 // e.g. "lighthouse/lighthouse_module.js" => "lighthouse"
Tim van der Lippe4fea78d2021-03-12 17:44:4544 const mapLineToModuleName = line => path.dirname(line.substring(1));
Blink Reformat4c46d092018-04-07 15:32:3745
Tim van der Lipped25ae402020-03-26 16:04:5146 const extraneousModules = lines.map(mapLineToModuleName).filter(module => !modules.includes(module));
Tim van der Lippeba26b2b2020-03-11 14:40:0047 if (extraneousModules.length) {
Blink Reformat4c46d092018-04-07 15:32:3748 errors.push(`Found extraneous modules [${extraneousModules.join(', ')}] in: ` + gnVariable);
Tim van der Lippeba26b2b2020-03-11 14:40:0049 }
Blink Reformat4c46d092018-04-07 15:32:3750
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 */
58function checkAllDevToolsFiles() {
Jan Schefflerf3277b22020-07-28 10:51:4459 return checkGNVariable('all_devtools_files', 'all_devtools_files', moduleJSON => {
Tim van der Lippeac961dd2019-12-16 13:32:4660 const resources = moduleJSON.resources || [];
61 return [
62 'module.json',
Tim van der Lippeac961dd2019-12-16 13:32:4663 ...resources,
64 ];
Tim van der Lippe334be382020-07-13 14:35:5865 });
Tim van der Lippeac961dd2019-12-16 13:32:4666}
67
Jan Schefflerf3277b22020-07-28 10:51:4468function checkGNVariable(fileName, gnVariable, obtainFiles, obtainRelativePath) {
Tim van der Lippe732be582021-04-09 15:58:5569 const filePath = path.resolve(__dirname, '..', 'config', 'gni', `${fileName}.gni`);
Tim van der Lippe334be382020-07-13 14:35:5870 const fileContent = fs.readFileSync(filePath, 'utf-8');
71 const linesToCheck = fileContent.split('\n');
72
Blink Reformat4c46d092018-04-07 15:32:3773 const errors = [];
vidorteg1fd76f82020-08-26 18:09:2074 const excludedFiles =
Tim van der Lippe7256e5e2021-04-14 15:00:4475 ['axe.js', 'entrypoints/formatter_worker/', 'third_party/lighthouse/', 'third_party/i18n/'].map(path.normalize);
Tim van der Lippe5822c1a2020-07-13 14:00:5176 const lines = selectGNLines(`${gnVariable} = [`, ']', linesToCheck).map(path.normalize);
Blink Reformat4c46d092018-04-07 15:32:3777 if (!lines.length) {
78 return [
Tim van der Lippeac961dd2019-12-16 13:32:4679 `Could not identify ${gnVariable} list in gn file`,
Blink Reformat4c46d092018-04-07 15:32:3780 'Please look at: ' + __filename,
81 ];
82 }
83 const gnFiles = new Set(lines);
Tim van der Lippeba26b2b2020-03-11 14:40:0084 let moduleFiles = [];
Blink Reformat4c46d092018-04-07 15:32:3785
Tim van der Lippe8bdbc7a2020-03-09 10:42:0986 function addModuleFilesForDirectory(moduleJSONPath, buildGNPath, folderName) {
Tim van der Lippe383de762020-01-13 16:53:0687 const moduleJSON = require(moduleJSONPath);
Tim van der Lippe8bdbc7a2020-03-09 10:42:0988 const files = obtainFiles(moduleJSON, folderName)
Tim van der Lippe383de762020-01-13 16:53:0689 .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 Reformat4c46d092018-04-07 15:32:3796 }
Tim van der Lippe383de762020-01-13 16:53:0697 }
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 Lipped25ae402020-03-26 16:04:51104 if (fs.existsSync(moduleJSONPath)) {
Tim van der Lippe8bdbc7a2020-03-09 10:42:09105 addModuleFilesForDirectory(moduleJSONPath, buildGNPath, path.basename(folderName));
Tim van der Lippe383de762020-01-13 16:53:06106 }
107
Tim van der Lippeba26b2b2020-03-11 14:40:00108 fs.readdirSync(folderName).forEach(nestedModuleName => {
Tim van der Lippe383de762020-01-13 16:53:06109 traverseDirectoriesForModuleJSONFiles(
110 path.join(folderName, nestedModuleName), `${buildGNPath}/${nestedModuleName}`);
111 });
112 }
113
Tim van der Lippeba26b2b2020-03-11 14:40:00114 fs.readdirSync(FRONTEND_PATH).forEach(moduleName => {
Tim van der Lippe383de762020-01-13 16:53:06115 traverseDirectoriesForModuleJSONFiles(path.join(FRONTEND_PATH, moduleName), moduleName);
Blink Reformat4c46d092018-04-07 15:32:37116 });
Tim van der Lippe383de762020-01-13 16:53:06117
Blink Reformat4c46d092018-04-07 15:32:37118 for (const file of moduleFiles) {
Tim van der Lippeba26b2b2020-03-11 14:40:00119 if (!gnFiles.has(file)) {
Blink Reformat4c46d092018-04-07 15:32:37120 errors.push(`Missing file in BUILD.gn for ${gnVariable}: ` + file);
Tim van der Lippeba26b2b2020-03-11 14:40:00121 }
Blink Reformat4c46d092018-04-07 15:32:37122 }
Tim van der Lippe383de762020-01-13 16:53:06123
Blink Reformat4c46d092018-04-07 15:32:37124 return errors;
125}
126
Tim van der Lippe5822c1a2020-07-13 14:00:51127function selectGNLines(startLine, endLine, linesToCheck = gnLines) {
128 const lines = linesToCheck.map(line => line.trim());
Tim van der Lippeba26b2b2020-03-11 14:40:00129 const startIndex = lines.indexOf(startLine);
130 if (startIndex === -1) {
Blink Reformat4c46d092018-04-07 15:32:37131 return [];
Tim van der Lippeba26b2b2020-03-11 14:40:00132 }
133 const endIndex = lines.indexOf(endLine, startIndex);
134 if (endIndex === -1) {
Blink Reformat4c46d092018-04-07 15:32:37135 return [];
Tim van der Lippeba26b2b2020-03-11 14:40:00136 }
Blink Reformat4c46d092018-04-07 15:32:37137 return lines.slice(startIndex + 1, endIndex);
138}
Tim van der Lippe3c976572020-07-10 12:15:33139
140function main() {
141 const errors = [
142 ...checkNonAutostartNonRemoteModules(),
143 ...checkAllDevToolsFiles(),
Tim van der Lippe3c976572020-07-10 12:15:33144 ];
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
154main();