Back to home page

EIC code displayed by LXR

 
 

    


Warning, /acts/Detray/cmake/detray-functions.cmake is written in an unsupported language. File is not indexed.

0001 # This file is part of the ACTS project.
0002 #
0003 # Copyright (C) 2016 CERN for the benefit of the ACTS project
0004 #
0005 # This Source Code Form is subject to the terms of the Mozilla Public
0006 # License, v. 2.0. If a copy of the MPL was not distributed with this
0007 # file, You can obtain one at https://mozilla.org/MPL/2.0/.
0008 
0009 # CMake include(s).
0010 include(CMakeParseArguments)
0011 
0012 # Helper function to create and set the `--keep` flag on CUDA targets.
0013 function(detray_add_cuda_artifact_dir_to_target target)
0014     set(cuda_keep_dir
0015         "${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/cuda_artifacts/${target}/"
0016     )
0017     add_custom_target(
0018         ${target}_cuda_artifact_mkdir
0019         COMMAND ${CMAKE_COMMAND} -E make_directory ${cuda_keep_dir}
0020     )
0021     add_dependencies(${target} ${target}_cuda_artifact_mkdir)
0022     target_compile_options(
0023         ${target}
0024         PRIVATE $<$<COMPILE_LANGUAGE:CUDA>:--keep --keep-dir ${cuda_keep_dir}>
0025     )
0026 endfunction(detray_add_cuda_artifact_dir_to_target)
0027 
0028 # Helper function for setting up the detray libraries.
0029 #
0030 # Usage: detray_add_library( detray_core core "header1.hpp"... )
0031 #
0032 function(detray_add_library fullname basename)
0033     # Create the library.
0034     add_library(${fullname} INTERFACE ${ARG_UNPARSED_ARGUMENTS})
0035 
0036     # Set up how clients should find its headers.
0037     target_include_directories(
0038         ${fullname}
0039         INTERFACE
0040             $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
0041             $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
0042     )
0043 
0044     # Make sure that the library is available as "detray::${basename}" in every
0045     # situation.
0046     set_target_properties(${fullname} PROPERTIES EXPORT_NAME ${basename})
0047     add_library(detray::${basename} ALIAS ${fullname})
0048 
0049     # Set up the installation of the library and its headers.
0050     install(
0051         TARGETS ${fullname}
0052         EXPORT detray-exports
0053         LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
0054         ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
0055         RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
0056     )
0057     install(
0058         DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/include/"
0059         DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
0060         OPTIONAL
0061     )
0062 endfunction(detray_add_library)
0063 
0064 # Helper function testing the detray public headers.
0065 #
0066 # It can be used to test that public headers would include everything
0067 # that they need to work, and that the CMake library targets would take
0068 # care of declaring all of their dependencies correctly for the public
0069 # headers to work.
0070 #
0071 # Usage: detray_test_public_headers( detray_core
0072 #                                    include/header1.hpp ... )
0073 #
0074 function(detray_test_public_headers library)
0075     # If testing is not turned on, don't do anything.
0076     if((NOT BUILD_TESTING) OR (NOT DETRAY_BUILD_TESTING))
0077         return()
0078     endif()
0079 
0080     # All arguments are treated as header file names.
0081     foreach(_headerName ${ARGN})
0082         # Make the header filename into a "string".
0083         string(REPLACE "/" "_" _headerNormName "${_headerName}")
0084         string(REPLACE "." "_" _headerNormName "${_headerNormName}")
0085 
0086         # Write a small source file that would test that the public
0087         # header can be used as-is.
0088         set(_testFileName
0089             "${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/test_${library}_${_headerNormName}.cpp"
0090         )
0091         if(NOT EXISTS "${_testFileName}")
0092             file(
0093                 WRITE "${_testFileName}"
0094                 "#include \"${_headerName}\"\n"
0095                 "int main() { return 0; }"
0096             )
0097         endif()
0098 
0099         # Set up an executable that would build it. But hide it, don't put it
0100         # into ${CMAKE_BINARY_DIR}/bin.
0101         add_executable("test_${library}_${_headerNormName}" "${_testFileName}")
0102         target_link_libraries(
0103             "test_${library}_${_headerNormName}"
0104             PRIVATE ${library}
0105         )
0106         set_target_properties(
0107             "test_${library}_${_headerNormName}"
0108             PROPERTIES
0109                 RUNTIME_OUTPUT_DIRECTORY
0110                     "${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}"
0111         )
0112     endforeach()
0113 endfunction(detray_test_public_headers)
0114 
0115 # Helper function for setting up the detray executables.
0116 #
0117 # The detray executables are *not* installed with the project, as they are only
0118 # used for testing / benchmarking the code. Clients of detray do not need them.
0119 #
0120 # Usage: detray_add_executable( foo bar.cpp
0121 #                               LINK_LIBRARIES detray::core )
0122 #
0123 function(detray_add_executable name)
0124     # Parse the function's options.
0125     cmake_parse_arguments(ARG "" "" "LINK_LIBRARIES" ${ARGN})
0126 
0127     # Create the executable.
0128     set(exe_name "detray_${name}")
0129     add_executable(${exe_name} ${ARG_UNPARSED_ARGUMENTS})
0130     if(ARG_LINK_LIBRARIES)
0131         target_link_libraries(${exe_name} PRIVATE ${ARG_LINK_LIBRARIES})
0132     endif()
0133 
0134     detray_add_cuda_artifact_dir_to_target(${exe_name})
0135 endfunction(detray_add_executable)
0136 
0137 # Helper function for setting up the detray tests.
0138 #
0139 # Usage: detray_add_test( core source1.cpp source2.cpp
0140 #                         LINK_LIBRARIES detray::core )
0141 #
0142 function(detray_add_test name)
0143     # Parse the function's options.
0144     cmake_parse_arguments(ARG "" "" "LINK_LIBRARIES" ${ARGN})
0145 
0146     # Create the test executable.
0147     set(test_exe_name "detray_${name}")
0148     add_executable(${test_exe_name} ${ARG_UNPARSED_ARGUMENTS})
0149     if(ARG_LINK_LIBRARIES)
0150         target_link_libraries(${test_exe_name} PRIVATE ${ARG_LINK_LIBRARIES})
0151     endif()
0152 
0153     # Run tests with sanitizers
0154     if(DETRAY_ENABLE_SANITIZER)
0155         # Common flags
0156         set(SANITIZER_FLAGS
0157             "-fsanitize=address,undefined,pointer-compare,pointer-subtract,float-divide-by-zero"
0158         )
0159 
0160         # Extra flags for clang
0161         if("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
0162             set(SANITIZER_FLAGS
0163                 "${SANITIZER_FLAGS},leak,integer,nullability,implicit-conversion,local-bounds"
0164             )
0165         endif()
0166 
0167         target_compile_options(${test_exe_name} PUBLIC ${SANITIZER_FLAGS})
0168         target_link_options(${test_exe_name} PUBLIC ${SANITIZER_FLAGS})
0169 
0170         # Clean up
0171         unset(SANITIZER_FLAGS)
0172     endif()
0173 
0174     # Run the executable as the test.
0175     add_test(NAME ${test_exe_name} COMMAND ${test_exe_name})
0176 
0177     # Set all properties for the test.
0178     set_tests_properties(
0179         ${test_exe_name}
0180         PROPERTIES ENVIRONMENT DETRAY_TEST_DATA_DIR=${PROJECT_SOURCE_DIR}/data/
0181     )
0182 
0183     detray_add_cuda_artifact_dir_to_target(${test_exe_name})
0184 endfunction(detray_add_test)
0185 
0186 # Helper function to set up a unit test
0187 #
0188 # Usage: See detray_add_test
0189 #
0190 function(detray_add_unit_test name)
0191     detray_add_test(unit_test_${name} ${ARGN})
0192 endfunction(detray_add_unit_test)
0193 
0194 # Helper function to set up an integration test
0195 #
0196 # Usage: See detray_add_test
0197 #
0198 function(detray_add_integration_test name)
0199     detray_add_test(integration_test_${name} ${ARGN})
0200 endfunction(detray_add_integration_test)
0201 
0202 # Helper function for setting up the detray tutorials.
0203 #
0204 # Usage: detray_add_tutorial( core source1.cpp source2.cpp
0205 #                             LINK_LIBRARIES detray::core )
0206 #
0207 function(detray_add_tutorial name)
0208     # Parse the function's options.
0209     cmake_parse_arguments(ARG "" "" "LINK_LIBRARIES" ${ARGN})
0210 
0211     # Create the tutorial executable.
0212     set(tutorial_exe_name "detray_tutorial_${name}")
0213     add_executable(${tutorial_exe_name} ${ARG_UNPARSED_ARGUMENTS})
0214     if(ARG_LINK_LIBRARIES)
0215         target_link_libraries(
0216             ${tutorial_exe_name}
0217             PRIVATE ${ARG_LINK_LIBRARIES}
0218         )
0219     endif()
0220 
0221     detray_add_cuda_artifact_dir_to_target(${tutorial_exe_name})
0222 endfunction(detray_add_tutorial)
0223 
0224 # Helper function for adding individual flags to "flag variables".
0225 #
0226 # Usage: detray_add_flag( CMAKE_CXX_FLAGS "-Wall" )
0227 #
0228 function(detray_add_flag name value)
0229     # Escape special characters in the value:
0230     set(matchedValue "${value}")
0231     foreach(
0232         c
0233         "*"
0234         "."
0235         "^"
0236         "$"
0237         "+"
0238         "?"
0239     )
0240         string(REPLACE "${c}" "\\${c}" matchedValue "${matchedValue}")
0241     endforeach()
0242 
0243     # Check if the variable already has this value in it:
0244     if("${${name}}" MATCHES "${matchedValue}")
0245         return()
0246     endif()
0247 
0248     # If not, then let's add it now:
0249     set(${name} "${${name}} ${value}" PARENT_SCOPE)
0250 endfunction(detray_add_flag)