Browse Source

Add macro to use iconv

experiment/meta_header
Martchus 6 years ago
parent
commit
323ad615cb
  1. 4
      CMakeLists.txt
  2. 115
      cmake/modules/3rdParty.cmake
  3. 24
      cmake/templates/Config.cmake.in

4
CMakeLists.txt

@ -106,6 +106,10 @@ set(META_VERSION_MAJOR 4)
set(META_VERSION_MINOR 0)
set(META_VERSION_PATCH 0)
# find required 3rd party libraries
include(3rdParty)
find_iconv(AUTO_LINKAGE REQUIRED)
# include modules to apply configuration
include(BasicConfig)
include(WindowsResources)

115
cmake/modules/3rdParty.cmake

@ -1,52 +1,52 @@
if(NOT DEFINED EXTERNAL_LIBRARIES_EXISTS)
set(EXTERNAL_LIBRARIES_EXISTS true)
macro(use_external_library_from_package NAME VERSION INCLUDE_VAR LIBRARY_VAR LINKAGE REQUIRED)
# need to set CMAKE_FIND_LIBRARY_SUFFIXES temporarily to be able to find static libs, save the current value to be able to restore
if(NOT DEFINED FIND_THIRD_PARTY_LIBRARIES_EXISTS)
set(FIND_THIRD_PARTY_LIBRARIES_EXISTS true)
macro(save_default_library_suffixes)
set(DEFAULT_CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES})
endmacro()
# handle arguments VERSION and REQUIRED
if("${VERSION}" STREQUAL "ANY_VERSION")
set(${NAME}_COMPATIBLE_VERSION "")
else()
set(${NAME}_COMPATIBLE_VERSION ${VERSION})
endif()
if("${REQUIRED}" STREQUAL "OPTIONAL")
set(${NAME}_REQUIRED "")
elseif("${REQUIRED}" STREQUAL "REQUIRED")
set(${NAME}_REQUIRED "REQUIRED")
macro(restore_default_library_suffixes)
set(CMAKE_FIND_LIBRARY_SUFFIXES ${DEFAULT_CMAKE_FIND_LIBRARY_SUFFIXES})
unset(DEFAULT_CMAKE_FIND_LIBRARY_SUFFIXES)
endmacro()
macro(configure_static_library_suffixes)
# allows to look for static libraries in particular
if(WIN32)
set(CMAKE_FIND_LIBRARY_SUFFIXES .a .lib)
else()
message(FATAL_ERROR "Invalid use of use_external_library; must specify either REQUIRED or OPTIONAL.")
set(CMAKE_FIND_LIBRARY_SUFFIXES .a)
endif()
endmacro()
# find dynamic library
macro(configure_dynamic_library_suffixes)
# allows to look for dynamic libraries in particular
if(WIN32)
set(CMAKE_FIND_LIBRARY_SUFFIXES .so)
set(CMAKE_FIND_LIBRARY_SUFFIXES .dll .dll.a)
else()
set(CMAKE_FIND_LIBRARY_SUFFIXES .dll.a .dll)
set(CMAKE_FIND_LIBRARY_SUFFIXES .so)
endif()
find_package(${NAME} ${${NAME}_COMPATIBLE_VERSION})
include_directories(${${INCLUDE_VAR}})
set(${NAME}_DYNAMIC_LIB ${${LIBRARY_VAR}})
set(${${LIBRARY_VAR}} "")
endmacro()
# find static library
if(WIN32)
set(CMAKE_FIND_LIBRARY_SUFFIXES .lib .a)
macro(link_against_library NAME LINKAGE REQUIRED)
# determine whether the library is required or optional
if("${REQUIRED}" STREQUAL "OPTIONAL")
set(${NAME}_REQUIRED "NO")
elseif("${REQUIRED}" STREQUAL "REQUIRED")
set(${NAME}_REQUIRED "REQUIRED")
else()
set(CMAKE_FIND_LIBRARY_SUFFIXES .a)
message(FATAL_ERROR "Invalid use of use_external_library; must specify either REQUIRED or OPTIONAL.")
endif()
find_package(${NAME} ${${NAME}_COMPATIBLE_VERSION})
set(${NAME}_STATIC_LIB ${${LIBRARY_VAR}})
# add library to list of libraries to link against when building dynamic libraries or applications
if(${NAME}_STATIC_LIB AND (("${LINKAGE}" STREQUAL "AUTO_LINKAGE" AND ((STATIC_LINKAGE AND "${META_PROJECT_TYPE}" STREQUAL "application") OR (STATIC_LIBRARY_LINKAGE AND ("${META_PROJECT_TYPE}" STREQUAL "" OR "${META_PROJECT_TYPE}" STREQUAL "library")))) OR ("${LINKAGE}" STREQUAL "STATIC")))
list(APPEND LIBRARIES ${${NAME}_STATIC_LIB})
message(STATUS "Linking ${META_PROJECT_NAME} statically against external library ${NAME}.")
message(STATUS "Linking ${META_PROJECT_NAME} statically against external library ${NAME} (${${NAME}_STATIC_LIB}).")
elseif(${NAME}_DYNAMIC_LIB AND ("${LINKAGE}" STREQUAL "AUTO_LINKAGE" OR ("${LINKAGE}" STREQUAL "SHARED")))
list(APPEND LIBRARIES ${${NAME}_DYNAMIC_LIB})
message(STATUS "Linking ${META_PROJECT_NAME} dynamically against external library ${NAME}.")
message(STATUS "Linking ${META_PROJECT_NAME} dynamically against external library ${NAME} (${${NAME}_DYNAMIC_LIB}).")
else()
if(${REQUIRED})
if(${NAME}_REQUIRED)
message(FATAL_ERROR "External library ${NAME} required by ${META_PROJECT_NAME} is not available for the specified linkage ${LINKAGE}.")
else()
message(WARNING "External library ${NAME} required by ${META_PROJECT_NAME} is not available for the specified linkage ${LINKAGE}.")
@ -54,10 +54,55 @@ if(NOT DEFINED EXTERNAL_LIBRARIES_EXISTS)
endif()
# add library to list of libraries to be provided as transitive dependencies when building static libraries
list(APPEND STATIC_LIBRARIES ${${LIBRARY_VAR}})
list(APPEND STATIC_LIBRARIES ${${NAME}_STATIC_LIB})
endmacro()
# restore altered CMAKE_FIND_LIBRARY_SUFFIXES
set(CMAKE_FIND_LIBRARY_SUFFIXES ${DEFAULT_CMAKE_FIND_LIBRARY_SUFFIXES})
set(DEFAULT_CMAKE_FIND_LIBRARY_SUFFIXES "")
macro(use_external_library_from_package NAME VERSION INCLUDE_VAR LIBRARY_VAR LINKAGE REQUIRED)
save_default_library_suffixes()
# handle specified VERSION
if("${VERSION}" STREQUAL "ANY_VERSION")
set(${NAME}_COMPATIBLE_VERSION "")
else()
set(${NAME}_COMPATIBLE_VERSION ${VERSION})
endif()
# find dynamic library
configure_dynamic_library_suffixes()
find_package(${NAME} ${${NAME}_COMPATIBLE_VERSION})
include_directories(${${INCLUDE_VAR}})
set(${NAME}_DYNAMIC_LIB ${${LIBRARY_VAR}})
unset(${${LIBRARY_VAR}})
# find static library
configure_static_library_suffixes()
find_package(${NAME} ${${NAME}_COMPATIBLE_VERSION})
set(${NAME}_STATIC_LIB ${${LIBRARY_VAR}})
link_against_library(${NAME} ${LINKAGE} ${REQUIRED})
restore_default_library_suffixes()
endmacro()
macro(find_iconv LINKAGE REQUIRED)
# check whether iconv exists in the standard library
include(CheckFunctionExists)
check_function_exists(iconv HAS_ICONV)
if(HAS_ICONV)
message(STATUS "Using iconv from the standard library for ${META_PROJECT_NAME}.")
else()
# find external iconv library
save_default_library_suffixes()
configure_dynamic_library_suffixes()
find_library(ICONV_DYNAMIC_LIB iconv)
configure_static_library_suffixes()
find_library(ICONV_STATIC_LIB iconv)
link_against_library(ICONV ${LINKAGE} ${REQUIRED})
restore_default_library_suffixes()
endif()
endmacro()
endif()

