FormatSource.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. file_extensions = ["h", "hpp", "c", "cpp", "glsl", "hlsl", "ankiprog"]
  12. directories = ["AnKi", "Tests", "Sandbox", "Tools", "Samples"]
  13. def thread_callback(tid):
  14. """ Call clang-format """
  15. global mutex
  16. global file_names
  17. while True:
  18. mutex.acquire()
  19. if len(file_names) > 0:
  20. file_name = file_names.pop()
  21. else:
  22. file_name = None
  23. mutex.release()
  24. if file_name is None:
  25. break
  26. unused, file_extension = os.path.splitext(file_name)
  27. if file_extension == ".hlsl" or file_extension == ".ankiprog":
  28. style_file = "--style=file:.clang-format-hlsl"
  29. else:
  30. style_file = "--style=file:.clang-format"
  31. subprocess.check_call(["./ThirdParty/Bin/Windows64/clang-format.exe",
  32. "-sort-includes=false", style_file, "-i", file_name])
  33. # Gather the filenames
  34. file_names = []
  35. for directory in directories:
  36. for extension in file_extensions:
  37. file_names.extend(glob.glob("./" + directory + "/**/*." + extension, recursive=True))
  38. file_name_count = len(file_names)
  39. # Start the threads
  40. mutex = threading.Lock()
  41. thread_count = multiprocessing.cpu_count()
  42. threads = []
  43. for i in range(0, thread_count):
  44. thread = threading.Thread(target=thread_callback, args=(i,))
  45. threads.append(thread)
  46. thread.start()
  47. # Join the threads
  48. for i in range(0, thread_count):
  49. threads[i].join()
  50. print("Done! Formatted %d files" % file_name_count)