Skip to content

Commit

Permalink
Testing both shared and static (#4)
Browse files Browse the repository at this point in the history
* Adding both shared and static libraries, now CI tests both, but not sure if this is efficient.

---------

Co-authored-by: kreijstal <[email protected]>
  • Loading branch information
Kreijstal and Kreijstal authored Nov 11, 2023
1 parent 086176b commit 30e06da
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 33 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/cmake-multi-platform.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ jobs:
matrix:
os: [ubuntu-latest, windows-latest]
build_type: [Release]
library_type: [STATIC, SHARED] # STATIC, SHARED, MODULE, OBJECT, INTERFACE
c_compiler: [gcc, clang]
include:
- os: windows-latest
Expand Down Expand Up @@ -66,7 +67,7 @@ jobs:
# See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type
if: matrix.os == 'windows-latest'
run: |
cmake -B $BUILD_OUTPUT_DIR -G"MSYS Makefiles" -S $BUILD_OUTPUT_DIR/..
cmake -DFMEM_STATIC=${{ matrix.library_type == 'STATIC' }} -B $BUILD_OUTPUT_DIR -G"MSYS Makefiles" -S $BUILD_OUTPUT_DIR/..
shell: msys2 {0}

- name: Configure CMake (Ubuntu)
Expand All @@ -76,10 +77,9 @@ jobs:
-DCMAKE_CXX_COMPILER=${{ matrix.cpp_compiler }}
-DCMAKE_C_COMPILER=${{ matrix.c_compiler }}
-DCMAKE_BUILD_TYPE=${{ matrix.build_type }}
-DFMEM_STATIC=${{ matrix.library_type == 'STATIC' }}
-S ${{ github.workspace }}
- name: Build (Windows)
if: matrix.os == 'windows-latest'
shell: msys2 {0}
Expand All @@ -88,7 +88,7 @@ jobs:

- name: Build (Ubuntu)
if: matrix.os == 'ubuntu-latest'
run: cmake --build $BUILD_OUTPUT_DIR --config ${{ matrix.build_type }}
run: VERBOSE=1 cmake --build $BUILD_OUTPUT_DIR --config ${{ matrix.build_type }}

- name: Test (Ubuntu)
working-directory: ${{ env.BUILD_OUTPUT_DIR }}
Expand Down
15 changes: 13 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@
cmake_minimum_required (VERSION 2.8.12)

option(BUILD_SHARED_LIBS "Build shared library." OFF)
# Add an option to control whether to build static or shared library
option(FMEM_STATIC "Build fmem as a static library." ON)

# Conditionally set the library type based on the FMEM_STATIC option
if(FMEM_STATIC)
set(FMEM_LIBRARY_TYPE STATIC)
else()
set(FMEM_LIBRARY_TYPE SHARED)
endif()

if (POLICY CMP0048)
# cmake warns if fmem is included from a parent directory whose CMakeLists.txt
Expand All @@ -24,7 +33,9 @@ if(MSVC AND NOT BUILD_SHARED_LIBS)
$<$<CONFIG:Release>:/MT>
)
endif(MSVC AND NOT BUILD_SHARED_LIBS)

if(BUILD_SHARED_LIBS)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
endif(BUILD_SHARED_LIBS)
include (CheckSymbolExists)
include (CheckCSourceCompiles)
include (GNUInstallDirs)
Expand Down Expand Up @@ -91,7 +102,7 @@ else ()
message (FATAL_ERROR "No memory stream implementation found")
endif ()

add_library (fmem ${SOURCES})
add_library(fmem ${FMEM_LIBRARY_TYPE} ${SOURCES})
add_library (fmem::fmem ALIAS fmem)
target_include_directories (fmem PUBLIC "${PROJECT_BINARY_DIR}/gen")

Expand Down
69 changes: 42 additions & 27 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,45 @@ else()
set(LIB_EXT ".a")
endif()

