Skip to content

Commit 924ad2d

Browse files
authored
when amalagamating, replace the SIMDJSON_IMPLEMENTATION (simdjson#1306)
1 parent 5202d07 commit 924ad2d

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

singleheader/amalgamate.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343

4444
found_includes = []
4545

46+
current_implementation=''
4647

4748
def doinclude(fid, file, line):
4849
p = os.path.join(AMALGAMATE_INCLUDE_PATH, file)
@@ -75,12 +76,15 @@ def doinclude(fid, file, line):
7576

7677

7778
def dofile(fid, prepath, filename):
79+
global current_implementation
7880
# print(f"// dofile: invoked with prepath={prepath}, filename={filename}",file=fid)
7981
file = os.path.join(prepath, filename)
8082
RELFILE = os.path.relpath(file, PROJECTPATH)
8183
# Last lines are always ignored. Files should end by an empty lines.
8284
print(f"/* begin file {RELFILE} */", file=fid)
8385
includepattern = re.compile('^#include "(.*)"')
86+
redefines_simdjson_implementation = re.compile('^#define SIMDJSON_IMPLEMENTATION (.*)')
87+
uses_simdjson_implementation = re.compile('SIMDJSON_IMPLEMENTATION([^_a-zA-Z0-9]|$)')
8488
with open(file, 'r') as fid2:
8589
for line in fid2:
8690
line = line.rstrip('\n')
@@ -97,8 +101,14 @@ def dofile(fid, prepath, filename):
97101
# we explicitly include simdjson headers, one time each (unless they are generic, in which case multiple times is fine)
98102
doinclude(fid, includedfile, line)
99103
else:
100-
# Otherwise we simply copy the line
101-
print(line, file=fid)
104+
# does it contain a redefinition of SIMDJSON_IMPLEMENTATION ?
105+
s=redefines_simdjson_implementation.search(line)
106+
if s:
107+
current_implementation=s.group(1)
108+
print(f"// redefining SIMDJSON_IMPLEMENTATION to \"{current_implementation}\"\n// {line}", file=fid)
109+
else:
110+
# copy the line, with SIMDJSON_IMPLEMENTATION replace to what it is currently defined to
111+
print(uses_simdjson_implementation.sub(current_implementation+"\\1",line), file=fid)
102112
print(f"/* end file {RELFILE} */", file=fid)
103113

104114

0 commit comments

Comments
 (0)