Difference between revisions of "CMakeMacroGenerateProject"

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


<pre>
<pre>
  # create hierarchical source groups
# create hierarchical source groups
  MACRO ( GenerateProject ProjectDir ProjectSources )
MACRO ( GenerateProject ProjectDir ProjectSources )
 
    SET ( DirSources "${ProjectSources}" )
  SET ( DirSources "${ProjectSources}" )
    FOREACH ( Source ${DirSources} )
  FOREACH ( Source ${DirSources} )
      STRING ( REGEX REPLACE "${ProjectDir}" "" RelativePath "${Source}" )
    STRING ( REGEX REPLACE "${ProjectDir}" "" RelativePath "${Source}" )
      STRING ( REGEX REPLACE "[\\\\/][^\\\\/]*$" "" RelativePath "${RelativePath}" )
    STRING ( REGEX REPLACE "[\\\\/][^\\\\/]*$" "" RelativePath "${RelativePath}" )
      STRING ( REGEX REPLACE "^[\\\\/]" "" RelativePath "${RelativePath}" )
    STRING ( REGEX REPLACE "^[\\\\/]" "" RelativePath "${RelativePath}" )
      STRING ( REGEX REPLACE "/" "\\\\\\\\" RelativePath "${RelativePath}" )
    STRING ( REGEX REPLACE "/" "\\\\\\\\" RelativePath "${RelativePath}" )
      SOURCE_GROUP ( "${RelativePath}" FILES ${Source} )
    SOURCE_GROUP ( "${RelativePath}" FILES ${Source} )
    ENDFOREACH ( Source )
  ENDFOREACH ( Source )
  ENDMACRO ( GenerateProject)
ENDMACRO ( GenerateProject)
</pre>
</pre>


Line 26: Line 26:


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



Revision as of 16:04, 20 April 2018

Back

This macro takes a list of files as input and generates source groups that follow the relative path of the files from the ProjectDir folder. This is strictly a cosmetic change, but it's nice if you're using the CMake generated projects also for development


# create hierarchical source groups
MACRO ( GenerateProject ProjectDir ProjectSources )
 
  SET ( DirSources "${ProjectSources}" )
  FOREACH ( Source ${DirSources} )
    STRING ( REGEX REPLACE "${ProjectDir}" "" RelativePath "${Source}" )
    STRING ( REGEX REPLACE "[\\\\/][^\\\\/]*$" "" RelativePath "${RelativePath}" )
    STRING ( REGEX REPLACE "^[\\\\/]" "" RelativePath "${RelativePath}" )
    STRING ( REGEX REPLACE "/" "\\\\\\\\" RelativePath "${RelativePath}" )
    SOURCE_GROUP ( "${RelativePath}" FILES ${Source} )
  ENDFOREACH ( Source )
ENDMACRO ( GenerateProject)


A typical use for this macro would be:

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


Back



CMake: [Welcome | Site Map]