FormatSource.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #!/usr/bin/python3
  2. # Copyright (C) 2009-present, Panagiotis Christopoulos Charitos and contributors.
  3. # All rights reserved.
  4. # Code licensed under the BSD License.
  5. # http://www.anki3d.org/LICENSE
  6. import glob
  7. import subprocess
  8. import threading
  9. import multiprocessing
  10. import os
  11. import tempfile
  12. import platform
  13. file_extensions = ["h", "hpp", "c", "cpp", "glsl", "hlsl", "ankiprog"]
  14. directories = ["AnKi", "Tests", "Sandbox", "Tools", "Samples"]
  15. hlsl_semantics = ["TEXCOORD", "SV_POSITION", "SV_TARGET0", "SV_TARGET1", "SV_TARGET2", "SV_TARGET3", "SV_TARGET4", "SV_TARGET5",
  16. "SV_TARGET6", "SV_TARGET7", "SV_DISPATCHTHREADID", "SV_GROUPINDEX", "SV_GROUPID", "SV_GROUPTHREADID"]
  17. hlsl_attribs = ["[shader(\"closesthit\")]", "[shader(\"anyhit\")]", "[shader(\"raygeneration\")]", "[shader(\"miss\")]",
  18. "[raypayload]", "[outputtopology(\"triangle\")]"]
  19. hlsl_attribs_fake = ["______shaderclosesthit", "______shaderanyhit", "______shaderraygeneration", "______shadermiss",
  20. "[[raypaylo]]", "______outputtopology_triangle"]
  21. def thread_callback(tid):
  22. """ Call clang-format """
  23. global mutex
  24. global file_names
  25. while True:
  26. mutex.acquire()
  27. if len(file_names) > 0:
  28. file_name = file_names.pop()
  29. else:
  30. file_name = None
  31. mutex.release()
  32. if file_name is None:
  33. break
  34. unused, file_extension = os.path.splitext(file_name)
  35. is_shader = file_extension == ".hlsl" or file_extension == ".ankiprog"
  36. if is_shader:
  37. # Read all text
  38. file = open(file_name, mode="r", newline="\n")
  39. file_txt = file.read()
  40. file.close()
  41. original_file_hash = hash(file_txt)
  42. # Replace all semantics
  43. for semantic in hlsl_semantics:
  44. file_txt = file_txt.replace(": " + semantic, "__" + semantic)
  45. for i in range(0, len(hlsl_attribs)):
  46. file_txt = file_txt.replace(hlsl_attribs[i], hlsl_attribs_fake[i])
  47. # Write the new file
  48. tmp_filefd, tmp_filename = tempfile.mkstemp(suffix=".txt")
  49. with open(tmp_filename, "w", newline="\n") as f:
  50. f.write(file_txt)
  51. os.close(tmp_filefd)
  52. orig_filename = file_name
  53. file_name = tmp_filename
  54. style_file = "--style=file:.clang-format-hlsl"
  55. else:
  56. style_file = "--style=file:.clang-format"
  57. if platform.system() == "Linux":
  58. exe = "./ThirdParty/Bin/Linux64/clang-format"
  59. else:
  60. exe = "./ThirdParty/Bin/Windows64/clang-format.exe"
  61. subprocess.check_call([exe, "-sort-includes=false", style_file, "-i", file_name])
  62. if is_shader:
  63. # Read tmp file
  64. file = open(tmp_filename, mode="r", newline="\n")
  65. file_txt = file.read()
  66. file.close()
  67. # Replace all semantics
  68. for semantic in hlsl_semantics:
  69. file_txt = file_txt.replace("__" + semantic, ": " + semantic)
  70. for i in range(0, len(hlsl_attribs)):
  71. file_txt = file_txt.replace(hlsl_attribs_fake[i], hlsl_attribs[i])
  72. new_file_hash = hash(file_txt)
  73. # Write formatted file
  74. if new_file_hash != original_file_hash:
  75. file = open(orig_filename, mode="w", newline="\n")
  76. file.write(file_txt)
  77. file.close()
  78. # Cleanup
  79. os.remove(tmp_filename)
  80. # Gather the filenames
  81. file_names = []
  82. for directory in directories:
  83. for extension in file_extensions:
  84. file_names.extend(glob.glob("./" + directory + "/**/*." + extension, recursive=True))
  85. file_name_count = len(file_names)
  86. # Start the threads
  87. mutex = threading.Lock()
  88. thread_count = multiprocessing.cpu_count()
  89. threads = []
  90. for i in range(0, thread_count):
  91. thread = threading.Thread(target=thread_callback, args=(i,))
  92. threads.append(thread)
  93. thread.start()
  94. # Join the threads
  95. for i in range(0, thread_count):
  96. threads[i].join()
  97. print("Done! Formatted %d files" % file_name_count)