blob: 7b41e2e5b7a5758ed7175cd83ef86501cd8cb0f5 [file] [log] [blame]
Jordan Bayles12325b82019-07-22 15:25:23 -07001#!/usr/bin/env python
2
Josh Sorefe6a588a2017-12-03 11:54:29 -05003"""Amalgamate json-cpp library sources into a single source and header file.
Christopher Dunndc0f7362011-06-21 21:18:49 +00004
Christopher Dunn99822b22015-03-03 16:16:47 -06005Works with python2.6+ and python3.4+.
Christopher Dunndc0f7362011-06-21 21:18:49 +00006
7Example of invocation (must be invoked from json-cpp top directory):
Josh Sorefe6a588a2017-12-03 11:54:29 -05008python amalgamate.py
Christopher Dunndc0f7362011-06-21 21:18:49 +00009"""
10import os
11import os.path
12import sys
13
Jordan Bayles12325b82019-07-22 15:25:23 -070014INCLUDE_PATH = "include/json"
15SRC_PATH = "src/lib_json"
16
Christopher Dunndc0f7362011-06-21 21:18:49 +000017class AmalgamationFile:
Christopher Dunn494950a2015-01-24 15:29:52 -060018 def __init__(self, top_dir):
Christopher Dunndc0f7362011-06-21 21:18:49 +000019 self.top_dir = top_dir
20 self.blocks = []
21
Christopher Dunn494950a2015-01-24 15:29:52 -060022 def add_text(self, text):
23 if not text.endswith("\n"):
SuperManitu83b43ca2014-09-10 11:02:01 -070024 text += "\n"
Christopher Dunn494950a2015-01-24 15:29:52 -060025 self.blocks.append(text)
Christopher Dunndc0f7362011-06-21 21:18:49 +000026
Christopher Dunn494950a2015-01-24 15:29:52 -060027 def add_file(self, relative_input_path, wrap_in_comment=False):
28 def add_marker(prefix):
29 self.add_text("")
30 self.add_text("// " + "/"*70)
31 self.add_text("// %s of content of file: %s" % (prefix, relative_input_path.replace("\\","/")))
32 self.add_text("// " + "/"*70)
33 self.add_text("")
34 add_marker("Beginning")
35 f = open(os.path.join(self.top_dir, relative_input_path), "rt")
Christopher Dunndc0f7362011-06-21 21:18:49 +000036 content = f.read()
37 if wrap_in_comment:
SuperManitu83b43ca2014-09-10 11:02:01 -070038 content = "/*\n" + content + "\n*/"
Christopher Dunn494950a2015-01-24 15:29:52 -060039 self.add_text(content)
Christopher Dunndc0f7362011-06-21 21:18:49 +000040 f.close()
Christopher Dunn494950a2015-01-24 15:29:52 -060041 add_marker("End")
42 self.add_text("\n\n\n\n")
Christopher Dunndc0f7362011-06-21 21:18:49 +000043
Christopher Dunn494950a2015-01-24 15:29:52 -060044 def get_value(self):
45 return "".join(self.blocks).replace("\r\n","\n")
Christopher Dunndc0f7362011-06-21 21:18:49 +000046
Christopher Dunn494950a2015-01-24 15:29:52 -060047 def write_to(self, output_path):
48 output_dir = os.path.dirname(output_path)
49 if output_dir and not os.path.isdir(output_dir):
50 os.makedirs(output_dir)
51 f = open(output_path, "wb")
52 f.write(str.encode(self.get_value(), 'UTF-8'))
Christopher Dunndc0f7362011-06-21 21:18:49 +000053 f.close()
54
Christopher Dunn494950a2015-01-24 15:29:52 -060055def amalgamate_source(source_top_dir=None,
Christopher Dunndc0f7362011-06-21 21:18:49 +000056 target_source_path=None,
Christopher Dunn494950a2015-01-24 15:29:52 -060057 header_include_path=None):
Josh Sorefe6a588a2017-12-03 11:54:29 -050058 """Produces amalgamated source.
Christopher Dunndc0f7362011-06-21 21:18:49 +000059 Parameters:
60 source_top_dir: top-directory
61 target_source_path: output .cpp path
62 header_include_path: generated header path relative to target_source_path.
63 """
Josh Sorefe6a588a2017-12-03 11:54:29 -050064 print("Amalgamating header...")
Christopher Dunn494950a2015-01-24 15:29:52 -060065 header = AmalgamationFile(source_top_dir)
Josh Sorefe6a588a2017-12-03 11:54:29 -050066 header.add_text("/// Json-cpp amalgamated header (http://jsoncpp.sourceforge.net/).")
Christopher Dunnc6582412015-02-15 11:37:37 -060067 header.add_text('/// It is intended to be used with #include "%s"' % header_include_path)
Christopher Dunn494950a2015-01-24 15:29:52 -060068 header.add_file("LICENSE", wrap_in_comment=True)
Josh Sorefe6a588a2017-12-03 11:54:29 -050069 header.add_text("#ifndef JSON_AMALGAMATED_H_INCLUDED")
70 header.add_text("# define JSON_AMALGAMATED_H_INCLUDED")
71 header.add_text("/// If defined, indicates that the source file is amalgamated")
Christopher Dunn494950a2015-01-24 15:29:52 -060072 header.add_text("/// to prevent private header inclusion.")
73 header.add_text("#define JSON_IS_AMALGAMATION")
Jordan Bayles12325b82019-07-22 15:25:23 -070074 header.add_file(os.path.join(INCLUDE_PATH, "version.h"))
75 header.add_file(os.path.join(INCLUDE_PATH, "allocator.h"))
76 header.add_file(os.path.join(INCLUDE_PATH, "config.h"))
77 header.add_file(os.path.join(INCLUDE_PATH, "forwards.h"))
78 header.add_file(os.path.join(INCLUDE_PATH, "features.h"))
79 header.add_file(os.path.join(INCLUDE_PATH, "value.h"))
80 header.add_file(os.path.join(INCLUDE_PATH, "reader.h"))
81 header.add_file(os.path.join(INCLUDE_PATH, "writer.h"))
82 header.add_file(os.path.join(INCLUDE_PATH, "assertions.h"))
Josh Sorefe6a588a2017-12-03 11:54:29 -050083 header.add_text("#endif //ifndef JSON_AMALGAMATED_H_INCLUDED")
Christopher Dunndc0f7362011-06-21 21:18:49 +000084
Christopher Dunn494950a2015-01-24 15:29:52 -060085 target_header_path = os.path.join(os.path.dirname(target_source_path), header_include_path)
Josh Sorefe6a588a2017-12-03 11:54:29 -050086 print("Writing amalgamated header to %r" % target_header_path)
Christopher Dunn494950a2015-01-24 15:29:52 -060087 header.write_to(target_header_path)
Christopher Dunndc0f7362011-06-21 21:18:49 +000088
Christopher Dunn494950a2015-01-24 15:29:52 -060089 base, ext = os.path.splitext(header_include_path)
SuperManitu83b43ca2014-09-10 11:02:01 -070090 forward_header_include_path = base + "-forwards" + ext
Josh Sorefe6a588a2017-12-03 11:54:29 -050091 print("Amalgamating forward header...")
Christopher Dunn494950a2015-01-24 15:29:52 -060092 header = AmalgamationFile(source_top_dir)
Josh Sorefe6a588a2017-12-03 11:54:29 -050093 header.add_text("/// Json-cpp amalgamated forward header (http://jsoncpp.sourceforge.net/).")
Christopher Dunnc6582412015-02-15 11:37:37 -060094 header.add_text('/// It is intended to be used with #include "%s"' % forward_header_include_path)
Christopher Dunn494950a2015-01-24 15:29:52 -060095 header.add_text("/// This header provides forward declaration for all JsonCpp types.")
96 header.add_file("LICENSE", wrap_in_comment=True)
Josh Sorefe6a588a2017-12-03 11:54:29 -050097 header.add_text("#ifndef JSON_FORWARD_AMALGAMATED_H_INCLUDED")
98 header.add_text("# define JSON_FORWARD_AMALGAMATED_H_INCLUDED")
99 header.add_text("/// If defined, indicates that the source file is amalgamated")
Christopher Dunn494950a2015-01-24 15:29:52 -0600100 header.add_text("/// to prevent private header inclusion.")
101 header.add_text("#define JSON_IS_AMALGAMATION")
Jordan Bayles12325b82019-07-22 15:25:23 -0700102 header.add_file(os.path.join(INCLUDE_PATH, "config.h"))
103 header.add_file(os.path.join(INCLUDE_PATH, "forwards.h"))
Josh Sorefe6a588a2017-12-03 11:54:29 -0500104 header.add_text("#endif //ifndef JSON_FORWARD_AMALGAMATED_H_INCLUDED")
Christopher Dunndc0f7362011-06-21 21:18:49 +0000105
Christopher Dunn494950a2015-01-24 15:29:52 -0600106 target_forward_header_path = os.path.join(os.path.dirname(target_source_path),
107 forward_header_include_path)
Josh Sorefe6a588a2017-12-03 11:54:29 -0500108 print("Writing amalgamated forward header to %r" % target_forward_header_path)
Christopher Dunn494950a2015-01-24 15:29:52 -0600109 header.write_to(target_forward_header_path)
Christopher Dunndc0f7362011-06-21 21:18:49 +0000110
Josh Sorefe6a588a2017-12-03 11:54:29 -0500111 print("Amalgamating source...")
Christopher Dunn494950a2015-01-24 15:29:52 -0600112 source = AmalgamationFile(source_top_dir)
Josh Sorefe6a588a2017-12-03 11:54:29 -0500113 source.add_text("/// Json-cpp amalgamated source (http://jsoncpp.sourceforge.net/).")
Christopher Dunnc6582412015-02-15 11:37:37 -0600114 source.add_text('/// It is intended to be used with #include "%s"' % header_include_path)
Christopher Dunn494950a2015-01-24 15:29:52 -0600115 source.add_file("LICENSE", wrap_in_comment=True)
116 source.add_text("")
Christopher Dunn0ee7e242015-02-15 10:47:49 -0600117 source.add_text('#include "%s"' % header_include_path)
118 source.add_text("""
Christopher Dunn2f4e40b2015-02-18 09:17:06 -0600119#ifndef JSON_IS_AMALGAMATION
Christopher Dunn0ee7e242015-02-15 10:47:49 -0600120#error "Compile with -I PATH_TO_JSON_DIRECTORY"
121#endif
122""")
Christopher Dunn494950a2015-01-24 15:29:52 -0600123 source.add_text("")
Jordan Bayles12325b82019-07-22 15:25:23 -0700124 source.add_file(os.path.join(SRC_PATH, "json_tool.h"))
125 source.add_file(os.path.join(SRC_PATH, "json_reader.cpp"))
126 source.add_file(os.path.join(SRC_PATH, "json_valueiterator.inl"))
127 source.add_file(os.path.join(SRC_PATH, "json_value.cpp"))
128 source.add_file(os.path.join(SRC_PATH, "json_writer.cpp"))
Christopher Dunndc0f7362011-06-21 21:18:49 +0000129
Josh Sorefe6a588a2017-12-03 11:54:29 -0500130 print("Writing amalgamated source to %r" % target_source_path)
Christopher Dunn494950a2015-01-24 15:29:52 -0600131 source.write_to(target_source_path)
Christopher Dunndc0f7362011-06-21 21:18:49 +0000132
133def main():
134 usage = """%prog [options]
Josh Sorefe6a588a2017-12-03 11:54:29 -0500135Generate a single amalgamated source and header file from the sources.
Christopher Dunndc0f7362011-06-21 21:18:49 +0000136"""
137 from optparse import OptionParser
138 parser = OptionParser(usage=usage)
139 parser.allow_interspersed_args = False
SuperManitu83b43ca2014-09-10 11:02:01 -0700140 parser.add_option("-s", "--source", dest="target_source_path", action="store", default="dist/jsoncpp.cpp",
Christopher Dunndc0f7362011-06-21 21:18:49 +0000141 help="""Output .cpp source path. [Default: %default]""")
SuperManitu83b43ca2014-09-10 11:02:01 -0700142 parser.add_option("-i", "--include", dest="header_include_path", action="store", default="json/json.h",
Josh Sorefe6a588a2017-12-03 11:54:29 -0500143 help="""Header include path. Used to include the header from the amalgamated source file. [Default: %default]""")
SuperManitu83b43ca2014-09-10 11:02:01 -0700144 parser.add_option("-t", "--top-dir", dest="top_dir", action="store", default=os.getcwd(),
Christopher Dunndc0f7362011-06-21 21:18:49 +0000145 help="""Source top-directory. [Default: %default]""")
146 parser.enable_interspersed_args()
147 options, args = parser.parse_args()
148
Christopher Dunn494950a2015-01-24 15:29:52 -0600149 msg = amalgamate_source(source_top_dir=options.top_dir,
Christopher Dunndc0f7362011-06-21 21:18:49 +0000150 target_source_path=options.target_source_path,
Christopher Dunn494950a2015-01-24 15:29:52 -0600151 header_include_path=options.header_include_path)
Christopher Dunndc0f7362011-06-21 21:18:49 +0000152 if msg:
Christopher Dunn494950a2015-01-24 15:29:52 -0600153 sys.stderr.write(msg + "\n")
154 sys.exit(1)
Christopher Dunndc0f7362011-06-21 21:18:49 +0000155 else:
Josh Sorefe6a588a2017-12-03 11:54:29 -0500156 print("Source successfully amalgamated")
SuperManitu83b43ca2014-09-10 11:02:01 -0700157
158if __name__ == "__main__":
Christopher Dunndc0f7362011-06-21 21:18:49 +0000159 main()