Skip to content

Commit 7beb695

Browse files
committed
make build_lib.sh executable by default and switch to using pathlib
1 parent b56a0bd commit 7beb695

File tree

1 file changed

+28
-27
lines changed

1 file changed

+28
-27
lines changed

hls4ml/writer/vivado_writer.py

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import glob
22
import os
3+
import stat
34
import tarfile
45
from collections import OrderedDict
6+
from pathlib import Path
57
from shutil import copyfile, copytree, rmtree
68

79
import numpy as np
@@ -692,45 +694,44 @@ def write_build_script(self, model):
692694
model (ModelGraph): the hls4ml model.
693695
"""
694696

695-
filedir = os.path.dirname(os.path.abspath(__file__))
697+
filedir = Path(__file__).parent
696698

697699
# project.tcl
698-
f = open(f'{model.config.get_output_dir()}/project.tcl', 'w')
699-
f.write('variable project_name\n')
700-
f.write(f'set project_name "{model.config.get_project_name()}"\n')
701-
f.write('variable backend\n')
702-
f.write('set backend "vivado"\n')
703-
f.write('variable part\n')
704-
f.write('set part "{}"\n'.format(model.config.get_config_value('Part')))
705-
f.write('variable clock_period\n')
706-
f.write('set clock_period {}\n'.format(model.config.get_config_value('ClockPeriod')))
707-
f.write('variable clock_uncertainty\n')
708-
f.write('set clock_uncertainty {}\n'.format(model.config.get_config_value('ClockUncertainty', '12.5%')))
709-
f.write('variable version\n')
710-
f.write('set version "{}"\n'.format(model.config.get_config_value('Version', '1.0.0')))
711-
f.close()
700+
prj_tcl_dst = Path(f'{model.config.get_output_dir()}/project.tcl')
701+
with open(prj_tcl_dst, 'w') as f:
702+
f.write('variable project_name\n')
703+
f.write(f'set project_name "{model.config.get_project_name()}"\n')
704+
f.write('variable backend\n')
705+
f.write('set backend "vivado"\n')
706+
f.write('variable part\n')
707+
f.write('set part "{}"\n'.format(model.config.get_config_value('Part')))
708+
f.write('variable clock_period\n')
709+
f.write('set clock_period {}\n'.format(model.config.get_config_value('ClockPeriod')))
710+
f.write('variable clock_uncertainty\n')
711+
f.write('set clock_uncertainty {}\n'.format(model.config.get_config_value('ClockUncertainty', '12.5%')))
712+
f.write('variable version\n')
713+
f.write('set version "{}"\n'.format(model.config.get_config_value('Version', '1.0.0')))
712714

713715
# build_prj.tcl
714-
srcpath = os.path.join(filedir, '../templates/vivado/build_prj.tcl')
716+
srcpath = (filedir / '../templates/vivado/build_prj.tcl').resolve()
715717
dstpath = f'{model.config.get_output_dir()}/build_prj.tcl'
716718
copyfile(srcpath, dstpath)
717719

718720
# vivado_synth.tcl
719-
srcpath = os.path.join(filedir, '../templates/vivado/vivado_synth.tcl')
721+
srcpath = (filedir / '../templates/vivado/vivado_synth.tcl').resolve()
720722
dstpath = f'{model.config.get_output_dir()}/vivado_synth.tcl'
721723
copyfile(srcpath, dstpath)
722724

723725
# build_lib.sh
724-
f = open(os.path.join(filedir, '../templates/vivado/build_lib.sh'))
725-
fout = open(f'{model.config.get_output_dir()}/build_lib.sh', 'w')
726-
727-
for line in f.readlines():
728-
line = line.replace('myproject', model.config.get_project_name())
729-
line = line.replace('mystamp', model.config.get_config_value('Stamp'))
730-
731-
fout.write(line)
732-
f.close()
733-
fout.close()
726+
build_lib_src = (filedir / '../templates/vivado/build_lib.sh').resolve()
727+
build_lib_dst = Path(f'{model.config.get_output_dir()}/build_lib.sh').resolve()
728+
with open(build_lib_src) as src, open(build_lib_dst, 'w') as dst:
729+
for line in src.readlines():
730+
line = line.replace('myproject', model.config.get_project_name())
731+
line = line.replace('mystamp', model.config.get_config_value('Stamp'))
732+
733+
dst.write(line)
734+
build_lib_dst.chmod(build_lib_dst.stat().st_mode | stat.S_IEXEC)
734735

735736
def write_nnet_utils(self, model):
736737
"""Copy the nnet_utils, AP types headers and any custom source to the project output directory

0 commit comments

Comments
 (0)