From 66431d8ec144437fe9dbd49dbb8efca7467d70c7 Mon Sep 17 00:00:00 2001 From: Todor Prokopov Date: Mon, 21 Nov 2022 13:41:29 +0200 Subject: [PATCH] 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. --- CMakeLists.txt | 1 + README.linux | 1 + backend/CMakeLists.txt | 31 +++++++++++++++++++++++-------- backend/tests/CMakeLists.txt | 8 +++++--- backend_qt/CMakeLists.txt | 7 ++++++- cmake/zint_add_test.cmake | 10 ++++++---- frontend/CMakeLists.txt | 6 +++++- frontend_qt/CMakeLists.txt | 7 ++++++- 8 files changed, 53 insertions(+), 18 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 851d3f43..246dd646 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,6 +22,7 @@ option(ZINT_NOOPT "Set no optimize compile flags" OFF) option(ZINT_SANITIZE "Set sanitize compile/link flags" OFF) option(ZINT_TEST "Set test compile flag" 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_USE_PNG "Build with PNG support" ON) option(ZINT_USE_QT "Build with Qt support" ON) diff --git a/README.linux b/README.linux index b1f90ec0..a9c050b3 100644 --- a/README.linux +++ b/README.linux @@ -114,6 +114,7 @@ ZINT_COVERAGE:BOOL=OFF # Set code coverage flags ZINT_DEBUG:BOOL=OFF # Set debug compile flags ZINT_NOOPT:BOOL=OFF # Set no optimize compile 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_TEST:BOOL=OFF # Set test compile flag ZINT_USE_PNG:BOOL=ON # Build with PNG support diff --git a/backend/CMakeLists.txt b/backend/CMakeLists.txt index 854674dc..184e89a0 100644 --- a/backend/CMakeLists.txt +++ b/backend/CMakeLists.txt @@ -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_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) - target_sources(${PROJECT_NAME} PRIVATE libzint.rc) + if(WIN32) + target_sources(${PROJECT_NAME} PRIVATE libzint.rc) + endif() endif() if(ZINT_STATIC) 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() function(zint_target_link_libraries library) - target_link_libraries(zint ${library}) + if(ZINT_SHARED) + target_link_libraries(zint ${library}) + endif() if(ZINT_STATIC) target_link_libraries(zint-static ${library}) endif() endfunction() function(zint_target_compile_definitions scope definition) - target_compile_definitions(zint ${scope} ${definition}) + if(ZINT_SHARED) + target_compile_definitions(zint ${scope} ${definition}) + endif() if(ZINT_STATIC) target_compile_definitions(zint-static ${scope} ${definition}) endif() endfunction() -set_target_properties(zint PROPERTIES SOVERSION "${ZINT_VERSION_MAJOR}.${ZINT_VERSION_MINOR}" - VERSION ${ZINT_VERSION}) +if(ZINT_SHARED) + set_target_properties(zint PROPERTIES SOVERSION "${ZINT_VERSION_MAJOR}.${ZINT_VERSION_MINOR}" + VERSION ${ZINT_VERSION}) +endif() if(ZINT_USE_PNG) find_package(PNG) @@ -63,7 +76,9 @@ if(MSVC) target_compile_definitions(zint PRIVATE DLL_EXPORT) endif() -install(TARGETS zint ${INSTALL_TARGETS_DEFAULT_ARGS}) +if(ZINT_SHARED) + install(TARGETS zint ${INSTALL_TARGETS_DEFAULT_ARGS}) +endif() if(ZINT_STATIC) install(TARGETS zint-static ${INSTALL_TARGETS_DEFAULT_ARGS}) endif() diff --git a/backend/tests/CMakeLists.txt b/backend/tests/CMakeLists.txt index 27ffad92..a1819c32 100644 --- a/backend/tests/CMakeLists.txt +++ b/backend/tests/CMakeLists.txt @@ -21,9 +21,11 @@ endif() set(testcommon_SRCS testcommon.c testcommon.h) -add_library(testcommon ${testcommon_SRCS}) -target_link_libraries(testcommon zint) -target_include_directories(testcommon PUBLIC ${zint_backend_tests_SOURCE_DIR}) +if(ZINT_SHARED) + add_library(testcommon ${testcommon_SRCS}) + target_link_libraries(testcommon zint) + target_include_directories(testcommon PUBLIC ${zint_backend_tests_SOURCE_DIR}) +endif() if(ZINT_STATIC) add_library(testcommon-static ${testcommon_SRCS}) diff --git a/backend_qt/CMakeLists.txt b/backend_qt/CMakeLists.txt index 7053a399..1ac488f2 100644 --- a/backend_qt/CMakeLists.txt +++ b/backend_qt/CMakeLists.txt @@ -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_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) add_subdirectory(tests) diff --git a/cmake/zint_add_test.cmake b/cmake/zint_add_test.cmake index 1fb0329b..ca019614 100644 --- a/cmake/zint_add_test.cmake +++ b/cmake/zint_add_test.cmake @@ -5,10 +5,12 @@ macro(zint_add_test test_name test_command) set(ADDITIONAL_LIBS "${ARGN}" ${LIBRARY_FLAGS}) - add_executable(${test_command} ${test_command}.c) - target_link_libraries(${test_command} testcommon ${ADDITIONAL_LIBS}) - add_test(${test_name} ${test_command}) - set_tests_properties(${test_name} PROPERTIES ENVIRONMENT "CMAKE_CURRENT_SOURCE_DIR=${CMAKE_CURRENT_SOURCE_DIR}") + if(ZINT_SHARED) + add_executable(${test_command} ${test_command}.c) + target_link_libraries(${test_command} testcommon ${ADDITIONAL_LIBS}) + add_test(${test_name} ${test_command}) + set_tests_properties(${test_name} PROPERTIES ENVIRONMENT "CMAKE_CURRENT_SOURCE_DIR=${CMAKE_CURRENT_SOURCE_DIR}") + endif() if(ZINT_STATIC) add_executable(${test_command}-static ${test_command}.c) target_link_libraries(${test_command}-static testcommon-static ${ADDITIONAL_LIBS}) diff --git a/frontend/CMakeLists.txt b/frontend/CMakeLists.txt index 155fe6ba..b5b6d98f 100644 --- a/frontend/CMakeLists.txt +++ b/frontend/CMakeLists.txt @@ -16,7 +16,11 @@ target_include_directories(${PROJECT_NAME} PUBLIC "${CMAKE_SOURCE_DIR}/backend") 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) target_link_libraries(${PROJECT_NAME} zint_bundled_getopt) endif() diff --git a/frontend_qt/CMakeLists.txt b/frontend_qt/CMakeLists.txt index 48808d68..f456b2d6 100644 --- a/frontend_qt/CMakeLists.txt +++ b/frontend_qt/CMakeLists.txt @@ -48,7 +48,12 @@ endif() 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}::Core)