forked from wesnoth/wesnoth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
add_source_file
executable file
·332 lines (278 loc) · 11.2 KB
/
add_source_file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#!/usr/bin/env python3
# encoding: utf-8
# known issues:
# xcode - if a file already exists in 'wesnoth' target, then it incorrectly thinks it also exists in the 'tests' target even though the tests build will fail
"""
Add files to the specified build targets, supporting
CMake, SCons, Xcode and the Code::Blocks projects.
Valid build targets are:
* "wesnoth" - the main game (default if no target is specified)
* "wesnothd" - the wesnoth server
* "campaignd"
* "lua"
* "tests" - boost unit tests
The files will be added to:
* the lists used by CMake and SCons in "source_lists"
* the Xcode project
* The Code::Blocks project
This only supports files inside the "src" directory.
"""
import argparse
import sys
import inspect
import pathlib
try:
import pbxproj
except:
print('\n'.join((
'This script requires the "pbxproj" module.',
'Install it using "pip install pbxproj"',
'optionally setting up a python3-venv first.',
)))
exit(1)
#=========#
# Globals #
#=========#
# Either the executable directory or the current working directory
# should be the wesnoth root directory
rootdir = pathlib.Path(inspect.getsourcefile(lambda:0))
if not rootdir.joinpath("projectfiles").exists():
rootdir = pathlib.Path()
if not rootdir.joinpath("projectfiles").exists():
raise Exception("Could not find project file directory")
# the names of the targets in the Xcode project
xcode_target_translations = {
"wesnoth": ["The Battle for Wesnoth", "unit_tests"],
"wesnothd": ["wesnothd"],
"campaignd": ["campaignd"],
"lua": ["liblua"],
"tests": ["unit_tests"],
}
# the names of the targets in source_lists
source_list_target_translations = {
"wesnoth": "wesnoth",
"wesnothd": "wesnothd",
"campaignd": "campaignd",
"lua": "lua",
"tests": "boost_unit_tests",
}
# the names of the targets in Code::Blocks
code_blocks_target_translations = {
"wesnoth": "wesnoth",
"wesnothd": "wesnothd",
"campaignd": "campaignd",
"lua": "liblua",
"tests": "tests",
}
#=======#
# XCode #
#=======#
def modify_xcode(filename, targets, remove):
"""Add the given file to the specified targets.
"""
projectfile = rootdir.joinpath(
"projectfiles",
"Xcode",
"The Battle for Wesnoth.xcodeproj",
"project.pbxproj",
)
project = pbxproj.XcodeProject.load(projectfile)
translated_targets = [item for t in targets for item in xcode_target_translations[t]]
translated_targets = list(set(translated_targets))
print(" xcode targets:", translated_targets)
for tname in translated_targets:
if not project.get_target_by_name(tname):
raise Exception(
f"Could not find target '{tname}' in Xcode project file")
# groups are organized by directory structure under "src"
# except for tests, which have a separate root, "tests"
if pathlib.Path("tests") in filename.parents:
src_groups = project.get_groups_by_name("tests")
else:
src_groups = project.get_groups_by_name("src")
if len(src_groups) != 1:
raise Exception("problem finding 'src' group in xcode project")
src_group = src_groups[0]
parent_group = src_group
for d in filename.parts[:-1]:
if d == "tests":
continue
found_groups = project.get_groups_by_name(d, parent=parent_group)
if len(found_groups) != 1:
groupname = parent_group.get_name()
raise Exception(f"problem finding '{d}' group in '{groupname}'")
parent_group = found_groups[0]
if remove :
# Remove from all targets if we want to remove
for file in project.get_files_by_name(filename.name, parent=parent_group):
project.remove_file_by_id(file.get_id())
else:
# if the group already has an entry with the same filename, loudly skip.
# note: this doesn't allow adding to targets one at a time.
# a new file should be added to all targets at once...
# or maybe targets could be checked somehow,
# or maybe the file could simply be completely removed and readded.
if project.get_files_by_name(filename.name, parent=parent_group):
print(" '"+filename.name+"' already found in Xcode project '"+",".join(translated_targets)+"', skipping")
return
# force is True here because otherwise a duplicate filename in
# a different place will block addition of the new file.
# the rest is just to match existing project file structure.
project.add_file(filename.name,
force=True,
tree="<group>",
parent=parent_group,
target_name=translated_targets,
)
# that's done, save the file
project.save()
return
#==============#
# source_lists #
#==============#
def modify_source_list(filename, source_list, remove):
source_list_file = rootdir.joinpath("source_lists", source_list)
sl_lines = open(source_list_file).readlines()
file_line = filename.as_posix() + '\n'
# we only need source files in the source_lists, not header files
if filename.suffix != ".cpp":
return
if remove:
if file_line in sl_lines: sl_lines.remove(file_line)
else:
# if the target already has an entry with the same filename, loudly skip
if file_line in sl_lines:
print(f" '{filename}' already found in '{source_list}', skipping")
return
sl_lines.append(file_line)
sl_lines.sort()
open(source_list_file, 'w').writelines(sl_lines)
def add_to_source_lists(filename, targets):
translated_targets = [source_list_target_translations[t] for t in targets]
print(" source_list targets:", translated_targets)
for t in translated_targets:
modify_source_list(filename, t, False)
def remove_from_source_lists(filename):
# remove from all tagerts if -r was specified.
for t in source_list_target_translations.values():
modify_source_list(filename, t, True)
#==============#
# Code::Blocks #
#==============#
def modify_code_blocks_target(filename, target, remove):
cbp_file = rootdir.joinpath(
"projectfiles",
"CodeBlocks",
f"{target}.cbp",
)
cbp_lines = open(cbp_file).readlines()
filename_for_cbp = pathlib.PurePath(
"..", "..", "src", filename
).as_posix()
elem = f"\t\t<Unit filename=\"{filename_for_cbp}\" />\n"
if remove:
if elem in cbp_lines: cbp_lines.remove(elem)
else:
# if the target already has an entry with the same filename, loudly skip
if elem in cbp_lines:
print(f" '{filename}' already found in '{target}.cbp', skipping")
return
# find an appropriate line to add before/after
index = 0
for line in cbp_lines:
if line.startswith("\t\t<Unit "):
if elem < line:
break
elif line.startswith("\t\t<Extensions>"):
# we must be the last entry, as this comes after the Unit section
break
index += 1
cbp_lines.insert(index, elem)
open(cbp_file, 'w').writelines(cbp_lines)
def modify_code_blocks(filename, targets, remove):
translated_targets = code_blocks_target_translations.values() if remove else [code_blocks_target_translations[t] for t in targets]
print(" code::blocks targets:", translated_targets)
for t in translated_targets:
modify_code_blocks_target(filename, t, remove)
def sanity_check_existing_cpp_hpp(filenames):
"""
If we're adding a .cpp file, check whether a .hpp should be added too, etc.
Only the files named on the command line are added, this exits if the check fails.
"""
any_check_failed = False
for filename in filenames:
if filenames.count(filename) > 1:
print(f"ERROR: File '{filename}' given multiple times")
any_check_failed = True
if not rootdir.joinpath("src", filename).exists():
print(f"WARN: File '{filename}' does not exist")
any_check_failed = True
spouse = None
if filename.suffix == ".cpp":
spouse = filename.with_suffix(".hpp")
elif filename.suffix == ".hpp":
spouse = filename.with_suffix(".cpp")
if rootdir.joinpath("src", spouse).exists() and not filenames.count(spouse):
print(f"WARN: Requested to add '{filename}', should '{spouse}' be added too?")
any_check_failed = True
if any_check_failed:
break
if any_check_failed:
print("ERROR: Not making changes, as checks failed and --no-checks option was not used.")
exit(1)
def canonicalise_filenames(original_filenames):
"""
The script supports giving the filenames with or without the "src/" prefix.
Strip the "src/" if present, functions that need it will add it again later.
"""
filenames = []
# If src/src/ exists, the filenames become ambiguous. No need to support that.
if rootdir.joinpath("src", "src").exists():
print("Please don't add a file or directory called src/src.")
exit(1)
for filename in options.filename:
filename = pathlib.PurePath(filename)
parts = filename.parts
if parts[0] == "src":
filename = pathlib.PurePath(*parts[1:])
else:
filename = pathlib.PurePath(*parts)
filenames.append(filename)
return filenames
#======#
# main #
#======#
if __name__ == "__main__":
ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
# a file argument is mandatory
ap.add_argument("filename", action="store", nargs="+",
help="the .cpp and .hpp files to add")
ap.add_argument("--target", action="store", nargs=1,
default=["wesnoth"],
help="which build targets to add the file to")
ap.add_argument("--no-checks", action="store_true",
help="do not check whether the files exist, etc")
ap.add_argument("-r", "--remove", action="store_true",
help="remove the specified files from projectfiles instead of adding them, --target is then ignored")
# By default, recognise --help too
options = ap.parse_args()
# Bail out if someone uses the old syntax of "add_source_file src/foo.cpp campaignd"
if not options.no_checks:
if len(options.filename) == 2 and not options.filename[1].count('.'):
print("The usage has changed, targets now need to be given using --target name")
exit(1)
# Convert the names to pathlib.PurePath objects without leading "src/"
filenames = canonicalise_filenames(options.filename)
if not options.no_checks:
sanity_check_existing_cpp_hpp(filenames)
for filename in filenames:
if options.remove:
print(f"removing '{filename}' from all targets")
modify_xcode(filename, options.target, True)
remove_from_source_lists(filename)
modify_code_blocks(filename, options.target, True)
else:
print(f"adding '{filename}' to targets: {options.target}")
modify_xcode(filename, options.target, False)
add_to_source_lists(filename, options.target)
modify_code_blocks(filename, options.target, False)