gem-ext-zig_builder 0.2.2 → 0.4.0

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: 67f8fea71de7a4772677938160444683b01d4d3aa96e55b1f719d108aa99c0d8
4
- data.tar.gz: 06b828adf36c78a949231c482130021d91022fcf625635b23750e92417b8ae36
3
+ metadata.gz: '0259aec55b71cdf4b1c568f08b02e2a81e0ab4716fd094d2335ff595e8772f10'
4
+ data.tar.gz: 1d50520f5be2d2855ee170c697c9f6aa9c0a107e62c4f3223f4290be4192d8bd
5
5
  SHA512:
6
- metadata.gz: 5c2088d9a06d0efaf5e17cdbc421b967b05e55077f0c95428ac5b8e9c283b9181c13599848b4639cec0f9b9a9783d2b235075edc49b8ca22ab6d8802071a380e
7
- data.tar.gz: 8ca3d7df154f3eadc672c88be8ea0ed3e88fa88dd7602528a9687c47fa067a3471c610268bcbc4cfa76e228df7748915f72c7bc77b54696eb75c376f428996e7
6
+ metadata.gz: '08f3e45cd701aff3ac296cd92f65adf5950d6848495f1779eeb37ccb5dd6205fc46a0d5f1812198ec0d15a76406852c9607f12f5eecb09fa8c20c0e37ce5990b'
7
+ data.tar.gz: f45f6758bd55d69af54c05475827e145bcc20c38d1ae4b8e6282f1503660ba018541717a962ee869d759c0a6240043d2017b76102022fa97c06b79855c5efbe4
@@ -1,8 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Gem
2
4
  module Ext
3
5
  class Builder; end
6
+
4
7
  class ZigBuilder < Builder
5
- VERSION = '0.2.2'
8
+ VERSION = '0.4.0'
6
9
  end
7
10
  end
8
11
  end
@@ -1,28 +1,94 @@
1
1
  # frozen_string_literal: true
2
- require 'gem/ext/zig_builder/version'
2
+
3
+ require_relative 'zig_builder/version'
3
4
  require 'rubygems/ext/builder'
4
- require 'rubygems/command'
5
- require 'shellwords'
6
5
  require 'rbconfig'
7
6
  require 'tmpdir'
8
7
 
8
+ # This class is used by rubygems to build Zig extensions. It is a thin-wrapper
9
+ # over the zig build command. To trigger this builder, specify a build.zig
10
+ # file in the gem spec.
11
+ #
12
+ # A minimal zig.build example:
13
+ #
14
+ # const std = @import("std");
15
+ # pub fn build(b: *std.Build) void {
16
+ # const optimize = b.standardOptimizeOption(.{});
17
+ # const target = b.standardTargetOptions(.{});
18
+ # const lib = b.addSharedLibrary(.{
19
+ # .name = "zigrb_example",
20
+ # .root_source_file = b.path("src/zigrb.zig"),
21
+ # .target = target,
22
+ # .optimize = optimize,
23
+ # });
24
+ # lib.linkSystemLibrary(std.posix.getenv("RUBY_PC") orelse "ruby");
25
+ # lib.linkSystemLibrary("c");
26
+ # b.installArtifact(lib);
27
+ #
28
+ # const unit_tests = b.addTest(.{
29
+ # .root_source_file = b.path("src/zigrb.zig"),
30
+ # .target = target,
31
+ # .optimize = optimize,
32
+ # });
33
+ # unit_tests.linkSystemLibrary(std.posix.getenv("RUBY_PC") orelse "ruby");
34
+ # unit_tests.linkSystemLibrary("c");
35
+ # const run_unit_tests = b.addRunArtifact(unit_tests);
36
+ # const test_step = b.step("test", "Run unit tests");
37
+ # test_step.dependOn(&run_unit_tests.step);
38
+ # }
39
+ #
40
+ # A minimal zigrb.zig example:
41
+ #
42
+ # const std = @import("std");
43
+ # const ruby = @cImport(@cInclude("ruby/ruby.h"));
44
+ # export fn Init_libzigrb_example() void {
45
+ # ruby.ruby_init();
46
+ # }
47
+ #
48
+ # An example of defining a ruby class and adding a zig method:
49
+ #
50
+ # const zig_rb_class: ruby.VALUE = ruby.rb_define_class("ZigExample", ruby.rb_cObject);
51
+ # _ = ruby.rb_define_method(zig_rb_class, "method_name", rb_example_method, 2);
52
+ #
53
+ # An example of a zig ruby method:
54
+ #
55
+ # fn rb_example_method(...) callconv(.C) ruby.VALUE {
56
+ # var ap = @cVaStart();
57
+ # defer @cVaEnd(&ap);
58
+ # // first argument is `self` in ruby; discard it
59
+ # const self = @cVaArg(&ap, ruby.VALUE); _ = self;
60
+ # // additional arguments
61
+ # const _ = @cVaArg(&ap, ruby.VALUE);
62
+ # const _ = @cVaArg(&ap, ruby.VALUE);
63
+ # return ruby.QNil
64
+ # }
65
+ #
66
+ # An example of ruby zig type conversions:
67
+ #
68
+ # const m = ruby.NUM2INT(@cVaArg(&ap, ruby.VALUE));
69
+ # const n = ruby.NUM2INT(@cVaArg(&ap, ruby.VALUE));
70
+ # return ruby.INT2NUM(f(m, n));
71
+ #
9
72
  class Gem::Ext::ZigBuilder < Gem::Ext::Builder
