Skip to content

Commit cb7d69c

Browse files
committed
CMake: Add detection of dependencies for building swig from source on linux
1 parent b8f8c5b commit cb7d69c

3 files changed

Lines changed: 208 additions & 0 deletions

File tree

cmake/modules/FindPCRE.cmake

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# CMake module to find PCRE headers & library
2+
#
3+
# Note:
4+
# This is for the older PCRE interface (i.e. not PCRE2). However this is named libpcre3 on ubuntu/debian...
5+
#
6+
# Usage:
7+
# find_package( PCRE )
8+
# if(PCRE_FOUND)
9+
# include_directories(${PCRE_INCLUDE_DIRS})
10+
# target_link_libraries(target ${PCRE_LIBRARIES})
11+
# endif()
12+
#
13+
# Variables:
14+
# PCRE_FOUND
15+
# PCRE_INCLUDE_DIRS
16+
# PCRE_LIBRARIES
17+
# PCRE_VERSION
18+
# PCRE_CONFIG
19+
20+
# Search for pcre.h in standard include paths
21+
find_path(PCRE_INCLUDE_DIRS
22+
NAMES
23+
pcre.h
24+
)
25+
26+
# Find the various pcre libraries
27+
find_library(PCRE_LIBRARY_8 NAMES pcre libpcre)
28+
find_library(PCRE_LIBRARY_16 NAMES pcre16 libpcre16 )
29+
find_library(PCRE_LIBRARY_32 NAMES pcre32 libpcre32 )
30+
find_library(PCRE_LIBRARY_POSIX NAMES pcreposix libpcreposix )
31+
32+
# Combine found libraries into a single list (adjust based on what your project needs)
33+
set(PCRE_LIBRARIES)
34+
if (PCRE_LIBRARY_8)
35+
list(APPEND PCRE_LIBRARIES ${PCRE_LIBRARY_8})
36+
endif()
37+
if (PCRE_LIBRARY_16)
38+
list(APPEND PCRE_LIBRARIES ${PCRE_LIBRARY_16})
39+
endif()
40+
if (PCRE_LIBRARY_32)
41+
list(APPEND PCRE_LIBRARIES ${PCRE_LIBRARY_32})
42+
endif()
43+
if (PCRE_LIBRARY_POSIX)
44+
list(APPEND PCRE_LIBRARIES ${PCRE_LIBRARY_POSIX})
45+
endif()
46+
47+
# Find pcre-config
48+
find_program(PCRE_CONFIG pcre-config)
49+
50+
# Extract the version number, which is defiend separately as PCRE_MAJOR and PCRE_MINOR (and PCRE_PRERELEASE, but not checking the prerelease component)
51+
set(PCRE_VERSION "unknown")
52+
if (PCRE_INCLUDE_DIRS AND EXISTS "${PCRE_INCLUDE_DIRS}/pcre.h")
53+
file(READ "${PCRE_INCLUDE_DIRS}/pcre.h" pcre_header_text)
54+
string(REGEX MATCH "define PCRE_MAJOR +([0-9]+)" PCRE_MAJOR_DEFINE ${pcre_header_text})
55+
set(PCRE_MAJOR "${CMAKE_MATCH_1}")
56+
string(REGEX MATCH "define PCRE_MINOR +([0-9]+)" PCRE_MINOR_DEFINE ${pcre_header_text})
57+
set(PCRE_MINOR "${CMAKE_MATCH_1}")
58+
if (NOT "${PCRE_MAJOR_DEFINE}" STREQUAL "" AND NOT "${PCRE_MAJOR_DEFINE}" STREQUAL "")
59+
set(PCRE_VERSION "${PCRE_MAJOR}.${PCRE_MINOR}")
60+
endif()
61+
endif()
62+
63+
include(FindPackageHandleStandardArgs)
64+
find_package_handle_standard_args(PCRE
65+
REQUIRED_VARS
66+
PCRE_LIBRARIES PCRE_INCLUDE_DIRS PCRE_CONFIG
67+
VERSION_VAR
68+
PCRE_VERSION
69+
)
70+
71+
72+
mark_as_advanced(PCRE_INCLUDE_DIRS PCRE_LIBRARY_8 PCRE_LIBRARY_16 PCRE_LIBRARY_32 PCRE_LIBRARY_POSIX PCRE_LIBRARIES PCRE_VERSION)
73+
74+
# Unset temporary variables
75+
unset(PCRE_MAJOR)
76+
unset(PCRE_MINOR)
77+
78+
# If we wanted to actually depend on pcre, we could create an imported library target here.
79+
# However there is no need currently as we are just checking for pcre presence to emit a warning if building swig from source and swig compilation failed.

