From c257e9c746f096ca4294168a8a2c26de538eb36c Mon Sep 17 00:00:00 2001 From: starkos Date: Fri, 31 Oct 2008 18:38:05 +0000 Subject: First integration of new Lua-based architecture --- src/base/globals.lua | 126 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 src/base/globals.lua (limited to 'src/base/globals.lua') diff --git a/src/base/globals.lua b/src/base/globals.lua new file mode 100644 index 0000000..9b686a8 --- /dev/null +++ b/src/base/globals.lua @@ -0,0 +1,126 @@ +-- +-- globals.lua +-- Global tables and variables, replacements and extensions to Lua's global functions. +-- Copyright (c) 2002-2008 Jason Perkins and the Premake project +-- + + +-- The list of defined solutions (which contain projects, etc.) + + _SOLUTIONS = { } + + +-- The list of built-in output templates + + _TEMPLATES = { } + + +-- A top-level namespace for support functions + + premake = { } + + +-- The list of registered actions + + premake.actions = { } + + +-- The list of tool interfaces + + premake.tools = { } + + +-- +-- A replacement for Lua's built-in dofile() function, this one sets the +-- current working directory to the script's location, enabling script-relative +-- referencing of other files and resources. +-- + + local builtin_dofile = dofile + function dofile(fname) + -- remember the current working directory; I'll restore it shortly + local oldcwd = os.getcwd() + + -- if the file doesn't exist, check the search path + if (not os.isfile(fname)) then + local path = os.pathsearch(fname, _OPTIONS["scripts"], os.getenv("PREMAKE_PATH")) + if (path) then + fname = path.."/"..fname + end + end + + -- use the absolute path to the script file, to avoid any file name + -- ambiguity if an error should arise + fname = path.getabsolute(fname) + + -- switch the working directory to the new script location + local newcwd = path.getdirectory(fname) + os.chdir(newcwd) + + -- run the chunk. How can I catch variable return values? + local a, b, c, d, e, f = builtin_dofile(fname) + + -- restore the previous working directory when done + os.chdir(oldcwd) + return a, b, c, d, e, f + end + + + +-- +-- A shortcut for including another "premake4.lua" file, often used for projects. +-- + + function include(fname) + return dofile(fname .. "/premake4.lua") + end + + + +-- +-- Open an overload of the io.open() function, which will create any missing +-- subdirectories in the filename if "mode" is set to writeable. +-- + + local builtin_open = io.open + function io.open(fname, mode) + if (mode) then + if (mode:find("w")) then + local dir = path.getdirectory(fname) + ok, err = os.mkdir(dir) + if (not ok) then + error(err, 0) + end + end + end + return builtin_open(fname, mode) + end + + + +-- +-- A shortcut for printing formatted output. +-- + + function printf(msg, ...) + print(string.format(msg, unpack(arg))) + end + + + +-- +-- An extension to type() to identify project object types by reading the +-- "__type" field from the metatable. +-- + + local builtin_type = type + function type(t) + local mt = getmetatable(t) + if (mt) then + if (mt.__type) then + return mt.__type + end + end + return builtin_type(t) + end + \ No newline at end of file -- cgit v1.2.3