summaryrefslogtreecommitdiff
path: root/docs/handbook/tomlplusplus/building.md
blob: c70c76c297b139e4ed11d0d4303afa9801c69c62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
# toml++ — Building

## Overview

toml++ supports multiple build modes and build systems. It can be consumed as a header-only library, a single-header drop-in, or a compiled static/shared library. The primary build system is Meson, with CMake as a first-class alternative and Visual Studio project files also provided.

---

## Build Modes

### 1. Header-Only Mode (Default)

The simplest way to use toml++. No compilation of the library itself is needed.

**Setup:**
1. Add `tomlplusplus/include` to your include paths
2. `#include <toml++/toml.hpp>` in your source files
3. Compile your project with C++17 or later

This is the default mode. The macro `TOML_HEADER_ONLY` defaults to `1`.

**Advantages:**
- Zero build configuration
- No separate library to link
- Works with any build system

**Disadvantages:**
- Every translation unit that includes toml++ compiles the full implementation
- Can increase compile times in large projects

**Example CMake integration:**
```cmake
# Add tomlplusplus as a subdirectory or fetch it
add_subdirectory(external/tomlplusplus)
target_link_libraries(my_target PRIVATE tomlplusplus::tomlplusplus)
```

### 2. Single-Header Mode

toml++ ships with a pre-amalgamated single-header file at the repository root: `toml.hpp`.

**Setup:**
1. Copy `toml.hpp` into your project
2. `#include "toml.hpp"` in your source files
3. Done

This file contains the entire library — all headers, all `.inl` implementation files — concatenated into one file. The API is identical to the multi-header version.

### 3. Compiled Library Mode

For projects where compile time matters, toml++ can be built as a compiled library.

**Setup:**

In exactly **one** translation unit, compile `src/toml.cpp`:

```cpp
// src/toml.cpp — this is the entire compiled-library source
#ifndef TOML_IMPLEMENTATION
#define TOML_IMPLEMENTATION
#endif
#ifndef TOML_HEADER_ONLY
#define TOML_HEADER_ONLY 0
#endif

#include <toml++/toml.hpp>
```

In all other translation units, define `TOML_HEADER_ONLY=0` before including the header:

```cpp
#define TOML_HEADER_ONLY 0
#include <toml++/toml.hpp>
```

Or set it project-wide via compiler flags:
```bash
g++ -DTOML_HEADER_ONLY=0 -I/path/to/tomlplusplus/include ...
```

**Advantages:**
- The parser and formatter implementations are compiled once
- Faster incremental builds
- Smaller binary size (fewer inlined copies)

**Disadvantages:**
- Requires linking the compiled translation unit
- Need to ensure `TOML_HEADER_ONLY=0` is consistent across all TUs

### 4. C++20 Modules Mode

When using CMake 3.28+ and a C++20 compiler:

```cmake
cmake -DTOMLPLUSPLUS_BUILD_MODULES=ON ..
```

Then in your source:
```cpp
import tomlplusplus;
```

Module support is experimental and requires:
- CMake ≥ 3.28
- A compiler with C++20 module support (recent GCC, Clang, or MSVC)

The module source files are in `src/modules/`.

---

## Meson Build System

Meson is the primary build system for toml++. The project file is `meson.build` at the repository root.

### Project Definition

```meson
project(
    'tomlplusplus',
    'cpp',
    license: 'MIT',
    version: '3.4.0',
    meson_version: '>=0.61.0',
    default_options: [
        'buildtype=release',
        'default_library=shared',
        'b_lto=false',
        'b_ndebug=if-release',
        'cpp_std=c++17'
    ]
)
```

### Meson Options

Options are defined in `meson_options.txt`:

| Option | Type | Default | Description |
|--------|------|---------|-------------|
| `devel` | bool | `false` | Development build (implies `build_tests`, `build_examples`, `pedantic`) |
| `build_lib` | bool | `false` | Compile as a library (implied by `devel`) |
| `build_examples` | bool | `false` | Build example programs (implied by `devel`) |
| `build_tests` | bool | `false` | Build test suite (implied by `devel`) |
| `build_tt` | bool | `false` | Build toml-test encoder/decoder (implied by `devel`, disabled by `unreleased_features`) |
| `pedantic` | bool | `false` | Enable maximum compiler warnings (implied by `devel`) |
| `permissive` | bool | `false` | MSVC `/permissive` mode (default is `/permissive-`) |
| `time_trace` | bool | `false` | Enable `-ftime-trace` (Clang only) |
| `unreleased_features` | bool | `false` | Enable `TOML_UNRELEASED_FEATURES=1` |
| `generate_cmake_config` | bool | `true` | Generate a CMake package config file |
| `use_vendored_libs` | bool | `true` | Use vendored Catch2 for tests |

