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. This is typically run as a second pass after generating the Rice binding source files.
3
+
The `CMake` format generates `CMakeLists.txt` and `CMakePresets.json` files for building Rice C++ bindings. This is run as a second pass after generating the Rice binding source files.
4
4
5
-
## Usage
6
-
7
-
The CMake format scans the output directory for `*-rb.cpp` files produced by the Rice format and generates:
8
-
9
-
-**`CMakeLists.txt`** (top-level) - Project configuration with Rice fetching, Ruby detection, and source file listing
10
-
-**`CMakeLists.txt`** (per subdirectory) - Source file listing for each subdirectory
11
-
-**`CMakePresets.json`** - Build presets for Linux, macOS, MSVC, MinGW, and Clang on Windows (debug and release variants)
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.
12
6
13
7
## Configuration
14
8
15
9
```yaml
16
10
format: CMake
17
11
extension: my_extension
18
12
19
-
# CMake expressions for target_include_directories
20
13
include_dirs:
21
14
- "${CMAKE_CURRENT_SOURCE_DIR}/../headers"
22
15
```
23
16
24
-
### Options
25
-
26
-
| Option | Description |
27
-
|--------|-------------|
28
-
| `extension` | Project name used in `project()` and target name. Required. |
29
-
| `include_dirs` | List of include directory expressions added via `target_include_directories`. These are written directly into CMakeLists.txt, so CMake variables like `${CMAKE_CURRENT_SOURCE_DIR}` work. |
30
-
31
-
## Generated CMakeLists.txt
17
+
See [configuration](configuration.md) for details on all options.
32
18
33
-
The top-level `CMakeLists.txt` includes:
34
-
35
-
- C++17 standard requirement
36
-
- Rice fetched from GitHub via `FetchContent`
37
-
- Ruby detection via `find_package(Ruby)`
38
-
- Library target (SHARED on MSVC, MODULE elsewhere)
- Subdirectory includes and `*-rb.cpp` source file listing
70
+
71
+
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.
72
+
73
+
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.
68
74
69
75
| Preset | Platform | Compiler |
70
76
|--------|----------|----------|
@@ -74,4 +80,19 @@ The generated `CMakePresets.json` includes presets for all major platforms:
74
80
|`mingw-debug` / `mingw-release`| Windows | MinGW GCC |
75
81
|`clang-windows-debug` / `clang-windows-release`| Windows | clang-cl |
76
82
77
-
All presets use Ninja as the build generator and include appropriate compiler flags for each platform (visibility settings, debug info, optimization levels).
83
+
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).
84
+
85
+
### Subdirectories
86
+
87
+
Each subdirectory containing `*-rb.cpp` files gets a minimal `CMakeLists.txt` that lists its source files and any nested subdirectories:
Copy file name to clipboardExpand all lines: docs/cpp/iterators.md
-12Lines changed: 0 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -89,18 +89,6 @@ namespace std
89
89
}
90
90
```
91
91
92
-
### How It Works
93
-
94
-
`ruby-bindgen` detects incomplete iterators by:
95
-
96
-
1. Checking if the iterator class defines the required typedefs (`value_type`, `reference`, `pointer`, `difference_type`, `iterator_category`)
97
-
2. If any are missing, it infers the types from the iterator's `operator*()` return type
98
-
3. The generated traits specialization is placed at the top of the output file, before the Rice bindings code
99
-
100
-
### Const Iterators
101
-
102
-
`ruby-bindgen` also detects const iterators by examining the `operator*()` return type. If it returns a const reference, the generated `reference` typedef will be `const T&` instead of `T&`.
103
-
104
92
## Examples
105
93
106
94
See [test/headers/cpp/iterators.hpp](../test/headers/cpp/iterators.hpp) for example iterator classes, including both complete and incomplete iterators. The generated bindings are in [test/bindings/cpp/iterators-rb.cpp](../test/bindings/cpp/iterators-rb.cpp).
Copy file name to clipboardExpand all lines: docs/prior_art.md
-4Lines changed: 0 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,16 +10,12 @@ This page lists related projects and adjacent tools that influenced, overlap wit
10
10
- Scope: Multi-language binding generator for C and C++
11
11
- Notes: Generates bindings for many languages including Ruby, Python, Java, and Go. Uses its own interface definition files rather than parsing headers directly. The most established tool in this space — active since 1996.
After generating bindings with `ruby-bindgen`, you'll need to regenerate them periodically as the upstream library evolves, `ruby-bindgen` improves, or you upgrade dependencies. This document covers strategies for managing that process.
4
4
5
-
All examples below reference the [opencv-ruby](https://github.com/ruby-rice/opencv-ruby) project, which wraps OpenCV's ~350 generated files and is a good real-world reference.
5
+
## Simple Case
6
6
7
-
## Regenerate and Done
8
-
9
-
The best case. Run `ruby-bindgen` again and the new output compiles and works:
7
+
If you have no [customizations](cpp/customizing.md) to preserve, then you can simply regenerate the bindings:
10
8
11
9
```bash
12
10
ruby-bindgen bindings.yaml
13
11
```
14
12
15
-
This works when:
16
-
- You're updating `ruby-bindgen` (bug fixes, new features) and don't have manual changes
17
-
- The upstream library's API didn't change in ways that break compilation
18
-
- Your bindings are simple enough that `ruby-bindgen` handles everything
13
+
In most cases, the updated bindings should compile and work.
19
14
20
-
If this is your situation, you're done. The sections below are for when you have [customizations](cpp/customizing.md)that need to be preserved across regenerations.
15
+
The rest of this page discusses strategies for more complex cases where you want to preserve [customizations](cpp/customizing.md)when regenerating bindings.
21
16
22
17
## Preserving Refinements
23
18
24
-
[Refinements](cpp/customizing.md#refinements-separate-manual-code) live in a separate directory that `ruby-bindgen` never touches. They survive regeneration automatically - no action needed.
19
+
[Refinements](cpp/customizing.md#refinements-separate-manual-code) live in a separate directory that `ruby-bindgen` never touches, so the source files survive regeneration automatically. However, the top-level `-rb.cpp` file is regenerated, so you will need to re-add the `#include` directives and `Init_*_Refinement` calls.
25
20
26
21
## Preserving Manual Edits
27
22
28
-
If you have [manual edits](cpp/customizing.md#manual-edits) to generated files, you need a way to reapply them after regeneration. There are two approaches:
23
+
If you have [manual edits](cpp/customizing.md#manual-edits) to generated files, you need a way to reapply them after regeneration. Two possible approaches include:
29
24
30
25
### Option A: Diff File
31
26
32
-
Maintain a unified diff file that can be reapplied after regeneration:
27
+
Maintain a diff file that can be reapplied after regeneration:
33
28
34
29
```bash
35
30
# After making all manual changes to generated files
@@ -51,98 +46,11 @@ git apply manual_updates.diff
51
46
52
47
**Cons**: Diffs are fragile. If `ruby-bindgen`'s output changes line numbers or formatting, the diff won't apply cleanly. You'll need to manually resolve failures and regenerate the diff.
53
48
54
-
### Option B: Instruction File
55
-
56
-
Maintain a structured document (`manual_updates.md`) that describes each change declaratively. An AI assistant or a human can follow the instructions after each regeneration.
57
-
58
-
Here's how opencv-ruby structures its `manual_updates.md`:
59
-
60
-
#### Missing Includes
61
-
62
-
A table mapping files to required manual includes:
Maintain a structured document (`manual_updates.md`) that describes each change declaratively. An AI assistant or a human can follow the instructions after each regeneration. For an example, see opencv-ruby's [manual_updates.md](https://github.com/ruby-rice/opencv-ruby/blob/main/ext/manual_updates.md).
103
52
104
53
**Pros**: Survives large formatting changes, human-readable, an AI assistant can apply the instructions even when line numbers shift.
105
54
106
-
**Cons**: Requires more effort to write initially. Must be kept in sync with actual changes.
107
-
108
-
### Combining Both Approaches
109
-
110
-
opencv-ruby maintains both files:
111
-
112
-
-`manual_updates.md` - the authoritative description of what changes are needed and why
113
-
-`manual_updates.diff` - a snapshot of the actual changes for quick verification
114
-
115
-
After regeneration:
116
-
1. Try applying the diff (`git apply manual_updates.diff`)
117
-
2. If it fails, use `manual_updates.md` as a guide (manually or with AI)
118
-
3. After all changes are applied, regenerate the diff:
0 commit comments