-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
openwrt_build.sh
executable file
·207 lines (183 loc) · 7.28 KB
/
openwrt_build.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
#!/bin/bash
# the name of the app
CHIPAPP_NAME="p44mbrd"
# the name of the openwrt package
OPENWRT_PACKAGE_NAME="${CHIPAPP_NAME}"
# process and consume leading option arguments
FOR_DEBUG=0
DEPLOY=1
NINJA_OPTS=""
while [[ $# -ge 1 ]]; do
case "$1" in
"-d"|"--debug") FOR_DEBUG=1;DEPLOY=0;shift;;
"-t"|"--test") DEPLOY=0;shift;;
"-v"|"--verbose") NINJA_OPTS="${NINJA_OPTS} -v";shift;;
"-c"|"--continue") NINJA_OPTS="${NINJA_OPTS} -k 0";shift;;
*) break;;
esac
done
# process remaining arguments
if [[ $# < 1 || $# > 2 ]]; then
echo "Usage: $0 [options] <openwrt buildroot path> [<OpenWrt .config file>]"
echo " builds '${CHIPAPP_NAME}' app from current dir, stores build binary into prebuilt_bin dir"
echo " of openwrt package named ${OPENWRT_PACKAGE_NAME}"
echo "Options:"
echo " --debug : build for debugging, do not store binary in openwrt package"
echo " --test : build normally, but do not store binary in openwrt package"
echo " --verbose : call ninja with verbose option"
echo " --continue : prevent ninja form stopping at first failed job"
exit 1
fi
BUILDROOT="$1"
CONFIGFILE="${BUILDROOT}/.config"
if [ ! -f "${CONFIGFILE}" ]; then
echo "${BUILDROOT} does not seem to be a openwrt buildroot (no .config found)"
exit 1
fi
if [[ $# > 1 ]]; then
CONFIGFILE="$2"
if [ ! -f "${CONFIGFILE}" ]; then
echo "config file not found: '${CONFIGFILE}'"
exit 1
fi
fi
# the root of the app is the current dir
CHIPAPP_ROOT="$(pwd -P)"
# get openwrt config
source "${CONFIGFILE}" 2>/dev/null
# OpenWrt config Definitions we need
# =========================== =========================================
# <case statement> TARGET_CPU=mips
# CONFIG_CPU_TYPE TARGET_CPU_TYPE=24kc
# CONFIG_TARGET_ARCH_PACKAGES TARGET_ARCH_PACKAGES=mipsel_24kc
# CONFIG_ARCH TARGET_ARCH=mipsel
# <case statemnet> TARGET_LIBC=musl
# CONFIG_TARGET_SUFFIX TARGET_SUFFIX=-musl
#
#
# <case statemnet> TARGET_CPU=arm
# CONFIG_CPU_TYPE TARGET_CPU_TYPE=arm1176jzf-s+vfp
# CONFIG_TARGET_ARCH_PACKAGES TARGET_ARCH_PACKAGES=arm_arm1176jzf-s_vfp
# CONFIG_ARCH TARGET_ARCH=arm
# <case statemnet> TARGET_LIBC=musl_eabi
# CONFIG_TARGET_SUFFIX TARGET_SUFFIX=-muslgnueabi
# figure out target CPU for gn build system
case "${CONFIG_ARCH}" in
mips*) TARGET_CPU="mips"; TARGET_LIBC="musl" ;;
arm*) TARGET_CPU="arm"; TARGET_LIBC="musl_eabi" ;;
*) echo "Architecture '${CONFIG_ARCH}' not yet supported"; exit 1;;
esac
# those we can use directly
TARGET_ARCH="${CONFIG_ARCH}"
TARGET_CPU_TYPE="${CONFIG_CPU_TYPE}"
TARGET_ARCH_PACKAGES="${CONFIG_TARGET_ARCH_PACKAGES}"
if [ -n "${CONFIG_TARGET_SUFFIX}" ]; then
TARGET_SUFFIX="-${CONFIG_TARGET_SUFFIX}"
fi
TARGET_COMPILER="gcc-${CONFIG_GCC_VERSION}"
# derived paths
export TOOLCHAIN_ARCH="${TARGET_ARCH}_${TARGET_CPU_TYPE}"
export STAGING_DIR="${BUILDROOT}/staging_dir"
export SYSROOT="${STAGING_DIR}/target-${TOOLCHAIN_ARCH}_${TARGET_LIBC}"
export TOOLCHAIN_PREFIX="${STAGING_DIR}/toolchain-${TOOLCHAIN_ARCH}_${TARGET_COMPILER}_${TARGET_LIBC}/bin/${TARGET_ARCH}-openwrt-linux${TARGET_SUFFIX}"
# - only needed for finding strip-new
BUILT_TOOLS="${BUILDROOT}/build_dir/toolchain-${TOOLCHAIN_ARCH}_${TARGET_COMPILER}_${TARGET_LIBC}"
PREBUILT_BIN=
EXTRA_GN_ARGS=
# build dir for gn/ninja
if [[ ${FOR_DEBUG} -ne 0 ]]; then
# just build, do not create prebuilt-bin
OUT_DIR="out/openwrt/${TARGET_ARCH_PACKAGES}/debug"
#EXTRA_GN_ARGS="target_cflags=[\"-ggdb3\"] chip_enable_schema_check=true chip_enable_transport_trace=true"
EXTRA_GN_ARGS="target_cflags=[\"-ggdb3\"]"
else
OUT_DIR="out/openwrt/${TARGET_ARCH_PACKAGES}/release"
# find the feed of the openwrt wrapper package
OPENWRT_FEED=$(${BUILDROOT}/scripts/feeds search "${OPENWRT_PACKAGE_NAME}" | sed -n -E -e "/Search/s/Search results in feed '(.*)'.*/\1/p")
if [ -z "${OPENWRT_FEED}" ]; then
echo "no openwrt feed found containing package ${OPENWRT_PACKAGE_NAME}"
exit 1
fi
OPENWRT_WRAPPER_PACKAGE="feeds/${OPENWRT_FEED}/${OPENWRT_PACKAGE_NAME}"
# check wrapper package prebuilt_bin dir in openwrt feed
PREBUILT_BIN="${BUILDROOT}/${OPENWRT_WRAPPER_PACKAGE}/prebuilt_bin/${TARGET_ARCH_PACKAGES}"
if [ ! -d "${PREBUILT_BIN}" ]; then
echo "Wrapper package prebuilt_bin missing: ${PREBUILT_BIN}"
exit 1
fi
fi
#### Now build
cd "${CHIPAPP_ROOT}"
echo " building from : ${CHIPAPP_ROOT}"
echo " into : ${OUT_DIR}"
echo " staging_dir : ${STAGING_DIR}"
echo " sysroot : ${SYSROOT}"
echo "toolchain_prefix : ${TOOLCHAIN_PREFIX}"
echo " built_tools : ${BUILT_TOOLS}"
if [ ${DEPLOY} -eq 1 -a -d "${PREBUILT_BIN}" ]; then
echo " prebuilt_bin : ${PREBUILT_BIN}"
fi
#exit 1
# prep matter ?
#source third_party/connectedhomeip/scripts/activate.sh
# gn gen
# - w/o WPA/Wifi and openthread
# - generating a compile_commands.json for clangd language server
gn gen \
--fail-on-unused-args \
--export-compile-commands \
--root=$CHIPAPP_ROOT \
"--args=target_os=\"openwrt\" ${EXTRA_GN_ARGS} openwrt_sdk_root=\"$BUILDROOT\" openwrt_sdk_sysroot=\"$SYSROOT\" openwrt_toolchain_prefix=\"$TOOLCHAIN_PREFIX\" target_cpu=\"$TARGET_CPU\" chip_device_platform=\"linux\" chip_enable_openthread=false chip_enable_wifi=false" \
$OUT_DIR
if [[ $? != 0 ]]; then
echo "# gn FAILED"
exit 1
fi
# HACK: force zap gen when .zap has changed. Apparently not caught by our gn setup (yet)
if [ ${CHIPAPP_ROOT}/zap/p44mbrd.zap -nt ${CHIPAPP_ROOT}/zap/p44mbrd.matter ]; then
${CHIPAPP_ROOT}/third_party/connectedhomeip/scripts/tools/zap/generate.py ${CHIPAPP_ROOT}/zap/p44mbrd.zap
if [[ $? != 0 ]]; then
echo "# generating .matter file FAILED"
exit 1
fi
fi
# build with ninja (only the app, not other stuff like address-resolve-tool)
ninja ${NINJA_OPTS} -C ${OUT_DIR} ${CHIPAPP_NAME}
if [[ $? != 0 ]]; then
echo "# ninja build FAILED - can be repeated with:"
echo "ninja -C ${OUT_DIR} ${CHIPAPP_NAME}"
exit 1
fi
# store into files of wrapper package in openwrt
if [ ${DEPLOY} -eq 1 -a -d "${PREBUILT_BIN}" ]; then
"${BUILT_TOOLS}/binutils/binutils/strip-new" -o "${PREBUILT_BIN}/${CHIPAPP_NAME}" "${OUT_DIR}/${CHIPAPP_NAME}"
echo "copied executable to: ${PREBUILT_BIN}"
# also save info about the source VERSION we built that binary from
git describe HEAD >"${PREBUILT_BIN}/git_version"
git rev-parse HEAD >"${PREBUILT_BIN}/git_rev"
echo "saved version/rev along with executable in: ${PREBUILT_BIN}"
fi
# debugging/testing only, do not store binary in openwrt package
if [ -z ${TARGET_HOST} ]; then
echo "# TARGET_HOST is not defined, need to define it"
# provide symbolically
echo "export TARGET_HOST=192.168.x.x"
TARGET_HOST='${TARGET_HOST}'
fi
echo ""
echo "# copy ${CHIPAPP_NAME} to a target's /tmp for testing, stripped:"
echo "\"${BUILT_TOOLS}/binutils/binutils/strip-new\" -o \"/tmp/${CHIPAPP_NAME}\" \"${OUT_DIR}/${CHIPAPP_NAME}\""
echo "scp \"/tmp/${CHIPAPP_NAME}\" root@${TARGET_HOST}:/tmp"
if [ ${FOR_DEBUG} -eq 1 ]; then
pushd ${OUT_DIR}
_APP=$(pwd)/${CHIPAPP_NAME}
popd
echo ""
echo "# debug target executable built as ${_APP}"
echo "# - send ${CHIPAPP_NAME} to target as debugtarget:"
echo "pushd ${BUILDROOT}"
echo "p44b send -s ${_APP}"
echo "popd"
echo "# - start debugging ${CHIPAPP_NAME}:"
echo "${BUILDROOT}/scripts/remote-gdb ${TARGET_HOST}:9000 ${_APP}"
fi