From 1ec753146fa7b2fe7187765fd51e167db4bee3c9 Mon Sep 17 00:00:00 2001
From: Romain Forlot <romain.forlot@iot.bzh>
Date: Mon, 9 Jul 2018 13:57:28 +0200
Subject: Use CACHE variable for other common CMAKE variable

This is done to be able to overwite from CLI those variables.
CMAKE_INSTALL_PREFIX is a special case since it is set by default
to '/usr/local' at CMake invocation. So overwriting by CLI is fine
but set it up using config.cmake file not. So since CMake v3.7 there
is a variable to know if this variable has been set by default or not.

Change-Id: I60c5161fa9a4134d100ef709d0966d599871ad44
Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
---
 cmake/cmake.d/03-macros.cmake | 7 +++++++
 cmake/common.cmake            | 1 +
 2 files changed, 8 insertions(+)

(limited to 'cmake')

diff --git a/cmake/cmake.d/03-macros.cmake b/cmake/cmake.d/03-macros.cmake
index 085c249..0d24a40 100644
--- a/cmake/cmake.d/03-macros.cmake
+++ b/cmake/cmake.d/03-macros.cmake
@@ -108,6 +108,13 @@ ENDMACRO(LIST_FILTER)
 
 # Generic useful macro
 # -----------------------
+macro(set_install_prefix)
+	if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT AND INSTALL_PREFIX)
+		message(STATUS "-- Overwrite the CMAKE default install prefix with ${INSTALL_PREFIX}")
+		set(CMAKE_INSTALL_PREFIX ${INSTALL_PREFIX} CACHE PATH "Install prefix" FORCE)
+	endif()
+endmacro()
+
 macro(PROJECT_TARGET_ADD TARGET_NAME)
 	set_property(GLOBAL APPEND PROPERTY PROJECT_TARGETS ${TARGET_NAME})
 	set(TARGET_NAME ${TARGET_NAME})
diff --git a/cmake/common.cmake b/cmake/common.cmake
index e764d34..8e57b26 100644
--- a/cmake/common.cmake
+++ b/cmake/common.cmake
@@ -85,6 +85,7 @@ foreach(file ${system_cmakefiles} ${home_cmakefiles} ${project_cmakefiles})
 	include(${file})
 endforeach()
 
+set_install_prefix()
 prevent_in_source_build()
 
 if(DEFINED PROJECT_SRC_DIR_PATTERN)
-- 
cgit