Skip to content

Commit

Permalink
Add scripts for building with CMake
Browse files Browse the repository at this point in the history
- support Conan
- finder for CppUTest
- options to disable unittests, examples and enable C++ mangling
  • Loading branch information
matlo607 authored and Matthieu Longo committed Jan 18, 2019
1 parent 6355112 commit ac11624
Show file tree
Hide file tree
Showing 6 changed files with 347 additions and 0 deletions.
117 changes: 117 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
project(CSlim-project)
set(CSlim_version_major 0)
set(CSlim_version_minor 2)

cmake_minimum_required(VERSION 3.5)

if (EXISTS "${CMAKE_SOURCE_DIR}/cmake/Modules/")
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")
endif()

option(CSLIM_DISABLE_UNIT_TESTS "Disable unit tests" OFF)
option(CSLIM_DISABLE_EXAMPLES "Disable unit tests" OFF)
option(CSLIM_BUILD_AS_CPP "Build CSlim as C++" OFF)

###############
# Conan support
###############

find_program (CONAN_BIN conan)
if(CONAN_BIN)
message (STATUS "Found conan C++ package manager: ${CONAN_BIN}")
find_file (CONANFILE NAMES "conanfile.txt" HINTS "${CMAKE_SOURCE_DIR}")
if (CONANFILE)
message (STATUS "Found ${CONANFILE}")
if (NOT EXISTS "${CMAKE_BINARY_DIR}/conanbuildinfo.cmake")
message(STATUS "Trying to execute 'conan install'")
get_filename_component(CONANFILE_DIR ${CONANFILE} DIRECTORY)
execute_process(COMMAND conan install ${CONANFILE_DIR} --build=missing)
endif()
endif()

if (EXISTS "${CMAKE_BINARY_DIR}/conanbuildinfo.cmake")
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
message(STATUS "conan_basic_setup()")
conan_basic_setup(TARGETS)
else()
message(WARNING "Please run 'conan install' if you plan to use conan")
endif()
endif()

if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
#set(CMAKE_BUILD_TYPE Debug)
endif()

###############################
# Check compiler's capabilities
###############################

include (CheckCCompilerFlag)
if (CSLIM_BUILD_AS_CPP)
include (CheckCXXCompilerFlag)
endif()

if(CMAKE_C_COMPILER_ID MATCHES "Clang")
set (CMAKE_COMPILER_IS_CLANG true)
elseif(CMAKE_C_COMPILER_ID MATCHES "MSVC")
set (CMAKE_COMPILER_IS_MSVC true)
endif()

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_COMPILER_IS_CLANG)
find_program (LINKER_BIN gold)
if(LINKER_BIN)
set(LINKER_BIN "gold")
else()
find_program (LINKER_BIN ld.gold)
if(LINKER_BIN)
set(LINKER_BIN "ld.gold")
else()
set(LINKER_BIN "ld")
endif()
endif()

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -W")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wextra")
#set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror")

CHECK_C_COMPILER_FLAG("-fuse-ld=${LINKER_BIN}" USE_LINKER_FLAG)
if(USE_LINKER_FLAG)
set (CMAKE_SHARED_LINKER_FLAGS "-fuse-ld=${LINKER_BIN} ${CMAKE_SHARED_LINKER_FLAGS}")
endif()
elseif(CMAKE_COMPILER_IS_MSVC)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_SECURE_SCL=0")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_CRT_SECURE_NO_DEPRECATE")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_CRT_NONSTDC_NO_DEPRECATE")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_SCL_SECURE_NO_DEPRECATE")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Zi")
endif()

if (CSLIM_BUILD_AS_CPP)
add_definitions(-DCPP_COMPILING)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_C_FLAGS}")
endif()

message(STATUS "CMAKE_INSTALL_PREFIX: ${CMAKE_INSTALL_PREFIX}")
set (CMAKE_INSTALL_INCLUDEDIR "${CMAKE_INSTALL_PREFIX}/include")
set (CMAKE_INSTALL_LIBDIR "${CMAKE_INSTALL_PREFIX}/lib")
set (CMAKE_INSTALL_BINDIR "${CMAKE_INSTALL_PREFIX}/bin")
message(STATUS "CMAKE_INSTALL_INCLUDEDIR: ${CMAKE_INSTALL_INCLUDEDIR}")
message(STATUS "CMAKE_INSTALL_LIBDIR: ${CMAKE_INSTALL_LIBDIR}")
message(STATUS "CMAKE_INSTALL_BINDIR: ${CMAKE_INSTALL_BINDIR}")

add_subdirectory(src)

if (CSLIM_DISABLE_EXAMPLES)
message(STATUS "Skipping examples")
else()
add_subdirectory("fixtures")
endif()