### Building with Meson

```bash
# Configure
meson setup build
# Or with options:
meson setup build -Dbuild_tests=true -Dbuild_examples=true

# Compile
meson compile -C build

# Run tests
meson test -C build

# Development build (builds everything, enables warnings)
meson setup build -Ddevel=true
```

### Using as a Meson Subproject

Create `subprojects/tomlplusplus.wrap`:
```ini
[wrap-git]
url = https://github.com/marzer/tomlplusplus.git
revision = v3.4.0

[provide]
tomlplusplus = tomlplusplus_dep
```

Then in your `meson.build`:
```meson
tomlplusplus_dep = dependency('tomlplusplus', version: '>=3.4.0')
executable('my_app', 'main.cpp', dependencies: [tomlplusplus_dep])
```

### Meson Library Target

When `build_lib` is true (or implied), the library is compiled:

```meson
# In the meson.build, the library creates a tomlplusplus_dep dependency
# that other targets consume
```

The compiled library defines:
- `TOML_HEADER_ONLY=0`
- `TOML_IMPLEMENTATION`

### Compiler Flag Management

The Meson build applies comprehensive compiler flags based on the detected compiler:

**Common flags:**
```
-ferror-limit=5          # Clang: max errors
-fmax-errors=5           # GCC: max errors
-fchar8_t                # Enable char8_t
```

**MSVC-specific:**
```
/bigobj                  # Large object file support
/utf-8                   # UTF-8 source encoding
/Zc:__cplusplus          # Correct __cplusplus value
/Zc:inline               # Remove unreferenced COMDAT
/Zc:externConstexpr      # External constexpr linkage
/Zc:preprocessor         # Standards-conforming preprocessor
```

**Pedantic mode** enables extensive warning flags for both GCC and Clang (`-Weverything`, `-Wcast-align`, `-Wshadow`, etc.) with targeted suppressions for unavoidable warnings (`-Wno-c++98-compat`, `-Wno-padded`, etc.).

---

## CMake Build System

### CMake Project

The `CMakeLists.txt` defines an interface (header-only) library:

```cmake
cmake_minimum_required(VERSION 3.14)

project(
    tomlplusplus
    VERSION 3.4.0
    DESCRIPTION "Header-only TOML config file parser and serializer for C++17"
    HOMEPAGE_URL "https://marzer.github.io/tomlplusplus/"
    LANGUAGES CXX
)

add_library(tomlplusplus_tomlplusplus INTERFACE)
add_library(tomlplusplus::tomlplusplus ALIAS tomlplusplus_tomlplusplus)

target_include_directories(
    tomlplusplus_tomlplusplus
    INTERFACE
    "$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>"
)

target_compile_features(tomlplusplus_tomlplusplus INTERFACE cxx_std_17)
```

### Using with CMake — Subdirectory

```cmake
add_subdirectory(path/to/tomlplusplus)
target_link_libraries(my_target PRIVATE tomlplusplus::tomlplusplus)
```

### Using with CMake — FetchContent

```cmake
include(FetchContent)
FetchContent_Declare(
    tomlplusplus
    GIT_REPOSITORY https://github.com/marzer/tomlplusplus.git
    GIT_TAG v3.4.0
)
FetchContent_MakeAvailable(tomlplusplus)

target_link_libraries(my_target PRIVATE tomlplusplus::tomlplusplus)
```

### Using with CMake — find_package

If toml++ is installed system-wide or the CMake config was generated:

```cmake
find_package(tomlplusplus REQUIRED)
target_link_libraries(my_target PRIVATE tomlplusplus::tomlplusplus)
```

### CMake Options

```cmake
option(BUILD_EXAMPLES "Build examples tree." OFF)
option(BUILD_FUZZER "Build fuzzer." OFF)
option(TOMLPLUSPLUS_BUILD_MODULES "Build C++ modules support" OFF)
```

### CMake Install

When `tomlplusplus_INSTALL` is true, install rules are included from `cmake/install-rules.cmake`.

---

## Visual Studio

The repository includes Visual Studio project files:
- `toml++.sln` — Solution file
- `toml++.vcxproj` — Main project file
- `toml++.vcxproj.filters` — Filter definition
- `toml++.props` — Property sheet
- `toml++.natvis` — Natvis debugger visualizer for TOML node types

