-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathextconf.rb
More file actions
75 lines (62 loc) · 1.97 KB
/
extconf.rb
File metadata and controls
75 lines (62 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# frozen_string_literal: true
require "mkmf"
require "fileutils"
def log(msg)
timestamp = Time.now.strftime("%Y-%m-%d %H:%M:%S")
$stderr.write("[#{timestamp}] [binaryen-rb] " + msg + "\n")
end
def to_mib(bytes)
(bytes.to_f / 1024.to_f / 1024.to_f).round(2)
end
total_saved = 0
if enable_config("prune", true)
begin
log("[info] removing unnecessary vendor directories")
cpu = RbConfig::CONFIG["host_cpu"]
os = RbConfig::CONFIG["host_os"]
if os.include?("darwin")
os = "darwin"
end
ruby_platform = "#{cpu}-#{os}"
Dir["#{__dir__}/vendor/*"].each do |dir|
next if dir.end_with?(ruby_platform)
space_saved = Dir["#{dir}/**/*"].sum { |f| File.file?(f) ? File.size(f) : 0 }
total_saved += space_saved
log("[info] removing #{dir} (#{to_mib(space_saved)} MiB saved)")
FileUtils.rm_rf(dir)
end
rescue => e
log("[warn] failed to remove unnecessary vendor directories: #{e}")
end
end
File.write("Makefile", <<~MAKEFILE)
install:
\t@echo "binaryen-rb does not need to be installed"
MAKEFILE
unless File.exist?("#{__dir__}/vendor/#{ruby_platform}")
log("[warning] no vendor directory found for #{ruby_platform}, cannot use binaryen-rb on this platform yet")
exit
end
if enable_config("strip", true)
begin
strip_command = RbConfig::MAKEFILE_CONFIG["STRIP"]
if strip_command.nil?
log("[warn] no strip command found, skipping")
else
Dir["#{__dir__}/vendor/#{ruby_platform}/{lib,bin}/*"].each do |lib|
start_size = File.size(lib)
if system("#{strip_command} #{lib}")
end_size = File.size(lib)
size_diff = (start_size - end_size)
total_saved += size_diff
log("[info] stripped #{lib.sub(__dir__, ".")} (#{size_diff} bytes saved)")
else
log("[warn] failed to strip #{lib}")
end
end
end
rescue => e
log("[warn] failed to strip binaries: #{e}")
end
end
log("[info] done (#{to_mib(total_saved)} MiB saved)")