Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e9d763b

Browse files
ludwigschwardtgijzelaerr
authored andcommitted
Improve CMake FindPython (#922)
* Rename original Python CMakeLists These versions will be used by CMake 3.11 and older. * Improve CMake FindPython routines CMake 3.12 introduced the FindPython2 and FindPython3 modules which supercede the old and buggy FindPythonLibs and FindPythonInterp modules. It finds consistent versions of the interpreter, development artifacts and NumPy include directories in one go, and does it separately for Python 2 and 3. I've kept the original CMake variable names since I'm not sure where they are used, but some of them may probably be removed or refactored. This has been tested on a Homebrew setup (but not on Linux), where it fixes detection errors using the previous commit. By copying the appropriate modules from CMake, this functionality could also be made to work on the older CMake 3.7 (although this might not be old enough for all installations of casacore). * Pick Python CMakeLists.txt based on CMake version Use the newer CMakeLists if CMake version >= 3.12. It would have been nice to use the CMAKE_VERSION variable instead, but that only appeared in CMake 2.6.3 (and we have a minimum version of 2.6.0). An alternative solution is to check for the presence of the FindPython2 and FindPython3 modules, using a CheckHasModule function like the one in https://stackoverflow.com/questions/15706318. * Improve Boost.Python library name setting Prefer EQUAL/GREATER to STREQUAL/STRGREATER, otherwise Boost 1.100 (Heaven forbid!) would be less than 1.67. Fix whitespace and reduce the line count.
1 parent 25ac98f commit e9d763b

6 files changed

Lines changed: 415 additions & 236 deletions

File tree

python/CMakeLists-cmake3.12.txt

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
message(STATUS "Looking for python2 specific environment...")
2+
3+
find_package(Python2 REQUIRED COMPONENTS Interpreter Development NumPy)
4+
5+
if (Python2_FOUND)
6+
find_package(Boost REQUIRED)
7+
if(${Boost_MAJOR_VERSION} EQUAL 1 AND ${Boost_MINOR_VERSION} GREATER 66)
8+
# Boost>=1.67 Python components require a Python version suffix
9+
set(BOOST_PYTHON_LIBRARY_NAME
10+
python${Python2_VERSION_MAJOR}${Python2_VERSION_MINOR}
11+
CACHE STRING "The name of the boost python library to search for")
12+
else()
13+
set(BOOST_PYTHON_LIBRARY_NAME
14+
python
15+
CACHE STRING "The name of the boost python library to search for")
16+
endif()
17+
find_package(Boost REQUIRED COMPONENTS ${BOOST_PYTHON_LIBRARY_NAME})
18+
19+
# copy the variables to their final destination
20+
set(PYTHON2_EXECUTABLE ${Python2_EXECUTABLE} CACHE FILEPATH "Path to Python2 interpreter")
21+
set(PYTHON2_LIBRARY ${Python2_LIBRARIES} CACHE PATH "Python2 library")
22+
set(PYTHON2_INCLUDE_DIR ${Python2_INCLUDE_DIRS} CACHE PATH "Python2 include folder")
23+
set(PYTHON2_FIND_PACKAGE_MESSAGE_DETAILS_PythonLibs ${FIND_PACKAGE_MESSAGE_DETAILS_PythonLibs} CACHE STRING "")
24+
25+
set(PYTHON2_LIBRARIES ${Python2_LIBRARIES} PARENT_SCOPE)
26+
set(PYTHON2_NUMPY_INCLUDE_DIRS ${Python2_NumPy_INCLUDE_DIRS} PARENT_SCOPE)
27+
set(PYTHON2_Boost_LIBRARIES ${Boost_LIBRARIES} PARENT_SCOPE)
28+
set(PYTHON2_Boost_INCLUDE_DIRS ${Boost_INCLUDE_DIRS} PARENT_SCOPE)
29+
set(PYTHON2_Boost_PYTHON_LIBRARY_RELEASE ${Boost_PYTHON_LIBRARY_RELEASE} PARENT_SCOPE)
30+
set(PYTHON2_INCLUDE_DIRS ${Python2_INCLUDE_DIRS} PARENT_SCOPE)
31+
32+
# to access the variables here we also need to set them in the local scope
33+
set(PYTHON2_LIBRARIES ${Python2_LIBRARIES} )
34+
set(PYTHON2_NUMPY_INCLUDE_DIRS ${Python2_NumPy_INCLUDE_DIRS} )
35+
set(PYTHON2_Boost_LIBRARIES ${Boost_LIBRARIES} )
36+
set(PYTHON2_Boost_INCLUDE_DIRS ${Boost_INCLUDE_DIRS} )
37+
set(PYTHON2_Boost_PYTHON_LIBRARY_RELEASE ${Boost_PYTHON_LIBRARY_RELEASE} )
38+
set(PYTHON2_INCLUDE_DIRS ${Python2_INCLUDE_DIRS} )
39+
endif(Python2_FOUND)
40+
41+
42+
include_directories (${PYTHON2_Boost_INCLUDE_DIRS} ${PYTHON2_NUMPY_INCLUDE_DIRS} ${PYTHON2_INCLUDE_DIRS})
43+
44+
add_library (casa_python
45+
Converters/PycArray.cc
46+
Converters/PycArrayNP.cc
47+
Converters/PycBasicData.cc
48+
Converters/PycExcp.cc
49+
Converters/PycImport.cc
50+
Converters/PycRecord.cc
51+
Converters/PycValueHolder.cc
52+
Converters/PycArray.h
53+
Converters/PycArrayComCC.h
54+
Converters/PycArrayComH.h
55+
Converters/PycArrayNP.h
56+
Converters/PycBasicData.h
57+
Converters/PycExcp.h
58+
Converters/PycRecord.h
59+
Converters/PycValueHolder.h
60+
Converters/PycArray.tcc
61+
)
62+
63+
target_link_libraries (casa_python casa_casa ${PYTHON2_Boost_LIBRARIES} ${PYTHON2_LIBRARIES} ${CASACORE_ARCH_LIBS})
64+
65+
install (TARGETS casa_python
66+
RUNTIME DESTINATION bin
67+
LIBRARY DESTINATION lib${LIB_SUFFIX}
68+
ARCHIVE DESTINATION lib${LIB_SUFFIX}
69+
LIBRARY PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE
70+
)
71+
72+
install (FILES
73+
Converters/PycArray.h
74+
Converters/PycArrayComCC.h
75+
Converters/PycArrayComH.h
76+
Converters/PycArrayNP.h
77+
Converters/PycBasicData.h
78+
Converters/PycExcp.h
79+
Converters/PycRecord.h
80+
Converters/PycValueHolder.h
81+
Converters/PycArray.tcc
82+
DESTINATION include/casacore/python/Converters
83+
)
84+
85+
install (FILES
86+
Converters.h
87+
DESTINATION include/casacore/python
88+
)
89+
90+
add_subdirectory (Converters/test ${EXCL_ALL})

python/CMakeLists-older-cmake.txt

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
message(STATUS "Looking for python2 specific environment...")
2+
3+
# tempororarly set variables used by findpython
4+
if (PYTHON2_EXECUTABLE)
5+
set(PYTHON_EXECUTABLE ${PYTHON2_EXECUTABLE} CACHE FILEPATH "")
6+
endif()
7+
8+
if (PYTHON2_LIBRARY)
9+
set(PYTHON_LIBRARY ${PYTHON2_LIBRARY} CACHE FILEPATH "")
10+
endif()
11+
12+
if (PYTHON2_INCLUDE_DIR)
13+
set(PYTHON_INCLUDE_DIR ${PYTHON2_INCLUDE_DIR} CACHE FILEPATH "")
14+
endif()
15+
16+
if (PYTHON2_FIND_PACKAGE_MESSAGE_DETAILS_PythonLibs)
17+
set(FIND_PACKAGE_MESSAGE_DETAILS_PythonLibs ${PYTHON2_FIND_PACKAGE_MESSAGE_DETAILS_PythonLibs} CACHE FILEPATH "")
18+
endif()
19+
20+
# Detect the python properties
21+
set(Python_FIND_VERSION 2)
22+
set(PythonInterp_FIND_VERSION_MAJOR 2)
23+
find_package(Python REQUIRED)
24+
if (PYTHONINTERP_FOUND)
25+
find_package(Boost REQUIRED)
26+
if(${Boost_MAJOR_VERSION} EQUAL 1 AND ${Boost_MINOR_VERSION} GREATER 66)
27+
# Boost>=1.67 Python components require a Python version suffix
28+
set(BOOST_PYTHON_LIBRARY_NAME
29+
python${PYTHON_VERSION_MAJOR}${PYTHON_VERSION_MINOR}
30+
CACHE STRING "The name of the boost python library to search for")
31+
else()
32+
set(BOOST_PYTHON_LIBRARY_NAME
33+
python
34+
CACHE STRING "The name of the boost python library to search for")
35+
endif()
36+
find_package(Boost REQUIRED COMPONENTS ${BOOST_PYTHON_LIBRARY_NAME})
37+
38+
find_package (NUMPY REQUIRED)
39+
40+
# copy the variables to their final destination
41+
set(PYTHON2_EXECUTABLE ${PYTHON_EXECUTABLE} CACHE FILEPATH "Path to Python2 interpreter")
42+
set(PYTHON2_LIBRARY ${PYTHON_LIBRARY} CACHE PATH "Python2 library")
43+
set(PYTHON2_INCLUDE_DIR ${PYTHON_INCLUDE_DIR} CACHE PATH "Python2 include folder")
44+
set(PYTHON2_FIND_PACKAGE_MESSAGE_DETAILS_PythonLibs ${FIND_PACKAGE_MESSAGE_DETAILS_PythonLibs} CACHE STRING "")
45+
46+
set(PYTHON2_LIBRARIES ${PYTHON_LIBRARIES} PARENT_SCOPE)
47+
set(PYTHON2_NUMPY_INCLUDE_DIRS ${NUMPY_INCLUDE_DIRS} PARENT_SCOPE)
48+
set(PYTHON2_Boost_LIBRARIES ${Boost_LIBRARIES} PARENT_SCOPE)
49+
set(PYTHON2_Boost_INCLUDE_DIRS ${Boost_INCLUDE_DIRS} PARENT_SCOPE)
50+
set(PYTHON2_Boost_PYTHON_LIBRARY_RELEASE ${Boost_PYTHON_LIBRARY_RELEASE} PARENT_SCOPE)
51+
set(PYTHON2_INCLUDE_DIRS ${PYTHON_INCLUDE_DIRS} PARENT_SCOPE)
52+
53+
# to access the variables here we also need to set them in the local scope
54+
set(PYTHON2_LIBRARIES ${PYTHON_LIBRARIES} )
55+
set(PYTHON2_NUMPY_INCLUDE_DIRS ${NUMPY_INCLUDE_DIRS} )
56+
set(PYTHON2_Boost_LIBRARIES ${Boost_LIBRARIES} )
57+
set(PYTHON2_Boost_INCLUDE_DIRS ${Boost_INCLUDE_DIRS} )
58+
set(PYTHON2_Boost_PYTHON_LIBRARY_RELEASE ${Boost_PYTHON_LIBRARY_RELEASE} )
59+
set(PYTHON2_INCLUDE_DIRS ${PYTHON_INCLUDE_DIRS} )
60+
61+
# Remove cached variable to not confuse user
62+
unset(PYTHON_EXECUTABLE CACHE)
63+
unset(PYTHON_LIBRARY CACHE)
64+
unset(PYTHON_INCLUDE_DIR CACHE)
65+
unset(FIND_PACKAGE_MESSAGE_DETAILS_PythonLibs CACHE)
66+
endif(PYTHONINTERP_FOUND)
67+
68+
69+
include_directories (${PYTHON2_Boost_INCLUDE_DIRS} ${PYTHON2_NUMPY_INCLUDE_DIRS} ${PYTHON2_INCLUDE_DIRS})
70+
71+
add_library (casa_python
72+
Converters/PycArray.cc
73+
Converters/PycArrayNP.cc
74+
Converters/PycBasicData.cc
75+
Converters/PycExcp.cc
76+
Converters/PycImport.cc
77+
Converters/PycRecord.cc
78+
Converters/PycValueHolder.cc
79+
Converters/PycArray.h
80+
Converters/PycArrayComCC.h
81+
Converters/PycArrayComH.h
82+
Converters/PycArrayNP.h
83+
Converters/PycBasicData.h
84+
Converters/PycExcp.h
85+
Converters/PycRecord.h
86+
Converters/PycValueHolder.h
87+
Converters/PycArray.tcc
88+
)
89+
90+
target_link_libraries (casa_python casa_casa ${PYTHON2_Boost_LIBRARIES} ${PYTHON2_LIBRARIES} ${CASACORE_ARCH_LIBS})
91+
92+
install (TARGETS casa_python
93+
RUNTIME DESTINATION bin
94+
LIBRARY DESTINATION lib${LIB_SUFFIX}
95+
ARCHIVE DESTINATION lib${LIB_SUFFIX}
96+
LIBRARY PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE
97+
)
98+
99+
install (FILES
100+
Converters/PycArray.h
101+
Converters/PycArrayComCC.h
102+
Converters/PycArrayComH.h
103+
Converters/PycArrayNP.h
104+
Converters/PycBasicData.h
105+
Converters/PycExcp.h
106+
Converters/PycRecord.h
107+
Converters/PycValueHolder.h
108+
Converters/PycArray.tcc
109+
DESTINATION include/casacore/python/Converters
110+
)
111+
112+
install (FILES
113+
Converters.h
114+
DESTINATION include/casacore/python
115+
)
116+
117+
add_subdirectory (Converters/test ${EXCL_ALL})

python/CMakeLists.txt

Lines changed: 6 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -1,121 +1,7 @@
1-
message(STATUS "Looking for python2 specific environment...")
2-
3-
# tempororarly set variables used by findpython
4-
if (PYTHON2_EXECUTABLE)
5-
set(PYTHON_EXECUTABLE ${PYTHON2_EXECUTABLE} CACHE FILEPATH "")
1+
# It would be nice to use CMAKE_VERSION instead but that requires CMake 2.6.3
2+
if(${CMAKE_MAJOR_VERSION} GREATER 3 OR
3+
(${CMAKE_MAJOR_VERSION} EQUAL 3 AND ${CMAKE_MINOR_VERSION} GREATER 11))
4+
include (${CMAKE_CURRENT_LIST_DIR}/CMakeLists-cmake3.12.txt)
5+
else()
6+
include (${CMAKE_CURRENT_LIST_DIR}/CMakeLists-older-cmake.txt)
67
endif()
7-
8-
if (PYTHON2_LIBRARY)
9-
set(PYTHON_LIBRARY ${PYTHON2_LIBRARY} CACHE FILEPATH "")
10-
endif()
11-
12-
if (PYTHON2_INCLUDE_DIR)
13-
set(PYTHON_INCLUDE_DIR ${PYTHON2_INCLUDE_DIR} CACHE FILEPATH "")
14-
endif()
15-
16-
if (PYTHON2_FIND_PACKAGE_MESSAGE_DETAILS_PythonLibs)
17-
set(FIND_PACKAGE_MESSAGE_DETAILS_PythonLibs ${PYTHON2_FIND_PACKAGE_MESSAGE_DETAILS_PythonLibs} CACHE FILEPATH "")
18-
endif()
19-
20-
# Detect the python properties
21-
set(Python_FIND_VERSION 2)
22-
set(PythonInterp_FIND_VERSION_MAJOR 2)
23-
find_package(Python REQUIRED)
24-
if (PYTHONINTERP_FOUND)
25-
find_package(Boost REQUIRED)
26-
if (${Boost_MAJOR_VERSION} STREQUAL 1 AND ${Boost_MINOR_VERSION} STRGREATER 66)
27-
# Boost>=1.67 Python components require a Python version suffix
28-
set(BOOST_PYTHON_LIBRARY_NAME
29-
python${PYTHON_VERSION_MAJOR}${PYTHON_VERSION_MINOR}
30-
CACHE
31-
FILEPATH
32-
"The name of the boost python library to search for")
33-
else ()
34-
set(BOOST_PYTHON_LIBRARY_NAME
35-
python
36-
CACHE
37-
FILEPATH
38-
"The name of the boost python library to search for")
39-
endif ()
40-
find_package(Boost REQUIRED COMPONENTS ${BOOST_PYTHON_LIBRARY_NAME})
41-
42-
find_package (NUMPY REQUIRED)
43-
44-
# copy the variables to their final destination
45-
set(PYTHON2_EXECUTABLE ${PYTHON_EXECUTABLE} CACHE FILEPATH "Path to Python2 interpreter")
46-
set(PYTHON2_LIBRARY ${PYTHON_LIBRARY} CACHE PATH "Python2 library")
47-
set(PYTHON2_INCLUDE_DIR ${PYTHON_INCLUDE_DIR} CACHE PATH "Python2 include folder")
48-
set(PYTHON2_FIND_PACKAGE_MESSAGE_DETAILS_PythonLibs ${FIND_PACKAGE_MESSAGE_DETAILS_PythonLibs} CACHE STRING "")
49-
50-
set(PYTHON2_LIBRARIES ${PYTHON_LIBRARIES} PARENT_SCOPE)
51-
set(PYTHON2_NUMPY_INCLUDE_DIRS ${NUMPY_INCLUDE_DIRS} PARENT_SCOPE)
52-
set(PYTHON2_Boost_LIBRARIES ${Boost_LIBRARIES} PARENT_SCOPE)
53-
set(PYTHON2_Boost_INCLUDE_DIRS ${Boost_INCLUDE_DIRS} PARENT_SCOPE)
54-
set(PYTHON2_Boost_PYTHON_LIBRARY_RELEASE ${Boost_PYTHON_LIBRARY_RELEASE} PARENT_SCOPE)
55-
set(PYTHON2_INCLUDE_DIRS ${PYTHON_INCLUDE_DIRS} PARENT_SCOPE)
56-
57-
# to access the variables here we also need to set them in the local scope
58-
set(PYTHON2_LIBRARIES ${PYTHON_LIBRARIES} )
59-
set(PYTHON2_NUMPY_INCLUDE_DIRS ${NUMPY_INCLUDE_DIRS} )
60-
set(PYTHON2_Boost_LIBRARIES ${Boost_LIBRARIES} )
61-
set(PYTHON2_Boost_INCLUDE_DIRS ${Boost_INCLUDE_DIRS} )
62-
set(PYTHON2_Boost_PYTHON_LIBRARY_RELEASE ${Boost_PYTHON_LIBRARY_RELEASE} )
63-
set(PYTHON2_INCLUDE_DIRS ${PYTHON_INCLUDE_DIRS} )
64-
65-
# Remove cached variable to not confuse user
66-
unset(PYTHON_EXECUTABLE CACHE)
67-
unset(PYTHON_LIBRARY CACHE)
68-
unset(PYTHON_INCLUDE_DIR CACHE)
69-
unset(FIND_PACKAGE_MESSAGE_DETAILS_PythonLibs CACHE)
70-
endif(PYTHONINTERP_FOUND)
71-
72-
73-
include_directories (${PYTHON2_Boost_INCLUDE_DIRS} ${PYTHON2_NUMPY_INCLUDE_DIRS} ${PYTHON2_INCLUDE_DIRS})
74-
75-
add_library (casa_python
76-
Converters/PycArray.cc
77-
Converters/PycArrayNP.cc
78-
Converters/PycBasicData.cc
79-
Converters/PycExcp.cc
80-
Converters/PycImport.cc
81-
Converters/PycRecord.cc
82-
Converters/PycValueHolder.cc
83-
Converters/PycArray.h
84-
Converters/PycArrayComCC.h
85-
Converters/PycArrayComH.h
86-
Converters/PycArrayNP.h
87-
Converters/PycBasicData.h
88-
Converters/PycExcp.h
89-
Converters/PycRecord.h
90-
Converters/PycValueHolder.h
91-
Converters/PycArray.tcc
92-
)
93-
94-
target_link_libraries (casa_python casa_casa ${PYTHON2_Boost_LIBRARIES} ${PYTHON2_LIBRARIES} ${CASACORE_ARCH_LIBS})
95-
96-
install (TARGETS casa_python
97-
RUNTIME DESTINATION bin
98-
LIBRARY DESTINATION lib${LIB_SUFFIX}
99-
ARCHIVE DESTINATION lib${LIB_SUFFIX}
100-
LIBRARY PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE
101-
)
102-
103-
install (FILES
104-
Converters/PycArray.h
105-
Converters/PycArrayComCC.h
106-
Converters/PycArrayComH.h
107-
Converters/PycArrayNP.h
108-
Converters/PycBasicData.h
109-
Converters/PycExcp.h
110-
Converters/PycRecord.h
111-
Converters/PycValueHolder.h
112-
Converters/PycArray.tcc
113-
DESTINATION include/casacore/python/Converters
114-
)
115-
116-
install (FILES
117-
Converters.h
118-
DESTINATION include/casacore/python
119-
)
120-
121-
add_subdirectory (Converters/test ${EXCL_ALL})

0 commit comments

Comments
 (0)