FormatSource.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #!/usr/bin/python3
  2. # Copyright (C) 2009-2022, 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 shutil
  13. import platform
  14. file_extensions = ["h", "hpp", "c", "cpp", "glsl", "hlsl", "ankiprog"]
  15. directories = ["AnKi", "Tests", "Sandbox", "Tools", "Samples"]
  16. hlsl_semantics = ["TEXCOORD", "SV_POSITION", "SV_TARGET0",
  17. "SV_TARGET", "SV_DISPATCHTHREADID", "SV_GROUPINDEX", "SV_GROUPID"]
  18. def thread_callback(tid):
  19. """ Call clang-format """
  20. global mutex
  21. global file_names
  22. while True:
  23. mutex.acquire()
  24. if len(file_names) > 0:
  25. file_name = file_names.pop()
  26. else:
  27. file_name = None
  28. mutex.release()
  29. if file_name is None:
  30. break
  31. unused, file_extension = os.path.splitext(file_name)
  32. is_shader = file_extension == ".hlsl" or file_extension == ".ankiprog"
  33. if is_shader:
  34. # Read all text
  35. file = open(file_name, mode="r", newline="\n")
  36. file_txt = file.read()
  37. file.close()
  38. original_file_hash = hash(file_txt)
  39. # Replace all semantics
  40. for semantic in hlsl_semantics:
  41. file_txt = file_txt.replace(": " + semantic, "__" + semantic)
  42. # Write the new file
  43. tmp_filefd, tmp_filename = tempfile.mkstemp()
  44. with open(tmp_filename, "w", newline="\n") as f:
  45. f.write(file_txt)
  46. os.close(tmp_filefd)
  47. orig_filename = file_name
  48. file_name = tmp_filename
  49. style_file = "--style=file:.clang-format-hlsl"
  50. else:
  51. style_file = "--style=file:.clang-format"
  52. if platform.system() == "Linux":
  53. exe = "./ThirdParty/Bin/Linux64/clang-format"
  54. else:
  55. exe = "./ThirdParty/Bin/Windows64/clang-format.exe"
  56. subprocess.check_call([exe, "-sort-includes=false", style_file, "-i", file_name])
  57. if is_shader:
  58. # Read tmp file
  59. file = open(tmp_filename, mode="r", newline="\n")
  60. file_txt = file.read()
  61. file.close()
  62. # Replace all semantics
  63. for semantic in hlsl_semantics:
  64. file_txt = file_txt.replace("__" + semantic, ": " + semantic)
  65. new_file_hash = hash(file_txt)
  66. # Write formatted file
  67. if new_file_hash != original_file_hash:
  68. file = open(orig_filename, mode="w", newline="\n")
  69. file.write(file_txt)
  70. file.close()
  71. # Cleanup
  72. os.remove(tmp_filename)
  73. # Gather the filenames
  74. file_names = []
  75. for directory in directories:
  76. for extension in file_extensions:
  77. file_names.extend(glob.glob("./" + directory + "/**/*." + extension, recursive=True))
  78. file_name_count = len(file_names)
  79. # Start the threads
  80. mutex = threading.Lock()
  81. thread_count = multiprocessing.cpu_count()
  82. threads = []
  83. for i in range(0, thread_count):
  84. thread = threading.Thread(target=thread_callback, args=(i,))
  85. threads.append(thread)
  86. thread.start()
  87. # Join the threads
  88. for i in range(0, thread_count):
  89. threads[i].join()
  90. print("Done! Formatted %d files" % file_name_count)