cmake/modules/FindPCRE2.cmake

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# CMake module to find PCRE2 headers & library
2+
#
3+
# Usage:
4+
# find_package( PCRE2 )
5+
# if(PCRE2_FOUND)
6+
# include_directories(${PCRE2_INCLUDE_DIRS})
7+
# target_link_libraries(target ${PCRE2_LIBRARIES})
8+
# endif()
9+
#
10+
# Variables:
11+
# PCRE2_FOUND
12+
# PCRE2_INCLUDE_DIRS
13+
# PCRE2_LIBRARIES
14+
# PCRE2_VERSION
15+
# PCRE2_CONFIG
16+
17+
# Search for pcre2.h in standard include paths
18+
find_path(PCRE2_INCLUDE_DIRS
19+
NAMES
20+
pcre2.h
21+
)
22+
23+
# Find the various pcre libraries
24+
find_library(PCRE2_LIBRARY_8 NAMES pcre2-8 libpcre2-8)
25+
find_library(PCRE2_LIBRARY_16 NAMES pcre2-16 libpcre2-16 )
26+
find_library(PCRE2_LIBRARY_32 NAMES pcre2-32 libpcre2-32 )
27+
find_library(PCRE2_LIBRARY_POSIX NAMES pcre2-posix libpcre2-posix )
28+
29+
# Combine found libraries into a single list (adjust based on what your project needs)
30+
set(PCRE2_LIBRARIES)
31+
if (PCRE2_LIBRARY_8)
32+
list(APPEND PCRE2_LIBRARIES ${PCRE2_LIBRARY_8})
33+
endif()
34+
if (PCRE2_LIBRARY_16)
35+
list(APPEND PCRE2_LIBRARIES ${PCRE2_LIBRARY_16})
36+
endif()
37+
if (PCRE2_LIBRARY_32)
38+
list(APPEND PCRE2_LIBRARIES ${PCRE2_LIBRARY_32})
39+
endif()
40+
if (PCRE2_LIBRARY_POSIX)
41+
list(APPEND PCRE2_LIBRARIES ${PCRE2_LIBRARY_POSIX})
42+
endif()
43+
44+
# Find pcre2-config
45+
find_program(PCRE2_CONFIG pcre2-config)
46+
47+
# Extract the version number, which is defiend separately as PCRE2_MAJOR and PCRE2_MINOR (and PCRE2_PRERELEASE, but not checking the prerelease component)
48+
if (PCRE2_INCLUDE_DIRS AND EXISTS "${PCRE2_INCLUDE_DIRS}/pcre2.h")
49+
file(READ "${PCRE2_INCLUDE_DIRS}/pcre2.h" pcre2_header_text)
50+
string(REGEX MATCH "define PCRE2_MAJOR +([0-9]+)" PCRE2_MAJOR_DEFINE ${pcre2_header_text})
51+
set(PCRE2_MAJOR "${CMAKE_MATCH_1}")
52+
string(REGEX MATCH "define PCRE2_MINOR +([0-9]+)" PCRE2_MINOR_DEFINE ${pcre2_header_text})
53+
set(PCRE2_MINOR "${CMAKE_MATCH_1}")
54+
if (NOT "${PCRE2_MAJOR_DEFINE}" STREQUAL "" AND NOT "${PCRE2_MAJOR_DEFINE}" STREQUAL "")
55+
set(PCRE2_VERSION "${PCRE2_MAJOR}.${PCRE2_MINOR}")
56+
else()
57+
set(PCRE2_VERSION "unknown")
58+
endif()
59+
endif()
60+
61+
include(FindPackageHandleStandardArgs)
62+
find_package_handle_standard_args(PCRE2
63+
REQUIRED_VARS
64+
PCRE2_LIBRARIES PCRE2_INCLUDE_DIRS PCRE2_CONFIG
65+
VERSION_VAR
66+
PCRE2_VERSION
67+
)
68+
69+
mark_as_advanced(PCRE2_INCLUDE_DIRS PCRE2_LIBRARY_8 PCRE2_LIBRARY_16 PCRE2_LIBRARY_32 PCRE2_LIBRARY_POSIX PCRE2_LIBRARIES PCRE2_VERSION)
70+
71+
# Unset temporary variables
72+
unset(PCRE2_MAJOR)
73+
unset(PCRE2_MINOR)
74+
75+
# If we wanted to actually depend on pcre2, we could create an imported library target here.
76+
# However there is no need currently as we are just checking for pcre2 presence to emit a warning if building swig from source and swig compilation failed.

swig/CMakeLists.txt

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ if(POLICY CMP0135)
2323
cmake_policy(SET CMP0135 NEW)
2424
endif()
2525

