cmakelist_gen

Crates.iocmakelist_gen
lib.rscmakelist_gen
version0.1.0
created_at2025-12-22 10:46:35.330512+00
updated_at2025-12-22 10:46:35.330512+00
descriptionA CMakeLists.txt generator for Rust projects
homepage
repositoryhttps://github.com/Robot-Exp-Platform/cmake-gen
max_upload_size
id1999526
size18,627
(yixing312)

documentation

README

cmake-gen

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.

🚀 Features

  • 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.


🛠 Usage

Basic Example

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(())
}


🏗 Core Components

CMakeProject

The root container for your CMake project.

  • Metadata: Define project name, version, and minimum CMake requirements.
  • Global Settings: Set the C++ standard (C++11 through C++23).
  • Variables: Define custom CMake variables via set_variable.

TargetBuilder

Managed within the add_target closure, it configures specific build artifacts.

  • Source Management: Add source files to the target.
  • Visibility Control: Explicitly set PUBLIC, PRIVATE, or INTERFACE for includes and links.
  • Install Logic: Quickly generate install(TARGETS ...) commands with standard destinations (lib/, bin/, include/).

Dependency

  • SystemDep: Corresponds to find_package(...) with support for versions and required components.
  • FetchDep: Corresponds to FetchContent, automating the download and integration of remote repositories.

📂 Output Preview

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
)
Commit count: 0

cargo fmt