CMakeMacroGatherProjectFiles: Difference between revisions

From KitwarePublic
Jump to navigationJump to search
(Add explicit preformat markup)
(Replace content with link to new CMake community wiki)
(One intermediate revision by one other user not shown)
Line 1: Line 1:

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.
This page has moved [ here].
  # add extra sources to the defined project
  MACRO ( GatherProjectFiles ProjectName ProjectDir ProjectSources )
    #look for the cache file
    SET ( FoundCacheFile "FoundCacheFile-NOTFOUND" )
    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}" )
    ENDIF ( FoundCacheFile )
    SET ( "${ProjectSources}" ${DirSources} )
  ENDMACRO ( GatherProjectFiles )
The template file (located in ''CMAKE_HOME_DIRECTORY\CMake\'') 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} )

Latest revision as of 15:41, 30 April 2018

The CMake community Wiki has moved to the Kitware GitLab Instance.

This page has moved here.