Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
CMake 3.30 KB | None | 0 0
  1. # sw_load_sources - Uses the glob approach to building source files
  2. # allows for platform specific folders to be automatically excluded/included
  3. # depending on the current platform.
  4. # For example, in the following project layout:
  5. #
  6. #   MyProj\*.cpp
  7. #       win\*.cpp
  8. #       lin\*.cpp
  9. #       mac\*.cpp
  10. #
  11. # Only the win, and MyProject paths will be scanned, excluding lin/mac folders
  12. # automatically.
  13. #
  14. # Usage:
  15. #   sw_load_sources(VarName
  16. #       NO_RECURSE
  17. #       EXCLUDE "SomeValue"
  18. #       LIST_ONLY_EXTS "json;pem"
  19. #       *.hpp
  20. #       *.cpp
  21. #       *.c
  22. #       *.json
  23. #       *.pem
  24. #       ...
  25. #   )
  26. #
  27. # Options:
  28. #   NO_RECURSE - If specified will prevent scanning recursively in paths.
  29. #
  30. # Arguments:
  31. #   EXCLUDE        - Defines a string to exclude, will be matched on with regex.
  32. #   ARGN           - One or more paths with wildcards, e.g. ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp
  33. #   LIST_ONLY_EXTS - Defines one or more file extensions (separated by semicolon) which should not be compiled.
  34. #                    This is useful for displaying configuration files or
  35. #                    resources in the project envs.
  36. function(sw_load_sources output_var)
  37.     set(options NO_RECURSE)
  38.     set(oneValueArgs EXCLUDE LIST_ONLY_EXTS)
  39.  
  40.     cmake_parse_arguments(sw_load_sources
  41.         "${options}" "${oneValueArgs}" "${multiArgs}" ${ARGN})
  42.  
  43.     foreach(pattern ${ARGN})
  44.         file(GLOB_RECURSE _source_files [LIST_DIRECTORIES false] ${pattern})
  45.         set(source_files ${source_files} ${_source_files})
  46.     endforeach()
  47.  
  48.     if (NOT source_files)
  49.         message(FATAL_ERROR "No sources loaded for filter: ${ARGN}")
  50.     endif()
  51.  
  52.     # Exclude platform folders that do not match our own
  53.     if (SW_PLAT_WIN)
  54.         list(FILTER source_files EXCLUDE REGEX "\/unx|mac|lin\/")
  55.     elseif (SW_PLAT_LIN)
  56.         list(FILTER source_files EXCLUDE REGEX "\/mac|win|win32\/")
  57.     elseif (SW_PLAT_MAC)
  58.         list(FILTER source_files EXCLUDE REGEX "\/lin|win32|win\/")
  59.     else()
  60.         message(FATAL_ERROR "Unknown platform")
  61.     endif()
  62.  
  63.     # Exclude, excludes
  64.     if (sw_load_sources_EXCLUDE)
  65.         list(FILTER _output_var EXCLUDE REGEX "${sw_load_sources_EXCLUDE}")
  66.     endif()
  67.  
  68.     # Now strip out any list only extensions so we can group them differently from the rest
  69.     if (sw_load_sources_LIST_ONLY_EXTS)
  70.         # Convert it to a list
  71.         separate_arguments(list_only_exts UNIX_COMMAND ${sw_load_sources_LIST_ONLY_EXTS})
  72.         message("List only extensions: ${list_only_exts}")
  73.         message("List only extensions: ${sw_load_sources_LIST_ONLY_EXTS}")
  74.         foreach(source_file ${source_files})
  75.             get_filename_component(source_file_ext ${source_file} EXT)
  76.  
  77.             if (source_file_ext)
  78.                 foreach(list_only_ext ${list_only_exts})
  79.                     message("Matching ${list_only_ext}")
  80.                     if (".${list_only_ext}" STREQUAL ${source_file_ext})
  81.                         set_source_files_properties(${source_file} PROPERTIES HEADER_FILE_ONLY TRUE)
  82.                         list(REMOVE_ITEM source_files ${source_file})
  83.                         set(list_only_files ${list_only_files} ${source_file})
  84.                     endif()
  85.                 endforeach()
  86.             endif()
  87.         endforeach()
  88.     endif()
  89.  
  90.     # Sort the dependencies in visual studio
  91.     if (SW_PLAT_WIN)
  92.         source_group(TREE ${CMAKE_CURRENT_LIST_DIR} PREFIX "Sources" FILES ${source_files})
  93.         source_group(TREE ${CMAKE_CURRENT_LIST_DIR} PREFIX "/" FILES ${list_only_files})
  94.     endif()
  95.  
  96.     # Now set this in the callers scope and combine both list only and source files
  97.     set(${output_var} ${source_files} ${list_only_files} PARENT_SCOPE)
  98.  
  99. endfunction()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement