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 3.0 kB

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