Skip to content

Commit bf7c871

Browse files
authored
Merge pull request #148 from staalmannen/claude/upgrade-ape-c-library-mmZGd
Claude/upgrade ape c library mm z gd
2 parents 4f5898e + 5d45b14 commit bf7c871

10 files changed

Lines changed: 200 additions & 50 deletions

File tree

sys/include/ape/exitfail.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#ifndef _EXITFAIL_H
2+
#define _EXITFAIL_H
3+
4+
#pragma lib "/$M/lib/ape/libap.a"
5+
6+
#ifdef __cplusplus
7+
extern "C" {
8+
#endif
9+
10+
extern int volatile exit_failure;
11+
12+
#ifdef __cplusplus
13+
}
14+
#endif
15+
16+
#endif /* _EXITFAIL_H */

sys/src/ape/cmd/bison/mkfile

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,12 @@ LIB_OBJS=\
6464
# libbison_a-areadlink.$O \
6565
libbison_a-argmatch.$O \
6666
# libbison_a-gl_array_list.$O \
67-
# libbison_a-basename-lgpl.$O \
67+
libbison_a-basename-lgpl.$O \
6868
# libbison_a-binary-io.$O \
6969
# libbison_a-bitrotate.$O \
7070
libbison_a-bitset.$O \
7171
# libbison_a-array.$O \
72-
# libbison_a-stats.$O \
72+
libbison_a-stats.$O \
7373
# libbison_a-table.$O \
7474
# libbison_a-list.$O \
7575
# libbison_a-vector.$O \
@@ -88,15 +88,15 @@ LIB_OBJS=\
8888
# libbison_a-dirname-lgpl.$O \
8989
# libbison_a-stripslash.$O \
9090
libbison_a-execute.$O \
91-
libbison_a-exitfail.$O \
92-
# libbison_a-fatal-signal.$O \
91+
# libbison_a-exitfail.$O \
92+
libbison_a-fatal-signal.$O \
9393
# libbison_a-fd-safer-flag.$O \
9494
# libbison_a-dup-safer-flag.$O \
95-
# libbison_a-file-set.$O \
96-
# libbison_a-findprog-in.$O \
95+
libbison_a-file-set.$O \
96+
libbison_a-findprog-in.$O \
9797
libbison_a-fopen-safer.$O \
98-
# libbison_a-fstrcmp.$O \
99-
# libbison_a-gethrxtime.$O \
98+
libbison_a-fstrcmp.$O \
99+
libbison_a-gethrxtime.$O \
100100
# libbison_a-xtime.$O \
101101
# libbison_a-getprogname.$O \
102102
# libbison_a-gettime.$O \
@@ -125,17 +125,17 @@ LIB_OBJS=\
125125
libbison_a-quotearg.$O \
126126
# libbison_a-gl_rbtree_oset.$O \
127127
# libbison_a-gl_rbtreehash_list.$O \
128-
# libbison_a-scratch_buffer_dupfree.$O \
128+
libbison_a-scratch_buffer_dupfree.$O \
129129
libbison_a-scratch_buffer_grow.$O \
130130
libbison_a-scratch_buffer_grow_preserve.$O \
131131
libbison_a-scratch_buffer_set_array_size.$O \
132132
# libbison_a-sig-handler.$O \
133-
# libbison_a-spawn-pipe.$O \
133+
libbison_a-spawn-pipe.$O \
134134
# libbison_a-threadlib.$O \
135135
# libbison_a-timespec.$O \
136136
libbison_a-timevar.$O \
137137
# libbison_a-tls.$O \
138-
# libbison_a-unicodeio.$O \
138+
libbison_a-unicodeio.$O \
139139
libbison_a-unistd.$O \
140140
libbison_a-dup-safer.$O \
141141
# libbison_a-fd-safer.$O \
@@ -152,7 +152,7 @@ LIB_OBJS=\
152152
# libbison_a-xreadlink.$O \
153153
libbison_a-xsize.$O \
154154
# libbison_a-xstrndup.$O \
155-
# libbison_a-get-errno.$O \
155+
libbison_a-get-errno.$O \
156156
libbison_a-path-join.$O \
157157
liby_a-main.$O \
158158
liby_a-yyerror.$O

sys/src/ape/cmd/diff/mkfile

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,16 @@ TARG=\
1111
LIB= libdiff.a$O
1212

