-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_lib.py
94 lines (70 loc) · 2.64 KB
/
create_lib.py
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
# The purpose of this script is to join all the header and source code files into a single header
# and source code files, respectively.
#
# This script was heavily inspired by Libfort's amalgamation script, which can be found here:
# https://github.com/seleznevae/libfort/blob/develop/amalgamate.py
import os
import re
def copy_file(filename: str, output_file):
with open(filename, "r") as file:
output_file.write("\n/********************************************************\n")
output_file.write(f" {filename}\n")
output_file.write(" ********************************************************/\n\n")
# copy the contents from the file
whitespace_lines = 0
for line in file.readlines():
# ignore relative includes
if re.search('#include ".+"', line.strip()):
whitespace_lines += 1
continue
if not line.isspace():
output_file.write(line)
whitespace_lines = 0
continue
# ignore multiple whitespace lines in a row
if whitespace_lines == 0:
output_file.write(line)
whitespace_lines += 1
def create_lib(config):
# create the directory, if it doesn't exist
os.makedirs(config["lib_dir"], exist_ok=True)
# create the header file
with open(f"{config["lib_dir"]}/{config["lib_name"]}.h", "w") as output_file:
for filename in config["header_files"]:
copy_file(filename, output_file)
# create the source file
with open(f"{config["lib_dir"]}/{config["lib_name"]}.cpp", "w") as output_file:
# include the header file
output_file.write(f"#include \"{config["lib_name"] + ".h"}\"\n")
for filename in config["source_files"]:
copy_file(filename, output_file)
def main():
# list the program modules
modules = [
"utils",
"lexer",
"manager",
"parser",
"writer",
]
# create the configuration
config = {
"lib_dir": "lib",
"lib_name": "helpy",
}
header_files = []
source_files = []
for module in modules:
dirname = f"src/{module}"
for filename in os.listdir(dirname):
# header files
if ".h" in filename or ".hpp" in filename:
header_files.append(f"{dirname}/{filename}")
# source files
elif ".c" in filename:
source_files.append(f"{dirname}/{filename}")
config["header_files"] = header_files
config["source_files"] = source_files + ["src/main.cpp"]
create_lib(config)
if __name__ == "__main__":
main()