From 9f9583ab24ee0c444a755729d76bc854b61b3cd8 Mon Sep 17 00:00:00 2001 From: bergmann89 Date: Sat, 19 Sep 2015 13:20:23 +0200 Subject: [PATCH] * implemented examples for linux --- header/examples/c++/Makefile | 10 +- header/examples/c++/example.cpp | 16 ++- header/examples/c/Makefile | 10 +- header/examples/c/example.c | 16 ++- header/examples/fpc/example.lpr | 15 ++- header/libShaderFile.h | 25 ++++- header/libShaderFile.hpp | 81 +++++++++------- header/ulibShaderFile.pas | 61 +++++++++--- libShaderFile.lpi | 166 +++++++++++++++++++++++++++++++- 9 files changed, 336 insertions(+), 64 deletions(-) diff --git a/header/examples/c++/Makefile b/header/examples/c++/Makefile index f99c686..3b6533a 100644 --- a/header/examples/c++/Makefile +++ b/header/examples/c++/Makefile @@ -1,5 +1,11 @@ +ifeq ($(OS),Windows_NT) + LDFLAGS= +else + LDFLAGS=-ldl +endif + all: example.cpp ../../libShaderFile.h - g++ -o example example.cpp + g++ -o example example.cpp $(LDFLAGS) clean: - rm -rf *.exe \ No newline at end of file + rm -rf *.exe diff --git a/header/examples/c++/example.cpp b/header/examples/c++/example.cpp index 5336997..a48c456 100644 --- a/header/examples/c++/example.cpp +++ b/header/examples/c++/example.cpp @@ -1,6 +1,18 @@ #include #include "../../libShaderFile.hpp" +#if _WIN64 +static const char* LibName = "..\..\..\libShaderFile-x86_64-win64.dll"; +#elif _WIN32 +static const char* LibName = "..\..\..\libShaderFile-i386-win32.dll"; +#elif __linux__ && (__amd64 || __x86_64 || _M_AMD64 || __ppc64__) +static const char* LibName = "../../../libShaderFile-x86_64-linux.so"; +#elif __linux__ && (__i386 || _X86_) +static const char* LibName = "../../../libShaderFile-i386-linux.so"; +#else +# error 'unknown operation system' +#endif + int main(int argc, char **argv) { try @@ -12,7 +24,7 @@ int main(int argc, char **argv) } // initialize library - lsf::Library lib("../../../libShaderFile-i386-win32.dll"); + lsf::Library lib(LibName); // create and load shader file lsf::ShaderFile shaderFile(lib); @@ -39,4 +51,4 @@ int main(int argc, char **argv) std::cout << "error: " << ex.what() << std::endl; return 2; } -} \ No newline at end of file +} diff --git a/header/examples/c/Makefile b/header/examples/c/Makefile index 03dfb6a..2e75d32 100644 --- a/header/examples/c/Makefile +++ b/header/examples/c/Makefile @@ -1,5 +1,11 @@ +ifeq ($(OS),Windows_NT) + LDFLAGS= +else + LDFLAGS=-ldl +endif + all: example.c ../../libShaderFile.h - gcc -o example example.c + gcc -g -o example example.c $(LDFLAGS) clean: - rm -rf *.exe \ No newline at end of file + rm -rf *.exe diff --git a/header/examples/c/example.c b/header/examples/c/example.c index 4a2d034..16ea244 100644 --- a/header/examples/c/example.c +++ b/header/examples/c/example.c @@ -1,6 +1,18 @@ #include #include "../../libShaderFile.h" +#if _WIN64 +# define LIB_NAME "..\..\..\libShaderFile-x86_64-win64.dll" +#elif _WIN32 +# define LIB_NAME "..\..\..\libShaderFile-i386-win32.dll" +#elif __linux__ && (__amd64 || __x86_64 || _M_AMD64 || __ppc64__) +# define LIB_NAME "../../../libShaderFile-x86_64-linux.so" +#elif __linux__ && (__i386 || _X86_) +# define LIB_NAME "../../../libShaderFile-i386-linux.so" +#else +# error 'unknown operation system' +#endif + int main(int argc, char **argv) { lsf_shader_file_handle_t sfHandle = 0; @@ -15,7 +27,7 @@ int main(int argc, char **argv) return 1; } - lsf_init("../../../libShaderFile-i386-win32.dll"); + lsf_init(LIB_NAME); // create shader file sfHandle = lsf_shader_file_create(); @@ -84,4 +96,4 @@ cleanup_lib: printf("warning: could finish library: %d\n", err); return ret; -} \ No newline at end of file +} diff --git a/header/examples/fpc/example.lpr b/header/examples/fpc/example.lpr index 412d37f..6df54e4 100644 --- a/header/examples/fpc/example.lpr +++ b/header/examples/fpc/example.lpr @@ -4,6 +4,19 @@ uses sysutils, variants, ulibShaderFile; +const + {$IF DEFINED(WIN32)} + LibName = '..\..\..\libShaderFile-i386-win32.dll'; + {$ELSEIF DEFINED(WIN64)} + LibName = '..\..\..\libShaderFile-x86_64-win64.dll'; + {$ELSEIF DEFINED(LINUX) AND DEFINED(CPU32)} + LibName = '../../../libShaderFile-i386-linux.so'; + {$ELSEIF DEFINED(LINUX) AND DEFINED(CPU64)} + LibName = '../../../libShaderFile-x86_64-linux.so'; + {$ELSE} + {$ERROR 'unknown operation system'} + {$IFEND} + var i: Integer; s: String; @@ -11,7 +24,7 @@ var Generator: TlsfGenerator; begin - lsf_init(ExtractFilePath(ParamStr(0)) + '..\..\..\libShaderFile-x86_64-win64.dll'); + lsf_init(ExtractFilePath(ParamStr(0)) + LibName); try if (ParamCount < 2) then begin WriteLn('error: expected input file and generator/class name as parameter'); diff --git a/header/libShaderFile.h b/header/libShaderFile.h index 3b975be..865850e 100644 --- a/header/libShaderFile.h +++ b/header/libShaderFile.h @@ -36,7 +36,11 @@ #define LSF_ERR_INVALID_METHOD_NAME 0x00002002 #define LSF_ERR_UNKNOWN 0xFFFFFFFF -#define WINAPI __stdcall +#if __MINGW32__ +# define WINAPI __stdcall +#else +# define WINAPI +#endif typedef uint32_t lsf_error_code_t; typedef uint32_t lsf_log_level_t; @@ -158,7 +162,7 @@ int lsf_finish(void); # include #endif -#if defined(WIN32) || defined(WIN64) +#if WIN32 || WIN64 || _WIN32 || _WIN64 # include typedef HMODULE lib_handle_t; @@ -172,9 +176,20 @@ void* get_addr(lib_handle_t handle, const char* name) int close_lib(lib_handle_t handle) { return FreeLibrary(handle); }; -#elif defined(LINUX) +#elif LINUX || __linux__ # include -# error "linux is not supported yet" + +typedef void* lib_handle_t; + +lib_handle_t open_lib(const char* name) + { return dlopen(name, RTLD_LAZY); }; + +void* get_addr(lib_handle_t handle, const char* name) + { return dlsym(handle, name); }; + +int close_lib(lib_handle_t handle) + { return !dlclose(handle); }; + #else # error "unknown operation system" #endif @@ -244,4 +259,4 @@ int lsf_finish(void) return err; } -#endif /* LIB_SHADER_FILE_H */ \ No newline at end of file +#endif /* LIB_SHADER_FILE_H */ diff --git a/header/libShaderFile.hpp b/header/libShaderFile.hpp index 817a5fa..6d3bf06 100644 --- a/header/libShaderFile.hpp +++ b/header/libShaderFile.hpp @@ -6,14 +6,20 @@ #include #include -#if defined(WIN32) || defined(WIN64) +#if WIN32 || WIN64 || _WIN32 || _WIN64 # include -#elif defined(LINUX) +#elif LINUX || __linux__ # include #else # error "unknown operation system" #endif +#if __MINGW32__ +# define WINAPI __stdcall +#else +# define WINAPI +#endif + /**********************************************************************************************************************************/ /* public interface */ /**********************************************************************************************************************************/ @@ -62,10 +68,10 @@ namespace lsf friend class Generator; public: - #if defined(WIN32) || defined(WIN64) + #if WIN32 || WIN64 || _WIN32 || _WIN64 typedef HMODULE Handle; //!< shader file handle - #elif defined(LINUX) - #error "linux is not supported yet" + #elif LINUX || __linux__ + typedef void* Handle; //!< shader file handle #else #error "unknown operation system" #endif @@ -149,7 +155,7 @@ namespace lsf * @param logLevel log level of the log message * @param msg log message * @param userargs user defined arguments */ - static void __stdcall logCallback(const LogLevel loglevel, const char* msg, void* userargs); + static void WINAPI logCallback(const LogLevel loglevel, const char* msg, void* userargs); }; /** class to manage a shader code generator */ @@ -253,23 +259,28 @@ namespace lsf /**********************************************************************************************************************************/ /* private implementation */ /**********************************************************************************************************************************/ -#if defined(WIN32) || defined(WIN64) +#if WIN32 || WIN64 || _WIN32 || _WIN64 lsf::Library::Handle libOpen(const char* name) { return LoadLibrary(name); }; template T getAddr(lsf::Library::Handle handle, const char* name) -{ - FARPROC tmp = GetProcAddress(handle, name); - return reinterpret_cast(tmp); -}; + { return reinterpret_cast(GetProcAddress(handle, name)); }; int libClose(lsf::Library::Handle handle) { return FreeLibrary(handle); }; -#elif defined(LINUX) -# include -# error "linux is not supported yet" +#elif LINUX || __linux__ +lsf::Library::Handle libOpen(const char* name) + { return dlopen(name, RTLD_LAZY); }; + +template +T getAddr(lsf::Library::Handle handle, const char* name) + { return reinterpret_cast(dlsym(handle, name)); }; + +int libClose(lsf::Library::Handle handle) + { return !dlclose(handle); }; + #else # error "unknown operation system" #endif @@ -277,28 +288,28 @@ int libClose(lsf::Library::Handle handle) /* Library::Impl ******************************************************************************************************************/ struct lsf::Library::Impl { - typedef ShaderFile::Handle (__stdcall *lsf_shader_file_create_t) (); - typedef void (__stdcall *lsf_shader_file_log_callback_t) (const LogLevel loglevel, const char* msg, void* userargs); - typedef ErrorCode (__stdcall *lsf_shader_file_set_log_callback_t) (const ShaderFile::Handle handle, const lsf_shader_file_log_callback_t callback, void* userargs); - typedef ErrorCode (__stdcall *lsf_shader_file_load_from_file_t) (const ShaderFile::Handle handle, const char* filename); - typedef ErrorCode (__stdcall *lsf_shader_file_save_to_file_t) (const ShaderFile::Handle handle, const char* filename); - typedef const char* (__stdcall *lsf_shader_file_get_generator_names_t) (const ShaderFile::Handle handle); - typedef ErrorCode (__stdcall *lsf_shader_file_destroy_t) (const ShaderFile::Handle handle); + typedef ShaderFile::Handle (WINAPI *lsf_shader_file_create_t) (); + typedef void (WINAPI *lsf_shader_file_log_callback_t) (const LogLevel loglevel, const char* msg, void* userargs); + typedef ErrorCode (WINAPI *lsf_shader_file_set_log_callback_t) (const ShaderFile::Handle handle, const lsf_shader_file_log_callback_t callback, void* userargs); + typedef ErrorCode (WINAPI *lsf_shader_file_load_from_file_t) (const ShaderFile::Handle handle, const char* filename); + typedef ErrorCode (WINAPI *lsf_shader_file_save_to_file_t) (const ShaderFile::Handle handle, const char* filename); + typedef const char* (WINAPI *lsf_shader_file_get_generator_names_t) (const ShaderFile::Handle handle); + typedef ErrorCode (WINAPI *lsf_shader_file_destroy_t) (const ShaderFile::Handle handle); - typedef Generator::Handle (__stdcall *lsf_generator_create_t) (const ShaderFile::Handle handle, const char* name); - typedef const char* (__stdcall *lsf_generator_get_property_names_t) (const Generator::Handle handle); - typedef const char* (__stdcall *lsf_generator_get_property_t) (const Generator::Handle handle, const int index); - typedef const char* (__stdcall *lsf_generator_get_property_by_name_t) (const Generator::Handle handle, const char* name); - typedef ErrorCode (__stdcall *lsf_generator_set_property_t) (const Generator::Handle handle, const int index, const char* value); - typedef ErrorCode (__stdcall *lsf_generator_set_property_by_name_t) (const Generator::Handle handle, const char* name, const char* value); - typedef const char* (__stdcall *lsf_generator_generate_code_t) (const Generator::Handle handle); - typedef ErrorCode (__stdcall *lsf_generator_destroy_t) (const Generator::Handle handle); - - typedef ErrorCode (__stdcall *lsf_init_t) (); - typedef ErrorCode (__stdcall *lsf_get_last_error_code_t) (); - typedef const char* (__stdcall *lsf_get_last_error_msg_t) (); - typedef const char* (__stdcall *lsf_get_last_error_trace_t) (); - typedef ErrorCode (__stdcall *lsf_finish_t) (); + typedef Generator::Handle (WINAPI *lsf_generator_create_t) (const ShaderFile::Handle handle, const char* name); + typedef const char* (WINAPI *lsf_generator_get_property_names_t) (const Generator::Handle handle); + typedef const char* (WINAPI *lsf_generator_get_property_t) (const Generator::Handle handle, const int index); + typedef const char* (WINAPI *lsf_generator_get_property_by_name_t) (const Generator::Handle handle, const char* name); + typedef ErrorCode (WINAPI *lsf_generator_set_property_t) (const Generator::Handle handle, const int index, const char* value); + typedef ErrorCode (WINAPI *lsf_generator_set_property_by_name_t) (const Generator::Handle handle, const char* name, const char* value); + typedef const char* (WINAPI *lsf_generator_generate_code_t) (const Generator::Handle handle); + typedef ErrorCode (WINAPI *lsf_generator_destroy_t) (const Generator::Handle handle); + + typedef ErrorCode (WINAPI *lsf_init_t) (); + typedef ErrorCode (WINAPI *lsf_get_last_error_code_t) (); + typedef const char* (WINAPI *lsf_get_last_error_msg_t) (); + typedef const char* (WINAPI *lsf_get_last_error_trace_t) (); + typedef ErrorCode (WINAPI *lsf_finish_t) (); const Library& library; diff --git a/header/ulibShaderFile.pas b/header/ulibShaderFile.pas index c081e2d..da983ee 100644 --- a/header/ulibShaderFile.pas +++ b/header/ulibShaderFile.pas @@ -21,6 +21,7 @@ type {$Z4} TlsfErrorCode = ( + errUnknown = -1, errNone = $00000000, errNotInit = $00000001, errInvalidHandleShaderFile = $00000010, @@ -43,8 +44,7 @@ type errShaderPart = $0000100b, errInvalidLibraryName = $00002000, errInvalidLibraryHandle = $00002001, - errInvalidMethodName = $00002002, - errUnknown = -1 + errInvalidMethodName = $00002002 ); TlsfShaderFileHandle = Pointer; @@ -152,19 +152,22 @@ procedure lsf_finish; implementation -uses {$IF DEFINED(WIN32) OR DEFINED(WIN64)} +uses windows; type TLibHandle = HMODULE; -function LibOpen(const aLibName: String; out aErrorCode: Cardinal): TLibHandle; +const + InvalidLibHandle: TLibHandle = 0; + +function LibOpen(const aLibName: String; out aError: String): TLibHandle; begin result := LoadLibraryA(PAnsiChar(AnsiString(aLibName))); if (result = 0) - then aErrorCode := GetLastError() - else aErrorCode := 0; + then aError := GetLastError() + else aError := ''; end; function GetAddr(const aLibHandle: TLibHandle; const aName: String): Pointer; @@ -172,10 +175,40 @@ begin result := GetProcAddress(aLibHandle, PAnsiChar(AnsiString(aName))); end; -function LibClose(const aLibHandle: TLibHandle): Boolean; +procedure LibClose(const aLibHandle: TLibHandle); begin - result := FreeLibrary(aLibHandle); + FreeLibrary(aLibHandle); end; + +{$ELSEIF DEFINED(LINUX)} +uses + dl; + +type + TLibHandle = Pointer; + +const + InvalidLibHandle: TLibHandle = nil; + +function LibOpen(const aLibName: String; out aError: String): TLibHandle; +begin + dlerror(); + result := dlopen(PChar(aLibName), RTLD_LAZY); + if (result = InvalidLibHandle) + then aError := dlerror() + else aError := ''; +end; + +function GetAddr(const aLibHandle: TLibHandle; const aName: String): Pointer; +begin + result := dlsym(aLibHandle, PChar(aName)); +end; + +procedure LibClose(const aLibHandle: TLibHandle); +begin + dlclose(aLibHandle); +end; + {$ELSE} {$ERROR 'unknown operation system'} {$IFEND} @@ -196,12 +229,12 @@ procedure lsf_init(const aLibName: String); end; var - e: Cardinal; + eMsg: String; err: TlsfErrorCode; begin - libHandle := LibOpen(aLibName, e); - if (libHandle = 0) then - raise Exception.CreateFmt('unable to load library (%d): %s', [e, aLibName]); + libHandle := LibOpen(aLibName, eMsg); + if (libHandle = InvalidLibHandle) then + raise Exception.Create('unable to load library: ' + eMsg); lsf_ShaderFile_create := Tlsf_ShaderFile_create( LoadProc('lsf_ShaderFile_create')); lsf_ShaderFile_setLogCallback := Tlsf_ShaderFile_setLogCallback( LoadProc('lsf_ShaderFile_setLogCallback')); @@ -243,9 +276,9 @@ begin lsf_getLastErrorCode := nil; lsf_getLastErrorMsg := nil; lsf_getLastErrorTrace := nil; - if (libHandle <> 0) then begin + if (libHandle <> InvalidLibHandle) then begin LibClose(libHandle); - libHandle := 0; + libHandle := InvalidLibHandle; end; end; diff --git a/libShaderFile.lpi b/libShaderFile.lpi index 2c5fb7a..05a1f6e 100644 --- a/libShaderFile.lpi +++ b/libShaderFile.lpi @@ -20,7 +20,7 @@ - + @@ -141,6 +141,170 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +