summaryrefslogtreecommitdiff
path: root/src/third_party/SConscript
blob: 9e0eaa8b6ef515ee83e839f59f861bc792f90932 (plain)
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
# -*- mode: python -*-
import SCons
import json

Import([
    'env',
    'get_option',
    'jsEngine',
    'use_libunwind',
    'use_system_version_of_library',
    'wiredtiger',
    'releaseBuild',
    'ssl_provider',
])

icuSuffix = '-57.1'
tomcryptSuffix = '-1.18.2'

thirdPartyEnvironmentModifications = {
    'abseil-cpp': {'CPPPATH': ['#/src/third_party/abseil-cpp/dist'], },
    'cares': {
        'CPPPATH': [
            '#src/third_party/cares/dist/include',
            '#src/third_party/cares/platform/${TARGET_OS}_${TARGET_ARCH}/install/include'
        ],
    },
    'croaring': {'CPPPATH': ['#src/third_party/croaring/dist'], },
    'fmt': {'CPPPATH': ['#src/third_party/fmt/dist/include'], },
    'immer': {'CPPPATH': ['#src/third_party/immer/dist'], },
    's2': {'CPPPATH': ['#src/third_party/s2'], },
    'safeint': {
        'CPPPATH': ['#src/third_party/SafeInt'],
        # SAFEINT_USE_INTRINSICS=0 for overflow-safe constexpr multiply. See comment in SafeInt.hpp.
        'CPPDEFINES': [('SAFEINT_USE_INTRINSICS', 0)],
    },
    'timelib': {'CPPPATH': ['#/src/third_party/timelib/dist'], },
    'unwind': {
        'CPPPATH':  [
            "#/src/third_party/unwind/platform/${TARGET_OS}_${TARGET_ARCH}/install/include",
        ]
    },
    'mozjs': {
        'CPPPATH': [
            '#/src/third_party/mozjs/include',
            '#/src/third_party/mozjs/mongo_sources',
            '#/src/third_party/mozjs/platform/' + env["TARGET_ARCH"] + "/" + env["TARGET_OS"] +
            "/include",
        ],
        'FORCEINCLUDES': ['js-config.h', ],
    },
    'murmurhash3': {'CPPPATH': ['#src/third_party/murmurhash3', ], },
    'librdkafka': {
        'CPPPATH': [
            '#/src/third_party/librdkafka/dist/src',
            '#/src/third_party/librdkafka/dist/src-cpp',
        ],
        'SYSLIBDEPS_PRIVATE': [
            'curl',
        ],
    },
}

def injectMozJS(thisEnv):
    thisEnv.InjectThirdParty(libraries=['mozjs'])

    if thisEnv.TargetOSIs('windows'):
        thisEnv.Append(CPPDEFINES=[
            '_SILENCE_CXX17_ITERATOR_BASE_CLASS_DEPRECATION_WARNING',
        ], )
    else:
        thisEnv.Append(CXXFLAGS=[
            '-Wno-non-virtual-dtor',
            '-Wno-invalid-offsetof',
        ], )

    thisEnv.Prepend(CPPDEFINES=[
        'JS_USE_CUSTOM_ALLOCATOR',
        'STATIC_JS_API=1',
    ])

    if get_option('spider-monkey-dbg') == "on":
        thisEnv.Prepend(CPPDEFINES=[
            'DEBUG',
            'JS_DEBUG',
            'JS_GC_ZEAL'
        ])

env.AddMethod(injectMozJS, 'InjectMozJS')

if not use_system_version_of_library('tcmalloc-gperf'):
    # GPerftools does this slightly differently than the others.
    thirdPartyEnvironmentModifications['gperftools'] = {
        'CPPPATH': ["#/src/third_party/gperftools/platform/${TARGET_OS}_${TARGET_ARCH}/include"]
    }

if not use_system_version_of_library('tcmalloc-google'):
    thirdPartyEnvironmentModifications['tcmalloc'] = {
        'CPPPATH': ['#/src/third_party/tcmalloc/dist'],
    }

    if get_option('link-model') == 'dynamic':
        thirdPartyEnvironmentModifications['tcmalloc']['CPPDEFINES'] = [
            ('MONGO_TCMALLOC_DYNAMIC_BUILD', 1)
        ]

