FileManip

Operations on files by extending the file() command. It requires CMake 4.0.1 or newer.

This module is dedicated to file and path manipulation requiring access to the filesystem. All files must exist on the disk. For other path manipulation, handling only syntactic aspects, see the PathManip module and the cmake_path() command.

Synopsis

file_manip(RELATIVE_PATH <file-list-var> BASE_DIR <dir-path> [OUTPUT_VARIABLE <output-list-var>])
file_manip(ABSOLUTE_PATH <file-list-var> BASE_DIR <dir-path> [OUTPUT_VARIABLE <output-list-var>])

Usage

file_manip(RELATIVE_PATH <file-list-var> BASE_DIR <dir-path> [OUTPUT_VARIABLE <output-list-var>])

Computes the relative path from a given BASE_DIR for each file and directory in the list variable named <file-list-var>. The files and the BASE_DIR must exist on the disk and must be passed as absolute path. The result is stored either in-place in <file-list-var>, or in the variable specified by <output-list-var>, if the OUTPUT_VARIABLE option is provided. The result is an empty list if the input list is empty.

Example usage:

set(files
  "${CMAKE_SOURCE_DIR}/src/main.cpp"
  "${CMAKE_SOURCE_DIR}/src")
file_manip(RELATIVE_PATH files BASE_DIR "${CMAKE_SOURCE_DIR}")
# files is:
#   src/main.cpp;src
file_manip(ABSOLUTE_PATH <file-list-var> BASE_DIR <dir-path> [OUTPUT_VARIABLE <output-list-var>])

Computes the absolute path from a given BASE_DIR for each file and directory in the list variable named <file-list-var>. Files and the BASE_DIR must exist on the disk. BASE_DIR must be passed as absolute path, while the files can be relative or absolute. The result is stored either in-place in <file-list-var>, or in the variable specified by <output-list-var>, if the OUTPUT_VARIABLE option is provided. The result is an empty list if the input list is empty.

Example usage:

set(files
  "src/main.cpp"
  "src"
  "${CMAKE_SOURCE_DIR}/src/main.cpp"
  "${CMAKE_SOURCE_DIR}/src")
file_manip(ABSOLUTE_PATH files BASE_DIR "${CMAKE_SOURCE_DIR}")
# files is:
#   /full/path/to/src/main.cpp;/full/path/to/src;/full/path/to/src/main.cpp;/full/path/to/src