add_custom_command(TARGET): USES_TERMINAL does not use console pool with Ninja
Mostly copied and pasted from this stack overflow post
If add_custom_command
is called using the "build events" signature with USES_TERMINAL
in order to get a prompt for a user's password when running a command using sudo
, as seen in the following CMakeLists.txt snippet:
add_executable(my-app main.c)
add_custom_command(
TARGET my-app
POST_BUILD
COMMAND sudo setcap cap_net_admin+eip $<TARGET_FILE:my-app>
USES_TERMINAL
)
And then configure and build using Ninja:
$ cmake -G Ninja -B build
$ cmake --build build
The prompt for the user's password does not appear, and the build hangs. Looking at the resulting build.ninja
file, I would expect there to be a pool = console
setting for building my-app, but it's not present:
build my-app: C_EXECUTABLE_LINKER__my-app_ CMakeFiles/my-app.dir/main.c.o
OBJECT_DIR = CMakeFiles/my-app.dir
POST_BUILD = cd /home/bob/test_cmake_uses_terminal/build && sudo setcap cap_net_admin+eip /home/bob/test_cmake_uses_terminal/build/my-app
PRE_LINK = :
TARGET_COMPILE_PDB = CMakeFiles/my-app.dir/
TARGET_FILE = my-app
TARGET_PDB = my-app.pdb
If pool = console
is manually added to that list, it properly prompts for the user's password when runnning the command.
I was able to get this to work in a more kludgey way using add_custom_target
, but I'd like to know why USES_TERMINAL
is not working as I interpret the documentation.
Tested using cmake version 3.24.1, Ninja version 1.10.1, and running on Ubuntu 22.04.