premake/util/sdl_gen_config.lua
author Ryan C. Gordon <icculus@icculus.org>
Sun, 10 Nov 2013 00:38:37 -0500
changeset 7925 f090a47eb7f7
child 8149 681eb46b8ac4
permissions -rwxr-xr-x
Added Ben Henning's GSoC2013 work: premake build system.
icculus@7925
     1
-- Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
icculus@7925
     2
--
icculus@7925
     3
-- This software is provided 'as-is', without any express or implied
icculus@7925
     4
-- warranty.  In no event will the authors be held liable for any damages
icculus@7925
     5
-- arising from the use of this software.
icculus@7925
     6
--
icculus@7925
     7
-- Permission is granted to anyone to use this software for any purpose,
icculus@7925
     8
-- including commercial applications, and to alter it and redistribute it
icculus@7925
     9
-- freely.
icculus@7925
    10
--
icculus@7925
    11
-- Meta-build system using premake created and maintained by
icculus@7925
    12
-- Benjamin Henning <b.henning@digipen.edu>
icculus@7925
    13
icculus@7925
    14
--[[
icculus@7925
    15
sdl_gen_config.lua
icculus@7925
    16
icculus@7925
    17
	Given a series of set configuration values from the project definitions,
icculus@7925
    18
	this file contains a series of functions that generate valid preprocessor
icculus@7925
    19
	definitions to enable or disable various features of the SDL2 library. These
icculus@7925
    20
	definitions are pasted into a template SDL config header file, which is then
icculus@7925
    21
	saved in the local directory and referenced to in generated project files.
icculus@7925
    22
icculus@7925
    23
	This file depends on sdl_file.lua.
icculus@7925
    24
]]
icculus@7925
    25
icculus@7925
    26
-- The line that must exist in the template file in order to properly paste
icculus@7925
    27
-- the generated definitions.
icculus@7925
    28
local searchKey = "/%* Paste generated code here %*/\n"
icculus@7925
    29
icculus@7925
    30
local configFile, templateFileContents
icculus@7925
    31
local insertLocation
icculus@7925
    32
icculus@7925
    33
-- This function begins config header generation given the name of the generated
icculus@7925
    34
-- file and the name of the template file to use.
icculus@7925
    35
function startGeneration(file, template)
icculus@7925
    36
	configFile = fileopen(file, "wt")
icculus@7925
    37
	templateFileContents = readfile(template, "rt")
icculus@7925
    38
	insertLocation = templateFileContents:find(searchKey)
icculus@7925
    39
	if insertLocation then
icculus@7925
    40
		configFile:write(templateFileContents:sub(1, insertLocation - 1))
icculus@7925
    41
	end
icculus@7925
    42
end
icculus@7925
    43
icculus@7925
    44
-- Adds a table of configuration values to the generated file. Each
icculus@7925
    45
-- configuration line is wrapped around a preprocessor definition check, so they
icculus@7925
    46
-- can be manually overwritten by the developer if necessary. The definition
icculus@7925
    47
-- pastes string versions of both the key and the value on the line, where
icculus@7925
    48
-- either is allowed to be empty. That means the table stores key-value pairs.
icculus@7925
    49
function addConfig(tbl)
icculus@7925
    50
	-- if no insert location, don't paste anything
icculus@7925
    51
	if not insertLocation then return end
icculus@7925
    52
	for k,v in pairs(tbl) do
icculus@7925
    53
		configFile:print(0, "#ifndef " .. k)
icculus@7925
    54
		configFile:print(0, "#define " .. tostring(k) .. " " .. tostring(v))
icculus@7925
    55
		configFile:print(0, "#endif")
icculus@7925
    56
	end
icculus@7925
    57
end
icculus@7925
    58
icculus@7925
    59
-- Finishes the generation and writes the remains of the template file into the
icculus@7925
    60
-- generated config file.
icculus@7925
    61
function endGeneration()
icculus@7925
    62
	if insertLocation then
icculus@7925
    63
		configFile:write(templateFileContents:sub(insertLocation + #searchKey - 2))
icculus@7925
    64
	else -- write entire file since nothing is being pasted
icculus@7925
    65
		configFile:write(templateFileContents)
icculus@7925
    66
	end
icculus@7925
    67
	configFile:close()
icculus@7925
    68
end