-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathfunctions
More file actions
178 lines (161 loc) · 4.94 KB
/
functions
File metadata and controls
178 lines (161 loc) · 4.94 KB
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
function toupper {
echo "$*" | tr '[a-z]' '[A-Z]'
}
# Check if switch $1 == $2
function _test_sw {
[[ "x$1" == "x$2" ]]
}
# Check if switch $1 matches regexp $2
function _test_re {
# it's important not to quote $2 here!
[[ "x$1" =~ x$2 ]]
}
# Used by replace_cxxstd, remove_cxxstd
CXXSTD_REGEXP="[-/]std(=|:)c\+\+[0-9][0-9]"
# Search $3... for a switch matching $2 using $1 (_test_* above)
# If found, echo index (0 for $3, 1 for $4...) and return 0.
# Otherwise return 1.
function _find {
local tester="$1"
local sought="$2"
local i
shift 2
for (( i=1 ; i <= $# ; i=i+1 ))
do
eval "local sw=\${$i}"
if $tester "$sw" "$sought"
then
echo $(( i - 1 ))
return 0
fi
done
return 1
}
# Search $3... using $1 (_test_* above) for $2 and remove it
function _remove {
local tester="$1"
local todel="$2"
shift 2
local out=("$@")
local idx
idx=$(_find $tester "$todel" "$@") && unset out\[$idx\]
echo "${out[@]}"
}
# Search $4... using $1 (_test_* above) for $2 and replace it with $3
function _replace {
local tester="$1"
local todel="$2"
local toins="$3"
shift 3
local out=("$@")
local idx
idx=$(_find $tester "$todel" "$@") && out[$idx]="$toins"
echo "${out[@]}"
}
# Usage:
# switches="$(remove_switch -DPIC $LL_BUILD)"
# It's important NOT to quote whichever compiler-arguments string you pass to
# remove_switch (LL_BUILD in the example above), just as it's important not to
# quote it when passing it to the compiler itself: bash must parse into
# separate tokens.
function remove_switch {
_remove _test_sw "$@"
}
# Usage:
# switches="$(remove_match regexp $LL_BUILD)"
# This is a special case of remove_switch that removes a regular expression.
function remove_match {
_remove _test_re "$@"
}
# Usage:
# switches="$(remove_cxxstd $LL_BUILD)"
# This is a special case of remove_match that removes -std=c++NN
# (or /std=c++NN, as for Microsoft)
function remove_cxxstd {
remove_match "$CXXSTD_REGEXP" "$@"
}
# Usage:
# switches="$(replace_switch -DPIC -DPOC $LL_BUILD)"
# It's important NOT to quote whichever compiler-arguments string you pass to
# replace_switch (LL_BUILD in the example above), just as it's important not to
# quote it when passing it to the compiler itself: bash must parse into
# separate tokens.
function replace_switch {
_replace _test_sw "$@"
}
# Usage:
# switches="$(replace_match regexp $LL_BUILD)"
# This is a special case of replace_switch that replaces a regular expression.
function replace_match {
_replace _test_re "$@"
}
# Usage:
# switches="$(replace_cxxver -std=c++20 $LL_BUILD)"
# This is a special case of replace_match that replaces -std=c++NN
# (or /std=c++NN, as for Microsoft)
function replace_cxxstd {
replace_match "$CXXSTD_REGEXP" "$@"
}
# Usage:
# cmake ... $(cmake_cxx_standard $LL_BUILD) ...
# If the passed set of switches contains -std=c++NN (or /std=c++NN), echo
# -DCMAKE_CXX_STANDARD=NN to cause CMake to set the relevant switch itself.
# Otherwise echo nothing.
function cmake_cxx_standard {
local idx
if idx=$(_find _test_re "$CXXSTD_REGEXP" "$@")
then
# Mac bash 3 doesn't support negative indexing from right end
eval "local sw=\${$((idx+1))}"
local veridx=$(( ${#sw} - 2 ))
echo "-DCMAKE_CXX_STANDARD=${sw:$veridx}"
fi
}
# To fix this (serious!) problem:
# clang: warning: overriding '-mmacosx-version-min=10.13' option
# with '-target x86_64-apple-macos11.7' [-Woverriding-t-option]
# Usage:
# switches="$(set_target $LL_BUILD)"
# If the argument list contains '-mmacosx-version-min=VVVV', find or add
# '-target x86_64-apple-macosVVVV'.
function set_target {
local opts=("$@")
local idx
if idx=$(_find _test_re "-mmacosx-version-min=.*" "${opts[@]}")
then
local versw="${opts[$idx]}"
local minver="${versw#*=}"
local target="x86_64-apple-macos$minver"
if idx=$(_find _test_sw "-target" "${opts[@]}")
then
# we already have a -target switch; replace its value
(( idx+=1 ))
opts[$idx]="$target"
else
# we don't already have a -target switch: add it
opts+=(-target "$target")
fi
local TARGET="-DCMAKE_OSX_DEPLOYMENT_TARGET="
if idx=$(_find _test_re "$TARGET.*" "${opts[@]}")
then
# we already have a -D switch as above; replace it
opts[$idx]="$TARGET$minver"
else
# we don't already have a -D switch as above: add it
opts+=("$TARGET$minver")
fi
fi
echo "${opts[*]}"
}
apply_patch()
{
local patch="$1"
local path="$2"
# Fix paths for windows
if [[ "$OSTYPE" == "cygwin" || "$OSTYPE" == "msys" ]] ; then
patch="$(cygpath -m $patch)"
path="$(cygpath -m $path)"
fi
echo "Applying $patch..."
git apply --check --reverse --directory="$path" "$patch" || git apply --directory="$path" "$patch"
}