generate-deps-hash.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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 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. hasher.update(`HASH_VERSION:${HASH_VERSIONS[process.platform] || FALLBACK_HASH_VERSION}`);
  33. for (const file of filesToHash) {
  34. hasher.update(fs.readFileSync(file));
  35. }
  36. // Add the GCLIENT_EXTRA_ARGS variable to the hash
  37. const extraArgs = process.env.GCLIENT_EXTRA_ARGS || 'no_extra_args';
  38. hasher.update(extraArgs);
  39. const effectivePlatform = extraArgs.includes('host_os=mac') ? 'darwin' : process.platform;
  40. // Write the hash to disk
  41. fs.writeFileSync(path.resolve(__dirname, '../.depshash'), hasher.digest('hex'));
  42. 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}`;
  43. const argsDir = path.resolve(__dirname, '../build/args');
  44. for (const argFile of fs.readdirSync(argsDir).sort()) {
  45. targetContent += `\n${argFile}--${crypto.createHash('SHA1').update(fs.readFileSync(path.resolve(argsDir, argFile))).digest('hex')}`;
  46. }
  47. fs.writeFileSync(path.resolve(__dirname, '../.depshash-target'), targetContent);