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