forked from Xilinx/mlir-aie
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.cmake
More file actions
140 lines (125 loc) · 6.35 KB
/
common.cmake
File metadata and controls
140 lines (125 loc) · 6.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# This file is licensed under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#
# (c) Copyright 2025 Advanced Micro Devices, Inc.
# Common CMake configuration for programming guide examples
# This file provides common setup for test_utils library linking
# -----------------------------------------------------------------------------
# Guard: project() must be called before this file is included, because
# find_package(XRT) below loads xrt-targets.cmake which calls
# add_library(... SHARED IMPORTED). Without a prior project() call, CMake
# has not initialised platform shared-library support and the call either
# fails ("does not support dynamic linking") or silently downgrades to
# STATIC. See Xilinx/mlir-aie#3048.
# -----------------------------------------------------------------------------
if(NOT PROJECT_NAME)
message(FATAL_ERROR
"common.cmake must be included after project(). "
"Call mlir_aie_init_example() (or your own project() call) first. "
"See https://github.com/Xilinx/mlir-aie/issues/3048")
endif()
# -----------------------------------------------------------------------------
# Resolve MLIR-AIE root directory
# -----------------------------------------------------------------------------
# In WSL, CMake runs on Windows via `powershell.exe cmake`. Therefore, we must
# prefer deterministic repo-root detection. Fall back to Python only if needed.
get_filename_component(_mlir_aie_repo_root "${CMAKE_CURRENT_LIST_DIR}/.." ABSOLUTE)
if(EXISTS "${_mlir_aie_repo_root}/runtime_lib/test_lib/xrt_test_wrapper.h")
set(MLIR_AIE_DIR "${_mlir_aie_repo_root}")
else()
find_package(Python3 COMPONENTS Interpreter QUIET)
if(Python3_Interpreter_FOUND)
execute_process(
COMMAND "${Python3_EXECUTABLE}" -c "from aie.utils.config import root_path; print(root_path())"
OUTPUT_VARIABLE MLIR_AIE_DIR
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET
)
endif()
endif()
if(NOT MLIR_AIE_DIR)
message(FATAL_ERROR "Unable to determine MLIR_AIE_DIR (repo root not found and Python probe unavailable).")
endif()
# -----------------------------------------------------------------------------
# XRT auto-detection (supports both Ubuntu packages and legacy /opt/xilinx/xrt)
# -----------------------------------------------------------------------------
if(NOT DEFINED XRT_INC_DIR OR NOT DEFINED XRT_LIB_DIR)
find_package(XRT QUIET)
if(XRT_FOUND)
# find_package(XRT) may resolve via the project's FindXRT.cmake (which
# sets XRT_INCLUDE_DIR / XRT_LIB_DIR, singular) or via XRT's own
# xrt-config.cmake (which sets XRT_INCLUDE_DIRS / XRT_LINK_DIRS,
# plural). Accept whichever set is available.
if(NOT DEFINED XRT_INC_DIR)
if(XRT_INCLUDE_DIRS)
set(XRT_INC_DIR "${XRT_INCLUDE_DIRS}" CACHE STRING "Path to XRT headers")
elseif(XRT_INCLUDE_DIR)
set(XRT_INC_DIR "${XRT_INCLUDE_DIR}" CACHE STRING "Path to XRT headers")
endif()
endif()
if(NOT DEFINED XRT_LIB_DIR)
if(XRT_LINK_DIRS)
set(XRT_LIB_DIR "${XRT_LINK_DIRS}" CACHE STRING "Path to XRT libraries")
endif()
endif()
endif()
# Fall back to legacy/default paths if still unset
if(NOT DEFINED XRT_INC_DIR OR NOT DEFINED XRT_LIB_DIR)
find_program(WSL NAMES powershell.exe)
if(NOT WSL)
if(NOT DEFINED XRT_INC_DIR)
set(XRT_INC_DIR /opt/xilinx/xrt/include CACHE STRING "Path to XRT headers")
endif()
if(NOT DEFINED XRT_LIB_DIR)
set(XRT_LIB_DIR /opt/xilinx/xrt/lib CACHE STRING "Path to XRT libraries")
endif()
else()
if(NOT DEFINED XRT_INC_DIR)
set(XRT_INC_DIR C:/Technical/XRT/src/runtime_src/core/include CACHE STRING "Path to XRT headers")
endif()
if(NOT DEFINED XRT_LIB_DIR)
set(XRT_LIB_DIR C:/Technical/xrtNPUfromDLL CACHE STRING "Path to XRT libraries")
endif()
endif()
endif()
endif()
# -----------------------------------------------------------------------------
# test_utils discovery
# -----------------------------------------------------------------------------
# Preferred: installed layout (from cmake --install). Fallback: build from source.
set(TEST_UTILS_INST_LIB_DIR "${MLIR_AIE_DIR}/runtime_lib/x86_64/test_lib/lib")
set(TEST_UTILS_INST_INC_DIR "${MLIR_AIE_DIR}/runtime_lib/x86_64/test_lib/include")
set(TEST_UTILS_SRC_DIR "${MLIR_AIE_DIR}/runtime_lib/test_lib")
set(TEST_UTILS_RUNTIME_LIB_DIR "${MLIR_AIE_DIR}/runtime_lib")
function(target_link_test_utils target_name)
target_include_directories(${target_name} PUBLIC "${TEST_UTILS_RUNTIME_LIB_DIR}")
# 1) Use installed/prebuilt if present
if(EXISTS "${TEST_UTILS_INST_INC_DIR}/xrt_test_wrapper.h" AND EXISTS "${TEST_UTILS_INST_LIB_DIR}")
target_include_directories(${target_name} PUBLIC "${TEST_UTILS_INST_INC_DIR}")
target_link_directories(${target_name} PUBLIC "${TEST_UTILS_INST_LIB_DIR}")
target_link_libraries(${target_name} PUBLIC test_utils)
return()
endif()
# 2) Otherwise build test_utils from source
if(NOT EXISTS "${TEST_UTILS_SRC_DIR}/test_utils.cpp")
message(FATAL_ERROR "test_utils source not found at: ${TEST_UTILS_SRC_DIR}")
endif()
target_include_directories(${target_name} PUBLIC "${TEST_UTILS_SRC_DIR}")
if(NOT TARGET test_utils)
add_library(test_utils STATIC "${TEST_UTILS_SRC_DIR}/test_utils.cpp")
target_include_directories(test_utils PUBLIC "${TEST_UTILS_SRC_DIR}" "${TEST_UTILS_RUNTIME_LIB_DIR}")
# Enable XRT helpers if an XRT include dir is available
if(DEFINED XRT_INC_DIR AND XRT_INC_DIR)
target_include_directories(test_utils PUBLIC "${XRT_INC_DIR}")
target_compile_definitions(test_utils PRIVATE TEST_UTILS_USE_XRT)
elseif(DEFINED XRT_INCLUDE_DIRS AND XRT_INCLUDE_DIRS)
target_include_directories(test_utils PUBLIC "${XRT_INCLUDE_DIRS}")
target_compile_definitions(test_utils PRIVATE TEST_UTILS_USE_XRT)
elseif(DEFINED XRT_INCLUDE_DIR AND XRT_INCLUDE_DIR)
target_include_directories(test_utils PUBLIC "${XRT_INCLUDE_DIR}")
target_compile_definitions(test_utils PRIVATE TEST_UTILS_USE_XRT)
endif()
endif()
target_link_libraries(${target_name} PUBLIC test_utils)
endfunction()