Guest User

Untitled

a guest
Jul 29th, 2016
5
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.01 KB | None | 0 0
  1. local function putTable(tbl, v)
  2.     tbl.n = tbl.n + 1
  3.     tbl[tbl.n] = v
  4. end
  5.  
  6. local function tblSize(tbl)
  7.     return tbl.n
  8. end
  9.  
  10. local function tblToString(tbl, delimiter)
  11.     local rstr = ""
  12.     for k, v in ipairs(tbl) do
  13.         if rstr:len() > 0 then
  14.             rstr = rstr .. delimiter
  15.         end
  16.         rstr = rstr .. tostring(v)
  17.     end
  18.     return rstr
  19. end
  20.  
  21. local function removeFileNameExt(name)
  22.     return name:gsub("^(.+)%..-$", "%1")
  23. end
  24.  
  25. local function asmAreaModifications(asmText)
  26.     local ret = asmText:gsub("%.area _INITIALIZED.-(%.area)", "%1"):gsub("_INITIALIZER", "_INITIALIZED")
  27.  
  28.     ret = ret:gsub("(%.area _INITIALIZED.-%.area)", function(s)
  29.         return s:gsub("__xinit_(.-):", "%1::")
  30.     end)
  31.  
  32.     return ret
  33. end
  34.  
  35.  
  36.  
  37.  
  38. --print("SDCC MAKE script\n================\n\n")
  39.  
  40.  
  41. print("Reading makefile...")
  42. local makefile = io.open("makefile", "rb")
  43. local makestring = makefile:read("*all")
  44. makefile:close()
  45.  
  46. local outfile, sdccoptions, linkoptions, cfiles, relfiles, asmfiles, binsize, nolink, createlib = nil, "", "", {n = 0}, {n = 0}, {n = 0}, 8192, false, false
  47. local exec_before, exec_after = {n = 0}, {n = 0}
  48. for line in makestring:gmatch("[^\r\n]+") do
  49.     local trimmed = line:gsub("^ *(.-) *$", "%1")
  50.     if (type(trimmed) == "string") and (trimmed:len() ~= 0) then
  51.         local cmd, arg = trimmed:gsub("^([^ ]+) .*$", "%1"), trimmed:gsub("^[^ ]+ (.*)$", "%1")
  52.         if cmd:upper() == "O" then
  53.             sdccoptions = arg
  54.         elseif cmd:upper() == "C" then
  55.             putTable(cfiles, arg)
  56.         elseif cmd:upper() == "R" then
  57.             putTable(relfiles, arg)
  58.         elseif cmd:upper() == "S" then
  59.             putTable(asmfiles, arg)
  60.         elseif cmd:upper() == "L" then
  61.             linkoptions = arg
  62.         elseif cmd:upper() == "F" then
  63.             outfile = arg
  64.         elseif cmd:upper() == "B" then
  65.             binsize = tonumber(arg)
  66.         elseif cmd:upper() == "NOLINK" then
  67.             if arg:upper() == "TRUE" then
  68.                 nolink = true
  69.             end
  70.         elseif cmd:upper() == "LIB" then
  71.             if arg:upper() == "TRUE" then
  72.                 createlib = true
  73.             end
  74.         elseif cmd:upper() == "EXEC_BEFORE" then
  75.             putTable(exec_before, arg)
  76.         elseif cmd:upper() == "EXEC_AFTER" then
  77.             putTable(exec_after, arg)
  78.         end
  79.     end
  80. end
  81.  
  82. print()
  83. print("C files:   " .. tblToString(cfiles, " "))
  84. print("ASM files: " .. tblToString(asmfiles, " "))
  85. print("REL files: " .. tblToString(relfiles, " "))
  86. --print("Compiler options: " .. sdccoptions)
  87. --print("Linker options:   " .. linkoptions)
  88.  
  89.  
  90. local anyError = false
  91.  
  92. io.open("__buildnumber", "a+"):close()
  93. local hbuild = io.open("__buildnumber", "rb")
  94. local bnumber = tonumber("0" .. hbuild:read("*all")) + 1
  95. hbuild:close()
  96. hbuild = io.open("__buildnumber", "wb")
  97. hbuild:write(tostring(bnumber))
  98. hbuild:close()
  99.  
  100. print("\nExecuting scripts...")
  101. for k, v in ipairs(exec_before) do
  102.     print(v)
  103.     os.execute(v)
  104. end
  105.  
  106. print("\nCompiling...")
  107. for k, v in ipairs(cfiles) do
  108.     local compErr = false
  109.     if os.execute("sdcc -DBUILD_NUMBER=" .. tostring(bnumber) .. " -S " .. sdccoptions .. " " .. v) ~= 0 then
  110.         anyError = true
  111.         compErr = true
  112.     end
  113.  
  114.     local asmFile = removeFileNameExt(v) .. ".asm"
  115.     if not compErr then
  116.         local asmFileH = io.open(asmFile, "rb")
  117.         local asmFileString = asmFileH:read("*all")
  118.         asmFileH:close()
  119.         asmFileH = io.open(asmFile, "wb")
  120.         local asmFileStringMod = asmAreaModifications(asmFileString)
  121.         asmFileH:write(asmFileStringMod)
  122.         asmFileH:close()
  123.  
  124.         putTable(asmfiles, asmFile)
  125.     end
  126. end
  127.  
  128. print("\nAssembling...")
  129. for k, v in ipairs(asmfiles) do
  130.     local relFile = removeFileNameExt(v) .. ".rel"
  131.     if os.execute("sdasz80 -g -o " .. relFile .. " " .. v) ~= 0 then
  132.         anyError = true
  133.     end
  134.     putTable(relfiles, relFile)
  135. end
  136.  
  137.  
  138.  
  139. if not outfile then
  140.     if tblSize(cfiles) >= 1 then
  141.         outfile = removeFileNameExt(cfiles[1]) .. ".ihx"
  142.     else
  143.         outfile = removeFileNameExt(relfiles[1]) .. ".ihx"
  144.     end
  145. end
  146.  
  147.  
  148. print()
  149. if (not anyError) and (not nolink) and (not createlib) then
  150.     print("Linking...")
  151.  
  152.     local fileList = tblToString(relfiles, " ")
  153.     print("REL file order: " .. fileList)
  154.     io.open(outfile, "wb"):close()
  155.     if os.execute("sdldz80 -nmwx -i " .. outfile .. " " .. linkoptions .. " " .. fileList .. " -e") ~= 0 then
  156.     --if os.execute("sdcc -o " .. outfile .. " " .. linkoptions .. " " .. fileList) ~= 0 then
  157.         anyError = true
  158.     end
  159.     --print("sdldz80 -m " .. mapfile .. " -i " .. outfile .. " " .. linkoptions .. " " .. fileList .. " -e")
  160. end
  161.  
  162. if (not anyError) and createlib then
  163.     print("Creating library...")
  164.  
  165.     outfile = removeFileNameExt(outfile) .. ".lib"
  166.     local fileList = tblToString(relfiles, " ")
  167.     --io.open(outfile, "wb"):close()
  168.     if os.execute("sdar -rc " .. outfile .. " " .. fileList) ~= 0 then
  169.         anyError = true
  170.     end
  171. end
  172.  
  173.  
  174. if (not anyError) and (not nolink) and (not createlib) then
  175.     local binfile = removeFileNameExt(outfile) .. ".bin"
  176.     os.execute("makebin -p -s " .. tostring(binsize) .. " " .. outfile .. " " .. binfile)
  177. end
  178.  
  179.  
  180. if anyError then
  181.     print("\nThere were errors during the build process.")
  182. else
  183.     print("\nExecuting scripts...")
  184.     for k, v in ipairs(exec_after) do
  185.         print(v)
  186.         os.execute(v)
  187.     end
  188.  
  189.     print("\nBuild successful.")
  190. end
  191.  
  192.  
  193. os.execute("pause")
Advertisement
Add Comment
Please, Sign In to add comment