From 90a93396864679d264b31e8634aba403908a036a Mon Sep 17 00:00:00 2001 From: NiccoloN Date: Thu, 21 May 2026 14:13:54 +0200 Subject: [PATCH] better cmake to keep IDEs analyses happy --- CMakeLists.txt | 116 +++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 92 insertions(+), 24 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6322be0..7b7a5d3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,31 +3,99 @@ cmake_minimum_required(VERSION 3.20.0) project(raptor) -# Add symlink to PIM as accelerator in onnx-mlir -function(raptor_ensure_symlink link_path target_path) - get_filename_component(link_parent "${link_path}" DIRECTORY) +# Materialize a CMake shim directory +function(raptor_write_external_cmake_shim shim_dir external_source_dir description) + get_filename_component(real_external_source_dir "${external_source_dir}" REALPATH) + file(RELATIVE_PATH relative_external_source_dir "${shim_dir}" "${real_external_source_dir}") - if(NOT EXISTS "${link_parent}") - message(FATAL_ERROR "Directory not found: ${link_parent}") - endif() - - if(NOT EXISTS "${link_path}") - message(STATUS "Creating symlink ${link_path} -> ${target_path}") - file(CREATE_LINK - "${target_path}" - "${link_path}" - SYMBOLIC + if (NOT EXISTS "${real_external_source_dir}/CMakeLists.txt") + message(FATAL_ERROR + "External CMake source directory not found or missing CMakeLists.txt:\n" + " ${real_external_source_dir}" ) - endif() + endif () + + if (IS_SYMLINK "${shim_dir}") + message(STATUS "Removing old full-directory symlink: ${shim_dir}") + file(REMOVE "${shim_dir}") + endif () + + if (EXISTS "${shim_dir}" AND NOT IS_DIRECTORY "${shim_dir}") + message(FATAL_ERROR "Expected directory or absent path, got file: ${shim_dir}") + endif () + + file(MAKE_DIRECTORY "${shim_dir}") + + set(shim_file "${shim_dir}/CMakeLists.txt") + set(shim_contents + "get_filename_component(raptor_external_source_dir + \"\${CMAKE_CURRENT_LIST_DIR}/${relative_external_source_dir}\" + REALPATH +) +add_subdirectory( + \"\${raptor_external_source_dir}\" + \"\${CMAKE_CURRENT_BINARY_DIR}/raptor-external\" +) +if (DEFINED PIM_ENABLED) + set(PIM_ENABLED \"\${PIM_ENABLED}\" PARENT_SCOPE) +endif () +" + ) + + if (EXISTS "${shim_file}") + file(READ "${shim_file}" old_contents) + else () + set(old_contents "") + endif () + + if (NOT old_contents STREQUAL shim_contents) + file(WRITE "${shim_file}" "${shim_contents}") + message(STATUS "Wrote CMake shim for ${description}: ${shim_file}") + else () + message(STATUS "CMake shim already up to date for ${description}") + endif () + + # Mirror the external tree's first-level entries into the shim directory + # so legacy includes like src/Accelerators/PIM/Compiler/... keep working. + file(GLOB children RELATIVE "${real_external_source_dir}" "${real_external_source_dir}/*") + + foreach (child IN LISTS children) + if (child STREQUAL "CMakeLists.txt") + continue() + endif () + + set(real_child "${real_external_source_dir}/${child}") + set(shim_child "${shim_dir}/${child}") + + if (IS_SYMLINK "${shim_child}") + file(READ_SYMLINK "${shim_child}" existing_link_target) + if (existing_link_target STREQUAL real_child) + continue() + endif () + file(REMOVE_RECURSE "${shim_child}") + elseif (EXISTS "${shim_child}") + # Do not delete real files/directories. This protects the generated shim. + continue() + endif () + + file(CREATE_LINK + "${real_child}" + "${shim_child}" + SYMBOLIC + ) + endforeach () endfunction() -raptor_ensure_symlink( - "${CMAKE_CURRENT_SOURCE_DIR}/onnx-mlir/src/Accelerators/PIM" - "${CMAKE_CURRENT_SOURCE_DIR}/src/PIM" +raptor_write_external_cmake_shim( + "${CMAKE_CURRENT_SOURCE_DIR}/onnx-mlir/src/Accelerators/PIM" + "${CMAKE_CURRENT_SOURCE_DIR}/src/PIM" + "PIM accelerator" ) -raptor_ensure_symlink( - "${CMAKE_CURRENT_SOURCE_DIR}/onnx-mlir/test/accelerators/PIM" - "${CMAKE_CURRENT_SOURCE_DIR}/test/PIM" + +raptor_write_external_cmake_shim( + "${CMAKE_CURRENT_SOURCE_DIR}/onnx-mlir/test/accelerators/PIM" + "${CMAKE_CURRENT_SOURCE_DIR}/test/PIM" + "PIM accelerator tests" ) # Patch onnx-mlir sources for PIM accelerator support. @@ -38,21 +106,21 @@ function(raptor_apply_patch file_path anchor replacement description) # Already applied – replacement text is present string(FIND "${contents}" "${replacement}" already_applied_pos) - if(NOT already_applied_pos EQUAL -1) + if (NOT already_applied_pos EQUAL -1) message(STATUS "Patch already applied: ${description}") return() - endif() + endif () # Anchor must exist for the patch to be applicable string(FIND "${contents}" "${anchor}" anchor_pos) - if(anchor_pos EQUAL -1) + if (anchor_pos EQUAL -1) message(FATAL_ERROR "Patch anchor not found – onnx-mlir may have changed.\n" " Patch : ${description}\n" " File : ${file_path}\n" " Anchor: ${anchor}" ) - endif() + endif () string(REPLACE "${anchor}" "${replacement}" patched "${contents}") file(WRITE "${file_path}" "${patched}")