Skip to content

Commit 8f0b197

Browse files
committed
Fixing compilation with postgresql under MXE/MinGW.
1 parent 139c43c commit 8f0b197

2 files changed

Lines changed: 226 additions & 22 deletions

File tree

CMakeLists.txt

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ cmake_minimum_required(VERSION 2.8)
2424

2525
project(sqlplot-tools)
2626

27+
# custom cmake scripts
28+
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
29+
2730
# prohibit in-source builds
2831
if("${PROJECT_SOURCE_DIR}" STREQUAL "${PROJECT_BINARY_DIR}")
2932
message(SEND_ERROR "In-source builds are not allowed.")
@@ -35,6 +38,10 @@ if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
3538
set(CMAKE_BUILD_TYPE "Debug")
3639
endif()
3740

41+
# options to build with various database libraries
42+
option(WITH_POSTGRESQL "Build SqlPlotTools with PostgreSQL interface" ON)
43+
option(WITH_MYSQL "Build SqlPlotTools with MySQL interface" ON)
44+
3845
# option to update all tests with new output
3946
option(UPDATE_TESTS "Update test output with program output" OFF)
4047

@@ -56,33 +63,42 @@ set(SQL_LIBRARIES "")
5663

5764
# Find PostgreSQL client library
5865

59-
set(PostgreSQL_ADDITIONAL_VERSIONS "9.3" "9.2")
66+
if(WITH_POSTGRESQL)
67+
68+
set(PostgreSQL_ADDITIONAL_VERSIONS "9.3" "9.2")
6069

61-
find_package(PostgreSQL)
70+
find_package(PostgreSQL)
71+
72+
if(PostgreSQL_FOUND)
73+
set(SQL_LIBRARIES ${SQL_LIBRARIES} ${PostgreSQL_LIBRARIES})
74+
add_definitions(-DHAVE_POSTGRESQL=1)
75+
message(STATUS "Found PostgreSQL: ${PostgreSQL_LIBRARIES}")
76+
include_directories(${PostgreSQL_INCLUDE_DIRS})
77+
endif()
6278

63-
if(PostgreSQL_FOUND)
64-
set(SQL_LIBRARIES ${SQL_LIBRARIES} ${PostgreSQL_LIBRARIES})
65-
add_definitions(-DHAVE_POSTGRESQL=1)
66-
message(STATUS "Found PostgreSQL: ${PostgreSQL_LIBRARIES}")
67-
include_directories(${PostgreSQL_INCLUDE_DIRS})
6879
endif()
6980

7081
# Find MySQL client library
71-
find_path(MYSQL_INCLUDE_DIRS mysql.h
72-
PATHS /usr/include/mysql /usr/local/include/mysql)
73-
74-
find_library(MYSQL_LIBRARIES NAMES mysqlclient
75-
PATHS /usr/lib/mysql /usr/local/lib/mysql)
76-
77-
if(MYSQL_INCLUDE_DIRS AND MYSQL_LIBRARIES)
78-
set(MYSQL_FOUND TRUE)
79-
add_definitions(-DHAVE_MYSQL=1)
80-
message(STATUS "Found MySQL: ${MYSQL_LIBRARIES}")
81-
include_directories(${MYSQL_INCLUDE_DIRS})
82-
set(SQL_LIBRARIES ${SQL_LIBRARIES} ${MYSQL_LIBRARIES})
83-
else()
84-
set(MYSQL_FOUND FALSE)
85-
message(STATUS "Could NOT find MySQL library")
82+
83+
if(WITH_MYSQL)
84+
85+
find_path(MYSQL_INCLUDE_DIRS mysql.h
86+
PATHS /usr/include/mysql /usr/local/include/mysql)
87+
88+
find_library(MYSQL_LIBRARIES NAMES mysqlclient
89+
PATHS /usr/lib/mysql /usr/local/lib/mysql)
90+
91+
if(MYSQL_INCLUDE_DIRS AND MYSQL_LIBRARIES)
92+
set(MYSQL_FOUND TRUE)
93+
add_definitions(-DHAVE_MYSQL=1)
94+
message(STATUS "Found MySQL: ${MYSQL_LIBRARIES}")
95+
include_directories(${MYSQL_INCLUDE_DIRS})
96+
set(SQL_LIBRARIES ${SQL_LIBRARIES} ${MYSQL_LIBRARIES})
97+
else()
98+
set(MYSQL_FOUND FALSE)
99+
message(STATUS "Could NOT find MySQL library")
100+
endif()
101+
86102
endif()
87103

