Skip to content
This repository
Browse code

Release mode with premake's embed script

  • Loading branch information...
commit 6bc7c744506e70bebdea79e53ee6d314ffeb9a11 1 parent 84726e4
Julien Hamaide authored May 04, 2012
3  .gitignore
@@ -17,4 +17,5 @@ bin/*
17 17
 
18 18
 #Generated project
19 19
 *.xcodeproj
20  
-*.xcworkspace
  20
+*.xcworkspace
  21
+src/host/scripts.cpp
4  premake4.lua
@@ -109,11 +109,11 @@
109 109
 -- a release build.
110 110
 --
111 111
 
112  
---	dofile("scripts/embed.lua")
  112
+	dofile("scripts/embed.lua")
113 113
 	
114 114
 	newaction {
115 115
 		trigger     = "embed",
116  
-		description = "Embed scripts in scripts.c; required before release builds",
  116
+		description = "Embed scripts in scripts.cpp; required before release builds",
117 117
 		execute     = doembed
118 118
 	}
119 119
 
99  scripts/embed.lua
... ...
@@ -0,0 +1,99 @@
  1
+-- Credits goes to premake4. This code has been copied from there.
  2
+-- Embed the Lua scripts into src/host/scripts.cpp as static data buffers.
  3
+-- I embed the actual scripts, rather than Lua bytecodes, because the 
  4
+-- bytecodes are not portable to different architectures, which causes 
  5
+-- issues in Mac OS X Universal builds.
  6
+--
  7
+
  8
+	local function stripfile(fname)
  9
+		local f = io.open(fname)
  10
+		local s = assert(f:read("*a"))
  11
+		f:close()
  12
+
  13
+		-- strip tabs
  14
+		s = s:gsub("[\t]", "")
  15
+		
  16
+		-- strip any CRs
  17
+		s = s:gsub("[\r]", "")
  18
+				
  19
+		-- strip out block comments
  20
+		s = s:gsub("%-%-%[%[.-%-%-%]%]", "")
  21
+
  22
+		-- strip out inline comments
  23
+		s = s:gsub("\n%-%-[^\n]*", "")
  24
+		
  25
+		-- escape backslashes
  26
+		s = s:gsub("\\", "\\\\")
  27
+
  28
+		-- strip duplicate line feeds
  29
+		s = s:gsub("\n+", "\n")
  30
+
  31
+		-- strip out leading comments
  32
+		s = s:gsub("^%-%-\n", "")
  33
+
  34
+		-- escape line feeds
  35
+		s = s:gsub("\n", "\\n")
  36
+		
  37
+		-- escape double quote marks
  38
+		s = s:gsub("\"", "\\\"")
  39
+		
  40
+		return s
  41
+	end
  42
+
  43
+
  44
+	local function writeline(out, s, continues)
  45
+		out:write("\t\"")
  46
+		out:write(s)
  47
+		out:write(iif(continues, "\"\n", "\",\n"))
  48
+	end
  49
+	
  50
+	
  51
+	local function writefile(out, fname, contents)
  52
+		local max = 1024
  53
+
  54
+		out:write("\t/* " .. fname .. " */\n")
  55
+		
  56
+		-- break up large strings to fit in Visual Studio's string length limit		
  57
+		local start = 1
  58
+		local len = contents:len()
  59
+		while start <= len do
  60
+			local n = len - start
  61
+			if n > max then n = max end
  62
+			local finish = start + n
  63
+
  64
+			-- make sure I don't cut an escape sequence
  65
+			while contents:sub(finish, finish) == "\\" do
  66
+				finish = finish - 1
  67
+			end			
  68
+
  69
+			writeline(out, contents:sub(start, finish), finish < len)
  70
+			start = finish + 1
  71
+		end		
  72
+
  73
+		out:write("\n")
  74
+	end
  75
+
  76
+
  77
+	function doembed()
  78
+		-- load the manifest of script files
  79
+		scripts = dofile("src/_manifest.lua")
  80
+		
  81
+		-- main script always goes at the end
  82
+		table.insert(scripts, "_shader_shaker_main.lua")
  83
+		
  84
+		-- open scripts.c and write the file header
  85
+		local out = io.open("src/host/scripts.cpp", "w+b")
  86
+		out:write("/* ShaderShaker's Lua scripts, as static data buffers for release mode builds */\n")
  87
+		out:write("/* DO NOT EDIT - this file is autogenerated */\n")
  88
+		out:write("/* To regenerate this file, run: premake4 embed */ \n\n")
  89
+		out:write("const char* builtin_scripts[] = {\n")
  90
+		
  91
+		for i,fn in ipairs(scripts) do
  92
+			print(fn)
  93
+			local s = stripfile("src/" .. fn)
  94
+			writefile(out, fn, s)
  95
+		end
  96
+		
  97
+		out:write("\t0\n};\n");		
  98
+		out:close()
  99
+	end
2  src/host/shader_shaker.cpp
@@ -188,7 +188,7 @@ bool load_builtin_scripts(lua_State* L)
188 188
 	}
189 189
 
190 190
 	/* hand off control to the scripts */
191  
-	lua_getglobal(L, "_premake_main");
  191
+	lua_getglobal(L, "_shader_shaker_main");
192 192
     
193 193
 	if (lua_pcall(L, 0, 1, 0) != 0)
194 194
 	{

0 notes on commit 6bc7c74

Please sign in to comment.
Something went wrong with that request. Please try again.