Add 'projects/rocprofiler/' from commit '16ae2e90c6157e98e846d2bccbaaf533ca5e662a'

git-subtree-dir: projects/rocprofiler
git-subtree-mainline: 2a52e3974d
git-subtree-split: 16ae2e90c6
这个提交包含在:
systems-assistant[bot]
2025-07-22 22:52:43 +00:00
当前提交 2e5bcec303
修改 386 个文件,包含 216890 行新增0 行删除
@@ -0,0 +1,21 @@
# Try to find LIBDW
#
# Once found, this will define:
# - LIBDW_FOUND - system has libelf
# - LIBDW_INCLUDE_DIRS - the libelf include directory
# - LIBDW_LIBRARIES - Link these to use libelf
# - LIBDW_DEFINITIONS - Compiler switches required for using libelf
find_path(
FIND_LIBDW_INCLUDES
NAMES elfutils/libdw.h
PATHS /usr/include /usr/local/include)
find_library(FIND_LIBDW_LIBRARIES NAMES dw PATH /usr/lib /usr/local/lib)
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(LibDw DEFAULT_MSG FIND_LIBDW_INCLUDES
FIND_LIBDW_LIBRARIES)
mark_as_advanced(FIND_LIBDW_INCLUDES FIND_LIBDW_LIBRARIES)
set(LIBDW_INCLUDES ${FIND_LIBDW_INCLUDES})
set(LIBDW_LIBRARIES ${FIND_LIBDW_LIBRARIES})
@@ -0,0 +1,21 @@
# Try to find LIBELF
#
# Once found, this will define:
# - LIBELF_FOUND - system has libelf
# - LIBELF_INCLUDE_DIRS - the libelf include directory
# - LIBELF_LIBRARIES - Link these to use libelf
# - LIBELF_DEFINITIONS - Compiler switches required for using libelf
find_path(
FIND_LIBELF_INCLUDES
NAMES libelf.h
PATHS /usr/include /usr/include/libelf /usr/local/include /usr/local/include/libelf)
find_library(FIND_LIBELF_LIBRARIES NAMES elf PATH /usr/lib /usr/local/lib)
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(LibElf DEFAULT_MSG FIND_LIBELF_INCLUDES
FIND_LIBELF_LIBRARIES)
mark_as_advanced(FIND_LIBELF_INCLUDES FIND_LIBELF_LIBRARIES)
set(LIBELF_INCLUDES ${FIND_LIBELF_INCLUDES})
set(LIBELF_LIBRARIES ${FIND_LIBELF_LIBRARIES})
@@ -0,0 +1,11 @@
cmake_minimum_required(VERSION 3.18.0 FATAL_ERROR)
if(NOT @GTEST_DISCOVER_TESTS_TARGET@_TESTS)
message(FATAL_ERROR "@GTEST_DISCOVER_TESTS_TARGET@_TESTS is not defined")
endif()
foreach(_TEST ${@GTEST_DISCOVER_TESTS_TARGET@_TESTS})
set_tests_properties(
${_TEST} PROPERTIES LABELS "@GTEST_DISCOVER_TESTS_LABELS@" ENVIRONMENT
"@GTEST_DISCOVER_TESTS_ENVIRONMENT@")
endforeach()
@@ -0,0 +1,76 @@
################################################################################
# Copyright (c) 2018 Advanced Micro Devices, Inc. All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
################################################################################
add_library(rocprofiler-build-flags INTERFACE)
add_library(rocprofiler::build-flags ALIAS rocprofiler-build-flags)
target_compile_options(
rocprofiler-build-flags
INTERFACE $<$<COMPILE_LANGUAGE:C,CXX>:-W -Wall -Wextra -Wno-unused-parameter>
$<$<COMPILE_LANGUAGE:CXX>:-fms-extensions>
$<$<COMPILE_LANGUAGE:CXX>:$<$<CXX_COMPILER_ID:Clang>:-ferror-limit=1000000>>
)
target_compile_definitions(rocprofiler-build-flags INTERFACE NEW_TRACE_API=1)
# Enable debug trace
if(ROCPROFILER_DEBUG_TRACE)
target_compile_definitions(rocprofiler-build-flags INTERFACE DEBUG_TRACE=1)
endif()
# Enable direct loading of AQL-profile HSA extension
if(ROCPROFILER_LD_AQLPROFILE)
target_compile_definitions(rocprofiler-build-flags INTERFACE ROCP_LD_AQLPROFILE=1)
endif()
if(NOT DEFINED ROCM_PATH)
set(ROCM_PATH "/opt/rocm" CACHE STRING "Default ROCM installation directory")
endif()
# Find hsa-runtime
find_package(
hsa-runtime64 CONFIG REQUIRED
HINTS ${CMAKE_PREFIX_PATH}
PATHS ${ROCM_PATH}
PATH_SUFFIXES lib/cmake/hsa-runtime64)
# Include path: /opt/rocm-ver/include. Go up one level to get ROCm path
get_filename_component(ROCM_ROOT_DIR ${ROCM_PATH}/include DIRECTORY)
# Basic Tool Chain Information
message("----------Build-Type: ${CMAKE_BUILD_TYPE}")
message("------------Compiler: ${CMAKE_CXX_COMPILER}")
message("----Compiler-Version: ${CMAKE_CXX_COMPILER_VERSION}")
message("-------ROCM_ROOT_DIR: ${ROCM_ROOT_DIR}")
message("-----CMAKE_CXX_FLAGS: ${CMAKE_CXX_FLAGS}")
message("---CMAKE_PREFIX_PATH: ${CMAKE_PREFIX_PATH}")
message("---------GPU_TARGETS: ${GPU_TARGETS}")
if("${ROCM_ROOT_DIR}" STREQUAL "")
message(FATAL_ERROR "ROCM_ROOT_DIR is not found.")
endif()
find_library(
HSA_AMD_AQLPROFILE_LIBRARY
NAMES hsa-amd-aqlprofile64
HINTS ${CMAKE_PREFIX_PATH}
PATHS ${ROCM_ROOT_DIR}
PATH_SUFFIXES lib REQUIRED)
@@ -0,0 +1,103 @@
# ------------------------------------------------------------------------------#
#
# creates following targets to format code:
# - format
# - format-source
# - format-cmake
# - format-python
# - format-rocprofiler-source
# - format-rocprofiler-cmake
# - format-rocprofiler-python
#
# ------------------------------------------------------------------------------#
include_guard(GLOBAL)
find_program(ROCPROFILER_CLANG_FORMAT_EXE NAMES clang-format-11 clang-format-mp-11)
find_program(ROCPROFILER_CMAKE_FORMAT_EXE NAMES cmake-format)
find_program(ROCPROFILER_BLACK_FORMAT_EXE NAMES black)
if(ROCPROFILER_CLANG_FORMAT_EXE
OR ROCPROFILER_BLACK_FORMAT_EXE
OR ROCPROFILER_CMAKE_FORMAT_EXE)
add_custom_target(format-rocprofiler)
if(NOT TARGET format)
add_custom_target(format)
endif()
foreach(_TYPE source python cmake)
if(NOT TARGET format-${_TYPE})
add_custom_target(format-${_TYPE})
endif()
endforeach()
set(rocp_sources)
set(rocp_headers)
set(rocp_cmake_files)
set(rocp_python_files)
foreach(_DIR include src plugin samples test tests-v2 script cmake_modules)
foreach(_TYPE headers sources cmake_files python_files)
set(${_TYPE})
endforeach()
file(GLOB_RECURSE headers ${PROJECT_SOURCE_DIR}/${_DIR}/*.h)
file(GLOB_RECURSE sources ${PROJECT_SOURCE_DIR}/${_DIR}/*.cpp)
file(GLOB_RECURSE cmake_files ${PROJECT_SOURCE_DIR}/${_DIR}/*CMakeLists.txt
${PROJECT_SOURCE_DIR}/${_DIR}/*.cmake)
file(GLOB_RECURSE python_files ${PROJECT_SOURCE_DIR}/${_DIR}/*.py)
foreach(_TYPE headers sources cmake_files python_files)
list(APPEND rocp_${_TYPE} ${${_TYPE}})
endforeach()
endforeach()
if(ROCPROFILER_CLANG_FORMAT_EXE)
add_custom_target(
format-rocprofiler-source
${ROCPROFILER_CLANG_FORMAT_EXE} -i ${rocp_sources} ${rocp_headers}
COMMENT
"[rocprofiler] Running source formatter ${ROCPROFILER_CLANG_FORMAT_EXE}..."
)
endif()
if(ROCPROFILER_BLACK_FORMAT_EXE)
add_custom_target(
format-rocprofiler-python
${ROCPROFILER_BLACK_FORMAT_EXE} -q ${rocp_python_files}
COMMENT
"[rocprofiler] Running Python formatter ${ROCPROFILER_BLACK_FORMAT_EXE}..."
)
if(NOT TARGET format-python)
add_custom_target(format-python)
endif()
endif()
if(ROCPROFILER_CMAKE_FORMAT_EXE)
add_custom_target(
format-rocprofiler-cmake
${ROCPROFILER_CMAKE_FORMAT_EXE} -i ${rocp_cmake_files}
COMMENT
"[rocprofiler] Running CMake formatter ${ROCPROFILER_CMAKE_FORMAT_EXE}..."
)
if(NOT TARGET format-cmake)
add_custom_target(format-cmake)
endif()
endif()
foreach(_TYPE source python cmake)
if(TARGET format-rocprofiler-${_TYPE})
add_dependencies(format-rocprofiler format-rocprofiler-${_TYPE})
add_dependencies(format-${_TYPE} format-rocprofiler-${_TYPE})
endif()
endforeach()
foreach(_TYPE source python)
if(TARGET format-rocprofiler-${_TYPE})
add_dependencies(format format-rocprofiler-${_TYPE})
endif()
endforeach()
else()
message(
STATUS
"no formatting tools (clang-format-11/black/cmake-format) could not be found. formatting build targets not available."
)
endif()
@@ -0,0 +1,30 @@
include_guard(DIRECTORY)
# ----------------------------------------------------------------------------------------#
#
# Clang Tidy
#
# ----------------------------------------------------------------------------------------#
if(ROCPROFILER_ENABLE_CLANG_TIDY)
find_program(ROCPROFILER_CLANG_TIDY_COMMAND NAMES clang-tidy)
if(NOT ROCPROFILER_CLANG_TIDY_COMMAND)
message(
WARNING "ROCPROFILER_ENABLE_CLANG_TIDY is ON but clang-tidy is not found!")
set(ROCPROFILER_ENABLE_CLANG_TIDY OFF)
else()
set(CMAKE_CXX_CLANG_TIDY ${ROCPROFILER_CLANG_TIDY_COMMAND}
-header-filter=${PROJECT_SOURCE_DIR}/.*)
# Create a preprocessor definition that depends on .clang-tidy content so the
# compile command will change when .clang-tidy changes. This ensures that a
# subsequent build re-runs clang-tidy on all sources even if they do not otherwise
# need to be recompiled. Nothing actually uses this definition. We add it to
# targets on which we run clang-tidy just to get the build dependency on the
# .clang-tidy file.
file(SHA1 ${PROJECT_SOURCE_DIR}/.clang-tidy clang_tidy_sha1)
set(CLANG_TIDY_DEFINITIONS "CLANG_TIDY_SHA1=${clang_tidy_sha1}")
unset(clang_tidy_sha1)
endif()
endif()
@@ -0,0 +1,139 @@
if("${PROJECT_SOURCE_DIR}" STREQUAL "${PROJECT_BINARY_DIR}")
message(STATUS "")
message(STATUS "rocprofiler does not support in-source builds.")
message(STATUS "Delete CMakeCache.txt and CMakeFiles in ${PROJECT_SOURCE_DIR}")
message(STATUS "and run cmake with `-B <build-directory>`")
message(STATUS "")
message(FATAL_ERROR "In-source build detected.")
endif()
option(ROCPROFILER_BUILD_TESTS "Enable building the tests" OFF)
option(ROCPROFILER_BUILD_SAMPLES "Enable building the code samples" OFF)
# CLI and FILE plugins are always built
foreach(_PLUGIN "ATT" "CTF" "PERFETTO")
option(ROCPROFILER_BUILD_PLUGIN_${_PLUGIN} "Enable building the ${_PLUGIN} plugin" ON)
endforeach()
option(ROCPROFILER_DEBUG_TRACE "Enable debug tracing" OFF)
mark_as_advanced(ROCPROFILER_DEBUG_TRACE)
option(ROCPROFILER_LD_AQLPROFILE "Enable direct loading of AQL-profile HSA extension" OFF)
mark_as_advanced(ROCPROFILER_LD_AQLPROFILE)
option(ROCPROFILER_BUILD_CI "Enable continuous integration additions" OFF)
mark_as_advanced(ROCPROFILER_BUILD_CI)
option(ROCPROFILER_ENABLE_CLANG_TIDY "Enable clang-tidy checks" OFF)
mark_as_advanced(ROCPROFILER_ENABLE_CLANG_TIDY)
set(ROCPROFILER_BUILD_TYPES "Release" "RelWithDebInfo" "Debug" "MinSizeRel" "Coverage")
# export compile commands in the project
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE
"Release"
CACHE STRING "Build type" FORCE)
endif()
if(NOT CMAKE_BUILD_TYPE IN_LIST ROCPROFILER_BUILD_TYPES)
message(
FATAL_ERROR
"Unsupported build type '${CMAKE_BUILD_TYPE}'. Options: ${ROCPROFILER_BUILD_TYPES}"
)
endif()
if(ROCPROFILER_BUILD_CI)
foreach(_BUILD_TYPE ${ROCPROFILER_BUILD_TYPES})
string(TOUPPER "${_BUILD_TYPE}" _BUILD_TYPE)
# remove NDEBUG preprocessor def so that asserts are triggered
string(REGEX REPLACE ".DNDEBUG" "" CMAKE_C_FLAGS_${_BUILD_TYPE}
"${CMAKE_C_FLAGS_${_BUILD_TYPE}}")
string(REGEX REPLACE ".DNDEBUG" "" CMAKE_CXX_FLAGS_${_BUILD_TYPE}
"${CMAKE_CXX_FLAGS_${_BUILD_TYPE}}")
endforeach()
endif()
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "${ROCPROFILER_BUILD_TYPES}")
endif()
set(ROCPROFILER_MEMCHECK
""
CACHE STRING "Memory checker type")
mark_as_advanced(ROCPROFILER_MEMCHECK)
# ASAN is defined by testing team on Jenkins
if(ASAN)
set(ROCPROFILER_MEMCHECK
"AddressSanitizer"
CACHE STRING "Memory checker type (forced by ASAN defined)" FORCE)
endif()
set(ROCPROFILER_MEMCHECK_TYPES "ThreadSanitizer" "AddressSanitizer" "LeakSanitizer"
"MemorySanitizer" "UndefinedBehaviorSanitizer")
if(ROCPROFILER_MEMCHECK AND NOT ROCPROFILER_MEMCHECK IN_LIST ROCPROFILER_MEMCHECK_TYPES)
message(
FATAL_ERROR
"Unsupported memcheck type '${ROCPROFILER_MEMCHECK}'. Options: ${ROCPROFILER_MEMCHECK_TYPES}"
)
endif()
set_property(CACHE ROCPROFILER_MEMCHECK PROPERTY STRINGS "${ROCPROFILER_MEMCHECK_TYPES}")
add_library(rocprofiler-memcheck INTERFACE)
add_library(rocprofiler::memcheck ALIAS rocprofiler-memcheck)
function(rocprofiler_add_memcheck_flags _TYPE)
target_compile_options(
rocprofiler-memcheck INTERFACE $<BUILD_INTERFACE:-g3 -Og -fno-omit-frame-pointer
-fsanitize=${_TYPE}>)
target_link_options(rocprofiler-memcheck INTERFACE
$<BUILD_INTERFACE:-fsanitize=${_TYPE} -Wl,--no-undefined>)
endfunction()
function(rocprofiler_set_memcheck_env _TYPE _LIB_BASE)
set(_LIBS ${_LIB_BASE})
foreach(_N 6 5 4 3 2 1 0)
list(
APPEND _LIBS
${CMAKE_SHARED_LIBRARY_PREFIX}${_LIB_BASE}${CMAKE_SHARED_LIBRARY_SUFFIX}.${_N}
)
endforeach()
foreach(_LIB ${_LIBS})
if(NOT ${_TYPE}_LIBRARY)
find_library(${_TYPE}_LIBRARY NAMES ${_LIB} ${ARGN})
endif()
endforeach()
target_link_libraries(rocprofiler-memcheck INTERFACE ${_LIB_BASE})
if(${_TYPE}_LIBRARY)
set(ROCPROFILER_MEMCHECK_PRELOAD_ENV
"LD_PRELOAD=${${_TYPE}_LIBRARY};LD_LIBRARY_PATH=${PROJECT_BINARY_DIR}/lib:$ENV{LD_LIBRARY_PATH}"
CACHE INTERNAL "LD_PRELOAD env variable for tests" FORCE)
endif()
endfunction()
# always unset so that it doesn't preload if memcheck disabled
unset(ROCPROFILER_MEMCHECK_PRELOAD_ENV CACHE)
if(ROCPROFILER_MEMCHECK STREQUAL "AddressSanitizer")
rocprofiler_add_memcheck_flags("address")
rocprofiler_set_memcheck_env("${ROCPROFILER_MEMCHECK}" "asan")
elseif(ROCPROFILER_MEMCHECK STREQUAL "LeakSanitizer")
rocprofiler_add_memcheck_flags("leak")
rocprofiler_set_memcheck_env("${ROCPROFILER_MEMCHECK}" "lsan")
elseif(ROCPROFILER_MEMCHECK STREQUAL "MemorySanitizer")
rocprofiler_add_memcheck_flags("memory")
elseif(ROCPROFILER_MEMCHECK STREQUAL "ThreadSanitizer")
rocprofiler_add_memcheck_flags("thread")
rocprofiler_set_memcheck_env("${ROCPROFILER_MEMCHECK}" "tsan")
elseif(ROCPROFILER_MEMCHECK STREQUAL "UndefinedBehaviorSanitizer")
rocprofiler_add_memcheck_flags("undefined")
rocprofiler_set_memcheck_env("${ROCPROFILER_MEMCHECK}" "ubsan")
endif()
@@ -0,0 +1,263 @@
################################################################################
# Copyright (c) 2018 Advanced Micro Devices, Inc. All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
################################################################################
# Parses the VERSION_STRING variable and places the first, second and third number values
# in the major, minor and patch variables.
function(rocprofiler_parse_version VERSION_STRING)
string(FIND ${VERSION_STRING} "-" STRING_INDEX)
if(${STRING_INDEX} GREATER -1)
math(EXPR STRING_INDEX "${STRING_INDEX} + 1")
string(SUBSTRING ${VERSION_STRING} ${STRING_INDEX} -1 VERSION_BUILD)
endif()
string(REGEX MATCHALL "[0123456789]+" VERSIONS ${VERSION_STRING})
list(LENGTH VERSIONS VERSION_COUNT)
if(${VERSION_COUNT} GREATER 0)
list(GET VERSIONS 0 MAJOR)
set(VERSION_MAJOR
${MAJOR}
PARENT_SCOPE)
set(TEMP_VERSION_STRING "${MAJOR}")
endif()
if(${VERSION_COUNT} GREATER 1)
list(GET VERSIONS 1 MINOR)
set(VERSION_MINOR
${MINOR}
PARENT_SCOPE)
set(TEMP_VERSION_STRING "${TEMP_VERSION_STRING}.${MINOR}")
endif()
if(${VERSION_COUNT} GREATER 2)
list(GET VERSIONS 2 PATCH)
set(VERSION_PATCH
${PATCH}
PARENT_SCOPE)
set(TEMP_VERSION_STRING "${TEMP_VERSION_STRING}.${PATCH}")
endif()
if(DEFINED VERSION_BUILD)
set(VERSION_BUILD
"${VERSION_BUILD}"
PARENT_SCOPE)
endif()
set(VERSION_STRING
"${TEMP_VERSION_STRING}"
PARENT_SCOPE)
endfunction()
# Gets the current version of the repository using versioning tags and git describe.
# Passes back a packaging version string and a library version string.
function(rocprofiler_get_version DEFAULT_VERSION_STRING)
rocprofiler_parse_version(${DEFAULT_VERSION_STRING})
find_program(GIT NAMES git)
if(GIT)
execute_process(
COMMAND "git describe --dirty --long --match [0-9]* 2>/dev/null"
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
OUTPUT_VARIABLE GIT_TAG_STRING
OUTPUT_STRIP_TRAILING_WHITESPACE
RESULT_VARIABLE RESULT)
if(${RESULT} EQUAL 0)
rocprofiler_parse_version(${GIT_TAG_STRING})
endif()
endif()
set(VERSION_STRING
"${VERSION_STRING}"
PARENT_SCOPE)
set(VERSION_MAJOR
"${VERSION_MAJOR}"
PARENT_SCOPE)
set(VERSION_MINOR
"${VERSION_MINOR}"
PARENT_SCOPE)
set(VERSION_PATCH
"${VERSION_PATCH}"
PARENT_SCOPE)
set(VERSION_BUILD
"${VERSION_BUILD}"
PARENT_SCOPE)
endfunction()
# ----------------------------------------------------------------------------------------#
# macro rocprofiler_checkout_git_submodule()
#
# Run "git submodule update" if a file in a submodule does not exist
#
# ARGS: RECURSIVE (option) -- add "--recursive" flag RELATIVE_PATH (one value) --
# typically the relative path to submodule from PROJECT_SOURCE_DIR WORKING_DIRECTORY (one
# value) -- (default: PROJECT_SOURCE_DIR) TEST_FILE (one value) -- file to check for
# (default: CMakeLists.txt) ADDITIONAL_CMDS (many value) -- any addition commands to pass
#
function(ROCPROFILER_CHECKOUT_GIT_SUBMODULE)
# parse args
cmake_parse_arguments(
CHECKOUT "RECURSIVE"
"RELATIVE_PATH;WORKING_DIRECTORY;TEST_FILE;REPO_URL;REPO_BRANCH"
"ADDITIONAL_CMDS" ${ARGN})
if(NOT CHECKOUT_WORKING_DIRECTORY)
set(CHECKOUT_WORKING_DIRECTORY ${PROJECT_SOURCE_DIR})
endif()
if(NOT CHECKOUT_TEST_FILE)
set(CHECKOUT_TEST_FILE "CMakeLists.txt")
endif()
find_package(Git)
set(_DIR "${CHECKOUT_WORKING_DIRECTORY}/${CHECKOUT_RELATIVE_PATH}")
# ensure the (possibly empty) directory exists
if(NOT EXISTS "${_DIR}")
if(NOT CHECKOUT_REPO_URL)
message(FATAL_ERROR "submodule directory does not exist")
endif()
endif()
# if this file exists --> project has been checked out if not exists --> not been
# checked out
set(_TEST_FILE "${_DIR}/${CHECKOUT_TEST_FILE}")
# assuming a .gitmodules file exists
set(_SUBMODULE "${PROJECT_SOURCE_DIR}/.gitmodules")
set(_TEST_FILE_EXISTS OFF)
if(EXISTS "${_TEST_FILE}" AND NOT IS_DIRECTORY "${_TEST_FILE}")
set(_TEST_FILE_EXISTS ON)
endif()
if(_TEST_FILE_EXISTS)
return()
endif()
find_package(Git REQUIRED)
set(_SUBMODULE_EXISTS OFF)
if(EXISTS "${_SUBMODULE}" AND NOT IS_DIRECTORY "${_SUBMODULE}")
set(_SUBMODULE_EXISTS ON)
endif()
set(_HAS_REPO_URL OFF)
if(NOT "${CHECKOUT_REPO_URL}" STREQUAL "")
set(_HAS_REPO_URL ON)
endif()
# if the module has not been checked out
if(NOT _TEST_FILE_EXISTS AND _SUBMODULE_EXISTS)
# perform the checkout
execute_process(
COMMAND ${GIT_EXECUTABLE} submodule update --init ${_RECURSE}
${CHECKOUT_ADDITIONAL_CMDS} ${CHECKOUT_RELATIVE_PATH}
WORKING_DIRECTORY ${CHECKOUT_WORKING_DIRECTORY}
RESULT_VARIABLE RET)
# check the return code
if(RET GREATER 0)
set(_CMD "${GIT_EXECUTABLE} submodule update --init ${_RECURSE}
${CHECKOUT_ADDITIONAL_CMDS} ${CHECKOUT_RELATIVE_PATH}")
message(STATUS "function(rocprofiler_checkout_git_submodule) failed.")
message(FATAL_ERROR "Command: \"${_CMD}\"")
else()
set(_TEST_FILE_EXISTS ON)
endif()
endif()
if(NOT _TEST_FILE_EXISTS AND _HAS_REPO_URL)
message(
STATUS
"Cloning '${CHECKOUT_REPO_URL}' into ${CHECKOUT_WORKING_DIRECTORY}/${CHECKOUT_RELATIVE_PATH}..."
)
# remove the existing directory
if(EXISTS "${_DIR}")
execute_process(COMMAND ${CMAKE_COMMAND} -E remove_directory ${_DIR})
endif()
# perform the checkout
execute_process(
COMMAND ${GIT_EXECUTABLE} clone ${CHECKOUT_ADDITIONAL_CMDS}
${CHECKOUT_REPO_URL} ${CHECKOUT_RELATIVE_PATH}
WORKING_DIRECTORY ${CHECKOUT_WORKING_DIRECTORY}
RESULT_VARIABLE RET_CLONE)
if(NOT RET_CLONE EQUAL 0)
message(
SEND_ERROR
"Failed to clone ${CHECKOUT_REPO_URL} into ${CHECKOUT_WORKING_DIRECTORY}/${CHECKOUT_RELATIVE_PATH}"
)
return()
endif()
if(CHECKOUT_REPO_BRANCH)
execute_process(
COMMAND ${GIT_EXECUTABLE} checkout ${CHECKOUT_REPO_BRANCH}
WORKING_DIRECTORY ${CHECKOUT_WORKING_DIRECTORY}/${CHECKOUT_RELATIVE_PATH}
RESULT_VARIABLE RET_BRANCH)
if(NOT RET_BRANCH EQUAL 0)
message(
SEND_ERROR
"Failed to checkout '${CHECKOUT_REPO_BRANCH}' for ${CHECKOUT_REPO_URL} in ${CHECKOUT_WORKING_DIRECTORY}/${CHECKOUT_RELATIVE_PATH}"
)
return()
endif()
endif()
# perform the submodule update
if(CHECKOUT_RECURSIVE
AND EXISTS "${_DIR}"
AND IS_DIRECTORY "${_DIR}")
execute_process(
COMMAND ${GIT_EXECUTABLE} submodule update --init ${_RECURSE}
WORKING_DIRECTORY ${_DIR}
RESULT_VARIABLE RET_RECURSIVE)
if(NOT RET_RECURSIVE EQUAL 0)
message(
SEND_ERROR
"Failed to update submodules for ${CHECKOUT_REPO_URL} in ${CHECKOUT_WORKING_DIRECTORY}/${CHECKOUT_RELATIVE_PATH}"
)
return()
endif()
endif()
set(_TEST_FILE_EXISTS ON)
endif()
if(NOT EXISTS "${_TEST_FILE}" OR NOT _TEST_FILE_EXISTS)
message(
FATAL_ERROR
"Error checking out submodule: '${CHECKOUT_RELATIVE_PATH}' to '${_DIR}'")
endif()
endfunction()