24
24
from __future__ import print_function
25
25
import sys
26
26
import os
27
+ import tempfile
27
28
import subprocess
28
29
29
30
@@ -33,7 +34,28 @@ def __init__(self, filelist, outputfile='pp.out', include=None, define=None):
33
34
if not isinstance (filelist , (tuple , list )):
34
35
filelist = list (filelist )
35
36
36
- self .filelist = filelist
37
+ # Elements in `filelist` can either be raw Verilog files, or Verilog code
38
+ # in python string. The following loop iterates through these `sources`,
39
+ # and normalizes all of them into files.
40
+ #
41
+ # For Verilog code in python string, the contents of the string is stored
42
+ # in a temporary file for further use with `iverilog`.
43
+ self .temp_files_paths = []
44
+ self .filelist = []
45
+
46
+ for source in filelist :
47
+ # If `source` is verilog code in python strings
48
+ if not os .path .isfile (source ):
49
+ temp_fd , temp_path = tempfile .mkstemp (prefix = "pyverilog_temp_" , suffix = ".v" )
50
+ with open (temp_fd , 'w' ) as f :
51
+ f .write (source )
52
+
53
+ self .temp_files_paths .append (temp_path )
54
+
55
+ else : # else if it is normal verilog file path
56
+ self .filelist .append (source )
57
+
58
+ self .filelist += self .temp_files_paths
37
59
38
60
iverilog = os .environ .get ('PYVERILOG_IVERILOG' )
39
61
if iverilog is None :
@@ -63,6 +85,10 @@ def preprocess(self):
63
85
cmd = self .iv + list (self .filelist )
64
86
subprocess .call (cmd )
65
87
88
+ # Removing the temporary files that were created
89
+ for temp_file_path in self .temp_files_paths :
90
+ os .remove (temp_file_path )
91
+
66
92
67
93
def preprocess (
68
94
filelist ,
0 commit comments