diff --git a/bazel/mongo_src_rules.bzl b/bazel/mongo_src_rules.bzl index 4c38a2d9c87..95bbf1ca5bb 100644 --- a/bazel/mongo_src_rules.bzl +++ b/bazel/mongo_src_rules.bzl @@ -1,7 +1,5 @@ # Common mongo-specific bazel build rules intended to be used in individual BUILD files in the "src/" subtree. -MONGO_DEFAULT_COPTS = ["-Isrc"] - # === Windows-specific compilation settings === # /RTC1 Enable Stack Frame Run-Time Error Checking; Reports when a variable is used without having been initialized (implies /Od: no optimizations) @@ -41,6 +39,31 @@ WINDOWS_RELEASE_COPTS = [ "/Od", ] +MONGO_GLOBAL_COPTS = ["-Isrc"] + select({ + "//bazel/config:windows_dbg": WINDOWS_DBG_COPTS, + "//bazel/config:windows_opt_on": WINDOWS_OPT_ON_COPTS, + "//bazel/config:windows_opt_off": WINDOWS_OPT_OFF_COPTS, + "//bazel/config:windows_opt_debug": WINDOWS_OPT_DBG_COPTS, + "//bazel/config:windows_opt_size": WINDOWS_OPT_SIZE_COPTS, + "//bazel/config:windows_release": WINDOWS_RELEASE_COPTS, + "//conditions:default": [], +}) + +MONGO_GLOBAL_DEFINES = select({ + "//bazel/config:dbg": ["MONGO_CONFIG_DEBUG_BUILD"], + "//conditions:default": ["NDEBUG"], +}) + +LIBUNWIND_DEPS = select({ + "//bazel/config:use_libunwind_enabled": ["//src/third_party/unwind:unwind"], + "//conditions:default": [], +}) + +LIBUNWIND_DEFINES = select({ + "//bazel/config:use_libunwind_enabled": ["MONGO_CONFIG_USE_LIBUNWIND"], + "//conditions:default": [], +}) + def mongo_cc_library( name, srcs = [], @@ -71,44 +94,64 @@ def mongo_cc_library( local_defines: macro definitions passed to all source and header files. """ - global_deps = [] - global_defines = [] - # Avoid injecting into unwind/libunwind_asm to avoid a circular dependency. if name not in ["unwind", "libunwind_asm"]: - global_deps = select({ - "//bazel/config:use_libunwind_enabled": ["//src/third_party/unwind:unwind"], - "//conditions:default": [], - }) - global_defines = select({ - "//bazel/config:use_libunwind_enabled": ["MONGO_CONFIG_USE_LIBUNWIND"], - "//conditions:default": [], - }) + deps += LIBUNWIND_DEPS + local_defines += LIBUNWIND_DEFINES native.cc_library( name = name, srcs = srcs, hdrs = hdrs, - deps = deps + global_deps, + deps = deps, visibility = visibility, testonly = testonly, - copts = MONGO_DEFAULT_COPTS + copts + - select({ - "//bazel/config:windows_dbg": WINDOWS_DBG_COPTS, - "//bazel/config:windows_opt_on": WINDOWS_OPT_ON_COPTS, - "//bazel/config:windows_opt_off": WINDOWS_OPT_OFF_COPTS, - "//bazel/config:windows_opt_debug": WINDOWS_OPT_DBG_COPTS, - "//bazel/config:windows_opt_size": WINDOWS_OPT_SIZE_COPTS, - "//bazel/config:windows_release": WINDOWS_RELEASE_COPTS, - "//conditions:default": [], - }), + copts = MONGO_GLOBAL_COPTS + copts, data = data, tags = tags, linkstatic = linkstatic, - local_defines = local_defines + global_defines + - select({ - "//bazel/config:dbg": ["MONGO_CONFIG_DEBUG_BUILD"], - "//conditions:default": ["NDEBUG"], - }), + local_defines = MONGO_GLOBAL_DEFINES + local_defines, + includes = [], + ) + +def mongo_cc_binary( + name, + srcs = [], + deps = [], + testonly = False, + visibility = None, + data = [], + tags = [], + copts = [], + linkopts = [], + linkstatic = False, + local_defines = []): + """Wrapper around cc_binary. + + Args: + name: The name of the library the target is compiling. + srcs: The source files to build. + deps: The targets the library depends on. + testonly: Whether or not the target is purely for tests. + visibility: The visibility of the target library. + data: Data targets the library depends on. + tags: Tags to add to the rule. + copts: Any extra compiler options to pass in. + linkopts: Any extra link options to pass in. + linkstatic: Whether or not linkstatic should be passed to the native bazel cc_test rule. + local_defines: macro definitions passed to all source and header files. + """ + + native.cc_binary( + name = name, + srcs = srcs, + deps = deps + LIBUNWIND_DEPS, + visibility = visibility, + testonly = testonly, + copts = MONGO_GLOBAL_COPTS + copts, + data = data, + tags = tags, + linkstatic = linkstatic, + local_defines = MONGO_GLOBAL_DEFINES + LIBUNWIND_DEFINES + local_defines, includes = [], ) diff --git a/buildscripts/bazel_testbuilds/BUILD.bazel b/buildscripts/bazel_testbuilds/BUILD.bazel new file mode 100644 index 00000000000..088b2b835dd --- /dev/null +++ b/buildscripts/bazel_testbuilds/BUILD.bazel @@ -0,0 +1,10 @@ +# Bazel build definitions used to test bazel features during build system development. + +load("//bazel:mongo_src_rules.bzl", "mongo_cc_binary") + +package(default_visibility = ["//visibility:public"]) + +mongo_cc_binary( + name = "unit_test", + srcs = ["unit_test.cpp"] +) diff --git a/buildscripts/bazel_testbuilds/unit_test.cpp b/buildscripts/bazel_testbuilds/unit_test.cpp new file mode 100644 index 00000000000..da64451ccae --- /dev/null +++ b/buildscripts/bazel_testbuilds/unit_test.cpp @@ -0,0 +1,8 @@ +// C++ compilation file used for testing bazel compilation. + +#include + +int main() { + printf("Hello World!\n"); + return 0; +} diff --git a/etc/evergreen_yml_components/definitions.yml b/etc/evergreen_yml_components/definitions.yml index 50458b9fe5e..2bfcb4c5a5a 100644 --- a/etc/evergreen_yml_components/definitions.yml +++ b/etc/evergreen_yml_components/definitions.yml @@ -2887,6 +2887,34 @@ tasks: vars: output: build/fast/third_party/unwind/libunwind.a +- name: compile_bazel_program + tags: [] + depends_on: + - name: version_expansions_gen + variant: generate-tasks-for-version + commands: + # TODO SERVER-81038: Remove "fetch bazel" once bazelisk is self-hosted. + - func: "fetch bazel" + - func: "scons compile" + vars: + targets: >- + build/fast/mongo/platform/visibility_test1 + separate_debug: off + # TODO SERVER-79852 remove "BAZEL_FLAGS=--config=local" flag + task_compile_flags: >- + BAZEL_BUILD_ENABLED=1 + BAZEL_INTEGRATION_DEBUG=1 + BAZEL_FLAGS=--config=local + ICECC= + --build-profile=fast + --ninja=disabled + --link-model=dynamic + --use-libunwind=off + --modules= + - func: "verify build output present" + vars: + output: build/fast/mongo/platform/visibility_test1 + ## compile - build all scons targets except unittests ## - name: compile_dist_test_half tags: [] @@ -9338,19 +9366,12 @@ task_groups: - compile_dist_test - <<: *compile_bazel_task_group_template - name: compile_bazel_dist_test_TG + name: compile_bazel_TG tasks: - compile_bazel_dist_test - -- <<: *compile_bazel_task_group_template - name: compile_bazel_c_and_asm_targets_TG - tasks: - compile_bazel_c_and_asm_targets - -- <<: *compile_bazel_task_group_template - name: compile_bazel_libunwind_TG - tasks: - compile_bazel_libunwind + - compile_bazel_program - <<: *compile_task_group_template name: compile_upload_benchmarks_TG diff --git a/etc/evergreen_yml_components/variants/compile_static_analysis.yml b/etc/evergreen_yml_components/variants/compile_static_analysis.yml index 4dc12feedb8..7b0e29e6c6b 100644 --- a/etc/evergreen_yml_components/variants/compile_static_analysis.yml +++ b/etc/evergreen_yml_components/variants/compile_static_analysis.yml @@ -226,9 +226,7 @@ buildvariants: expansions: compile_variant: *ubuntu2204-arm64-bazel-compile tasks: - - name: compile_bazel_dist_test_TG - - name: compile_bazel_c_and_asm_targets_TG - - name: compile_bazel_libunwind_TG + - name: compile_bazel_TG - <<: *linux-arm64-dynamic-compile-params name: &amazon-linux2-arm64-compile amazon-linux2-arm64-compile diff --git a/site_scons/site_tools/integrate_bazel.py b/site_scons/site_tools/integrate_bazel.py index 057c1641dc7..7590246589d 100644 --- a/site_scons/site_tools/integrate_bazel.py +++ b/site_scons/site_tools/integrate_bazel.py @@ -253,31 +253,30 @@ def bazel_batch_build_thread(log_dir: str) -> None: raise exc -def create_library_builder(env: SCons.Environment.Environment) -> None: - # The next section of builders are hook builders. These - # will be standin place holders for the original scons builders, and if bazel build is enabled - # these simply copy out the target from the underlying bazel build - if env.GetOption("link-model") in ["auto", "static"]: - lib_prefix = '' - name_prefix = 'Static' - else: - lib_prefix = 'SH' - name_prefix = 'Shared' - - builder = SCons.Builder.Builder( +def create_bazel_builder(builder): + return SCons.Builder.Builder( action=BazelCopyOutputsAction, - prefix=f'${lib_prefix}LIBPREFIX', - suffix=f'${lib_prefix}LIBSUFFIX', - src_suffix=env['BUILDERS'][f'{name_prefix}Library'].src_suffix, - source_scanner=env['BUILDERS'][f'{name_prefix}Library'].source_scanner, - target_scanner=env['BUILDERS'][f'{name_prefix}Library'].target_scanner, - emitter=env['BUILDERS'][f'{name_prefix}Library'].emitter, + prefix=builder.prefix, + suffix=builder.suffix, + src_suffix=builder.src_suffix, + source_scanner=builder.source_scanner, + target_scanner=builder.target_scanner, + emitter=SCons.Builder.ListEmitter([builder.emitter, bazel_target_emitter]), ) - env['BUILDERS']['BazelLibrary'] = builder - base_emitter = builder.emitter - new_emitter = SCons.Builder.ListEmitter([base_emitter, bazel_target_emitter]) - builder.emitter = new_emitter + +# The next section of builders are hook builders. These +# will be standin place holders for the original scons builders, and if bazel build is enabled +# these simply copy out the target from the underlying bazel build +def create_library_builder(env: SCons.Environment.Environment) -> None: + if env.GetOption("link-model") in ["auto", "static"]: + env['BUILDERS']['BazelLibrary'] = create_bazel_builder(env['BUILDERS']["StaticLibrary"]) + else: + env['BUILDERS']['BazelLibrary'] = create_bazel_builder(env['BUILDERS']["SharedLibrary"]) + + +def create_program_builder(env: SCons.Environment.Environment) -> None: + env['BUILDERS']['BazelProgram'] = create_bazel_builder(env['BUILDERS']["Program"]) # Establishes logic for BazelLibrary build rule @@ -355,6 +354,7 @@ def generate(env: SCons.Environment.Environment) -> None: # === Builders === create_library_builder(env) + create_program_builder(env) def shutdown_bazel_builer(): Globals.kill_bazel_thread_flag = True @@ -367,3 +367,4 @@ def generate(env: SCons.Environment.Environment) -> None: bazel_build_thread.start() else: env['BUILDERS']['BazelLibrary'] = env['BUILDERS']['Library'] + env['BUILDERS']['BazelProgram'] = env['BUILDERS']['Program'] diff --git a/src/mongo/platform/BUILD.bazel b/src/mongo/platform/BUILD.bazel index dc3f22bed39..ee5da17a026 100644 --- a/src/mongo/platform/BUILD.bazel +++ b/src/mongo/platform/BUILD.bazel @@ -1,4 +1,4 @@ -load("//bazel:mongo_src_rules.bzl", "mongo_cc_library") +load("//bazel:mongo_src_rules.bzl", "mongo_cc_library", "mongo_cc_binary") package(default_visibility = ["//visibility:public"]) @@ -13,3 +13,40 @@ mongo_cc_library( "visibility_test_libcommon.cpp", ], ) + +mongo_cc_library( + name="visibility_test_lib1", + deps=[ + ":visibility_test_libcommon" + ], + hdrs=[ + "visibility_test_lib1.h", + ], + srcs=[ + "visibility_test_lib1.cpp", + ], +) + +mongo_cc_library( + name="visibility_test_lib2", + deps=[ + ":visibility_test_lib1" + ], + hdrs=[ + "visibility_test_lib2.h", + ], + srcs=[ + "visibility_test_lib2.cpp", + ], +) + +mongo_cc_binary( + name="visibility_test1", + deps=[ + ":visibility_test_lib1", + "//src/mongo/util:exit_code", + ], + srcs=[ + "visibility_test1.cpp", + ], +) diff --git a/src/mongo/platform/SConscript b/src/mongo/platform/SConscript index f7f2f8976de..c54bbb5c86a 100644 --- a/src/mongo/platform/SConscript +++ b/src/mongo/platform/SConscript @@ -80,7 +80,7 @@ env.Library( MONGO_API_NAME="visibility_test_lib2", ) -visibility_test1 = env.Program( +visibility_test1 = env.BazelProgram( target="visibility_test1", source=[ "visibility_test1.cpp", diff --git a/src/mongo/util/BUILD.bazel b/src/mongo/util/BUILD.bazel new file mode 100644 index 00000000000..943587f64ce --- /dev/null +++ b/src/mongo/util/BUILD.bazel @@ -0,0 +1,10 @@ +load("//bazel:mongo_src_rules.bzl", "mongo_cc_library") + +package(default_visibility = ["//visibility:public"]) + +mongo_cc_library( + name="exit_code", + hdrs=[ + "exit_code.h", + ] +)