How to modify environment variables passed to custom CMake target?

A portable way of setting environment variables for a custom target is to use CMake’s command-line tool mode command env:

env [--unset=NAME]... [NAME=VALUE]... COMMAND [ARG]...

Run command in a modified environment.

E.g.:

add_custom_target(newtarget ${CMAKE_COMMAND} -E env NAME=VALUE somecommand)

Also see Command Line Tool Mode.

Leave a Comment