Skip to content

Commit 5c2d23a

Browse files
committed
Added support for CMake build system
Created CMakeLists.txt and updated .gitignore accordingly. Having support for CMake enables liburing to be added as a dependency via the FetchContent interface, so that other projects build and link against liburing on supported kernel versions even if no system installation is present. This can be useful on HPC systems or other shared clusters where the approval process for system-wide installation is slow. It also makes it easier for users to test and compare more recent versions of the library to an existing system installation. Signed-off-by: Alecto Perez <[email protected]>
1 parent 98db8e8 commit 5c2d23a

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,6 @@ config-host.mak
139139
config.log
140140

141141
liburing.pc
142+
143+
/.ccls-cache/
144+
/build/

CMakeLists.txt

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
cmake_minimum_required(VERSION 3.14)
2+
3+
project("liburing")
4+
set(libname "uring")
5+
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
6+
7+
############################
8+
## Add headers to library ##
9+
############################
10+
function(add_headers VAR dir)
11+
set(headers ${${VAR}})
12+
foreach (header ${ARGN})
13+
set(headers ${headers} ${dir}/${header})
14+
endforeach()
15+
set(${VAR} ${headers} PARENT_SCOPE)
16+
endfunction()
17+
18+
add_headers(
19+
liburing_headers # Variable name
20+
src/include # Directory to find headers
21+
liburing.h
22+
liburing/barrier.h
23+
liburing/io_uring.h)
24+
25+
file(GLOB liburing_sources src/*.c)
26+
# string(REPLACE ";" " " liburing_sources "${liburing_sources}")
27+
28+
add_library("${libname}" ${liburing_sources} ${liburing_headers})
29+
30+
target_include_directories(
31+
"${libname}"
32+
PUBLIC
33+
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/src/include/>
34+
$<INSTALL_INTERFACE:include>)
35+
36+
# If we're building this project directly (rather than as a dependency), we
37+
# have to build tests
38+
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
39+
40+
# Run configure to generate config-host.h, if configure hasn't been run
41+
if(NOT EXISTS "${PROJECT_SOURCE_DIR}/config-host.h")
42+
execute_process(
43+
COMMAND "${PROJECT_SOURCE_DIR}/configure"
44+
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}")
45+
endif()
46+
47+
include(CTest)
48+
49+
# Tests need to be linked against pthreads
50+
set(THREADS_PREFER_PTHREAD_FLAG ON)
51+
find_package(Threads REQUIRED)
52+
53+
set(liburing_test_flags
54+
-include "${PROJECT_SOURCE_DIR}/config-host.h"
55+
-g
56+
-O2
57+
-D_GNU_SOURCE
58+
-D__SANE_USERSPACE_TYPES__
59+
-Wall
60+
-Wextra
61+
-Wno-unused-parameter
62+
-Wno-sign-compare
63+
-Wstringop-overflow=0
64+
-Warray-bounds=0)
65+
file(GLOB liburing_tests "test/*.c")
66+
# Remove the helpers.c, which doesn't contain a main
67+
list(FILTER liburing_tests EXCLUDE REGEX "helpers.c")
68+
foreach(test_file ${liburing_tests})
69+
70+
# Get the name of the target. This will be the file name, stripped of
71+
# ".c" at the end.
72+
get_filename_component(target ${test_file} NAME_WLE)
73+
74+
# Create an executable target for the test; add test/helpers.c as a source
75+
add_executable(${target} ${test_file} test/helpers.c)
76+
77+
# Target liburing and pthreads
78+
target_link_libraries(
79+
${target}
80+
PRIVATE
81+
${libname}
82+
Threads::Threads)
83+
84+
# Add additional compiler options and definitions
85+
target_compile_options(${target} PRIVATE ${liburing_test_flags})
86+
87+
# Register the test with ctest
88+
add_test(NAME "test-${target}" COMMAND "${target}")
89+
endforeach()
90+
endif()

0 commit comments

Comments
 (0)