#!/usr/bin/env bash set -euo pipefail cd "$(dirname "$0")" linuxcnc_root=${LINUXCNC_ROOT:-../linuxcnc} cxx=${CXX:-g++} build_dir=$(mktemp -d "${TMPDIR:-/tmp}/cnc_sim_linuxcnc_wasm_rs274ngc_pre_link_blockers.XXXXXX") manifest=${1:-linuxcnc-rs274-wasm-source-files.txt} keep_report_file=0 cleanup_rs274ngc_pre_blocker_temps() { rm -rf "$build_dir" if [[ "$keep_report_file" -eq 0 && -n "${report_file:-}" ]]; then rm -f "$report_file" fi } trap cleanup_rs274ngc_pre_blocker_temps EXIT grep -Fx 'trap cleanup_rs274ngc_pre_blocker_temps EXIT' test-linuxcnc-wasm-rs274ngc-pre-link-blockers.sh >/dev/null if awk '/^} > "\$report_file" \|\| true$/{ found = 1 } END { exit !found }' test-linuxcnc-wasm-rs274ngc-pre-link-blockers.sh >/dev/null; then echo "rs274ngc_pre blocker scan still hides report pipeline failures with || true" >&2 exit 1 fi if ! awk ' index($0, "LINUXCNC_ROOT=\"$linuxcnc_root\" ./check-linuxcnc-inputs.sh \"$manifest\"") { check_line = NR } index($0, "report_file=$(mktemp ") { report_line = NR } END { exit !(check_line && report_line && check_line < report_line) } ' test-linuxcnc-wasm-rs274ngc-pre-link-blockers.sh; then echo "rs274ngc_pre blocker scan creates its report before input validation" >&2 exit 1 fi if ! awk ' index($0, "if [[ \"$keep_report_file\" -eq 0 && -n \"${report_file:-}\" ]]; then") { cleanup_line = NR } index($0, "keep_report_file=1") { keep_line = NR } index($0, "echo \"wrote $report_file\"") { wrote_line = NR } END { exit !(cleanup_line && keep_line && wrote_line && cleanup_line < keep_line && keep_line < wrote_line) } ' test-linuxcnc-wasm-rs274ngc-pre-link-blockers.sh; then echo "rs274ngc_pre blocker scan does not clean failed reports before preserving successful ones" >&2 exit 1 fi LINUXCNC_ROOT="$linuxcnc_root" ./check-linuxcnc-inputs.sh "$manifest" report_file=$(mktemp "${TMPDIR:-/tmp}/cnc_sim_linuxcnc_wasm_rs274ngc_pre_undefined.XXXXXX.txt") common_flag_output=$(LINUXCNC_ROOT="$linuxcnc_root" ./list-linuxcnc-wasm-common-cxxflags.sh --profile core20-sections) mapfile -t common_flags <<<"$common_flag_output" shim_output=$(./list-linuxcnc-wasm-safe-shims.sh) mapfile -t shim_sources <<<"$shim_output" project_source_output=$(./list-linuxcnc-wasm-safe-project-sources.sh) mapfile -t project_sources <<<"$project_source_output" source_output=$(LINUXCNC_ROOT="$linuxcnc_root" ./list-linuxcnc-wasm-manifest-sources.sh "$manifest" core full) mapfile -t linuxcnc_sources <<<"$source_output" shim_index=0 for source in "${shim_sources[@]}"; do obj="$build_dir/shim_${shim_index}.o" "$cxx" "${common_flags[@]}" -c "$source" -o "$obj" shim_index=$((shim_index + 1)) done project_index=0 for source in "${project_sources[@]}"; do obj="$build_dir/project_${project_index}.o" source_flags=() case "$source" in core/src/linuxcnc_corexykins_adapter.c|core/src/linuxcnc_genserfuncs_adapter.c|core/src/linuxcnc_xyzab_tdr_kins_adapter.c) source_flags+=(-fpermissive) ;; esac "$cxx" "${common_flags[@]}" "${source_flags[@]}" -c "$source" -o "$obj" project_index=$((project_index + 1)) done linuxcnc_index=0 for source in "${linuxcnc_sources[@]}"; do obj="$build_dir/linuxcnc_${linuxcnc_index}.o" "$cxx" "${common_flags[@]}" -c "$source" -o "$obj" linuxcnc_index=$((linuxcnc_index + 1)) done undefined_file="$build_dir/undefined.raw" defined_file="$build_dir/defined.raw" nm -u "$build_dir"/*.o \ | awk '{print $2}' \ | sed '/^$/d' \ | LC_ALL=C sort -u \ > "$undefined_file" nm --defined-only "$build_dir"/*.o \ | awk '{print $3}' \ | sed '/^$/d' \ | LC_ALL=C sort -u \ > "$defined_file" { comm -23 "$undefined_file" "$defined_file" \ | awk ' $0 ~ /^(_GLOBAL_OFFSET_TABLE_|_Unwind_Resume)$/ { next } $0 ~ /^(__assert_fail|__cxa_|__divdc3|__dso_handle|__errno_location|__gxx_personality_v0|__isoc23_|__muldc3|__stack_chk_fail)/ { next } $0 ~ /^(abort|access|acos|asin|atan2|atof|atoi|basename|cabs|calloc|ceil|close|closedir|copysign|cos|cosl|exit|exp|fclose|fdatasync|fdopen|feof|fflush|fgetc|fgets|fileno|floor|fmax|fmod|fopen|fprintf|fputs|free|freelocale|fseek|fstat|ftell|fwrite|getcwd|getenv|getpid|gettimeofday|hypot|islower|isprint|isspace|isupper|isxdigit|link|localtime|log|lstat|malloc|memchr|memcmp|memcpy|memmove|memset|mkstemp|nearbyint|newlocale|open|opendir|perror|pow|printf|pthread_mutex_lock|pthread_mutex_unlock|putc|puts|read|readdir|realpath|realloc|remove|rename|setlocale|sin|sinl|snprintf|sprintf|sqrt|sscanf|stat|stderr|stdout|strcasecmp|strcat|strchr|strcmp|strcpy|strdup|strerror|strlen|strncasecmp|strncat|strncmp|strncpy|strrchr|strspn|strstr|strtod|strtok_r|strtol|strtoul|tan|tanl|tolower|toupper|towlower|unlink|uselocale|vfprintf|vsnprintf|wordexp|wordfree)$/ { next } $0 ~ /^(_ZN3fmt|_ZSt|_ZNSt|_ZNKSt|_ZNKRSt|_ZNS|_ZTI|_ZTS|_ZTV|_Zdl|_Znwm|_Znam|_Zda|_ZdaPv|_ZdaPvm|_ZdlPvm|_ZTv|_ZTh)/ { next } { print } ' } > "$report_file" if grep -F "_ZN6Interp13read_commentEPcPiP12block_structPd" "$report_file" >/dev/null; then echo "unexpected unresolved read_comment after interp_read.cc" >&2 exit 1 fi if grep -F "_ZN6Interp15find_tool_indexEP5setupiPi" "$report_file" >/dev/null; then echo "unexpected unresolved find_tool_index after interp_find.cc" >&2 exit 1 fi if grep -F "_ZN6Interp16find_named_paramEPKcPiPd" "$report_file" >/dev/null; then echo "unexpected unresolved find_named_param after interp_namedparams.cc" >&2 exit 1 fi if grep -Fx "emcStatus" "$report_file" >/dev/null; then echo "unexpected unresolved emcStatus after shim" >&2 exit 1 fi if grep -Fx "gettext" "$report_file" >/dev/null; then echo "unexpected unresolved gettext after shim" >&2 exit 1 fi if grep -Fx "_task" "$report_file" >/dev/null; then echo "unexpected unresolved _task after runtime shim" >&2 exit 1 fi if grep -Fx "builtin_modules" "$report_file" >/dev/null; then echo "unexpected unresolved builtin_modules after runtime shim" >&2 exit 1 fi for hal_symbol in \ hal_get_param_value_by_name \ hal_get_pin_value_by_name \ hal_get_signal_value_by_name \ hal_init \ hal_ready; do if grep -Fx "$hal_symbol" "$report_file" >/dev/null; then echo "unexpected unresolved $hal_symbol after runtime shim" >&2 exit 1 fi done if [[ -s "$report_file" ]]; then echo "unexpected unresolved project symbols after rs274ngc_pre shim coverage:" >&2 sed -n '1,120p' "$report_file" >&2 exit 1 fi echo "linuxcnc wasm rs274ngc_pre link blocker scan passed" keep_report_file=1 echo "wrote $report_file"