Skip to content

Commit e5ddbd9

Browse files
committed
Introduce a pbproxy library to solve macOS linker issues.
On SDKs which use protobufs, the engine has objects compiled against a specific version of protobuf. Normally this is fine, we take care on Linux to use the same C++ ABI. On macOS however, we use libc++ to enable C++11 functionality, whereas the protobuf library has been compiled with libstc++. These ABIs are not compatible. To address the problem, we introduce PbHandle. PbHandle is a wrapper around protobuf::Message with two added pieces of state: whether or not the handle "owns" the message (and can free it in its destructor), and whether or not the handle was created by the engine (private) or created by SourceMod (local). Whenever we transfer a protobuf::Message pointer to SourceMod, we must take care to convert it to a Local version first. Whenever we transfer a protobuf pointer to the engine, we must convert it to a Private handle. For platforms with no ABI differences (almost all of them), the handle is a no-op. The private and local localities are compatible and no translation takes place. On macOS, CS:GO does require translation. SourceMod loads a tiny shim library that contains a copy of the protobuf sources compiled against the game's ABI. It then provides serialization and deserialization methods. SourceMod must not interact with the game's protobuf objects without first going through this proxy library. Note that PbHandle is not quite like unique_ptr. It can be converted into a PbHandle that does not destroy the underlying object. This is mainly because UserMessages.cpp has rather complex state, so it is useful to track locality without destroying an object. An unowned PbHandle must not outlive the owning PbHandle.
1 parent d525b46 commit e5ddbd9

10 files changed

+576
-99
lines changed

AMBuildScript

-18
Original file line numberDiff line numberDiff line change
@@ -553,25 +553,7 @@ class SMConfig(object):
553553
if builder.target.platform == 'linux':
554554
if sdk.name in ['csgo', 'blade']:
555555
compiler.linkflags.remove('-static-libstdc++')
556-
compiler.linkflags += ['-lstdc++']
557556
compiler.defines += ['_GLIBCXX_USE_CXX11_ABI=0']
558-
elif builder.target.platform == 'mac':
559-
if sdk.name in ['csgo']:
560-
# Switch libc++ to libstdc++ for protobuf linkage.
561-
compiler.cxxflags.remove('-stdlib=libc++')
562-
compiler.linkflags.remove('-stdlib=libc++')
563-
compiler.linkflags.remove('-lc++')
564-
565-
compiler.cxxflags += ['-stdlib=libstdc++']
566-
compiler.linkflags += ['-stdlib=libstdc++']
567-
compiler.linkflags += ['-lstdc++']
568-
569-
if 'c++1y' in compiler.cxxflags:
570-
compiler.cxxflags.remove('-std=c++1y')
571-
compiler.cxxflags += ['-std=c++11']
572-
elif 'c++14' in compiler.cxxflags:
573-
compiler.cxxflags.remove('-std=c++14')
574-
compiler.cxxflags += ['-std=c++11']
575557

576558
for path in paths:
577559
compiler.cxxincludes += [os.path.join(sdk.path, *path)]

core/AMBuilder

+71-33
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,43 @@ import os
44
project = SM.HL2Project(builder, 'sourcemod')
55

66
project.sources += [
7-
'MenuStyle_Valve.cpp',
8-
'logic_bridge.cpp',
9-
'smn_entities.cpp',
10-
'sm_stringutil.cpp',
11-
'MenuVoting.cpp',
12-
'smn_events.cpp',
13-
'frame_hooks.cpp',
14-
'smn_nextmap.cpp',
15-
'sourcemm_api.cpp',
167
'ChatTriggers.cpp',
17-
'smn_player.cpp',
18-
'sourcemod.cpp',
19-
'concmd_cleaner.cpp',
20-
'HalfLife2.cpp',
21-
'NextMap.cpp',
228
'ConCmdManager.cpp',
239
'ConVarManager.cpp',
24-
'PlayerManager.cpp',
25-
'TimerSys.cpp',
10+
'ConsoleDetours.cpp',
2611
'CoreConfig.cpp',
12+
'EventManager.cpp',
13+
'GameHooks.cpp',
14+
'HalfLife2.cpp',
2715
'Logger.cpp',
28-
'smn_halflife.cpp',
29-
'smn_console.cpp',
30-
'UserMessages.cpp',
3116
'MenuManager.cpp',
32-
'smn_hudtext.cpp',
33-
'smn_usermsgs.cpp',
3417
'MenuStyle_Base.cpp',
35-
'smn_keyvalues.cpp',
36-
'smn_vector.cpp',
37-
'EventManager.cpp',
3818
'MenuStyle_Radio.cpp',
19+
'MenuStyle_Valve.cpp',
20+
'MenuVoting.cpp',
21+
'NextMap.cpp',
22+
'PlayerManager.cpp',
23+
'TimerSys.cpp',
24+
'UserMessages.cpp',
25+
'concmd_cleaner.cpp',
26+
'frame_hooks.cpp',
27+
'logic_bridge.cpp',
28+
'pb_handle.cpp',
3929
'sm_autonatives.cpp',
40-
'ConsoleDetours.cpp',
30+
'sm_stringutil.cpp',
4131
'smn_commandline.cpp',
42-
'GameHooks.cpp',
32+
'smn_console.cpp',
33+
'smn_entities.cpp',
34+
'smn_events.cpp',
35+
'smn_halflife.cpp',
36+
'smn_hudtext.cpp',
37+
'smn_keyvalues.cpp',
38+
'smn_nextmap.cpp',
39+
'smn_player.cpp',
40+
'smn_usermsgs.cpp',
41+
'smn_vector.cpp',
42+
'sourcemm_api.cpp',
43+
'sourcemod.cpp',
4344
]
4445

