-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathalgamize.sh
executable file
·131 lines (104 loc) · 3.35 KB
/
algamize.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/bin/bash
set -e -o nounset
DST_DIR="$1"
VERSION="$2"
mkdir -p "$DST_DIR"
SRC_DIR="src/"
PORTABLE_FILES=(portable8439)
DST_HEADER="$DST_DIR/portable8439.h"
DST_SOURCE="$DST_DIR/portable8439.c"
function remove_header_guard() {
# we reverse lines so that it is easier to detect the last endif to drop
tac | \
awk '
BEGIN{LAST_END_FOUND=0;}
/#endif/ && !LAST_END_FOUND { LAST_END_FOUND=1; next; }
/#.*_H*$/ { next; }
42
' | \
tac
}
function remove_local_imports() {
sed 's/#include ".*h"//'
}
function remove_double_blank_lines() {
cat -s
}
function make_everything_static() {
sed \
-e $'s/^\([^\ \t\#{}()\/]\)/static \\1/' \
-e 's/static static/static/' \
-e 's/static struct/struct/' \
-e 's/static typedef/typedef/' \
-e 's/static extern/extern/' \
-e 's/static const/const/'
}
function add_decl_spec() {
sed \
-e 's/^static /static PORTABLE_8439_DECL /' \
-e $'s/^\([^\ \t#{}()\/*]\)/PORTABLE_8439_DECL \\1/' \
-e 's/^PORTABLE_8439_DECL static/static/' \
-e 's/^PORTABLE_8439_DECL typedef/typedef/'
}
{
echo "// portable8439 $VERSION
// Source: https://github.com/DavyLandman/portable8439
// Licensed under CC0-1.0
// Contains poly1305-donna e6ad6e091d30d7f4ec2d4f978be1fcfcbce72781 (Public Domain)
#ifndef __PORTABLE_8439_H
#define __PORTABLE_8439_H
#if defined(__cplusplus)
extern \"C\" {
#endif
// provide your own decl specificier like "-DPORTABLE_8439_DECL=ICACHE_RAM_ATTR"
#ifndef PORTABLE_8439_DECL
#define PORTABLE_8439_DECL
#endif
"
for h in "${PORTABLE_FILES[@]}"; do
cat "$SRC_DIR/$h.h" | remove_header_guard
done | remove_double_blank_lines | add_decl_spec
echo "#if defined(__cplusplus)
}
#endif
#endif"
} > "$DST_HEADER"
{
echo "// portable8439 $VERSION
// Source: https://github.com/DavyLandman/portable8439
// Licensed under CC0-1.0
// Contains poly1305-donna e6ad6e091d30d7f4ec2d4f978be1fcfcbce72781 (Public Domain)
#include \"portable8439.h\""
for h in "chacha-portable/chacha-portable" "poly1305-donna/poly1305-donna"; do
echo "// ******* BEGIN: $h.h ********"
cat "$SRC_DIR/$h.h" | remove_header_guard | \
remove_local_imports | remove_double_blank_lines | \
make_everything_static | add_decl_spec
echo "// ******* END: $h.h ********"
done
function inline_src() {
remove_local_imports | \
remove_double_blank_lines | \
make_everything_static # | \
#add_decl_spec
}
echo "// ******* BEGIN: chacha-portable.c ********"
inline_src <"$SRC_DIR/chacha-portable/chacha-portable.c"
echo "// ******* END: chacha-portable.c ********"
DONNA_ROOT="$SRC_DIR/poly1305-donna"
function merge_donna_src() {
awk '
/#.*include "poly1305-donna-[0-9]+.h"/ { system("cat src/poly1305-donna/"$3); next; }
42
' "$DONNA_ROOT/poly1305-donna.c"
}
echo "// ******* BEGIN: poly1305-donna.c ********"
merge_donna_src | inline_src
echo "// ******* END: poly1305-donna.c ********"
for h in "${PORTABLE_FILES[@]}"; do
echo "// ******* BEGIN: $h.c ********"
cat "$SRC_DIR/$h.c" | remove_local_imports | \
remove_double_blank_lines | add_decl_spec
echo "// ******* END: $h.c ********"
done
} > "$DST_SOURCE"