Added Ben Henning's GSoC2013 work: premake build system.

This commit is contained in:
Ryan C. Gordon 2013-11-10 00:38:37 -05:00
parent 694010e6e4
commit 5595fbc315
473 changed files with 89151 additions and 1 deletions

View file

@ -0,0 +1,307 @@
-- Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
--
-- This software is provided 'as-is', without any express or implied
-- warranty. In no event will the authors be held liable for any damages
-- arising from the use of this software.
--
-- Permission is granted to anyone to use this software for any purpose,
-- including commercial applications, and to alter it and redistribute it
-- freely.
--
-- Meta-build system using premake created and maintained by
-- Benjamin Henning <b.henning@digipen.edu>
--[[
sdl_check_compile.lua
This file provides various utility functions which allow the meta-build
system to perform more complex dependency checking than premake initially
allows. This is done using the (currently) GCC toolchain to build generated
C files which try to import certain headers, link to certain functions, link
to certain libraries, or a combination of the above. It supports providing a
custom source to try and build, link, and/or run per the implementation's
choice, so the possibilities are nearly endless with that this system is
capable of, though it could always do with more flexibility.
]]
local cxx = "gcc"
local cxx_flags = ""
local cxx_io_flags = "-o premakecheck.o -c premakecheck.c 2> /dev/null"
local cxx_includes = { }
local link = "gcc"
local link_flags = ""
local link_io_flags = "-o premakecheck.out premakecheck.o"
local link_end = " 2> /dev/null"
local run = "./premakecheck.out"
local run_flags = ""
local run_io_flags = " > ./premakecheck.stdout"
local checked_printf = false
local has_printf = false
-- Set the application used to compile the generated files.
function set_cxx(compiler)
cxx = compiler
end
-- Set custom flags for the compiler.
function set_cxx_flags(flags)
cxx_flags = flags
end
-- Include a search directory for libraries.
local function include_library_dir(dir)
link_flags = link_flags .. "-L" .. dir .. " "
end
-- Include a library to be linked durnig the link step.
local function link_library(lib)
link_flags = link_flags .. "-l" .. lib .. " "
end
-- Reset the link flags.
local function reset_link_flags()
link_flags = ""
end
-- Creates the build command line to be executed.
local function build_compile_line()
return cxx .. " " .. cxx_flags .. " " .. cxx_io_flags
end
-- Creates the link command line to be executed.
local function build_link_line()
return link .. " " .. link_io_flags .. " " .. link_flags .. link_end
end
-- Create the run line to be executed.
local function build_run_line()
return run .. " " .. run_flags .. " " .. run_io_flags
end
-- Builds a list of preprocessor include directives for all the include files
-- successfully found so far by these functions, so as to perform automatic
-- feature checking for the clientside code.
local function build_includes()
local includes = ""
for _,v in ipairs(cxx_includes) do
includes = includes .. '#include "' .. v .. '"\n'
end
return includes
end
-- Cleanup the generated build environment.
local function cleanup_build()
os.remove("./premakecheck.c")
os.remove("./premakecheck.o")
os.remove("./premakecheck.out")
os.remove("./premakecheck.stdout")
end
-- Check if a source builds, links, and or/runs, where running depends on
-- linking and linking depends on building. The return from this function is
-- a triple, where the first is a boolean value indicating if it successfully
-- was built, the second is a boolean value indicating if it successfully
-- linked, and the third represents nil if it was not run or run correctly, or
-- the output from the program executed (may be empty for no output).
local function check_build_source(source, link, run)
local file = fileopen("./premakecheck.c", "wt")
file:write(source)
file:close()
local result = os.execute(build_compile_line())
if not link then
cleanup_build()
if result == 0 then
return true, false, nil -- compile, no link, no run
end
return false, false, nil -- no compile, no link, no run
end
-- try linking, too
if result ~= 0 then
-- can't link if it doesn't compile
cleanup_build()
return false, false, nil -- no compile, no link, no run
end
result = os.execute(build_link_line())
if not run or result ~= 0 then -- have to link to run
cleanup_build()
return true, result == 0, nil -- compile, maybe link, no run
end
result = os.execute(build_run_line())
local output = readfile("./premakecheck.stdout", "rt")
cleanup_build()
return true, true, output -- compile, link, ran
end
-- Given C source code, determine whether the source code will compile in the
-- present environment. Returns true if the source was successfully compiled, or
-- false if otherwise.
function check_cxx_source_compiles(source)
local r1, _, __ = check_build_source(source, false, false)
return r1
end
-- Given C source code, determine whether the source code can be built into a
-- working executable. That is, it will check if the code both compiles and
-- links. Returns true if the code was successfully built (compiled and linked),
-- or false if otherwise.
function check_cxx_source_builds(source)
local r1, r2, _ = check_build_source(source, true, false)
return r1 and r2
end
-- Given C source code, attempt to compile, link, and execute the source code.
-- This function will return two values. The first is a boolean indicating
-- whether the source code was successfully run (meaning it was compiled, built,
-- and ran successfully), and the second value returned is the actual output
-- from running the application, or nil if it did not run correctly or was not
-- built. The output may be an empty string if the code does not print anything
-- to stdout.
function check_cxx_source_runs(source)
local r1, r2, r3 = check_build_source(source, true, true)
return r1 and r2 and (r3 ~= nil), r3
end
-- Given a header file, check whether the header file is visible to the compiler
-- in the given environment. Returns a boolean indicating thus. If a header file
-- is found in either of these functions, it will be added to a list of headers
-- that can be used in subsequent dependency checks.
function check_include_file(inc)
return check_include_files(inc)
end
-- Given a variable list of header files, check whether all of the includes are
-- visible in the given environment. Every file must be included in order for
-- this function to return true.
function check_include_files(...)
local source = ""
for _, v in ipairs{...} do
source = source .. '#include "' .. v .. '"\n'
end
local result = check_cxx_source_compiles(source)
if result then
for _, v in ipairs{...} do
table.insert(cxx_includes, v)
end
end
return result
end
-- Given a directory, determine whether the directory contains any header files.
-- Unfortunately it does assume the extension is .h, but this can be altered in
-- future versions of this software. The function returns true if the directory
-- (or any of its subdirectories) contain .h files, or false if otherwise (such
-- as if the directory does not exist).
function check_include_directory(incDir)
incDir = incDir:gsub("\\", "/"):gsub("//", "/")
if incDir:sub(#incDir, #incDir) ~= "/" then
incDir = incDir .. "/"
end
return #os.matchfiles(incDir .. "**.h") > 0
end
-- Given a variable list of directories, iteratively check if each one contains
-- header files, per the functionality of check_include_directory. This function
-- returns true if and only if every listed directory or its subdirectories
-- contain .h files.
function check_include_directories(...)
for _, v in ipairs{...} do
if not check_include_directory(v) then
return false
end
end
return true
end
-- Given a function name, attempt to determine whether the function can be found
-- within all of the known include files. Known include files are derived from
-- the check_include_file(s) functions.
function check_function_exists(func)
local source = build_includes()
source = source .. 'int main(int argc, char **argv) {\n'
source = source .. '\tvoid *check = (void *) ' .. func .. ';\n'
source = source .. '\treturn 0;\n'
return check_cxx_source_builds(source .. '}')
end
-- Given a library, a function that must exist within the library, and an
-- include file prototyping the function, this function determines whether those
-- three variables are able to build a working executable. That is, if a
-- function can be properly linked to using a given library, then the library
-- can be assumed to exist. Returns true if and only if the function was
-- correctly linked to.
function check_library_exists(lib, func, inc)
local source = build_includes()
if inc ~= nil then
source = source .. '#include "' .. inc .. '"\n'
end
source = source .. 'int main(int argc, char **argv) {\n'
source = source .. '\tvoid *check = (void *) ' .. func .. ';\n'
source = source .. '\treturn 0;\n'
if lib ~= nil then
link_library(lib)
end
local result = check_cxx_source_builds(source .. '}')
reset_link_flags()
return result
end
-- This is a merge variable list version of the check_library_exists function.
-- The thing to note with this function is that it will return true for the
-- first library found to correctly link to the function. This function is used
-- to determine whether the function is found in a list of libraries, not if it
-- is found in every one of the libraries.
function check_library_exists_multiple(func, inc, ...)
for _,v in ipairs{...} do
if check_library_exists(v, func, inc) then
return true
end
end
return false
end
-- This is a wrapper for the check_library_exists function that will also
-- attempt to locate the library in question, in case it's not in a path the
-- compiler is already aware of. This function has the same return consequences
-- as check_library_exists.
function check_library_exists_lookup(lib, func, inc)
local dir = os.findlib(lib)
if dir == nil then
return false
end
include_library_dir(dir)
return check_library_exists(lib, func, inc)
end
-- Given a valid C type name, this function generates a program that will print
-- the size of the type using the sizeof operator to the console, then parse the
-- size to indicate the byte size of the type on this platform. The resulting
-- executable is dependent on stdio and the printf function, which it safely
-- checks for behind the scenes. If these dependencies are not found for
-- whatever reason, this function returns 0, otherwise it returns a proper
-- numerical value representing the size of the specified type.
function check_type_size(typename)
if not checked_printf then
checked_printf = true
has_printf = check_include_file("stdio.h") and check_function_exists("printf")
if not has_printf then
print("Warning: cannot check the size of a type without stdio and printf.")
end
end
if not has_printf then
return 0
end
local source = '#include "stdio.h"\n'
source = source .. 'int main(int argc, char **argv) {\n'
source = source .. '\tprintf("%d", sizeof(' .. typename .. '));\n'
source = source .. '\treturn 0;\n'
local success, result = check_cxx_source_runs(source .. '}');
if not success then
print("Warning: could not get the size of type: " .. typename)
return 0
end
return tonumber(result)
end

View file

@ -0,0 +1,204 @@
-- Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
--
-- This software is provided 'as-is', without any express or implied
-- warranty. In no event will the authors be held liable for any damages
-- arising from the use of this software.
--
-- Permission is granted to anyone to use this software for any purpose,
-- including commercial applications, and to alter it and redistribute it
-- freely.
--
-- Meta-build system using premake created and maintained by
-- Benjamin Henning <b.henning@digipen.edu>
--[[
sdl_dependency_checkers.lua
This script contains a bunch of functions which determine whether certain
dependencies exist on the current platform. These functions are able to use
any and all available utilities for trying to determine both whether the
dependency is available on this platform, and how to build to the dependency.
There are a few limitations with these functions, but many of the limitations
can be mitigated by using the dependency definition functions in the project
definition files.
Each function in this file, in order to be a valid dependency function, must
return a table with the following entries:
'found' = boolean value indicating whether the dependency was found
'incDirs' = table of include directory strings, or nil if none are needed
'libDirs' = table of library directory strings, or nil if none are needed
'libs' = table of libraries to link to, or nil if none are needed
All functions must be properly registered with the project definition system
in order to be properly referenced by projects.
]]
-- dependency functions must return the following:
-- table with an element found, incDirs, libDirs, and libs
function openGLDep()
print("Checking OpenGL dependencies...")
if SDL_getos() == "macosx" then
-- mac should always have support for OpenGL...
return { found = true, libs = { "OpenGL.framework" } }
elseif SDL_getos() == "ios" then
--...unless on iOS
print("Desktop OpenGL is not supported on iOS targets.")
return { found = false, libs = { "OpenGL.framework" } }
elseif SDL_getos() == "cygwin" then
print("OpenGL is not currently supported on Cygwin.")
return { found = false, libDirs = { }, libs = { "OpenGL32" } }
end
local libpath = nil
local libname = nil
if SDL_getos() == "windows" or SDL_getos() == "mingw" then
libpath = os.findlib("OpenGL32")
libname = "OpenGL32"
else -- *nix
libpath = os.findlib("libGL")
libname = "GL"
end
local foundLib = libpath ~= nil
-- another way to possibly find the dependency on windows
--if not foundLib then
-- foundLib, libpath = find_dependency_dir_windows(nil, "C:/Program Files (x86);C:/Program Files", "Microsoft SDKs", "Lib")
--end
if not foundLib then return { found = false } end
if SDL_getos() == "mingw" then
libpath = libpath:gsub("\\", "/"):gsub("//", "/")
end
return { found = foundLib, libDirs = { }, libs = { libname } }
end
function directXDep()
print("Checking DirectX dependencies...")
-- enable this for more correct searching, but it's much slower
local searchPath = nil --os.getenvpath("ProgramFiles", "ProgramFiles(x86)")
local foundInc, incpath = find_dependency_dir_windows("DXSDK_DIR", searchPath, "DirectX", "Include")
local foundLib, libpath = find_dependency_dir_windows("DXSDK_DIR", searchPath, "DirectX", "Lib/x86")
if not foundInc or not foundLib then return { found = false } end
-- XXX: hacked mingw check...
if foundInc and SDL_getos() == "mingw" then
incpath = incpath:gsub("%$%(DXSDK_DIR%)", os.getenv("DXSDK_DIR")):gsub("\\", "/"):gsub("//", "/")
libpath = libpath:gsub("%$%(DXSDK_DIR%)", os.getenv("DXSDK_DIR")):gsub("\\", "/"):gsub("//", "/")
end
if SDL_getos() == "mingw" then
print("DirectX is not currently supported on MinGW targets.")
return { found = false, incDirs = { incpath }, libDirs = { libpath } }
end
if SDL_getos() == "cygwin" then
print("DirectX is not currently supported on Cygwin targets.")
return { found = false, incDirs = { incpath }, libDirs = { libpath } }
end
return { found = true, incDirs = { incpath }, libDirs = { libpath } }
end
function dbusDep()
print("Checking for D-Bus support...")
if not check_include_directories("/usr/include/dbus-1.0", "/usr/lib/x86_64-linux-gnu/dbus-1.0/include") then
print("Warning: D-Bus unsupported!")
return { found = false }
end
return { found = true, incDirs = { "/usr/include/dbus-1.0", "/usr/lib/x86_64-linux-gnu/dbus-1.0/include" } }
end
function alsaDep()
print("Checking for ALSA support...")
if not check_include_files("alsa/asoundlib.h")
or os.findlib("asound") == nil
or not check_library_exists_lookup("asound", "snd_pcm_open", "alsa/asoundlib.h")
or not SDL_assertdepfunc("DLOpen") then
print("Warning: ALSA unsupported!")
return { found = false }
end
return { found = true }
end
function pulseAudioDep()
print("Checking for PulseAudio support...")
if os.findlib("libpulse-simple") == nil
or not SDL_assertdepfunc("DLOpen") then
print("Warning: PulseAudio unsupported!")
return { found = false }
end
return { found = true }
end
function esdDep()
print("Checking for ESD support...")
if os.findlib("esd") == nil
or not SDL_assertdepfunc("DLOpen") then
print("Warning: ESD unsupported!")
return { found = false }
end
return { found = true }
end
function nasDep()
print("Checking for NAS support...")
if not check_include_file("audio/audiolib.h")
or not SDL_assertdepfunc("DLOpen") then
print("Warning: NAS unsupported!")
return { found = false }
end
return { found = true }
end
function ossDep()
print("Checking for OSS support...")
if not check_cxx_source_compiles([[
#include <sys/soundcard.h>
int main() { int arg = SNDCTL_DSP_SETFRAGMENT; return 0; }]])
and not check_cxx_source_compiles([[
#include <soundcard.h>
int main() { int arg = SNDCTL_DSP_SETFRAGMENT; return 0; }]]) then
print("Warning: OSS unsupported!")
return { found = false }
end
return { found = true }
end
function dlOpenDep()
print("Checking for DLOpen support...")
if not check_library_exists_multiple("dlopen", "dlfcn.h", "dl", "tdl") then
print("Warning: DLOpen unsupported!")
return { found = false }
end
return { found = true, libs = { "dl" } }
end
function x11Dep()
print("Checking for X11 support...")
for _, v in ipairs { "X11", "Xext", "Xcursor", "Xinerama", "Xi", "Xrandr", "Xrender", "Xss", "Xxf86vm" } do
if os.findlib(v) == nil then
print("Warning: X11 unsupported!")
return { found = false }
end
end
if not check_include_files("X11/Xcursor/Xcursor.h", "X11/extensions/Xinerama.h",
"X11/extensions/XInput2.h", "X11/extensions/Xrandr.h", "X11/extensions/Xrender.h",
"X11/extensions/scrnsaver.h", "X11/extensions/shape.h", "X11/Xlib.h",
"X11/extensions/xf86vmode.h") then
print("Warning: X11 unsupported!")
return { found = false }
end
if not SDL_assertdepfunc("DLOpen") then
print("Warning: X11 unsupported!")
return { found = false }
end
-- XXX: shared memory check...
-- there's a LOT more to check to properly configure X11...
return { found = true, libs = { "X11" } }
end
-- register all of these dependency functions with the definition system
SDL_registerDependencyChecker("OpenGL", openGLDep)
SDL_registerDependencyChecker("DirectX", directXDep)
SDL_registerDependencyChecker("DBus", dbusDep)
SDL_registerDependencyChecker("ALSA", alsaDep)
SDL_registerDependencyChecker("PulseAudio", pulseAudioDep)
SDL_registerDependencyChecker("ESD", esdDep)
SDL_registerDependencyChecker("NAS", nasDep)
SDL_registerDependencyChecker("OSS", ossDep)
SDL_registerDependencyChecker("DLOpen", dlOpenDep)
SDL_registerDependencyChecker("X11", x11Dep)

74
premake/util/sdl_depends.lua Executable file
View file

@ -0,0 +1,74 @@
-- Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
--
-- This software is provided 'as-is', without any express or implied
-- warranty. In no event will the authors be held liable for any damages
-- arising from the use of this software.
--
-- Permission is granted to anyone to use this software for any purpose,
-- including commercial applications, and to alter it and redistribute it
-- freely.
--
-- Meta-build system using premake created and maintained by
-- Benjamin Henning <b.henning@digipen.edu>
-- This is kept just for windows because the other platforms use different means
-- for determining dependence or compatibility.
--[[
sdl_depends.lua
This file simply contains a function for determining whether a dependency
exists on the Windows platform, given a possible environmental variable,
delimited search paths, and a main and/or sub-directory paths for more
elaborate pattern matching.
]]
-- find_dependency_dir_windows(env, main_search_path, main_dir_path)
-- Attempt to resolve a dependency (true or false) folder based on either an
-- environmental variable, start search path, or both. If both are present,
-- the environmental variable will be preferred. If neither are present, this
-- function returns false.
--
-- Arguments:
-- env The name of the environmental variable to treat as a path
-- main_search_paths Paths to look for the main directory in
-- main_dir_path The a path that must be contained between main_search_path and sub_dir_path
-- sub_dir_path The path of the directories that should exist at the searched path
function find_dependency_dir_windows(env, main_search_paths, main_dir_path, sub_dir_path)
if not os.is("windows") then -- if not windows, then fail
return false
end
if env == nil and (main_search_paths == nil or #main_search_paths == 0) then
return false
end
local env_path = nil
local main_path = nil
if env ~= nil then env_path = os.getenv(env) end
local search_table = { n = 0 }
if main_search_paths ~= nil then
for k,main_search_path in ipairs(explode(main_search_paths, ";")) do
local directories = os.matchdirs(main_search_path .. "/**" .. main_dir_path .. "*")
for k,v in pairs(directories) do
table.insert(search_table, v)
end
end
end
if env_path ~= nil then table.insert(search_table, env_path) end
local search_path = table.concat(search_table, ";")
local result_path = os.dirpathsearch(sub_dir_path, search_path, ";")
if result_path == nil then
return false
end
local found_dir = os.isdir(result_path)
local abs_path = path.getabsolute(result_path)
if found_dir and env_path ~= nil then
abs_path = abs_path:gsub("\\", "/")
env_path = env_path:gsub("\\", "/")
local pos = abs_path:indexOf(env_path)
if pos ~= nil then
abs_path = abs_path:sub(1, pos - 1) .. "$(" .. env .. ")/" .. abs_path:sub(pos + #env_path)
end
end
-- we want the path in terms of '/'
return found_dir, abs_path
end

141
premake/util/sdl_file.lua Executable file
View file

@ -0,0 +1,141 @@
-- Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
--
-- This software is provided 'as-is', without any express or implied
-- warranty. In no event will the authors be held liable for any damages
-- arising from the use of this software.
--
-- Permission is granted to anyone to use this software for any purpose,
-- including commercial applications, and to alter it and redistribute it
-- freely.
--
-- Meta-build system using premake created and maintained by
-- Benjamin Henning <b.henning@digipen.edu>
--[[
sdl_file.lua
This function contains a wrapper for the I/O file operations, providing a few
custom functions which simplify the file I/O process (especially useful for
the vast amount of generation used by the meta-build system).
]]
-- Given a filename and open mode (look at io.open for more information), opens
-- the file with various contained functions for printing to the file, writing
-- to the file, reading from the file, or closing the file. If the filename is
-- nil, then this will open a file in a special text mode. In that case, the
-- mode is ignored. Returned is an instanced table with all of the
-- aforementioned functions.
--
-- The print function is associated with textprint/fileprint, the write function
-- with textwrite/filewrite, the read function with fileread, and the close
-- function with textclose/fileclose.
function fileopen(file, mode)
if file == nil then
return { texth = "", print = textprint, write = textwrite, read = nil, close = textclose }
else
return { fileh = io.open(file, mode), print = fileprint, write = filewrite, read = fileread, close = fileclose }
end
end
-- Given a filename and file mode, reads the entire contents of the file and
-- returns the contents as a string.
function readfile(file, mode)
local file = fileopen(file, mode)
local content = file:read()
file:close()
return content
end
-- Given a file, the number of tabs to indent, and a line to print, append the
-- line tabbed n times with an appended newline to the end of the input text.
function textprint(f, tabs, line)
for i = 0, tabs - 1, 1 do
f.texth = f.texth .. "\t"
end
f.texth = f.texth .. line .. "\n"
end
-- Given a file, the number of tabs to indent, and a line to print, append the
-- line tabbed n times with an appended newline to the end of the input file.
function fileprint(f, tabs, line)
for i = 0, tabs - 1, 1 do
f.fileh:write("\t")
end
f.fileh:write(line .. "\n")
end
-- Given a file and some text, append the text to the end of the input text.
function textwrite(f, text)
f.texth = f.texth .. text
end
-- Given a file and some text, append the text to the end of the input file.
function filewrite(f, text)
f.fileh:write(text)
end
-- Given a file, read all the contents of the file and return them as a string.
function fileread(file)
return file.fileh:read("*all")
end
-- Given a file opened in text mode, return the result of the current file
-- operations as a text string.
function textclose(file)
return file.texth
end
-- Given a file opened regularly, close the file handle resource, preventing
-- any future I/O operations.
function fileclose(file)
file.fileh:close()
end
-- Given a source path, builds a table containing all directories and recursive
-- subdirectories which contain files, and returns the table. Each entry in the
-- table will have a '/' at the end of its path, plus they will all be relative
-- to the parent source path. The table will contain a single entry with the
-- value '/' to indicate the source path itself.
function createDirTable(sourcePath)
local dirs = os.matchdirs(sourcePath.."/**")
for k,d in pairs(dirs) do
dirs[k] = string.sub(d, #sourcePath + 1) .. "/"
end
table.insert(dirs, "/")
return dirs
end
-- This works like os.pathsearch, but for directories. Look at the premake
-- documentation for os.pathsearch for more information.
os.dirpathsearch = function(subdir, path, path_delimiter)
for i,p in ipairs(explode(path, path_delimiter)) do
local needle = p .. "/" .. subdir
if os.isdir(needle) then
return needle
end
end
return nil
end
-- Given a variable number of environmental variable names, this will join them
-- together based on the current OS path delimeter and quietly ignoring those
-- variables which do not exist on this system. The resulting path is always
-- normalized for Unix-based path separators, regardless of the system.
os.getenvpath = function(...)
local path = ""
local pathDelimeter = ":"
if os.is("windows") then
pathDelimeter = ";"
end
for i,a in ipairs(arg) do
local value = os.getenv(a)
if value then
if #path > 0 then
path = path .. pathDelimeter
end
path = path .. value
end
end
-- normalize path to unix
return path:gsub("\\", "/"):gsub("//", "/")
end

68
premake/util/sdl_gen_config.lua Executable file
View file

@ -0,0 +1,68 @@
-- Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
--
-- This software is provided 'as-is', without any express or implied
-- warranty. In no event will the authors be held liable for any damages
-- arising from the use of this software.
--
-- Permission is granted to anyone to use this software for any purpose,
-- including commercial applications, and to alter it and redistribute it
-- freely.
--
-- Meta-build system using premake created and maintained by
-- Benjamin Henning <b.henning@digipen.edu>
--[[
sdl_gen_config.lua
Given a series of set configuration values from the project definitions,
this file contains a series of functions that generate valid preprocessor
definitions to enable or disable various features of the SDL2 library. These
definitions are pasted into a template SDL config header file, which is then
saved in the local directory and referenced to in generated project files.
This file depends on sdl_file.lua.
]]
-- The line that must exist in the template file in order to properly paste
-- the generated definitions.
local searchKey = "/%* Paste generated code here %*/\n"
local configFile, templateFileContents
local insertLocation
-- This function begins config header generation given the name of the generated
-- file and the name of the template file to use.
function startGeneration(file, template)
configFile = fileopen(file, "wt")
templateFileContents = readfile(template, "rt")
insertLocation = templateFileContents:find(searchKey)
if insertLocation then
configFile:write(templateFileContents:sub(1, insertLocation - 1))
end
end
-- Adds a table of configuration values to the generated file. Each
-- configuration line is wrapped around a preprocessor definition check, so they
-- can be manually overwritten by the developer if necessary. The definition
-- pastes string versions of both the key and the value on the line, where
-- either is allowed to be empty. That means the table stores key-value pairs.
function addConfig(tbl)
-- if no insert location, don't paste anything
if not insertLocation then return end
for k,v in pairs(tbl) do
configFile:print(0, "#ifndef " .. k)
configFile:print(0, "#define " .. tostring(k) .. " " .. tostring(v))
configFile:print(0, "#endif")
end
end
-- Finishes the generation and writes the remains of the template file into the
-- generated config file.
function endGeneration()
if insertLocation then
configFile:write(templateFileContents:sub(insertLocation + #searchKey - 2))
else -- write entire file since nothing is being pasted
configFile:write(templateFileContents)
end
configFile:close()
end

461
premake/util/sdl_projects.lua Executable file
View file

@ -0,0 +1,461 @@
-- Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
--
-- This software is provided 'as-is', without any express or implied
-- warranty. In no event will the authors be held liable for any damages
-- arising from the use of this software.
--
-- Permission is granted to anyone to use this software for any purpose,
-- including commercial applications, and to alter it and redistribute it
-- freely.
--
-- Meta-build system using premake created and maintained by
-- Benjamin Henning <b.henning@digipen.edu>
--[[
sdl_projects.lua
This file contains all the functions which are needed to define any project
within the meta-build system. Many of these functions serve as
pseudo-replacements for many similarly named premake functions, and that is
intentional. Even the implementation of these functions are intended to look
similar to regular premake code. These functions serve to dramatically
simplify the project definition process to just a few lines of code, versus
the many more needed for projects defined purely with premake.
This approach is possible because this meta-build system adds another layer of
indirection to the premake system, creating a sort of 'meta-meta-build'
system. Nevertheless, there is a lot more flexibility because the meta-build
system itself can be used to check for dependencies in a much more complex way
than premake originally intended. All of the functions useful to the project
definition system are contained in this file and are documented.
]]
projects = { }
local currentProject = nil
local currentDep = nil
local nextFuncCompat = true -- by default, unless state otherwise changed
local dependencyFunctions = { }
local dependencyResults = { } -- for when the dependencies are executed
-- query whether this function is compatible; resets internal state of
-- compatibility to true until SDL_isos is called again
local function oscompat()
local compat = nextFuncCompat
nextFuncCompat = true
return compat
end
-- determine whether the specific OS name is within a pattern.
local function osmatch(name, pattern)
local checks = pattern:explode('|')
for i,v in pairs(checks) do
if name == v then
return true
end
end
return false
end
-- Registers a dependency checker function based on a name. This function is
-- used in order to determine compatibility with the current system for a given
-- SDL_dependency. See SDL_depfunc for more information.
--
-- Specifies a function which will be invoked upon determining whether this
-- dependency is valid for the current system setup (ie, whether the system
-- has the right architecture, operating system, or even if it's installed).
-- The dependency function takes no arguments, but it must return the following
-- values:
--
-- <foundDep> [includePaths] [libPaths] [inputLibLibraries]
--
-- The last three are optional, unless foundDep is true. The name should be
-- descriptive of the outside dependency, since it may be shown to the user.
-- This function is intended to be used only after invoking SDL_dependency.
function SDL_registerDependencyChecker(name, func)
dependencyFunctions[name:lower()] = func
end
-- Initializes the definition of a SDL project given the name of the project.
function SDL_project(name)
if not oscompat() then return end
currentProject = { }
currentProject.name = name
currentProject.compat = true
projects[name] = currentProject
currentProject.dependencyTree = { }
-- stores which dependencies have already been checked on behalf of this
-- project
currentProject.dependencyValues = { }
currentDep = nil
end
-- Specifies the build kind of the SDL project (e.g. StaticLib, SharedLib,
-- ConsoleApp, etc.), based on premake presets.
function SDL_kind(k)
if not oscompat() then return end
currentProject.kind = k
end
-- Specifies which platforms this project supports. Note: this list is not the
-- exact list of supported platforms in the generated project. The list of
-- platforms this project supports will be the unique list of all combined
-- projects for this SDL solution. Thus, only one project needs to actually
-- maintain a list. This function is additive, that is, everytime it is called
-- it adds it to a unique list of platforms
function SDL_platforms(tbl)
if not oscompat() then return end
if not currentProject.platforms then
currentProject.platforms = { }
end
for k,v in pairs(tbl) do
currentProject.platforms[#currentProject.platforms + 1] = v
end
end
-- Specifies the programming language of the project, such as C or C++.
function SDL_language(k)
if not oscompat() then return end
currentProject.language = k
end
-- Specifies the root directory in which the meta-build system should search for
-- source files, given the paths and files added.
function SDL_sourcedir(src)
if not oscompat() then return end
currentProject.sourcedir = src
end
-- Specifies the destination location of where the IDE files related to the
-- project should be saved after generation.
function SDL_projectLocation(loc)
if not oscompat() then return end
currentProject.projectLocation = loc
end
-- Specifies a table of files that should be copied from the source directory
-- to the end result build directory of the binary file.
function SDL_copy(tbl)
if not oscompat() then return end
currentProject.copy = tbl
end
-- Specifies a list of other SDL projects in this workspace the currently active
-- project is dependent on. If the dependent project is a library, the binary
-- result will be copied from its directory to the build directory of the
-- currently active project automatically.
function SDL_projectDependencies(tbl)
if not oscompat() then return end
currentProject.projectDependencies = tbl
end
-- Specifies a list of compiler-level preprocessor definitions that should be
-- set in the resulting project upon compile time. This adds to the current
-- table of defines.
function SDL_defines(tbl)
if not oscompat() then return end
if not currentProject.defines then
currentProject.defines = { }
end
for k,v in pairs(tbl) do
currentProject.defines[#currentProject.defines + 1] = v
end
end
-- Initializes an outside dependency this project has, such as X11 or DirectX.
-- This function, once invoked, may change the behavior of other SDL
-- project-related functions, so be sure to be familiar with all the functions
-- and any specified behavior when used around SDL_dependency.
function SDL_dependency(name)
if not oscompat() then return end
currentDep = { nil, compat = true, }
currentDep.name = name
table.insert(currentProject.dependencyTree, currentDep)
end
-- Special function for getting the current OS. This factors in whether the
-- metabuild system is in MinGW, Cygwin, or iOS mode.
function SDL_getos()
if _OPTIONS["ios"] ~= nil then
return "ios"
elseif _OPTIONS["mingw"] ~= nil then
return "mingw"
elseif _OPTIONS["cygwin"] ~= nil then
return "cygwin"
end
return os.get()
end
-- Specifies which operating system this dependency targets, such as windows or
-- macosx, as per premake presets.
function SDL_os(name)
if not oscompat() then return end
if not currentProject then return end
if not currentDep then
currentProject.opsys = name
currentProject.compat = osmatch(SDL_getos(), name)
else
currentDep.opsys = name
currentDep.compat = osmatch(SDL_getos(), name)
end
end
-- Specifies which operating system this dependency does not targets. This is
-- for nearly platform-independent projects or dependencies that will not work
-- on specific systems, such as ios.
function SDL_notos(name)
if not oscompat() then return end
if not currentProject then return end
if not currentDep then
currentProject.opsys = "~" .. name
currentProject.compat = not osmatch(SDL_getos(), name)
else
currentDep.opsys = "~" .. name
currentDep.compat = not osmatch(SDL_getos(), name)
end
end
-- Changes the internal state of function compatibility based on whether the
-- current os is the one expected; the next function will be affected by this
-- change, but no others. The name can be a pattern using '|' to separate
-- multiple operating systems, such as:
-- SDL_isos("windows|macosx")
function SDL_isos(name)
nextFuncCompat = osmatch(SDL_getos(), name)
end
-- Same as SDL_isos, except it negates the internal state for exclusion
-- checking.
function SDL_isnotos(name)
nextFuncCompat = not osmatch(SDL_getos(), name)
end
-- Changes the internal state of function compatibility based on whether the
-- current system is running a 64bit Operating System and architecture; the
-- next function will be affected by this change, but none thereafter.
function SDL_is64bit()
nextFuncCompat = os.is64bit()
end
-- Same as SDL_is64bit, except it negates the internal state for
-- exclusion checking.
function SDL_isnot64bit()
nextFuncCompat = not os.is64bit()
end
-- Look at SDL_depfunc and SDL_notdepfunc for detailed information about this
-- function.
local function SDL_depfunc0(funcname, exclude)
if not oscompat() then return end
if not currentDep.compat then return end
local force = _OPTIONS[funcname:lower()] ~= nil
local func = dependencyFunctions[funcname:lower()]
if not func then
print("Warning: could not find dependency function named: " .. funcname)
currentDep.compat = false
return
end
local cachedFuncResults = dependencyResults[funcname:lower()]
local depFound, depInc, depLib, depInput
if cachedFuncResults then
depFound = cachedFuncResults.depFound
-- just skip the rest of the function, the user was already warned
-- exclude mode varies the compatibility slightly
if force then
depFound = true
end
if not depFound and not exclude then
currentDep.compat = false
return
elseif depFound and exclude then
currentDep.compat = false
return
end
depInc = cachedFuncResults.depInc
depLib = cachedFuncResults.depLib
depInput = cachedFuncResults.depInput
else
local result = func()
if result.found then
depFound = result.found
else
depFound = false
end
if force then
depFound = true
end
if result.incDirs then
depInc = result.incDirs
else
depInc = { }
end
if result.libDirs then
depLib = result.libDirs
else
depLib = { }
end
if result.libs then
depInput = result.libs
else
depInput = { }
end
cachedFuncResults = { }
cachedFuncResults.depFound = depFound
cachedFuncResults.depInc = depInc
cachedFuncResults.depLib = depLib
cachedFuncResults.depInput = depInput
dependencyResults[funcname:lower()] = cachedFuncResults
if not depFound and not exclude then
currentDep.compat = false
return
elseif depFound and exclude then
currentDep.compat = false
return
end
end
-- we only want to embed this dependency if we're not in exclude mode
if depFound and not exclude then
local dependency = { }
if not currentDep.includes then
currentDep.includes = { }
end
for k,v in pairs(depInc) do
currentDep.includes[v] = v
end
if not currentDep.libs then
currentDep.libs = { }
end
for k,v in pairs(depLib) do
currentDep.libs[v] = v
end
if not currentDep.links then
currentDep.links = { }
end
for k,v in pairs(depInput) do
currentDep.links[v] = v
end
else -- end of dependency found check
-- if we are not excluding this dependency, then print a warning
-- if not found
if not exclude then
print("Warning: could not find dependency: " .. funcname)
end
currentDep.compat = exclude
end
end
-- Given a dependency name, this function will register the dependency and try
-- to pair it with a dependency function that was registered through
-- SDL_registerDependencyChecker. If the function is not found, compatibility
-- will automatically be dropped for this project and a warning will be printed
-- to the standard output. Otherwise, the dependency function will be invoked
-- and compatibility for the project will be updated. If the project currently
-- is not compatible based on the Operating System or previous dependency, the
-- dependency function will not be checked at all and this function will
-- silently return.
function SDL_depfunc(funcname)
SDL_depfunc0(funcname, false)
end
-- Same as SDL_depfunc, except this forces dependency on the function failing,
-- rather than succeeding. This is useful for situations where two different
-- files are required based on whether a dependency is found (such as the
-- joystick and haptic systems).
function SDL_notdepfunc(funcname)
SDL_depfunc0(funcname, true)
end
-- Determines whether the specified dependency is supported without actually
-- executing the dependency or changing the internal states of the current
-- project or dependency definition. This function will only work if the
-- dependency has already been checked and its results cached within the
-- definition system. This function returns true if the dependency is known to
-- be supported, or false if otherwise (or if it cannot be known at this time).
function SDL_assertdepfunc(funcname)
-- if forced, then of course it's on
if _OPTIONS[funcname:lower()] then
return true
end
local results = dependencyResults[funcname:lower()]
if not results or not results.depFound then
-- either not excuted yet, doesn't exist, or wasn't found
print("Warning: required dependency not found: " .. funcname ..
". Make sure your dependencies are in a logical order.")
return false
end
return true
end
-- Returns a list of currently registered dependencies. The values within the
-- table will be sorted, but their names will be lowercased due to internal
-- handling of case-insensitive dependency names.
function SDL_getDependencies()
local deps = { }
for k,_ in pairs(dependencyFunctions) do
deps[#deps + 1] = k
end
table.sort(deps)
return deps
end
-- Specifies a list of libraries that should always be linked to in this
-- project, regardless of a dependency function. If after a dependency
-- declaration, these files will only be included in the project if the
-- dependency is compatible with the native system, given SDL_os usage and any
-- sort of custom dependency function.
function SDL_links(tbl)
if not oscompat() then return end
if currentDep and not currentDep.compat then return end
if currentProject.customLinks == nil then
currentProject.customLinks = { }
end
for i,v in ipairs(tbl) do
currentProject.customLinks[#currentProject.customLinks + 1] = v
end
end
-- Specifies a list of configuration values that are assigned as preprocessor
-- definitions in the SDL configuration header, used to globally configure
-- features during the building of the SDL library. If after a dependency
-- declaration, these files will only be included in the project if the
-- dependency is compatible with the native system, given SDL_os usage and any
-- sort of custom dependency function.
function SDL_config(tbl)
if not oscompat() then return end
if not currentDep then
currentProject.config = tbl
return
end
if not currentDep.compat then return end
currentDep.config = tbl
end
-- Specifies a list of paths where all .c, .h, and .m files should be included
-- for compiling, where the source directory is the root. If after a dependency
-- declaration, these files will only be included in the project if the
-- dependency is compatible with the native system, given SDL_os usage and any
-- sort of custom dependency function.
function SDL_paths(tbl)
if not oscompat() then return end
if not currentDep then
currentProject.paths = tbl
return
end
if not currentDep.compat then return end
currentDep.paths = tbl
end
-- Specifies a list of files found within the source directory that this project
-- should include during compile time. If after a dependency declaration, these
-- files will only be included in the project if the dependency is compatible
-- with the native system, given SDL_os usage and any sort of custom dependency
-- function.
function SDL_files(tbl)
if not oscompat() then return end
if not currentDep then
currentProject.files = tbl
return
end
if not currentDep.compat then return end
currentDep.files = tbl
end

103
premake/util/sdl_string.lua Executable file
View file

@ -0,0 +1,103 @@
-- Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
--
-- This software is provided 'as-is', without any express or implied
-- warranty. In no event will the authors be held liable for any damages
-- arising from the use of this software.
--
-- Permission is granted to anyone to use this software for any purpose,
-- including commercial applications, and to alter it and redistribute it
-- freely.
--
-- Meta-build system using premake created and maintained by
-- Benjamin Henning <b.henning@digipen.edu>
--[[
sdl_string.lua
Contains a few convenient string utility functions which are not supported in
Lua or not supported as intended.
]]
-- Performs a non-pattern based substring search of one string in another
-- string. It's of O(n^2) complexity. It returns nil if the result cannot be
-- found, otherwise it returns the starting index of the first found occurrence.
string.indexOf = function(str, substr)
local pos = 1
local i = 1
for i = 1, str:len(), 1 do
if str:sub(i, i) == substr:sub(pos, pos) then
-- have we matched the complete string?
if pos == substr:len() then
return i - pos + 1-- starting pos
end
-- matched character...keep going
pos = pos + 1
else
-- restart, no match
pos = 0
end
end
if pos == substr:len() then
return i - pos + 1
end
return nil -- no match
end
-- This is a public-access version of the explode function defined below.
function explode(str, delim)
return str:explode(delim)
end
-- Explodes a string into an array of elements, separated by a non-pattern
-- delimiter. This function is part of the string table, allowing for a
-- member-based invocation for strings.
string.explode = function(str, delim)
local exploded = { }
local needle = string.find(str, delim)
while needle ~= nil do
table.insert(exploded, string.sub(str, 0, needle - 1))
str = string.sub(str, needle + 1)
needle = string.find(str, delim)
end
table.insert(exploded, str)
return exploded
end
-- Similar to table.concat, except it supports more advanced token pasting. This
-- function is vastly used by the main meta-build script (premake4.lua) to
-- generate all the main lines of code for various tables that need to be in the
-- generated lua file.
-- - tbl: table of values to implode into a string
-- - prefix: string to paste before entire result
-- - pre: string to always paste before each entry in table
-- - post: string to always paste after each entry in table
-- - join: string to paste between entries (inclusive)
-- - suffix: string to paste after entire result
-- Returns the imploded string.
function implode(tbl, prefix, pre, post, join, suffix)
local result = ""
-- not the most efficient way to do this, but...
local itbl = { }
for k,v in pairs(tbl) do
itbl[#itbl + 1] = v
end
for i = 1, #itbl, 1 do
if pre ~= nil then
result = result .. pre
end
result = result .. itbl[i]
if post ~= nil then
result = result .. post
end
if i ~= #itbl then
result = result .. join
end
end
if prefix ~= nil then
result = prefix .. result
end
if suffix ~= nil then
result = result .. suffix
end
return result
end