Просмотр исходного кода

Merge branch 'cross-compilation' of https://github.com/taylor001/crown into cross-compilation

Conflicts:
	CMakeLists.txt
	tests/CMakeLists.txt
Daniele Bartolini 12 лет назад
Родитель
Сommit
b55912c682

+ 12 - 12
CMakeLists.txt

@@ -35,7 +35,7 @@ set (INCLUDES
 )
 
 include_directories(${INCLUDES})
-add_definitions(-pipe -ansi -W -Wall -Wextra -Wno-long-long -pedantic -fPIC -g -pg -llua)
+add_definitions(-pipe -ansi -W -Wall -Wextra -Wno-long-long -pedantic -fPIC -g -pg)
 
 # detect operating system
 if (${CMAKE_SYSTEM_NAME} MATCHES "Linux")
@@ -48,6 +48,17 @@ if (${CMAKE_SYSTEM_NAME} MATCHES "Windows")
 	add_subdirectory(src/os/win)
 endif (${CMAKE_SYSTEM_NAME} MATCHES "Windows")
 
+#detect architecture
+if (CROWN_ARCH MATCHES "x86")
+	set (CROWN_THIRD third/x86)
+endif (CROWN_ARCH MATCHES "x86")
+
+if (CROWN_ARCH MATCHES "x86_64")
+	set (CROWN_THIRD third/x86_64)
+endif (CROWN_ARCH MATCHES "x86_64")
+
+add_subdirectory(${CROWN_THIRD})
+
 # add build directories
 add_subdirectory(src)
 
@@ -71,14 +82,3 @@ if (CROWN_BUILD_TESTS)
 	add_subdirectory(tests)
 endif (CROWN_BUILD_TESTS)
 
-#detect architecture
-if (CROWN_ARCH MATCHES "x86")
-	set (CROWN_THIRD third/x86)
-endif (CROWN_ARCH MATCHES "x86")
-
-if (CROWN_ARCH MATCHES "x86_64")
-	set (CROWN_THIRD third/x86_64)
-endif (CROWN_ARCH MATCHES "x86_64")
-
-add_subdirectory(${CROWN_THIRD})
-

+ 7 - 0
src/core/strings/String.h

@@ -49,6 +49,7 @@ const char*			strstr(const char* str1, const char* str2);
 int32_t				strcmp(const char* str1, const char* str2);
 char*				strcpy(char* dest, const char* src);
 char*				strncpy(char* dest, const char* src, size_t len);
+char* 				strcat(char* dest, const char* src);
 char*				strncat(char* dest, const char* src, size_t len);
 
 const char*			begin(const char* str);
@@ -341,6 +342,12 @@ inline char* strncpy(char* dest, const char* src, size_t len)
 	return ::strncpy(dest, src, len);
 }
 
