CMake ARGV2 is "dangerous"

SDK for VST 3 audio plug-in and host development.
Post Reply
User avatar
pongasoft
Posts: 116
Joined: Sun Mar 11, 2018 5:57 pm
Location: Las Vegas, USA
Contact:

CMake ARGV2 is "dangerous"

Post by pongasoft »

I have the following code:

Code: Select all

function(jamba_add_vst3_resource target type filename)
  smtg_add_vst3_resource(${target} "${JAMBA_RESOURCE_DIR}/${filename}")
  # use type...
endfunction()
The issue is that:

Code: Select all

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

Code: Select all

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

User avatar
pongasoft
Posts: 116
Joined: Sun Mar 11, 2018 5:57 pm
Location: Las Vegas, USA
Contact:

Re: CMake ARGV2 is "dangerous"

Post by pongasoft »

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

Code: Select all

// dangerous
if(ARGV2)

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

Post Reply