update makefile
[musl-tables] / makedecls.sh
1 #!/bin/sh
2
3 set -eu
4
5 export LC_ALL=C
6
7 # musl repo dir
8 MUSL="${MUSL:-../musl}"
9
10 ALL='
11 aarch64
12 arm
13 i386
14 microblaze
15 mips
16 mips64
17 mipsn32
18 or1k
19 powerpc
20 powerpc64
21 sh
22 x32
23 x86_64
24 '
25
26 ARCH="${ARCH:-$ALL}"
27
28 # install headers to /tmp/T.$arch
29 for arch in $ARCH
30 do
31 [ -e T.$arch ] && continue
32 make -f "$MUSL"/Makefile install-headers srcdir="$MUSL" prefix=/tmp/T.$arch ARCH=$arch
33 rm -rf obj/include/bits
34 done
35 rm -rf obj lib
36
37 # run ctags on headers
38 for arch in $ARCH
39 do
40 [ -e /tmp/T.$arch/musl.tags ] && continue
41 (
42 cd /tmp/T.$arch/include
43 ctags -f ../musl.tags -R -n -u --language-force=c --c-kinds=pxdstuve --fields=k .
44 # fix wchar_t bug of ctags (not ok for c++)
45 awk '/typedef.* wchar_t/{print "wchar_t\tbits/alltypes.h\t" NR ";\"\tt"}' bits/alltypes.h >>../musl.tags
46 )
47 done
48
49 # add declarations (slow)
50 for arch in $ARCH
51 do
52 [ -e /tmp/T.$arch/musl.decls ] && continue
53 (
54 cd /tmp/T.$arch/include
55 awk '
56 BEGIN { FS="\t" }
57
58 function decl(t,h,n) {
59         cmd = "awk '\''NR==" n
60         if (t ~ /[pxt]/)
61                 cmd = cmd "{s=$0; if(s!~/;/){getline; s=s \" \" $0} print s; exit}"
62         else if (t == "d")
63                 cmd = cmd "{s=$0; while(gsub(/\\\\$/,\"\",s)){getline; s=s $0} print s; exit}"
64         else
65                 return ""
66         cmd = cmd "'\'' " h
67         cmd | getline s
68         close(cmd)
69         gsub(/\t/, " ", s)
70         gsub(/ +/, " ", s)
71         if (t == "p")
72                 gsub(/ \(/, "(", s)
73         return s
74 }
75 /^[^!]/ {
76         gsub(/[^0-9]*/,"",$3)
77         if ($4 == "s")
78                 $1 = "struct " $1
79         if ($4 == "u")
80                 $1 = "union " $1
81 #       print $1 "\t" $2 "\t" $4 "\t" $3 "\t" decl($4,$2,$3)
82         # without line number
83         print $1 "\t" $2 "\t" $4 "\t" decl($4,$2,$3)
84 }' ../musl.tags >../musl.decls.raw
85
86 # fix ups
87 awk '
88 BEGIN { FS="\t" }
89 $3=="d" && $4 ~ /^#undef/ {next}
90 $3=="x" && $4 ~ /^(struct|union) [_0-9a-zA-Z]*;$/ {
91         a = ($4 ~ /^struct/) ? "struct " : "union "
92         b = ($4 ~ /^struct/) ? "S" : "U"
93         print a $1 "\t" $2 "\t" b "\t" $4
94         next
95 }
96 $1~/^(FILE|DIR)$/ {
97         print $1 "\t" $2 "\tT\t" $4
98         next
99 }
100 { print $0 }' ../musl.decls.raw | sort >../musl.decls
101 )
102 done
103
104 # add decls to data/
105 for arch in $ARCH
106 do
107 grep '  bits/' /tmp/T.$arch/musl.decls >data/musl.$arch.decls
108 done
109 for arch in $ARCH
110 do
111 grep -v '       bits/' /tmp/T.$arch/musl.decls >data/musl.generic.decls
112 break
113 done