blob: 8c643c7b35f9642864c98555de0463365129a374 [file] [log] [blame]
Parker Schuh343481e2014-02-09 18:28:43 -08001$LOAD_PATH.unshift(".")
brians343bc112013-02-10 01:53:46 +00002["tokenizer.rb","q_file.rb","queue_group.rb","queue.rb","namespaces.rb",
Parker Schuh343481e2014-02-09 18:28:43 -08003"interface.rb","errors.rb", "q_struct.rb"].each do |name|
brians343bc112013-02-10 01:53:46 +00004 require File.dirname(__FILE__) + "/objects/" + name
5end
6["standard_types.rb","auto_gen.rb","file_pair_types.rb",
7"dep_file_pair.rb","swig.rb"].each do |name|
8 require File.dirname(__FILE__) + "/cpp_pretty_print/" + name
9end
Parker Schuh343481e2014-02-09 18:28:43 -080010["q_file.rb","message_dec.rb","queue_dec.rb", "q_struct.rb"].each do |name|
brians343bc112013-02-10 01:53:46 +000011 require File.dirname(__FILE__) + "/output/" + name
12end
13require "fileutils"
14require "pathname"
15
16def parse_args(globals,args)
17 i = 0
18 $swig = false
19 $swigccout_path = ""
20 while(i < args.length)
21 if(args[i] == "-I")
22 args.delete_at(i)
23 if(!args[i])
24 $stderr.puts "hey! -I is followed by nothing."
25 $stderr.puts "\tnot a supported usage..."
26 $stderr.puts "\tWot. Wot."
27 exit!(-1)
28 end
29 path = args.delete_at(i)
30 globals.add_path(path)
31 elsif(args[i] == "--swigccout")
32 args.delete_at(i)
33 $swigccout_path = args.delete_at(i)
34 elsif(args[i] == "-cpp_out")
35 args.delete_at(i)
36 path = args.delete_at(i)
37 if(path =~ /\./)
38 $stderr.puts "hey! path #{path} has a \".\" char which is "
39 $stderr.puts "\tnot a supported usage..."
40 $stderr.puts "\tWot. Wot."
41 exit!(-1)
42 elsif(!path)
43 $stderr.puts "hey! No cpp_out path provided."
44 $stderr.puts "\tumm, you could try -cpp_out \"\""
45 $stderr.puts "\tThat might do the trick"
46 $stderr.puts "\tWot. Wot."
47 exit!(-1)
48 end
49 $cpp_out = path.split(/\\|\//)
50 elsif(args[i] == "--swig")
51 $swig = true
52 args.delete_at(i)
53 elsif(args[i] == "-cpp_base")
54 args.delete_at(i)
55 path = args.delete_at(i)
56 $cpp_base = File.expand_path(path)
57 if(!File.exists?($cpp_base))
58 $stderr.puts "output directory #{$cpp_base.inspect} does not exist."
59 $stderr.puts "\tI'm not going to make that! sheesh, who do you think I am?"
60 $stderr.puts "\tWot. Wot."
61 exit!(-1)
62 end
63 elsif(args[i] =~ /^-/)
64 $stderr.puts "hey! unknown argument #{args[i]}."
65 $stderr.puts "\tWot. Wot."
66 exit!(-1)
67 else
68 i += 1
69 end
70 end
71 if(!$cpp_base)
72 $stderr.puts "hey! missing -cpp_base argument."
73 $stderr.puts "\tWot. Wot."
74 exit!(-1)
75 end
76 if(!$cpp_out)
77 $stderr.puts "hey! missing -cpp_out argument."
78 $stderr.puts "\tWot. Wot."
79 exit!(-1)
80 end
81end
82def build(filename,globals_template)
83 globals = Globals.new()
84 globals_template.paths.each do |path|
85 globals.add_path(path)
86 end
87 filename = File.expand_path(filename)
88 q_file = QFile.parse(filename)
89 output_file = q_file.q_eval(globals)
90 q_filename = File.basename(filename)
91 rel_path = ($cpp_out + [q_filename]).join("/")
92
93 FileUtils.mkdir_p(Pathname.new($cpp_base) + $cpp_out.join("/"))
94
95 cpp_tree = output_file.make_cpp_tree(rel_path)
96
97 h_file_path = $cpp_base + "/" + rel_path + ".h"
98 cc_file_path = $cpp_base + "/" + rel_path + ".cc"
99 swig_file_path = $cpp_base + "/" + rel_path + ".swig"
100 java_directory = $cpp_base + "/" + rel_path + "_java/"
101 cpp_tree.add_cc_include((rel_path + ".h").inspect)
102 cpp_tree.add_cc_include("aos/common/byteorder.h".inspect)
103 cpp_tree.add_cc_include("aos/common/inttypes.h".inspect)
Parker Schuh711db3e2014-02-12 09:47:34 -0800104 cpp_tree.add_cc_include("aos/common/queue_types.h".inspect)
Brian Silverman665e60c2014-02-12 13:57:10 -0800105 cpp_tree.add_cc_include("aos/common/once.h".inspect)
brians343bc112013-02-10 01:53:46 +0000106 cpp_tree.add_cc_using("::aos::to_network")
107 cpp_tree.add_cc_using("::aos::to_host")
108 cpp_tree.add_swig_header_include("aos/common/queue.h".inspect)
Brian Silverman14fd0fb2014-01-14 21:42:01 -0800109 cpp_tree.add_swig_body_include("aos/linux_code/queue-tmpl.h".inspect)
brians343bc112013-02-10 01:53:46 +0000110 cpp_tree.add_swig_header_include("aos/common/time.h".inspect)
111 cpp_tree.add_swig_include((rel_path + ".h").inspect)
112
113 header_file = File.open(h_file_path,"w+")
114 cc_file = File.open(cc_file_path,"w+")
115 cpp_tree.write_header_file($cpp_base,header_file)
116 cpp_tree.write_cc_file($cpp_base,cc_file)
117 cc_file.close()
118 header_file.close()
119 if ($swig)
120 swig_file = File.open(swig_file_path,"w+")
121 cpp_tree.write_swig_file($cpp_base,swig_file,q_filename)
122 swig_file.close()
123 namespace = q_file.namespace.get_name()[1..-1]
124 FileUtils.mkdir_p(java_directory)
125 includes = globals.paths.collect { |a| "-I#{a}" }
126
127 if (!system('/usr/bin/swig', *(includes + ['-I' + $cpp_base + '/',
128 '-package', namespace,
129 '-outdir', java_directory,
130 '-o', $swigccout_path,
131 '-c++', '-Wall', '-Wextra', '-java', swig_file_path])))
132 puts "Swig failed."
133 exit -1
134 end
135 end
136end
137begin
138 args = ARGV.dup
139 globals = Globals.new()
140 parse_args(globals,args)
141 if(args.length == 0)
142 $stderr.puts "hey! you want me to do something,"
143 $stderr.puts "\tbut you gave me no q files to build!"
144 $stderr.puts "\tWot. Wot."
145 exit!(-1)
146 end
147 args.each do |filename|
148 build(filename,globals)
149 end
150 exit(0)
151rescue QError => e
152 $stderr.print(e.to_s)
153 exit!(-1)
154end