1
1
########################################################################
2
2
# Project setup
3
3
########################################################################
4
- cmake_minimum_required (VERSION 2.8 )
4
+ cmake_minimum_required (VERSION 3.3 )
5
5
project (SoapySDRPython CXX)
6
6
enable_testing ()
7
7
@@ -14,81 +14,14 @@ find_package(SWIG)
14
14
message (STATUS "SWIG_FOUND: ${SWIG_FOUND} - ${SWIG_VERSION} " )
15
15
16
16
########################################################################
17
- # Find python interp
17
+ # Find Python
18
18
########################################################################
19
- find_package (PythonInterp)
20
- message (STATUS "PYTHONINTERP_FOUND: ${PYTHONINTERP_FOUND} - ${PYTHON_VERSION_STRING} " )
21
- message (STATUS "PYTHON_EXECUTABLE: ${PYTHON_EXECUTABLE} " )
22
-
23
- #help find_package(PythonLibs) by setting Python_ADDITIONAL_VERSIONS from PYTHON_VERSION_STRING
24
- if (PYTHONINTERP_FOUND AND DEFINED PYTHON_VERSION_STRING AND NOT DEFINED Python_ADDITIONAL_VERSIONS)
25
- string (SUBSTRING "${PYTHON_VERSION_STRING} " 0 3 Python_ADDITIONAL_VERSIONS)
26
- endif ()
27
-
28
- ########################################################################
29
- # Determine install directory
30
- ########################################################################
31
- execute_process (
32
- COMMAND ${PYTHON_EXECUTABLE} "${CMAKE_CURRENT_SOURCE_DIR} /get_python_lib.py" "${CMAKE_INSTALL_PREFIX} "
33
- OUTPUT_STRIP_TRAILING_WHITESPACE
34
- OUTPUT_VARIABLE PYTHON_INSTALL_DIR_SYSCONF
35
- )
36
- set (PYTHON_INSTALL_DIR "${PYTHON_INSTALL_DIR_SYSCONF} " CACHE STRING "python install prefix" )
37
- message (STATUS "PYTHON_INSTALL_DIR: \$ {prefix}/${PYTHON_INSTALL_DIR} " )
38
-
39
- ########################################################################
40
- # Find Python libs
41
- ########################################################################
42
- option (USE_PYTHON_CONFIG "use python-config to locate development files" TRUE )
43
- set (PYTHON_CONFIG_EXECUTABLE ${PYTHON_EXECUTABLE} -config
44
- CACHE FILEPATH "Path to python-config executable" )
45
- if (USE_PYTHON_CONFIG AND EXISTS ${PYTHON_CONFIG_EXECUTABLE} )
46
- execute_process (
47
- COMMAND ${PYTHON_CONFIG_EXECUTABLE} --includes
48
- OUTPUT_STRIP_TRAILING_WHITESPACE
49
- OUTPUT_VARIABLE PYTHON_INCLUDE_DIRS)
50
- string (REGEX REPLACE "^[-I]" "" PYTHON_INCLUDE_DIRS "${PYTHON_INCLUDE_DIRS} " )
51
- string (REGEX REPLACE "[ ]-I" " " PYTHON_INCLUDE_DIRS "${PYTHON_INCLUDE_DIRS} " )
52
- separate_arguments (PYTHON_INCLUDE_DIRS)
53
- execute_process (
54
- COMMAND ${PYTHON_CONFIG_EXECUTABLE} --ldflags
55
- OUTPUT_STRIP_TRAILING_WHITESPACE
56
- OUTPUT_VARIABLE PYTHON_LIBRARIES)
57
- string (STRIP "${PYTHON_LIBRARIES} " PYTHON_LIBRARIES)
58
- set (PYTHONLIBS_VERSION_STRING ${PYTHON_VERSION_STRING} )
59
- set (PYTHONLIBS_FOUND TRUE )
60
- else ()
61
- find_package (PythonLibs)
62
- endif ()
63
-
64
- message (STATUS "PYTHONLIBS_FOUND: ${PYTHONLIBS_FOUND} - ${PYTHONLIBS_VERSION_STRING} " )
65
- message (STATUS "PYTHON_INCLUDE_DIRS: ${PYTHON_INCLUDE_DIRS} " )
66
- message (STATUS "PYTHON_LIBRARIES: ${PYTHON_LIBRARIES} " )
67
-
68
- #on windows, we require a pythonxx_d.lib in debug mode
69
- #require that the PYTHON_DEBUG_LIBRARY flag is set
70
- #or the build assumes that the debug library DNE
71
- set (PYTHON_DEBUG_OK TRUE )
72
- if (WIN32 AND NOT PYTHON_DEBUG_LIBRARY AND "${CMAKE_BUILD_TYPE} " STREQUAL "Debug" )
73
- message (WARNING "WIN32 Debug mode requires PYTHON_DEBUG_LIBRARY" )
74
- set (PYTHON_DEBUG_OK FALSE )
75
- endif ()
76
-
77
- ########################################################################
78
- # Python version check
79
- ########################################################################
80
- set (PYTHON_VERSION_MATCH TRUE )
81
- if (PYTHON_VERSION_STRING AND PYTHONLIBS_VERSION_STRING)
82
- if (NOT "${PYTHON_VERSION_STRING} " VERSION_EQUAL "${PYTHONLIBS_VERSION_STRING} " )
83
- message (WARNING "Python interp and library version mismatch" )
84
- set (PYTHON_VERSION_MATCH FALSE )
85
- endif ()
86
- endif ()
19
+ list (PREPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR} /cmake)
87
20
88
21
########################################################################
89
- ## set the swig flags - shared with python3 build
22
+ ## set the swig flags
90
23
########################################################################
91
- set (CMAKE_SWIG_FLAGS -c++ - threads -I${SoapySDR_INCLUDE_DIRS} )
24
+ set (CMAKE_SWIG_FLAGS -threads -I${SoapySDR_INCLUDE_DIRS} )
92
25
93
26
#check for size_t issue on arm 32-bit platforms
94
27
include (CheckCXXSourceCompiles)
@@ -102,80 +35,106 @@ if (SIZE_T_IS_UNSIGNED_INT)
102
35
list (APPEND CMAKE_SWIG_FLAGS -DSIZE_T_IS_UNSIGNED_INT)
103
36
endif (SIZE_T_IS_UNSIGNED_INT)
104
37
105
- ########################################################################
106
- ## Export variables for python3 subdirectory
107
- ########################################################################
108
-
109
- #this directory used as a stand-alone build since the library is pulled in externally
110
- #set ENABLE_LIBRARY for cmake_dependent_option() used in the feature setup below
111
- if ("${PROJECT_SOURCE_DIR} " STREQUAL "${CMAKE_SOURCE_DIR} " )
112
- set (ENABLE_LIBRARY ${SoapySDR_FOUND} )
113
-
114
- #otherwise export enable and swig variables used in the python3 directory
115
- else ()
116
-
117
- #set once we know that executable and libs are found and match
118
- #this tells the parent scope to build python3 when this is python2
119
- if (PYTHON_VERSION_STRING AND "${PYTHON_VERSION_STRING} " VERSION_LESS "3.0" )
120
- set (BUILD_PYTHON3 TRUE PARENT_SCOPE)
121
- endif ()
122
-
123
- #or enable search for python3 when this directory failed to find
124
- #a full set of python interpreter and devel files of any version
125
- if (NOT PYTHONINTERP_FOUND OR NOT PYTHONLIBS_FOUND)
126
- set (BUILD_PYTHON3 TRUE PARENT_SCOPE)
127
- endif ()
128
-
129
- set (CMAKE_SWIG_FLAGS ${CMAKE_SWIG_FLAGS} PARENT_SCOPE)
130
-
131
- endif ()
132
-
133
38
########################################################################
134
39
## Feature registration
135
40
########################################################################
136
41
include (FeatureSummary)
137
42
include (CMakeDependentOption)
138
- cmake_dependent_option(ENABLE_PYTHON "Enable python bindings" ON "ENABLE_LIBRARY;SWIG_FOUND;PYTHONINTERP_FOUND;PYTHONLIBS_FOUND;PYTHON_DEBUG_OK;PYTHON_VERSION_MATCH" OFF )
139
- add_feature_info(Python ENABLE_PYTHON "python bindings v${PYTHON_VERSION_STRING} " )
140
- if (NOT ENABLE_PYTHON)
141
- return ()
43
+
44
+ # Note: for build script compatibility, still use ENABLE_PYTHON for Python 2
45
+ message (STATUS "" )
46
+ message (STATUS "#############################################" )
47
+ message (STATUS "## Begin configuration for Python 2 support..." )
48
+ message (STATUS "#############################################" )
49
+ message (STATUS "Enabling optional Python 2 bindings if possible..." )
50
+ find_package (Python2 COMPONENTS Interpreter Development)
51
+ if (${Python2_FOUND} )
52
+ message (STATUS " * Interpreter: ${Python2_EXECUTABLE} (${Python2_INTERPRETER_ID} )" )
53
+ message (STATUS " * Include: ${Python2_INCLUDE_DIRS} " )
54
+ message (STATUS " * Library: ${Python2_LIBRARIES} " )
55
+ endif ()
56
+ cmake_dependent_option(ENABLE_PYTHON "Enable Python2 bindings" ON "ENABLE_LIBRARY;SWIG_FOUND;Python2_FOUND" OFF )
57
+ add_feature_info(Python2 ENABLE_PYTHON "Python2 bindings v${Python2_VERSION} " )
58
+
59
+ message (STATUS "" )
60
+ message (STATUS "#############################################" )
61
+ message (STATUS "## Begin configuration for Python 3 support..." )
62
+ message (STATUS "#############################################" )
63
+ message (STATUS "Enabling optional Python 3 bindings if possible..." )
64
+ find_package (Python3 COMPONENTS Interpreter Development)
65
+ if (${Python3_FOUND} )
66
+ message (STATUS " * Interpreter: ${Python3_EXECUTABLE} (${Python3_INTERPRETER_ID} )" )
67
+ message (STATUS " * Include: ${Python3_INCLUDE_DIRS} " )
68
+ message (STATUS " * Library: ${Python3_LIBRARIES} " )
142
69
endif ()
70
+ cmake_dependent_option(ENABLE_PYTHON3 "Enable Python3 bindings" ON "ENABLE_LIBRARY;SWIG_FOUND;Python3_FOUND" OFF )
71
+ add_feature_info(Python3 ENABLE_PYTHON3 "Python3 bindings v${Python3_VERSION} " )
143
72
144
73
########################################################################
145
- # Build Module
74
+ # Build and install module
146
75
########################################################################
147
76
include (UseSWIG)
77
+ set (SOAPYSDR_PYTHON_DIR ${CMAKE_CURRENT_SOURCE_DIR} )
148
78
149
- configure_file (
150
- SoapySDR.in.i
151
- ${CMAKE_CURRENT_BINARY_DIR} /SoapySDR.i
152
- @ONLY)
79
+ function (BUILD_PYTHON_MODULE PYTHON_VERSION)
80
+ configure_file (
81
+ ${SOAPYSDR_PYTHON_DIR} /SoapySDR.in.i
82
+ ${CMAKE_CURRENT_BINARY_DIR} /SoapySDR.i
83
+ @ONLY)
84
+ set_source_files_properties (${CMAKE_CURRENT_BINARY_DIR} /SoapySDR.i PROPERTIES CPLUSPLUS ON )
153
85
154
- message (STATUS "CMAKE_SWIG_FLAGS=${CMAKE_SWIG_FLAGS} " )
155
- set_source_files_properties (${CMAKE_CURRENT_BINARY_DIR} /SoapySDR.i PROPERTIES CPLUSPLUS ON )
86
+ if (MINGW)
87
+ string (APPEND CMAKE_SHARED_LINKER_FLAGS " -static-libgcc -static-libstdc++" )
88
+ endif ()
156
89
157
- if (${CMAKE_VERSION} VERSION_LESS "3.8" )
158
- SWIG_ADD_MODULE(SoapySDR python ${CMAKE_CURRENT_BINARY_DIR} /SoapySDR.i)
159
- else ()
160
- SWIG_ADD_LIBRARY(SoapySDR LANGUAGE python SOURCES ${CMAKE_CURRENT_BINARY_DIR} /SoapySDR.i)
161
- endif ()
90
+ if (${CMAKE_VERSION} VERSION_LESS "3.8" )
91
+ SWIG_ADD_MODULE(SoapySDR${PYTHON_VERSION} python ${CMAKE_CURRENT_BINARY_DIR} /SoapySDR.i)
92
+ else ()
93
+ SWIG_ADD_LIBRARY(SoapySDR${PYTHON_VERSION} LANGUAGE python SOURCES ${CMAKE_CURRENT_BINARY_DIR} /SoapySDR.i)
94
+ endif ()
162
95
163
- if (APPLE )
164
- list (APPEND PYTHON_LIBRARIES "-undefined dynamic_lookup" )
165
- endif ()
96
+ set (python_includes ${SoapySDR_INCLUDE_DIRS} )
97
+ set (python_libraries SoapySDR Python${PYTHON_VERSION} ::Module)
166
98
167
- target_include_directories (${SWIG_MODULE_SoapySDR_REAL_NAME} PRIVATE ${PYTHON_INCLUDE_DIRS} )
168
- SWIG_LINK_LIBRARIES(SoapySDR SoapySDR ${PYTHON_LIBRARIES} )
99
+ set_target_properties (${SWIG_MODULE_SoapySDR${PYTHON_VERSION} _REAL_NAME} PROPERTIES OUTPUT_NAME _SoapySDR)
169
100
170
- ########################################################################
171
- # Install Module
172
- ########################################################################
173
- install (
174
- TARGETS ${SWIG_MODULE_SoapySDR_REAL_NAME}
175
- DESTINATION ${PYTHON_INSTALL_DIR}
176
- )
177
-
178
- install (
179
- FILES ${CMAKE_CURRENT_BINARY_DIR} /SoapySDR.py
180
- DESTINATION ${PYTHON_INSTALL_DIR}
181
- )
101
+ if (MINGW)
102
+ # https://stackoverflow.com/a/50792585
103
+ if (CMAKE_SIZEOF_VOID_P EQUAL 8)
104
+ target_compile_definitions (${SWIG_MODULE_SoapySDR${PYTHON_VERSION} _REAL_NAME} PRIVATE -DMS_WIN64=1)
105
+ else ()
106
+ target_compile_definitions (${SWIG_MODULE_SoapySDR${PYTHON_VERSION} _REAL_NAME} PRIVATE -DMS_WIN32=1)
107
+ endif ()
108
+ endif ()
109
+
110
+ target_include_directories (${SWIG_MODULE_SoapySDR${PYTHON_VERSION} _REAL_NAME} PRIVATE ${python_includes} )
111
+ SWIG_LINK_LIBRARIES(SoapySDR${PYTHON_VERSION} ${python_libraries} )
112
+
113
+ execute_process (
114
+ COMMAND ${Python${PYTHON_VERSION} _EXECUTABLE} ${SOAPYSDR_PYTHON_DIR} /get_python_lib.py ${CMAKE_INSTALL_PREFIX}
115
+ OUTPUT_STRIP_TRAILING_WHITESPACE
116
+ OUTPUT_VARIABLE PYTHON_INSTALL_DIR)
117
+
118
+ install (
119
+ TARGETS ${SWIG_MODULE_SoapySDR${PYTHON_VERSION} _REAL_NAME}
120
+ DESTINATION ${PYTHON_INSTALL_DIR}
121
+ )
122
+
123
+ install (
124
+ FILES ${CMAKE_CURRENT_BINARY_DIR} /SoapySDR.py
125
+ DESTINATION ${PYTHON_INSTALL_DIR}
126
+ )
127
+ endfunction ()
128
+
129
+ # TODO: Windows has full Python installations in different directories, so all Python
130
+ # versions install file in the same subpaths. CMake doesn't catch this for some reason,
131
+ # so should we error out if both Python versions are detected or prioritize one over the
132
+ # other with a warning?
133
+
134
+ if (ENABLE_PYTHON)
135
+ add_subdirectory (python2)
136
+ endif ()
137
+
138
+ if (ENABLE_PYTHON3)
139
+ add_subdirectory (python3)
140
+ endif ()
0 commit comments