From 5c773a81dc7af53a81d24df506d17871f29efd35 Mon Sep 17 00:00:00 2001
From: Jonathan Hanks <jonathan.hanks@ligo.org>
Date: Tue, 31 Aug 2021 11:04:55 -0700
Subject: [PATCH] Fix for #229 in the install post_build_script.py

 * substitute $bdroot with the system name when resolving parts.
---
 src/epics/util/post_build_script.py | 22 +++++++++++++---------
 1 file changed, 13 insertions(+), 9 deletions(-)

diff --git a/src/epics/util/post_build_script.py b/src/epics/util/post_build_script.py
index ec0fd547c..91ff39da7 100755
--- a/src/epics/util/post_build_script.py
+++ b/src/epics/util/post_build_script.py
@@ -22,6 +22,7 @@ else:
 
 ifo_initial = model_name[0:2]
 site_initial = model_name[0]
+sysname = model_name[2:]
 global top_names
 top_names = None
 global model_params
@@ -115,7 +116,7 @@ class Block:
     self.subblocks = []
 
 #Parses a "block" of an .mdl file and returns the corresponding Block structure along with the last line number parsed
-def parse_block(data_lines,line_number,reference_name):
+def parse_block(data_lines,line_number,reference_name,system_name):
   new_block = Block()
   ###Normal, non-reference parsing
   if reference_name == None:
@@ -125,14 +126,14 @@ def parse_block(data_lines,line_number,reference_name):
       current_line = data_lines[line_number]
       #We encounter a new block definition so start a new node
       if current_line.split()[-1].strip() == '{':
-        temp_block, line_number = parse_block(data_lines,line_number,None)
+        temp_block, line_number = parse_block(data_lines,line_number,None, system_name)
         #Check to see if we've found a reference block
         try:
           if (temp_block.data['BlockType'] == 'Reference') &  (not (simulink_reference[0] in temp_block.data['SourceBlock'])) & (not (simulink_reference[1] in temp_block.data['SourceBlock'])):
             #If we did find a reference block, get the reference data
             current_name = temp_block.data['Name']
-            library_lines = find_library(temp_block.data['SourceBlock'])
-            scratch_block, scratch = parse_block(library_lines,0,temp_block.data['SourceBlock'])
+            library_lines = find_library(temp_block.data['SourceBlock'], system_name)
+            scratch_block, scratch = parse_block(library_lines,0,temp_block.data['SourceBlock'], system_name)
             #Keep the farthest back reference description for screen generation as well - allows changes to just the library part
             if ('Description' in list(scratch_block.data.keys())):
               if not ('Reference_Descrip' in list(scratch_block.data.keys())):
@@ -215,7 +216,7 @@ def parse_block(data_lines,line_number,reference_name):
         sys.stderr.write("Exiting\n")
         sys.exit(1)
 
-    new_block,scratch = parse_block(data_lines,current_line_count,None)
+    new_block,scratch = parse_block(data_lines,current_line_count,None, system_name)
     if description_present:
       new_block.data['Description'] = temp_description
     return new_block, scratch
@@ -223,8 +224,11 @@ def parse_block(data_lines,line_number,reference_name):
 
 #This function goes and finds the location of part's corresponding library .mdl
 #It then returns all lines from the library .mdl file.
-def find_library(library_name):
+def find_library(library_name, system_name):
   reference_file_name = re.search('([^\/]*)',library_name).group(1).strip('"').strip('/')
+  if reference_file_name == '$bdroot':
+    print('substituting {0} for $bdroot'.format(system_name))
+    reference_file_name = system_name
   #reference_file_name = re.search('(.*/(?!/))(.*)',library_name).group(1).strip('"').strip('/')
   for path in rcg_lib_path.split(':'):
     full_reference_path = path + '/' + reference_file_name + '.mdl'
@@ -243,12 +247,12 @@ def find_library(library_name):
   sys.exit(1)
 
 #This function is the top level function which recursively goes through the mdl file
-def parse_simulink_file(data_lines):
+def parse_simulink_file(data_lines, system_name):
   line_number = 0
   root = Block()
   root.data['MyBlockType'] = 'File'
   while (line_number < len(data_lines)-1):
-    temporary_block, line_number = parse_block(data_lines, line_number,None)
+    temporary_block, line_number = parse_block(data_lines, line_number,None, system_name)
     root.subblocks.append(temporary_block)
   return root
 
@@ -442,7 +446,7 @@ mdl_data = mdl_file.readlines()
 mdl_file.close()
 
 #Begin parsing the model
-root_block = parse_simulink_file(mdl_data)
+root_block = parse_simulink_file(mdl_data,sysname)
 
 #Figure out DCU_ID
 #Find the cdsParameter block
-- 
GitLab