cmake_qtcreator_project.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #!/usr/bin/env python3
  2. # ***** BEGIN GPL LICENSE BLOCK *****
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software Foundation,
  16. # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. #
  18. # ***** END GPL LICENSE BLOCK *****
  19. # <pep8 compliant>
  20. r"""
  21. Example Linux usage:
  22. python ~/blender-git/blender/build_files/cmake/cmake_qtcreator_project.py --build-dir ~/blender-git/cmake
  23. Example Win32 usage:
  24. c:\Python32\python.exe c:\blender_dev\blender\build_files\cmake\cmake_qtcreator_project.py --build-dir c:\blender_dev\cmake_build
  25. """
  26. import os
  27. def quote_define(define):
  28. if " " in define.strip():
  29. return '"%s"' % define
  30. else:
  31. return define
  32. def create_qtc_project_main(name):
  33. from project_info import (
  34. SIMPLE_PROJECTFILE,
  35. SOURCE_DIR,
  36. # CMAKE_DIR,
  37. PROJECT_DIR,
  38. source_list,
  39. is_project_file,
  40. is_c_header,
  41. cmake_advanced_info,
  42. cmake_compiler_defines,
  43. project_name_get,
  44. )
  45. files = list(source_list(SOURCE_DIR, filename_check=is_project_file))
  46. files_rel = [os.path.relpath(f, start=PROJECT_DIR) for f in files]
  47. files_rel.sort()
  48. # --- qtcreator specific, simple format
  49. if SIMPLE_PROJECTFILE:
  50. # --- qtcreator specific, simple format
  51. PROJECT_NAME = name or "Blender"
  52. FILE_NAME = PROJECT_NAME.lower()
  53. with open(os.path.join(PROJECT_DIR, "%s.files" % FILE_NAME), 'w') as f:
  54. f.write("\n".join(files_rel))
  55. with open(os.path.join(PROJECT_DIR, "%s.includes" % FILE_NAME), 'w') as f:
  56. f.write("\n".join(sorted(list(set(os.path.dirname(f)
  57. for f in files_rel if is_c_header(f))))))
  58. qtc_prj = os.path.join(PROJECT_DIR, "%s.creator" % FILE_NAME)
  59. with open(qtc_prj, 'w') as f:
  60. f.write("[General]\n")
  61. qtc_cfg = os.path.join(PROJECT_DIR, "%s.config" % FILE_NAME)
  62. if not os.path.exists(qtc_cfg):
  63. with open(qtc_cfg, 'w') as f:
  64. f.write("// ADD PREDEFINED MACROS HERE!\n")
  65. else:
  66. includes, defines = cmake_advanced_info()
  67. if (includes, defines) == (None, None):
  68. return
  69. # for some reason it doesn't give all internal includes
  70. includes = list(set(includes) | set(os.path.dirname(f)
  71. for f in files_rel if is_c_header(f)))
  72. includes.sort()
  73. # be tricky, get the project name from CMake if we can!
  74. PROJECT_NAME = name or project_name_get()
  75. FILE_NAME = PROJECT_NAME.lower()
  76. with open(os.path.join(PROJECT_DIR, "%s.files" % FILE_NAME), 'w') as f:
  77. f.write("\n".join(files_rel))
  78. with open(os.path.join(PROJECT_DIR, "%s.includes" % FILE_NAME), 'w', encoding='utf-8') as f:
  79. f.write("\n".join(sorted(includes)))
  80. qtc_prj = os.path.join(PROJECT_DIR, "%s.creator" % FILE_NAME)
  81. with open(qtc_prj, 'w') as f:
  82. f.write("[General]\n")
  83. qtc_cfg = os.path.join(PROJECT_DIR, "%s.config" % FILE_NAME)
  84. with open(qtc_cfg, 'w') as f:
  85. f.write("// ADD PREDEFINED MACROS TO %s_custom.config!\n" % FILE_NAME)
  86. qtc_custom_cfg = os.path.join(PROJECT_DIR, "%s_custom.config" % FILE_NAME)
  87. if os.path.exists(qtc_custom_cfg):
  88. with open(qtc_custom_cfg, 'r') as fc:
  89. f.write(fc.read())
  90. f.write("\n")
  91. defines_final = [("#define %s %s" % (item[0], quote_define(item[1]))) for item in defines]
  92. if os.name != "nt":
  93. defines_final += cmake_compiler_defines()
  94. f.write("\n".join(defines_final))
  95. print("Blender project file written to: %r" % qtc_prj)
  96. # --- end
  97. def create_qtc_project_python(name):
  98. from project_info import (
  99. SOURCE_DIR,
  100. # CMAKE_DIR,
  101. PROJECT_DIR,
  102. source_list,
  103. is_py,
  104. project_name_get,
  105. )
  106. files = list(source_list(SOURCE_DIR, filename_check=is_py))
  107. files_rel = [os.path.relpath(f, start=PROJECT_DIR) for f in files]
  108. files_rel.sort()
  109. # --- qtcreator specific, simple format
  110. # be tricky, get the project name from git if we can!
  111. PROJECT_NAME = (name or project_name_get()) + "_Python"
  112. FILE_NAME = PROJECT_NAME.lower()
  113. with open(os.path.join(PROJECT_DIR, "%s.files" % FILE_NAME), 'w') as f:
  114. f.write("\n".join(files_rel))
  115. qtc_prj = os.path.join(PROJECT_DIR, "%s.creator" % FILE_NAME)
  116. with open(qtc_prj, 'w') as f:
  117. f.write("[General]\n")
  118. qtc_cfg = os.path.join(PROJECT_DIR, "%s.config" % FILE_NAME)
  119. if not os.path.exists(qtc_cfg):
  120. with open(qtc_cfg, 'w') as f:
  121. f.write("// ADD PREDEFINED MACROS HERE!\n")
  122. print("Python project file written to: %r" % qtc_prj)
  123. def argparse_create():
  124. import argparse
  125. parser = argparse.ArgumentParser(
  126. description="This script generates Qt Creator project files for Blender",
  127. )
  128. parser.add_argument(
  129. "-n", "--name",
  130. dest="name",
  131. metavar='NAME', type=str,
  132. help="Override default project name (\"Blender\")",
  133. required=False,
  134. )
  135. parser.add_argument(
  136. "-b", "--build-dir",
  137. dest="build_dir",
  138. metavar='BUILD_DIR', type=str,
  139. help="Specify the build path (or fallback to the $PWD)",
  140. required=False,
  141. )
  142. return parser
  143. def main():
  144. parser = argparse_create()
  145. args = parser.parse_args()
  146. name = args.name
  147. import project_info
  148. if not project_info.init(args.build_dir):
  149. return
  150. create_qtc_project_main(name)
  151. create_qtc_project_python(name)
  152. if __name__ == "__main__":
  153. main()