Quellcode durchsuchen

Use popen() to execute process

Daniele Bartolini vor 10 Jahren
Ursprung
Commit
155ccb7047
3 geänderte Dateien mit 70 neuen und 77 gelöschten Zeilen
  1. 16 30
      src/core/os.h
  2. 41 35
      src/renderers/shader.cpp
  3. 13 12
      src/resource/lua_resource.cpp

+ 16 - 30
src/core/os.h

@@ -12,6 +12,7 @@
 #include "string_utils.h"
 #include "string_utils.h"
 #include "error.h"
 #include "error.h"
 #include "temp_allocator.h"
 #include "temp_allocator.h"
+#include "string_stream.h"
 
 
 #if CROWN_PLATFORM_POSIX
 #if CROWN_PLATFORM_POSIX
 	#include <dirent.h> // opendir, readdir
 	#include <dirent.h> // opendir, readdir
@@ -280,28 +281,22 @@ namespace os
 	}
 	}
 
 
 	/// Executes a process.
 	/// Executes a process.
-	/// @a args is an array of arguments where:
-	/// @a args[0] is the path to the program executable,
-	/// @a args[1, 2, ..., n-1] is a list of arguments to pass to the executable,
-	/// @a args[n] is NULL.
-	inline int execute_process(const char* args[])
+	inline int execute_process(const char* path, const char* args, StringStream& output)
 	{
 	{
 #if CROWN_PLATFORM_POSIX
 #if CROWN_PLATFORM_POSIX
-		pid_t pid = fork();
-		CE_ASSERT(pid != -1, "fork: errno = %d", errno);
-		if (pid)
-		{
-			int statval;
-			wait(&statval);
-			return (WIFEXITED(statval)) ? WEXITSTATUS(statval) : 1;
-		}
-		else
-		{
-			int err = execv(args[0], (char* const*)args);
-			CE_ASSERT(err != -1, "execv: errno = %d", errno);
-			CE_UNUSED(err);
-			exit(EXIT_SUCCESS);
-		}
+		using namespace string_stream;
+
+		TempAllocator512 ta;
+		DynamicString cmd(path, ta);
+		cmd += " 2>&1 ";
+		cmd += args;
+		FILE* file = popen(cmd.c_str(), "r");
+
+		char buf[1024];
+		while (fgets(buf, sizeof(buf), file) != NULL)
+			output << buf;
+
+		return pclose(file);
 #elif CROWN_PLATFORM_WINDOWS
 #elif CROWN_PLATFORM_WINDOWS
 		STARTUPINFO info;
 		STARTUPINFO info;
 		memset(&info, 0, sizeof(info));
 		memset(&info, 0, sizeof(info));
@@ -310,16 +305,7 @@ namespace os
 		PROCESS_INFORMATION process;
 		PROCESS_INFORMATION process;
 		memset(&process, 0, sizeof(process));
 		memset(&process, 0, sizeof(process));
 
 
-		TempAllocator1024 alloc;
-		DynamicString cmds(alloc);
-
-		for (uint32_t i = 0; args[i] != NULL; i++)
-		{
-			cmds += args[i];
-			cmds += ' ';
-		}
-
-		int err = CreateProcess(args[0], (char*)cmds.c_str(), NULL, NULL, TRUE, 0, NULL, NULL, &info, &process);
+		int err = CreateProcess(path, args, NULL, NULL, TRUE, 0, NULL, NULL, &info, &process);
 		CE_ASSERT(err != 0, "CreateProcess: GetLastError = %d", GetLastError());
 		CE_ASSERT(err != 0, "CreateProcess: GetLastError = %d", GetLastError());
 		CE_UNUSED(err);
 		CE_UNUSED(err);
 
 

+ 41 - 35
src/renderers/shader.cpp

@@ -11,9 +11,11 @@
 #include "reader_writer.h"
 #include "reader_writer.h"
 #include "resource_manager.h"
 #include "resource_manager.h"
 #include "compile_options.h"
 #include "compile_options.h"
+#include "temp_allocator.h"
+#include "string_stream.h"
 
 
 #if CROWN_DEBUG
 #if CROWN_DEBUG
-#	define SHADERC_NAME "shaderc-debug-"
+#	define SHADERC_NAME "./shaderc-debug-"
 #else
 #else
 #	define SHADERC_NAME "shaderc-development-"
 #	define SHADERC_NAME "shaderc-development-"
 #endif // CROWN_DEBUG
 #endif // CROWN_DEBUG
@@ -34,6 +36,27 @@ namespace crown
 {
 {
 namespace shader_resource
 namespace shader_resource
 {
 {
+	int run_external_compiler(const char* infile, const char* outfile, const char* varying, const char* type, const char* platform, StringStream& output)
+	{
+		using namespace string_stream;
+
+		TempAllocator256 ta;
+		StringStream args(ta);
+		args << " -f " << infile;
+		args << " -o " << outfile;
+		args << " --varyingdef " << varying;
+		args << " --type " << type;
+		args << " --platform " << platform;
+		args << " --profile ";
+#if CROWN_PLATFORM_LINUX
+		args <<	"120";
+#elif CROWN_PLATFORM_WINDOWS
+		args << (strcmp(type, "vertex") == 0) ? "vs_3_0" : "ps_3_0";
+#endif
+
+		return os::execute_process(SHADERC_PATH, c_str(args), output);
+	}
+
 	void compile(const char* path, CompileOptions& opts)
 	void compile(const char* path, CompileOptions& opts)
 	{
 	{
 		Buffer buf = opts.read(path);
 		Buffer buf = opts.read(path);
@@ -80,42 +103,25 @@ namespace shader_resource
 		varying_file->write(varying_def.c_str(), varying_def.length());
 		varying_file->write(varying_def.c_str(), varying_def.length());
 		opts._fs.close(varying_file);
 		opts._fs.close(varying_file);
 
 
-		const char* compile_vs[] =
-		{
-			SHADERC_PATH,
-			"-f", vs_code_path.c_str(),
-			"-o", tmpvs_path.c_str(),
-			"--varyingdef", varying_def_path.c_str(),
-			"--type", "vertex",
-			"--platform", opts.platform(),
-			"--profile",
-#if CROWN_PLATFORM_LINUX
-			"120",
-#elif CROWN_PLATFORM_WINDOWS
-			"vs_3_0",
-#endif
-			NULL
-		};
-		int exitcode = os::execute_process(compile_vs);
+		TempAllocator1024 ta;
+		StringStream output(ta);
+
+		int exitcode = run_external_compiler(vs_code_path.c_str()
+			, tmpvs_path.c_str()
+			, varying_def_path.c_str()
+			, "vertex"
+			, opts.platform()
+			, output
+			);
 		CE_ASSERT(exitcode == 0, "Failed to compile vertex shader");
 		CE_ASSERT(exitcode == 0, "Failed to compile vertex shader");
 
 
-		const char* compile_fs[] =
-		{
-			SHADERC_PATH,
-			"-f", fs_code_path.c_str(),
-			"-o", tmpfs_path.c_str(),
-			"--varyingdef", varying_def_path.c_str(),
-			"--type", "fragment",
-			"--platform", opts.platform(),
-			"--profile",
-#if CROWN_PLATFORM_LINUX
-			"120",
-#elif CROWN_PLATFORM_WINDOWS
-			"ps_3_0",
-#endif
-			NULL
-		};
-		exitcode = os::execute_process(compile_fs);
+		exitcode = run_external_compiler(fs_code_path.c_str()
+			, tmpfs_path.c_str()
+			, varying_def_path.c_str()
+			, "fragment"
+			, opts.platform()
+			, output
+			);
 		if (exitcode)
 		if (exitcode)
 		{
 		{
 			opts.delete_file(tmpvs_path.c_str());
 			opts.delete_file(tmpvs_path.c_str());

+ 13 - 12
src/resource/lua_resource.cpp

@@ -10,8 +10,9 @@
 #include "temp_allocator.h"
 #include "temp_allocator.h"
 #include "array.h"
 #include "array.h"
 #include "compile_options.h"
 #include "compile_options.h"
+#include "string_stream.h"
 
 
-#define LUAJIT_NAME "luajit"
+#define LUAJIT_NAME "./luajit"
 
 
 #if CROWN_PLATFORM_WINDOWS
 #if CROWN_PLATFORM_WINDOWS
 	#define EXE ".exe"
 	#define EXE ".exe"
@@ -33,6 +34,8 @@ namespace lua_resource
 {
 {
 	void compile(const char* path, CompileOptions& opts)
 	void compile(const char* path, CompileOptions& opts)
 	{
 	{
+		using namespace string_stream;
+
 		TempAllocator1024 alloc;
 		TempAllocator1024 alloc;
 		DynamicString res_abs_path(alloc);
 		DynamicString res_abs_path(alloc);
 		TempAllocator1024 alloc2;
 		TempAllocator1024 alloc2;
@@ -40,17 +43,15 @@ namespace lua_resource
 		opts.get_absolute_path(path, res_abs_path);
 		opts.get_absolute_path(path, res_abs_path);
 		opts.get_absolute_path("bc.tmp", bc_abs_path);
 		opts.get_absolute_path("bc.tmp", bc_abs_path);
 
 
-		const char* luajit[] =
-		{
-			LUAJIT_EXE,
-			LUAJIT_FLAGS,
-			res_abs_path.c_str(),
-			bc_abs_path.c_str(),
-			NULL
-		};
-
-		int exitcode = os::execute_process(luajit);
-		CE_ASSERT(exitcode == 0, "Failed to compile lua");
+		TempAllocator1024 ta;
+		StringStream args(ta);
+		args << " " << LUAJIT_FLAGS;
+		args << " " << res_abs_path.c_str();
+		args << " " << bc_abs_path.c_str();
+
+		StringStream output(ta);
+		int exitcode = os::execute_process(LUAJIT_EXE, c_str(args), output);
+		CE_ASSERT(exitcode == 0, "Failed to compile lua:\n%s", c_str(output));
 
 
 		Buffer blob = opts.read(bc_abs_path.c_str());
 		Buffer blob = opts.read(bc_abs_path.c_str());
 		opts.delete_file(bc_abs_path.c_str());
 		opts.delete_file(bc_abs_path.c_str());