forked from google/rust_icu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_bindgen.sh
executable file
·192 lines (170 loc) · 5.23 KB
/
run_bindgen.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#! /bin/bash
# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# A script that updates the bindgen library manually.
#
# Please refer to the file README.md
#
# Requirements:
# - bash
# - awk
# - tr
# - llvm-dev package (for llvm-config that bindgen needs)
set -eo pipefail
# The directory into which the rust bindings file will be written. If left
# unset, the current directory is the default.
OUTPUT_DIR=${OUTPUT_DIR:-.}
# The list of unicode source headers to generate bindings for. This list is
# intended to be kept in sync with the static variable by the same name in the
# build.rs file.
readonly BINDGEN_SOURCE_MODULES=(
"ubrk"
"ucal"
"uclean"
"ucol"
"udat"
"udatpg"
"udata"
"uenum"
"ufieldpositer"
"uformattable"
"ulistformatter"
"umisc"
"umsg"
"unum"
"unumberformatter"
"upluralrules"
"uset"
"ustring"
"utext"
"utrans"
)
# Types for which to generate the bindings. Expand this list if you need more.
# The syntax is regex. This list is intended to be kept in sync with the static
# variable by the same name in the build.rs file.
readonly BINDGEN_ALLOWLIST_TYPES=(
"UAcceptResult"
"UBool"
"UBreakIterator"
"UBreakIteratorType"
"UCalendar.*"
"UChar.*"
"UCol.*"
"UCollation.*"
"UCollator"
"UData.*"
"UDate.*"
"UDateTime.*"
"UDateFormat.*"
"UDisplayContext.*"
"UEnumeration.*"
"UErrorCode"
"UField.*"
"UFormat.*"
"UFormattedList.*"
"ULOC.*"
"ULineBreakTag"
"UListFormatter.*"
"ULoc.*"
"UMessageFormat"
"UNUM.*"
"UNumber.*",
"UParseError"
"UPlural.*"
"USentenceBreakTag"
"USet"
"UText"
"UTransDirection"
"UTransPosition"
"UTransliterator"
"UWordBreak"
)
# Functions for which to generate the bindings. Expand this list if you need
# more. This list is intended to be kept in sync with the static variable by
# the same name in the build.rs file.
readonly BINDGEN_ALLOWLIST_FUNCTIONS=(
"u_.*"
"ubrk_.*"
"ucal_.*"
"ucol_.*"
"udat_.*"
"udatpg_.*"
"udata_.*"
"uenum_.*"
"ufieldpositer_.*"
"ufmt_.*"
"ulistfmt_.*"
"uloc_.*"
"umsg_.*"
"unum_.*"
"unumf_.*"
"uplrules_.*"
"utext_.*"
"utrans_.*"
)
function check_requirements() {
icu-config --version &> /dev/null || \
(echo "The generator requires icu-config to be in PATH; see README.md"; exit 1)
bindgen --version &> /dev/null || \
(echo "The generator requires bindgen to be in PATH; see README.md"; exit 1)
awk --version &> /dev/null || \
(echo "The generator requires awk to be installed; see README.md"; exit 1)
llvm-config --version &> /dev/null || \
(echo "The generator requires llvm-config (package llvm-dev) to be installed; see README.md"; exit 1)
}
# Generates a temporary header file to be supplied to bindgen for binding generation.
# The file is automatically removed upon exit, so if you are debugging you may want
# to remove that part.
function generate_header_file() {
MAIN_HEADER_FILE="$(mktemp --suffix=.h)"
echo "MAIN_HEADER_FILE=${MAIN_HEADER_FILE}"
trap "rm ${MAIN_HEADER_FILE}" EXIT
readonly ICU_INCLUDE_DIR="$(icu-config --prefix)/include/unicode"
echo "// Automatically generated by run_bindgen.sh, DO NOT EDIT. " > ${MAIN_HEADER_FILE}
for module in ${BINDGEN_SOURCE_MODULES[@]}; do
echo "#include \"${ICU_INCLUDE_DIR}/${module}.h\"" >> "${MAIN_HEADER_FILE}"
done
}
function main() {
check_requirements
generate_header_file
# Joins all with a |, so ("a" "b") become "a|b"
local _allowlist_types_concat="$(\
echo ${BINDGEN_ALLOWLIST_TYPES[@]} | tr ' ' '|')"
local _functions_concat="$(\
echo ${BINDGEN_ALLOWLIST_FUNCTIONS[@]} | tr ' ' '|')"
set -x
# Example: "67.1", "66.0.1"
local _icu_version="$(icu-config --version)"
# Example: "67"
local _icu_version_major="${_icu_version%.*}"
local _icu_version_major="${_icu_version_major%.*}"
# Respectful code hack.
local _allowlist="$(echo "d2hpdGVsaXN0Cg==" | base64 -d -)"
bindgen \
--default-enum-style=rust \
--no-doc-comments \
--with-derive-default \
--with-derive-hash \
--with-derive-partialord \
--with-derive-partialeq \
--"${_allowlist}"-type="${_allowlist_types_concat}" \
--"${_allowlist}"-function="${_functions_concat}" \
--opaque-type="" \
--output="${OUTPUT_DIR}/lib_${_icu_version_major}.rs" \
"${MAIN_HEADER_FILE}" \
-- \
$(icu-config --cppflags)
}
main