]> git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/opentelemetry-cpp/tools/vcpkg/ports/vcpkg-cmake-config/vcpkg_cmake_config_fixup.cmake
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / jaegertracing / opentelemetry-cpp / tools / vcpkg / ports / vcpkg-cmake-config / vcpkg_cmake_config_fixup.cmake
1 #[===[.md:
2 # vcpkg_cmake_config_fixup
3
4 Merge release and debug CMake targets and configs to support multiconfig generators.
5
6 Additionally corrects common issues with targets, such as absolute paths and incorrectly placed binaries.
7
8 ```cmake
9 vcpkg_cmake_config_fixup(
10 [PACKAGE_NAME <name>]
11 [CONFIG_PATH <config-directory>]
12 [DO_NOT_DELETE_CONFIG_PATH_PARENT]
13 [NO_PREFIX_CORRECTION]
14 )
15 ```
16
17 For many ports, `vcpkg_cmake_config_fixup()` on its own should work,
18 as `PACKAGE_NAME` defaults to `${PORT}` and `CONFIG_PATH` defaults to `share/${PACKAGE_NAME}`.
19 For ports where the package name passed to `find_package` is distinct from the port name,
20 `PACKAGE_NAME` should be changed to be that name instead.
21 For ports where the directory of the `*config.cmake` files cannot be set,
22 use the `CONFIG_PATH` to change the directory where the files come from.
23
24 By default the parent directory of CONFIG_PATH is removed if it is named "cmake".
25 Passing the `DO_NOT_DELETE_PARENT_CONFIG_PATH` option disable such behavior,
26 as it is convenient for ports that install
27 more than one CMake package configuration file.
28
29 The `NO_PREFIX_CORRECTION` option disables the correction of `_IMPORT_PREFIX`
30 done by vcpkg due to moving the config files.
31 Currently the correction does not take into account how the files are moved,
32 and applies a rather simply correction which in some cases will yield the wrong results.
33
34 ## How it Works
35
36 1. Moves `/debug/<CONFIG_PATH>/*targets-debug.cmake` to `/share/${PACKAGE_NAME}`.
37 2. Removes `/debug/<CONFIG_PATH>/*config.cmake`.
38 3. Transform all references matching `/bin/*.exe` to `/tools/<port>/*.exe` on Windows.
39 4. Transform all references matching `/bin/*` to `/tools/<port>/*` on other platforms.
40 5. Fixes `${_IMPORT_PREFIX}` in auto generated targets.
41 6. Replace `${CURRENT_INSTALLED_DIR}` with `${_IMPORT_PREFIX}` in configs and targets.
42
43 ## Examples
44
45 * [concurrentqueue](https://github.com/Microsoft/vcpkg/blob/master/ports/concurrentqueue/portfile.cmake)
46 * [curl](https://github.com/Microsoft/vcpkg/blob/master/ports/curl/portfile.cmake)
47 * [nlohmann-json](https://github.com/Microsoft/vcpkg/blob/master/ports/nlohmann-json/portfile.cmake)
48 #]===]
49 if(Z_VCPKG_CMAKE_CONFIG_FIXUP_GUARD)
50 return()
51 endif()
52 set(Z_VCPKG_CMAKE_CONFIG_FIXUP_GUARD ON CACHE INTERNAL "guard variable")
53
54 function(vcpkg_cmake_config_fixup)
55 cmake_parse_arguments(PARSE_ARGV 0 "arg" "DO_NOT_DELETE_PARENT_CONFIG_PATH" "PACKAGE_NAME;CONFIG_PATH;NO_PREFIX_CORRECTION" "")
56
57 if(DEFINED arg_UNPARSED_ARGUMENTS)
58 message(FATAL_ERROR "vcpkg_cmake_config_fixup was passed extra arguments: ${arg_UNPARSED_ARGUMENTS}")
59 endif()
60 if(NOT arg_PACKAGE_NAME)
61 set(arg_PACKAGE_NAME "${PORT}")
62 endif()
63 if(NOT arg_CONFIG_PATH)
64 set(arg_CONFIG_PATH "share/${arg_PACKAGE_NAME}")
65 endif()
66 set(target_path "share/${arg_PACKAGE_NAME}")
67
68 string(REPLACE "." "\\." EXECUTABLE_SUFFIX "${VCPKG_TARGET_EXECUTABLE_SUFFIX}")
69
70 set(debug_share "${CURRENT_PACKAGES_DIR}/debug/${target_path}")
71 set(release_share "${CURRENT_PACKAGES_DIR}/${target_path}")
72
73 if(NOT arg_CONFIG_PATH STREQUAL "share/${arg_PACKAGE_NAME}")
74 if(arg_CONFIG_PATH STREQUAL "share")
75 set(arg_CONFIG_PATH z_vcpkg_share)
76 file(RENAME "${CURRENT_PACKAGES_DIR}/debug/share" "${CURRENT_PACKAGES_DIR}/debug/${arg_CONFIG_PATH}")
77 file(RENAME "${CURRENT_PACKAGES_DIR}/share" "${CURRENT_PACKAGES_DIR}/${arg_CONFIG_PATH}")
78 endif()
79
80 set(debug_config "${CURRENT_PACKAGES_DIR}/debug/${arg_CONFIG_PATH}")
81 set(release_config "${CURRENT_PACKAGES_DIR}/${arg_CONFIG_PATH}")
82 if(NOT DEFINED VCPKG_BUILD_TYPE OR VCPKG_BUILD_TYPE STREQUAL "debug")
83 if(NOT EXISTS "${debug_config}")
84 message(FATAL_ERROR "'${debug_config}' does not exist.")
85 endif()
86
87 # This roundabout handling enables CONFIG_PATH = share
88 file(MAKE_DIRECTORY "${debug_share}")
89 file(GLOB files "${debug_config}/*")
90 file(COPY ${files} DESTINATION "${debug_share}")
91 file(REMOVE_RECURSE "${debug_config}")
92 endif()
93
94 file(GLOB files "${release_config}/*")
95 file(COPY ${files} DESTINATION "${release_share}")
96 file(REMOVE_RECURSE "${release_config}")
97
98 if(NOT DEFINED VCPKG_BUILD_TYPE OR VCPKG_BUILD_TYPE STREQUAL "debug")
99 get_filename_component(debug_config_dir_name "${debug_config}" NAME)
100 string(TOLOWER "${debug_config_dir_name}" debug_config_dir_name)
101 if(debug_config_dir_name STREQUAL "cmake" AND NOT arg_DO_NOT_DELETE_PARENT_CONFIG_PATH)
102 file(REMOVE_RECURSE "${debug_config}")
103 else()
104 get_filename_component(debug_config_parent_dir "${debug_config}" DIRECTORY)
105 get_filename_component(debug_config_dir_name "${debug_config_parent_dir}" NAME)
106 string(TOLOWER "${debug_config_dir_name}" debug_config_dir_name)
107 if(debug_config_dir_name STREQUAL "cmake" AND NOT arg_DO_NOT_DELETE_PARENT_CONFIG_PATH)
108 file(REMOVE_RECURSE "${debug_config_parent_dir}")
109 endif()
110 endif()
111 endif()
112
113 get_filename_component(release_config_dir_name "${release_config}" NAME)
114 string(TOLOWER "${release_config_dir_name}" release_config_dir_name)
115 if(release_config_dir_name STREQUAL "cmake" AND NOT arg_DO_NOT_DELETE_PARENT_CONFIG_PATH)
116 file(REMOVE_RECURSE "${release_config}")
117 else()
118 get_filename_component(release_config_parent_dir "${release_config}" DIRECTORY)
119 get_filename_component(release_config_dir_name "${release_config_parent_dir}" NAME)
120 string(TOLOWER "${release_config_dir_name}" release_config_dir_name)
121 if(release_config_dir_name STREQUAL "cmake" AND NOT arg_DO_NOT_DELETE_PARENT_CONFIG_PATH)
122 file(REMOVE_RECURSE "${release_config_parent_dir}")
123 endif()
124 endif()
125 endif()
126
127 if(NOT DEFINED VCPKG_BUILD_TYPE OR VCPKG_BUILD_TYPE STREQUAL "debug")
128 if(NOT EXISTS "${debug_share}")
129 message(FATAL_ERROR "'${debug_share}' does not exist.")
130 endif()
131 endif()
132
133 file(GLOB_RECURSE unused_files
134 "${debug_share}/*[Tt]argets.cmake"
135 "${debug_share}/*[Cc]onfig.cmake"
136 "${debug_share}/*[Cc]onfigVersion.cmake"
137 "${debug_share}/*[Cc]onfig-version.cmake"
138 )
139 if(NOT unused_files STREQUAL "")
140 file(REMOVE "${unused_files}")
141 endif()
142
143 file(GLOB_RECURSE release_targets
144 "${release_share}/*-release.cmake"
145 )
146 foreach(release_target IN LISTS release_targets)
147 file(READ "${release_target}" contents)
148 string(REPLACE "${CURRENT_INSTALLED_DIR}" "\${_IMPORT_PREFIX}" contents "${contents}")
149 string(REGEX REPLACE "\\\${_IMPORT_PREFIX}/bin/([^ \"]+${EXECUTABLE_SUFFIX})" "\${_IMPORT_PREFIX}/tools/${PORT}/\\1" contents "${contents}")
150 file(WRITE "${release_target}" "${contents}")
151 endforeach()
152
153 if(NOT DEFINED VCPKG_BUILD_TYPE OR VCPKG_BUILD_TYPE STREQUAL "debug")
154 file(GLOB_RECURSE debug_targets
155 "${debug_share}/*-debug.cmake"
156 )
157 foreach(debug_target IN LISTS debug_targets)
158 file(RELATIVE_PATH debug_target_rel "${debug_share}" "${debug_target}")
159
160 file(READ "${debug_target}" contents)
161 string(REPLACE "${CURRENT_INSTALLED_DIR}" "\${_IMPORT_PREFIX}" contents "${contents}")
162 string(REGEX REPLACE "\\\${_IMPORT_PREFIX}/bin/([^ \";]+${EXECUTABLE_SUFFIX})" "\${_IMPORT_PREFIX}/tools/${PORT}/\\1" contents "${contents}")
163 string(REPLACE "\${_IMPORT_PREFIX}/lib" "\${_IMPORT_PREFIX}/debug/lib" contents "${contents}")
164 string(REPLACE "\${_IMPORT_PREFIX}/bin" "\${_IMPORT_PREFIX}/debug/bin" contents "${contents}")
165 file(WRITE "${release_share}/${debug_target_rel}" "${contents}")
166
167 file(REMOVE "${debug_target}")
168 endforeach()
169 endif()
170
171 #Fix ${_IMPORT_PREFIX} in cmake generated targets and configs;
172 #Since those can be renamed we have to check in every *.cmake
173 file(GLOB_RECURSE main_cmakes "${release_share}/*.cmake")
174
175 foreach(main_cmake IN LISTS main_cmakes)
176 file(READ "${main_cmake}" contents)
177 # Note: I think the following comment is no longer true, since we now require the path to be `share/blah`
178 # however, I don't know it for sure.
179 # - nimazzuc
180
181 #This correction is not correct for all cases. To make it correct for all cases it needs to consider
182 #original folder deepness to CURRENT_PACKAGES_DIR in comparison to the moved to folder deepness which
183 #is always at least (>=) 2, e.g. share/${PORT}. Currently the code assumes it is always 2 although
184 #this requirement is only true for the *Config.cmake. The targets are not required to be in the same
185 #folder as the *Config.cmake!
186 if(NOT arg_NO_PREFIX_CORRECTION)
187 string(REGEX REPLACE
188 [[get_filename_component\(_IMPORT_PREFIX "\${CMAKE_CURRENT_LIST_FILE}" PATH\)(
189 get_filename_component\(_IMPORT_PREFIX "\${_IMPORT_PREFIX}" PATH\))*]]
190 [[get_filename_component(_IMPORT_PREFIX "${CMAKE_CURRENT_LIST_FILE}" PATH)
191 get_filename_component(_IMPORT_PREFIX "${_IMPORT_PREFIX}" PATH)
192 get_filename_component(_IMPORT_PREFIX "${_IMPORT_PREFIX}" PATH)]]
193 contents "${contents}") # see #1044 for details why this replacement is necessary. See #4782 why it must be a regex.
194 string(REGEX REPLACE
195 [[get_filename_component\(PACKAGE_PREFIX_DIR "\${CMAKE_CURRENT_LIST_DIR}/\.\./(\.\./)*" ABSOLUTE\)]]
196 [[get_filename_component(PACKAGE_PREFIX_DIR "${CMAKE_CURRENT_LIST_DIR}/../../" ABSOLUTE)]]
197 contents "${contents}")
198 string(REGEX REPLACE
199 [[get_filename_component\(PACKAGE_PREFIX_DIR "\${CMAKE_CURRENT_LIST_DIR}/\.\.((\\|/)\.\.)*" ABSOLUTE\)]]
200 [[get_filename_component(PACKAGE_PREFIX_DIR "${CMAKE_CURRENT_LIST_DIR}/../../" ABSOLUTE)]]
201 contents "${contents}") # This is a meson-related workaround, see https://github.com/mesonbuild/meson/issues/6955
202 endif()
203
204 #Fix wrongly absolute paths to install dir with the correct dir using ${_IMPORT_PREFIX}
205 #This happens if vcpkg built libraries are directly linked to a target instead of using
206 #an imported target for it. We could add more logic here to identify defect target files.
207 #Since the replacement here in a multi config build always requires a generator expression
208 #in front of the absoulte path to ${CURRENT_INSTALLED_DIR}. So the match should always be at
209 #least >:${CURRENT_INSTALLED_DIR}.
210 #In general the following generator expressions should be there:
211 #\$<\$<CONFIG:DEBUG>:${CURRENT_INSTALLED_DIR}/debug/lib/somelib>
212 #and/or
213 #\$<\$<NOT:\$<CONFIG:DEBUG>>:${CURRENT_INSTALLED_DIR}/lib/somelib>
214 #with ${CURRENT_INSTALLED_DIR} being fully expanded
215 string(REPLACE "${CURRENT_INSTALLED_DIR}" [[${_IMPORT_PREFIX}]] contents "${contents}")
216
217 # Patch out any remaining absolute references
218 file(TO_CMAKE_PATH "${CURRENT_PACKAGES_DIR}" cmake_current_packages_dir)
219 string(REPLACE "${CMAKE_CURRENT_PACKAGES_DIR}" [[${_IMPORT_PREFIX}]] contents "${contents}")
220
221 file(WRITE "${main_cmake}" "${contents}")
222 endforeach()
223
224 # Remove /debug/<target_path>/ if it's empty.
225 file(GLOB_RECURSE remaining_files "${debug_share}/*")
226 if(NOT remaining_files STREQUAL "")
227 file(REMOVE_RECURSE "${debug_share}")
228 endif()
229
230 # Remove /debug/share/ if it's empty.
231 file(GLOB_RECURSE remaining_files "${CURRENT_PACKAGES_DIR}/debug/share/*")
232 if(NOT remaining_files STREQUAL "")
233 file(REMOVE_RECURSE "${CURRENT_PACKAGES_DIR}/debug/share")
234 endif()
235 endfunction()
236
237