155 lines
5.4 KiB
Bash
Executable File
155 lines
5.4 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
|
|
cd "$(dirname "$0")"
|
|
|
|
linuxcnc_root=${LINUXCNC_ROOT:-../linuxcnc}
|
|
cxx=${CXX:-g++}
|
|
if [[ -z ${CXX:-} ]] && command -v ccache >/dev/null 2>&1; then
|
|
cxx="ccache $cxx"
|
|
fi
|
|
build_jobs=${CNC_SIM_BUILD_JOBS:-8}
|
|
if ! [[ "$build_jobs" =~ ^[1-9][0-9]*$ ]]; then
|
|
echo "CNC_SIM_BUILD_JOBS must be a positive integer: $build_jobs" >&2
|
|
exit 1
|
|
fi
|
|
build_dir=${CNC_SIM_WASM_TOOLDATA_RUNTIME_SYMBOLS_BUILD_DIR-build/wasm-tooldata-runtime-symbols}
|
|
if [[ -z "$build_dir" ]]; then
|
|
echo "CNC_SIM_WASM_TOOLDATA_RUNTIME_SYMBOLS_BUILD_DIR must not be empty" >&2
|
|
exit 1
|
|
fi
|
|
if [[ "$build_dir" =~ [[:space:]] ]]; then
|
|
echo "CNC_SIM_WASM_TOOLDATA_RUNTIME_SYMBOLS_BUILD_DIR must not contain whitespace: $build_dir" >&2
|
|
exit 1
|
|
fi
|
|
mkdir -p "$build_dir"
|
|
build_dir=$(cd "$build_dir" && pwd)
|
|
if [[ "$build_dir" == "/" ]]; then
|
|
echo "CNC_SIM_WASM_TOOLDATA_RUNTIME_SYMBOLS_BUILD_DIR must not be the filesystem root" >&2
|
|
exit 1
|
|
fi
|
|
exec 9>"$build_dir/tooldata-runtime-symbols.lock"
|
|
flock 9
|
|
|
|
preflight_cache_dir=${CNC_SIM_PREFLIGHT_CACHE_DIR:-"$build_dir/preflight-cache"}
|
|
LINUXCNC_ROOT="$linuxcnc_root" ./check-linuxcnc-inputs-cached.sh --cache-dir "$preflight_cache_dir" --root-only
|
|
linuxcnc_root=$(cd "$linuxcnc_root" && pwd)
|
|
|
|
tooldata_mmap_backend_shim=$(./list-linuxcnc-wasm-safe-shims.sh tooldata_mmap_backend.cc)
|
|
tooldata_runtime_stubs_shim=$(./list-linuxcnc-wasm-safe-shims.sh tooldata_runtime_stubs.cc)
|
|
linuxcnc_source_output=$(LINUXCNC_ROOT="$linuxcnc_root" ./list-linuxcnc-source-files.sh \
|
|
src/emc/tooldata/tooldata_db.cc \
|
|
src/emc/tooldata/tooldata_nml.cc)
|
|
mapfile -t linuxcnc_sources <<<"$linuxcnc_source_output"
|
|
tooldata_db_source=${linuxcnc_sources[0]}
|
|
tooldata_nml_source=${linuxcnc_sources[1]}
|
|
|
|
common_flag_output=$(LINUXCNC_ROOT="$linuxcnc_root" ./list-linuxcnc-wasm-common-cxxflags.sh)
|
|
mapfile -t common_flags <<<"$common_flag_output"
|
|
|
|
sources=(
|
|
"$tooldata_runtime_stubs_shim"
|
|
"$tooldata_mmap_backend_shim"
|
|
"$tooldata_db_source"
|
|
"$tooldata_nml_source"
|
|
)
|
|
objects=(
|
|
"$build_dir/tooldata_runtime_stubs.o"
|
|
"$build_dir/tooldata_mmap_backend.o"
|
|
"$build_dir/linuxcnc_tooldata_db.o"
|
|
"$build_dir/linuxcnc_tooldata_nml.o"
|
|
)
|
|
|
|
signature="$build_dir/tooldata-runtime-symbols.signature"
|
|
next_signature="$build_dir/tooldata-runtime-symbols.signature.next"
|
|
{
|
|
printf 'CXX=%s\n' "$cxx"
|
|
printf 'PWD=%s\n' "$PWD"
|
|
printf 'LINUXCNC_ROOT=%s\n' "$linuxcnc_root"
|
|
printf 'BUILD_RULE_VERSION=1\n'
|
|
printf 'COMMON_FLAGS='
|
|
printf ' %s' "${common_flags[@]}"
|
|
printf '\n'
|
|
printf 'SOURCES:\n'
|
|
printf '%s\n' "${sources[@]}"
|
|
} > "$next_signature"
|
|
if [[ ! -f "$signature" ]] || ! cmp -s "$signature" "$next_signature"; then
|
|
rm -f "$build_dir"/*.o "$build_dir"/*.d
|
|
fi
|
|
mv "$next_signature" "$signature"
|
|
|
|
makefile="$build_dir/tooldata-runtime-symbols.mk"
|
|
{
|
|
printf 'CXX := %s\n' "$cxx"
|
|
printf 'COMMON_FLAGS :='
|
|
printf ' %s' "${common_flags[@]}"
|
|
printf '\n\n'
|
|
printf 'OBJECTS :='
|
|
printf ' %s' "${objects[@]}"
|
|
printf '\n\n'
|
|
printf 'DEP_FILES :=\n'
|
|
for object in "${objects[@]}"; do
|
|
printf 'DEP_FILES += %s.d\n' "$object"
|
|
done
|
|
printf '\n.PHONY: all\n'
|
|
printf 'all: $(OBJECTS)\n\n'
|
|
|
|
object_index=0
|
|
for source in "${sources[@]}"; do
|
|
printf '%s: %s\n' "${objects[$object_index]}" "$source"
|
|
printf '\t@$(CXX) $(COMMON_FLAGS) -MMD -MP -MF %s.d -c %s -o %s\n\n' "${objects[$object_index]}" "$source" "${objects[$object_index]}"
|
|
object_index=$((object_index + 1))
|
|
done
|
|
printf '\n-include $(DEP_FILES)\n'
|
|
} > "$makefile"
|
|
make --output-sync=target -j"$build_jobs" -f "$makefile" >/dev/null
|
|
|
|
symbols_signature="$build_dir/tooldata-runtime-symbols.nm.signature"
|
|
symbols_next_signature="$build_dir/tooldata-runtime-symbols.nm.signature.next"
|
|
{
|
|
printf 'SYMBOL_RULE_VERSION=1\n'
|
|
stat -c 'OBJECT=%n:%s:%y' "${objects[@]}"
|
|
} > "$symbols_next_signature"
|
|
if [[ ! -f "$symbols_signature" ||
|
|
! -f "$build_dir/runtime.exports" ||
|
|
! -f "$build_dir/backend.exports" ||
|
|
! -f "$build_dir/linuxcnc-runtime.exports" ]] ||
|
|
! cmp -s "$symbols_signature" "$symbols_next_signature"; then
|
|
nm --defined-only "$build_dir/tooldata_runtime_stubs.o" \
|
|
| awk '$2 ~ /^[A-Z]$/ {print $3}' \
|
|
| LC_ALL=C sort -u \
|
|
> "$build_dir/runtime.exports"
|
|
|
|
nm --defined-only "$build_dir/tooldata_mmap_backend.o" \
|
|
| awk '$2 ~ /^[A-Z]$/ {print $3}' \
|
|
| LC_ALL=C sort -u \
|
|
> "$build_dir/backend.exports"
|
|
|
|
nm --defined-only "$build_dir"/linuxcnc_tooldata_*.o \
|
|
| awk '$2 ~ /^[A-Z]$/ {print $3}' \
|
|
| LC_ALL=C sort -u \
|
|
> "$build_dir/linuxcnc-runtime.exports"
|
|
mv "$symbols_next_signature" "$symbols_signature"
|
|
else
|
|
rm -f "$symbols_next_signature"
|
|
fi
|
|
|
|
comm -23 "$build_dir/linuxcnc-runtime.exports" "$build_dir/backend.exports" \
|
|
> "$build_dir/expected.exports"
|
|
|
|
comm -23 "$build_dir/expected.exports" "$build_dir/runtime.exports" > "$build_dir/missing.exports"
|
|
if [[ -s "$build_dir/missing.exports" ]]; then
|
|
echo "missing wasm tooldata runtime stub exports from LinuxCNC runtime sources:" >&2
|
|
cat "$build_dir/missing.exports" >&2
|
|
exit 1
|
|
fi
|
|
|
|
comm -13 "$build_dir/expected.exports" "$build_dir/runtime.exports" > "$build_dir/unexpected.exports"
|
|
if [[ -s "$build_dir/unexpected.exports" ]]; then
|
|
echo "unexpected wasm tooldata runtime stub exports beyond LinuxCNC runtime sources:" >&2
|
|
cat "$build_dir/unexpected.exports" >&2
|
|
exit 1
|
|
fi
|
|
|
|
echo "linuxcnc wasm tooldata runtime symbol probe passed"
|