|
| 1 | +#!/usr/bin/env -S cmake -P |
| 2 | +# |
| 3 | +# Update the minimum required CMake version. |
| 4 | +# |
| 5 | +# Run as: |
| 6 | +# |
| 7 | +# cmake [-D DIR=<path>] -P bin/update-cmake.cmake |
| 8 | +# |
| 9 | +# DIR - path to the directory containing CMake files. |
| 10 | +# |
| 11 | +# This is CMake-based command-line script that updates cmake_minimum_required() |
| 12 | +# calls in all found CMake files with the cmake_minimum_required() as specified |
| 13 | +# in this file (see the first line of the code below). |
| 14 | + |
| 15 | +cmake_minimum_required(VERSION 3.25...3.31) |
| 16 | + |
| 17 | +if(NOT CMAKE_SCRIPT_MODE_FILE) |
| 18 | + message(FATAL_ERROR "This is a command-line script.") |
| 19 | +endif() |
| 20 | + |
| 21 | +if(NOT DIR) |
| 22 | + cmake_path(SET DIR NORMALIZE ${CMAKE_CURRENT_LIST_DIR}/../cmake) |
| 23 | +else() |
| 24 | + cmake_path( |
| 25 | + ABSOLUTE_PATH |
| 26 | + DIR |
| 27 | + BASE_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/.. |
| 28 | + NORMALIZE |
| 29 | + ) |
| 30 | +endif() |
| 31 | + |
| 32 | +if(NOT IS_DIRECTORY ${DIR}) |
| 33 | + message(FATAL_ERROR "Directory not found: ${DIR}") |
| 34 | +endif() |
| 35 | + |
| 36 | +string( |
| 37 | + CONCAT regex |
| 38 | + "cmake_minimum_required" |
| 39 | + "[ \t]*" |
| 40 | + "\\(" |
| 41 | + "[ \t\r\n]*" |
| 42 | + "VERSION" |
| 43 | + "[ \t\r\n]+" |
| 44 | + "[0-9]\\.[0-9]+[0-9.]*" |
| 45 | + "(\\.\\.\\.[0-9]\\.[0-9]+)?" |
| 46 | + "[ \t\r\n]*" |
| 47 | + "(FATAL_ERROR)?" |
| 48 | + "[ \t\r\n]*" |
| 49 | + "\\)" |
| 50 | +) |
| 51 | + |
| 52 | +# Get CMake minimum required version specification from this script. |
| 53 | +file(STRINGS ${CMAKE_CURRENT_LIST_FILE} update REGEX "^${regex}" LIMIT_COUNT 1) |
| 54 | +message(STATUS "Using: ${update}") |
| 55 | + |
| 56 | +message(STATUS "Checking CMake files in ${DIR}") |
| 57 | + |
| 58 | +file( |
| 59 | + GLOB_RECURSE files |
| 60 | + RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} |
| 61 | + ${DIR}/CMakeLists.txt |
| 62 | + ${DIR}/CMakeLists.txt.in |
| 63 | + ${DIR}/*.cmake |
| 64 | + ${DIR}/*.cmake.in |
| 65 | +) |
| 66 | + |
| 67 | +foreach(file IN LISTS files) |
| 68 | + file(READ ${file} content) |
| 69 | + |
| 70 | + if(NOT content MATCHES "${regex}") |
| 71 | + continue() |
| 72 | + endif() |
| 73 | + |
| 74 | + string(REGEX REPLACE "${regex}" "${update}" newContent "${content}") |
| 75 | + |
| 76 | + if(newContent STREQUAL "${content}") |
| 77 | + continue() |
| 78 | + endif() |
| 79 | + |
| 80 | + message(STATUS "Updating ${file}") |
| 81 | + file(WRITE ${file} "${newContent}") |
| 82 | +endforeach() |
0 commit comments