CMake List of all Project Targets

The recursion is broken because the condition doesn’t account for the actual behavior of list(APPEND). Running:

set(foo "4;")
list(APPEND foo "4;")
message("${foo}")

outputs

4;;4;

so the condition should be if(NOT "${${in_out_var}}" STREQUAL "4;;4;;4;").

Or we could rewrite the function to avoid this extra semicolon:

function(simpleRecursive in_out_var)
    list(APPEND ${in_out_var} "4")
    if(NOT "${${in_out_var}}" STREQUAL "4;4;4")
        simpleRecursive(${in_out_var})
    endif()
    set(${in_out_var} ${${in_out_var}} PARENT_SCOPE)
endfunction()

set(list_of_fours "4")
simpleRecursive(list_of_fours)
message("${list_of_fours}")

${${in_out_var}} means "the value of the value of in_out_var". Maybe an example can help:

function(print_variable var)
  message("${var}")  # this outputs "my_letter"
  message("${${var}}")  # this outputs "A"
endfunction()

set(my_letter "A")
print_variable(my_letter)

If you have more questions about the CMake syntax, don’t hesitate to create a new thread and to ping me on it!

1 Like