generate-deps-hash.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. const crypto = require('crypto');
  2. const fs = require('fs');
  3. const path = require('path');
  4. // Fallback to blow away old cache keys
  5. const HASH_VERSION = 3;
  6. // Base files to hash
  7. const filesToHash = [
  8. path.resolve(__dirname, '../DEPS'),
  9. path.resolve(__dirname, '../yarn.lock'),
  10. path.resolve(__dirname, '../script/sysroots.json')
  11. ];
  12. const addAllFiles = (dir) => {
  13. for (const child of fs.readdirSync(dir).sort()) {
  14. const childPath = path.resolve(dir, child);
  15. if (fs.statSync(childPath).isDirectory()) {
  16. addAllFiles(childPath);
  17. } else {
  18. filesToHash.push(childPath);
  19. }
  20. }
  21. };
  22. // Add all patch files to the hash
  23. addAllFiles(path.resolve(__dirname, '../patches'));
  24. // Create Hash
  25. const hasher = crypto.createHash('SHA256');
  26. hasher.update(`HASH_VERSION:${HASH_VERSION}`);
  27. for (const file of filesToHash) {
  28. hasher.update(fs.readFileSync(file));
  29. }
  30. // Add the GCLIENT_EXTRA_ARGS variable to the hash
  31. const extraArgs = process.env.GCLIENT_EXTRA_ARGS || 'no_extra_args';
  32. hasher.update(extraArgs);
  33. const effectivePlatform = extraArgs.includes('host_os=mac') ? 'darwin' : process.platform;
  34. // Write the hash to disk
  35. fs.writeFileSync(path.resolve(__dirname, '../.depshash'), hasher.digest('hex'));
  36. 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}`;
  37. const argsDir = path.resolve(__dirname, '../build/args');
  38. for (const argFile of fs.readdirSync(argsDir).sort()) {
  39. targetContent += `\n${argFile}--${crypto.createHash('SHA1').update(fs.readFileSync(path.resolve(argsDir, argFile))).digest('hex')}`;
  40. }
  41. fs.writeFileSync(path.resolve(__dirname, '../.depshash-target'), targetContent);