compiler_flags.cmake: fix flag detection w/cmake 3.17-3.18.2

Between 3.17.0 and 3.18.2 check_cxx_compiler_flag() sets a normal
variable at parent scope while check_cxx_source_compiles() continues to
set an internal cache variable, so we unset both to avoid the failure /
success state persisting between checks.

Change-Id: I4c9d63d89113d4ce9dc33a937b95df2e9acfce1a
(cherry picked from commit 1c69d568cabbceec8e09904ae513acb1d63358ac)
diff --git a/build/cmake/compiler_flags.cmake b/build/cmake/compiler_flags.cmake
index 24484bc..f008b96 100644
--- a/build/cmake/compiler_flags.cmake
+++ b/build/cmake/compiler_flags.cmake
@@ -59,6 +59,12 @@
     return()
   endif()
 
+  # Between 3.17.0 and 3.18.2 check_c_compiler_flag() sets a normal variable at
+  # parent scope while check_cxx_source_compiles() continues to set an internal
+  # cache variable, so we unset both to avoid the failure / success state
+  # persisting between checks. See
+  # https://gitlab.kitware.com/cmake/cmake/-/issues/21207.
+  unset(C_FLAG_SUPPORTED)
   unset(C_FLAG_SUPPORTED CACHE)
   message("Checking C compiler flag support for: " ${c_flag})
   check_c_compiler_flag("${c_flag}" C_FLAG_SUPPORTED)
@@ -89,6 +95,12 @@
     return()
   endif()
 
+  # Between 3.17.0 and 3.18.2 check_cxx_compiler_flag() sets a normal variable
+  # at parent scope while check_cxx_source_compiles() continues to set an
+  # internal cache variable, so we unset both to avoid the failure / success
+  # state persisting between checks. See
+  # https://gitlab.kitware.com/cmake/cmake/-/issues/21207.
+  unset(CXX_FLAG_SUPPORTED)
   unset(CXX_FLAG_SUPPORTED CACHE)
   message("Checking C++ compiler flag support for: " ${cxx_flag})
   check_cxx_compiler_flag("${cxx_flag}" CXX_FLAG_SUPPORTED)