blob: 51646702d67af849c645522cb1e037ba5cfa8add [file] [log] [blame]
Brian Silverman43216322015-09-13 02:23:09 -04001require_relative "load.rb"
brians343bc112013-02-10 01:53:46 +00002
3def parse_args(globals,args)
Brian Silverman43216322015-09-13 02:23:09 -04004 i = 0
5 while(i < args.length)
6 if(args[i] == "-I")
7 args.delete_at(i)
8 if(!args[i])
9 $stderr.puts "hey! -I is followed by nothing."
10 $stderr.puts "\tnot a supported usage..."
11 $stderr.puts "\tWot. Wot."
12 exit!(-1)
13 end
14 path = args.delete_at(i)
15 globals.add_path(path)
16 elsif(args[i] == "-cpp_out")
17 args.delete_at(i)
18 path = args.delete_at(i)
19 if(path =~ /\./)
20 $stderr.puts "hey! path #{path} has a \".\" char which is "
21 $stderr.puts "\tnot a supported usage..."
22 $stderr.puts "\tWot. Wot."
23 exit!(-1)
24 elsif(!path)
25 $stderr.puts "hey! No cpp_out path provided."
26 $stderr.puts "\tumm, you could try -cpp_out \"\""
27 $stderr.puts "\tThat might do the trick"
28 $stderr.puts "\tWot. Wot."
29 exit!(-1)
30 end
31 $cpp_out = path.split(/\\|\//)
32 elsif(args[i] == "-cpp_base")
33 args.delete_at(i)
34 path = args.delete_at(i)
35 $cpp_base = File.expand_path(path)
36 if(!File.exists?($cpp_base))
37 $stderr.puts "output directory #{$cpp_base.inspect} does not exist."
38 $stderr.puts "\tI'm not going to make that! sheesh, who do you think I am?"
39 $stderr.puts "\tWot. Wot."
40 exit!(-1)
41 end
42 elsif(args[i] == "-src_filename")
43 args.delete_at(i)
44 $src_filename = args.delete_at(i)
45 elsif(args[i] == "-h_file_path")
46 args.delete_at(i)
47 path = args.delete_at(i)
48 $h_file_path = File.expand_path(path)
49 if(!File.exists?(File.dirname($h_file_path)))
50 $stderr.puts "directory of output #{$h_file_path.inspect} does not exist."
51 $stderr.puts "\tI'm not going to make that! sheesh, who do you think I am?"
52 $stderr.puts "\tWot. Wot."
53 exit!(-1)
54 end
55 elsif(args[i] == "-cc_file_path")
56 args.delete_at(i)
57 path = args.delete_at(i)
58 $cc_file_path = File.expand_path(path)
59 if(!File.exists?(File.dirname($cc_file_path)))
60 $stderr.puts "directory of output #{$cc_file_path.inspect} does not exist."
61 $stderr.puts "\tI'm not going to make that! sheesh, who do you think I am?"
62 $stderr.puts "\tWot. Wot."
63 exit!(-1)
64 end
65 elsif(args[i] =~ /^-/)
66 $stderr.puts "hey! unknown argument #{args[i]}."
67 $stderr.puts "\tWot. Wot."
68 exit!(-1)
69 else
70 i += 1
71 end
72 end
73 if ($cpp_base && $cpp_out) == ($src_filename && $h_file_path && $cc_file_path)
74 $stderr.puts "hey! I'm not sure where to write the output files!"
75 exit!(-1)
76 end
brians343bc112013-02-10 01:53:46 +000077end
Brian Silverman16c82972014-02-13 15:36:40 -080078def format_pipeline(output)
79 read_in, write_in = IO.pipe()
Brian Silverman20141f92015-01-05 17:39:01 -080080 child = Process.spawn('/usr/bin/clang-format-3.5 --style=google',
Brian Silverman16c82972014-02-13 15:36:40 -080081 {:in=>read_in, write_in=>:close,
Brian Silvermancbbc3c42014-02-22 18:33:17 -080082 :out=>output.fileno})
Brian Silverman16c82972014-02-13 15:36:40 -080083 read_in.close
84 [child, write_in]
85end
brians343bc112013-02-10 01:53:46 +000086def build(filename,globals_template)
Brian Silverman43216322015-09-13 02:23:09 -040087 globals = Globals.new()
88 globals_template.paths.each do |path|
89 globals.add_path(path)
90 end
91 filename = File.expand_path(filename)
92 q_file = QFile.parse(filename)
93 output_file = q_file.q_eval(globals)
94 q_filename = File.basename(filename)
brians343bc112013-02-10 01:53:46 +000095
Brian Silverman43216322015-09-13 02:23:09 -040096 if $cpp_base && $cpp_out
97 $src_filename = ($cpp_out + [q_filename]).join("/")
98 $h_file_path = $cpp_base + "/" + $src_filename + ".h"
99 $cc_file_path = $cpp_base + "/" + $src_filename + ".cc"
100 FileUtils.mkdir_p(Pathname.new($cpp_base) + $cpp_out.join("/"))
101 end
brians343bc112013-02-10 01:53:46 +0000102
Brian Silverman43216322015-09-13 02:23:09 -0400103 cpp_tree = output_file.make_cpp_tree($src_filename)
brians343bc112013-02-10 01:53:46 +0000104
Brian Silverman43216322015-09-13 02:23:09 -0400105 cpp_tree.add_cc_include(($src_filename + ".h").inspect)
106 cpp_tree.add_cc_include("aos/common/byteorder.h".inspect)
107 cpp_tree.add_cc_include("<inttypes.h>")
108 cpp_tree.add_cc_include("aos/common/queue_types.h".inspect)
Sabina Davis2ed5ea22017-09-26 22:27:42 -0700109 cpp_tree.add_cc_include("aos/once.h".inspect)
Brian Silvermancb5da1f2015-12-05 22:19:58 -0500110 cpp_tree.add_cc_include("aos/common/logging/printf_formats.h".inspect)
Brian Silverman43216322015-09-13 02:23:09 -0400111 cpp_tree.add_cc_using("::aos::to_network")
112 cpp_tree.add_cc_using("::aos::to_host")
brians343bc112013-02-10 01:53:46 +0000113
Brian Silverman43216322015-09-13 02:23:09 -0400114 header_file = WriteIffChanged.new($h_file_path)
115 cc_file = WriteIffChanged.new($cc_file_path)
Brian Silverman16c82972014-02-13 15:36:40 -0800116 header_child, header_output = format_pipeline(header_file)
117 cc_child, cc_output = format_pipeline(cc_file)
Brian Silverman43216322015-09-13 02:23:09 -0400118 cpp_tree.write_header_file($cpp_base,header_output)
119 cpp_tree.write_cc_file($cpp_base,cc_output)
120 header_output.close()
121 cc_output.close()
Brian Silverman16c82972014-02-13 15:36:40 -0800122 if !Process.wait2(cc_child)[1].success?
123 $stderr.puts "Formatting cc file failed."
124 exit 1
125 end
126 if !Process.wait2(header_child)[1].success?
127 $stderr.puts "Formatting header file failed."
128 exit 1
129 end
Brian Silvermancbbc3c42014-02-22 18:33:17 -0800130 header_file.close()
131 cc_file.close()
brians343bc112013-02-10 01:53:46 +0000132end
133begin
Brian Silverman43216322015-09-13 02:23:09 -0400134 args = ARGV.dup
135 globals = Globals.new()
136 parse_args(globals,args)
137 if(args.length == 0)
138 $stderr.puts "hey! you want me to do something,"
139 $stderr.puts "\tbut you gave me no q files to build!"
140 $stderr.puts "\tWot. Wot."
141 exit!(-1)
142 end
143 args.each do |filename|
144 build(filename,globals)
145 end
146 exit(0)
brians343bc112013-02-10 01:53:46 +0000147rescue QError => e
Brian Silverman43216322015-09-13 02:23:09 -0400148 $stderr.print(e.to_s)
149 exit!(-1)
brians343bc112013-02-10 01:53:46 +0000150end