fix cmake: Fix identation
All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.
I hereby agree to the terms of the CLA available at: https://yandex.ru/legal/cla/
This does not seem to be a complete solution. How about adding https://github.com/cheshirekow/cmake_format to the CI and formatters?
After applying it directly (cmake_format):
cmake-format -i ../userver/CMakeLists.txt
we get following changes:
diff --git a/CMakeLists.txt b/CMakeLists.txt
@@ -13,11 +13,7 @@ project(userver)
set(USERVER_ROOT_DIR "${CMAKE_CURRENT_LIST_DIR}")
-option(
- USERVER_INSTALL
- "Prepare build of userver to install in system"
- OFF
-)
+option(USERVER_INSTALL "Prepare build of userver to install in system" OFF)
set(USERVER_AVAILABLE_COMPONENTS universal)
set(USERVER_NOT_INCLUDED_AS_SUBDIR OFF)
@@ -25,7 +21,10 @@ if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR AND NOT USERVER_INSTALL)
set(USERVER_NOT_INCLUDED_AS_SUBDIR ON)
endif()
-option(USERVER_FEATURE_CORE "Provide a core library with coroutines, otherwise build only userver-universal" ON)
+option(
+ USERVER_FEATURE_CORE
+ "Provide a core library with coroutines, otherwise build only userver-universal"
+ ON)
option(USERVER_FEATURE_CHAOTIC "Provide chaotic-codegen for jsonschema" ON)
set(USERVER_BUILD_PLATFORM_X86 OFF)
@@ -34,24 +33,23 @@ if(CMAKE_SYSTEM_PROCESSOR MATCHES "^x86")
endif()
function(_require_userver_core FEATURE)
- if (NOT USERVER_FEATURE_CORE)
+ if(NOT USERVER_FEATURE_CORE)
message(FATAL_ERROR "'${FEATURE}' requires 'USERVER_FEATURE_CORE=ON'")
endif()
endfunction()
-option(USERVER_FEATURE_UTEST "Provide 'utest' and 'ubench' for gtest and gbenchmark integration" ON)
-if (USERVER_FEATURE_UTEST)
+option(USERVER_FEATURE_UTEST
+ "Provide 'utest' and 'ubench' for gtest and gbenchmark integration" ON)
+if(USERVER_FEATURE_UTEST)
message(STATUS "Building utest with gtest and ubench with gbench")
endif()
-option(
- USERVER_IS_THE_ROOT_PROJECT
- "Contributor mode: build userver tests, samples and helper tools"
- "${USERVER_NOT_INCLUDED_AS_SUBDIR}"
-)
-if (USERVER_IS_THE_ROOT_PROJECT)
+option(USERVER_IS_THE_ROOT_PROJECT
+ "Contributor mode: build userver tests, samples and helper tools"
+ "${USERVER_NOT_INCLUDED_AS_SUBDIR}")
+if(USERVER_IS_THE_ROOT_PROJECT)
message(STATUS "Building userver as a primary project")
- if (NOT USERVER_FEATURE_UTEST)
+ if(NOT USERVER_FEATURE_UTEST)
message(FATAL_ERROR "Cannot build tests without utest")
endif()
else()
@@ -65,7 +63,9 @@ endif()
set(USERVER_MONGODB_DEFAULT OFF)
set(USERVER_CLICKHOUSE_DEFAULT OFF)
-if(USERVER_FEATURE_CORE AND USERVER_IS_THE_ROOT_PROJECT AND USERVER_BUILD_PLATFORM_X86)
+if(USERVER_FEATURE_CORE
+ AND USERVER_IS_THE_ROOT_PROJECT
+ AND USERVER_BUILD_PLATFORM_X86)
if(NOT CMAKE_SYSTEM_NAME MATCHES "BSD")
set(USERVER_MONGODB_DEFAULT ON)
endif()
@@ -73,31 +73,34 @@ if(USERVER_FEATURE_CORE AND USERVER_IS_THE_ROOT_PROJECT AND USERVER_BUILD_PLATFO
endif()
set(USERVER_YDB_DEFAULT OFF)
-if(USERVER_FEATURE_CORE AND USERVER_IS_THE_ROOT_PROJECT AND
- DEFINED CMAKE_CXX_STANDARD AND CMAKE_CXX_STANDARD GREATER_EQUAL 20)
+if(USERVER_FEATURE_CORE
+ AND USERVER_IS_THE_ROOT_PROJECT
+ AND DEFINED CMAKE_CXX_STANDARD
+ AND CMAKE_CXX_STANDARD GREATER_EQUAL 20)
set(USERVER_YDB_DEFAULT ON)
endif()
option(USERVER_CONAN "Build with Conan packages" ${CONAN_EXPORTED})
-option(
- USERVER_DOWNLOAD_PACKAGES
- "Download missing third party packages and use the downloaded versions"
- ON
-)
+option(USERVER_DOWNLOAD_PACKAGES
+ "Download missing third party packages and use the downloaded versions"
+ ON)
option(
USERVER_FORCE_DOWNLOAD_PACKAGES
"Download all possible third party packages even if a system package is available"
- OFF
-)
+ OFF)
-option(USERVER_FEATURE_CRYPTOPP_BLAKE2 "Provide wrappers for blake2 algorithms of crypto++" ON)
-if (NOT USERVER_FEATURE_CRYPTOPP_BLAKE2)
+option(USERVER_FEATURE_CRYPTOPP_BLAKE2
+ "Provide wrappers for blake2 algorithms of crypto++" ON)
+if(NOT USERVER_FEATURE_CRYPTOPP_BLAKE2)
add_compile_definitions("USERVER_NO_CRYPTOPP_BLAKE2=1")
endif()
-option(USERVER_FEATURE_CRYPTOPP_BASE64_URL "Provide wrappers for Base64 URL decoding and encoding algorithms of crypto++" ON)
-if (NOT USERVER_FEATURE_CRYPTOPP_BASE64_URL)
+option(
+ USERVER_FEATURE_CRYPTOPP_BASE64_URL
+ "Provide wrappers for Base64 URL decoding and encoding algorithms of crypto++"
+ ON)
+if(NOT USERVER_FEATURE_CRYPTOPP_BASE64_URL)
add_compile_definitions("USERVER_NO_CRYPTOPP_BASE64_URL=1")
endif()
@@ -106,29 +109,43 @@ if(CMAKE_SYSTEM_NAME MATCHES "BSD")
else()
set(JEMALLOC_DEFAULT ON)
endif()
-option(USERVER_FEATURE_JEMALLOC "Enable linkage with jemalloc memory allocator" ${JEMALLOC_DEFAULT})
+option(USERVER_FEATURE_JEMALLOC "Enable linkage with jemalloc memory allocator"
+ ${JEMALLOC_DEFAULT})
-option(USERVER_DISABLE_PHDR_CACHE "Disable caching of dl_phdr_info items, which interferes with dlopen" OFF)
+option(USERVER_DISABLE_PHDR_CACHE
+ "Disable caching of dl_phdr_info items, which interferes with dlopen"
+ OFF)
set(USERVER_DISABLE_RSEQ_DEFAULT ON)
-if (USERVER_BUILD_PLATFORM_X86 AND CMAKE_SYSTEM_NAME MATCHES "Linux")
+if(USERVER_BUILD_PLATFORM_X86 AND CMAKE_SYSTEM_NAME MATCHES "Linux")
set(USERVER_DISABLE_RSEQ_DEFAULT OFF)
message(STATUS "rseq-based acceleration is enabled by default")
endif()
-option(USERVER_DISABLE_RSEQ_ACCELERATION "Disable rseq-based optimizations" ${USERVER_DISABLE_RSEQ_DEFAULT})
+option(USERVER_DISABLE_RSEQ_ACCELERATION "Disable rseq-based optimizations"
+ ${USERVER_DISABLE_RSEQ_DEFAULT})
option(USERVER_CHECK_PACKAGE_VERSIONS "Check package versions" ON)
-option(USERVER_FEATURE_MONGODB "Provide asynchronous driver for MongoDB" "${USERVER_MONGODB_DEFAULT}")
-option(USERVER_FEATURE_POSTGRESQL "Provide asynchronous driver for PostgreSQL" "${USERVER_LIB_ENABLED_DEFAULT}")
-option(USERVER_FEATURE_REDIS "Provide asynchronous driver for Redis" "${USERVER_LIB_ENABLED_DEFAULT}")
-option(USERVER_FEATURE_GRPC "Provide asynchronous driver for gRPC" "${USERVER_LIB_ENABLED_DEFAULT}")
-option(USERVER_FEATURE_CLICKHOUSE "Provide asynchronous driver for ClickHouse" "${USERVER_CLICKHOUSE_DEFAULT}")
-option(USERVER_FEATURE_KAFKA "Provide asynchronous driver for Apache Kafka" "${USERVER_LIB_ENABLED_DEFAULT}")
-option(USERVER_FEATURE_RABBITMQ "Provide asynchronous driver for RabbitMQ" "${USERVER_LIB_ENABLED_DEFAULT}")
-option(USERVER_FEATURE_MYSQL "Provide asynchronous driver for MariaDB/MySQL" "${USERVER_LIB_ENABLED_DEFAULT}")
-option(USERVER_FEATURE_ROCKS "Provide asynchronous driver for Rocks" "${USERVER_LIB_ENABLED_DEFAULT}")
-option(USERVER_FEATURE_YDB "Provide asynchronous driver for YDB" "${USERVER_YDB_DEFAULT}")
+option(USERVER_FEATURE_MONGODB "Provide asynchronous driver for MongoDB"
+ "${USERVER_MONGODB_DEFAULT}")
+option(USERVER_FEATURE_POSTGRESQL "Provide asynchronous driver for PostgreSQL"
+ "${USERVER_LIB_ENABLED_DEFAULT}")
+option(USERVER_FEATURE_REDIS "Provide asynchronous driver for Redis"
+ "${USERVER_LIB_ENABLED_DEFAULT}")
+option(USERVER_FEATURE_GRPC "Provide asynchronous driver for gRPC"
+ "${USERVER_LIB_ENABLED_DEFAULT}")
+option(USERVER_FEATURE_CLICKHOUSE "Provide asynchronous driver for ClickHouse"
+ "${USERVER_CLICKHOUSE_DEFAULT}")
+option(USERVER_FEATURE_KAFKA "Provide asynchronous driver for Apache Kafka"
+ "${USERVER_LIB_ENABLED_DEFAULT}")
+option(USERVER_FEATURE_RABBITMQ "Provide asynchronous driver for RabbitMQ"
+ "${USERVER_LIB_ENABLED_DEFAULT}")
+option(USERVER_FEATURE_MYSQL "Provide asynchronous driver for MariaDB/MySQL"
+ "${USERVER_LIB_ENABLED_DEFAULT}")
+option(USERVER_FEATURE_ROCKS "Provide asynchronous driver for Rocks"
+ "${USERVER_LIB_ENABLED_DEFAULT}")
+option(USERVER_FEATURE_YDB "Provide asynchronous driver for YDB"
+ "${USERVER_YDB_DEFAULT}")
set(CMAKE_DEBUG_POSTFIX d)
@@ -151,52 +168,48 @@ include(CMakePackageConfigHelpers)
message(STATUS "Generating cmake files ...")
userver_venv_setup(
- NAME userver-cmake
- PYTHON_OUTPUT_VAR USERVER_CMAKE_GEN_PYTHON_BINARY
- REQUIREMENTS "${USERVER_ROOT_DIR}/scripts/external_deps/requirements.txt"
-)
+ NAME userver-cmake PYTHON_OUTPUT_VAR USERVER_CMAKE_GEN_PYTHON_BINARY
+ REQUIREMENTS "${USERVER_ROOT_DIR}/scripts/external_deps/requirements.txt")
execute_process(
COMMAND
- "${USERVER_CMAKE_GEN_PYTHON_BINARY}"
- -u "${USERVER_ROOT_DIR}/scripts/external_deps/cmake_generator.py"
- --repo-dir "${USERVER_ROOT_DIR}"
- --build-dir "${CMAKE_BINARY_DIR}"
+ "${USERVER_CMAKE_GEN_PYTHON_BINARY}" -u
+ "${USERVER_ROOT_DIR}/scripts/external_deps/cmake_generator.py" --repo-dir
+ "${USERVER_ROOT_DIR}" --build-dir "${CMAKE_BINARY_DIR}"
RESULT_VARIABLE RESULT
- WORKING_DIRECTORY "${USERVER_ROOT_DIR}"
-)
+ WORKING_DIRECTORY "${USERVER_ROOT_DIR}")
if(RESULT)
- message(FATAL_ERROR
- "Generating cmake files failed with exit code: ${RESULT}"
- )
+ message(FATAL_ERROR "Generating cmake files failed with exit code: ${RESULT}")
endif(RESULT)
-set(USERVER_THIRD_PARTY_DIRS ${USERVER_ROOT_DIR}/third_party CACHE INTERNAL "")
+set(USERVER_THIRD_PARTY_DIRS
+ ${USERVER_ROOT_DIR}/third_party
+ CACHE INTERNAL "")
init_debian_depends()
include(SetupGTest)
-if (USERVER_FEATURE_GRPC)
+if(USERVER_FEATURE_GRPC)
include(SetupProtobuf)
endif()
-if (USERVER_IS_THE_ROOT_PROJECT)
+if(USERVER_IS_THE_ROOT_PROJECT)
include(testsuite/SetupUserverTestsuiteEnv.cmake)
add_subdirectory(testsuite)
endif()
add_subdirectory(universal)
-if (USERVER_FEATURE_CORE)
+if(USERVER_FEATURE_CORE)
add_subdirectory(core)
list(APPEND USERVER_AVAILABLE_COMPONENTS core)
endif()
-if (USERVER_FEATURE_CHAOTIC)
+if(USERVER_FEATURE_CHAOTIC)
add_subdirectory(chaotic)
list(APPEND USERVER_AVAILABLE_COMPONENTS chaotic)
endif()
-if (USERVER_IS_THE_ROOT_PROJECT AND USERVER_FEATURE_CORE)
+if(USERVER_IS_THE_ROOT_PROJECT AND USERVER_FEATURE_CORE)
add_subdirectory(tools/engine)
add_subdirectory(tools/httpclient)
add_subdirectory(tools/netcat)
@@ -204,60 +217,60 @@ if (USERVER_IS_THE_ROOT_PROJECT AND USERVER_FEATURE_CORE)
add_subdirectory(tools/congestion_control_emulator)
endif()
-if (USERVER_FEATURE_MONGODB)
+if(USERVER_FEATURE_MONGODB)
_require_userver_core("USERVER_FEATURE_MONGODB")
add_subdirectory(mongo)
list(APPEND USERVER_AVAILABLE_COMPONENTS mongo)
endif()
-if (USERVER_FEATURE_POSTGRESQL)
+if(USERVER_FEATURE_POSTGRESQL)
_require_userver_core("USERVER_FEATURE_POSTGRESQL")
add_subdirectory(postgresql)
list(APPEND USERVER_AVAILABLE_COMPONENTS postgres)
endif()
-if (USERVER_FEATURE_REDIS)
+if(USERVER_FEATURE_REDIS)
_require_userver_core("USERVER_FEATURE_REDIS")
add_subdirectory(redis)
list(APPEND USERVER_AVAILABLE_COMPONENTS redis)
endif()
-if (USERVER_FEATURE_GRPC)
+if(USERVER_FEATURE_GRPC)
_require_userver_core("USERVER_FEATURE_GRPC")
add_subdirectory(grpc)
list(APPEND USERVER_AVAILABLE_COMPONENTS grpc)
endif()
-if (USERVER_FEATURE_CLICKHOUSE)
+if(USERVER_FEATURE_CLICKHOUSE)
_require_userver_core("USERVER_FEATURE_CLICKHOUSE")
add_subdirectory(clickhouse)
list(APPEND USERVER_AVAILABLE_COMPONENTS clickhouse)
endif()
-if (USERVER_FEATURE_KAFKA)
+if(USERVER_FEATURE_KAFKA)
_require_userver_core("USERVER_FEATURE_KAFKA")
add_subdirectory(kafka)
list(APPEND USERVER_AVAILABLE_COMPONENTS kafka)
endif()
-if (USERVER_FEATURE_RABBITMQ)
+if(USERVER_FEATURE_RABBITMQ)
_require_userver_core("USERVER_FEATURE_RABBITMQ")
add_subdirectory(rabbitmq)
list(APPEND USERVER_AVAILABLE_COMPONENTS rabbitmq)
endif()
-if (USERVER_FEATURE_MYSQL)
+if(USERVER_FEATURE_MYSQL)
_require_userver_core("USERVER_FEATURE_MYSQL")
add_subdirectory(mysql)
list(APPEND USERVER_AVAILABLE_COMPONENTS mysql)
endif()
-if (USERVER_FEATURE_ROCKS)
+if(USERVER_FEATURE_ROCKS)
_require_userver_core("USERVER_FEATURE_ROCKS")
add_subdirectory(rocks)
endif()
-if (USERVER_FEATURE_YDB)
+if(USERVER_FEATURE_YDB)
_require_userver_core("USERVER_FEATURE_YDB")
add_subdirectory(ydb)
list(APPEND USERVER_AVAILABLE_COMPONENTS ydb)
@@ -266,7 +279,7 @@ endif()
_userver_export_targets()
_userver_make_install_config()
-if (USERVER_IS_THE_ROOT_PROJECT AND USERVER_FEATURE_CORE)
+if(USERVER_IS_THE_ROOT_PROJECT AND USERVER_FEATURE_CORE)
add_subdirectory(samples)
endif()
So I'll try to write down some rules to keep the existing style.
Unfortunately, cmake_format cannot preserve the existing style (there are no such rules). So some style changes will be made in CMakeLists.txt.
After applying the formatter (cmake_format) to the whole project.
find . -type f -name 'CMakeLists.txt' | xargs cmake-format -i
The PR is meaningless without adding .cmake-format.py configuration and a GitHub CI check.
The style needs tweaking, defaults are just bad and contrast with our C++ code style:
- We probably need to adopt uniform 4-spaces indent and 4-spaces continuation indent, just like we've recently did for our C++ code
- Use a width of 120 columns to match our new C++ code style
- In the cases of long function calls, closing paren should be placed on a separate line
- Can an additional indent be applied to option values in cases like this?
(Debatable)function_with_lots_of_args( ARG_NAME value1 value2 ) - Wut
target_link_libraries(${PROJECT_NAME} PUBLIC PostgreSQLInternal userver-internal-compile-options)
For C++, we've adopted the style that formats single-line function calls as
frobnicate(bar, baz)
and multi-line function calls as
frobnicate(
bar,
baz
)
CMake code style should probably match that.
https://github.com/userver-framework/userver/pull/638#issuecomment-2605391844
- [x] We probably need to adopt uniform 4-spaces indent and 4-spaces continuation indent, just like we've recently did for our C++ code:
if(CMAKE_VERSION VERSION_GREATER_EQUAL "3.15")
- cmake_policy(SET CMP0093 NEW)
+ cmake_policy(SET CMP0093 NEW)
endif()
- [x] Use a width of 120 columns to match our new C++ code style:
-option(
- USERVER_DOWNLOAD_PACKAGES
- "Download missing third party packages and use the downloaded versions"
- ON
+option(USERVER_DOWNLOAD_PACKAGES "Download missing third party packages and use the downloaded versions" ON)
)
- [x] In case of long function calls closing parenthesis should be placed on a separate line:
file(GLOB_RECURSE SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/include/*.hpp
${CMAKE_CURRENT_SOURCE_DIR}/src/*.hpp
- ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp)
+ ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp
+)
- [x] Additional indent to be applied to option values in cases like this (debatable):
function_with_lots_of_args(
- ARG_NAME value1 value2
+ ARG_NAME
+ value1
+ value2
)
[!NOTE] After format we get such style:
userver_venv_setup( NAME userver-chaotic-tests PYTHON_OUTPUT_VAR USERVER_CHAOTIC_PYTEST_PYTHON_BINARY - REQUIREMENTS - "${USERVER_ROOT_DIR}/scripts/chaotic/requirements.txt" - "${USERVER_ROOT_DIR}/testsuite/requirements.txt" + REQUIREMENTS "${USERVER_ROOT_DIR}/scripts/chaotic/requirements.txt" + "${USERVER_ROOT_DIR}/testsuite/requirements.txt" UNIQUE )
- [x] For C++, we've adopted the style that formats single-line function calls as single and multi-line function calls - CMake code style should probably match that:
+frobnicate(bar, baz)
+frobnicate(
+ bar,
+ baz
+)
[!NOTE] After format we get such style (if a positional argument group contains more than 6 arguments, then force it to a vertical layout):
- userver_add_grpc_library(${PROJECT_NAME}-unittest-proto + userver_add_grpc_library( + ${PROJECT_NAME}-unittest-proto PROTOS # Absolute paths are allowed ${CMAKE_CURRENT_SOURCE_DIR}/proto/tests/unit_test.proto # As well as paths relative to CMAKE_CURRENT_SOURCE_DIR tests/messages.proto tests/protobuf.proto tests/same_service_and_method_name.proto tests/global_package.proto tests/repeating_word_in_package_name.proto tests/secret_fields.proto INCLUDE_DIRECTORIES ${CMAKE_CURRENT_SOURCE_DIR}/proto )
@Anton3 - need you feedback about https://github.com/userver-framework/userver/pull/638#issuecomment-2643013573.
@idzm That's such a great work, thank you! Any chance you'll also add a format check to .github/workflows?
Now if check fails, we get comments:
❌ Check with cmake-format failed
Diff for CMakeLists.txt:
diff --git a/CMakeLists.txt b/CMakeLists.txt index cd8d5bf..dae40f1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,10 +7,8 @@ set(CMAKE_POLICY_DEFAULT_CMP0028 NEW) set(CMAKE_POLICY_DEFAULT_CMP0048 NEW) set(CMAKE_POLICY_DEFAULT_CMP0063 NEW) set(CMAKE_POLICY_DEFAULT_CMP0069 NEW) -set( - CMAKE_POLICY_DEFAULT_CMP0074 NEW) -set( - CMAKE_POLICY_DEFAULT_CMP0077 NEW) +set(CMAKE_POLICY_DEFAULT_CMP0074 NEW) +set(CMAKE_POLICY_DEFAULT_CMP0077 NEW) project(userver)Diff for mysql/CMakeLists.txt:
diff --git a/mysql/CMakeLists.txt b/mysql/CMakeLists.txt index 71e73ab..a6bbcc9 100644 --- a/mysql/CMakeLists.txt +++ b/mysql/CMakeLists.txt @@ -10,7 +10,7 @@ userver_module( LINK_LIBRARIES_PRIVATE libmariadb ZLIB::ZLIB OpenSSL::SSL OpenSSL::Crypto DBTEST_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/tests" UTEST_LINK_LIBRARIES libmariadb - DBTEST_DATABASES mysql + DBTEST_DATABASES mysql UBENCH_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/benchmarks" UBENCH_LINK_LIBRARIES libmariadb UBENCH_DATABASES mysql @@ -21,7 +21,7 @@ if(USERVER_MYSQL_ALLOW_BUGGY_LIBMARIADB) endif() if(USERVER_MYSQL_OLD_INCLUDE) target_compile_definitions(${PROJECT_NAME} PRIVATE -DUSERVER_MYSQL_OLD_INCLUDE = 1) -if(USERVER_BUILD_TESTS) + if(USERVER_BUILD_TESTS) target_compile_definitions(${PROJECT_NAME}-dbtest PRIVATE -DUSERVER_MYSQL_OLD_INCLUDE=1) endif() endif() @@ -34,6 +34,5 @@ _userver_directory_install( if(USERVER_BUILD_TESTS) - add_subdirectory(functional_tests) endif()
@apolukhin - need you feedback.
Many thanks! Will start merging the PR in a week or two
[!NOTE]
I found a slightly more modern cmake formatter - gersemi. I want to try it and compare it with cmake-format.