Skip to content

Commit 36e4008

Browse files
committed
Add support for out-of-source builds
Signed-off-by: Ted Lyngmo <ted@lyncon.se>
1 parent cb0bb1a commit 36e4008

File tree

5 files changed

+45
-13
lines changed

5 files changed

+45
-13
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
*.d
33

44
# Compiled Object files
5+
build
56
*.slo
67
*.lo
78
*.o

CMakeLists.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
cmake_minimum_required(VERSION 3.19)
2+
project(CloCXX)
3+
4+
set(GENERATED_HEADER ${CMAKE_BINARY_DIR}/clocxx.hpp)
5+
6+
add_executable(mkclocxx mkclocxx.cpp)
7+
8+
add_custom_command(
9+
OUTPUT ${GENERATED_HEADER}
10+
COMMAND ./mkclocxx ${CMAKE_SOURCE_DIR}
11+
DEPENDS mkclocxx
12+
)
13+
14+
add_library(clocxx INTERFACE ${GENERATED_HEADER})
15+
16+
install(
17+
FILES ${GENERATED_HEADER}
18+
DESTINATION ${CMAKE_INSTALL_PREFIX}/include
19+
)

Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
LIB := $(shell $(CXX) -print-file-name=libstdc++.so)
22
STDLIB := $(shell realpath -e "$(LIB)")
33
STDDIR := $(shell dirname "$(STDLIB)")
4+
MAKEFILEPATH := $(abspath $(lastword $(MAKEFILE_LIST)))
5+
MAKEFILEDIR := $(dir $(MAKEFILEPATH))
46

57
CXXOPT := -std=c++17 -O3 -Wall -Wextra -pedantic-errors
68

@@ -10,7 +12,7 @@ clocxx.hpp: mkclocxx mkclocxx.cpp base.hpp seen_clockid_t
1012
mkclocxx: mkclocxx.cpp clock_test_driver.cpp Makefile
1113
$(CXX) $(CXXOPT) -o $@ $< -Wl,-rpath $(STDDIR)
1214

13-
clock_test_driver: clock_test_driver.cpp
15+
clock_test_driver: $(MAKEFILEDIR)clock_test_driver.cpp
1416
$(CXX) $(CXXOPT) -DCLOCK_TO_TEST=$(CLOCK_TO_TEST) -o $@ $<
1517

1618
clean:

README.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
# CloCXX
22
A header only C++ library with `std::chrono` compatible clocks satisfying the _TrivialClock_ requirement
33

4-
#### Building the `clocxx.hpp` header
5-
```
4+
#### Building the `clocxx.hpp` header in-source
5+
```bash
66
make
77
```
88

9+
#### Building and installing the `clocxx.hpp` header out-of-source
10+
11+
This will build `clocxx.hpp` and install it in `~/.local/include`:
12+
```bash
13+
cmake --install-prefix ~/.local -S . -B build
14+
cmake --build build
15+
cmake --install build
16+
```
17+
918
#### About
1019

1120
`CloCXX` comes with a file with recipes named `seen_clockid_t`. This contains the `clockid_t` macro name, a `bool` describing if the clock is steady or not and finally a description of the clock. When building the `clocxx.hpp` header, each recipe will be checked individually and only working clock definitions will be included in the generated header file. In addition to the `typedef`s required to be a _TrivialClock_, each `lyn::chrono` clock comes with two additional constants:

mkclocxx.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,10 @@ std::optional<pres> command(std::string command) {
7373
return pres{::pclose(pip), std::move(buf)};
7474
}
7575

76-
result test(const clockdef& cd, int width) {
76+
result test(const std::filesystem::path& source_dir, const clockdef& cd, int width) {
7777
std::cout << "Clock under test: " << std::setw(width) << cd.macro;
78-
auto res = command("make clock_test_driver CLOCK_TO_TEST=" + cd.macro);
78+
std::string cmd = "make -f '" + source_dir.string() + "/Makefile' clock_test_driver CLOCK_TO_TEST=" + cd.macro;
79+
auto res = command(cmd);
7980
if(not res || (*res).exitstatus) return UNSUPP;
8081

8182
res = command("./clock_test_driver");
@@ -93,8 +94,8 @@ std::string mkname(std::string macro) {
9394
return macro.substr(6) + "_clock";
9495
}
9596

96-
int createit() {
97-
auto& seen_file = "seen_clockid_t";
97+
int createit(std::filesystem::path source_dir) {
98+
auto seen_file = source_dir / "seen_clockid_t";
9899
std::cout << std::boolalpha;
99100
std::ifstream cids(seen_file);
100101
if(not cids) {
@@ -107,10 +108,10 @@ int createit() {
107108
return 1;
108109
}
109110
int maxwidth = std::max_element(cds.begin(), cds.end(), [](const clockdef& lhs, const clockdef& rhs) {
110-
return lhs.macro.size() < rhs.macro.size();
111-
})->macro.size();
111+
return lhs.macro.size() < rhs.macro.size();
112+
})->macro.size();
112113

113-
std::filesystem::copy("base.hpp", "clocxx.hpp", std::filesystem::copy_options::overwrite_existing);
114+
std::filesystem::copy(source_dir / "base.hpp", "clocxx.hpp", std::filesystem::copy_options::overwrite_existing);
114115

115116
auto& target = "clocxx.hpp";
116117
std::ofstream ofs(target, std::ios::app);
@@ -121,7 +122,7 @@ int createit() {
121122
ofs << std::boolalpha;
122123

123124
for(auto& cd : cds) {
124-
auto rv = test(cd, maxwidth);
125+
auto rv = test(source_dir, cd, maxwidth);
125126
std::cout << ": " << rv << '\n';
126127
if(rv != UNSUPP) {
127128
auto clname = mkname(cd.macro);
@@ -134,8 +135,8 @@ int createit() {
134135
return 0;
135136
}
136137

137-
int main() {
138-
int rv = createit();
138+
int main(int argc, char* argv[]) {
139+
int rv = createit(argc == 2 ? argv[1] : ".");
139140
if(rv == 0) {
140141
return system("clang-format -i clocxx.hpp");
141142
}

0 commit comments

Comments
 (0)