From 1bd13569b9aa7c0110522806c876fcbe13ee2b4f Mon Sep 17 00:00:00 2001 From: Jason Perkins Date: Tue, 3 Feb 2015 19:25:30 -0500 Subject: [PATCH] Populate new global premake.path with search path; use in script loader I've only implemented searching on the --scripts path with this commit; more paths coming in next commits. --- src/host/os_locate.c | 72 +++++++++------------------------------- src/host/os_pathsearch.c | 7 ++-- src/host/premake.c | 38 +++++++++++++++++++-- 3 files changed, 56 insertions(+), 61 deletions(-) diff --git a/src/host/os_locate.c b/src/host/os_locate.c index de2b5f4a..606e10df 100644 --- a/src/host/os_locate.c +++ b/src/host/os_locate.c @@ -1,7 +1,7 @@ /** * \file os_locate.c * \brief Locates files along the standard built-in search paths. - * \author Copyright (c) 2014 Jason Perkins and the Premake project + * \author Copyright (c) 2014-2015 Jason Perkins and the Premake project */ #include @@ -10,18 +10,15 @@ int os_locate(lua_State* L) { - int i, nArgs, vars; - const char* premake_path = getenv("PREMAKE_PATH"); + int i; + int nArgs = lua_gettop(L); - nArgs = lua_gettop(L); - - /* see if the global environment variables have been set yet */ - lua_getglobal(L, "_USER_HOME_DIR"); - vars = !lua_isnil(L, -1); - lua_pop(L, 1); + /* Fetch premake.path */ + lua_getglobal(L, "premake"); + lua_getfield(L, -1, "path"); for (i = 1; i <= nArgs; ++i) { - /* Direct path to file? Return fully qualified version */ + /* Direct path to file? Return as absolute path */ if (do_isfile(lua_tostring(L, i))) { lua_pushcfunction(L, path_getabsolute); lua_pushvalue(L, i); @@ -29,57 +26,18 @@ int os_locate(lua_State* L) return 1; } - /* Search for it... */ + /* Call os.pathsearch(arg[i], premake.path) */ lua_pushcfunction(L, os_pathsearch); lua_pushvalue(L, i); + lua_pushvalue(L, -3); + lua_call(L, 2, 1); - /* ...relative to the main project script */ - if (vars) { - lua_getglobal(L, "_MAIN_SCRIPT_DIR"); - } - - /* ...relative to the CWD */ - lua_pushstring(L, "."); - - /* ...on the paths specified by --scripts, if set */ - if (scripts_path) { - lua_pushstring(L, scripts_path); - } - - /* ... relative to ~/.premake */ - if (vars) { - lua_getglobal(L, "_USER_HOME_DIR"); - lua_pushstring(L, "/.premake"); - lua_concat(L, 2); - } - - /* ...on the PREMAKE_PATH environment variable, if set */ - if (premake_path) { - lua_pushstring(L, premake_path); - } - - /* ...relative to the Premake executable */ - if (vars) { - lua_getglobal(L, "_PREMAKE_DIR"); - } - - /* ...in ~/Library/Application Support/Premake (for OS X) */ - if (vars) { - lua_getglobal(L, "_USER_HOME_DIR"); - lua_pushstring(L, "/Library/Application Support/Premake"); - lua_concat(L, 2); - } - - /* ...in the expected Unix-y places */ - lua_pushstring(L, "/usr/local/share/premake"); - lua_pushstring(L, "/usr/share/premake"); - - lua_call(L, lua_gettop(L) - nArgs - 1, 1); + /* os.pathsearch() returns the directory containing the file; + * append the filename to complete the path */ if (!lua_isnil(L, -1)) { - lua_pushcfunction(L, path_join); - lua_pushvalue(L, -2); - lua_pushvalue(L, i); - lua_call(L, 2, 1); + lua_pushstring(L, "/"); + lua_pushvalue(L, 1); + lua_concat(L, 3); return 1; } diff --git a/src/host/os_pathsearch.c b/src/host/os_pathsearch.c index 44f775d6..ec11e579 100644 --- a/src/host/os_pathsearch.c +++ b/src/host/os_pathsearch.c @@ -1,7 +1,7 @@ /** * \file os_pathsearch.c * \brief Locates a file, given a set of search paths. - * \author Copyright (c) 2002-2008 Jason Perkins and the Premake project + * \author Copyright (c) 2002-2015 Jason Perkins and the Premake project * * \note This function is required by the bootstrapping code; it must be * implemented here in the host and not scripted. @@ -66,10 +66,13 @@ int os_pathsearch(lua_State* L) lua_pushvalue(L, 1); lua_concat(L, 3); - /* test it - if it exists return the path */ + /* test it - if it exists, return the absolute path */ if (do_isfile(lua_tostring(L, -1))) { lua_pop(L, 1); + lua_pushcfunction(L, path_getabsolute); + lua_pushvalue(L, -2); + lua_call(L, 1, 1); return 1; } diff --git a/src/host/premake.c b/src/host/premake.c index e9138c13..29415e59 100644 --- a/src/host/premake.c +++ b/src/host/premake.c @@ -20,6 +20,7 @@ #define ERROR_MESSAGE "Error: %s\n" +static void build_premake_path(lua_State* L); static int process_arguments(lua_State* L, int argc, const char** argv); @@ -138,6 +139,9 @@ int premake_execute(lua_State* L, int argc, const char** argv, const char* scrip return !OKAY; } + /* Use --scripts and PREMAKE_PATH to populate premake.path */ + build_premake_path(L); + /* load the main script */ if (luaL_dofile(L, script) != OKAY) { printf(ERROR_MESSAGE, lua_tostring(L, -1)); @@ -250,7 +254,7 @@ int premake_locate(lua_State* L, const char* argv0) -const char* set_scripts_path(const char* relativePath) +static const char* set_scripts_path(const char* relativePath) { char* path = (char*)malloc(PATH_MAX); do_getabsolute(path, relativePath, NULL); @@ -260,13 +264,43 @@ const char* set_scripts_path(const char* relativePath) +/** + * Set the premake.path variable, pulling from the --scripts argument + * and PREMAKE_PATH environment variable if present. + */ +static void build_premake_path(lua_State* L) +{ + int top; + + lua_getglobal(L, "premake"); + top = lua_gettop(L); + + /* Start by searching the current working directory */ + lua_pushstring(L, "."); + + /* The --scripts argument goes next, if present */ + if (scripts_path) { + lua_pushstring(L, ";"); + lua_pushstring(L, scripts_path); + } + + /* Put it all together and set the path variable */ + lua_concat(L, lua_gettop(L) - top); + lua_setfield(L, -2, "path"); + + /* Remove the premake namespace table */ + lua_pop(L, 1); +} + + + /** * Copy all command line arguments into the script-side _ARGV global, and * check for the presence of a /scripts= argument to help locate * the manifest if needed. * \returns OKAY if successful. */ -int process_arguments(lua_State* L, int argc, const char** argv) +static int process_arguments(lua_State* L, int argc, const char** argv) { int i;