From 04ca873abb03462bd1250133dcea6683caeb422e Mon Sep 17 00:00:00 2001
From: Dan Liew <daniel.liew@imperial.ac.uk>
Date: Wed, 9 Mar 2016 14:09:29 +0000
Subject: [PATCH] [CMake] Provide a way to customise the install directories
 used for executables, include files and libraries. We use
 ``GNUInstallDirs.cmake`` which ships with CMake to do the difficult work of
 setting a sensible default and setting up CMake cache variables. These can be
 overriden when running CMake by setting the ``CMAKE_INSTALL_BINDIR``,
 ``CMAKE_INSTALL_INCLUDEDIR`` and ``CMAKE_INSTALL_LIBDIR`` cache variables.

---
 CMakeLists.txt                         |  7 ++++---
 README-CMake.md                        | 10 +++++++---
 contrib/cmake/src/CMakeLists.txt       |  6 +++---
 contrib/cmake/src/shell/CMakeLists.txt |  2 +-
 4 files changed, 15 insertions(+), 10 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 3cc29780d..97c8b1862 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -321,9 +321,10 @@ message(STATUS "Z3_DEPENDENT_EXTRA_C_LINK_FLAGS: ${Z3_DEPENDENT_EXTRA_C_LINK_FLA
 ################################################################################
 # Z3 installation locations
 ################################################################################
-set (Z3_INSTALL_LIB_DIR "lib")
-set (Z3_INSTALL_BIN_DIR "bin")
-set (Z3_INSTALL_INCLUDE_DIR "include")
+include(GNUInstallDirs)
+message(STATUS "CMAKE_INSTALL_LIBDIR: \"${CMAKE_INSTALL_LIBDIR}\"")
+message(STATUS "CMAKE_INSTALL_BINDIR: \"${CMAKE_INSTALL_BINDIR}\"")
+message(STATUS "CMAKE_INSTALL_INCLUDEDIR: \"${CMAKE_INSTALL_INCLUDEDIR}\"")
 
 ################################################################################
 # CMake build file locations
diff --git a/README-CMake.md b/README-CMake.md
index 28f49cf27..cdedb8374 100644
--- a/README-CMake.md
+++ b/README-CMake.md
@@ -261,7 +261,10 @@ when invoking CMake and instead set the build type within Visual Studio itself.
 The following useful options can be passed to CMake whilst configuring.
 
 * ``CMAKE_BUILD_TYPE`` - STRING. The build type to use. Only relevant for single configuration generators (e.g. "Unix Makefile" and "Ninja").
-* ``CMAKE_INSTALL_PREFIX`` - STRING. The install prefix to use (e.g. ``/usr/local/``)
+* ``CMAKE_INSTALL_BINDIR`` - STRING. The path to install z3 binaries (relative to ``CMAKE_INSTALL_PREFIX``), e.g. ``bin``.
+* ``CMAKE_INSTALL_INCLUDEDIR`` - STRING. The path to install z3 include files (relative to ``CMAKE_INSTALL_PREFIX``), e.g. ``include``.
+* ``CMAKE_INSTALL_LIBDIR`` - STRING. The path to install z3 libraries (relative to ``CMAKE_INSTALL_PREFIX``), e.g. ``lib``.
+* ``CMAKE_INSTALL_PREFIX`` - STRING. The install prefix to use (e.g. ``/usr/local/``).
 * ``ENABLE_TRACING`` - BOOL. If set to ``TRUE`` enable tracing, if set to ``FALSE`` disable tracing.
 * ``BUILD_LIBZ3_SHARED`` - BOOL. If set to ``TRUE`` build libz3 as a shared library otherwise build as a static library.
 * ``ENABLE_EXAMPLE_TARGETS`` - BOOL. If set to ``TRUE`` add the build targets for building the API examples.
@@ -322,8 +325,9 @@ in order to be sure that the copied CMake files are not out of date.
 
 ### Install/Uninstall
 
-Install and uninstall targets are supported. Use ``CMAKE_INSTALL_PREFIX`` to set the install
-prefix.
+Install and uninstall targets are supported. Use ``CMAKE_INSTALL_PREFIX`` to
+set the install prefix. If you also need need to control which directories are
+used for install set the documented ``CMAKE_INSTALL_*`` options.
 
 To install run
 
diff --git a/contrib/cmake/src/CMakeLists.txt b/contrib/cmake/src/CMakeLists.txt
index 6708f505e..2bf7442ab 100644
--- a/contrib/cmake/src/CMakeLists.txt
+++ b/contrib/cmake/src/CMakeLists.txt
@@ -165,9 +165,9 @@ foreach (header ${libz3_public_headers})
 endforeach()
 
 install(TARGETS libz3
-  LIBRARY DESTINATION "${Z3_INSTALL_LIB_DIR}"
-  ARCHIVE DESTINATION "${Z3_INSTALL_LIB_DIR}"
-  PUBLIC_HEADER DESTINATION "${Z3_INSTALL_INCLUDE_DIR}"
+  LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
+  ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
+  PUBLIC_HEADER DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
 )
 
 if (MSVC)
diff --git a/contrib/cmake/src/shell/CMakeLists.txt b/contrib/cmake/src/shell/CMakeLists.txt
index 65b97aaaf..c5d5ca880 100644
--- a/contrib/cmake/src/shell/CMakeLists.txt
+++ b/contrib/cmake/src/shell/CMakeLists.txt
@@ -43,5 +43,5 @@ target_link_libraries(shell PRIVATE ${Z3_DEPENDENT_LIBS})
 z3_add_component_dependencies_to_target(shell ${shell_expanded_deps})
 z3_append_linker_flag_list_to_target(shell ${Z3_DEPENDENT_EXTRA_CXX_LINK_FLAGS})
 install(TARGETS shell
-  RUNTIME DESTINATION "${Z3_INSTALL_BIN_DIR}"
+  RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
 )