blob: d66a7a3e9258b0a9a4b85f151dde628732b20c25 [file] [log] [blame]
Gyorgy Szingfd03e0a2023-07-27 20:04:24 +02001#-------------------------------------------------------------------------------
2# Copyright (c) 2023, Arm Limited and Contributors. All rights reserved.
3#
4# SPDX-License-Identifier: BSD-3-Clause
5#
6#-------------------------------------------------------------------------------
7
8include_guard()
9
10#[===[.rst:
11RFC 4122 compatible UUID manipulation routines
12---------------------------------------------
13
14The functionality in this file allows manipulating (which mostly means conversion) of UUID strings
15to various formats used in the TS build system.
16
17#]===]
18
19#[===[.rst:
20.. cmake:command:: uuid_canon_to_octets
21
22 .. code-block:: cmake
23
24 uuid_canon_to_octets(UUID <canonical UUID string> RES <output variable name>)
25
26 Convert a canonical UUID string to list of bytes, where each byte is represented as a two digit
27 hex octet without any prefix of suffix. Order of bytes will match the order of octets in the
28 canonical string left to right.
29
30 INPUTS:
31
32 ``UUID``
33 Canonical UUID string.
34
35 OUTPUTS:
36
37 ``RES``
38 Name of variable to store the result to. The result is a list of strings, where each list item
39 is a two digit hex digit, without any prefix or suffix.
40
41#]===]
42function(uuid_canon_to_octets)
43 set(options)
44 set(oneValueArgs UUID RES)
45 set(multiValueArgs)
46 cmake_parse_arguments(_MY_PARAMS "${options}" "${oneValueArgs}"
47 "${multiValueArgs}" ${ARGN} )
48
49 check_args(UUID RES)
50
51 string(REGEX MATCHALL "([A-Za-z0-9][A-Za-z0-9])" _hex_bytes "${_MY_PARAMS_UUID}")
52 list(LENGTH _hex_bytes _len)
53 if(NOT _len EQUAL 16)
54 message(FATAL_ERROR "Failed to convert UUID \"${_MY_PARAMS_UUID}\" to bytes. Failed to get exactly 16 octets.")
55 endif()
56 set(${_MY_PARAMS_RES} ${_hex_bytes} PARENT_SCOPE)
57endfunction()
58
59#[===[.rst:
60.. cmake:command:: uuid_canon_to_fields
61
62 .. code-block:: cmake
63
64 uuid_canon_to_fields(UUID <canonical UUIdD string> TIME_LOW <output variable name> TIME_MID <output variable name>
65 TIME_HI_AND_VER <output variable name> CLOCK_AND_SEQ <output variable name>)
66
67 Convert a canonical UUID string to UUID fields. Each field is a
68
69 INPUTS:
70
71 ``UUID``
72 Canonical UUID string.
73
74 OUTPUTS:
75 ``TIME_LOW``
76 Name of variable to store the time low filed.
77
78 ``TIME_MID``
79 Name of variable to store the time mid filed.
80
81 ``TIME_HI_AND_VER``
82 Name of variable to store the time hi and version filed.
83
84 ``CLOCK_AND_SEQ``
85 Name of variable to store the clock and sequence filed.
86
87#]===]
88function(uuid_canon_to_fields)
89 set(options)
90 set(oneValueArgs UUID TIME_LOW TIME_MID TIME_HI_AND_VER CLOCK_AND_SEQ)
91 set(multiValueArgs)
92 cmake_parse_arguments(_MY_PARAMS "${options}" "${oneValueArgs}"
93 "${multiValueArgs}" ${ARGN} )
94
95 check_args(UUID TIME_LOW TIME_MID TIME_HI_AND_VER CLOCK_AND_SEQ)
96 uuid_canon_to_octets(UUID ${_MY_PARAMS_UUID} RES _uuid_octets)
97
98 #Split the list of bytes in to the struct fields
99 list(SUBLIST _uuid_octets 0 4 _uuid_timeLow)
100 list(JOIN _uuid_timeLow "" _uuid_timeLow)
101
102 list(SUBLIST _uuid_octets 4 2 _uuid_timeMid)
103 list(JOIN _uuid_timeMid "" _uuid_timeMid)
104
105 list(SUBLIST _uuid_octets 6 2 _uuid_timeHiAndVersion)
106 list(JOIN _uuid_timeHiAndVersion "" _uuid_timeHiAndVersion)
107
108 list(SUBLIST _uuid_octets 8 8 _uuid_clockSeqAndNode)
109 list(JOIN _uuid_clockSeqAndNode "" _uuid_clockSeqAndNode)
110
111 set(${_MY_PARAMS_TIME_LOW} ${_uuid_timeLow} PARENT_SCOPE)
112 set(${_MY_PARAMS_TIME_MID} ${_uuid_timeMid} PARENT_SCOPE)
113 set(${_MY_PARAMS_TIME_HI_AND_VER} ${_uuid_timeHiAndVersion} PARENT_SCOPE)
114 set(${_MY_PARAMS_CLOCK_AND_SEQ} ${_uuid_clockSeqAndNode} PARENT_SCOPE)
115endfunction()
116
117#[===[.rst:
118.. cmake:command:: uuid_canon_to_le_words
119
120 .. code-block:: cmake
121
122 uuid_canon_to_le_words(UUID <canonical UUID string> RES <output variable name>)
123
124 Convert a canonical UUID string to list of 32bit wide little-endian numbers represented
125 as hex strings.
126
127 INPUTS:
128
129 ``UUID``
130 Canonical UUID string.
131
132 ``RES``
133 Name of variable to store the result to.
134
135#]===]
136function(uuid_canon_to_le_words)
137 set(options)
138 set(oneValueArgs UUID RES)
139 set(multiValueArgs)
140 cmake_parse_arguments(_MY_PARAMS "${options}" "${oneValueArgs}"
141 "${multiValueArgs}" ${ARGN} )
142
143 check_args(UUID RES)
144 uuid_canon_to_octets(UUID ${_MY_PARAMS_UUID} RES _uuid_octets)
145
146 # Separate 32 bit chunks
147 list(SUBLIST _uuid_octets 0 4 _word1)
148 list(SUBLIST _uuid_octets 4 4 _word2)
149 list(SUBLIST _uuid_octets 8 4 _word3)
150 list(SUBLIST _uuid_octets 12 4 _word4)
151
152 # Reverse octet order each word
153 list(REVERSE _word1)
154 list(REVERSE _word2)
155 list(REVERSE _word3)
156 list(REVERSE _word4)
157
158 # Concatenate octets of each word to a single string
159 list(JOIN _word1 "" _word1)
160 list(JOIN _word2 "" _word2)
161 list(JOIN _word3 "" _word3)
162 list(JOIN _word4 "" _word4)
163
164 # Return the result
165 set(${_MY_PARAMS_RES} "${_word1}" "${_word2}" "${_word3}" "${_word4}" PARENT_SCOPE)
166endfunction()