-
Notifications
You must be signed in to change notification settings - Fork 5
/
Rakefile
114 lines (94 loc) · 3.34 KB
/
Rakefile
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# frozen_string_literal: true
require 'bundler/setup'
require 'bundler/gem_tasks'
APP_RAKEFILE = File.expand_path('playground/Rakefile', __dir__)
load 'rails/tasks/engine.rake'
CLOBBER.include 'pkg'
task default: %i[test rubocop]
task release: %i[build push]
#
# GO Binary | Ruby Gem
# --------------------------|---------------|
#
# darwin-10.12-arm64.dylib | arm64-darwin |
# darwin-10.12-amd64.dylib | x86_64-darwin |
#
# linux-arm64.so | aarch64-linux |
# linux-amd64.so | x86_64-linux |
#
# windows-4.0-386 | x86-mingw32 | ?
# windows-4.0-amd64 | x64-mingw32 | ?
#
# Ruby => Go
PLATFORMS = {
'x86_64-darwin' => 'darwin/amd64',
'arm64-darwin' => 'darwin/arm64',
'aarch64-linux' => 'linux/arm64',
'x86_64-linux' => 'linux/amd64'
# 'arm-linux' => { goos: 'linux', arch: 'arm' },
# 'x86-linux' => { goos: 'linux', arch: '386' },
# 'x86_64-linux' => { goos: 'linux', arch: 'amd64' }
# 'arm-windows' => { goos: 'windows', arch: 'arm' },
# 'x86_64-windows' => { goos: 'windows', arch: 'amd64' },
# 'aarch64-windows' => { goos: 'windows', arch: 'arm64' }
}.freeze
base = FileUtils.pwd
pkg_dir = File.join(base, 'pkg')
ext_dir = 'lib/proscenium/ext'
ext_path = Pathname.new(base).join(ext_dir)
built_path = ext_path.join('joelmoss')
gemspec = Bundler.load_gemspec('proscenium.gemspec')
desc 'Compile for local os/arch'
task 'compile:local' => 'clobber:ext' do
sh %(go build -buildmode=c-shared -mod=mod -o #{ext_dir}/proscenium main.go)
end
desc 'Build Proscenium gems into the pkg directory.'
task build: [:clobber] + PLATFORMS.keys.map { |platform| "build:#{platform}" }
desc 'Push Proscenium gems up to the gem server.'
task push: PLATFORMS.keys.map { |platform| "push:#{platform}" }
PLATFORMS.each do |ruby_platform, go_platform|
task "build:#{ruby_platform}" => ["compile:#{ruby_platform}"] do
sh 'gem', 'build', '-V', '--platform', ruby_platform do
gem_path = Gem::Util.glob_files_in_dir("proscenium-*-#{ruby_platform}.gem",
base).max_by do |f|
File.mtime(f)
end
FileUtils.mkdir_p pkg_dir
FileUtils.mv gem_path, 'pkg'
puts ''
puts "---> Built #{gemspec.version} to pkg/proscenium-#{gemspec.version}-#{ruby_platform}.gem"
end
end
desc "Compile for #{ruby_platform}"
task "compile:#{ruby_platform}" => 'clobber:ext' do
puts ''
puts "---> Compiling for #{ruby_platform} (#{go_platform})"
if go_platform.include?('darwin')
goos, goarch = go_platform.split('/')
# rubocop:disable Layout/LineLength
sh %(GOOS=#{goos} GOARCH=#{goarch} CGO_ENABLED=1 go build -buildmode=c-shared -v -o #{ext_dir}/proscenium main.go)
# rubocop:enable Layout/LineLength
else
sh %(xgo -buildmode=c-shared -dest="#{ext_dir}" -targets="#{go_platform}" .)
built_path.each_child do |child|
if child.extname == '.h'
child.rename "#{ext_dir}/proscenium.h"
else
child.rename "#{ext_dir}/proscenium"
end
end
built_path.rmtree
end
end
desc "Push built gem (#{ruby_platform})"
task "push:#{ruby_platform}" do
sh 'gem', 'push', "pkg/proscenium-#{gemspec.version}-#{ruby_platform}.gem"
end
end
desc 'Clobber ext'
task 'clobber:ext' do
ext_path.rmtree
end
Rake::Task['clobber'].tap do |task|
task.enhance ['clobber:ext']
end