Skip to content

Commit 0d51ecb

Browse files
committed
Add warning message when the values of legacy option and new option differs. Add available conditions for otelcpp_option_flag.
1 parent e51def7 commit 0d51ecb

2 files changed

Lines changed: 54 additions & 8 deletions

File tree

‎CMakeLists.txt‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -256,14 +256,14 @@ if(OTELCPP_VERSIONED_LIBS AND NOT BUILD_SHARED_LIBS)
256256
message(FATAL_ERROR "OTELCPP_VERSIONED_LIBS=ON requires BUILD_SHARED_LIBS=ON")
257257
endif()
258258

259-
if(WIN32)
260-
otelcpp_option_flag(OTELCPP_WITH_ETW
261-
"Whether to include the ETW Exporter in the SDK" ON)
262-
else()
263-
if(DEFINED OTELCPP_WITH_ETW OR DEFINED WITH_ETW)
264-
message(FATAL_ERROR "OTELCPP_WITH_ETW is only supported on Windows")
265-
endif()
266-
endif(WIN32)
259+
otelcpp_option_flag(
260+
OTELCPP_WITH_ETW
261+
"Whether to include the ETW Exporter in the SDK"
262+
ON
263+
AVAILABLE_CONDITION
264+
"WIN32"
265+
UNAVAILABLE_MESSAGE
266+
"OTELCPP_WITH_ETW is only supported on Windows")
267267

268268
# Do not convert deprecated message to error
269269
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang|AppleClang")

‎cmake/tools.cmake‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,11 @@ function(_otelcpp_get_option_default __OPTION_NAME __DEFAULT_VALUE
404404
# The namespaced option takes precedence when both names are provided.
405405
if(NOT DEFINED ${__OPTION_NAME})
406406
set(__RESOLVED_DEFAULT "${__LEGACY_VALUE}")
407+
elseif(NOT "${${__OPTION_NAME}}" EQUAL "${__LEGACY_VALUE}")
408+
message(
409+
WARNING
410+
"CMake option ${__LEGACY_OPTION_NAME} is deprecated. Use ${__OPTION_NAME} instead. The values of these options differ: ${__LEGACY_OPTION_NAME}=${__LEGACY_VALUE}, ${__OPTION_NAME}=${${__OPTION_NAME}}"
411+
)
407412
endif()
408413
endif()
409414
endif()
@@ -414,6 +419,47 @@ function(_otelcpp_get_option_default __OPTION_NAME __DEFAULT_VALUE
414419
endfunction()
415420

416421
function(otelcpp_option_flag __OPTION_NAME __HELP_TEXT __DEFAULT_VALUE)
422+
set(optionArgs)
423+
set(oneValueArgs AVAILABLE_CONDITION UNAVAILABLE_MESSAGE)
424+
set(multiValueArgs)
425+
cmake_parse_arguments(_otelcpp_option_flag "${optionArgs}" "${oneValueArgs}"
426+
"${multiValueArgs}" "${ARGN}")
427+
428+
if(_otelcpp_option_flag_AVAILABLE_CONDITION)
429+
set(_option_available 0)
430+
foreach(__cond ${_otelcpp_option_flag_AVAILABLE_CONDITION})
431+
string(REGEX REPLACE " +" ";" __cond_dep "${__cond}")
432+
if(${__cond_dep})
433+
set(_option_available 1)
434+
break()
435+
endif()
436+
endforeach()
437+
if(_option_available EQUAL 0)
438+
_otelcpp_get_legacy_option_name("${__OPTION_NAME}" __LEGACY_OPTION_NAME)
439+
unset(_option_fatal_error_message)
440+
if(NOT __LEGACY_OPTION_NAME STREQUAL "" AND DEFINED
441+
${__LEGACY_OPTION_NAME})
442+
set(_option_fatal_error_message
443+
"${__LEGACY_OPTION_NAME} is unavailable.")
444+
endif()
445+
if(DEFINED ${__OPTION_NAME})
446+
if(_option_fatal_error_message)
447+
set(_option_fatal_error_message
448+
"${_option_fatal_error_message} ${__OPTION_NAME} is unavailable.")
449+
else()
450+
set(_option_fatal_error_message "${__OPTION_NAME} is unavailable.")
451+
endif()
452+
endif()
453+
if(_option_fatal_error_message)
454+
message(
455+
FATAL_ERROR
456+
"${_option_fatal_error_message}${_otelcpp_option_flag_UNAVAILABLE_MESSAGE}"
457+
)
458+
endif()
459+
return()
460+
endif()
461+
endif()
462+
417463
_otelcpp_get_option_default("${__OPTION_NAME}" "${__DEFAULT_VALUE}"
418464
__RESOLVED_DEFAULT)
419465
option(${__OPTION_NAME} "${__HELP_TEXT}" "${__RESOLVED_DEFAULT}")

0 commit comments

Comments
 (0)