diff options
| author | KOBAYASHI Shuji <[email protected]> | 2020-12-13 15:27:53 +0900 |
|---|---|---|
| committer | KOBAYASHI Shuji <[email protected]> | 2020-12-13 15:27:53 +0900 |
| commit | 456878ba06358a77d4ab9312fdc69bf780f8fdf4 (patch) | |
| tree | 2c96fd5289d87e9b464517e7d30d406db7c1d04e /tasks | |
| parent | 116e128b1a103e2fb246cc9d53b82246b24dbd40 (diff) | |
| download | mruby-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 'tasks')
| -rw-r--r-- | tasks/benchmark.rake | 2 | ||||
| -rw-r--r-- | tasks/bin.rake | 35 | ||||
| -rw-r--r-- | tasks/core.rake | 5 | ||||
| -rw-r--r-- | tasks/libmruby.rake | 4 | ||||
| -rw-r--r-- | tasks/mrblib.rake | 30 | ||||
| -rw-r--r-- | tasks/presym.rake | 41 | ||||
| -rw-r--r-- | tasks/toolchains/gcc.rake | 4 | ||||
| -rw-r--r-- | tasks/toolchains/openwrt.rake | 23 | ||||
| -rw-r--r-- | tasks/toolchains/visualcpp.rake | 38 |
9 files changed, 131 insertions, 51 deletions
diff --git a/tasks/benchmark.rake b/tasks/benchmark.rake index 6f0b0ef6a..12e0d4602 100644 --- a/tasks/benchmark.rake +++ b/tasks/benchmark.rake @@ -50,7 +50,7 @@ end MRuby.each_target do |target| - next if target.name == 'host' + next if target.name == 'host' || target.internal? mruby_bin = "#{target.build_dir}/bin/mruby" bm_files.each do |bm_file| diff --git a/tasks/bin.rake b/tasks/bin.rake new file mode 100644 index 000000000..afef065a1 --- /dev/null +++ b/tasks/bin.rake @@ -0,0 +1,35 @@ +install_task = ->(src) do + dst = "#{MRuby::Build.install_dir}/#{File.basename(src)}" + file dst => src do + install_D src, dst + end + dst +end + +MRuby.each_target do |build| + if build.host? && build.mrbc_build && !build.gems["mruby-bin-mrbc"] + exe = build.exefile("#{build.mrbc_build.build_dir}/bin/mrbc") + build.products << install_task.(exe) + end + + build.bins.each do |bin| + exe = build.exefile("#{build.build_dir}/bin/#{bin}") + build.products << (build.host? ? install_task.(exe) : exe) + end + + linker_attrs = build.gems.map{|gem| gem.linker.run_attrs}.transpose + build.gems.each do |gem| + gem.bins.each do |bin| + exe = build.exefile("#{build.build_dir}/bin/#{bin}") + objs = Dir["#{gem.dir}/tools/#{bin}/*.{c,cpp,cxx,cc}"].map do |f| + build.objfile(f.pathmap("#{gem.build_dir}/tools/#{bin}/%n")) + end + + file exe => objs.concat(build.libraries) do |t| + build.linker.run t.name, t.prerequisites, *linker_attrs + end + + build.products << (build.host? ? install_task.(exe) : exe) + end + end +end diff --git a/tasks/core.rake b/tasks/core.rake index aca5faed8..a47722e8b 100644 --- a/tasks/core.rake +++ b/tasks/core.rake @@ -2,11 +2,10 @@ as_cxx_srcs = %w[vm error gc].map{|name| "#{MRUBY_ROOT}/src/#{name}.c"} MRuby.each_target do objs = Dir.glob("#{MRUBY_ROOT}/src/*.c").map do |src| - dst = src.pathmap("#{build_dir}/src/%n") if cxx_exception_enabled? && as_cxx_srcs.include?(src) - compile_as_cxx(src, "#{dst}.cxx") + compile_as_cxx(src) else - objfile(dst) + objfile(src.pathmap("#{build_dir}/src/%n")) end end self.libmruby_core_objs << objs diff --git a/tasks/libmruby.rake b/tasks/libmruby.rake index c73b25d3b..23cd992ff 100644 --- a/tasks/libmruby.rake +++ b/tasks/libmruby.rake @@ -3,6 +3,8 @@ MRuby.each_target do archiver.run t.name, t.prerequisites end + products << libmruby_core_static + next unless libmruby_enabled? file libmruby_static => libmruby_objs.flatten do |t| @@ -27,4 +29,6 @@ MRuby.each_target do f.puts "MRUBY_LIBMRUBY_PATH = #{libmruby_static}" end end + + products << libmruby_static end diff --git a/tasks/mrblib.rake b/tasks/mrblib.rake index a7f592593..5567515d6 100644 --- a/tasks/mrblib.rake +++ b/tasks/mrblib.rake @@ -2,13 +2,18 @@ MRuby.each_target do next unless libmruby_enabled? src = "#{build_dir}/mrblib/mrblib.c" - obj = objfile(src.ext) rbfiles = Dir["#{MRUBY_ROOT}/mrblib/*.rb"].sort! - self.libmruby_objs << obj + self.libmruby_objs << objfile(src.ext) - file obj => src file src => [mrbcfile, __FILE__, *rbfiles] do |t| + if presym_enabled? + cdump = true + suffix = "proc" + else + cdump = false + suffix = "irep" + end mkdir_p File.dirname(t.name) File.open(t.name, 'w') do |f| _pp "GEN", "mrblib/*.rb", "#{t.name.relative_path}" @@ -19,14 +24,17 @@ MRuby.each_target do f.puts %Q[ * This file was generated!] f.puts %Q[ * All manual changes will get lost.] f.puts %Q[ */] - mrbc.run f, rbfiles, 'mrblib_proc' - f.puts <<INIT_END -void -mrb_init_mrblib(mrb_state *mrb) -{ - mrb_load_proc(mrb, mrblib_proc); -} -INIT_END + unless presym_enabled? + f.puts %Q[#include <mruby.h>] + f.puts %Q[#include <mruby/irep.h>] + end + mrbc.run f, rbfiles, "mrblib_#{suffix}", cdump + f.puts %Q[void] + f.puts %Q[mrb_init_mrblib(mrb_state *mrb)] + f.puts %Q[{] + f.puts %Q[ mrblib_#{suffix}_init_syms(mrb);] if cdump + f.puts %Q[ mrb_load_#{suffix}(mrb, mrblib_#{suffix});] + f.puts %Q[}] end end end diff --git a/tasks/presym.rake b/tasks/presym.rake new file mode 100644 index 000000000..f3a076ac6 --- /dev/null +++ b/tasks/presym.rake @@ -0,0 +1,41 @@ +all_prerequisites = ->(task_name, prereqs) do + Rake::Task[task_name].prerequisites.each do |prereq_name| + next if prereqs[prereq_name] + prereqs[prereq_name] = true + all_prerequisites.(Rake::Task[prereq_name].name, prereqs) + end +end + +MRuby.each_target do |build| + gensym_task = task(:gensym) + next unless build.presym_enabled? + + presym = build.presym + + include_dir = "#{build.build_dir}/include" + build.compilers.each{|c| c.include_paths << include_dir} + build.gems.each{|gem| gem.compilers.each{|c| c.include_paths << include_dir}} + + prereqs = {} + pps = [] + mrbtest = "#{build.class.install_dir}/mrbtest" + mrbc_build_dir = "#{build.mrbc_build.build_dir}/" if build.mrbc_build + build.products.each do |product| + all_prerequisites.(product, prereqs) unless product == mrbtest + end + prereqs.each_key do |prereq| + next unless File.extname(prereq) == build.exts.object + next if mrbc_build_dir && prereq.start_with?(mrbc_build_dir) + pps << prereq.ext(build.exts.preprocessed) + end + + file presym.list_path => pps do + presyms = presym.scan(pps) + current_presyms = presym.read_list if File.exist?(presym.list_path) + update = presyms != current_presyms + presym.write_list(presyms) if update + presym.write_header(presyms) if update || !File.exist?(presym.header_path) + end + + gensym_task.enhance([presym.list_path]) +end diff --git a/tasks/toolchains/gcc.rake b/tasks/toolchains/gcc.rake index 316d2d9a1..8714e1854 100644 --- a/tasks/toolchains/gcc.rake +++ b/tasks/toolchains/gcc.rake @@ -3,6 +3,7 @@ MRuby::Toolchain.new(:gcc) do |conf, params| compiler_flags = %w(-g -O3 -Wall -Wundef) c_mandatory_flags = %w(-std=gnu99) cxx_invalid_flags = %w(-Werror-implicit-function-declaration) + compile_opt = '%{flags} -MMD -MF "%{outfile}.d" -o "%{outfile}" "%{infile}"' [conf.cc, conf.objc, conf.asm, conf.cxx].each do |compiler| if compiler == conf.cxx @@ -14,7 +15,8 @@ MRuby::Toolchain.new(:gcc) do |conf, params| end compiler.option_include_path = %q[-I"%s"] compiler.option_define = '-D%s' - compiler.compile_options = %q[%{flags} -MMD -o "%{outfile}" -c "%{infile}"] + compiler.compile_options = "-c #{compile_opt}" + compiler.preprocess_options = "-E -P #{compile_opt}" compiler.cxx_compile_flag = '-x c++ -std=gnu++03' compiler.cxx_exception_flag = '-fexceptions' compiler.cxx_invalid_flags = c_mandatory_flags + cxx_invalid_flags diff --git a/tasks/toolchains/openwrt.rake b/tasks/toolchains/openwrt.rake index c376d96ec..d5763d8de 100644 --- a/tasks/toolchains/openwrt.rake +++ b/tasks/toolchains/openwrt.rake @@ -1,24 +1,19 @@ # usage of environmental variables to set the # cross compiling toolchain proper MRuby::Toolchain.new(:openwrt) do |conf| - [conf.cc, conf.objc, conf.asm].each do |cc| - cc.command = ENV['TARGET_CC'] - cc.flags = ENV['TARGET_CFLAGS'] - cc.include_paths = ["#{MRUBY_ROOT}/include"] + [conf.cc, conf.cxx, conf.objc, conf.asm].each do |cc| + if cc == conf.cxx + cc.command = ENV['TARGET_CXX'] + cc.flags = ENV['TARGET_CXXFLAGS'] + else + cc.command = ENV['TARGET_CC'] + cc.flags = ENV['TARGET_CFLAGS'] + end cc.option_include_path = %q[-I"%s"] cc.option_define = '-D%s' - cc.compile_options = %q[%{flags} -MMD -o "%{outfile}" -c "%{infile}"] + cc.compile_options = %q[%{flags} -MMD -MF "%{outfile}.d" -o "%{outfile}" -c "%{infile}"] end - [conf.cxx].each do |cxx| - cxx.command = ENV['TARGET_CXX'] - cxx.flags = ENV['TARGET_CXXFLAGS'] - cxx.include_paths = ["#{MRUBY_ROOT}/include"] - cxx.option_include_path = %q[-I"%s"] - cxx.option_define = '-D%s' - cxx.compile_options = %q[%{flags} -MMD -o "%{outfile}" -c "%{infile}"] - end - conf.linker do |linker| linker.command = ENV['TARGET_CC'] linker.flags = ENV['TARGET_LDFLAGS'] diff --git a/tasks/toolchains/visualcpp.rake b/tasks/toolchains/visualcpp.rake index 5094495e3..00e364082 100644 --- a/tasks/toolchains/visualcpp.rake +++ b/tasks/toolchains/visualcpp.rake @@ -1,25 +1,21 @@ MRuby::Toolchain.new(:visualcpp) do |conf, _params| - conf.cc do |cc| - cc.command = ENV['CC'] || 'cl.exe' - # C4013: implicit function declaration - cc.flags = [ENV['CFLAGS'] || %w(/c /nologo /W3 /we4013 /Zi /MD /O2 /D_CRT_SECURE_NO_WARNINGS)] - cc.defines = %w(MRB_STACK_EXTEND_DOUBLING) - cc.option_include_path = %q[/I"%s"] - cc.option_define = '/D%s' - cc.compile_options = %Q[%{flags} /Fo"%{outfile}" "%{infile}"] - cc.cxx_compile_flag = '/TP' - cc.cxx_exception_flag = '/EHs' - end - - conf.cxx do |cxx| - cxx.command = ENV['CXX'] || 'cl.exe' - cxx.flags = [ENV['CXXFLAGS'] || ENV['CFLAGS'] || %w(/c /nologo /W3 /Zi /MD /O2 /EHs /D_CRT_SECURE_NO_WARNINGS)] - cxx.defines = %w(MRB_STACK_EXTEND_DOUBLING) - cxx.option_include_path = %q[/I"%s"] - cxx.option_define = '/D%s' - cxx.compile_options = %Q[%{flags} /Fo"%{outfile}" "%{infile}"] - cxx.cxx_compile_flag = '/TP' - cxx.cxx_exception_flag = '/EHs' + compiler_flags = %w(/nologo /W3 /MD /O2 /D_CRT_SECURE_NO_WARNINGS) + [conf.cc, conf.cxx].each do |compiler| + if compiler == conf.cc + compiler.command = ENV['CC'] || 'cl.exe' + # C4013: implicit function declaration + compiler.flags = [*(ENV['CFLAGS'] || compiler_flags + %w(/we4013))] + else + compiler.command = ENV['CXX'] || 'cl.exe' + compiler.flags = [*(ENV['CXXFLAGS'] || ENV['CFLAGS'] || compiler_flags + %w(/EHs))] + end + compiler.defines = %w(MRB_STACK_EXTEND_DOUBLING) + compiler.option_include_path = %q[/I"%s"] + compiler.option_define = '/D%s' + compiler.compile_options = %Q[/Zi /c /Fo"%{outfile}" %{flags} "%{infile}"] + compiler.preprocess_options = %Q[/EP %{flags} "%{infile}" > "%{outfile}"] + compiler.cxx_compile_flag = '/TP' + compiler.cxx_exception_flag = '/EHs' end conf.linker do |linker| |
