1#
2#    Copyright 2017 Kai Pastor
3#
4#    This file is part of OpenOrienteering.
5#
6#    OpenOrienteering is free software: you can redistribute it and/or modify
7#    it under the terms of the GNU General Public License as published by
8#    the Free Software Foundation, either version 3 of the License, or
9#    (at your option) any later version.
10#
11#    OpenOrienteering is distributed in the hope that it will be useful,
12#    but WITHOUT ANY WARRANTY; without even the implied warranty of
13#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14#    GNU General Public License for more details.
15#
16#    You should have received a copy of the GNU General Public License
17#    along with OpenOrienteering.  If not, see <http://www.gnu.org/licenses/>.
18
19
20# Sets CMAKE_CROSSCOMPILING and variables which describe the target system.
21macro(handle_crosscompiling)
22	set(CMAKE_CROSSCOMPILING @CMAKE_CROSSCOMPILING@)
23	# These variables must describe the target system
24	set(ANDROID @ANDROID@)
25	set(APPLE   @APPLE@)
26	set(MINGW   @MINGW@)
27	set(UNIX    @UNIX@)
28	set(WIN32   @WIN32@)
29endmacro()
30
31
32# This function wraps BundleUtilities' fixup_bundle()
33# to make it work for cross-builds.
34function(fixup_bundle_portable runtime dirs)
35	handle_crosscompiling()
36	if(MINGW)
37		# gp_tool and gp_cmd are needed for module GetPrerequisites.
38		set(gp_tool "objdump")
39		set(gp_cmd  "@CMAKE_OBJDUMP@")
40		# grep is used (and desperately needed) to speed up objdump parsing.
41		set(gp_grep_cmd "@gp_grep_cmd@")
42		# This function resolves all unknown items which do not match the
43		# MinGW DLL name pattern NAME-NUMBER.dll as 'system' libraries,
44		# thus catching the Windows system libraries in the MinGW context.
45		function(gp_resolve_item_override context item exepath dirs resolved_item_var resolved_var)
46			if(NOT ${resolved_var}
47			   AND NOT "${item}" MATCHES "-[0-9]*.dll$")
48				set(${resolved_item_var} "/system/${item}" PARENT_SCOPE)
49				set(${resolved_var} 1 PARENT_SCOPE)
50			endif()
51		endfunction()
52	endif()
53
54	if(WIN32)
55		include(BundleUtilities)
56		file(GLOB_RECURSE plugins "$ENV{DESTDIR}${CMAKE_INSTALL_PREFIX}${runtime_destination}/plugins/*.dll")
57		list(APPEND runtime ${plugins})
58		fixup_bundle("$ENV{DESTDIR}${CMAKE_INSTALL_PREFIX}${runtime_destination}/Mapper.exe" "${runtime}" "${dirs}")
59		# Strip bundled DLLs
60		if (CMAKE_INSTALL_DO_STRIP AND NOT "@CMAKE_STRIP@" STREQUAL "")
61			file(GLOB dlls "$ENV{DESTDIR}${CMAKE_INSTALL_PREFIX}${runtime_destination}/*.dll")
62			foreach(item ${dlls} ${runtime})
63				execute_process(COMMAND "@CMAKE_STRIP@" --strip-unneeded "${item}")
64			endforeach()
65		endif()
66	elseif(APPLE)
67		include(BundleUtilities)
68		file(GLOB_RECURSE plugins "$ENV{DESTDIR}${CMAKE_INSTALL_PREFIX}${runtime_destination}@MAPPER_MACOS_SUBDIR@/../PlugIns/*.dylib")
69		list(APPEND runtime "${plugins}")
70		fixup_bundle("$ENV{DESTDIR}${CMAKE_INSTALL_PREFIX}${runtime_destination}/Mapper.app" "${runtime}" "${dirs}")
71		if (CMAKE_INSTALL_DO_STRIP AND NOT "@CMAKE_STRIP@" STREQUAL "")
72			file(GLOB dylibs "$ENV{DESTDIR}${CMAKE_INSTALL_PREFIX}${runtime_destination}@MAPPER_MACOS_SUBDIR@/*.dylib")
73			foreach(item ${dylibs} ${runtime})
74				execute_process(COMMAND "@CMAKE_STRIP@" -x "${item}")
75			endforeach()
76		endif()
77	elseif(ANDROID)
78		# Do nothing
79	elseif(UNIX)
80		# Add required symlinks.
81		execute_process(COMMAND ldconfig -n "$ENV{DESTDIR}${CMAKE_INSTALL_PREFIX}@MAPPER_LIBRARY_DESTINATION@")
82	endif()
83endfunction()
84
85
86# Write a minimal qt.conf if needed.
87function(handle_qt_conf)
88	handle_crosscompiling()
89	set(qt_conf "$ENV{DESTDIR}${CMAKE_INSTALL_PREFIX}${runtime_destination}/qt.conf")
90	if(EXISTS "${qt_conf}")
91		message(STATUS "Skipping ${qt_conf}")
92		return()
93	elseif(WIN32)
94		message(STATUS "Writing ${qt_conf}")
95		file(RELATIVE_PATH rel_path
96		  "$ENV{DESTDIR}${CMAKE_INSTALL_PREFIX}/@MAPPER_RUNTIME_DESTINATION@"
97		  "$ENV{DESTDIR}${CMAKE_INSTALL_PREFIX}/@MAPPER_LIBRARY_DESTINATION@")
98		if(rel_path STREQUAL "")
99			set(rel_path ".")
100		endif()
101		file(WRITE "${qt_conf}" "\
102[Paths]
103Plugins=${rel_path}/plugins
104Translations=${rel_path}/translations
105")
106	elseif(APPLE)
107		set(qt_conf "$ENV{DESTDIR}${CMAKE_INSTALL_PREFIX}${runtime_destination}@MAPPER_MACOS_SUBDIR@/../Resources/qt.conf")
108		message(STATUS "Writing ${qt_conf}")
109		file(WRITE "${qt_conf}" "\
110[Paths]
111Plugins=PlugIns
112")
113	endif()
114endfunction()
115
116
117# BundleUtilities stumples upon "/."
118set(runtime_destination "/@MAPPER_RUNTIME_DESTINATION@")
119if(runtime_destination STREQUAL "/.")
120	set(runtime_destination "")
121endif()
122set(runtime "")
123set(dirs "@MAPPER_LIB_HINTS@")
124handle_qt_conf()
125#fixup_bundle_portable("${runtime}" "${dirs}")
126