Repository navigation
Expand file tree
/
Copy pathRakefile
More file actions
263 lines (221 loc) · 8.78 KB
/
Copy pathRakefile
File metadata and controls
263 lines (221 loc) · 8.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# frozen_string_literal: true
require "bundler/gem_tasks"
require "rake/clean"
begin
require "rake_compiler_dock"
rescue LoadError
# rake_compiler_dock is not available in versioned Gemfiles (gemfiles/2.7/, etc.)
# that don't include the gem. The native gem tasks will not be defined.
end
PRISM_SPEC = Bundler.load_gemspec("prism.gemspec")
task default: %i[compile test]
require_relative "templates/template"
# A stamp is a file that records some contents, such as the values of the
# environment variables that a build reads, and is rewritten only when they
# differ from what it holds. Rake only compares file timestamps, so a file that
# depends on a stamp is out of date exactly when the contents change. The
# contents are computed when the stamp is checked rather than when this file is
# loaded, so that they reflect the files generated earlier in the same run.
class StampTask < Rake::FileTask
def self.define(name, &contents)
define_task(name) do |task|
FileUtils.mkdir_p(File.dirname(task.name))
File.write(task.name, task.contents)
end.tap { |task| task.contents_block = contents }
end
attr_writer :contents_block
def contents
@contents_block.call
end
def needed?
super || File.read(name) != contents
end
end
desc "Generate all ERB template based files"
task templates: Prism::Template::TEMPLATES
# The make that builds libprism runs one job per processor unless MAKEFLAGS is
# already set. The variable is set in this process's environment, so the make
# that rake-compiler runs for the C extension uses the same number of jobs.
make = RUBY_PLATFORM.match?(/openbsd|freebsd/) ? "gmake" : "make"
task(make: :templates) {
require "etc"
ENV["MAKEFLAGS"] ||= "-j#{Etc.nprocessors}"
sh(make)
}
task compile: :make
# decorate the gem build task with prerequisites
task build: [:check_manifest, :templates]
# the C extension
task "compile:prism" => ["templates"] # must be before the ExtensionTask is created
RCD_CROSS_PLATFORMS = %w[
aarch64-linux-gnu
aarch64-linux-musl
aarch64-mingw-ucrt
arm-linux-gnu
arm-linux-musl
arm64-darwin
x64-mingw-ucrt
x86_64-darwin
x86_64-linux-gnu
x86_64-linux-musl
]
if defined?(RakeCompilerDock)
RakeCompilerDock.set_ruby_cc_version(">= 3.3")
namespace "gem" do
RCD_CROSS_PLATFORMS.each do |platform|
desc "build native gem for #{platform}"
task platform do
RakeCompilerDock.sh(<<~EOF, platform: platform, verbose: true)
gem install bundler --no-document &&
bundle &&
bundle exec rake gem:#{platform}:build
EOF
end
namespace platform do
# this runs in the rake-compiler-dock docker container
task "build" => ["templates"] do
Rake::Task["native:#{platform}"].invoke
Rake::Task["pkg/#{PRISM_SPEC.full_name}-#{Gem::Platform.new(platform)}.gem"].invoke
end
end
end
desc "build native gem for all platforms"
task "all" => [RCD_CROSS_PLATFORMS, "build"].flatten
end
end
if RUBY_ENGINE == "ruby" and !ENV["PRISM_FFI_BACKEND"]
require "rake/extensiontask"
require "shellwords"
build_variants = %w[debug minimal ndebug asan ubsan]
extension = Rake::ExtensionTask.new("prism", PRISM_SPEC) do |ext|
ext.ext_dir = "ext/prism"
ext.lib_dir = "lib/prism"
ext.cross_compile = true
ext.cross_platform = RCD_CROSS_PLATFORMS
# The build options of extconf.rb are inputs to the build that
# rake-compiler does not track: it configures the extension again only
# when extconf.rb itself changes. So each combination of options builds in
# its own directory, and switching between them never reuses objects that
# were compiled with different flags. An option is on when its environment
# variable is set or its flag is passed after `--`, as extconf.rb reads it.
#
# Match mkmf's enable_config: an enable option takes precedence over a
# disable option, and both take precedence over the environment default.
# Option values do not affect enable_config's presence checks.
options = [
*Shellwords.split(RbConfig::CONFIG["configure_args"] || ""),
*Shellwords.split(ENV["CONFIGURE_ARGS"] || ""),
*ext.config_options,
*ext.extra_options
].filter_map do |option|
argument, value = option.split("=", 2)
argument = argument.tr("_", "-")
unless argument.start_with?("--")
next unless value
argument = "--#{argument.downcase}"
end
argument
end
variant = build_variants.filter_map do |name|
name if (
options.include?("--enable-build-#{name}") ||
(!options.include?("--disable-build-#{name}") && ENV.key?("PRISM_BUILD_#{name.upcase}"))
)
end
unless variant.empty?
ext.tmp_dir = "tmp/#{variant.join("-")}"
task(:compile) { warn "Built the #{variant.join(" and ")} variant of the extension in #{ext.tmp_dir}" }
end
end
# The Makefile that extconf.rb writes lists the sources and headers that
# existed when it ran, and rake-compiler runs extconf.rb again only when
# extconf.rb itself changes. So each Makefile depends on a stamp of that list,
# and adding or removing a source or header configures the extension again,
# which recompiles every object through the rule that extconf.rb adds.
extension_files = "tmp/extension.files"
StampTask.define(extension_files) do
Dir["ext/prism/*.{c,h}", "src/**/*.c", "include/**/*.h"].sort.map { |path| "#{path}\n" }.join
end
# rake-compiler defines a Makefile task for every configured platform and
# Ruby version, including cross-compilation targets.
Rake::Task.tasks.each do |task|
next unless File.fnmatch?("#{extension.tmp_dir}/*/#{extension.name}/*/Makefile", task.name, File::FNM_PATHNAME)
extension_directory = File.dirname(task.name)
extension_configuration = "#{extension_directory}/configuration"
platform = extension_directory.delete_prefix("#{extension.tmp_dir}/").split("/").first
cross_options = if task.prerequisites.include?("#{extension_directory}/fake.rb")
extension.cross_config_options(platform)
else
[]
end
# Variants keep their objects separate, but other extconf options can change
# within a variant. Record the Ruby configuration, arguments and environment
# that extconf reads so changing compiler or linker flags configures it again.
StampTask.define(extension_configuration) do
{
ruby: RbConfig.ruby,
config: RbConfig::CONFIG.sort,
options: extension.config_options + cross_options + extension.extra_options,
environment: ["CONFIGURE_ARGS", *build_variants.map { |name| "PRISM_BUILD_#{name.upcase}" }].map { |name| [name, ENV[name]] }
}.inspect + "\n"
end
task.enhance([extension_files, extension_configuration])
end
elsif RUBY_ENGINE == "jruby"
require "rake/javaextensiontask"
# This compiles java to make sure any templating changes produces valid code.
Rake::JavaExtensionTask.new(:compile) do |ext|
ext.name = "prism"
ext.ext_dir = "java/api"
ext.lib_dir = "tmp"
ext.release = "21"
ext.gem_spec = PRISM_SPEC
end
end
# So `rake clobber` will delete generated files
CLOBBER.concat(Prism::Template::TEMPLATES)
CLOBBER.concat(["build"])
CLOBBER << "lib/prism/prism.#{RbConfig::CONFIG["DLEXT"]}"
CLOBBER << "java/wasm/src/main/resources/prism.wasm"
# The generated files depend on the environment variables listed in
# Prism::Template::ENVIRONMENT as well as on their templates, so they depend on
# a stamp of the variables' values.
template_environment = "tmp/template.env"
StampTask.define(template_environment) do
Prism::Template::ENVIRONMENT.map { |name, value| "#{name}=#{value.inspect}\n" }.join
end
Prism::Template::TEMPLATES.each do |filepath|
desc "Generate #{filepath}"
file filepath => ["templates/#{filepath}.erb", "templates/template.rb", "config.yml", template_environment] do |t|
Prism::Template.render(t.name)
end
end
namespace :build do
task :dev_version_set do
filepath = File.expand_path("prism.gemspec", __dir__)
File.write(filepath, File.read(filepath).sub(/spec\.version = ".+?"/, %Q{spec.version = "9999.9.9"}))
end
task :dev_version_clear do
sh "git checkout -- prism.gemspec Gemfile.lock"
end
desc "Build a development version of the gem"
task dev: ["build:dev_version_set", "build", "build:dev_version_clear"]
end
task :build_in_docker => :templates do
# Versions from https://github.com/ruby/ruby/blob/master/.github/workflows/compilers.yml
versions = (7..15).to_a
versions.each do |version|
dockerfile = <<~DOCKERFILE
FROM docker.io/library/gcc:#{version}
COPY Makefile /prism/
COPY src /prism/src
COPY include /prism/include
WORKDIR /prism
RUN gcc --version
RUN make SOEXT=so
DOCKERFILE
File.write 'Dockerfile', dockerfile
sh "docker build ."
rm 'Dockerfile'
end
end