blob: 502971cd0c273b59018e04ae6d2a02471a8f76fa [file] [log] [blame]
Baptiste Lepilleur62d7bc72011-05-02 07:06:07 +00001"""Amalgate json-cpp library sources into a single source and header file.
2
3Requires Python 2.6
4
5Example of invocation (must be invoked from json-cpp top directory):
6python amalgate.py
7"""
8import os
9import os.path
10import sys
11
12class AmalagatedFile:
13 def __init__( self, top_dir ):
14 self.top_dir = top_dir
15 self.blocks = []
16
17 def add_text( self, text ):
18 if not text.endswith( '\n' ):
19 text += '\n'
20 self.blocks.append( text )
21
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' )
31 content = f.read()
32 if wrap_in_comment:
33 content = '/*\n' + content + '\n*/'
34 self.add_text( content )
35 f.close()
36 add_marker( 'End' )
37 self.add_text( '\n\n\n\n' )
38
39 def get_value( self ):
40 return ''.join( self.blocks ).replace('\r\n','\n')
41
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( self.get_value() )
48 f.close()
49
50def amalgate_source( source_top_dir=None,
51 target_source_path=None,
52 header_include_path=None ):
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 """
59 print 'Amalgating header...'
60 header = AmalagatedFile( source_top_dir )
61 header.add_text( '/// Json-cpp amalgated header (http://jsoncpp.sourceforge.net/).' )
62 header.add_text( '/// It is intented to be used with #include <%s>' % header_include_path )
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_AMALGATED' )
69 header.add_file( 'include/json/config.h' )
70 header.add_file( 'include/json/forwards.h' )
71 header.add_file( 'include/json/features.h' )
72 header.add_file( 'include/json/value.h' )
73 header.add_file( 'include/json/reader.h' )
74 header.add_file( 'include/json/writer.h' )
75 header.add_text( '#endif //ifndef JSON_AMALGATED_H_INCLUDED' )
76
77 target_header_path = os.path.join( os.path.dirname(target_source_path), header_include_path )
78 print 'Writing amalgated header to %r' % target_header_path
79 header.write_to( target_header_path )
80
81 base, ext = os.path.splitext( header_include_path )
82 forward_header_include_path = base + '-forwards' + ext
83 print 'Amalgating forward header...'
84 header = AmalagatedFile( source_top_dir )
85 header.add_text( '/// Json-cpp amalgated forward header (http://jsoncpp.sourceforge.net/).' )
86 header.add_text( '/// It is intented to be used with #include <%s>' % forward_header_include_path )
87 header.add_text( '/// This header provides forward declaration for all JsonCpp types.' )
88 header.add_file( 'LICENSE', wrap_in_comment=True )
89 header.add_text( '#ifndef JSON_FORWARD_AMALGATED_H_INCLUDED' )
90 header.add_text( '# define JSON_FORWARD_AMALGATED_H_INCLUDED' )
91 header.add_text( '/// If defined, indicates that the source file is amalgated' )
92 header.add_text( '/// to prevent private header inclusion.' )
93 header.add_text( '#define JSON_IS_AMALGATED' )
94 header.add_file( 'include/json/config.h' )
95 header.add_file( 'include/json/forwards.h' )
96 header.add_text( '#endif //ifndef JSON_FORWARD_AMALGATED_H_INCLUDED' )
97
98 target_forward_header_path = os.path.join( os.path.dirname(target_source_path),
99 forward_header_include_path )
100 print 'Writing amalgated forward header to %r' % target_forward_header_path
101 header.write_to( target_forward_header_path )
102
103 print 'Amalgating source...'
104 source = AmalagatedFile( source_top_dir )
105 source.add_text( '/// Json-cpp amalgated source (http://jsoncpp.sourceforge.net/).' )
106 source.add_text( '/// It is intented to be used with #include <%s>' % header_include_path )
107 source.add_file( 'LICENSE', wrap_in_comment=True )
108 source.add_text( '' )
109 source.add_text( '#include <%s>' % header_include_path )
110 source.add_text( '' )
111 source.add_file( 'src/lib_json\json_tool.h' )
112 source.add_file( 'src/lib_json\json_reader.cpp' )
113 source.add_file( 'src/lib_json\json_batchallocator.h' )
114 source.add_file( 'src/lib_json\json_valueiterator.inl' )
115 source.add_file( 'src/lib_json\json_value.cpp' )
116 source.add_file( 'src/lib_json\json_writer.cpp' )
117
118 print 'Writing amalgated source to %r' % target_source_path
119 source.write_to( target_source_path )
120
121def main():
122 usage = """%prog [options]
123Generate a single amalgated source and header file from the sources.
124"""
125 from optparse import OptionParser
126 parser = OptionParser(usage=usage)
127 parser.allow_interspersed_args = False
128 parser.add_option('-s', '--source', dest="target_source_path", action='store', default='dist/jsoncpp.cpp',
129 help="""Output .cpp source path. [Default: %default]""")
130 parser.add_option('-i', '--include', dest="header_include_path", action='store', default='json/json.h',
131 help="""Header include path. Used to include the header from the amalgated source file. [Default: %default]""")
132 parser.add_option('-t', '--top-dir', dest="top_dir", action='store', default=os.getcwd(),
133 help="""Source top-directory. [Default: %default]""")
134 parser.enable_interspersed_args()
135 options, args = parser.parse_args()
136
137 msg = amalgate_source( source_top_dir=options.top_dir,
138 target_source_path=options.target_source_path,
139 header_include_path=options.header_include_path )
140 if msg:
141 sys.stderr.write( msg + '\n' )
142 sys.exit( 1 )
143 else:
144 print 'Source succesfully amalagated'
145
146if __name__ == '__main__':
147 main()