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

Commit

Permalink
Add support for Rust test cases.
Browse files Browse the repository at this point in the history
  • Loading branch information
anatol1234 committed Nov 26, 2017
1 parent 504e404 commit e7861c4
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 13 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ group: deprecated-2017Q3
language: java
python:
- "2.7"
rust:
- 1.19.0
addons:
apt:
sources:
Expand Down
13 changes: 10 additions & 3 deletions mx.sulong/mx_sulong.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from os.path import join
import shutil
import subprocess
import glob

import mx
import mx_subst
Expand Down Expand Up @@ -54,6 +55,7 @@
basicLLVMDependencies = [
'clang',
'clang++',
'rustc',
'opt',
'llc',
'llvm-as'
Expand All @@ -75,7 +77,7 @@

def _unittest_config_participant(config):
(vmArgs, mainClass, mainClassArgs) = config
libs = [mx_subst.path_substitutions.substitute('<path:SULONG_TEST_NATIVE>/<lib:sulongtest>')]
libs = [mx_subst.path_substitutions.substitute('<path:SULONG_TEST_NATIVE>/<lib:sulongtest>'), findRustLibrary("std")]
vmArgs = getCommonOptions(True, libs) + vmArgs
return (vmArgs, mainClass, mainClassArgs)

Expand Down Expand Up @@ -496,6 +498,13 @@ def findGCCProgram(gccProgram, optional=False):
else:
return installedProgram

def findRustLibrary(name):
process = subprocess.Popen(["rustc", "--print", "sysroot"], stdout=subprocess.PIPE)
sysroot = process.communicate()[0].rstrip()
process.wait()
lib_dir_path = os.path.join(sysroot, "lib", "lib" + name + "*.so")
return glob.glob(lib_dir_path)[0]

def getClasspathOptions():
"""gets the classpath of the Sulong distributions"""
return mx.get_runtime_jvm_args('SULONG')
Expand All @@ -519,8 +528,6 @@ def opt(args=None, version=None, out=None, err=None):

# Project classes

import glob

class ArchiveProject(mx.ArchivableProject):
def __init__(self, suite, name, deps, workingSets, theLicense, **args):
mx.ArchivableProject.__init__(self, suite, name, deps, workingSets, theLicense)
Expand Down
14 changes: 9 additions & 5 deletions mx.sulong/mx_testsuites.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ def renameBenchmarkFiles(directory):


class SulongTestSuite(mx.NativeProject):
def __init__(self, suite, name, deps, workingSets, subDir, results=None, output=None, buildRef=True, **args):
def __init__(self, suite, name, deps, workingSets, subDir, results=None, output=None, buildRef='REF', **args):
d = os.path.join(suite.dir, subDir) # use common Makefile for all test suites
mx.NativeProject.__init__(self, suite, name, subDir, [], deps, workingSets, results, output, d, **args)
self.vpath = True
Expand All @@ -279,7 +279,7 @@ def getTests(self):
absPath = os.path.join(path, f)
relPath = os.path.relpath(absPath, root)
test, ext = os.path.splitext(relPath)
if ext in ['.c', '.cpp']:
if ext in ['.c', '.cpp', '.rs']:
self._tests.append(test)
return self._tests

Expand All @@ -293,14 +293,16 @@ def getVariants(self):
self._variants.append(v)
return self._variants


def getBuildEnv(self, replaceVar=mx_subst.path_substitutions):
env = super(SulongTestSuite, self).getBuildEnv(replaceVar=replaceVar)
env['VPATH'] = os.path.join(self.dir, self.name)
env['PROJECT'] = self.name
env['TESTS'] = ' '.join(self.getTests())
env['VARIANTS'] = ' '.join(self.getVariants())
env['BUILD_REF'] = '1' if self.buildRef else '0'
env['BUILD_REF'] = {
'NO_REF':'2',
'REF_NO_BC':'1',
'REF':'0'}[self.buildRef]
env['SULONG_MAKE_CLANG_IMPLICIT_ARGS'] = mx_sulong.getClangImplicitArgs()
if SulongTestSuite.haveDragonegg():
env['DRAGONEGG'] = mx_sulong.dragonEggPath()
Expand All @@ -312,7 +314,9 @@ def getResults(self, replaceVar=mx_subst.results_substitutions):
if not self.results:
self.results = []
for t in self.getTests():
if self.buildRef:
if self.buildRef == 'REF_NO_BC':
self.results.append(os.path.join(t, 'ref.nobc.out'))
elif self.buildRef == 'REF':
self.results.append(os.path.join(t, 'ref.out'))
for v in self.getVariants():
self.results.append(os.path.join(t, v + '.bc'))
Expand Down
30 changes: 30 additions & 0 deletions mx.sulong/mx_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,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 @@ -164,6 +165,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 @@ -180,6 +208,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
14 changes: 12 additions & 2 deletions mx.sulong/suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@
"subDir" : "tests",
"class" : "SulongTestSuite",
"variants" : ['O0_MEM2REG'],
"buildRef" : False,
"buildRef" : 'NO_REF',
"buildEnv" : {
"CPPFLAGS" : "-I<sulong_include>",
},
Expand All @@ -283,7 +283,7 @@
"subDir" : "tests",
"class" : "SulongTestSuite",
"variants" : ['O0_MEM2REG'],
"buildRef" : False,
"buildRef" : 'NO_REF',
"buildEnv" : {
"CPPFLAGS" : "-I<sulong_include>",
},
Expand All @@ -305,6 +305,15 @@
"OS" : "<os>",
},
},
"com.oracle.truffle.llvm.tests.sulongrust" : {
"subDir" : "tests",
"class" : "SulongTestSuite",
"variants" : ['O0'],
"buildRef" : 'REF_NO_BC',
"buildEnv" : {
"OS" : "<os>",
},
},
"com.oracle.truffle.llvm.tests.libc" : {
"subDir" : "tests",
"class" : "SulongTestSuite",
Expand Down Expand Up @@ -388,6 +397,7 @@
"com.oracle.truffle.llvm.tests.nfi",
"com.oracle.truffle.llvm.tests.sulong",
"com.oracle.truffle.llvm.tests.sulongcpp",
"com.oracle.truffle.llvm.tests.sulongrust",
"com.oracle.truffle.llvm.tests.libc",
],
"license" : "BSD-new",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public final class SulongSuite extends BaseSuiteHarness {
public static Collection<Object[]> data() {
Path suitesPath = new File(TestOptions.TEST_SUITE_PATH).toPath();
try {
Stream<Path> destDirs = Files.walk(suitesPath).filter(path -> path.endsWith("ref.out")).map(Path::getParent);
Stream<Path> destDirs = Files.walk(suitesPath).filter(path -> path.getFileName().toString().startsWith("ref.") && path.getFileName().toString().endsWith(".out")).map(Path::getParent);
return destDirs.map(testPath -> new Object[]{testPath, suitesPath.relativize(testPath).toString()}).collect(Collectors.toList());
} catch (IOException e) {
throw new AssertionError("Test cases not found", e);
Expand All @@ -68,7 +68,8 @@ protected Predicate<? super Path> getIsSulongFilter() {
return f -> {
boolean isBC = f.getFileName().toString().endsWith(".bc");
boolean isOut = f.getFileName().toString().endsWith(".out");
return isBC || (isOut && !IS_MAC);
boolean isNoBCOut = f.getFileName().toString().endsWith(".nobc.out");
return isBC || (isOut && !isNoBCOut && !IS_MAC);
};
}

Expand Down
1 change: 1 addition & 0 deletions rust-toolchain
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.19.0
12 changes: 11 additions & 1 deletion tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
#
QUIETLY$(MX_VERBOSE) = @

ifeq ($(BUILD_REF),1)
ifeq ($(BUILD_REF),0)
REF_TARGETS:=$(TESTS:%=%/ref.out)
else ifeq ($(BUILD_REF),1)
REF_TARGETS:=$(TESTS:%=%/ref.nobc.out)
else
REF_TARGETS:=
endif
Expand Down Expand Up @@ -54,6 +56,10 @@ endif
@mkdir -p $(shell dirname $@)
$(QUIETLY) clang++ $(EMBED_BC) -Wno-everything $(CPPFLAGS) $(CXXFLAGS) $(LDFLAGS) -o $@ $^

%/ref.nobc.out: %.rs $
@mkdir -p $(shell dirname $@)
$(QUIETLY) rustc -o $@ $^

%.bc: %.ll
$(QUIETLY) $(DRAGONEGG_LLVMAS) -o $@ $<

Expand All @@ -66,6 +72,10 @@ define OPT_RULES
@mkdir -p $$(shell dirname $$@)
$(QUIETLY) clang++ -c -emit-llvm $(2) -Wno-everything $(CPPFLAGS) $(CXXFLAGS) -g -o $$@ $$<

%/$(1).bc: %.rs
@mkdir -p $$(shell dirname $$@)
$(QUIETLY) rustc --emit=llvm-bc -o $$@ $$<

%/gcc_$(1).ll: %.c
@mkdir -p $$(shell dirname $$@)
$(QUIETLY) $(DRAGONEGG_GCC) -w -S --std=gnu99 -fplugin=$(DRAGONEGG) -fplugin-arg-dragonegg-emit-ir -$(1) $(CPPFLAGS) $(CFLAGS) -o $$@ $$<
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main(){
std::process::exit(42);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main(){
println!("Hello World!");
}

0 comments on commit e7861c4

Please sign in to comment.