generate-deps-hash.js 1.8 KB

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