CustomCommands.cmake 6.23 KB
Newer Older
1 2 3
include(TestCXXAcceptsFlag)


4 5 6
# add a compiler flag only if it is accepted
macro(add_cxx_compiler_flag _flag)
  string(REPLACE "-" "_" _flag_var ${_flag})
7
  check_cxx_compiler_flag("${_flag}" CXX_COMPILER_${_flag_var}_OK)
8

9 10 11 12
  if(CXX_COMPILER_${_flag_var}_OK)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${_flag}")
  endif()
endmacro()
13

14

15 16
macro(add_c_compiler_flag _flag)
  string(REPLACE "-" "_" _flag_var ${_flag})
17
  check_c_compiler_flag("${_flag}" C_COMPILER_${_flag_var}_OK)
18 19 20 21 22 23
  if(C_COMPILER_${_flag_var}_OK)
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${_flag}")
  endif()
endmacro()


24 25
##########################################################################################
#
26 27 28 29 30 31 32
# Find a library and store it in _macro; if not a message error is issued.
    
# Example: morefem_find_library(LIB_OPEN_MPI "mpi" ${OPEN_MPI_LIB_DIR} "Openmpi")    
# _macro Name in which the library is accessible within CMake.
# _libnames Name or names under which the libraries might be found in the paths.
# _dir Directory into which the library should be found. When this macro is called, it is usually with a path set in Paths.cmake.
# _name Name of the library, for the error message. 
33 34
#
##########################################################################################
35 36 37 38 39 40 41
function(morefem_find_library _macro _libnames _dir _name)
    
    find_library(${_macro} ${_libnames} PATHS ${_dir} NO_DEFAULT_PATH)

    if (NOT ${_macro})
         message(FATAL_ERROR "${_name} library not found in path ${_dir}")
    endif()
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
endfunction()    


# ##########################################################################################
#
# Set the alias related to a module depending on the value of BUILD_MOREFEM_UNIQUE_LIBRARY:
# - If True, the alias is set to morefem, the unique library into which all the code is built.
# - If False, a new (empty at the moment) library is built and the alias points to this library.
# For instance morefem_library_module(MOREFEM_UTILITIES morefem_utilities) will create morefem_utilities and set MOREFEM_UTILITIES to point to this library.
# \param[out] _alias Alias to the library, e.g. MOREFEM_UTILITIES.
# \param[in] module Module considered; if BUILD_MOREFEM_UNIQUE_LIBRARY is False the module library will use that name.
# If True it is not used.
    #
# ##########################################################################################
function(morefem_library_module _alias _module)
    if (BUILD_MOREFEM_UNIQUE_LIBRARY)
58
        set(${_alias} MoReFEM PARENT_SCOPE)
59 60 61 62 63 64 65
    else()
        add_library(${_module} ${LIBRARY_TYPE} "")
        set(${_alias} ${_module} PARENT_SCOPE)
    endif()
endfunction()


66 67
# ##########################################################################################
# A wrapper over install() command which performs the following additional tasks:
68
# - If the target is an executable, call set_target_properties() to properly define RPATH for the target. This is 
69 70 71
# required at least for shared libraries on macOS.
# - Install the targets in the MoReFEM installation directories.  
# ##########################################################################################
72 73
include(GenerateExportHeader)

74 75 76 77 78
function(morefem_install)
    foreach(target ${ARGN})
        
        get_target_property(target_type ${target} TYPE)
        if (target_type STREQUAL "EXECUTABLE")
79 80 81 82
            if (APPLE)
                # Additional step to make shared library work on macOS; see https://gist.github.com/robertmaynard/5750737
                set_target_properties(${target} PROPERTIES INSTALL_RPATH "@loader_path/../lib")
            endif()
83 84
        elseif()
            generate_export_header(${target})
85
        endif ()
86
        
87
        install(TARGETS ${target}
88
                EXPORT "${target}TARGETS"
89 90
                RUNTIME DESTINATION ${MOREFEM_INSTALL_DIR_EXE}
                LIBRARY DESTINATION ${MOREFEM_INSTALL_DIR_LIB}
91 92 93 94
                ARCHIVE DESTINATION ${MOREFEM_INSTALL_DIR_LIB} 
                INCLUDES DESTINATION ${MOREFEM_INSTALL_DIR_INCL}) 
                
        
95 96
    endforeach()
endfunction()
97 98 99



100 101 102 103 104 105 106 107 108 109 110 111 112
# ##########################################################################################
#
# Set the alias related to a module depending on the value of BUILD_MOREFEM_UNIQUE_LIBRARY:
# - If True, the alias is set to morefem, the unique library into which all the code is built.
# - If False, a new (empty at the moment) library is built and the alias points to this library.
# For instance morefem_library_module(MOREFEM_UTILITIES morefem_utilities) will create morefem_utilities and set MOREFEM_UTILITIES to point to this library.
# \param[out] _alias Alias to the library, e.g. MOREFEM_UTILITIES.
# \param[in] module Module considered; if BUILD_MOREFEM_UNIQUE_LIBRARY is False the module library will use that name.
# If True it is not used.
    #
# ##########################################################################################
function(morefem_library_module _alias _module)
    if (BUILD_MOREFEM_UNIQUE_LIBRARY)
113
        set(${_alias} MoReFEM PARENT_SCOPE)
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
    else()
        add_library(${_module} ${LIBRARY_TYPE} "")
        set(${_alias} ${_module} PARENT_SCOPE)
    endif()
endfunction()


# ##########################################################################################
# There seems to be a way to get all the source files related to a given target, but what I 
# want when preparing installation part is just the list of header files, which extension is
# in my case .h, .hpp or .hxx.
# This function fulfills this need.
# \param[in] _target The target for which header files are sought.
# \param[out] _list The list of header files found.
# ##########################################################################################
129
function(extract_header_files _target _list _include_dir)
130 131 132 133
    get_property(sources TARGET ${_target} PROPERTY SOURCES)
    
    list(APPEND ret "")
    
134 135

    
136 137 138 139 140 141
    foreach(source in ${sources})
        string(REGEX MATCH .h$ h ${source})
        string(REGEX MATCH .hpp$ hpp ${source})
        string(REGEX MATCH .hxx$ hxx ${source})  

        if(h OR hpp OR hxx)
142 143 144
            
            string(REPLACE ${_include_dir} "\${MOREFEM_INSTALL_DIR_INCL}" shorter_path ${source})
            list(APPEND ret ${shorter_path})
145 146 147 148 149 150 151 152 153 154
        endif()
        
    endforeach()
    
    set(${_list} ${ret} PARENT_SCOPE)
    
endfunction()