common.cmake 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. cmake_minimum_required(VERSION 2.6)
  2. # create_source_group(relativeSourcePath sourceGroupName files)
  3. #
  4. # Creates a source group with the specified name relative to the relative path
  5. # specified.
  6. #
  7. # Parameters:
  8. # - sourceGroupName: Name of the source group to create.
  9. # - relativeSourcePath: Relative path to the files.
  10. # - sourceFiles: Files to add to the source group.
  11. #
  12. # For example if you have the following directory structure:
  13. #
  14. # - ExampleApplication
  15. # - include
  16. # - Main.h
  17. # - Window
  18. # Window.h
  19. # - source
  20. # - Main.cpp
  21. # - Window
  22. # Window.cpp
  23. #
  24. # You can get your list of files and call create_source_group the following way
  25. #
  26. # file(GLOB_RECURSE my_source_files ${CMAKE_CURRENT_SOURCE_DIR}/source/*)
  27. # create_source_group("Source Files" "${CMAKE_CURRENT_SOURCE_DIR}/source" ${my_source_files})
  28. # file(GLOB_RECURSE my_header_files ${CMAKE_CURRENT_SOURCE_DIR}/include/*)
  29. # create_source_group("Header Files" "${CMAKE_CURRENT_SOURCE_DIR}/include" ${my_header_files})
  30. # add_executable(ExampleApplication ${my_source_files} ${my_header_files})
  31. #
  32. # Then the generated solution would look like this
  33. #
  34. # - ExampleApplication (project)
  35. # - Header Files
  36. # - Main.h
  37. # - Window
  38. # Window.h
  39. # - Source Files
  40. # - Main.cpp
  41. # - Window
  42. # Window.cpp
  43. #
  44. function(create_source_group sourceGroupName relativeSourcePath)
  45. FOREACH(currentSourceFile ${ARGN})
  46. FILE(RELATIVE_PATH folder ${relativeSourcePath} ${currentSourceFile})
  47. get_filename_component(filename ${folder} NAME)
  48. string(REPLACE ${filename} "" folder ${folder})
  49. if(NOT folder STREQUAL "")
  50. string(REGEX REPLACE "/+$" "" folderlast ${folder})
  51. string(REPLACE "/" "\\" folderlast ${folderlast})
  52. SOURCE_GROUP("${sourceGroupName}\\${folderlast}" FILES ${currentSourceFile})
  53. endif(NOT folder STREQUAL "")
  54. ENDFOREACH(currentSourceFile ${ARGN})
  55. endfunction(create_source_group)