FormatSource.py 3.1 KB

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