Skip to content

Commit

Permalink
Add cmakelists
Browse files Browse the repository at this point in the history
  • Loading branch information
kassane committed Sep 10, 2023
1 parent 85958a3 commit 7db9b7d
Show file tree
Hide file tree
Showing 4 changed files with 375 additions and 0 deletions.
57 changes: 57 additions & 0 deletions .github/workflows/cmake.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
name: CMake Build

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
build:
strategy:
fail-fast: false
matrix:
runs-on: [ubuntu-latest, macos-latest]
runs-on: ${{ matrix.runs-on }}
steps:
- uses: actions/checkout@v3
with:
submodules: recursive
fetch-depth: 0
- uses: lukka/get-cmake@latest
with:
cmakeVersion: latest
ninjaVersion: latest

- name: Build Summary ${{ matrix.targets }}
run: cmake -B build -DCMAKE_BUILD_TYPE=Release; cmake --build build --parallel

# mingw:
# runs-on: windows-latest
# strategy:
# fail-fast: false
# matrix:
# include: [{ msystem: CLANG64, arch: x86_64, prefix: /clang64 }, { msystem: CLANG32, arch: i686, prefix: /clang32 }]
# steps:
# - uses: actions/checkout@v3
# with:
# path: temp
# submodules: recursive
# fetch-depth: 0
# - uses: msys2/setup-msys2@v2
# with:
# msystem: ${{ matrix.msystem }}
# path-type: inherit
# location: D:\
# install: git mingw-w64-clang-${{ matrix.arch }}-cmake mingw-w64-clang-${{ matrix.arch }}-openssl mingw-w64-clang-${{ matrix.arch }}-winpthreads-git mingw-w64-clang-${{ matrix.arch }}-headers
# update: true

# - name: Move Checkout
# run: |
# Copy-Item -Path ".\temp" -Destination "C:\_" -Recurse

# - name: Build Summary - ${{ matrix.arch }}
# shell: msys2 {0}
# run: |
# cd /C/_
# cmake -B build -DCMAKE_BUILD_TYPE=Release; cmake --build build --config release --parallel
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
tmp/*
.DS_Store
*.pem

build/
306 changes: 306 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,306 @@
cmake_minimum_required(VERSION 3.12)

project(cstl VERSION 0.80)

# Set the source directories
set(LIB_ROOT lib)
set(LIB_CONCAT_FOLDER fio-stl)

include_directories(".")

option(ENABLE_SHARED "Build cstl as shared library" OFF)
option(EXAMPLES_BUILD "Build Examples" ON)
option(TESTS_BUILD "Build Tests" ON)

# Detect endianess
include(CheckTypeSize)
check_type_size("void*" SIZE_OF_POINTER)
if(${SIZE_OF_POINTER} EQUAL 8)
set(FLAGS "__BIG_ENDIAN__")
else()
set(FLAGS "__BIG_ENDIAN__=0")
endif()

find_package(Threads REQUIRED)
# Detect SSL/TLS Libraries
find_package(OpenSSL 3.0 REQUIRED)

if(OpenSSL_FOUND)
message(STATUS "OpenSSL found.")
set(HAVE_OPENSSL 1)
else()
message(WARNING "OpenSSL not found. SSL/TLS support will be disabled.")
set(HAVE_OPENSSL 0)
endif()

# Detect Sodium Library
find_package(Sodium)

if(Sodium_FOUND)
message(STATUS "Sodium found.")
set(HAVE_SODIUM 1)
else()
message(WARNING "Sodium not found. Sodium support will be disabled.")
set(HAVE_SODIUM 0)
endif()

# Detect 'struct tm' fields
include(CheckStructHasMember)
check_struct_has_member("struct tm" tm_zone time.h HAVE_TM_TM_ZONE)

# Detect SystemV socket libraries
include(CheckLibraryExists)
check_library_exists(socket connect "" HAVE_SOCKET)
check_library_exists(nsl gethostname "" HAVE_NSL)

if(HAVE_SOCKET AND HAVE_NSL)
link_libraries(socket nsl)
endif()
if(WIN32)
link_libraries(ws2_32)
endif()

# Detect the `sendfile` system call
include(CheckCSourceCompiles)
check_c_source_compiles("
#define _GNU_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <sys/sendfile.h>
int main(void) {
off_t offset = 0;
ssize_t result = sendfile(2, 1, (off_t *)&offset, 300);
}
" HAVE_SENDFILE_LINUX)

check_c_source_compiles("
#define _GNU_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/uio.h>
int main(void) {
off_t sent = 0;
off_t offset = 0;
ssize_t result = sendfile(2, 1, offset, (size_t)sent, NULL, &sent, 0);
}
" HAVE_SENDFILE_BSD)

check_c_source_compiles("
#define _GNU_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/uio.h>
int main(void) {
off_t sent = 0;
off_t offset = 0;
ssize_t result = sendfile(2, 1, offset, &sent, NULL, 0);
}
" HAVE_SENDFILE_APPLE)

if(HAVE_SENDFILE_LINUX)
set(FLAGS "${FLAGS} USE_SENDFILE_LINUX HAVE_SENDFILE")
elseif(HAVE_SENDFILE_BSD)
set(FLAGS "${FLAGS} USE_SENDFILE_BSD HAVE_SENDFILE")
elseif(HAVE_SENDFILE_APPLE)
set(FLAGS "${FLAGS} USE_SENDFILE_APPLE HAVE_SENDFILE")
else()
set(FLAGS "${FLAGS} USE_SENDFILE=0")
endif()

# Detect polling mechanisms (kqueue / epoll / poll)
check_c_source_compiles("
#define _GNU_SOURCE
#include <stdlib.h>
#include <sys/event.h>
int main(void) {
int fd = kqueue();
}
" HAVE_KQUEUE)

check_c_source_compiles("
#define _GNU_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/epoll.h>
int main(void) {
int fd = epoll_create1(EPOLL_CLOEXEC);
}
" HAVE_EPOLL)

check_c_source_compiles("
#define _GNU_SOURCE
#include <stdlib.h>
#include <poll.h>
int main(void) {
struct pollfd plist[18];
memset(plist, 0, sizeof(plist[0]) * 18);
poll(plist, 1, 1);
}
" HAVE_POLL)

# Manual selection or fallback
if(NOT WIN32)
if(FIO_ENGINE_POLL)
set(FLAGS "${FLAGS} FIO_ENGINE_POLL HAVE_POLL")
elseif(FIO_ENGINE_EPOLL)
set(FLAGS "${FLAGS} FIO_ENGINE_EPOLL HAVE_EPOLL")
elseif(FIO_ENGINE_KQUEUE)
set(FLAGS "${FLAGS} FIO_ENGINE_KQUEUE HAVE_KQUEUE")
else()
if(HAVE_KQUEUE)
set(FLAGS "${FLAGS} FIO_ENGINE_KQUEUE HAVE_KQUEUE")
elseif(HAVE_EPOLL)
set(FLAGS "${FLAGS} FIO_ENGINE_EPOLL HAVE_EPOLL")
elseif(HAVE_POLL)
set(FLAGS "${FLAGS} FIO_ENGINE_POLL HAVE_POLL")
else()
message(FATAL_ERROR "No supported polling engine detected. Unable to compile facil.io.")
endif()
endif()
endif()



# Define the source files for the library
file(GLOB_RECURSE LIB_SOURCES ${LIB_ROOT}/*.c ${LIB_ROOT}/*.cpp ${LIB_ROOT}/*.cxx ${LIB_ROOT}/*.c++)
set(SOURCES ${LIB_SOURCES})

# Optimization level
set(OPTIMIZATION "-O3")
# Optimization level in debug mode
set(OPTIMIZATION_DEBUG "-O0 -g -coverage -fsanitize=address -fsanitize=undefined -fno-omit-frame-pointer -fno-builtin")
# Warning flags
set(WARNINGS "-Wno-missing-field-initializers -Wformat-security")
# Preprocessor definitions
set(FLAGS "-DFIO_LEAK_COUNTER -DFIO_FIOBJ -DFIOBJ_MALLOC")
if(WIN32 AND NOT MSVC)
set(FLAGS "${FLAGS} -D_GNU_SOURCE -D__MINGW32__")
endif()
# C specific compiler options
set(C_EXTRA_OPT "")
# C++ specific compiler options
set(CXX_EXTRA_OPT "-Wno-keyword-macro -Wno-vla-extension -Wno-c99-extensions -Wno-zero-length-array -Wno-variadic-macros -Wno-missing-braces")

# Create the library target
if(ENABLE_SHARED)
add_library(${PROJECT_NAME} SHARED ${SOURCES})
if(HAVE_SSL EQUAL 1)
target_link_libraries(${PROJECT_NAME} INTERFACE Threads::Threads OpenSSL::SSL OpenSSL::Crypto)
elseif(HAVE_SODIUM EQUAL 1)
target_link_libraries(${PROJECT_NAME} INTERFACE Threads::Threads Sodium::Sodium)
else()
target_link_libraries(${PROJECT_NAME} INTERFACE Threads::Threads)
endif()
else()
add_library(${PROJECT_NAME} STATIC ${SOURCES})
endif()
# Add the definitions to the target
target_compile_options(${PROJECT_NAME} PRIVATE
$<$<CONFIG:Debug>:${OPTIMIZATION_DEBUG} ${WARNINGS} ${CXX_EXTRA_OPT}> "-DDEBUG=1"
$<$<NOT:$<CONFIG:Debug>>:${OPTIMIZATION} ${WARNINGS}>
)
target_compile_definitions(${PROJECT_NAME} PRIVATE ${FLAGS})

set(PKG_CONFIG_FILE "${CMAKE_BINARY_DIR}/${PROJECT_NAME}.pc")
configure_file(${CMAKE_SOURCE_DIR}/${PROJECT_NAME}.pc.in ${PKG_CONFIG_FILE} @ONLY)

# Install the generated .pc file
install(FILES ${PKG_CONFIG_FILE} DESTINATION ${CMAKE_INSTALL_PREFIX}/lib/pkgconfig)

if(EXAMPLES_BUILD)
# Define a function to build and run an example
function(build_and_run_example EXAMPLE_NAME)
add_executable(${EXAMPLE_NAME} examples/${EXAMPLE_NAME}.c)
if(${EXAMPLE_NAME} STREQUAL server OR ${EXAMPLE_NAME} STREQUAL client)
if(HAVE_SODIUM EQUAL 1 AND HAVE_SSL EQUAL 0)
target_link_libraries(${EXAMPLE_NAME} PUBLIC ${PROJECT_NAME} PRIVATE Threads::Threads Sodium::Sodium)
elseif(HAVE_SSL EQUAL 1 AND HAVE_SODIUM EQUAL 0)
target_link_libraries(${EXAMPLE_NAME} PUBLIC ${PROJECT_NAME} PRIVATE Threads::Threads OpenSSL::SSL OpenSSL::Crypto)
endif()
else()
target_link_libraries(${EXAMPLE_NAME} PUBLIC ${PROJECT_NAME} PRIVATE Threads::Threads)
endif()
target_compile_definitions(${EXAMPLE_NAME} PRIVATE ${FLAGS})
add_custom_target(run_${EXAMPLE_NAME}
COMMAND ${EXAMPLE_NAME}
DEPENDS ${EXAMPLE_NAME}
)
endfunction()

# List of example files to build and run
# comment all examples w/ issue build
set(EXAMPLES
array
bstr
chat
client
# fiobj
map
server
string
)

# Build and run each example
foreach(EXAMPLE ${EXAMPLES})
build_and_run_example(${EXAMPLE})
endforeach()
endif()

if(TESTS_BUILD)
# Define a function to build and run a test
function(build_and_run_test TEST_NAME)
if(${TEST_NAME} STREQUAL cpp)
add_executable(${TEST_NAME} tests/${TEST_NAME}.cpp)
else()
add_executable(${TEST_NAME} tests/${TEST_NAME}.c)
endif()
if(${TEST_NAME} STREQUAL base64 OR ${TEST_NAME} STREQUAL stl-mutex)
if(HAVE_SODIUM EQUAL 1 AND HAVE_SSL EQUAL 0)
target_link_libraries(${TEST_NAME} PUBLIC ${PROJECT_NAME} PRIVATE Threads::Threads Sodium::Sodium)
elseif(HAVE_SSL EQUAL 1 AND HAVE_SODIUM EQUAL 0)
target_link_libraries(${TEST_NAME} PUBLIC ${PROJECT_NAME} PRIVATE Threads::Threads OpenSSL::SSL OpenSSL::Crypto)
endif()
else()
target_link_libraries(${TEST_NAME} PUBLIC ${PROJECT_NAME} PRIVATE Threads::Threads)
endif()
target_compile_definitions(${TEST_NAME} PRIVATE ${FLAGS} "-DTESTS=1")
add_custom_target(run_${TEST_NAME}
COMMAND ${TEST_NAME}
DEPENDS ${TEST_NAME}
)
endfunction()

# List of test files to build and run
# comment all tests w/ issue build
set(TESTS
# array
base64
# cpp
http1-parser-old
http1-parser
# json
# json_find
# json_minify
malloc
mempool
# mustache
noop
# random
slowloris
# stl-mutex
# stl
url
)

# Build and run each test
foreach(TEST ${TESTS})
build_and_run_test(${TEST})
endforeach()
endif()
10 changes: 10 additions & 0 deletions cstl.pc.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
prefix=@CMAKE_INSTALL_PREFIX@
exec_prefix=@CMAKE_INSTALL_PREFIX@
libdir=${prefix}/lib
includedir=${prefix}/include

Name: CSTL
Description: The Web microFramework and Server Toolbox library for C
Version: @PROJECT_VERSION@
Cflags: -I${includedir}
Libs: -L${libdir} -l@CMAKE_SHARED_LIBRARY_PREFIX@cstl

0 comments on commit 7db9b7d

Please sign in to comment.