FormatSource.py 1.4 KB

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