Skip to content

Commit 4dc3a70

Browse files
committed
cmake revisited all if() conditions 🙃
1 parent 384e54d commit 4dc3a70

File tree

9 files changed

+27
-24
lines changed

9 files changed

+27
-24
lines changed

‎cmake/CommonModules/CsDebug.cmake‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ endfunction()
1111
# Print all CMake variables (excluding all CMake variables by default)
1212
# cs_print_vars() == cs_print_vars(yes)
1313
function(cs_print_vars)
14-
set(exclude_cmake yes)
14+
set(exclude_cmake yes) # Exclude CMake by default
1515
if(ARGC GREATER_EQUAL 1 AND DEFINED ARGV0 AND NOT ARGV0)
1616
set(exclude_cmake no)
1717
message(STATUS "All variables:")
@@ -21,7 +21,7 @@ function(cs_print_vars)
2121

2222
get_cmake_property(variable_names VARIABLES)
2323
foreach (variable ${variable_names})
24-
if(exclude_cmake AND "${variable}" MATCHES "(^(CMAKE_.*)|^(_.*))")
24+
if(exclude_cmake AND variable MATCHES "(^(CMAKE_.*)|^(_.*))")
2525
continue()
2626
endif()
2727

@@ -50,7 +50,7 @@ endif()
5050
# Print all target properties
5151
function(cs_print_target_properties target)
5252

53-
if(NOT TARGET ${target})
53+
if(NOT TARGET ${target}) # Quotes not needed, the target cannot be a list and can't contain spaces
5454
message(FATAL_ERROR "There is no target named: ${target}")
5555
endif()
5656

@@ -82,7 +82,7 @@ endfunction()
8282
# Print all source file properties
8383
function(cs_print_source_properties source)
8484

85-
if(NOT EXISTS "${source}")
85+
if(NOT EXISTS ${source}) # Quotes not needed, the target cannot be a list (it fails on the list which is good, it quotes it doesn't fail with the list) and spaces are handled correctly without quotes
8686
message(FATAL_ERROR "There is no source file named: ${source}")
8787
endif()
8888

@@ -128,9 +128,9 @@ endfunction()
128128

129129
# Print clearly visible notice message about passed BOOL variable
130130
function(pb variable)
131-
if(NOT DEFINED ${variable})
131+
if(NOT DEFINED ${variable}) # Quotes not needed
132132
message("|||-- ${variable} : ${variable}-NOTFOUND")
133-
elseif(${variable})
133+
elseif(${variable}) # Quotes not needed because of the DEFINED check above and don't care about lists and strings
134134
message("|||-- ${variable} : ON")
135135
else()
136136
message("|||-- ${variable} : OFF")

‎cmake/CommonModules/TinyFeatureOptions.cmake‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ macro(tiny_dependent_string_option option strings doc default depends force)
5555

5656
# Determine whether the given option should be provided and visible (using the full
5757
# Condition Syntax (CMP0127 implementation))
58-
foreach(depend ${depends})
58+
foreach(depend ${depends}) # Don't use ITEMS keyword
5959
# Don't use the if(NOT ${depend}) without else() block here, the ${depend} can
6060
# contain complex condition and it can break meaning of this condition
6161
cmake_language(EVAL CODE "
@@ -66,7 +66,7 @@ macro(tiny_dependent_string_option option strings doc default depends force)
6666
)
6767
endforeach()
6868

69-
if(${option}_AVAILABLE)
69+
if(${option}_AVAILABLE) # Quotes not needed, will be _AVAILABLE at least, so FALSE on empty/undefined ${option}
7070
# Restore the previous option value from the INTERNAL cache variable saved earlier
7171
if(DEFINED CACHE{${option}})
7272
set(${option} "${${option}}" CACHE STRING "${doc}" FORCE)

‎cmake/CommonModules/TinyHelpers.cmake‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ macro(tiny_find_package package_name)
4545

4646
find_package(${package_name} ${ARGN})
4747

48-
if(${package_name}_FOUND)
48+
if(${package_name}_FOUND) # Quotes not needed, will be _FOUND at least, so FALSE on empty/undefined ${package_name}
4949
set(args "${package_name}")
5050
# These arguments will be forwarded to the find_package() by find_dependency()
5151
list(APPEND args "${ARGN}")
@@ -61,7 +61,7 @@ macro(tiny_find_package package_name)
6161
# Check if the given args are in the TINY_PACKAGE_DEPENDENCIES list
6262
get_property(packageDependencies GLOBAL PROPERTY TINY_PACKAGE_DEPENDENCIES)
6363

