gen_binding.rb (2790B)
1 #! /usr/bin/env ruby 2 # frozen_string_literal: true 3 4 ENV['LIBCLANG'] = "#{ENV['PREFIX']}/lib/libclang.so" if ENV['PREFIX'] 5 6 require_relative 'gen_binding/dump' 7 require_relative 'gen_binding/lua' 8 require_relative 'gen_binding/main' 9 require_relative 'gen_binding/util' 10 11 require 'etc' 12 require 'optparse' 13 14 Parser.add_opts %W( 15 -DLIBSHIT_BINDING_GENERATOR 16 -DLIBSHIT_WITH_LUA=1 17 -nostdinc++ 18 -D_LIBCPP_NO_CONFIG 19 -isystem #{__dir__}/ext/libcxx/libcxx/include 20 -isystem #{__dir__}/ext/libcxx/libcxxabi/include 21 -I#{__dir__}/src 22 -I#{__dir__}/ext/boost/boost 23 -I#{__dir__}/ext/lua/lua-5.3.6/src 24 -I#{__dir__}/ext/doctest/doctest/doctest 25 ) 26 27 Parser.add_src [LuaBinding], %w( 28 test/container/ordered_map.cpp 29 test/container/parent_list.cpp 30 test/lua/function_ref.cpp 31 test/lua/user_type.cpp 32 ).map {|f| File.join __dir__, f } 33 34 def main 35 jobs = Etc.nprocessors 36 single = false 37 dump = nil 38 if i = ARGV.index('--') 39 ARGV.insert i, '--' 40 end 41 42 OptionParser.new do |opt| 43 opt.banner = "Usage #$0 [options] files..." 44 opt.on '-j', '--jobs=N', Integer, 45 'Set job number (default: number of CPUs)' do |v| 46 jobs = v 47 end 48 49 opt.on '--single', 'Run single job (specify in, out, extra clang args)' do 50 single = true 51 end 52 53 opt.on '--dump=FILE' do |v| 54 dump = v 55 end 56 end.parse! 57 58 if single 59 Parser.gen_binding ARGV[0], ARGV[1], ARGV[2..] 60 elsif dump 61 Dumper.dump Parser.new.parse dump, ARGV 62 else 63 files, args = ARGV.slice_before('--').to_a 64 files ||= [] 65 args ||= [] 66 args.shift 67 68 src = files.empty? ? Parser.src.keys : files 69 70 i = 0 71 n = src.size 72 digits = n.to_s.size # ehh... 73 74 if jobs <= 1 || n <= 1 75 src.each do |f| 76 CI.einfo format '[%*d/%d] %s', digits, i+=1, n, f 77 begin 78 Parser.gen_binding f, nil, args 79 rescue 80 Util.print_error $! 81 end 82 end 83 else 84 files_mtx = Mutex.new 85 console_mtx = Mutex.new 86 jobs.times.map do 87 Thread.new do 88 loop do 89 f = files_mtx.synchronize { src.pop } || break 90 91 rd, wr = IO.pipe 92 pid = fork do 93 rd.close 94 $stdout = wr 95 $stderr = wr 96 Parser.gen_binding f, nil, args 97 exit Util.success? 98 rescue 99 Util.print_error $! 100 exit! false 101 end 102 wr.close 103 out = rd.read 104 Process.wait pid 105 106 console_mtx.synchronize do 107 CI.einfo format '[%*d/%d] %s', digits, i+=1, n, f 108 $stderr.write out 109 Util.fail! unless $?.success? 110 end 111 end 112 end 113 end.each { |t| nil until t.join 1 } 114 end 115 end 116 exit Util.success? 117 end 118 main if __FILE__ == $0