Add system script support

- Look for and run premake5-system.lua on startup, before loading project script
- Add --systemscript command line option to override default name
- Add dofileopt() function to look for and run file if it exists
- Move command line arguments to new home in _premake_init.lua
This commit is contained in:
Jason Perkins 2013-09-08 11:51:33 -04:00
parent 11b4f4177b
commit 2826271bf0
5 changed files with 199 additions and 144 deletions

View File

@ -34,7 +34,6 @@
-- runtime environment setup
"_premake_init.lua",
"base/cmdline.lua",
-- project APIs
"project/project.lua",

View File

@ -7,12 +7,6 @@
--
--
-- Set up the global environment for the systems I know about. I would like to see
-- at least some if not all of this moved into add-ons in the future.
--
--
-- Set the default module search paths. Modules will generally live in a
-- folder of the same name: ninja/ninja.lua. The search order is the same
@ -33,6 +27,9 @@
package.path = table.concat(packagePaths, ";")
--
-- Set up the global environment for the systems I know about. I would like to see
-- at least some if not all of this moved into add-ons in the future.
--
-- Use Posix-style target naming by default, since it is the most common.
--
@ -75,3 +72,81 @@
configuration { "Windows or Xbox360 or C#", "StaticLib" }
targetprefix ""
targetextension ".lib"
--
-- Install Premake's default set of command line arguments.
--
newoption
{
trigger = "cc",
value = "VALUE",
description = "Choose a C/C++ compiler set",
allowed = {
{ "clang", "Clang (clang)" },
{ "gcc", "GNU GCC (gcc/g++)" },
}
}
newoption
{
trigger = "dotnet",
value = "VALUE",
description = "Choose a .NET compiler set",
allowed = {
{ "msnet", "Microsoft .NET (csc)" },
{ "mono", "Novell Mono (mcs)" },
{ "pnet", "Portable.NET (cscc)" },
}
}
newoption
{
trigger = "file",
value = "FILE",
description = "Read FILE as a Premake script; default is 'premake4.lua'"
}
newoption
{
trigger = "help",
description = "Display this information"
}
newoption
{
trigger = "os",
value = "VALUE",
description = "Generate files for a different operating system",
allowed = {
{ "aix", "IBM AIX" },
{ "bsd", "OpenBSD, NetBSD, or FreeBSD" },
{ "haiku", "Haiku" },
{ "hurd", "GNU/Hurd" },
{ "linux", "Linux" },
{ "macosx", "Apple Mac OS X" },
{ "solaris", "Solaris" },
{ "windows", "Microsoft Windows" },
}
}
newoption
{
trigger = "scripts",
value = "path",
description = "Search for additional scripts on the given path"
}
newoption
{
trigger = "systemscript",
value = "FILE",
description = "Override default system script (premake5-system.lua)"
}
newoption
{
trigger = "version",
description = "Display version information"
}

View File

@ -29,8 +29,17 @@
end
end
-- Seed the random number generator so actions don't have to do it themselves
math.randomseed(os.time())
-- Look for and run the system-wide configuration script
dofileopt(_OPTIONS["systemscript"] or { "premake5-system.lua", "premake-system.lua" })
-- The "next-gen" actions have now replaced their deprecated counterparts.
-- Provide a warning for a little while before I remove them entirely.
if _ACTION and _ACTION:endswith("ng") then
premake.warnOnce(_ACTION, "'%s' has been deprecated; use '%s' instead", _ACTION, _ACTION:sub(1, -3))
end
@ -40,12 +49,6 @@
premake.action.set(_ACTION)
-- Seed the random number generator so actions don't have to do it themselves
math.randomseed(os.time())
-- If there is a project script available, run it to get the
-- project information, available options and actions, etc.

View File

@ -1,76 +0,0 @@
--
-- cmdline.lua
-- Functions to define and handle command line actions and options.
-- Copyright (c) 2002-2013 Jason Perkins and the Premake project
--
--
-- Built-in command line options
--
newoption
{
trigger = "cc",
value = "VALUE",
description = "Choose a C/C++ compiler set",
allowed = {
{ "clang", "Clang (clang)" },
{ "gcc", "GNU GCC (gcc/g++)" },
}
}
newoption
{
trigger = "dotnet",
value = "VALUE",
description = "Choose a .NET compiler set",
allowed = {
{ "msnet", "Microsoft .NET (csc)" },
{ "mono", "Novell Mono (mcs)" },
{ "pnet", "Portable.NET (cscc)" },
}
}
newoption
{
trigger = "file",
value = "FILE",
description = "Read FILE as a Premake script; default is 'premake4.lua'"
}
newoption
{
trigger = "help",
description = "Display this information"
}
newoption
{
trigger = "os",
value = "VALUE",
description = "Generate files for a different operating system",
allowed = {
{ "aix", "IBM AIX" },
{ "bsd", "OpenBSD, NetBSD, or FreeBSD" },
{ "haiku", "Haiku" },
{ "hurd", "GNU/Hurd" },
{ "linux", "Linux" },
{ "macosx", "Apple Mac OS X" },
{ "solaris", "Solaris" },
{ "windows", "Microsoft Windows" },
}
}
newoption
{
trigger = "scripts",
value = "path",
description = "Search for additional scripts on the given path"
}
newoption
{
trigger = "version",
description = "Display version information"
}