64-
if(NOT args IN_LIST packageDependencies)
64+
if(NOT args IN_LIST packageDependencies) # Automatic Variable Expansion applies
6565
set_property(GLOBAL APPEND PROPERTY TINY_PACKAGE_DEPENDENCIES "${args}")
6666
endif()
6767
endif()
@@ -134,7 +134,7 @@ ${TINY_UNPARSED_ARGUMENTS}")
134134

135135
option(${TINY_NAME} "${TINY_DESCRIPTION}" ${TINY_DEFAULT})
136136

137-
if(${${TINY_NAME}})
137+
if(${${TINY_NAME}}) # Quotes not needed, don't care about lists for now
138138
target_compile_definitions(${target} ${scope} ${TINY_ENABLED})
139139
else()
140140
target_compile_definitions(${target} ${scope} ${TINY_DISABLED})
@@ -163,7 +163,7 @@ endfunction()
163163
# Create an empty SQLite database file if it does not exist
164164
function(tiny_create_sqlite_db db_filepath)
165165

166-
if(EXISTS ${db_filepath})
166+
if(EXISTS ${db_filepath}) # Quotes not needed, the target cannot be a list (it fails on the list which is good, it quotes it doesn't fail with the list) and spaces are handled correctly without quotes
167167
return()
168168
endif()
169169

@@ -179,7 +179,7 @@ function(tiny_create_buildtree_tagfiles filepaths)
179179

180180
foreach(filepath ${filepaths})
181181
# Nothing to do, .build_tree tag already exists
182-
if(EXISTS ${filepath})
182+
if(EXISTS ${filepath}) # Quotes not needed, the target cannot be a list (it fails on the list which is good, it quotes it doesn't fail with the list) and spaces are handled correctly without quotes
183183
continue()
184184
endif()
185185

@@ -479,6 +479,9 @@ endfunction()
479479
# Helper function to replace /Zi and /ZI by /Z7 in the CMAKE_<C|CXX>_FLAGS_<CONFIG> option
480480
function(tiny_replace_Zi_by_Z7_for option help_string)
481481

482+
# Don't quote: ${option} MATCHES; it would change the meaning and stop working;
483+
# CMake first do the Variable Expansion and then the Automatic Variable Evaluation
484+
# on this replaced/expanded value (eg. CMAKE_CXX_FLAGS_DEBUG)
482485
if(DEFINED ${option} AND ${option} MATCHES "(/|-)(Zi|ZI)")
483486
string(REGEX REPLACE "(/|-)(Zi|ZI)" "/Z7" ${option} "${${option}}")
484487

‎cmake/CommonModules/TinyToolchainRequirement.cmake‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function(tiny_satisfies_minimum_required_qt_version out_variable)
1313

1414
# Nothing to do, Qt version was already populated (cache hit)
1515
if(DEFINED TINY_QT_VERSION AND NOT TINY_QT_VERSION STREQUAL "")
16-
if(TINY_QT_VERSION VERSION_GREATER_EQUAL minReqQtVersion)
16+
if(TINY_QT_VERSION VERSION_GREATER_EQUAL minReqQtVersion) # Automatic Variable Expansion
1717
set(${out_variable} TRUE PARENT_SCOPE)
1818

1919
# There is a very low chance that this code branch will be invoked, but I can't
@@ -59,7 +59,7 @@ in ${CMAKE_CURRENT_FUNCTION}().")
5959
"Qt version used to determine whether a minimum required Qt version was \
6060
satisfied (also used by tiny_configure_test_pch()).")
6161

62-
if(TINY_QT_VERSION VERSION_GREATER_EQUAL minReqQtVersion)
62+
if(TINY_QT_VERSION VERSION_GREATER_EQUAL minReqQtVersion) # Automatic Variable Expansion
6363
set(${out_variable} TRUE PARENT_SCOPE)
6464
else()
6565
set(${out_variable} FALSE PARENT_SCOPE)

‎cmake/Modules/FindMySQL.cmake‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ if(MySQL_FOUND)
7070
set(MySQL_INCLUDE_DIRS "${MySQL_INCLUDE_DIR}")
7171
set(MySQL_LIBRARIES "${MySQL_LIBRARY}")
7272

