Permalink
Browse files

Release mode with premake's embed script

  • Loading branch information...
1 parent 84726e4 commit 6bc7c744506e70bebdea79e53ee6d314ffeb9a11 @crazyjul crazyjul committed May 3, 2012
Showing with 104 additions and 4 deletions.
  1. +2 −1 .gitignore
  2. +2 −2 premake4.lua
  3. +99 −0 scripts/embed.lua
  4. +1 −1 src/host/shader_shaker.cpp
View
3 .gitignore
@@ -17,4 +17,5 @@ bin/*
#Generated project
*.xcodeproj
-*.xcworkspace
+*.xcworkspace
+src/host/scripts.cpp
View
4 premake4.lua
@@ -109,11 +109,11 @@
-- a release build.
--
--- dofile("scripts/embed.lua")
+ dofile("scripts/embed.lua")
newaction {
trigger = "embed",
- description = "Embed scripts in scripts.c; required before release builds",
+ description = "Embed scripts in scripts.cpp; required before release builds",
execute = doembed
}
View
99 scripts/embed.lua
@@ -0,0 +1,99 @@
+-- Credits goes to premake4. This code has been copied from there.
+-- Embed the Lua scripts into src/host/scripts.cpp as static data buffers.
+-- I embed the actual scripts, rather than Lua bytecodes, because the
+-- bytecodes are not portable to different architectures, which causes
+-- issues in Mac OS X Universal builds.
+--
+
+ local function stripfile(fname)
+ local f = io.open(fname)
+ local s = assert(f:read("*a"))
+ f:close()
+
+ -- strip tabs
+ s = s:gsub("[\t]", "")
+
+ -- strip any CRs
+ s = s:gsub("[\r]", "")
+
+ -- strip out block comments
+ s = s:gsub("%-%-%[%[.-%-%-%]%]", "")
+
+ -- strip out inline comments
+ s = s:gsub("\n%-%-[^\n]*", "")
+
+ -- escape backslashes
+ s = s:gsub("\\", "\\\\")
+
+ -- strip duplicate line feeds
+ s = s:gsub("\n+", "\n")
+
+ -- strip out leading comments
+ s = s:gsub("^%-%-\n", "")
+
+ -- escape line feeds
+ s = s:gsub("\n", "\\n")
+
+ -- escape double quote marks
+ s = s:gsub("\"", "\\\"")
+
+ return s
+ end
+
+
+ local function writeline(out, s, continues)
+ out:write("\t\"")
+ out:write(s)
+ out:write(iif(continues, "\"\n", "\",\n"))
+ end
+
+
+ local function writefile(out, fname, contents)
+ local max = 1024
+
+ out:write("\t/* " .. fname .. " */\n")
+
+ -- break up large strings to fit in Visual Studio's string length limit
+ local start = 1
+ local len = contents:len()
+ while start <= len do
+ local n = len - start
+ if n > max then n = max end
+ local finish = start + n
+
+ -- make sure I don't cut an escape sequence
+ while contents:sub(finish, finish) == "\\" do
+ finish = finish - 1
+ end
+
+ writeline(out, contents:sub(start, finish), finish < len)
+ start = finish + 1
+ end
+
+ out:write("\n")
+ end
+
+
+ function doembed()
+ -- load the manifest of script files
+ scripts = dofile("src/_manifest.lua")
+
+ -- main script always goes at the end
+ table.insert(scripts, "_shader_shaker_main.lua")
+
+ -- open scripts.c and write the file header
+ local out = io.open("src/host/scripts.cpp", "w+b")
+ out:write("/* ShaderShaker's Lua scripts, as static data buffers for release mode builds */\n")
+ out:write("/* DO NOT EDIT - this file is autogenerated */\n")
+ out:write("/* To regenerate this file, run: premake4 embed */ \n\n")
+ out:write("const char* builtin_scripts[] = {\n")
+
+ for i,fn in ipairs(scripts) do
+ print(fn)
+ local s = stripfile("src/" .. fn)
+ writefile(out, fn, s)
+ end
+
+ out:write("\t0\n};\n");
+ out:close()
+ end
View
2 src/host/shader_shaker.cpp
@@ -188,7 +188,7 @@ bool load_builtin_scripts(lua_State* L)
}
/* hand off control to the scripts */
- lua_getglobal(L, "_premake_main");
+ lua_getglobal(L, "_shader_shaker_main");
if (lua_pcall(L, 0, 1, 0) != 0)
{

0 comments on commit 6bc7c74

Please sign in to comment.