blob: b3af5ae0ec5d7db153500978d893b79314683ddb [file] [log] [blame]
Blink Reformat4c46d092018-04-07 15:32:37 +00001// 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 = [];
12for (var config of ['inspector.json', 'devtools_app.json', 'js_app.json', 'node_app.json', 'shell.json', 'worker_app.json'])
13 manifestModules.push(...require(path.resolve(FRONTEND_PATH, config)).modules);
14
15const utils = require('./utils');
16
17const gnPath = path.resolve(__dirname, '..', 'BUILD.gn');
18const gnFile = fs.readFileSync(gnPath, 'utf-8');
19const gnLines = gnFile.split('\n');
20
21function main() {
22 let errors = [];
23 errors = errors.concat(checkNonAutostartNonRemoteModules());
24 errors = errors.concat(checkAllDevToolsFiles());
25 if (errors.length) {
26 console.log('DevTools BUILD.gn checker detected errors!');
27 console.log(`There's an issue with: ${gnPath}`);
28 console.log(errors.join('\n'));
29 process.exit(1);
30 }
31 console.log('DevTools BUILD.gn checker passed');
32}
33
34main();
35
36/**
37 * Ensures that generated module files are in the right list in BUILD.gn.
38 * This is primarily to avoid remote modules from accidentally getting
39 * bundled with the main Chrome binary.
40 */
41function checkNonAutostartNonRemoteModules() {
42 const errors = [];
43 const gnVariable = 'generated_non_autostart_non_remote_modules';
44 const lines = selectGNLines(`${gnVariable} = [`, ']');
45 if (!lines.length) {
46 return [
47 'Could not identify non-autostart non-remote modules in gn file',
48 'Please look at: ' + __filename,
49 ];
50 }
51 const text = lines.join('\n');
52 const modules = manifestModules.filter(m => m.type !== 'autostart' && m.type !== 'remote').map(m => m.name);
53
54 const missingModules = modules.filter(m => !utils.includes(text, `${m}/${m}_module.js`));
55 if (missingModules.length)
56 errors.push(`Check that you've included [${missingModules.join(', ')}] modules in: ` + gnVariable);
57
58 // e.g. "$resources_out_dir/audits/audits_module.js" => "audits"
59 const mapLineToModuleName = line => line.split('/')[2].split('_module')[0];
60
61 const extraneousModules = lines.map(mapLineToModuleName).filter(module => !utils.includes(modules, module));
62 if (extraneousModules.length)
63 errors.push(`Found extraneous modules [${extraneousModules.join(', ')}] in: ` + gnVariable);
64
65 return errors;
66}
67
68/**
69 * Ensures that all source files (according to the various module.json files) are
70 * listed in BUILD.gn.
71 */
72function checkAllDevToolsFiles() {
73 const errors = [];
Amanda Baker06da9152019-03-29 17:04:12 +000074 const excludedFiles = ['InspectorBackendCommands.js', 'SupportedCSSProperties.js', 'ARIAProperties.js', 'axe.js'];
Blink Reformat4c46d092018-04-07 15:32:37 +000075 const gnVariable = 'all_devtools_files';
Jeff Fishera1d0db62019-02-20 18:56:14 +000076 const lines = selectGNLines(`${gnVariable} = [`, ']').map(path.normalize);
Blink Reformat4c46d092018-04-07 15:32:37 +000077 if (!lines.length) {
78 return [
79 'Could not identify all_devtools_files list in gn file',
80 'Please look at: ' + __filename,
81 ];
82 }
83 const gnFiles = new Set(lines);
84 var moduleFiles = [];
85 fs.readdirSync(FRONTEND_PATH).forEach(function(moduleName) {
86 const moduleJSONPath = path.join(FRONTEND_PATH, moduleName, 'module.json');
87 if (utils.isFile(moduleJSONPath)) {
88 const moduleJSON = require(moduleJSONPath);
89 const scripts = moduleJSON.scripts || [];
90 const resources = moduleJSON.resources || [];
Mandy Chen881e9392019-04-22 23:39:44 +000091 const files = ['module.json']
92 .concat(scripts)
93 .concat(resources)
Blink Reformat4c46d092018-04-07 15:32:37 +000094 .map(relativePathFromBuildGN)
95 .filter(file => excludedFiles.every(excludedFile => !file.includes(excludedFile)));
96 moduleFiles = moduleFiles.concat(files);
97
98 function relativePathFromBuildGN(filename) {
99 const relativePath = path.normalize(`front_end/${moduleName}/${filename}`);
100 return `"${relativePath}",`;
101 }
102 }
103 });
104 for (const file of moduleFiles) {
105 if (!gnFiles.has(file))
106 errors.push(`Missing file in BUILD.gn for ${gnVariable}: ` + file);
107 }
108 return errors;
109}
110
111function selectGNLines(startLine, endLine) {
112 let lines = gnLines.map(line => line.trim());
113 let startIndex = lines.indexOf(startLine);
114 if (startIndex === -1)
115 return [];
116 let endIndex = lines.indexOf(endLine, startIndex);
117 if (endIndex === -1)
118 return [];
119 return lines.slice(startIndex + 1, endIndex);
120}