how to configure a cmake function to have a named argument which can be undefined (so not used when calling the function), an empty string or a non-empty string? how to differentiate between being undefined or empty?
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
In CMake, functions are typically defined using positional arguments or with a keyword-style syntax (like ARGN, ARGV, etc.), and CMake doesn't natively support named optional parameters in the same way as Python or other scripting languages.
However, you can simulate named arguments using key-value pairs and handle cases where an argument is either undefined, empty string, or non-empty string.
Goal
You want to define a function that:
Accepts a named parameter (e.g., SOME_ARG)
Can be omitted when calling the function
Can be passed as an empty string ("")
Can be passed as a non-empty string
And you want to differentiate between:
Not provided at all
Provided as empty string
Provided with some value
✅ Solution Overview
We'll use a pattern where we parse key-value pairs from ARGN and explicitly check for:
Whether the key was present at all → undefined
Whether the key was present but had no value or was set to empty string → ""
Whether the key was present and has a non-empty value
🛠️ Example Implementation
cmake
function(my_function)
# Parse named arguments
set(options) # No options
set(oneValueArgs SOME_ARG) # Named arg that takes one value
set(multiValueArgs) # No multi-value args
cmake_parse_arguments(
PARSE "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}
)
# Check if SOME_ARG was passed at all
if(NOT DEFINED PARSE_SOME_ARG)
message(STATUS "SOME_ARG: undefined")
else()
if("${PARSE_SOME_ARG}" STREQUAL "")
message(STATUS "SOME_ARG: defined but empty")
else()
message(STATUS "SOME_ARG: '${PARSE_SOME_ARG}'")
endif()
endif()
endfunction()
✅ Usage Examples
cmake
my_function() # SOME_ARG: undefined
my_function(SOME_ARG) # SOME_ARG: defined but empty
my_function(SOME_ARG "") # SOME_ARG: defined but empty
my_function(SOME_ARG "hello") # SOME_ARG: 'hello'
🔍 How It Works
cmake_parse_arguments parses named arguments.
If a named argument is not provided, its variable (PARSE_SOME_ARG) will be undefined.
If it's provided without a value (SOME_ARG), it gets set to empty string.
If it's provided with a value like "hello", then it holds that value.
This lets you distinguish cleanly between:
Not provided (NOT DEFINED PARSE_SOME_ARG)
Empty string (STREQUAL "")
Non-empty string (NOT STREQUAL "")
⚠️ Important Notes
When passing SOME_ARG with no value, CMake treats it as "".
This behavior is by design in cmake_parse_arguments.
🧪 Bonus: Full Test Script
cmake
function(my_function)
set(oneValueArgs SOME_ARG)
cmake_parse_arguments(PARSE "" "${oneValueArgs}" "" ${ARGN})
if(NOT DEFINED PARSE_SOME_ARG)
message(STATUS "SOME_ARG: undefined")
elseif("${PARSE_SOME_ARG}" STREQUAL "")
message(STATUS "SOME_ARG: defined but empty")
else()
message(STATUS "SOME_ARG: '${PARSE_SOME_ARG}'")
endif()
endfunction()
# Test calls
message("Test 1: Not provided")
my_function()
message("Test 2: Provided with no value")
my_function(SOME_ARG)
message("Test 3: Provided as empty string")
my_function(SOME_ARG "")
message("Test 4: Provided with value")
my_function(SOME_ARG "hello world")
Let me know if you'd like to support multiple named arguments or more complex parsing!