Difference between revisions of "CMakeMacroGatherProjectFiles"

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


<pre>
<pre>
  # add extra sources to the defined project
# add extra sources to the defined project
  MACRO ( GatherProjectFiles ProjectName ProjectDir ProjectSources )
MACRO ( GatherProjectFiles ProjectName ProjectDir ProjectSources )
    
 
    #look for the cache file
   #look for the cache file
    SET ( FoundCacheFile "FoundCacheFile-NOTFOUND" )
  SET ( FoundCacheFile "FoundCacheFile-NOTFOUND" )
    FIND_FILE ( FoundCacheFile ${ProjectName}Sources.cmake PATHS ${CMAKE_CURRENT_BINARY_DIR} NO_DEFAULT_PATH NO_CMAKE_ENVIRONMENT_PATH NO_CMAKE_PATH NO_SYSTEM_ENVIRONMENT_PATH NO_CMAKE_SYSTEM_PATH)
  FIND_FILE ( FoundCacheFile ${ProjectName}Sources.cmake PATHS ${CMAKE_CURRENT_BINARY_DIR} NO_DEFAULT_PATH NO_CMAKE_ENVIRONMENT_PATH NO_CMAKE_PATH NO_SYSTEM_ENVIRONMENT_PATH NO_CMAKE_SYSTEM_PATH)
    
 
    IF ( FoundCacheFile )
   IF ( FoundCacheFile )
      # read cached file list
    # read cached file list
      MESSAGE ( STATUS "    Found cache file ${FoundCacheFile}" )
    MESSAGE ( STATUS "    Found cache file ${FoundCacheFile}" )
      INCLUDE ( ${CMAKE_CURRENT_BINARY_DIR}/${ProjectName}Sources.cmake )
    INCLUDE ( ${CMAKE_CURRENT_BINARY_DIR}/${ProjectName}Sources.cmake )
    
 
    ELSE ( FoundCacheFile )
   ELSE ( FoundCacheFile )
      # get the file lists
    # get the file lists
      SET ( Dir ${CMAKE_HOME_DIRECTORY}/${ProjectDir} )
    SET ( Dir ${CMAKE_HOME_DIRECTORY}/${ProjectDir} )
 
 
      # add your other extensions here (this could be probably done nicer)
    # add your other extensions here (this could be probably done nicer)
      FILE ( GLOB_RECURSE DirSources ${Dir}/*.c ${Dir}/*.cpp ${Dir}/*.h ${Dir}/*.inl ${Dir}/*.rc ${Dir}/*.ico )
    FILE ( GLOB_RECURSE DirSources ${Dir}/*.c ${Dir}/*.cpp ${Dir}/*.h ${Dir}/*.inl ${Dir}/*.rc ${Dir}/*.ico )
 
 
      # write cached file list
    # write cached file list
      MESSAGE ( STATUS "    Generating ${ProjectName}Sources.cmake" )
    MESSAGE ( STATUS "    Generating ${ProjectName}Sources.cmake" )
      STRING ( CONFIGURE "@DirSources@" TempString @ONLY )
    STRING ( CONFIGURE "@DirSources@" TempString @ONLY )
      STRING ( REPLACE ";" " " "DirSourcesAsString" "${TempString}" )
    STRING ( REPLACE ";" " " "DirSourcesAsString" "${TempString}" )
      CONFIGURE_FILE ( ${CMAKE_HOME_DIRECTORY}/CMake/SourceFilesTemplate.cmake.in ${CMAKE_CURRENT_BINARY_DIR}/${ProjectName}Sources.cmake @ONLY )
    CONFIGURE_FILE ( ${CMAKE_HOME_DIRECTORY}/CMake/SourceFilesTemplate.cmake.in ${CMAKE_CURRENT_BINARY_DIR}/${ProjectName}Sources.cmake @ONLY )
    
 
    ENDIF ( FoundCacheFile )
   ENDIF ( FoundCacheFile )
    SET ( "${ProjectSources}" ${DirSources} )
  SET ( "${ProjectSources}" ${DirSources} )
  ENDMACRO ( GatherProjectFiles )
ENDMACRO ( GatherProjectFiles )
</pre>
</pre>


Line 41: Line 41:


<pre>
<pre>
  # Cache source files
# Cache source files
 
 
  # All sources
# All sources
  SET ( DirSources @DirSourcesAsString@ )
SET ( DirSources @DirSourcesAsString@ )
</pre>
</pre>
-----
-----
Line 51: Line 51:


<pre>
<pre>
  GatherProjectFiles ( MyLib src/MyLib MyLibSources )
GatherProjectFiles ( MyLib src/MyLib MyLibSources )
  ADD_LIBRARY (MyLib SHARED ${MyLibSources} )
ADD_LIBRARY (MyLib SHARED ${MyLibSources} )
</pre>
</pre>



Revision as of 21:04, 20 April 2018

Back

This macro recursively collects the files from a project directory and caches the list for future use. Tailor the extensions that are included in the list according to your needs.


# add extra sources to the defined project
MACRO ( GatherProjectFiles ProjectName ProjectDir ProjectSources )

  #look for the cache file
  SET ( FoundCacheFile "FoundCacheFile-NOTFOUND" )
  FIND_FILE ( FoundCacheFile ${ProjectName}Sources.cmake PATHS ${CMAKE_CURRENT_BINARY_DIR} NO_DEFAULT_PATH NO_CMAKE_ENVIRONMENT_PATH NO_CMAKE_PATH NO_SYSTEM_ENVIRONMENT_PATH NO_CMAKE_SYSTEM_PATH)

  IF ( FoundCacheFile )
    # read cached file list
    MESSAGE ( STATUS "    Found cache file ${FoundCacheFile}" )
    INCLUDE ( ${CMAKE_CURRENT_BINARY_DIR}/${ProjectName}Sources.cmake )

  ELSE ( FoundCacheFile )
    # get the file lists
    SET ( Dir ${CMAKE_HOME_DIRECTORY}/${ProjectDir} )

    # add your other extensions here (this could be probably done nicer)
    FILE ( GLOB_RECURSE DirSources ${Dir}/*.c ${Dir}/*.cpp ${Dir}/*.h ${Dir}/*.inl ${Dir}/*.rc ${Dir}/*.ico )

    # write cached file list
    MESSAGE ( STATUS "    Generating ${ProjectName}Sources.cmake" )
    STRING ( CONFIGURE "@DirSources@" TempString @ONLY )
    STRING ( REPLACE ";" " " "DirSourcesAsString" "${TempString}" )
    CONFIGURE_FILE ( ${CMAKE_HOME_DIRECTORY}/CMake/SourceFilesTemplate.cmake.in ${CMAKE_CURRENT_BINARY_DIR}/${ProjectName}Sources.cmake @ONLY )

  ENDIF ( FoundCacheFile )
  SET ( "${ProjectSources}" ${DirSources} )
ENDMACRO ( GatherProjectFiles )

The template file (located in CMAKE_HOME_DIRECTORY\CMake\SourceFilesTemplate.cmake.in) looks like this:

# Cache source files

# All sources
SET ( DirSources @DirSourcesAsString@ )

A typical use for this macro would be:

GatherProjectFiles ( MyLib src/MyLib MyLibSources )
ADD_LIBRARY (MyLib SHARED ${MyLibSources} )


Back



CMake: [Welcome | Site Map]