Skip to content

Commit ac71a34

Browse files
committed
Detect ccache and CC for TimescaleDB from pg_config --cc
This simplifies the build configuration a little.
1 parent ccd24b5 commit ac71a34

File tree

1 file changed

+56
-25
lines changed

1 file changed

+56
-25
lines changed

CMakeLists.txt

Lines changed: 56 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,31 @@ function(GET_PG_CONFIG var)
6060
PARENT_SCOPE)
6161
endfunction()
6262

63+
# Search paths for Postgres binaries
64+
if(WIN32)
65+
find_path(
66+
PG_PATH postgres.exe
67+
PATHS "C:/PostgreSQL" "C:/Program Files/PostgreSQL"
68+
PATH_SUFFIXES bin 15/bin 16/bin
69+
DOC "The path to a PostgreSQL installation")
70+
elseif(UNIX)
71+
find_path(
72+
PG_PATH postgres
73+
PATHS $ENV{HOME} /opt/local/pgsql /usr/local/pgsql /usr/lib/postgresql
74+
PATH_SUFFIXES bin 15/bin 16/bin
75+
DOC "The path to a PostgreSQL installation")
76+
endif()
77+
78+
find_program(
79+
PG_CONFIG pg_config
80+
HINTS ${PG_PATH}
81+
PATH_SUFFIXES bin
82+
DOC "The path to the pg_config of the PostgreSQL version to compile against")
83+
84+
if(NOT PG_CONFIG)
85+
message(FATAL_ERROR "Unable to find 'pg_config'")
86+
endif()
87+
6388
configure_file("version.config" "version.config" COPYONLY)
6489
file(READ version.config VERSION_CONFIG)
6590

@@ -95,6 +120,37 @@ endif()
95120
# a hack to avoid change of SQL extschema variable
96121
set(extschema "@extschema@")
97122

123+
# Try to use the same compiler as Postgres to avoid mismatches and simplify
124+
# configuration.
125+
if(NOT DEFINED CMAKE_C_COMPILER)
126+
get_pg_config(PG_CC --cc)
127+
128+
# The first word might be ccache or similar, so handle this.
129+
string(REPLACE " " ";" PG_CC_LIST ${PG_CC})
130+
list(POP_FRONT PG_CC_LIST PG_CC_FIRST)
131+
if(PG_CC_LIST)
132+
# Got multi-word PG CC, treat the first word as ccache.
133+
find_program(PG_CCACHE_FOUND ${PG_CC_FIRST})
134+
if((NOT DEFINED CMAKE_C_COMPILER_LAUNCHER) AND PG_CCACHE_FOUND)
135+
message(
136+
STATUS
137+
"Using ${PG_CCACHE_FOUND} as C compiler launcher based on pg_config CC"
138+
)
139+
set(CMAKE_C_COMPILER_LAUNCHER ${PG_CCACHE_FOUND})
140+
endif()
141+
set(PG_CC ${PG_CC_LIST})
142+
else()
143+
# Single-word CC.
144+
set(PG_CC ${PG_CC_FIRST})
145+
endif()
146+
147+
find_program(PG_CC_FOUND ${PG_CC})
148+
if(PG_CC_FOUND)
149+
message(STATUS "Using ${PG_CC_FOUND} as CC based on pg_config CC")
150+
set(CMAKE_C_COMPILER ${PG_CC_FOUND})
151+
endif()
152+
endif()
153+
98154
# Set project name, version, and language. Language needs to be set for compiler
99155
# checks
100156
project(
@@ -298,31 +354,6 @@ if(ENABLE_OPTIMIZER_DEBUG)
298354
add_definitions(-DOPTIMIZER_DEBUG)
299355
endif()
300356

301-
# Search paths for Postgres binaries
302-
if(WIN32)
303-
find_path(
304-
PG_PATH postgres.exe
305-
PATHS "C:/PostgreSQL" "C:/Program Files/PostgreSQL"
306-
PATH_SUFFIXES bin 15/bin 16/bin
307-
DOC "The path to a PostgreSQL installation")
308-
elseif(UNIX)
309-
find_path(
310-
PG_PATH postgres
311-
PATHS $ENV{HOME} /opt/local/pgsql /usr/local/pgsql /usr/lib/postgresql
312-
PATH_SUFFIXES bin 15/bin 16/bin
313-
DOC "The path to a PostgreSQL installation")
314-
endif()
315-
316-
find_program(
317-
PG_CONFIG pg_config
318-
HINTS ${PG_PATH}
319-
PATH_SUFFIXES bin
320-
DOC "The path to the pg_config of the PostgreSQL version to compile against")
321-
322-
if(NOT PG_CONFIG)
323-
message(FATAL_ERROR "Unable to find 'pg_config'")
324-
endif()
325-
326357
find_package(Git)
327358

328359
# Check PostgreSQL version

0 commit comments

Comments
 (0)