From d017c4b42b27fe23d6279515289510128f6a3785 Mon Sep 17 00:00:00 2001 From: Chris Austen Date: Tue, 25 Jan 2022 09:50:56 -0500 Subject: [PATCH] Improve handling of generator expressions when getting the flags for hip (#1055) (#1063) Improve handling of generator expressions when getting the flags for hip Authored-by: Paul Fultz II --- cmake/TargetFlags.cmake | 63 ++++++++++++++++++++++++++-------- src/targets/gpu/CMakeLists.txt | 8 +++-- 2 files changed, 55 insertions(+), 16 deletions(-) diff --git a/cmake/TargetFlags.cmake b/cmake/TargetFlags.cmake index 4f83fb5d396..ede876fb55f 100644 --- a/cmake/TargetFlags.cmake +++ b/cmake/TargetFlags.cmake @@ -1,38 +1,73 @@ +function(eval_and_strip_genex OUTPUT_VAR INPUT) + string(REPLACE "$" "1" INPUT "${INPUT}") + string(REPLACE "$" "1" INPUT "${INPUT}") + string(REPLACE "SHELL:" "" INPUT "${INPUT}") + string(REPLACE "$" "0" INPUT "${INPUT}") + string(REGEX REPLACE "\\$" "0" INPUT "${INPUT}") + string(REGEX REPLACE "\\$]*-NOTFOUND>" "0" INPUT "${INPUT}") + string(REGEX REPLACE "\\$]*>" "1" INPUT "${INPUT}") + string(REPLACE "$" "1" INPUT "${INPUT}") + string(REPLACE "$" "0" INPUT "${INPUT}") + string(REGEX REPLACE "\\$<0:[^<>]*>" "" INPUT "${INPUT}") + string(REGEX REPLACE "\\$<1:([^<>]*)>" "\\1" INPUT "${INPUT}") + string(GENEX_STRIP "${INPUT}" INPUT) + set(${OUTPUT_VAR} "${INPUT}" PARENT_SCOPE) +endfunction() + function(get_target_property2 VAR TARGET PROPERTY) get_target_property(_pflags ${TARGET} ${PROPERTY}) if(_pflags) + eval_and_strip_genex(_pflags "${_pflags}") set(${VAR} ${_pflags} PARENT_SCOPE) else() set(${VAR} "" PARENT_SCOPE) endif() endfunction() +function(flags_requires_arg OUTPUT_VAR FLAG) + set(_args -x -isystem) + if(FLAG IN_LIST _args) + set(${OUTPUT_VAR} 1 PARENT_SCOPE) + else() + set(${OUTPUT_VAR} 0 PARENT_SCOPE) + endif() +endfunction() macro(append_flags FLAGS TARGET PROPERTY PREFIX) get_target_property2(_pflags ${TARGET} ${PROPERTY}) + set(_requires_arg 0) foreach(FLAG ${_pflags}) - if(TARGET ${FLAG}) - target_flags(_pflags2 ${FLAG}) - string(APPEND ${FLAGS} " ${_pflags2}") - else() - string(APPEND ${FLAGS} " ${PREFIX}${FLAG}") + string(STRIP "${FLAG}" FLAG) + if(FLAG) + if(TARGET ${FLAG} AND NOT _requires_arg) + target_flags(_pflags2 ${FLAG}) + string(APPEND ${FLAGS} " ${_pflags2}") + else() + string(APPEND ${FLAGS} " ${PREFIX}${FLAG}") + endif() + flags_requires_arg(_requires_arg "${FLAG}") endif() endforeach() endmacro() macro(append_link_flags FLAGS TARGET PROPERTY) get_target_property2(_pflags ${TARGET} ${PROPERTY}) + set(_requires_arg 0) foreach(FLAG ${_pflags}) - if(TARGET ${FLAG}) - target_flags(_pflags2 ${FLAG}) - string(APPEND ${FLAGS} " ${_pflags2}") - elseif(FLAG MATCHES "^-.*") - string(APPEND ${FLAGS} " ${FLAG}") - elseif(EXISTS ${FLAG}) - string(APPEND ${FLAGS} " ${FLAG}") - else() - string(APPEND ${FLAGS} " -l${FLAG}") + string(STRIP "${FLAG}" FLAG) + if(FLAG) + if(TARGET ${FLAG} AND NOT _requires_arg) + target_flags(_pflags2 ${FLAG}) + string(APPEND ${FLAGS} " ${_pflags2}") + elseif(FLAG MATCHES "^-.*") + string(APPEND ${FLAGS} " ${FLAG}") + elseif(EXISTS ${FLAG}) + string(APPEND ${FLAGS} " ${FLAG}") + else() + string(APPEND ${FLAGS} " -l${FLAG}") + endif() + flags_requires_arg(_requires_arg "${FLAG}") endif() endforeach() endmacro() diff --git a/src/targets/gpu/CMakeLists.txt b/src/targets/gpu/CMakeLists.txt index 7309f7997bf..89764ef52a1 100755 --- a/src/targets/gpu/CMakeLists.txt +++ b/src/targets/gpu/CMakeLists.txt @@ -316,8 +316,12 @@ target_flags(HIP_COMPILER_FLAGS hip::device) # Remove cuda arch flags string(REGEX REPLACE --cuda-gpu-arch=[a-z0-9]+ "" HIP_COMPILER_FLAGS "${HIP_COMPILER_FLAGS}") string(REGEX REPLACE --offload-arch=[a-z0-9:+-]+ "" HIP_COMPILER_FLAGS "${HIP_COMPILER_FLAGS}") -string(REPLACE "$" "1" HIP_COMPILER_FLAGS "${HIP_COMPILER_FLAGS}") -string(REPLACE "SHELL:" "" HIP_COMPILER_FLAGS "${HIP_COMPILER_FLAGS}") +# Skip library paths since hip will incorrectly treat it as a source file +string(APPEND HIP_COMPILER_FLAGS " ") +foreach(_unused RANGE 2) + string(REGEX REPLACE " /[^ ]+\\.(a|so) " " " HIP_COMPILER_FLAGS "${HIP_COMPILER_FLAGS}") +endforeach() + message(STATUS "Hip compiler flags: ${HIP_COMPILER_FLAGS}") target_compile_definitions(migraphx_gpu PRIVATE "-DMIGRAPHX_HIP_COMPILER=${CMAKE_CXX_COMPILER}"