# How to install in multiple locations (prefixes) without building again?

**URL:** https://discourse.cmake.org/t/how-to-install-in-multiple-locations-prefixes-without-building-again/9278
**Category:** Usage
**Created:** [October 25, 2023, 3:23pm UTC](https://discourse.cmake.org/t/how-to-install-in-multiple-locations-prefixes-without-building-again/9278 "2023-10-25T15:23:25Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![fferri](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/f/e9c0ed/32.png) [@fferri](https://discourse.cmake.org/u/fferri)
#### Post date: [October 25, 2023, 3:23pm UTC](https://discourse.cmake.org/t/how-to-install-in-multiple-locations-prefixes-without-building-again/9278/1 "2023-10-25T15:23:25Z")

</div>

I use a top-level CMake projects with ExternalProject to manage the build/assembling of a big software release consisting of many CMake subprojects (each one builds install with cmake --build / --install).

e.g. an entry for a subproject in the top-level CMakeLists.txt would look like:

```auto
ExternalProject_Add(subproject1
    DEPENDS dependency1 dependency2 ...
    GIT_REPOSITORY https://github.com/...
    SOURCE_DIR ${CMAKE_BINARY_DIR}/Source/subproject1
    CMAKE_ARGS ${COMMON_CMAKE_ARGS} ${COMMON_SUBPROJECTS_CMAKE_ARGS} -DFOO=BAR
    BUILD_COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR}/Build/subproject1 --config ${CMAKE_BUILD_TYPE}
    INSTALL_COMMAND ${CMAKE_COMMAND} --install ${CMAKE_BINARY_DIR}/Build/subproject1 --config ${CMAKE_BUILD_TYPE}
)

```

Because the project can be built in various “flavours”, I repeat the top-level build N times once for each flavor, passing CMake variables to differentiate the flavour. But only a few of the subprojects actually build differently based on the flavour, so this is a waste of time.

A more optimized approach would be to declare only those flavor-dependent subprojects multiple times (i.e. one ExternalProject\_Add call per flavour, with same git repo, but different CMake variables, which would end up installing the items in different locations depending on flavour)…  
**But then how to have the other subprojects (independent of flavour) installed into the multiple (flavour) locations from the same build, i.e. without building N times?**  
I want to keep using the install rules defined in the subproject’s CMakeLists.txt, i.e.:

```auto
INSTALL_COMMAND ${CMAKE_COMMAND} --install ${CMAKE_BINARY_DIR}/Build/subproject1 --config ${CMAKE_BUILD_TYPE}

```

and not a custom copy command such as:

```auto
INSTALL_COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_BINARY_DIR}/Build/subproject1/someBuildProduct ${TOPLEVELPROJECT_INSTALL_DIR}

```

Is there an option (to --install ideally) to switch install location without having to reconfigure/rebuild the subproject?

I have found only CMAKE\_INSTALL\_PREFIX / --prefix… doesn’t look like it is meant for this job.

Maybe I am complicating my life with using CMake for release engineering, and there is a simpler tool that does the job? But since I want to re-use the install rules defined in each subproject’s CMakeLists.txt, the question stands: how to avoid rebuilding the subproject, and install it into multiple locations?

---

<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: [October 25, 2023, 8:23pm UTC](https://discourse.cmake.org/t/how-to-install-in-multiple-locations-prefixes-without-building-again/9278/2 "2023-10-25T20:23:12Z")

</div>

I would have expected the `--prefix` option for `cmake --install` to do what you want here. If you’ve tried it, in what way does it not work for your scenario?

---

<div class="post-metadata">

### Author: ![fferri](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/f/e9c0ed/32.png) [@fferri](https://discourse.cmake.org/u/fferri)
#### Post date: [November 9, 2023, 1:10pm UTC](https://discourse.cmake.org/t/how-to-install-in-multiple-locations-prefixes-without-building-again/9278/3 "2023-11-09T13:10:21Z")

</div>

The reason I think it won’t work is that I have all my subprojects install their files in a specific directory (supplied at configuration time) or subdirectories of it, which is not a relative path, therefore ${CMAKE\_INSTALL\_PREFIX} is ignored by install().

To support ${CMAKE\_INSTALL\_PREFIX} I would have to change the install logic of those subprojects. (I was trying to keep backwards compatibility)

I kind of locked myself into a corner with that…

EDIT: I solved that by passing “.” as install directory variable mentioned above (but I also had that typed as a CACHE variable of type PATH, and I had to remove that as that would cause the path to become absolute, and ultimately ignoring CMAKE\_INSTALL\_PREFIX)

i.e. this is the part I had to remove in subprojects:  
`set(INSTALL_DIR "" CACHE PATH "If specified, it will be used as install destination")`

---

<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: [November 10, 2023, 1:31am UTC](https://discourse.cmake.org/t/how-to-install-in-multiple-locations-prefixes-without-building-again/9278/4 "2023-11-10T01:31:22Z")

</div>

If you are using `cmake --install <build-dir> --prefix <somewhere>`, that overrides `CMAKE_INSTALL_PREFIX`. The value of `CMAKE_INSTALL_PREFIX` is not used if you specify the `--prefix` option to `cmake --install`. The relative path of each installed item below the prefix is still used though, so your executables would still end up in `bin` below the prefix, libraries in `lib`, and so on.

I don’t know what `INSTALL_DIR` is, I don’t think that’s something CMake defines or uses. I’m assuming it is used somehow by your project.
