Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions cmake/Assertion.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,26 @@ endfunction()
# For each given line, if it is a variable, it will be expanded and indented by
# 2 spaces.
macro(fail FIRST_LINE)
if(DEFINED "${FIRST_LINE}")
set(MESSAGE "${${FIRST_LINE}}")
else()
set(MESSAGE "${FIRST_LINE}")
endif()
set(MESSAGE "${FIRST_LINE}")

unset(PREV_LINE_IS_MULTI_LINE)
foreach(LINE IN ITEMS ${ARGN})
if(DEFINED "${LINE}")
string(REPLACE "\n" "\n " LINE "${${LINE}}")
string(APPEND MESSAGE ":\n ${LINE}")
if("${${LINE}}" MATCHES "\n")
string(REPLACE "\n" "\n " LINE "${${LINE}}")
string(APPEND MESSAGE ":\n ${LINE}")
else()
string(APPEND MESSAGE " '${${LINE}}'")
endif()
else()
string(APPEND MESSAGE "\n${LINE}")
if(PREV_LINE_IS_MULTI_LINE)
string(APPEND MESSAGE "\n${LINE}")
else()
string(APPEND MESSAGE " ${LINE}")
endif()
endif()
endforeach()
unset(PREV_LINE_IS_MULTI_LINE)

message(FATAL_ERROR "${MESSAGE}")
endmacro()
Expand Down
34 changes: 18 additions & 16 deletions test/fail.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ section("it should fail with a formatted fatal error message")
MESSAGE "something happened\non multiple lines")

assert_fatal_error(
CALL fail "something happened" "on multiple lines"
MESSAGE "something happened\non multiple lines")
CALL fail "something happened" "not on multiple lines"
MESSAGE "something happened not on multiple lines")
endsection()

section("it should fail with a formatted fatal error message "
Expand All @@ -18,30 +18,32 @@ section("it should fail with a formatted fatal error message "
set(REASON_MULTILINE "some reason\non multiple lines")

assert_fatal_error(
CALL fail "something happened" REASON
MESSAGE "something happened:\n some reason")
CALL fail "something happened because of" REASON
MESSAGE "something happened because of 'some reason'")

assert_fatal_error(
CALL fail "something happened" REASON_MULTILINE
MESSAGE "something happened:\n some reason\n on multiple lines")
CALL fail "something happened because of" REASON_MULTILINE
MESSAGE "something happened because of:\n"
" some reason\n on multiple lines")

assert_fatal_error(
CALL fail "something happened\non multiple lines" REASON
MESSAGE "something happened\non multiple lines:\n some reason")
CALL fail "something happened\non multiple lines because of" REASON
MESSAGE "something happened\non multiple lines because of 'some reason'")

assert_fatal_error(
CALL fail "something happened\non multiple lines" REASON_MULTILINE
MESSAGE "something happened\non multiple lines:\n"
CALL fail "something happened\non multiple lines because of"
REASON_MULTILINE
MESSAGE "something happened\non multiple lines because of:\n"
" some reason\n on multiple lines")

assert_fatal_error(
CALL fail "something happened" "on multiple lines" REASON
MESSAGE "something happened\non multiple lines:\n some reason")
CALL fail "something happened" "not on multiple lines because of" REASON
MESSAGE "something happened not on multiple lines because of 'some reason'")

assert_fatal_error(
CALL fail
"something happened" "on multiple lines" REASON
"something else happened" REASON_MULTILINE
MESSAGE "something happened\non multiple lines:\n some reason\n"
"something else happened:\n some reason\n on multiple lines")
"something happened" "not on multiple lines because of" REASON
"something else happened because of" REASON_MULTILINE
MESSAGE "something happened not on multiple lines because of 'some reason' "
"something else happened because of:\n some reason\n on multiple lines")
endsection()