featuregen.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import subprocess
  2. import tempfile
  3. import os
  4. import sys
  5. archs = [
  6. ("amd64", "linux_amd64", "x86_64-pc-linux-gnu", [], []),
  7. ("i386", "linux_i386", "i386-pc-linux-gnu", [], []),
  8. ("arm32", "linux_arm32", "arm-linux-gnu", [], []),
  9. ("arm64", "linux_arm64", "aarch64-linux-elf", [], []),
  10. ("wasm32", "js_wasm32", "wasm32-js-js", [], []),
  11. ("wasm64p32", "js_wasm64p32","wasm32-js-js", [], []),
  12. ];
  13. SEEKING_CPUS = 0
  14. PARSING_CPUS = 1
  15. PARSING_FEATURES = 2
  16. with tempfile.NamedTemporaryFile(suffix=".odin", delete=True) as temp_file:
  17. temp_file.write(b"package main\n")
  18. for arch, target, triple, cpus, features in archs:
  19. cmd = ["odin", "build", temp_file.name, "-file", "-build-mode:llvm", "-out:temp", "-target-features:\"help\"", f"-target:\"{target}\""]
  20. process = subprocess.Popen(cmd, stderr=subprocess.PIPE, text=True)
  21. state = SEEKING_CPUS
  22. for line in process.stderr:
  23. if state == SEEKING_CPUS:
  24. if line == "Available CPUs for this target:\n":
  25. state = PARSING_CPUS
  26. elif state == PARSING_CPUS:
  27. if line == "Available features for this target:\n":
  28. state = PARSING_FEATURES
  29. continue
  30. parts = line.split(" -", maxsplit=1)
  31. if len(parts) < 2:
  32. continue
  33. cpu = parts[0].strip()
  34. cpus.append(cpu)
  35. elif state == PARSING_FEATURES:
  36. if line == "\n" and len(features) > 0:
  37. break
  38. parts = line.split(" -", maxsplit=1)
  39. if len(parts) < 2:
  40. continue
  41. feature = parts[0].strip()
  42. features.append(feature)
  43. process.wait()
  44. if process.returncode != 0:
  45. print(f"odin build returned with non-zero exit code {process.returncode}")
  46. sys.exit(1)
  47. os.remove("temp.ll")
  48. def print_default_features(triple, microarch):
  49. cmd = ["./featuregen", triple, microarch]
  50. process = subprocess.Popen(cmd, stdout=subprocess.PIPE, text=True)
  51. first = True
  52. for line in process.stdout:
  53. print("" if first else ",", line.strip(), sep="", end="")
  54. first = False
  55. process.wait()
  56. if process.returncode != 0:
  57. print(f"featuregen returned with non-zero exit code {process.returncode}")
  58. sys.exit(1)
  59. print("// Generated with the featuregen script in `misc/featuregen`")
  60. print("gb_global String target_microarch_list[TargetArch_COUNT] = {")
  61. print("\t// TargetArch_Invalid:")
  62. print('\tstr_lit(""),')
  63. for arch, target, triple, cpus, features in archs:
  64. print(f"\t// TargetArch_{arch}:")
  65. print(f'\tstr_lit("{','.join(cpus)}"),')
  66. print("};")
  67. print("")
  68. print("// Generated with the featuregen script in `misc/featuregen`")
  69. print("gb_global String target_features_list[TargetArch_COUNT] = {")
  70. print("\t// TargetArch_Invalid:")
  71. print('\tstr_lit(""),')
  72. for arch, target, triple, cpus, features in archs:
  73. print(f"\t// TargetArch_{arch}:")
  74. print(f'\tstr_lit("{','.join(features)}"),')
  75. print("};")
  76. print("")
  77. print("// Generated with the featuregen script in `misc/featuregen`")
  78. print("gb_global int target_microarch_counts[TargetArch_COUNT] = {")
  79. print("\t// TargetArch_Invalid:")
  80. print("\t0,")
  81. for arch, target, triple, cpus, feature in archs:
  82. print(f"\t// TargetArch_{arch}:")
  83. print(f"\t{len(cpus)},")
  84. print("};")
  85. print("")
  86. print("// Generated with the featuregen script in `misc/featuregen`")
  87. print("gb_global MicroarchFeatureList microarch_features_list[] = {")
  88. for arch, target, triple, cpus, features in archs:
  89. print(f"\t// TargetArch_{arch}:")
  90. for cpu in cpus:
  91. print(f'\t{{ str_lit("{cpu}"), str_lit("', end="")
  92. print_default_features(triple, cpu)
  93. print('") },')
  94. print("};")