Executing shell scripts at configure time

Finally, we generate osdef.h using the following function:

function(generate_osdef_h)  find_program(BASH_EXECUTABLE bash)  execute_process(    COMMAND      ${BASH_EXECUTABLE} osdef.sh ${CMAKE_CURRENT_BINARY_DIR}    WORKING_DIRECTORY      ${CMAKE_CURRENT_LIST_DIR}    )endfunction()

In order to generate osdef.h in ${CMAKE_CURRENT_BINARY_DIR}/src/auto instead of src/auto, we had to adapt osdef.sh to accept ${CMAKE_CURRENT_BINARY_DIR} as a command line argument.

Inside osdef.sh, we check whether this argument is given:

if [ $# -eq 0 ]  then    # there are no arguments    # assume the target directory is current directory    target_directory=$PWD  else    # target directory is provided as argument    target_directory=$1fi

And then, we generate ...

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.