Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function putTable(tbl, v)
- tbl.n = tbl.n + 1
- tbl[tbl.n] = v
- end
- local function tblSize(tbl)
- return tbl.n
- end
- local function tblToString(tbl, delimiter)
- local rstr = ""
- for k, v in ipairs(tbl) do
- if rstr:len() > 0 then
- rstr = rstr .. delimiter
- end
- rstr = rstr .. tostring(v)
- end
- return rstr
- end
- local function removeFileNameExt(name)
- return name:gsub("^(.+)%..-$", "%1")
- end
- local function asmAreaModifications(asmText)
- local ret = asmText:gsub("%.area _INITIALIZED.-(%.area)", "%1"):gsub("_INITIALIZER", "_INITIALIZED")
- ret = ret:gsub("(%.area _INITIALIZED.-%.area)", function(s)
- return s:gsub("__xinit_(.-):", "%1::")
- end)
- return ret
- end
- --print("SDCC MAKE script\n================\n\n")
- print("Reading makefile...")
- local makefile = io.open("makefile", "rb")
- local makestring = makefile:read("*all")
- makefile:close()
- local outfile, sdccoptions, linkoptions, cfiles, relfiles, asmfiles, binsize, nolink, createlib = nil, "", "", {n = 0}, {n = 0}, {n = 0}, 8192, false, false
- local exec_before, exec_after = {n = 0}, {n = 0}
- for line in makestring:gmatch("[^\r\n]+") do
- local trimmed = line:gsub("^ *(.-) *$", "%1")
- if (type(trimmed) == "string") and (trimmed:len() ~= 0) then
- local cmd, arg = trimmed:gsub("^([^ ]+) .*$", "%1"), trimmed:gsub("^[^ ]+ (.*)$", "%1")
- if cmd:upper() == "O" then
- sdccoptions = arg
- elseif cmd:upper() == "C" then
- putTable(cfiles, arg)
- elseif cmd:upper() == "R" then
- putTable(relfiles, arg)
- elseif cmd:upper() == "S" then
- putTable(asmfiles, arg)
- elseif cmd:upper() == "L" then
- linkoptions = arg
- elseif cmd:upper() == "F" then
- outfile = arg
- elseif cmd:upper() == "B" then
- binsize = tonumber(arg)
- elseif cmd:upper() == "NOLINK" then
- if arg:upper() == "TRUE" then
- nolink = true
- end
- elseif cmd:upper() == "LIB" then
- if arg:upper() == "TRUE" then
- createlib = true
- end
- elseif cmd:upper() == "EXEC_BEFORE" then
- putTable(exec_before, arg)
- elseif cmd:upper() == "EXEC_AFTER" then
- putTable(exec_after, arg)
- end
- end
- end
- print()
- print("C files: " .. tblToString(cfiles, " "))
- print("ASM files: " .. tblToString(asmfiles, " "))
- print("REL files: " .. tblToString(relfiles, " "))
- --print("Compiler options: " .. sdccoptions)
- --print("Linker options: " .. linkoptions)
- local anyError = false
- io.open("__buildnumber", "a+"):close()
- local hbuild = io.open("__buildnumber", "rb")
- local bnumber = tonumber("0" .. hbuild:read("*all")) + 1
- hbuild:close()
- hbuild = io.open("__buildnumber", "wb")
- hbuild:write(tostring(bnumber))
- hbuild:close()
- print("\nExecuting scripts...")
- for k, v in ipairs(exec_before) do
- print(v)
- os.execute(v)
- end
- print("\nCompiling...")
- for k, v in ipairs(cfiles) do
- local compErr = false
- if os.execute("sdcc -DBUILD_NUMBER=" .. tostring(bnumber) .. " -S " .. sdccoptions .. " " .. v) ~= 0 then
- anyError = true
- compErr = true
- end
- local asmFile = removeFileNameExt(v) .. ".asm"
- if not compErr then
- local asmFileH = io.open(asmFile, "rb")
- local asmFileString = asmFileH:read("*all")
- asmFileH:close()
- asmFileH = io.open(asmFile, "wb")
- local asmFileStringMod = asmAreaModifications(asmFileString)
- asmFileH:write(asmFileStringMod)
- asmFileH:close()
- putTable(asmfiles, asmFile)
- end
- end
- print("\nAssembling...")
- for k, v in ipairs(asmfiles) do
- local relFile = removeFileNameExt(v) .. ".rel"
- if os.execute("sdasz80 -g -o " .. relFile .. " " .. v) ~= 0 then
- anyError = true
- end
- putTable(relfiles, relFile)
- end
- if not outfile then
- if tblSize(cfiles) >= 1 then
- outfile = removeFileNameExt(cfiles[1]) .. ".ihx"
- else
- outfile = removeFileNameExt(relfiles[1]) .. ".ihx"
- end
- end
- print()
- if (not anyError) and (not nolink) and (not createlib) then
- print("Linking...")
- local fileList = tblToString(relfiles, " ")
- print("REL file order: " .. fileList)
- io.open(outfile, "wb"):close()
- if os.execute("sdldz80 -nmwx -i " .. outfile .. " " .. linkoptions .. " " .. fileList .. " -e") ~= 0 then
- --if os.execute("sdcc -o " .. outfile .. " " .. linkoptions .. " " .. fileList) ~= 0 then
- anyError = true
- end
- --print("sdldz80 -m " .. mapfile .. " -i " .. outfile .. " " .. linkoptions .. " " .. fileList .. " -e")
- end
- if (not anyError) and createlib then
- print("Creating library...")
- outfile = removeFileNameExt(outfile) .. ".lib"
- local fileList = tblToString(relfiles, " ")
- --io.open(outfile, "wb"):close()
- if os.execute("sdar -rc " .. outfile .. " " .. fileList) ~= 0 then
- anyError = true
- end
- end
- if (not anyError) and (not nolink) and (not createlib) then
- local binfile = removeFileNameExt(outfile) .. ".bin"
- os.execute("makebin -p -s " .. tostring(binsize) .. " " .. outfile .. " " .. binfile)
- end
- if anyError then
- print("\nThere were errors during the build process.")
- else
- print("\nExecuting scripts...")
- for k, v in ipairs(exec_after) do
- print(v)
- os.execute(v)
- end
- print("\nBuild successful.")
- end
- os.execute("pause")
Advertisement
Add Comment
Please, Sign In to add comment