Skip to content

Commit ebe59e8

Browse files
authored
Merge pull request #13 from itzmeanjan/modernize
Modernize Library
2 parents f521f17 + be4d2eb commit ebe59e8

28 files changed

Lines changed: 518 additions & 385 deletions

.clang-tidy

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---
2+
Checks: '
3+
bugprone-*,
4+
cert-*,
5+
clang-analyzer-*,
6+
concurrency-*,
7+
cppcoreguidelines-*,
8+
hicpp-*,
9+
misc-*,
10+
modernize-*,
11+
performance-*,
12+
portability-*,
13+
readability-*,
14+
-bugprone-easily-swappable-parameters,
15+
-clang-analyzer-fuchsia.*,
16+
-clang-analyzer-osx.*,
17+
-clang-analyzer-optin.mpi.*,
18+
-clang-analyzer-optin.osx.*,
19+
-clang-analyzer-optin.performance.GCDAntipattern,
20+
-clang-analyzer-webkit.*,
21+
-cppcoreguidelines-pro-bounds-constant-array-index,
22+
-cppcoreguidelines-avoid-magic-numbers,
23+
-hicpp-use-auto,
24+
-hicpp-signed-bitwise,
25+
-misc-include-cleaner,
26+
-modernize-use-trailing-return-type,
27+
-modernize-use-auto,
28+
-modernize-use-ranges,
29+
-readability-magic-numbers
30+
'
31+
CheckOptions:
32+
readability-identifier-length.MinimumLoopCounterLength: 1
33+
WarningsAsErrors: '*'
34+
HeaderFilterRegex: 'include/randomshake/.*'
35+
...

.github/workflows/test_ci.yml

Lines changed: 67 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# Collects inspiration from https://github.com/itzmeanjan/sha3/blob/fb21648e136d7a64ce5c065fa829d4e3254414f4/.github/workflows/test_ci.yml
21
name: Test RandomShake CSPRNG
32

43
on:
@@ -8,39 +7,75 @@ on:
87
branches: [ "main" ]
98

109
jobs:
11-
build:
12-
runs-on: ${{matrix.os}}
10+
lint:
11+
runs-on: ubuntu-latest
12+
env:
13+
LLVM_VERSION: 20
14+
steps:
15+
- uses: actions/checkout@v6
16+
17+
- name: Install LLVM ${{ env.LLVM_VERSION }}
18+
run: |
19+
wget -qO- https://apt.llvm.org/llvm-snapshot.gpg.key | sudo tee /etc/apt/trusted.gpg.d/apt.llvm.org.asc
20+
echo "deb http://apt.llvm.org/$(lsb_release -cs)/ llvm-toolchain-$(lsb_release -cs)-${{ env.LLVM_VERSION }} main" | sudo tee /etc/apt/sources.list.d/llvm.list
21+
sudo apt-get update
22+
sudo apt-get install -y clang-${{ env.LLVM_VERSION }} clang-tidy-${{ env.LLVM_VERSION }}
23+
24+
- name: Configure
25+
run: >
26+
cmake -B build
27+
-DCMAKE_CXX_COMPILER=clang++-${{ env.LLVM_VERSION }}
28+
-DCMAKE_BUILD_TYPE=Release
29+
-DRANDOMSHAKE_BUILD_TESTS=ON
30+
-DRANDOMSHAKE_BUILD_EXAMPLES=ON
31+
-DRANDOMSHAKE_FETCH_DEPS=ON
32+
-DCLANG_TIDY_EXE=clang-tidy-${{ env.LLVM_VERSION }}
33+
34+
- name: clang-tidy
35+
run: cmake --build build --target tidy
36+
37+
test:
38+
runs-on: ${{ matrix.os }}
1339
strategy:
40+
fail-fast: false
41+
max-parallel: 2
1442
matrix:
15-
os: [
16-
ubuntu-latest, # x86_64
17-
macos-latest, # aarch64
18-
]
43+
os: [ ubuntu-latest ]
1944
compiler: [g++, clang++]
20-
build_type: [debug, release]
21-
test_type: [standard, asan, ubsan]
45+
sanitizer: [none, asan, ubsan]
46+
build_type: [Debug, Release]
47+
include:
48+
- os: macos-latest
49+
compiler: clang++
50+
sanitizer: none
51+
build_type: Release
52+
exclude:
53+
- sanitizer: none
54+
build_type: Debug
2255

2356
steps:
24-
- uses: actions/checkout@v6
25-
26-
- name: Setup Google Test
27-
uses: Bacondish2023/setup-googletest@v1
28-
with:
29-
tag: v1.17.0
30-
31-
- name: Execute Tests on ${{matrix.os}}, compiled with ${{matrix.compiler}}
32-
if: ${{matrix.test_type == 'standard'}}
33-
run: |
34-
CXX=${{matrix.compiler}} make test -j
35-
make clean
36-
37-
- name: Execute Tests with ${{matrix.test_type}}, in ${{matrix.build_type}} mode, on ${{matrix.os}}, compiled with ${{matrix.compiler}}
38-
if: ${{matrix.test_type != 'standard'}}
39-
run: |
40-
CXX=${{matrix.compiler}} make ${{matrix.build_type}}_${{matrix.test_type}}_test -j
41-
make clean
42-
43-
- name: Build and run examples
44-
run: |
45-
make example -j
46-
make clean
57+
- uses: actions/checkout@v6
58+
59+
- name: Configure
60+
run: >
61+
cmake -B build
62+
-DCMAKE_CXX_COMPILER=${{ matrix.compiler }}
63+
-DCMAKE_BUILD_TYPE=${{ matrix.build_type }}
64+
-DRANDOMSHAKE_BUILD_TESTS=ON
65+
-DRANDOMSHAKE_BUILD_EXAMPLES=ON
66+
-DRANDOMSHAKE_FETCH_DEPS=ON
67+
-DRANDOMSHAKE_ASAN=${{ matrix.sanitizer == 'asan' && 'ON' || 'OFF' }}
68+
-DRANDOMSHAKE_UBSAN=${{ matrix.sanitizer == 'ubsan' && 'ON' || 'OFF' }}
69+
70+
- name: Build
71+
run: cmake --build build -j
72+
73+
- name: Test
74+
run: ctest --test-dir build --output-on-failure -j
75+
76+
- name: Run Examples
77+
if: ${{ matrix.sanitizer == 'none' && matrix.build_type == 'Release' }}
78+
run: |
79+
for exe in build/csprng_*_example; do
80+
./$exe
81+
done

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@
3232
*.app
3333

3434
.cache
35-
build
35+
build*/
3636
compile_commands.json

.gitmodules

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

