File: FindMySQL.cmake

package info (click to toggle)
tango 10.0.2%2Bdfsg1-1~bpo12%2B1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-backports
  • size: 89,480 kB
  • sloc: cpp: 201,245; sh: 1,645; python: 953; java: 800; perl: 467; javascript: 447; xml: 325; makefile: 269; sql: 72; ruby: 24
file content (230 lines) | stat: -rw-r--r-- 6,221 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#[=======================================================================[.rst:
FindMySQL
---------

Find MySQL library

Imported Targets
^^^^^^^^^^^^^^^^

This module provides the following imported targets, if found:

    ``MySQL::MySQL``
    The MySQL client library
    ``MySQL::exe``
    The MySQL client executable

Result Variables
^^^^^^^^^^^^^^^^

This will define the following variables:

    ``MySQL_FOUND``
    True if the system has the MySQL library.
    ``MySQL_exe_FOUND``
    True if the system has the MySQL library.
    ``MySQL_VERSION``
    The version of the MySQL library which was found, if known

Cache Variables
^^^^^^^^^^^^^^^

The following cache variables may also be set:

    ``MySQL_INCLUDE_DIR``
    The directory containing ``mysql.h``.
    ``MySQL_LIBRARY_RELEASE``
    The path to the release MySQL library.
    ``MySQL_LIBRARY_DEBUG``
    The path to the debug MySQL library.
    ``MySQL_LIBRARY``
    The path to the release MySQL library or the debug library
    if the release library is not found.
    ``MySQL_EXECUTABLE``
    The path to the mysql client program

#]=======================================================================]

if (NOT DEFINED PKG_CONFIG_FOUND)
    find_package(PkgConfig QUIET)
endif()

# Collect hints from pkg-config
if (PKG_CONFIG_FOUND)
    pkg_search_module(_MySQL_PKG mysql mariadb QUIET)
endif()

if (WIN32)
    set(_mysql_inc_paths
        "$ENV{ProgramFiles}/MySQL/*/include"
        "$ENV{ProgramFiles\(x86\)}/MySQL/*/include"
        "$ENV{ProgramFiles}/MariaDB/*/include"
        "$ENV{ProgramFiles\(x86\)}/MariaDB/*/include"
        "$ENV{ProgramFiles}/MariaDB/include"
        "$ENV{ProgramFiles\(x86\)}/MariaDB/include"
        )
endif()

find_path(MySQL_INCLUDE_DIR
    NAMES "mysql.h"
    PATHS
        ${_mysql_inc_paths}
        "${_MySQL_PKG_INCLUDE_DIRS}"
    PATH_SUFFIXES mysql mariadb
    )
unset(_mysql_inc_paths)

if (WIN32)
    set(_mysql_release_names libmariadb libmysql)
    set(_mysql_debug_names  libmariadbd libmysqld)
    set(_mysql_lib_paths
        "$ENV{ProgramFiles}/MySQL/*/lib"
        "$ENV{ProgramFiles\(x86\)}/MySQL/*/lib"
        "$ENV{ProgramFiles}/MariaDB/*/lib"
        "$ENV{ProgramFiles\(x86\)}/MariaDB/*/lib"
        "$ENV{ProgramFiles}/MariaDB/lib"
        "$ENV{ProgramFiles\(x86\)}/MariaDB/lib"
        )
else()
    set(_mysql_release_names  mariadb mysqlclient mysqlclient_r)
    set(_mysql_debug_names  mariadb mysqlclient mysqlclient_r)
endif()

find_library(MySQL_LIBRARY_RELEASE
    NAMES ${_mysql_release_names}
    NAMES_PER_DIR
    PATHS
        ""
        ${_mysql_lib_paths}
        ${_MySQL_PKG_LIBRARY_DIRS}
    )

find_library(MySQL_LIBRARY_DEBUG
    NAMES ${_mysql_debug_names}
    NAMES_PER_DIR
    PATHS
        ""
        ${_mysql_lib_paths}
        ${_MySQL_PKG_LIBRARY_DIRS}
    )

unset(_mysql_lib_paths)
unset(_mysql_release_names)
unset(_mysql_debug_names)

include(SelectLibraryConfigurations)
select_library_configurations(MySQL)

if (WIN32)
    set(_mysql_bin_paths
        "$ENV{ProgramFiles}/MySQL/*/bin"
        "$ENV{ProgramFiles\(x86\)}/MySQL/*/bin"
        "$ENV{ProgramFiles}/MariaDB/*/bin"
        "$ENV{ProgramFiles\(x86\)}/MariaDB/*/bin"
        "$ENV{ProgramFiles}/MariaDB/bin"
        "$ENV{ProgramFiles\(x86\)}/MariaDB/bin"
        )
