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