From f8b041a6c6483cd02da7ecc9b1388f10b9fdcc90 Mon Sep 17 00:00:00 2001 From: Alfi Maulana Date: Wed, 10 Jul 2024 13:01:11 +0700 Subject: [PATCH] fix: enable several CMake policies if supported in `assert` function --- cmake/Assertion.cmake | 253 ++++++++++++++++++++++-------------------- 1 file changed, 131 insertions(+), 122 deletions(-) diff --git a/cmake/Assertion.cmake b/cmake/Assertion.cmake index 49e7245..b9d7964 100644 --- a/cmake/Assertion.cmake +++ b/cmake/Assertion.cmake @@ -37,8 +37,6 @@ # modules by passing the paths of the other modules as additional arguments # after `--`. -cmake_minimum_required(VERSION 3.24) - # This variable contains the path to the included `Assertion.cmake` module. set(ASSERTION_LIST_FILE "${CMAKE_CURRENT_LIST_FILE}") @@ -139,133 +137,144 @@ endmacro() # supported conditions for the assertion. function(assert) cmake_parse_arguments(PARSE_ARGV 0 ARG "" "" "") - if(${ARG_UNPARSED_ARGUMENTS}) - # Assertion passed, exit immediately. - return() - endif() - # Assertion failed, determine the fatal error message based on the given - # condition. - if(ARGC EQUAL 2) - if(NOT ARGV0 STREQUAL NOT) - if(ARGV0 STREQUAL "COMMAND") - fail("expected command" ARGV1 "to be defined") - return() - elseif(ARGV0 STREQUAL "POLICY") - fail("expected policy" ARGV1 "to exist") - return() - elseif(ARGV0 STREQUAL "TARGET") - fail("expected target" ARGV1 "to exist") - return() - elseif(ARGV0 STREQUAL "TEST") - fail("expected test" ARGV1 "to exist") - return() - elseif(ARGV0 STREQUAL "DEFINED") - fail("expected variable" ARGV1 "to be defined") - return() - elseif(ARGV0 STREQUAL "EXISTS") - fail("expected path" ARGV1 "to exist") - return() - elseif(ARGV0 STREQUAL "IS_READABLE") - fail("expected path" ARGV1 "to be readable") - return() - elseif(ARGV0 STREQUAL "IS_WRITABLE") - fail("expected path" ARGV1 "to be writable") - return() - elseif(ARGV0 STREQUAL "IS_EXECUTABLE") - fail("expected path" ARGV1 "to be an executable") - return() - elseif(ARGV0 STREQUAL "IS_DIRECTORY") - fail("expected path" ARGV1 "to be a directory") - return() - elseif(ARGV0 STREQUAL "IS_SYMLINK") - fail("expected path" ARGV1 "to be a symbolic link") - return() - elseif(ARGV0 STREQUAL "IS_ABSOLUTE") - fail("expected path" ARGV1 "to be absolute") - return() + # This block will automatically set several policies to use the new behavior + # if it is available in the current CMake version. + block(SCOPE_FOR POLICIES) + foreach(POLICY IN ITEMS CMP0012 CMP0054 CMP0057 CMP0064 CMP0139) + if(POLICY "${POLICY}") + cmake_policy(SET "${POLICY}" NEW) endif() + endforeach() + + if(${ARG_UNPARSED_ARGUMENTS}) + # Assertion passed, exit immediately. + return() endif() - elseif(ARGC EQUAL 3) - if(ARGV0 STREQUAL NOT) - if(ARGV1 STREQUAL "COMMAND") - fail("expected command" ARGV2 "not to be defined") - return() - elseif(ARGV1 STREQUAL "POLICY") - fail("expected policy" ARGV2 "not to exist") - return() - elseif(ARGV1 STREQUAL "TARGET") - fail("expected target" ARGV2 "not to exist") - return() - elseif(ARGV1 STREQUAL "TEST") - fail("expected test" ARGV2 "not to exist") - return() - elseif(ARGV1 STREQUAL "DEFINED") - # Unset this to prevent the value from being formatted. - unset("${ARGV2}") - - fail("expected variable" ARGV2 "not to be defined") - return() - elseif(ARGV1 STREQUAL "EXISTS") - fail("expected path" ARGV2 "not to exist") - return() - elseif(ARGV1 STREQUAL "IS_READABLE") - fail("expected path" ARGV2 "not to be readable") - return() - elseif(ARGV1 STREQUAL "IS_WRITABLE") - fail("expected path" ARGV2 "not to be writable") - return() - elseif(ARGV1 STREQUAL "IS_EXECUTABLE") - fail("expected path" ARGV2 "not to be an executable") - return() - elseif(ARGV1 STREQUAL "IS_DIRECTORY") - fail("expected path" ARGV2 "not to be a directory") - return() - elseif(ARGV1 STREQUAL "IS_SYMLINK") - fail("expected path" ARGV2 "not to be a symbolic link") - return() - elseif(ARGV1 STREQUAL "IS_ABSOLUTE") - fail("expected path" ARGV2 "not to be absolute") - return() + + # Assertion failed, determine the fatal error message based on the given + # condition. + if(ARGC EQUAL 2) + if(NOT ARGV0 STREQUAL NOT) + if(ARGV0 STREQUAL "COMMAND") + fail("expected command" ARGV1 "to be defined") + return() + elseif(ARGV0 STREQUAL "POLICY") + fail("expected policy" ARGV1 "to exist") + return() + elseif(ARGV0 STREQUAL "TARGET") + fail("expected target" ARGV1 "to exist") + return() + elseif(ARGV0 STREQUAL "TEST") + fail("expected test" ARGV1 "to exist") + return() + elseif(ARGV0 STREQUAL "DEFINED") + fail("expected variable" ARGV1 "to be defined") + return() + elseif(ARGV0 STREQUAL "EXISTS") + fail("expected path" ARGV1 "to exist") + return() + elseif(ARGV0 STREQUAL "IS_READABLE") + fail("expected path" ARGV1 "to be readable") + return() + elseif(ARGV0 STREQUAL "IS_WRITABLE") + fail("expected path" ARGV1 "to be writable") + return() + elseif(ARGV0 STREQUAL "IS_EXECUTABLE") + fail("expected path" ARGV1 "to be an executable") + return() + elseif(ARGV0 STREQUAL "IS_DIRECTORY") + fail("expected path" ARGV1 "to be a directory") + return() + elseif(ARGV0 STREQUAL "IS_SYMLINK") + fail("expected path" ARGV1 "to be a symbolic link") + return() + elseif(ARGV0 STREQUAL "IS_ABSOLUTE") + fail("expected path" ARGV1 "to be absolute") + return() + endif() endif() - else() - if(ARGV1 STREQUAL "IN_LIST") - fail("expected string" ARGV0 "to exist in" ARGV2) - return() - elseif(ARGV1 STREQUAL "IS_NEWER_THAN") - fail("expected file" ARGV0 "to be newer than" ARGV2) - return() - elseif(ARGV1 STREQUAL "MATCHES") - fail("expected string" ARGV0 "to match" ARGV2) - return() - elseif(ARGV1 STREQUAL "STREQUAL") - fail("expected string" ARGV0 "to be equal to" ARGV2) - return() - elseif(ARGV1 STREQUAL "PATH_EQUAL") - fail("expected path" ARGV0 "to be equal to" ARGV2) - return() + elseif(ARGC EQUAL 3) + if(ARGV0 STREQUAL NOT) + if(ARGV1 STREQUAL "COMMAND") + fail("expected command" ARGV2 "not to be defined") + return() + elseif(ARGV1 STREQUAL "POLICY") + fail("expected policy" ARGV2 "not to exist") + return() + elseif(ARGV1 STREQUAL "TARGET") + fail("expected target" ARGV2 "not to exist") + return() + elseif(ARGV1 STREQUAL "TEST") + fail("expected test" ARGV2 "not to exist") + return() + elseif(ARGV1 STREQUAL "DEFINED") + # Unset this to prevent the value from being formatted. + unset("${ARGV2}") + + fail("expected variable" ARGV2 "not to be defined") + return() + elseif(ARGV1 STREQUAL "EXISTS") + fail("expected path" ARGV2 "not to exist") + return() + elseif(ARGV1 STREQUAL "IS_READABLE") + fail("expected path" ARGV2 "not to be readable") + return() + elseif(ARGV1 STREQUAL "IS_WRITABLE") + fail("expected path" ARGV2 "not to be writable") + return() + elseif(ARGV1 STREQUAL "IS_EXECUTABLE") + fail("expected path" ARGV2 "not to be an executable") + return() + elseif(ARGV1 STREQUAL "IS_DIRECTORY") + fail("expected path" ARGV2 "not to be a directory") + return() + elseif(ARGV1 STREQUAL "IS_SYMLINK") + fail("expected path" ARGV2 "not to be a symbolic link") + return() + elseif(ARGV1 STREQUAL "IS_ABSOLUTE") + fail("expected path" ARGV2 "not to be absolute") + return() + endif() + else() + if(ARGV1 STREQUAL "IN_LIST") + fail("expected string" ARGV0 "to exist in" ARGV2) + return() + elseif(ARGV1 STREQUAL "IS_NEWER_THAN") + fail("expected file" ARGV0 "to be newer than" ARGV2) + return() + elseif(ARGV1 STREQUAL "MATCHES") + fail("expected string" ARGV0 "to match" ARGV2) + return() + elseif(ARGV1 STREQUAL "STREQUAL") + fail("expected string" ARGV0 "to be equal to" ARGV2) + return() + elseif(ARGV1 STREQUAL "PATH_EQUAL") + fail("expected path" ARGV0 "to be equal to" ARGV2) + return() + endif() endif() - endif() - elseif(ARGC EQUAL 4) - if(ARGV0 STREQUAL "NOT") - if(ARGV2 STREQUAL "IN_LIST") - fail("expected string" ARGV1 "not to exist in" ARGV3) - return() - elseif(ARGV2 STREQUAL "IS_NEWER_THAN") - fail("expected file" ARGV1 "not to be newer than" ARGV3) - return() - elseif(ARGV2 STREQUAL "MATCHES") - fail("expected string" ARGV1 "not to match" ARGV3) - return() - elseif(ARGV2 STREQUAL "STREQUAL") - fail("expected string" ARGV1 "not to be equal to" ARGV3) - return() - elseif(ARGV2 STREQUAL "PATH_EQUAL") - fail("expected path" ARGV1 "not to be equal to" ARGV3) - return() + elseif(ARGC EQUAL 4) + if(ARGV0 STREQUAL "NOT") + if(ARGV2 STREQUAL "IN_LIST") + fail("expected string" ARGV1 "not to exist in" ARGV3) + return() + elseif(ARGV2 STREQUAL "IS_NEWER_THAN") + fail("expected file" ARGV1 "not to be newer than" ARGV3) + return() + elseif(ARGV2 STREQUAL "MATCHES") + fail("expected string" ARGV1 "not to match" ARGV3) + return() + elseif(ARGV2 STREQUAL "STREQUAL") + fail("expected string" ARGV1 "not to be equal to" ARGV3) + return() + elseif(ARGV2 STREQUAL "PATH_EQUAL") + fail("expected path" ARGV1 "not to be equal to" ARGV3) + return() + endif() endif() endif() - endif() + endblock() # Fallback to this message if it could not determine the fatal error message # from the given condition.