|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +######################################################################################### |
| 4 | +# Author : Jaekyu Lee (jq.lee17@gmail.com) |
| 5 | +# Description : wrapper script for scons build |
| 6 | +######################################################################################### |
| 7 | + |
| 8 | + |
| 9 | +import os |
| 10 | +import sys |
| 11 | +import itertools |
| 12 | +from optparse import OptionParser |
| 13 | +import subprocess |
| 14 | + |
| 15 | +######################################################################################### |
| 16 | +# argument parsing |
| 17 | +######################################################################################### |
| 18 | +def parse_arg(): |
| 19 | + parser = OptionParser(usage="usage: %prog [options] filename", version="%prog 1.0") |
| 20 | + parser.add_option("-j", "--thread", action="store", dest="thread", default=1, help="-j option for the parallel build") |
| 21 | + parser.add_option("-d", "--debug", action="store_true", dest="debug", default=False, help="debug build") |
| 22 | + parser.add_option("-p", "--gprof", action="store_true", dest="gprof", default=False, help="gprof build") |
| 23 | + parser.add_option("-c", "--clean", action="store_true", dest="clean", default=False, help="clean") |
| 24 | + parser.add_option("-t", "--test", action="store_true", dest="test", default=False, help="clean") |
| 25 | + parser.add_option("-v", "--val", action="store_true", dest="val", default=False, help="build version used for gpu validation") |
| 26 | + parser.add_option("--newpin", action="store_true", dest="pin_3_13_trace", default=False, help="trace based on pin 3.13 ") |
| 27 | + parser.add_option("-q", "--qsim", action="store_true", dest="qsim", default=False, help="use qsim to drive macsim") |
| 28 | + parser.add_option("--dramsim", action="store_true", dest="dramsim", default=False, help="DRAMSim2") |
| 29 | + parser.add_option("--power", action="store_true", dest="power", default=False, help="EI Power") |
| 30 | + parser.add_option("--iris", action="store_true", dest="iris", default=False, help="IRIS") |
| 31 | + parser.add_option("--ramulator", action="store_true", dest="ramulator", default=False, help="Ramulator") |
| 32 | + parser.add_option("--sst", action="store_true", dest="sst", default=False, help="Build Macsim SST Element") |
| 33 | + parser.add_option("--sst-install", action="store_true", dest="sst_install", default=False, help="Install Macsim SST Element") |
| 34 | + |
| 35 | + return parser |
| 36 | + |
| 37 | + |
| 38 | +######################################################################################### |
| 39 | +# build test for all possible build combinations |
| 40 | +######################################################################################### |
| 41 | +def build_test(): |
| 42 | + build_option = ['', 'debug=1', 'gprof=1', 'qsim=1', 'pin_3_13_trace=1'] |
| 43 | + build_dir = ['.opt_build', '.dbg_build', '.gpf_build'] |
| 44 | + build_libs = ['dram=1', 'power=1', 'iris=1', 'ramulator=1'] |
| 45 | + |
| 46 | + for ii in range(0, len(build_option)): |
| 47 | + os.system('rm -rf %s' % build_dir[ii]) |
| 48 | + for jj in range(0, len(build_libs)+1): |
| 49 | + for opt in itertools.combinations(build_libs, jj): |
| 50 | + cmd = 'scons -j 4 %s %s' % (build_option[ii], ' '.join(opt)) |
| 51 | + redir = '> /dev/null 2>&1' |
| 52 | + |
| 53 | + if os.path.exists('%s/macsim' % build_dir[ii]): |
| 54 | + os.system('rm -f %s/macsim' % build_dir[ii]) |
| 55 | + |
| 56 | + os.system('%s %s' % (cmd, redir)) |
| 57 | + if os.path.exists('%s/macsim' % build_dir[ii]): |
| 58 | + print('%s %s successful' % (build_option[ii], ' '.join(opt))) |
| 59 | + else: |
| 60 | + print('%s %s failed' % (build_option[ii], ' '.join(opt))) |
| 61 | + |
| 62 | +######################################################################################### |
| 63 | +# Util Functions |
| 64 | +######################################################################################### |
| 65 | +def get_cmd_output(cmd, abort_on_error=True): |
| 66 | + """ |
| 67 | + Run a command and return the output. |
| 68 | + :param cmd: The command to run as a list of tokens. |
| 69 | + :param abort_on_error: If True, abort on error. |
| 70 | + :return: The stdout, stderr, and return code of the command. |
| 71 | + """ |
| 72 | + process = subprocess.Popen(cmd, stdout=subprocess.PIPE) |
| 73 | + stdout, stderr = process.communicate() |
| 74 | + stdout = stdout.decode('utf-8', errors='replace').strip() if stdout is not None else '' |
| 75 | + stderr = stderr.decode('utf-8', errors='replace').strip() if stderr is not None else '' |
| 76 | + if abort_on_error and process.returncode != 0: |
| 77 | + #print(f'Error: Shell command failed (return code: {process.returncode}): ', ' '.join(cmd), file=sys.stderr) |
| 78 | + #print('\tstdout:', stdout, file=sys.stderr) |
| 79 | + #print('\tstderr:', stderr, file=sys.stderr) |
| 80 | + print >> sys.stderr, 'Error: Shell command failed (return code: {}): {}'.format(process.returncode, ' '.join(cmd)) |
| 81 | + print >> sys.stderr, '\tstdout:', stdout |
| 82 | + print >> sys.stderr, '\tstderr:', stderr |
| 83 | + sys.exit(1) |
| 84 | + return stdout |
| 85 | + |
| 86 | + |
| 87 | +######################################################################################### |
| 88 | +# main function |
| 89 | +######################################################################################### |
| 90 | +def main(): |
| 91 | + parser = parse_arg() |
| 92 | + (options, args) = parser.parse_args() |
| 93 | + |
| 94 | + ## Build test |
| 95 | + if options.test: |
| 96 | + build_test() |
| 97 | + sys.exit(0) |
| 98 | + |
| 99 | + |
| 100 | + ## Prepare scons command |
| 101 | + cmd = 'scons ' |
| 102 | + |
| 103 | + ## Main build options (opt, dbg, gpf) |
| 104 | + if options.debug: |
| 105 | + cmd += 'debug=1 ' |
| 106 | + elif options.gprof: |
| 107 | + cmd += 'gprof=1 ' |
| 108 | + |
| 109 | + if options.val: |
| 110 | + cmd += 'val=1 ' |
| 111 | + |
| 112 | + ## External libraries (dramsim, ei, iris) |
| 113 | + # DRAMSim2 |
| 114 | + if options.dramsim: |
| 115 | + cmd += 'dram=1 ' |
| 116 | + |
| 117 | + if options.sst: |
| 118 | + cmd += 'sst=1 ' |
| 119 | + |
| 120 | + if options.sst_install: |
| 121 | + component = 'macsimComponent' |
| 122 | + component_lib_dir = os.path.abspath('.sst_build') |
| 123 | + component_src_dir = os.getcwd() |
| 124 | + component_tests_dir = os.path.abspath('sst-unit-test') |
| 125 | + |
| 126 | + # Check if macsim component exists |
| 127 | + if not os.path.exists('{}/lib{}.so'.format(component_lib_dir, component)): |
| 128 | + print("ERROR: {} not found in {}, build with sst=1 option first".format(component, component_lib_dir)) |
| 129 | + #if not os.path.exists(f'{component_lib_dir}/lib{component}.so'): |
| 130 | + #print(f"ERROR: {component} not found in {component_lib_dir}, build with sst=1 option first") |
| 131 | + exit(0) |
| 132 | + |
| 133 | + print("Registering SST element: {}".format(component)) |
| 134 | + print(" SRCDIR: {}".format(component_src_dir)) |
| 135 | + print(" LIBDIR: {}".format(component_lib_dir)) |
| 136 | + print(" TESTDIR: {}".format(component_tests_dir)) |
| 137 | + os.system('sst-register {} {}_LIBDIR={}'.format(component, component, component_lib_dir)) |
| 138 | + os.system('sst-register SST_ELEMENT_SOURCE {}={}'.format(component, component_src_dir)) |
| 139 | + os.system('sst-register SST_ELEMENT_TESTS {}={}'.format(component, component_tests_dir)) |
| 140 | + |
| 141 | + # Check if component is registered successfully |
| 142 | + sst_info_out = get_cmd_output(['sst-info', component]) |
| 143 | + if 'Component 0: macsimComponent' in sst_info_out: |
| 144 | + print("Successfully registered SST element: {}".format(component)) |
| 145 | + exit(0) |
| 146 | + else: |
| 147 | + print("ERROR: Failed to register SST element: {}".format(component)) |
| 148 | + exit(1) |
| 149 | + |
| 150 | + # EI power |
| 151 | + if options.power: |
| 152 | + cmd += 'power=1 ' |
| 153 | + |
| 154 | + # IRIS |
| 155 | + if options.iris: |
| 156 | + cmd += 'iris=1 ' |
| 157 | + |
| 158 | + # Qsim |
| 159 | + if options.qsim: |
| 160 | + cmd += 'qsim=1 ' |
| 161 | + |
| 162 | + # NEW PIN |
| 163 | + if options.pin_3_13_trace: |
| 164 | + cmd += 'pin_3_13_trace=1 ' |
| 165 | + |
| 166 | + # Ramulator |
| 167 | + if options.ramulator: |
| 168 | + cmd += 'ramulator=1 ' |
| 169 | + |
| 170 | + ## Parallel building |
| 171 | + cmd += '-j %s ' % options.thread |
| 172 | + |
| 173 | + if options.clean: |
| 174 | + cmd += '-c' |
| 175 | + |
| 176 | + |
| 177 | + ## run scons command |
| 178 | + os.system(cmd) |
| 179 | + |
| 180 | + |
| 181 | + ## Create a symbolic link |
| 182 | + if not options.clean: |
| 183 | + if options.debug: |
| 184 | + build_dir = '.dbg_build' |
| 185 | + elif options.gprof: |
| 186 | + build_dir = '.gpf_build' |
| 187 | + else: |
| 188 | + build_dir = '.opt_build' |
| 189 | + |
| 190 | + if os.path.exists('%s/macsim' % build_dir): |
| 191 | + os.chdir('bin') |
| 192 | + |
| 193 | + if os.path.exists('macsim'): |
| 194 | + os.system('rm -f macsim') |
| 195 | + os.system('ln -s ../%s/macsim' % build_dir) |
| 196 | + |
| 197 | + |
| 198 | + |
| 199 | +if __name__ == '__main__': |
| 200 | + main() |
0 commit comments