gem-ext-zig_builder 0.1.1 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bfa1cdba6ede4366c9bbc5fd0973a1ff102e5284b25aafc5d18c6af6ab7d967e
4
- data.tar.gz: 3fb02e99621c79b78df6a7fe8d61e08b316bbdefb97f10941116d4a198946995
3
+ metadata.gz: 67f8fea71de7a4772677938160444683b01d4d3aa96e55b1f719d108aa99c0d8
4
+ data.tar.gz: 06b828adf36c78a949231c482130021d91022fcf625635b23750e92417b8ae36
5
5
  SHA512:
6
- metadata.gz: 4e55584a1a5e0997645c978d21b308d0a3c0f69158eddfd7c84300d6f457a1f11e0c7d231eb41449f09d2f603fb745ca9cd1d38bd3e99e8fa43c5c6a820705db
7
- data.tar.gz: 2e8f94cf078dd2b0a571feaa8a4f983eb485e09c730469820ffbc40066dda7bf9e2dd04c2e9e0b538891376ea9d304b13e271a70de058530d351ab04bf41daed
6
+ metadata.gz: 5c2088d9a06d0efaf5e17cdbc421b967b05e55077f0c95428ac5b8e9c283b9181c13599848b4639cec0f9b9a9783d2b235075edc49b8ca22ab6d8802071a380e
7
+ data.tar.gz: 8ca3d7df154f3eadc672c88be8ea0ed3e88fa88dd7602528a9687c47fa067a3471c610268bcbc4cfa76e228df7748915f72c7bc77b54696eb75c376f428996e7
@@ -2,7 +2,7 @@ module Gem
2
2
  module Ext
3
3
  class Builder; end
4
4
  class ZigBuilder < Builder
5
- VERSION = '0.1.1'
5
+ VERSION = '0.2.2'
6
6
  end
7
7
  end
8
8
  end
@@ -4,6 +4,7 @@ require 'rubygems/ext/builder'
4
4
  require 'rubygems/command'
5
5
  require 'shellwords'
6
6
  require 'rbconfig'
7
+ require 'tmpdir'
7
8
 
8
9
  class Gem::Ext::ZigBuilder < Gem::Ext::Builder
9
10
  module PatchBuilderLookup
@@ -12,24 +13,81 @@ class Gem::Ext::ZigBuilder < Gem::Ext::Builder
12
13
  end
13
14
  end
14
15
 
15
- def self.build(extension, dest_path, results, args=[], lib_dir=nil, extension_dir=nil)
16
- # older versions of ruby do not support the 5th `env` argument to run,
17
- # so pollute the global ENV as a cheap workaround
18
- ENV['PKG_CONFIG_PATH'] = "#{RbConfig::CONFIG['libdir']}/pkgconfig"
19
- ENV['RUBY_PC' ] = "#{File.basename(RbConfig::CONFIG['ruby_pc'], '.pc')}"
16
+ class <<self
17
+ def build(extension, dest_path, results, args=[], lib_dir=nil, extension_dir=nil)
18
+ _set_vars
20
19
 
21
- run(
20
+ tmp_dir = Dir.mktmpdir
21
+ cmd = _build_command(tmp_dir)
22
+ env = _build_env
23
+
24
+ _run(cmd, results, 'zig', extension_dir, env)
25
+ _install_artifacts(tmp_dir, dest_path)
26
+
27
+ results
28
+ ensure
29
+ FileUtils.remove_entry tmp_dir if tmp_dir
30
+ end
31
+
32
+ private
33
+
34
+ def _set_vars
35
+ @dlext = RbConfig::CONFIG['DLEXT']
36
+ @target_vendor = RbConfig::CONFIG['target_vendor']
37
+ @pkg_config_path = "#{RbConfig::CONFIG['libdir']}/pkgconfig"
38
+ @ruby_pc = "#{File.basename(RbConfig::CONFIG['ruby_pc'], '.pc')}"
39
+ end
40
+
41
+ def _build_command(tmp_dir)
22
42
  [].tap {|c|
23
43
  c << 'zig' << 'build' << '-Doptimize=ReleaseSafe'
24
- c << '--prefix-lib-dir' << dest_path
44
+ c << '--prefix-lib-dir' << tmp_dir
25
45
  c << 'test' << 'install'
26
- },
27
- results,
28
- 'zig',
29
- extension_dir
30
- )
46
+ }
47
+ end
31
48
 
32
- results
49
+ def _build_env
50
+ {
51
+ 'PKG_CONFIG_PATH' => @pkg_config_path,
52
+ 'RUBY_PC' => @ruby_pc,
53
+ }.tap do |env|
54
+ end
55
+ end
56
+
57
+ def _run(command, results, command_name, dir, env)
58
+ case method(:run).parameters.count
59
+ when 3
60
+ # old versions do not support the 4th `dir` arg
61
+ # old versions do not support the 5th `env` arg
62
+ _pollute_env(env)
63
+ run(command, results, command_name)
64
+ when 4
65
+ # old versions do not support the 5th `env` arg
66
+ _pollute_env(env)
67
+ run(command, results, command_name, dir)
68
+ when 5
69
+ run(command, results, command_name, dir, env)
70
+ else
71
+ raise ArgumentError.new('wrong number of arguments')
72
+ end
73
+ end
74
+
75
+ def _pollute_env(env)
76
+ env.each do |k,v| ENV[k] = v end
77
+ end
78
+
79
+ def _install_artifacts(tmp_dir, dest_path)
80
+ Dir.chdir tmp_dir do
81
+ if @target_vendor == 'apple' && @dlext == 'bundle'
82
+ # zig builds a .dylib but ruby wants to load a .bundle
83
+ Dir.glob('*.dylib').each do |dylib|
84
+ FileUtils.mv dylib, "#{File.basename(dylib, '.dylib')}.bundle"
85
+ end
86
+ end
87
+
88
+ FileUtils.mv Dir.glob("*.#@dlext"), dest_path
89
+ end
90
+ end
33
91
  end
34
92
  end
35
93
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gem-ext-zig_builder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Frank J. Cameron
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-05-08 00:00:00.000000000 Z
11
+ date: 2023-05-12 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: ''
14
14
  email: