ALib C++ Library
Library Version: 2402 R1
Documentation generated by doxygen
Loading...
Searching...
No Matches
ALibTools.cmake
1# #################################################################################################
2# ALibTools.cmake - CMake Tools for projects using ALib
3#
4# Copyright 2015-2024 A-Worx GmbH, Germany
5# Published under 'Boost Software License' (a free software license, see LICENSE.txt)
6#
7# Note:
8# This CMake file is included by "ALib.cmake" automatically and is not intended for manual
9# inclusion. Its contents was separated into a separated cmake file solely for clarity.
10# #################################################################################################
11
12# -------------------------------------------------------------------------------------------------
13# Collects resource files and add copy commands to a new target.
14# Then sets a dependency of the main target to this new resource target.
15#
16# This methods may be used when resource files like texts, json data or images have to be copied
17# to the target build directory. The use is simple and done as follows:
18#
19# 1. Create a LIST variable that holds one or more directory names with file wildcards, used for
20# "globbing" the resource files, for example:
21#
22# list( append MY_PROJECT_RESOURCE_FILES ../../resources/res_*.img )
23#
24# 2. After the executable (or library) is defined with cmake (add_executable or add_library),
25# insert the following invocation of this method:
26#
27# AddResourceTarget( my_resources_target my_target ${MY_PROJECT_RESOURCE_FILES})
28#
29# with the first parameter being an arbitrary name for the resource target to create.
30#
31# 3. In case of using "cotire", the unity projects need to receive the information of the added
32# project, because cotire today can't detect inter-project dependencies:
33#
34# add_dependencies ( my_target_unity my_resources_target )
35# -------------------------------------------------------------------------------------------------
36FUNCTION( AddResourceTarget resourceProjectName targetName resourceFileFilters)
37
38 #message(STATUS "### AddResourceTarget() ###")
39 #message(STATUS "resourceProjectName : ${resourceProjectName}")
40 #message(STATUS "targetName : ${targetName}")
41 #message(STATUS "resourceFilter : ${resourceFilter}")
42
43 FOREACH(_resourceFileFilter IN LISTS resourceFileFilters)
44 file( GLOB_RECURSE resourceList ${_resourceFileFilter} )
45 ENDFOREACH()
46 #message(STATUS "resourceList : ${resourceList}")
47
48 FOREACH(_resourceFile IN LISTS resourceList)
49
50 #get filename and create destination file
51 get_filename_component( _destFile ${_resourceFile} NAME)
52 set ( _destFile ${CMAKE_CURRENT_BINARY_DIR}/${_destFile} )
53 list ( APPEND _destFileList ${_destFile} )
54
55 #add copy command
56 add_custom_command( OUTPUT "${_destFile}"
57 COMMAND ${CMAKE_COMMAND} -E copy "${_resourceFile}" "${_destFile}"
58 COMMENT "Copying resource to ${_destFile}" )
59 ENDFOREACH()
60
61 # create the copy target
62 add_custom_target(${resourceProjectName} ALL DEPENDS "${_destFileList}")
63
64 # add a dependency to the main target
65 add_dependencies ( ${targetName} ${resourceProjectName} )
66
67ENDFUNCTION(AddResourceTarget)
68
69# -------------------------------------------------------------------------------------------------
70# Removes duplicates from a given LIST and collects those found in second list. The list of
71# found duplicates may contain duplicates as well, if entries had been trippled before.
72# In other words, the sum of entries of the modified input list and entries in the result list
73# is the same as the number of original entries.
74#
75# Note that both input parameters have to give the name of variables:
76# - "sourceListName" denotes the name of an existing variable in the calling scope which is
77# modified.
78# - "duplicateListName" denotes the name of an new variable that is created in the calling scope.
79# In the case it previously existed already, it will be overwritten.
80#
81# ===============================================================================================
82# Recipe for modifying existing variables of the, and creating new ones in the parent scope
83# -------------------------------------------------------------------------------------------------
84# CMake is not easy to understand for humans. Therefore, two recepies should be given here, as
85# it took us a while to understand that (or rather: to make it work).
86#
87# 1. How to create a variable in the parent scope:
88# - Have a function parameter that specifies the name (!) of the variable.
89# - Use an own local variable to create the result value.
90# - At the end, before the function returns, assign the local variable into the gloabl
91# scope using parameter PARENT_SCOPE of the SET command.
92#
93# Here is a sample:
94#
95# FUNCTION( SetHello resultVarName )
96# SET( resultVar "Hello World" )
97# SET( ${resultVarName} ${resultVar} PARENT_SCOPE)
98# ENDFUNCTION( SetHello )
99#
100# 2. How to modify an existing variable from the parent scope:
101# - Have a function parameter that specifies the name (!) of the existing variable.
102# - Copy the contents of the existing variable into a local one. This has to be done with
103# a nested ${} operator! (Yes, when you think about it...)
104# - Do modifications only with the local copy (e.g. add or remove list entries)
105# - At the end, before the function returns, assign the local variable into the gloabl
106# scope using parameter PARENT_SCOPE of the SET command.
107#
108# Here is a sample:
109#
110# FUNCTION( AddHello existingVarName )
111# SET( localVar ${${existingVarName}} )
112# SET( localVar "Hello World")
113# SET( ${existingVarName} ${localVar} PARENT_SCOPE)
114# ENDFUNCTION( AddHello )
115#
116# -------------------------------------------------------------------------------------------------
117FUNCTION( CollectAndRemoveDuplicates sourceListVariableName duplicateListVariableName )
118 #message(STATUS "****************** CollectAndRemoveDuplicates START ***********************" )
119
120 # copy source list to local list
121 SET( sourceList ${${sourceListVariableName}} )
122
123 # copy list to duplicates
124 SET( duplicates ${sourceList} )
125
126 # not empty?
127 LIST( LENGTH sourceList qtySource )
128 IF( ${qtySource} GREATER 0 )
129
130 # remove duplicates from source
131 LIST( REMOVE_DUPLICATES sourceList )
132
133 # remove each in source from duplicates
134 FOREACH( entry IN LISTS sourceList )
135 LIST(FIND duplicates ${entry} idx )
136 IF( ${idx} EQUAL -1 )
137 message( ERROR " Internal script error in CollectAndRemoveDuplicates: Item not found in original list." )
138 ENDIF()
139 LIST(REMOVE_AT duplicates ${idx})
140 ENDFOREACH()
141 ENDIF()
142
143 # Transfer local variables back to parent scope
144 SET( ${sourceListVariableName} ${sourceList} PARENT_SCOPE)
145 SET( ${duplicateListVariableName} ${duplicates} PARENT_SCOPE)
146
147 #message(STATUS "****************** CollectAndRemoveDuplicates END ************************" )
148ENDFUNCTION(CollectAndRemoveDuplicates)
149
150
151FUNCTION( DumpList listVariableName )
152
153 LIST( LENGTH ${listVariableName} length )
154 message(STATUS "Entries in list '${listVariableName}': ${length}" )
155
156 MATH(EXPR maxIdx "${length} - 1" )
157 FOREACH( idx RANGE 0 ${maxIdx} )
158 list(GET ${listVariableName} ${idx} entry )
159 message( STATUS " ${idx}: ${entry}" )
160 ENDFOREACH()
161
162ENDFUNCTION( DumpList )
163
164
165# -------------------------------------------------------------------------------------------------
166# Precompiled headers and unity build with 'cotire' CMake script.
167# More Info at: https://github.com/sakra/cotire/
168#
169# Note:
170# To enable/disable change CMake cache variable ALIB_CMAKE_COTIRE.
171# To change the variable permanently (on clean cmake builds), set the variable prior
172# to invoking this script.
173# -------------------------------------------------------------------------------------------------
174
175# download cotire (once)
176FUNCTION (CheckDownloadCotire)
177 set( _cotireFileName ${CMAKE_CURRENT_LIST_DIR}/cotire.cmake )
178 set( _cotireFileUrl "http://raw.githubusercontent.com/sakra/cotire/master/CMake/cotire.cmake" )
179
180 if( NOT EXISTS ${_cotireFileName} )
181 message( STATUS "Cotire not found. Trying to download ${_cotireFileName} from ${_cotireFileUrl}..." )
182 file( DOWNLOAD ${_cotireFileUrl} ${_cotireFileName} STATUS _status)
183 message( STATUS "Download status: ${_status}" )
184
185 if( NOT EXISTS ${_cotireFileName} )
186 message( "...Error: Download of 'cotire' failed. Continuing without cotire." )
187 set( ALIB_CMAKE_COTIRE "Off" )
188 else()
189 message( STATUS "... cotire downloaded successfully." )
190 endif()
191 endif()
192
193ENDFUNCTION()
194
195
196# --------------------------------------------------------------------------------------------------
197# ALibDumpCMakeVariables() Writes out all currently defined CMake variables
198# --------------------------------------------------------------------------------------------------
199FUNCTION (ALibDumpCMakeVariables)
200
201 message(STATUS "------------------------------ Dumping CMake Variables: ------------------------------")
202 get_cmake_property(_variableNames VARIABLES)
203 list (SORT _variableNames)
204 foreach (_variableName ${_variableNames})
205 if( "${_variableName}" STREQUAL "temp"
206 OR "${_variableName}" STREQUAL "_gccStdout"
207 OR "${_variableName}" STREQUAL "_defineLines"
208 OR "${_variableName}" STREQUAL "_dummy"
209 OR "${_variableName}" STREQUAL "OUTPUT"
210 OR "${_variableName}" STREQUAL "CMAKE_C_COMPILER_ID_TOOL_MATCH_REGEX"
211 OR "${_variableName}" STREQUAL "CMAKE_CXX_COMPILER_ID_TOOL_MATCH_REGEX"
212 OR "${_variableName}" STREQUAL "CMAKE_C_COMPILER_ID_PLATFORM_CONTENT"
213 OR "${_variableName}" STREQUAL "CMAKE_CXX_COMPILER_ID_PLATFORM_CONTENT"
214 OR "${_variableName}" STREQUAL "CMAKE_EXTRA_GENERATOR_C_SYSTEM_DEFINED_MACROS"
215 OR "${_variableName}" STREQUAL "CMAKE_EXTRA_GENERATOR_CXX_SYSTEM_DEFINED_MACROS"
216 OR "${_variableName}" STREQUAL "PTHREAD_C_CXX_TEST_SOURCE"
217 OR "${_variableName}" STREQUAL "Qt5Gui_PLUGINS" )
218 message(STATUS "${_variableName}=[...] (omitted)")
219 continue()
220 endif()
221
222 message(STATUS "${_variableName}=${${_variableName}}")
223 endforeach()
224 message(STATUS "------------------------------ Dumping CMake Variables (END) ------------------------------")
225
226ENDFUNCTION()
227