As long as you use the add_custom_command(OUTPUT ...) and not the add_custom_command(TARGET ...), what you want to do is possible.
What have you tried so far? Did you read the documentation of the BYPRODUCTS and DEPENDS keyword of add_custom_command?