Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # sw_load_sources - Uses the glob approach to building source files
- # allows for platform specific folders to be automatically excluded/included
- # depending on the current platform.
- # For example, in the following project layout:
- #
- # MyProj\*.cpp
- # win\*.cpp
- # lin\*.cpp
- # mac\*.cpp
- #
- # Only the win, and MyProject paths will be scanned, excluding lin/mac folders
- # automatically.
- #
- # Usage:
- # sw_load_sources(VarName
- # NO_RECURSE
- # EXCLUDE "SomeValue"
- # LIST_ONLY_EXTS "json;pem"
- # *.hpp
- # *.cpp
- # *.c
- # *.json
- # *.pem
- # ...
- # )
- #
- # Options:
- # NO_RECURSE - If specified will prevent scanning recursively in paths.
- #
- # Arguments:
- # EXCLUDE - Defines a string to exclude, will be matched on with regex.
- # ARGN - One or more paths with wildcards, e.g. ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp
- # LIST_ONLY_EXTS - Defines one or more file extensions (separated by semicolon) which should not be compiled.
- # This is useful for displaying configuration files or
- # resources in the project envs.
- function(sw_load_sources output_var)
- set(options NO_RECURSE)
- set(oneValueArgs EXCLUDE LIST_ONLY_EXTS)
- cmake_parse_arguments(sw_load_sources
- "${options}" "${oneValueArgs}" "${multiArgs}" ${ARGN})
- foreach(pattern ${ARGN})
- file(GLOB_RECURSE _source_files [LIST_DIRECTORIES false] ${pattern})
- set(source_files ${source_files} ${_source_files})
- endforeach()
- if (NOT source_files)
- message(FATAL_ERROR "No sources loaded for filter: ${ARGN}")
- endif()
- # Exclude platform folders that do not match our own
- if (SW_PLAT_WIN)
- list(FILTER source_files EXCLUDE REGEX "\/unx|mac|lin\/")
- elseif (SW_PLAT_LIN)
- list(FILTER source_files EXCLUDE REGEX "\/mac|win|win32\/")
- elseif (SW_PLAT_MAC)
- list(FILTER source_files EXCLUDE REGEX "\/lin|win32|win\/")
- else()
- message(FATAL_ERROR "Unknown platform")
- endif()
- # Exclude, excludes
- if (sw_load_sources_EXCLUDE)
- list(FILTER _output_var EXCLUDE REGEX "${sw_load_sources_EXCLUDE}")
- endif()
- # Now strip out any list only extensions so we can group them differently from the rest
- if (sw_load_sources_LIST_ONLY_EXTS)
- # Convert it to a list
- separate_arguments(list_only_exts UNIX_COMMAND ${sw_load_sources_LIST_ONLY_EXTS})
- message("List only extensions: ${list_only_exts}")
- message("List only extensions: ${sw_load_sources_LIST_ONLY_EXTS}")
- foreach(source_file ${source_files})
- get_filename_component(source_file_ext ${source_file} EXT)
- if (source_file_ext)
- foreach(list_only_ext ${list_only_exts})
- message("Matching ${list_only_ext}")
- if (".${list_only_ext}" STREQUAL ${source_file_ext})
- set_source_files_properties(${source_file} PROPERTIES HEADER_FILE_ONLY TRUE)
- list(REMOVE_ITEM source_files ${source_file})
- set(list_only_files ${list_only_files} ${source_file})
- endif()
- endforeach()
- endif()
- endforeach()
- endif()
- # Sort the dependencies in visual studio
- if (SW_PLAT_WIN)
- source_group(TREE ${CMAKE_CURRENT_LIST_DIR} PREFIX "Sources" FILES ${source_files})
- source_group(TREE ${CMAKE_CURRENT_LIST_DIR} PREFIX "/" FILES ${list_only_files})
- endif()
- # Now set this in the callers scope and combine both list only and source files
- set(${output_var} ${source_files} ${list_only_files} PARENT_SCOPE)
- endfunction()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement