From 74f2f47cb63b02638270beecd20acea1a0f5665e Mon Sep 17 00:00:00 2001 From: Tim Foley Date: Wed, 27 Sep 2017 11:17:39 -0700 Subject: First attempt at a Linux build (#193) * First attempt at a Linux build - Fix up places where C++ idioms were written assuming lenient behavior of Microsoft's compiler - Add a few more alternatives for platform-specific behavior where Windows was the only platform accounted for. - Add a basic Makefile that can at least invoke our build, even if it isn't going good dependency tracking, etc. - Build `libslang.so` and `slangc` that depends on it, using a relative `RPATH` to make the binary portable (I hope) - Add an initial `.travis.yml` to see if we can trigger their build process. * Fixup: const bug in `List::Sort` I'm not clear why this gets picked up by the gcc *and* clang that Travis uses, but not the (newer) gcc I'm using on Ubuntu here, but I'm hoping it is just some missing `const` qualifiers. * Fixup: reorder specialization of "class info" Clang complains about things being specialized after being instantiated (implicilty), and I hope it is just the fact that I generate the class info for the roots of the hierarchy after the other cases. We'll see. * Fixup: add `platform.cpp` to unified/lumped build * Fixup: Windows uses `FreeLibrary` and not `UnloadLibrary` * Fixup: fix Windows project file to include new source file This obviously points to the fact that we are going to need to be generating these files sooner or later. --- source/core/array-view.h | 2 +- source/core/common.h | 16 +++++----- source/core/core.vcxproj | 1 + source/core/hash.h | 1 + source/core/list.h | 2 +- source/core/platform.cpp | 74 +++++++++++++++++++++++++++++++++++++++++++++ source/core/platform.h | 38 +++++++++++++++++++++++ source/core/secure-crt.h | 1 + source/core/slang-string.h | 61 ++++++++++++++++--------------------- source/core/smart-pointer.h | 2 +- source/core/stream.cpp | 2 +- source/core/stream.h | 2 +- 12 files changed, 155 insertions(+), 47 deletions(-) create mode 100644 source/core/platform.cpp create mode 100644 source/core/platform.h (limited to 'source/core') diff --git a/source/core/array-view.h b/source/core/array-view.h index 4e0057cdb..193aedbbb 100644 --- a/source/core/array-view.h +++ b/source/core/array-view.h @@ -1,7 +1,7 @@ #ifndef CORE_LIB_ARRAY_VIEW_H #define CORE_LIB_ARRAY_VIEW_H -#include "Exception.h" +#include "exception.h" namespace Slang { diff --git a/source/core/common.h b/source/core/common.h index f0f6902d1..dbb837821 100644 --- a/source/core/common.h +++ b/source/core/common.h @@ -13,13 +13,15 @@ namespace Slang { + typedef int32_t Int32; + typedef uint32_t UInt32; + typedef int64_t Int64; - typedef unsigned short Word; -#ifdef _M_X64 - typedef int64_t PtrInt; -#else - typedef int PtrInt; -#endif + typedef uint64_t UInt64; + +// typedef unsigned short Word; + + typedef intptr_t PtrInt; template inline T&& _Move(T & obj) @@ -38,7 +40,7 @@ namespace Slang #ifdef _MSC_VER #define SLANG_RETURN_NEVER __declspec(noreturn) #else -#efine SLANG_RETURN_NEVER /* empty */ +#define SLANG_RETURN_NEVER /* empty */ #endif SLANG_RETURN_NEVER void signalUnexpectedError(char const* message); diff --git a/source/core/core.vcxproj b/source/core/core.vcxproj index 88e776703..ba9fe3d98 100644 --- a/source/core/core.vcxproj +++ b/source/core/core.vcxproj @@ -39,6 +39,7 @@ + diff --git a/source/core/hash.h b/source/core/hash.h index dd2086305..8ee5b088f 100644 --- a/source/core/hash.h +++ b/source/core/hash.h @@ -3,6 +3,7 @@ #include "slang-math.h" #include +#include namespace Slang { diff --git a/source/core/list.h b/source/core/list.h index aeba9557f..af32a39ef 100644 --- a/source/core/list.h +++ b/source/core/list.h @@ -525,7 +525,7 @@ namespace Slang void Sort() { - Sort([](T& t1, T& t2){return t1 + #undef WIN32_LEAN_AND_MEAN + #undef NOMINMAX +#else + #include +#endif + +namespace Slang +{ + // SharedLibrary + + SharedLibrary SharedLibrary::load(char const* name) + { + SharedLibrary result; + result.handle = nullptr; + +#ifdef _WIN32 + { + HMODULE h = LoadLibraryA(name); + result.handle = (Handle) h; + } +#else + { + void* h = dlopen(name, RTLD_LOCAL); + result.handle = (Handle) h; + + } +#endif + + return result; + } + + void SharedLibrary::unload() + { +#ifdef _WIN32 + { + FreeLibrary( + (HMODULE) handle); + } +#else + { + dlclose(handle); + } +#endif + + } + + SharedLibrary::FuncPtr SharedLibrary::findFuncByName(char const* name) + { + FuncPtr funcPtr = nullptr; + +#ifdef _WIN32 + { + funcPtr = (FuncPtr) GetProcAddress( + (HMODULE) handle, + name); + } +#else + { + funcPtr = (FuncPtr) dlsym( + (void*) handle, + name); + } +#endif + + return funcPtr; + } +} \ No newline at end of file diff --git a/source/core/platform.h b/source/core/platform.h new file mode 100644 index 000000000..fef698e6f --- /dev/null +++ b/source/core/platform.h @@ -0,0 +1,38 @@ +// platform.h +#ifndef SLANG_CORE_PLATFORM_H_INCLUDED +#define SLANG_CORE_PLATFORM_H_INCLUDED + +namespace Slang +{ + // Interface for working with shared libraries + // in a platfomr-independent fashion. + struct SharedLibrary + { + typedef struct SharedLibraryImpl* Handle; + Handle handle; + + // Attempt to load a shared library for + // the current platform. + static SharedLibrary load(char const* name); + + // If this refers to a valid loaded library, + // then attempt to unload it + void unload(); + + typedef void (*FuncPtr)(void); + + FuncPtr findFuncByName(char const* name); + + + operator Handle() { return handle; } + }; + +#ifndef _MSC_VER + #define _fileno fileno + #define _isatty isatty + #define _setmode setmode + #define _O_BINARY O_BINARY +#endif +} + +#endif diff --git a/source/core/secure-crt.h b/source/core/secure-crt.h index 12927cc24..a76fb2679 100644 --- a/source/core/secure-crt.h +++ b/source/core/secure-crt.h @@ -2,6 +2,7 @@ #ifndef CORE_LIB_SECURE_CRT_H #define CORE_LIB_SECURE_CRT_H #include +#include #include #include #include diff --git a/source/core/slang-string.h b/source/core/slang-string.h index ed333f8e8..23ab54c23 100644 --- a/source/core/slang-string.h +++ b/source/core/slang-string.h @@ -222,7 +222,7 @@ namespace Slang char* getData() const { - return buffer ? buffer->getData() : ""; + return buffer ? buffer->getData() : (char*)""; } UInt getLength() const @@ -270,50 +270,29 @@ namespace Slang void append(String const& str); void append(StringSlice const& slice); - String(int val, int radix = 10) + String(int32_t val, int radix = 10) { append(val, radix); -#if 0 - buffer = StringRepresentation::createWithLength(33); - buffer->length = IntToAscii(getData(), val, radix); - ReverseInternalAscii(getData(), getLength()); -#endif } - String(unsigned int val, int radix = 10) + String(uint32_t val, int radix = 10) { append(val, radix); -#if 0 - buffer = StringRepresentation::createWithLength(33); - buffer->length = IntToAscii(getData(), val, radix); - ReverseInternalAscii(getData(), getLength()); -#endif } - String(long long val, int radix = 10) + String(int64_t val, int radix = 10) + { + append(val, radix); + } + String(uint64_t val, int radix = 10) { append(val, radix); -#if 0 - buffer = StringRepresentation::createWithLength(65); - buffer->length = IntToAscii(getData(), val, radix); - ReverseInternalAscii(getData(), getLength()); -#endif } String(float val, const char * format = "%g") { append(val, format); -#if 0 - buffer = StringRepresentation::createWithLength(128); - sprintf_s(getData(), 128, format, val); - buffer->length = (int)strnlen_s(begin(), 128); -#endif } String(double val, const char * format = "%g") { append(val, format); -#if 0 - buffer = StringRepresentation::createWithLength(128); - sprintf_s(getData(), 128, format, val); - buffer->length = (int)strnlen_s(begin(), 128); -#endif } String(const char * str) { @@ -656,17 +635,22 @@ namespace Slang Append(&ch, 1); return *this; } - StringBuilder & operator << (int val) + StringBuilder & operator << (Int32 val) + { + Append(val); + return *this; + } + StringBuilder & operator << (UInt32 val) { Append(val); return *this; } - StringBuilder & operator << (unsigned int val) + StringBuilder & operator << (Int64 val) { Append(val); return *this; } - StringBuilder & operator << (long long val) + StringBuilder & operator << (UInt64 val) { Append(val); return *this; @@ -714,21 +698,28 @@ namespace Slang int len = (int)strnlen_s(buf, 128); Append(buf, len); } - void Append(unsigned int value, int radix = 10) + void Append(Int32 value, int radix = 10) { char vBuffer[33]; int len = IntToAscii(vBuffer, value, radix); ReverseInternalAscii(vBuffer, len); Append(vBuffer); } - void Append(int value, int radix = 10) + void Append(UInt32 value, int radix = 10) { char vBuffer[33]; int len = IntToAscii(vBuffer, value, radix); ReverseInternalAscii(vBuffer, len); Append(vBuffer); } - void Append(long long value, int radix = 10) + void Append(Int64 value, int radix = 10) + { + char vBuffer[65]; + int len = IntToAscii(vBuffer, value, radix); + ReverseInternalAscii(vBuffer, len); + Append(vBuffer); + } + void Append(UInt64 value, int radix = 10) { char vBuffer[65]; int len = IntToAscii(vBuffer, value, radix); diff --git a/source/core/smart-pointer.h b/source/core/smart-pointer.h index 3e64eac96..17d6caaa4 100644 --- a/source/core/smart-pointer.h +++ b/source/core/smart-pointer.h @@ -127,7 +127,7 @@ namespace Slang template typename EnableIf::value, void>::type - operator=(RefPtr const& ptr) + operator=(RefPtr const& p) { T* old = pointer; addReference(p.pointer); diff --git a/source/core/stream.cpp b/source/core/stream.cpp index 62e9092af..f78e5af83 100644 --- a/source/core/stream.cpp +++ b/source/core/stream.cpp @@ -1,4 +1,4 @@ -#include "Stream.h" +#include "stream.h" #ifdef _WIN32 #include #endif diff --git a/source/core/stream.h b/source/core/stream.h index 9a8ea8366..4eea6a909 100644 --- a/source/core/stream.h +++ b/source/core/stream.h @@ -1,7 +1,7 @@ #ifndef CORE_LIB_STREAM_H #define CORE_LIB_STREAM_H -#include "Basic.h" +#include "basic.h" namespace Slang { -- cgit v1.2.3