This is an automated email from the ASF dual-hosted git repository.

bneradt pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficserver.git


The following commit(s) were added to refs/heads/master by this push:
     new 9a54273290 Add AutoOptionHelpers.cmake (#10158)
9a54273290 is described below

commit 9a5427329067b6b350627ec33c1f78a7e0884910
Author: JosiahWI <[email protected]>
AuthorDate: Sat Sep 16 20:09:04 2023 -0500

    Add AutoOptionHelpers.cmake (#10158)
    
    This module assists with making the usage of optional libraries
    consistent and in agreement with automake. It provides functions
    to register new options, and to print out the state of all associated
    features.
    
    Each option based on a package will follow the format ENABLE_XXX,
    and will have an associated feature variable (e.g. TS_HAS_JEMALLOC).
    Its value can be ON, 1, TRUE, OFF, 0, FALSE, or AUTO.
    
    When the states of the features are printed out, features that are
    ON or OFF will generally be color coded green or red respectively to
    help configuration mistakes stand out. Options that were explicitly
    turned off are given special treatment: if the feature is off, it
    will not be color coded, because this is expected; if the feature is
    found to be enabled despite the user's request, it will be colored red
    and a CMake warning will be issued. All of this should help keep things
    consistent and avoid any potentially annoying bugs caused by 
misconfiguration.
---
 CMakeLists.txt                      |  37 ++++++---
 cmake/AutoOptionHelpers.cmake       | 157 ++++++++++++++++++++++++++++++++++++
 cmake/FindTSMallocReplacement.cmake |  76 -----------------
 plugins/CMakeLists.txt              |   2 +-
 4 files changed, 183 insertions(+), 89 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 2ea47090d6..922ace33b9 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -47,14 +47,26 @@ execute_process(COMMAND id -ng OUTPUT_VARIABLE BUILD_GROUP 
OUTPUT_STRIP_TRAILING
 execute_process(COMMAND uname -n OUTPUT_VARIABLE BUILD_MACHINE 
OUTPUT_STRIP_TRAILING_WHITESPACE)
 
 # Options
+include(AutoOptionHelpers)
+auto_option(HWLOC FEATURE_VAR TS_USE_HWLOC PACKAGE_DEPENDS hwloc)
+auto_option(JEMALLOC
+    FEATURE_VAR TS_HAS_JEMALLOC
+    PACKAGE_DEPENDS jemalloc
+    DEFAULT OFF
+)
+auto_option(MIMALLOC
+    FEATURE_VAR TS_HAS_MIMALLOC
+    PACKAGE_DEPENDS mimalloc
+    DEFAULT OFF
+)
+auto_option(LUAJIT PACKAGE_DEPENDS LuaJIT)
+auto_option(UNWIND FEATURE_VAR TS_USE_REMOTE_UNWINDING PACKAGE_DEPENDS unwind)
+
 option(ENABLE_ASAN "Use address sanitizer (default OFF)")
 option(BUILD_REGRESSION_TESTING "Build regression tests (default ON)" ON)
 option(BUILD_EXPERIMENTAL_PLUGINS "Build the experimental plugins (default 
OFF)")
 set(DEFAULT_STACK_SIZE 1048576 CACHE STRING "Default stack size (default 
1048576)")
 option(ENABLE_FAST_SDK "Use fast SDK APIs (default OFF)")
-option(ENABLE_JEMALLOC "Use jemalloc (default OFF)")
-option(ENABLE_LUAJIT   "Use LuaJIT (default OFF)")
-option(ENABLE_MIMALLOC "Use mimalloc (default OFF)")
 option(ENABLE_DOCS "Build docs (default OFF)")
 option(ENABLE_DISK_FAILURE_TESTS "Build disk failure tests (enables AIO fault 
injection, default OFF)" OFF)
 if(ENABLE_DISK_FAILURE_TESTS)
@@ -102,7 +114,7 @@ set(ENABLE_TPROXY
         anything else to enable."
 )
 option(ENABLE_QUICHE   "Use quiche (default OFF)")
-option(ENABLE_UNWIND   "Use libunwind if found on system (default ON)" ON)
+
 option(ENABLE_WCCP     "Use WCCP v2 (default OFF)")
 option(ENABLE_EXAMPLE  "Build example directory (default OFF)")
 set(TS_MAX_HOST_NAME_LEN 256 CACHE STRING "Max host name length (default 256)")
@@ -120,7 +132,6 @@ set(MAX_THREADS_PER_TYPE
 )
 set(TS_USE_SET_RBIO 1 CACHE STRING "Use openssl set_rbio (default 1)")
 set(TS_USE_DIAGS 1 CACHE STRING "Use diags (default 1)")
-option(TS_USE_HWLOC "Use hwloc (default OFF)")
 option(ENABLE_CRIPTS "Build and install the Cripts library and headers  
(default OFF)")
 
 set(TS_USE_FAST_SDK ${ENABLE_FAST_SDK})
@@ -177,10 +188,6 @@ if(brotli_FOUND)
     set(HAVE_BROTLI_ENCODE_H TRUE)
 endif()
 
-if(ENABLE_LUAJIT)
-    find_package(LuaJIT REQUIRED)
-endif()
-
 if(ENABLE_POSIX_CAP)
     find_package(cap REQUIRED)
     set(TS_USE_POSIX_CAP ${cap_FOUND})
@@ -222,9 +229,14 @@ if(ENABLE_PROFILER)
     set(TS_HAS_PROFILER ${profiler_FOUND})
 endif()
 
-find_package(TSMallocReplacement QUIET MODULE)
-if(TSMallocReplacement_FOUND)
-    link_libraries(ts::TSMallocReplacement)
+if(TS_HAS_JEMALLOC AND TS_HAS_MIMALLOC)
+    message(FATAL_ERROR "Cannot build with both jemalloc and mimalloc.")
+endif()
+
+if(TS_HAS_JEMALLOC)
+    link_libraries(jemalloc)
+elseif(TS_HAS_MIMALLOC)
+    link_libraries(mimalloc)
 endif()
 
 if(ENABLE_UNWIND)
@@ -530,3 +542,4 @@ cmake_print_variables(TS_PKGSYSGROUP)
 cmake_print_variables(BUILD_MACHINE)
 cmake_print_variables(DEFAULT_STACK_SIZE)
 cmake_print_variables(CMAKE_INSTALL_RPATH)
+print_auto_options_summary()
diff --git a/cmake/AutoOptionHelpers.cmake b/cmake/AutoOptionHelpers.cmake
new file mode 100644
index 0000000000..2527123715
--- /dev/null
+++ b/cmake/AutoOptionHelpers.cmake
@@ -0,0 +1,157 @@
+#######################
+#
+#  Licensed to the Apache Software Foundation (ASF) under one or more 
contributor license
+#  agreements.  See the NOTICE file distributed with this work for additional 
information regarding
+#  copyright ownership.  The ASF licenses this file to you under the Apache 
License, Version 2.0
+#  (the "License"); you may not use this file except in compliance with the 
License.  You may obtain
+#  a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing, software 
distributed under the License
+#  is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 
KIND, either express
+#  or implied. See the License for the specific language governing permissions 
and limitations under
+#  the License.
+#
+#######################
+
+set(GLOBAL_AUTO_OPTION_VARS "")
+
+function(_REGISTER_AUTO_OPTION _NAME _FEATURE_VAR _DESCRIPTION _DEFAULT)
+  add_custom_target(${_NAME}_target)
+  set_target_properties(${_NAME}_target
+    PROPERTIES
+      AUTO_OPTION_FEATURE_VAR ${_FEATURE_VAR}
+  )
+
+  set(${_NAME} ${_DEFAULT} CACHE STRING "${_DESCRIPTION}")
+  set_property(CACHE ${_NAME} PROPERTY STRINGS AUTO ON OFF)
+
+  set(LOCAL_AUTO_OPTION_VARS ${GLOBAL_AUTO_OPTION_VARS})
+  list(APPEND LOCAL_AUTO_OPTION_VARS "${_NAME}")
+  set(GLOBAL_AUTO_OPTION_VARS ${LOCAL_AUTO_OPTION_VARS} PARENT_SCOPE)
+endfunction()
+
+macro(_CHECK_PACKAGE_DEPENDS _OPTION_VAR _PACKAGE_DEPENDS _FEATURE_VAR)
+  if(${${_OPTION_VAR}} STREQUAL AUTO)
+    set(STRICTNESS QUIET)
+  else()
+    set(STRICTNESS REQUIRED)
+  endif()
+
+  foreach(PACKAGE_NAME ${_PACKAGE_DEPENDS})
+    find_package(${PACKAGE_NAME} ${STRICTNESS})
+    if(NOT ${PACKAGE_NAME}_FOUND)
+      set(_FEATURE_VAR FALSE)
+    endif()
+  endforeach()
+endmacro()
+
+# auto_option(<feature_name>
+#   [DESCRIPTION <description>]
+#   [DEFAULT <default>]
+#   [FEATURE_VAR <feature_var>]
+#   [PACKAGE_DEPENDS <package_one> <package_two> ...]
+# )
+#
+# This macro registers a new auto option and sets its corresponding feature
+# variable based on the requirements. The option it creates will be named
+# ENABLE_<feature_name>, and the default feature variable will be
+# USE_<feature_name>.
+#
+# It is necessary to have separate variables for the option and the feature
+# because the option may be AUTO, but the feature must be enabled or not.
+# It is expected that the option will have one of the values ON, OFF, or AUTO,
+# and the feature will have one of the values TRUE, or FALSE.
+#
+# Behavior of the option is as follows:
+#  - The option is falsey: the feature variable will be FALSE.
+#  - The option is AUTO: if all requirements are satisfied, the
+#      feature variable will be TRUE, otherwise FALSE.
+#  - The option is truthy: if all requirements are satisfied, the
+#      feature variable will be TRUE, otherwise a fatal error is produced.
+#
+# DESCRIPTION is the description that will go with the cache entry for the
+# option. If not provided, it will be empty.
+#
+# DEFAULT is the default value of the option. Permitted values are OFF, FALSE, 0
+# ON, TRUE, 1, AUTO. If no default is provided, AUTO is the default.
+#
+# FEATURE_VAR is the variable that will represent whether the feature should be
+# used, given the value of the option and whether the requirements for the
+# feature are satisfied. By default, it is USE_<feature_name>.
+#
+# PACKAGE_DEPENDS is a list of packages that are required for the feature.
+macro(auto_option _FEATURE_NAME)
+  cmake_parse_arguments(ARG
+    ""
+    "DESCRIPTION;DEFAULT;FEATURE_VAR"
+    "PACKAGE_DEPENDS"
+    ${ARGN}
+  )
+
+  set(OPTION_VAR "ENABLE_${_FEATURE_NAME}")
+  if(ARG_FEATURE_VAR)
+    set(FEATURE_VAR ${ARG_FEATURE_VAR})
+  else()
+    set(FEATURE_VAR "USE_${_FEATURE_NAME}")
+  endif()
+
+  if(NOT ARG_DEFAULT)
+    set(DEFAULT AUTO)
+  elseif(ARG_DEFAULT MATCHES "(ON)|(AUTO)|(TRUE)|(1)")
+    set(DEFAULT ${ARG_DEFAULT})
+  else()
+    message(FATAL_ERROR "Invalid auto_option default ${ARG_DEFAULT}")
+  endif()
+
+  _register_auto_option(${OPTION_VAR} ${FEATURE_VAR} "${ARG_DESCRIPTION}" 
"${DEFAULT}")
+
+  if(${${OPTION_VAR}})
+    set(${FEATURE_VAR} TRUE)
+    _check_package_depends(${OPTION_VAR} "${ARG_PACKAGE_DEPENDS}" 
${FEATURE_VAR})
+  else()
+    set(${FEATURE_VAR} FALSE)
+  endif()
+endmacro()
+
+# Prints a colorized summary of one auto option.
+function(PRINT_AUTO_OPTION _NAME)
+  string(ASCII 27 ESC)
+  set(COLOR_RED   "${ESC}[31m")
+  set(COLOR_GREEN "${ESC}[32m")
+  set(COLOR_RESET "${ESC}[m")
+
+  set(OPTION_VALUE ${${_NAME}})
+
+  get_target_property(FEATURE "${_NAME}_target" AUTO_OPTION_FEATURE_VAR)
+  set(FEATURE_VALUE ${${FEATURE}})
+
+  set(RESET ${COLOR_RESET})
+  if(FEATURE_VALUE)
+    if(OPTION_VALUE)
+      set(COLOR ${COLOR_GREEN})
+    else()
+      message(WARNING "${FEATURE} truthy but ${_NAME} isn't!")
+      set(COLOR ${COLOR_RED})
+    endif()
+  else()
+    if(OPTION_VALUE)
+      set(COLOR ${COLOR_RED})
+    else()
+      set(RESET "")
+    endif()
+  endif()
+
+  message(STATUS "${FEATURE}: ${COLOR}${FEATURE_VALUE}${RESET} 
(${OPTION_VALUE})")
+endfunction()
+
+# Prints out a colorized summary of all auto options.
+function(PRINT_AUTO_OPTIONS_SUMMARY)
+  message(STATUS "")
+  message(STATUS "-------- AUTO OPTIONS SUMMARY --------")
+  foreach(OPTION_NAME ${GLOBAL_AUTO_OPTION_VARS})
+    print_auto_option(${OPTION_NAME})
+  endforeach()
+  message(STATUS "")
+endfunction()
diff --git a/cmake/FindTSMallocReplacement.cmake 
b/cmake/FindTSMallocReplacement.cmake
deleted file mode 100644
index 110a4f684d..0000000000
--- a/cmake/FindTSMallocReplacement.cmake
+++ /dev/null
@@ -1,76 +0,0 @@
-#######################
-#
-#  Licensed to the Apache Software Foundation (ASF) under one or more 
contributor license
-#  agreements.  See the NOTICE file distributed with this work for additional 
information regarding
-#  copyright ownership.  The ASF licenses this file to you under the Apache 
License, Version 2.0
-#  (the "License"); you may not use this file except in compliance with the 
License.  You may obtain
-#  a copy of the License at
-#
-#      http://www.apache.org/licenses/LICENSE-2.0
-#
-#  Unless required by applicable law or agreed to in writing, software 
distributed under the License
-#  is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 
KIND, either express
-#  or implied. See the License for the specific language governing permissions 
and limitations under
-#  the License.
-#
-#######################
-
-# FindTSMallocReplacement.cmake
-#
-# This will define the following variables
-#
-#     TSMallocReplacement_FOUND
-#     TS_HAS_MALLOC_REPLACEMENT
-#     TS_HAS_JEMALLOC
-#     TS_HAS_MIMALLOC
-#
-# and the following imported targets
-#
-#     ts::TSMallocReplacement
-#
-
-if(ENABLE_JEMALLOC AND ENABLE_MIMALLOC)
-    message(FATAL_ERROR "Cannot build with both jemalloc and mimalloc.")
-endif()
-
-if(ENABLE_JEMALLOC)
-    find_package(jemalloc REQUIRED)
-endif()
-set(TS_HAS_JEMALLOC ${jemalloc_FOUND})
-
-if(ENABLE_MIMALLOC)
-    find_package(mimalloc REQUIRED)
-endif()
-set(TS_HAS_MIMALLOC ${mimalloc_FOUND})
-
-if(TS_HAS_JEMALLOC OR TS_HAS_MIMALLOC)
-    set(TS_HAS_MALLOC_REPLACEMENT TRUE)
-endif()
-
-mark_as_advanced(
-    TSMallocReplacement_FOUND
-    TS_HAS_MALLOC_REPLACEMENT
-    TS_HAS_JEMALLOC
-    TS_HAS_MIMALLOC
-)
-
-include(FindPackageHandleStandardArgs)
-find_package_handle_standard_args(TSMallocReplacement
-    REQUIRED_VARS TS_HAS_MALLOC_REPLACEMENT
-)
-
-if(TSMallocReplacement_FOUND AND NOT TARGET ts::TSMallocReplacement)
-    add_library(ts::TSMallocReplacement INTERFACE IMPORTED)
-    if(TS_HAS_JEMALLOC)
-        target_link_libraries(ts::TSMallocReplacement
-            INTERFACE
-                jemalloc::jemalloc
-        )
-    elseif(TS_HAS_MIMALLOC)
-        add_library(mimalloc::mimalloc ALIAS mimalloc)
-        target_link_libraries(ts::TSMallocReplacement
-            INTERFACE
-                mimalloc::mimalloc
-    )
-    endif()
-endif()
diff --git a/plugins/CMakeLists.txt b/plugins/CMakeLists.txt
index 65a41b04fe..925e48e56a 100644
--- a/plugins/CMakeLists.txt
+++ b/plugins/CMakeLists.txt
@@ -48,7 +48,7 @@ if(NOT OPENSSL_IS_BORINGSSL)
     add_subdirectory(ja3_fingerprint)
 endif()
 
-if(ENABLE_LUAJIT)
+if(USE_LUAJIT)
     add_subdirectory(lua)
 endif()
 

Reply via email to