73-
if(NOT TARGET MySQL::MySQL)
73+
if(NOT TARGET MySQL::MySQL) # Quotes not needed (my code style)
7474
add_library(MySQL::MySQL UNKNOWN IMPORTED)
7575
set_target_properties(MySQL::MySQL
7676
PROPERTIES

‎cmake/Modules/GeneratePkgConfig.cmake‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ function(generate_and_install_pkg_config_file _target _packageName)
148148

149149
# Since CMake 3.18 FindThreads may include a generator expression requiring a target, which gets propagated to us through INTERFACE_OPTIONS.
150150
# Before CMake 3.19 there's no way to solve this in a general way, so we work around the specific case. See #4956 and CMake bug #21074.
151-
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.19)
151+
if(CMAKE_VERSION VERSION_GREATER_EQUAL "3.19")
152152
set(_target_arg TARGET ${_target})
153153
else()
154154
string(REPLACE "<COMPILE_LANG_AND_ID:CUDA,NVIDIA>" "<COMPILE_LANGUAGE:CUDA>" _interface_compile_options "${_interface_compile_options}")

‎cmake/Modules/TinyDeployment.cmake‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,9 @@ list(APPEND CMAKE_MODULE_PATH \"\${CMAKE_CURRENT_LIST_DIR}/Modules\")")
170170

171171
# Used in the Package Config and Config Version files
172172
get_property(cvf_is_multi_config GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
173-
tiny_to_bool(cvf_is_multi_config ${cvf_is_multi_config})
173+
tiny_to_bool(cvf_is_multi_config ${cvf_is_multi_config}) # Don't quote, must fail if undefined
174174

175-
tiny_to_bool(cvf_is_vcpkg ${TINY_VCPKG})
175+
tiny_to_bool(cvf_is_vcpkg ${TINY_VCPKG}) # Don't quote, must fail if undefined
176176

177177
# Generate target includes for the TinyORM package config file
178178
set(tiny_target_includes)
@@ -291,7 +291,7 @@ list(APPEND CMAKE_MODULE_PATH \"\${CMAKE_CURRENT_LIST_DIR}/cmake/Modules\")")
291291
endif()
292292

293293
get_property(cvf_is_multi_config GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
294-
tiny_to_bool(cvf_is_multi_config ${cvf_is_multi_config})
294+
tiny_to_bool(cvf_is_multi_config ${cvf_is_multi_config}) # Don't quote, must fail if undefined
295295

296296
# Generate target includes for the TinyORM package config file
297297
set(tiny_target_includes)

‎cmake/Modules/TinyTestCommon.cmake‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ function(tiny_configure_test_pch name provides_pch)
146146
# variable, it also affects CI pipelines on GitHub self-hosted runners
147147
if(TINY_QT_VERSION VERSION_LESS "6.9.0" AND
148148
NOT (DEFINED ENV{TINY_QT6_TEST_TARGET_PATCHED} AND
149-
"$ENV{TINY_QT6_TEST_TARGET_PATCHED}")
149+
"$ENV{TINY_QT6_TEST_TARGET_PATCHED}") # Quotes needed to avoid fail if undefined as conditions don't short-circuit!
150150
)
151151
target_precompile_headers(${name} PRIVATE
152152
$<$<COMPILE_LANGUAGE:CXX>:"${${TinyOrm_ns}_SOURCE_DIR}/include/pch.h">
@@ -190,7 +190,7 @@ function(tiny_throw_if_wrong_reuse_from name)
190190

191191
# Nothing to do
192192
if(NOT DEFINED CACHE{TINY_TESTS_PCH_REUSE_FROM} OR
193-
"$CACHE{TINY_TESTS_PCH_REUSE_FROM}" STREQUAL name
193+
"$CACHE{TINY_TESTS_PCH_REUSE_FROM}" STREQUAL name # Quotes needed to avoid fail if undefined as conditions don't short-circuit!
194194
)
195195
return()
196196
endif()

‎cmake/TinyPackageConfigHelpers.cmake‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ function(tiny_build_type_requirements_install_tree
122122
list(LENGTH cvfTargetConfigurations count)
123123

124124
get_property(isMultiConfig GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
125-
tiny_to_bool(isMultiConfig ${isMultiConfig})
125+
tiny_to_bool(isMultiConfig ${isMultiConfig}) # Don't quote, must fail if undefined
126126

127127
# Used in STREQUAL comparisons
128128
string(TOLOWER "${CMAKE_BUILD_TYPE}" cmakeBuildTypeLower)
@@ -190,7 +190,7 @@ function(tiny_build_type_requirements_build_tree
190190

191191
if(NOT cvf_is_multi_config)
192192
get_property(isMultiConfig GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
193-
tiny_to_bool(isMultiConfig ${isMultiConfig})
193+
tiny_to_bool(isMultiConfig ${isMultiConfig}) # Don't quote, must fail if undefined
194194

195195
# Used in STREQUAL comparisons
196196
string(TOLOWER "${CMAKE_BUILD_TYPE}" cmakeBuildTypeLower)

0 commit comments

Comments
 (0)