@@ -69,6 +69,8 @@ class TestBase:
69
69
supported_lang = {
70
70
'C' : { 'cc' : 'gcc' , 'flags' : 'CFLAGS' , 'ext' : '.c' },
71
71
'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' },
72
74
}
73
75
74
76
TEST_SUCCESS = 0
@@ -573,6 +575,8 @@ def prerun(self, timeout):
573
575
self .exearg = 't-' + self .name
574
576
if self .lang == 'Python' :
575
577
self .exearg = TestBase .srcdir + '/tests/' + 's-' + self .name + '.py'
578
+ elif self .lang == 'Rust' :
579
+ self .exearg = TestBase .srcdir + '/tests/' + 's-' + self .name
576
580
577
581
cmd = self .prepare ()
578
582
if cmd == '' :
@@ -714,6 +718,22 @@ def __init__(self, name, result, lang='Python', cflags='', ldflags='', sort='sim
714
718
if orig_path != "" :
715
719
os .environ ["PYTHONPATH" ] += ':' + orig_path
716
720
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
+
717
737
RED = '\033 [1;31m'
718
738
GREEN = '\033 [1;32m'
719
739
YELLOW = '\033 [1;33m'
@@ -780,6 +800,19 @@ def run_python_case(T, case, timeout):
780
800
ret = tc .postrun (ret )
781
801
return (ret , dif )
782
802
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
+
783
816
def run_single_case (case , flags , opts , arg , compilers ):
784
817
result = []
785
818
timeout = int (arg .timeout )
@@ -795,6 +828,11 @@ def run_single_case(case, flags, opts, arg, compilers):
795
828
result .append ((ret , dif ))
796
829
continue
797
830
831
+ if compiler == 'rustc' :
832
+ ret , dif = run_rust_case (T , case , timeout )
833
+ result .append ((ret , dif ))
834
+ continue
835
+
798
836
for flag in flags :
799
837
for opt in opts :
800
838
tc = T .TestCase ()
@@ -910,6 +948,30 @@ def print_python_test_header(ftests):
910
948
ftests .write (header2 + '\n ' )
911
949
ftests .flush ()
912
950
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 ()
913
975
914
976
def print_test_report (color , shared ):
915
977
success = shared .stats [TestBase .TEST_SUCCESS ] + shared .stats [TestBase .TEST_SUCCESS_FIXED ]
@@ -962,6 +1024,8 @@ def parse_argument():
962
1024
help = "Hide normal results and print only abnormal results." )
963
1025
parser .add_argument ("-P" , "--python" , dest = 'python' , action = 'store_true' ,
964
1026
help = "Run python test cases instead" )
1027
+ parser .add_argument ("-R" , "--rust" , dest = 'rust' , action = 'store_true' ,
1028
+ help = "Run rust test cases instead" )
965
1029
966
1030
return parser .parse_args ()
967
1031
@@ -976,6 +1040,8 @@ def parse_argument():
976
1040
if arg .cases == 'all' :
977
1041
if arg .python :
978
1042
testcases = glob .glob ('p???_*.py' )
1043
+ elif arg .rust :
1044
+ testcases = glob .glob ('r???_*.py' )
979
1045
else :
980
1046
testcases = glob .glob ('t???_*.py' )
981
1047
else :
@@ -984,6 +1050,8 @@ def parse_argument():
984
1050
for case in cases :
985
1051
if arg .python :
986
1052
testcases .extend (glob .glob ('p*' + case + '*.py' ))
1053
+ elif arg .rust :
1054
+ testcases .extend (glob .glob ('r*' + case + '*.py' ))
987
1055
else :
988
1056
testcases .extend (glob .glob ('t*' + case + '*.py' ))
989
1057
arg .worker = min (arg .worker , len (testcases ))
@@ -1000,6 +1068,7 @@ def parse_argument():
1000
1068
patch_size = {
1001
1069
'x86_64' : 5 ,
1002
1070
'aarch64' : 2 ,
1071
+ 'riscv' : 2 ,
1003
1072
}
1004
1073
1005
1074
m = os .uname ()[- 1 ] # machine
@@ -1029,6 +1098,9 @@ def has_compiler(compiler):
1029
1098
compilers = []
1030
1099
if arg .python :
1031
1100
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' )
1032
1104
elif arg .compiler == 'all' :
1033
1105
for compiler in ['gcc' , 'clang' ]:
1034
1106
if has_compiler (compiler ):
@@ -1083,6 +1155,8 @@ class dotdict(dict):
1083
1155
1084
1156
if arg .python :
1085
1157
print_python_test_header (ftests )
1158
+ elif arg .rust :
1159
+ print_rust_test_header (flags , ftests , ['rustc' ])
1086
1160
else :
1087
1161
print_test_header (opts , flags , ftests , compilers )
1088
1162
0 commit comments