Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1# -------------------------------------------------------------------------------------------------- 

2# Copyright (c) Lukas Vik. All rights reserved. 

3# 

4# This file is part of the tsfpga project. 

5# https://tsfpga.com 

6# https://gitlab.com/tsfpga/tsfpga 

7# -------------------------------------------------------------------------------------------------- 

8 

9from pathlib import Path 

10import platform 

11from shutil import which, make_archive 

12import zipfile 

13 

14from tsfpga.system_utils import create_file, delete 

15 

16 

17class VivadoSimlibCommon: 

18 

19 """ 

20 Class for handling Vivado simlib used for simulation. Keeps track of when a 

21 (re)compile is needed. 

22 """ 

23 

24 _libraries = None 

25 _vivado_path = None 

26 _output_path = None 

27 

28 def compile_if_needed(self): 

29 """ 

30 Compile if needed (if :meth:`compile_is_needed <.compile_is_needed>` condition is not 

31 fulfilled). 

32 """ 

33 if self.compile_is_needed: 

34 self.compile() 

35 return True 

36 return False 

37 

38 @property 

39 def compile_is_needed(self): 

40 """ 

41 If there is compiled simlib available that matches 

42 

43 * Operating system 

44 * Vivado version 

45 * Simulator version 

46 

47 then there should not be a recompile. 

48 

49 .. note:: 

50 Child implementations might add further conditions. 

51 

52 Return: 

53 True if compiled simlib is not available. False otherwise. 

54 """ 

55 if self._done_token.exists(): 

56 return False 

57 return True 

58 

59 def compile(self): 

60 """ 

61 Compile simlib. 

62 """ 

63 delete(self._done_token) 

64 print(f"Compiling Vivado simlib in {self._output_path}") 

65 

66 self._compile() 

67 

68 create_file(self._done_token, "Done!") 

69 

70 def _compile(self): 

71 """ 

72 Compile simlib. 

73 """ 

74 raise NotImplementedError() 

75 

76 def add_to_vunit_project(self): 

77 """ 

78 Add the compiled simlib to your VUnit project. 

79 """ 

80 self._add_to_vunit_project() 

81 

82 def _add_to_vunit_project(self): 

83 raise NotImplementedError() 

84 

85 @property 

86 def artifact_name(self): 

87 """ 

88 str: The name of the folder where simlib is or will be compiled. 

89 Follows a format ``vivado-simlib-WW.XX.YY.ZZ`` suitable for storage and versioning 

90 in Artifactory. 

91 """ 

92 return self._output_path.name 

93 

94 def to_archive(self): 

95 """ 

96 Compress compiled simlib to an archive. 

97 

98 Return: 

99 `pathlib.Path`: Path to the archive. 

100 """ 

101 make_archive(self._output_path, "zip", self._output_path) 

102 archive = self._output_path.parent / (self._output_path.name + ".zip") 

103 return archive 

104 

105 def from_archive(self, archive): 

106 """ 

107 Unpack compiled simlib from an existing archive. 

108 

109 Arguments: 

110 archive (`pathlib.Path`): Path to a zip archive with previously compiled simlib. 

111 """ 

112 with zipfile.ZipFile(archive, "r") as zip_handle: 

113 zip_handle.extractall(self._output_path) 

114 

115 def _get_version_tag(self): 

116 tag = "vivado-simlib-" 

117 tag += self._get_operating_system_tag() 

118 tag += "." + self._get_vivado_version_tag() 

119 tag += "." + self._get_simulator_tag() 

120 return tag 

121 

122 def _get_operating_system_tag(self): 

123 """ 

124 Return e.g. "linux". 

125 """ 

126 return self._format_version(platform.system()) 

127 

128 def _get_vivado_version_tag(self): 

129 """ 

130 Return e.g. "vivado_2019_2". 

131 """ 

132 vivado_path = self._vivado_path 

133 if vivado_path == "vivado": 

134 vivado_path = which(vivado_path) 

135 assert vivado_path is not None, "Could not find vivado location" 

136 vivado_version = Path(vivado_path).parent.parent.name 

137 return self._format_version("vivado_" + vivado_version) 

138 

139 def _get_simulator_tag(self): 

140 raise NotImplementedError() 

141 

142 @staticmethod 

143 def _format_version(version): 

144 """ 

145 Format version string to something suitable fort artifactory versioning. 

146 """ 

147 return version.replace(".", "_").replace("-", "_").lower() 

148 

149 @property 

150 def _done_token(self): 

151 """ 

152 Path to "done" token file. 

153 """ 

154 return self._output_path / "done.txt"