1313
LIBO=\
14-
sigsegv.$O stdbit.$O
14+
sigsegv.$O stdbit.$O\
15+
xstrtoimax.$O\
16+
quotearg.$O\
17+
diagnose.$O\
18+
version-etc.$O version-etc-fsf.$O\
19+
progname.$O\
20+
c-stack.$O\
21+
xstdopen.$O\
22+
propername-lite.$O\
23+
cmpbuf.$O
1524

1625
CMPO=\
1726
cmp.$O system.$O

sys/src/ape/cmd/gnulib-deps.sh

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
#!/bin/sh
2+
# gnulib-deps.sh: find gnulib object files needed for undefined symbols
3+
#
4+
# Usage (pipe mk link errors into it):
5+
# mk 2>&1 | sh gnulib-deps.sh LIBDIR PREFIX
6+
#
7+
# LIBDIR: path to the package's gnulib lib/ directory
8+
# PREFIX: object file prefix used in the mkfile
9+
#
10+
# Examples:
11+
# mk 2>&1 | sh gnulib-deps.sh ../../../external/bison/lib libbison_a
12+
# mk 2>&1 | sh gnulib-deps.sh ../../../external/m4/lib libm4_a
13+
# mk 2>&1 | sh gnulib-deps.sh ../../../external/ggrep/lib libgrep_a
14+
# mk 2>&1 | sh gnulib-deps.sh ../../../external/diffutils/lib libdiff_a
15+
# mk 2>&1 | sh gnulib-deps.sh ../../../external/patch/lib libpatch_a
16+
# mk 2>&1 | sh gnulib-deps.sh ../../../external/gtar/lib libtar_a
17+
#
18+
# Output: sorted unique list of " PREFIX-stem.$O \" lines ready to paste
19+
# into the mkfile's LIB_OBJS section (or to uncomment existing lines).
20+
#
21+
# Note: the script finds where each symbol is DEFINED using grep.
22+
# It may find false positives (callers, not definers) for some symbols.
23+
# Review output before applying, and iterate if new undefined symbols appear.
24+
25+
LIBDIR=$1
26+
PREFIX=$2
27+
28+
if [ -z "$LIBDIR" ] || [ -z "$PREFIX" ]; then
29+
echo "Usage: $0 LIBDIR PREFIX" >&2
30+
echo "" >&2
31+
echo "Examples:" >&2
32+
echo " mk 2>&1 | sh gnulib-deps.sh ../../../external/bison/lib libbison_a" >&2
33+
exit 1
34+
fi
35+
36+
if [ ! -d "$LIBDIR" ]; then
37+
echo "Error: LIBDIR '$LIBDIR' not found" >&2
38+
exit 1
39+
fi
40+
41+
# Extract undefined symbol names from Plan9 linker error format:
42+
# "caller: undefined: SYMBOL in func"
43+
# or standard ld format:
44+
# "undefined reference to `SYMBOL'"
45+
extract_symbols() {
46+
grep ': undefined:' | sed 's/.*: undefined: //;s/ in .*//'
47+
grep "undefined reference to" | sed "s/.*undefined reference to \`//;s/'.*//"
48+
}
49+
50+
# Find the .c file in LIBDIR that most likely DEFINES a given symbol.
51+
# Strategy:
52+
# 1. Direct file name match: foo_bar -> foo-bar.c or foo_bar.c
53+
# 2. Grep for the symbol as a function definition (not extern/typedef)
54+
find_defining_file() {
55+
sym=$1
56+
dash=$(echo "$sym" | tr '_' '-')
57+
58+
# Try direct name match in top-level and one level of subdirs
59+
for name in "$sym" "$dash"; do
60+
for f in "$LIBDIR/$name.c" "$LIBDIR"/*/"$name.c"; do
61+
[ -f "$f" ] && { echo "$f"; return; }
62+
done
63+
done
64+
65+
# Grep: find files containing the symbol as a likely definition.
66+
# A definition line typically has the symbol followed by '(' not preceded
67+
# by 'extern', 'typedef', '#define', or inside a comment.
68+
# We pick the file with the fewest lines (most focused module).
69+
best=""
70+
best_lines=9999999
71+
for f in $(grep -rln "\b${sym}\b *(" "$LIBDIR"/ 2>/dev/null); do
72+
# Skip if symbol only appears as extern or call — check for definition
73+
# by looking for the symbol at the start of a line or after a type
74+
if grep -qE "^[A-Za-z_*][A-Za-z0-9_ *]*[[:space:]]+\b${sym}\b[[:space:]]*(|$)" "$f" 2>/dev/null ||
75+
grep -qE "^\b${sym}\b[[:space:]]*\(" "$f" 2>/dev/null; then
76+
n=$(wc -l < "$f")
77+
if [ "$n" -lt "$best_lines" ]; then
78+
best_lines=$n
79+
best=$f
80+
fi
81+
fi
82+
done
83+
84+
# Fall back: just take any file containing the symbol
85+
if [ -z "$best" ]; then
86+
best=$(grep -rln "\b${sym}\b" "$LIBDIR"/ 2>/dev/null | head -1)
87+
fi
88+
89+
echo "$best"
90+
}
91+
92+
# Map a file path to the object stem (PREFIX-stem.$O)
93+
file_to_obj() {
94+
f=$1
95+
base=$(basename "$f" .c)
96+
echo " ${PREFIX}-${base}.\$O \\"
97+
}
98+
99+
# Main: extract symbols, find files, output object names
100+
extract_symbols | sort -u | while read sym; do
101+
[ -z "$sym" ] && continue
102+
103+
f=$(find_defining_file "$sym")
104+
105+
if [ -z "$f" ]; then
106+
printf "# NOT FOUND: %s\n" "$sym" >&2
107+
continue
108+
fi
109+
110+
file_to_obj "$f"
111+
done | sort -u
112+
113+
echo ""
114+
echo "# Paste the above lines into your mkfile's LIB_OBJS section." >&2
115+
echo "# Then run mk again and repeat until the build succeeds." >&2

sys/src/ape/cmd/grep/mkfile

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,16 @@ LIB_OBJS=\
2828
# libgreputils_a-chdir-long.$O \
2929
# libgreputils_a-cloexec.$O \
3030
# libgreputils_a-close-stream.$O \
31-
# libgreputils_a-closeout.$O \
32-
# libgreputils_a-cycle-check.$O \
31+
libgreputils_a-closeout.$O \
32+
libgreputils_a-cycle-check.$O \
3333
libgreputils_a-dfa.$O \
34-
# libgreputils_a-localeinfo.$O \
34+
libgreputils_a-localeinfo.$O \
3535
# libgreputils_a-dirname-lgpl.$O \
36-
# libgreputils_a-stripslash.$O \
36+
libgreputils_a-stripslash.$O \
3737
# libgreputils_a-dup2.$O \
3838
# libgreputils_a-error.$O \
39-
# libgreputils_a-exclude.$O \
40-
libgreputils_a-exitfail.$O \
39+
libgreputils_a-exclude.$O \
40+
# libgreputils_a-exitfail.$O \
4141
# libgreputils_a-fcntl.$O \
4242
# libgreputils_a-creat-safer.$O \
4343
# libgreputils_a-open-safer.$O \
@@ -51,10 +51,10 @@ LIB_OBJS=\
5151
# libgreputils_a-getcwd-lgpl.$O \
5252
# libgreputils_a-getopt.$O \
5353
# libgreputils_a-getopt1.$O \
54-
# libgreputils_a-dynarray_at_failure.$O \
55-
# libgreputils_a-dynarray_emplace_enlarge.$O \
56-
# libgreputils_a-dynarray_finalize.$O \
57-
# libgreputils_a-dynarray_resize.$O \
54+
libgreputils_a-dynarray_at_failure.$O \
55+
libgreputils_a-dynarray_emplace_enlarge.$O \
56+
libgreputils_a-dynarray_finalize.$O \
57+
libgreputils_a-dynarray_resize.$O \
5858
# libgreputils_a-dynarray_resize_clear.$O \
5959
# libgreputils_a-hard-locale.$O \
6060
libgreputils_a-hash.$O \
@@ -75,20 +75,20 @@ LIB_OBJS=\
7575
# libgreputils_a-mbsrtowcs.$O \
7676
# libgreputils_a-mbszero.$O \
7777
# libgreputils_a-mcel.$O \
78-
# libgreputils_a-memchr2.$O \
78+
libgreputils_a-memchr2.$O \
7979
# libgreputils_a-memrchr.$O \
8080
# libgreputils_a-nl_langinfo.$O \
8181
libgreputils_a-obstack.$O \
8282
# libgreputils_a-once.$O \
8383
# libgreputils_a-openat.$O \
8484
# libgreputils_a-openat-die.$O \
85-
# libgreputils_a-openat-safer.$O \
85+
libgreputils_a-openat-safer.$O \
8686
# libgreputils_a-opendirat.$O \
8787
# libgreputils_a-quotearg.$O \
8888
# libgreputils_a-rawmemchr.$O \
8989
libgreputils_a-reallocarray.$O \
9090
libgreputils_a-regex.$O \
91-
# libgreputils_a-safe-read.$O \
91+
libgreputils_a-safe-read.$O \
9292
libgreputils_a-same-inode.$O \
9393
# libgreputils_a-save-cwd.$O \
9494
# libgreputils_a-setlocale_null.$O \
@@ -114,13 +114,13 @@ LIB_OBJS=\
114114
# libgreputils_a-wctype.$O \
115115
# libgreputils_a-wctype-h.$O \
116116
libgreputils_a-wmempcpy.$O \
117-
libgreputils_a-xmalloc.$O \
118-
libgreputils_a-xalloc-die.$O \
117+
# libgreputils_a-xmalloc.$O \
118+
# libgreputils_a-xalloc-die.$O \
119119
# libgreputils_a-xbinary-io.$O \
120120
libgreputils_a-xstrtoimax.$O \
121121
libgreputils_a-xstrtol.$O \
122122
libgreputils_a-xstrtoul.$O \
123-
# libgreputils_a-colorize-posix.$O \
123+
libgreputils_a-colorize-posix.$O \
124124
libgreputils_a-version-etc-fsf.$O
125125

126126
LIB=libgrep.a$O

sys/src/ape/cmd/m4/mkfile

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,30 +22,29 @@ OFILES=\
2222
LIB_OBJS= \
2323
# asyncsafe-spin.$O\
2424
# basename-lgpl.$O\
25+
c-stack.$O\
2526
# c-ctype.$O\
26-
# c-stack.$O\
2727
# c-strcasecmp.$O\
2828
# canonicalize.$O\
2929
# canonicalize-lgpl.$O\
30-
# clean-temp.$O\
31-
# clean-temp-simple.$O\
32-
# close-stream.$O\
30+
clean-temp.$O\
31+
clean-temp-simple.$O\
32+
close-stream.$O\
3333
# closein.$O\
3434
# closeout.$O\
3535
# concat-filename.$O\
3636
# dup-safer.$O\
3737
# dup-safer-flag.$O\
3838
error.$O\
39-
# execute.$O\
40-
exitfail.$O\
41-
# fatal-signal.$O\
39+
execute.$O\
40+
fatal-signal.$O\
4241
# fd-safer.$O\
4342
# fd-safer-flag.$O\
44-
# filenamecat.$O\
43+
filenamecat.$O\
4544
# filenamecat-lgpl.$O\
4645
# file-set.$O\
4746
# findprog-in.$O\
48-
# fopen-safer.$O\
47+
fopen-safer.$O\
4948
# fpending.$O\
5049
# freadahead.$O\
5150
# fseterr.$O\
@@ -61,23 +60,23 @@ LIB_OBJS= \
6160
# mbchar.$O\
6261
# mbslen.$O\
6362
# mbsstr.$O\
64-
# memchr2.$O\
63+
memchr2.$O\
6564
mempcpy.$O\
6665
# mkdtemp.$O\
67-
# mkstemp-safer.$O\
66+
mkstemp-safer.$O\
6867
# malloca.$O\
6968
# next-prime.$O\
7069
# obstack.$O\
7170
# pipe-safer.$O\
7271
# pipe2-safer.$O\
7372
progname.$O\
74-
# propername.$O\
73+
propername.$O\
7574
quotearg.$O\
7675
# reallocarray.$O\
7776
regex.$O\
7877
# rmdir.$O\
7978
# secure_getenv.$O\
80-
# spawn-pipe.$O\
79+
spawn-pipe.$O\
8180
# string.$O\
8281
# strnlen1.$O\
8382
strsignal.$O\
@@ -86,17 +85,15 @@ LIB_OBJS= \
8685
# tmpdir.$O\
8786
# trim.$O\
8887
# vaszprintf.$O\
89-
# version-etc.$O\
90-
# version-etc-fsf.$O\
88+
version-etc.$O\
89+
version-etc-fsf.$O\
9190
# vfzprintf.$O\
9291
# vzprintf.$O\
93-
# wait-process.$O\
94-
xalloc-die.$O\
92+
wait-process.$O\
9593
xasprintf.$O\
96-
xmalloc.$O\
9794
xmalloca.$O\
9895
xmemdup0.$O\
99-
# xprintf.$O\
96+
xprintf.$O\
10097
xvasprintf.$O\
10198
# lib/malloc \
10299
# dynarray_at_failure.$O\

0 commit comments

Comments
 (0)