Skip to content

Instantly share code, notes, and snippets.

@k-bharadwaj
Last active January 24, 2025 04:02
Show Gist options
  • Save k-bharadwaj/7f0922425a6ac6dd4b938894563b27a4 to your computer and use it in GitHub Desktop.
Save k-bharadwaj/7f0922425a6ac6dd4b938894563b27a4 to your computer and use it in GitHub Desktop.
Building SDL3 (or any CMake project) with Bazel

Building SDL3 (or any CMake project) with Bazel

NOTE: Currently only tested on macOS

Pre-requisites

  • Install Homebrew
  • Install bazel via brew install bazel
  • If needed, create a project

Building SDL3

NOTE: Adjust the versions and names below according to your project's needs.

Add Bazel rules_foreign_cc and SDL3 locations to the workspace

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",
)

Add an sdl.BUILD file

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.

Testing changes

  1. 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;
}
  1. Create a BUILD file for this test program
cc_binary(
    name = "sdl3-test",
    srcs = [
        "main.cpp"
    ],
    deps = [
        "@com_github_sdl//:sdl3",
    ]
)
  1. Run the test file
bazel run //path/to/program:sdl3-test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment