-
Notifications
You must be signed in to change notification settings - Fork 268
/
amalgamation.sh
executable file
·307 lines (252 loc) · 8.33 KB
/
amalgamation.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#!/bin/bash
########################################################################
# Generates an "amalgamation build" for roaring. Inspired by similar
# script used by whefs.
########################################################################
SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )"
DESTINATION=${1:-.}
case $SCRIPTPATH in
(*\ *) echo "Path ($SCRIPTPATH) cannot contain whitespace"; exit 1 ;;
esac
timestamp=$(date -u +"%Y-%m-%dT%H:%M:%SZ") # capture to label files with their generation time
function newline {
echo ""
}
echo "We are about to amalgamate all CRoaring files into one source file."
echo "For rationale, see: https://www.sqlite.org/amalgamation.html"
echo "and: https://en.wikipedia.org/wiki/Single_Compilation_Unit"
newline
# Files that this script generates
#
AMAL_H="roaring.h"
AMAL_HH="roaring.hh"
AMAL_C="roaring.c"
DEMOC="amalgamation_demo.c"
DEMOCPP="amalgamation_demo.cpp"
# .h files for the official API => Order matters
#
ALL_PUBLIC_H="
$SCRIPTPATH/include/roaring/roaring_version.h
$SCRIPTPATH/include/roaring/portability.h
$SCRIPTPATH/include/roaring/roaring_types.h
$SCRIPTPATH/include/roaring/bitset/bitset.h
$SCRIPTPATH/include/roaring/roaring.h
$SCRIPTPATH/include/roaring/memory.h
$SCRIPTPATH/include/roaring/roaring64.h
"
# .hh header files for the C++ API wrapper => Order does not matter at present
#
ALL_PUBLIC_HH="
$SCRIPTPATH/cpp/roaring.hh
$SCRIPTPATH/cpp/roaring64map.hh
"
# internal .h files => These are used in the implementation but aren't part of
# the API. They are all embedded at the head of the amalgamated C file, and
# need to be in this order.
#
ALL_PRIVATE_H="
$SCRIPTPATH/include/roaring/isadetection.h
$SCRIPTPATH/include/roaring/containers/perfparameters.h
$SCRIPTPATH/include/roaring/containers/container_defs.h
$SCRIPTPATH/include/roaring/array_util.h
$SCRIPTPATH/include/roaring/utilasm.h
$SCRIPTPATH/include/roaring/bitset_util.h
$SCRIPTPATH/include/roaring/containers/array.h
$SCRIPTPATH/include/roaring/containers/bitset.h
$SCRIPTPATH/include/roaring/containers/run.h
$SCRIPTPATH/include/roaring/containers/convert.h
$SCRIPTPATH/include/roaring/containers/mixed_equal.h
$SCRIPTPATH/include/roaring/containers/mixed_subset.h
$SCRIPTPATH/include/roaring/containers/mixed_andnot.h
$SCRIPTPATH/include/roaring/containers/mixed_intersection.h
$SCRIPTPATH/include/roaring/containers/mixed_negation.h
$SCRIPTPATH/include/roaring/containers/mixed_union.h
$SCRIPTPATH/include/roaring/containers/mixed_xor.h
$SCRIPTPATH/include/roaring/containers/containers.h
$SCRIPTPATH/include/roaring/roaring_array.h
$SCRIPTPATH/include/roaring/art/art.h
"
# .c implementation files
#
# The `#include <roaring/*>` lines are stripped out with sed, so all the C code
# has the definitions available from all the header files. Since the order of
# the top level declarations doesn't matter after that point, the file list is
# generated automatically from git-tracked C files in the /src/ directory.
# Sort them so every run uses the same order.
#
ALL_PRIVATE_C=$( ( ( \
[ -d $SCRIPTPATH/.git ] \
&& ( type git >/dev/null 2>&1 ) \
&& ( git -C $SCRIPTPATH ls-files 'src/*.c' ) \
) || ( find $SCRIPTPATH/src -name '*.c' ) ) | sort )
# Verify up-front that all the files exist
#
for i in ${ALL_PUBLIC_H} ${ALL_PUBLIC_HH} ${ALL_PRIVATE_H} ${ALL_PRIVATE_C}; do
test -e $i && continue
echo "FATAL: source file [$i] not found."
exit 127
done
function echo_timestamp()
{
echo "// !!! DO NOT EDIT - THIS IS AN AUTO-GENERATED FILE !!!"
echo "// Created by amalgamation.sh on ${timestamp}"
newline
}
function echo_license()
{
cat $SCRIPTPATH/src/license-comment.h
newline
}
function stripinc()
{
sed -e '/# *include *"/d' -e '/# *include *<roaring\//d'
}
function dofile()
{
RELFILE=${1#"$SCRIPTPATH/"}
echo "/* begin file $RELFILE */"
# The preprocessor has a feature which lets you redefine the line and file:
# https://en.cppreference.com/w/c/preprocessor/line
#
# This conceivably could be used to map back to the original source, e.g.:
#
# echo "#line 8 \"$1\""
#
# However, this breaks IDEs and is not nearly as useful as it sounds.
stripinc < $1
echo "/* end file $RELFILE */"
}
echo "Creating ${AMAL_H}..."
{
echo_timestamp
echo_license
for h in ${ALL_PUBLIC_H}; do
dofile $h
done
} > "${DESTINATION}/${AMAL_H}"
echo "Creating ${AMAL_C}..."
{
echo_timestamp
echo_license
echo "#include \"${AMAL_H}\""
newline
echo "/* used for http://dmalloc.com/ Dmalloc - Debug Malloc Library */"
echo "#ifdef DMALLOC"
echo "#include \"dmalloc.h\""
echo "#endif"
newline
echo "#include \"roaring.h\" /* include public API definitions */"
for h in ${ALL_PRIVATE_H} ${ALL_PRIVATE_C}; do
dofile $h
done
} > "${DESTINATION}/${AMAL_C}"
echo "Creating ${DEMOC}..."
{
echo_timestamp
cat <<< '
#include <stdio.h>
#include <stdlib.h>
#include "roaring.c"
static inline void or_many(void) {
roaring_bitmap_t *r1 = roaring_bitmap_from(500, 1000);
roaring_bitmap_t *r2 = roaring_bitmap_from(1000, 2000);
const roaring_bitmap_t *bitmap_arr[2] = {r1, r2};
fprintf(stderr, "Going to or many\n");
for (int i = 0; i < 10000; i++) {
roaring_bitmap_t *r = roaring_bitmap_or_many(2, bitmap_arr);
roaring_bitmap_free(r);
}
fprintf(stderr, "Got done\n");
roaring_bitmap_free(r2);
roaring_bitmap_free(r1);
}
int main() {
roaring_bitmap_t *r1 = roaring_bitmap_create();
for (uint32_t i = 100; i < 1000; i++) roaring_bitmap_add(r1, i);
printf("cardinality = %d\n", (int) roaring_bitmap_get_cardinality(r1));
roaring_bitmap_free(r1);
roaring64_bitmap_t *r2 = roaring64_bitmap_create();
for (uint64_t i = 100; i < 1000; i++) roaring64_bitmap_add(r2, i);
printf("cardinality (64-bit) = %d\n", (int) roaring64_bitmap_get_cardinality(r2));
roaring64_bitmap_free(r2);
bitset_t *b = bitset_create();
for (int k = 0; k < 1000; ++k) {
bitset_set(b, 3 * k);
}
printf("%zu \n", bitset_count(b));
bitset_free(b);
or_many();
return EXIT_SUCCESS;
}
'
} > "${DESTINATION}/${DEMOC}"
echo "Creating ${AMAL_HH}..."
{
echo_timestamp
echo_license
# using the C++ roaring:: namespace does not make the C API available by
# default. One must either `#include "roaring.h"` manually or use the
# `roaring::api::` namespace. The protection against putting the API in
# global scope is done via a #define surrounding the include, but that
# inclusion is stripped out so we repeat it here.
#
# !!! This would be better if it found the inclusion of roaring.h and
# replaced it with the amalgamated file.
#
echo "#define ROARING_API_NOT_IN_GLOBAL_NAMESPACE // see remarks in roaring.h"
echo "#include \"${AMAL_H}\""
echo "#undef ROARING_API_NOT_IN_GLOBAL_NAMESPACE"
for hh in ${ALL_PUBLIC_HH}; do
dofile $hh
done
} > "${DESTINATION}/${AMAL_HH}"
echo "Creating ${DEMOCPP}..."
{
echo_timestamp
echo_license
cat <<< '
#include <iostream>
#include "roaring.hh"
//#include "roaring.c"
int main() {
roaring::Roaring r1;
for (uint32_t i = 100; i < 1000; i++) {
r1.add(i);
}
std::cout << "cardinality = " << r1.cardinality() << std::endl;
roaring::Roaring64Map r2;
for (uint64_t i = 18000000000000000100ull; i < 18000000000000001000ull; i++) {
r2.add(i);
}
std::cout << "cardinality = " << r2.cardinality() << std::endl;
return 0;
}
'
} > "${DESTINATION}/${DEMOCPP}"
# Print out a directory listing of the output files and their sizes
#
newline
echo "Files have been written to ${DESTINATION} "
ls -la ${DESTINATION}/${AMAL_C} ${DESTINATION}/${AMAL_H} ${DESTINATION}/${AMAL_HH} ${DESTINATION}/${DEMOC} ${DESTINATION}/${DEMOCPP}
newline
CBIN=${DEMOC%%.*}
CPPBIN=${DEMOCPP%%.*}
echo "The interface is found in the file 'include/roaring/roaring.h'."
newline
echo "Go to ${DESTINATION}/."
echo "For C, try:"
echo "cc -O3 -std=c11 -o ${CBIN} ${DEMOC} && ./${CBIN} "
newline
echo "For C++, try:"
echo "c++ -O3 -std=c++11 -o ${CPPBIN} ${DEMOCPP} ${AMAL_C} && ./${CPPBIN} "
lowercase(){
echo "$1" | tr 'A-Z' 'a-z'
}
OS=`lowercase \`uname\``
newline
echo "You can build a shared library with the following command:"
if [ $OS == "darwin" ]; then
echo "cc -O3 -std=c11 -shared -o libroaring.dylib -fPIC roaring.c"
else
echo "cc -O3 -std=c11 -shared -o libroaring.so -fPIC roaring.c"
fi