| Crates.io | cmakelist_gen |
| lib.rs | cmakelist_gen |
| version | 0.1.0 |
| created_at | 2025-12-22 10:46:35.330512+00 |
| updated_at | 2025-12-22 10:46:35.330512+00 |
| description | A CMakeLists.txt generator for Rust projects |
| homepage | |
| repository | https://github.com/Robot-Exp-Platform/cmake-gen |
| max_upload_size | |
| id | 1999526 |
| size | 18,627 |
A type-safe, fluent Rust library for generating modern CMakeLists.txt files. Designed specifically for developers who want to manage C++ builds within Rust-based ecosystems (like robotics) without manually writing error-prone CMake scripts.
Type Safety: Prevents common syntax errors by using Rust enums for C++ versions, library types, and visibility levels.
Modern CMake: Generates target-based commands (target_link_libraries, target_sources) rather than global variables.
Logical Hierarchy: Uses a closure-based API to provide natural visual indentation in your Rust code, mirroring the structure of a CMakeLists.txt file.
Dependency Management: Seamlessly handles both system-wide packages (find_package) and remote Git repositories (FetchContent).
Installation Support: Built-in support for generating standard install() rules for libraries, binaries, and headers.
This example demonstrates setting up a project with C++20, a system dependency (Eigen), a remote dependency (JSON), and a shared library target with installation rules.
use cmake_gen::{
project::CMakeProject,
types::{CxxVersion, FetchDep, LibType, SystemDep, Visibility},
};
fn main() -> std::io::Result<()> {
let mut file = std::fs::File::create("CMakeLists.txt")?;
CMakeProject::new("RobotCore", "0.1.0")
.cmake_min_version("3.14")
.cxx_standard(CxxVersion::Cxx20)
// Add a system dependency (find_package)
.find_package(
SystemDep::new("Eigen")
.version("3.3")
.component("Core")
)
// Add a remote Git dependency (FetchContent)
.fetch_git(FetchDep::new(
"json",
"https://github.com/nlohmann/json",
"v3.11.2",
))
// Define a target using a closure for clear scoping
.add_target("perception", LibType::Shared, |t| {
t.source("src/main.cpp")
.include("include", Visibility::Public)
.link("Eigen3::Eigen", Visibility::Private)
.install() // Apply default installation paths
})
.render(&mut file)?;
Ok(())
}
CMakeProjectThe root container for your CMake project.
set_variable.TargetBuilderManaged within the add_target closure, it configures specific build artifacts.
PUBLIC, PRIVATE, or INTERFACE for includes and links.install(TARGETS ...) commands with standard destinations (lib/, bin/, include/).Dependencyfind_package(...) with support for versions and required components.FetchContent, automating the download and integration of remote repositories.The generated CMakeLists.txt is formatted for readability with automated comments:
# Generated by cmake-gen
cmake_minimum_required(VERSION 3.14)
# Project Information
project(RobotCore VERSION 0.1.0)
# C++ Standard
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# Dependencies
include(FetchContent)
find_package(Eigen 3.3 REQUIRED COMPONENTS Core)
FetchContent_Declare(
json
GIT_REPOSITORY https://github.com/nlohmann/json
GIT_TAG v3.11.2
)
FetchContent_MakeAvailable(json)
# Targets
add_library(perception SHARED)
target_sources(perception PRIVATE src/main.cpp)
target_include_directories(perception PUBLIC include)
target_link_libraries(perception PRIVATE Eigen3::Eigen)
install(TARGETS perception
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
RUNTIME DESTINATION bin
INCLUDES DESTINATION include
)