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