if not use_system_version_of_library('pcre2'):
    thirdPartyEnvironmentModifications['pcre2'] = {
        'CPPPATH': ['#/src/third_party/pcre2/src'],
    }

if not use_system_version_of_library('boost'):

    # On at least Apple clang, proto throws this error.
    #
    # See https://github.com/boostorg/proto/issues/30.
    #
    # We use a generator so we can filter out conf tests, where applying this
    # flag could change their meaning.
    def NoErrorForUnknownWarningOptionGenerator(target, source, env, for_signature):
        if 'conftest' in str(target[0]):
            return str()
        return '-Wno-error=unknown-warning-option'

    thirdPartyEnvironmentModifications['boost'] = {
        'CPPPATH': ['#/src/third_party/boost'],

        # We could narror further to just clang on Darwin, but there is
        # little harm in applying for all clang.
        'NOERROR_FOR_UNKNOWN_WARNING_OPTION_GEN': NoErrorForUnknownWarningOptionGenerator,
        'CCFLAGS': ['$NOERROR_FOR_UNKNOWN_WARNING_OPTION_GEN'] if env.ToolchainIs('clang') else [],
        'SYSLIBDEPS': ['synchronization'] if env.ToolchainIs('msvc') else [],
    }

if not use_system_version_of_library('snappy'):
    platform_directory = "build_posix"

    if env.TargetOSIs('linux'):
        platform_directory = "build_linux_" + env['TARGET_ARCH']
    elif env.TargetOSIs('windows'):
        platform_directory = "build_windows"
    elif env.TargetOSIs('darwin'):
        platform_directory = "build_" + env["TARGET_OS"] + "_" + env["TARGET_ARCH"]

    thirdPartyEnvironmentModifications['snappy'] = {
        'CPPPATH': [
            '#/src/third_party/snappy/dist',
            '#/src/third_party/snappy/platform/build_all',
            '#/src/third_party/snappy/platform/' + platform_directory,
        ],
    }

# Valgrind is a header only include as valgrind.h includes everything we need
if not use_system_version_of_library('valgrind'):
    thirdPartyEnvironmentModifications['valgrind'] = {
        'CPPPATH': ['#/src/third_party/valgrind/include'],
    }

if not use_system_version_of_library('zlib'):
    thirdPartyEnvironmentModifications['zlib'] = {
        'CPPPATH': ['#/src/third_party/zlib'],
    }

if not use_system_version_of_library('zstd'):
    thirdPartyEnvironmentModifications['zstd'] = {
        'CPPPATH': ['#/src/third_party/zstandard/zstd/lib'],
    }

if not use_system_version_of_library('google-benchmark'):
    thirdPartyEnvironmentModifications['benchmark'] = {
        'CPPPATH': ['#/src/third_party/benchmark/dist/include'],
    }

if "tom" in env["MONGO_CRYPTO"]:
    thirdPartyEnvironmentModifications['tomcrypt'] = {
        'CPPPATH': ['#/src/third_party/tomcrypt' + tomcryptSuffix + '/src/headers'],
    }

if not use_system_version_of_library('tomcrypt'):
    thirdPartyEnvironmentModifications['tomcrypt_md5'] = {
        'CPPPATH': ['#/src/third_party/tomcrypt' + tomcryptSuffix + '/src/headers'],
        'CPPDEFINES': ['LTC_NO_PROTOTYPES'] if env.TargetOSIs('windows') else [],
    }

if not use_system_version_of_library('stemmer'):
    thirdPartyEnvironmentModifications['stemmer'] = {
        'CPPPATH': ['#/src/third_party/libstemmer_c/include'],
    }

# Note that the wiredtiger.h header is generated, so
# we want to look for it in the build directory not
# the source directory.
if wiredtiger and not use_system_version_of_library('wiredtiger'):
    thirdPartyEnvironmentModifications['wiredtiger'] = {
        'CPPPATH': ["#$BAZEL_OUT_DIR/src/third_party/wiredtiger"],
    }

if not use_system_version_of_library('yaml'):
    thirdPartyEnvironmentModifications['yaml'] = {
        'CPPPATH': ['#/src/third_party/yaml-cpp/yaml-cpp/include'],
        'CPPDEFINES': ['_SILENCE_CXX17_ITERATOR_BASE_CLASS_DEPRECATION_WARNING']
                      if env.ToolchainIs('msvc') else [],
    }

