summaryrefslogtreecommitdiffhomepage
path: root/Rakefile
diff options
context:
space:
mode:
authorKOBAYASHI Shuji <[email protected]>2020-12-13 15:27:53 +0900
committerKOBAYASHI Shuji <[email protected]>2020-12-13 15:27:53 +0900
commit456878ba06358a77d4ab9312fdc69bf780f8fdf4 (patch)
tree2c96fd5289d87e9b464517e7d30d406db7c1d04e /Rakefile
parent116e128b1a103e2fb246cc9d53b82246b24dbd40 (diff)
downloadmruby-456878ba06358a77d4ab9312fdc69bf780f8fdf4.tar.gz
mruby-456878ba06358a77d4ab9312fdc69bf780f8fdf4.zip
Improve source scanning for presym
The accuracy is greatly improved by using the C preprocessor to scan C sources for presym. C preprocessor can perfectly interpret all comments and preprocessor directives, so it can detect all symbols defined, for example `mrbgems/mruby-socket/src/const.cstub`. Also, as described later, this change will greatly improve the accuracy of presym detection from Ruby sources. ## Result The number of lines in the `presym` file for all gems is as follows: ```console Previous: 999 (false positive = 89, undetected = 297) New: 1207 ``` ## Build process The new build process (with presym) is as follows: 1. Build `mrbc` without presym (more on building without presym later). 2. Compile Ruby sources to C struct format with the `mrbc` created in step 1, and create` mrblib.c` and `gem_init.c`. Note that the symbols in the created files are output as `MRB_SYM` family macros or `mrb_intern_lit` instead of IDs (details will be described later). 3. C preprocessor processes C sources including the created files of step 2 and outputs them as `.i` files. In these files, for example, `MRB_IVSYM(foo)` is converted to `<@! "@" "foo" !@>` and `mrb_define_module(mrb, "Foo")` is converted to `<@! "Foo" !@>`. 4. Scan the files created in step 3 and create `presym` and` presym.inc` files. The files created in step 2 should output all static symbols defined in Ruby sources, including local variables, so we can detect all presyms by just scanning C sources without scanning Ruby sources directly. Further, by this process, the files to be scanned becomes the same as the files to be compiled, so that there is no excess or deficiency. ## Related changes The following changes have been made in relation to realizing this feature. ### Allow build without presym It enables build without presym to achieve the "Build process: 1". This incorporates #5202, see its issue for details. Note that when presym is enabled, even adding a local variable to a Ruby source may change contents of presym and require recompilation of almost all C sources. This is inconvenient, especially during trial and error in development, but this feature is also useful because it does not cause this problem if presym is disabled. ### Automatically create build target for `mrbc` without presym The `mrbc` used in the "Build process: 1" will be built by automatically creating a build target for it. The build name is `SOURCE_BUILD_NAME/mrbc`. ### Constantize output of C struct format by `mrbc` To realizing the "Build process: 2", as mentioned above, symbol IDs are not output directly in C struct format output by `mrbc`. As a result, the output becomes constant regardless of the state of presym at the time of `mrbc` build, and it is possible to detect symbols of Ruby sources in the same way as other C sources. Note that `mrb_intern_lit` is used for symbols that do not become presym, but in this state, the corresponding element in the symbol array cannot be statically initialized, so it is initialized at run time (therefore, in this case, the `const` qualifier is not added to the symbol array). ### Specify arbitrary `mrbc` file To realizing the "Build process: 2", enabled to specify `mrbc` created by another build target or pre-built` mrbc`. Use `MRuby::Build#mrbcfile =` to specify it explicitly. You can omit the "Build process: 1" by specifying pre-built `mrbc`, and you can always use an optimized build to compile Ruby sources faster. I think changes that affect the output of `mrbc` are rare, so in many cases it helps to improve efficiency. With presym, the build will be a little slower due to more build steps, but this feature will improve it a bit. ### Create presym files for each build target This feature was proposed at #5194 and merged once, but was reverted in 5c205e6e due to problems especially with cross-compilation. It has been introduced again because this change solves the problem. The presym files will be created below. * `build/NAME/presym` * `build/NAME/include/mruby/presym.inc` ### Other changes * Because presym detection accuracy is greatly improved as mentioned above, `MRuby::Gem::Specification#cdump?` is set to true by default, and `disable_cdump` is added instead of `enable_cdump`. Also, support for gem specific presym files has been discontinued (https://github.com/mruby/mruby/issues/5151#issuecomment-730967232). * Previously, `mrbc` was automatically created for the `host` build, but it will not be created if the build target for `mrbc` mentioned above is automatically created. At this time, `mrbc` file of the `mrbc` build is copied to` bin/`. * Two types of `.d` files will be created, `.o.d` and `.i.d`. oThis is because if `.i` depends on `presym.inc`, the dependency will circulate, so the `.d` file cannot be shared. * Changed file created with `enable_cxx_exception` to `X-cxx.cxx` from `X.cxx` to use the mruby standard Rake rule. ### Note Almost all C sources will need to be recompiled if there are any changes to `persym.inc` (if not recompiled properly, it will often result in run-time error). If `gcc` toolchain is used, dependencies are resolved by the `.d` file, so it become automatically recompile target, but if not (e.g. MSVC), it is necessary to manually make it recompile target. Also, even if `gcc` toolchain is used, it may not become recompile target if external gems does not use the mruby standard Rake rule. In particular, if the standard rule is overwritten, such as https://github.com/mruby/mruby/pull/5112/files, `.d` file will not be read, so be careful.
Diffstat (limited to 'Rakefile')
-rw-r--r--Rakefile227
1 files changed, 12 insertions, 215 deletions
diff --git a/Rakefile b/Rakefile
index e54983f41..813486bf6 100644
--- a/Rakefile
+++ b/Rakefile
@@ -25,12 +25,11 @@ end
# load custom rules
load "#{MRUBY_ROOT}/tasks/core.rake"
load "#{MRUBY_ROOT}/tasks/mrblib.rake"
-
load "#{MRUBY_ROOT}/tasks/mrbgems.rake"
load "#{MRUBY_ROOT}/tasks/libmruby.rake"
-
+load "#{MRUBY_ROOT}/tasks/bin.rake"
+load "#{MRUBY_ROOT}/tasks/presym.rake"
load "#{MRUBY_ROOT}/tasks/benchmark.rake"
-
load "#{MRUBY_ROOT}/tasks/gitlab.rake"
load "#{MRUBY_ROOT}/tasks/doc.rake"
@@ -38,219 +37,19 @@ load "#{MRUBY_ROOT}/tasks/doc.rake"
# generic build targets, rules
task :default => :all
-bin_path = ENV['INSTALL_DIR'] || "#{MRUBY_ROOT}/bin"
-
-if MRuby.targets['host']
- target = MRuby.targets['host']
- depfiles = target.bins.map do |bin|
- install_path = target.exefile("#{bin_path}/#{bin}")
- source_path = target.exefile("#{target.build_dir}/bin/#{bin}")
-
- file install_path => source_path do |t|
- install_D t.prerequisites.first, t.name
- end
- install_path
- end
-else
- depfiles = []
-end
-
-cfiles = Dir.glob("#{MRUBY_ROOT}/src/*.c")
-rbfiles = Dir.glob("#{MRUBY_ROOT}/mrblib/**/*.rb")
-psfiles = []
-MRuby.each_target do |target|
- gems.each do |gem|
- current_dir = gem.dir.relative_path_from(Dir.pwd)
- relative_from_root = gem.dir.relative_path_from(MRUBY_ROOT)
- current_build_dir = File.expand_path "#{build_dir}/#{relative_from_root}"
-
- if current_build_dir !~ /^#{Regexp.escape(build_dir)}/
- current_build_dir = "#{build_dir}/mrbgems/#{gem.name}"
- end
-
- gem.bins.each do |bin|
- exec = exefile("#{build_dir}/bin/#{bin}")
- objs = Dir.glob("#{current_dir}/tools/#{bin}/*.{c,cpp,cxx,cc}").map { |f| objfile(f.pathmap("#{current_build_dir}/tools/#{bin}/%n")) }
-
- file exec => objs + target.libraries do |t|
- gem_flags = gems.map { |g| g.linker.flags }
- gem_flags_before_libraries = gems.map { |g| g.linker.flags_before_libraries }
- gem_flags_after_libraries = gems.map { |g| g.linker.flags_after_libraries }
- gem_libraries = gems.map { |g| g.linker.libraries }
- gem_library_paths = gems.map { |g| g.linker.library_paths }
- linker.run t.name, t.prerequisites, gem_libraries, gem_library_paths, gem_flags, gem_flags_before_libraries, gem_flags_after_libraries
- end
-
- if target == MRuby.targets['host']
- install_path = MRuby.targets['host'].exefile("#{bin_path}/#{bin}")
-
- file install_path => exec do |t|
- install_D t.prerequisites.first, t.name
- end
- depfiles += [ install_path ]
- else
- depfiles += [ exec ]
- end
- end
-
- cfiles += Dir.glob(gem.dir+"/{src,core,tools/*}/*.c")
- if gem.cdump?
- rbfiles += Dir.glob(gem.dir+"/mrblib/**/*.rb")
- psfiles += Dir.glob(gem.dir+"/**/presym")
- end
- end
-end
-
-mkdir_p "#{MRUBY_ROOT}/build"
-symbols = []
-psfiles.each do |file|
- symbols += File.readlines(file).grep_v(/^# /)
-end
-symbols.each{|x| x.chomp!}
-presym_file="#{MRUBY_ROOT}/build/presym"
-presym_inc="#{presym_file}.inc"
-op_table = {
- "!" => "not",
- "!=" => "neq",
- "!~" => "nmatch",
- "%" => "mod",
- "&" => "and",
- "&&" => "andand",
- "*" => "mul",
- "**" => "pow",
- "+" => "add",
- "+@" => "plus",
- "-" => "sub",
- "-@" => "minus",
- "/" => "div",
- "<" => "lt",
- "<=" => "le",
- "<<" => "lshift",
- "<=>" => "cmp",
- "==" => "eq",
- "===" => "eqq",
- "=~" => "match",
- ">" => "gt",
- ">=" => "ge",
- ">>" => "rshift",
- "[]" => "aref",
- "[]=" => "aset",
- "^" => "xor",
- "`" => "tick",
- "|" => "or",
- "||" => "oror",
- "~" => "neg",
-}
-macro_to_symbol = {
-# Macro Symbol
-# [prefix, suffix] => [prefix, suffix]
- ["CV" , "" ] => ["@@" , "" ],
- ["IV" , "" ] => ["@" , "" ],
- ["" , "_B" ] => ["" , "!" ],
- ["" , "_Q" ] => ["" , "?" ],
- ["" , "_E" ] => ["" , "=" ],
- ["" , "" ] => ["" , "" ],
-}
-
-file presym_file => cfiles+rbfiles+psfiles+[__FILE__] do
- prefix_re = Regexp.union(*macro_to_symbol.keys.map(&:first).uniq)
- suffix_re = Regexp.union(*macro_to_symbol.keys.map(&:last).uniq)
- macro_re = /MRB_(#{prefix_re})SYM(#{suffix_re})\((\w+)\)/o
- csymbols = cfiles.map do |f|
- src = File.read(f)
- src.gsub!(/\/\/.+(\n|$)/, "\n")
- [src.scan(/intern_lit\([^\n"]*"([^\n "]*)"/),
- src.scan(/mrb_define_method\([^\n"]*"([^\n"]*)"/),
- src.scan(/mrb_define_class_method\([^\n"]*"([^\n"]*)"/),
- src.scan(/mrb_define_class\([^\n"]*"([^\n"]*)"/),
- src.scan(/mrb_define_module\([^\n"]*"([^\n"]*)"/),
- src.scan(/mrb_define_module_function\([^\n"]*"([^\n"]*)"/),
- src.scan(/mrb_define_const\([^\n"]*"([^\n"]*)"/),
- src.scan(/mrb_define_global_const\([^\n"]*"([^\n"]*)"/),
- src.scan(macro_re).map{|prefix, suffix, name|
- macro_to_symbol[[prefix, suffix]] * name
- }]
- end
- csymbols += File.readlines("#{MRUBY_ROOT}/include/mruby.h").grep(/define E_/).join.scan(/MRB_SYM\((\w+)\)/)
-
- rbsymbols = rbfiles.map do |f|
- src = File.read(f)
- src.force_encoding(Encoding::BINARY)
- [src.scan(/\bclass +([A-Z]\w*)/),
- src.scan(/\bmodule +([A-Z]\w*)/),
- src.scan(/\bdef +(\w+[!?=]?)/),
- src.scan(/\balias +(\w+[!?]?)/),
- src.scan(/\b([A-Z]\w*) *=[^=]/),
- src.scan(/(\$[a-zA-Z_]\w*)/),
- src.scan(/(\$[$!?0-9]\w*)/),
- src.scan(/(@@?[a-zA-Z_]\w*)/),
- src.scan(/[^.]\.([a-zA-Z_]\w*[!?]?)/),
- src.scan(/\.([a-zA-Z_]\w* *=)/).map{|x|x.map{|s|s.gsub(' ', '')}},
- src.scan(/\b([a-zA-Z_]\w*):/),
- src.scan(/:([a-zA-Z_]\w*[!?=]?)/),
- src.scan(/[\(\[\{ ]:"([^"]+)"/).map{|x|x.map{|s|s.gsub('\#', '#')}},
- src.scan(/[ \(\[\{]:'([^']+)'/)
- ]
- end
- symbols = (symbols+csymbols+rbsymbols+op_table.keys).flatten.compact.uniq.grep_v(/#/).map{|x| x.gsub("\n", '\n')}.sort_by!{|x| [x.bytesize, x]}
- presyms = File.readlines(presym_file) rescue []
- presyms.each{|x| x.chomp!}
- if presyms != symbols
- _pp "GEN", presym_file.relative_path
- File.write(presym_file, symbols.join("\n"))
- Rake::Task[presym_inc].invoke
- end
-end
-
-task presym_inc do
- presyms = File.readlines(presym_file)
- presyms.each{|x| x.chomp!}
- symbol_to_macro = macro_to_symbol.invert
- prefix_re = Regexp.union(*symbol_to_macro.keys.map(&:first).uniq)
- suffix_re = Regexp.union(*symbol_to_macro.keys.map(&:last).uniq)
- sym_re = /\A(#{prefix_re})?([\w&&\D]\w*)(#{suffix_re})?\z/o
- _pp "GEN", presym_inc.relative_path
- File.open(presym_inc, "w") do |f|
- f.puts "/* MRB_PRESYM_NAMED(lit, num, type, name) */"
- f.puts "/* MRB_PRESYM_UNNAMED(lit, num) */"
- presyms.each.with_index(1) do |sym, num|
- if sym_re =~ sym && (affixes = symbol_to_macro[[$1, $3]])
- f.puts %|MRB_PRESYM_NAMED("#{sym}", #{num}, #{affixes * 'SYM'}, #{$2})|
- elsif name = op_table[sym]
- f.puts %|MRB_PRESYM_NAMED("#{sym}", #{num}, OPSYM, #{name})|
- elsif
- f.puts %|MRB_PRESYM_UNNAMED("#{sym}", #{num})|
- end
- end
- f.print "#define MRB_PRESYM_MAX #{presyms.size}"
- end
-end
-
-desc "preallocated symbols"
-task :gensym => presym_file
-
-depfiles += MRuby.targets.map { |n, t|
- t.libmruby_enabled? ? t.libraries : t.libmruby_core_static
-}.flatten
-
-depfiles += MRuby.targets.reject { |n, t| n == 'host' }.map { |n, t|
- t.bins.map { |bin| t.exefile("#{t.build_dir}/bin/#{bin}") }
-}.flatten
-
desc "build all targets, install (locally) in-repo"
-task :all => :build do
+task :all => :gensym do
+ Rake::Task[:build].invoke
puts
puts "Build summary:"
puts
- MRuby.each_target do
- print_build_summary
+ MRuby.each_target do |build|
+ build.print_build_summary
end
MRuby::Lockfile.write
end
-task :build => :gensym do
- depfiles.each {|dep| Rake::Task[dep].invoke}
-end
+task :build => MRuby.targets.flat_map{|_, build| build.products}
desc "run all mruby tests"
task :test
@@ -274,19 +73,17 @@ end
desc "clean all built and in-repo installed artifacts"
task :clean do
- MRuby.each_target do |t|
- rm_rf t.build_dir
+ MRuby.each_target do |build|
+ rm_rf build.products
+ rm_rf build.build_dir
end
- rm_f depfiles
- rm_f presym_file
- rm_f presym_inc
puts "Cleaned up target build folder"
end
desc "clean everything!"
task :deep_clean => ["clean", "clean_doc"] do
- MRuby.each_target do |t|
- rm_rf t.gem_clone_dir
+ MRuby.each_target do |build|
+ rm_rf build.gem_clone_dir
end
puts "Cleaned up mrbgems build folder"
end