You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The `CMake` format generates `CMakeLists.txt` and `CMakePresets.json` files for building Rice C++ bindings.
4
+
5
+
Rice supports building extensions with either [extconf.rb](https://ruby-rice.github.io/4.x/packaging/extconf.rb/) or [CMake](https://ruby-rice.github.io/4.x/packaging/cmake/). While `extconf.rb` works for simple bindings, CMake is vastly superior for anything more complex — it provides better cross-platform support, dependency management, and build configuration.
6
+
7
+
**Important:** CMake generation must run after Rice generation because it scans the output directory for `*-rb.cpp` files. If no Rice output exists, the generated CMake source lists will be empty.
8
+
9
+
## Getting Started
10
+
11
+
See [Getting Started](getting_started.md) for a step-by-step guide.
12
+
13
+
## Output
14
+
15
+
See [Output](output.md) for details on the generated files.
16
+
17
+
## Filtering
18
+
19
+
See [Filtering](filtering.md) for how to exclude files from the generated CMake build.
The CMake config supports a `skip` option to exclude specific `*-rb.cpp` files from the generated `CMakeLists.txt`. However, in most cases it's better to add skip patterns to your Rice config instead — that way the problematic files are never generated, and CMake won't find them to include. The CMake `skip` is useful as a quick fix when you have stale generated files on disk that you don't want to recompile.
This guide walks you through generating CMake build files for Rice C++ bindings.
4
+
5
+
## Prerequisites
6
+
7
+
You must first generate Rice C++ bindings. See [Getting Started with Rice](../cpp/getting_started.md).
8
+
9
+
## 1. Create a configuration file
10
+
11
+
Create a file named `cmake-bindings.yaml`:
12
+
13
+
```yaml
14
+
project: my_extension
15
+
output: ./ext/generated
16
+
format: CMake
17
+
18
+
include_dirs:
19
+
- "${CMAKE_CURRENT_SOURCE_DIR}/../include"
20
+
```
21
+
22
+
Key options:
23
+
24
+
- `project` — name used in the CMake `project()` command and build target. When omitted, only subdirectory `CMakeLists.txt` files are generated — useful when you manage the root project files yourself.
25
+
- `output`— directory containing the Rice `*-rb.cpp` files. `input` defaults to `output` for CMake.
26
+
- `include_dirs`— include directories added via `target_include_directories`. These are CMake expressions written directly into the generated `CMakeLists.txt`.
27
+
28
+
See [Configuration](../configuration.md) for all options.
29
+
30
+
## 2. Generate CMake files
31
+
32
+
Run this **after** generating Rice bindings:
33
+
34
+
```bash
35
+
ruby-bindgen cmake-bindings.yaml
36
+
```
37
+
38
+
## 3. Build
39
+
40
+
```bash
41
+
cd ./ext/generated
42
+
cmake --preset linux-debug # or macos-debug, msvc-debug, mingw-debug, etc.
43
+
cmake --build build/linux-debug
44
+
```
45
+
46
+
See [Output](output.md) for details on the available presets and generated files.
Copy file name to clipboardExpand all lines: docs/cmake/output.md
+19-50Lines changed: 19 additions & 50 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,50 +1,4 @@
1
-
# CMake Bindings
2
-
3
-
The `CMake` format generates `CMakeLists.txt` and `CMakePresets.json` files for building Rice C++ bindings.
4
-
5
-
**Important:** CMake generation must run after Rice generation because it scans the output directory for `*-rb.cpp` files. If no Rice output exists, the generated CMake source lists will be empty.
6
-
7
-
Rice supports building extensions with either [extconf.rb](https://ruby-rice.github.io/4.x/packaging/extconf.rb/) or [CMake](https://ruby-rice.github.io/4.x/packaging/cmake/). While `extconf.rb` works for simple bindings, CMake is vastly superior for anything more complex — it provides better cross-platform support, dependency management, and build configuration.
8
-
9
-
## Configuration
10
-
11
-
```yaml
12
-
format: CMake
13
-
project: my_extension
14
-
output: ./ext/generated
15
-
# input defaults to output for CMake and scans ./ext/generated for *-rb.cpp
16
-
17
-
include_dirs:
18
-
- "${CMAKE_CURRENT_SOURCE_DIR}/../headers"
19
-
```
20
-
21
-
See [configuration](configuration.md) for details on all options.
22
-
23
-
## Usage
24
-
25
-
Generate bindings in two passes:
26
-
27
-
```bash
28
-
# 1. Generate Rice C++ source files
29
-
ruby-bindgen rice-bindings.yaml
30
-
31
-
# 2. Generate CMake build files
32
-
ruby-bindgen cmake-bindings.yaml
33
-
```
34
-
35
-
Then build:
36
-
37
-
```bash
38
-
cd /path/to/output
39
-
cmake --preset linux-debug # or msvc-debug, macos-debug, etc.
40
-
cmake --build build/linux-debug
41
-
```
42
-
43
-
## Skipping Files
44
-
45
-
The CMake config supports a `skip` option to exclude specific `*-rb.cpp` files from the generated `CMakeLists.txt`. However, in most cases it's better to add skip patterns to your Rice/FFI config instead — that way the problematic files are never generated, and CMake won't find them to include. The CMake `skip` is useful as a quick fix when you have stale generated files on disk that you don't want to recompile.
46
-
47
-
## Output
1
+
# CMake Output
48
2
49
3
The CMake format scans the output directory for `*-rb.cpp` files and generates:
50
4
@@ -65,11 +19,11 @@ flowchart LR
65
19
RB --> C1 & C2
66
20
```
67
21
68
-
###Project Files (requires `project`)
22
+
## Project Files
69
23
70
24
When the `project` option is set, `ruby-bindgen` generates the root `CMakeLists.txt` and `CMakePresets.json`. When `project` is omitted, these files are **not** generated — only subdirectory `CMakeLists.txt` files are produced. This is useful when you want to create and manage the root project files yourself and only regenerate subdirectory files on subsequent runs.
71
25
72
-
###Top Level
26
+
## Top-level CMakeLists.txt
73
27
74
28
The top-level `CMakeLists.txt` is a complete project file that configures the entire build:
75
29
@@ -82,6 +36,8 @@ The top-level `CMakeLists.txt` is a complete project file that configures the en
82
36
83
37
For a well-documented example, see the [BitmapPlusPlus-ruby CMakeLists.txt](https://github.com/ruby-rice/BitmapPlusPlus-ruby/blob/main/ext/CMakeLists.txt). For details on how Rice uses CMake, see the Rice [CMake](https://ruby-rice.github.io/4.x/packaging/cmake/) documentation.
84
38
39
+
## CMakePresets.json
40
+
85
41
The top-level directory also gets a `CMakePresets.json` with build presets for all major platforms. For an example, see the [BitmapPlusPlus-ruby CMakePresets.json](https://github.com/ruby-rice/BitmapPlusPlus-ruby/blob/main/ext/CMakePresets.json). For details, see the Rice [CMakePresets.json](https://ruby-rice.github.io/4.x/packaging/cmake/#cmakepresetsjson) documentation.
86
42
87
43
| Preset | Platform | Compiler |
@@ -94,10 +50,23 @@ The top-level directory also gets a `CMakePresets.json` with build presets for a
94
50
95
51
All presets use [Ninja](https://ninja-build.org/) as the build generator and include appropriate compiler flags for each platform (visibility settings, debug info, optimization levels).
96
52
97
-
###Subdirectories
53
+
## Subdirectories
98
54
99
55
Each subdirectory containing `*-rb.cpp` files gets a minimal `CMakeLists.txt` that lists its source files and any nested subdirectories:
|`project`| none | Project name for the Ruby extension. Used for the `Init_` function name and project wrapper file names. Must be a valid C/C++ identifier. When provided, generates project wrapper files (`{project}-rb.cpp`, `{project}-rb.hpp`). When omitted, only per-file bindings are generated. |
48
-
|`include`| auto-generated | Path to a custom Rice include header. See [Include Header](cpp/cpp_bindings.md#include-header). |
48
+
|`include`| auto-generated | Path to a custom Rice include header. See [Include Header](cpp/output.md#include-header). |
49
49
50
50
## CMake Options
51
51
@@ -58,8 +58,8 @@ These options apply to all formats.
58
58
59
59
`ruby-bindgen` uses top-level toolchain keys to configure compiler settings for different platforms:
60
60
61
-
-**`clang-cl:`** - Used when `RUBY_PLATFORM` contains `mswin`or `mingw`
62
-
-**`clang:`** - Used on Linuxand macOS
61
+
-**`clang-cl:`** - Used when `RUBY_PLATFORM` contains `mswin`(MSVC toolchain)
62
+
-**`clang:`** - Used on Linux, macOS, and MinGW
63
63
64
64
| Option | Default | Description |
65
65
|------------|-------------|-------------|
@@ -73,9 +73,9 @@ You only need to include the toolchain sections for platforms you target.
Copy file name to clipboardExpand all lines: docs/cpp/cpp_bindings.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -18,7 +18,7 @@ See [Rice Output](output.md) for details on the generated files, including heade
18
18
19
19
## Build System
20
20
21
-
After generating Rice bindings, you will need to setup a build system for your extension. `ruby-bindgen` can generate [CMake build files](../cmake_bindings.md) to compile and link the generated bindings.
21
+
After generating Rice bindings, you will need to setup a build system for your extension. `ruby-bindgen` can generate [CMake build files](../cmake/cmake_bindings.md) to compile and link the generated bindings.
0 commit comments