Add `-s` option to `mrbc` for make variable static
MODIFY
include/mruby/dump.h
include/mruby/dump.h
*/
MRB_BEGIN_DECL
-#define DUMP_DEBUG_INFO 1
+/* flags for mrb_dump_irep{,_binary,_cfunc,_cstruct} */
+#define MRB_DUMP_DEBUG_INFO 1
+#define MRB_DUMP_STATIC 2
+#define DUMP_DEBUG_INFO MRB_DUMP_DEBUG_INFO /* deprecated */
int mrb_dump_irep(mrb_state *mrb, const mrb_irep *irep, uint8_t flags, uint8_t **bin, size_t *bin_size);
#ifndef MRB_NO_STDIO

MODIFY
lib/mruby/build/command.rb
lib/mruby/build/command.rb
@@ -328,13 +328,16 @@ module MRuby
@compile_options = "-B%{funcname} -o-"
end
- def run(out, infiles, funcname, cdump = true)
+ def run(out, infiles, funcname, cdump: true, static: false)
@command ||= @build.mrbcfile
infiles = [infiles].flatten
infiles.each_with_index do |f, i|
_pp i == 0 ? "MRBC" : "", f.relative_path, indent: 2
end
- cmd = %Q["#{filename @command}" #{cdump ? "-S" : ""} #{@compile_options % {:funcname => funcname}} #{filename(infiles).map{|f| %Q["#{f}"]}.join(' ')}]
+ opt = @compile_options % {funcname: funcname}
+ opt << " -S" if cdump
+ opt << " -s" if static
+ cmd = %["#{filename @command}" #{opt} #{filename(infiles).map{|f| %["#{f}"]}.join(' ')}]
puts cmd if Rake.verbose
IO.popen(cmd, 'r+') do |io|
out.puts io.read

MODIFY
lib/mruby/gem.rb
lib/mruby/gem.rb
@@ -198,10 +198,11 @@ module MRuby
open(fname, 'w') do |f|
print_gem_init_header f
unless rbfiles.empty?
+ opts = {cdump: cdump?, static: true}
if cdump?
- build.mrbc.run f, rbfiles, "gem_mrblib_#{funcname}_proc"
+ build.mrbc.run f, rbfiles, "gem_mrblib_#{funcname}_proc", **opts
else
- build.mrbc.run f, rbfiles, "gem_mrblib_irep_#{funcname}", false
+ build.mrbc.run f, rbfiles, "gem_mrblib_irep_#{funcname}", **opts
end
end
f.puts %Q[void mrb_#{funcname}_gem_init(mrb_state *mrb);]

MODIFY
mrbgems/mruby-bin-mrbc/tools/mrbc/mrbc.c
mrbgems/mruby-bin-mrbc/tools/mrbc/mrbc.c
#define C_EXT ".c"
struct mrbc_args {
- int argc;
- char **argv;
- int idx;
const char *prog;
const char *outfile;
const char *initname;
+ char **argv;
+ int argc;
+ int idx;
mrb_bool dump_struct : 1;
mrb_bool check_syntax : 1;
mrb_bool verbose : 1;
mrb_bool remove_lv : 1;
- unsigned int flags : 4;
+ uint8_t flags : 4;
};
static void
@@ -38,6 +38,7 @@ usage(const char *name)
"-g produce debugging information",
"-B<symbol> binary <symbol> output in C language format",
"-S dump C struct (requires -B)",
+ "-s define <symbol> as static variable",
"--remove-lv remove local variables",
"--verbose run at verbose mode",
"--version print the version",
@@ -131,7 +132,10 @@ parse_args(mrb_state *mrb, int argc, char **argv, struct mrbc_args *args)
args->verbose = TRUE;
break;
case 'g':
- args->flags |= DUMP_DEBUG_INFO;
+ args->flags |= MRB_DUMP_DEBUG_INFO;
+ break;
+ case 's':
+ args->flags |= MRB_DUMP_STATIC;
break;
case 'E':
case 'e':

MODIFY
mrbgems/mruby-test/mrbgem.rake
mrbgems/mruby-test/mrbgem.rake
@@ -20,7 +20,7 @@ MRuby::Gem::Specification.new('mruby-test') do |spec|
_pp "GEN", t.name.relative_path
mkdir_p File.dirname(t.name)
open(t.name, 'w') do |f|
- mrbc.run f, assert_rb, 'mrbtest_assert_irep', false
+ mrbc.run f, assert_rb, 'mrbtest_assert_irep', cdump: false
end
end
@@ -52,10 +52,10 @@ MRuby::Gem::Specification.new('mruby-test') do |spec|
if test_preload.nil?
f.puts %Q[extern const uint8_t mrbtest_assert_irep[];]
else
- g.build.mrbc.run f, test_preload, "gem_test_irep_#{g.funcname}_preload", false
+ g.build.mrbc.run f, test_preload, "gem_test_irep_#{g.funcname}_preload", cdump: false
end
g.test_rbfiles.flatten.each_with_index do |rbfile, i|
- g.build.mrbc.run f, rbfile, "gem_test_irep_#{g.funcname}_#{i}", false
+ g.build.mrbc.run f, rbfile, "gem_test_irep_#{g.funcname}_#{i}", cdump: false, static: true
end
f.puts %Q[void mrb_#{g.funcname}_gem_test(mrb_state *mrb);] if g.custom_test_init?
dep_list.each do |d|

MODIFY
src/dump.c
src/dump.c
@@ -806,7 +806,7 @@ dump_irep(mrb_state *mrb, const mrb_irep *irep, uint8_t flags, uint8_t **bin, si
section_irep_size += get_irep_record_size(mrb, irep);
/* DEBUG section size */
- if (flags & DUMP_DEBUG_INFO) {
+ if (flags & MRB_DUMP_DEBUG_INFO) {
if (debug_info_defined) {
section_lineno_size += sizeof(struct rite_section_debug_header);
/* filename table */
@@ -842,7 +842,7 @@ dump_irep(mrb_state *mrb, const mrb_irep *irep, uint8_t flags, uint8_t **bin, si
sizeof(struct rite_binary_footer);
/* write DEBUG section */
- if (flags & DUMP_DEBUG_INFO) {
+ if (flags & MRB_DUMP_DEBUG_INFO) {
if (debug_info_defined) {
result = write_section_debug(mrb, irep, cur, filenames, filenames_len);
if (result != MRB_DUMP_OK) {
@@ -920,11 +920,13 @@ mrb_dump_irep_cfunc(mrb_state *mrb, const mrb_irep *irep, uint8_t flags, FILE *f
return MRB_DUMP_WRITE_FAULT;
}
if (fprintf(fp,
- "#ifdef __cplusplus\n"
- "extern const uint8_t %s[];\n"
- "#endif\n"
+ "%s\n"
"const uint8_t %s[] = {",
- initname, initname) < 0) {
+ (flags & MRB_DUMP_STATIC) ? "static"
+ : "#ifdef __cplusplus\n"
+ "extern\n"
+ "#endif",
+ initname) < 0) {
mrb_free(mrb, bin);
return MRB_DUMP_WRITE_FAULT;
}
@@ -1232,8 +1234,14 @@ mrb_dump_irep_cstruct(mrb_state *mrb, const mrb_irep *irep, uint8_t flags, FILE
int max = 1;
int n = dump_irep_struct(mrb, irep, flags, fp, initname, 0, init_syms_code, &max);
if (n != MRB_DUMP_OK) return n;
- fprintf(fp, "#ifdef __cplusplus\nextern const struct RProc %s[];\n#endif\n", initname);
- fprintf(fp, "const struct RProc %s[] = {{\n", initname);
+ fprintf(fp,
+ "%s\n"
+ "const struct RProc %s[] = {{\n",
+ (flags & MRB_DUMP_STATIC) ? "static"
+ : "#ifdef __cplusplus\n"
+ "extern\n"
+ "#endif",
+ initname);
fprintf(fp, "NULL,NULL,MRB_TT_PROC,7,0,{&%s_irep_0},NULL,{NULL},\n}};\n", initname);
fputs("static void\n", fp);
fprintf(fp, "%s_init_syms(mrb_state *mrb)\n", initname);

MODIFY
tasks/mrblib.rake
tasks/mrblib.rake
@@ -28,7 +28,7 @@ MRuby.each_target do
f.puts %Q[#include <mruby.h>]
f.puts %Q[#include <mruby/irep.h>]
end
- mrbc.run f, rbfiles, "mrblib_#{suffix}", cdump
+ mrbc.run f, rbfiles, "mrblib_#{suffix}", cdump: cdump, static: true
f.puts %Q[void]
f.puts %Q[mrb_init_mrblib(mrb_state *mrb)]
f.puts %Q[{]

Add `-s` option to `mrbc` for make variable static