From 931e196e8311ddcb6e4c866ef27ffc42589cb4dc Mon Sep 17 00:00:00 2001 From: Leonardo de Moura Date: Mon, 30 Dec 2013 14:50:40 -0800 Subject: [PATCH] fix(build): problem with reading LEAN_PATH on cygwin Signed-off-by: Leonardo de Moura --- src/CMakeLists.txt | 4 +++ src/util/lean_path.cpp | 58 +++++++++++++++++++++++++++++++++++------- 2 files changed, 53 insertions(+), 9 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 907421c9f..abf5913ea 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -30,6 +30,10 @@ if((${CYGWIN} EQUAL "1") OR (${CMAKE_SYSTEM_NAME} MATCHES "Windows")) set(LEAN_EXTRA_CXX_FLAGS "${LEAN_EXTRA_CXX_FLAGS} -D LEAN_WINDOWS -D LEAN_WIN_STACK_SIZE=${LEAN_WIN_STACK_SIZE}") endif() +if("${CYGWIN}" EQUAL "1") + set(LEAN_EXTRA_CXX_FLAGS "${LEAN_EXTRA_CXX_FLAGS} -D LEAN_CYGWIN") +endif() + if("${MULTI_THREAD}" MATCHES "OFF") message(STATUS "Disabled multi-thread support, it will not be safe to run multiple threads in parallel") else() diff --git a/src/util/lean_path.cpp b/src/util/lean_path.cpp index 0fd7c0ad5..ec1d5c477 100644 --- a/src/util/lean_path.cpp +++ b/src/util/lean_path.cpp @@ -17,9 +17,9 @@ namespace lean { #if defined(LEAN_WINDOWS) // Windows version #include -static char g_path_sep = ';'; -static char g_sep = '\\'; -static char g_bad_sep = '/'; +static char g_path_sep = ';'; +static char g_sep = '\\'; +static char g_bad_sep = '/'; static std::string get_exe_location() { HMODULE hModule = GetModuleHandleW(NULL); WCHAR path[MAX_PATH]; @@ -31,9 +31,9 @@ static std::string get_exe_location() { // OSX version #include #include -static char g_path_sep = ':'; -static char g_sep = '/'; -static char g_bad_sep = '\\'; +static char g_path_sep = ':'; +static char g_sep = '/'; +static char g_bad_sep = '\\'; static std::string get_exe_location() { char buf[PATH_MAX]; uint32_t bufsize = PATH_MAX; @@ -49,9 +49,9 @@ static std::string get_exe_location() { #include #include // NOLINT #include -static char g_path_sep = ':'; -static char g_sep = '/'; -static char g_bad_sep = '\\'; +static char g_path_sep = ':'; +static char g_sep = '/'; +static char g_bad_sep = '\\'; static std::string get_exe_location() { char path[PATH_MAX]; char dest[PATH_MAX]; @@ -66,11 +66,51 @@ static std::string get_exe_location() { } #endif +#if defined(LEAN_CYGWIN) +/** + \brief Cleanup path when using cygwin. + This procedure performs two fixes, if the string contains '/'. + + 1- It replaces '/' with '\\' and ':' with ';' + + 2- Then, it replaces "\cygdrive\c\" with "c:" +*/ +static std::string fix_cygwin_path(std::string f) { + if (f.find('/') != std::string::npos) { + // Step 1. + for (auto & c : f) { + if (c == g_bad_sep) + c = g_sep; + else if (c == ':') + c = g_path_sep; + } + // Step 2. + size_t pos = 0; + size_t matchpos; + std::string cygdrive("\\cygdrive\\"); + while ((matchpos = f.find(cygdrive, pos)) != std::string::npos) { + // erase "\cygdrive\" + f = f.replace(matchpos, cygdrive.size(), ""); + // replace the next "\" with ":\" + if ((matchpos = f.find("\\", matchpos)) != std::string::npos) { + f.replace(matchpos, 1, ":\\"); + } + pos = matchpos; + } + } + return f; +} +#endif + std::string normalize_path(std::string f) { +#if defined(LEAN_CYGWIN) + f = fix_cygwin_path(f); +#else for (auto & c : f) { if (c == g_bad_sep) c = g_sep; } +#endif return f; }