Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplified CMake #7

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions .github/workflows/cmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,14 @@ jobs:

- name: Build with CMAKE
run: |
mkdir build
cd build
cmake ..
make
cmake -Bbuild -DCMAKE_INSTALL_PREFIX=${{github.workspace}}/libxutils && cmake --build build --target install
ls ${{github.workspace}}/libxutils
ls ${{github.workspace}}/libxutils/include
ls ${{github.workspace}}/libxutils/lib
ls ${{github.workspace}}/libxutils/bin

- name: Uploading libxutils
uses: actions/upload-artifact@v4
with:
name: ubuntu_latest_libxutils
path: ${{github.workspace}}/libxutils
18 changes: 9 additions & 9 deletions .github/workflows/script.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ jobs:
./build.sh --tool=smake --tools --examples --ssl=no
./tools/xutils

- name: Build with CMAKE (SSL)
run: |
./build.sh --tool=cmake --tools --examples
./tools/build/xutils

- name: Build with CMAKE (no SSL)
run: |
./build.sh --tool=cmake --tools --examples --ssl=no
./tools/build/xutils
# - name: Build with CMAKE (SSL)
# run: |
# ./build.sh --tool=cmake --tools --examples
# ./tools/build/xutils
#
# - name: Build with CMAKE (no SSL)
# run: |
# ./build.sh --tool=cmake --tools --examples --ssl=no
# ./tools/build/xutils

- name: Build with Makefile (SSL)
run: |
Expand Down
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,8 @@ modules.order
Module.symvers
Mkfile.old
dkms.conf

# Idea
.idea
cmake-build-debug
cmake-build-release
117 changes: 26 additions & 91 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,106 +7,41 @@ ELSE()
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -O2 -Wall")
ENDIF()

set(XUTILS_INSTALL_PREFIX "/usr/local")
string(SUBSTRING "${XUTILS_INSTALL_PREFIX}" 0 2 PREFIX_START)

if("${PREFIX_START}" STREQUAL "./")
get_filename_component(CURRENT_WORKING_DIR "${CMAKE_SOURCE_DIR}" REALPATH)
string(SUBSTRING "${XUTILS_INSTALL_PREFIX}" 1 -1 RELATIVE_PATH)
set(CMAKE_INSTALL_PREFIX "${CURRENT_WORKING_DIR}${RELATIVE_PATH}")
endif()

set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
option(EXAMPLES "Build examples" ON)
option(TOOLS "Build tools" ON)

find_package(OpenSSL)
IF(OpenSSL_FOUND)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_XUTILS_USE_SSL")
ENDIF()

SET(HEADER_DST "include/xutils")
SET(SOURCE_DIR "./src")
SET(CRYPT_DIR "./src/crypt")
SET(DATA_DIR "./src/data")
SET(NET_DIR "./src/net")
SET(SYS_DIR "./src/sys")
if(OpenSSL_FOUND)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_XUTILS_USE_SSL")
endif()

include_directories(
${SOURCE_DIR}
${CRYPT_DIR}
${DATA_DIR}
${NET_DIR}
${SYS_DIR}
)
src)

