libfort/amalgamate.py

120 lines
4.2 KiB
Python
Raw Permalink Normal View History

2018-05-06 15:21:45 +02:00
import os
2019-04-21 11:22:48 +02:00
import shutil
import sys
2018-05-06 15:21:45 +02:00
def comment_line(line):
return "/* {} */ /* Commented by amalgamation script */".format(line)
def amalgamate(config):
with open(config["output_file"], "w") as output_f:
2018-05-06 16:08:32 +02:00
output_f.write("""/*
2018-05-06 16:04:34 +02:00
libfort
MIT License
2020-02-02 12:26:05 +01:00
Copyright (c) 2017 - 2020 Seleznev Anton
2018-05-06 16:04:34 +02:00
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/* The file was GENERATED by an amalgamation script.*/
/* DO NOT EDIT BY HAND!!! */\n\n
#define FT_AMALGAMED_SOURCE /* Macros to make internal libfort functions static */\n
2018-05-06 16:08:32 +02:00
""")
2018-05-06 15:21:45 +02:00
for hdr_file in config["header_files"]:
with open(config["src_dir"] + "/" + hdr_file, "r") as input_f:
txt = input_f.read()
output_f.write('\n/********************************************************\n')
output_f.write(' Begin of file "{}"\n'.format(hdr_file))
output_f.write(' ********************************************************/\n\n')
output_f.write(txt)
output_f.write('\n/********************************************************\n')
output_f.write(' End of file "{}"\n'.format(hdr_file))
output_f.write(' ********************************************************/\n\n')
for src_file in config["src_files"]:
with open(config["src_dir"] + "/" + src_file, "r") as input_f:
txt = input_f.read()
output_f.write('\n/********************************************************\n')
output_f.write(' Begin of file "{}"\n'.format(src_file))
output_f.write(' ********************************************************/\n\n')
output_f.write(txt)
output_f.write('\n/********************************************************\n')
output_f.write(' End of file "{}"\n'.format(src_file))
output_f.write(' ********************************************************/\n\n')
with open(config["output_file"]) as f:
lines = f.readlines()
forbidden_strings = map(lambda hdr_name: '#include "{}"'.format(hdr_name), config["header_files"])
lines = map(lambda line: comment_line(line.strip()) + "\n" if line.strip() in forbidden_strings else line, lines)
with open(config["output_file"], "w") as f:
for line in lines:
f.write(line)
def is_c_header_file(file):
return ".h" in file
def is_c_source_file(file):
return ".c" in file
def main():
config = {}
if len(sys.argv) >= 3 and sys.argv[1] == "-o":
config["output_file"] = sys.argv[2]
else:
config["output_file"] = "./lib/fort.c"
2018-05-06 15:21:45 +02:00
config["src_dir"] = "./src"
all_files = os.listdir(config["src_dir"])
2018-11-18 07:27:14 +01:00
config["src_files"] = sorted(filter(is_c_source_file, all_files))
2018-05-06 15:21:45 +02:00
# config["header_files"] = filter(is_c_header_file, all_files)
config["header_files"] = [
2018-05-06 15:36:53 +02:00
"fort_utils.h",
2018-05-06 15:21:45 +02:00
"vector.h",
"wcwidth.h",
2019-08-14 21:01:57 +02:00
"utf8.h",
2018-05-06 15:21:45 +02:00
"string_buffer.h",
2018-11-03 21:50:30 +01:00
"properties.h",
2018-05-06 15:21:45 +02:00
"cell.h",
"row.h",
"table.h"
];
amalgamate(config)
2019-04-21 11:22:48 +02:00
# copy header files
shutil.copyfile('./src/fort.h', './lib/fort.h')
shutil.copyfile('./src/fort.hpp', './lib/fort.hpp')
2018-05-06 15:21:45 +02:00
if __name__ == '__main__':
2018-11-03 21:50:30 +01:00
main()