BinTarget

Operations to fully create and configure a binary target. It requires CMake 3.20 or newer.

Synopsis

`reset_local_bin_target_settings`_()
`add_bin_target`_(TARGET_NAME <target_name> <STATIC|SHARED|HEADER|EXEC>)
`configure_bin_target_settings`_(TARGET_NAME <target_name> COMPILE_DEFINITIONS <definition_list> ...)
`collect_source_files_by_location`_([SRC_DIR <directory_path> SRC_SOURCE_FILES <output_list_var> SRC_HEADER_FILES <output_list_var>]|[INCLUDE_DIR <directory_path> INCLUDE_HEADER_FILES <output_list_var>])
`collect_source_files_by_policy`_(PUBLIC_HEADERS_SEPARATED <on|off> [<include_directory_path>] SRC_DIR <directory_path> SRC_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>)
`add_sources_to_target`_(TARGET_NAME <target_name> SOURCE_FILES <file_path_list> ... PRIVATE_HEADER_FILES <file_path_list> ... PUBLIC_HEADER_FILES <file_path_list> ... PROJECT_DIR <directory_path>)
`add_precompiled_header_to_target`_(TARGET_NAME <target_name> HEADER_FILE <file_path>)
`add_include_directories_to_target`_(TARGET_NAME <target_name> INCLUDE_DIRECTORIES <directory_path_list> ...)

Usage

reset_local_bin_target_settings()

Reset all settings of the binary target currently under construction.

add_bin_target(TARGET_NAME <target_name> <STATIC|SHARED|HEADER|EXEC>)

Add a binary target <target_name> to the project as type STATIC, SHARED, HEADER or EXEC.

configure_bin_target_settings(TARGET_NAME <target_name> COMPILE_DEFINITIONS [<definition_list> ...])

Configure the binary target <target_name>: add it in a folder for IDE project, set compile features (C++ standard), set compile definitions, set compile options, add link options

collect_source_files_by_location([SRC_DIR <directory_path> SRC_SOURCE_FILES <output_list_var> SRC_HEADER_FILES <output_list_var>]|[INCLUDE_DIR <directory_path> INCLUDE_HEADER_FILES <output_list_var>])

Generates lists of source files (e.g. ".cpp") and header files (e.g. ".h") found in the specified directories by SRC_DIR and INCLUDE_DIR, and store them into each <output_list_var> variable.

  • If SRC_DIR is provided, the function searches for source and header files in the given directory:
    • Source files are stored in SRC_SOURCE_FILES.

    • Header files are stored in SRC_HEADER_FILES.

If INCLUDE_DIR is provided, the function searches for header files in the specified directory:
  • Header files are stored in INCLUDE_HEADER_FILES.

Both SRC_DIR and INCLUDE_DIR may be used together. In that case, files are collected from both locations into their respective output variables.

At least one of SRC_DIR or INCLUDE_DIR must be specified. If neither is provided, or if their corresponding output variables are missing, an error is raised.

collect_source_files_by_policy(PUBLIC_HEADERS_SEPARATED <on|off> [<include_directory_path>] SRC_DIR <directory_path> SRC_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>)

Collects source (e.g., .cpp) and header (e.g. .h) files from specified directories, applying a policy (``PUBLIC_HEADERS_SEPARATED```value) that determines how public and private headers are handled.

This function generates a list of all source files (e.g.,``.cpp``) in the directory specified by SRC_DIR and stores the list into SRC_SOURCE_FILES. The source file collection is not affected by the PUBLIC_HEADERS_SEPARATED policy.

Header file handling depends on the value of PUBLIC_HEADERS_SEPARATED:

  • If PUBLIC_HEADERS_SEPARATED is set to on, public and private headers are considered to be in separate directories:
    • Public headers are expected to reside in <include_directory_path>, typically a subdirectory of include/. These files are stored in PUBLIC_HEADER_FILES, and PUBLIC_HEADER_DIR is set to <include_directory_path>.

    • Private headers are searched in SRC_DIR. These files are stored in PRIVATE_HEADER_FILES, and PRIVATE_HEADER_DIR is set to SRC_DIR.

  • If PUBLIC_HEADERS_SEPARATED is set to off, all headers are treated as public and are expected to reside in SRC_DIR:
    • The function searches SRC_DIR for header files, stores them in PUBLIC_HEADER_FILES, and sets PUBLIC_HEADER_DIR to SRC_DIR.

    • In this mode, <include_directory_path> is ignored, and PRIVATE_HEADER_FILES and PRIVATE_HEADER_DIR are set to empty.

An error is raised if PUBLIC_HEADERS_SEPARATED is on but <include_directory_path> is either empty or not exists.

add_sources_to_target(TARGET_NAME <target_name> SOURCE_FILES <file_path_list> ... PRIVATE_HEADER_FILES <file_path_list> ... PUBLIC_HEADER_FILES <file_path_list> ... PROJECT_DIR <directory_path>)

Assign sources of SOURCE_FILES, PRIVATE_HEADER_FILES and headers of PUBLIC_HEADER_FILES to the target <target_name>, and define a grouping for source files in IDE project generation in PROJECT_DIR.

add_precompiled_header_to_target(TARGET_NAME <target_name> HEADER_FILE <file_path>)

Add a precompiled header file <file_path> to the target <target_name>.

add_include_directories_to_target(TARGET_NAME <target_name> INCLUDE_DIRECTORIES <directory_path_list> ...)

Add include directories <directory_path_list> to the target <target_name>.