Directory¶
Operations to manipule directories. It requires CMake 4.0.1 or newer.
Synopsis¶
directory(SCAN <output-list-var> [...]) directory(SCAN_DIRS <output-list-var> [...]) directory(FIND_LIB <output-lib-var> [...]) directory(COLLECT_SOURCES_BY_LOCATION [...]) directory(COLLECT_SOURCES_BY_POLICY [...])
Usage¶
- directory(SCAN <output-list-var> [...])¶
Recursively get files and directories:
directory(SCAN <output-list-var> LIST_DIRECTORIES <true|false> RELATIVE <true|false> ROOT_DIR <dir-path> <INCLUDE_REGEX|EXCLUDE_REGEX> <regular-expression>)
Recursively scan and collect all files and directories under
ROOT_DIRthat match the filter provided with eitherINCLUDE_REGEXorEXCLUDE_REGEX, and store the result in<output-list-var>.Paths are returned as relative to
ROOT_DIRifRELATIVEistrue, or as absolute paths ifRELATIVEisfalse.If
LIST_DIRECTORIESistrue, directories are included in the result. IfLIST_DIRECTORIESisfalse, only files are listed.Example usage:
directory(SCAN files_found LIST_DIRECTORIES false RELATIVE true ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}" INCLUDE_REGEX ".*[.]cpp$|.*[.]cc$|.*[.]cxx$" ) # files_found is: # src/main.cpp;src/util.cpp;lib/module.cpp
- directory(SCAN_DIRS <output-list-var> [...])¶
Recursively get directories only:
directory(SCAN_DIRS <output-list-var> RECURSE <true|false> RELATIVE <true|false> ROOT_DIR <dir-path> <INCLUDE_REGEX|EXCLUDE_REGEX> <regular-expression>)
Scan and collect all directories under
ROOT_DIRthat match the regular expression provided with eitherINCLUDE_REGEXorEXCLUDE_REGEX, and store the result in<output-list-var>.If
RECURSEistrue, the function traverses subdirectories recursively. IfRECURSEisfalse, only the directories directly underROOT_DIRare considered.The paths in the result are returned relative to
ROOT_DIRifRELATIVEistrue, or as absolute paths ifRELATIVEisfalse.Example usage:
directory(SCAN_DIRS matched_dirs RECURSE true RELATIVE true ROOT_DIR "${CMAKE_SOURCE_DIR}" INCLUDE_REGEX "include" ) # matched_dirs is: # src/include;src/lib/include
- directory(FIND_LIB <output-lib-var> [...])¶
Recursively search for libs:
directory(FIND_LIB <output-lib-var> FIND_IMPLIB <output-implib-var> NAME <raw-filename> <STATIC|SHARED> RELATIVE <true|false> ROOT_DIR <dir-path>)
Search recursively in
ROOT_DIRfor a library and, on DLL platforms, its import library. The name to search for is given viaNAME, which should represent the base name of the library (without prefix or suffix).The matching uses system-defined prefixes and suffixes depending on the
STATIC(byCMAKE_STATIC_LIBRARY_PREFIXandCMAKE_STATIC_LIBRARY_SUFFIX) orSHARED(byCMAKE_SHARED_LIBRARY_PREFIXandCMAKE_SHARED_LIBRARY_SUFFIX) flag, as well asCMAKE_FIND_LIBRARY_PREFIXESandCMAKE_FIND_LIBRARY_SUFFIXESif defined. This makes the behavior similar tofind_library(), but more robust.Note
The different value of prefix and suffix that the variables used to search for libraries can take are:
# With GCC on Windows # CMAKE_SHARED_LIBRARY_PREFIX: lib # CMAKE_SHARED_LIBRARY_SUFFIX: .dll # CMAKE_STATIC_LIBRARY_PREFIX: lib # CMAKE_STATIC_LIBRARY_SUFFIX: .a # CMAKE_FIND_LIBRARY_PREFIXES: lib; # CMAKE_FIND_LIBRARY_SUFFIXES: .dll.a;.a;.lib # With MSVC on Windows # CMAKE_SHARED_LIBRARY_PREFIX: # CMAKE_SHARED_LIBRARY_SUFFIX: .dll # CMAKE_STATIC_LIBRARY_PREFIX: # CMAKE_STATIC_LIBRARY_SUFFIX: .lib # CMAKE_FIND_LIBRARY_PREFIXES: ;lib # CMAKE_FIND_LIBRARY_SUFFIXES: .dll.lib;.lib;.a
If a matching library is found, its path is stored in
<output-lib-var>. If a matching import library is found, its path is stored in<output-implib-var>. IfRELATIVEis set totrue, the results are relative toROOT_DIR. Otherwise, absolute paths are returned.If no match is found, the values will be
<output-lib-var>-NOTFOUNDand<output-implib-var>-NOTFOUND. InSTATICmode,<output-implib-var>-NOTFOUNDis always returned, because an import library is only for a shared library. If multiple matches are found, a fatal error is raised.This command is especially useful to locate dependency artifacts when configuring Imported Target manually (see also CMake's guide to binary import and export). The resulting paths are typically used to set properties like
IMPORTED_LOCATIONandIMPORTED_IMPLIBon an imported target, particularly in development or custom build setups where standardfind_library()behavior is not sufficient.Example usage:
directory(FIND_LIB mylib_path FIND_IMPLIB mylib_import NAME "zlib1" SHARED RELATIVE true ROOT_DIR "${CMAKE_SOURCE_DIR}/libs" ) # mylib_path is: # lib/zlib1.dll # mylib_import is: # lib/zlib1.lib
- directory(COLLECT_SOURCES_BY_LOCATION [...])¶
Recursively get source and header file in a dir:
directory(COLLECT_SOURCES_BY_LOCATION [SRC_DIR <dir-path> SRC_SOURCE_FILES <output-list-var> SRC_HEADER_FILES <output-list-var>] [INCLUDE_DIR <dir-path> INCLUDE_HEADER_FILES <output-list-var>])
Recursively collect source and header files from specified directories and store them in output variables.
If the
SRC_DIRoption is specified, the command searches the directory recursively for C++ source files with the following extensions:.cpp,.cc, or.cxx, and header files with the extensions.h,.hpp,.hxx,.inl, or.tpp.Source files found in
SRC_DIRare assigned to the variable specified bySRC_SOURCE_FILES, and header files are assigned to the variable specified bySRC_HEADER_FILES.If the
INCLUDE_DIRoption is specified, only header files are collected recursively using the same extensions as above. These are assigned to the variable specified byINCLUDE_HEADER_FILES.Both
SRC_DIRandINCLUDE_DIRmay be specified simultaneously. In that case, files are collected from both locations and stored into their respective output variables.At least one of
SRC_DIRorINCLUDE_DIRmust be provided. If neither is specified, or if any of the corresponding output variable arguments are missing, the command raises a fatal error.Example usage:
directory(COLLECT_SOURCES_BY_LOCATION SRC_DIR "${CMAKE_SOURCE_DIR}/src" SRC_SOURCE_FILES src_sources SRC_HEADER_FILES src_headers INCLUDE_DIR "${CMAKE_SOURCE_DIR}/include" INCLUDE_HEADER_FILES include_headers ) # Or directory(COLLECT_SOURCES_BY_LOCATION SRC_DIR "${CMAKE_SOURCE_DIR}/src" SRC_SOURCE_FILES src_sources SRC_HEADER_FILES src_headers ) # Or directory(INCLUDE_DIR "${CMAKE_SOURCE_DIR}/include" INCLUDE_HEADER_FILES include_headers )
- directory(COLLECT_SOURCES_BY_POLICY [...])¶
Recursively get source and header file according to a policy:
directory(COLLECT_SOURCES_BY_POLICY PUBLIC_HEADERS_SEPARATED <true|false> [<include-dir-path>] PRIVATE_SOURCE_DIR <dir-path> PRIVATE_SOURCE_FILES <output-list-var> PUBLIC_HEADER_DIR <output-var> PUBLIC_HEADER_FILES <output-list-var> PRIVATE_HEADER_DIR <output-var> PRIVATE_HEADER_FILES <output-list-var>)
Recursively collect source and header files from specified directories according to a header separation policy.
This command collects source files with extensions
.cpp,.cc, or.cxxfrom the directory specified byPRIVATE_SOURCE_DIRand stores them in the variable referenced byPRIVATE_SOURCE_FILES.The behavior for header files depends on the policy value given to
PUBLIC_HEADERS_SEPARATED:If
PUBLIC_HEADERS_SEPARATED true <include-dir-path>is specified:Public headers are collected from
<include-dir-path>and stored in the variable referenced byPUBLIC_HEADER_FILES.The variable referenced by
PUBLIC_HEADER_DIRis set to<include-dir-path>.Private headers are collected from
PRIVATE_SOURCE_DIRand stored in the variable referenced byPRIVATE_HEADER_FILES.The variable referenced by
PRIVATE_HEADER_DIRis set toPRIVATE_SOURCE_DIR.
If
PUBLIC_HEADERS_SEPARATED falseis specified:All headers are treated as public and collected from
PRIVATE_SOURCE_DIR.The variable referenced by
PUBLIC_HEADER_FILEScontains the header file list.The variable referenced by
PUBLIC_HEADER_DIRis set toPRIVATE_SOURCE_DIR.The variables referenced by
PRIVATE_HEADER_DIRandPRIVATE_HEADER_FILESare set to empty.
If the policy is set to
true, but<include-dir-path>is missing or does not refer to an existing directory, a fatal error is raised.Example usage:
directory(COLLECT_SOURCES_BY_POLICY PUBLIC_HEADERS_SEPARATED true "${CMAKE_SOURCE_DIR}/include/mylib" PRIVATE_SOURCE_DIR "${CMAKE_SOURCE_DIR}/src" PRIVATE_SOURCE_FILES private_sources PUBLIC_HEADER_DIR public_header_dir PUBLIC_HEADER_FILES public_headers PRIVATE_HEADER_DIR private_header_dir PRIVATE_HEADER_FILES private_headers )