2019-08-13 22:41:43 -07:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
2017-12-03 11:54:29 -05:00
|
|
|
"""Amalgamate json-cpp library sources into a single source and header file.
|
2011-06-21 21:18:49 +00:00
|
|
|
|
2015-03-03 16:16:47 -06:00
|
|
|
Works with python2.6+ and python3.4+.
|
2011-06-21 21:18:49 +00:00
|
|
|
|
|
|
|
Example of invocation (must be invoked from json-cpp top directory):
|
2017-12-03 11:54:29 -05:00
|
|
|
python amalgamate.py
|
2011-06-21 21:18:49 +00:00
|
|
|
"""
|
|
|
|
import os
|
|
|
|
import os.path
|
|
|
|
import sys
|
|
|
|
|
2019-08-13 22:41:43 -07:00
|
|
|
INCLUDE_PATH = "include/json"
|
|
|
|
SRC_PATH = "src/lib_json"
|
|
|
|
|
2011-06-21 21:18:49 +00:00
|
|
|
class AmalgamationFile:
|
2015-01-24 15:29:52 -06:00
|
|
|
def __init__(self, top_dir):
|
2011-06-21 21:18:49 +00:00
|
|
|
self.top_dir = top_dir
|
|
|
|
self.blocks = []
|
|
|
|
|
2015-01-24 15:29:52 -06:00
|
|
|
def add_text(self, text):
|
|
|
|
if not text.endswith("\n"):
|
2014-09-10 11:02:01 -07:00
|
|
|
text += "\n"
|
2015-01-24 15:29:52 -06:00
|
|
|
self.blocks.append(text)
|
|
|
|
|
|
|
|
def add_file(self, relative_input_path, wrap_in_comment=False):
|
|
|
|
def add_marker(prefix):
|
|
|
|
self.add_text("")
|
|
|
|
self.add_text("// " + "/"*70)
|
|
|
|
self.add_text("// %s of content of file: %s" % (prefix, relative_input_path.replace("\\","/")))
|
|
|
|
self.add_text("// " + "/"*70)
|
|
|
|
self.add_text("")
|
|
|
|
add_marker("Beginning")
|
|
|
|
f = open(os.path.join(self.top_dir, relative_input_path), "rt")
|
2011-06-21 21:18:49 +00:00
|
|
|
content = f.read()
|
|
|
|
if wrap_in_comment:
|
2014-09-10 11:02:01 -07:00
|
|
|
content = "/*\n" + content + "\n*/"
|
2015-01-24 15:29:52 -06:00
|
|
|
self.add_text(content)
|
2011-06-21 21:18:49 +00:00
|
|
|
f.close()
|
2015-01-24 15:29:52 -06:00
|
|
|
add_marker("End")
|
|
|
|
self.add_text("\n\n\n\n")
|
|
|
|
|
|
|
|
def get_value(self):
|
|
|
|
return "".join(self.blocks).replace("\r\n","\n")
|
|
|
|
|
|
|
|
def write_to(self, output_path):
|
|
|
|
output_dir = os.path.dirname(output_path)
|
|
|
|
if output_dir and not os.path.isdir(output_dir):
|
|
|
|
os.makedirs(output_dir)
|
|
|
|
f = open(output_path, "wb")
|
|
|
|
f.write(str.encode(self.get_value(), 'UTF-8'))
|
2011-06-21 21:18:49 +00:00
|
|
|
f.close()
|
|
|
|
|
2015-01-24 15:29:52 -06:00
|
|
|
def amalgamate_source(source_top_dir=None,
|
2011-06-21 21:18:49 +00:00
|
|
|
target_source_path=None,
|
2015-01-24 15:29:52 -06:00
|
|
|
header_include_path=None):
|
2017-12-03 11:54:29 -05:00
|
|
|
"""Produces amalgamated source.
|
2011-06-21 21:18:49 +00:00
|
|
|
Parameters:
|
|
|
|
source_top_dir: top-directory
|
|
|
|
target_source_path: output .cpp path
|
|
|
|
header_include_path: generated header path relative to target_source_path.
|
|
|
|
"""
|
2017-12-03 11:54:29 -05:00
|
|
|
print("Amalgamating header...")
|
2015-01-24 15:29:52 -06:00
|
|
|
header = AmalgamationFile(source_top_dir)
|
2024-09-10 04:43:32 +03:00
|
|
|
header.add_text("/// Json-cpp amalgamated header (https://github.com/open-source-parsers/jsoncpp/).")
|
2015-02-15 11:37:37 -06:00
|
|
|
header.add_text('/// It is intended to be used with #include "%s"' % header_include_path)
|
2015-01-24 15:29:52 -06:00
|
|
|
header.add_file("LICENSE", wrap_in_comment=True)
|
2017-12-03 11:54:29 -05:00
|
|
|
header.add_text("#ifndef JSON_AMALGAMATED_H_INCLUDED")
|
|
|
|
header.add_text("# define JSON_AMALGAMATED_H_INCLUDED")
|
|
|
|
header.add_text("/// If defined, indicates that the source file is amalgamated")
|
2015-01-24 15:29:52 -06:00
|
|
|
header.add_text("/// to prevent private header inclusion.")
|
|
|
|
header.add_text("#define JSON_IS_AMALGAMATION")
|
2019-08-13 22:41:43 -07:00
|
|
|
header.add_file(os.path.join(INCLUDE_PATH, "version.h"))
|
|
|
|
header.add_file(os.path.join(INCLUDE_PATH, "allocator.h"))
|
|
|
|
header.add_file(os.path.join(INCLUDE_PATH, "config.h"))
|
|
|
|
header.add_file(os.path.join(INCLUDE_PATH, "forwards.h"))
|
2019-09-25 14:04:53 -07:00
|
|
|
header.add_file(os.path.join(INCLUDE_PATH, "json_features.h"))
|
2019-08-13 22:41:43 -07:00
|
|
|
header.add_file(os.path.join(INCLUDE_PATH, "value.h"))
|
|
|
|
header.add_file(os.path.join(INCLUDE_PATH, "reader.h"))
|
|
|
|
header.add_file(os.path.join(INCLUDE_PATH, "writer.h"))
|
|
|
|
header.add_file(os.path.join(INCLUDE_PATH, "assertions.h"))
|
2017-12-03 11:54:29 -05:00
|
|
|
header.add_text("#endif //ifndef JSON_AMALGAMATED_H_INCLUDED")
|
2015-01-24 15:29:52 -06:00
|
|
|
|
|
|
|
target_header_path = os.path.join(os.path.dirname(target_source_path), header_include_path)
|
2017-12-03 11:54:29 -05:00
|
|
|
print("Writing amalgamated header to %r" % target_header_path)
|
2015-01-24 15:29:52 -06:00
|
|
|
header.write_to(target_header_path)
|
2011-06-21 21:18:49 +00:00
|
|
|
|
2015-01-24 15:29:52 -06:00
|
|
|
base, ext = os.path.splitext(header_include_path)
|
2014-09-10 11:02:01 -07:00
|
|
|
forward_header_include_path = base + "-forwards" + ext
|
2017-12-03 11:54:29 -05:00
|
|
|
print("Amalgamating forward header...")
|
2015-01-24 15:29:52 -06:00
|
|
|
header = AmalgamationFile(source_top_dir)
|
2024-09-10 04:43:32 +03:00
|
|
|
header.add_text("/// Json-cpp amalgamated forward header (https://github.com/open-source-parsers/jsoncpp/).")
|
2015-02-15 11:37:37 -06:00
|
|
|
header.add_text('/// It is intended to be used with #include "%s"' % forward_header_include_path)
|
2015-01-24 15:29:52 -06:00
|
|
|
header.add_text("/// This header provides forward declaration for all JsonCpp types.")
|
|
|
|
header.add_file("LICENSE", wrap_in_comment=True)
|
2017-12-03 11:54:29 -05:00
|
|
|
header.add_text("#ifndef JSON_FORWARD_AMALGAMATED_H_INCLUDED")
|
|
|
|
header.add_text("# define JSON_FORWARD_AMALGAMATED_H_INCLUDED")
|
|
|
|
header.add_text("/// If defined, indicates that the source file is amalgamated")
|
2015-01-24 15:29:52 -06:00
|
|
|
header.add_text("/// to prevent private header inclusion.")
|
|
|
|
header.add_text("#define JSON_IS_AMALGAMATION")
|
2019-12-30 11:37:14 -05:00
|
|
|
header.add_file(os.path.join(INCLUDE_PATH, "version.h"))
|
|
|
|
header.add_file(os.path.join(INCLUDE_PATH, "allocator.h"))
|
2019-08-13 22:41:43 -07:00
|
|
|
header.add_file(os.path.join(INCLUDE_PATH, "config.h"))
|
|
|
|
header.add_file(os.path.join(INCLUDE_PATH, "forwards.h"))
|
2017-12-03 11:54:29 -05:00
|
|
|
header.add_text("#endif //ifndef JSON_FORWARD_AMALGAMATED_H_INCLUDED")
|
2015-01-24 15:29:52 -06:00
|
|
|
|
|
|
|
target_forward_header_path = os.path.join(os.path.dirname(target_source_path),
|
|
|
|
forward_header_include_path)
|
2017-12-03 11:54:29 -05:00
|
|
|
print("Writing amalgamated forward header to %r" % target_forward_header_path)
|
2015-01-24 15:29:52 -06:00
|
|
|
header.write_to(target_forward_header_path)
|
2011-06-21 21:18:49 +00:00
|
|
|
|
2017-12-03 11:54:29 -05:00
|
|
|
print("Amalgamating source...")
|
2015-01-24 15:29:52 -06:00
|
|
|
source = AmalgamationFile(source_top_dir)
|
2024-09-10 04:43:32 +03:00
|
|
|
source.add_text("/// Json-cpp amalgamated source (https://github.com/open-source-parsers/jsoncpp/).")
|
2015-02-15 11:37:37 -06:00
|
|
|
source.add_text('/// It is intended to be used with #include "%s"' % header_include_path)
|
2015-01-24 15:29:52 -06:00
|
|
|
source.add_file("LICENSE", wrap_in_comment=True)
|
|
|
|
source.add_text("")
|
2015-02-15 10:47:49 -06:00
|
|
|
source.add_text('#include "%s"' % header_include_path)
|
|
|
|
source.add_text("""
|
2015-02-18 09:17:06 -06:00
|
|
|
#ifndef JSON_IS_AMALGAMATION
|
2015-02-15 10:47:49 -06:00
|
|
|
#error "Compile with -I PATH_TO_JSON_DIRECTORY"
|
|
|
|
#endif
|
|
|
|
""")
|
2015-01-24 15:29:52 -06:00
|
|
|
source.add_text("")
|
2019-08-13 22:41:43 -07:00
|
|
|
source.add_file(os.path.join(SRC_PATH, "json_tool.h"))
|
|
|
|
source.add_file(os.path.join(SRC_PATH, "json_reader.cpp"))
|
|
|
|
source.add_file(os.path.join(SRC_PATH, "json_valueiterator.inl"))
|
|
|
|
source.add_file(os.path.join(SRC_PATH, "json_value.cpp"))
|
|
|
|
source.add_file(os.path.join(SRC_PATH, "json_writer.cpp"))
|
2014-09-10 11:02:01 -07:00
|
|
|
|
2017-12-03 11:54:29 -05:00
|
|
|
print("Writing amalgamated source to %r" % target_source_path)
|
2015-01-24 15:29:52 -06:00
|
|
|
source.write_to(target_source_path)
|
2011-06-21 21:18:49 +00:00
|
|
|
|
|
|
|
def main():
|
|
|
|
usage = """%prog [options]
|
2017-12-03 11:54:29 -05:00
|
|
|
Generate a single amalgamated source and header file from the sources.
|
2011-06-21 21:18:49 +00:00
|
|
|
"""
|
|
|
|
from optparse import OptionParser
|
|
|
|
parser = OptionParser(usage=usage)
|
|
|
|
parser.allow_interspersed_args = False
|
2014-09-10 11:02:01 -07:00
|
|
|
parser.add_option("-s", "--source", dest="target_source_path", action="store", default="dist/jsoncpp.cpp",
|
2011-06-21 21:18:49 +00:00
|
|
|
help="""Output .cpp source path. [Default: %default]""")
|
2014-09-10 11:02:01 -07:00
|
|
|
parser.add_option("-i", "--include", dest="header_include_path", action="store", default="json/json.h",
|
2017-12-03 11:54:29 -05:00
|
|
|
help="""Header include path. Used to include the header from the amalgamated source file. [Default: %default]""")
|
2014-09-10 11:02:01 -07:00
|
|
|
parser.add_option("-t", "--top-dir", dest="top_dir", action="store", default=os.getcwd(),
|
2011-06-21 21:18:49 +00:00
|
|
|
help="""Source top-directory. [Default: %default]""")
|
|
|
|
parser.enable_interspersed_args()
|
|
|
|
options, args = parser.parse_args()
|
|
|
|
|
2015-01-24 15:29:52 -06:00
|
|
|
msg = amalgamate_source(source_top_dir=options.top_dir,
|
2011-06-21 21:18:49 +00:00
|
|
|
target_source_path=options.target_source_path,
|
2015-01-24 15:29:52 -06:00
|
|
|
header_include_path=options.header_include_path)
|
2011-06-21 21:18:49 +00:00
|
|
|
if msg:
|
2015-01-24 15:29:52 -06:00
|
|
|
sys.stderr.write(msg + "\n")
|
|
|
|
sys.exit(1)
|
2011-06-21 21:18:49 +00:00
|
|
|
else:
|
2017-12-03 11:54:29 -05:00
|
|
|
print("Source successfully amalgamated")
|
2014-09-10 11:02:01 -07:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2011-06-21 21:18:49 +00:00
|
|
|
main()
|