Skip to content

Commit 88e9241

Browse files
committed
Improve onboarding experience
Thanks Matthaus :give:
1 parent eff9672 commit 88e9241

File tree

9 files changed

+117
-73
lines changed

9 files changed

+117
-73
lines changed

.github/FUNDING.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
github: [mrexodia]

README.md

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,39 +7,48 @@
77
```toml
88
[project]
99
name = "cmkr_for_beginners"
10-
description = "A minimal cmkr project."
1110

1211
[target.hello_world]
1312
type = "executable"
1413
sources = ["src/main.cpp"]
1514
```
1615

17-
`cmkr` can bootstrap itself from CMake and consumers of your project do not need to install anything to work with it.
16+
`cmkr` can bootstrap itself from CMake and you only need CMake to use it.
1817

1918
## Getting started
2019

21-
The easiest way to get started is to use the [cmkr_for_beginners](https://github.com/build-cpp/cmkr_for_beginners) template repository. Either open it in [Gitpod](https://gitpod.io/#https://github.com/build-cpp/cmkr_for_beginners), or clone the repository and run:
20+
To get started run the following commands from your project directory:
21+
22+
```sh
23+
curl https://raw.githubusercontent.com/build-cpp/cmkr/main/cmake/cmkr.cmake -o cmkr.cmake
24+
cmake -P cmkr.cmake
25+
```
26+
27+
After the bootstrapping process finishes, modify [`cmake.toml`](https://build-cpp.github.io/cmkr/cmake-toml) and open the project in your favorite IDE or build with CMake:
2228

2329
```sh
2430
cmake -B build
2531
cmake --build build
2632
```
2733

28-
Alternatively you can check out the [cmkr topic](https://github.com/topics/cmkr), the [build-cpp organization](https://github.com/build-cpp) or the [tests](https://github.com/build-cpp/cmkr/tree/main/tests) for more examples and templates. You can also check out the [documentation](https://build-cpp.github.io/cmkr).
34+
Once bootstrapped, `cmkr` does not introduce extra steps to your workflow. After modifying `cmake.toml` you simply build/configure your CMake project and `cmkr` will automatically regenerate `CMakeLists.txt`.
2935

30-
### Migrating an existing project
36+
In CI settings the `cmkr` bootstrapping process is skipped so there is no extra configure-time overhead in your pipelines.
3137

32-
When migrating an existing project it's easiest to download a [cmkr release](https://github.com/build-cpp/cmkr/releases) and put `cmkr` in your PATH. Then go to your project directory and run:
38+
## Template repositories
3339

34-
```
35-
cmkr init
40+
Another way to get started is to use the [cmkr_for_beginners](https://github.com/build-cpp/cmkr_for_beginners) template repository. Either open it in [Gitpod](https://gitpod.io/#https://github.com/build-cpp/cmkr_for_beginners), or clone the repository and run:
41+
42+
```sh
43+
cmake -B build
44+
cmake --build build
3645
```
3746

38-
This will bootstrap `cmake.toml` and `CMakeLists.txt` that you can then build as normal with CMake.
47+
Check out the [cmkr topic](https://github.com/topics/cmkr), the [build-cpp organization](https://github.com/build-cpp) or the [tests](https://github.com/build-cpp/cmkr/tree/main/tests) for more examples and templates.
3948

4049
## Command line
4150

42-
Optionally you can install `cmkr` in your `PATH` and use it as a utility from the command line:
51+
Optionally you can put a [`cmkr` release](https://github.com/build-cpp/cmkr/releases) in your `PATH` and use it as a utility from the command line:
4352

4453
```
4554
Usage: cmkr [arguments]

cmake/cmkr.cmake

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
include_guard()
22

3+
# To bootstrap/generate a cmkr project: cmake -P cmkr.cmake
34
if(CMAKE_SCRIPT_MODE_FILE)
4-
message(FATAL_ERROR "Running cmkr.cmake as a script is unsupported. To build your project try: cmake -B build")
5+
set(CMAKE_BINARY_DIR "${CMAKE_BINARY_DIR}/build")
6+
set(CMAKE_CURRENT_BINARY_DIR "${CMAKE_BINARY_DIR}")
7+
file(MAKE_DIRECTORY "${CMAKE_BINARY_DIR}")
58
endif()
69

710
# Change these defaults to point to your infrastructure if desired
@@ -23,7 +26,7 @@ if(DEFINED ENV{CI} OR CMKR_SKIP_GENERATION OR CMKR_BUILD_SKIP_GENERATION)
2326
endif()
2427

2528
# Disable cmkr if no cmake.toml file is found
26-
if(NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/cmake.toml")
29+
if(NOT CMAKE_SCRIPT_MODE_FILE AND NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/cmake.toml")
2730
message(AUTHOR_WARNING "[cmkr] Not found: ${CMAKE_CURRENT_SOURCE_DIR}/cmake.toml")
2831
macro(cmkr)
2932
endmacro()
@@ -143,6 +146,29 @@ if(NOT CMKR_EXEC_RESULT EQUAL 0)
143146
message(FATAL_ERROR "[cmkr] Failed to get version, try clearing the cache and rebuilding")
144147
endif()
145148

149+
# Use cmkr.cmake as a script
150+
if(CMAKE_SCRIPT_MODE_FILE)
151+
if(NOT EXISTS "${CMAKE_SOURCE_DIR}/cmake.toml")
152+
execute_process(COMMAND "${CMKR_EXECUTABLE}" init
153+
RESULT_VARIABLE CMKR_EXEC_RESULT
154+
)
155+
if(NOT CMKR_EXEC_RESULT EQUAL 0)
156+
message(FATAL_ERROR "[cmkr] Failed to bootstrap cmkr project. Please report an issue: https://github.com/build-cpp/cmkr/issues/new")
157+
else()
158+
message(STATUS "[cmkr] Modify cmake.toml and then configure using: cmake -B build")
159+
endif()
160+
else()
161+
execute_process(COMMAND "${CMKR_EXECUTABLE}" gen
162+
RESULT_VARIABLE CMKR_EXEC_RESULT
163+
)
164+
if(NOT CMKR_EXEC_RESULT EQUAL 0)
165+
message(FATAL_ERROR "[cmkr] Failed to generate project.")
166+
else()
167+
message(STATUS "[cmkr] Configure using: cmake -B build")
168+
endif()
169+
endif()
170+
endif()
171+
146172
# This is the macro that contains black magic
147173
macro(cmkr)
148174
# When this macro is called from the generated file, fake some internal CMake variables

docs/cmake-toml.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
---
22
layout: page
3-
title: cmake.toml
3+
title: Reference
44
permalink: /cmake-toml/
5-
nav_order: 3
5+
nav_order: 1
66
---
77

8-
# cmake.toml
8+
# Reference
99

10-
This page is supposed to be a reference for the options in the `cmake.toml` file. If you think anything is missing or unclear, please [edit this page](https://github.com/build-cpp/cmkr/edit/main/docs/cmake-toml.md) or open an [issue](https://github.com/build-cpp/cmkr/issues).
10+
This page is a reference for the options in the `cmake.toml` file. If you think anything is missing or unclear, please [edit this page](https://github.com/build-cpp/cmkr/edit/main/docs/cmake-toml.md) or open an [issue](https://github.com/build-cpp/cmkr/issues).
1111

12-
See the [examples](/examples) section for more real-world examples.
12+
See the [examples](/examples) section for concrete examples.
1313

1414
## CMake configuration
1515

@@ -132,7 +132,7 @@ tag = "v0.1"
132132
[target.mytarget]
133133
condition = "linux"
134134
alias = "mytarget::mytarget"
135-
type = "static" # executable, library, shared, static, interface, custom
135+
type = "static" # executable, shared (DLL), static, interface, object, library, custom
136136
headers = ["src/mytarget.h"]
137137
sources = ["src/mytarget.cpp"]
138138

docs/command-line.md

Lines changed: 0 additions & 22 deletions
This file was deleted.

docs/getting-started.md

Lines changed: 0 additions & 27 deletions
This file was deleted.

docs/index.md

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,65 @@ nav_order: 0
1313
```toml
1414
[project]
1515
name = "cmkr_for_beginners"
16-
description = "A minimal cmkr project."
1716

1817
[target.hello_world]
1918
type = "executable"
2019
sources = ["src/main.cpp"]
2120
```
2221

23-
`cmkr` can bootstrap itself from CMake and consumers of your project do not need to install anything to work with it.
22+
`cmkr` can bootstrap itself from CMake and you only need CMake to use it.
23+
24+
## Getting started
25+
26+
To get started run the following commands from your project directory:
27+
28+
```sh
29+
curl https://raw.githubusercontent.com/build-cpp/cmkr/main/cmake/cmkr.cmake -o cmkr.cmake
30+
cmake -P cmkr.cmake
31+
```
32+
33+
After the bootstrapping process finishes, modify [`cmake.toml`](https://build-cpp.github.io/cmkr/cmake-toml) and open the project in your favorite IDE or build with CMake:
34+
35+
```sh
36+
cmake -B build
37+
cmake --build build
38+
```
39+
40+
Once bootstrapped, `cmkr` does not introduce extra steps to your workflow. After modifying `cmake.toml` you simply build/configure your CMake project and `cmkr` will automatically regenerate `CMakeLists.txt`.
41+
42+
In CI settings the `cmkr` bootstrapping process is skipped so there is no extra configure-time overhead in your pipelines.
43+
44+
## Template repositories
45+
46+
Another way to get started is to use the [cmkr_for_beginners](https://github.com/build-cpp/cmkr_for_beginners) template repository. Either open it in [Gitpod](https://gitpod.io/#https://github.com/build-cpp/cmkr_for_beginners), or clone the repository and run:
47+
48+
```sh
49+
cmake -B build
50+
cmake --build build
51+
```
52+
53+
Check out the [cmkr topic](https://github.com/topics/cmkr), the [build-cpp organization](https://github.com/build-cpp) or the [tests](https://github.com/build-cpp/cmkr/tree/main/tests) for more examples and templates.
54+
55+
## Command line
56+
57+
Optionally you can put a [`cmkr` release](https://github.com/build-cpp/cmkr/releases) in your `PATH` and use it as a utility from the command line:
58+
59+
```
60+
Usage: cmkr [arguments]
61+
arguments:
62+
init [executable|library|shared|static|interface] Starts a new project in the same directory.
63+
gen Generates CMakeLists.txt file.
64+
build <extra cmake args> Run cmake and build.
65+
install Run cmake --install. Needs admin privileges.
66+
clean Clean the build directory.
67+
help Show help.
68+
version Current cmkr version.
69+
```
70+
71+
## Credits
72+
73+
- https://github.com/gulrak/filesystem
74+
- https://github.com/Tessil/ordered-map
75+
- https://github.com/ToruNiina/toml11
76+
- https://github.com/mpark/variant
77+
- https://www.svgrepo.com/svg/192268/hammer

include/literals.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,10 @@ static const char *toml_migration = &R"lit(
9191
name = "@name"
9292
9393
# TODO: define a target for each of your executables/libraries like this:
94-
#[target.@name]
95-
#type = "executable"
94+
#[target.myexecutable]
95+
#type = "executable" # static, shared
9696
#sources = ["src/@name/*.cpp", "include/@name/*.hpp"]
9797
#include-directories = ["include"]
9898
#compile-features = ["cxx_std_11"]
99-
#link-libraries = ["other-targets"]
99+
#link-libraries = ["target-or-library"]
100100
)lit"[1]; // skip initial newline

src/cmake_generator.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,11 @@ void generate_project(const std::string &type) {
105105
};
106106

107107
if (!fs::is_empty(fs::current_path())) {
108+
// Make a backup of an existing CMakeLists.txt if it exists
109+
std::error_code ec;
110+
fs::rename("CMakeLists.txt", "CMakeLists.txt.bak", ec);
111+
// Create an empty cmake.toml for migration purporses
108112
create_file("cmake.toml", format(toml_migration, variables));
109-
puts("Generated migration cmake.toml in existing project directory...");
110113
return;
111114
}
112115

0 commit comments

Comments
 (0)