@@ -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 -C passes=ee-instrument<post-inline> " , 'ext' : '.rs' },
7274 }
7375
7476 TEST_SUCCESS = 0
@@ -714,6 +716,22 @@ def __init__(self, name, result, lang='Python', cflags='', ldflags='', sort='sim
714716 if orig_path != "" :
715717 os .environ ["PYTHONPATH" ] += ':' + orig_path
716718
719+ class RustTestBase (TestBase ):
720+ def __init__ (self , name , result , lang = 'Rust' , cflags = '' , ldflags = '' , sort = 'simple' , serial = False ):
721+ TestBase .__init__ (self , name , result , lang , cflags , ldflags , sort , serial )
722+
723+ def build (self , name , rflags = '' , ldflags = '' ):
724+
725+ lang = TestBase .supported_lang [self .lang ]
726+ prog = 't-' + name
727+ src = 's-' + name + ".rs"
728+ rflags = self .supported_lang ['Rust' ]['flags' ]
729+
730+ build_cmd = '%s %s -o %s %s' % (lang ['cc' ], rflags , prog , src )
731+
732+ self .pr_debug ("build command: %s" % build_cmd )
733+ return self .build_it (build_cmd )
734+
717735RED = '\033 [1;31m'
718736GREEN = '\033 [1;32m'
719737YELLOW = '\033 [1;33m'
@@ -780,6 +798,18 @@ def run_python_case(T, case, timeout):
780798 ret = tc .postrun (ret )
781799 return (ret , dif )
782800
801+ def run_rust_case (T , case , timeout ):
802+ tc = T .TestCase ()
803+ tc .set_debug (arg .debug )
804+ tc .set_keep (arg .keep )
805+ ret = tc .build (tc .name , "" )
806+ ret = tc .prerun (timeout )
807+ dif = ''
808+ if ret == TestBase .TEST_SUCCESS :
809+ ret , dif = tc .run (case , "" , arg .diff , timeout )
810+ ret = tc .postrun (ret )
811+ return (ret , dif )
812+
783813def run_single_case (case , flags , opts , arg , compilers ):
784814 result = []
785815 timeout = int (arg .timeout )
@@ -795,6 +825,11 @@ def run_single_case(case, flags, opts, arg, compilers):
795825 result .append ((ret , dif ))
796826 continue
797827
828+ if compiler == 'rustc' :
829+ ret , dif = run_rust_case (T , case , timeout )
830+ result .append ((ret , dif ))
831+ continue
832+
798833 for flag in flags :
799834 for opt in opts :
800835 tc = T .TestCase ()
@@ -910,6 +945,30 @@ def print_python_test_header(ftests):
910945 ftests .write (header2 + '\n ' )
911946 ftests .flush ()
912947
948+ def print_rust_test_header (flags , ftests , compilers ):
949+ header1 = '%-24s ' % 'Compiler'
950+ header2 = '%-24s ' % 'Runtime test case'
951+ header3 = '-' * 24 + ':'
952+ empty = ' ' * 100
953+
954+ for i , compiler in enumerate (compilers ):
955+ if i != 0 :
956+ header1 += ' '
957+ header2 += ' '
958+ header3 += ' '
959+ for flag in flags :
960+ # align with optimization flags
961+ header2 += ' ' + flag
962+ header1 += ' ' + compiler
963+
964+ print ("" )
965+ print (header1 )
966+ print (header2 )
967+ print (header3 )
968+ ftests .write (header1 + '\n ' )
969+ ftests .write (header2 + '\n ' )
970+ ftests .write (header3 + '\n ' )
971+ ftests .flush ()
913972
914973def print_test_report (color , shared ):
915974 success = shared .stats [TestBase .TEST_SUCCESS ] + shared .stats [TestBase .TEST_SUCCESS_FIXED ]
@@ -962,6 +1021,8 @@ def parse_argument():
9621021 help = "Hide normal results and print only abnormal results." )
9631022 parser .add_argument ("-P" , "--python" , dest = 'python' , action = 'store_true' ,
9641023 help = "Run python test cases instead" )
1024+ parser .add_argument ("-R" , "--rust" , dest = 'rust' , action = 'store_true' ,
1025+ help = "Run rust test cases instead" )
9651026
9661027 return parser .parse_args ()
9671028
@@ -976,6 +1037,8 @@ def parse_argument():
9761037 if arg .cases == 'all' :
9771038 if arg .python :
9781039 testcases = glob .glob ('p???_*.py' )
1040+ elif arg .rust :
1041+ testcases = glob .glob ('r???_*.py' )
9791042 else :
9801043 testcases = glob .glob ('t???_*.py' )
9811044 else :
@@ -984,6 +1047,8 @@ def parse_argument():
9841047 for case in cases :
9851048 if arg .python :
9861049 testcases .extend (glob .glob ('p*' + case + '*.py' ))
1050+ elif arg .rust :
1051+ testcases .extend (glob .glob ('r*' + case + '*.py' ))
9871052 else :
9881053 testcases .extend (glob .glob ('t*' + case + '*.py' ))
9891054 arg .worker = min (arg .worker , len (testcases ))
@@ -1029,6 +1094,9 @@ def has_compiler(compiler):
10291094 compilers = []
10301095 if arg .python :
10311096 compilers .append ('python' )
1097+ elif arg .rust :
1098+ if has_compiler ('rustc' ) and os .system ('rustup default nightly > /dev/null' ) == 0 :
1099+ compilers .append ('rustc' )
10321100 elif arg .compiler == 'all' :
10331101 for compiler in ['gcc' , 'clang' ]:
10341102 if has_compiler (compiler ):
@@ -1083,6 +1151,8 @@ class dotdict(dict):
10831151
10841152 if arg .python :
10851153 print_python_test_header (ftests )
1154+ elif arg .rust :
1155+ print_rust_test_header (flags , ftests , ['rustc' ])
10861156 else :
10871157 print_test_header (opts , flags , ftests , compilers )
10881158
0 commit comments