Skip to content

Commit d4e5a08

Browse files
committed
Bug #31095993 (99093) MYSQL-CONNECTOR-CPP CMAKE FILES NOT WORK WELL WHEN AS A THIRD PARTY
Our libutils need to be updated to work correctly when used from within a cmake sub-project. Thanks to Lou Shuai for providing the initial patch.
1 parent 2635431 commit d4e5a08

File tree

1 file changed

+74
-63
lines changed

1 file changed

+74
-63
lines changed

cmake/libutils.cmake

Lines changed: 74 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -35,89 +35,100 @@
3535
# Includes all transitive dependencies of the libraries being merged.
3636
#
3737

38-
get_filename_component(LIBUTILS_SCRIPT_DIR ${CMAKE_CURRENT_LIST_FILE} PATH)
39-
set(LIBUTILS_SCRIPT_DIR "${LIBUTILS_SCRIPT_DIR}/libutils")
38+
# Include this script only once.
4039

41-
#
42-
# Locate required tools.
43-
#
40+
if(COMMAND libutils_setup)
41+
return()
42+
endif()
4443

45-
if(CMAKE_BUILD_TOOL MATCHES "MSBuild")
4644

47-
set(MSBUILD ON)
45+
macro(libutils_setup)
4846

49-
# Use lib.exe from the same location as other compiler tools
47+
get_filename_component(LIBUTILS_SCRIPT_DIR ${CMAKE_CURRENT_LIST_FILE} PATH)
48+
set(LIBUTILS_SCRIPT_DIR "${LIBUTILS_SCRIPT_DIR}/libutils")
49+
set(LIBUTILS_BIN_DIR "${CMAKE_CURRENT_BINARY_DIR}/libutils" CACHE INTERNAL "")
5050

51-
get_filename_component(path "${CMAKE_LINKER}" DIRECTORY)
52-
set(LIB_TOOL "${path}/lib.exe")
51+
#
52+
# Locate required tools.
53+
#
5354

54-
endif()
55+
if(CMAKE_BUILD_TOOL MATCHES "MSBuild")
5556

57+
set(MSBUILD ON)
5658

57-
if(APPLE)
59+
# Use lib.exe from the same location as other compiler tools
5860

59-
find_program(LIB_TOOL libtool)
61+
get_filename_component(path "${CMAKE_LINKER}" DIRECTORY)
62+
set(LIB_TOOL "${path}/lib.exe")
6063

61-
# We need install_name_tool to do rpath mangling (see below)
64+
endif()
6265

63-
find_program(INSTALL_NAME_TOOL install_name_tool)
6466

65-
# If available, otool is used to show runtime dependencies for libraries we
66-
# build
67+
if(APPLE)
6768

68-
find_program(OTOOL otool)
69+
find_program(LIB_TOOL libtool)
6970

70-
endif()
71+
# We need install_name_tool to do rpath mangling (see below)
7172

73+
find_program(INSTALL_NAME_TOOL install_name_tool)
7274

73-
#
74-
# Infrastructure for merging static libraries
75-
# ===========================================
76-
#
77-
# It is used to merge a static library with all other static libraries
78-
# on which it depends, so that, when using the merged library, one does
79-
# not have to worry about dependencies.
80-
#
81-
# The main logic for mering static libraries on different platforms is
82-
# in the merge_archives.cmake script. Calling merge_static_library() on
83-
# a library target arranges for this script to be called with all required
84-
# parameters every time the library is (re-)built.
85-
#
86-
# Extra effort is needed to get the list of all dependencies of the library.
87-
# These dependencies are computed by cmake, but there is no easy way to
88-
# get them out of cmake. We use the trick with custom language linker. Hovewer,
89-
# it does not work with MSBuild generator where we do other tricks. In either
90-
# case the idea is to define a phony target that depends on the static library
91-
# and capture link options that cmake uses to build this phony target.
92-
#
75+
# If available, otool is used to show runtime dependencies for libraries we
76+
# build
9377

94-
#
95-
# Create merge script from template, setting required internal variables in it.
96-
#
97-
# TODO: This will work only if this file is included in the same folder in which
98-
# static libraries are linked.
99-
#
78+
find_program(OTOOL otool)
10079

