# Filename: MakeComposite.cmake
#
# Description: When run, creates a single C++ file which includes multiple
#   other C++ files, to help facilitate unity builds.
#   Unity builds provide significantly increased compile speed.
#
# Usage:
#   This script is invoked via add_custom_target, like this:
#   cmake -P MakeComposite.cmake -D COMPOSITE_FILE="x_composite1.cxx" -D COMPOSITE_SOURCES="a.cxx b.cxx"
#

if(CMAKE_SCRIPT_MODE_FILE)
  if(NOT DEFINED COMPOSITE_FILE)
    message(FATAL_ERROR "COMPOSITE_FILE should be defined when running MakeComposite.cmake!")
    return()
  endif()

  file(WRITE "${COMPOSITE_FILE}" "/* Generated by CMake.  DO NOT EDIT. */\n")

  separate_arguments(COMPOSITE_SOURCES)
  foreach(source ${COMPOSITE_SOURCES})
    file(APPEND "${COMPOSITE_FILE}" "#include \"${source}\"\n")
  endforeach()

else()
  message(SEND_ERROR "MakeComposite.cmake should not be included but run in script mode.")

endif()
