diff --git a/.gitignore b/.gitignore index 91c614e..ce59207 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,8 @@ compile_commands.json # Added by cargo /target + +# fpc stuff +.fpc +fpc/.fpc +fpc/fpc diff --git a/engine/engine.cpp b/engine/engine.cpp index 1e60137..e8879cc 100644 --- a/engine/engine.cpp +++ b/engine/engine.cpp @@ -52,18 +52,6 @@ void IEngine_Signal(int sig) void IEngine::Init() { /* trap signals */ - #ifdef __linux - signal(SIGHUP, IEngine_Signal); - signal(SIGINT, IEngine_Signal); - signal(SIGQUIT, IEngine_Signal); - signal(SIGILL, IEngine_Signal); - signal(SIGTRAP, IEngine_Signal); - signal(SIGIOT, IEngine_Signal); - signal(SIGBUS, IEngine_Signal); - signal(SIGFPE, IEngine_Signal); - signal(SIGSEGV, IEngine_Signal); - signal(SIGTERM, IEngine_Signal); - #endif IFileSystem::InitFilesystem(); IVideo::Init(); diff --git a/fpc/.fpc/cc/1762019572/build.cpp.o b/fpc/.fpc/cc/1762019572/build.cpp.o deleted file mode 100644 index e6f37d3..0000000 Binary files a/fpc/.fpc/cc/1762019572/build.cpp.o and /dev/null differ diff --git a/fpc/.fpc/ld/855499559/libbuild.so b/fpc/.fpc/ld/855499559/libbuild.so deleted file mode 100644 index 0049cbc..0000000 Binary files a/fpc/.fpc/ld/855499559/libbuild.so and /dev/null differ diff --git a/fpc/Makefile b/fpc/Makefile index c3a87a6..53fa97c 100644 --- a/fpc/Makefile +++ b/fpc/Makefile @@ -2,8 +2,12 @@ TIER0_FILES := $(wildcard ../tier0/*.cpp) TIER1_FILES := $(wildcard ../tier1/*.cpp) FPC_FILES := main.cpp library/runner.cpp library/helper.cpp library/c.cpp library/ld.cpp CC = clang -OUTPUT_DIR = ../build/tools/fpc +OUTPUT_DIR = fpc -full: $(TIER0_FILES) $(TIER1_FILES) $(FPC_FILES) +install: + +../build/tools: mkdir -p ../build/tools + +minimal: $(TIER0_FILES) $(TIER1_FILES) $(FPC_FILES) ../build/tools $(CC) -g -rdynamic $(TIER0_FILES) $(TIER1_FILES) $(FPC_FILES) -I../public -Ipublic -lc -lstdc++ -o $(OUTPUT_DIR) diff --git a/fpc/build.cpp b/fpc/build.cpp index 5e8c243..625beec 100644 --- a/fpc/build.cpp +++ b/fpc/build.cpp @@ -1,8 +1,46 @@ -#include "helper.h" +#include "public/c.h" +#include "public/ld.h" +#include "public/c.h" +#include "public/helper.h" +#include "tier0/platform.h" +#include "signal.h" + +CUtlVector g_CompiledFiles = { + "main.cpp", + "library/runner.cpp", + "library/helper.cpp", + "library/c.cpp", + "library/ld.cpp", + "../tier0/lib.cpp", + "../tier0/mem.cpp", + "../tier0/platform.cpp", + "../tier1/utlbuffer.cpp", + "../tier1/utlstring.cpp", + "../tier1/utlvector.cpp", + "../tier1/utlmap.cpp", + "../tier1/commandline.cpp", +}; + +CUtlVector g_IncludeDirectories = { + "public", + "../public", +}; + int build_fpc() { - V_printf("cool\n"); + CCProject compileProject = {}; + CLDProject ldProject = {}; + + compileProject.m_szName = "fpc"; + compileProject.files = g_CompiledFiles; + compileProject.includeDirectories = g_IncludeDirectories; + ldProject = compileProject.Compile(); + + ldProject.Link(); + + IFileSystem2::MakeDirectory("../build/tools"); + return 0; }; DECLARE_BUILD_STAGE(fpc, build_fpc); diff --git a/fpc/library/c.cpp b/fpc/library/c.cpp index 87183c2..4812e3d 100644 --- a/fpc/library/c.cpp +++ b/fpc/library/c.cpp @@ -19,12 +19,13 @@ CLDProject CCProject::Compile() unsigned int hash = GenerateProjectHash(); for (auto &file: files) { - CUtlString szOutputFile = CUtlString("%s/cc/%u/%s.o",FPC_TEMPORAL_DIRNAME, hash, file.GetString()); + CUtlString szOutputFile = CUtlString("%s/cc/%u_%s/%s/%s.o",FPC_TEMPORAL_DIRNAME, hash, m_szName.GetString(), IFileSystem2::OwnDirectory(), file.GetString()); CUtlString szOutputDir = szOutputFile; szOutputDir = dirname(szOutputDir); IFileSystem2::MakeDirectory(szOutputDir); CUtlVector args = { + "-g", "-c", "-o", szOutputFile, @@ -52,10 +53,10 @@ CLDProject CCProject::Compile() IRunner::Run("clang", args); proj.objects.AppendTail((CObject){szOutputFile}); - ClangFile_t file = {}; - file.m_szName = m_szName; - file.m_szArguments = args; - g_clangFiles.AppendTail(file); + ClangFile_t cfile = {}; + cfile.m_szName = m_szName; + cfile.m_szArguments = args; + g_clangFiles.AppendTail(cfile); } return proj; } diff --git a/fpc/library/helper.cpp b/fpc/library/helper.cpp index f06c2b8..41e0eac 100644 --- a/fpc/library/helper.cpp +++ b/fpc/library/helper.cpp @@ -3,6 +3,8 @@ #include "tier0/platform.h" #include "tier1/utlvector.h" #include "tier1/utlstring.h" +#include "unistd.h" +#include "libgen.h" unsigned int g_hashState = 102851263; unsigned int CProject::GenerateProjectHash( void ) @@ -19,11 +21,19 @@ unsigned int CProject::GenerateProjectHash( void ) return hash; }; +static char path[1024]; +static ssize_t pathSize = readlink("/proc/self/exe", path, sizeof(path) - 1); +static char* pathDir = dirname(path); +char *IFileSystem2::OwnDirectory() +{ + return pathDir; +}; + void IFileSystem2::MakeDirectory( const char *psz ) { CUtlVector args = { "-p", - psz, + CUtlString(psz), }; IRunner::Run("mkdir", args); }; diff --git a/fpc/library/ld.cpp b/fpc/library/ld.cpp index cad8e50..e11b107 100644 --- a/fpc/library/ld.cpp +++ b/fpc/library/ld.cpp @@ -17,7 +17,7 @@ CUtlString CLDProject::Link( void ) szFileName = CUtlString("lib%s.so", m_szName.GetString()); break; } - CUtlString szOutputFile = CUtlString("%s/ld/%u/%s",FPC_TEMPORAL_DIRNAME, hash, szFileName.GetString()); + CUtlString szOutputFile = CUtlString("%s/ld/%u_%s/%s",FPC_TEMPORAL_DIRNAME, hash, m_szName.GetString(), szFileName.GetString()); CUtlString szOutputDir = szOutputFile; szOutputDir = dirname(szOutputDir); IFileSystem2::MakeDirectory(szOutputDir); @@ -29,6 +29,7 @@ CUtlString CLDProject::Link( void ) IRunner::Run("ar", args); } else { CUtlVector args = { + "-rdynamic", "-o", szOutputFile, }; diff --git a/fpc/main.cpp b/fpc/main.cpp index 1a0775f..0a50f1f 100644 --- a/fpc/main.cpp +++ b/fpc/main.cpp @@ -4,6 +4,7 @@ #include "tier1/commandline.h" #include "c.h" #include "tier1/utlvector.h" +#include "signal.h" int build() { @@ -24,10 +25,37 @@ int build() return 0; }; - +void IEngine_Signal(int sig) +{ + switch (sig) + { + case SIGSEGV: + case SIGILL: + case SIGABRT: + Plat_Backtrace(); + Plat_FatalErrorFunc("Fault"); + break; + default: + break; + }; + _exit(0); +}; int main(int c, char **v) { + #ifdef __linux + signal(SIGHUP, IEngine_Signal); + signal(SIGINT, IEngine_Signal); + signal(SIGQUIT, IEngine_Signal); + signal(SIGILL, IEngine_Signal); + signal(SIGTRAP, IEngine_Signal); + signal(SIGIOT, IEngine_Signal); + signal(SIGBUS, IEngine_Signal); + signal(SIGFPE, IEngine_Signal); + signal(SIGSEGV, IEngine_Signal); + signal(SIGTERM, IEngine_Signal); + V_printf("cool\n"); + #endif ICommandLine::CreateCommandLine(c, v); if (ICommandLine::CheckParam("build")) return build(); diff --git a/fpc/public/helper.h b/fpc/public/helper.h index e3d21be..c6084c8 100644 --- a/fpc/public/helper.h +++ b/fpc/public/helper.h @@ -15,13 +15,12 @@ public: interface IFileSystem2 { public: + static char *OwnDirectory(); static void MakeDirectory( const char *psz ); static void CopyFile( const char *szDestination, const char *szOrigin ); static void CopyDirectory( const char *szDestination, const char *szOrigin ); }; - - class CBuildStage { public: diff --git a/public/tier1/utlbuffer.h b/public/tier1/utlbuffer.h index 7746a6a..edc82b7 100644 --- a/public/tier1/utlbuffer.h +++ b/public/tier1/utlbuffer.h @@ -5,6 +5,7 @@ #include "tier0/mem.h" #include "tier0/platform.h" #include "tier0/lib.h" +#include "new" template class CUtlBuffer; @@ -31,8 +32,8 @@ public: T operator []( const size_t nIndex ) const; CUtlBuffer& operator=(const CUtlBuffer& other); private: - T* m_pData; - size_t m_nSize; + T* m_pData = NULL; + size_t m_nSize = 0; }; //----------------------------------------------------------------------------- @@ -156,6 +157,7 @@ public: CUtlResizableBuffer( size_t nSize ); CUtlResizableBuffer( const CUtlBuffer& buffer ); CUtlResizableBuffer( const CUtlResizableBuffer& buffer ); + ~CUtlResizableBuffer(); size_t GetSize() const; size_t GetRealSize() const; @@ -168,9 +170,9 @@ public: CUtlResizableBuffer& operator=(const CUtlResizableBuffer& other); private: size_t CalculateMemorySize(size_t nSize); - T* m_pData; - size_t m_nSize; - size_t m_nAllocatedSize; + T* m_pData = NULL; + size_t m_nSize = 0; + size_t m_nAllocatedSize = 0; }; @@ -179,24 +181,20 @@ private: // Constructor. //----------------------------------------------------------------------------- template -CUtlResizableBuffer::CUtlResizableBuffer() : m_nSize(1) +CUtlResizableBuffer::CUtlResizableBuffer() { - m_pData = (T*)V_malloc(1); - m_nAllocatedSize = 1; + m_pData = NULL; + m_nSize = 0; + m_nAllocatedSize = 0; } //----------------------------------------------------------------------------- // Constructor. //----------------------------------------------------------------------------- template -CUtlResizableBuffer::CUtlResizableBuffer( size_t nSize ) : m_nSize(nSize) +CUtlResizableBuffer::CUtlResizableBuffer( size_t nSize ) { - if ( nSize == 0 ) - nSize = 1; - size_t nAllocSize = CalculateMemorySize(sizeof(T)*nSize); - m_pData = (T*)V_malloc(nAllocSize); - m_nAllocatedSize = nAllocSize; - V_memset(m_pData, 0, sizeof(T)*nSize); + Resize(nSize); } //----------------------------------------------------------------------------- @@ -205,10 +203,6 @@ CUtlResizableBuffer::CUtlResizableBuffer( size_t nSize ) : m_nSize(nSize) template CUtlResizableBuffer::CUtlResizableBuffer( const CUtlBuffer& buffer ) : m_nSize(buffer.nSize) { - size_t nAllocSize = CalculateMemorySize(sizeof(T)*buffer.nSize); - m_pData = (T*)V_malloc(nAllocSize); - m_nAllocatedSize = nAllocSize; - V_memcpy(m_pData,buffer.m_pData,sizeof(T)*buffer.nSize); } //----------------------------------------------------------------------------- @@ -217,11 +211,21 @@ CUtlResizableBuffer::CUtlResizableBuffer( const CUtlBuffer& buffer ) : m_n template CUtlResizableBuffer::CUtlResizableBuffer( const CUtlResizableBuffer& buffer ) : m_nSize(buffer.m_nSize) { - m_pData = (T*)V_malloc(sizeof(T)*buffer.m_nAllocatedSize); - m_nAllocatedSize = buffer.m_nAllocatedSize; - V_memcpy(m_pData,buffer.m_pData,sizeof(T)*buffer.m_nSize); + *this = buffer; } +template +CUtlResizableBuffer::~CUtlResizableBuffer() +{ + for ( size_t i = 0; i < m_nSize; i++) + m_pData[i].~T(); + if (m_pData) + V_free(m_pData); + m_pData = 0; + m_nSize = 0; + m_nAllocatedSize = 0; +}; + //----------------------------------------------------------------------------- // Gets memory size. //----------------------------------------------------------------------------- @@ -246,25 +250,21 @@ size_t CUtlResizableBuffer::GetRealSize( void ) const template void CUtlResizableBuffer::Resize( size_t nSize ) { - if ( nSize == 0 ) - nSize = 1; - - - if ( m_pData == 0 ) - m_pData = (T*)V_malloc(CalculateMemorySize(sizeof(T)*nSize)); - else + if (nSize > m_nAllocatedSize) { - size_t nAllocSize = CalculateMemorySize(sizeof(T)*nSize); - if (nAllocSize!=m_nAllocatedSize) + size_t nAllocationSize = CalculateMemorySize(nSize); + T *pData = (T*)V_malloc(nAllocationSize*sizeof(T)); + for (size_t i = 0; i < m_nSize; i++) { - // not ideal - T* pData = (T*)V_malloc(nAllocSize); - V_memcpy(pData, m_pData, m_nAllocatedSize>nAllocSize ? nAllocSize : m_nAllocatedSize ); - m_nAllocatedSize = nAllocSize; - V_free(m_pData); - m_pData = pData; + new (&pData[i]) T(m_pData[i]); + m_pData[i].~T(); } + V_free(m_pData); + m_pData = pData; + m_nAllocatedSize = nAllocationSize; } + for ( size_t i = m_nSize; i < nSize; ++i ) + new (&m_pData[i]) T(); m_nSize = nSize; } @@ -320,11 +320,17 @@ CUtlResizableBuffer& CUtlResizableBuffer::operator=(const CUtlResizableBuf { if ( this != &other ) { - V_free(m_pData); - m_pData = (T*)V_malloc(other.m_nAllocatedSize); - V_memcpy(m_pData, other.m_pData, sizeof(T)*other.m_nSize); + if (m_pData) + { + for (size_t i = 0; i < m_nSize; i++) + m_pData[i].~T(); + V_free(m_pData); + } + m_pData = (T*)V_malloc(other.m_nAllocatedSize*sizeof(T)); m_nAllocatedSize = other.m_nAllocatedSize; m_nSize = other.m_nSize; + for ( size_t i = 0; i < other.m_nSize; i++) + new (&m_pData[i]) T(other.m_pData[i]); } return *this; } @@ -343,7 +349,9 @@ size_t CUtlResizableBuffer::CalculateMemorySize(size_t nSize) x |= x >> 4; x |= x >> 8; x |= x >> 16; +#if SIZE_MAX > UINT32_MAX x |= x >> 32; +#endif return x + 1; } diff --git a/public/tier1/utlstring.h b/public/tier1/utlstring.h index 9ad3098..2aad6a0 100644 --- a/public/tier1/utlstring.h +++ b/public/tier1/utlstring.h @@ -26,7 +26,7 @@ public: bool operator==(CUtlString& string); bool operator!=(CUtlString& string); private: - CUtlVector m_data; + CUtlVector m_data = 0; }; #endif diff --git a/public/tier1/utlvector.h b/public/tier1/utlvector.h index c1086e5..90d7def 100644 --- a/public/tier1/utlvector.h +++ b/public/tier1/utlvector.h @@ -75,7 +75,7 @@ private: template CUtlVector::CUtlVector( void ) { - m_data.Resize(0); + }; //---------------------------------------------------------------------------- @@ -85,6 +85,7 @@ template CUtlVector::CUtlVector( size_t nSize ) { m_data.Resize(nSize); + V_memset(m_data.GetMemory(),0,m_data.GetSize()*sizeof(T)); m_nSize = nSize; }; @@ -95,20 +96,20 @@ template CUtlVector::CUtlVector( CUtlInitializerList initalizerList ) { m_data.Resize(initalizerList.size()); + V_memset(m_data.GetMemory(),0,m_data.GetSize()*sizeof(T)); m_nSize = m_data.GetSize(); - V_memcpy(m_data.GetMemory(), initalizerList.begin(), m_data.GetSize()*sizeof(T)); + for (size_t i = 0; i CUtlVector::CUtlVector( const CUtlVector& vector ) { - m_data = vector.m_data; + *this = vector; } template CUtlVector::~CUtlVector() { - for ( uint32_t i = 0; i < m_nSize; i++ ) - m_data[i].~T(); } template @@ -133,7 +134,8 @@ template void CUtlVector::AppendTail( const T *pData, size_t n ) { m_data.Resize(m_data.GetSize()+n); - V_memcpy(m_data+m_nSize,pData,sizeof(T)*n); + for ( size_t i = 0; i < n; i++ ) + m_data[i+m_nSize] = pData[i]; m_nSize+=n; } @@ -180,6 +182,7 @@ size_t CUtlVector::GetSize( void ) template void CUtlVector::Resize( size_t nSize ) { + m_data.Resize(nSize); m_nSize = nSize; } template @@ -193,9 +196,7 @@ CUtlVector &CUtlVector::operator=(const CUtlVector &vec) if (this != &vec) { m_nSize = vec.m_nSize; - m_data.Resize(m_nSize); - for ( uint32_t i = 0; i < m_nSize; i++ ) - m_data[i] = vec.m_data[i]; + m_data = vec.m_data; } return *this; } diff --git a/tier0/platform.cpp b/tier0/platform.cpp index c5fdfb5..6f32473 100644 --- a/tier0/platform.cpp +++ b/tier0/platform.cpp @@ -60,7 +60,35 @@ PLATFORM_INTERFACE void Plat_ListDirRecursive(const char* szPath, ListDirCallbac } PLATFORM_INTERFACE void Plat_ListDir(const char* szPath, ListDirCallbackFn file, ListDirCallbackFn dir) { + struct dirent *entry; + DIR *dp = opendir(szPath); + if (!dp) { + return; + } + + while ((entry = readdir(dp)) != NULL) { + char full_path[1024]; + if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) + continue; + + snprintf(full_path, sizeof(full_path), "%s/%s", szPath, entry->d_name); + + struct stat statbuf; + if (stat(full_path, &statbuf) == -1) { + continue; + } + + if (S_ISDIR(statbuf.st_mode)) { + if (dir) + dir(full_path); + } else { + if (file) + file(full_path); + } + } + + closedir(dp); } PLATFORM_INTERFACE char *Plat_GetExtension( const char *szPath ) diff --git a/tier1/utlstring.cpp b/tier1/utlstring.cpp index b605330..35677cc 100644 --- a/tier1/utlstring.cpp +++ b/tier1/utlstring.cpp @@ -23,7 +23,7 @@ CUtlString::CUtlString( const char *szFormat, ... ) m_data.Reserve(nSize + 1); va_end(vlArgs2); V_vsnprintf(m_data.GetData(), nSize + 1, szFormat, vlArgs); - m_data.Resize(nSize); + m_data.Resize(nSize+1); va_end(vlArgs); } @@ -34,6 +34,7 @@ CUtlString::CUtlString( const CUtlString &sz ) void CUtlString::AppendTail( const char *psz ) { + m_data.Resize(m_data.GetSize()-1); m_data.AppendTail(psz,V_strlen(psz)); m_data.Reserve(m_data.GetSize()+1); m_data[m_data.GetSize()] = 0; @@ -73,7 +74,9 @@ CUtlString::operator char*( void ) CUtlString &CUtlString::operator=(const CUtlString &sz) { if (this != &sz) + { m_data = sz.m_data; + } return *this; }