endif()

find_program(MySQL_EXECUTABLE
    NAMES mariadb mysql
    NAMES_PER_DIR
    PATHS
        ${_mysql_bin_paths}
    )

if (MySQL_EXECUTABLE)
    set(MySQL_exe_FOUND TRUE)
endif()

if (NOT MySQL_INCLUDE_DIR OR
    (CMAKE_CROSSCOMPILING AND NOT CMAKE_CROSSCOMPILING_EMULATOR))
    set(MySQL_VERSION MySQL_VERSION-NOTFOUND)
endif()

if(NOT DEFINED MySQL_VERSION)
    file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/mysql_test_db_client.cpp [===[
#include <mysql.h>
#include <stdio.h>

int main(int argc, char** argv)
{
  printf("%s\n", mysql_get_client_info());

  return 0;
}
]===])

    try_run(
        DB_CLIENT_RUN
        DB_CLIENT_COMPILE
        ${CMAKE_CURRENT_BINARY_DIR}
        ${CMAKE_CURRENT_BINARY_DIR}/mysql_test_db_client.cpp
        COMPILE_DEFINITIONS "-I \"${MySQL_INCLUDE_DIR}\""
        LINK_LIBRARIES "${MySQL_LIBRARY}"
        COMPILE_OUTPUT_VARIABLE DB_CLIENT_COMPILE_OUTPUT
        RUN_OUTPUT_VARIABLE DB_CLIENT_VERSION)

    if (NOT DB_CLIENT_COMPILE)
      message(FATAL_ERROR "Failed to compile simple database client program:\n${DB_CLIENT_COMPILE_OUTPUT}")
    endif()

    if (NOT DB_CLIENT_RUN EQUAL 0)
      message(FATAL_ERROR "Failed to run simple database client program:\n${DB_CLIENT_VERSION}")
    endif()

    string(STRIP "${DB_CLIENT_VERSION}" DB_CLIENT_VERSION)
    set(MySQL_VERSION ${DB_CLIENT_VERSION} CACHE INTERNAL "database client library version")
endif()

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(MySQL
    FOUND_VAR MySQL_FOUND
    REQUIRED_VARS
        MySQL_LIBRARY
        MySQL_INCLUDE_DIR
    VERSION_VAR MySQL_VERSION
    HANDLE_COMPONENTS)

if (MySQL_FOUND)
    mark_as_advanced(MySQL_INCLUDE_DIR)
    mark_as_advanced(MySQL_LIBRARY_RELEASE)
    mark_as_advanced(MySQL_LIBRARY_DEBUG)
    mark_as_advanced(MySQL_LIBRARY)
endif()

if (MySQL_FOUND)
    if (NOT TARGET MySQL::MySQL)
        add_library(MySQL::MySQL UNKNOWN IMPORTED)
    endif()
    if (MySQL_LIBRARY_RELEASE)
        set_property(TARGET MySQL::MySQL APPEND PROPERTY
            IMPORTED_CONFIGURATIONS RELEASE
        )
        set_target_properties(MySQL::MySQL PROPERTIES
            IMPORTED_LOCATION_RELEASE "${MySQL_LIBRARY_RELEASE}"
        )
    endif()
    if (MySQL_LIBRARY_DEBUG)
        set_property(TARGET MySQL::MySQL APPEND PROPERTY
            IMPORTED_CONFIGURATIONS DEBUG
        )
        set_target_properties(MySQL::MySQL PROPERTIES
            IMPORTED_LOCATION_DEBUG "${MySQL_LIBRARY_DEBUG}"
        )
    endif()
    set_target_properties(MySQL::MySQL PROPERTIES
        INTERFACE_INCLUDE_DIRECTORIES "${MySQL_INCLUDE_DIR}"
        INTERFACE_DEFINITIONS "${_MySQL_PKG_CFLAGS_OTHER}"
        )
endif()

if (MySQL_exe_FOUND)
    mark_as_advanced(MySQL_EXECUTABLE)
endif()

if (MySQL_exe_FOUND AND NOT TARGET MySQL::exe)
    add_executable(MySQL::exe IMPORTED)
    set_property(TARGET MySQL::exe PROPERTY IMPORTED_LOCATION "${MySQL_EXECUTABLE}")
endif()