4

In CMakeLists.txt, is there a way to know whether the file was executed from a terminal or via a gui (of some form, e.g. ccmake or cmake-gui)? CMAKE_COMMAND and CMAKE_EDIT_COMMAND are both populated regardless of whether I am running cmake or ccmake.

I was searching through the variables and these two seemed the most promising. I have become enamored with colorizing cmake output, the many wonderful answers there have their drawbacks. Namely, I can either get:

  1. It works as expected in a terminal, but there are escape sequences in ccmake.
  2. It works as expected in a terminal, but the ccmake terminal is overwritten by colorized output (LOL).

Just curious if there is a way to determine this in the CMakeLists.txt. There were variables for script mode (-P) that seemed potentially helpful, but I think I would first need to know which executable (cmake or ccmake) called my CMakeLists.txt in the first place.

svenevs
  • 833
  • 9
  • 24
  • As far as I know, there is no way to distinguish cmake from cmake-gui within CMake. What's your usecase? It should behave the samea anyhow, everything else would be a surprise. – usr1234567 Jun 15 '17 at 20:36
  • Yeah I don't think it is possible to distinguish either. The use case is quite minute, and I honestly can't think of another reason why anybody would even want this. My library is able to compile its external dependencies for the user, but **not** able to perform final steps of installation (adding files to locations that need elevated permissions). I added colorized output to draw attention to this (because my application cannot run without the user performing the extra steps). But the GUIs don't like my colorized output x0 – svenevs Jun 18 '17 at 05:00
  • Your terminal should handle the colorized output. Probably you are adding it directly to the output, which is the real cause for your problem. – usr1234567 Jun 18 '17 at 17:30
  • 1
    Yes this is exactly the problem. I suppose I'll just stick with the warning variant of message, that's what it's there for after all – svenevs Jun 18 '17 at 18:12

1 Answers1

0

As far as I know, there is no way to distinguish whether the code was invoked from CMake or from CMake-gui. The code should behave in the same way, everything else would be a surprise for the user.

You could try to detect whether an instance of CMake-gui is running, but this is error-prone and is not justified for real use cases.

usr1234567
  • 21,601
  • 16
  • 108
  • 128
  • Too bad. I just might have to look into the source code and submit a patch assuming the solution would be unintrusive. I've seen in Python the ability to detect whether output is going to a terminal or pipe to a file, and if file they removed the coloring. Regardless, thanks for weighing in :) – svenevs Jun 18 '17 at 18:10
  • @svenevs, I came here because I stumbled in exatly the same issue as you: the colorized output does not work when running `ccmake`. Did you find a solution for that? Or, alternatively, did you find how to make `ccmake` interpret the colorized sequences correctly? Thanks a lot for your kind help in advance!! (BTW: +1 for the question!) – rmbianchi Feb 24 '21 at 12:28
  • Is CMAKE_COLOR_MAKEFILE of any help? See https://cmake.org/cmake/help/latest/variable/CMAKE_COLOR_MAKEFILE.html – usr1234567 Feb 25 '21 at 07:25