-
Notifications
You must be signed in to change notification settings - Fork 15
/
mk_hwtype.sh
executable file
·373 lines (348 loc) · 11.3 KB
/
mk_hwtype.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#!/bin/bash
### Configuration vars
# Top level folders that might contain hardware type subfolders
TOP_DIRS_WITH_HW_SUBDIRS='files kernel libs toolchains'
# Support packages custom files patterns ($HW_TYPE - hardware type)
SUP_PKG_FILES_PAT='config-$HW_TYPE config-$HW_TYPE.h options_$HW_TYPE.h unum-$HW_TYPE.mk'
### Script code
START_DIR="$PWD"
MY_NAME=$(basename "$0")
MY_DIR=$(dirname "$0")
cd "$MY_DIR"
ROOT_DIR="$PWD"
_onexit() {
cd "$START_DIR"
}
trap _onexit EXIT SIGHUP SIGINT SIGTERM
if [ ! -d ./src/unum ]; then
echo "Unable to find the ./src/unum folder!"
exit 1;
fi
HW_TYPES=`ls -1 ./src/unum/unum-*.mk | sed -e 's:\./src/unum/unum-\(.*\).mk:\1:'`
if [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
echo "Usage: $MY_NAME [-l] <TARGET_TYPE> <SOURCE_TYPE>"
echo " $MY_NAME [-R] <TARGET_TYPE> <SOURCE_TYPE>"
echo " $MY_NAME [-D] <TARGET_TYPE>"
echo " $MY_NAME [-X] <DIR> <TARGET_TYPE>"
echo "TARGET_TYPE - target hardware type to create or delete"
echo "SOURCE_TYPE - hardware type to use as the source"
echo "-l - make lightweight (symlinks-only) hardware type"
echo "-R - rename hardware type (note the order of the names)"
echo "-D - delete the target hardware type"
echo "-X <DIR> - export the target hardware type to DIR"
echo "-h/--help - this help"
echo "Currently available hardware types:"
echo "$HW_TYPES"
exit 1
fi
# Normal hardware type (i.e. copy over, not symlink)
LWT=0
# Adding HW type (not deleting/cleaning up)
DEL=0
# Renaming HW type (not deleting/cleaning up)
REN=0
# external repo folder to export the hardware kind to
unset X_DIR
while [ "${1:0:1}" == "-" ]; do
case $1 in
-l)
echo "Lightweight (symlinked) platform"
export LWT=1
shift
;;
-D)
echo "Deleting hardware type"
export DEL=1
shift
;;
-R)
echo "Renaming hardware type"
export REN=1
shift
;;
-X)
echo "Exporting hardware type"
shift
export X_DIR="$1"
if [ ! -d "$X_DIR" ]; then
echo "Error, directory $X_DIR not found!"
exit 2
fi
shift
;;
*)
echo "Unrecognized option $1"
echo "See $MY_NAME -h output for help"
exit 2
;;
esac
done
T_TYPE="$1"
S_TYPE="$2"
if [ "$T_TYPE" == "" ]; then
echo "The target hardware type cannot be empty!"
exit 3
fi
if [ $DEL -ne 1 ] && [ "$X_DIR" == "" ]; then
if [ "$S_TYPE" == "" ]; then
echo "The source hardware type cannot be empty!"
exit 4
fi
if [[ ! $HW_TYPES =~ (^|[[:space:]])$S_TYPE($|[[:space:]]) ]]; then
echo "The hardware type \"$S_TYPE\" not found!"
exit 5
fi
else # deleting or exporting
if [ "$S_TYPE" != "" ]; then
echo "The source hardware type is not empty!"
exit 6
fi
# set S_TYPE to be the same
S_TYPE="$T_TYPE"
fi
# Walk through all the harware type specific folders and files
# These might have the hardware type subfolders
echo "Processing top level folders..."
for _DIR in $TOP_DIRS_WITH_HW_SUBDIRS; do
echo " Processing folder ./$_DIR"
if [ "$X_DIR" != "" ]; then
if [ -e "./$_DIR/$T_TYPE" ] || [ -L "./$_DIR/$T_TYPE" ]; then
echo " Exporting ./$_DIR/$T_TYPE to $X_DIR"
mkdir -p "$X_DIR/$_DIR"
cp -a "./$_DIR/$T_TYPE" "$X_DIR/$_DIR/$T_TYPE"
fi
elif [ $DEL -eq 1 ]; then
echo " Cleaning ./$_DIR/$T_TYPE"
rm -Rf "./$_DIR/$T_TYPE" 2>/dev/null 1>&2
elif [ -d "./$_DIR/$S_TYPE" ]; then
if [ -e "./$_DIR/$T_TYPE" ]; then
echo " Warning, skipping ./$_DIR/$T_TYPE, already exists!"
continue
fi
if [ $LWT -eq 1 ]; then
if [ -L "./$_DIR/$T_TYPE" ]; then
echo " Copying symlink ./$_DIR/$S_TYPE =>./$_DIR/$T_TYPE"
cp -a "./$_DIR/$S_TYPE" "./$_DIR/$T_TYPE"
else
echo " Creating symlink ./$_DIR/$T_TYPE -> $S_TYPE"
ln -s "$S_TYPE" "./$_DIR/$T_TYPE"
fi
else
if [ $REN -eq 1 ]; then
echo " Renaming folder ./$_DIR/$S_TYPE => ./$_DIR/$T_TYPE"
mv -T "./$_DIR/$S_TYPE" "./$_DIR/$T_TYPE"
else
echo " Copying folder ./$_DIR/$S_TYPE => ./$_DIR/$T_TYPE"
cp -a "./$_DIR/$S_TYPE" "./$_DIR/$T_TYPE"
fi
fi
fi
done
# These are various support packages that we compile from sources.
# Some of them contain hardware kind specifc files.
# The script has hardcoded patterns for matching those files,
# so it has to be updated if something new is added.
echo "Processing various packages under ./src..."
for _DIR in `find ./src/ -mindepth 1 -maxdepth 1 -type d`; do
(
echo " Changing directory to $_DIR"
cd "$_DIR"
for _FILE in $SUP_PKG_FILES_PAT; do
HW_TYPE="$S_TYPE"
FILE=`eval "echo $_FILE"`
HW_TYPE="$T_TYPE"
NEW_FILE=`eval "echo $_FILE"`
if [ ! -e "$FILE" ] && [ ! -L "$FILE" ]; then
continue
fi
if [ "$X_DIR" != "" ]; then
if [ -e "./$FILE" ] || [ -L "$FILE" ]; then
echo " Exporting ./$FILE to $X_DIR/$_DIR/$FILE"
mkdir -p `dirname "$X_DIR/$_DIR/$FILE"`
cp -a "./$FILE" "$X_DIR/$_DIR/$FILE"
fi
elif [ $DEL -eq 1 ]; then
echo " Cleaning up ./$FILE"
rm -f "./$FILE" 2>/dev/null 1>&2
elif [ -e "./$NEW_FILE" ]; then
echo " Already exists, skipping ./$NEW_FILE"
elif [ -e "./$FILE" ]; then
if [ $LWT -eq 1 ]; then
if [ -L "./$FILE" ]; then
echo " Copying symlink ./$FILE => ./$NEW_FILE"
cp -a "./$FILE" "./$NEW_FILE"
else
echo " Creating symlink ./$NEW_FILE -> $FILE"
ln -s "$FILE" "./$NEW_FILE"
fi
else
if [ $REN -eq 1 ]; then
echo " Renaming ./$FILE => ./$NEW_FILE"
mv -T "./$FILE" "./$NEW_FILE"
else
echo " Copying ./$FILE => ./$NEW_FILE"
cp -a "./$FILE" "./$NEW_FILE"
fi
fi
fi
done
)
done
# Process Unum subsystems
echo "Processing Unum subsystems..."
for _DIR in `find ./src/unum -mindepth 1 -maxdepth 1 -type d`; do
(
echo " Changing directory to $_DIR"
cd "$_DIR"
SNAME=`basename "$_DIR"`
MK_FILE="$SNAME-$S_TYPE.mk"
NEW_MK_FILE="$SNAME-$T_TYPE.mk"
DIR_NAME="$S_TYPE"
NEW_DIR_NAME="$T_TYPE"
# if no makefile for the hardware type then skip the subsystem
if [ ! -e "./$MK_FILE" -a ! -L "./$MK_FILE" ] && \
[ ! -d "./$DIR_NAME" -a ! -L "./$DIR_NAME" ]; then
continue
fi
if [ "$X_DIR" != "" ]; then
if [ -e "./$MK_FILE" ] || [ -L "./$MK_FILE" ]; then
echo " Exporting ./$MK_FILE to $X_DIR/$_DIR/"
mkdir -p `dirname "$X_DIR/$_DIR/$MK_FILE"`
cp -a "./$MK_FILE" "$X_DIR/$_DIR/$MK_FILE"
fi
if [ -d "$DIR_NAME" ] || [ -L "$DIR_NAME" ]; then
echo " Exporting folder ./$DIR_NAME to $X_DIR/$_DIR/"
mkdir -p `dirname "$X_DIR/$_DIR/$DIR_NAME"`
cp -a "./$DIR_NAME" "$X_DIR/$_DIR/$DIR_NAME"
fi
elif [ $DEL -eq 1 ]; then
echo " Cleaning up ./$MK_FILE"
rm -f "./$MK_FILE" 2>/dev/null 1>&2
if [ -e "$DIR_NAME" ] || [ -L "$DIR_NAME" ]; then
echo " Cleaning up folder ./$DIR_NAME"
rm -Rf "./$DIR_NAME" 2>/dev/null 1>&2
fi
else
if [ $LWT -eq 1 ]; then
if [ -e "./$MK_FILE" ]; then
if [ -e "./$NEW_MK_FILE" ]; then
echo " Already exists, skipping ./$NEW_MK_FILE"
elif [ -L "./$MK_FILE" ]; then
echo " Copying symlink ./$MK_FILE => ./$NEW_MK_FILE"
cp -a "./$MK_FILE" "./$NEW_MK_FILE"
else
echo " Creating symlink ./$NEW_MK_FILE -> $MK_FILE"
ln -s "$MK_FILE" "./$NEW_MK_FILE"
fi
fi
if [ -d "./$DIR_NAME" ]; then
if [ -e "./$NEW_DIR_NAME" ]; then
echo " Already exists, skipping ./$NEW_DIR_NAME"
elif [ -L "$DIR_NAME" ]; then
echo " Copying symlink ./$DIR_NAME => ./$NEW_DIR_NAME"
cp -a "./$DIR_NAME" "./$NEW_DIR_NAME"
elif [ -d "$DIR_NAME" ]; then
echo " Creating symlink ./$NEW_DIR_NAME -> $DIR_NAME"
ln -s "$DIR_NAME" "./$NEW_DIR_NAME"
fi
fi
else
if [ -e "./$MK_FILE" ]; then
if [ -e "./$NEW_MK_FILE" ]; then
echo " Already exists, skipping ./$NEW_MK_FILE"
elif [ $REN -eq 1 ]; then
echo " Renaming file ./$MK_FILE => ./$NEW_MK_FILE"
mv -T "./$MK_FILE" "./$NEW_MK_FILE"
else
echo " Copying file ./$MK_FILE => ./$NEW_MK_FILE"
cp -a "./$MK_FILE" "./$NEW_MK_FILE"
fi
fi
if [ -d "./$DIR_NAME" ]; then
if [ -e "./$NEW_DIR_NAME" ]; then
echo " Already exists, skipping ./$NEW_DIR_NAME"
elif [ $REN -eq 1 ]; then
echo " Renaming $DIR_NAME => ./$NEW_DIR_NAME"
mv -T "./$DIR_NAME" "./$NEW_DIR_NAME"
elif [ -L "$DIR_NAME" ]; then
echo " Copying symlink $DIR_NAME => ./$NEW_DIR_NAME"
cp -a "./$DIR_NAME" "./$NEW_DIR_NAME"
elif [ -d "$DIR_NAME" ]; then
echo " Copying folder $DIR_NAME => ./$NEW_DIR_NAME"
cp -a "./$DIR_NAME" "./$NEW_DIR_NAME"
fi
fi
fi
fi
)
done
# Individual custom items
NEW_FILE="README-$T_TYPE"
FILE="README-$S_TYPE"
if [ "$X_DIR" != "" ]; then
if [ -e "./$NEW_FILE" ] || [ -L "./$NEW_FILE" ]; then
echo " Exporting ./$NEW_FILE to $X_DIR"
cp -a "./$NEW_FILE" "$X_DIR/$NEW_FILE"
fi
elif [ $DEL -eq 1 ]; then
echo "Removing readme file ./$NEW_FILE"
rm -f "./$NEW_FILE" 2>/dev/null 1>&2
else
if [ -e "./$NEW_FILE" ]; then
echo "Readme already exists, skipping ./$NEW_FILE"
elif [ $REN -eq 1 ]; then
echo "Renaming ./$FILE => ./$NEW_FILE"
mv -T "./$FILE" "./$NEW_FILE"
elif [ ! -e "$NEW_FILE" ]; then
echo "Creating readme file ./$NEW_FILE"
cat << READMETPL > "./$NEW_FILE"
This applies to <HARDWARE_NAME> agent build
This hardware type should be integrated with
https://github.com/violetatrium/<REPO_NAME>
branch
<BRANCH_NAME>
Build it under <WHERE_TO_BUILD>
Build environment setup:
------------------------
<INSTRUCTIONS_OR_WHERE_TO_GET_THEM>
READMETPL
fi
fi
# Finally the main rules file
FILE="rules/$S_TYPE.mk"
NEW_FILE="rules/$T_TYPE.mk"
if [ "$X_DIR" != "" ]; then
if [ -e "./$FILE" ] || [ -L "./$FILE" ]; then
echo " Exporting ./$FILE to $X_DIR"
mkdir -p `dirname "$X_DIR/$FILE"`
cp -a "./$FILE" "$X_DIR/$FILE"
fi
elif [ $DEL -eq 1 ]; then
echo "Removing rules file ./$FILE"
rm -f "./$FILE" 2>/dev/null 1>&2
else
if [ -e "./$NEW_FILE" ]; then
echo "The rules file already exists, skipping ./$FILE"
elif [ $REN -eq 1 ]; then
echo "Renaming ./$FILE => ./$NEW_FILE"
mv -T "./$FILE" "./$NEW_FILE"
else
# Rules file is always copy-only
echo "Copying rules file ./$FILE => ./$NEW_FILE"
cp -a "./$FILE" "./$NEW_FILE"
fi
fi
# If renaming fix the symlinks
if [ $REN -eq 1 ]; then
echo "Fixing symlinks $S_TYPE => $T_TYPE"
find ./src $TOP_DIRS_WITH_HW_SUBDIRS -lname "$S_TYPE/" -exec \
bash -c 'ln -sfn `readlink $1 | sed -e "'s/$S_TYPE/$T_TYPE/'"` $1' bash {} \;
echo "Fixing symlinks ...-$S_TYPE.mk => ...-$T_TYPE.mk"
find ./src $TOP_DIRS_WITH_HW_SUBDIRS -lname '*-'"$S_TYPE.mk" -exec \
bash -c 'ln -sfn `readlink $1 | sed -e "'s/-$S_TYPE.mk/-$T_TYPE.mk/'"` $1' bash {} \;
fi
# Remind about the extras
echo "You might also want to add/remove info under ./extras/ folder."
if [ $REN -eq 1 ]; then
echo "Also, search for references to $S_TYPE in the files: git grep -w $S_TYPE"
fi