# Append additional libraries to CRITERION_LIBRARIES if they exist
if(EXISTS "${CMAKE_BINARY_DIR}/Criterion/subprojects/nanomsg/libnanomsg${LIB_EXT}")
list(APPEND CRITERION_LIBRARIES "${CMAKE_BINARY_DIR}/Criterion/subprojects/nanomsg/libnanomsg${LIB_EXT}")
endif()
function(append_library LIB_PATH LIB_NAME)
if(EXISTS "${CMAKE_BINARY_DIR}/Criterion/subprojects/${LIB_PATH}/lib${LIB_NAME}${LIB_EXT}")
list(APPEND CRITERION_LIBRARIES "${CMAKE_BINARY_DIR}/Criterion/subprojects/${LIB_PATH}/lib${LIB_NAME}${LIB_EXT}")
else()
execute_process(
COMMAND find "${CMAKE_BINARY_DIR}/Criterion" -name "lib${LIB_NAME}${LIB_EXT}"
OUTPUT_VARIABLE FOUND_LIB
OUTPUT_STRIP_TRAILING_WHITESPACE
)
if(FOUND_LIB)
message(STATUS "Found lib${LIB_NAME}${LIB_EXT} at: ${FOUND_LIB}")
list(APPEND CRITERION_LIBRARIES "${FOUND_LIB}")
else()
message(WARNING "lib${LIB_NAME}${LIB_EXT} not found in Criterion subprojects.")
endif()
endif()
set(CRITERION_LIBRARIES "${CRITERION_LIBRARIES}" PARENT_SCOPE)
endfunction()

if(EXISTS "${CMAKE_BINARY_DIR}/Criterion/subprojects/nanopb/libprotobuf_nanopb_static${LIB_EXT}")
list(APPEND CRITERION_LIBRARIES "${CMAKE_BINARY_DIR}/Criterion/subprojects/nanopb/libprotobuf_nanopb_static${LIB_EXT}")
endif()
# Usage examples
append_library("nanomsg" "nanomsg")
append_library("nanopb" "protobuf_nanopb_static")
append_library("boxfort/src" "boxfort")
append_library("libgit2" "git2")

if(EXISTS "${CMAKE_BINARY_DIR}/Criterion/subprojects/boxfort/src/libboxfort${LIB_EXT}")
list(APPEND CRITERION_LIBRARIES "${CMAKE_BINARY_DIR}/Criterion/subprojects/boxfort/src/libboxfort${LIB_EXT}")
endif()
function(copy_dll DLL_FULL_PATH DLL_NAME)
if(WIN32)
# Construct the full path to the DLL
set(DLL_PATH "${CMAKE_BINARY_DIR}/${DLL_FULL_PATH}/${DLL_NAME}.dll")

if(EXISTS "${CMAKE_BINARY_DIR}/Criterion/subprojects/libgit2/libgit2${LIB_EXT}")
list(APPEND CRITERION_LIBRARIES "${CMAKE_BINARY_DIR}/Criterion/subprojects/libgit2/libgit2${LIB_EXT}")
else()
# Execute the find program to locate libgit2.a don't write libgit2${LIB_EXT} because FIND: Parameter format not correct
execute_process(
COMMAND find "${CMAKE_BINARY_DIR}/Criterion" -name libgit2${LIB_EXT}
OUTPUT_VARIABLE FOUND_LIBGIT2
OUTPUT_STRIP_TRAILING_WHITESPACE
)

# If found, append to CRITERION_LIBRARIES
if(FOUND_LIBGIT2)
message(STATUS "Found libgit2.a at: ${FOUND_LIBGIT2}")
list(APPEND CRITERION_LIBRARIES "${FOUND_LIBGIT2}")
else()
message(WARNING "libgit2.a not found in Criterion subprojects.")
# Add a custom command to copy the DLL if it exists
add_custom_command(TARGET unit_tests POST_BUILD
COMMAND ${CMAKE_COMMAND} -E cmake_echo_color --green --bold "Attempting to copy ${DLL_NAME}.dll"
COMMAND bash -c "if [[ -f '${DLL_PATH}' ]]; then ${CMAKE_COMMAND} -E copy_if_different '${DLL_PATH}' '$<TARGET_FILE_DIR:unit_tests>'; fi"
)
endif()
endif()
endfunction()




include_directories(SYSTEM "${CRITERION_INCLUDE_DIRS}")
Expand All @@ -49,4 +57,11 @@ message(STATUS "Linking unit_tests with libraries: fmem, ${CRITERION_LIBRARIES}"

target_link_libraries (unit_tests fmem "${CRITERION_LIBRARIES}")

copy_dll("Criterion/subprojects/nanomsg/nanomsg/" "nanomsg")
copy_dll("Criterion/subprojects/nanopb/nanopb/" "protobuf_nanopb_static")
copy_dll("Criterion/subprojects/boxfort/src/" "boxfort")
copy_dll("Criterion/subprojects/libgit2/" "libgit2")
copy_dll("" "libfmem") # For libfmem.dll in the build directory


add_test (unit_tests unit_tests)

0 comments on commit 30e06da

Please sign in to comment.