Monday, May 9, 2022

[SOLVED] Delete install target in CMake

Issue

I have a CMake-managed project where is is never sensible to execute make install after generation of the Makefile. Moreover, if a user types make install and puts headers in the default CMAKE_INSTALL_PREFIX of /usr/local/include, then future builds can be contaminated.

I would like to instruct cmake to not generate an install target in the generated Makefile. Is this possible?

Note: A solution getting close to this is to mark_as_advanced(CMAKE_INSTALL_PREFIX), but this only hides the option from ccmake. Maybe set(CMAKE_INSTALL_PREFIX /dont/do/this)?


Solution

Set CMAKE_SKIP_INSTALL_RULES to ON at the start of your top-level CMakeLists.txt (with a comment explaining why). As it says in the docs:

If TRUE, CMake will neither generate installation rules nor will it generate cmake_install.cmake files. This variable is FALSE by default.

A quick interaction to prove this works:

$ cat CMakeLists.txt
cmake_minimum_required(VERSION 3.23)
project(test)

set(CMAKE_SKIP_INSTALL_RULES YES)

add_executable(app main.cpp)

$ cmake -S . -B build
-- The C compiler identification is GNU 9.3.0
-- The CXX compiler identification is GNU 9.3.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /path/to/build
$ cmake --build build/ --target install
ninja: error: unknown target 'install'

Attempting to call install() will issue the following warning:

CMake Warning in CMakeLists.txt:
  CMAKE_SKIP_INSTALL_RULES was enabled even though installation rules have
  been specified


Answered By - Alex Reinking
Answer Checked By - David Marino (WPSolving Volunteer)