add_custom_command() supports option DEPFILE when generator is Makefiles or Ninja. And swig tool is able to generate a dependencies file which is compatible with DEPFILE option.
Fixes: #17875