-
Notifications
You must be signed in to change notification settings - Fork 76
/
mplab_project.sh
377 lines (314 loc) · 11.6 KB
/
mplab_project.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
374
375
376
377
#!/bin/bash
# Generate MPLAB X projects for examples for the available variants
set -e
### Arguments parsing
DIR="$1"
TOP="$2"
BINNAME="$3"
TARGET="$4"
AVAILABLE_VARIANTS="$5"
PROJECT_TEMPLATE="$6"
CONFIGURATION_TEMPLATE="$7"
DEBUG_TEMPLATE="$8"
### Global variable
SCRIPTS="$DIR/$TOP/scripts"
folder_idx=1
TOOLCHAINS="ARM XC32"
tpl-split() {
local template_src="$1"
local template="$2"
local nod="$3"
local head
local tail
echo "SPLIT $template_src"
echo $*
head=$(sed -n "/<${nod}>/=" "${template_src}" | sed -n '1p')
tail=$(sed -n "/<\/${nod}>/=" "${template_src}" | sed -n '$p')
# head: from start of file to <configuration> (excluded)
sed -n "1,`expr ${head} - 1`p" "${template_src}" > "$DIR/${template}.head"
# body: from <configuration> to </configuration> (both included)
sed -n "${head},${tail}p" "${template_src}" > "$DIR/${template}.body"
# tail: from </configuration> (excluded) to end of file
sed -n "`expr ${tail} + 1`,\$p" "${template_src}" > "$DIR/${template}.tail"
}
helper-use-windows-path() {
local str=${1//\/\//\/}
echo ${str//\//\\\\}
}
tpl-finalize() {
local tpl="$1"
sed -i -e "s/\(__REPLACE_DEP_LIST__\|__REPLACE_PROJECT_FILES__\)//g" "$tpl"
}
find-source() {
local dir="$1"
local obj="$2"
local path=${obj//.o/.c}
if [ ! -f "$dir/$path" ]; then
path=${obj//.o/.S}
if [ ! -f "$dir/$path" ]; then
echo "Source file for $obj not found!" 1>&2
exit 3
fi
fi
echo $path
}
tpl-set-prj-files() {
local tpl="$1"
local section="$2"; shift; shift
local input="$*"
local tmpxml=`mktemp -p "$DIR"`
(
for path in ${input}; do
path=$(find-source "$DIR/$TOP" "$path")
echo -e " <itemPath>../../../$path</itemPath>"
done
) > "$tmpxml"
sed -i -e "/__REPLACE_PROJECT_FILES__/r $tmpxml" "$tpl"
rm -f "$tmpxml" 2>&1 > /dev/null
}
tpl-set-deps() {
local tpl="$1"
local section="$2"; shift; shift
local input=$*
local empty_group=true
local tmpxml=`mktemp -p "$DIR"`
# Read file and fill input array
(
for path in ${input}; do
path=$(find-source "$DIR/$TOP" "$path")
if [ "$empty_group" = true ]
then
empty_group=false
echo -e " <logicalFolder name=\"$section\" displayName=\"$section\" projectFiles=\"true\">"
fi
echo -e " <itemPath>../../../$path</itemPath>"
done
if [ "$empty_group" = false ]
then
echo -e " </logicalFolder>"
fi
) > "$tmpxml"
sed -i -e "/__REPLACE_PROJECT_FILES__/r $tmpxml" "$tpl"
rm -f "$tmpxml" 2>&1 > /dev/null
}
tpl-set-version() {
local tpl="$1"
sed -i -e "s/__REPLACE_VERSION__/$VERSION/g" "$tpl"
}
tpl-set-defines() {
local tpl="$1"
local tmpxml=`mktemp -p "$DIR"`
echo -n " value=\"" > "$tmpxml"
for flag in $CFLAGS_DEFS; do
flag=${flag//:/ }
echo -n "${flag//-D/};"
done >> "$tmpxml"
echo -n "\"" >> "$tmpxml"
sed -i -e "/__REPLACE_DEFINES__/r $tmpxml" "$tpl"
sed -i -e "s/__REPLACE_DEFINES__//g" "$tpl"
rm -f "$tmpxml" 2>&1 > /dev/null
}
tpl-set-includes() {
local tpl="$1"
local tmpxml=`mktemp -p "$DIR"`
echo -n " <property key=\"common-include-directories\" value=\"" > "$tmpxml"
for include in $CFLAGS_INC; do
local inc=$(helper-use-windows-path "${include//-I/}")
echo -n -e "..\\$inc;"
done >> "$tmpxml"
echo "\"/>" >> "$tmpxml"
sed -i -e "/__REPLACE_INCLUDES__/r $tmpxml" "$tpl"
sed -i -e "s/__REPLACE_INCLUDES__//g" "$tpl"
rm -f "$tmpxml" 2>&1 > /dev/null
}
tpl-set-linker-script() {
local tpl="$1"
local tmpxml=`mktemp -p "$DIR"`
local script_path=""
for toolchain in ${TOOLCHAINS}; do
if [ $toolchain = "XC32" ]; then
script_path=$xc32_linker_script_y
elif [ $toolchain = "ARM" ]; then
script_path=${gnu_linker_script_y%/*}/
else
echo "Compiler Toolchain $toolchain is not supported!" 1>&2
exit 3
fi
echo "<logicalFolder name=\"LinkerScript_${toolchain}\" displayName=\"${toolchain}\" projectFiles=\"true\">" >> $tmpxml
for linker_script in ${script_path}; do
for variant in $AVAILABLE_VARIANTS; do
if [ ! -f $linker_script$variant.ld ]; then
echo "File $linker_script$variant.ld not found!" 1>&2
exit 3
fi
echo "<itemPath>../$linker_script$variant.ld</itemPath>" >> $tmpxml
done
done
echo "</logicalFolder>" >> $tmpxml
done
sed -i -e "/__REPLACE_LINK_SCRIPT__/r $tmpxml" "$tpl"
sed -i -e "s/__REPLACE_LINK_SCRIPT__//g" "$tpl"
rm -f "$tmpxml" 2>&1 > /dev/null
}
tpl-set-program-entry() {
local tpl="$1"
local entry="$2"
sed -i -e "s%__REPLACE_ENTRY__%$entry%g" "$tpl"
}
tpl-set-binary-name() {
local tpl="$1"
local binary_name="$2"
if [ -z $binary_name ]; then
echo binary_name not defined! 1>&2
exit 3
fi
sed -i -e "s/__REPLACE_BIN_NAME__/$binary_name/g" "$tpl"
}
tpl-set-configuration() {
local tpl="$1"
local target=$2
local variant=$3
echo "SET target=$target, variant=$variant"
sed -i "s/<conf>/<conf /g" "$tpl"
sed -i "s/__REPLACE_CONF__/$variant/g" "$tpl"
sed -i "s/__REPLACE_TARGET__/$target/g" "$tpl"
}
tpl-set-chip() {
local tpl="$1"
local chip=
for flag in $CFLAGS_DEFS; do
if [ "$chip" \< "$(echo $flag | grep CONFIG_CHIP_ | sed 's/-DCONFIG_CHIP_//')" ]; then
chip=$(echo $flag | grep CONFIG_CHIP_ | sed 's/-DCONFIG_CHIP_/AT/')
fi
done
echo "SET CHIP=$chip"
if [ "$chip" = ATSAM9X60 ]; then
sed -i "s/__REPLACE_CHIP__/SAM9X60/g" "$tpl"
sed -i "s/__REPLACE_DFP__/<pack name=\"SAM9X6_DFP\" vendor=\"Microchip\" version=\"1.7.85\"\/>/g" "$tpl"
sed -i "s/__REPLACE_ADDITIONAL_OPT__/\n <appendMe value=\"-mfloat-abi=soft\"\/>/g" "$tpl"
elif [ "$chip" = ATSAMA5D27 ]; then
sed -i "s/__REPLACE_CHIP__/$chip/g" "$tpl"
sed -i "s/__REPLACE_DFP__/<pack name=\"SAMA5D2_DFP\" vendor=\"Microchip\" version=\"1.9.106\"\/>/g" "$tpl"
sed -i "s/__REPLACE_ADDITIONAL_OPT__/\n <appendMe value=\"-mfloat-abi=hard\"\/>/g" "$tpl"
fi
}
generate-bodies-conf() {
local file="$1"
local variant="$2"
for toolchain in ${TOOLCHAINS}; do
local tpl="$DIR/${file}_${variant}_${toolchain}.conf"
echo "GEN temporary file ${file}_${variant}_${toolchain}.conf"
cat "$DIR/mplab_conf.body" > "$tpl"
tpl-set-defines "$tpl" ${variant} ${toolchain}
tpl-set-includes "$tpl"
tpl-set-configuration "$tpl" $TARGET ${variant}_${toolchain}
local tmpxml=`mktemp -p "$DIR"`
if [ $toolchain = "XC32" ]; then
for var in $AVAILABLE_VARIANTS; do
if [ ! $var = $variant ]; then
echo "<item path=\"../${xc32_linker_script_y}$var.ld\" ex=\"true\" overriding=\"false\"></item>" >> $tmpxml
fi
echo "<item path=\"../${gnu_linker_script_y%/*}/$var.ld\" ex=\"true\" overriding=\"false\"></item>" >> $tmpxml
done
for path in ${startup_ARM}; do
path=$(find-source "$DIR/$TOP" "$path")
echo "<item path=\"../../../$path\" ex=\"true\" overriding=\"false\"></item>" >> $tmpxml
done
sed -i -e "s/__REPLACE_TOOLCHAIN_VERSION__//g" "$tpl"
elif [ $toolchain = "ARM" ]; then
for var in $AVAILABLE_VARIANTS; do
if [ ! $var = $variant ]; then
echo "<item path=\"../${gnu_linker_script_y%/*}/$var.ld\" ex=\"true\" overriding=\"false\"></item>" >> $tmpxml
fi
echo "<item path=\"../$xc32_linker_script_y$var.ld\" ex=\"true\" overriding=\"false\"></item>" >> $tmpxml
done
for path in ${startup_XC32}; do
path=$(find-source "$DIR/$TOP" "$path")
echo "<item path=\"../../../$path\" ex=\"true\" overriding=\"false\"></item>" >> $tmpxml
done
sed -i -e "s/__REPLACE_TOOLCHAIN_VERSION__/10.2.1/g" "$tpl"
else
echo "Compiler Toolchain $toolchain is not supported!" 1>&2
exit 3
fi
sed -i -e "s/__REPLACE_TOOLCHAIN__/$toolchain/g" "$tpl"
sed -i -e "/__REPLACE_LINK_SCRIPT_EX__/r $tmpxml" "$tpl"
sed -i -e "s/__REPLACE_LINK_SCRIPT_EX__//g" "$tpl"
if [ $variant = "sram" ]; then
sed -i -e "s/__REPLACE_BOOTSTRAP_USE__/false/g" "$tpl"
sed -i -e "s/__REPLACE_BOOTSTRAP_PATH__//g" "$tpl"
else
sed -i -e "s/__REPLACE_BOOTSTRAP_USE__/true/g" "$tpl"
sed -i -e "s/__REPLACE_BOOTSTRAP_PATH__/\${ProjectDir}\/..\/..\/..\/flash_loaders\/mplab\/${TARGET}\/at91bootstrap.elf/g" "$tpl"
fi
rm -f "$tmpxml" 2>&1 > /dev/null
done
}
generate-conf() {
local file="$1"
local tpl="$DIR/${TARGET}.X/nbproject/configurations.xml"
if [ ! -e "$DIR/${TARGET}.X/nbproject" ]; then
mkdir -p "$DIR/${TARGET}.X/nbproject"
fi
cp $CONFIGURATION_TEMPLATE $tpl
tpl-split "$CONFIGURATION_TEMPLATE" mplab_conf conf
for variant in $AVAILABLE_VARIANTS; do
if [ -f "$DIR/.mplab-$variant.sh" ]; then
. "$DIR/.mplab-$variant.sh"
generate-bodies-conf "$file" $variant
fi
done
# Remove temporary files after merging them
rm -f "$DIR/$file.conf.bodies"
touch "$DIR/$file.conf.bodies"
for variant in ${AVAILABLE_VARIANTS}; do
for toolchain in ${TOOLCHAINS}; do
cat "$DIR/${file}_${variant}_${toolchain}.conf" >> "$DIR/$file.conf.bodies"
rm -f "$DIR/${file}_${variant}_${toolchain}.conf"
done
done
echo "GEN ${file}_$TARGET.conf"
cat "$DIR/mplab_conf.head" > "$tpl"
rm -f "$DIR/mplab_conf.head"
cat "$DIR/$file.conf.bodies" >> "$tpl"
rm -f "$DIR/mplab_conf.body"
rm -f "$DIR/$file.conf.bodies"
cat "$DIR/mplab_conf.tail" >> "$tpl"
rm -f "$DIR/mplab_conf.tail"
tpl-set-linker-script "$tpl"
tpl-set-deps "$tpl" "utils" "$utils_y"
tpl-set-deps "$tpl" "usb" "$usb_y"
tpl-set-deps "$tpl" "uip" "$uip_y"
tpl-set-deps "$tpl" "target" "$target_y"
tpl-set-deps "$tpl" "startup_ARM" "$startup_ARM"
tpl-set-deps "$tpl" "startup_XC32" "$startup_XC32"
tpl-set-deps "$tpl" "lwip" "$lwip_y"
tpl-set-deps "$tpl" "libsdmmc" "$libsdmmc_y"
tpl-set-deps "$tpl" "libfatfs" "$libfatfs_y"
tpl-set-deps "$tpl" "freertos" "$libfreertos_y"
tpl-set-deps "$tpl" "drivers" "$drivers_y"
tpl-set-deps "$tpl" "arch" "$arch_y"
tpl-set-chip "$tpl"
tpl-set-prj-files "$tpl" "project_files" "$obj_y"
tpl-finalize "$tpl"
sed -i -e "s%__REPLACE_PROJRCT__%${file}_$TARGET%g" $tpl
}
generate-project() {
local file=$1
if [ ! -e "$DIR/${TARGET}.X/nbproject" ]; then
mkdir -p "$DIR/${TARGET}.X/nbproject"
fi
echo "GEN project.xml"
sed -e "s%__REPLACE_PROJRCT__%${file}_$TARGET%g" < "$PROJECT_TEMPLATE" |
sed -e "s%/%/%g" > "$DIR/${TARGET}.X/nbproject/project.xml"
}
for variant in $AVAILABLE_VARIANTS; do
make -C "$DIR" TARGET=$TARGET VARIANT=$variant mplab-env
done
generate-project $BINNAME
generate-conf $BINNAME
cp -p ../../scripts/mplab_Makefile.template $DIR/${TARGET}.X/Makefile
for variant in $AVAILABLE_VARIANTS; do
rm -f "$DIR/.mplab-$variant.sh"
done
exit 0