[CMake] Best way to handle application data path for local run vs. installation

Zac Bergquist zbergquist99 at gmail.com
Sun Nov 29 22:38:47 EST 2015


So you want to hard code a location into the executable, but support
multiple locations without making multiple executables?  These sound like
conflicting requirements.

Supporting multiple locations seems reasonable, so this suggests that
you'll have to change how your application looks for these data files.

My suggestion would be to use the hard-coded location (the one you specify
with add_definition) as the default location, and add an alternate way to
look in other directories for when you want to run local (not installed)
dev builds.

I would either do this by:

1) searching the current working directory first, and falling back to the
default only if you didn't find data files in the current directory.

2) checking an environment variable for a location and falling back to the
default if the variable is not set.  When you want to run out of your build
directory you would just set the environment variable first.  You could
even have CMake copy a script into the build directory (but not the install
directory) that sets up the environment for you to save a few keystrokes.

Zac
On Nov 29, 2015 9:17 PM, "Dmitry Marakasov" <amdmi3 at amdmi3.ru> wrote:

> Hi!
>
> This question bugs me for a long time so I though maybe someone has
> a solution. I have a project which includes an application and some
> data for it. An application needs to know path to its data files, so
> I pass it via compiler definition: ADD_DEFINITIONS(-DDATADIR="...")
>
> The problem is that this path is different based on whether I want to
> run the application from build directory:
>
> ADD_DEFINITIONS(-DDATADIR="${PROJECT_SOURCE_DIR}/data")
>
> or want to install it systemwide:
>
> ADD_DEFINITIONS(-DDATADIR="${CMAKE_INSTALL_PREFIX}/share/myapp")
>
> I want my project to both run from build directory and to be
> installable systemwide, without the need to rebuild or specify extra
> options.
>
> - I don't want to make an applications search for its data relative
>   to executable path
>   - There's no cross-platform way to get an executable path
>   - This will break when executable is moved or hardlinked
> - I don't want to try both paths, as this is error prone, as installed
>   executable may pick data files from repository or vice versa
> - I don't want to make user specify additional cmake flags like
>   -DSYSTEMWIDE_INSTALLATION or -DRUN_LOCALLY.
> - I don't want to use any wrapper scripts
> - I don't want to build two executables
>
> The best solution would be for cmake to fix path in executable file
> right after installation, something similar to what cmake does with
> rpaths. Or there could be a cross-platform way for executable to know
> whether it was installed which I've missed.
>
> Any ideas?
>
> --
> Dmitry Marakasov   .   55B5 0596 FF1E 8D84 5F56  9510 D35A 80DD F9D2 F77D
> amdmi3 at amdmi3.ru  ..:  jabber: amdmi3 at jabber.ru      http://amdmi3.ru
> --
>
> Powered by www.kitware.com
>
> Please keep messages on-topic and check the CMake FAQ at:
> http://www.cmake.org/Wiki/CMake_FAQ
>
> Kitware offers various services to support the CMake community. For more
> information on each offering, please visit:
>
> CMake Support: http://cmake.org/cmake/help/support.html
> CMake Consulting: http://cmake.org/cmake/help/consulting.html
> CMake Training Courses: http://cmake.org/cmake/help/training.html
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/mailman/listinfo/cmake
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/cmake/attachments/20151129/f4c10427/attachment.html>


More information about the CMake mailing list