if(CSLIM_DISABLE_UNIT_TESTS)
message(STATUS "Skipping unit tests")
else()
enable_testing()
add_subdirectory(tests)
endif()
64 changes: 64 additions & 0 deletions cmake/Modules/FindCppUTest.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# - Try to find CppUTest
#
# The following variables are optionally searched for defaults:
# CONAN_CPPUTEST_ROOT: Base directory where all CppUTest components are found
#
# The following are set after configuration is done:
# CppUTest_FOUND
# CPPUTEST_INCLUDE_DIRS --> CppUTest's include directories
# CPPUTEST_LIBRARIES --> CppUTest's static libraries

include(FindPackageHandleStandardArgs)

if (CONAN_CPPUTEST_ROOT)
message(STATUS "CONAN_CPPUTEST_ROOT: ${CONAN_CPPUTEST_ROOT}")
set(CONAN_CPPUTEST_ROOT "" CACHE PATH "Folder contains CppUTest")
endif()


find_path(CPPUTEST_INCLUDE_DIR "CppUTest/CommandLineTestRunner.h"
PATHS ${CONAN_CPPUTEST_ROOT} "/usr/include")

find_library(CPPUTEST_LIBRARY "CppUTest"
PATHS ${CONAN_CPPUTEST_ROOT} "/usr/lib"
PATH_SUFFIXES "lib" "lib64")

find_library(CPPUTESTEXT_LIBRARY "CppUTestExt"
PATHS ${CONAN_CPPUTEST_ROOT} "/usr/lib"
PATH_SUFFIXES "lib" "lib64")

find_package_handle_standard_args(CppUTest DEFAULT_MSG
CPPUTEST_INCLUDE_DIR
CPPUTEST_LIBRARY
CPPUTESTEXT_LIBRARY)

if (CppUTest_FOUND)
set(CPPUTEST_INCLUDE_DIRS ${CPPUTEST_INCLUDE_DIR})
set(CPPUTEST_LIBRARIES ${CPPUTEST_LIBRARY} ${CPPUTESTEXT_LIBRARY})
if (WIN32)
set(CPPUTEST_LIBRARIES ${CPPUTEST_LIBRARIES} "winmm.lib")
endif()

mark_as_advanced(CONAN_CPPUTEST_ROOT
CPPUTEST_INCLUDE_DIR
CPPUTEST_LIBRARY
CPPUTESTEXT_LIBRARY)

