From 737ec6c415f28fa4b22889023bb63a53b7cb463e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Sun, 12 Jun 2022 20:22:53 +0200 Subject: [PATCH] Prefix all CMake options with MAGNUM_*. --- CMakeLists.txt | 77 +++++++++++++++++++++++++-- doc/python/pages/building.rst | 2 +- doc/python/pages/changelog.rst | 4 ++ modules/FindMagnumBindings.cmake | 2 +- src/CMakeLists.txt | 2 +- src/Corrade/CMakeLists.txt | 2 +- src/Corrade/Containers/CMakeLists.txt | 2 +- src/Magnum/CMakeLists.txt | 4 +- src/Magnum/GL/CMakeLists.txt | 2 +- src/Magnum/SceneGraph/CMakeLists.txt | 2 +- src/python/corrade/CMakeLists.txt | 2 +- 11 files changed, 87 insertions(+), 14 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f26ddf5..7a5958c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -36,7 +36,8 @@ endif() if(POLICY CMP0071) cmake_policy(SET CMP0071 NEW) endif() -# Superprojects can use just set(WITH_BLAH ON) without FORCE CACHE on 3.13+ +# Superprojects can use just set(MAGNUM_WITH_BLAH ON) without FORCE CACHE on +# 3.13+ if(POLICY CMP0077) cmake_policy(SET CMP0077 NEW) endif() @@ -49,11 +50,79 @@ find_package(Magnum REQUIRED) include(CMakeDependentOption) +# Options that used to be unprefixed. New options shouldn't be added to this +# list. +set(_MAGNUMBINDINGS_DEPRECATED_UNPREFIXED_OPTIONS + WITH_PYTHON + BUILD_TESTS) +# If during the first run (i.e., when the variable isn't in cache yet), check +# if any of the prefixed options are already set. If so, we assume the user is +# already switched to the prefixed options and won't accept the deprecated +# unprefixed options for backwards compatibility. This way it's possible for +# projects to reuse these variables for other purposes without affecting +# Corrade in any way. +if(NOT DEFINED _MAGNUMBINDINGS_ACCEPT_DEPRECATED_UNPREFIXED_OPTIONS) + set(_MAGNUMBINDINGS_ACCEPT_DEPRECATED_UNPREFIXED_OPTIONS ON CACHE INTERNAL "") + foreach(option ${_MAGNUMBINDINGS_DEPRECATED_UNPREFIXED_OPTIONS}) + if(DEFINED MAGNUM_${option}) + set(_MAGNUMBINDINGS_ACCEPT_DEPRECATED_UNPREFIXED_OPTIONS OFF CACHE INTERNAL "") + break() + endif() + endforeach() +endif() + # Libraries to build -option(WITH_PYTHON "Build Python bindings" OFF) -option(BUILD_TESTS "Build unit tests" OFF) +option(MAGNUM_WITH_PYTHON "Build Python bindings" OFF) +option(MAGNUM_BUILD_TESTS "Build unit tests" OFF) + +# Backwards compatibility for unprefixed CMake options. If the user isn't +# explicitly using prefixed options in the first run already, accept the +# unprefixed options, and remember this decision for subsequent runs +if(NOT DEFINED _MAGNUMBINDINGS_ACCEPT_DEPRECATED_UNPREFIXED_OPTIONS) + set(_MAGNUMBINDINGS_ACCEPT_DEPRECATED_UNPREFIXED_OPTIONS ON CACHE INTERNAL "") +endif() +# If the user wasn't explicitly using prefixed options in the first run and the +# MAGNUM_BUILD_DEPRECATED option is not currently disabled (which can get +# changed subsequently), accept the unprefixed options and print a warning if +# they're different from the prefixed ones. +if(_MAGNUMBINDINGS_ACCEPT_DEPRECATED_UNPREFIXED_OPTIONS AND MAGNUM_BUILD_DEPRECATED) + set(_MAGNUMBINDINGS_WARN_DEPRECATED_UNPREFIXED_OPTION ) + foreach(option ${_MAGNUMBINDINGS_DEPRECATED_UNPREFIXED_OPTIONS}) + if(DEFINED ${option}) + # CMake has no comparison of boolean values (EQUAL returns false if + # comparing ON and 1 or OFF and FALSE, STREQUAL also), so we have + # to do it this way. Also warn only on the first encountered + # variable so people can fix it, reconfigure and go to the next one + # that warns. + if((${option} AND NOT MAGNUM_${option}) OR + (NOT ${option} AND MAGNUM_${option}) AND NOT _MAGNUMBINDINGS_WARN_DEPRECATED_UNPREFIXED_OPTION) + set(_MAGNUMBINDINGS_WARN_DEPRECATED_UNPREFIXED_OPTION ${option}) + endif() + set(MAGNUM_${option} ${${option}}) + # If variables specified on the command line don't match any + # options, they're kept in cache but set as UNINITIALIZED, meaning + # they don't appear in cmake-gui or ccmake, so there's no way to + # fix the warning apart from hand-enditing the CMakeCache.txt or + # recreating the build dir. Update their cached type to be BOOL to + # make them appear. + set(${option} ${${option}} CACHE BOOL "Deprecated, use MAGNUM_${option} instead" FORCE) + endif() + endforeach() + + if(_MAGNUMBINDINGS_WARN_DEPRECATED_UNPREFIXED_OPTION) + # CMake 3.5+ has deprecation warnings enabled by default (which makes + # sense), 3.4 not. Use a warning there instead. + # TODO: drop when 3.4 is not supported anymore + if(CMAKE_VERSION VERSION_LESS 3.5) + set(DEPRECATION_OR_WARNING WARNING) + else() + set(DEPRECATION_OR_WARNING DEPRECATION) + endif() + message(${DEPRECATION_OR_WARNING} "Unprefixed options such as ${_MAGNUMBINDINGS_WARN_DEPRECATED_UNPREFIXED_OPTION} are deprecated, use MAGNUM_${_MAGNUMBINDINGS_WARN_DEPRECATED_UNPREFIXED_OPTION} instead. Delete the unprefixed variable from CMake cache or set both to the same value to silence this warning.") + endif() +endif() -if(BUILD_TESTS) +if(MAGNUM_BUILD_TESTS) find_package(Corrade REQUIRED TestSuite) if(CORRADE_TARGET_IOS) set(CORRADE_TESTSUITE_BUNDLE_IDENTIFIER_PREFIX "cz.mosra.magnum-bindings") diff --git a/doc/python/pages/building.rst b/doc/python/pages/building.rst index bb76038..05b1474 100644 --- a/doc/python/pages/building.rst +++ b/doc/python/pages/building.rst @@ -119,7 +119,7 @@ default location known to CMake, add their path to ``CMAKE_PREFIX_PATH``. mkdir build && cd build cmake .. \ - -DWITH_PYTHON=ON + -DMAGNUM_WITH_PYTHON=ON make Note that pybind11 compilation is quite time- and memory-hungry, so you might diff --git a/doc/python/pages/changelog.rst b/doc/python/pages/changelog.rst index 6d54829..2191f61 100644 --- a/doc/python/pages/changelog.rst +++ b/doc/python/pages/changelog.rst @@ -96,6 +96,10 @@ Changelog - Exposed :ref:`Color3.red()` and other convenience constructors (see :gh:`mosra/magnum-bindings#12`) - Fixed issues with an in-source build (see :gh:`mosra/magnum-bindings#13`) +- All CMake build options are now prefixed with ``MAGNUM_``. For backwards + compatibility, unless ``MAGNUM_BUILD_DEPRECATED`` is disabled and unless a + prefixed option is already set during the initial run, the unprefixed + options are still recognized with a warning. See also :gh:`mosra/corrade#139`. `2020.06`_ ========== diff --git a/modules/FindMagnumBindings.cmake b/modules/FindMagnumBindings.cmake index 0bcbb1a..22a48c8 100644 --- a/modules/FindMagnumBindings.cmake +++ b/modules/FindMagnumBindings.cmake @@ -160,7 +160,7 @@ if(NOT CMAKE_VERSION VERSION_LESS 3.16) # misleading messages. elseif(NOT _component IN_LIST _MAGNUMBINDINGS_IMPLICITLY_ENABLED_COMPONENTS) string(TOUPPER ${_component} _COMPONENT) - list(APPEND _MAGNUMBINDINGS_REASON_FAILURE_MESSAGE "${_component} is not built by default. Make sure you enabled WITH_${_COMPONENT} when building Magnum Bindings") + list(APPEND _MAGNUMBINDINGS_REASON_FAILURE_MESSAGE "${_component} is not built by default. Make sure you enabled MAGNUM_WITH_${_COMPONENT} when building Magnum Bindings") # Otherwise we have no idea. Better be silent than to print something # misleading. else() diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index db9f15a..8280650 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -42,7 +42,7 @@ set_directory_properties(PROPERTIES add_subdirectory(Corrade) add_subdirectory(Magnum) -if(WITH_PYTHON) +if(MAGNUM_WITH_PYTHON) add_subdirectory(python) endif() diff --git a/src/Corrade/CMakeLists.txt b/src/Corrade/CMakeLists.txt index 6b9b279..e93e7b0 100644 --- a/src/Corrade/CMakeLists.txt +++ b/src/Corrade/CMakeLists.txt @@ -27,7 +27,7 @@ # property that would have to be set on each target separately. set(CMAKE_FOLDER "Corrade/Python") -if(WITH_PYTHON) +if(MAGNUM_WITH_PYTHON) add_custom_target(CorradePython SOURCES PythonBindings.h) install(FILES PythonBindings.h DESTINATION ${CORRADE_INCLUDE_INSTALL_DIR}) diff --git a/src/Corrade/Containers/CMakeLists.txt b/src/Corrade/Containers/CMakeLists.txt index 0f3cdfe..a22105e 100644 --- a/src/Corrade/Containers/CMakeLists.txt +++ b/src/Corrade/Containers/CMakeLists.txt @@ -23,7 +23,7 @@ # DEALINGS IN THE SOFTWARE. # -if(WITH_PYTHON) +if(MAGNUM_WITH_PYTHON) set(CorradeContainersPython_HEADERS OptionalPythonBindings.h PythonBindings.h diff --git a/src/Magnum/CMakeLists.txt b/src/Magnum/CMakeLists.txt index d0b0f65..bf3d370 100644 --- a/src/Magnum/CMakeLists.txt +++ b/src/Magnum/CMakeLists.txt @@ -69,7 +69,7 @@ configure_file(${CMAKE_CURRENT_SOURCE_DIR}/versionBindings.h.cmake install(FILES ${CMAKE_CURRENT_BINARY_DIR}/versionBindings.h DESTINATION ${MAGNUM_INCLUDE_INSTALL_DIR}) -if(WITH_PYTHON) +if(MAGNUM_WITH_PYTHON) add_custom_target(MagnumPython SOURCES PythonBindings.h) install(FILES PythonBindings.h DESTINATION ${MAGNUM_INCLUDE_INSTALL_DIR}) endif() @@ -84,6 +84,6 @@ if(Magnum_SceneGraph_FOUND) add_subdirectory(SceneGraph) endif() -if(BUILD_TESTS) +if(MAGNUM_BUILD_TESTS) add_subdirectory(Test) endif() diff --git a/src/Magnum/GL/CMakeLists.txt b/src/Magnum/GL/CMakeLists.txt index 87c4ba9..f0418a8 100644 --- a/src/Magnum/GL/CMakeLists.txt +++ b/src/Magnum/GL/CMakeLists.txt @@ -23,7 +23,7 @@ # DEALINGS IN THE SOFTWARE. # -if(WITH_PYTHON) +if(MAGNUM_WITH_PYTHON) add_custom_target(MagnumGLPython SOURCES PythonBindings.h) install(FILES PythonBindings.h DESTINATION ${MAGNUM_INCLUDE_INSTALL_DIR}/GL) endif() diff --git a/src/Magnum/SceneGraph/CMakeLists.txt b/src/Magnum/SceneGraph/CMakeLists.txt index 4fc64c2..c27b7f4 100644 --- a/src/Magnum/SceneGraph/CMakeLists.txt +++ b/src/Magnum/SceneGraph/CMakeLists.txt @@ -23,7 +23,7 @@ # DEALINGS IN THE SOFTWARE. # -if(WITH_PYTHON) +if(MAGNUM_WITH_PYTHON) add_custom_target(MagnumSceneGraphPython SOURCES PythonBindings.h) install(FILES PythonBindings.h DESTINATION ${MAGNUM_INCLUDE_INSTALL_DIR}/SceneGraph) endif() diff --git a/src/python/corrade/CMakeLists.txt b/src/python/corrade/CMakeLists.txt index 1aaadf8..5dbdb23 100644 --- a/src/python/corrade/CMakeLists.txt +++ b/src/python/corrade/CMakeLists.txt @@ -98,6 +98,6 @@ set_target_properties(corrade PROPERTIES file(GENERATE OUTPUT ${output_dir}/corrade/__init__.py INPUT ${CMAKE_CURRENT_SOURCE_DIR}/__init__.py) -if(BUILD_TESTS) +if(MAGNUM_BUILD_TESTS) add_subdirectory(test) endif()