From 4e022f99ed550bb08b5aacd38ccb96cfc4ba4be9 Mon Sep 17 00:00:00 2001 From: Ancher Herve Date: Thu, 19 Mar 2020 11:26:48 +0100 Subject: [PATCH 1/2] Manage export for static library under Windows --- blitz/blitz.h | 30 +++++++++++++++--------------- src/CMakeLists.txt | 4 ++++ 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/blitz/blitz.h b/blitz/blitz.h index 3e714d29..a364c9d3 100644 --- a/blitz/blitz.h +++ b/blitz/blitz.h @@ -86,22 +86,22 @@ namespace blitz { using namespace std; #endif -#ifdef _WIN32 -#ifdef BZ_GENERATE_GLOBAL_INSTANCES -#define _bz_global __declspec(dllexport) -#define BZ_GLOBAL_INIT(X) =X +#if defined(_WIN32) && !defined(BZ_STATIC_LIB) +# ifdef BZ_GENERATE_GLOBAL_INSTANCES +# define _bz_global __declspec(dllexport) +# define BZ_GLOBAL_INIT(X) =X +# else +# define _bz_global __declspec(dllimport) +# define BZ_GLOBAL_INIT(X) +# endif #else -#define _bz_global __declspec(dllimport) -#define BZ_GLOBAL_INIT(X) -#endif -#else -#ifdef BZ_GENERATE_GLOBAL_INSTANCES - #define _bz_global - #define BZ_GLOBAL_INIT(X) =X -#else - #define _bz_global extern - #define BZ_GLOBAL_INIT(X) -#endif +# ifdef BZ_GENERATE_GLOBAL_INSTANCES +# define _bz_global +# define BZ_GLOBAL_INIT(X) =X +# else +# define _bz_global extern +# define BZ_GLOBAL_INIT(X) +# endif #endif /* Define types for indexing, depending on whether 64- or 32-bit diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 62704458..d8631244 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -38,6 +38,10 @@ set_target_properties(blitz-static PROPERTIES ${BLITZ-STATIC-DEFS} CLEAN_DIRECT_OUTPUT 1) +# Under Windows and MinGW, set BZ_STATIC_LIB flag +if (WIN32 OR MINGW) + set_property(TARGET blitz-static PROPERTY COMPILE_FLAGS -DBZ_STATIC_LIB) +endif() install(TARGETS ${BLITZ_LIBS} EXPORT ${PROJECT_NAME}Targets From 8916547a50d940ef81b9a758a54e1fa95727662c Mon Sep 17 00:00:00 2001 From: Ancher Herve Date: Thu, 19 Mar 2020 14:59:52 +0100 Subject: [PATCH 2/2] Manage static flag for pkg-config and CMake config files --- CMakeLists.txt | 4 +++- cmake/CreatePkgConfig.cmake | 20 +++++++++++++++++--- src/CMakeLists.txt | 4 ++-- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 621a39fe..d4ac2629 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -61,7 +61,8 @@ add_custom_target(lib DEPENDS blitz) # Configuration files. -CreatePkgConfigInfo(blitz) +CreatePkgConfigInfo(blitz "") +CreatePkgConfigInfo(blitz-static static) write_basic_package_version_file( cmake/${PROJECT_NAME}ConfigVersion.cmake @@ -77,6 +78,7 @@ configure_package_config_file( install(EXPORT ${PROJECT_NAME}Targets DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake) install(FILES ${CMAKE_BINARY_DIR}/blitz.pc + ${CMAKE_BINARY_DIR}/blitz-static.pc DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig) install(FILES ${CMAKE_CURRENT_BINARY_DIR}/cmake/${PROJECT_NAME}Config.cmake diff --git a/cmake/CreatePkgConfig.cmake b/cmake/CreatePkgConfig.cmake index 799f97c8..4afc41eb 100644 --- a/cmake/CreatePkgConfig.cmake +++ b/cmake/CreatePkgConfig.cmake @@ -1,8 +1,22 @@ -function (CreatePkgConfigInfo filename) +function (CreatePkgConfigInfo filename mode) set(template_filename ${filename}.pc.in) if (EXISTS ${CMAKE_SOURCE_DIR}/${template_filename}) set(PKGCONFIG_TEMPLATE ${CMAKE_SOURCE_DIR}/${template_filename}) else() + # Under MinGW, add stdc++ library + set(addlink) + if (MINGW) + set(addlink -lstdc++) + endif() + set(cflags) + set(static) + if ("${mode}" STREQUAL "static") + set(static -static) + # Under Windows and MinGW, add BZ_STATIC_LIB flag + if (WIN32 OR MINGW) + set(cflags -DBZ_STATIC_LIB) + endif() + endif() file(WRITE ${CMAKE_BINARY_DIR}/${template_filename} "# generated by cmake. remove this line to disable regeneration prefix=@pkgconfig_prefix@ @@ -15,8 +29,8 @@ Name: @pkgconfig_package_name@ Description: @pkgconfig_package_description@ Version: @CPACK_PACKAGE_VERSION_MAJOR@.@CPACK_PACKAGE_VERSION_MINOR@.@CPACK_PACKAGE_VERSION_PATCH@ Requires: @PACKAGE_REQUIRES@ -Libs: -L\${libdir} @PKGCONFIG_LIBS@ -Cflags: -I\${includedir} -I\${confdir} @CPPFLAGS@") +Libs: -L\${libdir} ${static} @PKGCONFIG_LIBS@ ${addlink} +Cflags: -I\${includedir} -I\${confdir} @CPPFLAGS@ ${cflags}") set(PKGCONFIG_TEMPLATE ${CMAKE_BINARY_DIR}/${template_filename}) endif() diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index d8631244..f8fd9cd2 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -38,9 +38,9 @@ set_target_properties(blitz-static PROPERTIES ${BLITZ-STATIC-DEFS} CLEAN_DIRECT_OUTPUT 1) -# Under Windows and MinGW, set BZ_STATIC_LIB flag +# Under Windows and MinGW, set BZ_STATIC_LIB flag during compilation if (WIN32 OR MINGW) - set_property(TARGET blitz-static PROPERTY COMPILE_FLAGS -DBZ_STATIC_LIB) + target_compile_options(blitz-static PUBLIC -DBZ_STATIC_LIB) endif() install(TARGETS ${BLITZ_LIBS}