diff --git a/Makefile b/Makefile index a3faceff201..c20fdc9f9e8 100644 --- a/Makefile +++ b/Makefile @@ -13,6 +13,7 @@ SOEXT ?= $(shell ruby -e 'puts RbConfig::CONFIG["SOEXT"]') CPPFLAGS := -Iinclude $(CPPFLAGS) CFLAGS := -g -O2 -std=c99 -Wall -Werror -Wextra -Wpedantic -Wundef -Wconversion -Wno-missing-braces -fPIC -fvisibility=hidden $(CFLAGS) CC ?= cc +AR ?= ar WASI_SDK_PATH := /opt/wasi-sdk MAKEDIRS ?= mkdir -p @@ -31,11 +32,11 @@ wasm: javascript/src/prism.wasm java-wasm: java-wasm/src/test/resources/prism.wasm build/libprism.$(SOEXT): $(SHARED_OBJECTS) - $(ECHO) "linking $@" + $(ECHO) "linking $@ with $(CC)" $(Q) $(CC) $(DEBUG_FLAGS) $(CFLAGS) -shared -o $@ $(SHARED_OBJECTS) build/libprism.a: $(STATIC_OBJECTS) - $(ECHO) "building $@" + $(ECHO) "building $@ with $(AR)" $(Q) $(AR) $(ARFLAGS) $@ $(STATIC_OBJECTS) $(Q1:0=>/dev/null) javascript/src/prism.wasm: Makefile $(SOURCES) $(HEADERS) diff --git a/ext/prism/extconf.rb b/ext/prism/extconf.rb index f6868da0a5f..53800785efc 100644 --- a/ext/prism/extconf.rb +++ b/ext/prism/extconf.rb @@ -40,15 +40,34 @@ def generate_templates end end +# We're going to need to run `make` using prism's `Makefile`. We want to match +# up as much of the configuration to the configuration that built the current +# version of Ruby as possible. require "rbconfig" +env = RbConfig::CONFIG.slice("SOEXT", "CPPFLAGS", "CFLAGS", "CC", "AR", "ARFLAGS", "MAKEDIRS", "RMALL") + +# It's possible that the Ruby that is being run wasn't actually compiled on this +# machine, in which case the configuration might be incorrect. In this case +# we'll need to do some additional checks and potentially fall back to defaults. +if env.key?("CC") && !File.exist?(env["CC"]) + env.delete("CC") + env.delete("CFLAGS") + env.delete("CPPFLAGS") +end + +if env.key?("AR") && !File.exist?(env["AR"]) + env.delete("AR") + env.delete("ARFLAGS") +end # Runs `make` in the root directory of the project. Note that this is the # `Makefile` for the overall project, not the `Makefile` that is being generated # by this script.` -def make(target) +def make(env, target) + puts "Running make #{target} with #{env.inspect}" Dir.chdir(File.expand_path("../..", __dir__)) do system( - RbConfig::CONFIG.slice(*%w[SOEXT CPPFLAGS CFLAGS CC AR ARFLAGS MAKEDIRS RMALL]), # env + env, RUBY_PLATFORM.include?("openbsd") ? "gmake" : "make", target, exception: true @@ -62,7 +81,7 @@ def make(target) # but we want to use the native toolchain here since libprism is run natively. if RUBY_ENGINE != "ruby" generate_templates - make("build/libprism.#{RbConfig::CONFIG["SOEXT"]}") + make(env, "build/libprism.#{RbConfig::CONFIG["SOEXT"]}") File.write("Makefile", "all install clean:\n\t@#{RbConfig::CONFIG["NULLCMD"]}\n") return end @@ -110,7 +129,7 @@ def make(target) archive_target = "build/libprism.a" archive_path = File.expand_path("../../#{archive_target}", __dir__) -make(archive_target) unless File.exist?(archive_path) +make(env, archive_target) unless File.exist?(archive_path) $LOCAL_LIBS << " #{archive_path}" # Finally, we'll create the `Makefile` that is going to be used to configure and