diff --git a/Saphirefile.lua b/Saphirefile.lua index 8391545..34c847c 100644 --- a/Saphirefile.lua +++ b/Saphirefile.lua @@ -16,6 +16,8 @@ ]] local saphire = require "saphire" +local c = require "saphire-c" +local Future = require "saphire-future" local los = require "los" local cc = os.getenv "CC" or "cc" @@ -23,14 +25,12 @@ local ar = os.getenv "AR" or "ar" local windres = os.getenv "WINDRES" or "windres" -- TODO: Use current lua interpreter -local lua = "luajit"--os.getenv "LUA" or "luajit\\src\\luajit" -local lua_ready = os.getenv "LUA" or false +local lua = os.getenv "LUA" or "luajit\\src\\luajit" +local needs_luajit_built = not (os.getenv "LUA") local cflags = os.getenv "CFLAGS" or "-O2 -s" local ldflags = os.getenv "LDFLAGS" or "-O2 -s -lm" -local windres = os.getenv "WINDRES" or "windres" - local modules = "raymath rlgl easings gestures physac raygui" cflags = cflags .. " -Iluajit/src -Iraygui/src -Iraylib/src" @@ -42,162 +42,102 @@ if los.type() == "linux" then ldflags = ldflags .. " -ldl -pthread" elseif los.type() == "win32" then ldflags = ldflags .. " -lopengl32 -lgdi32 -lwinmm -static " - exe_ldflags = exe_ldflags .. "src/res/icon.res" end -local function build_c(src, obj, name, flags) - if saphire.targets.clean then - return { - command = "rm -f " .. obj, - name = name - } - end - - return { - command = string.format("%s -c -o %s %s %s %s", cc, obj, src, flags or "", cflags), - name = name, - display = obj +local libluajit +if saphire.targets.clean then + libluajit = { + command = "make -C luajit clean", + name = "LuaJIT" + } +else + libluajit = { + command = string.format("make -C luajit amalg CC=%s BUILDMODE=static MACOSX_DEPLOYMENT_TARGET=10.13", cc), + name = "LuaJIT" } end +saphire.do_single(libluajit) +libluajit[1] = "luajit/src/libluajit.a" -local function build_a(objs, lib, name) +local libraylib = Future "raylib/src/libraylib.a" +saphire.do_subdir("raylib/src", false, "build/buildRaylib.lua", libraylib) + +local function lua2c(files, output, name) if saphire.targets.clean then - return { - command = "rm -f " .. lib, - name = name - } + return string.format("rm -f %s", output) + else + return string.format("%s tools/lua2str.lua %s %s %s", lua, output, name, table.concat(files, " ")) end - - return { - command = string.format("%s rcs %s %s", ar, lib, table.concat(objs, " ")), - name = name - } end -local function build_e(files, output, name, flags) +local function genbind(output, modules) if saphire.targets.clean then - return { - command = "rm -f " .. output, - name = name - } + return string.format("rm -f %s", output) + else + return string.format("%s tools/genbind.lua src/autogen/bind.c %s", lua, modules) end - - return { - command = string.format("%s -o %s %s %s %s %s", - cc, output, table.concat(files, " "), flags or "", ldflags, exe_ldflags), - name = name, - display = string.format("%s -o %s", cc, output) - } end -local function build_res(src, output, name) - if saphire.targets.clean then - return { - command = "rm -f " .. output, - name = name - } - end - - return { - command = string.format("%s %s -O coff %s", windres, src, output), - name = name - } -end - -local function lua2c(files, output, name, dname) - --while not lua_ready do - -- coroutine.yield() - --end - if saphire.targets.clean then - return { - command = "rm -f " .. output, - name = dname - } - end - - return { - command = string.format("%s tools/lua2str.lua %s %s %s", lua, output, name, table.concat(files, " ")), - name = dname - } -end - -saphire.do_multi({ - function () -- LuaJIT - -- TODO: Improve LuaJIT building - --saphire.do_subdir("luajit", true, "build/buildLuaJIT.lua") - - if saphire.targets.clean then - saphire.do_single({ - command = "make -C luajit clean", - name = "LuaJIT" - }, true) - else - saphire.do_single({ - command = string.format("make -C luajit amalg CC=%s BUILDMODE=static MACOSX_DEPLOYMENT_TARGET=10.13", cc), - name = "LuaJIT" - }, true) +local raylua_src = { + c.src("src/raylua.c", function () + -- Generate bind.c and boot.c + if needs_luajit_built then + -- LuaJIT needs to be built + libluajit:wait() end - lua_ready = true - end, - function () -- Raylib - saphire.do_subdir("raylib/src", true, "build/buildRaylib.lua") - end, - function () -- libraylua.a - saphire.do_single(lua2c( - { "src/raylib.lua", "src/compat.lua", "src/raylua.lua" }, - "src/autogen/boot.c", - "raylua_boot_lua", - "libraylua.a" - ), true) + saphire.do_multi({ + { + command = lua2c( + { "src/raylib.lua", "src/compat.lua", "src/raylua.lua" }, + "src/autogen/boot.c", + "raylua_boot_lua" + ), + name = "boot.c" + }, + { + command = genbind("src/autogen/bind.c", modules), + name = "bind.c" + } + }, true) + end) +} +local raylua_obj = c.compile(raylua_src, cflags, "raylua", cc) - if saphire.targets.clean then - saphire.do_single({ - command = "rm -f src/autogen/bind.c", - name = "libraylua.a" - }, true) - else - saphire.do_single({ - command = string.format("%s tools/genbind.lua src/autogen/bind.c %s", lua, modules), - name = "libraylua.a" - }, true) - end +local libraylua = c.lib("libraylua.a", raylua_obj, "raylua", ar) - saphire.do_single(build_c("src/raylua.c", "src/raylua.o", "libraylua.a"), true) - saphire.do_single(build_a({ "src/raylua.o" }, "libraylua.a", "libraylua.a"), true) - end, - function () -- src/raylua_builder.o - saphire.do_single(lua2c({ "src/raylua_builder.lua" }, "src/autogen/builder.c", "raylua_builder_lua", "raylua_e"), true) - saphire.do_single(build_c("src/raylua_builder.c", "src/raylua_builder.o", "raylua_e"), true) - end, - function () -- Ressources - if los.type() == "win32" then - saphire.do_single(build_res("src/res/icon.rc", "src/res/icon.res", "icon"), true) - end - end, - function () - local entries = { - { "src/raylua_s.o", "src/raylua_s.c" }, - { "src/raylua_e.o", "src/raylua_e.c" }, - { "src/lib/miniz.o", "src/lib/miniz.c" }, - { "src/raylua_self.o", "src/raylua_self.c" } - } +local raylua_s_src = { + "src/raylua_s.c" +} +local raylua_s_objs = c.compile(raylua_s_src, cflags, "raylua_s", cc) - saphire.do_multi(saphire.map(entries, function(entry) - return build_c(entry[2], entry[1], "raylua") - end), true) - end -}, true) +local raylua_e_src = { + c.src("src/raylua_builder.c", function () + saphire.do_single(lua2c({ "src/raylua_builder.lua" }, "src/autogen/builder.c", "raylua_builder_lua"), true) + end), + "src/raylua_e.c", + "src/lib/miniz.c", + "src/raylua_self.c", +} +local raylua_e_objs = c.compile(raylua_e_src, cflags, "raylua_e", cc) -saphire.do_multi({ - -- raylua_s - build_e({ "src/raylua_s.o", "libraylua.a" }, "raylua_s", "raylua_s"), - -- raylua_e - build_e({ - "src/raylua_e.o", - "src/raylua_self.o", - "src/raylua_builder.o", - "src/lib/miniz.o", - "libraylua.a" - }, "raylua_e", "raylua_e") -}, true) +local icon +if los.type() == "win32" then + icon = c.res("src/res/icon.rc", { "src/res/icon.ico" }, "icon", windres) +end + +local raylua_s = c.link("raylua_s", + saphire.merge(raylua_s_objs, { libraylua, libraylib, libluajit, icon }), + ldflags, + false, + "raylua_s", + cc +) + +local raylua_e = c.link("raylua_e", + saphire.merge(raylua_e_objs, { libraylua, libraylib, libluajit, icon }), + ldflags, + false, + "raylua_e", + cc +) \ No newline at end of file diff --git a/build/buildRaylib.lua b/build/buildRaylib.lua index a8e3987..29309e8 100644 --- a/build/buildRaylib.lua +++ b/build/buildRaylib.lua @@ -1,3 +1,5 @@ +local future = arg[1] + local saphire = require "saphire" local c = require "saphire-c" @@ -5,7 +7,6 @@ local cc = os.getenv "CC" or "cc" local ar = os.getenv "AR" or "ar" local cflags = os.getenv "CFLAGS" or "-O2 -s" -local ldflags = os.getenv "LDFLAGS" or "-O2 -s -lm" local include_paths = "-I. -Iexternal/glfw/include -Iexternal/glfw/deps/mingw" @@ -33,9 +34,11 @@ for i,v in ipairs(vars) do vars[v[1]] = v[2] end +local flags = string.format("%s -D%s -D%s %s", cflags, vars.PLATFORM, vars.GRAPHICS, include_paths) + local src = { -- ["obj.o"] = { src... } - { "rcore.o", { "rcore.c", "raylib.h", "rlgl.h", "utils.h", "raymath.h", "camera.h", "rgestures.h" } }, + { "rcore.o", { "rcore.c", "raylib.h", "rlgl.h", "utils.h", "raymath.h", "rcamera.h", "rgestures.h" } }, { "rglfw.o", { "rglfw.c", flags = os.getenv "GLFW_OSX" or "" } }, { "rshapes.o", { "rshapes.c", "raylib.h", "rlgl.h" } }, { "rtextures.o", { "rtextures.c", "raylib.h", "rlgl.h", "utils.h" } }, @@ -45,5 +48,10 @@ local src = { { "raudio.o", { "raudio.c", "raylib.h" } } } -local objs = c.compile(src, table.concat({ "-D" .. vars.PLATFORM, "-D" .. vars.GRAPHICS, include_paths }, " ")) -local libraylib = c.lib("libraylib.a", objs) \ No newline at end of file +local objs = c.compile(src, flags, "raylib", cc) +local libraylib = c.lib("libraylib.a", objs, "raylib") + +libraylib:wait() +if future then + future:resolve() +end \ No newline at end of file