In several projects I've had a need to compute the "full" absolute path for an installation directory. The logic to do this is in the GNUInstallDirs module, used to compute the CMAKE_INSTALL_FULL_* variables for each of the CMAKE_INSTALL_* cache variables it defines. In order to allow for this logic to be reused, I've separated it out into a macro "set_full_install_dir". This can then be used by any end user to compute their own absolute paths using the same logic.

The reason for this is that my projects are creating their own project-specific installation directory cache variables based upon the GNUInstallDirs variables, or sometimes even from scratch. I then need to compute the full path. But I need this macro to be able to do that to match the GNUInstallDirs behaviour.

The macro name might need adjusting, but I hope the basic approach is OK in principle.


Kind regards,
Roger
>From ed6301e40a8b5d71fb45b55a8ac2dbacb5804be7 Mon Sep 17 00:00:00 2001
From: Roger Leigh <rle...@codelibre.net>
Date: Sun, 28 Aug 2016 18:19:02 +0100
Subject: [PATCH] GNUInstallDirs: Add set_full_install_dir macro

---
 Modules/GNUInstallDirs.cmake | 80 +++++++++++++++++++++++++++-----------------
 1 file changed, 50 insertions(+), 30 deletions(-)

diff --git a/Modules/GNUInstallDirs.cmake b/Modules/GNUInstallDirs.cmake
index b42084e..50a6797 100644
--- a/Modules/GNUInstallDirs.cmake
+++ b/Modules/GNUInstallDirs.cmake
@@ -99,6 +99,22 @@
 #   `Filesystem Hierarchy Standard`_.
 #
 # .. _`Filesystem Hierarchy Standard`: https://refspecs.linuxfoundation.org/FHS_3.0/fhs/index.html
+#
+# Macros
+# ^^^^^^
+#
+# ::
+#
+#    set_full_install_dir(fullvar var)
+#
+# Set the given variable ``fullvar`` to the absolute path contained
+# within the variable ``var``.  This is to allow the computation of an
+# absolute path, accounting for all the special cases documented
+# above.  While this macro is used to compute the various
+# ``CMAKE_INSTALL_FULL_<dir>`` variables, it is exposed publicly to
+# allow users who create additional path variables to also compute
+# absolute paths where necessary, using the same logic.
+#
 
 #=============================================================================
 # Copyright 2015 Alex Turbov <i.za...@gmail.com>
@@ -300,55 +316,59 @@ mark_as_advanced(
   CMAKE_INSTALL_DOCDIR
   )
 
-# Result directories
-#
-foreach(dir
-    BINDIR
-    SBINDIR
-    LIBEXECDIR
-    SYSCONFDIR
-    SHAREDSTATEDIR
-    LOCALSTATEDIR
-    LIBDIR
-    INCLUDEDIR
-    OLDINCLUDEDIR
-    DATAROOTDIR
-    DATADIR
-    INFODIR
-    LOCALEDIR
-    MANDIR
-    DOCDIR
-    )
-  if(NOT IS_ABSOLUTE "${CMAKE_INSTALL_${dir}}")
+macro(set_full_install_dir fullvar var)
+  if(NOT IS_ABSOLUTE "${${var}}")
     # Handle special cases:
     # - CMAKE_INSTALL_PREFIX == /
     # - CMAKE_INSTALL_PREFIX == /usr
     # - CMAKE_INSTALL_PREFIX == /opt/...
     if("${CMAKE_INSTALL_PREFIX}" STREQUAL "/")
       if("${dir}" STREQUAL "SYSCONFDIR" OR "${dir}" STREQUAL "LOCALSTATEDIR")
-        set(CMAKE_INSTALL_FULL_${dir} "/${CMAKE_INSTALL_${dir}}")
+        set(${fullvar} "/${${var}}")
       else()
-        if (NOT "${CMAKE_INSTALL_${dir}}" MATCHES "^usr/")
-          set(CMAKE_INSTALL_${dir} "usr/${CMAKE_INSTALL_${dir}}")
+        if (NOT "${${var}}" MATCHES "^usr/")
+          set(${var} "usr/${${var}}")
         endif()
-        set(CMAKE_INSTALL_FULL_${dir} "/${CMAKE_INSTALL_${dir}}")
+        set(${fullvar} "/${${var}}")
       endif()
     elseif("${CMAKE_INSTALL_PREFIX}" MATCHES "^/usr/?$")
       if("${dir}" STREQUAL "SYSCONFDIR" OR "${dir}" STREQUAL "LOCALSTATEDIR")
-        set(CMAKE_INSTALL_FULL_${dir} "/${CMAKE_INSTALL_${dir}}")
+        set(${fullvar} "/${${var}}")
       else()
-        set(CMAKE_INSTALL_FULL_${dir} "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_${dir}}")
+        set(${fullvar} "${CMAKE_INSTALL_PREFIX}/${${var}}")
       endif()
     elseif("${CMAKE_INSTALL_PREFIX}" MATCHES "^/opt/.*")
       if("${dir}" STREQUAL "SYSCONFDIR" OR "${dir}" STREQUAL "LOCALSTATEDIR")
-        set(CMAKE_INSTALL_FULL_${dir} "/${CMAKE_INSTALL_${dir}}${CMAKE_INSTALL_PREFIX}")
+        set(${fullvar} "/${${var}}${CMAKE_INSTALL_PREFIX}")
       else()
-        set(CMAKE_INSTALL_FULL_${dir} "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_${dir}}")
+        set(${fullvar} "${CMAKE_INSTALL_PREFIX}/${${var}}")
       endif()
     else()
-      set(CMAKE_INSTALL_FULL_${dir} "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_${dir}}")
+      set(${fullvar} "${CMAKE_INSTALL_PREFIX}/${${var}}")
     endif()
   else()
-    set(CMAKE_INSTALL_FULL_${dir} "${CMAKE_INSTALL_${dir}}")
+    set(${fullvar} "${${var}}")
   endif()
+endmacro()
+
+# Result directories
+#
+foreach(dir
+    BINDIR
+    SBINDIR
+    LIBEXECDIR
+    SYSCONFDIR
+    SHAREDSTATEDIR
+    LOCALSTATEDIR
+    LIBDIR
+    INCLUDEDIR
+    OLDINCLUDEDIR
+    DATAROOTDIR
+    DATADIR
+    INFODIR
+    LOCALEDIR
+    MANDIR
+    DOCDIR
+    )
+  set_full_install_dir(CMAKE_INSTALL_FULL_${dir} CMAKE_INSTALL_${dir})
 endforeach()
-- 
2.7.4

-- 

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-developers

Reply via email to