Forging Our Own Window:
Building GLFW From Source
Every epic C++ adventure eventually runs into the dreaded boss battle of dependency management. Until now, getting RoboEngine to compile meant hunting down a prebuilt GLFW binary package, unpacking it to the right directory, and praying your compiler matched whatever toolchain built those .lib and .dll files.
I slayed that dragon for good!
It was a surgical strike, just 35 lines added and 13 removed across my CMakeLists.txt and .env.cmake.example that teaches CMake how to compile GLFW 3.4 directly from source using the exact same compiler as the engine, linking it statically right into the executable.
The payoff?
No more glfw3.dll tagalong at runtime, zero toolchain mismatch errors, and if you don’t even have GLFW on your machine, CMake fetches and builds it automatically. One less thing to install before hitting Run!
Before:
At the Mercy of Prebuilt Binaries
Previously, GLFW lived in a downloaded binary bundle on disk. Your .env.cmake pointed GLFW_PATH at a precompiled folder (like glfw-3.3.4.bin.WIN64), and CMakeLists.txt wired up the include and library directories by hand:
set(GLFW_INCLUDE "${GLFW_PATH}/include")
set(GLFW_LIB "${GLFW_PATH}/lib-static-ucrt")
...
target_include_directories(${PROJECT_NAME} PUBLIC ... ${GLFW_INCLUDE} ...)
target_link_directories(${PROJECT_NAME} PUBLIC ... ${GLFW_LIB})
target_link_libraries(${PROJECT_NAME} imgui glfw3 vulkan-1)
Even our ImGui target had to manually add ${GLFW_INCLUDE} so its GLFW backend could find the headers.
Worse yet, prebuilt binaries are chained to the exact compiler that forged them. A warning comment at the top of the old CMakeLists.txt spelled out the trap: if your IDE defaulted to MSVC, the linker would flatly refuse to link a MinGW-built GLFW library. Any mismatch between your toolchain and the downloaded package meant instant linker grief.
The Upgrade:
GLFW as a CMake Subproject
Since GLFW ships with its own well-behaved CMakeLists.txt, we can fold it directly into RoboEngine’s build pipeline. The new CMake block pulls off the switch in three clean steps:
1. Trim the Fat & Force a Static Build
GLFW reads several cache options the moment its subproject is configured, so we set those flags up front to strip out examples, tests, docs, and install rules, and lock BUILD_SHARED_LIBS to OFF so everything compiles statically:
set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE)
set(GLFW_INSTALL OFF CACHE BOOL "" FORCE)
set(BUILD_SHARED_LIBS OFF CACHE BOOL "" FORCE) # Static GLFW—no DLL needed!
2. Use a Local Source Tree or Fetch One Automatically
GLFW_PATH now points to a GLFW source directory. If CMake spots a CMakeLists.txt inside it, it builds directly from disk; otherwise, it pulls a shallow clone of tag 3.4 straight from GitHub:
if(DEFINED GLFW_PATH AND EXISTS "${GLFW_PATH}/CMakeLists.txt")
add_subdirectory(${GLFW_PATH} ${CMAKE_BINARY_DIR}/glfw EXCLUDE_FROM_ALL)
else()
include(FetchContent)
FetchContent_Declare(glfw
GIT_REPOSITORY https://github.com/glfw/glfw.git
GIT_TAG 3.4
GIT_SHALLOW TRUE
)
FetchContent_GetProperties(glfw)
if(NOT glfw_POPULATED)
FetchContent_Populate(glfw)
endif()
add_subdirectory(${glfw_SOURCE_DIR} ${glfw_BINARY_DIR} EXCLUDE_FROM_ALL)
endif()
This mirrors the local-or-download pattern we already use for EnTT and nlohmann/json, with one key twist: for header-only libraries, FetchContent_Populate intentionally skips their CMake projects. GLFW actually needs to be compiled, so we mount it via add_subdirectory with EXCLUDE_FROM_ALL, ensuring CMake builds only the targets RoboEngine actually links.
3. Link Modern Targets, Not Raw Paths
GLFW’s CMake project exports a clean glfw target that carries its own include paths automatically. Linking that target wipes out all our manual path wrangling:
target_link_libraries(imgui PUBLIC glfw) # Automatically forwards GLFW's include directories
...
target_link_libraries(${PROJECT_NAME} imgui glfw vulkan-1)
GLFW_INCLUDE, GLFW_LIB, and target_link_directories vanish completely. Better still, because imgui links glfw as PUBLIC, anything linking ImGui inherits GLFW’s headers for free.
The example environment config updated right alongside it:
# GLFW *source* folder (has a CMakeLists.txt; built with the project). Optional: downloaded if unset.
# e.g. git clone --branch 3.4 https://github.com/glfw/glfw.git
set(GLFW_PATH X:/dev/Libraries/glfw-3.4)
Traps & Mobs in the Build System
Even a 35-line CMake upgrade hides a few subtle landmines if you aren’t watching your step:
Set subproject options before add_subdirectory—and use FORCE
- GLFW’s option() calls evaluate the cache the instant add_subdirectory runs. Setting flags afterward does nothing, and omitting FORCE lets stale values from an earlier CMake cache silently override your intent.
BUILD_SHARED_LIBS is a global switch
- It isn’t a GLFW-specific variable, CMake’s add_library() obeys it everywhere. Forcing it OFF gives us a static GLFW, but it also forces any subsequent dependency without an explicit STATIC/SHARED keyword to build statically too. Great for our self-contained binary, just something to keep in mind before adding shared libraries later.
Old GLFW_PATH values silently fall back to downloading
- If someone’s existing .env.cmake still points at an old prebuilt binary folder, CMake won’t throw an error, that folder lacks a CMakeLists.txt, so it quietly downloads GLFW 3.4 instead. A status line in the configure output (Using GLFW source at: … (downloaded)) makes it obvious which path won.
Pick your compiler before project()
- We kept the compiler selection warning at the top of CMakeLists.txt, just updated for the new reality: we no longer need to match a prebuilt binary’s toolchain, we just need to lock in MinGW before project() runs so CMake doesn’t cling to the IDE’s default choice.
FetchContent_Populate is living on borrowed time
- Calling FetchContent_Populate directly on a declared dependency was deprecated in CMake 3.30 (CMP0169). It works fine today (and RoboEngine targets CMake 3.11+), though a future cleanup can swap GLFW over to FetchContent_MakeAvailable since we actually want GLFW’s CMake targets.
Next Up on the Quest
In the last DevBlog, we taught the editor to warn you about unsaved changes when clicking New or Open, but clicking the window’s [X] button still ruthlessly nuked your unsaved work without a word! In our next DevBlog, we’ll tackle intercepting window close events so RoboEngine asks to save before shutting its doors.
