|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# ***************************************************************************** |
| 3 | +# Copyright (c) 2026, Intel Corporation |
| 4 | +# All rights reserved. |
| 5 | +# |
| 6 | +# Redistribution and use in source and binary forms, with or without |
| 7 | +# modification, are permitted provided that the following conditions are met: |
| 8 | +# - Redistributions of source code must retain the above copyright notice, |
| 9 | +# this list of conditions and the following disclaimer. |
| 10 | +# - Redistributions in binary form must reproduce the above copyright notice, |
| 11 | +# this list of conditions and the following disclaimer in the documentation |
| 12 | +# and/or other materials provided with the distribution. |
| 13 | +# - Neither the name of the copyright holder nor the names of its contributors |
| 14 | +# may be used to endorse or promote products derived from this software |
| 15 | +# without specific prior written permission. |
| 16 | +# |
| 17 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 18 | +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 | +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 21 | +# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 22 | +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 23 | +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 24 | +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 25 | +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 26 | +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 27 | +# THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | +# ***************************************************************************** |
| 29 | + |
| 30 | +# TODO: rework this logic to remove current duplication |
| 31 | +if(WIN32) |
| 32 | + string( |
| 33 | + CONCAT WARNING_FLAGS |
| 34 | + "-Wall " |
| 35 | + "-Wextra " |
| 36 | + "-Winit-self " |
| 37 | + "-Wunused-function " |
| 38 | + "-Wuninitialized " |
| 39 | + "-Wmissing-declarations " |
| 40 | + "-Wstrict-prototypes " |
| 41 | + "-Wno-unused-parameter " |
| 42 | + ) |
| 43 | + string(CONCAT SDL_FLAGS "/GS " "/DynamicBase ") |
| 44 | + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /Ox ${WARNING_FLAGS} ${SDL_FLAGS}") |
| 45 | + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Ox ${WARNING_FLAGS} ${SDL_FLAGS}") |
| 46 | + set(CMAKE_C_FLAGS_DEBUG |
| 47 | + "${CMAKE_C_FLAGS_DEBUG} ${WARNING_FLAGS} ${SDL_FLAGS} -O0 -g1 -DDEBUG -Xsycl-target-frontend=spir64 \"-g0\"" |
| 48 | + ) |
| 49 | + set(CMAKE_CXX_FLAGS_DEBUG |
| 50 | + "${CMAKE_CXX_FLAGS_DEBUG} ${WARNING_FLAGS} ${SDL_FLAGS} -O0 -g1 -DDEBUG -Xsycl-target-frontend=spir64 \"-g0\"" |
| 51 | + ) |
| 52 | + set(CMAKE_C_FLAGS_COVERAGE |
| 53 | + "${CMAKE_C_FLAGS_DEBUG} ${WARNING_FLAGS} ${SDL_FLAGS} -O1 -g1 -DDEBUG" |
| 54 | + ) |
| 55 | + set(CMAKE_CXX_FLAGS_COVERAGE |
| 56 | + "${CMAKE_CXX_FLAGS_DEBUG} ${WARNING_FLAGS} ${SDL_FLAGS} -O1 -g1 -DDEBUG" |
| 57 | + ) |
| 58 | + set(CMAKE_MODULE_LINKER_FLAGS_COVERAGE "${CMAKE_MODULE_LINKER_FLAGS_DEBUG}") |
| 59 | + set(DPCTL_LDFLAGS "/NXCompat;/DynamicBase") |
| 60 | + mark_as_advanced( |
| 61 | + CMAKE_CXX_FLAGS_COVERAGE |
| 62 | + CMAKE_C_FLAGS_COVERAGE |
| 63 | + CMAKE_MODULE_LINKER_FLAGS_COVERAGE |
| 64 | + ) |
| 65 | +elseif(UNIX) |
| 66 | + string( |
| 67 | + CONCAT WARNING_FLAGS |
| 68 | + "-Wall " |
| 69 | + "-Wextra " |
| 70 | + "-Winit-self " |
| 71 | + "-Wunused-function " |
| 72 | + "-Wuninitialized " |
| 73 | + "-Wmissing-declarations " |
| 74 | + "-Wstrict-prototypes " |
| 75 | + "-Wno-unused-parameter " |
| 76 | + "-fdiagnostics-color=auto " |
| 77 | + ) |
| 78 | + string( |
| 79 | + CONCAT SDL_FLAGS |
| 80 | + "-fstack-protector " |
| 81 | + "-fstack-protector-all " |
| 82 | + "-fpic " |
| 83 | + "-fPIC " |
| 84 | + "-D_FORTIFY_SOURCE=2 " |
| 85 | + "-Wformat " |
| 86 | + "-Wformat-security " |
| 87 | + # "-fno-strict-overflow " # no-strict-overflow is implied by -fwrapv |
| 88 | + "-fno-delete-null-pointer-checks " |
| 89 | + "-fwrapv " |
| 90 | + ) |
| 91 | + string(CONCAT CFLAGS "${WARNING_FLAGS}" "${SDL_FLAGS}") |
| 92 | + string(CONCAT CXXFLAGS "${WARNING_FLAGS}" "${SDL_FLAGS}") |
| 93 | + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3 ${CFLAGS}") |
| 94 | + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 ${CXXFLAGS}") |
| 95 | + set(CMAKE_C_FLAGS_DEBUG |
| 96 | + "${CMAKE_C_FLAGS_DEBUG} ${CFLAGS} -O0 -g -DDEBUG -Xsycl-target-frontend=spir64 \"-g0\"" |
| 97 | + ) |
| 98 | + set(CMAKE_CXX_FLAGS_DEBUG |
| 99 | + "${CMAKE_CXX_FLAGS_DEBUG} ${CXXFLAGS} -O0 -g -DDEBUG -Xsycl-target-frontend=spir64 \"-g0\"" |
| 100 | + ) |
| 101 | + set(CMAKE_C_FLAGS_COVERAGE "${CMAKE_C_FLAGS_DEBUG} ${CFLAGS} -O1 -g1 -DDEBUG") |
| 102 | + set(CMAKE_CXX_FLAGS_COVERAGE "${CMAKE_CXX_FLAGS_DEBUG} ${CXXFLAGS} -O1 -g1 -DDEBUG") |
| 103 | + set(CMAKE_MODULE_LINKER_FLAGS_COVERAGE "${CMAKE_MODULE_LINKER_FLAGS_DEBUG}") |
| 104 | + set(DPCTL_LDFLAGS "-z,noexecstack,-z,relro,-z,now") |
| 105 | + mark_as_advanced( |
| 106 | + CMAKE_CXX_FLAGS_COVERAGE |
| 107 | + CMAKE_C_FLAGS_COVERAGE |
| 108 | + CMAKE_MODULE_LINKER_FLAGS_COVERAGE |
| 109 | + ) |
| 110 | +else() |
| 111 | + message(FATAL_ERROR "Unsupported system.") |
| 112 | +endif() |
| 113 | + |
| 114 | +# at build time create include/ directory and copy header files over |
| 115 | +set(DPCTL_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include) |
| 116 | + |
| 117 | +set(CMAKE_INSTALL_RPATH "$ORIGIN") |
| 118 | + |
| 119 | +function(build_dpctl_ext _trgt _src _dest) |
| 120 | + set(options SYCL) |
| 121 | + cmake_parse_arguments(BUILD_DPCTL_EXT "${options}" "RELATIVE_PATH" "" ${ARGN}) |
| 122 | + add_cython_target(${_trgt} ${_src} CXX OUTPUT_VAR _generated_src) |
| 123 | + set(_cythonize_trgt "${_trgt}_cythonize_pyx") |
| 124 | + python_add_library(${_trgt} MODULE WITH_SOABI ${_generated_src}) |
| 125 | + if(BUILD_DPCTL_EXT_SYCL) |
| 126 | + add_sycl_to_target(TARGET ${_trgt} SOURCES ${_generated_src}) |
| 127 | + target_compile_options(${_trgt} PRIVATE -fno-sycl-id-queries-fit-in-int) |
| 128 | + target_link_options(${_trgt} PRIVATE -fsycl-device-code-split=per_kernel) |
| 129 | + if(DPCTL_OFFLOAD_COMPRESS) |
| 130 | + target_link_options(${_trgt} PRIVATE --offload-compress) |
| 131 | + endif() |
| 132 | + if(_dpctl_sycl_targets) |
| 133 | + # make fat binary |
| 134 | + target_compile_options( |
| 135 | + ${_trgt} |
| 136 | + PRIVATE ${_dpctl_sycl_target_compile_options} |
| 137 | + ) |
| 138 | + target_link_options(${_trgt} PRIVATE ${_dpctl_sycl_target_link_options}) |
| 139 | + endif() |
| 140 | + endif() |
| 141 | + target_link_libraries(${_trgt} PRIVATE Python::NumPy) |
| 142 | + if(DPCTL_GENERATE_COVERAGE) |
| 143 | + target_compile_definitions(${_trgt} PRIVATE CYTHON_TRACE=1 CYTHON_TRACE_NOGIL=1) |
| 144 | + if(BUILD_DPCTL_EXT_SYCL) |
| 145 | + target_compile_options(${_trgt} PRIVATE -fno-sycl-use-footer) |
| 146 | + endif() |
| 147 | + endif() |
| 148 | + # Dpctl |
| 149 | + target_include_directories(${_trgt} PRIVATE ${Dpctl_INCLUDE_DIR}) |
| 150 | + target_link_directories(${_trgt} PRIVATE ${Dpctl_INCLUDE_DIR}/..) |
| 151 | + target_link_libraries(${_trgt} PRIVATE DPCTLSyclInterface) |
| 152 | + set(_linker_options "LINKER:${DPCTL_LDFLAGS}") |
| 153 | + target_link_options(${_trgt} PRIVATE ${_linker_options}) |
| 154 | + get_filename_component(_name_wle ${_generated_src} NAME_WLE) |
| 155 | + get_filename_component(_generated_src_dir ${_generated_src} DIRECTORY) |
| 156 | + set(_generated_public_h "${_generated_src_dir}/${_name_wle}.h") |
| 157 | + set(_generated_api_h "${_generated_src_dir}/${_name_wle}_api.h") |
| 158 | + |
| 159 | + # TODO: create separate folder inside build folder that contains only |
| 160 | + # headers related to this target and appropriate folder structure to |
| 161 | + # eliminate shadow dependencies |
| 162 | + # Go up two levels to build root for "dpctl_ext/tensor/_usmarray.h" resolution |
| 163 | + get_filename_component(_parent_dir ${_generated_src_dir} DIRECTORY) |
| 164 | + get_filename_component(_build_root ${_parent_dir} DIRECTORY) |
| 165 | + # TODO: do not set directory if we did not generate header |
| 166 | + target_include_directories(${_trgt} INTERFACE ${_build_root}) |
| 167 | + set(_rpath_value "$ORIGIN") |
| 168 | + if(BUILD_DPCTL_EXT_RELATIVE_PATH) |
| 169 | + set(_rpath_value "${_rpath_value}/${BUILD_DPCTL_EXT_RELATIVE_PATH}") |
| 170 | + endif() |
| 171 | + if(DPCTL_WITH_REDIST) |
| 172 | + set(_rpath_value "${_rpath_value}:${_rpath_value}/../../..") |
| 173 | + endif() |
| 174 | + set_target_properties(${_trgt} PROPERTIES INSTALL_RPATH ${_rpath_value}) |
| 175 | + |
| 176 | + install(TARGETS ${_trgt} LIBRARY DESTINATION ${_dest}) |
| 177 | + install( |
| 178 | + FILES ${_generated_api_h} |
| 179 | + DESTINATION ${CMAKE_INSTALL_PREFIX}/dpctl_ext/include/${_dest} |
| 180 | + OPTIONAL |
| 181 | + ) |
| 182 | + install( |
| 183 | + FILES ${_generated_public_h} |
| 184 | + DESTINATION ${CMAKE_INSTALL_PREFIX}/dpctl_ext/include/${_dest} |
| 185 | + OPTIONAL |
| 186 | + ) |
| 187 | + if(DPCTL_GENERATE_COVERAGE) |
| 188 | + get_filename_component(_original_src_dir ${_src} DIRECTORY) |
| 189 | + file(RELATIVE_PATH _rel_dir ${CMAKE_SOURCE_DIR} ${_original_src_dir}) |
| 190 | + install(FILES ${_generated_src} DESTINATION ${CMAKE_INSTALL_PREFIX}/${_rel_dir}) |
| 191 | + endif() |
| 192 | + |
| 193 | + # Create target with headers only, because python is managing all the |
| 194 | + # library imports at runtime |
| 195 | + set(_trgt_headers ${_trgt}_headers) |
| 196 | + add_library(${_trgt_headers} INTERFACE) |
| 197 | + add_dependencies(${_trgt_headers} ${_trgt}) |
| 198 | + get_target_property(_trgt_headers_dir ${_trgt} INTERFACE_INCLUDE_DIRECTORIES) |
| 199 | + target_include_directories(${_trgt_headers} INTERFACE ${_trgt_headers_dir}) |
| 200 | +endfunction() |
| 201 | + |
| 202 | +add_subdirectory(tensor) |
0 commit comments