Individual examples also have `.vcxproj` files:
- `examples/simple_parser.vcxproj`
- `examples/toml_to_json_transcoder.vcxproj`
- `examples/toml_generator.vcxproj`
- `examples/error_printer.vcxproj`
- `examples/parse_benchmark.vcxproj`

---

## Package Managers

### Vcpkg

```bash
vcpkg install tomlplusplus
```

### Conan

In your `conanfile.txt`:
```
[requires]
tomlplusplus/3.4.0
```

### DDS

In your `package.json5`:
```json5
depends: [
    'tomlpp^3.4.0',
]
```

### tipi.build

In `.tipi/deps`:
```json
{
    "marzer/tomlplusplus": {}
}
```

---

## Compiler Requirements

### Minimum Versions

| Compiler | Minimum Version |
|----------|----------------|
| GCC | 8+ |
| Clang | 8+ |
| Apple Clang | Xcode 10+ |
| MSVC | VS2019 (19.20+) |
| Intel C++ | ICC 19+, ICL 19+ |

### Required Standard

C++17 is required. The preprocessor enforces this:

```cpp
#if TOML_CPP < 17
#error toml++ requires C++17 or higher.
#endif
```

### Compiler Feature Detection

The library detects compiler capabilities:

```cpp
// TOML_CPP — detected C++ standard version (11, 14, 17, 20, 23, 26, 29)
// TOML_GCC — GCC major version (0 if not GCC)
// TOML_CLANG — Clang major version (0 if not Clang)
// TOML_MSVC — MSVC version (0 if not MSVC)
// TOML_ICC — Intel compiler detection
// TOML_NVCC — NVIDIA CUDA compiler detection
// TOML_HAS_CHAR8 — char8_t available
// TOML_HAS_EXCEPTIONS — exceptions enabled
```

---

## Configuration Macros Reference

Define these **before** including `<toml++/toml.hpp>`:

### Core Configuration

```cpp
// Library mode
#define TOML_HEADER_ONLY 1        // 1 = header-only (default), 0 = compiled

// Feature toggles
#define TOML_ENABLE_PARSER 1      // 1 = include parser (default), 0 = no parser
#define TOML_ENABLE_FORMATTERS 1  // 1 = include formatters (default), 0 = no formatters

// Exception handling
// Auto-detected from compiler settings. Override with:
#define TOML_EXCEPTIONS 1         // or 0

// Unreleased TOML features
#define TOML_UNRELEASED_FEATURES 0  // 1 = enable upcoming TOML spec features
```

### Platform Configuration

```cpp
// Windows wide-string support (auto-detected on Windows)
#define TOML_ENABLE_WINDOWS_COMPAT 1

// Custom optional type
#define TOML_OPTIONAL_TYPE std::optional  // or your custom type

// Disable environment checks
#define TOML_DISABLE_ENVIRONMENT_CHECKS
```

### Example: Minimal Parse-Only Build

```cpp
#define TOML_ENABLE_FORMATTERS 0  // Don't need serialization
#include <toml++/toml.hpp>
```

### Example: Serialize-Only Build

```cpp
#define TOML_ENABLE_PARSER 0  // Don't need parsing
#include <toml++/toml.hpp>
```

---

## Build Troubleshooting

### Common Issues

**"toml++ requires C++17 or higher"**
Ensure your compiler is invoked with `-std=c++17` (or later) or the equivalent flag.

**Large object files on MSVC**
Use `/bigobj` flag (the Meson build adds this automatically).

**Long compile times**
Switch to compiled library mode (`TOML_HEADER_ONLY=0` + compile `src/toml.cpp`).

**ODR violations when mixing settings**
Ensure all translation units use the same values for `TOML_EXCEPTIONS`, `TOML_ENABLE_PARSER`, etc. The ABI namespace system catches some mismatches at link time, but not all.

**`char8_t` errors on older compilers**
Add `-fchar8_t` flag if your compiler supports it, or compile with C++20 mode.

**RTTI disabled**
toml++ does not require RTTI. It uses virtual dispatch, not `dynamic_cast` or `typeid`.

**Exceptions disabled**
Set `TOML_EXCEPTIONS=0` or use `-fno-exceptions`. The API adapts: `parse()` returns `parse_result` instead of throwing.

---

## Related Documentation

- [overview.md](overview.md) — Library feature list
- [basic-usage.md](basic-usage.md) — Getting started with parsing and serialization
- [testing.md](testing.md) — Running the test suite