if not use_system_version_of_library('asio'):
    thirdPartyEnvironmentModifications['asio'] = {
        'CPPPATH': ['#/src/third_party/asio-master/asio/include'],
    }

if not use_system_version_of_library('intel_decimal128'):
    thirdPartyEnvironmentModifications['intel_decimal128'] = {
        'CPPPATH': ['#/src/third_party/IntelRDFPMathLib20U1/LIBRARY/src'],
    }

if not use_system_version_of_library('icu'):
    thirdPartyEnvironmentModifications['icu'] = {
        'CPPPATH': [
            '#/src/third_party/icu4c' + icuSuffix + '/source/common',
            '#/src/third_party/icu4c' + icuSuffix + '/source/i18n',
        ],
    }

if not use_system_version_of_library('bson'):

    platform_directory = "build_linux"

    if env['TARGET_ARCH'] == 's390x':
        platform_directory = "build_linux_s390x"

    if env.TargetOSIs('windows'):
        platform_directory = "build_windows"
    elif env.TargetOSIs('darwin'):
        platform_directory = "build_osx"

    thirdPartyEnvironmentModifications['bson'] = {
        'CPPPATH': [
            '#/src/third_party/libbson/dist/src/libbson/src',
            '#/src/third_party/libbson/' + platform_directory,
            '#/src/third_party/libbson/' + platform_directory + '/bson',
        ],
        'CPPDEFINES': ['BSON_STATIC'],
    }

if not use_system_version_of_library('libmongocrypt'):

    platform_directory = "build_linux"
    crypto_directory = "build_libcrypto"

    if env.TargetOSIs('windows'):
        platform_directory = "build_windows"
        crypto_directory = "build_cng"
    elif env.TargetOSIs('darwin'):
        platform_directory = "build_osx"
        crypto_directory = "build_commoncrypto"

    if get_option('ssl') == 'off':
        crypto_directory = "build_none"

    thirdPartyEnvironmentModifications['libmongocrypt'] = {
        'CPPPATH': [
            '#/src/third_party/libmongocrypt/dist/src',
            '#/src/third_party/libmongocrypt/dist/kms-message/src',
            '#/src/third_party/libmongocrypt/' + platform_directory,
            '#/src/third_party/libmongocrypt/' + crypto_directory,
        ],
        'CPPDEFINES': [
            'KMS_MSG_STATIC',
            'MONGOCRYPT_STATIC_DEFINE',
        ],
    }

if not use_system_version_of_library('protobuf'):
    thirdPartyEnvironmentModifications['protobuf'] = {
        'CPPPATH': ['#src/third_party/protobuf/dist/src'],
    }

if not use_system_version_of_library('grpc'):
    thirdPartyEnvironmentModifications['grpc'] = {
        'CPPPATH': ['#src/third_party/grpc/dist/include', '#src/third_party/grpc/dist/'],
    }
    thirdPartyEnvironmentModifications['re2'] = {
        'CPPPATH': ['#src/third_party/re2/dist'],
    }
    thirdPartyEnvironmentModifications['cares'] = {
        'CPPPATH': [
            '#src/third_party/cares/dist/include',
            '#src/third_party/cares/platform/${TARGET_OS}_${TARGET_ARCH}/install/include'
        ],
    }

# Vendored libunwind can be configured to use liblzma for decompressing
# minidebuginfo sections, but we disable that feature via
# `unwind/scripts/host-config.sh`. If vendored libunwind ever needs
# minidebuginfo support, we'd say so here, and we'd dynamically link with
# system lzma. Until then, it would be an unnecessary dynamic dependency.
# However, the system libunwind is assumed to need `lzma` unconditionally.
vendored_libunwind_needs_lzma = False

if vendored_libunwind_needs_lzma:
    thirdPartyEnvironmentModifications['unwind'] = {
        'SYSLIBDEPS_PRIVATE': [env['LIBDEPS_LZMA_SYSLIBDEP']],
    }


