# CMAKE\_STAGING\_PREFIX not being used by find\_\*

**URL:** https://discourse.cmake.org/t/cmake-staging-prefix-not-being-used-by-find/1746
**Category:** Usage
**Tags:** os:linux
**Created:** [August 25, 2020, 1:50pm UTC](https://discourse.cmake.org/t/cmake-staging-prefix-not-being-used-by-find/1746 "2020-08-25T13:50:29Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Daniel\_Stonier](https://discourse.cmake.org/user_avatar/discourse.cmake.org/daniel_stonier/32/802_2.png) [@Daniel\_Stonier](https://discourse.cmake.org/u/Daniel_Stonier)
#### Post date: [August 25, 2020, 1:50pm UTC](https://discourse.cmake.org/t/cmake-staging-prefix-not-being-used-by-find/1746/1 "2020-08-25T13:50:30Z")

</div>

[CMAKE\_STAGING\_PREFIX](https://cmake.org/cmake/help/v3.18/variable/CMAKE_STAGING_PREFIX.html#variable:CMAKE_STAGING_PREFIX) would indicate that it’s used as one of the search paths for the `find_***` methods. However, I’m not finding that in practice - wondering if I’m missing some other configuration.

Toolchain (for ubuntu’s `arm-linux-gnueabihf`):

```auto
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR "arm")
set(CMAKE_C_COMPILER arm-linux-gnueabihf-gcc)
set(CMAKE_CXX_COMPILER arm-linux-gnueabihf-g++)
list(APPEND CMAKE_FIND_ROOT_PATH "/usr/arm-linux-gnueabihf")
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) 
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)

```

Args passed to cmake:

```auto
-DCMAKE_TOOLCHAIN_FILE=${PWD}/arm-linux-gnueabihf.cmake -DCMAKE_STAGING_PREFIX=${PWD}/install

```

Debugging code in CMakeLists.txt:

```auto
message(WARNING "CMAKE_FIND_ROOT_PATH.......${CMAKE_FIND_ROOT_PATH}")
message(WARNING "CMAKE_STAGING_PREFIX..........${CMAKE_STAGING_PREFIX}")
message(WARNING "CMAKE_PREFIX_PATH..........${CMAKE_PREFIX_PATH}")
message(WARNING "CMAKE_SYSTEM_PREFIX_PATH...${CMAKE_SYSTEM_PREFIX_PATH}")
set(CMAKE_FIND_DEBUG_MODE TRUE)
find_package(ament_cmake_core REQUIRED)
message(FATAL_ERROR "BOOM")

```

Output:

```auto
CMake Warning at CMakeLists.txt:5 (message):
  CMAKE_FIND_ROOT_PATH......./usr/arm-linux-gnueabihf;/usr/arm-linux-gnueabihf

CMake Warning at CMakeLists.txt:6 (message):
  CMAKE_STAGING_PREFIX........../mnt/mervin/workspaces/foxy/skobuki/install

CMake Warning at CMakeLists.txt:7 (message):
  CMAKE_PREFIX_PATH..........

CMake Warning at CMakeLists.txt:9 (message):
  CMAKE_SYSTEM_PREFIX_PATH.../usr/local;/usr;/;/usr;/usr/X11R6;/usr/pkg;/opt

Checking prefix [/usr/arm-linux-gnueabihf/mnt/mervin/workspaces/foxy/skobuki/install/]
Checking prefix [/usr/arm-linux-gnueabihf/mnt/mervin/workspaces/foxy/skobuki/.venv/]
Checking prefix [/usr/arm-linux-gnueabihf/usr/local/]
Checking prefix [/usr/arm-linux-gnueabihf/usr/]
Checking prefix [/usr/arm-linux-gnueabihf/]
Checking file [/usr/arm-linux-gnueabihf/ament_cmake_coreConfig.cmake]
Checking file [/usr/arm-linux-gnueabihf/ament_cmake_core-config.cmake]
Checking prefix [/usr/arm-linux-gnueabihf/usr/games/]
Checking prefix [/usr/arm-linux-gnueabihf/usr/local/games/]
Checking prefix [/usr/arm-linux-gnueabihf/snap/]
Checking prefix [/usr/arm-linux-gnueabihf/usr/arm-linux-gnueabihf/]
Checking prefix [/usr/arm-linux-gnueabihf/usr/X11R6/]
Checking prefix [/usr/arm-linux-gnueabihf/usr/pkg/]
Checking prefix [/usr/arm-linux-gnueabihf/opt/]
Checking prefix [/usr/arm-linux-gnueabihf/mnt/mervin/workspaces/foxy/skobuki/install/]
Checking prefix [/usr/arm-linux-gnueabihf/mnt/mervin/workspaces/foxy/skobuki/.venv/]
Checking prefix [/usr/arm-linux-gnueabihf/usr/local/]
Checking prefix [/usr/arm-linux-gnueabihf/usr/]
Checking prefix [/usr/arm-linux-gnueabihf/]
Checking file [/usr/arm-linux-gnueabihf/ament_cmake_coreConfig.cmake]
Checking file [/usr/arm-linux-gnueabihf/ament_cmake_core-config.cmake]
Checking prefix [/usr/arm-linux-gnueabihf/usr/games/]
Checking prefix [/usr/arm-linux-gnueabihf/usr/local/games/]
Checking prefix [/usr/arm-linux-gnueabihf/snap/]
Checking prefix [/usr/arm-linux-gnueabihf/usr/arm-linux-gnueabihf/]
Checking prefix [/usr/arm-linux-gnueabihf/usr/X11R6/]
Checking prefix [/usr/arm-linux-gnueabihf/usr/pkg/]
Checking prefix [/usr/arm-linux-gnueabihf/opt/]
CMake Error at CMakeLists.txt:11 (find_package):
  By not providing "Findament_cmake_core.cmake" in CMAKE_MODULE_PATH this
  project has asked CMake to find a package configuration file provided by
  "ament_cmake_core", but CMake did not find one.

```

Importantly, no `CMAKE_STAGING_PREFIX` being used in the search…

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [August 25, 2020, 9:29pm UTC](https://discourse.cmake.org/t/cmake-staging-prefix-not-being-used-by-find/1746/2 "2020-08-25T21:29:45Z")

</div>

Ordinarily, `find_package()` would search all relevant locations with `/` as the root and again with `CMAKE_FIND_ROOT_PATH` as the root. Your toolchain file sets `CMAKE_FIND_ROOT_PATH_MODE_PACKAGE` to `ONLY`, which changes this to skip searching `/` as a base and it only uses `CMAKE_FIND_ROOT_PATH` as the base. You can see that in the very first search location, which is essentially `${CMAKE_FIND_ROOT_PATH}/${CMAKE_STAGING_PREFIX}`

---

<div class="post-metadata">

### Author: ![Daniel\_Stonier](https://discourse.cmake.org/user_avatar/discourse.cmake.org/daniel_stonier/32/802_2.png) [@Daniel\_Stonier](https://discourse.cmake.org/u/Daniel_Stonier)
#### Post date: [August 28, 2020, 2:03am UTC](https://discourse.cmake.org/t/cmake-staging-prefix-not-being-used-by-find/1746/3 "2020-08-28T02:03:20Z")

</div>

Oh, indeed. Just confirmed this before skipping over here and reading the answer. Nonetheless, still baffled.

From the documentation for [find\_package](https://cmake.org/cmake/help/latest/command/find_package.html):

> The CMake variable CMAKE\_FIND\_ROOT\_PATH specifies one or more directories to be prepended to all other search directories. This effectively “re-roots” the entire search under given locations. Paths which are descendants of the CMAKE\_STAGING\_PREFIX are excluded from this re-rooting, because that variable is always a path on the host system. By default the CMAKE\_FIND\_ROOT\_PATH is empty.

and

> ONLY\_CMAKE\_FIND\_ROOT\_PATH Search only the re-rooted directories and directories below `CMAKE\_STAGING\_PREFIX.

also, from the documentation of [CMAKE\_STAGING\_PREFIX](https://cmake.org/cmake/help/latest/variable/CMAKE_STAGING_PREFIX.html?highlight=cmake_staging_prefix):

```auto
The CMAKE_STAGING_PREFIX location is also used as a search prefix by the find_* commands. 

```

This would seem to indicate that directories related to the staging prefix _not_ be re-rooted and that it also acts as an alternative root to `CMAKE_FIND_ROOT_PATH`. Admittedly though, I’m finding the preceding paragraphs not easy to follow.

Essentially, I figured there must be some means of co-ordinating the search across a set of directories that included a) installed toolchain, b) sysroot, c) staging area whilst excluding the root filesystem. e.g.

```auto
/ <- not searchable
/usr/arm-linux-gnueabihf/ <- searchable
/opt/staging <- searchable (and this is where to be compiled code will install)
/opt/sysroot <- searchable

```

That’s something I’ve done in the past and can force by setting `CMAKE_FIND_ROOT_PATH` capture both `/usr/arm-linux-gnueabihf/` and `/opt/staging`, but I would have thought `CMAKE_STAGING_PREFIX` would have been useful here so I could decouple workspace specifics from toolchain specifics.

Is there an example of how `CMAKE_STAGING_PREFIX` is intended to be used? There’s not much available, even on a google search.

---

<div class="post-metadata">

### Author: ![Daniel\_Stonier](https://discourse.cmake.org/user_avatar/discourse.cmake.org/daniel_stonier/32/802_2.png) [@Daniel\_Stonier](https://discourse.cmake.org/u/Daniel_Stonier)
#### Post date: [August 28, 2020, 2:34am UTC](https://discourse.cmake.org/t/cmake-staging-prefix-not-being-used-by-find/1746/4 "2020-08-28T02:34:22Z")

</div>

Aha, an example [here](https://cmake.org/cmake/help/v3.0/manual/cmake-toolchains.7.html#cross-compiling).

```auto
set(CMAKE_SYSTEM_NAME Linux)

set(CMAKE_SYSROOT /home/devel/rasp-pi-rootfs)
set(CMAKE_STAGING_PREFIX /home/devel/stage)

set(CMAKE_C_COMPILER /home/devel/gcc-4.7-linaro-rpi-gnueabihf/bin/arm-linux-gnueabihf-gcc)
set(CMAKE_CXX_COMPILER /home/devel/gcc-4.7-linaro-rpi-gnueabihf/bin/arm-linux-gnueabihf-g++)

set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)

```

This however, would fail to discover cmake modules in `/home/devel/stage` when you have multiple, dependent projects installing to `/home/devel/stage`. In short, it would only be useful for a single project, or completely independent projects.

---

<div class="post-metadata">

### Author: ![hsattler](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/h/59ef9b/32.png) [@hsattler](https://discourse.cmake.org/u/hsattler)
#### Post date: [August 28, 2020, 4:41am UTC](https://discourse.cmake.org/t/cmake-staging-prefix-not-being-used-by-find/1746/5 "2020-08-28T04:41:21Z")

</div>

I use it like that in my toplevel CMakeLists.txt:

> if ( CMAKE\_CROSSCOMPILING )  
> set ( CMAKE\_INSTALL\_PREFIX “/opt” )  
> set ( STAGING\_BASE\_DIR “${CMAKE\_BINARY\_DIR}/staging” )  
> set ( CMAKE\_STAGING\_PREFIX “${STAGING\_BASE\_DIR}${CMAKE\_INSTALL\_PREFIX}” )  
> endif ()
