rb_sys 0.1.2 → 0.9.1
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 +4 -4
- checksums.yaml.gz.sig +0 -0
- data/LICENSE-APACHE +190 -0
- data/LICENSE-MIT +21 -0
- data/lib/rb_sys/cargo_builder/link_flag_converter.rb +25 -0
- data/lib/rb_sys/cargo_builder.rb +333 -0
- data/lib/rb_sys/mkmf.rb +53 -8
- data/lib/rb_sys/version.rb +1 -1
- data.tar.gz.sig +0 -0
- metadata +4 -4
- metadata.gz.sig +0 -0
- data/LICENSE-APACHE +0 -1
- data/LICENSE-MIT +0 -1
- data/vendor/rubygems/ext/cargo_builder/link_flag_converter.rb +0 -23
- data/vendor/rubygems/ext/cargo_builder.rb +0 -330
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: b22b7aeab8b1c9d4e6ad7225f110ad9274594ac53bf1b6284421bb9728d6cdb2
|
|
4
|
+
data.tar.gz: d14f49223946ead201af5b97ab3bf872924da318427762db28939201cca10248
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: '049e3e24630545530e01983ab43288e992e73cfcc74e8b2f070735ece4aa3b1e5488a5fc473410c6539f00d162e8366c0b7aebde3bbda2cdf57f579a6d684f18'
|
|
7
|
+
data.tar.gz: 979ba1c4c4faf11316f576ea1ec103c244bb20dcca42a13e0e23cf7b886e5f249bb8169a14b6bcf2cf22aedf121b53a9e96a6068e6e6432e873924714f6368ae
|
checksums.yaml.gz.sig
CHANGED
|
Binary file
|
data/LICENSE-APACHE
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
Copyright 2021-2022 Ian Ker-Seymer
|
|
179
|
+
|
|
180
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
181
|
+
you may not use this file except in compliance with the License.
|
|
182
|
+
You may obtain a copy of the License at
|
|
183
|
+
|
|
184
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
185
|
+
|
|
186
|
+
Unless required by applicable law or agreed to in writing, software
|
|
187
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
188
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
189
|
+
See the License for the specific language governing permissions and
|
|
190
|
+
limitations under the License.
|
data/LICENSE-MIT
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021-2022 Ian Ker-Seymer
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RbSys
|
|
4
|
+
class CargoBuilder
|
|
5
|
+
# Converts Ruby link flags into something cargo understands
|
|
6
|
+
class LinkFlagConverter
|
|
7
|
+
def self.convert(arg)
|
|
8
|
+
case arg.chomp
|
|
9
|
+
when /^-L\s*(.+)$/
|
|
10
|
+
["-L", "native=#{$1}"]
|
|
11
|
+
when /^--library=(\w+\S+)$/, /^-l\s*(\w+\S+)$/
|
|
12
|
+
["-l", $1]
|
|
13
|
+
when /^-l\s*:lib(\S+).a$/
|
|
14
|
+
["-l", "static=#{$1}"]
|
|
15
|
+
when /^-l\s*:lib(\S+).(so|dylib|dll)$/
|
|
16
|
+
["-l", "dylib=#{$1}"]
|
|
17
|
+
when /^-F\s*(.*)$/
|
|
18
|
+
["-l", "framework=#{$1}"]
|
|
19
|
+
else
|
|
20
|
+
["-C", "link_arg=#{arg}"]
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,333 @@
|
|
|
1
|
+
module RbSys
|
|
2
|
+
class CargoBuilder < Gem::Ext::Builder
|
|
3
|
+
attr_accessor :spec, :runner, :profile, :env, :features, :target, :extra_rustc_args
|
|
4
|
+
|
|
5
|
+
def initialize(spec)
|
|
6
|
+
require "rubygems/command"
|
|
7
|
+
require_relative "cargo_builder/link_flag_converter"
|
|
8
|
+
|
|
9
|
+
@spec = spec
|
|
10
|
+
@runner = self.class.method(:run)
|
|
11
|
+
@profile = ENV.fetch("CARGO_BUILD_PROFILE", :release).to_sym
|
|
12
|
+
@env = {}
|
|
13
|
+
@features = []
|
|
14
|
+
@target = ENV["CARGO_BUILD_TARGET"]
|
|
15
|
+
@extra_rustc_args = []
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def build(_extension, dest_path, results, args = [], lib_dir = nil, cargo_dir = Dir.pwd)
|
|
19
|
+
require "fileutils"
|
|
20
|
+
require "shellwords"
|
|
21
|
+
|
|
22
|
+
build_crate(dest_path, results, args, cargo_dir)
|
|
23
|
+
validate_cargo_build!(dest_path)
|
|
24
|
+
rename_cdylib_for_ruby_compatibility(dest_path)
|
|
25
|
+
finalize_directory(dest_path, lib_dir, cargo_dir)
|
|
26
|
+
results
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def build_crate(dest_path, results, args, cargo_dir)
|
|
30
|
+
env = build_env
|
|
31
|
+
cmd = cargo_command(cargo_dir, dest_path, args)
|
|
32
|
+
runner.call cmd, results, "cargo", cargo_dir, env
|
|
33
|
+
|
|
34
|
+
results
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def build_env
|
|
38
|
+
build_env = rb_config_env
|
|
39
|
+
build_env["RUBY_STATIC"] = "true" if ruby_static? && ENV.key?("RUBY_STATIC")
|
|
40
|
+
build_env.merge(env)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def cargo_command(cargo_dir, dest_path, args = [])
|
|
44
|
+
manifest = File.join(cargo_dir, "Cargo.toml")
|
|
45
|
+
cargo = ENV.fetch("CARGO", "cargo")
|
|
46
|
+
|
|
47
|
+
cmd = []
|
|
48
|
+
cmd += [cargo, "rustc"]
|
|
49
|
+
cmd += ["--target", target] if target
|
|
50
|
+
cmd += ["--target-dir", dest_path]
|
|
51
|
+
cmd += ["--features", features.join(",")] unless features.empty?
|
|
52
|
+
cmd += ["--manifest-path", manifest]
|
|
53
|
+
cmd += ["--lib"]
|
|
54
|
+
cmd += ["--profile", profile.to_s]
|
|
55
|
+
cmd += ["--locked"] if profile.to_s == "release"
|
|
56
|
+
cmd += Gem::Command.build_args
|
|
57
|
+
cmd += args
|
|
58
|
+
cmd += ["--"]
|
|
59
|
+
cmd += [*cargo_rustc_args(dest_path)]
|
|
60
|
+
cmd += extra_rustc_args
|
|
61
|
+
cmd
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def cargo_dylib_path(dest_path)
|
|
65
|
+
prefix = so_ext == "dll" ? "" : "lib"
|
|
66
|
+
path_parts = [dest_path]
|
|
67
|
+
path_parts << target if target
|
|
68
|
+
path_parts += [profile_target_directory, "#{prefix}#{cargo_crate_name}.#{so_ext}"]
|
|
69
|
+
File.join(*path_parts)
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
private
|
|
73
|
+
|
|
74
|
+
def rb_config_env
|
|
75
|
+
result = {}
|
|
76
|
+
RbConfig::CONFIG.each { |k, v| result["RBCONFIG_#{k}"] = v }
|
|
77
|
+
result
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def cargo_rustc_args(dest_dir)
|
|
81
|
+
[
|
|
82
|
+
*linker_args,
|
|
83
|
+
*mkmf_libpath,
|
|
84
|
+
*rustc_dynamic_linker_flags(dest_dir),
|
|
85
|
+
*rustc_lib_flags(dest_dir),
|
|
86
|
+
*platform_specific_rustc_args(dest_dir),
|
|
87
|
+
*debug_flags
|
|
88
|
+
]
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def platform_specific_rustc_args(dest_dir, flags = [])
|
|
92
|
+
if mingw_target?
|
|
93
|
+
# On mingw platforms, mkmf adds libruby to the linker flags
|
|
94
|
+
flags += libruby_args(dest_dir)
|
|
95
|
+
|
|
96
|
+
# Make sure ALSR is used on mingw
|
|
97
|
+
# see https://github.com/rust-lang/rust/pull/75406/files
|
|
98
|
+
flags += ["-C", "link-arg=-Wl,--dynamicbase"]
|
|
99
|
+
flags += ["-C", "link-arg=-Wl,--disable-auto-image-base"]
|
|
100
|
+
|
|
101
|
+
# If the gem is installed on a host with build tools installed, but is
|
|
102
|
+
# run on one that isn't the missing libraries will cause the extension
|
|
103
|
+
# to fail on start.
|
|
104
|
+
flags += ["-C", "link-arg=-static-libgcc"]
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
flags
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
# We want to use the same linker that Ruby uses, so that the linker flags from
|
|
111
|
+
# mkmf work properly.
|
|
112
|
+
def linker_args
|
|
113
|
+
# Have to handle CC="cl /nologo" on mswin
|
|
114
|
+
cc_flag = Shellwords.split(makefile_config("CC"))
|
|
115
|
+
linker = cc_flag.shift
|
|
116
|
+
link_args = cc_flag.flat_map { |a| ["-C", "link-arg=#{a}"] }
|
|
117
|
+
|
|
118
|
+
["-C", "linker=#{linker}", *link_args]
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
def libruby_args(dest_dir)
|
|
122
|
+
libs = makefile_config(ruby_static? ? "LIBRUBYARG_STATIC" : "LIBRUBYARG_SHARED")
|
|
123
|
+
raw_libs = Shellwords.split(libs)
|
|
124
|
+
raw_libs.flat_map { |l| ldflag_to_link_modifier(l) }
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
def ruby_static?
|
|
128
|
+
return true if %w[1 true].include?(ENV["RUBY_STATIC"])
|
|
129
|
+
|
|
130
|
+
makefile_config("ENABLE_SHARED") == "no"
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
# Ruby expects the dylib to follow a file name convention for loading
|
|
134
|
+
def rename_cdylib_for_ruby_compatibility(dest_path)
|
|
135
|
+
new_path = final_extension_path(dest_path)
|
|
136
|
+
FileUtils.cp(cargo_dylib_path(dest_path), new_path)
|
|
137
|
+
new_path
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
def validate_cargo_build!(dir)
|
|
141
|
+
dylib_path = cargo_dylib_path(dir)
|
|
142
|
+
|
|
143
|
+
raise DylibNotFoundError, dir unless File.exist?(dylib_path)
|
|
144
|
+
|
|
145
|
+
dylib_path
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
def final_extension_path(dest_path)
|
|
149
|
+
dylib_path = cargo_dylib_path(dest_path)
|
|
150
|
+
dlext_name = "#{spec.name}.#{makefile_config("DLEXT")}"
|
|
151
|
+
dylib_path.gsub(File.basename(dylib_path), dlext_name)
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
def cargo_crate_name
|
|
155
|
+
spec.metadata.fetch("cargo_crate_name", spec.name).tr("-", "_")
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
def rustc_dynamic_linker_flags(dest_dir)
|
|
159
|
+
split_flags("DLDFLAGS")
|
|
160
|
+
.map { |arg| maybe_resolve_ldflag_variable(arg, dest_dir) }
|
|
161
|
+
.compact
|
|
162
|
+
.flat_map { |arg| ldflag_to_link_modifier(arg) }
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
def rustc_lib_flags(dest_dir)
|
|
166
|
+
split_flags("LIBS").flat_map { |arg| ldflag_to_link_modifier(arg) }
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
def split_flags(var)
|
|
170
|
+
Shellwords.split(RbConfig::CONFIG.fetch(var, ""))
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
def ldflag_to_link_modifier(arg)
|
|
174
|
+
LinkFlagConverter.convert(arg)
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
def msvc_target?
|
|
178
|
+
makefile_config("target_os").include?("msvc")
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
def darwin_target?
|
|
182
|
+
makefile_config("target_os").include?("darwin")
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
def mingw_target?
|
|
186
|
+
makefile_config("target_os").include?("mingw")
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
def win_target?
|
|
190
|
+
target_platform = RbConfig::CONFIG["target_os"]
|
|
191
|
+
!!Gem::WIN_PATTERNS.find { |r| target_platform =~ r }
|
|
192
|
+
end
|
|
193
|
+
|
|
194
|
+
# Interpolate substition vars in the arg (i.e. $(DEFFILE))
|
|
195
|
+
def maybe_resolve_ldflag_variable(input_arg, dest_dir)
|
|
196
|
+
var_matches = input_arg.match(/\$\((\w+)\)/)
|
|
197
|
+
|
|
198
|
+
return input_arg unless var_matches
|
|
199
|
+
|
|
200
|
+
var_name = var_matches[1]
|
|
201
|
+
|
|
202
|
+
return input_arg if var_name.nil? || var_name.chomp.empty?
|
|
203
|
+
|
|
204
|
+
case var_name
|
|
205
|
+
# On windows, it is assumed that mkmf has setup an exports file for the
|
|
206
|
+
# extension, so we have to to create one ourselves.
|
|
207
|
+
when "DEFFILE"
|
|
208
|
+
write_deffile(dest_dir)
|
|
209
|
+
else
|
|
210
|
+
RbConfig::CONFIG[var_name]
|
|
211
|
+
end
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
def write_deffile(dest_path)
|
|
215
|
+
dest_dir = File.dirname(final_extension_path(dest_path))
|
|
216
|
+
FileUtils.mkdir_p(dest_dir)
|
|
217
|
+
deffile_path = File.join(dest_dir, "#{spec.name}-#{RbConfig::CONFIG["arch"]}.def")
|
|
218
|
+
export_prefix = makefile_config("EXPORT_PREFIX") || ""
|
|
219
|
+
|
|
220
|
+
File.open(deffile_path, "w") do |f|
|
|
221
|
+
f.puts "EXPORTS"
|
|
222
|
+
f.puts "#{export_prefix.strip}Init_#{spec.name}"
|
|
223
|
+
end
|
|
224
|
+
|
|
225
|
+
deffile_path
|
|
226
|
+
end
|
|
227
|
+
|
|
228
|
+
# We have to basically reimplement RbConfig::CONFIG['SOEXT'] here to support
|
|
229
|
+
# Ruby < 2.5
|
|
230
|
+
#
|
|
231
|
+
# @see https://github.com/ruby/ruby/blob/c87c027f18c005460746a74c07cd80ee355b16e4/configure.ac#L3185
|
|
232
|
+
def so_ext
|
|
233
|
+
return RbConfig::CONFIG["SOEXT"] if RbConfig::CONFIG.key?("SOEXT")
|
|
234
|
+
|
|
235
|
+
if win_target?
|
|
236
|
+
"dll"
|
|
237
|
+
elsif darwin_target?
|
|
238
|
+
"dylib"
|
|
239
|
+
else
|
|
240
|
+
"so"
|
|
241
|
+
end
|
|
242
|
+
end
|
|
243
|
+
|
|
244
|
+
# Corresponds to $(LIBPATH) in mkmf
|
|
245
|
+
def mkmf_libpath
|
|
246
|
+
["-L", "native=#{makefile_config("libdir")}"]
|
|
247
|
+
end
|
|
248
|
+
|
|
249
|
+
def makefile_config(var_name)
|
|
250
|
+
val = RbConfig::MAKEFILE_CONFIG[var_name]
|
|
251
|
+
|
|
252
|
+
return unless val
|
|
253
|
+
|
|
254
|
+
RbConfig.expand(val.dup)
|
|
255
|
+
end
|
|
256
|
+
|
|
257
|
+
# Good balance between binary size and debugability
|
|
258
|
+
def debug_flags
|
|
259
|
+
return [] if profile == :dev
|
|
260
|
+
|
|
261
|
+
["-C", "debuginfo=1"]
|
|
262
|
+
end
|
|
263
|
+
|
|
264
|
+
# Copied from ExtConfBuilder
|
|
265
|
+
def finalize_directory(dest_path, lib_dir, extension_dir)
|
|
266
|
+
require "fileutils"
|
|
267
|
+
require "tempfile"
|
|
268
|
+
|
|
269
|
+
ext_path = final_extension_path(dest_path)
|
|
270
|
+
|
|
271
|
+
begin
|
|
272
|
+
tmp_dest = Dir.mktmpdir(".gem.", extension_dir)
|
|
273
|
+
|
|
274
|
+
# Some versions of `mktmpdir` return absolute paths, which will break make
|
|
275
|
+
# if the paths contain spaces. However, on Ruby 1.9.x on Windows, relative
|
|
276
|
+
# paths cause all C extension builds to fail.
|
|
277
|
+
#
|
|
278
|
+
# As such, we convert to a relative path unless we are using Ruby 1.9.x on
|
|
279
|
+
# Windows. This means that when using Ruby 1.9.x on Windows, paths with
|
|
280
|
+
# spaces do not work.
|
|
281
|
+
#
|
|
282
|
+
# Details: https://github.com/rubygems/rubygems/issues/977#issuecomment-171544940
|
|
283
|
+
tmp_dest_relative = get_relative_path(tmp_dest.clone, extension_dir)
|
|
284
|
+
|
|
285
|
+
if tmp_dest_relative
|
|
286
|
+
full_tmp_dest = File.join(extension_dir, tmp_dest_relative)
|
|
287
|
+
|
|
288
|
+
# TODO: remove in RubyGems 3
|
|
289
|
+
if Gem.install_extension_in_lib && lib_dir
|
|
290
|
+
FileUtils.mkdir_p lib_dir
|
|
291
|
+
FileUtils.cp_r ext_path, lib_dir, remove_destination: true
|
|
292
|
+
end
|
|
293
|
+
|
|
294
|
+
FileUtils::Entry_.new(full_tmp_dest).traverse do |ent|
|
|
295
|
+
destent = ent.class.new(dest_path, ent.rel)
|
|
296
|
+
destent.exist? || FileUtils.mv(ent.path, destent.path)
|
|
297
|
+
end
|
|
298
|
+
end
|
|
299
|
+
ensure
|
|
300
|
+
FileUtils.rm_rf tmp_dest if tmp_dest
|
|
301
|
+
end
|
|
302
|
+
end
|
|
303
|
+
|
|
304
|
+
def get_relative_path(path, base)
|
|
305
|
+
path[0..base.length - 1] = "." if path.start_with?(base)
|
|
306
|
+
path
|
|
307
|
+
end
|
|
308
|
+
|
|
309
|
+
def profile_target_directory
|
|
310
|
+
case profile
|
|
311
|
+
when :release then "release"
|
|
312
|
+
when :dev then "debug"
|
|
313
|
+
else raise "unknown target directory for profile: #{profile}"
|
|
314
|
+
end
|
|
315
|
+
end
|
|
316
|
+
|
|
317
|
+
# Error raised when no cdylib artifact was created
|
|
318
|
+
class DylibNotFoundError < StandardError
|
|
319
|
+
def initialize(dir)
|
|
320
|
+
files = Dir.glob(File.join(dir, "**", "*")).map { |f| "- #{f}" }.join "\n"
|
|
321
|
+
|
|
322
|
+
super <<~MSG
|
|
323
|
+
Dynamic library not found for Rust extension (in #{dir})
|
|
324
|
+
|
|
325
|
+
Make sure you set "crate-type" in Cargo.toml to "cdylib"
|
|
326
|
+
|
|
327
|
+
Found files:
|
|
328
|
+
#{files}
|
|
329
|
+
MSG
|
|
330
|
+
end
|
|
331
|
+
end
|
|
332
|
+
end
|
|
333
|
+
end
|
data/lib/rb_sys/mkmf.rb
CHANGED
|
@@ -2,13 +2,33 @@
|
|
|
2
2
|
|
|
3
3
|
require "rubygems/ext"
|
|
4
4
|
require "shellwords"
|
|
5
|
-
require_relative "
|
|
5
|
+
require_relative "cargo_builder"
|
|
6
6
|
|
|
7
7
|
# Root module
|
|
8
8
|
module RbSys
|
|
9
|
-
#
|
|
9
|
+
# Helper class for creating Rust Makefiles
|
|
10
10
|
module Mkmf
|
|
11
|
-
|
|
11
|
+
# Helper for building Rust extensions by creating a Ruby compatible makefile
|
|
12
|
+
# for Rust. By using this class, your rust extension will be 100% compatible
|
|
13
|
+
# with the rake-compiler gem, which allows for easy cross compilation.
|
|
14
|
+
#
|
|
15
|
+
# @example Basic
|
|
16
|
+
# require 'mkmf'
|
|
17
|
+
# . require 'rb_sys/mkmf'
|
|
18
|
+
#
|
|
19
|
+
# . create_rust_makefile("my_extension") #=> Generate a Makefile in the current directory
|
|
20
|
+
#
|
|
21
|
+
# @example Configure a custom build profile
|
|
22
|
+
# require 'mkmf'
|
|
23
|
+
# . require 'rb_sys/mkmf'
|
|
24
|
+
#
|
|
25
|
+
# . create_rust_makefile("my_extension") do |r|
|
|
26
|
+
# . # All of these are optional
|
|
27
|
+
# . r.env = { 'FOO' => 'bar' }
|
|
28
|
+
# . r.profile = ENV.fetch('CARGO_BUILD_PROFILE', :dev).to_sym
|
|
29
|
+
# . r.features = %w[some_cargo_feature]
|
|
30
|
+
# . end
|
|
31
|
+
def create_rust_makefile(target, srcprefix = nil, &blk)
|
|
12
32
|
if target.include?("/")
|
|
13
33
|
target_prefix, target = File.split(target)
|
|
14
34
|
target_prefix[0, 0] = "/"
|
|
@@ -17,14 +37,21 @@ module RbSys
|
|
|
17
37
|
end
|
|
18
38
|
|
|
19
39
|
spec = Struct.new(:name, :metadata).new(target, {})
|
|
20
|
-
builder =
|
|
40
|
+
builder = CargoBuilder.new(spec)
|
|
41
|
+
|
|
42
|
+
yield builder if blk
|
|
43
|
+
|
|
21
44
|
srcprefix ||= "$(srcdir)/#{srcprefix}".chomp("/")
|
|
22
45
|
RbConfig.expand(srcdir = srcprefix.dup)
|
|
23
46
|
|
|
47
|
+
full_cargo_command = cargo_command(srcdir, builder)
|
|
48
|
+
gsub_cargo_command!(full_cargo_command, builder: builder)
|
|
49
|
+
|
|
24
50
|
# rubocop:disable Style/GlobalVars
|
|
25
51
|
make_install = <<~MAKE
|
|
52
|
+
RB_SYS_CARGO_PROFILE ?= #{builder.profile}
|
|
53
|
+
RB_SYS_CARGO_FEATURES ?= #{builder.features.join(",")}
|
|
26
54
|
target_prefix = #{target_prefix}
|
|
27
|
-
CARGO_PROFILE = release
|
|
28
55
|
CLEANLIBS = $(RUSTLIB) $(DLLIB)
|
|
29
56
|
DISTCLEANDIRS = target/
|
|
30
57
|
RUBYARCHDIR = $(sitearchdir)$(target_prefix)
|
|
@@ -39,10 +66,10 @@ module RbSys
|
|
|
39
66
|
FORCE: ;
|
|
40
67
|
|
|
41
68
|
$(DLLIB): FORCE
|
|
42
|
-
\t#{
|
|
69
|
+
\t#{full_cargo_command}
|
|
43
70
|
\t$(COPY) "$(RUSTLIB)" $@
|
|
44
71
|
|
|
45
|
-
install: $(DLLIB)
|
|
72
|
+
install: $(DLLIB) Makefile
|
|
46
73
|
\t$(INSTALL_PROG) $(DLLIB) $(RUBYARCHDIR)
|
|
47
74
|
|
|
48
75
|
all: #{$extout ? "install" : "$(DLLIB)"}
|
|
@@ -70,12 +97,30 @@ module RbSys
|
|
|
70
97
|
end
|
|
71
98
|
|
|
72
99
|
def env_vars(builder)
|
|
73
|
-
builder.build_env.map { |k, v|
|
|
100
|
+
lines = builder.build_env.map { |k, v| env_line(k, v) }
|
|
101
|
+
lines << env_line("CC", env_or_makefile_config("CC"))
|
|
102
|
+
lines << env_line("AR", env_or_makefile_config("AR")) unless env_or_makefile_config("AR") == "libtool -static"
|
|
103
|
+
lines.compact.join("\n")
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def env_line(k, v)
|
|
107
|
+
return unless v
|
|
108
|
+
%($(DLLIB): export #{k} = #{v.gsub("\n", '\n')})
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def env_or_makefile_config(key)
|
|
112
|
+
ENV[key] || RbConfig::MAKEFILE_CONFIG[key]
|
|
74
113
|
end
|
|
75
114
|
|
|
76
115
|
def dllib_path(builder)
|
|
77
116
|
builder.cargo_dylib_path(File.join(Dir.pwd, "target"))
|
|
78
117
|
end
|
|
118
|
+
|
|
119
|
+
def gsub_cargo_command!(cargo_command, builder:)
|
|
120
|
+
cargo_command.gsub!("--profile #{builder.profile}", "--profile $(RB_SYS_CARGO_PROFILE)")
|
|
121
|
+
cargo_command.gsub!(%r{--features \S+}, "--features $(RB_SYS_CARGO_FEATURES)")
|
|
122
|
+
cargo_command
|
|
123
|
+
end
|
|
79
124
|
end
|
|
80
125
|
end
|
|
81
126
|
|
data/lib/rb_sys/version.rb
CHANGED
data.tar.gz.sig
CHANGED
|
Binary file
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: rb_sys
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1
|
|
4
|
+
version: 0.9.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Ian Ker-Seymer
|
|
@@ -30,7 +30,7 @@ cert_chain:
|
|
|
30
30
|
Rl+ASkq2/1i07TkBpCf+2hq66+h/hx+/Y/KrUzXfe0jtvil0WESkJT2kqRqHWNhD
|
|
31
31
|
9GKBxaQlXokNDtWCm1/gl6cD8WRZ0N5S4ZGJT1FLLsA=
|
|
32
32
|
-----END CERTIFICATE-----
|
|
33
|
-
date: 2022-
|
|
33
|
+
date: 2022-06-01 00:00:00.000000000 Z
|
|
34
34
|
dependencies: []
|
|
35
35
|
description:
|
|
36
36
|
email:
|
|
@@ -43,11 +43,11 @@ files:
|
|
|
43
43
|
- LICENSE-MIT
|
|
44
44
|
- certs/ianks.pem
|
|
45
45
|
- lib/rb_sys.rb
|
|
46
|
+
- lib/rb_sys/cargo_builder.rb
|
|
47
|
+
- lib/rb_sys/cargo_builder/link_flag_converter.rb
|
|
46
48
|
- lib/rb_sys/mkmf.rb
|
|
47
49
|
- lib/rb_sys/version.rb
|
|
48
50
|
- sig/rb_sys.rbs
|
|
49
|
-
- vendor/rubygems/ext/cargo_builder.rb
|
|
50
|
-
- vendor/rubygems/ext/cargo_builder/link_flag_converter.rb
|
|
51
51
|
homepage: https://github.com/oxidize-rb/rb-sys
|
|
52
52
|
licenses:
|
|
53
53
|
- MIT
|
metadata.gz.sig
CHANGED
|
Binary file
|
data/LICENSE-APACHE
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
/Users/ianks/Code/open-source/rb-sys/LICENSE-APACHE
|
data/LICENSE-MIT
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
/Users/ianks/Code/open-source/rb-sys/LICENSE-MIT
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
class Gem::Ext::CargoBuilder < Gem::Ext::Builder
|
|
4
|
-
# Converts Ruby link flags into something cargo understands
|
|
5
|
-
class LinkFlagConverter
|
|
6
|
-
def self.convert(arg)
|
|
7
|
-
case arg.chomp
|
|
8
|
-
when /^-L\s*(.+)$/
|
|
9
|
-
["-L", "native=#{$1}"]
|
|
10
|
-
when /^--library=(\w+\S+)$/, /^-l\s*(\w+\S+)$/
|
|
11
|
-
["-l", $1]
|
|
12
|
-
when /^-l\s*:lib(\S+).a$/
|
|
13
|
-
["-l", "static=#{$1}"]
|
|
14
|
-
when /^-l\s*:lib(\S+).(so|dylib|dll)$/
|
|
15
|
-
["-l", "dylib=#{$1}"]
|
|
16
|
-
when /^-F\s*(.*)$/
|
|
17
|
-
["-l", "framework=#{$1}"]
|
|
18
|
-
else
|
|
19
|
-
["-C", "link_arg=#{arg}"]
|
|
20
|
-
end
|
|
21
|
-
end
|
|
22
|
-
end
|
|
23
|
-
end
|
|
@@ -1,330 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
# This class is used by rubygems to build Rust extensions. It is a thin-wrapper
|
|
4
|
-
# over the `cargo rustc` command which takes care of building Rust code in a way
|
|
5
|
-
# that Ruby can use.
|
|
6
|
-
class Gem::Ext::CargoBuilder < Gem::Ext::Builder
|
|
7
|
-
attr_accessor :spec, :runner, :profile
|
|
8
|
-
|
|
9
|
-
def initialize(spec)
|
|
10
|
-
require "rubygems/command"
|
|
11
|
-
require_relative "cargo_builder/link_flag_converter"
|
|
12
|
-
|
|
13
|
-
@spec = spec
|
|
14
|
-
@runner = self.class.method(:run)
|
|
15
|
-
@profile = :release
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
def build(_extension, dest_path, results, args = [], lib_dir = nil, cargo_dir = Dir.pwd)
|
|
19
|
-
require "fileutils"
|
|
20
|
-
require "shellwords"
|
|
21
|
-
|
|
22
|
-
build_crate(dest_path, results, args, cargo_dir)
|
|
23
|
-
validate_cargo_build!(dest_path)
|
|
24
|
-
rename_cdylib_for_ruby_compatibility(dest_path)
|
|
25
|
-
finalize_directory(dest_path, lib_dir, cargo_dir)
|
|
26
|
-
results
|
|
27
|
-
end
|
|
28
|
-
|
|
29
|
-
def build_crate(dest_path, results, args, cargo_dir)
|
|
30
|
-
env = build_env
|
|
31
|
-
cmd = cargo_command(cargo_dir, dest_path, args)
|
|
32
|
-
runner.call cmd, results, "cargo", cargo_dir, env
|
|
33
|
-
|
|
34
|
-
results
|
|
35
|
-
end
|
|
36
|
-
|
|
37
|
-
def build_env
|
|
38
|
-
build_env = rb_config_env
|
|
39
|
-
build_env["RUBY_STATIC"] = "true" if ruby_static? && ENV.key?("RUBY_STATIC")
|
|
40
|
-
build_env
|
|
41
|
-
end
|
|
42
|
-
|
|
43
|
-
def cargo_command(cargo_dir, dest_path, args = [])
|
|
44
|
-
manifest = File.join(cargo_dir, "Cargo.toml")
|
|
45
|
-
cargo = ENV.fetch("CARGO", "cargo")
|
|
46
|
-
|
|
47
|
-
cmd = []
|
|
48
|
-
cmd += [cargo, "rustc"]
|
|
49
|
-
cmd += ["--target", ENV["CARGO_BUILD_TARGET"]] if ENV["CARGO_BUILD_TARGET"]
|
|
50
|
-
cmd += ["--target-dir", dest_path]
|
|
51
|
-
cmd += ["--manifest-path", manifest]
|
|
52
|
-
cmd += ["--lib"]
|
|
53
|
-
cmd += ["--profile", profile.to_s]
|
|
54
|
-
cmd += ["--locked"] if profile == :release
|
|
55
|
-
cmd += Gem::Command.build_args
|
|
56
|
-
cmd += args
|
|
57
|
-
cmd += ["--"]
|
|
58
|
-
cmd += [*cargo_rustc_args(dest_path)]
|
|
59
|
-
cmd
|
|
60
|
-
end
|
|
61
|
-
|
|
62
|
-
def cargo_dylib_path(dest_path)
|
|
63
|
-
prefix = so_ext == "dll" ? "" : "lib"
|
|
64
|
-
path_parts = [dest_path]
|
|
65
|
-
path_parts << ENV["CARGO_BUILD_TARGET"] if ENV["CARGO_BUILD_TARGET"]
|
|
66
|
-
path_parts += [profile_target_directory, "#{prefix}#{cargo_crate_name}.#{so_ext}"]
|
|
67
|
-
File.join(*path_parts)
|
|
68
|
-
end
|
|
69
|
-
|
|
70
|
-
private
|
|
71
|
-
|
|
72
|
-
def rb_config_env
|
|
73
|
-
result = {}
|
|
74
|
-
RbConfig::CONFIG.each { |k, v| result["RBCONFIG_#{k}"] = v }
|
|
75
|
-
result
|
|
76
|
-
end
|
|
77
|
-
|
|
78
|
-
def cargo_rustc_args(dest_dir)
|
|
79
|
-
[
|
|
80
|
-
*linker_args,
|
|
81
|
-
*mkmf_libpath,
|
|
82
|
-
*rustc_dynamic_linker_flags(dest_dir),
|
|
83
|
-
*rustc_lib_flags(dest_dir),
|
|
84
|
-
*platform_specific_rustc_args(dest_dir),
|
|
85
|
-
*debug_flags
|
|
86
|
-
]
|
|
87
|
-
end
|
|
88
|
-
|
|
89
|
-
def platform_specific_rustc_args(dest_dir, flags = [])
|
|
90
|
-
if mingw_target?
|
|
91
|
-
# On mingw platforms, mkmf adds libruby to the linker flags
|
|
92
|
-
flags += libruby_args(dest_dir)
|
|
93
|
-
|
|
94
|
-
# Make sure ALSR is used on mingw
|
|
95
|
-
# see https://github.com/rust-lang/rust/pull/75406/files
|
|
96
|
-
flags += ["-C", "link-arg=-Wl,--dynamicbase"]
|
|
97
|
-
flags += ["-C", "link-arg=-Wl,--disable-auto-image-base"]
|
|
98
|
-
|
|
99
|
-
# If the gem is installed on a host with build tools installed, but is
|
|
100
|
-
# run on one that isn't the missing libraries will cause the extension
|
|
101
|
-
# to fail on start.
|
|
102
|
-
flags += ["-C", "link-arg=-static-libgcc"]
|
|
103
|
-
end
|
|
104
|
-
|
|
105
|
-
flags
|
|
106
|
-
end
|
|
107
|
-
|
|
108
|
-
# We want to use the same linker that Ruby uses, so that the linker flags from
|
|
109
|
-
# mkmf work properly.
|
|
110
|
-
def linker_args
|
|
111
|
-
# Have to handle CC="cl /nologo" on mswin
|
|
112
|
-
cc_flag = Shellwords.split(makefile_config("CC"))
|
|
113
|
-
linker = cc_flag.shift
|
|
114
|
-
link_args = cc_flag.flat_map { |a| ["-C", "link-arg=#{a}"] }
|
|
115
|
-
|
|
116
|
-
["-C", "linker=#{linker}", *link_args]
|
|
117
|
-
end
|
|
118
|
-
|
|
119
|
-
def libruby_args(dest_dir)
|
|
120
|
-
libs = makefile_config(ruby_static? ? "LIBRUBYARG_STATIC" : "LIBRUBYARG_SHARED")
|
|
121
|
-
raw_libs = Shellwords.split(libs)
|
|
122
|
-
raw_libs.flat_map { |l| ldflag_to_link_modifier(l) }
|
|
123
|
-
end
|
|
124
|
-
|
|
125
|
-
def ruby_static?
|
|
126
|
-
return true if %w[1 true].include?(ENV["RUBY_STATIC"])
|
|
127
|
-
|
|
128
|
-
makefile_config("ENABLE_SHARED") == "no"
|
|
129
|
-
end
|
|
130
|
-
|
|
131
|
-
# Ruby expects the dylib to follow a file name convention for loading
|
|
132
|
-
def rename_cdylib_for_ruby_compatibility(dest_path)
|
|
133
|
-
new_path = final_extension_path(dest_path)
|
|
134
|
-
FileUtils.cp(cargo_dylib_path(dest_path), new_path)
|
|
135
|
-
new_path
|
|
136
|
-
end
|
|
137
|
-
|
|
138
|
-
def validate_cargo_build!(dir)
|
|
139
|
-
dylib_path = cargo_dylib_path(dir)
|
|
140
|
-
|
|
141
|
-
raise DylibNotFoundError, dir unless File.exist?(dylib_path)
|
|
142
|
-
|
|
143
|
-
dylib_path
|
|
144
|
-
end
|
|
145
|
-
|
|
146
|
-
def final_extension_path(dest_path)
|
|
147
|
-
dylib_path = cargo_dylib_path(dest_path)
|
|
148
|
-
dlext_name = "#{spec.name}.#{makefile_config("DLEXT")}"
|
|
149
|
-
dylib_path.gsub(File.basename(dylib_path), dlext_name)
|
|
150
|
-
end
|
|
151
|
-
|
|
152
|
-
def cargo_crate_name
|
|
153
|
-
spec.metadata.fetch("cargo_crate_name", spec.name).tr("-", "_")
|
|
154
|
-
end
|
|
155
|
-
|
|
156
|
-
def rustc_dynamic_linker_flags(dest_dir)
|
|
157
|
-
split_flags("DLDFLAGS")
|
|
158
|
-
.map { |arg| maybe_resolve_ldflag_variable(arg, dest_dir) }
|
|
159
|
-
.compact
|
|
160
|
-
.flat_map { |arg| ldflag_to_link_modifier(arg) }
|
|
161
|
-
end
|
|
162
|
-
|
|
163
|
-
def rustc_lib_flags(dest_dir)
|
|
164
|
-
split_flags("LIBS").flat_map { |arg| ldflag_to_link_modifier(arg) }
|
|
165
|
-
end
|
|
166
|
-
|
|
167
|
-
def split_flags(var)
|
|
168
|
-
Shellwords.split(RbConfig::CONFIG.fetch(var, ""))
|
|
169
|
-
end
|
|
170
|
-
|
|
171
|
-
def ldflag_to_link_modifier(arg)
|
|
172
|
-
LinkFlagConverter.convert(arg)
|
|
173
|
-
end
|
|
174
|
-
|
|
175
|
-
def msvc_target?
|
|
176
|
-
makefile_config("target_os").include?("msvc")
|
|
177
|
-
end
|
|
178
|
-
|
|
179
|
-
def darwin_target?
|
|
180
|
-
makefile_config("target_os").include?("darwin")
|
|
181
|
-
end
|
|
182
|
-
|
|
183
|
-
def mingw_target?
|
|
184
|
-
makefile_config("target_os").include?("mingw")
|
|
185
|
-
end
|
|
186
|
-
|
|
187
|
-
def win_target?
|
|
188
|
-
target_platform = RbConfig::CONFIG["target_os"]
|
|
189
|
-
!!Gem::WIN_PATTERNS.find { |r| target_platform =~ r }
|
|
190
|
-
end
|
|
191
|
-
|
|
192
|
-
# Interpolate substition vars in the arg (i.e. $(DEFFILE))
|
|
193
|
-
def maybe_resolve_ldflag_variable(input_arg, dest_dir)
|
|
194
|
-
var_matches = input_arg.match(/\$\((\w+)\)/)
|
|
195
|
-
|
|
196
|
-
return input_arg unless var_matches
|
|
197
|
-
|
|
198
|
-
var_name = var_matches[1]
|
|
199
|
-
|
|
200
|
-
return input_arg if var_name.nil? || var_name.chomp.empty?
|
|
201
|
-
|
|
202
|
-
case var_name
|
|
203
|
-
# On windows, it is assumed that mkmf has setup an exports file for the
|
|
204
|
-
# extension, so we have to to create one ourselves.
|
|
205
|
-
when "DEFFILE"
|
|
206
|
-
write_deffile(dest_dir)
|
|
207
|
-
else
|
|
208
|
-
RbConfig::CONFIG[var_name]
|
|
209
|
-
end
|
|
210
|
-
end
|
|
211
|
-
|
|
212
|
-
def write_deffile(dest_path)
|
|
213
|
-
dest_dir = File.dirname(final_extension_path(dest_path))
|
|
214
|
-
FileUtils.mkdir_p(dest_dir)
|
|
215
|
-
deffile_path = File.join(dest_dir, "#{spec.name}-#{RbConfig::CONFIG["arch"]}.def")
|
|
216
|
-
export_prefix = makefile_config("EXPORT_PREFIX") || ""
|
|
217
|
-
|
|
218
|
-
File.open(deffile_path, "w") do |f|
|
|
219
|
-
f.puts "EXPORTS"
|
|
220
|
-
f.puts "#{export_prefix.strip}Init_#{spec.name}"
|
|
221
|
-
end
|
|
222
|
-
|
|
223
|
-
deffile_path
|
|
224
|
-
end
|
|
225
|
-
|
|
226
|
-
# We have to basically reimplement RbConfig::CONFIG['SOEXT'] here to support
|
|
227
|
-
# Ruby < 2.5
|
|
228
|
-
#
|
|
229
|
-
# @see https://github.com/ruby/ruby/blob/c87c027f18c005460746a74c07cd80ee355b16e4/configure.ac#L3185
|
|
230
|
-
def so_ext
|
|
231
|
-
return RbConfig::CONFIG["SOEXT"] if RbConfig::CONFIG.key?("SOEXT")
|
|
232
|
-
|
|
233
|
-
if win_target?
|
|
234
|
-
"dll"
|
|
235
|
-
elsif darwin_target?
|
|
236
|
-
"dylib"
|
|
237
|
-
else
|
|
238
|
-
"so"
|
|
239
|
-
end
|
|
240
|
-
end
|
|
241
|
-
|
|
242
|
-
# Corresponds to $(LIBPATH) in mkmf
|
|
243
|
-
def mkmf_libpath
|
|
244
|
-
["-L", "native=#{makefile_config("libdir")}"]
|
|
245
|
-
end
|
|
246
|
-
|
|
247
|
-
def makefile_config(var_name)
|
|
248
|
-
val = RbConfig::MAKEFILE_CONFIG[var_name]
|
|
249
|
-
|
|
250
|
-
return unless val
|
|
251
|
-
|
|
252
|
-
RbConfig.expand(val.dup)
|
|
253
|
-
end
|
|
254
|
-
|
|
255
|
-
# Good balance between binary size and debugability
|
|
256
|
-
def debug_flags
|
|
257
|
-
return [] if profile == :dev
|
|
258
|
-
|
|
259
|
-
["-C", "debuginfo=1"]
|
|
260
|
-
end
|
|
261
|
-
|
|
262
|
-
# Copied from ExtConfBuilder
|
|
263
|
-
def finalize_directory(dest_path, lib_dir, extension_dir)
|
|
264
|
-
require "fileutils"
|
|
265
|
-
require "tempfile"
|
|
266
|
-
|
|
267
|
-
ext_path = final_extension_path(dest_path)
|
|
268
|
-
|
|
269
|
-
begin
|
|
270
|
-
tmp_dest = Dir.mktmpdir(".gem.", extension_dir)
|
|
271
|
-
|
|
272
|
-
# Some versions of `mktmpdir` return absolute paths, which will break make
|
|
273
|
-
# if the paths contain spaces. However, on Ruby 1.9.x on Windows, relative
|
|
274
|
-
# paths cause all C extension builds to fail.
|
|
275
|
-
#
|
|
276
|
-
# As such, we convert to a relative path unless we are using Ruby 1.9.x on
|
|
277
|
-
# Windows. This means that when using Ruby 1.9.x on Windows, paths with
|
|
278
|
-
# spaces do not work.
|
|
279
|
-
#
|
|
280
|
-
# Details: https://github.com/rubygems/rubygems/issues/977#issuecomment-171544940
|
|
281
|
-
tmp_dest_relative = get_relative_path(tmp_dest.clone, extension_dir)
|
|
282
|
-
|
|
283
|
-
if tmp_dest_relative
|
|
284
|
-
full_tmp_dest = File.join(extension_dir, tmp_dest_relative)
|
|
285
|
-
|
|
286
|
-
# TODO: remove in RubyGems 3
|
|
287
|
-
if Gem.install_extension_in_lib && lib_dir
|
|
288
|
-
FileUtils.mkdir_p lib_dir
|
|
289
|
-
FileUtils.cp_r ext_path, lib_dir, remove_destination: true
|
|
290
|
-
end
|
|
291
|
-
|
|
292
|
-
FileUtils::Entry_.new(full_tmp_dest).traverse do |ent|
|
|
293
|
-
destent = ent.class.new(dest_path, ent.rel)
|
|
294
|
-
destent.exist? || FileUtils.mv(ent.path, destent.path)
|
|
295
|
-
end
|
|
296
|
-
end
|
|
297
|
-
ensure
|
|
298
|
-
FileUtils.rm_rf tmp_dest if tmp_dest
|
|
299
|
-
end
|
|
300
|
-
end
|
|
301
|
-
|
|
302
|
-
def get_relative_path(path, base)
|
|
303
|
-
path[0..base.length - 1] = "." if path.start_with?(base)
|
|
304
|
-
path
|
|
305
|
-
end
|
|
306
|
-
|
|
307
|
-
def profile_target_directory
|
|
308
|
-
case profile
|
|
309
|
-
when :release then "release"
|
|
310
|
-
when :dev then "debug"
|
|
311
|
-
else raise "unknown target directory for profile: #{profile}"
|
|
312
|
-
end
|
|
313
|
-
end
|
|
314
|
-
|
|
315
|
-
# Error raised when no cdylib artifact was created
|
|
316
|
-
class DylibNotFoundError < StandardError
|
|
317
|
-
def initialize(dir)
|
|
318
|
-
files = Dir.glob(File.join(dir, "**", "*")).map { |f| "- #{f}" }.join "\n"
|
|
319
|
-
|
|
320
|
-
super <<~MSG
|
|
321
|
-
Dynamic library not found for Rust extension (in #{dir})
|
|
322
|
-
|
|
323
|
-
Make sure you set "crate-type" in Cargo.toml to "cdylib"
|
|
324
|
-
|
|
325
|
-
Found files:
|
|
326
|
-
#{files}
|
|
327
|
-
MSG
|
|
328
|
-
end
|
|
329
|
-
end
|
|
330
|
-
end
|