From d95adc14f04ffc1af2a9b09152b9d44205741a4f Mon Sep 17 00:00:00 2001 From: Sasha Rahlin Date: Fri, 23 Aug 2024 14:25:58 -0500 Subject: [PATCH] Add a CMake option for adjusting the build for python wheels --- CMakeLists.txt | 10 +++++++++- cmake/Spt3gBoostPython.cmake | 4 +++- core/CMakeLists.txt | 25 +++++++++++++++---------- examples/CMakeLists.txt | 6 ++++-- 4 files changed, 31 insertions(+), 14 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3c101c52..f94711f4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,6 +17,8 @@ if(NOT CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose the type of build, options are: None(CMAKE_CXX_FLAGS or CMAKE_C_FLAGS used) Debug Release RelWithDebInfo MinSizeRel" FORCE) endif(NOT CMAKE_BUILD_TYPE) +option(BUILD_WHEELS "Configure build for python wheels") + # Work around stupid broken Red Hat systems set(CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES "") @@ -61,6 +63,8 @@ target_compile_options(spt3g INTERFACE -Wno-unknown-warning-option -Wno-unused - # Fix bugs in GCC 4.4's strict aliasing code by turning it off target_compile_options(spt3g INTERFACE -fno-strict-aliasing) +set(CMAKE_POSITION_INDEPENDENT_CODE ON) + # Boost bits we need set(Boost_USE_STATIC_LIBS OFF) set(Boost_USE_MULTITHREADED ON) @@ -76,7 +80,11 @@ if(Boost_VERSION VERSION_GREATER 1.79) endif() target_include_directories(spt3g INTERFACE ${Boost_INCLUDE_DIR} ${Python_INCLUDE_DIRS}) -target_link_libraries(spt3g INTERFACE pthread ${Boost_LIBRARIES} ${Python_LIBRARIES}) +if(BUILD_WHEELS) + target_link_libraries(spt3g INTERFACE pthread ${Boost_LIBRARIES} Python::Module) +else() + target_link_libraries(spt3g INTERFACE pthread ${Boost_LIBRARIES} ${Python_LIBRARIES}) +endif() # Work around yet more bugs in GCC 4.4, this time with C++ 11 support # Also increase maximum number of arguments in python bindings diff --git a/cmake/Spt3gBoostPython.cmake b/cmake/Spt3gBoostPython.cmake index 34698316..794ebdb6 100644 --- a/cmake/Spt3gBoostPython.cmake +++ b/cmake/Spt3gBoostPython.cmake @@ -1,6 +1,8 @@ # Locate Python -if(${CMAKE_VERSION} VERSION_GREATER_EQUAL 3.12) +if(BUILD_WHEELS) + find_package(Python COMPONENTS Interpreter Development.Module) +elseif(${CMAKE_VERSION} VERSION_GREATER_EQUAL 3.12) find_package(Python COMPONENTS Interpreter Development) else() find_package(PythonInterp) diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt index 1e11241b..a28f6302 100644 --- a/core/CMakeLists.txt +++ b/core/CMakeLists.txt @@ -1,12 +1,15 @@ # Build a minimal library with a constant name which can be loaded as a python # module with a plain `import`, which can then load other modules which are # native libraries with normal names. -add_library(dload SHARED src/dload.c) -set_target_properties(dload PROPERTIES PREFIX "") -set_target_properties(dload PROPERTIES SUFFIX ".so") +if(BUILD_WHEELS) + Python_add_library(dload MODULE src/dload.c) +else() + set_target_properties(dload PROPERTIES PREFIX "") + set_target_properties(dload PROPERTIES SUFFIX ".so") + target_include_directories(dload PRIVATE ${Python_INCLUDE_DIRS}) + target_link_libraries(dload PUBLIC ${Python_LIBRARIES}) +endif() install(TARGETS dload DESTINATION ${PYTHON_MODULE_DIR}/spt3g) -target_include_directories(dload PRIVATE ${Python_INCLUDE_DIRS}) -target_link_libraries(dload PUBLIC ${Python_LIBRARIES}) # OS X has a broken implementatin of pthreads. if(APPLE) @@ -105,8 +108,10 @@ add_spt3g_test(pipelineinfo) add_spt3g_test(quaternions) add_spt3g_test(timesample) -add_spt3g_test_program(test - SOURCE_FILES - ${CMAKE_CURRENT_SOURCE_DIR}/tests/G3TimestreamTest.cxx - ${CMAKE_CURRENT_SOURCE_DIR}/tests/G3TimestreamMapTest.cxx - USE_PROJECTS core) +if(NOT BUILD_WHEELS) + add_spt3g_test_program(test + SOURCE_FILES + ${CMAKE_CURRENT_SOURCE_DIR}/tests/G3TimestreamTest.cxx + ${CMAKE_CURRENT_SOURCE_DIR}/tests/G3TimestreamMapTest.cxx + USE_PROJECTS core) +endif() diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 2e588a9e..7182fc70 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -1,2 +1,4 @@ -add_executable(cppexample cppexample.cxx) -target_link_libraries(cppexample core) +if(NOT BUILD_WHEELS) + add_executable(cppexample cppexample.cxx) + target_link_libraries(cppexample core) +endif()