| 1 |
# Distributed under the original FontForge BSD 3-clause license |
| 2 |
|
| 3 |
#[=======================================================================[.rst: |
| 4 |
TargetUtils |
| 5 |
----------- |
| 6 |
|
| 7 |
``fixup_link_options`` inspects link options and fixes link options, |
| 8 |
particularly those inferred from pkg-config. Specifically for MacOS, |
| 9 |
-framework are fixed. target_link_options deduplicates flags, |
| 10 |
so ``-framework A -framework B`` becomes ``-framework A B``. This |
| 11 |
is incorrect syntax. To fix this, the list must be altered to specify |
| 12 |
framework options as ``SHELL:-framework A`` ``SHELL:-framework B`` |
| 13 |
|
| 14 |
``alias_imported_target` copies an imported target of name ``src` into |
| 15 |
a new imported target of name ``dest``. This is used because CMake |
| 16 |
currently does not support aliasing imported targets. |
| 17 |
|
| 18 |
``make_object_interface`` creates an interface library from an |
| 19 |
object library of name ``objlib``. This interface includes the |
| 20 |
target objects from ``objlib``. Additional arguments passed into |
| 21 |
this function will be treated as libraries to link to this interface |
| 22 |
library. This function is for use on CMake versions less than 3.12, |
| 23 |
which do not support calling target_link_libraries directly on |
| 24 |
object libraries. |
| 25 |
|
| 26 |
``set_supported_compiler_flags`` checks to see if the provided list |
| 27 |
of flags are supported by the C/C++ compiler. Supported flags are stored |
| 28 |
into ``dst`` / ``cxxdst`` as a cache variable. |
| 29 |
|
| 30 |
``add_uninstall_target`` add an uninstall target |
| 31 |
|
| 32 |
#]=======================================================================] |
| 33 |
|
| 34 |
function(fixup_link_options dest) |
| 35 |
unset(_item_buffer) |
| 36 |
foreach(_item ${ARGN}) |
| 37 |
if (APPLE AND "${_item}" STREQUAL "-framework") |
| 38 |
set(_item_buffer "-framework") |
| 39 |
elseif(DEFINED _item_buffer) |
| 40 |
list(APPEND _items "SHELL:${_item_buffer} ${_item}") |
| 41 |
unset(_item_buffer) |
| 42 |
else() |
| 43 |
list(APPEND _items "${_item}") |
| 44 |
endif() |
| 45 |
endforeach() |
| 46 |
if(DEFINED _item_buffer) |
| 47 |
message(FATAL_ERROR "Could not fixup/parse ${ARGN} --> stray argument ${_item_buffer}") |
| 48 |
endif() |
| 49 |
set(${dest} ${_items} PARENT_SCOPE) |
| 50 |
endfunction() |
| 51 |
|
| 52 |
function(alias_imported_target dest src) |
| 53 |
if(TARGET ${src} AND NOT TARGET ${dest}) |
| 54 |
add_library(${dest} INTERFACE IMPORTED) |
| 55 |
foreach(prop INTERFACE_INCLUDE_DIRECTORIES INTERFACE_LINK_LIBRARIES INTERFACE_LINK_OPTIONS INTERFACE_COMPILE_OPTIONS) |
| 56 |
get_property(_prop_val TARGET ${src} PROPERTY ${prop}) |
| 57 |
if(APPLE AND prop STREQUAL INTERFACE_LINK_OPTIONS) |
| 58 |
fixup_link_options(_prop_val ${_prop_val}) |
| 59 |
endif() |
| 60 |
set_property(TARGET ${dest} PROPERTY ${prop} ${_prop_val}) |
| 61 |
endforeach() |
| 62 |
endif() |
| 63 |
endfunction() |
| 64 |
|
| 65 |
function(make_object_interface objlib) |
| 66 |
add_library(${objlib}_interface INTERFACE) |
| 67 |
target_sources(${objlib}_interface INTERFACE $<TARGET_OBJECTS:${objlib}>) |
| 68 |
target_link_libraries(${objlib}_interface |
| 69 |
INTERFACE |
| 70 |
${ARGN} |
| 71 |
) |
| 72 |
target_include_directories(${objlib} |
| 73 |
PUBLIC |
| 74 |
$<TARGET_PROPERTY:${objlib}_interface,INTERFACE_INCLUDE_DIRECTORIES> |
| 75 |
) |
| 76 |
endfunction() |
| 77 |
|
| 78 |
function(set_supported_compiler_flags dst cxxdst) |
| 79 |
if(NOT DEFINED "${dst}" AND NOT DEFINED "${cxxdst}") |
| 80 |
include(CheckCCompilerFlag) |
| 81 |
include(CheckCXXCompilerFlag) |
| 82 |
include(CMakePushCheckState) |
| 83 |
cmake_push_check_state(RESET) |
| 84 |
set(CMAKE_REQUIRED_QUIET TRUE) |
| 85 |
|
| 86 |
cmake_parse_arguments(_COMPILER_FLAGS "" "" "CFLAGS;CXXFLAGS;BOTH" ${ARGN}) |
| 87 |
list(APPEND _COMPILER_FLAGS_CFLAGS ${_COMPILER_FLAGS_BOTH}) |
| 88 |
list(APPEND _COMPILER_FLAGS_CXXFLAGS ${_COMPILER_FLAGS_BOTH}) |
| 89 |
|
| 90 |
macro(_do_check _lang _flaglist) |
| 91 |
foreach(_arg ${ARGN}) |
| 92 |
message(STATUS "Checking if ${_arg} is supported by the ${_lang} compiler...") |
| 93 |
if("${_lang}" STREQUAL C) |
| 94 |
check_c_compiler_flag("-Werror ${_arg}" _supported_flag) |
| 95 |
else() |
| 96 |
check_cxx_compiler_flag("-Werror ${_arg}" _supported_flag) |
| 97 |
endif() |
| 98 |
if(_supported_flag) |
| 99 |
message(STATUS " Flag is supported: ${_arg}") |
| 100 |
list(APPEND ${_flaglist} ${_arg}) |
| 101 |
else() |
| 102 |
message(STATUS " Flag is unsupported: ${_arg}") |
| 103 |
endif() |
| 104 |
unset(_supported_flag CACHE) |
| 105 |
unset(_supported_flag) |
| 106 |
endforeach() |
| 107 |
endmacro() |
| 108 |
|
| 109 |
_do_check(C _supported_cflags ${_COMPILER_FLAGS_CFLAGS}) |
| 110 |
_do_check(C++ _supported_cxxflags ${_COMPILER_FLAGS_CXXFLAGS}) |
| 111 |
|
| 112 |
set("${dst}" "${_supported_cflags}" CACHE STRING "Supported C compiler flags") |
| 113 |
set("${cxxdst}" "${_supported_cxxflags}" CACHE STRING "Supported C++ compiler flags") |
| 114 |
cmake_pop_check_state() |
| 115 |
endif() |
| 116 |
endfunction() |
| 117 |
|
| 118 |
# Based on https://gitlab.kitware.com/cmake/community/wikis/FAQ#can-i-do-make-uninstall-with-cmake |
| 119 |
function(add_uninstall_target) |
| 120 |
if(NOT TARGET uninstall) |
| 121 |
configure_file( |
| 122 |
"${PROJECT_SOURCE_DIR}/cmake/scripts/Uninstall.cmake.in" |
| 123 |
"${PROJECT_BINARY_DIR}/cmake_uninstall.cmake" |
| 124 |
IMMEDIATE @ONLY |
| 125 |
) |
| 126 |
add_custom_target(uninstall |
| 127 |
COMMAND "${CMAKE_COMMAND}" -P "${PROJECT_BINARY_DIR}/cmake_uninstall.cmake" |
| 128 |
VERBATIM USES_TERMINAL |
| 129 |
) |
| 130 |
endif() |
| 131 |
endfunction() |