FindComputeCpp.cmake 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. #.rst:
  2. # FindComputeCpp
  3. #---------------
  4. #
  5. # Copyright 2016 Codeplay Software Ltd.
  6. #
  7. # Licensed under the Apache License, Version 2.0 (the "License");
  8. # you may not use these files except in compliance with the License.
  9. # You may obtain a copy of the License at
  10. #
  11. # http://www.apache.org/licenses/LICENSE-2.0
  12. #
  13. #
  14. # Unless required by applicable law or agreed to in writing, software
  15. # distributed under the License is distributed on an "AS IS" BASIS,
  16. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. # See the License for the specific language governing permissions and
  18. # limitations under the License.
  19. #########################
  20. # FindComputeCpp.cmake
  21. #########################
  22. #
  23. # Tools for finding and building with ComputeCpp.
  24. #
  25. # User must define COMPUTECPP_PACKAGE_ROOT_DIR pointing to the ComputeCpp
  26. # installation.
  27. #
  28. # Latest version of this file can be found at:
  29. # https://github.com/codeplaysoftware/computecpp-sdk
  30. # Require CMake version 3.2.2 or higher
  31. cmake_minimum_required(VERSION 3.2.2)
  32. # Check that a supported host compiler can be found
  33. if(CMAKE_COMPILER_IS_GNUCXX)
  34. # Require at least gcc 4.8
  35. if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.8)
  36. message(FATAL_ERROR
  37. "host compiler - Not found! (gcc version must be at least 4.8)")
  38. # Require the GCC dual ABI to be disabled for 5.1 or higher
  39. elseif (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 5.1)
  40. set(COMPUTECPP_DISABLE_GCC_DUAL_ABI "True")
  41. message(STATUS
  42. "host compiler - gcc ${CMAKE_CXX_COMPILER_VERSION} (note pre 5.1 gcc ABI enabled)")
  43. else()
  44. message(STATUS "host compiler - gcc ${CMAKE_CXX_COMPILER_VERSION}")
  45. endif()
  46. elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
  47. # Require at least clang 3.6
  48. if (${CMAKE_CXX_COMPILER_VERSION} VERSION_LESS 3.6)
  49. message(FATAL_ERROR
  50. "host compiler - Not found! (clang version must be at least 3.6)")
  51. else()
  52. message(STATUS "host compiler - clang ${CMAKE_CXX_COMPILER_VERSION}")
  53. endif()
  54. else()
  55. message(WARNING
  56. "host compiler - Not found! (ComputeCpp supports GCC and Clang, see readme)")
  57. endif()
  58. set(COMPUTECPP_64_BIT_DEFAULT ON)
  59. option(COMPUTECPP_64_BIT_CODE "Compile device code in 64 bit mode"
  60. ${COMPUTECPP_64_BIT_DEFAULT})
  61. mark_as_advanced(COMPUTECPP_64_BIT_CODE)
  62. # Find OpenCL package
  63. find_package(OpenCL REQUIRED)
  64. # Find ComputeCpp packagee
  65. if(NOT COMPUTECPP_PACKAGE_ROOT_DIR)
  66. message(FATAL_ERROR
  67. "ComputeCpp package - Not found! (please set COMPUTECPP_PACKAGE_ROOT_DIR")
  68. else()
  69. message(STATUS "ComputeCpp package - Found")
  70. endif()
  71. option(COMPUTECPP_PACKAGE_ROOT_DIR "Path to the ComputeCpp Package")
  72. # Obtain the path to compute++
  73. find_program(COMPUTECPP_DEVICE_COMPILER compute++ PATHS
  74. ${COMPUTECPP_PACKAGE_ROOT_DIR} PATH_SUFFIXES bin)
  75. if (EXISTS ${COMPUTECPP_DEVICE_COMPILER})
  76. mark_as_advanced(COMPUTECPP_DEVICE_COMPILER)
  77. message(STATUS "compute++ - Found")
  78. else()
  79. message(FATAL_ERROR "compute++ - Not found! (${COMPUTECPP_DEVICE_COMPILER})")
  80. endif()
  81. # Obtain the path to computecpp_info
  82. find_program(COMPUTECPP_INFO_TOOL computecpp_info PATHS
  83. ${COMPUTECPP_PACKAGE_ROOT_DIR} PATH_SUFFIXES bin)
  84. if (EXISTS ${COMPUTECPP_INFO_TOOL})
  85. mark_as_advanced(${COMPUTECPP_INFO_TOOL})
  86. message(STATUS "computecpp_info - Found")
  87. else()
  88. message(FATAL_ERROR "computecpp_info - Not found! (${COMPUTECPP_INFO_TOOL})")
  89. endif()
  90. # Obtain the path to the ComputeCpp runtime library
  91. find_library(COMPUTECPP_RUNTIME_LIBRARY ComputeCpp PATHS ${COMPUTECPP_PACKAGE_ROOT_DIR}
  92. HINTS ${COMPUTECPP_PACKAGE_ROOT_DIR}/lib PATH_SUFFIXES lib
  93. DOC "ComputeCpp Runtime Library" NO_DEFAULT_PATH)
  94. if (EXISTS ${COMPUTECPP_RUNTIME_LIBRARY})
  95. mark_as_advanced(COMPUTECPP_RUNTIME_LIBRARY)
  96. message(STATUS "libComputeCpp.so - Found")
  97. else()
  98. message(FATAL_ERROR "libComputeCpp.so - Not found!")
  99. endif()
  100. # Obtain the ComputeCpp include directory
  101. set(COMPUTECPP_INCLUDE_DIRECTORY ${COMPUTECPP_PACKAGE_ROOT_DIR}/include/)
  102. if (NOT EXISTS ${COMPUTECPP_INCLUDE_DIRECTORY})
  103. message(FATAL_ERROR "ComputeCpp includes - Not found!")
  104. else()
  105. message(STATUS "ComputeCpp includes - Found")
  106. endif()
  107. # Obtain the package version
  108. execute_process(COMMAND ${COMPUTECPP_INFO_TOOL} "--dump-version"
  109. OUTPUT_VARIABLE COMPUTECPP_PACKAGE_VERSION
  110. RESULT_VARIABLE COMPUTECPP_INFO_TOOL_RESULT OUTPUT_STRIP_TRAILING_WHITESPACE)
  111. if(NOT COMPUTECPP_INFO_TOOL_RESULT EQUAL "0")
  112. message(FATAL_ERROR "Package version - Error obtaining version!")
  113. else()
  114. mark_as_advanced(COMPUTECPP_PACKAGE_VERSION)
  115. message(STATUS "Package version - ${COMPUTECPP_PACKAGE_VERSION}")
  116. endif()
  117. # Obtain the device compiler flags
  118. execute_process(COMMAND ${COMPUTECPP_INFO_TOOL} "--dump-device-compiler-flags"
  119. OUTPUT_VARIABLE COMPUTECPP_DEVICE_COMPILER_FLAGS
  120. RESULT_VARIABLE COMPUTECPP_INFO_TOOL_RESULT OUTPUT_STRIP_TRAILING_WHITESPACE)
  121. if(NOT COMPUTECPP_INFO_TOOL_RESULT EQUAL "0")
  122. message(FATAL_ERROR "compute++ flags - Error obtaining compute++ flags!")
  123. else()
  124. mark_as_advanced(COMPUTECPP_COMPILER_FLAGS)
  125. message(STATUS "compute++ flags - ${COMPUTECPP_DEVICE_COMPILER_FLAGS}")
  126. endif()
  127. set(COMPUTECPP_DEVICE_COMPILER_FLAGS ${COMPUTECPP_DEVICE_COMPILER_FLAGS} -sycl-compress-name -no-serial-memop -DEIGEN_NO_ASSERTION_CHECKING=1)
  128. # Check if the platform is supported
  129. execute_process(COMMAND ${COMPUTECPP_INFO_TOOL} "--dump-is-supported"
  130. OUTPUT_VARIABLE COMPUTECPP_PLATFORM_IS_SUPPORTED
  131. RESULT_VARIABLE COMPUTECPP_INFO_TOOL_RESULT OUTPUT_STRIP_TRAILING_WHITESPACE)
  132. if(NOT COMPUTECPP_INFO_TOOL_RESULT EQUAL "0")
  133. message(FATAL_ERROR "platform - Error checking platform support!")
  134. else()
  135. mark_as_advanced(COMPUTECPP_PLATFORM_IS_SUPPORTED)
  136. if (COMPUTECPP_PLATFORM_IS_SUPPORTED)
  137. message(STATUS "platform - your system can support ComputeCpp")
  138. else()
  139. message(STATUS "platform - your system CANNOT support ComputeCpp")
  140. endif()
  141. endif()
  142. ####################
  143. # __build_sycl
  144. ####################
  145. #
  146. # Adds a custom target for running compute++ and adding a dependency for the
  147. # resulting integration header.
  148. #
  149. # targetName : Name of the target.
  150. # sourceFile : Source file to be compiled.
  151. # binaryDir : Intermediate directory to output the integration header.
  152. #
  153. function(__build_spir targetName sourceFile binaryDir)
  154. # Retrieve source file name.
  155. get_filename_component(sourceFileName ${sourceFile} NAME)
  156. # Set the path to the Sycl file.
  157. set(outputSyclFile ${binaryDir}/${sourceFileName}.sycl)
  158. # Add any user-defined include to the device compiler
  159. get_property(includeDirectories DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY
  160. INCLUDE_DIRECTORIES)
  161. set(device_compiler_includes "")
  162. foreach(directory ${includeDirectories})
  163. set(device_compiler_includes "-I${directory}" ${device_compiler_includes})
  164. endforeach()
  165. if (CMAKE_INCLUDE_PATH)
  166. foreach(directory ${CMAKE_INCLUDE_PATH})
  167. set(device_compiler_includes "-I${directory}"
  168. ${device_compiler_includes})
  169. endforeach()
  170. endif()
  171. # Convert argument list format
  172. separate_arguments(COMPUTECPP_DEVICE_COMPILER_FLAGS)
  173. # Add custom command for running compute++
  174. add_custom_command(
  175. OUTPUT ${outputSyclFile}
  176. COMMAND ${COMPUTECPP_DEVICE_COMPILER}
  177. ${COMPUTECPP_DEVICE_COMPILER_FLAGS}
  178. -isystem ${COMPUTECPP_INCLUDE_DIRECTORY}
  179. ${COMPUTECPP_PLATFORM_SPECIFIC_ARGS}
  180. ${device_compiler_includes}
  181. -o ${outputSyclFile}
  182. -c ${CMAKE_CURRENT_SOURCE_DIR}/${sourceFile}
  183. DEPENDS ${sourceFile}
  184. WORKING_DIRECTORY ${binaryDir}
  185. COMMENT "Building ComputeCpp integration header file ${outputSyclFile}")
  186. # Add a custom target for the generated integration header
  187. add_custom_target(${targetName}_integration_header DEPENDS ${outputSyclFile})
  188. # Add a dependency on the integration header
  189. add_dependencies(${targetName} ${targetName}_integration_header)
  190. # Set the host compiler C++ standard to C++11
  191. set_property(TARGET ${targetName} PROPERTY CXX_STANDARD 11)
  192. # Disable GCC dual ABI on GCC 5.1 and higher
  193. if(COMPUTECPP_DISABLE_GCC_DUAL_ABI)
  194. set_property(TARGET ${targetName} APPEND PROPERTY COMPILE_DEFINITIONS
  195. "_GLIBCXX_USE_CXX11_ABI=0")
  196. endif()
  197. endfunction()
  198. #######################
  199. # add_sycl_to_target
  200. #######################
  201. #
  202. # Adds a SYCL compilation custom command associated with an existing
  203. # target and sets a dependancy on that new command.
  204. #
  205. # targetName : Name of the target to add a SYCL to.
  206. # sourceFile : Source file to be compiled for SYCL.
  207. # binaryDir : Intermediate directory to output the integration header.
  208. #
  209. function(add_sycl_to_target targetName sourceFile binaryDir)
  210. # Add custom target to run compute++ and generate the integration header
  211. __build_spir(${targetName} ${sourceFile} ${binaryDir})
  212. # Link with the ComputeCpp runtime library
  213. target_link_libraries(${targetName} PUBLIC ${COMPUTECPP_RUNTIME_LIBRARY}
  214. PUBLIC ${OpenCL_LIBRARIES})
  215. endfunction(add_sycl_to_target)