View File

@ -1,50 +1,50 @@
--
-- globals.lua
-- Global tables and variables, replacements and extensions to Lua's global functions.
-- Copyright (c) 2002-2011 Jason Perkins and the Premake project
-- Copyright (c) 2002-2013 Jason Perkins and the Premake project
--
--
-- Create a top-level namespace for Premake's own APIs. The premake5 namespace
-- is a place to do next-gen (4.5) work without breaking the existing code (yet).
-- Create a top-level namespace for Premake's own APIs. The premake5 namespace
-- is a place to do next-gen (5.0) work without breaking the existing code (yet).
-- I think it will eventually go away.
--
premake = { }
premake5 = { }
premake.tools = { }
premake = {}
premake5 = {}
premake.tools = {}
-- The list of supported platforms; also update list in cmdline.lua
premake.platforms =
premake.platforms =
{
Native =
{
Native =
{
cfgsuffix = "",
},
x32 =
{
x32 =
{
cfgsuffix = "32",
},
x64 =
{
x64 =
{
cfgsuffix = "64",
},
Universal =
{
Universal =
{
cfgsuffix = "univ",
},
Universal32 =
{
Universal32 =
{
cfgsuffix = "univ32",
},
Universal64 =
{
Universal64 =
{
cfgsuffix = "univ64",
},
PS3 =
{
PS3 =
{
cfgsuffix = "ps3",
iscrosscompiler = true,
nosharedlibs = true,
@ -56,8 +56,8 @@
iscrosscompiler = true,
namestyle = "PS3",
},
Xbox360 =
{
Xbox360 =
{
cfgsuffix = "xbox360",
iscrosscompiler = true,
namestyle = "windows",
@ -65,6 +65,36 @@
}
--
-- Helper for the dofile() and include() function: locate a script on the
-- standard search paths of: the /scripts argument provided on the command
-- line, then the PREMAKE_PATH environment variable.
--
-- @param ...
-- A list of file names for which to search. The first one discovered
-- is the one that will be returned.
-- @return
-- The path to file if found, or nil if not.
--
local function locate(...)
for i = 1, select("#",...) do
local fname = select(i,...)
-- is this a direct path to a file?
if os.isfile(fname) then
return fname
end
-- find it on my paths
local dir = os.pathsearch(fname, _OPTIONS["scripts"], os.getenv("PREMAKE_PATH"))
if dir then
return path.join(dir, fname)
end
end
end
--
-- A replacement for Lua's built-in dofile() function, this one sets the
-- current working directory to the script's location, enabling script-relative
@ -72,30 +102,27 @@
--
local builtin_dofile = dofile
function dofile(fname)
-- remember the current working directory and file; I'll restore it shortly
local oldcwd = os.getcwd()
local oldfile = _SCRIPT
-- 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
-- find it; if I can't just continue with the name and let the
-- built-in dofile() handle reporting the error as it will
fname = locate(fname) or fname
-- use the absolute path to the script file, to avoid any file name
-- ambiguity if an error should arise
_SCRIPT = path.getabsolute(fname)
-- switch the working directory to the new script location
local newcwd = path.getdirectory(_SCRIPT)
os.chdir(newcwd)
-- run the chunk. How can I catch variable return values?
local a, b, c, d, e, f = builtin_dofile(_SCRIPT)
-- restore the previous working directory when done
_SCRIPT = oldfile
os.chdir(oldcwd)
@ -104,6 +131,31 @@
--
-- Find and execute a Lua source file present on the filesystem, but
-- continue without error if the file is not present. This is used to
-- handle optional files such as the premake-system.lua script.
--
-- @param fname
-- The name of the file to load. This may be specified as a single
-- file path or an array of file paths, in which case the first
-- file found is run.
-- @return
-- Any return values from the executed script are passed back.
--
function dofileopt(fname)
if type(fname) == "string" then fname = {fname} end
for i = 1, #fname do
local found = locate(fname[i])
if found then
return dofile(found)
end
end
end
--
-- "Immediate If" - returns one of the two values depending on the value of expr.
--
@ -115,28 +167,31 @@
return falseval
end
end
--
-- Load and run an external script file, with a bit of extra logic to make
-- including projects easier. if "path" is a directory, will look for
-- Load and run an external script file, with a bit of extra logic to make
-- including projects easier. if "path" is a directory, will look for
-- path/premake4.lua. And each file is tracked, and loaded only once.
--
io._includedFiles = { }
function include(filename)
-- if a directory, load the premake script inside it
if os.isdir(filename) then
filename = path.join(filename, "premake4.lua")
io._includedFiles = {}
function include(fname)
local found = locate(fname)
if not found then
found = locate(path.join(fname, "premake5.lua"))
end
if not found then
found = locate(path.join(fname, "premake4.lua"))
end
-- but only load each file once
filename = path.getabsolute(filename)
if not io._includedFiles[filename] then
io._includedFiles[filename] = true
dofile(filename)
fname = path.getabsolute(found or fname)
if not io._includedFiles[fname] then
io._includedFiles[fname] = true
dofile(fname)
end
end
@ -150,14 +205,14 @@
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
local builtin_type = type
function type(t)
local mt = getmetatable(t)
if (mt) then
@ -167,4 +222,3 @@
end
return builtin_type(t)
end