1616#
1717
1818import os
19+ import shutil
20+ import re
1921from avocado import Test
2022from avocado .utils import process , build , memory , archive
2123from avocado .utils .software_manager .manager import SoftwareManager
@@ -42,6 +44,48 @@ def setUp(self):
4244 for pkg in ['gcc' , 'make' ]:
4345 if not smm .check_installed (pkg ) and not smm .install (pkg ):
4446 self .cancel ('%s is needed for the test to be run' % pkg )
47+
48+ self .gcc_backup_path = None
49+ self .gcc_replaced = False
50+
51+ try :
52+ gcc_version_output = process .run ('gcc --version' , shell = True ).stdout_text
53+ version_match = re .search (r'gcc.*?(\d+)\.' , gcc_version_output )
54+ if version_match :
55+ gcc_major_version = int (version_match .group (1 ))
56+ self .log .info (f"Detected GCC version: { gcc_major_version } " )
57+
58+ if gcc_major_version >= 15 :
59+ self .log .info ("GCC version >= 15 detected, replacing with GCC-13" )
60+
61+ gcc13_path = '/usr/bin/gcc-13'
62+ if not os .path .exists (gcc13_path ):
63+ self .log .info ("gcc-13 not found, attempting to install" )
64+ if not smm .check_installed ('gcc13' ) and not smm .install ('gcc13' ):
65+ if not smm .check_installed ('gcc-13' ) and not smm .install ('gcc-13' ):
66+ self .cancel ('gcc-13 is required but could not be installed' )
67+
68+ if not os .path .exists (gcc13_path ):
69+ self .cancel ('gcc-13 package installed but binary not found at /usr/bin/gcc-13' )
70+
71+ gcc_path = '/usr/bin/gcc'
72+ self .gcc_backup_path = '/usr/bin/gcc.backup.memtester'
73+
74+ try :
75+ shutil .copy2 (gcc_path , self .gcc_backup_path )
76+ self .log .info (f"Backed up { gcc_path } to { self .gcc_backup_path } " )
77+
78+ shutil .copy2 (gcc13_path , gcc_path )
79+ self .gcc_replaced = True
80+ self .log .info (f"Replaced { gcc_path } with { gcc13_path } " )
81+ except Exception as e :
82+ self .log .error (f"Failed to replace GCC: { e } " )
83+ # Clean up backup if replacement failed
84+ if os .path .exists (self .gcc_backup_path ):
85+ os .remove (self .gcc_backup_path )
86+ raise
87+ except Exception as e :
88+ self .log .warning (f"Could not check GCC version: { e } " )
4589 tarball = self .fetch_asset ('memtester.zip' ,
4690 locations = ['https://github.com/jnavila/'
4791 'memtester/archive/master.zip' ],
@@ -53,6 +97,21 @@ def setUp(self):
5397 ignore_status = True )
5498 build .make (sourcedir )
5599
100+ def tearDown (self ):
101+ '''
102+ Restore original GCC if it was replaced
103+ '''
104+ if self .gcc_replaced and self .gcc_backup_path :
105+ try :
106+ gcc_path = '/usr/bin/gcc'
107+ if os .path .exists (self .gcc_backup_path ):
108+ shutil .copy2 (self .gcc_backup_path , gcc_path )
109+ self .log .info (f"Restored original GCC from { self .gcc_backup_path } " )
110+ os .remove (self .gcc_backup_path )
111+ self .log .info (f"Removed backup file { self .gcc_backup_path } " )
112+ except Exception as e :
113+ self .log .error (f"Failed to restore original GCC: { e } " )
114+
56115 def test (self ):
57116 '''
58117 Run memtester
0 commit comments