def injectThirdParty(thisEnv, libraries=[], parts=[]):
    libraries = thisEnv.Flatten([libraries])
    parts = thisEnv.Flatten([parts])
    for lib in libraries:
        mods = thirdPartyEnvironmentModifications.get(lib, None)
        if mods is None:
            continue
        if not parts:
            thisEnv.PrependUnique(**mods)
        else:
            for part in parts:
                thisEnv.PrependUnique({part: mods[part]})


env.AddMethod(injectThirdParty, 'InjectThirdParty')

env = env.Clone()

# Construct an empty object file that we can use to produce the
# library for every shim. This avoids the need to create and name a
# different empty source file for every third-party library, as we did
# in the past.

empty_source = env.Textfile(
    target='third_party_shim.cpp',
    source=str(),
)
env.Alias('generated-sources', empty_source)

empty_object = env.LibraryObject(target='third_party_shim', source=empty_source,
                                 NINJA_GENSOURCE_INDEPENDENT=True)


def shim_library(env, name, **kwargs):
    # Add the 'virtual-libdep' tag, which will prevent shim libraries
    # from actually being linked to. They don't provide any symbols,
    # so there is no need to do so. Instead, they just act as a node
    # in the library dependency graph to reach other libraries.
    libdeps_tags = kwargs.get('LIBDEPS_TAGS', env.get('LIBDEPS_TAGS', [])).copy()
    libdeps_tags.append('virtual-libdep')
    kwargs['LIBDEPS_TAGS'] = libdeps_tags
    return env.Library(
        target=f'shim_{name}',
        source=empty_object[0],
        # Since nothing will link to this library per the
        # `virtual-libdep` tag above, we can also skip installing it.
        AIB_IGNORE=True,
        **kwargs,
    )


env.AddMethod(shim_library, 'ShimLibrary')

if jsEngine:
    mozjsEnv = env.Clone()

wiredtigerEnv = env.Clone()
if wiredtiger:
    if use_system_version_of_library("wiredtiger"):
        wiredtigerEnv = wiredtigerEnv.Clone(SYSLIBDEPS=[
            env['LIBDEPS_WIREDTIGER_SYSLIBDEP'],
        ])
    else:
        wiredtigerEnv = wiredtigerEnv.Clone()
        wiredtigerEnv.InjectThirdParty(libraries=['wiredtiger'])
        wiredtigerEnv.SConscript('wiredtiger/SConscript', must_exist=1, exports={'env': wiredtigerEnv})
        wiredtigerEnv = wiredtigerEnv.Clone(LIBDEPS_INTERFACE=[
            'wiredtiger/wiredtiger',
        ])

if env.TargetOSIs('linux') and env['ENABLE_GRPC_BUILD']:

    protobufEnv = env.Clone(NINJA_GENSOURCE_INDEPENDENT=True)
    if use_system_version_of_library("protobuf"):
        protobufEnv = protobufEnv.Clone(
            SYSLIBDEPS=[
                env['LIBDEPS_PROTOBUF_SYSLIBDEP'],
                env['LIBDEPS_PROTOC_SYSLIBDEP'],
            ], )
    else:
        protobufEnv.SConscript(dirs=[
            'protobuf',
        ], duplicate=False, exports={'env': env},  must_exist=1,)
        protobufEnv = protobufEnv.Clone(LIBDEPS_INTERFACE=[
            'protobuf/protoc',
        ])
    protobufEnv.ShimLibrary(name="protobuf", )

    grpcEnv = env.Clone()
    if use_system_version_of_library("grpc"):
        grpcEnv = grpcEnv.Clone(SYSLIBDEPS=[
            env['LIBDEPS_GRPC_SYSLIBDEP'],
            env['LIBDEPS_GRPCXX_SYSLIBDEP'],
            env['LIBDEPS_GRPCXX_REFLECTION_SYSLIBDEP'],
        ])
    else:
        grpcEnv.SConscript(
   dirs=[
            'grpc',
        ], duplicate=False, exports={'env': env}, must_exist=1,)
        grpcEnv = grpcEnv.Clone(LIBDEPS_INTERFACE=[
            'grpc/grpc++_reflection',
        ])
    grpcEnv.ShimLibrary(name="grpc", )

if env.ShouldBuildStreams():
    libKafkaEnv = env.Clone()
    libKafkaEnv.InjectThirdParty(libraries=['librdkafka'])