Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 1 | """Amalgate json-cpp library sources into a single source and header file. |
| 2 | |
Christopher Dunn | 99822b2 | 2015-03-03 16:16:47 -0600 | [diff] [blame^] | 3 | Works with python2.6+ and python3.4+. |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 4 | |
| 5 | Example of invocation (must be invoked from json-cpp top directory): |
| 6 | python amalgate.py |
| 7 | """ |
| 8 | import os |
| 9 | import os.path |
| 10 | import sys |
| 11 | |
| 12 | class AmalgamationFile: |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 13 | def __init__(self, top_dir): |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 14 | self.top_dir = top_dir |
| 15 | self.blocks = [] |
| 16 | |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 17 | def add_text(self, text): |
| 18 | if not text.endswith("\n"): |
SuperManitu | 83b43ca | 2014-09-10 11:02:01 -0700 | [diff] [blame] | 19 | text += "\n" |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 20 | self.blocks.append(text) |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 21 | |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 22 | def add_file(self, relative_input_path, wrap_in_comment=False): |
| 23 | def add_marker(prefix): |
| 24 | self.add_text("") |
| 25 | self.add_text("// " + "/"*70) |
| 26 | self.add_text("// %s of content of file: %s" % (prefix, relative_input_path.replace("\\","/"))) |
| 27 | self.add_text("// " + "/"*70) |
| 28 | self.add_text("") |
| 29 | add_marker("Beginning") |
| 30 | f = open(os.path.join(self.top_dir, relative_input_path), "rt") |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 31 | content = f.read() |
| 32 | if wrap_in_comment: |
SuperManitu | 83b43ca | 2014-09-10 11:02:01 -0700 | [diff] [blame] | 33 | content = "/*\n" + content + "\n*/" |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 34 | self.add_text(content) |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 35 | f.close() |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 36 | add_marker("End") |
| 37 | self.add_text("\n\n\n\n") |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 38 | |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 39 | def get_value(self): |
| 40 | return "".join(self.blocks).replace("\r\n","\n") |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 41 | |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 42 | def write_to(self, output_path): |
| 43 | output_dir = os.path.dirname(output_path) |
| 44 | if output_dir and not os.path.isdir(output_dir): |
| 45 | os.makedirs(output_dir) |
| 46 | f = open(output_path, "wb") |
| 47 | f.write(str.encode(self.get_value(), 'UTF-8')) |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 48 | f.close() |
| 49 | |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 50 | def amalgamate_source(source_top_dir=None, |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 51 | target_source_path=None, |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 52 | header_include_path=None): |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 53 | """Produces amalgated source. |
| 54 | Parameters: |
| 55 | source_top_dir: top-directory |
| 56 | target_source_path: output .cpp path |
| 57 | header_include_path: generated header path relative to target_source_path. |
| 58 | """ |
Christopher Dunn | bd1e895 | 2014-11-19 23:30:47 -0600 | [diff] [blame] | 59 | print("Amalgating header...") |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 60 | header = AmalgamationFile(source_top_dir) |
| 61 | header.add_text("/// Json-cpp amalgated header (http://jsoncpp.sourceforge.net/).") |
Christopher Dunn | c658241 | 2015-02-15 11:37:37 -0600 | [diff] [blame] | 62 | header.add_text('/// It is intended to be used with #include "%s"' % header_include_path) |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 63 | header.add_file("LICENSE", wrap_in_comment=True) |
| 64 | header.add_text("#ifndef JSON_AMALGATED_H_INCLUDED") |
| 65 | header.add_text("# define JSON_AMALGATED_H_INCLUDED") |
| 66 | header.add_text("/// If defined, indicates that the source file is amalgated") |
| 67 | header.add_text("/// to prevent private header inclusion.") |
| 68 | header.add_text("#define JSON_IS_AMALGAMATION") |
| 69 | header.add_file("include/json/version.h") |
| 70 | header.add_file("include/json/config.h") |
| 71 | header.add_file("include/json/forwards.h") |
| 72 | header.add_file("include/json/features.h") |
| 73 | header.add_file("include/json/value.h") |
| 74 | header.add_file("include/json/reader.h") |
| 75 | header.add_file("include/json/writer.h") |
| 76 | header.add_file("include/json/assertions.h") |
| 77 | header.add_text("#endif //ifndef JSON_AMALGATED_H_INCLUDED") |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 78 | |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 79 | target_header_path = os.path.join(os.path.dirname(target_source_path), header_include_path) |
Christopher Dunn | bd1e895 | 2014-11-19 23:30:47 -0600 | [diff] [blame] | 80 | print("Writing amalgated header to %r" % target_header_path) |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 81 | header.write_to(target_header_path) |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 82 | |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 83 | base, ext = os.path.splitext(header_include_path) |
SuperManitu | 83b43ca | 2014-09-10 11:02:01 -0700 | [diff] [blame] | 84 | forward_header_include_path = base + "-forwards" + ext |
Christopher Dunn | bd1e895 | 2014-11-19 23:30:47 -0600 | [diff] [blame] | 85 | print("Amalgating forward header...") |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 86 | header = AmalgamationFile(source_top_dir) |
| 87 | header.add_text("/// Json-cpp amalgated forward header (http://jsoncpp.sourceforge.net/).") |
Christopher Dunn | c658241 | 2015-02-15 11:37:37 -0600 | [diff] [blame] | 88 | header.add_text('/// It is intended to be used with #include "%s"' % forward_header_include_path) |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 89 | header.add_text("/// This header provides forward declaration for all JsonCpp types.") |
| 90 | header.add_file("LICENSE", wrap_in_comment=True) |
| 91 | header.add_text("#ifndef JSON_FORWARD_AMALGATED_H_INCLUDED") |
| 92 | header.add_text("# define JSON_FORWARD_AMALGATED_H_INCLUDED") |
| 93 | header.add_text("/// If defined, indicates that the source file is amalgated") |
| 94 | header.add_text("/// to prevent private header inclusion.") |
| 95 | header.add_text("#define JSON_IS_AMALGAMATION") |
| 96 | header.add_file("include/json/config.h") |
| 97 | header.add_file("include/json/forwards.h") |
| 98 | header.add_text("#endif //ifndef JSON_FORWARD_AMALGATED_H_INCLUDED") |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 99 | |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 100 | target_forward_header_path = os.path.join(os.path.dirname(target_source_path), |
| 101 | forward_header_include_path) |
Christopher Dunn | bd1e895 | 2014-11-19 23:30:47 -0600 | [diff] [blame] | 102 | print("Writing amalgated forward header to %r" % target_forward_header_path) |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 103 | header.write_to(target_forward_header_path) |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 104 | |
Christopher Dunn | bd1e895 | 2014-11-19 23:30:47 -0600 | [diff] [blame] | 105 | print("Amalgating source...") |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 106 | source = AmalgamationFile(source_top_dir) |
| 107 | source.add_text("/// Json-cpp amalgated source (http://jsoncpp.sourceforge.net/).") |
Christopher Dunn | c658241 | 2015-02-15 11:37:37 -0600 | [diff] [blame] | 108 | source.add_text('/// It is intended to be used with #include "%s"' % header_include_path) |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 109 | source.add_file("LICENSE", wrap_in_comment=True) |
| 110 | source.add_text("") |
Christopher Dunn | 0ee7e24 | 2015-02-15 10:47:49 -0600 | [diff] [blame] | 111 | source.add_text('#include "%s"' % header_include_path) |
| 112 | source.add_text(""" |
Christopher Dunn | 2f4e40b | 2015-02-18 09:17:06 -0600 | [diff] [blame] | 113 | #ifndef JSON_IS_AMALGAMATION |
Christopher Dunn | 0ee7e24 | 2015-02-15 10:47:49 -0600 | [diff] [blame] | 114 | #error "Compile with -I PATH_TO_JSON_DIRECTORY" |
| 115 | #endif |
| 116 | """) |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 117 | source.add_text("") |
SuperManitu | 83b43ca | 2014-09-10 11:02:01 -0700 | [diff] [blame] | 118 | lib_json = "src/lib_json" |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 119 | source.add_file(os.path.join(lib_json, "json_tool.h")) |
| 120 | source.add_file(os.path.join(lib_json, "json_reader.cpp")) |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 121 | source.add_file(os.path.join(lib_json, "json_valueiterator.inl")) |
| 122 | source.add_file(os.path.join(lib_json, "json_value.cpp")) |
| 123 | source.add_file(os.path.join(lib_json, "json_writer.cpp")) |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 124 | |
Christopher Dunn | bd1e895 | 2014-11-19 23:30:47 -0600 | [diff] [blame] | 125 | print("Writing amalgated source to %r" % target_source_path) |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 126 | source.write_to(target_source_path) |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 127 | |
| 128 | def main(): |
| 129 | usage = """%prog [options] |
| 130 | Generate a single amalgated source and header file from the sources. |
| 131 | """ |
| 132 | from optparse import OptionParser |
| 133 | parser = OptionParser(usage=usage) |
| 134 | parser.allow_interspersed_args = False |
SuperManitu | 83b43ca | 2014-09-10 11:02:01 -0700 | [diff] [blame] | 135 | parser.add_option("-s", "--source", dest="target_source_path", action="store", default="dist/jsoncpp.cpp", |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 136 | help="""Output .cpp source path. [Default: %default]""") |
SuperManitu | 83b43ca | 2014-09-10 11:02:01 -0700 | [diff] [blame] | 137 | parser.add_option("-i", "--include", dest="header_include_path", action="store", default="json/json.h", |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 138 | help="""Header include path. Used to include the header from the amalgated source file. [Default: %default]""") |
SuperManitu | 83b43ca | 2014-09-10 11:02:01 -0700 | [diff] [blame] | 139 | parser.add_option("-t", "--top-dir", dest="top_dir", action="store", default=os.getcwd(), |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 140 | help="""Source top-directory. [Default: %default]""") |
| 141 | parser.enable_interspersed_args() |
| 142 | options, args = parser.parse_args() |
| 143 | |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 144 | msg = amalgamate_source(source_top_dir=options.top_dir, |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 145 | target_source_path=options.target_source_path, |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 146 | header_include_path=options.header_include_path) |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 147 | if msg: |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 148 | sys.stderr.write(msg + "\n") |
| 149 | sys.exit(1) |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 150 | else: |
Christopher Dunn | bd1e895 | 2014-11-19 23:30:47 -0600 | [diff] [blame] | 151 | print("Source succesfully amalagated") |
SuperManitu | 83b43ca | 2014-09-10 11:02:01 -0700 | [diff] [blame] | 152 | |
| 153 | if __name__ == "__main__": |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 154 | main() |