26+
# Ensure custom Find* modules are avialable
27+
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/../cmake/modules/ ${CMAKE_MODULE_PATH})
28+
2629
# Find Python3, optionally an exact version for windows CI, or use in manylinux
2730
if(PYTHON3_EXACT_VERSION)
2831
set(PYTHON3_EXACT_VERSION_ARG ${PYTHON3_EXACT_VERSION} EXACT)
@@ -113,6 +116,54 @@ if(NOT SWIG_FOUND)
113116
)
114117
FetchContent_GetProperties(swig)
115118
if(NOT swig_POPULATED)
119+
# Quitely Find PCRE (Swig < 4.1) or PCRE2 >= swig 4.1. If not found, emit a warning, but do not error (incase cmake/modules/FindPCRE.cmake is not perfect)
120+
if (FLAMEGPU_SWIG_DOWNLOAD VERSION_LESS "4.1")
121+
find_package(PCRE QUIET)
122+
message("a")
123+
if(NOT PCRE_FOUND)
124+
message(
125+
WARNING " Swig ${FLAMEGPU_SWIG_DOWNLOAD} requires 'pcre-config', but PCRE could not be found.\n"
126+
" Swig compilation from source may fail.\n"
127+
" If so, please insall PCRE with development dependencies, e.g.:\n"
128+
" - On DEB-based distributions (Debian, Ubuntu, etc): apt-get install libpcre3-dev\n"
129+
" - On RPM-based distributions (Fedora, RHEL, etc): dnf install libpcre-devel"
130+
)
131+
endif()
132+
else()
133+
find_package(PCRE2 QUIET)
134+
if(NOT PCRE2_FOUND)
135+
message(
136+
WARNING " Swig ${FLAMEGPU_SWIG_DOWNLOAD} requires 'pcre2-config', but PCRE could not be found.\n"
137+
" Swig compilation from source may fail.\n"
138+
" If so, please insall PCRE with development dependencies, e.g.:\n"
139+
" - On DEB-based distributions (Debian, Ubuntu, etc): apt-get install libpcre2-dev\n"
140+
" - On DNF-based distributions (Fedora, RHEL, etc): dnf install libpcre2-devel"
141+
)
142+
endif()
143+
endif()
144+
145+
# Ensure that autotools/automake is available, as required to build swig from source.
146+
find_program(AUTOMAKE_EXECUTABLE NAMES automake)
147+
find_program(ACLOCAL_EXECUTABLE NAMES aclocal)
148+
if(NOT AUTOMAKE_EXECUTABLE OR NOT ACLOCAL_EXECUTABLE)
149+
message(
150+
FATAL_ERROR " Building Swig ${FLAMEGPU_SWIG_DOWNLOAD} from source requires automake, but it could not be found\n"
151+
" Please insall automake, e.g.:\n"
152+
" - On DEB-based distributions (Debian, Ubuntu, etc): apt-get install automake\n"
153+
" - On DNF-based distributions (Fedora, RHEL, etc): dnf install automake"
154+
)
155+
endif()
156+
157+
find_package(BISON)
158+
if(NOT BISON_FOUND)
159+
message(
160+
FATAL_ERROR " Buidling Swig ${FLAMEGPU_SWIG_DOWNLOAD} from source requires 'bison', but it could not be found\n"
161+
" Please insall bison, e.g.:\n"
162+
" - On DEB-based distributions (Debian, Ubuntu, etc): apt-get install bison\n"
163+
" - On DNF-based distributions (Fedora, RHEL, etc): dnf install bison"
164+
)
165+
endif()
166+
116167
message(STATUS "[swig] Downloading swig-${FLAMEGPU_SWIG_DOWNLOAD}.tar.gz")
117168
# Download the content
118169
FetchContent_Populate(swig)
@@ -145,6 +196,7 @@ if(NOT SWIG_FOUND)
145196
WORKING_DIRECTORY ${swig_SOURCE_DIR}
146197
RESULT_VARIABLE swig_make_RESULT
147198
OUTPUT_VARIABLE swig_make_OUTPUT
199+
ERROR_FILE ${swig_make_ERROR_FILE}
148200
)
149201
if(NOT swig_make_RESULT EQUAL "0")
150202
message(FATAL_ERROR
@@ -160,6 +212,7 @@ if(NOT SWIG_FOUND)
160212
WORKING_DIRECTORY ${swig_SOURCE_DIR}
161213
RESULT_VARIABLE swig_makeinstall_RESULT
162214
OUTPUT_VARIABLE swig_makeinstall_OUTPUT
215+
ERROR_FILE ${swig_makeinstall_ERROR_FILE}
163216
)
164217
if(NOT swig_makeinstall_RESULT EQUAL "0")
165218
message(FATAL_ERROR

0 commit comments

Comments
 (0)