From 198ed337e32faf9ea5dbf70beea53c72821c1bb3 Mon Sep 17 00:00:00 2001 From: John McCardle Date: Sun, 25 Feb 2024 15:38:38 -0500 Subject: [PATCH] Windows build --- CMakeLists.txt | 7 +++--- deps/platform/linux/platform.h | 40 ++++++++++++++++++++++++++++++++ deps/platform/windows/platform.h | 39 +++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+), 4 deletions(-) create mode 100644 deps/platform/linux/platform.h create mode 100644 deps/platform/windows/platform.h diff --git a/CMakeLists.txt b/CMakeLists.txt index b8abf29..5c59e93 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,10 +17,6 @@ include_directories(${CMAKE_SOURCE_DIR}/deps/libtcod) include_directories(${CMAKE_SOURCE_DIR}/deps/cpython) include_directories(${CMAKE_SOURCE_DIR}/deps/Python) -# TODO: Move this into the WIN32 if block below (as 'else') -#include_directories(${CMAKE_SOURCE_DIR}/platform/linux) -include_directories(${CMAKE_SOURCE_DIR}/deps/platform/linux) - # Collect all the source files file(GLOB_RECURSE SOURCES "src/*.cpp") @@ -43,6 +39,9 @@ if(WIN32) # include_directories(path_to_additional_includes) # link_directories(path_to_additional_libs) # list(APPEND LINK_LIBS additional_windows_libs) + include_directories(${CMAKE_SOURCE_DIR}/deps/platform/windows) +else() + include_directories(${CMAKE_SOURCE_DIR}/deps/platform/linux) endif() # Add the directory where the linker should look for the libraries diff --git a/deps/platform/linux/platform.h b/deps/platform/linux/platform.h new file mode 100644 index 0000000..955e3e8 --- /dev/null +++ b/deps/platform/linux/platform.h @@ -0,0 +1,40 @@ +#ifndef __PLATFORM +#define __PLATFORM +#define __PLATFORM_SET_PYTHON_SEARCH_PATHS 1 +std::wstring executable_path() +{ + /* + wchar_t buffer[MAX_PATH]; + GetModuleFileName(NULL, buffer, MAX_PATH); + std::wstring exec_path = buffer; + */ + auto exec_path = std::filesystem::canonical("/proc/self/exe").parent_path(); + return exec_path.wstring(); + //size_t path_index = exec_path.find_last_of('/'); + //return exec_path.substr(0, path_index); + +} + +std::wstring executable_filename() +{ + auto exec_path = std::filesystem::canonical("/proc/self/exe"); + return exec_path.wstring(); +} + +std::wstring working_path() +{ + auto cwd = std::filesystem::current_path(); + return cwd.wstring(); +} + +std::string narrow_string(std::wstring convertme) +{ + //setup converter + using convert_type = std::codecvt_utf8; + std::wstring_convert converter; + + //use converter (.to_bytes: wstr->str, .from_bytes: str->wstr) + return converter.to_bytes(convertme); +} + +#endif diff --git a/deps/platform/windows/platform.h b/deps/platform/windows/platform.h new file mode 100644 index 0000000..ee4176e --- /dev/null +++ b/deps/platform/windows/platform.h @@ -0,0 +1,39 @@ +#ifndef __PLATFORM +#define __PLATFORM +#define __PLATFORM_SET_PYTHON_SEARCH_PATHS 0 +#include + +std::wstring executable_path() +{ + wchar_t buffer[MAX_PATH]; + GetModuleFileName(NULL, buffer, MAX_PATH); + std::wstring exec_path = buffer; + size_t path_index = exec_path.find_last_of(L"\\/"); + return exec_path.substr(0, path_index); +} + +std::wstring executable_filename() +{ + wchar_t buffer[MAX_PATH]; + GetModuleFileName(NULL, buffer, MAX_PATH); + std::wstring exec_path = buffer; + return exec_path; +} + +std::wstring working_path() +{ + auto cwd = std::filesystem::current_path(); + return cwd.wstring(); +} + +std::string narrow_string(std::wstring convertme) +{ + //setup converter + using convert_type = std::codecvt_utf8; + std::wstring_convert converter; + + //use converter (.to_bytes: wstr->str, .from_bytes: str->wstr) + return converter.to_bytes(convertme); +} + +#endif