4546
for sdk_name in SM.sdks:
@@ -57,18 +58,20 @@ for sdk_name in SM.sdks:
5758
builder.sourcePath
5859
]
5960

61+
pb_includes = []
6062
if sdk.name == 'csgo':
61-
compiler.cxxincludes += [
63+
pb_includes = [
6264
os.path.join(sdk.path, 'common', 'protobuf-2.5.0', 'src'),
6365
os.path.join(sdk.path, 'public', 'engine', 'protobuf'),
6466
os.path.join(sdk.path, 'public', 'game', 'shared', 'csgo', 'protobuf')
6567
]
6668
elif sdk.name == 'blade':
67-
compiler.cxxincludes += [
69+
pb_includes = [
6870
os.path.join(sdk.path, 'common', 'protobuf-2.5.0', 'src'),
6971
os.path.join(sdk.path, 'public', 'engine', 'protobuf'),
7072
os.path.join(sdk.path, 'public', 'game', 'shared', 'berimbau', 'protobuf')
7173
]
74+
compiler.cxxincludes += pb_includes
7275

7376
if compiler.like('msvc'):
7477
compiler.defines += ['_ALLOW_KEYWORD_MACROS']
@@ -84,9 +87,9 @@ for sdk_name in SM.sdks:
8487
compiler.linkflags += ['-Wl,--exclude-libs=libprotobuf.a']
8588
elif builder.target.platform == 'mac':
8689
if arch == 'x86':
87-
lib_path = os.path.join(sdk.path, 'lib', 'osx32', 'release', 'libprotobuf.a')
90+
lib_path = os.path.join(sdk.path, 'lib', 'osx32', 'release', 'libprotobuf-libcxx.a')
8891
elif arch == 'x64':
89-
lib_path = os.path.join(sdk.path, 'lib', 'osx64', 'release', 'libprotobuf.a')
92+
lib_path = os.path.join(sdk.path, 'lib', 'osx64', 'release', 'libprotobuf-libcxx.a')
9093
elif builder.target.platform == 'windows':
9194
msvc_ver = compiler.version
9295
vs_year = ''
@@ -111,18 +114,53 @@ for sdk_name in SM.sdks:
111114
'vprof_tool.cpp',
112115
]
113116

117+
pb_sources = []
114118
if sdk.name == 'csgo':
115-
binary.sources += [
119+
pb_sources = [
116120
os.path.join(sdk.path, 'public', 'engine', 'protobuf', 'netmessages.pb.cc'),
117121
os.path.join(sdk.path, 'public', 'game', 'shared', 'csgo', 'protobuf', 'cstrike15_usermessages.pb.cc'),
118122
os.path.join(sdk.path, 'public', 'game', 'shared', 'csgo', 'protobuf', 'cstrike15_usermessage_helpers.cpp'),
119123
]
120124
elif sdk.name == 'blade':
121-
binary.sources += [
125+
pb_sources = [
122126
os.path.join(sdk.path, 'public', 'engine', 'protobuf', 'netmessages.pb.cc'),
123127
os.path.join(sdk.path, 'public', 'game', 'shared', 'berimbau', 'protobuf', 'berimbau_usermessages.pb.cc'),
124128
os.path.join(sdk.path, 'public', 'game', 'shared', 'berimbau', 'protobuf', 'berimbau_usermessage_helpers.cpp'),
125129
]
130+
if len(pb_sources):
131+
binary.sources += pb_sources
132+
binary.compiler.cxxdefines += ['PROTOBUF_ENABLE']
126133

127-
SM.binaries += builder.Add(project)
134+
if builder.target.platform == 'mac' and sdk.name in ['csgo']:
135+
# We need a proxy library since the game uses libstdc++.
136+
pb_binary = SM.HL2Library(builder, 'pbproxy.' + sdk.ext, sdk, arch)
137+
pb_binary.sources += pb_sources
138+
pb_binary.sources += ['pb_proxy.cpp']
139+
pb_binary.compiler.cxxincludes += pb_includes
140+
141+
# Switch from libc++ to libstdc++.
142+
pb_binary.compiler.cxxflags.remove('-stdlib=libc++')
143+
pb_binary.compiler.linkflags.remove('-lc++')
144+
pb_binary.compiler.linkflags.remove('-stdlib=libc++')
145+
pb_binary.compiler.cxxflags.append('-stdlib=libstdc++')
146+
pb_binary.compiler.linkflags.append('-lstdc++')
147+
pb_binary.compiler.linkflags.append('-stdlib=libstdc++')
148+
if '-std=c++1y' in pb_binary.compiler.cxxflags:
149+
pb_binary.compiler.cxxflags.remove('-std=c++1y')
150+
elif '-std=c++14' in pb_binary.compiler.cxxflags:
151+
pb_binary.compiler.cxxflags.remove('-std=c++14')
152+
153+
if arch == 'x86':
154+
pb_lib_path = os.path.join(sdk.path, 'lib', 'osx32', 'release', 'libprotobuf.a')
155+
elif arch == 'x64':
156+
pb_lib_path = os.path.join(sdk.path, 'lib', 'osx64', 'release', 'libprotobuf.a')
157+
pb_binary.compiler.linkflags.append(pb_lib_path)
158+
159+
SM.binaries += [builder.Add(pb_binary)]
128160

161+
binary.compiler.cxxdefines += [
162+
'PROTOBUF_PROXY_ENABLE',
163+
'PROTOBUF_PROXY_BINARY_NAME="pbproxy.{}"'.format(sdk.ext),
164+
]
165+
166+
SM.binaries += builder.Add(project)

0 commit comments

Comments
 (0)