Add ZINT_SHARED CMake option

Either shared or static (or both) libraries can be built now.
Executables (zint and zint-qt) are linked with the static library if the shared
library it not built.
This commit is contained in:
Todor Prokopov 2022-11-21 13:41:29 +02:00
parent 2f8681b21a
commit 66431d8ec1
8 changed files with 53 additions and 18 deletions

View File

@ -22,6 +22,7 @@ option(ZINT_NOOPT "Set no optimize compile flags" OFF)
option(ZINT_SANITIZE "Set sanitize compile/link flags" OFF) option(ZINT_SANITIZE "Set sanitize compile/link flags" OFF)
option(ZINT_TEST "Set test compile flag" OFF) option(ZINT_TEST "Set test compile flag" OFF)
option(ZINT_COVERAGE "Set code coverage flags" OFF) option(ZINT_COVERAGE "Set code coverage flags" OFF)
option(ZINT_SHARED "Build shared library" ON)
option(ZINT_STATIC "Build static library" OFF) option(ZINT_STATIC "Build static library" OFF)
option(ZINT_USE_PNG "Build with PNG support" ON) option(ZINT_USE_PNG "Build with PNG support" ON)
option(ZINT_USE_QT "Build with Qt support" ON) option(ZINT_USE_QT "Build with Qt support" ON)

View File

@ -114,6 +114,7 @@ ZINT_COVERAGE:BOOL=OFF # Set code coverage flags
ZINT_DEBUG:BOOL=OFF # Set debug compile flags ZINT_DEBUG:BOOL=OFF # Set debug compile flags
ZINT_NOOPT:BOOL=OFF # Set no optimize compile flags ZINT_NOOPT:BOOL=OFF # Set no optimize compile flags
ZINT_SANITIZE:BOOL=OFF # Set sanitize compile/link flags ZINT_SANITIZE:BOOL=OFF # Set sanitize compile/link flags
ZINT_SHARED:BOOL=ON # Build shared library
ZINT_STATIC:BOOL=OFF # Build static library ZINT_STATIC:BOOL=OFF # Build static library
ZINT_TEST:BOOL=OFF # Set test compile flag ZINT_TEST:BOOL=OFF # Set test compile flag
ZINT_USE_PNG:BOOL=ON # Build with PNG support ZINT_USE_PNG:BOOL=ON # Build with PNG support

View File

