generate-deps-hash.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. const crypto = require('node:crypto');
  2. const fs = require('node:fs');
  3. const path = require('node:path');
  4. // Fallback to blow away old cache keys
  5. const FALLBACK_HASH_VERSION = 3;
  6. // Per platform hash versions to bust the cache on different platforms
  7. const HASH_VERSIONS = {
  8. darwin: 3,
  9. win32: 4,
  10. linux: 3
  11. };
  12. // Base files to hash
  13. const filesToHash = [
  14. path.resolve(__dirname, '../DEPS'),
  15. path.resolve(__dirname, '../yarn.lock'),
  16. path.resolve(__dirname, '../script/sysroots.json')
  17. ];
  18. const addAllFiles = (dir) => {
  19. for (const child of fs.readdirSync(dir).sort()) {
  20. const childPath = path.resolve(dir, child);
  21. if (fs.statSync(childPath).isDirectory()) {
  22. addAllFiles(childPath);
  23. } else {
  24. filesToHash.push(childPath);
  25. }
  26. }
  27. };
  28. // Add all patch files to the hash
  29. addAllFiles(path.resolve(__dirname, '../patches'));
  30. // Create Hash
  31. const hasher = crypto.createHash('SHA256');
  32. const addToHashAndLog = (s) => {
  33. console.log('Hashing:', s);
  34. return hasher.update(s);
  35. };
  36. addToHashAndLog(`HASH_VERSION:${HASH_VERSIONS[process.platform] || FALLBACK_HASH_VERSION}`);
  37. for (const file of filesToHash) {
  38. console.log('Hashing Content:', file, crypto.createHash('SHA256').update(fs.readFileSync(file)).digest('hex'));
  39. hasher.update(fs.readFileSync(file));
  40. }
  41. // Add the GCLIENT_EXTRA_ARGS variable to the hash
  42. const extraArgs = process.env.GCLIENT_EXTRA_ARGS || 'no_extra_args';
  43. addToHashAndLog(extraArgs);
  44. const effectivePlatform = extraArgs.includes('host_os=mac') ? 'darwin' : process.platform;
  45. // Write the hash to disk
  46. fs.writeFileSync(path.resolve(__dirname, '../.depshash'), hasher.digest('hex'));
  47. let targetContent = `${effectivePlatform}\n${process.env.TARGET_ARCH}\n${process.env.GN_CONFIG}\n${undefined}\n${process.env.GN_EXTRA_ARGS}\n${process.env.GN_BUILDFLAG_ARGS}`;
  48. const argsDir = path.resolve(__dirname, '../build/args');
  49. for (const argFile of fs.readdirSync(argsDir).sort()) {
  50. targetContent += `\n${argFile}--${crypto.createHash('SHA1').update(fs.readFileSync(path.resolve(argsDir, argFile))).digest('hex')}`;
  51. }
  52. fs.writeFileSync(path.resolve(__dirname, '../.depshash-target'), targetContent);