Beispiel #1
0
/**
 * Checks one or more of the standard script search locations to locate the
 * specified file. If found, returns the discovered path to the script on
 * the top of the Lua stack.
 */
int premake_test_file(lua_State* L, const char* filename, int searchMask)
{
	if (searchMask & TEST_LOCAL) {
		if (do_isfile(filename)) {
			lua_pushcfunction(L, path_getabsolute);
			lua_pushstring(L, filename);
			lua_call(L, 1, 1);
			return OKAY;
		}
	}

	if (scripts_path && (searchMask & TEST_SCRIPTS)) {
		if (do_locate(L, filename, scripts_path)) return OKAY;
	}

	if (searchMask & TEST_PATH) {
		const char* path = getenv("PREMAKE_PATH");
		if (path && do_locate(L, filename, path)) return OKAY;
	}

	#if !defined(PREMAKE_NO_BUILTIN_SCRIPTS)
	if ((searchMask & TEST_EMBEDDED) != 0) {
		/* Try to locate a record matching the filename */
		if (premake_find_embedded_script(filename) != NULL) {
			lua_pushstring(L, "$/");
			lua_pushstring(L, filename);
			lua_concat(L, 2);
			return OKAY;
		}
	}
	#endif

	return !OKAY;
}
Beispiel #2
0
int os_locate(lua_State* L)
{
	const char* path;
	int i;
	int nArgs = lua_gettop(L);

	/* Fetch premake.path */
	lua_getglobal(L, "premake");
	lua_getfield(L, -1, "path");
	path = lua_tostring(L, -1);

	for (i = 1; i <= nArgs; ++i) {
		const char* name = lua_tostring(L, i);

		/* Direct path to file? Return as absolute path */
		if (do_isfile(name)) {
			lua_pushcfunction(L, path_getabsolute);
			lua_pushvalue(L, i);
			lua_call(L, 1, 1);
			return 1;
		}

		/* do_locate(arg[i], premake.path) */
		if (do_locate(L, name, path)) {
			return 1;
		}

		/* embedded in the executable? */
		if (premake_find_embedded_script(name)) {
			lua_pushstring(L, "$/");
			lua_pushvalue(L, i);
			lua_concat(L, 2);
			return 1;
		}
	}

	return 0;
}
Beispiel #3
0
/**
 * Checks one or more of the standard script search locations to locate the
 * specified file. If found, returns the discovered path to the script on
 * the top of the Lua stack.
 */
int premake_test_file(lua_State* L, const char* filename, int searchMask)
{
	int i;

	if (searchMask & TEST_LOCAL) {
		if (do_isfile(filename)) {
			lua_pushcfunction(L, path_getabsolute);
			lua_pushstring(L, filename);
			lua_call(L, 1, 1);
			return OKAY;
		}
	}

	if (scripts_path && (searchMask & TEST_SCRIPTS)) {
		if (do_locate(L, filename, scripts_path)) return OKAY;
	}

	if (searchMask & TEST_PATH) {
		const char* path = getenv("PREMAKE_PATH");
		if (path && do_locate(L, filename, path)) return OKAY;
	}

	if ((searchMask & TEST_EMBEDDED) != 0) {
		/* Try to locate a record matching the filename */
		for (i = 0; builtin_scripts_index[i] != NULL; ++i) {
			if (strcmp(builtin_scripts_index[i], filename) == 0) {
				lua_pushstring(L, "$/");
				lua_pushstring(L, filename);
				lua_concat(L, 2);
				return OKAY;
			}
		}
	}

	return !OKAY;
}
Beispiel #4
0
int os_pathsearch(lua_State* L)
{
	int i;
	for (i = 2; i <= lua_gettop(L); ++i)
	{
		const char* path;

		if (lua_isnil(L, i))
			continue;

		path = luaL_checkstring(L, i);
		do
		{
			const char* split;

			/* look for the closest path separator ; or : */
			/* can't use : on windows because it breaks on C:\path */
			const char* semi = strchr(path, ';');
#if !defined(PLATFORM_WINDOWS)
			const char* full = strchr(path, ':');
#else
			const char* full = NULL;
#endif

			if (!semi)
			{
				split = full;
			}
			else if (!full)
			{
				split = semi;
			}
			else
			{
				split = (semi < full) ? semi : full;
			}

			/* push this piece of the full search string onto the stack */
			if (split)
			{
				lua_pushlstring(L, path, split - path);
			}
			else
			{
				lua_pushstring(L, path);
			}

			/* keep an extra copy around, so I can return it if I have a match */
			lua_pushvalue(L, -1);

			/* append the filename to make the full test path */
			lua_pushstring(L, "/");
			lua_pushvalue(L, 1);
			lua_concat(L, 3);

			/* test it - if it exists return the path */
			if (do_isfile(lua_tostring(L, -1)))
			{
				lua_pop(L, 1);
				return 1;
			}

			/* no match, set up the next try */
			lua_pop(L, 2);
			path = (split) ? split + 1 : NULL;
		}
		while (path);
	}

	return 0;
}
Beispiel #5
0
int os_locate(lua_State* L)
{
	int i, nArgs, vars;
	const char* premake_path = getenv("PREMAKE_PATH");

	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);

	for (i = 1; i <= nArgs; ++i) {
		/* Direct path to file? Return fully qualified version */
		if (do_isfile(lua_tostring(L, i))) {
			lua_pushcfunction(L, path_getabsolute);
			lua_pushvalue(L, i);
			lua_call(L, 1, 1);
			return 1;
		}

		/* Search for it... */
		lua_pushcfunction(L, os_pathsearch);
		lua_pushvalue(L, i);

		/* ...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);
		if (!lua_isnil(L, -1)) {
			lua_pushcfunction(L, path_join);
			lua_pushvalue(L, -2);
			lua_pushvalue(L, i);
			lua_call(L, 2, 1);
			return 1;
		}

		lua_pop(L, 1);
	}

	return 0;
}