Files
2026-08-03 12:41:26 +08:00

89 lines
4.4 KiB
CMake

include_guard(GLOBAL)
include("${CMAKE_CURRENT_LIST_DIR}/../Common/internal.cmake")
function(psc_add_coupling_target target_name)
set(options)
set(one_value_args OUTPUT_DIR TOP_N DOT_EXECUTABLE)
set(multi_value_args INPUT_DIRS EXCLUDE_DIRS DEPENDS)
cmake_parse_arguments(ARG "${options}" "${one_value_args}" "${multi_value_args}" ${ARGN})
_psc_validate_target_call(psc_add_coupling_target "${target_name}" ARG)
if (NOT ARG_OUTPUT_DIR)
set(ARG_OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/metrics/${target_name}/coupling")
endif()
if ("${ARG_TOP_N}" STREQUAL "")
set(ARG_TOP_N 50)
endif()
if (NOT ARG_INPUT_DIRS)
set(ARG_INPUT_DIRS "${CMAKE_CURRENT_SOURCE_DIR}")
endif()
_psc_absolute_path(output_dir "${ARG_OUTPUT_DIR}" "${CMAKE_CURRENT_BINARY_DIR}")
_psc_absolute_paths(input_dirs "${CMAKE_CURRENT_SOURCE_DIR}" ${ARG_INPUT_DIRS})
_psc_absolute_paths(exclude_dirs "${CMAKE_CURRENT_SOURCE_DIR}" ${ARG_EXCLUDE_DIRS})
_psc_resolve_executable(pwsh "" "${PSC_PWSH_EXECUTABLE}" "PowerShell 7")
_psc_resolve_executable(dot "${ARG_DOT_EXECUTABLE}" "${PSC_DOT_EXECUTABLE}" "Graphviz dot")
_psc_ps_array(PSC_COUPLING_INPUT_DIRS ${input_dirs})
_psc_ps_array(PSC_COUPLING_EXCLUDE_DIRS ${exclude_dirs})
set(PSC_COUPLING_OUTPUT_DIR "${output_dir}")
set(PSC_COUPLING_TOP_N "${ARG_TOP_N}")
set(PSC_COUPLING_DOT "${dot}")
set(script_dir "${CMAKE_CURRENT_BINARY_DIR}/psc_metrics_scripts/${target_name}")
file(MAKE_DIRECTORY "${script_dir}")
set(script "${script_dir}/${target_name}_coupling.ps1")
configure_file("${CMAKE_CURRENT_FUNCTION_LIST_DIR}/Coupling.ps1.in" "${script}" @ONLY)
add_custom_target("${target_name}"
COMMAND "${pwsh}" -NoProfile -File "${script}"
DEPENDS ${ARG_DEPENDS}
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
VERBATIM
)
_psc_set_quality_target_properties("${target_name}" coupling "${output_dir}")
endfunction()
function(psc_add_metrics_report_target target_name)
set(options)
set(one_value_args OUTPUT_DIR PROJECT_NAME)
set(multi_value_args INPUT_TARGETS DEPENDS)
cmake_parse_arguments(ARG "${options}" "${one_value_args}" "${multi_value_args}" ${ARGN})
_psc_validate_target_call(psc_add_metrics_report_target "${target_name}" ARG)
if (NOT ARG_INPUT_TARGETS)
message(FATAL_ERROR "psc_add_metrics_report_target: INPUT_TARGETS is required")
endif()
if (NOT ARG_OUTPUT_DIR)
set(ARG_OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/metrics/${target_name}")
endif()
if (NOT ARG_PROJECT_NAME)
set(ARG_PROJECT_NAME "${PROJECT_NAME}")
endif()
_psc_absolute_path(output_dir "${ARG_OUTPUT_DIR}" "${CMAKE_CURRENT_BINARY_DIR}")
set(report_inputs)
set(report_kinds)
foreach(input_target IN LISTS ARG_INPUT_TARGETS)
if (NOT TARGET "${input_target}")
message(FATAL_ERROR "psc_add_metrics_report_target: input target does not exist: ${input_target}")
endif()
get_property(kind TARGET "${input_target}" PROPERTY PSC_QUALITY_KIND)
get_property(input_dir TARGET "${input_target}" PROPERTY PSC_QUALITY_OUTPUT_DIR)
if (NOT kind OR NOT input_dir)
message(FATAL_ERROR "psc_add_metrics_report_target: target is not a quality target: ${input_target}")
endif()
if (kind IN_LIST report_kinds)
message(FATAL_ERROR "psc_add_metrics_report_target: duplicate quality kind: ${kind}")
endif()
list(APPEND report_kinds "${kind}")
list(APPEND report_inputs "${kind}|${input_dir}")
endforeach()
_psc_resolve_executable(pwsh "" "${PSC_PWSH_EXECUTABLE}" "PowerShell 7")
_psc_ps_array(PSC_REPORT_INPUTS ${report_inputs})
set(PSC_REPORT_OUTPUT_DIR "${output_dir}")
set(PSC_REPORT_PROJECT_NAME "${ARG_PROJECT_NAME}")
set(script_dir "${CMAKE_CURRENT_BINARY_DIR}/psc_metrics_scripts/${target_name}")
file(MAKE_DIRECTORY "${script_dir}")
set(script "${script_dir}/${target_name}_report.ps1")
configure_file("${CMAKE_CURRENT_FUNCTION_LIST_DIR}/Report.ps1.in" "${script}" @ONLY)
add_custom_target("${target_name}"
COMMAND "${pwsh}" -NoProfile -File "${script}"
DEPENDS ${ARG_INPUT_TARGETS} ${ARG_DEPENDS}
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
VERBATIM
)
_psc_set_artifact_target_properties("${target_name}" metrics_report "${output_dir}")
endfunction()