Skip to content

Commit

Permalink
Fall back to default AR and CC
Browse files Browse the repository at this point in the history
  • Loading branch information
kddnewton committed Apr 22, 2024
1 parent 7142b76 commit c08eebf
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
27 changes: 23 additions & 4 deletions ext/prism/extconf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
Binary file added prism-0.26.0.gem
Binary file not shown.

0 comments on commit c08eebf

Please sign in to comment.