CMake ARGV2 is "dangerous"

I have the following code:

function(jamba_add_vst3_resource target type filename)
  smtg_add_vst3_resource(${target} "${JAMBA_RESOURCE_DIR}/${filename}")
  # use type...
endfunction()

The issue is that:

function(smtg_add_vst3_resource target input_file)
    smtg_add_plugin_resource(${target} ${input_file})
endfunction()

and then

function(smtg_add_plugin_resource target input_file)
# ...
    elseif(SMTG_MAC)
        target_sources(${target} PRIVATE ${input_file})
        set(destination_folder "Resources")
        if(ARGV2)
            set(destination_folder "${destination_folder}/${ARGV2}")
# ...

CMake has some very weird behavior going on here… the issue is every time you invoke a function, all variables defined are passed along as-is… my first function (jamba_add_vst3_resource) takes 3 arguments, so at the moment it invokes smtg_add_vst3_resource, then ARGV2 is defined… and because CMake happily passes around any variable defined, it gets passed along as well and so does smtg_add_vst3_resource. And as a result when it reaches smtg_add_plugin_resource, ARGV2 is defined even if smtg_add_vst3_resource did no provide a 3 arguments!! I think this is crazy behavior from CMake, but there you have it…

I think the moral of the story is that you should be careful using ARGVX and IMO is much safer to use cmake_parse_arguments instead…

Yan

Note that you could use ${ARGC} to check if more arguments are provided so you could write instead:

// dangerous
if(ARGV2)

// instead use
if(ARGC GREATER 2 AND ARGV2)

Yan