-
Notifications
You must be signed in to change notification settings - Fork 0
/
TargetInfo.py
206 lines (169 loc) · 6.13 KB
/
TargetInfo.py
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
import os.path
KNOWN_ROOTS = ['SDKROOT', 'SOURCE_ROOT', 'DEVELOPER_DIR', 'BUILT_PRODUCTS_DIR']
class TargetInfo:
def __init__(self, project, target):
self._project = project
self._target = target
self._index_groups()
self._info = {
'name': target.name,
'type': target.productType,
'buildConfigurations': self._get_configurations(),
'buildPhases': self._get_phases(),
'dependencies': self._get_dependencies(),
'files': self._get_files(),
}
def dict(self):
return self._info
def obj(self, id):
return self._project.objects[id]
def get_buildfile_path(self, key):
buildfile = self.obj(key)
if buildfile is None or buildfile['fileRef'] is None: return f"(null) {key}"
fileref = self.obj(buildfile.fileRef)
if fileref is None or fileref['path'] is None: return f"(null) {key}"
return repr(fileref)
def _index_groups(self):
self._group_parents = {}
self._group_paths = {}
self._group_paths[self._project.rootObject] = ''
root = self.obj(self._project.rootObject)
self._group_paths[root.mainGroup] = ''
for group in self._project.objects.get_objects_in_section('PBXGroup'):
group_id = group.get_id()
if group['children'] is not None:
for child in group.children:
self._group_parents[child] = group_id
if group.sourceTree in KNOWN_ROOTS:
self._group_paths[group_id] = f"$({group.sourceTree})"
def _get_configurations(self):
if self._target['buildConfigurationList'] is None: return None
list = self.obj(self._target.buildConfigurationList)
if list is None or list['buildConfigurations'] is None: return None
all_confs = {}
for ckey in list.buildConfigurations:
conf = self.obj(ckey)
if conf is None or conf['name'] is None: continue
all_confs[conf.name] = {
'buildSettings': {skey : conf.buildSettings[skey] for skey in conf.buildSettings.get_keys()}
}
return all_confs
def _get_phases(self):
if self._target['buildPhases'] is None: return None
all_phases = []
for pkey in self._target.buildPhases:
phase = self.obj(pkey)
if phase is None or phase.isa in ['PBXSourcesBuildPhase', 'PBXResourcesBuildPhase', 'PBXFrameworksBuildPhase']: continue
phase_info = {
'name': phase['name'],
'type': phase.isa,
}
if 'PBXShellScriptBuildPhase' == phase.isa:
phase_info['script'] = phase.shellScript
elif 'PBXCopyFilesBuildPhase' == phase.isa:
phase_info['files'] = [self.get_buildfile_path(fkey) for fkey in phase.files]
all_phases.append(phase_info)
return all_phases
def _get_dependencies(self):
all_dependencies = []
if self._target['dependencies'] is not None:
for dkey in self._target.dependencies:
dep = self.obj(dkey)
if dep is None: continue
if 'PBXTargetDependency' == dep.isa:
dep_target = self.obj(dep.target)
all_dependencies.append({
'type': dep.isa,
'target': dep_target.name,
})
else:
all_dependencies.append({'type':dep.isa})
if self._target['packageProductDependencies'] is not None:
for dkey in self._target.packageProductDependencies:
dep = self.obj(dkey)
if dep is None: continue
if 'XCSwiftPackageProductDependency' == dep.isa:
info = {
'type': dep.isa,
'productName': dep['productName'],
}
pkg = self.obj(dep.package)
if pkg is not None:
info['url'] = pkg['repositoryURL']
all_dependencies.append(info)
else:
all_dependencies.append({'type':dep.isa})
return all_dependencies
def _get_files(self):
if self._target['buildPhases'] is None: return None
sources = []
resources = []
frameworks = []
for pkey in self._target.buildPhases:
phase = self.obj(pkey)
if phase is None or phase['files'] is None: continue
files = self._resolve_files(phase.files)
if 'PBXSourcesBuildPhase' == phase.isa:
sources.extend(files)
elif 'PBXResourcesBuildPhase' == phase.isa:
resources.extend(files)
elif 'PBXFrameworksBuildPhase' == phase.isa:
frameworks.extend(files)
sources.sort()
resources.sort()
frameworks.sort()
return {
'sources': sources,
'resources': resources,
'frameworks': frameworks,
}
def _resolve_files(self, files):
for id in files:
file = self.obj(id)
if file.isa == 'PBXBuildFile':
if 'fileRef' in file:
id = file.fileRef
file = self.obj(id)
elif 'productRef' in file:
id = file.productRef
file = self.obj(id)
else:
yield f"(null) {id}"
continue
if file.isa == 'PBXVariantGroup':
variant_path = self._relative_path(id)
for child_id in file.children:
yield self._relative_path(child_id, variant_path)
elif file.isa == 'XCSwiftPackageProductDependency':
yield f"{file.productName} (Swift package)"
else:
yield self._relative_path(id)
def _relative_path(self, id, variant_path=None):
if id in self._group_paths:
return self._group_paths[id]
obj = self.obj(id)
if obj is None:
return f"(null) {id}"
if obj.isa not in ['PBXFileReference', 'PBXGroup', 'XCVersionGroup', 'PBXVariantGroup']:
raise Exception(f"Unknown object {obj.isa}")
if obj.sourceTree in KNOWN_ROOTS:
parent_path = f"$({obj.sourceTree})"
elif obj.sourceTree == '<group>':
if id in self._group_parents:
parent_path = self._relative_path(self._group_parents[id])
elif variant_path is not None:
parent_path = variant_path
elif obj.get_parent() == self._project.objects:
parent_path = ''
else:
raise Exception(f"Unknown parent for {id}")
else:
raise Exception(f"Unknown source {obj.sourceTree}")
if 'path' in obj:
path = os.path.join(parent_path, obj.path)
else:
path = parent_path
path = os.path.normpath(path)
if 'PBXGroup' == obj.isa:
self._group_paths[id] = path
return path