Skip to content

Commit

Permalink
WIP: use the native toolchain
Browse files Browse the repository at this point in the history
  • Loading branch information
eregon committed Oct 25, 2023
1 parent fded3d2 commit db58b32
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 20 deletions.
18 changes: 12 additions & 6 deletions lib/mri/mkmf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
end
end

if defined?(::TruffleRuby) and Truffle::Boot.get_option('cexts-prepend-toolchain-to-path')
if defined?(::TruffleRuby) and Truffle::Boot.get_option('cexts-prepend-toolchain-to-path') and Truffle::Boot.get_option('cexts-sulong')
ENV['PATH'] = "#{RbConfig::CONFIG['toolchain_path']}:#{ENV['PATH']}"
end

Expand Down Expand Up @@ -2346,7 +2346,7 @@ def depend_rules(depend)
# +VPATH+ and added to the list of +INCFLAGS+.
#
def create_makefile(target, srcprefix = nil)
if defined?(::TruffleRuby) and ($LIBRUBYARG == nil or !Truffle::Boot.get_option('building-core-cexts'))
if defined?(::TruffleRuby) and Truffle::Boot.get_option('cexts-sulong') and ($LIBRUBYARG == nil or !Truffle::Boot.get_option('building-core-cexts'))
# $LIBRUBYARG was explicitly unset, the built library is not a C extension but used with FFI (e.g., sassc does).
# Since $LIBRUBYARG is unset we won't link to libgraalvm-llvm.so, which is expected.
# In the case the library uses C++ code, libc++.so/libc++abi.so will be linked and needs to be found by NFI.
Expand Down Expand Up @@ -2894,10 +2894,16 @@ def MAIN_DOES_NOTHING(*refs)
# We need to link to libtruffleruby for MakeMakefile#try_link to succeed.
# The created executable will link against both libgraalvm-llvm.so and libtruffleruby and
# might be executed for #try_constant and #try_run so we also need -rpath for both.
libtruffleruby_dir = File.dirname(RbConfig::CONFIG['libtruffleruby'])
TRY_LINK << " -L#{libtruffleruby_dir} -rpath #{libtruffleruby_dir} -ltruffleruby"
libgraalvm_llvm_dir = ::Truffle::Boot.toolchain_paths(:LD_LIBRARY_PATH)
TRY_LINK << " -rpath #{libgraalvm_llvm_dir}"
# TODO: could likely always use the second branch here
if Truffle::Boot.get_option('cexts-sulong')
libtruffleruby_dir = File.dirname(RbConfig::CONFIG['libtruffleruby'])
TRY_LINK << " -L#{libtruffleruby_dir} -rpath #{libtruffleruby_dir} -ltruffleruby"
libgraalvm_llvm_dir = ::Truffle::Boot.toolchain_paths(:LD_LIBRARY_PATH)
TRY_LINK << " -rpath #{libgraalvm_llvm_dir}"
else
libtrufflerubytrampoline_dir = File.dirname(RbConfig::CONFIG['libtrufflerubytrampoline'])
TRY_LINK << " -L#{libtrufflerubytrampoline_dir} -Wl,-rpath,#{libtrufflerubytrampoline_dir} -ltrufflerubytrampoline"
end
end

##
Expand Down
47 changes: 37 additions & 10 deletions lib/truffle/rbconfig.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ module RbConfig
raise 'The TruffleRuby home needs to be set to require RbConfig' unless ruby_home
TOPDIR = ruby_home

sulong = Truffle::Boot.get_option('cexts-sulong')

host_os = Truffle::System.host_os
host_cpu = Truffle::System.host_cpu
host_vendor = 'unknown'
Expand All @@ -61,26 +63,48 @@ module RbConfig
prefix = ruby_home
rubyhdrdir = "#{prefix}/lib/cext/include"

ar = Truffle::Boot.toolchain_executable(:AR)
cc = Truffle::Boot.toolchain_executable(:CC)
cxx = Truffle::Boot.toolchain_executable(:CXX)
ranlib = Truffle::Boot.toolchain_executable(:RANLIB)
strip = Truffle::Boot.toolchain_executable(:STRIP)
if sulong
ar = Truffle::Boot.toolchain_executable(:AR)
cc = Truffle::Boot.toolchain_executable(:CC)
cxx = Truffle::Boot.toolchain_executable(:CXX)
ranlib = Truffle::Boot.toolchain_executable(:RANLIB)
strip = Truffle::Boot.toolchain_executable(:STRIP)

strip = "#{strip} --keep-section=.llvmbc" unless Truffle::Platform.darwin?
strip = "#{strip} --keep-section=.llvmbc" unless Truffle::Platform.darwin?
gcc, clang = false, true
else
if Truffle::Platform.linux?
ar = 'gcc-ar'
cc = 'gcc' # -std=gnu99 ?
cxx = 'g++'
ranlib = 'gcc-ranlib'
strip = 'strip -S -x'
gcc, clang = true, false
elsif Truffle::Platform.darwin?
ar = 'ar'
cc = 'clang -fdeclspec'
cxx = 'clang++ -fdeclspec'
ranlib = 'ranlib'
strip = 'strip -A -n'
gcc, clang = false, true
else
raise 'Unknown platform'
end
end

