blob: 864d1c6b84609c8516793901214b521dbd05ccbe [file] [log] [blame]
Brian Silverman1885bd02014-02-13 12:28:12 -08001require File.dirname(__FILE__) + '/load.rb'
brians343bc112013-02-10 01:53:46 +00002
3def parse_args(globals,args)
4 i = 0
brians343bc112013-02-10 01:53:46 +00005 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)
brians343bc112013-02-10 01:53:46 +000016 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(/\\|\//)
brians343bc112013-02-10 01:53:46 +000032 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] =~ /^-/)
43 $stderr.puts "hey! unknown argument #{args[i]}."
44 $stderr.puts "\tWot. Wot."
45 exit!(-1)
46 else
47 i += 1
48 end
49 end
50 if(!$cpp_base)
51 $stderr.puts "hey! missing -cpp_base argument."
52 $stderr.puts "\tWot. Wot."
53 exit!(-1)
54 end
55 if(!$cpp_out)
56 $stderr.puts "hey! missing -cpp_out argument."
57 $stderr.puts "\tWot. Wot."
58 exit!(-1)
59 end
60end
Brian Silverman16c82972014-02-13 15:36:40 -080061def format_pipeline(output)
62 read_in, write_in = IO.pipe()
Brian Silverman9b7a6842014-05-05 16:19:11 -070063 child = Process.spawn('/opt/clang-3.5/bin/clang-format --style=google',
Brian Silverman16c82972014-02-13 15:36:40 -080064 {:in=>read_in, write_in=>:close,
Brian Silvermancbbc3c42014-02-22 18:33:17 -080065 :out=>output.fileno})
Brian Silverman16c82972014-02-13 15:36:40 -080066 read_in.close
67 [child, write_in]
68end
brians343bc112013-02-10 01:53:46 +000069def build(filename,globals_template)
70 globals = Globals.new()
71 globals_template.paths.each do |path|
72 globals.add_path(path)
73 end
74 filename = File.expand_path(filename)
75 q_file = QFile.parse(filename)
76 output_file = q_file.q_eval(globals)
77 q_filename = File.basename(filename)
78 rel_path = ($cpp_out + [q_filename]).join("/")
79
80 FileUtils.mkdir_p(Pathname.new($cpp_base) + $cpp_out.join("/"))
81
82 cpp_tree = output_file.make_cpp_tree(rel_path)
83
84 h_file_path = $cpp_base + "/" + rel_path + ".h"
85 cc_file_path = $cpp_base + "/" + rel_path + ".cc"
brians343bc112013-02-10 01:53:46 +000086 cpp_tree.add_cc_include((rel_path + ".h").inspect)
87 cpp_tree.add_cc_include("aos/common/byteorder.h".inspect)
Brian4a424a22014-04-02 11:52:45 -070088 cpp_tree.add_cc_include("<inttypes.h>")
Parker Schuh711db3e2014-02-12 09:47:34 -080089 cpp_tree.add_cc_include("aos/common/queue_types.h".inspect)
Brian Silverman665e60c2014-02-12 13:57:10 -080090 cpp_tree.add_cc_include("aos/common/once.h".inspect)
Brian Silvermana7234c62014-03-24 20:23:25 -070091 cpp_tree.add_cc_include("aos/common/logging/logging_printf_formats.h".inspect)
brians343bc112013-02-10 01:53:46 +000092 cpp_tree.add_cc_using("::aos::to_network")
93 cpp_tree.add_cc_using("::aos::to_host")
brians343bc112013-02-10 01:53:46 +000094
Brian Silvermancbbc3c42014-02-22 18:33:17 -080095 header_file = WriteIffChanged.new(h_file_path)
96 cc_file = WriteIffChanged.new(cc_file_path)
Brian Silverman16c82972014-02-13 15:36:40 -080097 header_child, header_output = format_pipeline(header_file)
98 cc_child, cc_output = format_pipeline(cc_file)
99 cpp_tree.write_header_file($cpp_base,header_output)
100 cpp_tree.write_cc_file($cpp_base,cc_output)
Brian Silverman16c82972014-02-13 15:36:40 -0800101 header_output.close()
Brian Silvermancbbc3c42014-02-22 18:33:17 -0800102 cc_output.close()
Brian Silverman16c82972014-02-13 15:36:40 -0800103 if !Process.wait2(cc_child)[1].success?
104 $stderr.puts "Formatting cc file failed."
105 exit 1
106 end
107 if !Process.wait2(header_child)[1].success?
108 $stderr.puts "Formatting header file failed."
109 exit 1
110 end
Brian Silvermancbbc3c42014-02-22 18:33:17 -0800111 header_file.close()
112 cc_file.close()
brians343bc112013-02-10 01:53:46 +0000113end
114begin
115 args = ARGV.dup
116 globals = Globals.new()
117 parse_args(globals,args)
118 if(args.length == 0)
119 $stderr.puts "hey! you want me to do something,"
120 $stderr.puts "\tbut you gave me no q files to build!"
121 $stderr.puts "\tWot. Wot."
122 exit!(-1)
123 end
124 args.each do |filename|
125 build(filename,globals)
126 end
127 exit(0)
128rescue QError => e
129 $stderr.print(e.to_s)
130 exit!(-1)
131end