Scope visibility between add_custom_command and targets can be a bit weird. I’d recommend moving the add_custom_command calls to be beside the target (not made by add_custom_target) that will use them.
This discussion has some relevant details for that.