# Determine the various flags for native compilation
optflags = ''
debugflags = ''
optflags = sulong ? '' : '-O3 -fno-fast-math'
debugflags = sulong ? '' : '-ggdb3'
warnflags = [
'-Werror=implicit-function-declaration', # https://bugs.ruby-lang.org/issues/18615
'-Wno-int-conversion', # MRI has VALUE defined as long while we have it as void*
'-Wno-int-to-pointer-cast', # Same as above
'-Wno-incompatible-pointer-types', # Fix byebug 8.2.1 compile (st_data_t error)
'-Wno-format-invalid-specifier', # Our PRIsVALUE generates this because compilers ignore printf extensions
'-Wno-format-extra-args', # Our PRIsVALUE generates this because compilers ignore printf extensions
'-ferror-limit=500'
]
warnflags << '-Wno-format-invalid-specifier' if clang # Our PRIsVALUE generates this because compilers ignore printf extensions
# TODO fix it, happens in openssl
warnflags << '-Wno-discarded-qualifiers' if gcc

defs = ''
cppflags = ''
Expand All @@ -107,6 +131,7 @@ module RbConfig
# Set extra flags needed for --building-core-cexts
if Truffle::Boot.get_option 'building-core-cexts'
libtruffleruby = "#{ruby_home}/src/main/c/cext/libtruffleruby.#{soext}"
libtrufflerubytrampoline = "#{ruby_home}/src/main/c/cext-trampoline/libtrufflerubytrampoline.#{soext}"

relative_debug_paths = " -fdebug-prefix-map=#{ruby_home}=."
cppflags << relative_debug_paths
Expand All @@ -115,6 +140,7 @@ module RbConfig
warnflags << '-Werror' # Make sure there are no warnings in core C extensions
else
libtruffleruby = "#{cext_dir}/libtruffleruby.#{soext}"
libtrufflerubytrampoline = "#{cext_dir}/libtrufflerubytrampoline.#{soext}"
end

# We do not link to libtruffleruby here to workaround GR-29448
Expand Down Expand Up @@ -166,6 +192,7 @@ module RbConfig
'LIBRUBY_SO' => "cext/libtruffleruby.#{soext}",
'LIBS' => libs,
'libtruffleruby' => libtruffleruby,
'libtrufflerubytrampoline' => libtrufflerubytrampoline,
'MAKEDIRS' => 'mkdir -p',
'MKDIR_P' => 'mkdir -p',
'NULLCMD' => ':',
Expand Down
4 changes: 2 additions & 2 deletions src/main/c/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ cext-trampoline/trampoline.c: $(CEXT_C_FILES) $(BASIC_DEPS) $(ROOT)/tool/generat
$(TRAMPOLINE): cext-trampoline/Makefile cext-trampoline/trampoline.c cext-trampoline/*.c Makefile
$(Q) cd cext-trampoline && $(MAKE)

# libtruffleruby
# libtruffleruby, must be compiled with the GraalVM LLVM Toolchain as it needs to run on Sulong
cext/Makefile: cext/extconf.rb $(BASIC_EXTCONF_DEPS) $(TRAMPOLINE)
$(Q) cd cext && $(RUBY) extconf.rb || $(IF_EXTCONF_FAIL)
$(Q) cd cext && $(RUBY) --experimental-options --cexts-sulong extconf.rb || $(IF_EXTCONF_FAIL)

$(LIBTRUFFLERUBY): cext/Makefile cext/*.c cext/*.h
$(Q) cd cext && $(MAKE)
Expand Down
2 changes: 2 additions & 0 deletions src/main/c/cext-trampoline/st.c
Original file line number Diff line number Diff line change
Expand Up @@ -2044,6 +2044,7 @@ st_numhash(st_data_t n)
return (st_index_t)((n>>s1|(n<<s2)) ^ (n>>s2));
}

#ifndef TRUFFLERUBY
/* Expand TAB to be suitable for holding SIZ entries in total.
Pre-existing entries remain not deleted inside of TAB, but its bins
are cleared to expect future reconstruction. See rehash below. */
Expand Down Expand Up @@ -2251,3 +2252,4 @@ rb_st_nth_key(st_table *tab, st_index_t index)
}

#endif
#endif /* TRUFFLERUBY */
2 changes: 2 additions & 0 deletions src/main/c/cext/st.c
Original file line number Diff line number Diff line change
Expand Up @@ -2044,6 +2044,7 @@ st_numhash(st_data_t n)
return (st_index_t)((n>>s1|(n<<s2)) ^ (n>>s2));
}

#ifndef TRUFFLERUBY
/* Expand TAB to be suitable for holding SIZ entries in total.
Pre-existing entries remain not deleted inside of TAB, but its bins
are cleared to expect future reconstruction. See rehash below. */
Expand Down Expand Up @@ -2251,3 +2252,4 @@ rb_st_nth_key(st_table *tab, st_index_t index)
}

#endif
#endif /* TRUFFLERUBY */
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
import org.truffleruby.core.exception.RubyException;
import org.truffleruby.core.string.StringOperations;
import org.truffleruby.core.string.StringUtils;
import org.truffleruby.language.RubyGuards;
import org.truffleruby.language.RubyRootNode;
import org.truffleruby.language.control.RaiseException;
import org.truffleruby.language.dispatch.DispatchNode;
import org.truffleruby.language.library.RubyStringLibrary;
import org.truffleruby.language.methods.TranslateExceptionNode;
import org.truffleruby.parser.RubySource;

Expand Down Expand Up @@ -137,7 +137,8 @@ public void printRubyExceptionOnEnvStderr(String info, AbstractTruffleException
"get_formatted_backtrace",
exceptionObject);
final String formatted = fullMessage != null
? RubyGuards.getJavaString(fullMessage)
// Use toJavaStringUncached() instead of RubyGuards.getJavaString() here so it still shows something if BINARY encoding and there are non-ASCII bytes
? RubyStringLibrary.getUncached().getTString(fullMessage).toJavaStringUncached()
: "<no message>";
if (formatted.endsWith("\n")) {
printer.print(formatted);
Expand Down

0 comments on commit db58b32

Please sign in to comment.