+//-----------------------------------------------------------------------------
+inline char* strcat(char* dest, const char* src)
+{
+	return ::strcat(dest, src);
+}
+
 //-----------------------------------------------------------------------------
 inline char* strncat(char* dest, const char* src, size_t len)
 {

+ 3 - 1
tests/CMakeLists.txt

@@ -22,8 +22,10 @@ target_link_libraries(compressors crown)
 target_link_libraries(connections crown)
 target_link_libraries(strings crown)
 target_link_libraries(paths crown)
-target_link_libraries(script crown)
 
+link_directories(${CROWN_THIRD}/luajit/lib)
+include_directories(${CROWN_THIRD}/luajit/include)
+target_link_libraries(script crown lua m)
 
 add_test(allocators-test ${EXECUTABLE_OUTPUT_PATH}/allocators)
 add_test(containers-test containers)

+ 5 - 2
tests/script.cpp

@@ -18,7 +18,9 @@ int main ()
 
     /* Load the file containing the script we are going to run */
     status = luaL_loadfile(L, "hello.lua");
-    if (status) {
+
+    if (status) 
+    {
         /* If something went wrong, error message is at the top of */
         /* the stack */
         fprintf(stderr, "Couldn't load file: %s\n", lua_tostring(L, -1));
@@ -47,7 +49,8 @@ int main ()
      * of the stack, so that after it has been called, the table is at the
      * top of the stack.
      */
-    for (i = 1; i <= 5; i++) {
+    for (i = 1; i <= 5; i++) 
+    {
         lua_pushnumber(L, i);   /* Push the table index */
         lua_pushnumber(L, i*2); /* Push the cell value */
         lua_rawset(L, -3);      /* Stores the pair in the table */

+ 4 - 3
third/x86/CMakeLists.txt

@@ -5,9 +5,10 @@ install (FILES 	luajit/include/luajit-2.0/lua.hpp
 				luajit/include/luajit-2.0/lua.h
 				luajit/include/luajit-2.0/luaconf.h
 				luajit/include/luajit-2.0/luajit.h
-				luajit/include/luajit-2.0/lualib.h DESTINATION include/crown)
+				luajit/include/luajit-2.0/lualib.h DESTINATION include/crown/luajit)
 				
 install (DIRECTORY luajit/lib/lua
 				   luajit/lib/pkgconfig DESTINATION lib/crown)
-install (FILES luajit/lib/libluajit-5.1.so.2.0.1
-			   luajit/lib/libluajit-5.1.so.2 DESTINATION lib/crown)
+install (FILES luajit/lib/libluajit-5.1.so.2.0.1 DESTINATION lib/crown)
+
+install (FILES luajit/bin/luajit-2.0.1 DESTINATION bin)

+ 4 - 3
third/x86_64/CMakeLists.txt

@@ -5,9 +5,10 @@ install (FILES 	luajit/include/luajit-2.0/lua.hpp
 				luajit/include/luajit-2.0/lua.h
 				luajit/include/luajit-2.0/luaconf.h
 				luajit/include/luajit-2.0/luajit.h
-				luajit/include/luajit-2.0/lualib.h DESTINATION include/crown)
+				luajit/include/luajit-2.0/lualib.h DESTINATION include/crown/luajit)
 				
 install (DIRECTORY luajit/lib/lua
 				   luajit/lib/pkgconfig DESTINATION lib/crown)
-install (FILES luajit/lib/libluajit-5.1.so.2.0.1
-			   luajit/lib/libluajit-5.1.so.2 DESTINATION lib/crown)
+install (FILES luajit/lib/libluajit-5.1.so.2.0.1 DESTINATION lib/crown)
+
+install (FILES luajit/bin/luajit-2.0.1 DESTINATION bin)

+ 0 - 1
third/x86_64/luajit/lib/libluajit-5.1.so

@@ -1 +0,0 @@
-libluajit-5.1.so.2.0.1

+ 0 - 1
third/x86_64/luajit/lib/libluajit-5.1.so.2

@@ -1 +0,0 @@
-libluajit-5.1.so.2.0.1

+ 1 - 1
tools/compilers/CMakeLists.txt

@@ -22,7 +22,7 @@ target_link_libraries(resource-hash crown)
 
 add_subdirectory(txt)
 add_subdirectory(tga)
-#add_subdirectory(lua)
+add_subdirectory(lua)
 add_subdirectory(mat)
 
 install (TARGETS crown-compiler-utils DESTINATION lib/${CMAKE_PROJECT_NAME})

+ 3 - 1
tools/compilers/lua/CMakeLists.txt

@@ -10,6 +10,8 @@ set (HEADERS
 )
 
 add_executable(lua-compiler ${SRC} ${HEADERS})
-target_link_libraries(lua-compiler crown-compiler-utils crown-luajit)
+link_directories(${CROWN_THIRD}/luajit/lib)
+include_directories(${CROWN_THIRD}/luajit/include)
+target_link_libraries(lua-compiler crown-compiler-utils lua)
 
 install (TARGETS lua-compiler DESTINATION bin)

+ 22 - 0
tools/compilers/lua/LuaCompiler.cpp

@@ -17,6 +17,28 @@ LuaCompiler::LuaCompiler(const char* root_path, const char* dest_path, const cha
 //-----------------------------------------------------------------------------
 bool LuaCompiler::compile()
 {
+	int32_t status;
+    lua_State *L;
+
+    luaL_openlibs(L); // Load Lua libraries 
+
+    char* file;
+    strcpy(file, root_path());
+    strcat(file, resource_path());
+
+    /* Load the file containing the script we are going to run */
+    status = luaL_loadfile(L, file);
+
+    if (status)
+    {
+        printf("Couldn't load file: %s\n", lua_tostring(L, -1));
+        return -1;
+    }
+    else
+    {
+    	printf("yeah!\n");
+    }
+
 	return true;
 }
 

+ 77 - 0
tools/compilers/lua/bytecode-generator.lua

@@ -0,0 +1,77 @@
+local root_path = arg[2]
+local dest_path = arg[4]
+
+-------------------------------------------------------------
+function get_files(path)
+
+	local files = {}
+	local tmp_file = 'tmp.txt'
+
+	os.execute('ls -1 ' .. path .. '*.lua > ' .. tmp_file)
+
+	local f = io.open(tmp_file)
+	if not f then
+		return
+	end
+
+	local i = 1;
+
+	for line in f:lines() do
+		files[i] = line
+		i = i + 1
+	end
+
+	f.close()
+
+	return files
+
+end
+
+--------------------------------------------------------------
+function prepare_destination(files)
+	-- Portable bytecode
+	local ext = ".raw"
+	local raws =  {}
+
+	for i,s in ipairs(files) do
+		raws[i] = string.sub(s, 1 + string.len(root_path))
+		local len = string.len(raws[i])
+		raws[i] = string.sub(raws[i], 1, len -4)
+		raws[i] = raws[i] .. ext
+		raws[i] = dest_path .. raws[i]
+	end
+
+	return raws
+end
+
+
+
+--------------------------------------------------------------
+function generate_bytecode(src, dest)
+
+	local chunk = string.dump(loadfile(src), true)
+
+	local f,err = io.open(dest, "w")
+
+	if not f then 
+		return print(err) 
+	end
+
+	f:write(chunk)
+
+	f:close()
+
+end
+
+--------------------------------------------------------------
+
+local src  = get_files(root_path)
+
+local dest = prepare_destination(src)
+
+for i,s in ipairs(src) do
+	generate_bytecode(s, dest[i])
+end
+
+
+

+ 42 - 35
tools/compilers/lua/main.cpp

@@ -4,14 +4,15 @@
 #include <sys/wait.h>
 #include <unistd.h>
 
-#include "lua.hpp"
 #include "Crown.h"
+#include "LuaCompiler.h"
 
 using namespace crown;
 
 const char* root_path = NULL;
+const char* dest_path = NULL;
 const char* resource_in = NULL;
-const char* resource_out = NULL;
+uint32_t hash_seed = 0;
 
 void 	parse_command_line(int argc, char** argv);
 void 	print_help_message(const char* program_name);
@@ -20,34 +21,29 @@ void	compile_script(char* tmp_out);
 /// Lua scripts compiler
 int main(int argc, char** argv)
 {
-	// parse_command_line(argc, argv);
+	parse_command_line(argc, argv);
 	
-	// // FIXME: validate input
+	if (root_path == NULL)
+	{
+		printf("%s: ERROR: you have to specify the root path with `--root-path`\n", argv[0]);
+		exit(-1);
+	}
 
-	// Filesystem fs_root(root_path);
-	
-	// if (!fs_root.exists(resource_in))
-	// {
-	// 	printf("%s: ERROR: %s does not exist. Aborting.\n", argv[0], resource_in);
-	// 	return -1;
-	// }
-
-	// char resource_basename[256];
-	// char resource_extension[256];
-	
-	// path::filename_without_extension(resource_in, resource_basename, 256);
-	// path::extension(resource_in, resource_extension, 256);
-	
-	// uint32_t resource_basename_hash = hash::fnv1a_32(resource_basename, string::strlen(resource_basename));
-	// uint32_t resource_extension_hash = hash::fnv1a_32(resource_extension, string::strlen(resource_extension));
+	if (dest_path == NULL)
+	{
+		printf("%s: ERROR: you have to specify the destination path with `--dest-path`\n", argv[0]);
+		exit(-1);
+	}
+
+	if (resource_in == NULL)
+	{
+		printf("%s: ERROR: you have to specify the resource name with `--resource-in`\n", argv[0]);
+		exit(-1);
+	}
 
-	// char tmp_file[256];
-	// compile_script(tmp_file);
+	LuaCompiler compiler(root_path, dest_path, resource_in, hash_seed);
+	compiler.compile();
 
-	// FileStream* src_file = (FileStream*)fs_root.open(tmp_file, SOM_READ);
-	
-	// size_t src_file_size = src_file->size();
-	
 	// ArchiveEntry archive_entry;
 	// archive_entry.name = resource_basename_hash;
 	// archive_entry.type = resource_extension_hash;
@@ -70,13 +66,11 @@ int main(int argc, char** argv)
 	// fs_root.close(dest_file);	
 
 	// printf("Resource compilation completed: %s\n", resource_out);
-	// lua_State *L;
-	 // = lua_open();
- //  	lua_close(L);
 
 	return 0;
 }
 
+//-----------------------------------------------------------------------------
 void parse_command_line(int argc, char** argv)
 {
 	// Parse arguments
@@ -84,8 +78,9 @@ void parse_command_line(int argc, char** argv)
 	{
 		"help",         AOA_NO_ARGUMENT,       NULL,        'h',
 		"root-path",    AOA_REQUIRED_ARGUMENT, NULL,        'r',
+		"dest-path",    AOA_REQUIRED_ARGUMENT, NULL,        'd',
 		"resource-in",  AOA_REQUIRED_ARGUMENT, NULL,        'i',
-		"resource-out", AOA_REQUIRED_ARGUMENT, NULL,        'o',
+		"seed",         AOA_REQUIRED_ARGUMENT, NULL,        's',
 		NULL, 0, NULL, 0
 	};
 
@@ -120,6 +115,19 @@ void parse_command_line(int argc, char** argv)
 				
 				break;
 			}
+			// Dest path
+			case 'd':
+			{
+				if (args.option_argument() == NULL)
+				{
+					printf("%s: ERROR: missing path after `--dest-path`\n", argv[0]);
+					exit(-1);
+				}
+				
+				dest_path = args.option_argument();
+				
+				break;
+			}
 			// Resource in
 			case 'i':
 			{
@@ -133,17 +141,16 @@ void parse_command_line(int argc, char** argv)
 				
 				break;
 			}
-			// Resource out
-			case 'o':
+			case 's':
 			{
 				if (args.option_argument() == NULL)
 				{
-					printf("%s: ERROR: missing path after `--resource-out`\n", argv[0]);
+					printf("%s: ERROR: missing seed value after `--seed`\n", argv[0]);
 					exit(-1);
 				}
 
-				resource_out = args.option_argument();
-				
+				hash_seed = atoi(args.option_argument());
+
 				break;
 			}
 			default:

+ 6 - 1
tools/pycrown/Compiler.py

@@ -26,6 +26,7 @@ import subprocess
 
 TXT_C = "txt-compiler"
 TGA_C = "tga-compiler"
+LUA_C = "lua-compiler"
 RES_H = "resource-hash"
 
 ROOT_P = "--root-path"
@@ -95,5 +96,9 @@ class Compiler:
 		if resource.endswith('.txt'):
 			p = subprocess.call([TXT_C, ROOT_P, root_path, DEST_P, self.m_dest_path, RES_IN, resource, SEED, str(self.m_perfect_seed)]);
 		if resource.endswith('.tga'):
-			p = subprocess.call([TGA_C, ROOT_P, root_path, DEST_P, self.m_dest_path, RES_IN, resource, SEED, str(self.m_perfect_seed)]);	
+			p = subprocess.call([TGA_C, ROOT_P, root_path, DEST_P, self.m_dest_path, RES_IN, resource, SEED, str(self.m_perfect_seed)]);
+		if resource.endswith('.lua'):
+			p = subprocess.call([LUA_C, ROOT_P, root_path, DEST_P, self.m_dest_path, RES_IN, resource, SEED, str(self.m_perfect_seed)]);
+
+