From 40b1abf9d3ec8eb3f798946263f144988fa3a416 Mon Sep 17 00:00:00 2001
From: Robin Voetter <rvoetter@amd.com>
Date: Sat, 4 Apr 2026 21:50:57 +0000
Subject: [PATCH] [rocprofiler-sdk] allow using system elfio dependency

This adds a new option, ROCPROFILER_BUILD_ELFIO, which can be set to OFF to
make rocprofiler use a system dependency for ELFIO instead of trying to build
it manually. To mimick old behavior, ROCPROFILER_BUILD_ELFIO is set to ON by
default.

Related: https://github.com/ROCm/rocm-systems/pull/2319
---
 cmake/rocprofiler_options.cmake |  2 ++
 external/CMakeLists.txt         | 33 +++++++++++++++++++--------------
 2 files changed, 21 insertions(+), 14 deletions(-)

diff --git a/cmake/rocprofiler_options.cmake b/cmake/rocprofiler_options.cmake
index c29311cc8c..da9ee9ad29 100644
--- a/cmake/rocprofiler_options.cmake
+++ b/cmake/rocprofiler_options.cmake
@@ -67,6 +67,8 @@ rocprofiler_add_option(ROCPROFILER_BUILD_GOTCHA
                        "Enable building gotcha library internally" ON)
 rocprofiler_add_option(ROCPROFILER_BUILD_YAML_CPP
                        "Enable building yaml-cpp library internally" ON)
+rocprofiler_add_option(ROCPROFILER_BUILD_ELFIO "Enable building ELFIO library internally"
+                       ON)
 if(ROCPROFILER_BUILD_TESTS)
     rocprofiler_add_option(
         ROCPROFILER_BUILD_GTEST
diff --git a/external/CMakeLists.txt b/external/CMakeLists.txt
index fcc872d118..ade8d25c1b 100644
--- a/external/CMakeLists.txt
+++ b/external/CMakeLists.txt
@@ -201,20 +201,25 @@ target_link_libraries(
     INTERFACE $<BUILD_INTERFACE:rocprofiler-sdk-perfetto-static-library>)
 
 # ELFIO
-rocprofiler_checkout_git_submodule(
-    RECURSIVE
-    RELATIVE_PATH external/elfio
-    WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
-    REPO_URL https://github.com/serge1/ELFIO.git
-    REPO_BRANCH "Release_3.12")
-
-set(ELFIO_BUILD_EXAMPLES OFF)
-set(ELFIO_BUILD_TESTS OFF)
-add_subdirectory(elfio EXCLUDE_FROM_ALL)
-if(TARGET rocprofiler-sdk-elfio)
-    get_target_property(ELFIO_INCLUDE_DIR elfio::elfio INTERFACE_INCLUDE_DIRECTORIES)
-    target_include_directories(rocprofiler-sdk-elfio SYSTEM
-                               INTERFACE ${ELFIO_INCLUDE_DIR})
+if(ROCPROFILER_BUILD_ELFIO)
+    rocprofiler_checkout_git_submodule(
+        RECURSIVE
+        RELATIVE_PATH external/elfio
+        WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
+        REPO_URL https://github.com/serge1/ELFIO.git
+        REPO_BRANCH "Release_3.12")
+
+    set(ELFIO_BUILD_EXAMPLES OFF)
+    set(ELFIO_BUILD_TESTS OFF)
+    add_subdirectory(elfio EXCLUDE_FROM_ALL)
+    if(TARGET rocprofiler-sdk-elfio)
+        get_target_property(ELFIO_INCLUDE_DIR elfio::elfio INTERFACE_INCLUDE_DIRECTORIES)
+        target_include_directories(rocprofiler-sdk-elfio SYSTEM
+                                   INTERFACE ${ELFIO_INCLUDE_DIR})
+    endif()
+else()
+    find_package(elfio REQUIRED GLOBAL)
+    target_link_libraries(rocprofiler-sdk-elfio INTERFACE elfio::elfio)
 endif()
 
 #
-- 
2.43.0

