Update build.lua (#10)

Peak software

Reviewed-on: NeoFlock/noom#10
Co-authored-by: tema5002 <tema5002@tuta.io>
Co-committed-by: tema5002 <tema5002@tuta.io>
This commit is contained in:
2026-06-23 20:49:37 +02:00
committed by IonutParau
parent 995f53958e
commit 404332fcba
2 changed files with 158 additions and 87 deletions

243
build.lua
View File

@@ -1,66 +1,4 @@
#!/usr/bin/env lua #!/usr/bin/env lua
-- i love build scripts i love build scripts i love build scripts scripts build love i script build love me i love script build i script love build
local isBlendi = os.getenv("USER") == "blendi"
local separator = package.config:sub(1,1)
local function filename(path)
local s,e = 1, #path
for i = 1,#path do
if path:sub(i,i) == '/' then s = i+1 end;
if path:sub(i,i) == '.' then e = i-1 end;
end
return path:sub(s,e)
end
local function runCommand(cmd, ignore_fail)
print("> " .. cmd)
local result = os.execute(cmd)
if (result ~= true and result ~= 0) and (not ignore_fail) then
os.exit(1)
end
end
local function fixPath(path)
local new = ""
for i = 1,#path do
local ch = path:sub(i,i)
if ch == '/' then new = new .. separator
else new = new .. ch end
end
return new
end
if arg[1] == "clean" then
if separator == '\\' then
runCommand("rmdir /s /q build 2>nul", true)
else
runCommand("rm -rf build")
end
return
end
local needsDir = false
if separator == '\\' then
needsDir = true
else
-- TODO: maybe use exit code instead of the first return value?
local ok, _, code = os.execute('test -d build');
local exists = (ok == 0 or (ok == true and code == 0))
needsDir = not exists;
end
if needsDir then
if separator == '\\' then
runCommand('mkdir build 2>nul')
else
runCommand('mkdir -p build')
end
end
local files = { local files = {
'src/error.c', 'src/error.c',
'src/helper.c', 'src/helper.c',
@@ -72,11 +10,56 @@ local files = {
'src/main.c', 'src/main.c',
} }
local objects = {} -- i love build scripts i love build scripts i love build scripts scripts build love i script build love me i love script build i script love build
local isBlendi = os.getenv("USER") == "blendi"
local separator = package.config:sub(1, 1)
-- win mean and lean!!!!
local windows = separator == '\\'
local coolArgs = {} local help = [[
usage: lua build.lua [commands...] [options]
commands:
(none)|build compile and link noom
run compile, link and run noom
files print source files separated by spaces (for shell use)
e.g. clang -O3 $(lua build.lua files) -o noom
clean clean the build cache
help show this!!! hello :DDDDD!!!!!
options:
-O<level> optimisation level passed to clang (e.g. -O2, -O3)
defaults to -O0
-j parallel compilation (UNIX only!!!!)
]]
local function filename(path)
local s, e = 1, #path
for i = 1, #path do
if path:sub(i, i) == '/' then s = i + 1 end;
if path:sub(i, i) == '.' then e = i - 1 end;
end
return path:sub(s, e)
end
local function fixPath(path)
if not windows then return path end
return path:gsub('/', '\\')
end
local function runCommand(cmd, ignore_fail)
print("> " .. cmd)
local result = os.execute(cmd)
if (result ~= true and result ~= 0) and (not ignore_fail) then
os.exit(1)
end
end
-- might fail on MacOS and other *NIXes, haven't tried
-- prob can be get functional there with just stat -c shit || ...
local function getTime(path) local function getTime(path)
if windows then return 0 end
local handle = assert(io.popen('stat -c %Y "' .. path .. '" 2>/dev/null')) local handle = assert(io.popen('stat -c %Y "' .. path .. '" 2>/dev/null'))
local result = handle:read("*a") local result = handle:read("*a")
handle:close() handle:close()
@@ -84,37 +67,123 @@ local function getTime(path)
end end
local function needsRebuild(src, obj) local function needsRebuild(src, obj)
if separator == '\\' then return true end if windows then return true end -- TODO?
local srcTime = getTime(src) return getTime(src) > getTime(obj)
local objTime = getTime(obj)
return srcTime > objTime
end end
if not isBlendi then table.insert(coolArgs, '-fsanitize=undefined,address') end local function dirExists(path)
if windows then return false end -- always recreate on windows duh, TODO
local ok, _, code = os.execute('test -d "' .. path .. '"')
return (ok == true) or (code == 0)
end
local needsLinking = false local function ensureDir(path)
path = fixPath(path)
if dirExists(path) then return end
if windows then
runCommand('mkdir "' .. path .. '" 2>nul', true)
else
runCommand('mkdir -p "' .. path .. '"')
end
end
for i = 1,#files do local function rmdir(path)
local fname = files[i] path = fixPath(path)
local out = "build/" .. filename(fname) .. '.o' if windows then
runCommand('rmdir /s /q "' .. path .. '" 2>nul', true)
else
runCommand('rm -rf "' .. path .. '"')
end
end
fname = fixPath(fname) local commands = {}
out = fixPath(out) local optLevel = nil
local parallel = false
if needsRebuild(fname, out) then local shi = { build=true, clean=true, run=true, files=true, help=true }
needsLinking = true
runCommand('clang -g -c -o ' .. out .. ' ' .. fname .. ' ' .. table.concat(coolArgs, ' ')) for i = 1, #arg do
local a = arg[i]
if shi[a] then
if commands[a] then
io.stderr:write("warning: duplicate command '" .. a .. "'\n")
end
commands[a] = true
elseif a:match("^%-O") then
optLevel = a
elseif a == "-j" then
parallel = true
else
io.stderr:write("mmm no i don't know what is " .. a .. "\n")
os.exit(1)
end
end
if parallel and windows then
parallel = false -- fuck you
end
optLevel = optLevel or "-O0"
if not next(commands) then commands.build = true end
if commands.run then commands.build = true end
if commands.help then
print(help)
return
end
if commands.files then
print(table.concat(files, " "))
return
end
if commands.clean then
rmdir("build")
end
if commands.build then
ensureDir("build")
local coolArgs = { optLevel }
-- Why does blendi even need that bro 😭🙏 Do not run noom on your RISC-V shi
if not isBlendi then table.insert(coolArgs, '-fsanitize=undefined,address') end
local coolerArgs = table.concat(coolArgs, ' ')
local objects = {}
local needsLinking = false
local toCompile = {}
for _, fname in ipairs(files) do
fname = fixPath(fname)
local out = fixPath("build/" .. filename(fname) .. ".o")
table.insert(objects, out)
if needsRebuild(fname, out) then
needsLinking = true
local cmd = 'clang -g -c -o ' .. out .. ' ' .. fname .. ' ' .. coolerArgs
if parallel then
table.insert(toCompile, cmd)
else
runCommand(cmd)
end
end
end end
objects[#objects+1] = out; if next(toCompile) then
end local shellScript = table.concat(toCompile, " &\n") .. " &\nwait"
runCommand(shellScript)
end
local exe = separator == '\\' and ".\\noom.exe" or "./noom" local exe = windows and ".\\noom.exe" or "./noom"
if needsLinking then if needsLinking then
runCommand('clang -g -o ' .. exe .. ' ' .. table.concat(objects, ' ') .. ' ' .. table.concat(coolArgs, ' ')) runCommand('clang -g -o ' .. exe .. ' ' .. table.concat(objects, ' ') .. ' ' .. coolerArgs)
end end
if arg[1] == "run" then if commands.run then
runCommand(exe) runCommand(exe)
end
end end

View File

@@ -5,6 +5,8 @@
#include "compiler.h" #include "compiler.h"
#include "vm.h" #include "vm.h"
volatile const char wawa[] = "if you are reading this with either the `strings` utility or through a hex viewer please know that you can instead just check out the source code on gitea: https://gitea.codersquack.nl/NeoFlock/noom";
void tab(noom_uint_t amount) { void tab(noom_uint_t amount) {
amount *= 2; amount *= 2;
for (noom_uint_t i = 0; i < amount; i++) { for (noom_uint_t i = 0; i < amount; i++) {