2020-03-22 14:50:45 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
# This is a script to run a command in ARGV, expanding tabs in some files
|
2020-03-24 01:31:38 -04:00
|
|
|
# included by vm.c to normalize indentation of MJIT header. You can enable
|
|
|
|
# this feature by passing `--without-mjit-tabs` in configure.
|
2020-03-22 14:50:45 -04:00
|
|
|
#
|
|
|
|
# Note that preprocessor of GCC converts a hard tab to one spaces, where
|
|
|
|
# we expect it to be shown as 8 spaces. To obviate this script, we need
|
|
|
|
# to convert all tabs to spaces in these files.
|
|
|
|
|
|
|
|
require 'fileutils'
|
|
|
|
|
2020-03-22 19:36:54 -04:00
|
|
|
EXPAND_TARGETS = %w[
|
|
|
|
vm*.*
|
|
|
|
include/ruby/ruby.h
|
|
|
|
]
|
|
|
|
|
|
|
|
# These files have no hard tab indentations. Skip normalizing these files from the glob result.
|
2020-03-22 14:50:45 -04:00
|
|
|
SKIPPED_FILES = %w[
|
|
|
|
vm_callinfo.h
|
|
|
|
vm_debug.h
|
|
|
|
vm_exec.h
|
|
|
|
vm_opts.h
|
2021-06-01 01:56:15 -04:00
|
|
|
vm_sync.h
|
|
|
|
vm_sync.c
|
2020-03-22 14:50:45 -04:00
|
|
|
]
|
|
|
|
|
|
|
|
srcdir = File.expand_path('..', __dir__)
|
2020-03-22 19:36:54 -04:00
|
|
|
targets = EXPAND_TARGETS.flat_map { |t| Dir.glob(File.join(srcdir, t)) } - SKIPPED_FILES.map { |f| File.join(srcdir, f) }
|
2020-03-22 14:50:45 -04:00
|
|
|
sources = {}
|
|
|
|
mtimes = {}
|
|
|
|
|
2020-03-24 01:31:38 -04:00
|
|
|
mjit_tabs, *command = ARGV
|
2020-03-23 02:07:09 -04:00
|
|
|
|
2020-03-22 14:50:45 -04:00
|
|
|
targets.each do |target|
|
2020-03-24 01:31:38 -04:00
|
|
|
next if mjit_tabs != 'false'
|
2020-03-22 15:18:46 -04:00
|
|
|
unless File.writable?(target)
|
2021-06-01 01:56:15 -04:00
|
|
|
puts "tool/mjit_tabs.rb: Skipping #{target.dump} as it's not writable."
|
2020-03-22 15:18:46 -04:00
|
|
|
next
|
|
|
|
end
|
|
|
|
source = File.read(target)
|
|
|
|
begin
|
|
|
|
expanded = source.gsub(/^\t+/) { |tab| ' ' * 8 * tab.length }
|
|
|
|
rescue ArgumentError # invalid byte sequence in UTF-8 (Travis, RubyCI)
|
2021-06-01 01:56:15 -04:00
|
|
|
puts "tool/mjit_tabs.rb: Skipping #{target.dump} as the encoding is #{source.encoding}."
|
2020-03-22 15:18:46 -04:00
|
|
|
next
|
|
|
|
end
|
|
|
|
|
|
|
|
sources[target] = source
|
2020-03-22 14:50:45 -04:00
|
|
|
mtimes[target] = File.mtime(target)
|
|
|
|
|
|
|
|
if sources[target] == expanded
|
2021-06-01 01:56:15 -04:00
|
|
|
puts "#{target.dump} has no hard tab indentation. This should be ignored in tool/mjit_tabs.rb."
|
2020-03-22 14:50:45 -04:00
|
|
|
end
|
|
|
|
File.write(target, expanded)
|
|
|
|
FileUtils.touch(target, mtime: mtimes[target])
|
2020-03-22 15:18:46 -04:00
|
|
|
end
|
2020-03-22 14:50:45 -04:00
|
|
|
|
2020-03-22 23:20:26 -04:00
|
|
|
result = system(*command)
|
2020-03-22 14:50:45 -04:00
|
|
|
|
|
|
|
targets.each do |target|
|
2020-03-22 15:18:46 -04:00
|
|
|
if sources.key?(target)
|
|
|
|
File.write(target, sources[target])
|
|
|
|
FileUtils.touch(target, mtime: mtimes.fetch(target))
|
|
|
|
end
|
|
|
|
end
|
2020-03-22 14:50:45 -04:00
|
|
|
|
|
|
|
exit result
|