|
1 | 1 | import glob |
2 | 2 | import os |
| 3 | +import stat |
3 | 4 | import tarfile |
4 | 5 | from collections import OrderedDict |
| 6 | +from pathlib import Path |
5 | 7 | from shutil import copyfile, copytree, rmtree |
6 | 8 |
|
7 | 9 | import numpy as np |
@@ -692,45 +694,44 @@ def write_build_script(self, model): |
692 | 694 | model (ModelGraph): the hls4ml model. |
693 | 695 | """ |
694 | 696 |
|
695 | | - filedir = os.path.dirname(os.path.abspath(__file__)) |
| 697 | + filedir = Path(__file__).parent |
696 | 698 |
|
697 | 699 | # 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'))) |
712 | 714 |
|
713 | 715 | # build_prj.tcl |
714 | | - srcpath = os.path.join(filedir, '../templates/vivado/build_prj.tcl') |
| 716 | + srcpath = (filedir / '../templates/vivado/build_prj.tcl').resolve() |
715 | 717 | dstpath = f'{model.config.get_output_dir()}/build_prj.tcl' |
716 | 718 | copyfile(srcpath, dstpath) |
717 | 719 |
|
718 | 720 | # vivado_synth.tcl |
719 | | - srcpath = os.path.join(filedir, '../templates/vivado/vivado_synth.tcl') |
| 721 | + srcpath = (filedir / '../templates/vivado/vivado_synth.tcl').resolve() |
720 | 722 | dstpath = f'{model.config.get_output_dir()}/vivado_synth.tcl' |
721 | 723 | copyfile(srcpath, dstpath) |
722 | 724 |
|
723 | 725 | # 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) |
734 | 735 |
|
735 | 736 | def write_nnet_utils(self, model): |
736 | 737 | """Copy the nnet_utils, AP types headers and any custom source to the project output directory |
|
0 commit comments