Brian Silverman | 4321632 | 2015-09-13 02:23:09 -0400 | [diff] [blame] | 1 | require_relative "load.rb" |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 2 | |
| 3 | def parse_args(globals,args) |
Brian Silverman | 4321632 | 2015-09-13 02:23:09 -0400 | [diff] [blame] | 4 | 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 |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 77 | end |
Brian Silverman | 16c8297 | 2014-02-13 15:36:40 -0800 | [diff] [blame] | 78 | def format_pipeline(output) |
| 79 | read_in, write_in = IO.pipe() |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 80 | child = Process.spawn('/usr/bin/clang-format-3.5 --style=google', |
Brian Silverman | 16c8297 | 2014-02-13 15:36:40 -0800 | [diff] [blame] | 81 | {:in=>read_in, write_in=>:close, |
Brian Silverman | cbbc3c4 | 2014-02-22 18:33:17 -0800 | [diff] [blame] | 82 | :out=>output.fileno}) |
Brian Silverman | 16c8297 | 2014-02-13 15:36:40 -0800 | [diff] [blame] | 83 | read_in.close |
| 84 | [child, write_in] |
| 85 | end |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 86 | def build(filename,globals_template) |
Brian Silverman | 4321632 | 2015-09-13 02:23:09 -0400 | [diff] [blame] | 87 | 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) |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 95 | |
Brian Silverman | 4321632 | 2015-09-13 02:23:09 -0400 | [diff] [blame] | 96 | 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 |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 102 | |
Brian Silverman | 4321632 | 2015-09-13 02:23:09 -0400 | [diff] [blame] | 103 | cpp_tree = output_file.make_cpp_tree($src_filename) |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 104 | |
Brian Silverman | 4321632 | 2015-09-13 02:23:09 -0400 | [diff] [blame] | 105 | 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) |
Brian Silverman | 665e60c | 2014-02-12 13:57:10 -0800 | [diff] [blame] | 109 | cpp_tree.add_cc_include("aos/common/once.h".inspect) |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame^] | 110 | cpp_tree.add_cc_include("aos/common/logging/printf_formats.h".inspect) |
Brian Silverman | 4321632 | 2015-09-13 02:23:09 -0400 | [diff] [blame] | 111 | cpp_tree.add_cc_using("::aos::to_network") |
| 112 | cpp_tree.add_cc_using("::aos::to_host") |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 113 | |
Brian Silverman | 4321632 | 2015-09-13 02:23:09 -0400 | [diff] [blame] | 114 | header_file = WriteIffChanged.new($h_file_path) |
| 115 | cc_file = WriteIffChanged.new($cc_file_path) |
Brian Silverman | 16c8297 | 2014-02-13 15:36:40 -0800 | [diff] [blame] | 116 | header_child, header_output = format_pipeline(header_file) |
| 117 | cc_child, cc_output = format_pipeline(cc_file) |
Brian Silverman | 4321632 | 2015-09-13 02:23:09 -0400 | [diff] [blame] | 118 | 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 Silverman | 16c8297 | 2014-02-13 15:36:40 -0800 | [diff] [blame] | 122 | 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 Silverman | cbbc3c4 | 2014-02-22 18:33:17 -0800 | [diff] [blame] | 130 | header_file.close() |
| 131 | cc_file.close() |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 132 | end |
| 133 | begin |
Brian Silverman | 4321632 | 2015-09-13 02:23:09 -0400 | [diff] [blame] | 134 | 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) |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 147 | rescue QError => e |
Brian Silverman | 4321632 | 2015-09-13 02:23:09 -0400 | [diff] [blame] | 148 | $stderr.print(e.to_s) |
| 149 | exit!(-1) |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 150 | end |