Skip to content

Commit 064868b

Browse files
committed
Upgrade to boost 1.89.0
Boost 1.89.0 is required to have a compatible standard library. Older versionis would use std::unary_function which is not supported anymore in C++17. Using the proper internal type name boost::uuids::detail::sha1::digest_type result in 2 different behaviors depending on the Boost version. The type can either be an array of "unsigned int[5]" (older Boost) or "char[20]" (newer Boost) which the serialization code needs to handle. The serialization code is also moved away from using sprintf as modern compilers will emit lots of different warnings or errors.
1 parent 0410195 commit 064868b

3 files changed

Lines changed: 51 additions & 32 deletions

File tree

.tipi/env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CMAKE_TIPI_PROVIDER_ENABLE=ON

CMakeLists.txt

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,44 +14,36 @@ if ("${COMPILER_IN_USE}" STREQUAL "GNU" OR "${COMPILER_IN_USE}" MATCHES "CLANG")
1414
)
1515
endif()
1616

17-
find_package(Threads)
18-
1917
include (FetchContent)
2018
FetchContent_Declare(
2119
Boost
22-
GIT_REPOSITORY https://github.com/boostorg/boost.git
23-
# boost 1.80
24-
GIT_TAG 32da69a36f84c5255af8a994951918c258bac601
20+
GIT_REPOSITORY https://github.com/nxxm/boost.git
21+
# boost-1.89.0 with updates
22+
GIT_TAG 23090bb21676cb22f1d4ceed21d59fda8b420cc9
23+
EXCLUDE_FROM_ALL
2524
)
2625
FetchContent_MakeAvailable(Boost)
27-
find_package(boost_filesystem CONFIG REQUIRED)
28-
find_package(boost_system CONFIG REQUIRED)
29-
find_package(boost_uuid CONFIG REQUIRED)
26+
find_package(boost_filesystem CONFIG)
27+
find_package(boost_system CONFIG)
28+
find_package(boost_uuid CONFIG)
3029

3130
# Define library
32-
add_library(file INTERFACE )
31+
add_library(file INTERFACE)
3332
add_library(cpp-pre_file::file ALIAS file)
34-
35-
set(include_install_dir "include")
36-
37-
target_include_directories(file BEFORE INTERFACE
38-
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}>
39-
$<INSTALL_INTERFACE:${include_install_dir}/>)
40-
41-
target_link_libraries(file INTERFACE
42-
Boost::system Boost::filesystem Boost::uuid
43-
${CMAKE_THREAD_LIBS_INIT}
33+
target_sources(file
34+
PUBLIC
35+
FILE_SET HEADERS
36+
FILES
37+
pre/file/hash.hpp
38+
pre/file/string.hpp
39+
pre/file/home.hpp
4440
)
41+
target_compile_features(file INTERFACE cxx_std_17)
4542

46-
47-
# Targets:
48-
install(
49-
TARGETS
50-
EXPORT "${targets_export_name}"
51-
LIBRARY DESTINATION "lib"
52-
ARCHIVE DESTINATION "lib"
53-
RUNTIME DESTINATION "bin"
54-
INCLUDES DESTINATION "${include_install_dir}"
43+
target_link_libraries(file INTERFACE
44+
Boost::filesystem
45+
Boost::system
46+
Boost::uuid
5547
)
5648

5749
add_subdirectory(tests)
@@ -100,7 +92,7 @@ install(
10092
LIBRARY DESTINATION "lib"
10193
ARCHIVE DESTINATION "lib"
10294
RUNTIME DESTINATION "bin"
103-
INCLUDES DESTINATION "${include_install_dir}"
95+
FILE_SET HEADERS DESTINATION "include"
10496
)
10597

10698
# Headers:

pre/file/hash.hpp

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <stdexcept>
44
#include <system_error>
5+
#include <iterator>
56
#include <fstream>
67
#include <string>
78
#include <vector>
@@ -10,6 +11,13 @@
1011

1112
namespace pre::file {
1213

14+
namespace {
15+
char int_to_hex[16] = {
16+
'0', '1', '2', '3', '4', '5', '6', '7', '8',
17+
'9', 'a', 'b', 'c', 'd', 'e', 'f'
18+
};
19+
}
20+
1321
using namespace std::string_literals;
1422

1523
//! Calculate the SHA1 sum for a file on disk
@@ -41,13 +49,31 @@ namespace pre::file {
4149
}
4250

4351
// compute the hash
44-
uint32_t hash[5] = {0};
52+
boost::uuids::detail::sha1::digest_type hash = {0};
4553
char hash_buf[41] = {0};
4654

4755
sha1.get_digest(hash);
4856

49-
for (int i = 0; i < 5; i++) {
50-
std::sprintf(hash_buf + (i << 3), "%08x", hash[i]);
57+
if constexpr (std::size(hash) == 20) {
58+
// Newer Boost has digest_type of type "char[20]"
59+
for (size_t i = 0; i < std::size(hash); i++) {
60+
hash_buf[i * 2 + 0] = int_to_hex[(hash[i] >> 4) & 0xF];
61+
hash_buf[i * 2 + 1] = int_to_hex[(hash[i] ) & 0xF];
62+
}
63+
} else if constexpr (std::size(hash) == 5) {
64+
// Older Boost has digest_type of type "unsigned int[20]"
65+
for (size_t i = 0; i < std::size(hash); i++) {
66+
hash_buf[i * 2 + 0] = int_to_hex[(hash[i] >> 28) & 0xF];
67+
hash_buf[i * 2 + 1] = int_to_hex[(hash[i] >> 24) & 0xF];
68+
hash_buf[i * 2 + 2] = int_to_hex[(hash[i] >> 20) & 0xF];
69+
hash_buf[i * 2 + 3] = int_to_hex[(hash[i] >> 16) & 0xF];
70+
hash_buf[i * 2 + 4] = int_to_hex[(hash[i] >> 12) & 0xF];
71+
hash_buf[i * 2 + 5] = int_to_hex[(hash[i] >> 8) & 0xF];
72+
hash_buf[i * 2 + 6] = int_to_hex[(hash[i] >> 4) & 0xF];
73+
hash_buf[i * 2 + 7] = int_to_hex[(hash[i] ) & 0xF];
74+
}
75+
} else {
76+
throw std::runtime_error("Unsupported boost::uuids::detail::sha1::digest_type size");
5177
}
5278

5379
return std::string(hash_buf);

0 commit comments

Comments
 (0)