Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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"

Expand Down Expand Up @@ -232,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
Expand Down Expand Up @@ -286,7 +288,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

Expand All @@ -309,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:
Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
83 changes: 74 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,24 @@ 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
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)
CC ?= cc
Expand Down Expand Up @@ -40,6 +58,26 @@ 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.
#
# 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

shared: build/libprism.$(SOEXT)
Expand All @@ -48,15 +86,42 @@ 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/.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/.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) -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 build/.arflags
$(ECHO) "building $@ with $(AR)"
$(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) \
Expand All @@ -74,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 \
Expand All @@ -84,15 +149,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"
Expand Down
130 changes: 123 additions & 7 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,45 @@ 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) {
ENV["MAKEFLAGS"] ||= "-j"
require "etc"
ENV["MAKEFLAGS"] ||= "-j#{Etc.nprocessors}"
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]
Expand Down Expand Up @@ -80,12 +105,94 @@ end

if RUBY_ENGINE == "ruby" and !ENV["PRISM_FFI_BACKEND"]
require "rake/extensiontask"
require "shellwords"

Rake::ExtensionTask.new("prism", PRISM_SPEC) do |ext|
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"
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"
Expand All @@ -106,9 +213,18 @@ 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"] do |t|
file filepath => ["templates/#{filepath}.erb", "templates/template.rb", "config.yml", template_environment] do |t|
Prism::Template.render(t.name)
end
end
Expand Down
Loading
Loading