bazel/docs/developer_workflow.md
This document describes the Server Developer workflow for modifying Bazel build definitions.
A build target is defined in the directory where its source code exists. To create a target that compiles src/mongo/hello_world.cpp, you would create src/mongo/BUILD.bazel.
src/mongo/BUILD.bazel would contain:
mongo_cc_binary(
name = "hello_world",
srcs = [
"hello_world.cpp"
],
}
Once you've obtained bazel by running python buildscripts/install_bazel.py, you can then build this target via "bazel build":
bazel build //src/mongo:hello_world
Or run this target via "bazel run":
bazel run //src/mongo:hello_world
The full target name is a combination between the directory of the BUILD.bazel file and the target name:
//{BUILD.bazel dir}:{targetname}
Bazel makes use of static analysis wherever possible to improve execution and querying speed. As part of this, source and header files must not be declared dynamically (ex. glob, wildcard, etc). Instead, you'll need to manually add a reference to each header or source file you add into your build target.
mongo_cc_binary(
name = "hello_world",
srcs = [
"hello_world.cpp",
"new_source.cpp" # If adding a source file
],
hdrs = [
"new_header.h" # If adding a header file
],
}
The DevProd Build Team created MongoDB-specific macros for the different types of build targets you may want to specify. These include:
Creating a new library is similar to the steps above for creating a new binary. A new mongo_cc_library definition would be created in the BUILD.bazel file.
mongo_cc_library(
name = "new_library",
srcs = [
"new_library_source_file.cpp"
]
}
If a library or binary depends on another library, this must be declared in the deps section of the target. The syntax for referring to the library is the same syntax used in the bazel build/run command.
mongo_cc_library(
name = "new_library",
# ...
}
mongo_cc_binary(
name = "hello_world",
srcs = [
"hello_world.cpp",
],
deps = [
":new_library", # if referring to the library declared in the same directory as this build file
# "//src/mongo:new_library" # absolute path
# "sub_directory:new_library" # relative path of a subdirectory
],
}
Note: This feature is still in development; see https://jira.mongodb.org/browse/SERVER-80396 for details)
To run clang-tidy via Bazel, do the following:
bazel build --config=clang-tidy src/...environment_buffer), run the following command (note that _with_debug suffix on the target): bazel build --config=clang-tidy src/mongo/db/commands:environment_buffer_with_debugTesting notes:
cpp file to generate a bugprone-incorrect-roundings warning:const double f = 1.0;
const int foo = (int)(f + 0.5);
Follow this loop to figure out where the header needs to be added
bazel build //src/...scoped_timer.h is part of scope_timer library so add //src/mongo/db/exec:scoped_timer to deps field (this will take care of scoped_timer.h transitive dependencies). If not add the header directly to the hdrs field of the library that's failing to compile.bazel build //src/...If you've put in a significant amount of work to try to get a header added and have found to get it added to the right place (usually alongside the associated .cpp file, having all dependents add that library as a dep) will take a significant refactor, create a SERVER ticket explaining the problem, solution, and complexity required to resolve it. Then, open up src/mongo/BUILD.bazel and add the header to "core_headers" file group referencing your ticket in a TODO comment.
This is very much a last resort and should only be done if the refactor will take a very significant amount of time and is blocking other work.