/** * Initialize the Premake Lua environment. */ int premake_init(lua_State* L) { luaL_register(L, "criteria", criteria_functions); luaL_register(L, "debug", debug_functions); luaL_register(L, "path", path_functions); luaL_register(L, "os", os_functions); luaL_register(L, "string", string_functions); /* push the application metadata */ lua_pushstring(L, LUA_COPYRIGHT); lua_setglobal(L, "_COPYRIGHT"); lua_pushstring(L, VERSION); lua_setglobal(L, "_PREMAKE_VERSION"); lua_pushstring(L, COPYRIGHT); lua_setglobal(L, "_PREMAKE_COPYRIGHT"); lua_pushstring(L, PROJECT_URL); lua_setglobal(L, "_PREMAKE_URL"); /* set the OS platform variable */ lua_pushstring(L, PLATFORM_STRING); lua_setglobal(L, "_OS"); /* publish the initial working directory */ os_getcwd(L); lua_setglobal(L, "_WORKING_DIR"); return OKAY; }
/** * Initialize the Premake Lua environment. */ int premake_init(lua_State* L) { const char* value; luaL_register(L, "criteria", criteria_functions); luaL_register(L, "debug", debug_functions); luaL_register(L, "path", path_functions); luaL_register(L, "os", os_functions); luaL_register(L, "string", string_functions); luaL_register(L, "buffered", buffered_functions); #ifdef PREMAKE_CURL luaL_register(L, "http", http_functions); #endif #ifdef PREMAKE_COMPRESSION luaL_register(L, "zip", zip_functions); #endif /* push the application metadata */ lua_pushstring(L, LUA_COPYRIGHT); lua_setglobal(L, "_COPYRIGHT"); lua_pushstring(L, VERSION); lua_setglobal(L, "_PREMAKE_VERSION"); lua_pushstring(L, COPYRIGHT); lua_setglobal(L, "_PREMAKE_COPYRIGHT"); lua_pushstring(L, PROJECT_URL); lua_setglobal(L, "_PREMAKE_URL"); /* set the OS platform variable */ lua_pushstring(L, PLATFORM_STRING); lua_setglobal(L, "_OS"); /* find the user's home directory */ value = getenv("HOME"); if (!value) value = getenv("USERPROFILE"); if (!value) value = "~"; lua_pushstring(L, value); lua_setglobal(L, "_USER_HOME_DIR"); /* publish the initial working directory */ os_getcwd(L); lua_setglobal(L, "_WORKING_DIR"); /* start the premake namespace */ lua_newtable(L); lua_setglobal(L, "premake"); return OKAY; }
/** * Locate the Premake executable, and push its full path to the Lua stack. * Based on: * http://sourceforge.net/tracker/index.php?func=detail&aid=3351583&group_id=71616&atid=531880 * http://stackoverflow.com/questions/933850/how-to-find-the-location-of-the-executable-in-c * http://stackoverflow.com/questions/1023306/finding-current-executables-path-without-proc-self-exe */ int premake_locate_executable(lua_State* L, const char* argv0) { char buffer[PATH_MAX]; const char* path = NULL; #if PLATFORM_WINDOWS DWORD len = GetModuleFileName(NULL, buffer, PATH_MAX); if (len > 0) path = buffer; #endif #if PLATFORM_MACOSX CFURLRef bundleURL = CFBundleCopyExecutableURL(CFBundleGetMainBundle()); CFStringRef pathRef = CFURLCopyFileSystemPath(bundleURL, kCFURLPOSIXPathStyle); if (CFStringGetCString(pathRef, buffer, PATH_MAX - 1, kCFStringEncodingUTF8)) path = buffer; #endif #if PLATFORM_LINUX int len = readlink("/proc/self/exe", buffer, PATH_MAX); if (len > 0) path = buffer; #endif #if PLATFORM_BSD int len = readlink("/proc/curproc/file", buffer, PATH_MAX); if (len < 0) len = readlink("/proc/curproc/exe", buffer, PATH_MAX); if (len > 0) path = buffer; #endif #if PLATFORM_SOLARIS int len = readlink("/proc/self/path/a.out", buffer, PATH_MAX); if (len > 0) path = buffer; #endif /* As a fallback, search the PATH with argv[0] */ if (!path) { lua_pushcfunction(L, os_pathsearch); lua_pushstring(L, argv0); lua_pushstring(L, getenv("PATH")); if (lua_pcall(L, 2, 1, 0) == OKAY && !lua_isnil(L, -1)) { lua_pushstring(L, "/"); lua_pushstring(L, argv0); lua_concat(L, 3); path = lua_tostring(L, -1); } } /* If all else fails, use argv[0] as-is and hope for the best */ if (!path) { /* make it absolute, if needed */ os_getcwd(L); lua_pushstring(L, "/"); lua_pushstring(L, argv0); if (!path_isabsolute(L)) { lua_concat(L, 3); } else { lua_pop(L, 1); } path = lua_tostring(L, -1); } lua_pushstring(L, path); return 1; }