Ada 4.1.1
Fast spec-compliant URL parser
Loading...
Searching...
No Matches
amalgamate.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2#
3# Creates the amalgamated source files.
4#
5
6import sys
7import os.path
8import subprocess
9import os
10import re
11import shutil
12import datetime
13if sys.version_info[0] < 3:
14 sys.stdout.write("Sorry, requires Python 3.x or better\n")
15 sys.exit(1)
16
17SCRIPTPATH = os.path.dirname(os.path.abspath(sys.argv[0]))
18PROJECTPATH = os.path.dirname(SCRIPTPATH)
19print(f"SCRIPTPATH={SCRIPTPATH} PROJECTPATH={PROJECTPATH}")
20
21
22print("We are about to amalgamate all idna files into one source file.")
23print("See https://www.sqlite.org/amalgamation.html and https://en.wikipedia.org/wiki/Single_Compilation_Unit for rationale.")
24if "AMALGAMATE_SOURCE_PATH" not in os.environ:
25 AMALGAMATE_SOURCE_PATH = os.path.join(PROJECTPATH, "src")
26else:
27 AMALGAMATE_SOURCE_PATH = os.environ["AMALGAMATE_SOURCE_PATH"]
28if "AMALGAMATE_INCLUDE_PATH" not in os.environ:
29 AMALGAMATE_INCLUDE_PATH = os.path.join(PROJECTPATH, "include")
30else:
31 AMALGAMATE_INCLUDE_PATH = os.environ["AMALGAMATE_INCLUDE_PATH"]
32if "AMALGAMATE_OUTPUT_PATH" not in os.environ:
33 AMALGAMATE_OUTPUT_PATH = os.path.join(SCRIPTPATH)
34else:
35 AMALGAMATE_OUTPUT_PATH = os.environ["AMALGAMATE_OUTPUT_PATH"]
36
37AMALGAMATE_UNILIB_SOURCE_PATH = AMALGAMATE_SOURCE_PATH +"/unilib"
38# this list excludes the "src/generic headers"
39ALLCFILES = ["idna.cpp"]
40
41# order matters
42ALLCHEADERS = ["idna.h"]
43
44found_includes = []
45
46found_unilib = []
47
48def doinclude(fid, file, line, origin):
49 if(file == "uninorms.h"): return #exception
50
51 p = os.path.join(AMALGAMATE_INCLUDE_PATH, file)
52 pi = os.path.join(AMALGAMATE_SOURCE_PATH, file)
53 puni = os.path.join(AMALGAMATE_UNILIB_SOURCE_PATH, file)
54 if os.path.exists(puni):
55 if file not in found_unilib:
56 found_unilib.append(file)
57 dofile(fid, AMALGAMATE_UNILIB_SOURCE_PATH, file)
58 else:
59 pass
60 elif os.path.exists(p):
61 if file not in found_includes:
62 found_includes.append(file)
63 dofile(fid, AMALGAMATE_INCLUDE_PATH, file)
64 else:
65 pass
66 elif os.path.exists(pi):
67 if file not in found_includes:
68 found_includes.append(file)
69 dofile(fid, AMALGAMATE_SOURCE_PATH, file)
70 else:
71 pass
72 else:
73 # If we don't recognize it, just emit the #include
74 print("unrecognized:", file, " from ", line, " in ", origin)
75 print(line, file=fid)
76
77def dofile(fid, prepath, filename):
78 file = os.path.join(prepath, filename)
79 RELFILE = os.path.relpath(file, PROJECTPATH)
80 # Last lines are always ignored. Files should end by an empty lines.
81 print(f"/* begin file {RELFILE} */", file=fid)
82 includepattern = re.compile('\s*#\s*include "(.*)"')
83 with open(file, 'r') as fid2:
84 for line in fid2:
85 line = line.rstrip('\n')
86 s = includepattern.search(line)
87 if s:
88 includedfile = s.group(1)
89 if includedfile == "idna.h" and filename == "idan.cpp":
90 print(line, file=fid)
91 continue
92
93 if includedfile.startswith('../'):
94 includedfile = includedfile[2:]
95 # we explicitly include idna headers, one time each
96 doinclude(fid, includedfile, line, filename)
97 elif line.startswith("#pragma once"):
98 continue
99 else:
100 print(line, file=fid)
101 print(f"/* end file {RELFILE} */", file=fid)
102
103
104# Get the generation date from git, so the output is reproducible.
105# The %ci specifier gives the unambiguous ISO 8601 format, and
106# does not change with locale and timezone at time of generation.
107# Forcing it to be UTC is difficult, because it needs to be portable
108# between gnu date and busybox date.
109try:
110 timestamp = subprocess.run(['git', 'show', '-s', '--format=%ci', 'HEAD'],
111 stdout=subprocess.PIPE).stdout.decode('utf-8').strip()
112except:
113 print("git not found, timestamp based on current time")
114 timestamp = str(datetime.datetime.now())
115print(f"timestamp is {timestamp}")
116
117os.makedirs(AMALGAMATE_OUTPUT_PATH, exist_ok=True)
118AMAL_H = os.path.join(AMALGAMATE_OUTPUT_PATH, "ada_idna.h")
119AMAL_C = os.path.join(AMALGAMATE_OUTPUT_PATH, "ada_idna.cpp")
120DEMOCPP = os.path.join(AMALGAMATE_OUTPUT_PATH, "cpp")
121README = os.path.join(AMALGAMATE_OUTPUT_PATH, "README.md")
122
123print(f"Creating {AMAL_H}")
124amal_h = open(AMAL_H, 'w')
125print(f"/* auto-generated on {timestamp}. Do not edit! */", file=amal_h)
126for h in ALLCHEADERS:
127 doinclude(amal_h, h, f"ERROR {h} not found", h)
128
129amal_h.close()
130print()
131print()
132# The .cpp amalgamation is self-contained (demo includes it alone). Re-emit
133# headers when processing .cpp so constants/types from public headers are
134# visible; otherwise includes already pulled into ada_idna.h are skipped.
135found_includes = []
136found_unilib = []
137print(f"Creating {AMAL_C}")
138amal_c = open(AMAL_C, 'w')
139print(f"/* auto-generated on {timestamp}. Do not edit! */", file=amal_c)
140for c in ALLCFILES:
141 doinclude(amal_c, c, f"ERROR {c} not found", c)
142
143amal_c.close()
144
145# Normalize formatting of amalgamated outputs (skipped re-includes leave blanks).
146for amal in (AMAL_H, AMAL_C):
147 try:
148 r = subprocess.run(['clang-format', '-i', amal], check=False,
149 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
150 if r.returncode != 0:
151 print(f"clang-format skipped for {amal}: {r.stderr.decode().strip()}")
152 except FileNotFoundError:
153 print("clang-format not found; leaving amalgamated files unformatted")
154
155# copy the README and DEMOCPP
156if SCRIPTPATH != AMALGAMATE_OUTPUT_PATH:
157 shutil.copy2(os.path.join(SCRIPTPATH,"demo.cpp"),AMALGAMATE_OUTPUT_PATH)
158 shutil.copy2(os.path.join(SCRIPTPATH,"README.md"),AMALGAMATE_OUTPUT_PATH)
159
160import zipfile
161zf = zipfile.ZipFile(os.path.join(AMALGAMATE_OUTPUT_PATH,'singleheader.zip'), 'w', zipfile.ZIP_DEFLATED)
162zf.write(os.path.join(AMALGAMATE_OUTPUT_PATH,"ada_idna.cpp"), "ada_idna.cpp")
163zf.write(os.path.join(AMALGAMATE_OUTPUT_PATH,"ada_idna.h"), "ada_idna.h")
164
165
166print("Done with all files generation.")
167
168print(f"Files have been written to directory: {AMALGAMATE_OUTPUT_PATH}/")
169print("Done with all files generation.")
170
dofile(fid, prepath, filename)
Definition amalgamate.py:77
doinclude(fid, file, line, origin)
Definition amalgamate.py:48