ruipacheco
ruipacheco

Reputation: 16502

CMake cannot find ninja when run in QtCreator

I have a call to find_program in my CMakeLists.txt file to find the path to Ninja. This returns the right value when I run ninja via the command line but fails when I run it within QtCreator:

find_program(
    CMAKE_MAKE_PROGRAM
    NAME ninja
    PATHS /opt/local/bin
  )
  message(${CMAKE_MAKE_PROGRAM})

In ninja this returns:

/opt/local/bin/ninja

In QtCreator this returns:

/usr/bin/make

Why would CMake not find something that is present in the $PATH?

Upvotes: 0

Views: 3455

Answers (2)

Kane
Kane

Reputation: 6300

From the documentation of find_program():

A cache entry named by VAR is created to store the result of this command. If the program is found the result is stored in the variable and the search will not be repeated unless the variable is cleared.

In your case CMAKE_MAKE_PROGRAM happens to be cached as /usr/bin/make (probably, it was at some point set by QtCreator), so find_program() does nothing.

A proper way to switch between make and ninja would be to use CMake generators.

Upvotes: 1

Tsyvarev
Tsyvarev

Reputation: 66338

The variable CMAKE_MAKE_PROGRAM is cached by the CMake generator. find_program does not update a cached variable unless it contains *-NOTFOUND.

You need to use other variable in find_program call, and then update CMAKE_MAKE_PROGRAM variable with set(CACHE ... FORCE):

set(CMAKE_MAKE_PROGRAM <new-value> CACHE FILEPATH "" FORCE)

Note, that switching CMAKE_MAKE_PROGRAM from make to ninja is not a right way for change CMake generator. You need to pass a proper CMake generator via -G option to cmake itself.

Upvotes: 0

Related Questions