0

Suppose I maintain a library called mylib, whose build is configured using CMake. Installing mylib involves copying header files, a static library file, and some .cmake package configuration files. Right now, the filenames for v1 and v2 are mostly the same, so that if I install to the same location (same CMAKE_INSTALL_PREFIX), the versions overwrite each other's files.

There are multiple versions of mylib, say versions 1 and 2; and some apps which I want to build and run, app_a and app_b, which rely on different versions of mylib (v1 and v2 respectively). For convenience, assume app_a and app_b also use CMake.

Naturally, I can install each version to a separate directory, and configure app_a to look for mylib at the first path and app_b to look for it at the second path. But what I would really like is to install both version to the "same place", i.e. under the same CMAKE_INSTALL_PREFIX directory, and have the config files arranged so that, when app_a and app_b search for mylib there, they find two versions, and using their eligibility criteria choose the appropriate one.

My question: Assuming I had control of the CMakeLists.txt file of all of myapp_a, myapp_b and mylib - can this be done?

einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • @KamilCuk: Tried eing more explicit about what gets copied. – einpoklum Apr 20 '22 at 21:57
  • Well, obviously, you can't create a different header file with the same name in the same directory, I do not understand how do you intent to solve that. Just install them in a subdirectory inside `CMAKE_INSTALL_PREFIX`. You might want to look at how `FUSE_USE_VERSION` works. (If you really ask "can this be done" - well, pack all files you install inside a zip named `mylibv1.zip` and `mylibv2.zip`, install that `zip`, create a `find_package` with a target that unpacks the zip on build time to `CMAKE_BINARY_DIR` with all the headers and add a library target that links with those unpacked files) – KamilCuk Apr 20 '22 at 22:14
  • @KamilCuk: A master config file will tell users' CMake which subdirectory to find the header of which version in. – einpoklum Apr 21 '22 at 06:31
  • @einpoklum: Yes, using a single config file you could deliver both versions of the library to the user. E.g. via imported targets `mylib_v1::mylib` and `mylib_v2::mylib`. It seems you already know what to do. So, what is your real problem which you cannot solve? What kind of answers do you expect? – Tsyvarev Apr 21 '22 at 09:13
  • @Tsyvarev: No, I don't want two imported targets like that. I want `find_package(my_lib 1 REQUIRED)` to choose one v1 and present it as `mylib::mylib`, and `find_package(my_lib 2 REQUIRED)` to choose one v1 and present it as `mylib::mylib`, with both of these commands searching the same path. – einpoklum Apr 21 '22 at 09:30
  • Ok, so write the config script which checks a version passed to `find_package`. What is a problem with that? – Tsyvarev Apr 21 '22 at 09:33
  • @Tsyvarev: No problem, except right now, only a single installed version will be found, because the config files simply overwrite each other. – einpoklum Apr 21 '22 at 10:43
  • At last, the *actual* problem! One solution could be having for different library versions the **same config file** (so its overwriting is not a problem) and **separate export files** (installed with `install(EXPORT)`). That way your config file could select which export file to `include()`. – Tsyvarev Apr 21 '22 at 10:51
  • @Tsyvarev: Ok, so, you're saying there's no support for this right now, but there could be and I should file an issue? – einpoklum Apr 21 '22 at 11:27
  • By "there's no support" do you mean out-of-the-box solution from CMake itself? Yes, CMake cannot automatically create a config file with the targets selected according to the version. But I don't think that CMake *should* do that: That usage scenario is quite rare and can be implementing easily by using those CMake features which are already supported. – Tsyvarev Apr 21 '22 at 11:37
  • @Tsyvarev: Wanting to install multiple versions of a library on the same system is not that rare IMHO. ATM people just settle on installing them at different locations with completely distinct configuration files. – einpoklum Apr 21 '22 at 11:44

0 Answers0