CMakeLists.txt

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
cmake_minimum_required(VERSION 3.28)
2+
3+
project(randomshake VERSION 0.1.0 LANGUAGES CXX)
4+
5+
# --- Options ---
6+
option(RANDOMSHAKE_BUILD_TESTS "Build tests" OFF)
7+
option(RANDOMSHAKE_BUILD_EXAMPLES "Build examples" OFF)
8+
option(RANDOMSHAKE_BUILD_BENCHMARKS "Build benchmarks" OFF)
9+
option(RANDOMSHAKE_FETCH_DEPS "Fetch missing dependencies (GTest, Benchmark)" OFF)
10+
11+
# --- Top-level-only settings (skipped when consumed via FetchContent/add_subdirectory) ---
12+
if(PROJECT_IS_TOP_LEVEL)
13+
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
14+
15+
option(RANDOMSHAKE_ENABLE_LTO "Enable Interprocedural Optimization (LTO)" ON)
16+
option(RANDOMSHAKE_NATIVE_OPT "Enable -march=native (not suitable for cross-compilation)" OFF)
17+
option(RANDOMSHAKE_ASAN "Enable AddressSanitizer" OFF)
18+
option(RANDOMSHAKE_UBSAN "Enable UndefinedBehaviorSanitizer" OFF)
19+
20+
# --- Tools ---
21+
find_program(CLANG_TIDY_EXE clang-tidy)
22+
find_program(CLANG_FORMAT_EXE clang-format)
23+
24+
if(CLANG_TIDY_EXE)
25+
file(GLOB_RECURSE TIDY_SOURCES "examples/*.cpp")
26+
27+
add_custom_target(tidy
28+
COMMAND ${CLANG_TIDY_EXE} -p ${CMAKE_BINARY_DIR} --config-file=${CMAKE_CURRENT_SOURCE_DIR}/.clang-tidy --header-filter=${CMAKE_CURRENT_SOURCE_DIR}/include/randomshake/.* ${TIDY_SOURCES}
29+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
30+
COMMENT "Running clang-tidy"
31+
)
32+
endif()
33+
34+
if(CLANG_FORMAT_EXE)
35+
file(GLOB_RECURSE FORMAT_SOURCES
36+
"include/**/*.hpp" "examples/*.cpp"
37+
"benches/*.hpp" "benches/*.cpp"
38+
"tests/*.hpp" "tests/*.cpp")
39+
40+
add_custom_target(format
41+
COMMAND ${CLANG_FORMAT_EXE} -i -style=file ${FORMAT_SOURCES}
42+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
43+
COMMENT "Running clang-format"
44+
)
45+
endif()
46+
47+
# --- Standard settings ---
48+
set(CMAKE_CXX_STANDARD 20)
49+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
50+
set(CMAKE_CXX_EXTENSIONS OFF)
51+
52+
# --- Compiler Warnings ---
53+
if(MSVC)
54+
set(RANDOMSHAKE_WARNING_FLAGS /W4)
55+
else()
56+
set(RANDOMSHAKE_WARNING_FLAGS -Wall -Wextra -Wpedantic -Wshadow -Wconversion -Wformat=2 -Wcast-qual -Wold-style-cast -Wundef -Werror)
57+
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
58+
list(APPEND RANDOMSHAKE_WARNING_FLAGS -Wno-pass-failed)
59+
endif()
60+
endif()
61+
62+
# --- Sanitizers ---
63+
if(RANDOMSHAKE_ASAN)
64+
add_compile_options(-fsanitize=address -fno-omit-frame-pointer -fno-optimize-sibling-calls)
65+
add_link_options(-fsanitize=address)
66+
endif()
67+
68+
if(RANDOMSHAKE_UBSAN)
69+
add_compile_options(-fsanitize=undefined -fno-omit-frame-pointer -fno-optimize-sibling-calls -fno-sanitize-recover=undefined)
70+
add_link_options(-fsanitize=undefined)
71+
endif()
72+
73+
# --- Native Optimization ---
74+
if(RANDOMSHAKE_NATIVE_OPT)
75+
add_compile_options(-march=native)
76+
message(STATUS "Enabled -march=native (machine-specific optimization)")
77+
endif()
78+
79+
# --- LTO ---
80+
if(RANDOMSHAKE_ENABLE_LTO)
81+
include(CheckIPOSupported)
82+
check_ipo_supported(RESULT result OUTPUT output)
83+
if(result)
84+
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
85+
else()
86+
message(WARNING "LTO is not supported: ${output}")
87+
endif()
88+
endif()
89+
endif()
90+
91+
# --- Dependency: sha3 (via FetchContent) ---
92+
include(FetchContent)
93+
FetchContent_Declare(
94+
sha3
95+
GIT_REPOSITORY https://github.com/itzmeanjan/sha3.git
96+
GIT_TAG master
97+
GIT_SHALLOW TRUE
98+
)
99+
FetchContent_MakeAvailable(sha3)
100+
101+
# --- Library (header-only → INTERFACE) ---
102+
add_library(randomshake INTERFACE)
103+
target_include_directories(randomshake INTERFACE
104+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
105+
$<INSTALL_INTERFACE:include>
106+
)
107+
target_link_libraries(randomshake INTERFACE sha3)
108+
target_compile_features(randomshake INTERFACE cxx_std_20)
109+
110+
# --- Tests ---
111+
if(RANDOMSHAKE_BUILD_TESTS)
112+
enable_testing()
113+
if(RANDOMSHAKE_FETCH_DEPS)
114+
message(STATUS "Fetching GTest...")
115+
include(FetchContent)
116+
FetchContent_Declare(
117+
googletest
118+
URL https://github.com/google/googletest/archive/refs/tags/v1.17.0.zip
119+
DOWNLOAD_EXTRACT_TIMESTAMP TRUE
120+
)
121+
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
122+
FetchContent_MakeAvailable(googletest)
123+
else()
124+
find_package(GTest REQUIRED)
125+
endif()
126+
127+
file(GLOB TEST_SOURCES CONFIGURE_DEPENDS "tests/*.cpp")
128+
129+
add_executable(randomshake_tests ${TEST_SOURCES})
130+
target_link_libraries(randomshake_tests PRIVATE randomshake GTest::gtest_main)
131+
target_include_directories(randomshake_tests PRIVATE tests)
132+
target_compile_options(randomshake_tests PRIVATE ${RANDOMSHAKE_WARNING_FLAGS})
133+
134+
include(GoogleTest)
135+
gtest_discover_tests(randomshake_tests)
136+
endif()
137+
138+
# --- Benchmarks ---
139+
if(RANDOMSHAKE_BUILD_BENCHMARKS)
140+
if(RANDOMSHAKE_FETCH_DEPS)
141+
message(STATUS "Fetching Google Benchmark...")
142+
include(FetchContent)
143+
FetchContent_Declare(
144+
benchmark
145+
URL https://github.com/google/benchmark/archive/refs/tags/v1.9.5.zip
146+
DOWNLOAD_EXTRACT_TIMESTAMP TRUE
147+
)
148+
set(BENCHMARK_ENABLE_TESTING OFF CACHE BOOL "" FORCE)
149+
set(BENCHMARK_ENABLE_INSTALL OFF CACHE BOOL "" FORCE)
150+
FetchContent_MakeAvailable(benchmark)
151+
else()
152+
find_package(benchmark REQUIRED)
153+
endif()
154+
155+
file(GLOB BENCHMARK_SOURCES CONFIGURE_DEPENDS "benches/*.cpp")
156+
find_library(LIBPFM pfm)
157+
158+
add_executable(randomshake_benchmarks ${BENCHMARK_SOURCES})
159+
target_link_libraries(randomshake_benchmarks PRIVATE randomshake benchmark::benchmark_main)
160+
161+
if(LIBPFM)
162+
target_link_libraries(randomshake_benchmarks PRIVATE ${LIBPFM})
163+
message(STATUS "Found libpfm: ${LIBPFM} - linking it to benchmarks")
164+
endif()
165+
166+
target_include_directories(randomshake_benchmarks PRIVATE benches)
167+
target_compile_options(randomshake_benchmarks PRIVATE ${RANDOMSHAKE_WARNING_FLAGS})
168+
endif()
169+
170+
# --- Examples ---
171+
if(RANDOMSHAKE_BUILD_EXAMPLES)
172+
file(GLOB EXAMPLE_SOURCES CONFIGURE_DEPENDS "examples/*.cpp")
173+
foreach(ex_src ${EXAMPLE_SOURCES})
174+
get_filename_component(ex_name ${ex_src} NAME_WE)
175+
set(ex_target "${ex_name}_example")
176+
add_executable(${ex_target} ${ex_src})
177+
target_link_libraries(${ex_target} PRIVATE randomshake)
178+
target_compile_options(${ex_target} PRIVATE ${RANDOMSHAKE_WARNING_FLAGS})
179+
endforeach()
180+
endif()
181+
182+
# --- Install ---
183+
include(GNUInstallDirs)
184+
install(TARGETS randomshake EXPORT randomshake-config INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
185+
install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
186+
install(EXPORT randomshake-config NAMESPACE randomshake:: DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/randomshake)

Makefile

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

0 commit comments

Comments
 (0)