Bump the minimum CMake version to 3.22

See https://github.com/google/oss-policies-info/blob/main/foundational-cxx-support-matrix.md

Change-Id: Id465dbff5ccac66597c0d987ed6e7e80a54489f0
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/82067
Auto-Submit: David Benjamin <davidben@google.com>
Commit-Queue: Lily Chen <chlily@google.com>
Reviewed-by: Lily Chen <chlily@google.com>
diff --git a/BUILDING.md b/BUILDING.md
index 5915652..3e67e15 100644
--- a/BUILDING.md
+++ b/BUILDING.md
@@ -14,7 +14,7 @@
 [Abseil guidelines](https://abseil.io/about/compatibility). If in doubt, use the
 most recent stable version of each tool.
 
-  * [CMake](https://cmake.org/download/) 3.12 or later is required.
+  * [CMake](https://cmake.org/download/) 3.22 or later is required.
 
   * Building with [Ninja](https://ninja-build.org/) instead of Make is
     recommended, because it makes builds faster. On Windows, CMake's Visual
@@ -74,8 +74,8 @@
 ### Building for Android
 
 It's possible to build BoringSSL with the Android NDK using CMake. Recent
-versions of the NDK include a CMake toolchain file which works with CMake 3.6.0
-or later. This has been tested with version r16b of the NDK.
+versions of the NDK include a CMake toolchain file. This has been tested with
+version r16b of the NDK.
 
 Unpack the Android NDK somewhere and export `ANDROID_NDK` to point to the
 directory. Then run CMake like this:
diff --git a/CMakeLists.txt b/CMakeLists.txt
index c86687a..1e086c5 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -12,7 +12,7 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-cmake_minimum_required(VERSION 3.16)
+cmake_minimum_required(VERSION 3.22)
 
 # Defer enabling C and CXX languages.
 project(BoringSSL NONE)
@@ -43,11 +43,6 @@
 
 set(INSTALL_ENABLED 1)
 
-if(CMAKE_VERSION VERSION_LESS 3.21 AND
-   CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
-  set(PROJECT_IS_TOP_LEVEL 1)
-endif()
-
 if(CMAKE_SYSTEM_NAME STREQUAL "Linux" AND NOT CMAKE_CROSSCOMPILING AND
    BUILD_TESTING)
   find_package(PkgConfig QUIET)
@@ -384,16 +379,6 @@
     if (NOT OPENSSL_NO_ASM)
       set(OPENSSL_ASM TRUE)
     endif()
-    # Work around https://gitlab.kitware.com/cmake/cmake/-/issues/20771 in older
-    # CMake versions.
-    if(APPLE AND CMAKE_VERSION VERSION_LESS 3.19)
-      if(CMAKE_OSX_SYSROOT)
-        set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -isysroot \"${CMAKE_OSX_SYSROOT}\"")
-      endif()
-      foreach(arch ${CMAKE_OSX_ARCHITECTURES})
-        set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -arch ${arch}")
-      endforeach()
-    endif()
     if(NOT WIN32)
       set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -Wa,--noexecstack")
     endif()
@@ -742,12 +727,8 @@
 endif()
 
 if(RUST_BINDINGS)
-  find_program(BINDGEN_EXECUTABLE bindgen)
-  if(NOT BINDGEN_EXECUTABLE)
-    message(FATAL_ERROR "Could not find bindgen but was asked to generate Rust bindings.")
-  else()
-    add_subdirectory(rust)
-  endif()
+  find_program(BINDGEN_EXECUTABLE bindgen REQUIRED)
+  add_subdirectory(rust)
 endif()
 
 if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
diff --git a/cmake/go.cmake b/cmake/go.cmake
index 9b2f411..13d21a5 100644
--- a/cmake/go.cmake
+++ b/cmake/go.cmake
@@ -25,37 +25,19 @@
 function(go_executable dest package)
   require_go()
   set(godeps "${PROJECT_SOURCE_DIR}/util/godeps.go")
-  if(NOT CMAKE_GENERATOR STREQUAL "Ninja")
-    # The DEPFILE parameter to add_custom_command only works with Ninja. Query
-    # the sources at configure time. Additionally, everything depends on go.mod.
-    # That affects what external packages to use.
-    #
-    # TODO(davidben): Starting CMake 3.20, it also works with Make. Starting
-    # 3.21, it works with Visual Studio and Xcode too.
-    execute_process(COMMAND ${GO_EXECUTABLE} run ${godeps} -format cmake
-                            -pkg ${package}
-                    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
-                    OUTPUT_VARIABLE sources
-                    RESULT_VARIABLE godeps_result)
-    add_custom_command(OUTPUT ${dest}
-                       COMMAND ${GO_EXECUTABLE} build
-                               -o ${CMAKE_CURRENT_BINARY_DIR}/${dest} ${package}
-                       WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
-                       DEPENDS ${sources} ${PROJECT_SOURCE_DIR}/go.mod)
-  else()
-    # Ninja expects the target in the depfile to match the output. This is a
-    # relative path from the build directory.
-    binary_dir_relative_path(${dest} target)
+  # Ninja expects the target in the depfile to match the output. This is a
+  # relative path from the build directory.
+  set(target "${CMAKE_CURRENT_BINARY_DIR}/${dest}")
+  cmake_path(RELATIVE_PATH target BASE_DIRECTORY "${CMAKE_BINARY_DIR}")
 
-    set(depfile "${CMAKE_CURRENT_BINARY_DIR}/${dest}.d")
-    add_custom_command(OUTPUT ${dest}
-                       COMMAND ${GO_EXECUTABLE} build
-                               -o ${CMAKE_CURRENT_BINARY_DIR}/${dest} ${package}
-                       COMMAND ${GO_EXECUTABLE} run ${godeps} -format depfile
-                               -target ${target} -pkg ${package} -out ${depfile}
-                       WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
-                       DEPENDS ${godeps} ${PROJECT_SOURCE_DIR}/go.mod
-                       DEPFILE ${depfile})
-  endif()
+  set(depfile "${CMAKE_CURRENT_BINARY_DIR}/${dest}.d")
+  add_custom_command(OUTPUT ${dest}
+                      COMMAND ${GO_EXECUTABLE} build
+                              -o ${CMAKE_CURRENT_BINARY_DIR}/${dest} ${package}
+                      COMMAND ${GO_EXECUTABLE} run ${godeps} -format depfile
+                              -target ${target} -pkg ${package} -out ${depfile}
+                      WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
+                      DEPENDS ${godeps} ${PROJECT_SOURCE_DIR}/go.mod
+                      DEPFILE ${depfile})
 endfunction()
 
diff --git a/cmake/paths.cmake b/cmake/paths.cmake
index 625b188..c6285f9 100644
--- a/cmake/paths.cmake
+++ b/cmake/paths.cmake
@@ -12,18 +12,6 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-# binary_dir_relative_path sets outvar to
-# ${CMAKE_CURRENT_BINARY_DIR}/${cur_bin_dir_relative}, but expressed relative to
-# ${CMAKE_BINARY_DIR}.
-#
-# TODO(davidben): When we require CMake 3.20 or later, this can be replaced with
-# the built-in cmake_path(RELATIVE_PATH) function.
-function(binary_dir_relative_path cur_bin_dir_relative outvar)
-  string(LENGTH "${CMAKE_BINARY_DIR}/" root_dir_length)
-  string(SUBSTRING "${CMAKE_CURRENT_BINARY_DIR}/${cur_bin_dir_relative}" ${root_dir_length} -1 result)
-  set(${outvar} ${result} PARENT_SCOPE)
-endfunction()
-
 # copy_post_build causes targets in ${ARGN} to be copied to
 # ${CMAKE_CURRENT_BINARY_DIR}/${dir} after being built.
 function(copy_post_build dir)
diff --git a/rust/bssl-sys/CMakeLists.txt b/rust/bssl-sys/CMakeLists.txt
index a5ca2c3..c5ea315 100644
--- a/rust/bssl-sys/CMakeLists.txt
+++ b/rust/bssl-sys/CMakeLists.txt
@@ -3,8 +3,10 @@
 # the depfile using the same syntax as the command-line argument, and ninja
 # requires a path relative to the top-level build directory.
 set(wrapper_rs wrapper_${RUST_BINDINGS}.rs)
-binary_dir_relative_path(${wrapper_rs} wrapper_rs_relative)
-binary_dir_relative_path(${wrapper_rs}.d depfile_relative)
+set(wrapper_rs_relative "${CMAKE_CURRENT_BINARY_DIR}/${wrapper_rs}")
+set(depfile_relative "${CMAKE_CURRENT_BINARY_DIR}/${wrapper_rs}.d")
+cmake_path(RELATIVE_PATH wrapper_rs_relative BASE_DIRECTORY "${CMAKE_BINARY_DIR}")
+cmake_path(RELATIVE_PATH depfile_relative BASE_DIRECTORY "${CMAKE_BINARY_DIR}")
 
 add_custom_command(
   OUTPUT ${wrapper_rs} wrapper.c