premake/util/sdl_gen_config.lua
changeset 7925 f090a47eb7f7
child 8149 681eb46b8ac4
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/premake/util/sdl_gen_config.lua	Sun Nov 10 00:38:37 2013 -0500
     1.3 @@ -0,0 +1,68 @@
     1.4 +-- Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
     1.5 +--
     1.6 +-- This software is provided 'as-is', without any express or implied
     1.7 +-- warranty.  In no event will the authors be held liable for any damages
     1.8 +-- arising from the use of this software.
     1.9 +--
    1.10 +-- Permission is granted to anyone to use this software for any purpose,
    1.11 +-- including commercial applications, and to alter it and redistribute it
    1.12 +-- freely.
    1.13 +--
    1.14 +-- Meta-build system using premake created and maintained by
    1.15 +-- Benjamin Henning <b.henning@digipen.edu>
    1.16 +
    1.17 +--[[
    1.18 +sdl_gen_config.lua
    1.19 +
    1.20 +	Given a series of set configuration values from the project definitions,
    1.21 +	this file contains a series of functions that generate valid preprocessor
    1.22 +	definitions to enable or disable various features of the SDL2 library. These
    1.23 +	definitions are pasted into a template SDL config header file, which is then
    1.24 +	saved in the local directory and referenced to in generated project files.
    1.25 +
    1.26 +	This file depends on sdl_file.lua.
    1.27 +]]
    1.28 +
    1.29 +-- The line that must exist in the template file in order to properly paste
    1.30 +-- the generated definitions.
    1.31 +local searchKey = "/%* Paste generated code here %*/\n"
    1.32 +
    1.33 +local configFile, templateFileContents
    1.34 +local insertLocation
    1.35 +
    1.36 +-- This function begins config header generation given the name of the generated
    1.37 +-- file and the name of the template file to use.
    1.38 +function startGeneration(file, template)
    1.39 +	configFile = fileopen(file, "wt")
    1.40 +	templateFileContents = readfile(template, "rt")
    1.41 +	insertLocation = templateFileContents:find(searchKey)
    1.42 +	if insertLocation then
    1.43 +		configFile:write(templateFileContents:sub(1, insertLocation - 1))
    1.44 +	end
    1.45 +end
    1.46 +
    1.47 +-- Adds a table of configuration values to the generated file. Each
    1.48 +-- configuration line is wrapped around a preprocessor definition check, so they
    1.49 +-- can be manually overwritten by the developer if necessary. The definition
    1.50 +-- pastes string versions of both the key and the value on the line, where
    1.51 +-- either is allowed to be empty. That means the table stores key-value pairs.
    1.52 +function addConfig(tbl)
    1.53 +	-- if no insert location, don't paste anything
    1.54 +	if not insertLocation then return end
    1.55 +	for k,v in pairs(tbl) do
    1.56 +		configFile:print(0, "#ifndef " .. k)
    1.57 +		configFile:print(0, "#define " .. tostring(k) .. " " .. tostring(v))
    1.58 +		configFile:print(0, "#endif")
    1.59 +	end
    1.60 +end
    1.61 +
    1.62 +-- Finishes the generation and writes the remains of the template file into the
    1.63 +-- generated config file.
    1.64 +function endGeneration()
    1.65 +	if insertLocation then
    1.66 +		configFile:write(templateFileContents:sub(insertLocation + #searchKey - 2))
    1.67 +	else -- write entire file since nothing is being pasted
    1.68 +		configFile:write(templateFileContents)
    1.69 +	end
    1.70 +	configFile:close()
    1.71 +end
    1.72 \ No newline at end of file