blob: 6ca141d8dc21c7c48feeb89edef6b59bbadedb16 [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()
Philipp Schrader9b1790e2018-03-10 20:21:30 -080080 # TODO(phil): Is there a better way to use the sandboxed clang-format here?
81 child = Process.spawn({'LD_LIBRARY_PATH' => './bazel-out/host/bin/aos/build/queues/compiler.runfiles/org_frc971/external/clang_3p6_repo/usr/lib/x86_64-linux-gnu'},
82 './bazel-out/host/bin/aos/build/queues/compiler.runfiles/org_frc971/external/clang_3p6_repo/usr/bin/clang-format-3.6 --style=google',
Brian Silverman16c82972014-02-13 15:36:40 -080083 {:in=>read_in, write_in=>:close,
Brian Silvermancbbc3c42014-02-22 18:33:17 -080084 :out=>output.fileno})
Brian Silverman16c82972014-02-13 15:36:40 -080085 read_in.close
86 [child, write_in]
87end
brians343bc112013-02-10 01:53:46 +000088def build(filename,globals_template)
Brian Silverman43216322015-09-13 02:23:09 -040089 globals = Globals.new()
90 globals_template.paths.each do |path|
91 globals.add_path(path)
92 end
93 filename = File.expand_path(filename)
94 q_file = QFile.parse(filename)
95 output_file = q_file.q_eval(globals)
96 q_filename = File.basename(filename)
brians343bc112013-02-10 01:53:46 +000097
Brian Silverman43216322015-09-13 02:23:09 -040098 if $cpp_base && $cpp_out
99 $src_filename = ($cpp_out + [q_filename]).join("/")
100 $h_file_path = $cpp_base + "/" + $src_filename + ".h"
101 $cc_file_path = $cpp_base + "/" + $src_filename + ".cc"
102 FileUtils.mkdir_p(Pathname.new($cpp_base) + $cpp_out.join("/"))
103 end
brians343bc112013-02-10 01:53:46 +0000104
Brian Silverman43216322015-09-13 02:23:09 -0400105 cpp_tree = output_file.make_cpp_tree($src_filename)
brians343bc112013-02-10 01:53:46 +0000106
Brian Silverman43216322015-09-13 02:23:09 -0400107 cpp_tree.add_cc_include(($src_filename + ".h").inspect)
John Park33858a32018-09-28 23:05:48 -0700108 cpp_tree.add_cc_include("aos/byteorder.h".inspect)
Brian Silverman43216322015-09-13 02:23:09 -0400109 cpp_tree.add_cc_include("<inttypes.h>")
John Park33858a32018-09-28 23:05:48 -0700110 cpp_tree.add_cc_include("aos/queue_types.h".inspect)
Sabina Davis2ed5ea22017-09-26 22:27:42 -0700111 cpp_tree.add_cc_include("aos/once.h".inspect)
John Park33858a32018-09-28 23:05:48 -0700112 cpp_tree.add_cc_include("aos/logging/printf_formats.h".inspect)
Brian Silverman43216322015-09-13 02:23:09 -0400113 cpp_tree.add_cc_using("::aos::to_network")
114 cpp_tree.add_cc_using("::aos::to_host")
brians343bc112013-02-10 01:53:46 +0000115
Brian Silverman43216322015-09-13 02:23:09 -0400116 header_file = WriteIffChanged.new($h_file_path)
117 cc_file = WriteIffChanged.new($cc_file_path)
Brian Silverman16c82972014-02-13 15:36:40 -0800118 header_child, header_output = format_pipeline(header_file)
119 cc_child, cc_output = format_pipeline(cc_file)
Brian Silverman43216322015-09-13 02:23:09 -0400120 cpp_tree.write_header_file($cpp_base,header_output)
121 cpp_tree.write_cc_file($cpp_base,cc_output)
122 header_output.close()
123 cc_output.close()
Brian Silverman16c82972014-02-13 15:36:40 -0800124 if !Process.wait2(cc_child)[1].success?
125 $stderr.puts "Formatting cc file failed."
126 exit 1
127 end
128 if !Process.wait2(header_child)[1].success?
129 $stderr.puts "Formatting header file failed."
130 exit 1
131 end
Brian Silvermancbbc3c42014-02-22 18:33:17 -0800132 header_file.close()
133 cc_file.close()
brians343bc112013-02-10 01:53:46 +0000134end
135begin
Brian Silverman43216322015-09-13 02:23:09 -0400136 args = ARGV.dup
137 globals = Globals.new()
138 parse_args(globals,args)
139 if(args.length == 0)
140 $stderr.puts "hey! you want me to do something,"
141 $stderr.puts "\tbut you gave me no q files to build!"
142 $stderr.puts "\tWot. Wot."
143 exit!(-1)
144 end
145 args.each do |filename|
146 build(filename,globals)
147 end
148 exit(0)
brians343bc112013-02-10 01:53:46 +0000149rescue QError => e
Brian Silverman43216322015-09-13 02:23:09 -0400150 $stderr.print(e.to_s)
151 exit!(-1)
brians343bc112013-02-10 01:53:46 +0000152end