yaml-cpp
yaml-cpp copied to clipboard
Restructure CMake Build System to Follow Modern Best Practices
Restructure CMake Build System to Follow Modern Best Practices
Problem
The current yaml-cpp build system has several issues:
-
It uses
file(GLOB ...)for finding source files, which:- Forces CMake to scan the filesystem during each configuration run
- Makes builds non-deterministic
- Doesn't detect new files automatically until reconfiguration
- Slows down configuration, especially in large projects
-
The build system structure doesn't follow modern CMake organization:
- All build logic is in a single monolithic CMakeLists.txt
- Library targets, source files, and properties are defined in the root file
- Lacks proper modularization, making maintenance challenging
Solution
This MR implements a proper hierarchical CMake structure that:
-
Follows modern CMake best practices:
- Moves library target definition to
src/CMakeLists.txt - Moves source file management to the directories where files are located
- Uses proper
add_subdirectory()approach instead of globbing - Explicitly lists source files for better control and determinism
- Moves library target definition to
-
The structure establishes clear responsibilities:
- Root CMakeLists.txt: Project configuration, options, and installation
- src/CMakeLists.txt: Library definition, core source files, properties
- src/contrib/CMakeLists.txt: Contrib module source files
Benefits
- Improved Build Performance: Eliminates slow filesystem scanning during configuration
- Deterministic Builds: Source files are explicitly listed, not dynamically discovered
- Better Scalability: Adding new source directories is simple with this structure
- Clearer Organization: Build logic is located close to the source files it manages
- Easier Maintenance: Each component manages its own sources
- Easier Integration: Better compatibility with build systems that cache configurations
- Follows Best Practices: Aligns with modern CMake recommendations
Implementation Details
The implementation follows a carefully structured approach:
-
Root CMakeLists.txt:
- Defines project, options, and configuration settings
- Uses
add_subdirectory(src)to delegate library building - Handles installation and packaging
-
src/CMakeLists.txt:
- Creates the library target (
add_library(yaml-cpp)) - Sets library properties and compile options
- Lists all core source files explicitly
- Adds the contrib subdirectory
- Creates the library target (
-
src/contrib/CMakeLists.txt:
- Lists all contrib source files explicitly
- Adds them to the main target conditionally based on options
The provided script can automatically generate this structure from an existing yaml-cpp repository.
Notes for Maintainers
With this new structure:
- New source files should be added to the CMakeLists.txt in their directory
- New source directories would add their own CMakeLists.txt and be included via
add_subdirectory() - The structure can easily scale to more complex organization as the library grows