NOTE: Currently only tested on macOS
NOTE: Adjust the versions and names below according to your project's needs.
Add the following to your project's WORKSPACE
file:
# Add support for foreign build systems
http_archive(
name = "rules_foreign_cc",
sha256 = "8e5605dc2d16a4229cb8fbe398514b10528553ed4f5f7737b663fdd92f48e1c2",
strip_prefix = "rules_foreign_cc-0.13.0",
url = "https://github.com/bazel-contrib/rules_foreign_cc/releases/download/0.13.0/rules_foreign_cc-0.13.0.tar.gz",
)
load("@rules_foreign_cc//foreign_cc:repositories.bzl", "rules_foreign_cc_dependencies")
# This sets up some common toolchains for building targets. For more details, please see
# https://bazelbuild.github.io/rules_foreign_cc/0.13.0/flatten.html#rules_foreign_cc_dependencies
rules_foreign_cc_dependencies(
cmake_version = "3.31.1",
)
# Add SDL3 support
http_archive(
name = "com_github_sdl",
build_file = "//third_party:sdl.BUILD",
urls = ["https://github.com/libsdl-org/SDL/releases/download/release-3.2.0/SDL3-3.2.0.tar.gz"],
strip_prefix = "SDL3-3.2.0",
)
If not already present, create a third_party directory in your workspace root. Create a third_party/sdl.BUILD
file,
and add the following:
package(default_visibility = ["//visibility:public"])
load("@rules_foreign_cc//foreign_cc:defs.bzl", "cmake")
filegroup(
name = "srcs",
srcs = glob(["**"]),
visibility = ["//visibility:public"],
)
cmake (
name = "sdl3",
lib_source = ":srcs",
defines = select({
"@bazel_tools//src/conditions:darwin": [
"CMAKE_OSX_DEPLOYMENT_TARGET=10.13",
],
}),
out_include_dir = "include",
out_shared_libs = select({
"@bazel_tools//src/conditions:darwin": ["libSDL3.0.dylib"],
}),
visibility = ["//visibility:public"],
)
NOTE: The sdl.BUILD file location should match the one in the WORKSPACE file.
- Create a minimal
main.cpp
to test build
// @file main.cpp
#include <iostream>
#include <SDL3/SDL.h>
int main() {
auto ret = SDL_Init(0);
std::cout << "SDL init returned: " << ret << std::endl;
SDL_Quit();
return 0;
}
- Create a
BUILD
file for this test program
cc_binary(
name = "sdl3-test",
srcs = [
"main.cpp"
],
deps = [
"@com_github_sdl//:sdl3",
]
)
- Run the test file
bazel run //path/to/program:sdl3-test