Skip to content
This repository has been archived by the owner on Aug 22, 2019. It is now read-only.

Add support for Rust test cases #817

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions .rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
max_width = 100
hard_tabs = false
tab_spaces = 4
newline_style = "Unix"
use_small_heuristics = "Default"
indent_style = "Block"
wrap_comments = false
comment_width = 80
normalize_comments = false
format_strings = false
empty_item_single_line = true
struct_lit_single_line = true
fn_single_line = false
where_single_line = false
imports_indent = "Block"
imports_layout = "Mixed"
merge_imports = false
reorder_imports = true
reorder_modules = true
reorder_impl_items = false
type_punctuation_density = "Wide"
space_before_colon = false
space_after_colon = true
spaces_around_ranges = false
binop_separator = "Front"
remove_nested_parens = true
combine_control_expr = true
struct_field_align_threshold = 0
match_arm_blocks = true
force_multiline_blocks = false
fn_args_density = "Tall"
brace_style = "SameLineWhere"
control_brace_style = "AlwaysSameLine"
trailing_semicolon = true
trailing_comma = "Vertical"
match_block_trailing_comma = false
blank_lines_upper_bound = 1
blank_lines_lower_bound = 0
merge_derives = true
use_try_shorthand = false
use_field_init_shorthand = false
force_explicit_abi = true
condense_wildcard_suffixes = false
color = "Auto"
required_version = "0.8.2"
unstable_features = false
disable_all_formatting = false
skip_children = false
hide_parse_errors = false
error_on_line_overflow = false
error_on_unformatted = false
report_todo = "Never"
report_fixme = "Never"
ignore = []
emit_mode = "Files"
make_backup = false
14 changes: 14 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ cache:
dist: trusty
sudo: required
language: generic
before_install:
# Travis does not auto-delete the rust-toolchain file
- rm rust-toolchain
install:
- cd ..
- gem install mdl
Expand All @@ -31,6 +34,7 @@ install:
- mx sforceimports
# the style gate needs the full commit history for checking copyright years
- test "$GATE_TAGS" != "style" || git fetch --unshallow
- test "$GATE_TAGS" != "style" || rustup component add rustfmt-preview --toolchain=$TRAVIS_RUST_VERSION
script:
- cd ../llvmDir
- ln -s /usr/bin/clang-$LLVM_VER clang
Expand All @@ -46,6 +50,8 @@ matrix:
fast_finish: true
include:
- env: LLVM_VER=3.8 GATE_TAGS='sulongBasic' COMPILE_COMMAND='mx build'
language: rust
rust: 1.19.0
addons:
apt:
packages:
Expand All @@ -56,6 +62,8 @@ matrix:
- libc++1
- libc++-dev
- env: LLVM_VER=3.9 GATE_TAGS='sulongBasic' COMPILE_COMMAND='mx build'
language: rust
rust: 1.27.1
addons:
apt:
sources:
Expand Down Expand Up @@ -108,6 +116,8 @@ matrix:
- libc++1
- libc++-dev
- env: LLVM_VER=3.8 GATE_TAGS='style'
language: rust
rust: nightly
addons:
apt:
packages:
Expand All @@ -119,6 +129,8 @@ matrix:
- libc++1
- libc++-dev
- env: LLVM_VER=3.8 GATE_TAGS='fullbuild'
language: rust
rust: 1.19.0
addons:
apt:
packages:
Expand Down Expand Up @@ -189,6 +201,8 @@ matrix:
- libc++1
- libc++-dev
- env: LLVM_VER=3.8 DOWNSTREAM_REPO='https://github.com/graalvm/graal.git' DOWNSTREAM_SUITE='vm' DYNAMIC_IMPORTS='sulong,/substratevm' NATIVE_IMAGE_TESTING='True' DOWNSTREAM_COMMAND='--disable-polyglot --disable-libpolyglot gate -B=--force-deprecation-as-warning --tags build,sulong'
language: rust
rust: 1.19.0
addons:
apt:
packages:
Expand Down
16 changes: 13 additions & 3 deletions README.md
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ GraalVM by [Oracle Labs](https://labs.oracle.com).
Sulong is written in Java and uses the Truffle language implementation
framework and Graal as a dynamic compiler.

With Sulong you can execute C/C++, Fortran, and other programming languages
With Sulong you can execute C/C++, Fortran, Rust and other programming languages
that can be transformed to LLVM bitcode on Graal VM. To execute a program,
you have to compile the program to LLVM bitcode by a LLVM front end such
as `clang`.
Expand Down Expand Up @@ -63,7 +63,7 @@ through the Graal Polyglot SDK.

#### Compiling to LLVM bitcode format

Graal VM can execute C/C++, Fortran, and other programs that can be compiled to
Graal VM can execute C/C++, Fortran, Rust and other programs that can be compiled to
LLVM bitcode. As a first step, you have to compile the program to LLVM bitcode
using an LLVM frontend such as `clang`. C/C++ code can be compiled to LLVM
bitcode using `clang` with the `-emit-llvm` option.
Expand Down Expand Up @@ -99,6 +99,11 @@ Sulong is mostly implemented in Java. However, parts of Sulong are
implemented in C/C++ and will be compiled to a shared library or a bitcode
file. For a successful build you need to have LLVM (incl. `CLANG` and `OPT`
tool) v3.8 - v6.0 installed.
Sulong also optionally depends on a Rust installation (refer to [Install Rust](https://www.rust-lang.org/en-US/install.html)). On Linux the recommended command line looks as follows:

curl https://sh.rustup.rs -sSf | sh -s -- --default-toolchain none

This will download the Rust toolchain installer `rustup`, which in turn will automatically install the correct Rust toolchain upon building Sulong. You can prohibit Sulong from using an available Rust installation by setting `SULONG_USE_RUSTC` to `false`.

MacOS: Apple's default LLVM does not contain the `opt` tool, which a Sulong
build needs. We recommend installing LLVM via `homebrew` and appending the
Expand All @@ -115,7 +120,7 @@ bitcode files, there are no special runtime dependencies, but additional
libraries might be needed if the user code has external dependencies.

In particular, for running C++ code, you need libc++ (the C++ standard
library from the LLVM project).
library from the LLVM project). For running Rust code you may need to link the Rust standard library `libstd`.

How to get started?
-------------------
Expand Down Expand Up @@ -174,6 +179,11 @@ multiple arguments separated by `:`.

mx lli -Dpolyglot.llvm.libraryPath=lib -Dpolyglot.llvm.libraries=liba.so test.bc

Executing Rust bitcode files may require linking the Rust standard library. Use the alias `lstdrust` to conveniently load the standard library of the currently active Rust toolchain (default: specified in the `rust-toolchain` file). Note that this may automatically install a missing Rust toolchain if the Rust version specified in the `rust-toolchain` file is unavailable:

rustc --emit=llvm-bc test.rs
mx lli -Dpolyglot.llvm.libraries=lstdrust test.bc

If you want to use the project from within Eclipse, use the following
command to generate the Eclipse project files (there is also mx ideinit
for other IDEs):
Expand Down
18 changes: 18 additions & 0 deletions docs/FAQ.md
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,21 @@ In Eclipse, set a breakpoint, navigate to
of the debug configurations, e.g.,`truffle-attach-localhost-8000`.
After clicking `Debug`, execution starts and the program should stop at
the specified breakpoint.

## Rust

### How can I fix the following warning I get when building Sulong: `Rust is not available, not building Rust test files`

Make sure that Rust is available on your system and that the `PATH` is configured to include the `.cargo/bin` directory of your Rust installation.

### How can I override the Rust version specified in the `rust-toolchain` file?

By default, Sulong uses the pinned Rust toolchain specified in the `rust-toolchain` file for compiling the Rust test cases and for linking the Rust standard library in the case that the library alias `lstdrust` is given. For instructions on how to override a `rust-toolchain` file, please refer to the `rustup` docs (-> [Directory overrides](https://github.com/rust-lang-nursery/rustup.rs/blob/master/README.md#directory-overrides)).

Here is an example showing how to temporarily override the current toolchain setting:

rustup install nightly
rustup run nightly rustc --emit=llvm-bc test.rs
rustup run nightly mx lli -Dpolyglot.llvm.libraries=lstdrust test.bc


10 changes: 8 additions & 2 deletions docs/TESTS.md
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ Please note that this command aborts as soon as one testsuite has failed.
| callback | CallbackTest | Test calling native functions |
| type | - | Test floating point arithmetic |

The test cases consist of LLVM IR, C, C++, and Fortran files. While
The test cases consist of LLVM IR, C, C++, Fortran and Rust files. While
Sulong's Truffle LLVM IR interpreter can directly execute the LLVM IR
files it uses Clang and/or GCC to compile the other source files to LLVM IR
files it uses a selection from amongst the tools Clang, GCC and rustc to compile the other source files to LLVM IR
before executing them.

### Testgate
Expand Down Expand Up @@ -133,6 +133,12 @@ DYLD_LIBRARY_PATH cannot normally be set on the Mac.

ln -s /usr/local/Cellar/gcc46/4.6.4/lib/gcc/4.6/libgfortran.3.dylib /usr/local/lib

## Rust

Some of our tests are Rust files. Make sure you have a recent version of the Rust toolchain installer `rustup` available. When doing a complete rebuild of Sulong, an attempt will be made to compile the Rust test files using the rustc of the currently active toolchain (default: specified in the `rust-toolchain` file). Note that this may automatically install a missing Rust toolchain if the Rust version specified in the `rust-toolchain` file is unavailable.

For unittests Sulong automatically links the Rust standard library of the currently active toolchain.

## Reference output

For most test cases, Sulong obtains the reference output (return value and/or
Expand Down
30 changes: 30 additions & 0 deletions mx.sulong/mx_buildtools.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def lookupFile(f):
ProgrammingLanguage.register('C', 'c')
ProgrammingLanguage.register('C_PLUS_PLUS', 'cpp', 'cc', 'C')
ProgrammingLanguage.register('OBJECTIVE_C', 'm')
ProgrammingLanguage.register('RUST', 'rs')
ProgrammingLanguage.register('LLVMIR', 'll')
ProgrammingLanguage.register('LLVMBC', 'bc')
ProgrammingLanguage.register('LLVMSU', 'su')
Expand Down Expand Up @@ -192,6 +193,33 @@ def compileReferenceFile(self, inputFile, outputFile, flags):
tool, toolFlags = self.getTool(inputFile, outputFile)
return self.runTool([tool, '-o', outputFile] + toolFlags + flags + [inputFile], errorMsg='Cannot compile %s with %s' % (inputFile, tool))

class RustCompiler(Tool):
def __init__(self, name=None, supportedLanguages=None):
if name is None:
self.name = 'rustc'
else:
self.name = name

if supportedLanguages is None:
self.supportedLanguages = [ProgrammingLanguage.RUST]
else:
self.supportedLanguages = supportedLanguages

def getTool(self, inputFile):
inputLanguage = ProgrammingLanguage.lookupFile(inputFile)
if inputLanguage == ProgrammingLanguage.RUST:
return 'rustc'
else:
raise Exception('Unsupported input language')

def run(self, inputFile, outputFile, flags):
tool = self.getTool(inputFile)
return self.runTool([tool, '--emit=llvm-bc', '-o', outputFile] + flags + [inputFile], errorMsg='Cannot compile %s with %s' % (inputFile, tool))

def compileReferenceFile(self, inputFile, outputFile, flags):
tool = self.getTool(inputFile)
return self.runTool([tool, '-o', outputFile] + flags + [inputFile], errorMsg='Cannot compile %s with %s' % (inputFile, tool))

class Opt(Tool):
def __init__(self, name, passes):
self.name = name
Expand All @@ -208,6 +236,8 @@ def run(self, inputFile, outputFile, flags):
Tool.GCC = GCCCompiler()
Tool.GFORTRAN = GCCCompiler('gfortran', [ProgrammingLanguage.FORTRAN])

Tool.RUSTC = RustCompiler()

Tool.MISC_OPTS = Opt('MISC_OPTS', ['-functionattrs', '-instcombine', '-always-inline', '-jump-threading', '-simplifycfg', '-mem2reg'])
Tool.MEM2REG = Opt('MEM2REG', ['-mem2reg'])

Expand Down
Loading