aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/README.md
blob: d8b4b10e21a180d103178482ba3ee2f5353e599c (plain) (blame)
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
cmake-common
============

[![Basic usage](https://github.com/egor-tensin/cmake-common/workflows/Basic%20usage/badge.svg)](https://github.com/egor-tensin/cmake-common/actions?query=workflow%3A%22Basic+usage%22)
[![Boost (toolsets)](https://github.com/egor-tensin/cmake-common/workflows/Boost%20(toolsets)/badge.svg)](https://github.com/egor-tensin/cmake-common/actions?query=workflow%3A%22Boost+%28toolsets%29%22)
[![Examples (toolsets)](https://github.com/egor-tensin/cmake-common/workflows/Examples%20(toolsets)/badge.svg)](https://github.com/egor-tensin/cmake-common/actions?query=workflow%3A%22Examples+%28toolsets%29%22)

Various utilities to help develop C++/CMake projects.

Toolchains
----------

Supported platform/build system/compiler combinations include, but are not
limited to:

| Platform | Build system | Compiler
| -------- | ------------ | --------
| Linux    | make         | Clang
|          |              | GCC
|          |              | MinGW-w64
| Windows  | make \[1\]   | Clang (clang/clang++)
|          |              | Clang (clang-cl \[2\])
|          |              | MinGW-w64
|          | msbuild      | MSVC
| Cygwin   | make         | Clang
|          |              | GCC
|          |              | MinGW-w64

1. Both GNU `make` and MinGW `mingw32-make`.
2. Boost 1.69.0 or higher only.

All of those are verified continuously by the "Boost (toolsets)" and "Examples
(toolsets)" workflows.

Usage
-----

### common.cmake

Use in a project by putting

    include(path/to/common.cmake)

in CMakeLists.txt.

This file aids in quick-and-dirty development by

* linking everything (including the runtime) statically by default,
* setting some useful compilation options (enables warnings, defines useful
Windows-specific macros, strips debug symbols in release builds, etc.).

Everything is optional (use the `CC_*` CMake options to opt out).

### Boost

Download & build the Boost libraries in a cross-platform way.

    $ python3 -m project.boost.download 1.72.0
    ...

    $ python3 -m project.boost.build -- boost_1_72_0/ --with-filesystem --with-program_options
    ...

Pass the `--help` flag to view detailed usage information.

    $ python3 -m project.boost.download --help
    $ python3 -m project.boost.build --help

### CMake project

Build (and optionally, install) a CMake project.

    $ python3 -m project.cmake.build --configuration Release --install path/to/somewhere -- examples/simple
    ...

    $ ./path/to/somewhere/bin/foo
    foo

Pass the `--help` flag to view detailed usage information.

    $ python3 -m project.cmake.build --help

### CI

Utility modules `project.ci.boost` and `project.ci.cmake` allow building Boost
and CMake projects on multiple CI systems.
They work by calling the generic scripts from above, auto-filling some
parameters from environment variables.

|                   | Travis                               | AppVeyor                                   | GitHub Actions
| ----------------- | ------------------------------------ | ------------------------------------------ | ------------------------------------
| `--toolset`       | `$toolset`                           | `%toolset%`                                | `$toolset`
| `--platform`      | `$platform`                          | `%PLATFORM%`                               | `$platform`
| `--configuration` | `$configuration`                     | `%CONFIGURATION%`                          | `$configuration`
| Boost version     | `$boost_version`                     | `%boost_version%`                          | `$boost_version`
| Boost path        | `$TRAVIS_BUILD_DIR/../build/boost`   | `%APPVEYOR_BUILD_FOLDER%\..\build\boost`   | `$GITHUB_WORKSPACE/../build/boost`
| Build path        | `$TRAVIS_BUILD_DIR/../build/cmake`   | `%APPVEYOR_BUILD_FOLDER%\..\build\cmake`   | `$GITHUB_WORKSPACE/../build/cmake`
| Install path      | `$TRAVIS_BUILD_DIR/../build/install` | `%APPVEYOR_BUILD_FOLDER%\..\build\install` | `$GITHUB_WORKSPACE/../build/install`

For example, the following Travis workflow:

```
language: cpp
os: linux
dist: focal

env:
  global:
    boost_version: 1.65.0
  jobs:
    - configuration=Debug   platform=x64
    - configuration=Release platform=x64

before_script: python3 -m project.ci.boost -- --with-filesystem
script: python3 -m project.ci.cmake --install
```

is roughly equivalent to running

```
python3 -m project.boost.download --cache "$TRAVIS_BUILD_DIR/../build" -- 1.65.0
mv -- \
    "$TRAVIS_BUILD_DIR/../build/boost_1_65_0" \
    "$TRAVIS_BUILD_DIR/../build/boost"

python3 -m project.boost.build         \
    --platform x64                     \
    --configuration Debug Release      \
    --                                 \
    "$TRAVIS_BUILD_DIR/../build/boost" \
    --with-filesystem

for configuration in Debug Release; do
    python3 -m project.cmake.build                     \
        --platform x64                                 \
        --configuration "$configuration"               \
        --boost "$TRAVIS_BUILD_DIR/../build/boost"     \
        --build "$TRAVIS_BUILD_DIR/../build/cmake"     \
        --install "$TRAVIS_BUILD_DIR/../build/install" \
        --                                             \
        "$TRAVIS_BUILD_DIR"
done
```

### clang-format.py

`clang-format` all C/C++ files in a project.

    $ cd project/
    $ python3 path/to/tools/clang-format.py      # Prints a diff
    $ python3 path/to/tools/clang-format.py -i   # Edits files in-place

Examples
--------

I use this in all of my C++/CMake projects, e.g. [aes-tools] and [math-server].

[aes-tools]: https://github.com/egor-tensin/aes-tools
[math-server]: https://github.com/egor-tensin/math-server

License
-------

Distributed under the MIT License.
See [LICENSE.txt] for details.

[LICENSE.txt]: LICENSE.txt