build.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #!/usr/bin/env python3
  2. from collections import OrderedDict
  3. import json
  4. import os
  5. import sys
  6. dir_path = os.path.dirname(os.path.realpath(__file__))
  7. SENTINEL = "dL7pKGdnNz796PbbjQWNKmHXBZaB9tsX"
  8. TEMPLATE_H = """
  9. #ifndef ELECTRON_FUSES_H_
  10. #define ELECTRON_FUSES_H_
  11. #if defined(WIN32)
  12. #define FUSE_EXPORT __declspec(dllexport)
  13. #else
  14. #define FUSE_EXPORT __attribute__((visibility("default")))
  15. #endif
  16. namespace electron {
  17. namespace fuses {
  18. extern const volatile char kFuseWire[];
  19. {getters}
  20. } // namespace fuses
  21. } // namespace electron
  22. #endif // ELECTRON_FUSES_H_
  23. """
  24. TEMPLATE_CC = """
  25. #include "electron/fuses.h"
  26. namespace electron {
  27. namespace fuses {
  28. const volatile char kFuseWire[] = { /* sentinel */ {sentinel}, /* fuse_version */ {fuse_version}, /* fuse_wire_length */ {fuse_wire_length}, /* fuse_wire */ {initial_config}};
  29. {getters}
  30. }
  31. }
  32. """
  33. with open(os.path.join(dir_path, "fuses.json5"), 'r') as f:
  34. fuse_defaults = json.loads(''.join(line for line in f.readlines() if not line.strip()[0] == "/"), object_pairs_hook=OrderedDict)
  35. fuse_version = fuse_defaults['_version']
  36. del fuse_defaults['_version']
  37. del fuse_defaults['_schema']
  38. del fuse_defaults['_comment']
  39. if fuse_version >= pow(2, 8):
  40. raise Exception("Fuse version can not exceed one byte in size")
  41. fuses = fuse_defaults.keys()
  42. initial_config = ""
  43. getters_h = ""
  44. getters_cc = ""
  45. index = len(SENTINEL) + 1
  46. for fuse in fuses:
  47. index += 1
  48. initial_config += fuse_defaults[fuse]
  49. name = ''.join(word.title() for word in fuse.split('_'))
  50. getters_h += "FUSE_EXPORT bool Is{name}Enabled();\n".replace("{name}", name)
  51. getters_cc += """
  52. bool Is{name}Enabled() {
  53. return kFuseWire[{index}] == '1';
  54. }
  55. """.replace("{name}", name).replace("{index}", str(index))
  56. def c_hex(n):
  57. s = hex(n)[2:]
  58. return "0x" + s.rjust(2, '0')
  59. def hex_arr(s):
  60. arr = []
  61. for char in s:
  62. arr.append(c_hex(ord(char)))
  63. return ",".join(arr)
  64. header = TEMPLATE_H.replace("{getters}", getters_h.strip())
  65. impl = TEMPLATE_CC.replace("{sentinel}", hex_arr(SENTINEL))
  66. impl = impl.replace("{fuse_version}", c_hex(fuse_version))
  67. impl = impl.replace("{fuse_wire_length}", c_hex(len(fuses)))
  68. impl = impl.replace("{initial_config}", hex_arr(initial_config))
  69. impl = impl.replace("{getters}", getters_cc.strip())
  70. with open(sys.argv[1], 'w') as f:
  71. f.write(header)
  72. with open(sys.argv[2], 'w') as f:
  73. f.write(impl)