Skip to content

Commit

Permalink
build either static or shared by default (#496)
Browse files Browse the repository at this point in the history
* build either static or shared by default

* fixing tests with shared libs

* fixed jenkins
  • Loading branch information
DiamonDinoia authored Jul 23, 2024
1 parent 3e231e2 commit 8baaa97
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 94 deletions.
90 changes: 35 additions & 55 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,20 @@ option(FINUFFT_ENABLE_SANITIZERS "Whether to enable sanitizers, only effective f
option(FINUFFT_USE_OPENMP "Whether to use OpenMP for parallelization. If disabled, the finufft library will be single threaded. This does not affect the choice of FFTW library." ON)
option(FINUFFT_USE_CUDA "Whether to build CUDA accelerated FINUFFT library (libcufinufft). This is completely independent of the main FINUFFT library" OFF)
option(FINUFFT_USE_CPU "Whether to build the ordinary FINUFFT library (libfinufft)." ON)
option(FINUFFT_STATIC_LINKING "Whether to link the static FINUFFT library (libfinufft_static)." ON)
option(FINUFFT_STATIC_LINKING "Whether to build the static FINUFFT library." ON)
option(FINUFFT_BUILD_DEVEL "Whether to build development executables" OFF)
option(FINUFFT_USE_DUCC0 "Whether to use DUCC0 (instead of FFTW) for CPU FFTs" OFF)
option(FINUFFT_BUILD_DEVEL "Whether to build developement executables" OFF)
option(FINUFFT_ENABLE_INSTALL "Whether to enable installation of FINUFFT library" ON)
# sphinx tag (don't remove): @cmake_opts_end


if (NOT FINUFFT_STATIC_LINKING)
set (FINUFFT_SHARED_LINKING ON)
else ()
set (FINUFFT_SHARED_LINKING OFF)
endif ()

if (FINUFFT_USE_CPU)
# suppress Windows warnings about "unsafe" functions
if (WIN32)
Expand Down Expand Up @@ -144,31 +151,25 @@ endfunction()

# Utility function to link static/dynamic lib
function(finufft_link_test target)

if (FINUFFT_USE_DUCC0)
target_compile_definitions(${target} PRIVATE FINUFFT_USE_DUCC0)
endif ()

if (FINUFFT_STATIC_LINKING)
target_link_libraries(${target} PRIVATE finufft_static)
if(FINUFFT_USE_OPENMP)
target_link_libraries(${target} PRIVATE OpenMP::OpenMP_CXX)
if(WIN32)
target_link_options(${target} PRIVATE ${OpenMP_CXX_FLAGS})
endif()
endif()
else()
target_link_libraries(${target} PRIVATE finufft)
target_link_libraries(${target} PRIVATE finufft ${FINUFFT_FFTLIBS})
if(FINUFFT_USE_OPENMP)
target_link_libraries(${target} PRIVATE OpenMP::OpenMP_CXX)
if(WIN32)
target_compile_definitions(${target} PRIVATE FINUFFT_DLL)
target_link_options(${target} PRIVATE ${OpenMP_CXX_FLAGS})
if (FINUFFT_SHARED_LINKING)
target_compile_definitions(${target} PRIVATE FINUFFT_DLL)
endif()
endif()
endif()
enable_asan(${target})
endfunction()

# Utility function to set finufft compilation options.
function(set_finufft_options target)
set_property(TARGET ${target} PROPERTY POSITION_INDEPENDENT_CODE ON)
set_property(TARGET ${target} PROPERTY POSITION_INDEPENDENT_CODE ${FINUFFT_SHARED_LINKING})
target_compile_features(${target} PRIVATE cxx_std_17)

enable_asan(${target})
Expand Down Expand Up @@ -210,8 +211,6 @@ function(set_finufft_options target)
target_include_directories(${target} PUBLIC ${FFTW_INCLUDE_DIR})
endif ()
endif ()
# target_link_libraries(${target} PRIVATE xsimd)
# target_link_libraries(${target} PUBLIC ${FINUFFT_FFTW_LIBRARIES})
endfunction()

if (FINUFFT_USE_CPU)
Expand All @@ -223,59 +222,44 @@ if (FINUFFT_USE_CPU)

add_library(finufft_f32 OBJECT ${FINUFFT_PRECISION_DEPENDENT_SOURCES})
target_compile_definitions(finufft_f32 PRIVATE SINGLE)
if (WIN32 AND FINUFFT_SHARED_LINKING)
target_compile_definitions(finufft_f32 PRIVATE dll_EXPORTS FINUFFT_DLL)
endif ()
set_finufft_options(finufft_f32)
target_link_libraries(finufft_f32 PUBLIC ${FINUFFT_FFTLIBS})
target_link_libraries(finufft_f32 PRIVATE xsimd)

add_library(finufft_f64 OBJECT ${FINUFFT_PRECISION_DEPENDENT_SOURCES})
target_compile_definitions(finufft_f64 PRIVATE)
if (WIN32 AND FINUFFT_SHARED_LINKING)
target_compile_definitions(finufft_f64 PRIVATE dll_EXPORTS FINUFFT_DLL)
endif ()
set_finufft_options(finufft_f64)
target_link_libraries(finufft_f64 PUBLIC ${FINUFFT_FFTLIBS})
target_link_libraries(finufft_f64 PRIVATE xsimd)
if (WIN32)
add_library(finufft_f32_dll OBJECT ${FINUFFT_PRECISION_DEPENDENT_SOURCES})
target_compile_definitions(finufft_f32_dll PRIVATE SINGLE dll_EXPORTS FINUFFT_DLL)
set_finufft_options(finufft_f32_dll)
target_link_libraries(finufft_f32_dll PUBLIC ${FINUFFT_FFTLIBS})
target_link_libraries(finufft_f32_dll PRIVATE xsimd)

add_library(finufft_f64_dll OBJECT ${FINUFFT_PRECISION_DEPENDENT_SOURCES})
target_compile_definitions(finufft_f64_dll PRIVATE dll_EXPORTS FINUFFT_DLL)
set_finufft_options(finufft_f64_dll)
target_link_libraries(finufft_f64_dll PUBLIC ${FINUFFT_FFTLIBS})
target_link_libraries(finufft_f64_dll PRIVATE xsimd)
endif ()

add_library(finufft SHARED src/utils_precindep.cpp contrib/legendre_rule_fast.cpp)
target_compile_definitions(finufft PRIVATE dll_EXPORTS FINUFFT_DLL)
if (FINUFFT_SHARED_LINKING)
add_library(finufft SHARED src/utils_precindep.cpp contrib/legendre_rule_fast.cpp)
if (WIN32)
target_compile_definitions(finufft PRIVATE dll_EXPORTS FINUFFT_DLL)
endif()
else()
add_library(finufft STATIC src/utils_precindep.cpp contrib/legendre_rule_fast.cpp)
endif()
target_link_libraries(finufft PRIVATE finufft_f32 finufft_f64)
set_finufft_options(finufft)
if (NOT WIN32)
target_link_libraries(finufft PUBLIC finufft_f32 finufft_f64)
else ()
target_link_libraries(finufft PUBLIC finufft_f32_dll finufft_f64_dll)
endif ()

# windows does not have a math library, so we need to exclude it
if (NOT WIN32)
target_link_libraries(finufft PUBLIC m)
endif ()
target_include_directories(finufft PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>)
target_include_directories(finufft SYSTEM INTERFACE $<INSTALL_INTERFACE:${CMAKE_INSTALL_PREFIX}/include>)

add_library(finufft_static STATIC src/utils_precindep.cpp contrib/legendre_rule_fast.cpp)
set_finufft_options(finufft_static)
target_link_libraries(finufft_static PUBLIC finufft_f32 finufft_f64)
# windows does not have a math library, so we need to exclude it
if (NOT WIN32)
target_link_libraries(finufft_static PUBLIC m)
endif ()
target_include_directories(finufft_static PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>)
target_include_directories(finufft_static SYSTEM INTERFACE $<INSTALL_INTERFACE:${CMAKE_INSTALL_PREFIX}/include>)

if (FINUFFT_ENABLE_INSTALL)
file(GLOB FINUFFT_PUBLIC_HEADERS "${CMAKE_CURRENT_SOURCE_DIR}/include/finufft*.h")
set_target_properties(finufft PROPERTIES PUBLIC_HEADER "${FINUFFT_PUBLIC_HEADERS}")
endif()
list(APPEND INSTALL_TARGETS finufft finufft_static)
list(APPEND INSTALL_TARGETS finufft)
endif ()

if (FINUFFT_USE_CUDA)
Expand All @@ -291,7 +275,7 @@ if (FINUFFT_USE_CUDA)
add_subdirectory(perftest/cuda)
endif ()

list(APPEND INSTALL_TARGETS cufinufft cufinufft_static)
list(APPEND INSTALL_TARGETS cufinufft)
endif ()

# Add tests defined in their own directory
Expand Down Expand Up @@ -343,11 +327,7 @@ message(STATUS " FINUFFT_FFTW_LIBRARIES: ${FINUFFT_FFTW_LIBRARIES}")
message(STATUS " FINUFFT_ARCH_FLAGS: ${FINUFFT_ARCH_FLAGS}")

if (FINUFFT_USE_CPU)
if (FINUFFT_STATIC_LINKING)
get_target_property(FINUFFT_COMPILE_OPTIONS finufft_static COMPILE_OPTIONS)
else ()
get_target_property(FINUFFT_COMPILE_OPTIONS finufft COMPILE_OPTIONS)
endif ()
get_target_property(FINUFFT_COMPILE_OPTIONS finufft COMPILE_OPTIONS)
message("Compile options for finufft: ${MY_COMPILE_OPTIONS}")
endif ()

Expand Down
3 changes: 2 additions & 1 deletion Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ pipeline {
-DFINUFFT_USE_CPU=OFF \
-DFINUFFT_BUILD_TESTS=ON \
-DCMAKE_CUDA_ARCHITECTURES="$cuda_arch" \
-DBUILD_TESTING=ON
-DBUILD_TESTING=ON \
-DFINUFFT_STATIC_LINKING=OFF
cd build
make -j4
'''
Expand Down
2 changes: 1 addition & 1 deletion cmake/setupFFTW.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ if (FINUFFT_FFTW_LIBRARIES STREQUAL DEFAULT OR FINUFFT_FFTW_LIBRARIES STREQUAL D
endif ()

foreach (element IN LISTS FINUFFT_FFTW_LIBRARIES)
set_property(TARGET ${element} PROPERTY POSITION_INDEPENDENT_CODE ON)
set_property(TARGET ${element} PROPERTY POSITION_INDEPENDENT_CODE ${FINUFFT_SHARED_LINKING})
endforeach ()

else ()
Expand Down
2 changes: 1 addition & 1 deletion examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ endforeach()
if(FINUFFT_USE_OPENMP)
foreach(EXAMPLE ${EXAMPLES_OPENMP})
add_executable(${EXAMPLE} ${EXAMPLE}.cpp)
target_link_libraries(${EXAMPLE} PRIVATE finufft)
target_link_libraries(${EXAMPLE} PRIVATE finufft OpenMP::OpenMP_CXX)
enable_asan(${EXAMPLE})
endforeach()
endif()
Expand Down
2 changes: 1 addition & 1 deletion examples/cuda/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ foreach(srcfile ${example_src})
get_filename_component(executable ${executable} NAME)
add_executable(${executable} ${srcfile})
target_include_directories(${executable} PUBLIC ${CUFINUFFT_INCLUDE_DIRS})
target_link_libraries(${executable} cufinufft_static)
target_link_libraries(${executable} cufinufft)
endforeach()
2 changes: 1 addition & 1 deletion python/finufft/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ build-dir = "build/{wheel_tag}"
# Tell skbuild to look for the CMakeLists.txt file two directories up.
cmake.source-dir = "../../"
cmake.targets = ["finufft"]
cmake.define = {"FINUFFT_BUILD_PYTHON" = "ON", "FINUFFT_ENABLE_INSTALL" = "OFF"}
cmake.define = {"FINUFFT_BUILD_PYTHON" = "ON", "FINUFFT_ENABLE_INSTALL" = "OFF", "FINUFFT_STATIC_LINKING" = "OFF"}

wheel.packages = ["finufft"]

Expand Down
70 changes: 36 additions & 34 deletions src/cuda/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@ set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CUDA_SEPARABLE_COMPILATION ON)

set(PRECISION_INDEPENDENT_SRC precision_independent.cu utils.cpp
${PROJECT_SOURCE_DIR}/contrib/legendre_rule_fast.cpp)
${PROJECT_SOURCE_DIR}/contrib/legendre_rule_fast.cpp)
set(PRECISION_DEPENDENT_SRC
spreadinterp.cpp
1d/cufinufft1d.cu 1d/spread1d_wrapper.cu 1d/interp1d_wrapper.cu
2d/cufinufft2d.cu 2d/spread2d_wrapper.cu 2d/interp2d_wrapper.cu
3d/spread3d_wrapper.cu 3d/interp3d_wrapper.cu 3d/cufinufft3d.cu
memtransfer_wrapper.cu deconvolve_wrapper.cu cufinufft.cu common.cu
spreadinterp.cpp
1d/cufinufft1d.cu 1d/spread1d_wrapper.cu 1d/interp1d_wrapper.cu
2d/cufinufft2d.cu 2d/spread2d_wrapper.cu 2d/interp2d_wrapper.cu
3d/spread3d_wrapper.cu 3d/interp3d_wrapper.cu 3d/cufinufft3d.cu
memtransfer_wrapper.cu deconvolve_wrapper.cu cufinufft.cu common.cu
)

set(CUFINUFFT_INCLUDE_DIRS
${PROJECT_SOURCE_DIR}/include
${PROJECT_SOURCE_DIR}/contrib
$<TARGET_PROPERTY:CUDA::cudart,INTERFACE_INCLUDE_DIRECTORIES>
$<TARGET_PROPERTY:CUDA::cufft,INTERFACE_INCLUDE_DIRECTORIES>
$<TARGET_PROPERTY:CUDA::nvToolsExt,INTERFACE_INCLUDE_DIRECTORIES>
${PROJECT_SOURCE_DIR}/include
${PROJECT_SOURCE_DIR}/contrib
$<TARGET_PROPERTY:CUDA::cudart,INTERFACE_INCLUDE_DIRECTORIES>
$<TARGET_PROPERTY:CUDA::cufft,INTERFACE_INCLUDE_DIRECTORIES>
$<TARGET_PROPERTY:CUDA::nvToolsExt,INTERFACE_INCLUDE_DIRECTORIES>
)
set(CUFINUFFT_INCLUDE_DIRS ${CUFINUFFT_INCLUDE_DIRS} PARENT_SCOPE)

Expand All @@ -30,29 +30,31 @@ add_library(cufinufft_objects OBJECT ${PRECISION_DEPENDENT_SRC})
target_include_directories(cufinufft_objects PUBLIC ${CUFINUFFT_INCLUDE_DIRS})
set_property(TARGET cufinufft_objects PROPERTY POSITION_INDEPENDENT_CODE ON)

add_library(cufinufft SHARED
$<TARGET_OBJECTS:cufinufft_common_objects>
$<TARGET_OBJECTS:cufinufft_objects>
)
target_link_libraries(cufinufft CUDA::cudart CUDA::cufft CUDA::nvToolsExt)
set_target_properties(
cufinufft PROPERTIES
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}"
)

add_library(cufinufft_static STATIC
$<TARGET_OBJECTS:cufinufft_common_objects>
$<TARGET_OBJECTS:cufinufft_objects>
)
if(WIN32)
target_link_libraries(cufinufft_static PUBLIC CUDA::cudart CUDA::cufft CUDA::nvToolsExt)
else()
target_link_libraries(cufinufft_static PUBLIC CUDA::cudart_static CUDA::cufft_static CUDA::nvToolsExt)
endif()
set_target_properties(
cufinufft_static PROPERTIES
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}"
)
if (FINUFFT_SHARED_LINKING)
add_library(cufinufft SHARED
$<TARGET_OBJECTS:cufinufft_common_objects>
$<TARGET_OBJECTS:cufinufft_objects>
)
set_target_properties(
cufinufft PROPERTIES
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}"
)
else ()
add_library(cufinufft STATIC
$<TARGET_OBJECTS:cufinufft_common_objects>
$<TARGET_OBJECTS:cufinufft_objects>
)
set_target_properties(
cufinufft PROPERTIES
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}"
)
endif ()

if (WIN32)
target_link_libraries(cufinufft PUBLIC CUDA::cudart CUDA::cufft CUDA::nvToolsExt)
else ()
target_link_libraries(cufinufft PUBLIC CUDA::cudart_static CUDA::cufft_static CUDA::nvToolsExt)
endif ()

file(GLOB CUFINUFFT_PUBLIC_HEADERS "${CMAKE_SOURCE_DIR}/include/cufinufft*.h")
set_target_properties(cufinufft PROPERTIES PUBLIC_HEADER "${CUFINUFFT_PUBLIC_HEADERS}")

0 comments on commit 8baaa97

Please sign in to comment.