24
cmake/templates/Config.cmake.in

@ -1,10 +1,10 @@
@PACKAGE_INIT@
set(@META_PROJECT_VARNAME@_HAS_LIBS "@BUILD_SHARED_LIBS@")
set(@META_PROJECT_VARNAME@_LIBS "@META_PROJECT_NAME@")
set(@META_PROJECT_VARNAME@_HAS_STATIC_LIBS "@BUILD_STATIC_LIBS@")
set(@META_PROJECT_VARNAME@_STATIC_LIBS "@META_PROJECT_NAME@.a")
set(@META_PROJECT_VARNAME@_STATIC_LIBS_DEPENDS @META_LIB_DEPENDS@)
set(@META_PROJECT_VARNAME@_HAS_SHARED_LIB "@BUILD_SHARED_LIBS@")
set(@META_PROJECT_VARNAME@_SHARED_LIB "@META_PROJECT_NAME@")
set(@META_PROJECT_VARNAME@_HAS_STATIC_LIB "@BUILD_STATIC_LIBS@")
set(@META_PROJECT_VARNAME@_STATIC_LIB "@META_PROJECT_NAME@.a")
set(@META_PROJECT_VARNAME@_STATIC_LIB_DEPENDS @META_LIB_DEPENDS@)
set(@META_PROJECT_VARNAME@_INCLUDE_DIRS "@PACKAGE_HEADER_INSTALL_DESTINATION@")
set(@META_PROJECT_VARNAME@_BIN_DIR "@PACKAGE_BIN_INSTALL_DESTINATION@")
set(@META_PROJECT_VARNAME@_LIB_DIR "@PACKAGE_LIB_INSTALL_DESTINATION@")
@ -15,15 +15,17 @@ macro(use_@META_PROJECT_VARNAME@)
include_directories(BEFORE SYSTEM ${@META_PROJECT_VARNAME@_INCLUDE_DIRS})
link_directories(${@META_PROJECT_VARNAME@_LIB_DIR})
# add library to list of libraries to link against when building dynamic libraries or applications
if(@META_PROJECT_VARNAME@_HAS_STATIC_LIBS AND ((NOT ARGV0 AND ((STATIC_LINKAGE AND "${META_PROJECT_TYPE}" STREQUAL "application") OR (STATIC_LIBRARY_LINKAGE AND ("${META_PROJECT_TYPE}" STREQUAL "" OR "${META_PROJECT_TYPE}" STREQUAL "library")))) OR ("${ARGV0}" STREQUAL "STATIC")))
list(APPEND LIBRARIES ${@META_PROJECT_VARNAME@_STATIC_LIBS} ${@META_PROJECT_VARNAME@_STATIC_LIBS_DEPENDS})
elseif(@META_PROJECT_VARNAME@_HAS_LIBS AND (NOT ARGV0 OR ("${ARGV0}" STREQUAL "SHARED")))
list(APPEND LIBRARIES ${@META_PROJECT_VARNAME@_LIBS})
if(@META_PROJECT_VARNAME@_HAS_STATIC_LIB AND ((NOT ARGV0 AND ((STATIC_LINKAGE AND "${META_PROJECT_TYPE}" STREQUAL "application") OR (STATIC_LIBRARY_LINKAGE AND ("${META_PROJECT_TYPE}" STREQUAL "" OR "${META_PROJECT_TYPE}" STREQUAL "library")))) OR ("${ARGV0}" STREQUAL "STATIC")))
list(APPEND LIBRARIES ${@META_PROJECT_VARNAME@_STATIC_LIB} ${@META_PROJECT_VARNAME@_STATIC_LIB_DEPENDS})
message(STATUS "Linking ${META_PROJECT_NAME} statically against @META_PROJECT_NAME@ (${@META_PROJECT_VARNAME@_STATIC_LIB} ${@META_PROJECT_VARNAME@_STATIC_LIB_DEPENDS}).")
elseif(@META_PROJECT_VARNAME@_HAS_SHARED_LIB AND (NOT ARGV0 OR ("${ARGV0}" STREQUAL "SHARED")))
list(APPEND LIBRARIES ${@META_PROJECT_VARNAME@_SHARED_LIB})
message(STATUS "Linking ${META_PROJECT_NAME} dynamically against @META_PROJECT_NAME@ (${@META_PROJECT_VARNAME@_SHARED_LIB}).")
else()
message(ERROR "Specified linkage ${ARGV0} is not available.")
endif()
# add library to list of libraries to be provided as transitive dependencies when building static libraries
list(APPEND STATIC_LIBRARIES ${@META_PROJECT_VARNAME@_STATIC_LIBS} ${@META_PROJECT_VARNAME@_STATIC_LIBS_DEPENDS})
# add library (including dependencies) to list of libraries to be provided as transitive dependencies when building static libraries
list(APPEND STATIC_LIBRARIES ${@META_PROJECT_VARNAME@_STATIC_LIB} ${@META_PROJECT_VARNAME@_STATIC_LIB_DEPENDS})
# make CMake modules of the project available
list(APPEND CMAKE_MODULE_PATH ${@META_PROJECT_VARNAME@_MODULE_DIRS})
endmacro()

Loading…
Cancel
Save