From 06550f8d30aacfee359877a0c4976551d5ec9eae Mon Sep 17 00:00:00 2001 From: Gonzalo Mier Date: Thu, 29 Apr 2021 14:53:15 +0200 Subject: [PATCH] Update CMakeLists.txt to support Python2 easily --- CMakeLists.txt | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index bb2decd..7a5d5e1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,7 +4,7 @@ project(matplotlib_cpp LANGUAGES CXX) include(GNUInstallDirs) set(PACKAGE_NAME matplotlib_cpp) set(INSTALL_CONFIGDIR ${CMAKE_INSTALL_LIBDIR}/${PACKAGE_NAME}/cmake) - +set(USING_PYTHON3 True) # Library target add_library(matplotlib_cpp INTERFACE) @@ -17,19 +17,37 @@ target_compile_features(matplotlib_cpp INTERFACE cxx_std_11 ) # TODO: Use `Development.Embed` component when requiring cmake >= 3.18 -find_package(Python3 COMPONENTS Interpreter Development REQUIRED) -target_link_libraries(matplotlib_cpp INTERFACE - Python3::Python - Python3::Module -) -find_package(Python3 COMPONENTS NumPy) -if(Python3_NumPy_FOUND) +if(USING_PYTHON3) + find_package(Python3 COMPONENTS Interpreter Development REQUIRED) target_link_libraries(matplotlib_cpp INTERFACE - Python3::NumPy + Python3::Python + Python3::Module ) + find_package(Python3 COMPONENTS NumPy) + if(Python3_NumPy_FOUND) + target_link_libraries(matplotlib_cpp INTERFACE + Python3::NumPy + ) + else() + target_compile_definitions(matplotlib_cpp INTERFACE WITHOUT_NUMPY) + endif() else() - target_compile_definitions(matplotlib_cpp INTERFACE WITHOUT_NUMPY) + find_package(Python2 COMPONENTS Interpreter Development REQUIRED) + target_link_libraries(matplotlib_cpp INTERFACE + Python2::Python + Python2::Module + ) + find_package(Python2 COMPONENTS NumPy) + if(Python2_NumPy_FOUND) + target_link_libraries(matplotlib_cpp INTERFACE + Python2::NumPy + ) + else() + target_compile_definitions(matplotlib_cpp INTERFACE WITHOUT_NUMPY) + endif() endif() + + install( TARGETS matplotlib_cpp EXPORT install_targets