What is the function of add_custom_target in CMake?

That’s because the files generated by the add_custom_command() are also added as sources to the target_sources(math ...) command. The add_custom_target(BLAS_LAPACK_wrappers) call is not necessary in this particular example unless you explicitly wanted to be able to generate the sources as a standalone step at build time (e.g. by doing make BLAS_LAPACK_wrappers).