From: Kefu Chai Date: Tue, 17 Jun 2025 09:22:16 +0000 (+0800) Subject: cmake: use find_program(REQUIRED) to fail early on missing programs X-Git-Url: http://git.apps.os.sepia.ceph.com/?a=commitdiff_plain;h=448633e325b743038677274d0a623f3471cea4c1;p=ceph.git cmake: use find_program(REQUIRED) to fail early on missing programs Since upgrading minimum CMake version to 3.22.1 (commit 469d82a1), we can now use find_program(REQUIRED) which was introduced in CMake 3.18. This change replaces manual FATAL_ERROR checks with the REQUIRED option and adds it to programs that are actually needed during the build. This ensures the build fails early during configuration rather than later during compilation when missing programs are invoked. Changes: - Replace find_program() + message(FATAL_ERROR) patterns with REQUIRED - Add REQUIRED to programs that are used during build but previously had no error checking Reference: https://cmake.org/cmake/help/latest/command/find_program.html Signed-off-by: Kefu Chai --- diff --git a/CMakeLists.txt b/CMakeLists.txt index fde3fed781a37..4fcb87c631f7e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -76,10 +76,8 @@ if(WITH_CCACHE) if(CMAKE_C_COMPILER_LAUNCHER OR CMAKE_CXX_COMPILER_LAUNCHER) message(WARNING "Compiler launcher already set. stop configuring ccache") else() - find_program(CCACHE_EXECUTABLE ccache) - if(NOT CCACHE_EXECUTABLE) - message(FATAL_ERROR "Can't find ccache. Is it installed?") - endif() + find_program(CCACHE_EXECUTABLE ccache + REQUIRED) message(STATUS "Building with ccache: ${CCACHE_EXECUTABLE}, CCACHE_DIR=$ENV{CCACHE_DIR}") set(CMAKE_C_COMPILER_LAUNCHER ${CCACHE_EXECUTABLE}) set(CMAKE_CXX_COMPILER_LAUNCHER ${CCACHE_EXECUTABLE}) @@ -88,10 +86,8 @@ endif(WITH_CCACHE) option(WITH_SCCACHE "Build with sccache.") if(WITH_SCCACHE) - find_program(SCCACHE_EXECUTABLE sccache) - if(NOT SCCACHE_EXECUTABLE) - message(FATAL_ERROR "Can't find sccache. Is it installed?") - endif() + find_program(SCCACHE_EXECUTABLE sccache + REQUIRED) if(NOT NINJA_MAX_COMPILE_JOBS) if(CMAKE_VERSION VERSION_GREATER_EQUAL "3.19") execute_process( @@ -123,10 +119,8 @@ endif(WITH_SCCACHE) option(WITH_MANPAGE "Build man pages." ON) if(WITH_MANPAGE) find_program(SPHINX_BUILD - NAMES sphinx-build sphinx-build-3) - if(NOT SPHINX_BUILD) - message(FATAL_ERROR "Can't find sphinx-build.") - endif(NOT SPHINX_BUILD) + NAMES sphinx-build sphinx-build-3 + REQUIRED) endif(WITH_MANPAGE) include_directories( @@ -670,10 +664,8 @@ option(WITH_LTTNG "LTTng tracing is enabled" ON) if(${WITH_LTTNG}) find_package(LTTngUST REQUIRED) find_program(LTTNG_GEN_TP - lttng-gen-tp) - if(NOT LTTNG_GEN_TP) - message(FATAL_ERROR "Can't find lttng-gen-tp.") - endif() + lttng-gen-tp + REQUIRED) endif(${WITH_LTTNG}) option(WITH_OSD_INSTRUMENT_FUNCTIONS OFF) @@ -808,10 +800,8 @@ include_directories(BEFORE SYSTEM ${Boost_INCLUDE_DIRS}) option(WITH_MGR_DASHBOARD_FRONTEND "Build the mgr/dashboard frontend using `npm`" ON) option(WITH_SYSTEM_NPM "Assume that dashboard build tools already installed through packages" OFF) if(WITH_SYSTEM_NPM) - find_program(NPM_EXECUTABLE npm) - if(NOT NPM_EXECUTABLE) - message(FATAL_ERROR "Can't find npm.") - endif() + find_program(NPM_EXECUTABLE npm + REQUIRED) endif() set(DASHBOARD_FRONTEND_LANGS "ALL" CACHE STRING "List of comma separated ceph-dashboard frontend languages to build. \ diff --git a/cmake/modules/FindMake.cmake b/cmake/modules/FindMake.cmake index 2a57a8df7f45e..755e127a30ba8 100644 --- a/cmake/modules/FindMake.cmake +++ b/cmake/modules/FindMake.cmake @@ -3,10 +3,9 @@ function(find_make make_exe make_cmd) # executable # make_cmd the name of the variable whose value will be the command to # used in the generated build script executed by the cmake generator - find_program(MAKE_EXECUTABLE NAMES gmake make) - if(NOT MAKE_EXECUTABLE) - message(FATAL_ERROR "Can't find make") - endif() + find_program(MAKE_EXECUTABLE + NAMES gmake make + REQUIRED) set(${make_exe} "${MAKE_EXECUTABLE}" PARENT_SCOPE) if(CMAKE_MAKE_PROGRAM MATCHES "make") # try to inherit command line arguments passed by parent "make" job diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 99b1456a59265..3b6dfc473ded4 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -290,10 +290,8 @@ endif() option(ENABLE_COVERAGE "Coverage is enabled" OFF) if(${ENABLE_COVERAGE}) - find_program(HAVE_GCOV gcov) - if(NOT HAVE_GCOV) - message(FATAL_ERROR "Coverage Enabled but gcov Not Found") - endif() + find_program(HAVE_GCOV gcov + REQUIRED) add_compile_options( --coverage -O0) @@ -1000,7 +998,8 @@ if(WITH_NVMEOF_GATEWAY_MONITOR_CLIENT) set(_REFLECTION grpc++_reflection) if(CMAKE_CROSSCOMPILING) - find_program(_PROTOBUF_PROTOC protoc) + find_program(_PROTOBUF_PROTOC protoc + REQUIRED) else() set(_PROTOBUF_PROTOC $) endif() @@ -1011,7 +1010,8 @@ if(WITH_NVMEOF_GATEWAY_MONITOR_CLIENT) message(STATUS "Using gRPC ${gRPC_VERSION}") set(_GRPC_GRPCPP gRPC::grpc++) if(CMAKE_CROSSCOMPILING) - find_program(_GRPC_CPP_PLUGIN_EXECUTABLE grpc_cpp_plugin) + find_program(_GRPC_CPP_PLUGIN_EXECUTABLE grpc_cpp_plugin + REQUIRED) else() set(_GRPC_CPP_PLUGIN_EXECUTABLE $) endif() diff --git a/src/rgw/CMakeLists.txt b/src/rgw/CMakeLists.txt index f235f18793698..ba8c259869342 100644 --- a/src/rgw/CMakeLists.txt +++ b/src/rgw/CMakeLists.txt @@ -1,7 +1,5 @@ -find_program(GPERF gperf) -if(NOT GPERF) - message(FATAL_ERROR "Can't find gperf") -endif() +find_program(GPERF gperf + REQUIRED) if(WITH_RADOSGW_BACKTRACE_LOGGING) add_definitions(-D_BACKTRACE_LOGGING)