@ -13,32 +13,45 @@ set(zint_TWODIM_SRCS code16k.c codablock.c dmatrix.c pdf417.c qr.c maxicode.c co
set(zint_OUTPUT_SRCS vector.c ps.c svg.c emf.c bmp.c pcx.c gif.c png.c tif.c raster.c output.c) set(zint_OUTPUT_SRCS vector.c ps.c svg.c emf.c bmp.c pcx.c gif.c png.c tif.c raster.c output.c)
set(zint_SRCS ${zint_OUTPUT_SRCS} ${zint_COMMON_SRCS} ${zint_ONEDIM_SRCS} ${zint_POSTAL_SRCS} ${zint_TWODIM_SRCS}) set(zint_SRCS ${zint_OUTPUT_SRCS} ${zint_COMMON_SRCS} ${zint_ONEDIM_SRCS} ${zint_POSTAL_SRCS} ${zint_TWODIM_SRCS})
add_library(zint SHARED ${zint_SRCS}) if(ZINT_SHARED)
add_library(zint SHARED ${zint_SRCS})
if(WIN32) if(WIN32)
target_sources(${PROJECT_NAME} PRIVATE libzint.rc) target_sources(${PROJECT_NAME} PRIVATE libzint.rc)
endif()
endif() endif()
if(ZINT_STATIC) if(ZINT_STATIC)
add_library(zint-static STATIC ${zint_SRCS}) add_library(zint-static STATIC ${zint_SRCS})
if(WIN32)
set_target_properties(zint-static PROPERTIES OUTPUT_NAME zint-static)
else()
set_target_properties(zint-static PROPERTIES OUTPUT_NAME zint)
endif()
endif() endif()
function(zint_target_link_libraries library) function(zint_target_link_libraries library)
if(ZINT_SHARED)
target_link_libraries(zint ${library}) target_link_libraries(zint ${library})
endif()
if(ZINT_STATIC) if(ZINT_STATIC)
target_link_libraries(zint-static ${library}) target_link_libraries(zint-static ${library})
endif() endif()
endfunction() endfunction()
function(zint_target_compile_definitions scope definition) function(zint_target_compile_definitions scope definition)
if(ZINT_SHARED)
target_compile_definitions(zint ${scope} ${definition}) target_compile_definitions(zint ${scope} ${definition})
endif()
if(ZINT_STATIC) if(ZINT_STATIC)
target_compile_definitions(zint-static ${scope} ${definition}) target_compile_definitions(zint-static ${scope} ${definition})
endif() endif()
endfunction() endfunction()
set_target_properties(zint PROPERTIES SOVERSION "${ZINT_VERSION_MAJOR}.${ZINT_VERSION_MINOR}" if(ZINT_SHARED)
set_target_properties(zint PROPERTIES SOVERSION "${ZINT_VERSION_MAJOR}.${ZINT_VERSION_MINOR}"
VERSION ${ZINT_VERSION}) VERSION ${ZINT_VERSION})
endif()
if(ZINT_USE_PNG) if(ZINT_USE_PNG)
find_package(PNG) find_package(PNG)
@ -63,7 +76,9 @@ if(MSVC)
target_compile_definitions(zint PRIVATE DLL_EXPORT) target_compile_definitions(zint PRIVATE DLL_EXPORT)
endif() endif()
install(TARGETS zint ${INSTALL_TARGETS_DEFAULT_ARGS}) if(ZINT_SHARED)
install(TARGETS zint ${INSTALL_TARGETS_DEFAULT_ARGS})
endif()
if(ZINT_STATIC) if(ZINT_STATIC)
install(TARGETS zint-static ${INSTALL_TARGETS_DEFAULT_ARGS}) install(TARGETS zint-static ${INSTALL_TARGETS_DEFAULT_ARGS})
endif() endif()

View File

@ -21,9 +21,11 @@ endif()
set(testcommon_SRCS testcommon.c testcommon.h) set(testcommon_SRCS testcommon.c testcommon.h)
add_library(testcommon ${testcommon_SRCS}) if(ZINT_SHARED)
target_link_libraries(testcommon zint) add_library(testcommon ${testcommon_SRCS})
target_include_directories(testcommon PUBLIC ${zint_backend_tests_SOURCE_DIR}) target_link_libraries(testcommon zint)
target_include_directories(testcommon PUBLIC ${zint_backend_tests_SOURCE_DIR})
endif()
if(ZINT_STATIC) if(ZINT_STATIC)
add_library(testcommon-static ${testcommon_SRCS}) add_library(testcommon-static ${testcommon_SRCS})

View File

@ -19,7 +19,12 @@ set_target_properties(${PROJECT_NAME} PROPERTIES SOVERSION "${ZINT_VERSION_MAJO
target_include_directories(${PROJECT_NAME} PUBLIC "${CMAKE_SOURCE_DIR}/backend") target_include_directories(${PROJECT_NAME} PUBLIC "${CMAKE_SOURCE_DIR}/backend")
target_link_libraries(${PROJECT_NAME} zint Qt${QT_VERSION_MAJOR}::Widgets Qt${QT_VERSION_MAJOR}::Gui) if(ZINT_SHARED)
target_link_libraries(${PROJECT_NAME} zint)
else()
target_link_libraries(${PROJECT_NAME} zint-static)
endif()
target_link_libraries(${PROJECT_NAME} Qt${QT_VERSION_MAJOR}::Widgets Qt${QT_VERSION_MAJOR}::Gui)
if(ZINT_TEST) if(ZINT_TEST)
add_subdirectory(tests) add_subdirectory(tests)

View File

@ -5,10 +5,12 @@
macro(zint_add_test test_name test_command) macro(zint_add_test test_name test_command)
set(ADDITIONAL_LIBS "${ARGN}" ${LIBRARY_FLAGS}) set(ADDITIONAL_LIBS "${ARGN}" ${LIBRARY_FLAGS})
if(ZINT_SHARED)
add_executable(${test_command} ${test_command}.c) add_executable(${test_command} ${test_command}.c)
target_link_libraries(${test_command} testcommon ${ADDITIONAL_LIBS}) target_link_libraries(${test_command} testcommon ${ADDITIONAL_LIBS})
add_test(${test_name} ${test_command}) add_test(${test_name} ${test_command})
set_tests_properties(${test_name} PROPERTIES ENVIRONMENT "CMAKE_CURRENT_SOURCE_DIR=${CMAKE_CURRENT_SOURCE_DIR}") set_tests_properties(${test_name} PROPERTIES ENVIRONMENT "CMAKE_CURRENT_SOURCE_DIR=${CMAKE_CURRENT_SOURCE_DIR}")
endif()
if(ZINT_STATIC) if(ZINT_STATIC)
add_executable(${test_command}-static ${test_command}.c) add_executable(${test_command}-static ${test_command}.c)
target_link_libraries(${test_command}-static testcommon-static ${ADDITIONAL_LIBS}) target_link_libraries(${test_command}-static testcommon-static ${ADDITIONAL_LIBS})

View File

@ -16,7 +16,11 @@ target_include_directories(${PROJECT_NAME} PUBLIC "${CMAKE_SOURCE_DIR}/backend")
set_target_properties(${PROJECT_NAME} PROPERTIES OUTPUT_NAME "zint") set_target_properties(${PROJECT_NAME} PROPERTIES OUTPUT_NAME "zint")
target_link_libraries(${PROJECT_NAME} zint) if(ZINT_SHARED)
target_link_libraries(${PROJECT_NAME} zint)
else()
target_link_libraries(${PROJECT_NAME} zint-static)
endif()
if(NOT HAVE_GETOPT) if(NOT HAVE_GETOPT)
target_link_libraries(${PROJECT_NAME} zint_bundled_getopt) target_link_libraries(${PROJECT_NAME} zint_bundled_getopt)
endif() endif()

View File

@ -48,7 +48,12 @@ endif()
target_include_directories(${PROJECT_NAME} PUBLIC "${CMAKE_SOURCE_DIR}/backend" "${CMAKE_SOURCE_DIR}/backend_qt") target_include_directories(${PROJECT_NAME} PUBLIC "${CMAKE_SOURCE_DIR}/backend" "${CMAKE_SOURCE_DIR}/backend_qt")
target_link_libraries(${PROJECT_NAME} zint QZint if(ZINT_SHARED)
target_link_libraries(${PROJECT_NAME} zint)
else()
target_link_libraries(${PROJECT_NAME} zint-static)
endif()
target_link_libraries(${PROJECT_NAME} QZint
Qt${QT_VERSION_MAJOR}::UiTools Qt${QT_VERSION_MAJOR}::Gui Qt${QT_VERSION_MAJOR}::Svg Qt${QT_VERSION_MAJOR}::UiTools Qt${QT_VERSION_MAJOR}::Gui Qt${QT_VERSION_MAJOR}::Svg
Qt${QT_VERSION_MAJOR}::Core) Qt${QT_VERSION_MAJOR}::Core)