cmake_qtcreator_project.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. # Contributor(s): Campbell Barton, M.G. Kishalmi
  19. #
  20. # ***** END GPL LICENSE BLOCK *****
  21. # <pep8 compliant>
  22. r"""
  23. Example Linux usage:
  24. python ~/blender-git/blender/build_files/cmake/cmake_qtcreator_project.py --build-dir ~/blender-git/cmake
  25. Example Win32 usage:
  26. c:\Python32\python.exe c:\blender_dev\blender\build_files\cmake\cmake_qtcreator_project.py --build-dir c:\blender_dev\cmake_build
  27. """
  28. import os
  29. def quote_define(define):
  30. if " " in define.strip():
  31. return '"%s"' % define
  32. else:
  33. return define
  34. def create_qtc_project_main(name):
  35. from project_info import (
  36. SIMPLE_PROJECTFILE,
  37. SOURCE_DIR,
  38. # CMAKE_DIR,
  39. PROJECT_DIR,
  40. source_list,
  41. is_project_file,
  42. is_c_header,
  43. cmake_advanced_info,
  44. cmake_compiler_defines,
  45. project_name_get,
  46. )
  47. files = list(source_list(SOURCE_DIR, filename_check=is_project_file))
  48. files_rel = [os.path.relpath(f, start=PROJECT_DIR) for f in files]
  49. files_rel.sort()
  50. # --- qtcreator specific, simple format
  51. if SIMPLE_PROJECTFILE:
  52. # --- qtcreator specific, simple format
  53. PROJECT_NAME = name or "Blender"
  54. FILE_NAME = PROJECT_NAME.lower()
  55. with open(os.path.join(PROJECT_DIR, "%s.files" % FILE_NAME), 'w') as f:
  56. f.write("\n".join(files_rel))
  57. with open(os.path.join(PROJECT_DIR, "%s.includes" % FILE_NAME), 'w') as f:
  58. f.write("\n".join(sorted(list(set(os.path.dirname(f)
  59. for f in files_rel if is_c_header(f))))))
  60. qtc_prj = os.path.join(PROJECT_DIR, "%s.creator" % FILE_NAME)
  61. with open(qtc_prj, 'w') as f:
  62. f.write("[General]\n")
  63. qtc_cfg = os.path.join(PROJECT_DIR, "%s.config" % FILE_NAME)
  64. if not os.path.exists(qtc_cfg):
  65. with open(qtc_cfg, 'w') as f:
  66. f.write("// ADD PREDEFINED MACROS HERE!\n")
  67. else:
  68. includes, defines = cmake_advanced_info()
  69. if (includes, defines) == (None, None):
  70. return
  71. # for some reason it doesnt give all internal includes
  72. includes = list(set(includes) | set(os.path.dirname(f)
  73. for f in files_rel if is_c_header(f)))
  74. includes.sort()
  75. # be tricky, get the project name from CMake if we can!
  76. PROJECT_NAME = name or project_name_get()
  77. FILE_NAME = PROJECT_NAME.lower()
  78. with open(os.path.join(PROJECT_DIR, "%s.files" % FILE_NAME), 'w') as f:
  79. f.write("\n".join(files_rel))
  80. with open(os.path.join(PROJECT_DIR, "%s.includes" % FILE_NAME), 'w', encoding='utf-8') as f:
  81. f.write("\n".join(sorted(includes)))
  82. qtc_prj = os.path.join(PROJECT_DIR, "%s.creator" % FILE_NAME)
  83. with open(qtc_prj, 'w') as f:
  84. f.write("[General]\n")
  85. qtc_cfg = os.path.join(PROJECT_DIR, "%s.config" % FILE_NAME)
  86. with open(qtc_cfg, 'w') as f:
  87. f.write("// ADD PREDEFINED MACROS TO %s_custom.config!\n" % FILE_NAME)
  88. qtc_custom_cfg = os.path.join(PROJECT_DIR, "%s_custom.config" % FILE_NAME)
  89. if os.path.exists(qtc_custom_cfg):
  90. with open(qtc_custom_cfg, 'r') as fc:
  91. f.write(fc.read())
  92. f.write("\n")
  93. defines_final = [("#define %s %s" % (item[0], quote_define(item[1]))) for item in defines]
  94. if os.name != "nt":
  95. defines_final += cmake_compiler_defines()
  96. f.write("\n".join(defines_final))
  97. print("Blender project file written to: %r" % qtc_prj)
  98. # --- end
  99. def create_qtc_project_python(name):
  100. from project_info import (
  101. SOURCE_DIR,
  102. # CMAKE_DIR,
  103. PROJECT_DIR,
  104. source_list,
  105. is_py,
  106. project_name_get,
  107. )
  108. files = list(source_list(SOURCE_DIR, filename_check=is_py))
  109. files_rel = [os.path.relpath(f, start=PROJECT_DIR) for f in files]
  110. files_rel.sort()
  111. # --- qtcreator specific, simple format
  112. # be tricky, get the project name from git if we can!
  113. PROJECT_NAME = (name or project_name_get()) + "_Python"
  114. FILE_NAME = PROJECT_NAME.lower()
  115. with open(os.path.join(PROJECT_DIR, "%s.files" % FILE_NAME), 'w') as f:
  116. f.write("\n".join(files_rel))
  117. qtc_prj = os.path.join(PROJECT_DIR, "%s.creator" % FILE_NAME)
  118. with open(qtc_prj, 'w') as f:
  119. f.write("[General]\n")
  120. qtc_cfg = os.path.join(PROJECT_DIR, "%s.config" % FILE_NAME)
  121. if not os.path.exists(qtc_cfg):
  122. with open(qtc_cfg, 'w') as f:
  123. f.write("// ADD PREDEFINED MACROS HERE!\n")
  124. print("Python project file written to: %r" % qtc_prj)
  125. def argparse_create():
  126. import argparse
  127. parser = argparse.ArgumentParser(
  128. description="This script generates Qt Creator project files for Blender",
  129. )
  130. parser.add_argument(
  131. "-n", "--name",
  132. dest="name",
  133. metavar='NAME', type=str,
  134. help="Override default project name (\"Blender\")",
  135. required=False,
  136. )
  137. parser.add_argument(
  138. "-b", "--build-dir",
  139. dest="build_dir",
  140. metavar='BUILD_DIR', type=str,
  141. help="Specify the build path (or fallback to the $PWD)",
  142. required=False,
  143. )
  144. return parser
  145. def main():
  146. parser = argparse_create()
  147. args = parser.parse_args()
  148. name = args.name
  149. import project_info
  150. if not project_info.init(args.build_dir):
  151. return
  152. create_qtc_project_main(name)
  153. create_qtc_project_python(name)
  154. if __name__ == "__main__":
  155. main()