73
+ #
74
+ # This module patches the Builder builder_for method to ensure that
75
+ # this builder class is used for build.zig extensions.
76
+ #
10
77
  module PatchBuilderLookup
11
78
  def builder_for(extension)
12
79
  /build.zig/ === extension ? Gem::Ext::ZigBuilder : super
13
80
  end
14
81
  end
15
82
 
16
- class <<self
17
- def build(extension, dest_path, results, args=[], lib_dir=nil, extension_dir=nil)
18
- _set_vars
19
-
83
+ class << self
84
+ def build(_extension, dest_path, results, _args = [], _lib_dir = nil, extension_dir = nil)
85
+ dlext = RbConfig::CONFIG['DLEXT']
20
86
  tmp_dir = Dir.mktmpdir
21
- cmd = _build_command(tmp_dir)
22
- env = _build_env
87
+ command = _build_command(tmp_dir)
88
+ envvars = _build_envvars
23
89
 
24
- _run(cmd, results, 'zig', extension_dir, env)
25
- _install_artifacts(tmp_dir, dest_path)
90
+ _run(command, results, 'zig', extension_dir, envvars)
91
+ _install_artifacts(tmp_dir, dlext, dest_path)
26
92
 
27
93
  results
28
94
  ensure
@@ -31,61 +97,54 @@ class Gem::Ext::ZigBuilder < Gem::Ext::Builder
31
97
 
32
98
  private
33
99
 
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
100
  def _build_command(tmp_dir)
42
- [].tap {|c|
101
+ [].tap { |c|
43
102
  c << 'zig' << 'build' << '-Doptimize=ReleaseSafe'
44
103
  c << '--prefix-lib-dir' << tmp_dir
45
104
  c << 'test' << 'install'
46
105
  }
47
106
  end
48
107
 
49
- def _build_env
108
+ def _build_envvars
50
109
  {
51
- 'PKG_CONFIG_PATH' => @pkg_config_path,
52
- 'RUBY_PC' => @ruby_pc,
53
- }.tap do |env|
54
- end
110
+ 'PKG_CONFIG_PATH' => "#{RbConfig::CONFIG['libdir']}/pkgconfig",
111
+ 'RUBY_PC' => File.basename(RbConfig::CONFIG['ruby_pc'], '.pc'),
112
+ }
55
113
  end
56
114
 
57
115
  def _run(command, results, command_name, dir, env)
116
+ # old versions do not support the 4th `dir` arg
117
+ # old versions do not support the 5th `env` arg
58
118
  case method(:run).parameters.count
