99 require "rubocop/rake_task"
1010 RuboCop ::RakeTask . new
1111rescue LoadError
12- # rubocop not available in cross-compilation environment
13- end
14-
15- begin
16- require "rake/extensiontask"
17-
18- PLATFORMS = [
19- "aarch64-linux-gnu" ,
20- "aarch64-linux-musl" ,
21- "arm-linux-gnu" ,
22- "arm-linux-musl" ,
23- "arm64-darwin" ,
24- "x86-linux-gnu" ,
25- "x86-linux-musl" ,
26- "x86_64-darwin" ,
27- "x86_64-linux-gnu" ,
28- "x86_64-linux-musl"
29- ] . freeze
30-
31- GO_PLATFORMS = {
32- "aarch64-linux-gnu" => { goos : "linux" , goarch : "arm64" , cc : "aarch64-linux-gnu-gcc" } ,
33- "aarch64-linux-musl" => { goos : "linux" , goarch : "arm64" , cc : "aarch64-linux-musl-gcc" } ,
34- "arm-linux-gnu" => { goos : "linux" , goarch : "arm" , cc : "arm-linux-gnueabihf-gcc" } ,
35- "arm-linux-musl" => { goos : "linux" , goarch : "arm" , cc : "arm-linux-musleabihf-gcc" } ,
36- "arm64-darwin" => { goos : "darwin" , goarch : "arm64" , cc : "o64-clang" } ,
37- "x86-linux-gnu" => { goos : "linux" , goarch : "386" , cc : "i686-linux-gnu-gcc" } ,
38- "x86-linux-musl" => { goos : "linux" , goarch : "386" , cc : "i686-unknown-linux-musl-gcc" } ,
39- "x86_64-darwin" => { goos : "darwin" , goarch : "amd64" , cc : "o64-clang" } ,
40- "x86_64-linux-gnu" => { goos : "linux" , goarch : "amd64" , cc : "x86_64-linux-gnu-gcc" } ,
41- "x86_64-linux-musl" => { goos : "linux" , goarch : "amd64" , cc : "gcc" }
42- } . freeze
43-
44- def go_version
45- go_mod = File . read ( "go/go.mod" )
46- go_mod [ /^go (\d +\. \d +\. \d +)/ , 1 ] || go_mod [ /^go (\d +\. \d +)/ , 1 ]
47- end
48-
49- def detect_go_platform
50- cpu = RbConfig ::CONFIG [ "host_cpu" ]
51- os = RbConfig ::CONFIG [ "host_os" ]
52-
53- arch = case cpu
54- when /aarch64|arm64/ then "arm64"
55- when /x86_64|amd64/ then "amd64"
56- else cpu
57- end
58-
59- goos = case os
60- when /darwin/ then "darwin"
61- else "linux"
62- end
63-
64- "#{ goos } _#{ arch } "
65- end
66-
67- namespace :go do
68- desc "Build Go archive for current platform"
69- task :build do
70- platform = detect_go_platform
71- output_dir = "go/build/#{ platform } "
72- FileUtils . mkdir_p ( output_dir )
73- sh "cd go && CGO_ENABLED=1 go build -buildmode=c-archive -o build/#{ platform } /liblipgloss.a ."
74- end
75-
76- desc "Build Go archives for all platforms"
77- task :build_all do
78- GO_PLATFORMS . each_value do |env |
79- output_dir = "go/build/#{ env [ :goos ] } _#{ env [ :goarch ] } "
80- FileUtils . mkdir_p ( output_dir )
81- sh "cd go && CGO_ENABLED=1 GOOS=#{ env [ :goos ] } GOARCH=#{ env [ :goarch ] } go build -buildmode=c-archive -o build/#{ env [ :goos ] } _#{ env [ :goarch ] } /liblipgloss.a ."
82- end
83- end
84-
85- desc "Clean Go build artifacts"
86- task :clean do
87- FileUtils . rm_rf ( "go/build" )
88- end
89-
90- desc "Format Go source files"
91- task :fmt do
92- sh "gofmt -s -w go/"
93- end
94- end
95-
96- Rake ::ExtensionTask . new do |ext |
97- ext . name = "lipgloss"
98- ext . ext_dir = "ext/lipgloss"
99- ext . lib_dir = "lib/lipgloss"
100- ext . source_pattern = "*.c"
101- ext . gem_spec = Gem ::Specification . load ( "lipgloss.gemspec" )
102- ext . cross_compile = true
103- ext . cross_platform = PLATFORMS
104- end
105-
106- namespace "gem" do
107- task "prepare" do
108- require "rake_compiler_dock"
109-
110- sh "bundle config set cache_all true"
111-
112- gemspec_path = File . expand_path ( "./lipgloss.gemspec" , __dir__ )
113- spec = eval ( File . read ( gemspec_path ) , binding , gemspec_path )
114-
115- RakeCompilerDock . set_ruby_cc_version ( spec . required_ruby_version . as_list )
116- rescue LoadError
117- abort "rake_compiler_dock is required for this task"
118- end
119-
120- PLATFORMS . each do |platform |
121- desc "Build the native gem for #{ platform } "
122- task platform => "prepare" do
123- require "rake_compiler_dock"
124-
125- env = GO_PLATFORMS [ platform ]
126-
127- build_script = <<~BASH
128- curl -sSL https://go.dev/dl/go#{ go_version } .linux-amd64.tar.gz -o /tmp/go.tar.gz && \
129- sudo tar -C /usr/local -xzf /tmp/go.tar.gz && \
130- rm /tmp/go.tar.gz && \
131- export PATH=$PATH:/usr/local/go/bin && \
132- cd go && \
133- mkdir -p build/#{ env [ :goos ] } _#{ env [ :goarch ] } && \
134- CGO_ENABLED=1 CC=#{ env [ :cc ] } GOOS=#{ env [ :goos ] } GOARCH=#{ env [ :goarch ] } go build -buildmode=c-archive -o build/#{ env [ :goos ] } _#{ env [ :goarch ] } /liblipgloss.a . && \
135- cd .. && \
136- rm -f .ruby-version && \
137- rm -rf vendor/bundle && \
138- bundle install && \
139- rake native:#{ platform } gem RUBY_CC_VERSION='#{ ENV . fetch ( "RUBY_CC_VERSION" , nil ) } '
140- BASH
141-
142- RakeCompilerDock . sh ( build_script , platform : platform )
143- end
144- end
145- end
146- rescue LoadError => e
147- desc "Compile task not available (rake-compiler not installed)"
148- task :compile do
149- puts e
150- abort <<~MESSAGE
151-
152- rake-compiler is required for this task.
153-
154- Are you running `rake` using `bundle exec rake`?
155-
156- Otherwise:
157- * try to run bundle install
158- * add it to your Gemfile
159- * or install it with: gem install rake-compiler
160- MESSAGE
161- end
12+ # rubocop not available
16213end
16314
16415task :rbs_inline do
@@ -179,4 +30,4 @@ task :rbs_inline do
17930 end
18031end
18132
182- task default : [ :test , :rubocop , :compile ]
33+ task default : [ :test , :rubocop ]
0 commit comments