From 1bb57d6ff9ac16b5e569f938df1df2a81d6d3cc2 Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Mon, 9 Nov 2026 22:11:35 -0500 Subject: [PATCH 01/16] Regenerate templates when env vars impacting them change --- .github/workflows/main.yml | 4 ++-- Rakefile | 24 +++++++++++++++++++++++- templates/template.rb | 17 +++++++++++++---- 3 files changed, 38 insertions(+), 7 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 08498f38a3..cba359b75d 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -58,7 +58,7 @@ jobs: - name: Validate RBS run: bundle exec rake typecheck:rbs - name: Check field kinds - run: rm lib/prism/node.rb && CHECK_FIELD_KIND=true bundle exec rake + run: CHECK_FIELD_KIND=true bundle exec rake build: strategy: @@ -286,7 +286,7 @@ jobs: - name: Serialized size stats with all fields run: bundle exec rake serialized_size:topgems - name: Recompile with only semantic fields - run: PRISM_SERIALIZE_ONLY_SEMANTICS_FIELDS=1 bundle exec rake clobber compile + run: PRISM_SERIALIZE_ONLY_SEMANTICS_FIELDS=1 bundle exec rake compile - name: Serialized size stats with only semantic fields run: bundle exec rake serialized_size:topgems diff --git a/Rakefile b/Rakefile index fd37486dbd..b1e7b414e0 100644 --- a/Rakefile +++ b/Rakefile @@ -106,9 +106,31 @@ 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. Rake only compares +# file timestamps, so this stamp records the variables' values and is rewritten +# whenever they differ from what it holds. Its timestamp then moves only when a +# value changes, which makes the generated files out of date exactly then. +class TemplateEnvironmentTask < Rake::FileTask + def contents + Prism::Template::ENVIRONMENT.map { |name, value| "#{name}=#{value.inspect}\n" }.join + end + + def needed? + super || File.read(name) != contents + end +end + +template_environment = "tmp/template.env" + +TemplateEnvironmentTask.define_task(template_environment) do |t| + mkdir_p(File.dirname(t.name)) + File.write(t.name, t.contents) +end + Prism::Template::TEMPLATES.each do |filepath| desc "Generate #{filepath}" - file filepath => ["templates/#{filepath}.erb", "templates/template.rb", "config.yml"] do |t| + file filepath => ["templates/#{filepath}.erb", "templates/template.rb", "config.yml", template_environment] do |t| Prism::Template.render(t.name) end end diff --git a/templates/template.rb b/templates/template.rb index c43ee6430e..2d68b733b5 100755 --- a/templates/template.rb +++ b/templates/template.rb @@ -7,10 +7,19 @@ module Prism module Template # :nodoc: all - SERIALIZE_ONLY_SEMANTICS_FIELDS = ENV.fetch("PRISM_SERIALIZE_ONLY_SEMANTICS_FIELDS", false) - CHECK_FIELD_KIND = ENV.fetch("CHECK_FIELD_KIND", false) - - JAVA_BACKEND = ENV["PRISM_JAVA_BACKEND"] || "default" + # The environment variables that change the generated files. The Rakefile + # records their values in a stamp file that every generated file depends + # on, so any variable read by a template must be listed here. + ENVIRONMENT = %w[ + CHECK_FIELD_KIND + PRISM_JAVA_BACKEND + PRISM_SERIALIZE_ONLY_SEMANTICS_FIELDS + ].to_h { |name| [name, ENV[name]] }.freeze + + SERIALIZE_ONLY_SEMANTICS_FIELDS = ENVIRONMENT["PRISM_SERIALIZE_ONLY_SEMANTICS_FIELDS"] || false + CHECK_FIELD_KIND = ENVIRONMENT["CHECK_FIELD_KIND"] || false + + JAVA_BACKEND = ENVIRONMENT["PRISM_JAVA_BACKEND"] || "default" JAVA_IDENTIFIER_TYPE = JAVA_BACKEND == "truffleruby" ? "String" : "byte[]" INCLUDE_NODE_ID = !SERIALIZE_ONLY_SEMANTICS_FIELDS || JAVA_BACKEND == "jruby" From 361b3e903758a35ff64291be2d6fb67ac50b9cca Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Mon, 9 Nov 2026 22:21:07 -0500 Subject: [PATCH 02/16] Build each extension variant in its own directory --- Rakefile | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/Rakefile b/Rakefile index b1e7b414e0..3fd1459e7c 100644 --- a/Rakefile +++ b/Rakefile @@ -86,6 +86,21 @@ if RUBY_ENGINE == "ruby" and !ENV["PRISM_FFI_BACKEND"] 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. + variant = ["debug", "minimal"].filter_map do |name| + name if ENV.key?("PRISM_BUILD_#{name.upcase}") || ext.extra_options.include?("--enable-build-#{name}") + 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 elsif RUBY_ENGINE == "jruby" require "rake/javaextensiontask" From 034dbf7c1e542279544bbce124944024abd56161 Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Tue, 10 Nov 2026 10:17:41 -0500 Subject: [PATCH 03/16] Rebuild libprism when its compile flags change --- Makefile | 39 ++++++++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index 0419b77112..577c63dfe9 100644 --- a/Makefile +++ b/Makefile @@ -40,6 +40,20 @@ SOURCES := $(wildcard src/*.c src/*/*.c) SHARED_OBJECTS := $(subst src/,build/shared/,$(SOURCES:.c=.o)) STATIC_OBJECTS := $(subst src/,build/static/,$(SOURCES:.c=.o)) +# The objects depend on the flags they are compiled with as well as on their +# sources. Make only compares timestamps, so build/.flags records the flags and +# is rewritten whenever they differ from what it holds. Its timestamp then moves +# only when the flags change, which makes the objects out of date exactly then. +# Target-specific flags, such as those of `minimal`, reach it as a prerequisite. +# In the same way, build/.sources records the list of sources, so that the +# libraries are linked again when a source is removed. +# +# Some makes compare timestamps to the second, such as the GNU Make 3.81 that +# ships with macOS. A stamp written in the same second as the last objects of +# the previous build would not be newer than them, so a changed stamp is written +# a second after it is found to differ. +BUILD_FLAGS = $(CC) $(DEBUG_FLAGS) $(CPPFLAGS) $(CFLAGS) $(OPTFLAGS) + all: shared static shared: build/libprism.$(SOEXT) @@ -48,12 +62,23 @@ wasm: javascript/src/prism.wasm java-wasm: java/wasm/src/main/wasm/prism.wasm playground: doc/playground -build/libprism.$(SOEXT): $(SHARED_OBJECTS) +build/.flags: FORCE + $(Q) $(MAKEDIRS) $(@D) + $(Q) printf '%s\n' '$(BUILD_FLAGS)' | cmp -s - $@ || { sleep 1; printf '%s\n' '$(BUILD_FLAGS)' > $@; } + +build/.sources: FORCE + $(Q) $(MAKEDIRS) $(@D) + $(Q) printf '%s\n' '$(SOURCES)' | cmp -s - $@ || { sleep 1; printf '%s\n' '$(SOURCES)' > $@; } + +build/libprism.$(SOEXT): $(SHARED_OBJECTS) build/.flags build/.sources $(ECHO) "linking $@ with $(CC)" - $(Q) $(CC) $(DEBUG_FLAGS) $(CFLAGS) -shared $(SHARED_LDFLAGS) -o $@ $(SHARED_OBJECTS) + $(Q) $(CC) $(DEBUG_FLAGS) $(CFLAGS) $(OPTFLAGS) -shared $(SHARED_LDFLAGS) -o $@ $(SHARED_OBJECTS) -build/libprism.a: $(STATIC_OBJECTS) +# `ar -r` only adds and replaces members, so the archive is created afresh to +# hold exactly the current objects. +build/libprism.a: $(STATIC_OBJECTS) build/.flags build/.sources $(ECHO) "building $@ with $(AR)" + $(Q) $(RMALL) $@ $(Q) $(AR) $(ARFLAGS) $@ $(STATIC_OBJECTS) javascript/src/prism.wasm: Makefile $(SOURCES) $(HEADERS) @@ -84,15 +109,15 @@ java/wasm/src/main/wasm/prism.wasm: Makefile $(SOURCES) $(HEADERS) -Wl,--export-all -Wl,--no-entry -mexec-model=reactor -lc++ -lc++abi \ -o $@ $(SOURCES) -build/shared/%.o: src/%.c Makefile $(HEADERS) +build/shared/%.o: src/%.c Makefile $(HEADERS) build/.flags $(ECHO) "compiling $@" $(Q) $(MAKEDIRS) $(@D) - $(Q) $(CC) $(DEBUG_FLAGS) -DPRISM_EXPORT_SYMBOLS $(CPPFLAGS) $(CFLAGS) -c -o $@ $< + $(Q) $(CC) $(DEBUG_FLAGS) -DPRISM_EXPORT_SYMBOLS $(CPPFLAGS) $(CFLAGS) $(OPTFLAGS) -c -o $@ $< -build/static/%.o: src/%.c Makefile $(HEADERS) +build/static/%.o: src/%.c Makefile $(HEADERS) build/.flags $(ECHO) "compiling $@" $(Q) $(MAKEDIRS) $(@D) - $(Q) $(CC) $(DEBUG_FLAGS) $(CPPFLAGS) $(CFLAGS) -c -o $@ $< + $(Q) $(CC) $(DEBUG_FLAGS) $(CPPFLAGS) $(CFLAGS) $(OPTFLAGS) -c -o $@ $< build/fuzz.%: $(SOURCES) test/fuzz/%.c test/fuzz/fuzz.c $(ECHO) "building $* fuzzer" From 887d45ad10bf759b3261f8e7c4abb1fc9644e632 Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Tue, 10 Nov 2026 15:07:35 -0500 Subject: [PATCH 04/16] Choose build variants with the same environment variable in both builds --- .github/workflows/main.yml | 6 ++++-- Makefile | 15 +++++++++++++++ Rakefile | 6 +----- docs/build_system.md | 16 ++++++++++++++++ ext/prism/extconf.rb | 23 +++++++++++++++++++++++ 5 files changed, 59 insertions(+), 7 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index cba359b75d..ca9024826b 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -138,7 +138,9 @@ jobs: ruby-version: ruby bundler-cache: true - name: Run Ruby tests - run: bundle exec rake compile_no_debug test + run: bundle exec rake + env: + PRISM_BUILD_NDEBUG: "1" build-debug: strategy: @@ -172,7 +174,7 @@ jobs: ruby-version: ruby bundler-cache: true - name: Run Ruby tests - run: bundle exec rake compile_minimal test + run: bundle exec rake env: PRISM_BUILD_MINIMAL: "1" diff --git a/Makefile b/Makefile index 577c63dfe9..d9d2e0df2d 100644 --- a/Makefile +++ b/Makefile @@ -11,6 +11,21 @@ FUZZ_OUTPUT_DIR = $(CURDIR)/test/fuzz/output SOEXT ?= $(shell ruby -e 'puts RbConfig::CONFIG["SOEXT"]') CPPFLAGS := -Iinclude $(CPPFLAGS) + +# The build variants. Each is on when its variable is set, even to an empty +# value, in the environment or on the command line. ext/prism/extconf.rb reads +# the same variables the same way, so one variable selects the same variant of +# libprism and of the C extension. +ifneq ($(origin PRISM_BUILD_DEBUG),undefined) +CPPFLAGS += -DPRISM_BUILD_DEBUG +endif +ifneq ($(origin PRISM_BUILD_MINIMAL),undefined) +CPPFLAGS += -DPRISM_BUILD_MINIMAL +endif +ifneq ($(origin PRISM_BUILD_NDEBUG),undefined) +CPPFLAGS += -DNDEBUG=1 +endif + CFLAGS := -g -O2 -std=c99 -Wall -Werror -Wextra -Wpedantic -Wundef -Wconversion -Wno-missing-braces -fPIC -fvisibility=hidden -Wimplicit-fallthrough $(CFLAGS) JAVA_WASM_CFLAGS := -g0 -O2 -Oz -std=c99 -Wall -Werror -Wextra -Wpedantic -Wundef -Wconversion -Wno-missing-braces -fno-sanitize=all -fno-stack-protector -fPIC -fvisibility=hidden -Wimplicit-fallthrough $(JAVA_WASM_CFLAGS) CC ?= cc diff --git a/Rakefile b/Rakefile index 3fd1459e7c..df174e2c39 100644 --- a/Rakefile +++ b/Rakefile @@ -24,12 +24,8 @@ task(make: :templates) { ENV["MAKEFLAGS"] ||= "-j" sh(make) } -task(make_no_debug: :templates) { sh("#{make} all-no-debug") } -task(make_minimal: :templates) { sh("#{make} minimal") } task compile: :make -task compile_no_debug: %i[make_no_debug compile] -task compile_minimal: %i[make_minimal compile] # decorate the gem build task with prerequisites task build: [:check_manifest, :templates] @@ -93,7 +89,7 @@ if RUBY_ENGINE == "ruby" and !ENV["PRISM_FFI_BACKEND"] # 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. - variant = ["debug", "minimal"].filter_map do |name| + variant = ["debug", "minimal", "ndebug"].filter_map do |name| name if ENV.key?("PRISM_BUILD_#{name.upcase}") || ext.extra_options.include?("--enable-build-#{name}") end diff --git a/docs/build_system.md b/docs/build_system.md index c141a7b776..96609dd22b 100644 --- a/docs/build_system.md +++ b/docs/build_system.md @@ -107,6 +107,22 @@ If you want to build prism as a shared library and link against it, you should c MAKEFLAGS="-j10" bundle exec rake compile ``` +## Build variants + +A build variant is chosen by an environment variable, which both the C extension (through `ext/prism/extconf.rb`) and `libprism` (through the `Makefile`) read. A variant is on when its variable is set, even to an empty value: + +* `PRISM_BUILD_DEBUG` - Defines the `PRISM_BUILD_DEBUG` build option below. +* `PRISM_BUILD_MINIMAL` - Defines the `PRISM_BUILD_MINIMAL` build option below. +* `PRISM_BUILD_NDEBUG` - Defines `NDEBUG`, which compiles out the assertions. + +For example, this builds the minimal variant and runs the tests against it: + +``` +PRISM_BUILD_MINIMAL=1 bundle exec rake +``` + +Each combination of variants builds the C extension in its own directory under `tmp/`, and `libprism` is rebuilt in `build/` whenever its flags change, so switching between variants never needs `rake clobber`. The `minimal` and `all-no-debug` targets of the `Makefile` remain as shorthands for building `libprism` alone; `all-no-debug` also compiles with `-O3`. + ## Build options * `PRISM_BUILD_DEBUG` - Will cause all file reading to copy into its own allocation to allow easier tracking of reading off the end of the buffer. By default this is off. diff --git a/ext/prism/extconf.rb b/ext/prism/extconf.rb index fafdd701ba..3ad96b0ccc 100644 --- a/ext/prism/extconf.rb +++ b/ext/prism/extconf.rb @@ -16,6 +16,10 @@ Enable minimal build. You may also set the PRISM_BUILD_MINIMAL environment variable. + --enable-build-ndebug + Enable build without assertions. + You may also set the PRISM_BUILD_NDEBUG environment variable. + --help Display this message. @@ -27,6 +31,9 @@ PRISM_BUILD_MINIMAL Equivalent to `--enable-build-minimal` when set, even if nil or blank. + PRISM_BUILD_NDEBUG + Equivalent to `--enable-build-ndebug` when set, even if nil or blank. + TEXT exit!(0) end @@ -104,6 +111,16 @@ def make(env, target) append_cflags("-DPRISM_BUILD_MINIMAL") end +# If `--enable-build-ndebug` is passed to this script or the +# `PRISM_BUILD_NDEBUG` environment variable is defined, we'll build with the +# `NDEBUG` macro defined, which compiles out the assertions in the parser. This +# skips the check that append_cflags makes, which compiles ruby.h with -Werror: +# a Ruby built with RUBY_DEBUG warns there that its own headers ignore NDEBUG. +# The sources of libprism do not include ruby.h, so NDEBUG still applies to them. +if enable_config("build-ndebug", ENV["PRISM_BUILD_NDEBUG"] || false) + $defs << "-DNDEBUG=1" +end + # By default, all symbols are hidden in the shared library. append_cflags("-fvisibility=hidden") @@ -124,3 +141,9 @@ def add_libprism_source(path) # Finally, we'll create the `Makefile` that is going to be used to configure and # build the C extension. create_makefile("prism/prism") + +# The Makefile that mkmf creates makes the objects depend on their sources and +# headers, but not on the Makefile itself, which holds the flags they are +# compiled with. This script writes a new Makefile whenever it runs, so with +# this rule a change to the build options recompiles every object. +File.open("Makefile", "a") { |file| file.puts("", "$(OBJS): Makefile") } From c192634bba51ec15e0f1589de8c35303704f6f3f Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Mon, 16 Nov 2026 12:10:55 -0500 Subject: [PATCH 05/16] Run tests that need a different build in a new rake process instead of clobbering --- .github/workflows/main.yml | 2 +- rakelib/bear.rake | 6 ++++-- rakelib/serialization.rake | 20 +++++++++----------- rakelib/test.rake | 9 +++++---- 4 files changed, 19 insertions(+), 18 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index ca9024826b..30baac6af7 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -234,7 +234,7 @@ jobs: ruby-version: jruby bundler-cache: true - name: Run Java Loader test - run: PRISM_SERIALIZE_ONLY_SEMANTICS_FIELDS=1 JRUBY_OPTS="-J-ea" bundle exec rake test:java_loader + run: JRUBY_OPTS="-J-ea" bundle exec rake test:java_loader build-java-truffleruby: runs-on: ubuntu-latest diff --git a/rakelib/bear.rake b/rakelib/bear.rake index c99feda07b..4428410025 100644 --- a/rakelib/bear.rake +++ b/rakelib/bear.rake @@ -1,10 +1,12 @@ # frozen_string_literal: true desc "Generate compilation database" -task bear: [:clobber, :templates] do +task bear: :templates do sh "which bear" do |ok, _| abort("Installing bear is required to generate the compilation database") unless ok end - sh "bear -- make" + # bear records only the commands that run, so every object is compiled + # whether or not it is up to date. + sh "bear -- make -B" end diff --git a/rakelib/serialization.rake b/rakelib/serialization.rake index 8ff3bfae91..189b060fbf 100644 --- a/rakelib/serialization.rake +++ b/rakelib/serialization.rake @@ -1,17 +1,13 @@ # frozen_string_literal: true +# The expected newlines are generated with the default (full) serialization +# mode, since newlines.rb needs location fields, and the Java loader is tested +# with only the semantic fields serialized, as the Java bindings use it. The +# templates read the serialization mode when they are loaded, so each half runs +# in a new rake process with the mode set for it. task "test:java_loader" do - # Recompile with PRISM_SERIALIZE_ONLY_SEMANTICS_FIELDS=1 - # Due to some JRuby bug this does not get propagated to the compile task, so require the caller to set the env var - # ENV["PRISM_SERIALIZE_ONLY_SEMANTICS_FIELDS"] = "1" - raise "this task requires $SERIALIZE_ONLY_SEMANTICS_FIELDS to be set" unless ENV["PRISM_SERIALIZE_ONLY_SEMANTICS_FIELDS"] - - # Generate the expected newlines with the default (full) serialization mode - # in a subprocess, since newlines.rb needs location fields. - sh({ "PRISM_SERIALIZE_ONLY_SEMANTICS_FIELDS" => nil }, "bundle", "exec", "rake", "clobber", "test:java_loader:newline_fixtures") - - Rake::Task["clobber"].invoke - Rake::Task["test:java_loader:internal"].invoke + sh({ "PRISM_SERIALIZE_ONLY_SEMANTICS_FIELDS" => nil }, "bundle", "exec", "rake", "test:java_loader:newline_fixtures") + sh({ "PRISM_SERIALIZE_ONLY_SEMANTICS_FIELDS" => "1" }, "bundle", "exec", "rake", "test:java_loader:internal") end # Generates the fixtures for the MarkNewlinesVisitor check of test:java_loader: @@ -53,6 +49,8 @@ task "test:java_loader:newline_fixtures" => :compile do end task "test:java_loader:internal" => :compile do + raise "this task requires PRISM_SERIALIZE_ONLY_SEMANTICS_FIELDS to be set" unless ENV["PRISM_SERIALIZE_ONLY_SEMANTICS_FIELDS"] + fixtures = File.expand_path("../test/prism/fixtures", __dir__) $:.unshift(File.expand_path("../lib", __dir__)) diff --git a/rakelib/test.rake b/rakelib/test.rake index 6ed6dac64b..856619696c 100644 --- a/rakelib/test.rake +++ b/rakelib/test.rake @@ -61,11 +61,12 @@ namespace :test do # Hide test:valgrind_internal from rake -T Rake::Task["test:valgrind_internal"].clear_comments + # The tests run under valgrind against the debug variant, which copies the + # input into its own allocation so that reads past its end are reported. The + # Rakefile chooses the variant when it is loaded, so the variable is set for a + # new rake process rather than in this one. desc "Run tests under valgrind" task :valgrind do - # Recompile with PRISM_BUILD_DEBUG=1 - ENV["PRISM_BUILD_DEBUG"] = "1" - Rake::Task["clobber"].invoke - Rake::Task["test:valgrind_internal"].invoke + sh({ "PRISM_BUILD_DEBUG" => "1" }, "bundle", "exec", "rake", "test:valgrind_internal") end end From f34e3e32955184cd3ed865f3bb7d2ef9a61b75ad Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Mon, 16 Nov 2026 12:49:55 -0500 Subject: [PATCH 06/16] Add a build variant with the undefined behavior sanitizer --- .github/workflows/main.yml | 12 ++++++++++++ CONTRIBUTING.md | 1 + Makefile | 3 +++ Rakefile | 2 +- docs/build_system.md | 1 + ext/prism/extconf.rb | 19 +++++++++++++++++++ rakelib/test.rake | 8 ++++++++ 7 files changed, 45 insertions(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 30baac6af7..1eb6f0b731 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -311,6 +311,18 @@ jobs: - name: Run Ruby tests with valgrind run: bundle exec rake test:valgrind + ubsan: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v7 + - name: Set up Ruby + uses: ruby/setup-ruby@v1 + with: + ruby-version: ruby + bundler-cache: true + - name: Run Ruby tests with the undefined behavior sanitizer + run: bundle exec rake test:ubsan + gcc-analyzer: runs-on: ubuntu-latest steps: diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 8e6fa3b85d..efd70fa6b5 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -55,4 +55,5 @@ bundle exec rake bear Some useful rake tasks: - `test:valgrind` runs the test suite under valgrind to look for illegal memory access or memory leaks +- `test:ubsan` runs the test suite against a build with the undefined behavior sanitizer, which aborts on the first undefined behavior it finds - `test:gdb` and `test:lldb` run the test suite under those debuggers diff --git a/Makefile b/Makefile index d9d2e0df2d..72533764cd 100644 --- a/Makefile +++ b/Makefile @@ -25,6 +25,9 @@ endif ifneq ($(origin PRISM_BUILD_NDEBUG),undefined) CPPFLAGS += -DNDEBUG=1 endif +ifneq ($(origin PRISM_BUILD_UBSAN),undefined) +CFLAGS += -fsanitize=undefined -fno-sanitize-recover=undefined -fno-omit-frame-pointer +endif CFLAGS := -g -O2 -std=c99 -Wall -Werror -Wextra -Wpedantic -Wundef -Wconversion -Wno-missing-braces -fPIC -fvisibility=hidden -Wimplicit-fallthrough $(CFLAGS) JAVA_WASM_CFLAGS := -g0 -O2 -Oz -std=c99 -Wall -Werror -Wextra -Wpedantic -Wundef -Wconversion -Wno-missing-braces -fno-sanitize=all -fno-stack-protector -fPIC -fvisibility=hidden -Wimplicit-fallthrough $(JAVA_WASM_CFLAGS) diff --git a/Rakefile b/Rakefile index df174e2c39..7d6835aa73 100644 --- a/Rakefile +++ b/Rakefile @@ -89,7 +89,7 @@ if RUBY_ENGINE == "ruby" and !ENV["PRISM_FFI_BACKEND"] # 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. - variant = ["debug", "minimal", "ndebug"].filter_map do |name| + variant = ["debug", "minimal", "ndebug", "ubsan"].filter_map do |name| name if ENV.key?("PRISM_BUILD_#{name.upcase}") || ext.extra_options.include?("--enable-build-#{name}") end diff --git a/docs/build_system.md b/docs/build_system.md index 96609dd22b..5765440580 100644 --- a/docs/build_system.md +++ b/docs/build_system.md @@ -114,6 +114,7 @@ A build variant is chosen by an environment variable, which both the C extension * `PRISM_BUILD_DEBUG` - Defines the `PRISM_BUILD_DEBUG` build option below. * `PRISM_BUILD_MINIMAL` - Defines the `PRISM_BUILD_MINIMAL` build option below. * `PRISM_BUILD_NDEBUG` - Defines `NDEBUG`, which compiles out the assertions. +* `PRISM_BUILD_UBSAN` - Compiles with the undefined behavior sanitizer, which aborts on the first undefined behavior it detects. `rake test:ubsan` runs the tests against this variant. For example, this builds the minimal variant and runs the tests against it: diff --git a/ext/prism/extconf.rb b/ext/prism/extconf.rb index 3ad96b0ccc..b65f9ff192 100644 --- a/ext/prism/extconf.rb +++ b/ext/prism/extconf.rb @@ -20,6 +20,10 @@ Enable build without assertions. You may also set the PRISM_BUILD_NDEBUG environment variable. + --enable-build-ubsan + Enable build with the undefined behavior sanitizer. + You may also set the PRISM_BUILD_UBSAN environment variable. + --help Display this message. @@ -34,6 +38,9 @@ PRISM_BUILD_NDEBUG Equivalent to `--enable-build-ndebug` when set, even if nil or blank. + PRISM_BUILD_UBSAN + Equivalent to `--enable-build-ubsan` when set, even if nil or blank. + TEXT exit!(0) end @@ -121,6 +128,18 @@ def make(env, target) $defs << "-DNDEBUG=1" end +# If `--enable-build-ubsan` is passed to this script or the `PRISM_BUILD_UBSAN` +# environment variable is defined, we'll build with the undefined behavior +# sanitizer. Any undefined behavior it detects, such as a misaligned access or a +# signed overflow, aborts the process with a report of where it happened. The +# sanitizer runtime is linked into the extension itself, so the Ruby that loads +# it does not need to be built with the sanitizer. +if enable_config("build-ubsan", ENV["PRISM_BUILD_UBSAN"] || false) + flags = "-fsanitize=undefined -fno-sanitize-recover=undefined -fno-omit-frame-pointer" + append_cflags(flags) + append_ldflags(flags) +end + # By default, all symbols are hidden in the shared library. append_cflags("-fvisibility=hidden") diff --git a/rakelib/test.rake b/rakelib/test.rake index 856619696c..aa6a58997a 100644 --- a/rakelib/test.rake +++ b/rakelib/test.rake @@ -69,4 +69,12 @@ namespace :test do task :valgrind do sh({ "PRISM_BUILD_DEBUG" => "1" }, "bundle", "exec", "rake", "test:valgrind_internal") end + + # The tests run against the variant built with the undefined behavior + # sanitizer, which aborts on the first undefined behavior it detects. As with + # test:valgrind, the variable is set for a new rake process. + desc "Run tests with the undefined behavior sanitizer" + task :ubsan do + sh({ "PRISM_BUILD_UBSAN" => "1" }, "bundle", "exec", "rake", "compile", "test") + end end From 49861bd7e76d15ca5d6ff3ac964dc1272acc4e5e Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Mon, 16 Nov 2026 13:24:42 -0500 Subject: [PATCH 07/16] Limit parallel make jobs to the number of processors --- Rakefile | 6 +++++- docs/build_system.md | 4 ++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/Rakefile b/Rakefile index 7d6835aa73..4137c15c85 100644 --- a/Rakefile +++ b/Rakefile @@ -19,9 +19,13 @@ require_relative "templates/template" 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) { - ENV["MAKEFLAGS"] ||= "-j" + require "etc" + ENV["MAKEFLAGS"] ||= "-j#{Etc.nprocessors}" sh(make) } diff --git a/docs/build_system.md b/docs/build_system.md index 5765440580..94e5d1bd78 100644 --- a/docs/build_system.md +++ b/docs/build_system.md @@ -101,10 +101,10 @@ If you want to build prism as a shared library and link against it, you should c #### Flags -`make` respects the `MAKEFLAGS` environment variable. As such, to speed up the build you can run: +`make` respects the `MAKEFLAGS` environment variable. `rake` sets it to run one job per processor when it is not already set, so to choose a different number of jobs you can run: ``` -MAKEFLAGS="-j10" bundle exec rake compile +MAKEFLAGS="-j4" bundle exec rake compile ``` ## Build variants From bb8a7da4f1ef0e642a7750be53652f603a26cbcc Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Mon, 16 Nov 2026 13:30:00 -0500 Subject: [PATCH 08/16] Flush the help text of extconf.rb before exiting --- ext/prism/extconf.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/prism/extconf.rb b/ext/prism/extconf.rb index b65f9ff192..f9a1788bc0 100644 --- a/ext/prism/extconf.rb +++ b/ext/prism/extconf.rb @@ -42,7 +42,7 @@ Equivalent to `--enable-build-ubsan` when set, even if nil or blank. TEXT - exit!(0) + exit end # If this gem is being build from a git source, then we need to run From 1a7f2a623224d52e6e5964a153e9449d26ba7116 Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Mon, 16 Nov 2026 17:42:44 -0500 Subject: [PATCH 09/16] Configure the extension again when its sources or headers change --- Rakefile | 61 +++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 43 insertions(+), 18 deletions(-) diff --git a/Rakefile b/Rakefile index 4137c15c85..7b31201983 100644 --- a/Rakefile +++ b/Rakefile @@ -16,6 +16,31 @@ 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 @@ -81,7 +106,7 @@ end if RUBY_ENGINE == "ruby" and !ENV["PRISM_FFI_BACKEND"] require "rake/extensiontask" - Rake::ExtensionTask.new("prism", PRISM_SPEC) do |ext| + extension = Rake::ExtensionTask.new("prism", PRISM_SPEC) do |ext| ext.ext_dir = "ext/prism" ext.lib_dir = "lib/prism" ext.cross_compile = true @@ -102,6 +127,19 @@ if RUBY_ENGINE == "ruby" and !ENV["PRISM_FFI_BACKEND"] 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 the 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 + + file "#{extension.tmp_dir}/#{extension.platform}/#{extension.name}/#{RUBY_VERSION}/Makefile" => extension_files elsif RUBY_ENGINE == "jruby" require "rake/javaextensiontask" @@ -122,25 +160,12 @@ 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. Rake only compares -# file timestamps, so this stamp records the variables' values and is rewritten -# whenever they differ from what it holds. Its timestamp then moves only when a -# value changes, which makes the generated files out of date exactly then. -class TemplateEnvironmentTask < Rake::FileTask - def contents - Prism::Template::ENVIRONMENT.map { |name, value| "#{name}=#{value.inspect}\n" }.join - end - - def needed? - super || File.read(name) != contents - end -end - +# Prism::Template::ENVIRONMENT as well as on their templates, so they depend on +# a stamp of the variables' values. template_environment = "tmp/template.env" -TemplateEnvironmentTask.define_task(template_environment) do |t| - mkdir_p(File.dirname(t.name)) - File.write(t.name, t.contents) +StampTask.define(template_environment) do + Prism::Template::ENVIRONMENT.map { |name, value| "#{name}=#{value.inspect}\n" }.join end Prism::Template::TEMPLATES.each do |filepath| From 28c366a8348e0eda66ca8d7f2c6e799a171561ff Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Mon, 16 Nov 2026 18:15:30 -0500 Subject: [PATCH 10/16] Match extension build variant selection to mkmf option parsing --- Rakefile | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/Rakefile b/Rakefile index 7b31201983..39fb10a636 100644 --- a/Rakefile +++ b/Rakefile @@ -105,6 +105,7 @@ end if RUBY_ENGINE == "ruby" and !ENV["PRISM_FFI_BACKEND"] require "rake/extensiontask" + require "shellwords" extension = Rake::ExtensionTask.new("prism", PRISM_SPEC) do |ext| ext.ext_dir = "ext/prism" @@ -118,8 +119,32 @@ if RUBY_ENGINE == "ruby" and !ENV["PRISM_FFI_BACKEND"] # 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 = ["debug", "minimal", "ndebug", "ubsan"].filter_map do |name| - name if ENV.key?("PRISM_BUILD_#{name.upcase}") || ext.extra_options.include?("--enable-build-#{name}") + name if ( + options.include?("--enable-build-#{name}") || + (!options.include?("--disable-build-#{name}") && ENV.key?("PRISM_BUILD_#{name.upcase}")) + ) end unless variant.empty? From 5f73c7ba6c679ac892971cd4550007f55f2942e3 Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Mon, 16 Nov 2026 19:14:01 -0500 Subject: [PATCH 11/16] Reconfigure the extension when its build options change --- Rakefile | 21 +++++++++++++++++++-- docs/build_system.md | 2 ++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/Rakefile b/Rakefile index 39fb10a636..85175ad2bc 100644 --- a/Rakefile +++ b/Rakefile @@ -107,6 +107,8 @@ if RUBY_ENGINE == "ruby" and !ENV["PRISM_FFI_BACKEND"] require "rake/extensiontask" require "shellwords" + build_variants = %w[debug minimal ndebug ubsan] + extension = Rake::ExtensionTask.new("prism", PRISM_SPEC) do |ext| ext.ext_dir = "ext/prism" ext.lib_dir = "lib/prism" @@ -140,7 +142,7 @@ if RUBY_ENGINE == "ruby" and !ENV["PRISM_FFI_BACKEND"] argument end - variant = ["debug", "minimal", "ndebug", "ubsan"].filter_map do |name| + 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}")) @@ -164,7 +166,22 @@ if RUBY_ENGINE == "ruby" and !ENV["PRISM_FFI_BACKEND"] Dir["ext/prism/*.{c,h}", "src/**/*.c", "include/**/*.h"].sort.map { |path| "#{path}\n" }.join end - file "#{extension.tmp_dir}/#{extension.platform}/#{extension.name}/#{RUBY_VERSION}/Makefile" => extension_files + extension_directory = "#{extension.tmp_dir}/#{extension.platform}/#{extension.name}/#{RUBY_VERSION}" + extension_configuration = "#{extension_directory}/configuration" + + # 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 + extension.extra_options, + environment: ["CONFIGURE_ARGS", *build_variants.map { |name| "PRISM_BUILD_#{name.upcase}" }].map { |name| [name, ENV[name]] } + }.inspect + "\n" + end + + file "#{extension_directory}/Makefile" => [extension_files, extension_configuration] elsif RUBY_ENGINE == "jruby" require "rake/javaextensiontask" diff --git a/docs/build_system.md b/docs/build_system.md index 94e5d1bd78..33f42a4356 100644 --- a/docs/build_system.md +++ b/docs/build_system.md @@ -32,6 +32,8 @@ loaded per process (i.e., at most one version of the prism *gem* loaded in a pro `rake` calls `make` and then uses `Rake::ExtensionTask` to compile the C extension (see above). +Changes to the extension's configuration options, passed after `--` or through `CONFIGURE_ARGS`, rerun `extconf.rb` and rebuild the extension. For example, `bundle exec rake compile -- --with-cflags=-O0` changes its compiler flags without requiring `rake clobber`. + ### Building the prism gem by `gem install/bundle install` The gem contains the pre-generated templates. From 54bdcd550fe0e6c2b91cd351def2c81d6a78ce97 Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Mon, 16 Nov 2026 19:17:36 -0500 Subject: [PATCH 12/16] Reconfigure cross-built extensions when sources or headers change --- Rakefile | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/Rakefile b/Rakefile index 85175ad2bc..354d6bd44a 100644 --- a/Rakefile +++ b/Rakefile @@ -157,7 +157,7 @@ if RUBY_ENGINE == "ruby" and !ENV["PRISM_FFI_BACKEND"] # 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 the Makefile depends on a stamp of that list, + # 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" @@ -166,6 +166,14 @@ if RUBY_ENGINE == "ruby" and !ENV["PRISM_FFI_BACKEND"] 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| + if File.fnmatch?("#{extension.tmp_dir}/*/#{extension.name}/*/Makefile", task.name, File::FNM_PATHNAME) + task.enhance([extension_files]) + end + end + extension_directory = "#{extension.tmp_dir}/#{extension.platform}/#{extension.name}/#{RUBY_VERSION}" extension_configuration = "#{extension_directory}/configuration" @@ -181,7 +189,7 @@ if RUBY_ENGINE == "ruby" and !ENV["PRISM_FFI_BACKEND"] }.inspect + "\n" end - file "#{extension_directory}/Makefile" => [extension_files, extension_configuration] + file "#{extension_directory}/Makefile" => extension_configuration elsif RUBY_ENGINE == "jruby" require "rake/javaextensiontask" From 14f2d61ca22792a2d69de74a0b3209dfb56d9331 Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Mon, 16 Nov 2026 19:22:53 -0500 Subject: [PATCH 13/16] Rebuild libprism when linker or archive settings change --- Makefile | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 72533764cd..17fa2a4961 100644 --- a/Makefile +++ b/Makefile @@ -66,6 +66,9 @@ STATIC_OBJECTS := $(subst src/,build/static/,$(SOURCES:.c=.o)) # In the same way, build/.sources records the list of sources, so that the # libraries are linked again when a source is removed. # +# Separate stamps track shared-library linker flags and the archive command, +# so changing those settings rebuilds only the affected library, not its objects. +# # Some makes compare timestamps to the second, such as the GNU Make 3.81 that # ships with macOS. A stamp written in the same second as the last objects of # the previous build would not be newer than them, so a changed stamp is written @@ -88,13 +91,21 @@ build/.sources: FORCE $(Q) $(MAKEDIRS) $(@D) $(Q) printf '%s\n' '$(SOURCES)' | cmp -s - $@ || { sleep 1; printf '%s\n' '$(SOURCES)' > $@; } -build/libprism.$(SOEXT): $(SHARED_OBJECTS) build/.flags build/.sources +build/.ldflags: FORCE + $(Q) $(MAKEDIRS) $(@D) + $(Q) printf '%s\n' '$(SHARED_LDFLAGS)' | cmp -s - $@ || { sleep 1; printf '%s\n' '$(SHARED_LDFLAGS)' > $@; } + +build/.arflags: FORCE + $(Q) $(MAKEDIRS) $(@D) + $(Q) printf '%s\n' '$(AR) $(ARFLAGS)' | cmp -s - $@ || { sleep 1; printf '%s\n' '$(AR) $(ARFLAGS)' > $@; } + +build/libprism.$(SOEXT): $(SHARED_OBJECTS) build/.flags build/.sources build/.ldflags $(ECHO) "linking $@ with $(CC)" $(Q) $(CC) $(DEBUG_FLAGS) $(CFLAGS) $(OPTFLAGS) -shared $(SHARED_LDFLAGS) -o $@ $(SHARED_OBJECTS) # `ar -r` only adds and replaces members, so the archive is created afresh to # hold exactly the current objects. -build/libprism.a: $(STATIC_OBJECTS) build/.flags build/.sources +build/libprism.a: $(STATIC_OBJECTS) build/.flags build/.sources build/.arflags $(ECHO) "building $@ with $(AR)" $(Q) $(RMALL) $@ $(Q) $(AR) $(ARFLAGS) $@ $(STATIC_OBJECTS) From 3b9c9c6a365d85750242b0ee0281e8789f972d09 Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Mon, 16 Nov 2026 19:26:06 -0500 Subject: [PATCH 14/16] Added flag stamps and source-list tracking for both Wasm targets --- Makefile | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 17fa2a4961..d6b90f18d8 100644 --- a/Makefile +++ b/Makefile @@ -68,12 +68,15 @@ STATIC_OBJECTS := $(subst src/,build/static/,$(SOURCES:.c=.o)) # # Separate stamps track shared-library linker flags and the archive command, # so changing those settings rebuilds only the affected library, not its objects. +# Each Wasm target also tracks its own flags and the shared source list. # # Some makes compare timestamps to the second, such as the GNU Make 3.81 that # ships with macOS. A stamp written in the same second as the last objects of # the previous build would not be newer than them, so a changed stamp is written # a second after it is found to differ. BUILD_FLAGS = $(CC) $(DEBUG_FLAGS) $(CPPFLAGS) $(CFLAGS) $(OPTFLAGS) +WASM_BUILD_FLAGS = $(WASI_SDK_PATH) $(DEBUG_FLAGS) $(CPPFLAGS) $(CFLAGS) $(WASM_EXPORTS) +JAVA_WASM_BUILD_FLAGS = $(WASI_SDK_PATH) $(DEBUG_FLAGS) $(CPPFLAGS) $(JAVA_WASM_CFLAGS) all: shared static @@ -99,6 +102,14 @@ build/.arflags: FORCE $(Q) $(MAKEDIRS) $(@D) $(Q) printf '%s\n' '$(AR) $(ARFLAGS)' | cmp -s - $@ || { sleep 1; printf '%s\n' '$(AR) $(ARFLAGS)' > $@; } +build/.wasm.flags: FORCE + $(Q) $(MAKEDIRS) $(@D) + $(Q) printf '%s\n' '$(WASM_BUILD_FLAGS)' | cmp -s - $@ || { sleep 1; printf '%s\n' '$(WASM_BUILD_FLAGS)' > $@; } + +build/.java-wasm.flags: FORCE + $(Q) $(MAKEDIRS) $(@D) + $(Q) printf '%s\n' '$(JAVA_WASM_BUILD_FLAGS)' | cmp -s - $@ || { sleep 1; printf '%s\n' '$(JAVA_WASM_BUILD_FLAGS)' > $@; } + build/libprism.$(SOEXT): $(SHARED_OBJECTS) build/.flags build/.sources build/.ldflags $(ECHO) "linking $@ with $(CC)" $(Q) $(CC) $(DEBUG_FLAGS) $(CFLAGS) $(OPTFLAGS) -shared $(SHARED_LDFLAGS) -o $@ $(SHARED_OBJECTS) @@ -110,7 +121,7 @@ build/libprism.a: $(STATIC_OBJECTS) build/.flags build/.sources build/.arflags $(Q) $(RMALL) $@ $(Q) $(AR) $(ARFLAGS) $@ $(STATIC_OBJECTS) -javascript/src/prism.wasm: Makefile $(SOURCES) $(HEADERS) +javascript/src/prism.wasm: Makefile $(SOURCES) $(HEADERS) build/.sources build/.wasm.flags $(ECHO) "building $@" $(Q) $(WASI_SDK_PATH)/bin/clang --sysroot=$(WASI_SDK_PATH)/share/wasi-sysroot/ \ $(DEBUG_FLAGS) \ @@ -128,7 +139,7 @@ doc/playground: javascript/src/prism.wasm $(Q) $(MAKEDIRS) $@ $(Q) cp javascript/src/*.js javascript/src/prism.wasm javascript/package.json $@ -java/wasm/src/main/wasm/prism.wasm: Makefile $(SOURCES) $(HEADERS) +java/wasm/src/main/wasm/prism.wasm: Makefile $(SOURCES) $(HEADERS) build/.sources build/.java-wasm.flags $(ECHO) "building $@" $(Q) $(MAKEDIRS) $(@D) $(Q) $(WASI_SDK_PATH)/bin/clang \ From 646479ba29c890c0e8d4fc2c373aa708ce07fdd9 Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Mon, 16 Nov 2026 19:33:38 -0500 Subject: [PATCH 15/16] Reconfigure cross-built extensions when build options change --- Rakefile | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/Rakefile b/Rakefile index 354d6bd44a..e46c1962ad 100644 --- a/Rakefile +++ b/Rakefile @@ -169,27 +169,31 @@ if RUBY_ENGINE == "ruby" and !ENV["PRISM_FFI_BACKEND"] # rake-compiler defines a Makefile task for every configured platform and # Ruby version, including cross-compilation targets. Rake::Task.tasks.each do |task| - if File.fnmatch?("#{extension.tmp_dir}/*/#{extension.name}/*/Makefile", task.name, File::FNM_PATHNAME) - task.enhance([extension_files]) + 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 - end - extension_directory = "#{extension.tmp_dir}/#{extension.platform}/#{extension.name}/#{RUBY_VERSION}" - extension_configuration = "#{extension_directory}/configuration" - - # 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 + extension.extra_options, - environment: ["CONFIGURE_ARGS", *build_variants.map { |name| "PRISM_BUILD_#{name.upcase}" }].map { |name| [name, ENV[name]] } - }.inspect + "\n" - 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 - file "#{extension_directory}/Makefile" => extension_configuration + task.enhance([extension_files, extension_configuration]) + end elsif RUBY_ENGINE == "jruby" require "rake/javaextensiontask" From 8602c50b7fb7ddd863c6da9cba8c7ed9725e4adf Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Mon, 16 Nov 2026 20:56:44 -0500 Subject: [PATCH 16/16] Use zu in the debug allocator --- include/prism/internal/allocator_debug.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/include/prism/internal/allocator_debug.h b/include/prism/internal/allocator_debug.h index 846e96ba2d..b555df8c67 100644 --- a/include/prism/internal/allocator_debug.h +++ b/include/prism/internal/allocator_debug.h @@ -46,7 +46,7 @@ pm_allocator_debug_free_sized(void *ptr, size_t old_size) { if (ptr != NULL) { size_t *memory = (size_t *)ptr; if (old_size != memory[-1]) { - fprintf(stderr, "[BUG] buffer %p was allocated with size %lu but freed with size %lu\n", ptr, memory[-1], old_size); + fprintf(stderr, "[BUG] buffer %p was allocated with size %zu but freed with size %zu\n", ptr, memory[-1], old_size); abort(); } xfree_sized(memory - 1, old_size + sizeof(size_t)); @@ -57,7 +57,7 @@ static inline void * pm_allocator_debug_realloc_sized(void *ptr, size_t size, size_t old_size) { if (ptr == NULL) { if (old_size != 0) { - fprintf(stderr, "[BUG] realloc_sized called with NULL pointer and old size %lu\n", old_size); + fprintf(stderr, "[BUG] realloc_sized called with NULL pointer and old size %zu\n", old_size); abort(); } return pm_allocator_debug_malloc(size); @@ -65,7 +65,7 @@ pm_allocator_debug_realloc_sized(void *ptr, size_t size, size_t old_size) { size_t *memory = (size_t *)ptr; if (old_size != memory[-1]) { - fprintf(stderr, "[BUG] buffer %p was allocated with size %lu but realloced with size %lu\n", ptr, memory[-1], old_size); + fprintf(stderr, "[BUG] buffer %p was allocated with size %zu but realloced with size %zu\n", ptr, memory[-1], old_size); abort(); } return pm_allocator_debug_realloc(ptr, size);