# attempt to provide a convenience cmake file # that can be included in a CMakeLists.txt and that # will automatically call jpp_add_executable for any # cc file in that directory that contains a main function # # WARNING: to achieve this functionallity we are using file globbing # which is generally not the recommended way of working with CMake. # function(jpp_auto_add_executables) cmake_parse_arguments(PARSE_ARGV 0 A "" "" "LINK_LIBRARIES;COMPILE_DEFINITIONS") if(A_UNPARSED_ARGUMENTS) message(FATAL_ERROR "Got trailing arguments ${A_UNPARSED_ARGUMENTS}") endif() message(WARNING "Auto adding source files is _not_ a good idea. See cmake/GLOB.md for explanations") file(GLOB ccfiles CONFIGURE_DEPENDS *.cc) list(APPEND CMAKE_MESSAGE_CONTEXT "auto-add") foreach(file IN LISTS ccfiles) file(STRINGS ${file} hasMain REGEX "int +main") if(NOT "${hasMain}" STREQUAL "") cmake_path(GET file STEM stem) if(A_LINK_LIBRARIES) list(JOIN A_LINK_LIBRARIES " " targets) message(DEBUG "jpp_add_executable(${stem} LINK_LIBRARIES ${targets})") else() message(DEBUG "jpp_add_executable(${stem})") endif() jpp_add_executable(${stem} LINK_LIBRARIES ${A_LINK_LIBRARIES}) endif() endforeach() list(POP_BACK CMAKE_MESSAGE_CONTEXT) endfunction()