premake/util/sdl_gen_config.lua
author Sam Lantinga
Thu, 01 Sep 2016 01:26:56 -0700
changeset 10304 ee83e0b4a36f
parent 9998 f67cf37e9cd4
child 10422 34fb2e531f7c
permissions -rwxr-xr-x
wayland: Add support for relative mouse mode, by Jonas Ã…dahl <jadahl@gmail.com>

Generate the C protocol files from the protocol XML files installed by
wayland-protocols, and use them to implement support for relative pointer
motions and pointer locking.

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