-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSdkHelpers.ambuild
215 lines (180 loc) · 7.98 KB
/
SdkHelpers.ambuild
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
# vim: set sts=2 ts=8 sw=2 tw=99 et ft=python:
import json
import os
class SdkTarget(object):
def __init__(self, sdk, cxx, protoc):
self.sdk = sdk
self.cxx = cxx
self.protoc = protoc
class SdkHelpers(object):
def __init__(self):
self.sdks = {}
self.sdk_manifests = []
self.sdk_filter = None
self.find_sdk_path = None
self.sdk_targets = []
# find_sdk_path must be set to use.
def findSdks(self, builder, cxx_list, sdk_list):
not_found = []
sdk_remaining = set(sdk_list)
for sdk_name, sdk in SdkHelpers.getSdks(builder):
self.sdk_manifests.append(sdk)
# Skip SDKs that weren't specified or are not supported.
if not self.shouldFindSdk(sdk, sdk_list):
continue
# Skip SDKs that won't build on any targets.
if not SdkHelpers.sdkHasBuildTargets(sdk, cxx_list):
continue
sdk_path = self.find_sdk_path(sdk_name)
if sdk_path is None:
if SdkHelpers.shouldRequireSdk(sdk_name, sdk_list):
raise Exception('Could not find a valid path for {0}'.format(sdk_name))
not_found.append(sdk_name)
continue
sdk_remaining.discard(sdk_name)
sdk['path'] = sdk_path
self.sdks[sdk_name] = sdk
for cxx in cxx_list:
if SdkHelpers.shouldBuildSdk(sdk, cxx):
protoc = None
rel_protoc_path = sdk[cxx.target.platform].get('protoc_path', None)
if rel_protoc_path:
protoc_path = os.path.join(sdk['path'], rel_protoc_path)
protoc = builder.DetectProtoc(path = protoc_path)
for path in sdk['include_paths']:
protoc.includes += [os.path.join(sdk['path'], path)]
self.sdk_targets += [SdkTarget(sdk, cxx, protoc)]
if 'present' in sdk_list:
for sdk in not_found:
print('Warning: hl2sdk-{} was not found, and will not be included in build.'.format(sdk))
elif len(sdk_remaining) and 'all' not in sdk_list:
for sdk in sdk_remaining:
print('Error: hl2sdk-{} was not found.'.format(sdk))
raise Exception('Missing hl2sdks: {}'.format(','.join(sdk_remaining)))
if not len(self.sdk_targets) and len(sdk_list):
raise Exception('No buildable SDKs were found, nothing to build.')
@staticmethod
def shouldRequireSdk(sdk_name, sdk_list):
if 'all' in sdk_list:
return sdk_name != 'mock'
if sdk_name in sdk_list:
return True
return 'present' not in sdk_list
def shouldFindSdk(self, sdk, sdk_list):
# Remove SDKs that the project doesn't support.
if self.sdk_filter and not self.sdk_filter(sdk):
return False
if 'all' in sdk_list or 'present' in sdk_list:
return True
return sdk['name'] in sdk_list
@staticmethod
def sdkHasBuildTargets(sdk, cxx_list):
for cxx in cxx_list:
if SdkHelpers.shouldBuildSdk(sdk, cxx):
return True
return False
@staticmethod
def shouldBuildSdk(sdk, cxx):
if cxx.target.platform in sdk['platforms']:
if cxx.target.arch in sdk['platforms'][cxx.target.platform]:
return True
return False
@staticmethod
def addLists(sdk, list_name, cxx):
result = SdkHelpers.getLists(sdk, list_name, cxx)
cxx_list = getattr(cxx, list_name)
cxx_list.extend(result)
@staticmethod
def getLists(sdk, list_name, cxx):
result = SdkHelpers.getListsImpl(sdk, list_name, cxx)
if Project in sdk:
result += SdkHelpers.getListsImpl(sdk[Project], list_name, cxx)
return result
@staticmethod
def getListsImpl(info, list_name, cxx):
result = []
if cxx.target.platform in info:
platform_info = info[cxx.target.platform]
result += platform_info.get(list_name, [])
if cxx.target.arch in platform_info:
arch_info = platform_info[cxx.target.arch]
result += arch_info.get(list_name, [])
return result
@staticmethod
def getSdks(builder):
try:
# Requires new enough ambuild version for __file__ to be defined
sdk_manifest_dir = os.path.join(os.path.dirname(__file__), 'manifests')
except NameError:
sdk_manifest_dir = os.path.join(builder.sourcePath, 'hl2sdk-manifests', 'manifests')
out = []
for sdk_manifest in os.listdir(sdk_manifest_dir):
sdk_name, _ = os.path.splitext(sdk_manifest)
sdk_manifest_path = os.path.join(sdk_manifest_dir, sdk_manifest)
with open(sdk_manifest_path, 'rt') as fp:
sdk = json.load(fp)
builder.AddConfigureFile(sdk_manifest_path)
out.append((sdk_name, sdk))
return out
@staticmethod
def configureCxx(context, binary, sdk):
cxx = binary.compiler
# Includes/defines.
cxx.defines += ['SOURCE_ENGINE={}'.format(sdk['code'])]
if sdk['name'] in ['sdk2013', 'bms', 'pvkii'] and cxx.like('gcc'):
# The 2013 SDK already has these in public/tier0/basetypes.h
rm_defines = [
'stricmp=strcasecmp', '_stricmp=strcasecmp',
'_snprintf=snprintf', '_vsnprintf=vsnprintf,'
]
for rm_define in rm_defines:
if rm_define in cxx.defines:
cxx.defines.remove(rm_define)
if cxx.family == 'msvc':
cxx.defines += ['COMPILER_MSVC']
if cxx.target.arch == 'x86':
cxx.defines += ['COMPILER_MSVC32']
elif cxx.target.arch == 'x86_64':
cxx.defines += ['COMPILER_MSVC64']
if cxx.version >= 1900:
cxx.linkflags += ['legacy_stdio_definitions.lib']
else:
cxx.defines += ['COMPILER_GCC']
if cxx.target.arch == 'x86_64':
cxx.defines += ['X64BITS', 'PLATFORM_64BITS']
SdkHelpers.addLists(sdk, 'defines', cxx)
SdkHelpers.addLists(sdk, 'linkflags', cxx)
for path in sdk['include_paths']:
cxx.cxxincludes += [os.path.join(sdk['path'], path)]
# Link steps.
for lib in SdkHelpers.getLists(sdk, 'libs', cxx):
cxx.linkflags += [os.path.join(sdk['path'], lib)]
for lib in SdkHelpers.getLists(sdk, 'postlink_libs', cxx):
cxx.postlink += [os.path.join(sdk['path'], lib)]
if cxx.target.platform == 'linux':
cxx.linkflags[0:0] = ['-lm']
elif cxx.target.platform == 'mac':
cxx.linkflags.append('-liconv')
dynamic_libs = SdkHelpers.getLists(sdk, 'dynamic_libs', cxx)
for library in dynamic_libs:
file_name = os.path.split(library)[1]
source_path = os.path.join(sdk['path'], library)
output_path = os.path.join(binary.localFolder, file_name)
context.AddFolder(binary.localFolder)
output = context.AddSymlink(source_path, output_path)
cxx.weaklinkdeps += [output]
cxx.linkflags[0:0] = [file_name]
if cxx.target.platform == 'linux':
if sdk[cxx.target.platform]['uses_system_cxxlib']:
cxx.linkflags.remove('-static-libstdc++')
cxx.linkflags += ['-lstdc++']
elif cxx.target.platform == 'mac':
if sdk[cxx.target.platform]['cxxlib'] == 'stdc++':
# Switch libc++ to libstdc++ for protobuf linkage.
cxx.cxxflags.remove('-stdlib=libc++')
cxx.linkflags.remove('-stdlib=libc++')
cxx.linkflags.remove('-lc++')
cxx.cxxflags += ['-stdlib=libstdc++']
cxx.linkflags += ['-stdlib=libstdc++']
cxx.linkflags += ['-lstdc++']
rvalue = SdkHelpers()