file(GLOB_RECURSE xutils_SOURCE src/*.c)
add_library(xutils STATIC
./src/xver.c
./src/crypt/aes.c
./src/crypt/base64.c
./src/crypt/crc32.c
./src/crypt/crypt.c
./src/crypt/hmac.c
./src/crypt/md5.c
./src/crypt/rsa.c
./src/crypt/sha256.c
./src/crypt/sha1.c
./src/data/array.c
./src/data/hash.c
./src/data/jwt.c
./src/data/list.c
./src/data/map.c
./src/data/xbuf.c
./src/data/xjson.c
./src/data/xstr.c
./src/net/addr.c
./src/net/event.c
./src/net/http.c
./src/net/mdtp.c
./src/net/ntp.c
./src/net/rtp.c
./src/net/sock.c
./src/net/api.c
./src/net/ws.c
./src/sys/sync.c
./src/sys/thread.c
./src/sys/xcli.c
./src/sys/xcpu.c
./src/sys/xfs.c
./src/sys/xlog.c
./src/sys/xsig.c
./src/sys/xtime.c
./src/sys/xtop.c
./src/sys/xtype.c
)
${xutils_SOURCE})

install(TARGETS xutils DESTINATION lib)
target_link_libraries(xutils
PUBLIC
OpenSSL::SSL)

install(DIRECTORY "${SOURCE_DIR}/"
DESTINATION "${HEADER_DST}"
FILES_MATCHING
PATTERN "*.h"
)

install(DIRECTORY "${CRYPT_DIR}/"
DESTINATION "${HEADER_DST}"
FILES_MATCHING
PATTERN "*.h"
)
if(EXAMPLES)
add_subdirectory(examples)
endif()

install(DIRECTORY "${DATA_DIR}/"
DESTINATION "${HEADER_DST}"
FILES_MATCHING
PATTERN "*.h"
)
if(TOOLS)
add_subdirectory(tools)
endif()

install(DIRECTORY "${NET_DIR}/"
DESTINATION "${HEADER_DST}"
FILES_MATCHING
PATTERN "*.h"
)
include(GNUInstallDirs)
install(TARGETS xutils EXPORT xutils
ARCHIVE DESTINATION ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_BINDIR})

install(DIRECTORY "${SYS_DIR}/"
DESTINATION "${HEADER_DST}"
install(DIRECTORY
src/
DESTINATION ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_INCLUDEDIR}/xutils
FILES_MATCHING
PATTERN "*.h"
)
PATTERN "*.h")
11 changes: 6 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
####################################

CFLAGS = -D_XUTILS_USE_SSL -g -O2 -Wall -D_XUTILS_DEBUG -D_XUTILS_USE_GNU
CFLAGS += -I./src/crypt -I./src/data -I./src/net -I./src/sys -I./src
#CFLAGS += -I./src/crypt -I./src/data -I./src/net -I./src/sys -I./src
CFLAGS += -I./src
LIBS = -lssl -lcrypto -lpthread
NAME = libxutils.a
ODIR = ./build
Expand Down Expand Up @@ -65,10 +66,10 @@ install:
@test -d $(INSTALL_BIN) || mkdir -p $(INSTALL_BIN)
install -m 0755 $(ODIR)/$(NAME) $(INSTALL_BIN)/
@test -d $(INSTALL_INC) || mkdir -p $(INSTALL_INC)
cp -r ./src/crypt/*.h $(INSTALL_INC)/
cp -r ./src/data/*.h $(INSTALL_INC)/
cp -r ./src/net/*.h $(INSTALL_INC)/
cp -r ./src/sys/*.h $(INSTALL_INC)/
cp -r ./src/crypt/*.h $(INSTALL_INC)/crypt
cp -r ./src/data/*.h $(INSTALL_INC)/data
cp -r ./src/net/*.h $(INSTALL_INC)/net
cp -r ./src/sys/*.h $(INSTALL_INC)/sys
cp -r ./src/*.h $(INSTALL_INC)/

.PHONY: clean
Expand Down
24 changes: 15 additions & 9 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,11 @@ build_tools() {
[ "$TOOLS_DONE" -eq 1 ] && return
cd $PROJ_PATH/tools

if [[ $MAKE_TOOL == "cmake" ]]; then
mkdir -p build && cd build && cmake ..
TOOL_PATH=$PROJ_PATH/tools/build
fi
# if [[ $MAKE_TOOL == "cmake" ]]; then
# cmake -Bbuild -DCMAKE_INSTALL_PREFIX=./libxutils && cmake --build build --target install
# cmake -Bbuild #mkdir -p build && cd build && cmake ..
# TOOL_PATH=$PROJ_PATH/tools/build
# fi

make -j $CPU_COUNT
cd $PROJ_PATH
Expand All @@ -101,9 +102,9 @@ build_examples() {
[ "$EXAMPLES_DONE" -eq 1 ] && return
cd $PROJ_PATH/examples

if [[ $MAKE_TOOL == "cmake" ]]; then
mkdir -p build && cd build && cmake ..
fi
# if [[ $MAKE_TOOL == "cmake" ]]; then
# mkdir -p build && cd build && cmake ..
# fi

make -j $CPU_COUNT
cd $PROJ_PATH
Expand All @@ -119,8 +120,7 @@ build_library() {
make -j $CPU_COUNT
LIB_PATH=$PROJ_PATH
elif [[ $MAKE_TOOL == "cmake" ]]; then
mkdir -p build && cd build
cmake .. && make -j $CPU_COUNT
cmake -Bbuild -DCMAKE_INSTALL_PREFIX=./install && cmake --build build --target install
LIB_PATH=$PROJ_PATH/build
elif [[ $MAKE_TOOL == "smake" ]]; then
smake . && make -j $CPU_COUNT
Expand Down Expand Up @@ -172,16 +172,22 @@ build_library

for arg in "$@"; do
if [[ $arg == "--examples" ]]; then
if [[ $MAKE_TOOL != "cmake" ]]; then
build_examples
fi
fi

if [[ $arg == "--tools" ]]; then
if [[ $MAKE_TOOL != "cmake" ]]; then
build_tools
fi
fi

if [[ $arg == "--install" ]]; then
if [[ $MAKE_TOOL != "cmake" ]]; then
install_library
install_tools
fi
fi
done

Expand Down
70 changes: 31 additions & 39 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,67 +1,59 @@
cmake_minimum_required(VERSION 3.10)
project(xutils)
project(examples)

set(CMAKE_C_STANDARD 11)

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -O2 -Wall")

set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
set(THREADS_PREFER_PTHREAD_FLAG TRUE)

set(XUTILS_INCLUDE_DIRS
../src/crypt
../src/data
../src/net
../src/sys
../src
)

include_directories(${XUTILS_INCLUDE_DIRS})
find_package(Threads REQUIRED)

set(XUTILS_LIBRARY_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../build)
set(XUTILS_STATIC_LIB ${XUTILS_LIBRARY_ROOT}/libxutils.a)
set(XUTILS_SHARED_LIB ${XUTILS_LIBRARY_ROOT}/libxutils.so)

# Check if static library exists
if(EXISTS ${XUTILS_STATIC_LIB})
list(APPEND EXTRA_LIBS ${XUTILS_STATIC_LIB})
elseif(EXISTS ${XUTILS_SHARED_LIB})
list(APPEND EXTRA_LIBS ${XUTILS_SHARED_LIB})
else()
message(FATAL_ERROR "Neither static nor shared library found for xutils.")
endif()

# if OpenSSL is found, add the definitions and libraries
set(XUTILS_USE_SSL $ENV{XUTILS_USE_SSL})
if(XUTILS_USE_SSL STREQUAL "y")
find_package(OpenSSL)
if(OpenSSL_FOUND)
add_definitions(-D_XUTILS_USE_SSL)
list(APPEND EXTRA_LIBS ${OPENSSL_LIBRARIES})
list(APPEND EXTRA_LIBS OpenSSL::SSL)#${OPENSSL_LIBRARIES})
endif()
endif()

# replace with your actual source files
set(SOURCE_FILES
# this is a guarantee that we will use library packages outside of this project well
include_directories(${CMAKE_INSTALL_PREFIX}/include)
set(EXAMPLES_NAMES
array.c
http-server.c
tcp-server.c
ws-server.c
ws-client.c
statcov.c
strings.c
thread.c
events.c
files.c
http-server.c
json.c
xlog.c
jwt.c
list.c
ntp.c
jwt.c
rsa.c)
rsa.c
statcov.c
strings.c
tcp-server.c
thread.c
ws-client.c
ws-server.c
xlog.c)

foreach(sourcefile ${SOURCE_FILES})
include(GNUInstallDirs)
foreach(sourcefile ${EXAMPLES_NAMES})
string(REPLACE ".c" "" execname ${sourcefile})
add_executable(${execname} ${sourcefile})
target_link_libraries(${execname} Threads::Threads ${EXTRA_LIBS})
target_link_libraries(${execname}
-L${CMAKE_INSTALL_PREFIX}/lib
xutils
Threads::Threads
${EXTRA_LIBS})

add_dependencies(${execname} xutils)

install(TARGETS ${execname} EXPORT examples
ARCHIVE DESTINATION ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_BINDIR})
endforeach()

2 changes: 1 addition & 1 deletion examples/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

#include "xstd.h"
#include "array.h"
#include <data/array.h>

typedef struct {
int key;
Expand Down
Loading
Loading