amalgam.rb: support gems with core-affecting defines

Gems like mruby-task add preprocessor defines (MRB_USE_TASK_SCHEDULER)
that affect mrb_state structure. The amalgamation generator now detects
these defines from the build configuration and adds them at the top of
mruby.h before struct definitions are encountered.

Supported define patterns: MRB_USE_*, MRB_UTF8_*, HAVE_MRUBY_*

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Yukihiro "Matz" Matsumoto
2026-01-02 17:35:31 +09:00
parent 07cd188264
commit f78ac530c8
2 changed files with 27 additions and 4 deletions
+7 -4
View File
@@ -72,6 +72,7 @@ The following gems work with amalgamation:
- `mruby-enum-ext`, `mruby-compar-ext`
- `mruby-error`, `mruby-math`, `mruby-struct`, `mruby-bigint`
- `mruby-io` (with `hal-posix-io`)
- `mruby-task` (with `hal-posix-task`)
### Excluded Gems
@@ -79,10 +80,6 @@ Binary gems (`mruby-bin-*`) are automatically excluded as they contain
their own `main()` function. The amalgamation produces a library, not
an executable.
### Incompatible Gems
- `mruby-task` - Requires special build configuration (`mrb->task` member)
## Example Configuration
A minimal configuration for amalgamation:
@@ -130,6 +127,12 @@ Typical sizes depend on included gems:
- Local includes (`.cstub` files) are automatically inlined
- Generated files (`mrblib.c`, `gem_init.c`) are included
### Gem Defines
Gems that add preprocessor defines affecting core structures (like
`MRB_USE_TASK_SCHEDULER` from mruby-task) are automatically detected
and included at the top of `mruby.h`.
### Build Order
1. Core sources (`src/*.c`)
+20
View File
@@ -169,6 +169,26 @@ module MRuby
#include <string.h>
PREAMBLE
# Add build-level defines from gems (e.g., MRB_USE_TASK_SCHEDULER)
gem_defines = collect_gem_defines
unless gem_defines.empty?
f.puts "/* Gem-required defines */"
gem_defines.each do |d|
f.puts "#define #{d}"
end
f.puts
end
end
# Collect defines added by gems that affect core headers
def collect_gem_defines
defines = []
@build.defines.each do |d|
# Include defines that affect mrb_state or core functionality
defines << d if d =~ /^MRB_USE_|^MRB_UTF8_|^HAVE_MRUBY_/
end
defines.uniq.sort
end
def write_header_postamble(f)