How to do it

Similar to the previous recipe, we need to configure a template file, but this time, we will emulate the configure_file() function with a Python script. We keep the CMakeLists.txt largely unchanged, but we replace configure_file(print_info.c.in print_info.c @ONLY) with a set of commands, which we will introduce step by step:

  1. First, we construct a variable, _config_script, which will hold a Python script that we will execute a moment later:
set(_config_script"from pathlib import Pathsource_dir = Path('${CMAKE_CURRENT_SOURCE_DIR}')binary_dir = Path('${CMAKE_CURRENT_BINARY_DIR}')input_file = source_dir / 'print_info.c.in'output_file = binary_dir / 'print_info.c'import syssys.path.insert(0, str(source_dir))from configurator import ...

Get CMake Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.