From 2ada6730b2037b6e1d2d9ac9c620f748d5b1334c Mon Sep 17 00:00:00 2001 From: "Ryan M. Richard" Date: Tue, 17 Jun 2025 09:56:25 -0500 Subject: [PATCH 1/2] adds function for getting enabled languages --- cmake/cmakepp_lang/cmakepp_lang.cmake | 1 + cmake/cmakepp_lang/detection/detection.cmake | 20 +++++++++ cmake/cmakepp_lang/detection/languages.cmake | 43 ++++++++++++++++++++ tests/CMakeLists.txt | 3 +- tests/detection/languages.cmake | 19 +++++++++ 5 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 cmake/cmakepp_lang/detection/detection.cmake create mode 100644 cmake/cmakepp_lang/detection/languages.cmake create mode 100644 tests/detection/languages.cmake diff --git a/cmake/cmakepp_lang/cmakepp_lang.cmake b/cmake/cmakepp_lang/cmakepp_lang.cmake index 9741c8d4..c7dbb85c 100644 --- a/cmake/cmakepp_lang/cmakepp_lang.cmake +++ b/cmake/cmakepp_lang/cmakepp_lang.cmake @@ -24,6 +24,7 @@ include_guard() include(cmakepp_lang/algorithm/algorithm) include(cmakepp_lang/asserts/asserts) include(cmakepp_lang/class/class) +include(cmakepp_lang/detection/detection) include(cmakepp_lang/exceptions/exceptions) include(cmakepp_lang/map/map) include(cmakepp_lang/object/object) diff --git a/cmake/cmakepp_lang/detection/detection.cmake b/cmake/cmakepp_lang/detection/detection.cmake new file mode 100644 index 00000000..eb1b2424 --- /dev/null +++ b/cmake/cmakepp_lang/detection/detection.cmake @@ -0,0 +1,20 @@ +# Copyright 2025 CMakePP +# +# Licensed 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. + +#[[[ @module +# Provides functions for detecting the state of CMake and/or the runtime. +#]] + +include_guard() +include(cmakepp_lang/detection/languages) \ No newline at end of file diff --git a/cmake/cmakepp_lang/detection/languages.cmake b/cmake/cmakepp_lang/detection/languages.cmake new file mode 100644 index 00000000..448d5572 --- /dev/null +++ b/cmake/cmakepp_lang/detection/languages.cmake @@ -0,0 +1,43 @@ +# Copyright 2025 CMakePP +# +# Licensed 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. + +include_guard() +include(cmakepp_lang/utilities/return) + +#[[[ +# Wraps the process of retrieving a list of enabled languages. +# +# CMake sets a global property ``ENABLED_LANGUAGES`` to be a list of coding +# languages which have been enabled either via ``project()`` or the +# ``enable_language()`` commands. CMake's documentation is a bit dodgy in +# discussing what variables the aforementioned commands set, so we wrote this +# function as a wrapper around the logic in the event that CMake changes how +# they do things. At the very least, this function should be more user- +# friendly. +# +# :param: rv A variable to hold the resulting list. +# :type: list* +#]] +function(cpp_enabled_languages _el_rv) + get_property("${_el_rv}" GLOBAL PROPERTY ENABLED_LANGUAGES) + + # CMake apparently doesn't remove NONE from the list if other languages are + # also in the list... + list(LENGTH "${_el_rv}" _el_length) + if("${_el_length}" GREATER 1) + list(REMOVE_ITEM "${_el_rv}" "NONE") + endif() + + cpp_return("${_el_rv}") +endfunction() \ No newline at end of file diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 19dbd689..851b741d 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -20,14 +20,15 @@ FetchContent_Declare( set(build_testing_old "${BUILD_TESTING}") set(BUILD_TESTING OFF CACHE BOOL "" FORCE) FetchContent_MakeAvailable(cmake_test) + include(cmake_test/cmake_test) set(BUILD_TESTING "${build_testing_old}" CACHE BOOL "" FORCE) - ct_add_dir(algorithm) ct_add_dir(asserts) ct_add_dir(class) +ct_add_dir(detection) ct_add_dir(docs) ct_add_dir(examples) ct_add_dir(exceptions) diff --git a/tests/detection/languages.cmake b/tests/detection/languages.cmake new file mode 100644 index 00000000..8445005f --- /dev/null +++ b/tests/detection/languages.cmake @@ -0,0 +1,19 @@ +ct_add_test(NAME test_cpp_enabled_languages) +function(${test_cpp_enabled_languages}) + include(cmakepp_lang/detection/languages) + + #cpp_enabled_languages(the_languages) + #set(correct "NONE") + #ct_assert_equal(the_languages "${correct}") + + enable_language(C) + cpp_enabled_languages(the_languages) + set(correct "C") + ct_assert_equal(the_languages "${correct}") + + enable_language(CXX) + cpp_enabled_languages(the_languages) + set(correct "C" "CXX") + ct_assert_equal(the_languages "${correct}") +endfunction() + From 446ec52926314099a59a9d22118477f9750170ce Mon Sep 17 00:00:00 2001 From: "Ryan M. Richard" Date: Wed, 18 Jun 2025 08:50:31 -0500 Subject: [PATCH 2/2] suggested review changes --- cmake/cmakepp_lang/detection/languages.cmake | 23 ++++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/cmake/cmakepp_lang/detection/languages.cmake b/cmake/cmakepp_lang/detection/languages.cmake index 448d5572..0e9f81f5 100644 --- a/cmake/cmakepp_lang/detection/languages.cmake +++ b/cmake/cmakepp_lang/detection/languages.cmake @@ -18,26 +18,31 @@ include(cmakepp_lang/utilities/return) #[[[ # Wraps the process of retrieving a list of enabled languages. # -# CMake sets a global property ``ENABLED_LANGUAGES`` to be a list of coding -# languages which have been enabled either via ``project()`` or the -# ``enable_language()`` commands. CMake's documentation is a bit dodgy in +# CMake sets a global property +# :ref:`ENABLED_LANGUAGES ` +# to be a list of coding +# languages which have been enabled either via +# :ref:`project() ` or +# the +# :ref:`enable_language() ` +# commands. CMake's documentation is a bit dodgy in # discussing what variables the aforementioned commands set, so we wrote this # function as a wrapper around the logic in the event that CMake changes how # they do things. At the very least, this function should be more user- # friendly. # -# :param: rv A variable to hold the resulting list. +# :param: return_variable A variable to hold the resulting list. # :type: list* #]] -function(cpp_enabled_languages _el_rv) - get_property("${_el_rv}" GLOBAL PROPERTY ENABLED_LANGUAGES) +function(cpp_enabled_languages _el_return_variable) + get_property("${_el_return_variable}" GLOBAL PROPERTY ENABLED_LANGUAGES) # CMake apparently doesn't remove NONE from the list if other languages are # also in the list... - list(LENGTH "${_el_rv}" _el_length) + list(LENGTH "${_el_return_variable}" _el_length) if("${_el_length}" GREATER 1) - list(REMOVE_ITEM "${_el_rv}" "NONE") + list(REMOVE_ITEM "${_el_return_variable}" "NONE") endif() - cpp_return("${_el_rv}") + cpp_return("${_el_return_variable}") endfunction() \ No newline at end of file