-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpactool.sh
executable file
·175 lines (153 loc) · 4.44 KB
/
pactool.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
#!/usr/bin/env bash
set -e
function usage {
printf 'Usage: pactool.sh <command>\nWhere <command> is\n\tgenerate - (re)generate all PACs from their respective SVD source files.\n\tpublish - (re)publish all PACs to crates.io.\n'
}
function generate {
# Parse any args
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
*)
INPUT_SVD="$1"
shift
break # The rest of the args will go to the handler
;;
esac
done
if [ "$(which svd2rust)" == "" ]; then
cargo install svd2rust --version 0.27.2
fi
if [ "$(which form)" == "" ]; then
cargo install form --version 0.10.0
fi
TOP="${PWD}"
#
# Run through a first pass and create skeleton PAC crates for any that are missing.
#
svds=()
if [ "${INPUT_SVD}" != "" ]; then
# Process the specific svds given
while IFS='' read -r line; do svds+=("$line"); done < <(find "${TOP}/svd" -name "${INPUT_SVD}")
elif [ "${FORCE}" == "true" ]; then
# If forced, process all SVD files
while IFS='' read -r line; do svds+=("$line"); done < <(find "${TOP}/svd" -name '*.svd')
else
# Only process SVDs that git says are new or changed.
while IFS=$'\n' read -r line; do svds+=("$line"); done < <(git status --porcelain | grep -e ".svd$" | perl -n -e'/\s*(\S*)\s*(\S+)/ && printf "'"${TOP}"'/%s\n",$2')
fi
for svd in "${svds[@]}"; do
echo "Processing: ${svd}..."
CHIP=$(basename "${svd}" .svd)
chip=$(echo "${CHIP}" | tr '[:upper:]' '[:lower:]')
xsl=svd/devices/${chip}.xsl
# If the xsl doesn't exist, create one from a template.
if [ ! -f "${xsl}" ]; then
cat > "${xsl}" <<-EOF
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:include href="include/common.xsl"/>
</xsl:stylesheet>
EOF
fi
# if the output directory doesn't exist, create one from a template.
if [ ! -d "${TOP}/pac/${chip}" ]; then
# Make the PAC directory (and ./src subdirectory)
mkdir -p "${TOP}/pac/${chip}/src"
# Create an empty lib.rs
touch "${TOP}/pac/${chip}/src/lib.rs"
# Create a Cargo.toml from a template.
cat > "${TOP}/pac/${chip}/Cargo.toml" <<-EOF
[package]
authors = ["John Terrell <[email protected]>", "Jacob Alexander <[email protected]>"]
categories = ["embedded", "hardware-support", "no-std"]
description = "Peripheral access crate for the ${CHIP} microcontroller"
keywords = ["arm", "cortex-m", "${chip}", "svd2rust"]
license = "MIT OR Apache-2.0"
name = "${chip}-pac"
repository = "https://github.com/atsam-rs/atsam-pac"
version = "0.1.0"
[dependencies]
cortex-m = "0.7.7"
critical-section = { version = "1.1.1", optional = true }
vcell = "0.1.3"
[dependencies.cortex-m-rt]
optional = true
version = "0.7.3"
[features]
rt = ["cortex-m-rt/device"]
critical-section = ["dep:critical-section"]
[package.metadata.docs.rs]
features = ["rt"]
EOF
fi
done
#
# Create the crates using svd2rust
#
for svd in "${svds[@]}"; do
CHIP=$(basename "${svd}" .svd)
chip=$(echo "${CHIP}" | tr '[:upper:]' '[:lower:]')
xsl=svd/devices/${chip}.xsl
pushd "${TOP}/pac/${chip}"
xsltproc "${TOP}/${xsl}" "${svd}" | svd2rust
rm -rf src/
form -i lib.rs -o src
rm lib.rs
cargo fmt
rustfmt build.rs
popd
done
}
function publish {
echo "Getting list of crates to pass to cargo smart-release..."
echo "Add --execute to actually publish the crates."
crates=($(ls pac | awk '{print $0"-pac"}'))
cargo smart-release -u ${crates[@]} $@
}
#
# Parse arguments
#
COMMAND=""
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-f|--force)
FORCE=true
shift
;;
*)
COMMAND="$1"
shift
break # The rest of the args will go to the handler
;;
esac
done
#
# If no argument specified, print usage info and exit.
#
if [ "${COMMAND}" == "" ]; then
usage
exit 1
fi
#
# Handle any commands
#
case ${COMMAND} in
help)
usage
;;
generate)
generate "$@"
;;
publish)
publish "$@"
;;
*)
echo "ERROR: Unrecognized command: ${COMMAND}"
usage
exit 1
;;
esac