From e314c24db4f436efd8eabc401bfc13eea2e4e015 Mon Sep 17 00:00:00 2001
From: Pawel Szostek <pawel.szostek@cern.ch>
Date: Thu, 7 Apr 2011 12:08:21 +0000
Subject: [PATCH] Add ISE list generating

---
 synthesis/depend.py  |  63 +++++++++++++++++++-------
 synthesis/hdlmake.py | 102 ++++++++++++++++++++++++-------------------
 synthesis/path.py    |  18 +++++---
 3 files changed, 116 insertions(+), 67 deletions(-)

diff --git a/synthesis/depend.py b/synthesis/depend.py
index 1551fb03..0d3b5446 100644
--- a/synthesis/depend.py
+++ b/synthesis/depend.py
@@ -43,7 +43,7 @@ def search_for_use(file):
             ret.append((m.group(1),m.group(2)))
     f.close()
     return ret
-    
+
 def search_for_package(file):
     """
     Reads a file and looks for package clase. Returns list of packages' names
@@ -55,7 +55,7 @@ def search_for_package(file):
     if text is None:
           return []
     text = text.split("\n")
-    
+
     ret = []
     package_pattern = re.compile("^[ \t]*package[ \t]+([^ \t]+)[ \t]+is[ \t]*$")
     for line in text:
@@ -64,11 +64,12 @@ def search_for_package(file):
             ret.append(m.group(1))
     f.close()
     return ret
+
 def generate_deps_for_sv_files(files):
     def search_for_sv_include(file):
         f = open(file,"r")
         text = f.readlines()
-        
+
         ret = []
         package_pattern = re.compile("^[ \t]*`include[ \t]+\"([^ \t]+)\"[ \t]*$")
         for line in text:
@@ -83,32 +84,32 @@ def generate_deps_for_sv_files(files):
     file_file_dict = {}
     for file in files:
         file_file_dict[file] = search_for_sv_include(file)
-        
+
 def generate_deps_for_vhdl_in_modules(modules_paths):
     if not isinstance(modules_paths, list):
         modules_paths = [modules_paths]
-        
+
     from hdlmake import search_for_manifest
     module_manifest_dict = {} 
     for module in modules_paths:
         module_manifest_dict[module] = search_for_manifest(module)
-        
+
     from hdlmake import parse_manifest
     opt_map_dict = {}
     for module in list(module_manifest_dict.keys()):
         if module_manifest_dict[module] != None:
             opt_map_dict[module] = parse_manifest(module_manifest_dict[module])
-    
+
     module_files_dict = {}
     from path import make_list_of_files
     for module in modules_paths:
         if module in opt_map_dict:
-            module_files_dict[module] = make_list_of_files(module, opt_map_dict[module].files, os.path.dirname(module_manifest_dict[module]))
+            module_files_dict[module] = make_list_of_files(module, opt_map_dict[module].files, os.path.dirname(module_manifest_dict[module]), file_type="vhd")
         else:
-            module_files_dict[module] = make_list_of_files(module)
+            module_files_dict[module] = make_list_of_files(module, file_type="vhd")
     all_files = []
     file_lib_dict = {}
-    
+
     for k in module_files_dict:
         for x in module_files_dict[k]:
             if k in opt_map_dict:
@@ -117,15 +118,16 @@ def generate_deps_for_vhdl_in_modules(modules_paths):
                 file_lib_dict[os.path.abspath(x)] = "work"
             all_files.append(os.path.abspath(x))
     all_files = list(set(all_files))
+    p.pprint(all_files)
     #all_files = [(opt_map_dict[k].library, os.path.abspath(x)) for k in module_files_dict for x in module_files_dict[k]]
     all_vhdl_files = [x for x in all_files if os.path.splitext(x)[1] == '.vhd']
     p.vpprint(all_vhdl_files)
-    
+
     file_use_clause_dict = {}
     for file in all_vhdl_files:
         file_use_clause_dict[file] = search_for_use(file)
     p.vpprint(file_use_clause_dict)    
-    
+
     package_file_dict = {}
     for file in all_vhdl_files:
         packages = search_for_package(file) #look for package definitions
@@ -142,10 +144,10 @@ def generate_deps_for_vhdl_in_modules(modules_paths):
             p.echo("There might be a problem... Compilation unit " + file_purename +
                 " has several instances:\n\t" + file + "\n\t" + package_file_dict[file_purename])
         package_file_dict[file_purename.lower()] = file
-    
+
     p.vpprint(package_file_dict)
     p.vpprint(file_lib_dict)
-    
+
     file_file_dict = {}
     for file in all_vhdl_files:
         file_units_list = file_use_clause_dict[file]
@@ -169,6 +171,37 @@ def modelsim_ini_path():
     vsim_path = os.popen("which vsim").read().strip()
     bin_path = os.path.dirname(vsim_path)
     return os.path.abspath(bin_path+"/../")
+
+def generate_list_makefile(file_deps_dict, file_lib_dict):
+    from time import gmtime, strftime
+    import path
+    #p.vpprint(file_deps_dict)
+    #p.vpprint(file_lib_dict)
+    #from path import relpath as rp
+    date = strftime("%a, %d %b %Y %H:%M:%S", gmtime())
+    notices = """#######################################################################
+#   This makefile has been automatically generated by hdl-make 
+#   for project """ + path.url_basename(global_mod.cwd) + """ on """ + date + """
+#######################################################################
+
+"""
+    filename = "ise_list"
+    rp = os.path.relpath
+    f = open("Makefile_list", "w")
+    f.write(notices)
+    f.write("file: create_a_file\n")
+    f.write("create_a_file:\n\t\t@printf \"\" > " + filename + "\n")
+    f.write("file: ")
+    for file in file_deps_dict:
+        f.write(rp(file)+"__print \\\n")
+    f.write("\n")
+    for file in file_deps_dict:
+        f.write(rp(file)+"__print: ")
+        f.write(' '.join( rp(x)+"__print" for x in file_deps_dict[file]))
+        f.write('\n')
+        f.write("\t\t@echo \'"+file_lib_dict[file]+';'+rp(file)+"\' >> ise_list\n\n")
+    f.write("\t\t@echo Done.")
+
 def generate_makefile(file_deps_dict, file_lib_dict):
     from time import gmtime, strftime
     import path
@@ -226,7 +259,7 @@ clean:
     f.write('LIBS := ')
     f.write(' '.join(libs))
     f.write('\n')
-    #tell hwo to make libraries
+    #tell how to make libraries
     f.write('LIB_IND := ')
     f.write(' '.join([lib+"/."+lib for lib in libs]))
     f.write('\n')
diff --git a/synthesis/hdlmake.py b/synthesis/hdlmake.py
index f03bd958..bad65cca 100755
--- a/synthesis/hdlmake.py
+++ b/synthesis/hdlmake.py
@@ -29,7 +29,7 @@ def parse_repo_url(url) :
     """
     url_pat = re.compile("[ \t]*([^ \t]+)[ \t]*(@[ \t]*(.+))?[ \t]*")
     url_match = re.match(url_pat, url)
-    
+
     if url_match == None:
         p.echo("Skipping")
         raise RuntimeError("Not a correct repo url: " + url)
@@ -38,7 +38,31 @@ def parse_repo_url(url) :
     else:
         ret = url_match.group(1)
     return ret
-    
+
+def make_list_of_modules(top_manifest, top_opt_map):
+    cur_manifest = top_manifest 
+    modules = []
+    new_manifests = []
+    opt_map = top_opt_map
+    while True:
+        if opt_map.local != None:
+            modules.extend(opt_map.local)
+            for i in opt_map.local:
+                manifest = search_for_manifest(i)
+                if manifest != None:
+                    new_manifests.append(manifest)
+
+        if len(new_manifests) == 0:
+            break;
+        cur_manifest = new_manifests.pop()
+        opt_map = parse_manifest(cur_manifest)
+
+    if os.path.exists(global_mod.hdlm_path):
+        modules += [global_mod.hdlm_path+"/"+x for x in os.listdir(global_mod.hdlm_path)]
+    if len(modules) == 0:
+        p.vprint("No modules were found in " + global_mod.hdlm_path)
+    return modules
+
 def check_module_and_append(list, module):
     """
     Appends a module to the list if it doesn't belong to it. If it is already there, complain
@@ -52,9 +76,9 @@ def check_module_and_append(list, module):
             return 1
     list.append(module)
     return 0
-    
+
 def parse_manifest(manifest_file):
-    
+
     def make_list(sth):
         if sth != None:
             if not isinstance(sth, (list,tuple)):
@@ -62,9 +86,9 @@ def parse_manifest(manifest_file):
         else:
             sth = []
         return sth
-        
+
     manifest_path = os.path.dirname(manifest_file)
-    
+
     manifest_parser = cfgparse.ConfigParser(allow_py = True)
     manifest_parser.add_option('root', default=None)
     manifest_parser.add_option('name', default=None)
@@ -77,13 +101,13 @@ def parse_manifest(manifest_file):
     manifest_parser.add_option('rtl', default=None)
     manifest_parser.add_option('files', default=None)
     manifest_parser.add_file(manifest_file)
-    
+
     #Take configuration parser from the global namespace
     opt_map = manifest_parser.parse()
-    
+
     if opt_map.root == None:
         opt_map.root = "."
-        
+
     if opt_map.rtl == None:
         opt_map.rtl = ["."]
     elif not isinstance (opt_map.rtl, list):
@@ -91,7 +115,7 @@ def parse_manifest(manifest_file):
 
     if opt_map.ise == None:
         opt_map.ise = "13.1"
-        
+
     opt_map.local = make_list(opt_map.local) 
     for i in opt_map.local:
         if path.is_abs_path(i):
@@ -108,14 +132,14 @@ def convert_xise(xise):
     if not os.path.exists(xise):
         p.echo("Given .xise file does not exist:" + xise)
         quit()
-        
+
     modules = ["."]
     modules.append(global_mod.hdlm_path)
     files = make_list_of_files(modules)
-    
+
     ise = open(xise, "r")
     ise_lines = [x.strip() for x in ise.readlines()]
-    
+
     new_ise = []
     file_pattern = re.compile('([ \t]*<file xil_pn:name=")([^"]+)(".*>)')
     #print ise_lines
@@ -136,7 +160,7 @@ def convert_xise(xise):
                 new_ise.append(line)
         else:
             new_ise.append(line + "\n")
-    
+
     new_ise_file = open(xise + ".new", "w")
 
 def search_for_manifest(search_path):
@@ -177,16 +201,17 @@ def main():
     parser.add_option("-o", "--convert-xise", dest="xise", default=None, help="convert paths in the given XISE_FILE", metavar="XISE_FILE")
     parser.add_option("-s", "--synth-server", dest="synth_server", default=None, help="use given SERVER for remote synthesis", metavar="SERVER")
     parser.add_option("-u", "--synth-user", dest="synth_user", default=None, help="use given USER for remote synthesis", metavar="USER")
-    parser.add_option("--no-del", dest="no_del", default=None, help="do not delete catalog after remote synthesis")
+    parser.add_option("--make-list", dest="make_list", action="store_true", default=None, help="make list of project files in ISE format")
+    parser.add_option("--no-del", dest="no_del", action="store_true", default=None, help="do not delete catalog after remote synthesis")
     (global_mod.options, args) = parser.parse_args()
     
 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
     #Check if any option was selected
-    if global_mod.options.local == global_mod.options.fetch == global_mod.options.remote == global_mod.options.make == global_mod.options.clean == None:
-        import sys
-        p.echo("Are you sure you didn't forget to specify an option? At least one?")
-        p.echo("Maybe you should try " + sys.argv[0] + " -h") 
-        quit()
+#    if global_mod.options.local == global_mod.options.fetch == global_mod.options.remote == global_mod.options.make == global_mod.options.clean == None:
+#        import sys
+#        p.echo("Are you sure you didn't forget to specify an option? At least one?")
+#        p.echo("Maybe you should try " + sys.argv[0] + " -h") 
+#        quit()
 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
     if global_mod.options.xise != None:
         convert_xise(global_mod.options.xise)
@@ -368,35 +393,20 @@ def main():
         if global_mod.options.no_del != True:
             p.echo("Deleting synthesis folder")
             global_mod.ssh.system('rm -rf ' + dest_folder)
-        
+# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
+    if global_mod.options.make_list == True:
+        import depend
+        modules = make_list_of_modules(global_mod.top_manifest, global_mod.opt_map)
+
+        p.vprint("Modules that will be taken into account in the makefile: " + str(modules))
+        deps, libs = depend.generate_deps_for_vhdl_in_modules(modules)
+        depend.generate_list_makefile(deps, libs)
+
 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
     if global_mod.options.make == True:
         import depend
-        
-        cur_manifest = global_mod.top_manifest 
-        involved_modules = []
-        new_manifests = []
-        opt_map = global_mod.opt_map
-        while True:
-            if opt_map.local != None:
-                involved_modules.extend(opt_map.local)
-                for i in opt_map.local:
-                    manifest = search_for_manifest(i)
-                    if manifest != None:
-                        new_manifests.append(manifest)
-                    
-            if len(new_manifests) == 0:
-                break;
-            cur_manifest = new_manifests.pop()
-            opt_map = parse_manifest(cur_manifest)
-            
-        modules = involved_modules
-        if os.path.exists(global_mod.hdlm_path):
-            modules += [global_mod.hdlm_path+"/"+x for x in os.listdir(global_mod.hdlm_path)]
-        if len(modules) == 0:
-            p.vprint("No modules were found in " + global_mod.hdlm_path)
-        
-        #modules += global_mod.opt_map.rtl
+        modules = make_list_of_modules(global_mod.top_manifest, global_mod.opt_map)
+
         p.vprint("Modules that will be taken into account in the makefile: " + str(modules))
         deps, libs = depend.generate_deps_for_vhdl_in_modules(modules)
         depend.generate_makefile(deps, libs)
diff --git a/synthesis/path.py b/synthesis/path.py
index 7858073e..b1b3bd4d 100644
--- a/synthesis/path.py
+++ b/synthesis/path.py
@@ -60,11 +60,11 @@ def rel2abs(path, base = os.curdir):
     retval = os.path.join(base,path)
     return os.path.abspath(retval)
     
-def make_list_of_files(modules, take_files = None, base_folder = None):
+def make_list_of_files(modules, take_files = None, base_folder = None, file_type = None):
     """
     Make list of all files included in the list of folders
     """
-    def getfiles(path):
+    def getfiles(path, file_type = None):
         """
         Get lists of normal files and list folders recursively
         """
@@ -73,17 +73,23 @@ def make_list_of_files(modules, take_files = None, base_folder = None):
             if filename[0] == ".": #a hidden file/catalogue -> skip
                 continue
             if os.path.isdir(path + "/" + filename):
-                ret.extend(getfiles(path + "/" + filename))
+                ret.extend(getfiles(path + "/" + filename, file_type))
             else:
-                ret.append(path + "/" + filename)
+                if file_type == None:
+                    ret.append(path + "/" + filename)
+                else:
+                    tmp = filename.rsplit('.')
+                    ext = tmp[len(tmp)-1]
+                    if ext == file_type:
+                        ret.append(path + '/' + filename)
         return ret
         
     files = []
     if not isinstance(modules, list):
-        files =  getfiles(modules)
+        files =  getfiles(modules, file_type)
     else:
         for module in modules:
-            files.extend(getfiles(module))
+            files.extend(getfiles(module, file_type))
             
     if take_files != None and take_files != []:
         ret_files = []
-- 
GitLab