You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

cmakeformat.py 2.6 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. import argparse
  4. import os
  5. import subprocess
  6. from pathlib import Path
  7. CMAKE_FILS_DIRS = [
  8. "test",
  9. "dnn",
  10. "tools",
  11. "sdk",
  12. "src",
  13. "imperative",
  14. "lite",
  15. "cmake",
  16. "toolchains",
  17. ]
  18. def main():
  19. os.chdir(str(Path(__file__).resolve().parent.parent))
  20. parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter)
  21. parser.add_argument("--check", action="store_true", help="check model")
  22. parser.add_argument(
  23. "--cmake_files",
  24. nargs="+",
  25. default=None,
  26. dest="cmake_files",
  27. help="cmake files to format, please split with space",
  28. )
  29. args = parser.parse_args()
  30. handle_files = []
  31. if args.cmake_files:
  32. handle_files = args.cmake_files
  33. for cmake_file in handle_files:
  34. assert os.path.isfile(
  35. cmake_file
  36. ), "error input --cmake_files, can not find file: {}".format(cmake_file)
  37. else:
  38. handle_files.append("CMakeLists.txt")
  39. for cmake_file_dir in CMAKE_FILS_DIRS:
  40. assert os.path.isdir(
  41. cmake_file_dir
  42. ), "{} is not a directory, may config error for CMAKE_FILS_DIRS".format(
  43. cmake_file_dir
  44. )
  45. for cmake_file in [
  46. os.path.join(root, file)
  47. for root, dirs, files in os.walk(cmake_file_dir)
  48. for file in files
  49. if file.endswith("CMakeLists.txt") or file.endswith(".cmake")
  50. ]:
  51. print("find cmake_file: {}".format(cmake_file))
  52. assert os.path.isfile(cmake_file), "code issue happened!!"
  53. handle_files.append(cmake_file)
  54. for cmake_file in handle_files:
  55. handle_type = ["format", "--in-place"]
  56. if args.check:
  57. handle_type = ["check", "--check"]
  58. cmd = "cmake-format -c tools/cmake_format_config.json {} {}".format(
  59. handle_type[1], cmake_file
  60. )
  61. print("try {}: {} with command: {}".format(handle_type[0], cmake_file, cmd))
  62. try:
  63. subprocess.check_call(cmd, shell=True)
  64. except Exception as exc:
  65. print("run cmd {} failed".format(cmd))
  66. if args.check:
  67. print(
  68. 'please run: "python3 tools/cmakeformat.py" to format cmake files'
  69. )
  70. else:
  71. print("code issue happened!!, please FIXME!!")
  72. raise exc
  73. if __name__ == "__main__":
  74. subprocess.check_call("python3 -m pip install cmakelang==0.6.13 --user", shell=True)
  75. main()