"Fossies" - the Fresh Open Source Software Archive

Member "fimex-1.4.1/cmake/FimexUtils.cmake" (30 Oct 2019, 8712 Bytes) of package /linux/privat/fimex-1.4.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) CMake source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 # Fimex, cmake/FimexUtils.cmake
    2 #
    3 # Copyright (C) 2018-2019 met.no
    4 #
    5 # Contact information:
    6 # Norwegian Meteorological Institute
    7 # Box 43 Blindern
    8 # 0313 OSLO
    9 # NORWAY
   10 # email: diana@met.no
   11 #
   12 # Project Info:  https://wiki.met.no/fimex/start
   13 #
   14 # This library is free software; you can redistribute it and/or modify it
   15 # under the terms of the GNU Lesser General Public License as published by
   16 # the Free Software Foundation; either version 2.1 of the License, or
   17 # (at your option) any later version.
   18 #
   19 # This library is distributed in the hope that it will be useful, but
   20 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
   21 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
   22 # License for more details.
   23 #
   24 # You should have received a copy of the GNU Lesser General Public
   25 # License along with this library; if not, write to the Free Software
   26 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
   27 # USA.
   28 
   29 
   30 MACRO(FIMEX_CMAKE_SETUP)
   31   IF(CMAKE_VERSION VERSION_LESS "3.1")
   32     IF(CMAKE_C_COMPILER_ID STREQUAL "GNU")
   33       SET(CMAKE_C_FLAGS "--std=c99 ${CMAKE_C_FLAGS}")
   34     ELSEIF (CMAKE_C_COMPILER_ID STREQUAL "Intel")
   35       SET(CMAKE_C_FLAGS "-std=c99 ${CMAKE_C_FLAGS}")
   36     ELSE()
   37       MESSAGE(WARNING "Do not know how to set C99 for compiler '${CMAKE_C_COMPILER_ID}'. Please set the CFLAGS environment variable before running cmake.")
   38     ENDIF()
   39   ELSE()
   40     SET(CMAKE_C_STANDARD 99)
   41   ENDIF()
   42 
   43   INCLUDE(CMakePackageConfigHelpers)
   44   INCLUDE(GNUInstallDirs)
   45   INCLUDE(FindPkgConfig)
   46 
   47   SET(CMAKE_CXX_STANDARD 11)
   48   SET(CMAKE_CXX_STANDARD_REQUIRED ON)
   49   IF ((CMAKE_VERSION VERSION_LESS 3.6) AND (CMAKE_CXX_COMPILER_ID STREQUAL "Intel"))
   50     SET(CMAKE_CXX_FLAGS "-std=c++11 ${CMAKE_CXX_FLAGS}")
   51   ENDIF()
   52 
   53   # see https://gitlab.kitware.com/cmake/community/wikis/doc/cmake/RPATH-handling
   54   SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
   55   LIST(FIND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}" INSTALL_LIBDIR_IS_SYSTEM_DIR)
   56   IF("${INSTALL_LIBDIR_IS_SYSTEM_DIR}" STREQUAL "-1")
   57     LIST(APPEND CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}")
   58   ENDIF()
   59 ENDMACRO()
   60 
   61 #########################################################################
   62 
   63 FUNCTION(FIMEX_GEN_PKGCONFIG pc_in pc_out pc_deps pc_libs_ pc_libdirs_ pc_includedirs_)
   64   STRING(REPLACE ";" ", " pc_deps "${pc_deps}")
   65   STRING(REPLACE ">=" " >= " pc_deps "${pc_deps}")
   66 
   67   FOREACH(X ${pc_libs_})
   68     SET(pc_libs "${pc_libs} -l${X}")
   69   ENDFOREACH()
   70 
   71   FOREACH(X ${pc_libdirs_})
   72     IF((${X} STREQUAL "/usr/lib") OR (${X} STREQUAL "/usr/lib/${CMAKE_INSTALL_LIBDIR}"))
   73       # skip
   74     ELSE()
   75       SET(pc_libdirs "${pc_libdirs} -L${X}")
   76     ENDIF()
   77   ENDFOREACH()
   78 
   79   FOREACH(X ${pc_includedirs_})
   80     IF(${X} STREQUAL "/usr/include")
   81       # skip
   82     ELSE()
   83       SET(pc_includedirs "${pc_includedirs} -I${X}")
   84     ENDIF()
   85   ENDFOREACH()
   86 
   87   SET(pc_prefix "${CMAKE_INSTALL_PREFIX}")
   88   SET(pc_libdir "\${prefix}/${CMAKE_INSTALL_LIBDIR}")
   89   SET(pc_includedir "\${prefix}/${CMAKE_INSTALL_INCLUDEDIR}")
   90 
   91   CONFIGURE_FILE(${pc_in} ${pc_out} @ONLY)
   92   INSTALL(FILES ${CMAKE_BINARY_DIR}/${pc_out} DESTINATION "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}/pkgconfig")
   93 ENDFUNCTION()
   94 
   95 ########################################################################
   96 
   97 FUNCTION(FIMEX_VERSION_DEFINES pack header_file)
   98   FILE(STRINGS "${header_file}" version_definitions REGEX "^#define.*_VERSION_(MAJOR|MINOR|PATCH|STATUS) +")
   99   FOREACH(v_def ${version_definitions})
  100     STRING(REGEX REPLACE "^#define.*_VERSION_(MAJOR|MINOR|PATCH|STATUS) +([0-9A-Fx]+) *$" \\1 v_type   "${v_def}")
  101     STRING(REGEX REPLACE "^#define.*_VERSION_(MAJOR|MINOR|PATCH|STATUS) +([0-9A-Fx]+) *$" \\2 v_number "${v_def}")
  102     SET(version_${v_type} "${v_number}")
  103   ENDFOREACH()
  104 
  105   SET(${pack}_VERSION_MAJOR "${version_MAJOR}" PARENT_SCOPE)
  106   SET(${pack}_VERSION_MINOR "${version_MINOR}" PARENT_SCOPE)
  107   SET(${pack}_VERSION_PATCH "${version_PATCH}" PARENT_SCOPE)
  108 
  109 
  110   # cmake does not understand hex numbers
  111   STRING(REGEX REPLACE "0x([ABCF])[0-9]" \\1 v_status_type   "${version_STATUS}")
  112   STRING(REGEX REPLACE "0x[ABCF]([0-9])" \\1 v_status_number "${version_STATUS}")
  113   IF(${v_status_type} STREQUAL "A")
  114     MATH(EXPR v_status_dec 10*16)
  115     SET(v_status_string "~alpha${v_status_number}")
  116   ELSEIF(${v_status_type} STREQUAL "B")
  117     MATH(EXPR v_status_dec 11*16)
  118     SET(v_status_string "~beta${v_status_number}")
  119   ELSEIF(${v_status_type} STREQUAL "C")
  120     MATH(EXPR v_status_dec 12*16)
  121     SET(v_status_string "~rc${v_status_number}")
  122   ELSEIF(${v_status_type} STREQUAL "F")
  123     MATH(EXPR v_status_dec 15*16)
  124     SET(v_status_string "")
  125   ENDIF()
  126   MATH(EXPR v_status_dec ${v_status_dec}+${v_status_number})
  127 
  128   SET(${pack}_VERSION_STATUS_HEX "${version_STATUS}"  PARENT_SCOPE)
  129   SET(${pack}_VERSION_STATUS_DEC "${v_status_dec}"    PARENT_SCOPE)
  130   SET(${pack}_VERSION_STATUS_STR "${v_status_string}" PARENT_SCOPE)
  131 
  132 
  133   SET(${pack}_VERSION "${version_MAJOR}.${version_MINOR}" PARENT_SCOPE)
  134   SET(${pack}_VERSION_FULL "${version_MAJOR}.${version_MINOR}.${version_PATCH}${v_status_string}" PARENT_SCOPE)
  135 ENDFUNCTION()
  136 
  137 ########################################################################
  138 
  139 FUNCTION(FIMEX_HEADERS headers sources source_suffix header_suffix)
  140   FOREACH(src ${${sources}})
  141     STRING(REPLACE ${source_suffix} ${header_suffix} hdr ${src})
  142     LIST(APPEND headers ${hdr})
  143   ENDFOREACH()
  144 ENDFUNCTION()
  145 
  146 
  147 FUNCTION(FIMEX_ADD_LIBRARY name sources libs includes options)
  148   IF(BUILD_SHARED_LIBS)
  149     SET(shared_lib lib${name})
  150     ADD_LIBRARY(${shared_lib} SHARED ${sources})
  151     TARGET_LINK_LIBRARIES(${shared_lib} PRIVATE ${libs})
  152     TARGET_INCLUDE_DIRECTORIES(${shared_lib}
  153       PUBLIC
  154       $<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/include>
  155       $<INSTALL_INTERFACE:${FIMEX_INSTALL_INCLUDEDIR}>
  156       PRIVATE
  157       $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>
  158       $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
  159       ${includes}
  160       )
  161     IF(options)
  162       TARGET_COMPILE_OPTIONS(${shared_lib} PUBLIC ${options})
  163     ENDIF()
  164     SET_TARGET_PROPERTIES(${shared_lib} PROPERTIES
  165       VERSION ${lib_version}
  166       SOVERSION ${lib_soversion}
  167       OUTPUT_NAME "${name}${MINUS_FIMEX_VERSION}"
  168       )
  169     INSTALL(TARGETS ${shared_lib}
  170       EXPORT ${name}
  171       LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
  172       )
  173   ENDIF()
  174 
  175   IF((NOT (BUILD_SHARED_LIBS)) OR (BUILD_SHARED_LIBS MATCHES "[Bb][Oo][Tt][Hh]"))
  176     SET(static_lib lib${name}-static)
  177     SET(lib${name}_STATICLIBS ${libs})
  178     ADD_LIBRARY(${static_lib} STATIC ${sources})
  179     TARGET_LINK_LIBRARIES(${static_lib} PRIVATE ${libs})
  180     TARGET_INCLUDE_DIRECTORIES(${static_lib}
  181       PUBLIC
  182       $<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/include>
  183       $<INSTALL_INTERFACE:${FIMEX_INSTALL_INCLUDEDIR}>
  184       PRIVATE
  185       $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>
  186       $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
  187       ${includes}
  188       )
  189     SET_TARGET_PROPERTIES(${static_lib} PROPERTIES
  190       OUTPUT_NAME "${name}${MINUS_FIMEX_VERSION}"
  191       )
  192     INSTALL(TARGETS ${static_lib}
  193       # disabled: fimex needs libfelt, find_package(fimex) fails unless preceded by find_package(felt)
  194       # EXPORT ${name}
  195       ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
  196       )
  197   ENDIF()
  198 ENDFUNCTION()
  199 
  200 
  201 INCLUDE(CheckFunctionExists)
  202 
  203 FUNCTION(CHECK_NETCDF_HAS_HDF5 found)
  204   IF (netcdf_PC)
  205     # we want only netcdf
  206     PKG_CHECK_MODULES(PC_NETCDF QUIET REQUIRED ${netcdf_PC})
  207     SET(CMAKE_REQUIRED_FLAGS ${PC_NETCDF_LDFLAGS})
  208     SET(CMAKE_REQUIRED_DEFINITIONS ${PC_NETCDF_CFLAGS_OTHER})
  209     SET(CMAKE_REQUIRED_INCLUDES ${PC_NETCDF_INCLUDE_DIRS})
  210     SET(CMAKE_REQUIRED_LIBRARIES ${PC_NETCDF_LIBRARIES})
  211   ELSE ()
  212     SET(CMAKE_REQUIRED_INCLUDES ${netcdf_INC_DIR})
  213     SET(CMAKE_REQUIRED_LIBRARIES ${netcdf_LIB})
  214   ENDIF ()
  215   CHECK_FUNCTION_EXISTS("nc_def_var_deflate" ${found})
  216 ENDFUNCTION()
  217 
  218 
  219 FUNCTION(FIMEX_FIND_PACKAGE name
  220     pkg_pc
  221     pkg_libname
  222     pkg_hdr
  223     )
  224 
  225   UNSET(p_pc_FOUND CACHE)
  226 
  227   IF(pkg_pc)
  228     PKG_CHECK_MODULES(p_pc QUIET "${pkg_pc}")
  229   ENDIF()
  230   IF(p_pc_FOUND)
  231     MESSAGE(STATUS "Found ${name}: pkg-config '${pkg_pc}'")
  232     SET(${name}_PC ${pkg_pc} PARENT_SCOPE)
  233   ELSE()
  234     FIND_PATH(${name}_INC_DIR
  235       ${pkg_hdr}
  236       HINTS "${${name}_INCLUDE_DIR}" "${${name}_DIR}/include"
  237     )
  238     FIND_LIBRARY(${name}_LIB
  239       NAMES ${pkg_libname}
  240       HINTS "${${name}_LIB_DIR}" "${${name}_DIR}/lib"
  241     )
  242     IF((${name}_INC_DIR) AND (${name}_LIB))
  243       MESSAGE(STATUS "Found ${name}: include: '${${name}_INC_DIR}/${pkg_hdr}'  library: '${${name}_LIB}'")
  244     ELSE()
  245       MESSAGE(FATAL_ERROR "Required ${name} include/library not found")
  246     ENDIF()
  247   ENDIF()
  248 ENDFUNCTION()