Skip to content

Commit

Permalink
Merge pull request #48 from IBM/refactor/generating_makefile_in_python
Browse files Browse the repository at this point in the history
Refactor/generating makefile in python
  • Loading branch information
edmundreinhardt authored Dec 16, 2021
2 parents c2cc033 + fa7aa56 commit 1db7f33
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 61 deletions.
3 changes: 2 additions & 1 deletion bob.spec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
%undefine _disable_source_fetch
Name: bob
Version: 2.1.3
Version: 2.2.1
Release: 0
License: Apache-2.0
Summary: Better Object Builder for IBM i
Expand All @@ -19,6 +19,7 @@ Requires: sed-gnu
Requires: grep-gnu
Requires: gawk
Requires: make-gnu
Requires: python >= 3.4

Source0: https://github.com/IBM/ibmi-bob/archive/refs/tags/v%{version}.tar.gz
Source1: https://github.com/BrianGarland/CRTFRMSTMF/archive/16db76aba5c94243396297f022a0dfc39dd4f8ee.tar.gz
Expand Down
64 changes: 4 additions & 60 deletions makei
Original file line number Diff line number Diff line change
Expand Up @@ -112,64 +112,6 @@ parse_placeholder() {
fi
}

recursive_generate() {
local dir=$1
local parentObjLoc=${2//&/}
local parentCcsid=$3
local objlibraw
local tgtCcsid
cd "$dir" || (
echo "${dir} not found"
exit 1
)

if [[ -f ".ibmi.json" ]]; then
objlibraw=$(retrieve_from_ibmijson ".ibmi.json" "build.objlib" "")
tgtCcsid=$(retrieve_from_ibmijson ".ibmi.json" "build.tgtCcsid" "")
fi
local objlib=$(parse_placeholder "$objlibraw")
if test $? -ne 0; then
exit 1
fi

if [[ $objlib == "*CURLIB" ]]; then
if [[ $curlib == "*CRTDFT" ]]; then
objlib="QGPL"
else
objlib=$curlib
fi
fi

if [ -z "${objlib}" ]; then
local objPath=
else
local objPath=/QSYS.LIB/${objlib}.LIB
fi

# If ObjLoc and tgtCcsid are not defined, then use the parent's configuration
objPath="${objPath:-$parentObjLoc}"
tgtCcsid="${tgtCcsid:-$parentCcsid}"

if [[ -f "Rules.mk" ]]; then
echo "TGTCCSID_$(pwd) := ${tgtCcsid}" >>"$BUILDVARSMK"
echo "OBJPATH_$(pwd) := $objPath" >>"$BUILDVARSMK"

grep -v ":=" Rules.mk | grep -v "#" | cut -d':' -f1 | grep . | sort | uniq | while read line ; do
echo "${line}_d := $dir" >>"$BUILDVARSMK"
done

for f in "$dir"/*; do
if [[ -d $f ]]; then
recursive_generate "$f" "$objPath" "$tgtCcsid"
if test $? -ne 0; then
exit 1
fi
cd ..
fi
done
fi
}

main() {
exitCode=0

Expand All @@ -194,6 +136,7 @@ main() {
MAKEFILE="${srcDir}/Makefile"
fi


echo -e "${BLUE}makei: Reading from iproj.json${NOCOLOR}"
objlib=$(parse_placeholder $(retrieve_from_iproj objlib "*CURLIB"))
curlib=$(parse_placeholder $(retrieve_from_iproj curlib ""))
Expand Down Expand Up @@ -240,7 +183,7 @@ main() {
echo "preUsrlibl := ${preUsrlibl}"
echo "postUsrlibl := ${postUsrlibl}"
echo "INCDIR := ${includePath}"

while read -r cmd; do
IBMiEnvCmd+="cl '$cmd' > /dev/null 2>&1 && "
done <<<"$IBMiEnvCmdList"
Expand All @@ -250,7 +193,8 @@ main() {

echo -e "${BLUE}Generating makefile for build variables at ${BUILDVARSMK}${NOCOLOR}"
echo
recursive_generate "$srcDir" "/QSYS.LIB/${objlib}.LIB" "$tgtCcsid"
# time recursive_generate "$srcDir" "/QSYS.LIB/${objlib}.LIB" "$tgtCcsid"
python3 $MK/genBuildVars.py $BUILDVARSMK $srcDir
ibmijsonError=$?

targetName=$(get_target_from_srcfile "$filename");
Expand Down
103 changes: 103 additions & 0 deletions mk/genBuildVars.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import argparse
import os
from pathlib import Path
from glob import glob
import json


def parse_args():
parser = argparse.ArgumentParser(description='Generate a single makefile of build variables from ibmi.json')
parser.add_argument('target_file', help='The generated file path')
parser.add_argument('proj_dir', help='The project path')
parsed_args = parser.parse_args()
return parsed_args


def read_ibmi_json(path, parent_value):
if path.exists():
with path.open() as f:
data = json.load(f)
try:
objlib = parse_placeholder(data['build']['objlib'])

except Exception:
objlib = parent_value[0]
try:
tgtCcsid = data['build']['tgtCcsid']
except Exception:
tgtCcsid = parent_value[1]
return (objlib, tgtCcsid)
else:
return parent_value

def parse_placeholder(varName):
if varName.startswith("&") and len(varName) > 1:
varName = varName[1:]
try:
value = os.environ[varName]
return value
except Exception:
print(f"{varName} must be defined first in the environment variable.")
else:
return varName

def read_iproj_json(iproj_json_path):
with iproj_json_path.open() as f:
iproj_json = json.load(f)
objlib = parse_placeholder(iproj_json["objlib"]) if "objlib" in iproj_json else ""
curlib = parse_placeholder(iproj_json["curlib"]) if "curlib" in iproj_json else ""
if objlib == "*CURLIB":
if curlib == "*CRTDFT":
objlib="QGPL"
else:
objlib=curlib

return {
"objlib": objlib,
"curlib": curlib,
"tgtCcsid": iproj_json["tgtCcsid"] if "tgtCcsid" in iproj_json else "*JOB",
}

def objlib_to_path(objlib):
return f"/QSYS.LIB/{objlib}.LIB"


def main(args):
target_file_path = Path(args.target_file)
proj_dir_path = Path(args.proj_dir)
os.chdir(proj_dir_path)

iproj_json = read_iproj_json(proj_dir_path / "iproj.json")

# ibmi_jsons = list(Path(".").rglob(".ibmi.json"))
rules_mks = list(Path(".").rglob("Rules.mk"))
subdirs = list(map(lambda x: x.parents[0], rules_mks))

subdirs.sort(key=lambda x: len(x.parts))
dir_var_map = {Path('.'): (iproj_json['objlib'], iproj_json['tgtCcsid'])}


def map_ibmi_json_var(path):
if path != Path("."):
dir_var_map[path] = read_ibmi_json(path / ".ibmi.json", dir_var_map[path.parents[0]])

list(map(map_ibmi_json_var, subdirs))

with target_file_path.open("a") as f:
for subdir in subdirs:
f.write(f"TGTCCSID_{subdir.absolute()} := {dir_var_map[subdir][1]}\n")
f.write(f"OBJPATH_{subdir.absolute()} := {objlib_to_path(dir_var_map[subdir][0])}\n")

for rules_mk in rules_mks:
with rules_mk.open('r') as r:
ls = r.readlines()
for l in ls:
l = l.rstrip()
if l and not l.startswith("#") and not "=" in l and not l.startswith((' ', '\t')):
f.write(f"{l.split(':')[0]}_d := {rules_mk.parents[0].absolute()}\n")



if __name__ == "__main__":
args = parse_args()
main(args)

0 comments on commit 1db7f33

Please sign in to comment.