FileManip

Operations on files. It requires CMake 3.20 or newer.

Synopsis

file_manip(RELATIVE_PATH <file_list_var> BASE_DIR <directory_path> [OUTPUT_VARIABLE <output_list_var>])
file_manip(ABSOLUTE_PATH <file_list_var> BASE_DIR <directory_path> [OUTPUT_VARIABLE <output_list_var>])
file_manip(STRIP_PATH <file_list_var> BASE_DIR <directory_path> [OUTPUT_VARIABLE <output_list_var>])
file_manip(GET_COMPONENT <file_list> ... MODE <mode> OUTPUT_VARIABLE <output_list_var>)

Usage

file_manip(RELATIVE_PATH <file_list_var> BASE_DIR <directory_path> [OUTPUT_VARIABLE <output_list_var>])

Computes the relative path from a given BASE_DIR for each file in the list variable named <file_list_var>. 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.

Example usage:

set(files
  "/project/src/main.cpp"
  "/project/include/lib.h"
)
file_manip(RELATIVE_PATH "${files}" BASE_DIR "/project")
# output is:
#   src/main.cpp;include/lib.hpp
file_manip(ABSOLUTE_PATH <file_list_var> BASE_DIR <directory_path> [OUTPUT_VARIABLE <output_list_var>])

Computes the absolute path from a given BASE_DIR for each file in the list variable named <file_list_var>. 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.

Example usage:

set(files
  "src/main.cpp"
  "include/lib.hpp"
)
file_manip(ABSOLUTE_PATH "${files}" BASE_DIR "/project")
# output is:
#   /project/src/main.cpp;/project/include/lib.h
file_manip(STRIP_PATH <file_list_var> BASE_DIR <directory_path> [OUTPUT_VARIABLE <output_list_var>])

Removes the BASE_DIR prefix from each file path in <file_list_var>. 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. Unlike to file_manip(RELATIVE_PATH), it performs no checks on the existence of files. Paths are processed like any string.

Example usage:

set(files
  "/project/src/main.cpp"
  "/project/include/lib.h"
)
file_manip(STRIP_PATH "${files}" BASE_DIR "/project")
# output is:
#   src/main.cpp;include/lib.hpp
file_manip(GET_COMPONENT <file_list> ... MODE <mode> OUTPUT_VARIABLE <output_list_var>)

Extracts a specific component from each path in the given <file_list> and stores the result in the variable specified by OUTPUT_VARIABLE option.

The MODE argument determines which component to extract and must be one of:

  • DIRECTORY - Directory without file name.

  • NAME - File name without directory.

Example usage:

set(files
  "/project/src/main.cpp"
  "/project/include/lib.hpp"
)
file_manip(GET_COMPONENT "${files}" MODE DIRECTORY OUTPUT_VARIABLE dirs)
# output is:
#   /project/src;/project/include