From 835718ec30c8960abf8f70da5b8f7b81e3d1fe48 Mon Sep 17 00:00:00 2001 From: Pete Miller Date: Fri, 17 Jan 2020 10:30:44 -0800 Subject: [PATCH] Do not iterate through GRDs for every single script and lib Anything which required Util was also in turn requiring l10nUtil, which performs computations in the module root and has some expectations as to the state of the source tree. This isn't ideal for scripts which run at any time. Whilst the perfect fix would be exporting this functionality as a function to be run on-demand and not in the module root, the fix in this commit reduces the time this occurs by removing the require from Util.js and therefore every other lib and script --- lib/build.js | 3 +- lib/createDist.js | 3 +- lib/updateBranding.js | 164 ++++++++++++++++++++++++++++++++++++++++++ lib/util.js | 160 ----------------------------------------- 4 files changed, 168 insertions(+), 162 deletions(-) create mode 100644 lib/updateBranding.js diff --git a/lib/build.js b/lib/build.js index b7df3c2ffb..1c906ecd2a 100644 --- a/lib/build.js +++ b/lib/build.js @@ -1,6 +1,7 @@ const config = require('../lib/config') const licensing = require('../lib/licensing') const util = require('../lib/util') +const updateBranding = require('../lib/updateBranding') const path = require('path') const fs = require('fs-extra') @@ -93,7 +94,7 @@ const build = (buildConfig = config.defaultBuildConfig, options) => { touchOverriddenFiles() touchOverriddenVectorIconFiles() - util.updateBranding() + updateBranding() if (buildConfig === 'Release') { licensing.updateLicenses() } diff --git a/lib/createDist.js b/lib/createDist.js index 2510d3c99a..9cebd6dfee 100644 --- a/lib/createDist.js +++ b/lib/createDist.js @@ -1,5 +1,6 @@ const config = require('../lib/config') const util = require('../lib/util') +const updateBranding = require('../lib/updateBranding') const path = require('path') const fs = require('fs-extra') @@ -13,7 +14,7 @@ const createDist = (buildConfig = config.defaultBuildConfig, options) => { notary_password = config.notary_password } - util.updateBranding() + updateBranding() fs.removeSync(path.join(config.outputDir, 'dist')) config.buildTarget = 'create_dist' util.buildTarget() diff --git a/lib/updateBranding.js b/lib/updateBranding.js new file mode 100644 index 0000000000..4b67b1c7b3 --- /dev/null +++ b/lib/updateBranding.js @@ -0,0 +1,164 @@ +const path = require('path') +const config = require('./config') +const fs = require('fs-extra') +const util = require('./util') +const autoGeneratedBraveToChromiumMapping = Object.assign({}, require('./l10nUtil').autoGeneratedBraveToChromiumMapping) + +module.exports = function UpdateBranding () { + console.log('update branding...') + const chromeComponentsDir = path.join(config.srcDir, 'components') + const braveComponentsDir = path.join(config.projects['brave-core'].dir, 'components') + const chromeAppDir = path.join(config.srcDir, 'chrome', 'app') + const braveAppDir = path.join(config.projects['brave-core'].dir, 'app') + const chromeBrowserResourcesDir = path.join(config.srcDir, 'chrome', 'browser', 'resources') + const braveBrowserResourcesDir = path.join(config.projects['brave-core'].dir, 'browser', 'resources') + const braveAppVectorIconsDir = path.join(config.projects['brave-core'].dir, 'vector_icons', 'chrome', 'app') + const chromeAndroidJavaStringsTranslationsDir = path.join(config.srcDir, 'chrome', 'android', 'java', 'strings', 'translations') + const braveAndroidJavaStringsTranslationsDir = path.join(config.projects['brave-core'].dir, 'android', 'java', 'strings', 'translations') + + let fileMap = new Set(); + // The following 3 entries we map to the same name, not the chromium equivalent name for copying back + autoGeneratedBraveToChromiumMapping[path.join(braveAppDir, 'brave_strings.grd')] = path.join(chromeAppDir, 'brave_strings.grd') + autoGeneratedBraveToChromiumMapping[path.join(braveAppDir, 'settings_brave_strings.grdp')] = path.join(chromeAppDir, 'settings_brave_strings.grdp') + autoGeneratedBraveToChromiumMapping[path.join(braveComponentsDir, 'components_brave_strings.grd')] = path.join(chromeComponentsDir, 'components_brave_strings.grd') + + Object.entries(autoGeneratedBraveToChromiumMapping).forEach(mapping => fileMap.add(mapping)) + + // Copy xtb files for: + // brave/app/resources/chromium_strings*.xtb + // brave/app/resources/generated_resoruces*.xtb + // brave/components/strings/components_chromium_strings*.xtb + // brave/android/java/strings/translations/android_chrome_strings*.xtb + fileMap.add([path.join(braveAppDir, 'resources'), path.join(chromeAppDir, 'resources')]) + fileMap.add([path.join(braveComponentsDir, 'strings'), path.join(chromeComponentsDir, 'strings')]) + fileMap.add([braveAndroidJavaStringsTranslationsDir, chromeAndroidJavaStringsTranslationsDir]) + // By overwriting, we don't need to modify some grd files. + fileMap.add([path.join(braveAppDir, 'theme', 'brave'), path.join(chromeAppDir, 'theme', 'brave')]) + fileMap.add([path.join(braveAppDir, 'theme', 'brave'), path.join(chromeAppDir, 'theme', 'chromium')]) + fileMap.add([path.join(braveAppDir, 'theme', 'default_100_percent', 'brave'), path.join(chromeAppDir, 'theme', 'default_100_percent', 'brave')]) + fileMap.add([path.join(braveAppDir, 'theme', 'default_200_percent', 'brave'), path.join(chromeAppDir, 'theme', 'default_200_percent', 'brave')]) + fileMap.add([path.join(braveAppDir, 'theme', 'default_100_percent', 'brave'), path.join(chromeAppDir, 'theme', 'default_100_percent', 'chromium')]) + fileMap.add([path.join(braveAppDir, 'theme', 'default_200_percent', 'brave'), path.join(chromeAppDir, 'theme', 'default_200_percent', 'chromium')]) + fileMap.add([path.join(braveAppDir, 'theme', 'default_100_percent', 'common'), path.join(chromeAppDir, 'theme', 'default_100_percent', 'common')]) + fileMap.add([path.join(braveAppDir, 'theme', 'default_200_percent', 'common'), path.join(chromeAppDir, 'theme', 'default_200_percent', 'common')]) + fileMap.add([path.join(braveComponentsDir, 'resources', 'default_100_percent', 'brave'), path.join(chromeComponentsDir, 'resources', 'default_100_percent', 'chromium')]) + fileMap.add([path.join(braveComponentsDir, 'resources', 'default_200_percent', 'brave'), path.join(chromeComponentsDir, 'resources', 'default_200_percent', 'chromium')]) + fileMap.add([path.join(braveAppVectorIconsDir, 'vector_icons', 'brave'), path.join(chromeAppDir, 'vector_icons', 'brave')]) + // Copy chrome-logo-faded.png for replacing chrome logo of welcome page with brave's on Win8. + fileMap.add([path.join(braveBrowserResourcesDir, 'chrome-logo-faded.png'), path.join(chromeBrowserResourcesDir, 'chrome-logo-faded.png')]) + // Copy to make our ${branding_path_component}_behaviors.cc + fileMap.add([path.join(config.projects['brave-core'].dir, 'chromium_src', 'chrome', 'installer', 'setup', 'brave_behaviors.cc'), + path.join(config.srcDir, 'chrome', 'installer', 'setup', 'brave_behaviors.cc')]) + + for (const [source, output] of fileMap) { + if (!fs.existsSync(source)) { + console.warn(`Warning: The following file-system entry was not found for copying contents to a chromium destination: ${source}. Consider removing the entry from the file-map, or investigating whether the correct source code reference is checked out.`) + continue + } + + let sourceFiles = [] + + // get all the files if source if a directory + if (fs.statSync(source).isDirectory()) { + sourceFiles = util.walkSync(source) + } else { + sourceFiles = [source] + } + + for (const sourceFile of sourceFiles) { + let destinationFile = path.join(output, path.relative(source, sourceFile)) + + // The destination file might be newer when updating chromium so + // we check for an exact match on the timestamp. We use seconds instead + // of ms because utimesSync doesn't set the times with ms precision + if (!fs.existsSync(destinationFile) || + Math.floor(new Date(fs.statSync(sourceFile).mtimeMs).getTime() / 1000) != + Math.floor(new Date(fs.statSync(destinationFile).mtimeMs).getTime() / 1000)) { + fs.copySync(sourceFile, destinationFile) + // can't set the date in the past so update the source file + // to match the newly copied destionation file + const date = fs.statSync(destinationFile).mtime + fs.utimesSync(sourceFile, date, date) + console.log(sourceFile + ' copied to ' + destinationFile) + } + } + } + + if (process.platform === 'darwin') { + // Copy proper mac app icon for channel to chrome/app/theme/mac/app.icns. + // Each channel's app icons are stored in brave/app/theme/$channel/app.icns. + // With this copying, we don't need to modify chrome/BUILD.gn for this. + const iconSource = path.join(braveAppDir, 'theme', 'brave', 'mac', config.channel, 'app.icns') + const iconDest = path.join(chromeAppDir, 'theme', 'brave', 'mac', 'app.icns') + if (!fs.existsSync(iconDest) || + util.calculateFileChecksum(iconSource) != util.calculateFileChecksum(iconDest)) { + console.log('copy app icon') + fs.copySync(iconSource, iconDest) + } + + // Copy branding file + let branding_file_name = 'BRANDING' + if (config.channel) + branding_file_name = branding_file_name + '.' + config.channel + + const brandingSource = path.join(braveAppDir, 'theme', 'brave', branding_file_name) + const brandingDest = path.join(chromeAppDir, 'theme', 'brave', 'BRANDING') + if (!fs.existsSync(brandingDest) || + util.calculateFileChecksum(brandingSource) != util.calculateFileChecksum(brandingDest)) { + console.log('copy branding file') + fs.copySync(brandingSource, brandingDest) + } + } + if (config.targetOS === 'android') { + + let androidIconSet = '' + if (config.channel === 'development') { + androidIconSet = 'res_brave_default' + } else if (config.channel === '') { + androidIconSet = 'res_brave' + } else if (config.channel === 'beta') { + androidIconSet = 'res_brave_beta' + } else if (config.channel === 'dev') { + androidIconSet = 'res_brave_dev' + } else if (config.channel === 'nightly') { + androidIconSet = 'res_brave_nightly' + } + + const androidIconSource = path.join(braveAppDir, 'theme', 'brave', 'android', androidIconSet) + const androidIconDest = path.join(config.srcDir, 'chrome', 'android', 'java', 'res_chromium') + const androidResSource = path.join(config.projects['brave-core'].dir, 'android', 'java', 'res') + const androidResDest = path.join(config.srcDir, 'chrome', 'android', 'java', 'res') + const androidResNightSource = path.join(config.projects['brave-core'].dir, 'android', 'java', 'res_night') + const androidResNightDest = path.join(config.srcDir, 'chrome', 'android', 'java', 'res_night') + const androidNtpTilesResSource = path.join(config.projects['brave-core'].dir, 'components', 'ntp_tiles', 'resources') + const androidNtpTilesResDest = path.join(config.srcDir, 'components', 'ntp_tiles', 'resources') + const androidResTemplateSource = path.join(config.projects['brave-core'].dir, 'android', 'java', 'res_template') + const androidResTemplateDest = path.join(config.srcDir, 'chrome', 'android', 'java', 'res_template') + + // Mapping for copying Brave's Android resource into chromium folder. + const copyAndroidResourceMapping = { + [androidIconSource]: androidIconDest, + [androidResSource]: androidResDest, + [androidResNightSource]: androidResNightDest, + [androidNtpTilesResSource]: androidNtpTilesResDest, + [androidResTemplateSource]: androidResTemplateDest + } + + console.log('copy Android app icons and app resources') + Object.entries(copyAndroidResourceMapping).map(([sourcePath, destPath]) => { + let androidSourceFiles = [] + if (fs.statSync(sourcePath).isDirectory()) { + androidSourceFiles = util.walkSync(sourcePath) + } else { + androidSourceFiles = [sourcePath] + } + + for (const androidSourceFile of androidSourceFiles) { + let destinationFile = path.join(destPath, path.relative(sourcePath, androidSourceFile)) + if (!fs.existsSync(destinationFile) || util.calculateFileChecksum(androidSourceFile) != util.calculateFileChecksum(destinationFile)) { + fs.copySync(androidSourceFile, destinationFile) + } + } + }) + } +} \ No newline at end of file diff --git a/lib/util.js b/lib/util.js index 41a2229259..eb3e47cc7b 100755 --- a/lib/util.js +++ b/lib/util.js @@ -3,7 +3,6 @@ const { spawn, spawnSync } = require('child_process') const config = require('./config') const fs = require('fs-extra') const crypto = require('crypto') -const autoGeneratedBraveToChromiumMapping = Object.assign({}, require('./l10nUtil').autoGeneratedBraveToChromiumMapping) const os = require('os') const fixPywin32 = (options = {}) => { @@ -147,165 +146,6 @@ const util = { return md5.digest('hex') }, - updateBranding: () => { - console.log('update branding...') - const chromeComponentsDir = path.join(config.srcDir, 'components') - const braveComponentsDir = path.join(config.projects['brave-core'].dir, 'components') - const chromeAppDir = path.join(config.srcDir, 'chrome', 'app') - const braveAppDir = path.join(config.projects['brave-core'].dir, 'app') - const chromeBrowserResourcesDir = path.join(config.srcDir, 'chrome', 'browser', 'resources') - const braveBrowserResourcesDir = path.join(config.projects['brave-core'].dir, 'browser', 'resources') - const braveAppVectorIconsDir = path.join(config.projects['brave-core'].dir, 'vector_icons', 'chrome', 'app') - const chromeAndroidJavaStringsTranslationsDir = path.join(config.srcDir, 'chrome', 'android', 'java', 'strings', 'translations') - const braveAndroidJavaStringsTranslationsDir = path.join(config.projects['brave-core'].dir, 'android', 'java', 'strings', 'translations') - - let fileMap = new Set(); - // The following 3 entries we map to the same name, not the chromium equivalent name for copying back - autoGeneratedBraveToChromiumMapping[path.join(braveAppDir, 'brave_strings.grd')] = path.join(chromeAppDir, 'brave_strings.grd') - autoGeneratedBraveToChromiumMapping[path.join(braveAppDir, 'settings_brave_strings.grdp')] = path.join(chromeAppDir, 'settings_brave_strings.grdp') - autoGeneratedBraveToChromiumMapping[path.join(braveComponentsDir, 'components_brave_strings.grd')] = path.join(chromeComponentsDir, 'components_brave_strings.grd') - - Object.entries(autoGeneratedBraveToChromiumMapping).forEach(mapping => fileMap.add(mapping)) - - // Copy xtb files for: - // brave/app/resources/chromium_strings*.xtb - // brave/app/resources/generated_resoruces*.xtb - // brave/components/strings/components_chromium_strings*.xtb - // brave/android/java/strings/translations/android_chrome_strings*.xtb - fileMap.add([path.join(braveAppDir, 'resources'), path.join(chromeAppDir, 'resources')]) - fileMap.add([path.join(braveComponentsDir, 'strings'), path.join(chromeComponentsDir, 'strings')]) - fileMap.add([braveAndroidJavaStringsTranslationsDir, chromeAndroidJavaStringsTranslationsDir]) - // By overwriting, we don't need to modify some grd files. - fileMap.add([path.join(braveAppDir, 'theme', 'brave'), path.join(chromeAppDir, 'theme', 'brave')]) - fileMap.add([path.join(braveAppDir, 'theme', 'brave'), path.join(chromeAppDir, 'theme', 'chromium')]) - fileMap.add([path.join(braveAppDir, 'theme', 'default_100_percent', 'brave'), path.join(chromeAppDir, 'theme', 'default_100_percent', 'brave')]) - fileMap.add([path.join(braveAppDir, 'theme', 'default_200_percent', 'brave'), path.join(chromeAppDir, 'theme', 'default_200_percent', 'brave')]) - fileMap.add([path.join(braveAppDir, 'theme', 'default_100_percent', 'brave'), path.join(chromeAppDir, 'theme', 'default_100_percent', 'chromium')]) - fileMap.add([path.join(braveAppDir, 'theme', 'default_200_percent', 'brave'), path.join(chromeAppDir, 'theme', 'default_200_percent', 'chromium')]) - fileMap.add([path.join(braveAppDir, 'theme', 'default_100_percent', 'common'), path.join(chromeAppDir, 'theme', 'default_100_percent', 'common')]) - fileMap.add([path.join(braveAppDir, 'theme', 'default_200_percent', 'common'), path.join(chromeAppDir, 'theme', 'default_200_percent', 'common')]) - fileMap.add([path.join(braveComponentsDir, 'resources', 'default_100_percent', 'brave'), path.join(chromeComponentsDir, 'resources', 'default_100_percent', 'chromium')]) - fileMap.add([path.join(braveComponentsDir, 'resources', 'default_200_percent', 'brave'), path.join(chromeComponentsDir, 'resources', 'default_200_percent', 'chromium')]) - fileMap.add([path.join(braveAppVectorIconsDir, 'vector_icons', 'brave'), path.join(chromeAppDir, 'vector_icons', 'brave')]) - // Copy chrome-logo-faded.png for replacing chrome logo of welcome page with brave's on Win8. - fileMap.add([path.join(braveBrowserResourcesDir, 'chrome-logo-faded.png'), path.join(chromeBrowserResourcesDir, 'chrome-logo-faded.png')]) - // Copy to make our ${branding_path_component}_behaviors.cc - fileMap.add([path.join(config.projects['brave-core'].dir, 'chromium_src', 'chrome', 'installer', 'setup', 'brave_behaviors.cc'), - path.join(config.srcDir, 'chrome', 'installer', 'setup', 'brave_behaviors.cc')]) - - for (const [source, output] of fileMap) { - if (!fs.existsSync(source)) { - console.warn(`Warning: The following file-system entry was not found for copying contents to a chromium destination: ${source}. Consider removing the entry from the file-map, or investigating whether the correct source code reference is checked out.`) - continue - } - - let sourceFiles = [] - - // get all the files if source if a directory - if (fs.statSync(source).isDirectory()) { - sourceFiles = util.walkSync(source) - } else { - sourceFiles = [source] - } - - for (const sourceFile of sourceFiles) { - let destinationFile = path.join(output, path.relative(source, sourceFile)) - - // The destination file might be newer when updating chromium so - // we check for an exact match on the timestamp. We use seconds instead - // of ms because utimesSync doesn't set the times with ms precision - if (!fs.existsSync(destinationFile) || - Math.floor(new Date(fs.statSync(sourceFile).mtimeMs).getTime() / 1000) != - Math.floor(new Date(fs.statSync(destinationFile).mtimeMs).getTime() / 1000)) { - fs.copySync(sourceFile, destinationFile) - // can't set the date in the past so update the source file - // to match the newly copied destionation file - const date = fs.statSync(destinationFile).mtime - fs.utimesSync(sourceFile, date, date) - console.log(sourceFile + ' copied to ' + destinationFile) - } - } - } - - if (process.platform === 'darwin') { - // Copy proper mac app icon for channel to chrome/app/theme/mac/app.icns. - // Each channel's app icons are stored in brave/app/theme/$channel/app.icns. - // With this copying, we don't need to modify chrome/BUILD.gn for this. - const iconSource = path.join(braveAppDir, 'theme', 'brave', 'mac', config.channel, 'app.icns') - const iconDest = path.join(chromeAppDir, 'theme', 'brave', 'mac', 'app.icns') - if (!fs.existsSync(iconDest) || - util.calculateFileChecksum(iconSource) != util.calculateFileChecksum(iconDest)) { - console.log('copy app icon') - fs.copySync(iconSource, iconDest) - } - - // Copy branding file - let branding_file_name = 'BRANDING' - if (config.channel) - branding_file_name = branding_file_name + '.' + config.channel - - const brandingSource = path.join(braveAppDir, 'theme', 'brave', branding_file_name) - const brandingDest = path.join(chromeAppDir, 'theme', 'brave', 'BRANDING') - if (!fs.existsSync(brandingDest) || - util.calculateFileChecksum(brandingSource) != util.calculateFileChecksum(brandingDest)) { - console.log('copy branding file') - fs.copySync(brandingSource, brandingDest) - } - } - if (config.targetOS === 'android') { - - let androidIconSet = '' - if (config.channel === 'development') { - androidIconSet = 'res_brave_default' - } else if (config.channel === '') { - androidIconSet = 'res_brave' - } else if (config.channel === 'beta') { - androidIconSet = 'res_brave_beta' - } else if (config.channel === 'dev') { - androidIconSet = 'res_brave_dev' - } else if (config.channel === 'nightly') { - androidIconSet = 'res_brave_nightly' - } - - const androidIconSource = path.join(braveAppDir, 'theme', 'brave', 'android', androidIconSet) - const androidIconDest = path.join(config.srcDir, 'chrome', 'android', 'java', 'res_chromium') - const androidResSource = path.join(config.projects['brave-core'].dir, 'android', 'java', 'res') - const androidResDest = path.join(config.srcDir, 'chrome', 'android', 'java', 'res') - const androidResNightSource = path.join(config.projects['brave-core'].dir, 'android', 'java', 'res_night') - const androidResNightDest = path.join(config.srcDir, 'chrome', 'android', 'java', 'res_night') - const androidNtpTilesResSource = path.join(config.projects['brave-core'].dir, 'components', 'ntp_tiles', 'resources') - const androidNtpTilesResDest = path.join(config.srcDir, 'components', 'ntp_tiles', 'resources') - const androidResTemplateSource = path.join(config.projects['brave-core'].dir, 'android', 'java', 'res_template') - const androidResTemplateDest = path.join(config.srcDir, 'chrome', 'android', 'java', 'res_template') - - // Mapping for copying Brave's Android resource into chromium folder. - const copyAndroidResourceMapping = { - [androidIconSource]: androidIconDest, - [androidResSource]: androidResDest, - [androidResNightSource]: androidResNightDest, - [androidNtpTilesResSource]: androidNtpTilesResDest, - [androidResTemplateSource]: androidResTemplateDest - } - - console.log('copy Android app icons and app resources') - Object.entries(copyAndroidResourceMapping).map(([sourcePath, destPath]) => { - let androidSourceFiles = [] - if (fs.statSync(sourcePath).isDirectory()) { - androidSourceFiles = util.walkSync(sourcePath) - } else { - androidSourceFiles = [sourcePath] - } - - for (const androidSourceFile of androidSourceFiles) { - let destinationFile = path.join(destPath, path.relative(sourcePath, androidSourceFile)) - if (!fs.existsSync(destinationFile) || util.calculateFileChecksum(androidSourceFile) != util.calculateFileChecksum(destinationFile)) { - fs.copySync(androidSourceFile, destinationFile) - } - } - }) - } - }, - // Chromium compares pre-installed midl files and generated midl files from IDL during the build to check integrity. // Generated files during the build time and upstream pre-installed files are different because we use different IDL file. // So, we should copy our pre-installed files to overwrite upstream pre-installed files.