59
119
  when 3
60
- # old versions do not support the 4th `dir` arg
61
- # old versions do not support the 5th `env` arg
62
120
  _pollute_env(env)
63
121
  run(command, results, command_name)
64
122
  when 4
65
- # old versions do not support the 5th `env` arg
66
123
  _pollute_env(env)
67
124
  run(command, results, command_name, dir)
68
125
  when 5
69
126
  run(command, results, command_name, dir, env)
70
127
  else
71
- raise ArgumentError.new('wrong number of arguments')
128
+ raise ArgumentError, 'wrong number of arguments'
72
129
  end
73
130
  end
74
131
 
75
132
  def _pollute_env(env)
76
- env.each do |k,v| ENV[k] = v end
133
+ env.each do |k, v| ENV[k] = v end
77
134
  end
78
135
 
79
- def _install_artifacts(tmp_dir, dest_path)
136
+ def _install_artifacts(tmp_dir, dlext, dest_path)
137
+ target_vendor = RbConfig::CONFIG['target_vendor']
138
+
80
139
  Dir.chdir tmp_dir do
81
- if @target_vendor == 'apple' && @dlext == 'bundle'
140
+ if target_vendor == 'apple' && dlext == 'bundle'
82
141
  # zig builds a .dylib but ruby wants to load a .bundle
83
142
  Dir.glob('*.dylib').each do |dylib|
84
143
  FileUtils.mv dylib, "#{File.basename(dylib, '.dylib')}.bundle"
85
144
  end
86
145
  end
87
146
 
88
- FileUtils.mv Dir.glob("*.#@dlext"), dest_path
147
+ FileUtils.mv Dir.glob("*.#{dlext}"), dest_path
89
148
  end
90
149
  end
91
150
  end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ # RubyGems will load plugins in the latest version of each installed gem
4
+ # or $LOAD_PATH. Plugins must be named ‘rubygems_plugin’ (.rb, .so,
5
+ # etc) and placed at the root of your gem’s #require_path. Plugins are
6
+ # installed at a special location and loaded on boot.
7
+ # https://guides.rubygems.org/plugins/
8
+
9
+ require_relative 'gem/ext/zig_builder'
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.2.2
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Frank J. Cameron
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-05-12 00:00:00.000000000 Z
11
+ date: 2024-05-22 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: ''
14
14
  email:
@@ -19,6 +19,7 @@ extra_rdoc_files: []
19
19
  files:
20
20
  - lib/gem/ext/zig_builder.rb
21
21
  - lib/gem/ext/zig_builder/version.rb
22
+ - lib/rubygems_plugin.rb
22
23
  homepage: https://gitlab.com/fjc/gem-ext-zig_builder
23
24
  licenses:
24
25
  - MIT
@@ -26,7 +27,8 @@ metadata:
26
27
  homepage_uri: https://gitlab.com/fjc/gem-ext-zig_builder
27
28
  source_code_uri: https://gitlab.com/fjc/gem-ext-zig_builder
28
29
  changelog_uri: https://gitlab.com/fjc/gem-ext-zig_builder
29
- post_install_message:
30
+ rubygems_mfa_required: 'true'
31
+ post_install_message:
30
32
  rdoc_options: []
31
33
  require_paths:
32
34
  - lib
@@ -34,15 +36,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
34
36
  requirements:
35
37
  - - ">="
36
38
  - !ruby/object:Gem::Version
37
- version: 2.3.0
39
+ version: 2.5.0
38
40
  required_rubygems_version: !ruby/object:Gem::Requirement
39
41
  requirements:
40
42
  - - ">="
41
43
  - !ruby/object:Gem::Version
42
44
  version: '0'
43
45
  requirements: []
44
- rubygems_version: 3.2.5
45
- signing_key:
46
+ rubygems_version: 3.3.26
47
+ signing_key:
46
48
  specification_version: 4
47
49
  summary: Gem extension builder for zig
48
50
  test_files: []