I want to create a builder in scons works with COBOL.
Here is a start:
import re
Import('env')
# Source:
# src/cpy/COPYBK1.cpy
# src/cpy/COPYBK2.cpy
# src/cpy/COPYBK3.cpy
# src/bat/PROG1.cbl
# src/bat/PROG2.cbl
# These commands would run:
# cobc -o lib/PROG1.so -Isrc/cpy src/cbl/PROG1.cbl
# cobc -o lib/PROG2.so -Isrc/cpy src/cbl/PROG2.cbl
# +-.
# +-SConstruct
# +-PROG1.cbl
# +-PROG1.so
# | +-PROG1.cbl
# | +-COPYBK1.cpy
# | +-COPYBK2.cpy
# +-PROG2.cbl
# +-PROG2.so
# | +-PROG2.cbl
# | +-COPYBK1.cpy
# | +-COPYBK3.cpy
#
# Also, PROG2 is called from PROG1 so lib/PROG2.so target should be automatically generated.
# PROG2 is dynamically loaded so it does not need to linked into PROG1 target.
"""
def getCalls(fullprogrampath):
# This needs to be modified to support multiple lines.
# This needs to be modified to support nested COPY.
theregex = r'^......]\s*CALL\s*([A-Z0-9]*)\.$'
calllist = []
with open(fullprogrampath, 'r') as f:
linenum = 0
for line in f.readlines():
linenum += 1
line = line.rstrip()
m = re.match(theregex, line, re.I)
if m:
calllist.append(m.group(1))
return(calllist)
"""
def getCopyBooks(fullprogrampath):
# This needs to be modified to support multiple lines.
# This needs to be modified to support nested COPY.
theregex = r'^...... \s*COPY\s*([A-Z0-9]*)\.$'
copybooklist = []
with open(fullprogrampath, 'r') as f:
linenum = 0
for line in f.readlines():
linenum += 1
line = line.rstrip()
m = re.match(theregex, line, re.I)
if m:
copybooklist.append(m.group(1))
return(copybooklist)
bld = Builder(action = 'cobc -o $TARGET -Icpy $SOURCE')
env.Append(BUILDERS = {'CobolProgram': bld})
env.CobolProgram('lib/PROG1.so', 'bat/PROG1.cbl')
env.Depends(target = 'lib/PROG1.so', dependency = getCopyBooks('bat/PROG1.cbl'))
env.CobolProgram('lib/PROG2.so', 'cbl/PROG2.cbl')
env.Depends(target = 'lib/PROG2.so', dependency = getCopyBooks('cbl/PROG2.cbl'))
That actually runs
here is what needs to be added:
- Cache the scan for copybooks so that we only scan for copybooks if the files have changed. Scons seems to do that for C files so it must be possible.
- Detect call statements and add targets. There is no analogy to this in C.
A. How do I do that?
B. Is there some sample builders I can look at that I can model off of?
I acknowledge that COBOL is difficult to scan and that it will be limited to how crazy the COBOL is formatted. it will be up to the developer of the COBOL to add Depends
calls for their copybooks and call statements that are not detected.