rcee_precompiled 0.4.0 → 0.5.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +1 -1
- data/README.md +47 -23
- data/Rakefile +15 -6
- data/ext/precompiled/extconf.rb +64 -27
- data/lib/rcee/precompiled/version.rb +1 -1
- data/rcee_precompiled.gemspec +1 -1
- metadata +5 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6c583c2991831575bd4b095940cab064079fd65c438c5e3a165e96a5d8ac7c90
|
4
|
+
data.tar.gz: 1ce001df04c891b01bbf8d16914d2535548b557f10eda7f65409e0aae0550c08
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5c2bce5e9222da79298c9f62a2080b3cdd96fa73dc79dc937385d9ca68531734e946edd9e94c199bef24da4e70dd319d18ab360b4b871f33277aca93347b7d6b
|
7
|
+
data.tar.gz: 7133eb473bd9f35f52335b4429291497ecda8c62e217d58a60d78d3a1dfa34bfd2e7dbdfe7d098aab497243c7ae81abc0695ceb77584c411cc1620fb9fc957e4
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -26,15 +26,20 @@ This is really powerful stuff, and once we assume that we can cross-compile reli
|
|
26
26
|
First, we need to add some features to our `Rake::ExtensionTask` in `Rakefile`:
|
27
27
|
|
28
28
|
``` ruby
|
29
|
-
cross_rubies = ["3.
|
29
|
+
cross_rubies = ["3.3.0", "3.2.0", "3.1.0", "3.0.0"]
|
30
30
|
cross_platforms = [
|
31
|
-
"
|
31
|
+
"aarch64-linux-gnu",
|
32
|
+
"aarch64-linux-musl",
|
33
|
+
"arm-linux-gnu",
|
34
|
+
"arm-linux-musl",
|
35
|
+
"arm64-darwin",
|
32
36
|
"x64-mingw-ucrt",
|
33
|
-
"
|
34
|
-
"
|
35
|
-
"
|
37
|
+
"x64-mingw32",
|
38
|
+
"x86-linux-gnu",
|
39
|
+
"x86-linux-musl",
|
36
40
|
"x86_64-darwin",
|
37
|
-
"
|
41
|
+
"x86_64-linux-gnu",
|
42
|
+
"x86_64-linux-musl",
|
38
43
|
]
|
39
44
|
ENV["RUBY_CC_VERSION"] = cross_rubies.join(":")
|
40
45
|
|
@@ -92,30 +97,33 @@ The top task (or, really, _set_ of tasks) runs on the host system, and invokes t
|
|
92
97
|
Changes to the `extconf.rb`:
|
93
98
|
|
94
99
|
``` ruby
|
95
|
-
|
100
|
+
def configure_cross_compilers
|
101
|
+
RbConfig::CONFIG["CC"] = RbConfig::MAKEFILE_CONFIG["CC"] = ENV["CC"] if ENV["CC"]
|
102
|
+
ENV["CC"] = RbConfig::CONFIG["CC"]
|
103
|
+
end
|
96
104
|
```
|
97
105
|
|
98
106
|
This makes sure that the cross-compiler is the compiler used within the guest container (and not the native linux compiler).
|
99
107
|
|
100
108
|
``` ruby
|
101
|
-
|
109
|
+
def cross_build?
|
110
|
+
enable_config("cross-build")
|
111
|
+
end
|
102
112
|
```
|
103
113
|
|
104
114
|
The cross-compile rake task signals to `extconf.rb` that it's cross-compiling by using a commandline flag that we can inspect. We'll need this for `libyaml` to make sure that set the appropriate flags during precompilation (flags which shouldn't be set when compiling natively).
|
105
115
|
|
106
116
|
``` ruby
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
117
|
+
# pass some environment variables to the libyaml configuration for cross-compilation
|
118
|
+
if cross_build?
|
119
|
+
ENV.to_h.tap do |env|
|
120
|
+
# -fPIC is necessary for linking into a shared library
|
121
|
+
env["CFLAGS"] = [env["CFLAGS"], "-fPIC"].join(" ")
|
122
|
+
env["SUBDIRS"] = "include src" # libyaml: skip tests
|
123
|
+
|
124
|
+
recipe.configure_options += env.map { |key, value| "#{key}=#{value.strip}" }
|
125
|
+
end
|
116
126
|
end
|
117
|
-
# ...
|
118
|
-
end
|
119
127
|
```
|
120
128
|
|
121
129
|
The rest of the extconf changes are related to configuring libyaml at build time. We need to set the -fPIC option so we can mix static and shared libraries together. (This should probably always be set.)
|
@@ -128,14 +136,14 @@ We have one more small change we'll need to make to how the extension is require
|
|
128
136
|
lib
|
129
137
|
└── rcee
|
130
138
|
├── precompiled
|
131
|
-
│ ├── 2.6
|
132
|
-
│ │ └── precompiled.so
|
133
|
-
│ ├── 2.7
|
134
|
-
│ │ └── precompiled.so
|
135
139
|
│ ├── 3.0
|
136
140
|
│ │ └── precompiled.so
|
137
141
|
│ ├── 3.1
|
138
142
|
│ │ └── precompiled.so
|
143
|
+
│ ├── 3.2
|
144
|
+
│ │ └── precompiled.so
|
145
|
+
│ ├── 3.3
|
146
|
+
│ │ └── precompiled.so
|
139
147
|
│ └── version.rb
|
140
148
|
└── precompiled.rb
|
141
149
|
```
|
@@ -158,6 +166,22 @@ end
|
|
158
166
|
Go ahead and try it! `gem install rcee_precompiled`. If you're on windows, linux, or macos you should get a precompiled version that installs in under a second. Everyone else (hello FreeBSD people!) it'll take a few more seconds to build the vanilla gem's packaged tarball.
|
159
167
|
|
160
168
|
|
169
|
+
### Windows notes
|
170
|
+
|
171
|
+
You may have noticed that the gem for `x64-mingw32` only contains `.../3.0/precompiled.so` and not a shared library for 3.1, 3.2, or 3.3. Conversely, the gem for `x64-mingw-ucrt` does *not* contain 3.0 (but contains 3.1..3.3 inclusive).
|
172
|
+
|
173
|
+
This is because the Windows installer for Ruby switched from `msvcrt` to `ucrt` for the C standard library. Rubies 3.0 and earlier use `msvcrt` but later Rubies use `ucrt`. So if you're on Ruby 3.0 on Windows, bundler will resolve your platform as `x64-mingw32`; but if you're on Ruby 3.1 or later, bundler will resolve your platform as `x64-mingw-ucrt`. See [the rubyinstaller release notes](https://rubyinstaller.org/2021/12/31/rubyinstaller-3.1.0-1-released.html) for more information.
|
174
|
+
|
175
|
+
This is all taken care of for you by `rake-compiler-dock` and there's no additional work necessary to create those specialized native gems. However, you'll see this libc transition in the CI testing matrix in `.github/workflows/precompiled.yml`.
|
176
|
+
|
177
|
+
|
178
|
+
### Linux notes
|
179
|
+
|
180
|
+
On Linux, the default C library for a long time was GNU libc, and so when you see native gem platforms like `x86_64-linux` the _implied_ libc is `gnu`. However, in recent years the `musl` libc project has gotten off the ground in distributions like Alpine. Often, `musl` systems are able to run code compiled on (and for) `gnu` systems; but not always. And in those incompatible cases, users had to work around it or avoid Alpine.
|
181
|
+
|
182
|
+
As of Rubygems 3.3.22, the `gem` and `bundle` tools are able to distinguish platforms by their libc type (e.g., `x86_64-linux-gnu` versus `x86_64-linux-musl`); and `rake-compiler-dock` started to be able to build `musl` native gems as of v1.5.0.
|
183
|
+
|
184
|
+
|
161
185
|
## Testing
|
162
186
|
|
163
187
|
See [.github/workflows/precompiled.yml](../.github/workflows/precompiled.yml)
|
data/Rakefile
CHANGED
@@ -6,22 +6,26 @@ require "rake/testtask"
|
|
6
6
|
require "rake/extensiontask"
|
7
7
|
require "rake_compiler_dock"
|
8
8
|
|
9
|
-
cross_rubies = ["3.
|
9
|
+
cross_rubies = ["3.3.0", "3.2.0", "3.1.0", "3.0.0"]
|
10
10
|
cross_platforms = [
|
11
|
-
"aarch64-linux",
|
12
|
-
"
|
11
|
+
"aarch64-linux-gnu",
|
12
|
+
"aarch64-linux-musl",
|
13
|
+
"arm-linux-gnu",
|
14
|
+
"arm-linux-musl",
|
13
15
|
"arm64-darwin",
|
14
16
|
"x64-mingw-ucrt",
|
15
17
|
"x64-mingw32",
|
16
|
-
"x86-linux",
|
18
|
+
"x86-linux-gnu",
|
19
|
+
"x86-linux-musl",
|
17
20
|
"x86_64-darwin",
|
18
|
-
"x86_64-linux",
|
21
|
+
"x86_64-linux-gnu",
|
22
|
+
"x86_64-linux-musl",
|
19
23
|
]
|
20
24
|
ENV["RUBY_CC_VERSION"] = cross_rubies.join(":")
|
21
25
|
|
22
26
|
rcee_precompiled_spec = Bundler.load_gemspec("rcee_precompiled.gemspec")
|
23
27
|
Gem::PackageTask.new(rcee_precompiled_spec).define #packaged_tarball version of the gem for platform=ruby
|
24
|
-
task "package" =>
|
28
|
+
task "package" => "gem:all"
|
25
29
|
|
26
30
|
Rake::TestTask.new(:test) do |t|
|
27
31
|
t.libs << "test"
|
@@ -38,6 +42,11 @@ Rake::ExtensionTask.new("precompiled", rcee_precompiled_spec) do |ext|
|
|
38
42
|
# remove things not needed for precompiled gems
|
39
43
|
spec.dependencies.reject! { |dep| dep.name == "mini_portile2" }
|
40
44
|
spec.files.reject! { |file| File.fnmatch?("*.tar.gz", file) }
|
45
|
+
|
46
|
+
if spec.platform.os == "linux"
|
47
|
+
# the `-gnu` suffix is not recognized in earlier versions of rubygems
|
48
|
+
spec.required_rubygems_version.concat([">= 3.3.22"])
|
49
|
+
end
|
41
50
|
end
|
42
51
|
end
|
43
52
|
|
data/ext/precompiled/extconf.rb
CHANGED
@@ -1,41 +1,78 @@
|
|
1
1
|
require "mkmf"
|
2
2
|
require "mini_portile2"
|
3
3
|
|
4
|
-
|
4
|
+
module RCEE
|
5
|
+
module Precompiled
|
6
|
+
module ExtConf
|
7
|
+
PACKAGE_ROOT_DIR = File.expand_path(File.join(File.dirname(__FILE__), "..", ".."))
|
5
8
|
|
6
|
-
|
7
|
-
|
9
|
+
class << self
|
10
|
+
def configure
|
11
|
+
configure_cross_compilers
|
12
|
+
configure_packaged_libraries
|
13
|
+
create_makefile("rcee/precompiled/precompiled")
|
14
|
+
end
|
8
15
|
|
9
|
-
|
16
|
+
def configure_cross_compilers
|
17
|
+
RbConfig::CONFIG["CC"] = RbConfig::MAKEFILE_CONFIG["CC"] = ENV["CC"] if ENV["CC"]
|
18
|
+
ENV["CC"] = RbConfig::CONFIG["CC"]
|
19
|
+
end
|
10
20
|
|
11
|
-
|
12
|
-
|
13
|
-
url: "https://github.com/yaml/libyaml/releases/download/0.2.5/yaml-0.2.5.tar.gz",
|
14
|
-
sha256: "c642ae9b75fee120b2d96c712538bd2cf283228d2337df2cf2988e3c02678ef4",
|
15
|
-
}]
|
16
|
-
recipe.target = File.join(package_root_dir, "ports")
|
21
|
+
def configure_packaged_libraries
|
22
|
+
recipe = libyaml_recipe
|
17
23
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
+
# pass some environment variables to the libyaml configuration for cross-compilation
|
25
|
+
if cross_build?
|
26
|
+
ENV.to_h.tap do |env|
|
27
|
+
# -fPIC is necessary for linking into a shared library
|
28
|
+
env["CFLAGS"] = [env["CFLAGS"], "-fPIC"].join(" ")
|
29
|
+
env["SUBDIRS"] = "include src" # libyaml: skip tests
|
24
30
|
|
25
|
-
|
26
|
-
|
27
|
-
|
31
|
+
recipe.configure_options += env.map { |key, value| "#{key}=#{value.strip}" }
|
32
|
+
end
|
33
|
+
end
|
28
34
|
|
29
|
-
|
30
|
-
|
31
|
-
|
35
|
+
# ensure libyaml has already been unpacked, configured, and compiled
|
36
|
+
unless File.exist?(File.join(recipe.target, recipe.host, recipe.name, recipe.version))
|
37
|
+
recipe.cook
|
38
|
+
end
|
39
|
+
|
40
|
+
# use the packaged libyaml
|
41
|
+
recipe.activate
|
42
|
+
pkg_config(File.join(recipe.path, "lib", "pkgconfig", "yaml-0.1.pc"))
|
43
|
+
|
44
|
+
# assert that we can build against the packaged libyaml
|
45
|
+
unless have_library("yaml", "yaml_get_version", "yaml.h")
|
46
|
+
abort("\nERROR: *** could not find libyaml development environment ***\n\n")
|
47
|
+
end
|
48
|
+
end
|
32
49
|
|
33
|
-
|
34
|
-
|
50
|
+
def cross_build?
|
51
|
+
enable_config("cross-build")
|
52
|
+
end
|
53
|
+
|
54
|
+
def download
|
55
|
+
libyaml_recipe.download
|
56
|
+
end
|
57
|
+
|
58
|
+
def libyaml_recipe
|
59
|
+
MiniPortile.new("yaml", "0.2.5").tap do |recipe|
|
60
|
+
recipe.files = [{
|
61
|
+
url: "https://github.com/yaml/libyaml/releases/download/0.2.5/yaml-0.2.5.tar.gz",
|
62
|
+
sha256: "c642ae9b75fee120b2d96c712538bd2cf283228d2337df2cf2988e3c02678ef4"
|
63
|
+
}]
|
64
|
+
recipe.target = File.join(PACKAGE_ROOT_DIR, "ports")
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
35
70
|
end
|
36
71
|
|
37
|
-
|
38
|
-
|
72
|
+
# run "ruby ./ext/precompiled/extconf.rb -- --download-dependencies" to download the tarball
|
73
|
+
if arg_config("--download-dependencies")
|
74
|
+
RCEE::Precompiled::ExtConf.download
|
75
|
+
exit!(0)
|
39
76
|
end
|
40
77
|
|
41
|
-
|
78
|
+
RCEE::Precompiled::ExtConf.configure
|
data/rcee_precompiled.gemspec
CHANGED
@@ -15,7 +15,7 @@ Gem::Specification.new do |spec|
|
|
15
15
|
spec.summary = "Example gem demonstrating a basic C extension."
|
16
16
|
spec.description = "Part of a project to explain how Ruby C extensions work."
|
17
17
|
spec.homepage = "https://github.com/flavorjones/ruby-c-extensions-explained"
|
18
|
-
spec.required_ruby_version = ">=
|
18
|
+
spec.required_ruby_version = ">= 3.0.0"
|
19
19
|
spec.license = "MIT"
|
20
20
|
|
21
21
|
# Specify which files should be added to the gem when it is released.
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rcee_precompiled
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mike Dalessio
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-01-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mini_portile2
|
@@ -24,6 +24,7 @@ dependencies:
|
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
|
+
force_ruby_platform: false
|
27
28
|
description: Part of a project to explain how Ruby C extensions work.
|
28
29
|
email:
|
29
30
|
- mike.dalessio@gmail.com
|
@@ -55,14 +56,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
55
56
|
requirements:
|
56
57
|
- - ">="
|
57
58
|
- !ruby/object:Gem::Version
|
58
|
-
version:
|
59
|
+
version: 3.0.0
|
59
60
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
61
|
requirements:
|
61
62
|
- - ">="
|
62
63
|
- !ruby/object:Gem::Version
|
63
64
|
version: '0'
|
64
65
|
requirements: []
|
65
|
-
rubygems_version: 3.
|
66
|
+
rubygems_version: 3.4.19
|
66
67
|
signing_key:
|
67
68
|
specification_version: 4
|
68
69
|
summary: Example gem demonstrating a basic C extension.
|