# Install sources and rebuild from installed sources after build?

**URL:** https://discourse.cmake.org/t/install-sources-and-rebuild-from-installed-sources-after-build/6233
**Category:** Usage
**Tags:** os:linux
**Created:** [August 10, 2022, 7:02pm UTC](https://discourse.cmake.org/t/install-sources-and-rebuild-from-installed-sources-after-build/6233 "2022-08-10T19:02:51Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![shabiel](https://discourse.cmake.org/user_avatar/discourse.cmake.org/shabiel/32/1285_2.png) [@shabiel](https://discourse.cmake.org/u/shabiel)
#### Post date: [August 10, 2022, 7:02pm UTC](https://discourse.cmake.org/t/install-sources-and-rebuild-from-installed-sources-after-build/6233/1 "2022-08-10T19:02:51Z")

</div>

Hello CMakers,

Bear with me, as I think this may be an unusual scenario.

I will illustrate it with an example. Let’s say we have a file `/home/sam/project/x.m`. This file is compiled into `/home/sam/project/x.o` and then linked to `/home/sam/project/x.so`. The file x.so has a link to `/home/sam/project/x.m` to get the sources, and at runtime it can interrogate `/home/sam/project/x.m` for the source. CMake can accomplish this scheme well, and is what is usually expected.

Now here is the twist. When the file `/home/sam/project/x.so` gets installed into `/usr/local/lib/x.so`, it still refers back to `/home/sam/project/x.m` for source information; but that file can be modified or deleted. I know this is not a common scenario, as in most compiled languages, there is no link back to the sources.

The way we have dealt with this in our pre-CMake days is to have two separate sets of compilations/linking:

1. “make”: compile `x.m` in the user directory, into x.o and link into x.so; and then run tests on that.
2. “make install”: copy `x.m` into a system directory (say `/usr/local/src/x.m`), compile it and link it again, but this time using the copy in `/usr/local/src`, and install `x.so` into `/usr/local/lib/x.so`.

I wonder if CMake has a way for us to do the second step (first step CMake can handle well…).

–Sam

---

<div class="post-metadata">

### Author: ![kyle.edwards](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/k/65b543/32.png) [@kyle.edwards](https://discourse.cmake.org/u/kyle.edwards)
#### Post date: [August 10, 2022, 7:14pm UTC](https://discourse.cmake.org/t/install-sources-and-rebuild-from-installed-sources-after-build/6233/2 "2022-08-10T19:14:28Z")

</div>

Instead of dealing with absolute paths, my advice would be to use paths relative to the shared library. It’s possible to find the path to the shared library by using `dladdr1()`. From there, you can traverse to where you expect the source file to be.

---

<div class="post-metadata">

### Author: ![shabiel](https://discourse.cmake.org/user_avatar/discourse.cmake.org/shabiel/32/1285_2.png) [@shabiel](https://discourse.cmake.org/u/shabiel)
#### Post date: [August 10, 2022, 7:35pm UTC](https://discourse.cmake.org/t/install-sources-and-rebuild-from-installed-sources-after-build/6233/3 "2022-08-10T19:35:12Z")

</div>

> Instead of dealing with absolute paths, my advice would be to use paths relative to the shared library.

Thank you Kyle. Your reply is instructive in that what we want to accomplish is not easily doable in CMake.

---

<div class="post-metadata">

### Author: ![kyle.edwards](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/k/65b543/32.png) [@kyle.edwards](https://discourse.cmake.org/u/kyle.edwards)
#### Post date: [August 10, 2022, 7:43pm UTC](https://discourse.cmake.org/t/install-sources-and-rebuild-from-installed-sources-after-build/6233/4 "2022-08-10T19:43:21Z")

</div>

> [@shabiel](#):
>
> what we want to accomplish is not easily doable in CMake.

That’s not what I said.

If you really want to use absolute paths, you could create two different targets that compile from the same source files but have a preprocessor definition to control where it reads the source from at runtime. For example:

```cmake
add_library(build_lib SHARED ${sources})
target_compile_definitions(build_lib PRIVATE "SOURCE_LOCATION=${CMAKE_CURRENT_SOURCE_DIR}")
add_library(install_lib SHARED ${sources})
target_compile_definitions(install_lib PRIVATE "SOURCE_LOCATION=/usr/local/src")

```

However, I suggest using relative paths if you can, because that eliminates both the complexity and increased build time (admittedly not much if it’s just a single `.o` file) associated with compiling the target twice. The CMake executable itself uses this approach - the executable determines its absolute location, then traverses from there to figure out whether it’s in a build directory or install directory, then traverses to its `Modules` directory to get the files that it needs.

---

<div class="post-metadata">

### Author: ![shabiel](https://discourse.cmake.org/user_avatar/discourse.cmake.org/shabiel/32/1285_2.png) [@shabiel](https://discourse.cmake.org/u/shabiel)
#### Post date: [August 11, 2022, 3:29pm UTC](https://discourse.cmake.org/t/install-sources-and-rebuild-from-installed-sources-after-build/6233/5 "2022-08-11T15:29:28Z")

</div>

Thank you again. It does give me an idea of what’s possible.
