cmake_netbeans_project.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. """
  23. Example linux usage
  24. python3 ~/blender-git/blender/build_files/cmake/cmake_netbeans_project.py ~/blender-git/cmake
  25. Windows not supported so far
  26. """
  27. import sys
  28. # until we have arg parsing
  29. import project_info
  30. if not project_info.init(sys.argv[-1]):
  31. sys.exit(1)
  32. from project_info import (
  33. SIMPLE_PROJECTFILE,
  34. SOURCE_DIR,
  35. CMAKE_DIR,
  36. PROJECT_DIR,
  37. source_list,
  38. is_project_file,
  39. is_c_header,
  40. # is_py,
  41. cmake_advanced_info,
  42. cmake_compiler_defines,
  43. cmake_cache_var,
  44. project_name_get,
  45. )
  46. import os
  47. from os.path import join, dirname, normpath, relpath, exists
  48. def create_nb_project_main():
  49. from xml.sax.saxutils import escape
  50. files = list(source_list(SOURCE_DIR, filename_check=is_project_file))
  51. files_rel = [relpath(f, start=PROJECT_DIR) for f in files]
  52. files_rel.sort()
  53. if SIMPLE_PROJECTFILE:
  54. pass
  55. else:
  56. includes, defines = cmake_advanced_info()
  57. if (includes, defines) == (None, None):
  58. return
  59. # for some reason it doesnt give all internal includes
  60. includes = list(set(includes) | set(dirname(f) for f in files if is_c_header(f)))
  61. includes.sort()
  62. if 0:
  63. PROJECT_NAME = "Blender"
  64. else:
  65. # be tricky, get the project name from git if we can!
  66. PROJECT_NAME = project_name_get()
  67. make_exe = cmake_cache_var("CMAKE_MAKE_PROGRAM")
  68. make_exe_basename = os.path.basename(make_exe)
  69. # --------------- NB specific
  70. defines = [("%s=%s" % cdef) if cdef[1] else cdef[0] for cdef in defines]
  71. defines += [cdef.replace("#define", "").strip() for cdef in cmake_compiler_defines()]
  72. def file_list_to_nested(files):
  73. # convert paths to hierarchy
  74. paths_nested = {}
  75. def ensure_path(filepath):
  76. filepath_split = filepath.split(os.sep)
  77. pn = paths_nested
  78. for subdir in filepath_split[:-1]:
  79. pn = pn.setdefault(subdir, {})
  80. pn[filepath_split[-1]] = None
  81. for path in files:
  82. ensure_path(path)
  83. return paths_nested
  84. PROJECT_DIR_NB = join(PROJECT_DIR, "nbproject")
  85. if not exists(PROJECT_DIR_NB):
  86. os.mkdir(PROJECT_DIR_NB)
  87. # SOURCE_DIR_REL = relpath(SOURCE_DIR, PROJECT_DIR)
  88. f = open(join(PROJECT_DIR_NB, "project.xml"), 'w')
  89. f.write('<?xml version="1.0" encoding="UTF-8"?>\n')
  90. f.write('<project xmlns="http://www.netbeans.org/ns/project/1">\n')
  91. f.write(' <type>org.netbeans.modules.cnd.makeproject</type>\n')
  92. f.write(' <configuration>\n')
  93. f.write(' <data xmlns="http://www.netbeans.org/ns/make-project/1">\n')
  94. f.write(' <name>%s</name>\n' % PROJECT_NAME)
  95. f.write(' <c-extensions>c,m</c-extensions>\n')
  96. f.write(' <cpp-extensions>cpp,cxx,cc,mm</cpp-extensions>\n')
  97. f.write(' <header-extensions>h,hxx,hh,hpp,inl</header-extensions>\n')
  98. f.write(' <sourceEncoding>UTF-8</sourceEncoding>\n')
  99. f.write(' <make-dep-projects/>\n')
  100. f.write(' <sourceRootList>\n')
  101. f.write(' <sourceRootElem>%s</sourceRootElem>\n' % SOURCE_DIR) # base_root_rel
  102. f.write(' </sourceRootList>\n')
  103. f.write(' <confList>\n')
  104. f.write(' <confElem>\n')
  105. f.write(' <name>Default</name>\n')
  106. f.write(' <type>0</type>\n')
  107. f.write(' </confElem>\n')
  108. f.write(' </confList>\n')
  109. f.write(' <formatting>\n')
  110. f.write(' <project-formatting-style>false</project-formatting-style>\n')
  111. f.write(' </formatting>\n')
  112. f.write(' </data>\n')
  113. f.write(' </configuration>\n')
  114. f.write('</project>\n')
  115. f.close()
  116. f = open(join(PROJECT_DIR_NB, "configurations.xml"), 'w')
  117. f.write('<?xml version="1.0" encoding="UTF-8"?>\n')
  118. f.write('<configurationDescriptor version="95">\n')
  119. f.write(' <logicalFolder name="root" displayName="root" projectFiles="true" kind="ROOT">\n')
  120. f.write(' <df root="%s" name="0">\n' % SOURCE_DIR) # base_root_rel
  121. # write files!
  122. files_rel_local = [normpath(relpath(join(CMAKE_DIR, path), SOURCE_DIR)) for path in files_rel]
  123. files_rel_hierarchy = file_list_to_nested(files_rel_local)
  124. # print(files_rel_hierarchy)
  125. def write_df(hdir, ident):
  126. dirs = []
  127. files = []
  128. for key, item in sorted(hdir.items()):
  129. if item is None:
  130. files.append(key)
  131. else:
  132. dirs.append((key, item))
  133. for key, item in dirs:
  134. f.write('%s <df name="%s">\n' % (ident, key))
  135. write_df(item, ident + " ")
  136. f.write('%s </df>\n' % ident)
  137. for key in files:
  138. f.write('%s<in>%s</in>\n' % (ident, key))
  139. write_df(files_rel_hierarchy, ident=" ")
  140. f.write(' </df>\n')
  141. f.write(' <logicalFolder name="ExternalFiles"\n')
  142. f.write(' displayName="Important Files"\n')
  143. f.write(' projectFiles="false"\n')
  144. f.write(' kind="IMPORTANT_FILES_FOLDER">\n')
  145. # f.write(' <itemPath>../GNUmakefile</itemPath>\n')
  146. f.write(' </logicalFolder>\n')
  147. f.write(' </logicalFolder>\n')
  148. # default, but this dir is infact not in blender dir so we can ignore it
  149. # f.write(' <sourceFolderFilter>^(nbproject)$</sourceFolderFilter>\n')
  150. f.write(' <sourceFolderFilter>^(nbproject|__pycache__|.*\.py|.*\.html|.*\.blend)$</sourceFolderFilter>\n')
  151. f.write(' <sourceRootList>\n')
  152. f.write(' <Elem>%s</Elem>\n' % SOURCE_DIR) # base_root_rel
  153. f.write(' </sourceRootList>\n')
  154. f.write(' <projectmakefile>Makefile</projectmakefile>\n')
  155. # paths again
  156. f.write(' <confs>\n')
  157. f.write(' <conf name="Default" type="0">\n')
  158. f.write(' <toolsSet>\n')
  159. f.write(' <compilerSet>default</compilerSet>\n')
  160. f.write(' <dependencyChecking>false</dependencyChecking>\n')
  161. f.write(' <rebuildPropChanged>false</rebuildPropChanged>\n')
  162. f.write(' </toolsSet>\n')
  163. f.write(' <codeAssistance>\n')
  164. f.write(' </codeAssistance>\n')
  165. f.write(' <makefileType>\n')
  166. f.write(' <makeTool>\n')
  167. f.write(' <buildCommandWorkingDir>.</buildCommandWorkingDir>\n')
  168. if make_exe_basename == "ninja":
  169. build_cmd = "ninja"
  170. clean_cmd = "ninja -t clean"
  171. else:
  172. build_cmd = "${MAKE} -f Makefile"
  173. clean_cmd = "${MAKE} -f Makefile clean"
  174. f.write(' <buildCommand>%s</buildCommand>\n' % escape(build_cmd))
  175. f.write(' <cleanCommand>%s</cleanCommand>\n' % escape(clean_cmd))
  176. f.write(' <executablePath>./bin/blender</executablePath>\n')
  177. del build_cmd, clean_cmd
  178. def write_toolinfo():
  179. f.write(' <incDir>\n')
  180. for inc in includes:
  181. f.write(' <pElem>%s</pElem>\n' % inc)
  182. f.write(' </incDir>\n')
  183. f.write(' <preprocessorList>\n')
  184. for cdef in defines:
  185. f.write(' <Elem>%s</Elem>\n' % escape(cdef))
  186. f.write(' </preprocessorList>\n')
  187. f.write(' <cTool>\n')
  188. write_toolinfo()
  189. f.write(' </cTool>\n')
  190. f.write(' <ccTool>\n')
  191. write_toolinfo()
  192. f.write(' </ccTool>\n')
  193. f.write(' </makeTool>\n')
  194. f.write(' </makefileType>\n')
  195. # finishe makefle info
  196. f.write(' \n')
  197. for path in files_rel_local:
  198. is_c = path.endswith(".c")
  199. f.write(' <item path="%s"\n' % path)
  200. f.write(' ex="false"\n')
  201. f.write(' tool="%d"\n' % (0 if is_c else 1))
  202. f.write(' flavor2="%d">\n' % (3 if is_c else 0))
  203. f.write(' </item>\n')
  204. f.write(' <runprofile version="9">\n')
  205. f.write(' <runcommandpicklist>\n')
  206. f.write(' </runcommandpicklist>\n')
  207. f.write(' <runcommand>%s</runcommand>\n' % os.path.join(CMAKE_DIR, "bin/blender"))
  208. f.write(' <rundir>%s</rundir>\n' % SOURCE_DIR)
  209. f.write(' <buildfirst>false</buildfirst>\n')
  210. f.write(' <terminal-type>0</terminal-type>\n')
  211. f.write(' <remove-instrumentation>0</remove-instrumentation>\n')
  212. f.write(' <environment>\n')
  213. f.write(' </environment>\n')
  214. f.write(' </runprofile>\n')
  215. f.write(' </conf>\n')
  216. f.write(' </confs>\n')
  217. # todo
  218. f.write('</configurationDescriptor>\n')
  219. f.close()
  220. def main():
  221. create_nb_project_main()
  222. if __name__ == "__main__":
  223. main()