FormatSource.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. # Replace all semantics
  39. for semantic in hlsl_semantics:
  40. file_txt = file_txt.replace(": " + semantic, "__" + semantic)
  41. # Write the new file
  42. tmp_filefd, tmp_filename = tempfile.mkstemp()
  43. with open(tmp_filename, "w", newline="\n") as f:
  44. f.write(file_txt)
  45. os.close(tmp_filefd)
  46. orig_filename = file_name
  47. file_name = tmp_filename
  48. style_file = "--style=file:.clang-format-hlsl"
  49. else:
  50. style_file = "--style=file:.clang-format"
  51. if platform.system() == "Linux":
  52. exe = "./ThirdParty/Bin/Linux64/clang-format"
  53. else:
  54. exe = "./ThirdParty/Bin/Windows64/clang-format.exe"
  55. subprocess.check_call([exe, "-sort-includes=false", style_file, "-i", file_name])
  56. if is_shader:
  57. shutil.move(tmp_filename, orig_filename)
  58. file_name = orig_filename
  59. # Read all text
  60. file = open(file_name, mode="r", newline="\n")
  61. file_txt = file.read()
  62. file.close()
  63. # Replace all semantics
  64. for semantic in hlsl_semantics:
  65. file_txt = file_txt.replace("__" + semantic, ": " + semantic)
  66. # Write the new file
  67. file = open(file_name, mode="w", newline="\n")
  68. file.write(file_txt)
  69. file.close()
  70. # Gather the filenames
  71. file_names = []
  72. for directory in directories:
  73. for extension in file_extensions:
  74. file_names.extend(glob.glob("./" + directory + "/**/*." + extension, recursive=True))
  75. file_name_count = len(file_names)
  76. # Start the threads
  77. mutex = threading.Lock()
  78. thread_count = multiprocessing.cpu_count()
  79. threads = []
  80. for i in range(0, thread_count):
  81. thread = threading.Thread(target=thread_callback, args=(i,))
  82. threads.append(thread)
  83. thread.start()
  84. # Join the threads
  85. for i in range(0, thread_count):
  86. threads[i].join()
  87. print("Done! Formatted %d files" % file_name_count)