1 # list command no longer ignores empty elements
2 cmake_policy(SET CMP0007 NEW)
5 list(POP_FRONT ARGV outlist)
9 OUTPUT_VARIABLE cmd_out
10 OUTPUT_STRIP_TRAILING_WHITESPACE
13 string(REPLACE "\n" ";" cmd_out "${cmd_out}")
14 list(TRANSFORM cmd_out STRIP)
15 set(${outlist} ${cmd_out} PARENT_SCOPE)
18 function(GatherRuntimePaths source)
19 Execute(output otool -l ${source})
22 foreach (line ${output})
23 if(line MATCHES "^.*cmd LC_RPATH")
28 if (line_index EQUAL 1)
33 if (line_index EQUAL 2)
34 string(REGEX REPLACE "^.*path" "" extracted_path ${line})
35 string(REGEX REPLACE "\\(offset .*\\)$" "" extracted_path ${extracted_path})
36 string(STRIP ${extracted_path} extracted_path)
37 list(APPEND runpaths ${extracted_path})
43 set(runpaths ${runpaths} PARENT_SCOPE)
46 function(GatherSFMLLibrariesForCopying libraries postcmds)
47 foreach(library ${libraries})
48 Execute(output otool -L ${library})
50 foreach(line ${output})
51 if(line MATCHES "^.*libsfml.*.[^0-9].[^0-9].[^0-9].*\\.dylib ")
52 string(REGEX REPLACE "dylib .*" "dylib" line "${line}")
54 if(line MATCHES "^@rpath/")
55 get_filename_component(refname "${line}" NAME)
56 list(APPEND postcmds "sh -c 'install_name_tool -change ${line} @executable_path/../Frameworks/${refname} ${library}'")
57 list(APPEND libs ${dylib_location})
59 elseif(line MATCHES "^@rpath/../Frameworks/.*.framework/Versions/.*/.*")
60 string(REGEX MATCH "^@rpath/../Frameworks/(.*)/Versions/.*$" _ ${line})
61 list(APPEND frameworksToCopy ${CMAKE_MATCH_1})
66 set(postcmds ${postcmds} PARENT_SCOPE)
67 set(frameworksToCopy ${frameworksToCopy} PARENT_SCOPE)
70 function (InstallSFMLDependencies frameworks postcmds destination)
71 foreach (framework ${frameworks})
72 unset(frameworkLocation)
73 string(REGEX MATCH "(.*).framework" _ ${framework})
74 string(APPEND frameworkLocation ${SFML_FRAMEWORKS_LOCATION} "/" ${framework})
75 file(INSTALL ${frameworkLocation} DESTINATION ${destination})
79 function(AdjustSFMLDependencies libraries destination)
80 foreach (libraryFilename ${libraries})
81 string(REGEX MATCH "(.*).framework" _ ${libraryFilename})
82 string(CONCAT libraryName ${CMAKE_MATCH_1})
83 string(CONCAT frameworkLocation ${destination} "/" ${libraryFilename} "/" ${libraryName})
84 Execute(output otool -L ${frameworkLocation})
86 foreach (oldLocation ${output})
87 string(REGEX REPLACE " \\\(compatibility .*" "" oldLocation ${oldLocation})
88 if(oldLocation MATCHES "^@rpath")
89 string(REPLACE "@rpath/" "@executable_path/" newLocation ${oldLocation})
91 COMMAND sh -c "install_name_tool -change ${oldLocation} ${newLocation} ${frameworkLocation}"
99 function(AdjustSFMLLibraries libraries)
100 foreach(library ${libraries})
101 Execute(output otool -L ${library})
103 foreach(oldLocation ${output})
104 string(REGEX REPLACE " \\\(compatibility .*" "" oldLocation ${oldLocation})
105 if(oldLocation MATCHES "^@rpath")
106 string(REPLACE "@rpath/" "@executable_path/" newLocation ${oldLocation})
108 COMMAND sh -c "install_name_tool -change ${oldLocation} ${newLocation} ${library}"
116 function(GatherLibrariesForCopying source)
117 if (CMAKE_HOST_SYSTEM_NAME MATCHES "Darwin")
118 # Gather runtime paths first.
119 if(NOT source MATCHES "^@rpath")
120 GatherRuntimePaths(${source})
123 Execute(output otool -L ${source})
125 get_filename_component(LibraryName "${source}" NAME)
127 if(LibraryName MATCHES ".*dylib")
128 string(PREPEND LibraryName "${DST}/")
130 set(LibraryName "${source}")
133 foreach (line ${output})
134 if(line MATCHES "^@rpath/../Frameworks/.*.framework/Versions/.*/.*")
138 if(line MATCHES "^@rpath")
139 string(REGEX REPLACE "^@rpath/" "" filename ${line})
140 string(REGEX REPLACE "dylib .*" "dylib" filename ${filename})
143 foreach (runpath ${runpaths})
144 string(CONCAT dylib_location ${runpath} "/" ${filename})
145 if (EXISTS ${dylib_location})
146 set(dylib_location ${dylib_location} PARENT_SCOPE)
152 if(dylib_found EQUAL 0)
153 message(FATAL_ERROR "Unable to find dynamic library ${filename} within the runtime paths.")
158 if(line MATCHES "^.*libwx.*\\.dylib " )
159 string(REGEX REPLACE "dylib .*" "dylib" line "${line}")
160 if(NOT line STREQUAL "${source}" AND NOT line MATCHES "@executable")
162 if(line MATCHES "^@rpath/")
163 list(APPEND libs ${dylib_location})
165 list(APPEND libs ${lib})
167 get_filename_component(refname "${lib}" NAME)
168 list(APPEND postcmds "sh -c 'install_name_tool -change ${lib} @executable_path/../Frameworks/${refname} ${LibraryName}'")
170 GatherLibrariesForCopying(${lib})
172 elseif(line MATCHES "^.*libsfml.*.[^0-9].[^0-9].[^0-9].*\\.dylib ")
173 string(REGEX REPLACE "dylib .*" "dylib" line "${line}")
175 if(line MATCHES "^@rpath/")
176 get_filename_component(refname "${dylib_location}" NAME)
177 list(APPEND postcmds "sh -c 'install_name_tool -change ${lib} @executable_path/../Frameworks/${refname} ${LibraryName}'")
178 list(APPEND libs ${dylib_location})
179 elseif(line MATCHES "^@executable_path/")
182 get_filename_component(refname "${lib}" NAME)
183 list(APPEND postcmds "sh -c 'install_name_tool -change ${lib} @executable_path/../Frameworks/${refname} ${LibraryName}'")
184 list(APPEND libs ${lib})
186 get_filename_component(libraryPath "${LibraryName}" DIRECTORY)
187 string(APPEND libraryPath "/../Frameworks/${refname}")
188 list(APPEND sfmllibs ${libraryPath})
189 elseif(line MATCHES "^.*libcurl.[0-9]\\.dylib " OR
190 line MATCHES "^.*libxml2.[0-9]\\.dylib " OR
191 line MATCHES "^.*libsqlite3.[0-9]\\.dylib ")
192 string(REGEX REPLACE "dylib .*" "dylib" line "${line}")
195 if(line MATCHES "^@rpath/")
196 get_filename_component(refname "${dylib_location}" NAME)
197 list(APPEND postcmds "sh -c 'install_name_tool -change ${lib} @executable_path/../Frameworks/${refname} ${LibraryName}'")
198 list(APPEND libs ${dylib_location})
199 elseif(line MATCHES "^@executable_path/")
202 get_filename_component(refname "${lib}" NAME)
203 list(APPEND postcmds "sh -c 'install_name_tool -change ${lib} @executable_path/../Frameworks/${refname} ${LibraryName}'")
204 list(APPEND libs ${lib})
210 set(libs ${libs} PARENT_SCOPE)
211 set(sfmllibs ${sfmllibs} PARENT_SCOPE)
212 set(postcmds ${postcmds} PARENT_SCOPE)
215 GatherLibrariesForCopying(${SRC})
217 list(REMOVE_DUPLICATES libs)
218 file(INSTALL ${libs} DESTINATION ${DST} FOLLOW_SYMLINK_CHAIN)
220 GatherSFMLLibrariesForCopying("${sfmllibs}" "${postcmds}")
221 InstallSFMLDependencies("${frameworksToCopy}" "${postcmds}" "${DST}")
223 foreach(cmd ${postcmds})
225 COMMAND sh -c "${cmd}"
230 AdjustSFMLLibraries("${sfmllibs}")
231 AdjustSFMLDependencies("${frameworksToCopy}" ${DST})