#!/usr/bin/env bash set -euo pipefail cd "$(dirname "$0")" linuxcnc_root=${LINUXCNC_ROOT:-../linuxcnc} cxx=${CXX:-g++} build_dir=${TMPDIR:-/tmp}/cnc_sim_rs274_source_link python_includes=$(python3.13-config --includes 2>/dev/null || python3-config --includes) rm -rf "$build_dir" mkdir -p "$build_dir" common_flags=( -std=c++17 -DULAPI $python_includes -I core/include -I core/src -I "$linuxcnc_root/src" -I "$linuxcnc_root/src/emc" -I "$linuxcnc_root/src/emc/nml_intf" -I "$linuxcnc_root/src/emc/rs274ngc" -I "$linuxcnc_root/src/emc/motion" -I "$linuxcnc_root/src/emc/pythonplugin" -I "$linuxcnc_root/include" ) objects=() while IFS=: read -r group path note; do case "$group" in core) obj="$build_dir/$(basename "$path" .cc).o" "$cxx" "${common_flags[@]}" -c "$linuxcnc_root/$path" -o "$obj" objects+=("$obj") ;; ""|\#*|binding|tooldata) ;; *) echo "unknown manifest group: $group" >&2 exit 1 ;; esac done < linuxcnc-rs274-source-files.txt for source in \ core/src/canon_event_sink.cpp \ core/src/linuxcnc_canon_bridge.cpp \ core/src/linuxcnc_tooldata_fixture.cpp \ core/src/rtcp_kinematics.cpp \ core/src/simulator_gcode_controls.cpp \ core/tools/linuxcnc_rs274_dump.cpp; do obj="$build_dir/$(basename "$source" .cpp).o" "$cxx" "${common_flags[@]}" -c "$source" -o "$obj" objects+=("$obj") done support_objects=( "$linuxcnc_root/src/objects/emc/rs274ngc/interpmodule.o" "$linuxcnc_root/src/objects/emc/rs274ngc/canonmodule.o" "$linuxcnc_root/src/objects/emc/rs274ngc/pyarrays.o" "$linuxcnc_root/src/objects/emc/rs274ngc/pyblock.o" "$linuxcnc_root/src/objects/emc/rs274ngc/pyemctypes.o" "$linuxcnc_root/src/objects/emc/rs274ngc/pyinterp1.o" "$linuxcnc_root/src/objects/emc/rs274ngc/pyparamclass.o" "$linuxcnc_root/src/objects/emc/nml_intf/emcops.o" "$linuxcnc_root/src/objects/emc/sai/dummyemcstat.o" "$linuxcnc_root/src/objects/libnml/nml/stat_msg.o" ) for obj in "${support_objects[@]}"; do if [[ ! -f "$obj" ]]; then echo "missing LinuxCNC support object: $obj" >&2 echo "build LinuxCNC first or set LINUXCNC_ROOT to a built tree" >&2 exit 1 fi done "$cxx" "${objects[@]}" "${support_objects[@]}" \ -L "$linuxcnc_root/lib" \ -Wl,-rpath,"$linuxcnc_root/lib" \ -llinuxcnc-uspace-posix \ -llinuxcncini \ -llinuxcnchal \ -lpyplugin \ -ltooldata \ -lnml \ -lposemath \ -lboost_python313 \ -lpython3.13 \ -lfmt \ -ldl \ -o "$build_dir/linuxcnc_rs274_source_dump" var_file="$build_dir/rs274ngc-source.var" base_var_file="$build_dir/rs274ngc-source-base.var" cp "$linuxcnc_root/tests/halui/jogging/sim.var" "$base_var_file" cp "$base_var_file" "$var_file" CNC_SIM_RS274_VAR="$var_file" \ "$build_dir/linuxcnc_rs274_source_dump" tests/gcode/basic_mill.ngc \ >/tmp/cnc_sim_linuxcnc_source_basic_mill.json CNC_SIM_RS274_VAR="$var_file" \ "$build_dir/linuxcnc_rs274_source_dump" tests/gcode/linuxcnc_rtcp_controls.ngc \ >/tmp/cnc_sim_linuxcnc_source_rtcp_controls.json CNC_SIM_RS274_VAR="$var_file" \ "$build_dir/linuxcnc_rs274_source_dump" tests/gcode/linuxcnc_spindle_direction.ngc \ >/tmp/cnc_sim_linuxcnc_source_spindle_direction.json CNC_SIM_RS274_VAR="$var_file" \ "$build_dir/linuxcnc_rs274_source_dump" tests/gcode/linuxcnc_tool_number.ngc \ >/tmp/cnc_sim_linuxcnc_source_tool_number.json CNC_SIM_RS274_VAR="$var_file" \ "$build_dir/linuxcnc_rs274_source_dump" tests/gcode/linuxcnc_program_stops.ngc \ >/tmp/cnc_sim_linuxcnc_source_program_stops.json CNC_SIM_RS274_VAR="$var_file" \ "$build_dir/linuxcnc_rs274_source_dump" tests/gcode/linuxcnc_coolant.ngc \ >/tmp/cnc_sim_linuxcnc_source_coolant.json CNC_SIM_RS274_VAR="$var_file" \ "$build_dir/linuxcnc_rs274_source_dump" tests/gcode/linuxcnc_feed_modes.ngc \ >/tmp/cnc_sim_linuxcnc_source_feed_modes.json CNC_SIM_RS274_VAR="$var_file" \ "$build_dir/linuxcnc_rs274_source_dump" tests/gcode/linuxcnc_motion_modes.ngc \ >/tmp/cnc_sim_linuxcnc_source_motion_modes.json CNC_SIM_RS274_VAR="$var_file" \ "$build_dir/linuxcnc_rs274_source_dump" tests/gcode/linuxcnc_tool_length.ngc \ >/tmp/cnc_sim_linuxcnc_source_tool_length.json CNC_SIM_RS274_VAR="$var_file" \ "$build_dir/linuxcnc_rs274_source_dump" tests/gcode/linuxcnc_canned_cycle.ngc \ >/tmp/cnc_sim_linuxcnc_source_canned_cycle.json CNC_SIM_RS274_VAR="$var_file" \ "$build_dir/linuxcnc_rs274_source_dump" tests/gcode/linuxcnc_canned_cycles_extended.ngc \ >/tmp/cnc_sim_linuxcnc_source_canned_cycles_extended.json CNC_SIM_RS274_VAR="$var_file" \ "$build_dir/linuxcnc_rs274_source_dump" tests/gcode/linuxcnc_cutter_comp.ngc \ >/tmp/cnc_sim_linuxcnc_source_cutter_comp.json CNC_SIM_RS274_VAR="$var_file" \ "$build_dir/linuxcnc_rs274_source_dump" tests/gcode/linuxcnc_probe_no_error.ngc \ >/tmp/cnc_sim_linuxcnc_source_probe_no_error.json CNC_SIM_RS274_FILE_MODE=1 CNC_SIM_RS274_VAR="$var_file" \ "$build_dir/linuxcnc_rs274_source_dump" tests/gcode/smoke_oword_subprogram.ngc \ >/tmp/cnc_sim_linuxcnc_source_oword_subprogram.json cp "$base_var_file" "$var_file" CNC_SIM_RS274_VAR="$var_file" \ "$build_dir/linuxcnc_rs274_source_dump" tests/gcode/linuxcnc_coordinate_offsets.ngc \ >/tmp/cnc_sim_linuxcnc_source_coordinate_offsets.json cp "$base_var_file" "$var_file" CNC_SIM_RS274_VAR="$var_file" \ "$build_dir/linuxcnc_rs274_source_dump" tests/gcode/linuxcnc_coordinate_l20.ngc \ >/tmp/cnc_sim_linuxcnc_source_coordinate_l20.json python3 - <<'PY' import json from pathlib import Path events = json.loads(Path("/tmp/cnc_sim_linuxcnc_source_basic_mill.json").read_text()) types = {event["type"] for event in events} required = { "set-units", "set-plane", "set-spindle", "tool-change", "rapid", "set-feed", "linear-feed", "arc-feed", "program-end", } missing = sorted(required - types) if missing: raise SystemExit("missing source-linked events: " + ", ".join(missing)) final_motion = [event for event in events if event["type"] in {"rapid", "linear-feed", "arc-feed"}][-1] if final_motion["end"]["x"] != 0 or final_motion["end"]["y"] != 0: raise SystemExit("unexpected source-linked final XY position") controls = json.loads(Path("/tmp/cnc_sim_linuxcnc_source_rtcp_controls.json").read_text()) kin = [event for event in controls if event["type"] == "kinematics-switch"] if [(event["reserved"], event["feed"]) for event in kin] != [(1, 1), (0, 0), (2, 1)]: raise SystemExit("unexpected source-linked kinematics switch sequence") rtcp = [event for event in controls if event["type"] == "rtcp-state"] if [(event["line"], event["tool"], event["feed"]) for event in rtcp] != [(3, 0, 0), (4, 7, 1)]: raise SystemExit("unexpected source-linked RTCP state sequence") spindle = json.loads(Path("/tmp/cnc_sim_linuxcnc_source_spindle_direction.json").read_text()) spindle_states = [ (event["line"], event["spindle"], event["reserved"]) for event in spindle if event["type"] == "set-spindle" ] if (2, 1200, 1) not in spindle_states: raise SystemExit("missing source-linked M3 clockwise spindle state") if (3, 1200, 2) not in spindle_states: raise SystemExit("missing source-linked M4 counterclockwise spindle state") if (4, 0, 0) not in spindle_states: raise SystemExit("missing source-linked M5 stopped spindle state") tool_number = json.loads(Path("/tmp/cnc_sim_linuxcnc_source_tool_number.json").read_text()) if not any( event["type"] == "tool-change" and event["line"] == 2 and event["tool"] == 1 for event in tool_number ): raise SystemExit("missing source-linked M61 Q1 tool-number event line") stops = json.loads(Path("/tmp/cnc_sim_linuxcnc_source_program_stops.json").read_text()) stop_states = [ (event["line"], event["reserved"]) for event in stops if event["type"] == "program-end" ] for expected in [(2, 1), (3, 2), (4, 3), (4, 1), (5, 0)]: if expected not in stop_states: raise SystemExit(f"missing source-linked program stop state {expected!r}") coolant = json.loads(Path("/tmp/cnc_sim_linuxcnc_source_coolant.json").read_text()) coolant_states = [ (event["line"], event["reserved"]) for event in coolant if event["type"] == "comment" ] for expected in [(2, 11), (3, 21), (4, 10), (4, 20)]: if expected not in coolant_states: raise SystemExit(f"missing source-linked coolant state {expected!r}") feed_modes = json.loads(Path("/tmp/cnc_sim_linuxcnc_source_feed_modes.json").read_text()) feed_mode_comments = [ (event["line"], event["reserved"]) for event in feed_modes if event["type"] == "comment" ] for expected in [(2, 93), (3, 94), (4, 95)]: if expected not in feed_mode_comments: raise SystemExit(f"missing source-linked feed mode comment state {expected!r}") feed_mode_events = [ (event["line"], event["reserved"]) for event in feed_modes if event["type"] == "set-feed" ] for expected in [(3, 0), (4, 0)]: if expected not in feed_mode_events: raise SystemExit(f"missing source-linked feed mode event {expected!r}") motion_modes = json.loads(Path("/tmp/cnc_sim_linuxcnc_source_motion_modes.json").read_text()) motion_mode_states = [ (event["line"], event["reserved"], event["feed"]) for event in motion_modes if event["type"] == "comment" ] for expected in [(2, 611, 0), (3, 612, 0), (4, 640, 0.05)]: if expected not in motion_mode_states: raise SystemExit(f"missing source-linked motion mode state {expected!r}") tool_length = json.loads(Path("/tmp/cnc_sim_linuxcnc_source_tool_length.json").read_text()) if not any( event["type"] == "comment" and event["line"] == 2 and event["reserved"] == 430 and event["start"]["z"] == 12.5 for event in tool_length ): raise SystemExit("missing source-linked G43 H1 tool length offset event") if not any( event["type"] == "comment" and event["line"] == 3 and event["reserved"] == 430 and event["start"]["z"] == 0 for event in tool_length ): raise SystemExit("missing source-linked G49 tool length clear event") cycle = json.loads(Path("/tmp/cnc_sim_linuxcnc_source_canned_cycle.json").read_text()) motions = [event for event in cycle if event["type"] in {"rapid", "linear-feed"}] expected_cycle = [ ("rapid", 0, 0, 5), ("rapid", 20, 0, 5), ("rapid", 20, 0, 3), ("linear-feed", 20, 0, -2), ("rapid", 20, 0, 3), ] actual_cycle = [(event["type"], event["end"]["x"], event["end"]["y"], event["end"]["z"]) for event in motions] if actual_cycle != expected_cycle: raise SystemExit(f"unexpected source-linked G81/G80 expansion: {actual_cycle!r}") extended_cycle = json.loads(Path("/tmp/cnc_sim_linuxcnc_source_canned_cycles_extended.json").read_text()) if not any( event["type"] == "linear-feed" and event["line"] == 3 and event["end"]["x"] == 10 and event["end"]["z"] == -2 for event in extended_cycle ): raise SystemExit("missing source-linked G82 feed to bottom") if not any( event["type"] == "dwell" and event["line"] == 3 and event["dwellSeconds"] == 0.25 for event in extended_cycle ): raise SystemExit("missing source-linked G82 dwell at bottom") g83_feed_depths = [ event["end"]["z"] for event in extended_cycle if event["type"] == "linear-feed" and event["line"] == 4 ] if g83_feed_depths != [1, 0, -1, -2, -3]: raise SystemExit(f"unexpected source-linked G83 peck depths: {g83_feed_depths!r}") if not any( event["type"] == "rapid" and event["line"] == 4 and event["end"]["x"] == 20 and event["end"]["z"] == 2 for event in extended_cycle ): raise SystemExit("missing source-linked G83 final retract to R plane") cutter_comp = json.loads(Path("/tmp/cnc_sim_linuxcnc_source_cutter_comp.json").read_text()) cutter_comp_motions = [ event for event in cutter_comp if event["type"] in {"rapid", "linear-feed", "arc-feed"} ] expected_cutter_comp = [ ("rapid", 2, 0, 0), ("linear-feed", 6, 17, 3), ("linear-feed", 7, 17, 20), ("linear-feed", 8, 0, 0), ("linear-feed", 11, 17, -3), ("linear-feed", 12, 17, -20), ("linear-feed", 13, 0, 0), ("linear-feed", 16, 17, 3), ("linear-feed", 17, 17, 20), ("linear-feed", 18, 0, 0), ("linear-feed", 21, 17, -3), ("linear-feed", 22, 17, -20), ("linear-feed", 23, 0, 0), ] actual_cutter_comp = [ (event["type"], event["line"], event["end"]["x"], event["end"]["y"]) for event in cutter_comp_motions ] if actual_cutter_comp != expected_cutter_comp: raise SystemExit(f"unexpected source-linked cutter compensation path: {actual_cutter_comp!r}") probe = json.loads(Path("/tmp/cnc_sim_linuxcnc_source_probe_no_error.json").read_text()) if not any( event["type"] == "probe" and event["line"] == 4 and event["feed"] == 100 and event["start"]["z"] == 5 and event["end"]["z"] == 0 for event in probe ): raise SystemExit("missing source-linked G38.3 probe event") if any(event["type"] == "linear-feed" and event["line"] == 4 for event in probe): raise SystemExit("source-linked G38.3 probe should not be emitted as linear-feed") oword = json.loads(Path("/tmp/cnc_sim_linuxcnc_source_oword_subprogram.json").read_text()) if not any( event["type"] == "linear-feed" and event["line"] == 8 and event["start"]["x"] == 0 and event["end"]["x"] == 7 for event in oword ): raise SystemExit("missing source-linked O-word expression motion") if not any( event["type"] == "linear-feed" and event["line"] == 9 and event["start"]["y"] == 0 and event["end"]["y"] == 2 for event in oword ): raise SystemExit("missing source-linked O-word #2 argument motion") if any( event["type"] == "linear-feed" and event["line"] == 11 and event["end"]["x"] == 99 for event in oword ): raise SystemExit("source-linked O-word return should skip remaining subprogram body") coords = json.loads(Path("/tmp/cnc_sim_linuxcnc_source_coordinate_offsets.json").read_text()) g5x = [event for event in coords if event["type"] == "set-g5x-offset"] g92 = [event for event in coords if event["type"] == "set-g92-offset"] rot = [event for event in coords if event["type"] == "set-xy-rotation"] if not any( event["line"] == 2 and event["tool"] == 1 and event["start"]["x"] == 12.5 and event["start"]["y"] == -3 and event["start"]["z"] == 4 for event in g5x ): raise SystemExit("missing source-linked G10 L2 G5X offset event") if not any(event["feed"] == 30 for event in rot): raise SystemExit("missing source-linked G10 L2 XY rotation event") if not any( event["line"] == 4 and event["start"]["x"] == 0 and event["start"]["y"] == 0 and event["start"]["z"] == 0 for event in g92 ): raise SystemExit("missing source-linked G92.1 clear offset event") if not any(event["type"] == "program-end" and event["line"] == 5 for event in coords): raise SystemExit("missing source-linked coordinate program end line number") l20 = json.loads(Path("/tmp/cnc_sim_linuxcnc_source_coordinate_l20.json").read_text()) if not any( event["type"] == "set-g5x-offset" and event["line"] == 4 and event["tool"] == 2 and event["start"]["x"] == 4 and event["start"]["y"] == 4 and event["start"]["z"] == 4 for event in l20 ): raise SystemExit("missing source-linked G10 L20 P2 offset event after G55 selection") if not any(event["type"] == "program-end" and event["line"] == 5 for event in l20): raise SystemExit("missing source-linked G10 L20 program end line number") PY echo "linuxcnc rs274 source link smoke passed (${#objects[@]} local objects)" echo "dumped /tmp/cnc_sim_linuxcnc_source_basic_mill.json" echo "dumped /tmp/cnc_sim_linuxcnc_source_rtcp_controls.json" echo "dumped /tmp/cnc_sim_linuxcnc_source_spindle_direction.json" echo "dumped /tmp/cnc_sim_linuxcnc_source_tool_number.json" echo "dumped /tmp/cnc_sim_linuxcnc_source_program_stops.json" echo "dumped /tmp/cnc_sim_linuxcnc_source_coolant.json" echo "dumped /tmp/cnc_sim_linuxcnc_source_feed_modes.json" echo "dumped /tmp/cnc_sim_linuxcnc_source_motion_modes.json" echo "dumped /tmp/cnc_sim_linuxcnc_source_tool_length.json" echo "dumped /tmp/cnc_sim_linuxcnc_source_canned_cycle.json" echo "dumped /tmp/cnc_sim_linuxcnc_source_canned_cycles_extended.json" echo "dumped /tmp/cnc_sim_linuxcnc_source_cutter_comp.json" echo "dumped /tmp/cnc_sim_linuxcnc_source_probe_no_error.json" echo "dumped /tmp/cnc_sim_linuxcnc_source_oword_subprogram.json" echo "dumped /tmp/cnc_sim_linuxcnc_source_coordinate_offsets.json" echo "dumped /tmp/cnc_sim_linuxcnc_source_coordinate_l20.json"