Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmake-rdc: add support for MODULE library. #848

Merged
merged 2 commits into from
Jul 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 40 additions & 34 deletions cmake/CeleritasLibrary.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -131,25 +131,25 @@ define_property(TARGET PROPERTY CELERITAS_CUDA_OBJECT_LIBRARY
##############################################################################
# Separate the OPTIONS out from the sources
#
macro(CUDA_GET_SOURCES_AND_OPTIONS _sources _cmake_options _options)
macro(cuda_get_sources_and_options _sources _cmake_options _options)
set( ${_sources} )
set( ${_cmake_options} )
set( ${_options} )
set( _found_options FALSE )
foreach(arg ${ARGN})
if("x${arg}" STREQUAL "xOPTIONS")
if(arg STREQUAL "OPTIONS")
set( _found_options TRUE )
elseif(
"x${arg}" STREQUAL "xWIN32" OR
"x${arg}" STREQUAL "xMACOSX_BUNDLE" OR
"x${arg}" STREQUAL "xEXCLUDE_FROM_ALL" OR
"x${arg}" STREQUAL "xSTATIC" OR
"x${arg}" STREQUAL "xSHARED" OR
"x${arg}" STREQUAL "xMODULE"
arg STREQUAL "WIN32" OR
arg STREQUAL "MACOSX_BUNDLE" OR
arg STREQUAL "EXCLUDE_FROM_ALL" OR
arg STREQUAL "STATIC" OR
arg STREQUAL "SHARED" OR
arg STREQUAL "MODULE"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe simplify to arg MATCHES "^[A-Z_]+$"? All source files will have an extension.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I could do that .. note that this code is literally inspired by Kitware's code in FindCUDA.mk: Kitware/CMake@b8b53db/Modules/FindCUDA.cmake#L1327

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The FindCUDA is very old but you have a point... I'm just concerned about more library arguments being added by kitware.

)
list(APPEND ${_cmake_options} ${arg})
else()
if ( _found_options )
if( _found_options )
list(APPEND ${_options} ${arg})
else()
# Assume this is a file
Expand All @@ -168,11 +168,11 @@ function(celeritas_sources_contains_cuda var)
set(_result)
foreach(_source ${ARGN})
get_source_file_property(_iscudafile "${_source}" LANGUAGE)
if ("x${_iscudafile}" STREQUAL "xCUDA")
if(_iscudafile STREQUAL "CUDA")
list(APPEND _result "${_source}")
elseif(NOT _iscudafile)
get_filename_component(_ext "${_source}" LAST_EXT)
if("${_ext}" STREQUAL ".cu")
if(_ext STREQUAL ".cu")
list(APPEND _result "${_source}")
endif()
endif()
Expand Down Expand Up @@ -237,7 +237,7 @@ endfunction()
#
function(celeritas_rdc_add_library target)

CUDA_GET_SOURCES_AND_OPTIONS(_sources _cmake_options _options ${ARGN})
cuda_get_sources_and_options(_sources _cmake_options _options ${ARGN})

celeritas_sources_contains_cuda(_cuda_sources ${_sources})

Expand Down Expand Up @@ -273,14 +273,19 @@ function(celeritas_rdc_add_library target)
set(_lib_requested_type "SHARED")
set(_cudaruntime_requested_type "Shared")
set(_staticsuf "_static")
if((NOT BUILD_SHARED_LIBS AND NOT _ADDLIB_PARSE_SHARED)
if((NOT BUILD_SHARED_LIBS AND NOT _ADDLIB_PARSE_SHARED AND NOT _ADDLIB_PARSE_MODULE)
OR _ADDLIB_PARSE_STATIC)
set(_lib_requested_type "STATIC")
set(_cudaruntime_requested_type "Static")
set(_staticsuf "")
endif()
if(_ADDLIB_PARSE_MODULE)
message(FATAL_ERROR "celeritas_rdc_add_library does not support MODULE library containing device code")
add_library(${target} ${ARGN})
set_target_properties(${target} PROPERTIES
CUDA_SEPARABLE_COMPILATION ON
CUDA_RUNTIME_LIBRARY ${_cudaruntime_requested_type}
)
return()
endif()
if(_ADDLIB_PARSE_OBJECT)
message(FATAL_ERROR "celeritas_rdc_add_library does not support OBJECT library")
Expand All @@ -293,7 +298,7 @@ function(celeritas_rdc_add_library target)
CUDA_SEPARABLE_COMPILATION ON
CUDA_RUNTIME_LIBRARY ${_cudaruntime_requested_type}
)
if("x${_lib_requested_type}" STREQUAL "xSHARED")
if(_lib_requested_type STREQUAL "SHARED")
list(APPEND _object_props
POSITION_INDEPENDENT_CODE ON
)
Expand Down Expand Up @@ -432,7 +437,7 @@ endfunction()
# (static, middle, final) libraries needed for a separatable CUDA library
#
function(celeritas_install subcommand firstarg)
if(NOT "x${subcommand}" STREQUAL "xTARGETS" OR NOT TARGET ${firstarg})
if(NOT subcommand STREQUAL "TARGETS" OR NOT TARGET ${firstarg})
install(${ARGV})
return()
endif()
Expand All @@ -446,14 +451,14 @@ function(celeritas_install subcommand firstarg)
foreach(_toinstall ${_targets})
celeritas_strip_alias(_prop_target ${_toinstall})
get_target_property(_lib_target_type ${_prop_target} TYPE)
if(NOT "x${_lib_target_type}" STREQUAL "xINTERFACE_LIBRARY")
if(NOT _lib_target_type STREQUAL "INTERFACE_LIBRARY")
get_target_property(_targettype ${_prop_target} CELERITAS_CUDA_LIBRARY_TYPE)
if(_targettype)
get_target_property(_target_final ${_prop_target} CELERITAS_CUDA_FINAL_LIBRARY)
get_target_property(_target_middle ${_prop_target} CELERITAS_CUDA_MIDDLE_LIBRARY)
get_target_property(_target_static ${_prop_target} CELERITAS_CUDA_STATIC_LIBRARY)
set(_toinstall ${_target_final} ${_target_middle} ${_target_static})
if(NOT "${_target_middle}" STREQUAL "${_target_static}")
if(NOT _target_middle STREQUAL _target_static)
# Avoid duplicate middle/static library for static builds
list(APPEND _toinstall ${_target_middle})
endif()
Expand All @@ -470,11 +475,11 @@ function(celeritas_depends_on OUTVARNAME lib potentialdepend)
if(TARGET ${lib} AND TARGET ${potentialdepend})
set(lib_link_libraries "")
get_target_property(_lib_target_type ${lib} TYPE)
if(NOT "x${_lib_target_type}" STREQUAL "xINTERFACE_LIBRARY")
if(NOT _lib_target_type STREQUAL "INTERFACE_LIBRARY")
get_target_property(lib_link_libraries ${lib} LINK_LIBRARIES)
endif()
foreach(linklib ${lib_link_libraries})
if("${linklib}" STREQUAL "${potentialdepend}")
if(linklib STREQUAL potentialdepend)
set(${OUTVARNAME} TRUE PARENT_SCOPE)
return()
endif()
Expand Down Expand Up @@ -503,7 +508,7 @@ endfunction()
# Return the middle/shared library of the target, if any.
macro(celeritas_get_library_middle_target outvar target)
get_target_property(_target_type ${target} TYPE)
if(NOT "x${_target_type}" STREQUAL "xINTERFACE_LIBRARY")
if(NOT _target_type STREQUAL "INTERFACE_LIBRARY")
get_target_property(${outvar} ${target} CELERITAS_CUDA_MIDDLE_LIBRARY)
else()
set(${outvar} ${target})
Expand Down Expand Up @@ -578,7 +583,7 @@ function(celeritas_find_final_library OUTLIST flat_dependency_list)
foreach(_lib ${_result})
if(TARGET ${_lib})
get_target_property(_lib_target_type ${_lib} TYPE)
if(NOT "x${_lib_target_type}" STREQUAL "xINTERFACE_LIBRARY")
if(NOT _lib_target_type STREQUAL "INTERFACE_LIBRARY")
get_target_property(_final_lib ${_lib} CELERITAS_CUDA_FINAL_LIBRARY)
if(_final_lib)
list(APPEND _final_result ${_final_lib})
Expand Down Expand Up @@ -607,7 +612,7 @@ function(celeritas_check_cuda_runtime OUTVAR library)
# case we probably need to 'error out'.
get_target_property(_cuda_library_type ${library} CELERITAS_CUDA_LIBRARY_TYPE)
get_target_property(_cuda_find_library ${library} CELERITAS_CUDA_FINAL_LIBRARY)
if("${_cuda_library_type}" STREQUAL "Shared")
if(_cuda_library_type STREQUAL "Shared")
set_target_properties(${library} PROPERTIES CUDA_RUNTIME_LIBRARY "Shared")
set(_runtime_setting "Shared")
elseif(NOT _cuda_find_library)
Expand Down Expand Up @@ -666,7 +671,8 @@ function(celeritas_target_link_libraries target)
celeritas_find_final_library(_finallibs "${_alldependencies}")

get_target_property(_target_type ${target} TYPE)
if("x${_target_type}" STREQUAL "xEXECUTABLE")
if(_target_type STREQUAL "EXECUTABLE"
OR _target_type STREQUAL "MODULE_LIBRARY")
list(LENGTH _finallibs _final_count)
if(_contains_cuda)
if(${_final_count} GREATER 0)
Expand All @@ -689,11 +695,11 @@ function(celeritas_target_link_libraries target)
get_target_property(_final_target_type ${target} TYPE)

get_target_property(_final_runtime ${_finallibs} CUDA_RUNTIME_LIBRARY)
if("${_final_runtime}" STREQUAL "Shared")
if(_final_runtime STREQUAL "Shared")
set_target_properties(${target} PROPERTIES CUDA_RUNTIME_LIBRARY "Shared")
endif()

if("x${_final_target_type}" STREQUAL "xSTATIC_LIBRARY")
if(_final_target_type STREQUAL "STATIC_LIBRARY")
# for static libraries we need to list the libraries a second time (to resolve symbol from the final library)
get_target_property(_current_link_libraries ${target} LINK_LIBRARIES)
set_property(TARGET ${target} PROPERTY LINK_LIBRARIES ${_current_link_libraries} ${_finallibs} ${_current_link_libraries} )
Expand Down Expand Up @@ -727,9 +733,9 @@ function(celeritas_target_link_libraries target)
celeritas_cuda_gather_dependencies(_flat_target_link_libraries ${_target_middle})
foreach(_lib ${_flat_target_link_libraries})
get_target_property(_lib_target_type ${_lib} TYPE)
if(NOT "x${_lib_target_type}" STREQUAL "xINTERFACE_LIBRARY")
if(NOT _lib_target_type STREQUAL "INTERFACE_LIBRARY")
celeritas_check_cuda_runtime(_lib_runtime_setting ${_lib})
if (NOT ${_need_to_use_shared_runtime} AND ${_lib_runtime_setting} STREQUAL "Shared")
if(NOT _need_to_use_shared_runtime AND _lib_runtime_setting STREQUAL "Shared")
set(_need_to_use_shared_runtime TRUE)
endif()
if(NOT _target_runtime_setting)
Expand All @@ -742,7 +748,7 @@ function(celeritas_target_link_libraries target)
set(_target_runtime_setting ${_lib_runtime_setting})
endif()
endif()
if (NOT _current_runtime_setting)
if(NOT _current_runtime_setting)
set_target_properties(${target} PROPERTIES CUDA_RUNTIME_LIBRARY ${_target_runtime_setting})
endif()
get_target_property(_libstatic ${_lib} CELERITAS_CUDA_STATIC_LIBRARY)
Expand All @@ -766,7 +772,7 @@ function(celeritas_target_link_libraries target)

if(_need_to_use_shared_runtime)
get_target_property(_current_runtime ${target} CUDA_RUNTIME_LIBRARY)
if(NOT "${_current_runtime}" STREQUAL "Shared")
if(NOT _current_runtime STREQUAL "Shared")
set_target_properties(${_target_middle} PROPERTIES CUDA_RUNTIME_LIBRARY "Shared")
set_target_properties(${_target_final} PROPERTIES CUDA_RUNTIME_LIBRARY "Shared")
set_target_properties(${_target_object} PROPERTIES CUDA_RUNTIME_LIBRARY "Shared")
Expand All @@ -782,13 +788,13 @@ function(celeritas_target_link_libraries target)
# Note: we might be able to move this to celeritas_target_link_libraries
CUDA_RESOLVE_DEVICE_SYMBOLS OFF
)
if(NOT ${_target_type} STREQUAL "EXECUTABLE")
if(NOT _target_type STREQUAL "EXECUTABLE")
get_target_property(_current_runtime ${target} CUDA_RUNTIME_LIBRARY)
if(NOT "${_current_runtime}" STREQUAL "Shared")
if(NOT _current_runtime STREQUAL "Shared")
set(_need_to_use_shared_runtime FALSE)
foreach(_lib ${_alldependencies})
celeritas_check_cuda_runtime(_runtime ${_lib})
if(${_runtime} STREQUAL "Shared")
if(_runtime STREQUAL "Shared")
set(_need_to_use_shared_runtime TRUE)
break()
endif()
Expand All @@ -815,7 +821,7 @@ function(celeritas_cuda_gather_dependencies outlist target)
endif()
celeritas_strip_alias(target ${target})
get_target_property(_target_type ${target} TYPE)
if(NOT "x${_target_type}" STREQUAL "xINTERFACE_LIBRARY")
if(NOT _target_type STREQUAL "INTERFACE_LIBRARY")
get_target_property(_target_link_libraries ${target} LINK_LIBRARIES)
if(_target_link_libraries)
foreach(_lib ${_target_link_libraries})
Expand Down