2424from __future__ import print_function
2525import sys
2626import os
27+ import tempfile
2728import subprocess
2829
2930
@@ -33,7 +34,28 @@ def __init__(self, filelist, outputfile='pp.out', include=None, define=None):
3334 if not isinstance (filelist , (tuple , list )):
3435 filelist = list (filelist )
3536
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
3759
3860 iverilog = os .environ .get ('PYVERILOG_IVERILOG' )
3961 if iverilog is None :
@@ -63,6 +85,10 @@ def preprocess(self):
6385 cmd = self .iv + list (self .filelist )
6486 subprocess .call (cmd )
6587
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+
6692
6793def preprocess (
6894 filelist ,
0 commit comments