|
| 1 | +# Copyright (C) 2021 Intel Corporation |
| 2 | +# SPDX-License-Identifier: BSD-3-Clause |
| 3 | + |
| 4 | +cmake_minimum_required(VERSION 3.10) |
| 5 | + |
| 6 | +project(fpga-runtime C CXX) |
| 7 | + |
| 8 | +# Enforce keeping the source directory clean by building in a separate |
| 9 | +# directory. This avoids unnecessary corner cases, e.g., copying files |
| 10 | +# from source to binary directory with identical relative paths. |
| 11 | +if(PROJECT_BINARY_DIR STREQUAL PROJECT_SOURCE_DIR) |
| 12 | + message(FATAL_ERROR "build directory must be different from source directory") |
| 13 | +endif() |
| 14 | + |
| 15 | +# We exclusively support Ninja as the generator used to build the project. |
| 16 | +if(NOT CMAKE_GENERATOR STREQUAL "Ninja") |
| 17 | + message(FATAL_ERROR "unsupported cmake generator, please use `cmake -G Ninja`") |
| 18 | +endif() |
| 19 | + |
| 20 | +list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake/modules") |
| 21 | + |
| 22 | +find_package(Elf REQUIRED) |
| 23 | +find_package(ZLIB) |
| 24 | +find_package(Git) |
| 25 | + |
| 26 | +set(CMAKE_THREAD_PREFER_PTHREAD ON) |
| 27 | +find_package(Threads REQUIRED) |
| 28 | + |
| 29 | +math(EXPR ACL_TARGET_BIT "${CMAKE_SIZEOF_VOID_P} * 8") |
| 30 | +message(STATUS "Target pointer size in bits: ${ACL_TARGET_BIT}") |
| 31 | + |
| 32 | +################################################################################ |
| 33 | +### Modified Default flags |
| 34 | +################################################################################ |
| 35 | +# Use "multithread-specific and DLL-specific version of the run-time library" (Windows) |
| 36 | +# There's a better way for CMake >= 3.15 |
| 37 | +# See https://cmake.org/cmake/help/latest/prop_tgt/MSVC_RUNTIME_LIBRARY.html |
| 38 | +if(WIN32 AND "${CMAKE_BUILD_TYPE}" STREQUAL "") |
| 39 | + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /MD") |
| 40 | + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MD") |
| 41 | +endif() |
| 42 | + |
| 43 | +# Flag for coverage scans |
| 44 | +option(ACL_CODE_COVERAGE "Build with coverage" OFF) |
| 45 | +message(STATUS "Build with coverage: ${ACL_CODE_COVERAGE}") |
| 46 | +if(ACL_CODE_COVERAGE) |
| 47 | + if(CMAKE_COMPILER_IS_GNUCXX) |
| 48 | + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --coverage") |
| 49 | + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} --coverage") |
| 50 | + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --coverage") |
| 51 | + set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} --coverage") |
| 52 | + else() |
| 53 | + message(FATAL_ERROR "cannot build with coverage tools due to unsupported CXX compiler") |
| 54 | + endif() |
| 55 | +endif() |
| 56 | + |
| 57 | +################################################################################ |
| 58 | +### Modified Debug flags |
| 59 | +################################################################################ |
| 60 | +# pkg_editor_test and acl_test have trouble with the debug version of the multithread Windows runtime library |
| 61 | +# There's a better way for CMake >= 3.15 |
| 62 | +# See https://cmake.org/cmake/help/latest/prop_tgt/MSVC_RUNTIME_LIBRARY.html |
| 63 | +string(REGEX REPLACE "/MDd" "/MD" CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG}") |
| 64 | +string(REGEX REPLACE "/MDd" "/MD" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}") |
| 65 | +# pkg_editor_test errors out if we enable runtime error checking |
| 66 | +string(REGEX REPLACE "( /RTC1$|/RTC1 )" "" CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG}") |
| 67 | +string(REGEX REPLACE "( /RTC1$|/RTC1 )" "" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}") |
| 68 | + |
| 69 | +# -DNDEBUG will disable calls to assert() in our unit tests |
| 70 | +foreach(flags |
| 71 | + CMAKE_C_FLAGS_RELWITHDEBINFO |
| 72 | + CMAKE_CXX_FLAGS_RELWITHDEBINFO |
| 73 | + CMAKE_C_FLAGS_RELEASE |
| 74 | + CMAKE_CXX_FLAGS_RELEASE |
| 75 | + CMAKE_C_FLAGS_MINSIZEREL |
| 76 | + CMAKE_CXX_FLAGS_MINSIZEREL |
| 77 | + ) |
| 78 | + string(REGEX REPLACE "(^|[ \t])[/-][dD][ \t]*NDEBUG($|[ \t])" " " "${flags}" "${${flags}}") |
| 79 | +endforeach() |
| 80 | + |
| 81 | +# Windows debug builds frequently crash mspdbsrv.exe, resulting in fatal |
| 82 | +# error C1090: PDB API call failed, error code '23': (0x000006BA). Work |
| 83 | +# around this unresolved issue by disabling separate PDB files (`/Zi`) |
| 84 | +# and instead embedding debugging symbols in object files (`/Z7`). |
| 85 | +# |
| 86 | +# https://bugs.chromium.org/p/chromium/issues/detail?id=659439 |
| 87 | +# https://developercommunity.visualstudio.com/t/c1090-pdb-api-call-failed-error-code-23/48897 |
| 88 | +# https://docs.microsoft.com/en-us/cpp/build/reference/z7-zi-zi-debug-information-format?view=msvc-160 |
| 89 | +if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") |
| 90 | + foreach(flags |
| 91 | + CMAKE_C_FLAGS_DEBUG |
| 92 | + CMAKE_CXX_FLAGS_DEBUG |
| 93 | + CMAKE_C_FLAGS_RELWITHDEBINFO |
| 94 | + CMAKE_CXX_FLAGS_RELWITHDEBINFO |
| 95 | + ) |
| 96 | + string(REGEX REPLACE "(^|[ \t])/Zi($|[ \t])" " /Z7 " "${flags}" "${${flags}}") |
| 97 | + endforeach() |
| 98 | +endif() |
| 99 | + |
| 100 | +# https://clang.llvm.org/docs/AddressSanitizer.html |
| 101 | +option(ACL_WITH_ASAN "Build with address sanitizer" OFF) |
| 102 | +message(STATUS "Build with address sanitizer: ${ACL_WITH_ASAN}") |
| 103 | +if(ACL_WITH_ASAN) |
| 104 | + foreach(lang C CXX) |
| 105 | + if(CMAKE_${lang}_COMPILER_ID MATCHES "^(Clang|GNU)$") |
| 106 | + set(CMAKE_${lang}_FLAGS "${CMAKE_${lang}_FLAGS} -fsanitize=address -fno-omit-frame-pointer") |
| 107 | + else() |
| 108 | + message(FATAL_ERROR "cannot build with address sanitizer due to unsupported ${lang} compiler") |
| 109 | + endif() |
| 110 | + endforeach() |
| 111 | +endif() |
| 112 | + |
| 113 | +include(CTest) |
| 114 | + |
| 115 | +# Get CMAKE_INSTALL_<dir> variables to enable custom library, binary, and include paths |
| 116 | +# See https://cmake.org/cmake/help/latest/module/GNUInstallDirs.html |
| 117 | +include(GNUInstallDirs) |
| 118 | + |
| 119 | +# acl_hostxml includes acl.h, but we don't want to create a cyclic dependency, |
| 120 | +# so we create a header only library here |
| 121 | +add_library(acl_headers INTERFACE) |
| 122 | +target_include_directories(acl_headers INTERFACE include) |
| 123 | + |
| 124 | +# Generate header containing the runtime version. The OUTPUT file *.phony is in |
| 125 | +# fact never written and ensures ninja always considers this target out of date. |
| 126 | +add_custom_command( |
| 127 | + OUTPUT |
| 128 | + "${CMAKE_BINARY_DIR}/include/acl_version.h" |
| 129 | + "${CMAKE_BINARY_DIR}/include/acl_version.h.phony" |
| 130 | + DEPENDS "${CMAKE_SOURCE_DIR}/include/acl_version.h.in" |
| 131 | + COMMAND "${CMAKE_COMMAND}" |
| 132 | + -D "ACL_VERSION_INPUT_FILE=${CMAKE_SOURCE_DIR}/include/acl_version.h.in" |
| 133 | + -D "ACL_VERSION_OUTPUT_FILE=${CMAKE_BINARY_DIR}/include/acl_version.h" |
| 134 | + -D "GIT_FOUND=${GIT_FOUND}" |
| 135 | + -D "GIT_EXECUTABLE=${GIT_EXECUTABLE}" |
| 136 | + -P "${CMAKE_CURRENT_LIST_DIR}/cmake/scripts/version.cmake" |
| 137 | +) |
| 138 | + |
| 139 | +add_library(acl_objs OBJECT |
| 140 | + "${CMAKE_BINARY_DIR}/include/acl_version.h" |
| 141 | + src/acl_auto_configure.cpp |
| 142 | + src/acl_bsp_io.cpp |
| 143 | + src/acl_command.cpp |
| 144 | + src/acl_command_queue.cpp |
| 145 | + src/acl_context.cpp |
| 146 | + src/acl_device.cpp |
| 147 | + src/acl_device_binary.cpp |
| 148 | + src/acl_device_op.cpp |
| 149 | + src/acl_device_program_info.cpp |
| 150 | + src/acl_event.cpp |
| 151 | + src/acl_globals.cpp |
| 152 | + src/acl_hal.cpp |
| 153 | + src/acl_hal_mmd.cpp |
| 154 | + src/acl_hostch.cpp |
| 155 | + src/acl_icd_dispatch.cpp |
| 156 | + src/acl_kernel.cpp |
| 157 | + src/acl_kernel_if.cpp |
| 158 | + src/acl_mem.cpp |
| 159 | + src/acl_offline_hal.cpp |
| 160 | + src/acl_platform.cpp |
| 161 | + src/acl_pll.cpp |
| 162 | + src/acl_printf.cpp |
| 163 | + src/acl_profiler.cpp |
| 164 | + src/acl_program.cpp |
| 165 | + src/acl_sampler.cpp |
| 166 | + src/acl_shared_aligned_ptr.cpp |
| 167 | + src/acl_support.cpp |
| 168 | + src/acl_svm.cpp |
| 169 | + src/acl_thread.cpp |
| 170 | + src/acl_usm.cpp |
| 171 | + src/check_copy_overlap.c |
| 172 | + ) |
| 173 | +set_target_properties(acl_objs PROPERTIES |
| 174 | + CXX_EXTENSIONS OFF |
| 175 | + POSITION_INDEPENDENT_CODE ON |
| 176 | + ) |
| 177 | +target_compile_features(acl_objs PRIVATE cxx_std_11) |
| 178 | +# These compile_definitions need to be public, since we're building an OBJECT library (?) |
| 179 | +target_compile_definitions(acl_objs PUBLIC |
| 180 | + _GLIBCXX_USE_CXX11_ABI=0 |
| 181 | + ACL_SUPPORT_DOUBLE=0 |
| 182 | + ACL_HAS_STDLIB_STDIO |
| 183 | + CL_USE_DEPRECATED_OPENCL_1_0_APIS=1 |
| 184 | + CL_USE_DEPRECATED_OPENCL_1_1_APIS=1 |
| 185 | + CL_USE_DEPRECATED_OPENCL_1_2_APIS=1 |
| 186 | + CL_TARGET_OPENCL_VERSION=200 |
| 187 | + ) |
| 188 | +target_include_directories(acl_objs PUBLIC include) |
| 189 | +target_include_directories(acl_objs PRIVATE |
| 190 | + "${CMAKE_BINARY_DIR}/include" |
| 191 | + src |
| 192 | + ) |
| 193 | +# "Linking" libraries to an OBJECT library is the preferred approach, but it is supported only after CMake 3.12 |
| 194 | +# The oldest CMake version we need to support is 3.10 |
| 195 | +# As older OS support becomes deprecated and we don't need to worry about CMake < 3.12, we can change this |
| 196 | +# See: https://cmake.org/cmake/help/v3.12/command/target_link_libraries.html#linking-object-libraries |
| 197 | +target_include_directories(acl_objs PRIVATE |
| 198 | + "${CMAKE_SOURCE_DIR}/lib/acl_check_sys_cmd/include" |
| 199 | + "${CMAKE_SOURCE_DIR}/lib/acl_hash/include" |
| 200 | + "${CMAKE_SOURCE_DIR}/lib/acl_threadsupport/include" |
| 201 | + "${CMAKE_SOURCE_DIR}/lib/pkg_editor/include" |
| 202 | + ) |
| 203 | + |
| 204 | +add_library(acl SHARED $<TARGET_OBJECTS:acl_objs>) |
| 205 | +set_property(TARGET acl PROPERTY OUTPUT_NAME "alteracl") |
| 206 | +target_link_libraries(acl PRIVATE acl_check_sys_cmd acl_hash acl_threadsupport pkg_editor) |
| 207 | +if(UNIX) |
| 208 | + # we need this flag to expose symbols, otherwise linking will fail with: |
| 209 | + # "relocation against protected symbol X can not be used when making a shared object" |
| 210 | + set_target_properties(acl PROPERTIES LINK_FLAGS "-Wl,-Bsymbolic") |
| 211 | +endif() |
| 212 | + |
| 213 | +set_property(TARGET acl PROPERTY PUBLIC_HEADER |
| 214 | + include/acl.h |
| 215 | + include/acl_auto.h |
| 216 | + include/acl_auto_configure.h |
| 217 | + include/acl_auto_configure_version.h |
| 218 | + include/acl_context.h |
| 219 | + include/acl_device_binary.h |
| 220 | + include/acl_globals.h |
| 221 | + include/acl_hal.h |
| 222 | + include/acl_hal_mmd.h |
| 223 | + include/acl_hostch.h |
| 224 | + include/acl_icd_dispatch.h |
| 225 | + include/acl_shared_aligned_ptr.h |
| 226 | + include/acl_support.h |
| 227 | + include/acl_thread.h |
| 228 | + include/acl_types.h |
| 229 | + include/acl_util.h |
| 230 | + include/acl_visibility.h |
| 231 | + ) |
| 232 | + |
| 233 | +set_property(TARGET acl PROPERTY WINDOWS_EXPORT_ALL_SYMBOLS ON) |
| 234 | + |
| 235 | +install(TARGETS acl |
| 236 | + COMPONENT acl |
| 237 | + LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" |
| 238 | + ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" |
| 239 | + RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" |
| 240 | + PUBLIC_HEADER DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" |
| 241 | + ) |
| 242 | + |
| 243 | +# Workaround for CL/[name].h and MMD/[name].h headers |
| 244 | +# There's a better way for CMake >= 3.15 |
| 245 | +# Change this to be a header-only library and use PUBLIC_HEADER |
| 246 | +# see https://cmake.org/cmake/help/latest/command/add_library.html#interface-libraries |
| 247 | + |
| 248 | +install(FILES |
| 249 | + include/CL/cl.h |
| 250 | + include/CL/cl_d3d10.h |
| 251 | + include/CL/cl_d3d11.h |
| 252 | + include/CL/cl_dx9_media_sharing.h |
| 253 | + include/CL/cl_dx9_media_sharing_intel.h |
| 254 | + include/CL/cl_egl.h |
| 255 | + include/CL/cl_ext.h |
| 256 | + include/CL/cl_ext_intel.h |
| 257 | + include/CL/cl_ext_intelfpga.h |
| 258 | + include/CL/cl_gl.h |
| 259 | + include/CL/cl_gl_ext.h |
| 260 | + include/CL/cl_half.h |
| 261 | + include/CL/cl_icd.h |
| 262 | + include/CL/cl_platform.h |
| 263 | + include/CL/cl_va_api_media_sharing_intel.h |
| 264 | + include/CL/cl_version.h |
| 265 | + include/CL/opencl.h |
| 266 | + include/CL/opencl.hpp |
| 267 | + COMPONENT CL |
| 268 | + DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/CL" |
| 269 | + ) |
| 270 | + |
| 271 | +install(FILES |
| 272 | + include/MMD/aocl_mmd.h |
| 273 | + include/MMD/aocl_mmd_deprecated.h |
| 274 | + COMPONENT MMD |
| 275 | + DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/MMD" |
| 276 | + ) |
| 277 | + |
| 278 | +add_subdirectory(lib) |
| 279 | +add_subdirectory(test) |
0 commit comments