101-
configure_file(
102-
${LIBUTILS_SCRIPT_DIR}/merge_archives.cmake.in
103-
${CMAKE_CURRENT_BINARY_DIR}/merge_archives.cmake
104-
@ONLY
105-
)
80+
endif()
10681

107-
#
108-
# This small program saves in a file all command line options that
109-
# were passed to it. It is used to capture linker invocation options.
110-
#
11182

112-
if(NOT MSBUILD AND NOT TARGET save_linker_opts)
113-
add_executable(save_linker_opts ${LIBUTILS_SCRIPT_DIR}/save_linker_opts.cc)
114-
set_property(TARGET save_linker_opts PROPERTY
115-
OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}
83+
#
84+
# Infrastructure for merging static libraries
85+
# ===========================================
86+
#
87+
# It is used to merge a static library with all other static libraries
88+
# on which it depends, so that, when using the merged library, one does
89+
# not have to worry about dependencies.
90+
#
91+
# The main logic for mering static libraries on different platforms is
92+
# in the merge_archives.cmake script. Calling merge_static_library() on
93+
# a library target arranges for this script to be called with all required
94+
# parameters every time the library is (re-)built.
95+
#
96+
# Extra effort is needed to get the list of all dependencies of the library.
97+
# These dependencies are computed by cmake, but there is no easy way to
98+
# get them out of cmake. We use the trick with custom language linker. Hovewer,
99+
# it does not work with MSBuild generator where we do other tricks. In either
100+
# case the idea is to define a phony target that depends on the static library
101+
# and capture link options that cmake uses to build this phony target.
102+
#
103+
104+
#
105+
# Create merge script from template, setting required internal variables in it.
106+
#
107+
108+
configure_file(
109+
${LIBUTILS_SCRIPT_DIR}/merge_archives.cmake.in
110+
${LIBUTILS_BIN_DIR}/merge_archives.cmake
111+
@ONLY
116112
)
117-
endif()
113+
114+
#
115+
# This small program saves in a file all command line options that
116+
# were passed to it. It is used to capture linker invocation options.
117+
#
118+
119+
if(NOT MSBUILD AND NOT TARGET save_linker_opts)
120+
add_executable(save_linker_opts ${LIBUTILS_SCRIPT_DIR}/save_linker_opts.cc)
121+
set_property(TARGET save_linker_opts PROPERTY
122+
RUNTIME_OUTPUT_DIRECTORY ${LIBUTILS_BIN_DIR}
123+
)
124+
endif()
125+
126+
endmacro(libutils_setup)
127+
128+
libutils_setup()
118129

119130
#
120-
# Merge static libraries into single static or shared library.
131+
# Merge static libraries into a single static or shared library.
121132
#
122133
# Given a static library target, this function sets up an infrastructure
123134
# for merging this static libraray with its dependencies. It creates
@@ -185,7 +196,7 @@ function(merge_libraries TARGET)
185196
-DMSBUILD=${MSBUILD}
186197
-DINFO=${INFO}
187198
-DINFO_PREFIX=${INFO_PREFIX}
188-
-P merge_archives.cmake
199+
-P ${LIBUTILS_BIN_DIR}/merge_archives.cmake
189200
)
190201

191202
endif()
@@ -218,7 +229,7 @@ function(merge_libraries TARGET)
218229

219230
add_dependencies(${TARGET}-deps save_linker_opts)
220231
set_target_properties(${TARGET}-deps PROPERTIES
221-
RULE_LAUNCH_LINK "${CMAKE_BINARY_DIR}/save_linker_opts ${log_file}.STATIC "
232+
RULE_LAUNCH_LINK "${LIBUTILS_BIN_DIR}/save_linker_opts ${log_file}.STATIC "
222233
)
223234

224235
# Arrange for ${TARGET}-deps to be built before ${TARGET}
@@ -236,7 +247,7 @@ function(merge_libraries TARGET)
236247
#
237248

238249
set_target_properties(${TARGET} PROPERTIES
239-
RULE_LAUNCH_LINK "${CMAKE_BINARY_DIR}/save_linker_opts ${log_file}.SHARED "
250+
RULE_LAUNCH_LINK "${LIBUTILS_BIN_DIR}/save_linker_opts ${log_file}.SHARED "
240251
)
241252

242253
else(NOT MSBUILD)

0 commit comments

Comments
 (0)