@@ -69,6 +69,8 @@ class TestBase:
6969 supported_lang = {
7070 'C' : { 'cc' : 'gcc' , 'flags' : 'CFLAGS' , 'ext' : '.c' },
7171 'C++' : { 'cc' : 'g++' , 'flags' : 'CXXFLAGS' , 'ext' : '.cpp' },
72+ # https://github.com/emosenkis/esp-rs/issues/10
73+ 'Rust' : { 'cc' : 'rustc' , 'flags' : "+nightly -Z instrument-mcount" , 'ext' : '.rs' },
7274 }
7375
7476 TEST_SUCCESS = 0
@@ -573,6 +575,8 @@ def prerun(self, timeout):
573575 self .exearg = 't-' + self .name
574576 if self .lang == 'Python' :
575577 self .exearg = TestBase .srcdir + '/tests/' + 's-' + self .name + '.py'
578+ elif self .lang == 'Rust' :
579+ self .exearg = TestBase .srcdir + '/tests/' + 's-' + self .name
576580
577581 cmd = self .prepare ()
578582 if cmd == '' :
@@ -714,6 +718,22 @@ def __init__(self, name, result, lang='Python', cflags='', ldflags='', sort='sim
714718 if orig_path != "" :
715719 os .environ ["PYTHONPATH" ] += ':' + orig_path
716720
721+ class RustTestBase (TestBase ):
722+ def __init__ (self , name , result , lang = 'Rust' , cflags = '' , ldflags = '' , sort = 'simple' , serial = False ):
723+ TestBase .__init__ (self , name , result , lang , cflags , ldflags , sort , serial )
724+
725+ def build (self , name , rflags = '' , ldflags = '' ):
726+
727+ prog = 's-' + name
728+ src = 's-' + name + ".rs"
729+ rflags = self .supported_lang ['Rust' ]['flags' ]
730+
731+ build_cmd = '%s %s %s' % \
732+ ('rustc' , rflags , src )
733+
734+ self .pr_debug ("build command: %s" % build_cmd )
735+ return self .build_it (build_cmd )
736+
717737RED = '\033 [1;31m'
718738GREEN = '\033 [1;32m'
719739YELLOW = '\033 [1;33m'
@@ -780,6 +800,19 @@ def run_python_case(T, case, timeout):
780800 ret = tc .postrun (ret )
781801 return (ret , dif )
782802
803+ def run_rust_case (T , case , timeout ):
804+ tc = T .TestCase ()
805+ tc .set_debug (arg .debug )
806+ tc .set_keep (arg .keep )
807+
808+ ret = tc .build (tc .name , "" )
809+ ret = tc .prerun (timeout )
810+ dif = ''
811+ if ret == TestBase .TEST_SUCCESS :
812+ ret , dif = tc .run (case , "" , arg .diff , timeout )
813+ ret = tc .postrun (ret )
814+ return (ret , dif )
815+
783816def run_single_case (case , flags , opts , arg , compilers ):
784817 result = []
785818 timeout = int (arg .timeout )
@@ -795,6 +828,11 @@ def run_single_case(case, flags, opts, arg, compilers):
795828 result .append ((ret , dif ))
796829 continue
797830
831+ if compiler == 'rustc' :
832+ ret , dif = run_rust_case (T , case , timeout )
833+ result .append ((ret , dif ))
834+ continue
835+
798836 for flag in flags :
799837 for opt in opts :
800838 tc = T .TestCase ()
@@ -910,6 +948,30 @@ def print_python_test_header(ftests):
910948 ftests .write (header2 + '\n ' )
911949 ftests .flush ()
912950
951+ def print_rust_test_header (flags , ftests , compilers ):
952+ header1 = '%-24s ' % 'Compiler'
953+ header2 = '%-24s ' % 'Runtime test case'
954+ header3 = '-' * 24 + ':'
955+ empty = ' ' * 100
956+
957+ for i , compiler in enumerate (compilers ):
958+ if i != 0 :
959+ header1 += ' '
960+ header2 += ' '
961+ header3 += ' '
962+ for flag in flags :
963+ # align with optimization flags
964+ header2 += ' ' + flag
965+ header1 += ' ' + compiler
966+
967+ print ("" )
968+ print (header1 )
969+ print (header2 )
970+ print (header3 )
971+ ftests .write (header1 + '\n ' )
972+ ftests .write (header2 + '\n ' )
973+ ftests .write (header3 + '\n ' )
974+ ftests .flush ()
913975
914976def print_test_report (color , shared ):
915977 success = shared .stats [TestBase .TEST_SUCCESS ] + shared .stats [TestBase .TEST_SUCCESS_FIXED ]
@@ -962,6 +1024,8 @@ def parse_argument():
9621024 help = "Hide normal results and print only abnormal results." )
9631025 parser .add_argument ("-P" , "--python" , dest = 'python' , action = 'store_true' ,
9641026 help = "Run python test cases instead" )
1027+ parser .add_argument ("-R" , "--rust" , dest = 'rust' , action = 'store_true' ,
1028+ help = "Run rust test cases instead" )
9651029
9661030 return parser .parse_args ()
9671031
@@ -976,6 +1040,8 @@ def parse_argument():
9761040 if arg .cases == 'all' :
9771041 if arg .python :
9781042 testcases = glob .glob ('p???_*.py' )
1043+ elif arg .rust :
1044+ testcases = glob .glob ('r???_*.py' )
9791045 else :
9801046 testcases = glob .glob ('t???_*.py' )
9811047 else :
@@ -984,6 +1050,8 @@ def parse_argument():
9841050 for case in cases :
9851051 if arg .python :
9861052 testcases .extend (glob .glob ('p*' + case + '*.py' ))
1053+ elif arg .rust :
1054+ testcases .extend (glob .glob ('r*' + case + '*.py' ))
9871055 else :
9881056 testcases .extend (glob .glob ('t*' + case + '*.py' ))
9891057 arg .worker = min (arg .worker , len (testcases ))
@@ -1000,6 +1068,7 @@ def parse_argument():
10001068 patch_size = {
10011069 'x86_64' : 5 ,
10021070 'aarch64' : 2 ,
1071+ 'riscv' : 2 ,
10031072 }
10041073
10051074 m = os .uname ()[- 1 ] # machine
@@ -1029,6 +1098,9 @@ def has_compiler(compiler):
10291098 compilers = []
10301099 if arg .python :
10311100 compilers .append ('python' )
1101+ elif arg .rust :
1102+ if has_compiler ('rustc' ) and os .system ('rustup default nightly > /dev/null' ) == 0 :
1103+ compilers .append ('rustc' )
10321104 elif arg .compiler == 'all' :
10331105 for compiler in ['gcc' , 'clang' ]:
10341106 if has_compiler (compiler ):
@@ -1083,6 +1155,8 @@ class dotdict(dict):
10831155
10841156 if arg .python :
10851157 print_python_test_header (ftests )
1158+ elif arg .rust :
1159+ print_rust_test_header (flags , ftests , ['rustc' ])
10861160 else :
10871161 print_test_header (opts , flags , ftests , compilers )
10881162
0 commit comments