blob: 4a328ab5a86ad6bed05a0936e1d63be8d34e92c1 [file] [log] [blame]
Haibo Huangb0bee822021-02-24 15:40:15 -08001#!/usr/bin/env python
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -04002
Haibo Huangb0bee822021-02-24 15:40:15 -08003"""Amalgamate json-cpp library sources into a single source and header file.
4
5Works with python2.6+ and python3.4+.
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -04006
7Example of invocation (must be invoked from json-cpp top directory):
Haibo Huangb0bee822021-02-24 15:40:15 -08008python amalgamate.py
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -04009"""
10import os
11import os.path
12import sys
13
Haibo Huangb0bee822021-02-24 15:40:15 -080014INCLUDE_PATH = "include/json"
15SRC_PATH = "src/lib_json"
16
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -040017class AmalgamationFile:
Haibo Huangb0bee822021-02-24 15:40:15 -080018 def __init__(self, top_dir):
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -040019 self.top_dir = top_dir
20 self.blocks = []
21
Haibo Huangb0bee822021-02-24 15:40:15 -080022 def add_text(self, text):
23 if not text.endswith("\n"):
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050024 text += "\n"
Haibo Huangb0bee822021-02-24 15:40:15 -080025 self.blocks.append(text)
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -040026
Haibo Huangb0bee822021-02-24 15:40:15 -080027 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")
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -040036 content = f.read()
37 if wrap_in_comment:
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050038 content = "/*\n" + content + "\n*/"
Haibo Huangb0bee822021-02-24 15:40:15 -080039 self.add_text(content)
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -040040 f.close()
Haibo Huangb0bee822021-02-24 15:40:15 -080041 add_marker("End")
42 self.add_text("\n\n\n\n")
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -040043
Haibo Huangb0bee822021-02-24 15:40:15 -080044 def get_value(self):
45 return "".join(self.blocks).replace("\r\n","\n")
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -040046
Haibo Huangb0bee822021-02-24 15:40:15 -080047 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'))
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -040053 f.close()
54
Haibo Huangb0bee822021-02-24 15:40:15 -080055def amalgamate_source(source_top_dir=None,
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -040056 target_source_path=None,
Haibo Huangb0bee822021-02-24 15:40:15 -080057 header_include_path=None):
58 """Produces amalgamated source.
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -040059 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 """
Haibo Huangb0bee822021-02-24 15:40:15 -080064 print("Amalgamating header...")
65 header = AmalgamationFile(source_top_dir)
66 header.add_text("/// Json-cpp amalgamated header (http://jsoncpp.sourceforge.net/).")
67 header.add_text('/// It is intended to be used with #include "%s"' % header_include_path)
68 header.add_file("LICENSE", wrap_in_comment=True)
69 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")
72 header.add_text("/// to prevent private header inclusion.")
73 header.add_text("#define JSON_IS_AMALGAMATION")
74 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, "json_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"))
83 header.add_text("#endif //ifndef JSON_AMALGAMATED_H_INCLUDED")
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -040084
Haibo Huangb0bee822021-02-24 15:40:15 -080085 target_header_path = os.path.join(os.path.dirname(target_source_path), header_include_path)
86 print("Writing amalgamated header to %r" % target_header_path)
87 header.write_to(target_header_path)
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -040088
Haibo Huangb0bee822021-02-24 15:40:15 -080089 base, ext = os.path.splitext(header_include_path)
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050090 forward_header_include_path = base + "-forwards" + ext
Haibo Huangb0bee822021-02-24 15:40:15 -080091 print("Amalgamating forward header...")
92 header = AmalgamationFile(source_top_dir)
93 header.add_text("/// Json-cpp amalgamated forward header (http://jsoncpp.sourceforge.net/).")
94 header.add_text('/// It is intended to be used with #include "%s"' % forward_header_include_path)
95 header.add_text("/// This header provides forward declaration for all JsonCpp types.")
96 header.add_file("LICENSE", wrap_in_comment=True)
97 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")
100 header.add_text("/// to prevent private header inclusion.")
101 header.add_text("#define JSON_IS_AMALGAMATION")
102 header.add_file(os.path.join(INCLUDE_PATH, "version.h"))
103 header.add_file(os.path.join(INCLUDE_PATH, "allocator.h"))
104 header.add_file(os.path.join(INCLUDE_PATH, "config.h"))
105 header.add_file(os.path.join(INCLUDE_PATH, "forwards.h"))
106 header.add_text("#endif //ifndef JSON_FORWARD_AMALGAMATED_H_INCLUDED")
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400107
Haibo Huangb0bee822021-02-24 15:40:15 -0800108 target_forward_header_path = os.path.join(os.path.dirname(target_source_path),
109 forward_header_include_path)
110 print("Writing amalgamated forward header to %r" % target_forward_header_path)
111 header.write_to(target_forward_header_path)
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400112
Haibo Huangb0bee822021-02-24 15:40:15 -0800113 print("Amalgamating source...")
114 source = AmalgamationFile(source_top_dir)
115 source.add_text("/// Json-cpp amalgamated source (http://jsoncpp.sourceforge.net/).")
116 source.add_text('/// It is intended to be used with #include "%s"' % header_include_path)
117 source.add_file("LICENSE", wrap_in_comment=True)
118 source.add_text("")
119 source.add_text('#include "%s"' % header_include_path)
120 source.add_text("""
121#ifndef JSON_IS_AMALGAMATION
122#error "Compile with -I PATH_TO_JSON_DIRECTORY"
123#endif
124""")
125 source.add_text("")
126 source.add_file(os.path.join(SRC_PATH, "json_tool.h"))
127 source.add_file(os.path.join(SRC_PATH, "json_reader.cpp"))
128 source.add_file(os.path.join(SRC_PATH, "json_valueiterator.inl"))
129 source.add_file(os.path.join(SRC_PATH, "json_value.cpp"))
130 source.add_file(os.path.join(SRC_PATH, "json_writer.cpp"))
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400131
Haibo Huangb0bee822021-02-24 15:40:15 -0800132 print("Writing amalgamated source to %r" % target_source_path)
133 source.write_to(target_source_path)
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400134
135def main():
136 usage = """%prog [options]
Haibo Huangb0bee822021-02-24 15:40:15 -0800137Generate a single amalgamated source and header file from the sources.
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400138"""
139 from optparse import OptionParser
140 parser = OptionParser(usage=usage)
141 parser.allow_interspersed_args = False
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500142 parser.add_option("-s", "--source", dest="target_source_path", action="store", default="dist/jsoncpp.cpp",
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400143 help="""Output .cpp source path. [Default: %default]""")
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500144 parser.add_option("-i", "--include", dest="header_include_path", action="store", default="json/json.h",
Haibo Huangb0bee822021-02-24 15:40:15 -0800145 help="""Header include path. Used to include the header from the amalgamated source file. [Default: %default]""")
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500146 parser.add_option("-t", "--top-dir", dest="top_dir", action="store", default=os.getcwd(),
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400147 help="""Source top-directory. [Default: %default]""")
148 parser.enable_interspersed_args()
149 options, args = parser.parse_args()
150
Haibo Huangb0bee822021-02-24 15:40:15 -0800151 msg = amalgamate_source(source_top_dir=options.top_dir,
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400152 target_source_path=options.target_source_path,
Haibo Huangb0bee822021-02-24 15:40:15 -0800153 header_include_path=options.header_include_path)
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400154 if msg:
Haibo Huangb0bee822021-02-24 15:40:15 -0800155 sys.stderr.write(msg + "\n")
156 sys.exit(1)
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400157 else:
Haibo Huangb0bee822021-02-24 15:40:15 -0800158 print("Source successfully amalgamated")
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500159
160if __name__ == "__main__":
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400161 main()