88104
# Find Sqlite3 library

cmake/FindPostgreSQL.cmake

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
# - Find the PostgreSQL installation.
2+
# In Windows, we make the assumption that, if the PostgreSQL files are installed, the default directory
3+
# will be C:\Program Files\PostgreSQL.
4+
#
5+
# This module defines
6+
# PostgreSQL_LIBRARIES - the PostgreSQL libraries needed for linking
7+
# PostgreSQL_INCLUDE_DIRS - the directories of the PostgreSQL headers
8+
# PostgreSQL_VERSION_STRING - the version of PostgreSQL found (since CMake 2.8.8)
9+
10+
#=============================================================================
11+
# Copyright 2004-2009 Kitware, Inc.
12+
#
13+
# Distributed under the OSI-approved BSD License (the "License");
14+
# see accompanying file Copyright.txt for details.
15+
#
16+
# This software is distributed WITHOUT ANY WARRANTY; without even the
17+
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18+
# See the License for more information.
19+
#=============================================================================
20+
# (To distribute this file outside of CMake, substitute the full
21+
# License text for the above reference.)
22+
23+
# ----------------------------------------------------------------------------
24+
# History:
25+
# This module is derived from the module originally found in the VTK source tree.
26+
#
27+
# ----------------------------------------------------------------------------
28+
# Note:
29+
# PostgreSQL_ADDITIONAL_VERSIONS is a variable that can be used to set the
30+
# version mumber of the implementation of PostgreSQL.
31+
# In Windows the default installation of PostgreSQL uses that as part of the path.
32+
# E.g C:\Program Files\PostgreSQL\8.4.
33+
# Currently, the following version numbers are known to this module:
34+
# "9.1" "9.0" "8.4" "8.3" "8.2" "8.1" "8.0"
35+
#
36+
# To use this variable just do something like this:
37+
# set(PostgreSQL_ADDITIONAL_VERSIONS "9.2" "8.4.4")
38+
# before calling find_package(PostgreSQL) in your CMakeLists.txt file.
39+
# This will mean that the versions you set here will be found first in the order
40+
# specified before the default ones are searched.
41+
#
42+
# ----------------------------------------------------------------------------
43+
# You may need to manually set:
44+
# PostgreSQL_INCLUDE_DIR - the path to where the PostgreSQL include files are.
45+
# PostgreSQL_LIBRARY_DIR - The path to where the PostgreSQL library files are.
46+
# If FindPostgreSQL.cmake cannot find the include files or the library files.
47+
#
48+
# ----------------------------------------------------------------------------
49+
# The following variables are set if PostgreSQL is found:
50+
# PostgreSQL_FOUND - Set to true when PostgreSQL is found.
51+
# PostgreSQL_INCLUDE_DIRS - Include directories for PostgreSQL
52+
# PostgreSQL_LIBRARY_DIRS - Link directories for PostgreSQL libraries
53+
# PostgreSQL_LIBRARIES - The PostgreSQL libraries.
54+
#
55+
# ----------------------------------------------------------------------------
56+
# If you have installed PostgreSQL in a non-standard location.
57+
# (Please note that in the following comments, it is assumed that <Your Path>
58+
# points to the root directory of the include directory of PostgreSQL.)
59+
# Then you have three options.
60+
# 1) After CMake runs, set PostgreSQL_INCLUDE_DIR to <Your Path>/include and
61+
# PostgreSQL_LIBRARY_DIR to wherever the library pq (or libpq in windows) is
62+
# 2) Use CMAKE_INCLUDE_PATH to set a path to <Your Path>/PostgreSQL<-version>. This will allow find_path()
63+
# to locate PostgreSQL_INCLUDE_DIR by utilizing the PATH_SUFFIXES option. e.g. In your CMakeLists.txt file
64+
# set(CMAKE_INCLUDE_PATH ${CMAKE_INCLUDE_PATH} "<Your Path>/include")
65+
# 3) Set an environment variable called ${PostgreSQL_ROOT} that points to the root of where you have
66+
# installed PostgreSQL, e.g. <Your Path>.
67+
#
68+
# ----------------------------------------------------------------------------
69+
70+
set(PostgreSQL_INCLUDE_PATH_DESCRIPTION "top-level directory containing the PostgreSQL include directories. E.g /usr/local/include/PostgreSQL/8.4 or C:/Program Files/PostgreSQL/8.4/include")
71+
set(PostgreSQL_INCLUDE_DIR_MESSAGE "Set the PostgreSQL_INCLUDE_DIR cmake cache entry to the ${PostgreSQL_INCLUDE_PATH_DESCRIPTION}")
72+
set(PostgreSQL_LIBRARY_PATH_DESCRIPTION "top-level directory containing the PostgreSQL libraries.")
73+
set(PostgreSQL_LIBRARY_DIR_MESSAGE "Set the PostgreSQL_LIBRARY_DIR cmake cache entry to the ${PostgreSQL_LIBRARY_PATH_DESCRIPTION}")
74+
set(PostgreSQL_ROOT_DIR_MESSAGE "Set the PostgreSQL_ROOT system variable to where PostgreSQL is found on the machine E.g C:/Program Files/PostgreSQL/8.4")
75+
76+
77+
set(PostgreSQL_KNOWN_VERSIONS ${PostgreSQL_ADDITIONAL_VERSIONS}
78+
"9.1" "9.0" "8.4" "8.3" "8.2" "8.1" "8.0")
79+
80+
# Define additional search paths for root directories.
81+
if ( WIN32 )
82+
foreach (suffix ${PostgreSQL_KNOWN_VERSIONS} )
83+
set(PostgreSQL_ADDITIONAL_SEARCH_PATHS ${PostgreSQL_ADDITIONAL_SEARCH_PATHS} "C:/Program Files/PostgreSQL/${suffix}" )
84+
endforeach()
85+
endif()
86+
set( PostgreSQL_ROOT_DIRECTORIES
87+
ENV PostgreSQL_ROOT
88+
${PostgreSQL_ROOT}
89+
${PostgreSQL_ADDITIONAL_SEARCH_PATHS}
90+
)
91+
92+
#
93+
# Look for an installation.
94+
#
95+
find_path(PostgreSQL_INCLUDE_DIR
96+
NAMES libpq-fe.h
97+
PATHS
98+
# Look in other places.
99+
${PostgreSQL_ROOT_DIRECTORIES}
100+
PATH_SUFFIXES
101+
pgsql
102+
postgresql
103+
include
104+
# Help the user find it if we cannot.
105+
DOC "The ${PostgreSQL_INCLUDE_DIR_MESSAGE}"
106+
)
107+
108+
# The PostgreSQL library.
109+
set (PostgreSQL_LIBRARY_TO_FIND pq)
110+
# Setting some more prefixes for the library
111+
set (PostgreSQL_LIB_PREFIX "")
112+
if ( FALSE AND WIN32 )
113+
set (PostgreSQL_LIB_PREFIX ${PostgreSQL_LIB_PREFIX} "lib")
114+
set ( PostgreSQL_LIBRARY_TO_FIND ${PostgreSQL_LIB_PREFIX}${PostgreSQL_LIBRARY_TO_FIND})
115+
endif()
116+
117+
find_library( PostgreSQL_LIBRARY
118+
NAMES ${PostgreSQL_LIBRARY_TO_FIND}
119+
PATHS
120+
${PostgreSQL_ROOT_DIRECTORIES}
121+
PATH_SUFFIXES
122+
lib
123+
)
124+
get_filename_component(PostgreSQL_LIBRARY_DIR ${PostgreSQL_LIBRARY} PATH)
125+
126+
if ( MINGW )
127+
128+
find_library(SSL_LIBRARY
129+
NAMES ssl
130+
PATHS ${PostgreSQL_ROOT_DIRECTORIES}
131+
PATH_SUFFIXES lib)
132+
set(PostgreSQL_LIBRARY ${PostgreSQL_LIBRARY} ${SSL_LIBRARY})
133+
134+
find_library(CRYPTO_LIBRARY
135+
NAMES crypto
136+
PATHS ${PostgreSQL_ROOT_DIRECTORIES}
137+
PATH_SUFFIXES lib)
138+
set(PostgreSQL_LIBRARY ${PostgreSQL_LIBRARY} ${CRYPTO_LIBRARY})
139+
140+
find_library(ZLIB_LIBRARY
141+
NAMES z
142+
PATHS ${PostgreSQL_ROOT_DIRECTORIES}
143+
PATH_SUFFIXESlib)
144+
set(PostgreSQL_LIBRARY ${PostgreSQL_LIBRARY} ${ZLIB_LIBRARY})
145+
146+
find_library(WS2_32_LIBRARY
147+
NAMES ws2_32
148+
PATHS ${PostgreSQL_ROOT_DIRECTORIES}
149+
PATH_SUFFIXES lib)
150+
set(PostgreSQL_LIBRARY ${PostgreSQL_LIBRARY} ${WS2_32_LIBRARY})
151+
152+
find_library(SECUR32_LIBRARY
153+
NAMES secur32
154+
PATHS ${PostgreSQL_ROOT_DIRECTORIES}
155+
PATH_SUFFIXES lib)
156+
set(PostgreSQL_LIBRARY ${PostgreSQL_LIBRARY} ${SECUR32_LIBRARY})
157+
158+
endif()
159+
160+
if (PostgreSQL_INCLUDE_DIR AND EXISTS "${PostgreSQL_INCLUDE_DIR}/pg_config.h")
161+
file(STRINGS "${PostgreSQL_INCLUDE_DIR}/pg_config.h" pgsql_version_str
162+
REGEX "^#define[\t ]+PG_VERSION[\t ]+\".*\"")
163+
164+
string(REGEX REPLACE "^#define[\t ]+PG_VERSION[\t ]+\"([^\"]*)\".*" "\\1"
165+
PostgreSQL_VERSION_STRING "${pgsql_version_str}")
166+
unset(pgsql_version_str)
167+
endif()
168+
169+
# Did we find anything?
170+
include(FindPackageHandleStandardArgs)
171+
find_package_handle_standard_args(PostgreSQL
172+
REQUIRED_VARS PostgreSQL_LIBRARY
173+
VERSION_VAR PostgreSQL_VERSION_STRING)
174+
set(PostgreSQL_FOUND ${POSTGRESQL_FOUND})
175+
176+
# Now try to get the include and library path.
177+
if(PostgreSQL_FOUND)
178+
179+
set(PostgreSQL_INCLUDE_DIRS ${PostgreSQL_INCLUDE_DIR})
180+
set(PostgreSQL_LIBRARY_DIRS ${PostgreSQL_LIBRARY_DIR})
181+
set(PostgreSQL_LIBRARIES ${PostgreSQL_LIBRARY})
182+
183+
#message("Final PostgreSQL include dir: ${PostgreSQL_INCLUDE_DIRS}")
184+
#message("Final PostgreSQL library dir: ${PostgreSQL_LIBRARY_DIRS}")
185+
#message("Final PostgreSQL libraries: ${PostgreSQL_LIBRARIES}")
186+
endif()
187+
188+
mark_as_advanced(PostgreSQL_INCLUDE_DIRS PostgreSQL_LIBRARIES)

0 commit comments

Comments
 (0)