|
7 | 7 | # Common CMake configuration for programming examples |
8 | 8 | # This file provides common setup for test_utils library linking |
9 | 9 |
|
10 | | -# Get MLIR_AIE_DIR from Python |
11 | | -execute_process( |
12 | | - COMMAND python3 -c "from aie.utils.config import root_path; print(root_path())" |
13 | | - OUTPUT_VARIABLE MLIR_AIE_DIR |
14 | | - OUTPUT_STRIP_TRAILING_WHITESPACE |
15 | | -) |
16 | | - |
17 | | -# Set test_utils library paths |
18 | | -set(TEST_UTILS_LIB_DIR "${MLIR_AIE_DIR}/runtime_lib/x86_64/test_lib/lib") |
19 | | -set(TEST_UTILS_INC_DIR "${MLIR_AIE_DIR}/runtime_lib/x86_64/test_lib/include") |
| 10 | +# ----------------------------------------------------------------------------- |
| 11 | +# Resolve MLIR-AIE root directory |
| 12 | +# ----------------------------------------------------------------------------- |
| 13 | +# In WSL, CMake runs on Windows via `powershell.exe cmake`. Therefore, we must |
| 14 | +# prefer deterministic repo-root detection. Fall back to Python only if needed. |
| 15 | + |
| 16 | +get_filename_component(_mlir_aie_repo_root "${CMAKE_CURRENT_LIST_DIR}/.." ABSOLUTE) |
| 17 | +if(EXISTS "${_mlir_aie_repo_root}/runtime_lib/test_lib/xrt_test_wrapper.h") |
| 18 | + set(MLIR_AIE_DIR "${_mlir_aie_repo_root}") |
| 19 | +else() |
| 20 | + find_package(Python3 COMPONENTS Interpreter QUIET) |
| 21 | + if(Python3_Interpreter_FOUND) |
| 22 | + execute_process( |
| 23 | + COMMAND "${Python3_EXECUTABLE}" -c "from aie.utils.config import root_path; print(root_path())" |
| 24 | + OUTPUT_VARIABLE MLIR_AIE_DIR |
| 25 | + OUTPUT_STRIP_TRAILING_WHITESPACE |
| 26 | + ERROR_QUIET |
| 27 | + ) |
| 28 | + endif() |
| 29 | +endif() |
| 30 | + |
| 31 | +if(NOT MLIR_AIE_DIR) |
| 32 | + message(FATAL_ERROR "Unable to determine MLIR_AIE_DIR (repo root not found and Python probe unavailable).") |
| 33 | +endif() |
| 34 | + |
| 35 | +# ----------------------------------------------------------------------------- |
| 36 | +# test_utils discovery |
| 37 | +# ----------------------------------------------------------------------------- |
| 38 | +# Preferred: installed layout (from cmake --install). Fallback: build from source. |
| 39 | +set(TEST_UTILS_INST_LIB_DIR "${MLIR_AIE_DIR}/runtime_lib/x86_64/test_lib/lib") |
| 40 | +set(TEST_UTILS_INST_INC_DIR "${MLIR_AIE_DIR}/runtime_lib/x86_64/test_lib/include") |
| 41 | +set(TEST_UTILS_SRC_DIR "${MLIR_AIE_DIR}/runtime_lib/test_lib") |
20 | 42 | set(TEST_UTILS_RUNTIME_LIB_DIR "${MLIR_AIE_DIR}/runtime_lib") |
21 | 43 |
|
22 | | -# Function to add test_utils library to a target |
23 | 44 | function(target_link_test_utils target_name) |
24 | | - target_include_directories(${target_name} PUBLIC |
25 | | - ${TEST_UTILS_RUNTIME_LIB_DIR} |
26 | | - ${TEST_UTILS_INC_DIR} |
27 | | - ) |
28 | | - |
29 | | - target_link_directories(${target_name} PUBLIC |
30 | | - ${TEST_UTILS_LIB_DIR} |
31 | | - ) |
32 | | - |
33 | | - target_link_libraries(${target_name} PUBLIC |
34 | | - test_utils |
35 | | - ) |
| 45 | + target_include_directories(${target_name} PUBLIC "${TEST_UTILS_RUNTIME_LIB_DIR}") |
| 46 | + |
| 47 | + # 1) Use installed/prebuilt if present |
| 48 | + if(EXISTS "${TEST_UTILS_INST_INC_DIR}/xrt_test_wrapper.h" AND EXISTS "${TEST_UTILS_INST_LIB_DIR}") |
| 49 | + target_include_directories(${target_name} PUBLIC "${TEST_UTILS_INST_INC_DIR}") |
| 50 | + target_link_directories(${target_name} PUBLIC "${TEST_UTILS_INST_LIB_DIR}") |
| 51 | + target_link_libraries(${target_name} PUBLIC test_utils) |
| 52 | + return() |
| 53 | + endif() |
| 54 | + |
| 55 | + # 2) Otherwise build test_utils from source |
| 56 | + if(NOT EXISTS "${TEST_UTILS_SRC_DIR}/test_utils.cpp") |
| 57 | + message(FATAL_ERROR "test_utils source not found at: ${TEST_UTILS_SRC_DIR}") |
| 58 | + endif() |
| 59 | + |
| 60 | + target_include_directories(${target_name} PUBLIC "${TEST_UTILS_SRC_DIR}") |
| 61 | + |
| 62 | + if(NOT TARGET test_utils) |
| 63 | + add_library(test_utils STATIC "${TEST_UTILS_SRC_DIR}/test_utils.cpp") |
| 64 | + target_include_directories(test_utils PUBLIC "${TEST_UTILS_SRC_DIR}" "${TEST_UTILS_RUNTIME_LIB_DIR}") |
| 65 | + |
| 66 | + # Enable XRT helpers if the example provided an XRT include dir |
| 67 | + if(DEFINED XRT_INCLUDE_DIR) |
| 68 | + target_include_directories(test_utils PUBLIC "${XRT_INCLUDE_DIR}") |
| 69 | + target_compile_definitions(test_utils PRIVATE TEST_UTILS_USE_XRT) |
| 70 | + elseif(DEFINED XRT_INC_DIR) |
| 71 | + target_include_directories(test_utils PUBLIC "${XRT_INC_DIR}") |
| 72 | + target_compile_definitions(test_utils PRIVATE TEST_UTILS_USE_XRT) |
| 73 | + endif() |
| 74 | + endif() |
| 75 | + |
| 76 | + target_link_libraries(${target_name} PUBLIC test_utils) |
36 | 77 | endfunction() |
| 78 | + |
0 commit comments