11#
2- # Modifications, Copyright (C) 2021 Intel Corporation
2+ # Modifications, Copyright (C) 2022 Intel Corporation
33#
44# This software and the related documents are Intel copyrighted materials, and
55# your use of them is governed by the express license under which they were
1515# file Copyright.txt or https://cmake.org/licensing for details.
1616
1717#[=======================================================================[.rst:
18- IntelDPCPPConfig
18+ IntelSYCLConfig
1919-------
2020
21- DPCPP Library to verify DPCPP/ SYCL compatability of CMAKE_CXX_COMPILER
21+ Library to verify SYCL compatability of CMAKE_CXX_COMPILER
2222and passes relevant compiler flags.
2323
2424Result Variables
2525^^^^^^^^^^^^^^^^
2626
2727This will define the following variables:
2828
29- ``IntelDPCPP_FOUND ``
30- True if the system has the DPCPP library.
29+ ``IntelSYCL_FOUND ``
30+ True if the system has the SYCL library.
3131``SYCL_LANGUAGE_VERSION``
3232 The SYCL language spec version by Compiler.
3333``SYCL_INCLUDE_DIR``
@@ -37,35 +37,39 @@ This will define the following variables:
3737``SYCL_FLAGS``
3838 SYCL specific flags for the compiler.
3939
40+ ``IntelSYCL::SYCL_CXX``
41+ Target for using Intel SYCL (DPC++). The following properties are defined
42+ for the target: ``INTERFACE_COMPILE_OPTIONS``, ``INTERFACE_LINK_OPTIONS``,
43+ ``INTERFACE_INCLUDE_DIRECTORIES``, and ``INTERFACE_LINK_DIRECTORIES``
44+
4045Cache Variables
4146^^^^^^^^^^^^^^^
4247
43- The following cache variables may also be set:
48+ The following cache variable may also be set:
4449
45- ``SYCL_INCLUDE_DIR``
46- The directory containing ``sycl.hpp``.
47- ``SYCL_LIBRARY_DIR``
48- The path to the SYCL library.
49- ``SYCL_FLAGS``
50- SYCL specific flags for the compiler.
5150``SYCL_LANGUAGE_VERSION``
5251 The SYCL language spec version by Compiler.
5352
5453
55- .. note ::
54+ .. Note ::
5655
57- For now, user needs to set -DCMAKE_CXX_COMPILER or environment of
56+ 1. User needs to set -DCMAKE_CXX_COMPILER or environment of
5857 CXX pointing to SYCL compatible compiler ( eg: icx, clang++, icpx)
5958
60- Note: do not set to DPCPP compiler. If set to a Compiler family
61- that supports dpcpp ( eg: IntelLLVM) both DPCPP and SYCL
62- features are enabled.
6359
64- And add this package to user's Cmake config file.
60+ 2. Add this package to user's Cmake config file.
61+
62+ .. code-block:: cmake
63+
64+ find_package(IntelSYCL REQUIRED)
65+
66+ 3. Add sources to target through add_sycl_to_target()
6567
6668 .. code-block:: cmake
6769
68- find_package(IntelDPCPP REQUIRED)
70+ # Compile specific sources for SYCL and build target for SYCL
71+ add_executable(target_proj A.cpp B.cpp offload1.cpp offload2.cpp)
72+ add_sycl_to_target(TARGET target_proj SOURCES offload1.cpp offload2.cpp)
6973
7074#]=======================================================================]
7175
@@ -83,25 +87,33 @@ endif()
8387
8488string (COMPARE EQUAL "${CMAKE_CXX_COMPILER} " "" nocmplr)
8589if (nocmplr)
86- set (IntelDPCPP_FOUND False )
90+ set (IntelSYCL_FOUND False )
8791 set (SYCL_REASON_FAILURE "SYCL: CMAKE_CXX_COMPILER not set!!" )
88- set (IntelDPCPP_NOT_FOUND_MESSAGE "${SYCL_REASON_FAILURE} " )
92+ set (IntelSYCL_NOT_FOUND_MESSAGE "${SYCL_REASON_FAILURE} " )
93+ endif ()
94+
95+ # Check if a Compiler ID is being set. project() should be set prior to find_package()
96+
97+ if ("x${CMAKE_CXX_COMPILER_ID} " STREQUAL "x" )
98+ set (IntelSYCL_FOUND False )
99+ set (SYCL_REASON_FAILURE "CMake CXX Compiler family is not set. Please make sure find_package(IntelSYCL) is called after project()!!" )
100+ set (IntelSYCL_NOT_FOUND_MESSAGE "${SYCL_REASON_FAILURE} " )
101+ return ()
89102endif ()
90103
91104# Check for known compiler family that supports SYCL
92105
93106if ( NOT "x${CMAKE_CXX_COMPILER_ID} " STREQUAL "xClang" AND
94107 NOT "x${CMAKE_CXX_COMPILER_ID} " STREQUAL "xIntelLLVM" )
95- set (IntelDPCPP_FOUND False )
108+ set (IntelSYCL_FOUND False )
96109 set (SYCL_REASON_FAILURE "Unsupported compiler family ${CMAKE_CXX_COMPILER_ID} and compiler ${CMAKE_CXX_COMPILER} !!" )
97- set (IntelDPCPP_NOT_FOUND_MESSAGE "${SYCL_REASON_FAILURE} " )
110+ set (IntelSYCL_NOT_FOUND_MESSAGE "${SYCL_REASON_FAILURE} " )
98111 return ()
99112endif ()
100113
101114# Assume that CXX Compiler supports SYCL and then test to verify.
102115set (SYCL_COMPILER ${CMAKE_CXX_COMPILER} )
103116
104-
105117# Function to write a test case to verify SYCL features.
106118
107119function (SYCL_FEATURE_TEST_WRITE src)
@@ -144,7 +156,7 @@ function(SYCL_FEATURE_TEST_BUILD TEST_SRC_FILE TEST_EXE)
144156 OUTPUT_VARIABLE output ERROR_VARIABLE output
145157 OUTPUT_FILE ${SYCL_TEST_DIR} /Compile.log
146158 RESULT_VARIABLE result
147- TIMEOUT 20
159+ TIMEOUT 60
148160 )
149161
150162 # Verify if test case build properly.
@@ -168,12 +180,12 @@ function(SYCL_FEATURE_TEST_RUN TEST_EXE)
168180 WORKING_DIRECTORY ${SYCL_TEST_DIR}
169181 OUTPUT_VARIABLE output ERROR_VARIABLE output
170182 RESULT_VARIABLE result
171- TIMEOUT 20
183+ TIMEOUT 60
172184 )
173185
174186 # Verify the test execution output.
175187 if (test_result)
176- set (IntelDPCPP_FOUND False )
188+ set (IntelSYCL_FOUND False )
177189 set (SYCL_REASON_FAILURE "SYCL: feature test execution failed!!" )
178190 endif ()
179191 # TODO: what iff the result is false.. error or ignore?
@@ -236,14 +248,14 @@ set(SYCL_LINK_FLAGS "")
236248# Based on Compiler ID, add support for SYCL
237249if ( "x${CMAKE_CXX_COMPILER_ID} " STREQUAL "xClang" OR
238250 "x${CMAKE_CXX_COMPILER_ID} " STREQUAL "xIntelLLVM" )
239- set ( SYCL_FLAGS "-fsycl " )
240- set ( SYCL_LINK_FLAGS "-fsycl " )
251+ list ( APPEND SYCL_FLAGS "-fsycl" )
252+ list ( APPEND SYCL_LINK_FLAGS "-fsycl" )
241253endif ()
242254
243255# TODO verify if this is needed
244256# Windows: Add Exception handling
245257if (WIN32 )
246- set (SYCL_FLAGS " ${ SYCL_FLAGS} /EHsc" )
258+ list ( APPEND SYCL_FLAGS " /EHsc" )
247259endif ()
248260
249261set (SYCL_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${SYCL_FLAGS} " )
@@ -273,32 +285,76 @@ SYCL_FEATURE_TEST_EXTRACT(${test_output})
273285# define macro SYCL_LANGUAGE_VERSION
274286string (COMPARE EQUAL "${SYCL_LANGUAGE_VERSION} " "" nosycllang)
275287if (nosycllang)
276- set (IntelDPCPP_FOUND False )
288+ set (IntelSYCL_FOUND False )
277289 set (SYCL_REASON_FAILURE "SYCL: It appears that the ${CMAKE_CXX_COMPILER} does not support SYCL" )
278- set (IntelDPCPP_NOT_FOUND_MESSAGE "${SYCL_REASON_FAILURE} " )
290+ set (IntelSYCL_NOT_FOUND_MESSAGE "${SYCL_REASON_FAILURE} " )
279291endif ()
280292
281293# Placeholder for identifying various implemenations of SYCL compilers.
282294# for now, set to the CMAKE_CXX_COMPILER_ID
283295set (SYCL_IMPLEMENTATION_ID "${CMAKE_CXX_COMPILER_ID} " )
284296
285- set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${SYCL_FLAGS} " )
286- set (CMAKE_CXX_LINK_FLAGS "${CMAKE_CXX_LINK_FLAGS} ${SYCL_LINK_FLAGS} " )
297+ message (DEBUG "The SYCL compiler is ${SYCL_COMPILER} " )
298+ message (DEBUG "The SYCL Flags are ${SYCL_FLAGS} " )
299+ message (DEBUG "The SYCL Language Version is ${SYCL_LANGUAGE_VERSION} " )
287300
288- message (STATUS "Echo from ${CMAKE_CURRENT_SOURCE_DIR} /IntelDPCPPConfig.cmake" )
289- message (STATUS "The SYCL compiler is ${SYCL_COMPILER} " )
290- message (STATUS "The SYCL Flags are ${SYCL_FLAGS} " )
291- message (STATUS "The SYCL Language Version is ${SYCL_LANGUAGE_VERSION} " )
301+ add_library (IntelSYCL::SYCL_CXX INTERFACE IMPORTED )
302+ set_property (TARGET IntelSYCL::SYCL_CXX PROPERTY
303+ INTERFACE_COMPILE_OPTIONS ${SYCL_FLAGS} )
304+ set_property (TARGET IntelSYCL::SYCL_CXX PROPERTY
305+ INTERFACE_LINK_OPTIONS ${SYCL_LINK_FLAGS} )
306+ set_property (TARGET IntelSYCL::SYCL_CXX PROPERTY
307+ INTERFACE_INCLUDE_DIRECTORIES ${SYCL_INCLUDE_DIR} )
308+ set_property (TARGET IntelSYCL::SYCL_CXX PROPERTY
309+ INTERFACE_LINK_DIRECTORIES ${SYCL_LIBRARY_DIR} )
292310
293311find_package_handle_standard_args(
294- IntelDPCPP
295- FOUND_VAR IntelDPCPP_FOUND
312+ IntelSYCL
313+ FOUND_VAR IntelSYCL_FOUND
296314 REQUIRED_VARS SYCL_INCLUDE_DIR SYCL_LIBRARY_DIR SYCL_FLAGS
297315 VERSION_VAR SYCL_LANGUAGE_VERSION
298316 REASON_FAILURE_MESSAGE "${SYCL_REASON_FAILURE} " )
299317
300318# Include in Cache
301319set (SYCL_LANGUAGE_VERSION "${SYCL_LANGUAGE_VERSION} " CACHE STRING "SYCL Language version" )
302- set (SYCL_INCLUDE_DIR "${SYCL_INCLUDE_DIR} " CACHE FILEPATH "SYCL Include directory" )
303- set (SYCL_LIBRARY_DIR "${SYCL_LIBRARY_DIR} " CACHE FILEPATH "SYCL Library Directory" )
304- set (SYCL_FLAGS "${SYCL_FLAGS} " CACHE STRING "SYCL flags for the compiler" )
320+
321+ function (add_sycl_to_target)
322+
323+ set (one_value_args TARGET )
324+ set (multi_value_args SOURCES )
325+ cmake_parse_arguments (SYCL
326+ ""
327+ "${one_value_args} "
328+ "${multi_value_args} "
329+ ${ARGN} )
330+
331+
332+ get_target_property (__sycl_cxx_options IntelSYCL::SYCL_CXX INTERFACE_COMPILE_OPTIONS)
333+ get_target_property (__sycl_cxx_include_directories IntelSYCL::SYCL_CXX INTERFACE_INCLUDE_DIRECTORIES )
334+
335+ if (NOT ${ARGC} )
336+ message (FATAL_ERROR " add_sycl_to_target() does not have any arguments" )
337+ elseif (${ARGC} EQUAL 1)
338+ message (WARNING "add_sycl_to_target() have only one argument specified.. assuming the target to be ${ARGV} .
339+ Adding sycl to all sources but that may effect compilation times" )
340+ set (SYCL_TARGET ${ARGV} )
341+ endif ()
342+
343+ if (NOT SYCL_SOURCES)
344+ message (WARNING "add_sycl_to_target() does not have sources specified.. Adding sycl to all sources but that may effect compilation times" )
345+ target_compile_options (${SYCL_TARGET} PUBLIC ${__sycl_cxx_options} )
346+ target_include_directories (${SYCL_TARGET} PUBLIC ${__sycl_cxx_include_directories} )
347+ endif ()
348+
349+ foreach (source ${SYCL_SOURCES} )
350+ set_source_files_properties (${source} PROPERTIES COMPILE_OPTIONS "${__sycl_cxx_options} " )
351+ set_source_files_properties (${source} PROPERTIES INCLUDE_DIRECTORIES "${__sycl_cxx_include_directories} " )
352+ endforeach ()
353+
354+ get_target_property (__sycl_link_options
355+ IntelSYCL::SYCL_CXX INTERFACE_LINK_OPTIONS)
356+ target_link_options (${SYCL_TARGET} PRIVATE "${__sycl_link_options} " )
357+ get_target_property (__sycl_link_directories
358+ IntelSYCL::SYCL_CXX INTERFACE_LINK_DIRECTORIES)
359+ target_link_directories (${SYCL_TARGET} PUBLIC "${__sycl_link_directories} " )
360+ endfunction (add_sycl_to_target)
0 commit comments