if(NOT TARGET CppUTest::CppUTest)
add_library(CppUTest::CppUTest UNKNOWN IMPORTED)
set_target_properties(CppUTest::CppUTest PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${CPPUTEST_INCLUDE_DIRS}")
set_target_properties(CppUTest::CppUTest PROPERTIES
IMPORTED_LOCATION ${CPPUTEST_LIBRARY})
if (WIN32)
set_target_properties(CppUTest::CppUTest PROPERTIES
IMPORTED_LINK_INTERFACE_LIBRARIES "winmm.lib")
endif()

add_library(CppUTest::CppUTestExt UNKNOWN IMPORTED)
set_target_properties(CppUTest::CppUTestExt PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${CPPUTEST_INCLUDE_DIRS}")
set_target_properties(CppUTest::CppUTestExt PROPERTIES
IMPORTED_LOCATION ${CPPUTESTEXT_LIBRARY})
endif()
endif()
21 changes: 21 additions & 0 deletions fixtures/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
set(TARGET examples)
project(${TARGET})

set(fixtures_sources
Main.c
DecisionTableExample.c
ExceptionsExample.c
FixtureInCpp.cpp
Fixtures.c
QueryTableExample.c
ScriptTableExample.c
ScriptTableExampleEcho.c
ScriptTableExampleEchoLoudly.c
)
if (CSLIM_BUILD_AS_CPP)
set_source_files_properties(${fixtures_sources} PROPERTIES LANGUAGE "CXX")
endif()

add_executable(${TARGET} ${fixtures_sources})

target_link_libraries(${TARGET} PUBLIC CSlim)
80 changes: 80 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
set(TARGET_CSlim CSlim)
project(${TARGET_CSlim})

if (UNIX)
set(cslim_com_sources
Com/SocketServer.c
Com/TcpComLink.c
)
else()
set(cslim_com_sources
ComWin32/SocketServer.c
ComWin32/TcpComLink.c
)
endif()

set(cslim_sources
CSlim/ListExecutor.c
CSlim/Slim.c
CSlim/SlimConnectionHandler.c
CSlim/SlimList.c
CSlim/SlimListDeserializer.c
CSlim/SlimListSerializer.c
CSlim/SlimUtil.c
ExecutorC/StatementExecutor.c
ExecutorC/SymbolTable.c
)

if (CSLIM_BUILD_AS_CPP)
set_source_files_properties(${cslim_com_sources} PROPERTIES LANGUAGE "CXX")
set_source_files_properties(${cslim_sources} PROPERTIES LANGUAGE "CXX")
endif()

add_library(${TARGET_CSlim} STATIC
${cslim_sources}
${cslim_com_sources}
)

target_include_directories(${TARGET_CSlim} PUBLIC
$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/include/CSlim>
$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/include/ExecutorC>
$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/include/Com>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}> # <prefix>/include/CSlim
)
if (WIN32)
target_link_libraries(${TARGET_CSlim} PUBLIC WS2_32)
endif()

install(DIRECTORY "${CMAKE_SOURCE_DIR}/include/CSlim" DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
install(DIRECTORY "${CMAKE_SOURCE_DIR}/include/ExecutorC" DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
install(DIRECTORY "${CMAKE_SOURCE_DIR}/include/Com" DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})

install(TARGETS "${TARGET_CSlim}"
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)

set(TARGET_CSlim_main CSlim_main)
set(cslim_main_sources CSlim_main/Main.c)
if (CSLIM_BUILD_AS_CPP)
set_source_files_properties(${cslim_main_sources} PROPERTIES LANGUAGE "CXX")
endif()
add_library(${TARGET_CSlim_main} STATIC ${cslim_main_sources})
target_link_libraries(${TARGET_CSlim_main} PUBLIC ${TARGET_CSlim})
install(TARGETS "${TARGET_CSlim_main}"
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})

if(CMAKE_COMPILER_IS_MSVC)
# TARGET_PDB_FILE is not supported with static libraries
# https://gitlab.kitware.com/cmake/cmake/issues/16932
#install(FILES $<TARGET_PDB_FILE:${TARGET_CSlim}> DESTINATION ${CMAKE_INSTALL_BINDIR} OPTIONAL)
#install(FILES $<TARGET_PDB_FILE:${TARGET_CSlim_main}> DESTINATION ${CMAKE_INSTALL_BINDIR} OPTIONAL)

# workaround
install(FILES
"${CMAKE_CURRENT_BINARY_DIR}/${TARGET_CSlim}.dir/$\{CMAKE_INSTALL_CONFIG_NAME\}/${TARGET_CSlim}.pdb"
"${CMAKE_CURRENT_BINARY_DIR}/${TARGET_CSlim_main}.dir/$\{CMAKE_INSTALL_CONFIG_NAME\}/${TARGET_CSlim_main}.pdb"
DESTINATION ${CMAKE_INSTALL_LIBDIR} OPTIONAL)
endif()

34 changes: 34 additions & 0 deletions src/CSlim_main/Main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include "Slim.h"
#include "SocketServer.h"
#include "SlimConnectionHandler.h"
#include "TcpComLink.h"
#include <stdlib.h>
#include <string.h>

Slim * slim;

int connection_handler(int socket)
{
int result = 0;
TcpComLink * comLink = TcpComLink_Create(socket);

result = Slim_HandleConnection(slim, (void*)comLink, &TcpComLink_send, &TcpComLink_recv);

TcpComLink_Destroy(comLink);

return result;
}

int main(int ac, char** av)
{
slim = Slim_Create();
SocketServer* server = SocketServer_Create();
SocketServer_register_handler(server, &connection_handler);

int result = SocketServer_Run(server, av[1]);

SocketServer_Destroy(server);
Slim_Destroy(slim);
return result;
}

31 changes: 31 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
include(CTest)

set(TARGET CSlim-tests)
project(${TARGET})

find_package(CppUTest REQUIRED)

if (CSLIM_BUILD_AS_CPP)
set_source_files_properties(CSlim/TestSlim.c PROPERTIES LANGUAGE "CXX")
endif()

add_executable(${TARGET}
AllTests.cpp
CSlim/SlimListDeserializerTest.cpp
CSlim/ListExecutorTest.cpp
CSlim/SymbolTableTest.cpp
CSlim/SlimConnectionHandlerTest.cpp
CSlim/SlimListTest.cpp
CSlim/SlimListSerializerTest.cpp
CSlim/StatementExecutorTest.cpp
CSlim/SlimUtilTest.cpp
CSlim/TestSlim.c
)
target_include_directories(${TARGET} PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/CSlim")
target_link_libraries(${TARGET} PUBLIC CSlim CppUTest::CppUTest CppUTest::CppUTestExt)
#target_compile_options(${TARGET} PUBLIC
# -Dmalloc=cpputest_malloc
# -Dfree=cpputest_free
# -Drealloc=cpputest_realloc
# )
add_test(NAME ${TARGET} COMMAND ${TARGET} -v)

0 comments on commit ac11624

Please sign in to comment.