CMakeMacroCreateFinalFile: Difference between revisions

From KitwarePublic
Jump to navigationJump to search
(Add explicit preformat markup)
(Remove leading space rectangles from preformatted blocks)
Line 8: Line 8:


<pre>
<pre>
  SET(app_SRCS main.cpp app.cpp bar.c foo.c)
SET(app_SRCS main.cpp app.cpp bar.c foo.c)
  OPTION(ENABLE_FINAL "Enable final all-in-one compilation.")
OPTION(ENABLE_FINAL "Enable final all-in-one compilation.")
  IF(ENABLE_FINAL)
IF(ENABLE_FINAL)
      CREATE_FINAL_FILE(_final.cpp ${_app_SRCS} )
  CREATE_FINAL_FILE(_final.cpp ${_app_SRCS} )
      ADD_EXECUTABLE(foo _final.cpp)
  ADD_EXECUTABLE(foo _final.cpp)
  ELSE(ENABLE_FINAL)
ELSE(ENABLE_FINAL)
      ADD_EXECUTABLE(foo ${app_SRCS} )
  ADD_EXECUTABLE(foo ${app_SRCS} )
  ENDIF(ENABLE_FINAL)
ENDIF(ENABLE_FINAL)
</pre>
</pre>
-----
-----
Line 25: Line 25:


<pre>
<pre>
  MACRO(CREATE_FINAL_FILE _filename)
MACRO(CREATE_FINAL_FILE _filename)
      FILE(WRITE ${_filename} "//autogenerated file\n")
  FILE(WRITE ${_filename} "//autogenerated file\n")
      FOREACH (_current_FILE ${ARGN})
  FOREACH (_current_FILE ${ARGN})
        FILE(APPEND ${_filename} "#include \"${_current_FILE}\"\n")
      FILE(APPEND ${_filename} "#include \"${_current_FILE}\"\n")
      ENDFOREACH (_current_FILE)
  ENDFOREACH (_current_FILE)
  ENDMACRO(CREATE_FINAL_FILE _filename)
ENDMACRO(CREATE_FINAL_FILE _filename)
</pre>
</pre>



Revision as of 21:04, 20 April 2018

Back

Sometimes it's faster (and the compiler might be able to optimize better) to include all source files of a project into one source file and compile only this one file instead of all files each one by one. This can be done with the macro CREATE_FINAL_FILE().

Usage is like this:


SET(app_SRCS main.cpp app.cpp bar.c foo.c)
OPTION(ENABLE_FINAL "Enable final all-in-one compilation.")
IF(ENABLE_FINAL)
   CREATE_FINAL_FILE(_final.cpp ${_app_SRCS} )
   ADD_EXECUTABLE(foo _final.cpp)
ELSE(ENABLE_FINAL)
   ADD_EXECUTABLE(foo ${app_SRCS} )
ENDIF(ENABLE_FINAL)

This example creates an executable named "foo" from the sources main.cpp, app.cpp, bar.c and foo.c. If the option "ENABLE_FINAL" is enabled, these for files will be included in the file "_final.cpp" and only this will be compiled. Otherwise the four source files are compiled one by one as you know it. And here comes the macro:


MACRO(CREATE_FINAL_FILE _filename)
   FILE(WRITE ${_filename} "//autogenerated file\n")
   FOREACH (_current_FILE ${ARGN})
      FILE(APPEND ${_filename} "#include \"${_current_FILE}\"\n")
   ENDFOREACH (_current_FILE)
ENDMACRO(CREATE_FINAL_FILE _filename)

Back



CMake: [Welcome | Site Map]