Debian Codegraph @

src/vendor/azure-uamqp-c/deps/azure-c-shared-utility/tools/mbed_build_scripts/mbedbldtemplate.txt

raw
1 #Copyright (c) Microsoft. All rights reserved.
2 #Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
4 ### This option would be used by build scripts so to release a project directly to the mbed repo sights. No compiles
5 ### would occur.
6 ###
7 ### To use add -Drelease_the_project:bool=ON
8 option(release_the_project "Used to just push the project and adjust the appending of _bld" off)
9
10 cmake_minimum_required(VERSION 2.8.11)
11 set(HG_COMMIT_MSG "Automatic Build Commit" CACHE STRING "Passed to the mercurial commit")
12
13 project(${mbed_project_base})
14
15 ### Functions
16 function(verifyRequiredParameters)
17 set(ALL_PARAMETERS_PROVIDED TRUE)
18
19 if(NOT DEFINED ENV{MBED_HG_USER})
20 message(WARNING "MBED_HG_USER environment variable not defined.")
21 set(ALL_PARAMETERS_PROVIDED FALSE)
22 endif()
23
24 if(NOT DEFINED ENV{MBED_USER})
25 message(WARNING "MBED_USER environment variable not defined.")
26 set(ALL_PARAMETERS_PROVIDED FALSE)
27 endif()
28
29 if(NOT DEFINED ENV{MBED_PWD})
30 message(WARNING "MBED_PWD environment variable not defined.")
31 set(ALL_PARAMETERS_PROVIDED FALSE)
32 endif()
33
34 if (NOT ${ALL_PARAMETERS_PROVIDED})
35 message(FATAL_ERROR "Not all expected parameters provided. Please provide the parameters listed above.")
36 endif()
37 endfunction(verifyRequiredParameters)
38
39
40 function(verifyTarIsInstalled)
41 execute_process(COMMAND tar "--version" WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} RESULT_VARIABLE result_code OUTPUT_QUIET)
42
43 if(WIN32)
44 if(${result_code} MATCHES "The system cannot find the file specified")
45 message(FATAL_ERROR "Please install 'tar' and make it available on $PATH.")
46 endif()
47 endif()
48 endfunction(verifyTarIsInstalled)
49
50
51 function(verifyMercurialIsInstalled)
52 execute_process(COMMAND hg "--version" WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} RESULT_VARIABLE result_code OUTPUT_QUIET)
53
54 if(WIN32)
55 if(${result_code} MATCHES "The system cannot find the file specified")
56 message(FATAL_ERROR "Please install 'Mercurial' and make it available on $PATH.")
57 endif()
58 endif()
59 endfunction(verifyMercurialIsInstalled)
60
61
62 function(verifyCompileMbedTool)
63 if(WIN32)
64 file(GLOB expanded_compile_mbed_tool ${compile_mbed_tool})
65 if (NOT EXISTS ${expanded_compile_mbed_tool})
66 message(FATAL_ERROR "Required tool " ${expanded_compile_mbed_tool} " could not be found. Please compile it first.")
67 endif()
68 endif()
69 endfunction(verifyCompileMbedTool)
70
71
72 function(verifyRequiredTools)
73 verifyMercurialIsInstalled()
74 verifyCompileMbedTool()
75 endfunction(verifyRequiredTools)
76
77
78 function(cloneMBEDRepository)
79 set(mbed_repo_address https://$ENV{MBED_USER}:$ENV{MBED_PWD}@os.mbed.com/users/$ENV{MBED_USER}/code/${mbed_repo_name}/)
80
81 message(STATUS "Trying to delete " ${local_repo_path})
82 message(STATUS "Trying to delete " ${local_sublib_dir})
83 message(STATUS "Trying to clone " ${mbed_repo_address})
84
85 if (EXISTS ${local_sublib_dir})
86 message(STATUS "Removing old sublib directory: " ${local_sublib_dir})
87 file(REMOVE_RECURSE ${local_sublib_dir})
88
89 if(EXISTS ${local_subdir_path})
90 message(FATAL_ERROR "Failed removing sublib directory: " ${local_sublib_dir})
91 endif()
92 endif()
93 file(MAKE_DIRECTORY ${local_sublib_dir})
94 if(EXISTS ${local_repo_path})
95 message(STATUS "Removing existing cloned directory.")
96 file(REMOVE_RECURSE ${local_repo_path})
97
98 if(EXISTS ${local_repo_path})
99 message(FATAL_ERROR "Failed removing existing repository clone.")
100 endif()
101 endif()
102
103
104 message(STATUS "Cloning MBED code.")
105 execute_process(COMMAND hg "clone" "--insecure" ${mbed_repo_address} WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} RESULT_VARIABLE result_code)
106
107 if(NOT ${result_code} EQUAL 0)
108 message(FATAL_ERROR "Failed cloning MBED repository '" ${mbed_repo_name} "' (error code " ${result_code} ")")
109 endif()
110 endfunction(cloneMBEDRepository)
111
112 function(updateMBEDSourceFiles)
113 set(source_file_destination ${local_repo_path})
114 set(exported_file_destination ${local_repo_path}/${mbed_project_base})
115 #
116 # go through and delete every non .lib .hg .bld file in the directory. Don't delete the exported file directory though!!!
117 #
118 file(GLOB existingFiles ${local_repo_path}/*)
119 foreach (file_item ${existingFiles})
120 STRING(FIND ${file_item} ".lib" string_with_lib)
121 STRING(FIND ${file_item} ".bld" string_with_bld)
122 STRING(FIND ${file_item} ".hg" string_with_hg)
123 #
124 # We try to see if the we have the subdirectory of the exported files. We don't want to delete that directory
125 # basically we are detecting if the subdirectory name is the very last thing in the file name.
126 #
127 STRING(FIND ${file_item} ${mbed_project_base} string_with_base REVERSE)
128 if (NOT (${string_with_base} EQUAL -1))
129 STRING(LENGTH ${file_item} file_item_length)
130 STRING(LENGTH ${mbed_project_base} mbed_project_base_length)
131 math(EXPR offset_of_end_of_base "${string_with_base} + ${mbed_project_base_length}")
132 if (NOT (${offset_of_end_of_base} EQUAL ${file_item_length}))
133 set(string_with_base -1)
134 endif()
135 endif()
136 if ((${string_with_lib} EQUAL -1) AND
137 (${string_with_hg} EQUAL -1) AND
138 (${string_with_bld} EQUAL -1) AND
139 (${string_with_base} EQUAL -1))
140 file(REMOVE ${file_item})
141 endif()
142 endforeach()
143 #
144 # go through and delete every non .lib .hg .bld file in the exported file directory
145 #
146 file(GLOB existingFiles ${local_repo_path}/${mbed_project_base}/*)
147 foreach (file_item ${existingFiles})
148 STRING(FIND ${file_item} ".lib" string_with_lib)
149 STRING(FIND ${file_item} ".bld" string_with_bld)
150 STRING(FIND ${file_item} ".hg" string_with_hg)
151 STRING(FIND ${file_item} ${mbed_project_base} string_with_base REVERSE)
152 #
153 # We try to see if the we have the subdirectory of the exported files. We don't want to delete that directory
154 # basically we are detecting if the subdirectory name is the very last thing in the file name.
155 #
156 if (NOT (${string_with_base} EQUAL -1))
157 STRING(LENGTH ${file_item} file_item_length)
158 STRING(LENGTH ${mbed_project_base} mbed_project_base_length)
159 math(EXPR offset_of_end_of_base "${string_with_base} + ${mbed_project_base_length}")
160 if (NOT (${offset_of_end_of_base} EQUAL ${file_item_length}))
161 set(string_with_base -1)
162 endif()
163 endif()
164 if ((${string_with_lib} EQUAL -1) AND
165 (${string_with_hg} EQUAL -1) AND
166 (${string_with_bld} EQUAL -1))
167 file(REMOVE ${file_item})
168 endif()
169 endforeach()
170
171 set(curr_destination)
172
173 message(STATUS "Copying target project files.")
174 foreach(file_item ${mbed_project_files})
175 string(FIND ${file_item} "/internal/" position REVERSE)
176 if (position EQUAL -1)
177 set(curr_destination ${source_file_destination})
178 else ()
179 # There is an internal directory, set the curr_destination accordingly
180 set(curr_destination "${source_file_destination}/internal/")
181 endif ()
182
183 file(GLOB expanded_file_item ${file_item})
184 message(STATUS ${expanded_file_item})
185 file(COPY ${expanded_file_item} DESTINATION ${curr_destination})
186 endforeach()
187
188 foreach(file_item ${mbed_exported_project_files})
189 string(FIND ${file_item} "/internal/" position REVERSE)
190 if (position EQUAL -1)
191 set(curr_destination ${exported_file_destination})
192 else ()
193 # There is an internal directory, set the curr_destination accordingly
194 set(curr_destination "${exported_file_destination}/internal/")
195 endif ()
196
197 file(GLOB expanded_file_item ${file_item})
198 message(STATUS ${expanded_file_item})
199 file(COPY ${expanded_file_item} DESTINATION ${curr_destination})
200 endforeach()
201
202 endfunction(updateMBEDSourceFiles)
203
204 function(updateMBEDLibraries)
205
206 file(GLOB libfiles ${local_repo_path}/*.lib)
207 foreach(file_item ${libfiles})
208
209 #
210 # We go through all of the .lib files. We only want to update what we own.
211 #
212 file (READ ${file_item} fileContents)
213 STRING(FIND ${fileContents} /$ENV{MBED_USER}/ matchResult)
214 if (NOT ${matchResult} EQUAL -1)
215
216 #
217 # Found one. We want to get the the clone path of the library. Go up to the slash and pound sign. (The sha follows that.)
218 #
219 STRING(FIND ${fileContents} /\# uriEnd)
220 if (NOT ${uriEnd} EQUAL -1)
221 STRING(SUBSTRING ${fileContents} 0 ${uriEnd} libraryUri)
222
223 #
224 # Get the sha that exists in the file. We'll compare after the clone to see if we need to change the file.
225 # This is because even if we have the same sha, we could change the whitespace and this will cause hg to think the file has
226 # changed.
227
228 math(EXPR beginSha "${uriEnd} + 2}")
229 STRING(SUBSTRING ${fileContents} ${beginSha} -1 rawOldSha)
230 STRING(STRIP ${rawOldSha} oldSha)
231
232 #
233 # Get the name that the clone will go to. (This is the last part of the uri).
234 #
235 STRING(FIND ${libraryUri} / finalSlash REVERSE)
236 math(EXPR finalSlash "${finalSlash} + 1")
237 STRING(SUBSTRING ${libraryUri} ${finalSlash} -1 libraryName)
238
239 #
240 # Our compile sandboxes are actually private. So we need to pass the username and password to do the clone.
241 # So we actually form the address all over again. Sigh.
242 #
243 set(subLibraryCloningUri https://$ENV{MBED_USER}:$ENV{MBED_PWD}@os.mbed.com/users/$ENV{MBED_USER}/code/${libraryName}/)
244
245 #
246 # We got the parts we need. Clone the lib and then go get the sha.
247 #
248 execute_process(COMMAND hg "clone" "--insecure" ${subLibraryCloningUri} WORKING_DIRECTORY ${local_sublib_dir} RESULT_VARIABLE result_code)
249 if(NOT ${result_code} EQUAL 0)
250 message(FATAL_ERROR "Failed to clone the sub library " ${libraryName} " (error code " ${result_code} ")")
251 endif()
252 execute_process(COMMAND hg "id" "-i" WORKING_DIRECTORY ${local_sublib_dir}/${libraryName} RESULT_VARIABLE result_code OUTPUT_VARIABLE outputSha OUTPUT_STRIP_TRAILING_WHITESPACE)
253 if(NOT ${result_code} EQUAL 0)
254 message(FATAL_ERROR "Failed to obtain the sha of the sublibrary" ${libraryName} "(error code " ${result_code} ")")
255 endif()
256
257 if (NOT ${outputSha} STREQUAL ${oldSha})
258 message(STATUS "Need to update the reference to libary at " ${libraryUri})
259 file(WRITE ${file_item} ${libraryUri} /\# ${outputSha})
260 endif()
261 else()
262 message(FATAL "Improperly formed lib file: " ${file_item})
263 endif()
264 endif()
265 endforeach()
266 endfunction(updateMBEDLibraries)
267
268 function(pushMBEDRepository)
269
270 set(mbed_repo_address https://$ENV{MBED_USER}:$ENV{MBED_PWD}@os.mbed.com/users/$ENV{MBED_USER}/code/${mbed_repo_name}/)
271
272 message(STATUS "Commiting changes to local repository.")
273
274 execute_process(COMMAND hg "addremove" WORKING_DIRECTORY ${local_repo_path} RESULT_VARIABLE result_code)
275 if(NOT ${result_code} EQUAL 0)
276 message(FATAL_ERROR "Failed adding/removing local changes (error code " ${result_code} ")")
277 endif()
278
279 execute_process(COMMAND hg "commit" "-u" $ENV{MBED_HG_USER} "-m ${HG_COMMIT_MSG}" WORKING_DIRECTORY ${local_repo_path} RESULT_VARIABLE result_code)
280 if(NOT ${result_code} EQUAL 0 AND NOT ${result_code} EQUAL 1)
281 message(FATAL_ERROR "Failed commiting changes to local repository (error code " ${result_code} ")")
282 endif()
283
284
285 message(STATUS "Pushing changes to repo " ${mbed_repo_name})
286 execute_process(COMMAND hg "push" "--insecure" ${mbed_repo_address} WORKING_DIRECTORY ${local_repo_path} RESULT_VARIABLE result_code)
287
288 if(NOT ${result_code} EQUAL 0 AND NOT ${result_code} EQUAL 1)
289 message(FATAL_ERROR "Failed pushing changes to remote MBED repository '" ${mbed_repo_name} "' (error code " ${result_code} ")")
290 endif()
291 endfunction(pushMBEDRepository)
292
293
294 function(compileMBEDProject)
295 set(mbed_repo_address http://os.mbed.com/users/$ENV{MBED_USER}/code/${mbed_repo_name}/)
296
297 file(GLOB expanded_compile_mbed_tool ${compile_mbed_tool})
298 if (DEFINED mbed_output_bin_path AND NOT ${mbed_output_bin_path} EQUAL "")
299 execute_process(COMMAND ${expanded_compile_mbed_tool} -un $ENV{MBED_USER} -pwd $ENV{MBED_PWD} -r ${mbed_repo_address} -plat ${mbed_platform} -o ${mbed_output_bin_path} WORKING_DIRECTORY . RESULT_VARIABLE result_code)
300 else()
301 execute_process(COMMAND ${expanded_compile_mbed_tool} -un $ENV{MBED_USER} -pwd $ENV{MBED_PWD} -r ${mbed_repo_address} -plat ${mbed_platform} WORKING_DIRECTORY . RESULT_VARIABLE result_code)
302 endif()
303
304 if(NOT ${result_code} EQUAL 0)
305 message(FATAL_ERROR "Failed compiling MBED repository '" ${mbed_repo_name} "' (error code " ${result_code} ")")
306 endif()
307 endfunction(compileMBEDProject)
308
309 function(releaseMBEDRepository)
310 verifyRequiredParameters()
311 verifyMercurialIsInstalled()
312 cloneMBEDRepository()
313 updateMBEDLibraries()
314 updateMBEDSourceFiles()
315 pushMBEDRepository()
316 endfunction(releaseMBEDRepository)
317
318 function(compileMBEDRepository)
319 releaseMBEDRepository()
320 compileMBEDProject()
321 endfunction(compileMBEDRepository)
322
323 ###
324 ### The compilation environment had the normal project base name with an appended _bld They will be constantly overwritten.
325 if (release_the_project)
326 set(mbed_repo_name ${mbed_project_base} CACHE STRING "What will be cloned")
327 else()
328 set(mbed_repo_name ${mbed_project_base}_bld CACHE STRING "What will be cloned")
329 endif()
330
331 set(local_repo_path ${CMAKE_CURRENT_BINARY_DIR}/${mbed_repo_name})
332 set(local_sublib_dir ${CMAKE_CURRENT_BINARY_DIR}/temp)
333 set(compile_mbed_tool ${shared_util_base_path}/tools/compilembed/bin/release/compilembed.exe)
334
335 set(mbed_platform "FRDM-K64F")
336
337 # seperate out the file lists so that they can used by the release cmake
338 include (${mbed_project_base}_filelist.txt)
339
340 ### Main
341 if(WIN32)
342 if (${release_the_project})
343 releaseMBEDRepository()
344 else ()
345 compileMBEDRepository()
346 endif()
347 else()
348 message(FATAL_ERROR "Apologies, but the only currently supported platform is Microsoft Windows.")
349 endif()