| name | build-pmars |
| description | Guide for building pMARS (Portable Memory Array Redcode Simulator) and similar software from Debian/Ubuntu source packages. Use this skill when tasks involve enabling source repositories, downloading distribution source packages, removing X11/GUI dependencies, modifying Makefiles, diagnosing segmentation faults, and building headless versions of applications. Applies to Core War simulators and similar legacy software with optional graphics support. |
Building pMARS from Distribution Sources
Overview
This skill provides a systematic approach for building pMARS (and similar software) from Debian/Ubuntu source packages with modifications such as removing X11 dependencies. It emphasizes proper diagnosis of build and runtime issues, verification at each step, and understanding root causes before applying fixes.
When to Use This Skill
Apply this skill when tasks require:
- Building pMARS or similar Core War simulators from source
- Downloading and extracting Debian/Ubuntu source packages
- Removing X11/GUI dependencies to create headless versions
- Modifying Makefiles to change build flags or library linkage
- Diagnosing and fixing segmentation faults in built software
- Working with DEB822-format apt source configurations
Core Workflow
Phase 1: Enable Source Repositories
Modern Debian/Ubuntu systems use DEB822 format for apt sources. Enable source packages correctly:
Check current apt sources format:
ls /etc/apt/sources.list.d/*.sources
cat /etc/apt/sources.list.d/debian.sources
Enable sources in DEB822 format:
grep "^Types:" /etc/apt/sources.list.d/debian.sources
sed -i 's/^Types: deb$/Types: deb deb-src/' /etc/apt/sources.list.d/debian.sources
Enable sources in traditional format:
sed -i 's/^#deb-src/deb-src/' /etc/apt/sources.list
Update package cache:
apt-get update
Verification - critical step:
cat /etc/apt/sources.list.d/debian.sources
apt-cache policy
Common pitfall: File edits may be truncated or malformed. Always read back the modified file to verify correctness before proceeding.
Phase 2: Download Source Package
Install required tools and download the source:
apt-get install -y dpkg-dev
cd /app
apt-get source pmars
What gets created:
pmars_<version>.orig.tar.xz - Original upstream source
pmars_<version>.debian.tar.xz - Debian patches and metadata
pmars_<version>.dsc - Package description file
pmars-<version>/ - Extracted and patched source directory
Verification:
ls -la /app/pmars-*/
ls /app/pmars-*/src/
Phase 3: Review Debian Patches First
Before making modifications, examine existing Debian patches:
cd /app/pmars-*/
ls debian/patches/
cat debian/patches/series
cat debian/patches/*.patch
Why this matters:
- Debian maintainers have often fixed known issues
- Understanding existing patches prevents duplicate work
- Some patches may reveal build system quirks
- Conflicts with custom modifications can be avoided
Phase 4: Identify Build Configuration
Locate and understand the build configuration:
cd /app/pmars-*/src/
head -100 Makefile | grep "^#"
grep -E "^CFLAGS|^LIBS|^LIB|^CC" Makefile
grep -i "x11\|xwin\|graphx" Makefile
Common pMARS Makefile structure:
CFLAGS += -O -DEXT94 -DXWINGRAPHX
LIB = -L/usr/X11R6/lib -lX11
Phase 5: Remove X11 Dependencies
Modify CFLAGS to remove graphics defines:
Remove or comment out library linkage:
Verification after edit:
grep -E "^CFLAGS|^LIB" Makefile
grep -i "x11\|xwin" Makefile
Common pitfall: Edits may not apply correctly or may be truncated. Always verify the modified section by reading it back.
Phase 6: Build the Software
cd /app/pmars-*/src/
make clean
make
Handle compiler warnings appropriately:
- Warnings in C code may indicate real bugs
- Examine warnings rather than dismissing them
- Common legacy code warnings:
- Implicit function declarations
- Unused variables
- Comparison between signed/unsigned
Verification:
ls -lh pmars
ldd pmars | grep -i x11
./pmars --help 2>&1 || ./pmars 2>&1 | head -5
Phase 7: Install and Test
Install to target location:
cp /app/pmars-*/src/pmars /usr/local/bin/pmars
chmod +x /usr/local/bin/pmars
ls -lh /usr/local/bin/pmars
which pmars
Progressive testing approach:
-
Test minimal functionality first:
/usr/local/bin/pmars --help
/usr/local/bin/pmars file1.red file2.red
-
Add flags incrementally:
/usr/local/bin/pmars -b file1.red file2.red
/usr/local/bin/pmars -b -r 50 file1.red file2.red
/usr/local/bin/pmars -b -r 50 -f file1.red file2.red
-
Identify problematic flags:
- If a flag causes crashes, note which flag
- Determine if output requirements can be met without problematic flag
- Investigate root cause before applying fixes
Debugging Segmentation Faults
When encountering crashes, follow this systematic approach:
Step 1: Establish Baseline
/usr/local/bin/pmars file1.red file2.red
/usr/local/bin/pmars -b file1.red file2.red
/usr/local/bin/pmars -b -r 50 file1.red file2.red
Step 2: Use Debugger for Diagnosis
apt-get install -y gdb
gdb -batch -ex "run -b -r 50 -f file1.red file2.red" -ex "bt" /usr/local/bin/pmars
gdb /usr/local/bin/pmars
(gdb) run -b -r 50 -f file1.red file2.red
(gdb) bt
Step 3: Analyze the Discrepancy
Critical insight: If a crash occurs standalone but not under gdb, this indicates:
- Undefined behavior (memory layout differences)
- Timing-sensitive issues
- Uninitialized memory being used
- Memory corruption
Do not assume a simple NULL check will fix the issue. Investigate:
- What variable is NULL or uninitialized?
- Why is it in that state?
- Is this a symptom of a deeper initialization problem?
Step 4: Understand Before Fixing
Before applying source code fixes:
- Read relevant source code sections
- Understand the data flow - trace where problematic variables are initialized
- Check if Debian patches address the issue
- Document the root cause hypothesis
Example analysis:
Step 5: Apply Targeted Fix
Only after understanding root cause:
if (instBank != NULL) {
}
Verify the fix:
make clean && make
grep -A5 -B5 "your_fix" modified_file.c
/usr/local/bin/pmars -b -r 50 -f file1.red file2.red
Common Pitfalls and Solutions
Pitfall 1: File Edit Truncation
Problem: Edit operations may truncate strings or introduce errors.
Prevention:
cat modified_file | grep -A2 -B2 "modified_line"
Pitfall 2: Incomplete Diagnosis
Problem: Applying speculative fixes without confirming root cause.
Prevention:
- Use debugger to get exact crash location
- Read source code to understand context
- Document hypothesis before fixing
- If behavior differs between gdb and standalone, investigate why
Pitfall 3: Bypassing Debian Build System
Problem: Modifying upstream Makefile directly may miss Debian patches.
Consideration:
cat debian/rules
dpkg-buildpackage -us -uc -b
Pitfall 4: Ignoring Compiler Warnings
Problem: Warnings in C code may indicate real bugs.
Prevention:
make 2>&1 | grep -i warning
make CFLAGS="-Wall -Wextra"
Pitfall 5: Not Verifying Test Files
Problem: Test input files may be malformed.
Prevention:
ls -l file1.red file2.red
head file1.red
Priority Framework
When working on build tasks:
P0 - Must Complete:
- Binary builds successfully
- Binary installs to correct location
- Core functionality works (basic execution)
- Required output format matches specification
P1 - Important:
- All specified flags work correctly
- No X11 dependencies in final binary
- Clean build without errors
P2 - Nice to Have:
- No compiler warnings
- All optional features working
- Perfect adherence to original test command
Apply framework: If a specific flag causes issues but core output requirement can be met without it, evaluate whether the flag is truly required or just part of an example command.
Verification Checklist
Before marking task complete:
Quick Reference
sed -i 's/^Types: deb$/Types: deb deb-src/' /etc/apt/sources.list.d/debian.sources
apt-get update
apt-get install -y dpkg-dev
cd /app && apt-get source pmars
cd /app/pmars-*/src/
make clean && make
ldd pmars | grep -i x11
cp pmars /usr/local/bin/pmars
chmod +x /usr/local/bin/pmars
/usr/local/bin/pmars file1.red file2.red
/usr/local/bin/pmars -b -r 50 file1.red file2.red