Commit 41b6d30c5fbde4d3b7ccd44028d98969afecfee1

Patrick Steinhardt 2020-02-24T21:03:11

cmake: sanitize boolean options passed by user Starting with our conversions to mix backend-autodetection and selection into a single variable (USE_GSSAPI, USE_HTTPS, USE_SHA1), we have introduced a simple STREQUAL to check for "ON", which indicates that the user wants us to auto-detect available backends and pick any one that's available. This behaviour deviates from previous behaviour, as passing a value like "yes", "on" or "true" will in fact be treated like a backend name and result in autodetection failure. Fix the issue by introducing a new function `SanitizeBool`. Given a variable that may hold a boolean value, the function will sanitize that variable to hold either "ON" or "OFF". In case it is not a recognized boolean, we will just keep the value as-is. This fixes the above described issue.

diff --git a/cmake/Modules/SanitizeBool.cmake b/cmake/Modules/SanitizeBool.cmake
new file mode 100644
index 0000000..b5b99a6
--- /dev/null
+++ b/cmake/Modules/SanitizeBool.cmake
@@ -0,0 +1,20 @@
+FUNCTION(SanitizeBool VAR)
+	STRING(TOLOWER "${${VAR}}" VALUE)
+	IF(VALUE STREQUAL "on")
+		SET(${VAR} "ON" PARENT_SCOPE)
+	ELSEIF(VALUE STREQUAL "yes")
+		SET(${VAR} "ON" PARENT_SCOPE)
+	ELSEIF(VALUE STREQUAL "true")
+		SET(${VAR} "ON" PARENT_SCOPE)
+	ELSEIF(VALUE STREQUAL "1")
+		SET(${VAR} "ON" PARENT_SCOPE)
+	ELSEIF(VALUE STREQUAL "off")
+		SET(${VAR} "OFF" PARENT_SCOPE)
+	ELSEIF(VALUE STREQUAL "no")
+		SET(${VAR} "OFF" PARENT_SCOPE)
+	ELSEIF(VALUE STREQUAL "false")
+		SET(${VAR} "OFF" PARENT_SCOPE)
+	ELSEIF(VALUE STREQUAL "0")
+		SET(${VAR} "OFF" PARENT_SCOPE)
+	ENDIF()
+ENDFUNCTION()
diff --git a/cmake/Modules/SelectGSSAPI.cmake b/cmake/Modules/SelectGSSAPI.cmake
index 857c449..9b2bb1f 100644
--- a/cmake/Modules/SelectGSSAPI.cmake
+++ b/cmake/Modules/SelectGSSAPI.cmake
@@ -2,12 +2,15 @@
 
 # We try to find any packages our backends might use
 
+INCLUDE(SanitizeBool)
+
 FIND_PACKAGE(GSSAPI)
 IF (CMAKE_SYSTEM_NAME MATCHES "Darwin")
 	INCLUDE(FindGSSFramework)
 ENDIF()
 
 # Auto-select GSS backend
+SanitizeBool(USE_GSSAPI)
 IF (USE_GSSAPI STREQUAL ON)
 	IF (GSSFRAMEWORK_FOUND)
 		SET(GSS_BACKEND "GSS.framework")
diff --git a/cmake/Modules/SelectHTTPSBackend.cmake b/cmake/Modules/SelectHTTPSBackend.cmake
index c7f6b8f..f9b0b1c 100644
--- a/cmake/Modules/SelectHTTPSBackend.cmake
+++ b/cmake/Modules/SelectHTTPSBackend.cmake
@@ -1,5 +1,7 @@
 # Select the backend to use
 
+INCLUDE(SanitizeBool)
+
 # We try to find any packages our backends might use
 FIND_PACKAGE(OpenSSL)
 FIND_PACKAGE(mbedTLS)
@@ -9,6 +11,7 @@ IF (CMAKE_SYSTEM_NAME MATCHES "Darwin")
 ENDIF()
 
 # Auto-select TLS backend
+SanitizeBool(USE_HTTPS)
 IF (USE_HTTPS STREQUAL ON)
 	IF (SECURITY_FOUND)
 		IF (SECURITY_HAS_SSLCREATECONTEXT)
diff --git a/cmake/Modules/SelectHashes.cmake b/cmake/Modules/SelectHashes.cmake
index ce28ac2..a1339c1 100644
--- a/cmake/Modules/SelectHashes.cmake
+++ b/cmake/Modules/SelectHashes.cmake
@@ -1,7 +1,10 @@
 # Select a hash backend
 
+INCLUDE(SanitizeBool)
+
 # USE_SHA1=CollisionDetection(ON)/HTTPS/Generic/OFF
 
+SanitizeBool(USE_SHA1)
 IF(USE_SHA1 STREQUAL ON OR USE_SHA1 STREQUAL "CollisionDetection")
 	SET(SHA1_BACKEND "CollisionDetection")
 ELSEIF(USE_SHA1 STREQUAL "HTTPS")