CapnProtoTargets.cmake (9328B)
1 # This CMake script adds imported targets for each shared library and executable distributed by 2 # Cap'n Proto's autotools build. 3 # 4 # This file IS NOT USED by the CMake build! The CMake build generates its own version of this script 5 # from its set of exported targets. I used such a generated script as a reference when writing this 6 # one. 7 # 8 # The set of library targets provided by this script is automatically generated from the list of .pc 9 # files maintained in configure.ac. The set of executable targets is hard-coded in this file. 10 # 11 # You can request that this script print debugging information by invoking cmake with: 12 # 13 # -DCapnProto_DEBUG=ON 14 # 15 # TODO(someday): Distinguish between debug and release builds. I.e., set IMPORTED_LOCATION_RELEASE 16 # rather than IMPORTED_LOCATION, etc., if this installation was configured as a release build. But 17 # how do we tell? grep for -g in CXXFLAGS? 18 19 if(CMAKE_VERSION VERSION_LESS 3.1) 20 message(FATAL_ERROR "CMake >= 3.1 required") 21 endif() 22 23 set(forwarded_config_flags) 24 if(CapnProto_FIND_QUIETLY) 25 list(APPEND forwarded_config_flags QUIET) 26 endif() 27 if(CapnProto_FIND_REQUIRED) 28 list(APPEND forwarded_config_flags REQUIRED) 29 endif() 30 # If the consuming project called find_package(CapnProto) with the QUIET or REQUIRED flags, forward 31 # them to calls to find_package(PkgConfig) and pkg_check_modules(). Note that find_dependency() 32 # would do this for us in the former case, but there is no such forwarding wrapper for 33 # pkg_check_modules(). 34 35 find_package(PkgConfig ${forwarded_config_flags}) 36 if(NOT ${PkgConfig_FOUND}) 37 # If we're here, the REQUIRED flag must not have been passed, else we would have had a fatal 38 # error. Nevertheless, a diagnostic for this case is probably nice. 39 if(NOT CapnProto_FIND_QUIETLY) 40 message(WARNING "pkg-config cannot be found") 41 endif() 42 set(CapnProto_FOUND OFF) 43 return() 44 endif() 45 46 function(_capnp_import_pkg_config_target target) 47 # Add an imported library target named CapnProto::${target}, using the output of various 48 # invocations of `pkg-config ${target}`. The generated imported library target tries to mimic the 49 # behavior of a real CMake-generated imported target as closely as possible. 50 # 51 # Usage: _capnp_import_pkg_config_target(target <all Cap'n Proto targets>) 52 53 set(all_targets ${ARGN}) 54 55 pkg_check_modules(${target} ${forwarded_config_flags} ${target}) 56 57 if(NOT ${${target}_FOUND}) 58 if(NOT CapnProto_FIND_QUIETLY) 59 message(WARNING "CapnProtoConfig.cmake was configured to search for ${target}.pc, but pkg-config cannot find it. Ignoring this target.") 60 endif() 61 return() 62 endif() 63 64 if(CapnProto_DEBUG) 65 # Dump the information pkg-config discovered. 66 foreach(var VERSION LIBRARY_DIRS LIBRARIES LDFLAGS_OTHER INCLUDE_DIRS CFLAGS_OTHER) 67 message(STATUS "${target}_${var} = ${${target}_${var}}") 68 endforeach() 69 endif() 70 71 if(NOT ${${target}_VERSION} VERSION_EQUAL ${CapnProto_VERSION}) 72 if(NOT CapnProto_FIND_QUIETLY) 73 message(WARNING "CapnProtoConfig.cmake was configured to search for version ${CapnProto_VERSION}, but ${target} version ${${target}_VERSION} was found. Ignoring this target.") 74 endif() 75 return() 76 endif() 77 78 # Make an educated guess as to what the target's .so and .a filenames must be. 79 set(target_name_shared 80 ${CMAKE_SHARED_LIBRARY_PREFIX}${target}-${CapnProto_VERSION}${CMAKE_SHARED_LIBRARY_SUFFIX}) 81 set(target_name_static 82 ${CMAKE_STATIC_LIBRARY_PREFIX}${target}${CMAKE_STATIC_LIBRARY_SUFFIX}) 83 84 # Find the actual target's file. find_library() sets a cache variable, so I made the variable name 85 # unique-ish. 86 find_library(CapnProto_${target}_IMPORTED_LOCATION 87 NAMES ${target_name_shared} ${target_name_static} # prefer libfoo-version.so over libfoo.a 88 PATHS ${${target}_LIBRARY_DIRS} 89 NO_DEFAULT_PATH 90 ) 91 # If the installed version of Cap'n Proto is in a system location, pkg-config will not have filled 92 # in ${target}_LIBRARY_DIRS. To account for this, fall back to a regular search. 93 find_library(CapnProto_${target}_IMPORTED_LOCATION 94 NAMES ${target_name_shared} ${target_name_static} # prefer libfoo-version.so over libfoo.a 95 ) 96 97 if(NOT CapnProto_${target}_IMPORTED_LOCATION) 98 # Not an error if the library doesn't exist -- we may have found a lite mode installation. 99 if(CapnProto_DEBUG) 100 message(STATUS "${target} library does not exist") 101 endif() 102 return() 103 endif() 104 105 # Record some information about this target -- shared versus static, location and soname -- which 106 # we'll use to build our imported target later. 107 108 set(target_location ${CapnProto_${target}_IMPORTED_LOCATION}) 109 get_filename_component(target_name "${target_location}" NAME) 110 111 set(target_type STATIC) 112 set(imported_soname_property) 113 if(target_name STREQUAL ${target_name_shared}) 114 set(target_type SHARED) 115 set(imported_soname_property IMPORTED_SONAME ${target_name}) 116 endif() 117 118 # Each library dependency of the target is either the target itself, a sibling Cap'n Proto 119 # library, or a system library. We ignore the first case by removing this target from the 120 # dependencies. The remaining dependencies are either passed through or, if they are a sibling 121 # Cap'n Proto library, prefixed with `CapnProto::`. 122 set(dependencies ${${target}_LIBRARIES}) 123 list(REMOVE_ITEM dependencies ${target}) 124 set(target_interface_libs) 125 foreach(dependency ${dependencies}) 126 list(FIND all_targets ${dependency} target_index) 127 # TODO(cleanup): CMake >= 3.3 lets us write: `if(NOT ${dependency} IN_LIST all_targets)` 128 if(target_index EQUAL -1) 129 list(APPEND target_interface_libs ${dependency}) 130 else() 131 list(APPEND target_interface_libs CapnProto::${dependency}) 132 endif() 133 endforeach() 134 135 add_library(CapnProto::${target} ${target_type} IMPORTED) 136 set_target_properties(CapnProto::${target} PROPERTIES 137 ${imported_soname_property} 138 IMPORTED_LOCATION "${target_location}" 139 # TODO(cleanup): Use cxx_std_14 once it's safe to require cmake 3.8. 140 INTERFACE_COMPILE_FEATURES "cxx_generic_lambdas" 141 INTERFACE_COMPILE_OPTIONS "${${target}_CFLAGS_OTHER}" 142 INTERFACE_INCLUDE_DIRECTORIES "${${target}_INCLUDE_DIRS}" 143 144 # I'm dumping LDFLAGS_OTHER in with the libraries because there exists no 145 # INTERFACE_LINK_OPTIONS. See https://gitlab.kitware.com/cmake/cmake/issues/16543. 146 INTERFACE_LINK_LIBRARIES "${target_interface_libs};${${target}_LDFLAGS_OTHER}" 147 ) 148 149 if(CapnProto_DEBUG) 150 # Dump all the properties we generated for the imported target. 151 foreach(prop 152 IMPORTED_LOCATION 153 IMPORTED_SONAME 154 INTERFACE_COMPILE_FEATURES 155 INTERFACE_COMPILE_OPTIONS 156 INTERFACE_INCLUDE_DIRECTORIES 157 INTERFACE_LINK_LIBRARIES) 158 get_target_property(value CapnProto::${target} ${prop}) 159 message(STATUS "CapnProto::${target} ${prop} = ${value}") 160 endforeach() 161 endif() 162 endfunction() 163 164 # ======================================================================================== 165 # Imported library targets 166 167 # Build a list of targets to search for from the list of .pc files. 168 # I.e. [somewhere/foo.pc, somewhere/bar.pc] -> [foo, bar] 169 set(library_targets) 170 foreach(filename ${CAPNP_PKG_CONFIG_FILES}) 171 get_filename_component(target ${filename} NAME_WE) 172 list(APPEND library_targets ${target}) 173 endforeach() 174 175 # Try to add an imported library target CapnProto::foo for each foo.pc distributed with Cap'n Proto. 176 foreach(target ${library_targets}) 177 _capnp_import_pkg_config_target(${target} ${library_targets}) 178 endforeach() 179 180 # Handle lite-mode and no libraries found cases. It is tempting to set a CapnProto_LITE variable 181 # here, but the real CMake-generated implementation does no such thing -- we'd need to set it in 182 # CapnProtoConfig.cmake.in itself. 183 if(TARGET CapnProto::capnp AND TARGET CapnProto::kj) 184 if(NOT TARGET CapnProto::capnp-rpc) 185 if(NOT CapnProto_FIND_QUIETLY) 186 message(STATUS "Found an installation of Cap'n Proto lite. Executable and library targets beyond libkj and libcapnp will be unavailable.") 187 endif() 188 # Lite mode doesn't include the executables, so return here. 189 return() 190 endif() 191 else() 192 # If we didn't even find capnp or kj, then we didn't find anything usable. 193 set(CapnProto_FOUND OFF) 194 return() 195 endif() 196 197 # ======================================================================================== 198 # Imported executable targets 199 200 get_filename_component(_IMPORT_PREFIX "${CMAKE_CURRENT_LIST_FILE}" PATH) 201 get_filename_component(_IMPORT_PREFIX "${_IMPORT_PREFIX}" PATH) 202 get_filename_component(_IMPORT_PREFIX "${_IMPORT_PREFIX}" PATH) 203 get_filename_component(_IMPORT_PREFIX "${_IMPORT_PREFIX}" PATH) 204 205 # Add executable targets for the capnp compiler and plugins. This list must be kept manually in sync 206 # with the rest of the project. 207 208 add_executable(CapnProto::capnp_tool IMPORTED) 209 set_target_properties(CapnProto::capnp_tool PROPERTIES 210 IMPORTED_LOCATION "${_IMPORT_PREFIX}/bin/capnp${CMAKE_EXECUTABLE_SUFFIX}" 211 ) 212 213 add_executable(CapnProto::capnpc_cpp IMPORTED) 214 set_target_properties(CapnProto::capnpc_cpp PROPERTIES 215 IMPORTED_LOCATION "${_IMPORT_PREFIX}/bin/capnpc-c++${CMAKE_EXECUTABLE_SUFFIX}" 216 ) 217 218 add_executable(CapnProto::capnpc_capnp IMPORTED) 219 set_target_properties(CapnProto::capnpc_capnp PROPERTIES 220 IMPORTED_LOCATION "${_IMPORT_PREFIX}/bin/capnpc-capnp${CMAKE_EXECUTABLE_SUFFIX}" 221 )