Skip to content

CMake utility functions to improve file globbing, file regexing and manage transitive object link dependencies

License

Notifications You must be signed in to change notification settings

tschuchortdev/cmake_utils

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 

Repository files navigation

CMake Utils

License: MIT contributions welcome

miscellaneous cmake utility functions for file gathering and object library dependency management

fileRegex

Find files that match a regex pattern and add them to a list. Replacement for CMake's file function.

Options:

  • RELATIVE_PATHS: output relative paths into the list
  • FOLLOW_SYMLINKS: follow symlinks when traversing subdirectories
  • RECURSIVE: traverse subdirectories

Arguments:

  • INCLUDE: list of file regex patterns that should be included
  • EXCLUDE: list of file regex patterns that should be excluded

Example:

fileRegex(
        outputList 
        RECURSIVE
        FOLLOW_SYMLINKS
        RELATIVE_PATHS
        INCLUDE ".*" 
        EXCLUDE "^.*icon.*\\.png$" "^.*image.*\\.img$")

fileGlob

Find files that match a globbing pattern and add them to a list. Replacement for CMake's file function.

Options:

  • RELATIVE_PATHS: output relative paths into the list
  • FOLLOW_SYMLINKS: follow symlinks when traversing subdirectories
  • RECURSIVE: traverse subdirectories

Arguments:

  • INCLUDE: list of file glob patterns that should be included
  • EXCLUDE: list of file glob patterns that should be excluded

Example:

fileGlob(
        sources 
        RECURSIVE 
        INCLUDE "*.c*" "*.h*" 
        EXCLUDE "test/*" "temp/*")

target_link_dependencies

target_link_dependencies works similar to target_link_libraries except that you can link OBJECT libraries directly just as if they were regular libraries. This is very helpful because transitive dependencies, even on third OBJECT libraries, are propagated automatically.

Arguments:

  • PRIVATE: list of private dependencied
  • PUBLIC: list of public dependencies
  • INTERFACE: list of interface dependencies

dependencies specified without a scope are PRIVATE by default.

Note that PRIVATE and INTERFACE behave differently for OBJECT libraries than they do for regular libraries. Since OBJECT libraries are more like a collection of source files, their dependencies are only linked together when the OBJECT itself is first linked to a regular library, so PRIVATE dependencies will in fact be available to the target, while INTERFACE dependencies will also be available to the OBJECT.

Example:

add_library(my_transitive_obj OBJECT "2/test.cpp")
target_include_directories(my_transitive_obj "1/")

add_library(my_regular_lib "3/test.cpp")

add_library(my_obj OBJECT "3/test.cpp")
target_include_directories(my_obj "3/")
target_link_dependencies(my_obj PUBLIC my_transitive_obj my_regular_lib)

add_executable(my_exe "")
target_link_dependencies(my_exe PRIVATE my_obj)

append_target_property

append_target_property works similar to set_target_properties except it appends instead of overwrites and can only set one property at a time.

Arguments:

  • target name
  • property name
  • value

Example:

append_target_property(
        my_target 
        MY_PROPERTY_NAME 
        "this will be appended to MY_PROPERTY_NAME")

safe_get_target_property

safe_get_target_property works similar to get_target_property except it returns an empty string when the property is not found.

Arguments:

  • name of the variable that will contain the result
  • target name
  • property name

Example:

safe_get_target_property(
        my_target_dependencies
        my_target
        DEPENDENCIES)
        
some_other_function("${other_target_dependencies};${my_target_dependencies}")      

About

CMake utility functions to improve file globbing, file regexing and manage transitive object link dependencies

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages