From 363af568b8491af1a4256b09b04cfa8a0606d8cc Mon Sep 17 00:00:00 2001 From: Spencer Low Date: Sat, 7 Nov 2015 18:51:54 -0800 Subject: [PATCH] adb/base: minor compiler portability improvements I've been using these changes to compile with Visual Studio. - GetFileBasename(): __FILE__ uses \ with Visual Studio. - adb_trace.cpp: Apparently VS needs an ampersand before the function name. - "expr1 ? : expr2" is a GCC extension. - contains std::min(). - seekdir can't always be #define'd because some headers have members named seekdir. - adb_utils.cpp: Not really a compiler issue, just a random fix: 0x7F/DEL is not printable. Change-Id: I0dfb634f1ba4ccbc0d1b9f71b00e838fbebb3b41 Signed-off-by: Spencer Low --- adb/adb_trace.cpp | 2 +- adb/adb_utils.cpp | 7 ++----- adb/file_sync_client.cpp | 2 +- adb/sockets.cpp | 2 ++ adb/sysdeps.h | 7 ++++++- adb/transport.cpp | 1 + base/logging.cpp | 19 ++++++++++++++----- 7 files changed, 27 insertions(+), 13 deletions(-) diff --git a/adb/adb_trace.cpp b/adb/adb_trace.cpp index 9586f7c75..cf99df7ac 100644 --- a/adb/adb_trace.cpp +++ b/adb/adb_trace.cpp @@ -163,7 +163,7 @@ void adb_trace_init(char** argv) { } #endif - android::base::InitLogging(argv, AdbLogger); + android::base::InitLogging(argv, &AdbLogger); setup_trace_mask(); VLOG(ADB) << adb_version(); diff --git a/adb/adb_utils.cpp b/adb/adb_utils.cpp index fd61bda61..42f1c7de7 100644 --- a/adb/adb_utils.cpp +++ b/adb/adb_utils.cpp @@ -182,11 +182,8 @@ std::string dump_hex(const void* data, size_t byte_count) { line.push_back(' '); for (size_t i = 0; i < byte_count; ++i) { - int c = p[i]; - if (c < 32 || c > 127) { - c = '.'; - } - line.push_back(c); + int ch = p[i]; + line.push_back(isprint(ch) ? ch : '.'); } return line; diff --git a/adb/file_sync_client.cpp b/adb/file_sync_client.cpp index 7f34adeec..268a11f59 100644 --- a/adb/file_sync_client.cpp +++ b/adb/file_sync_client.cpp @@ -753,7 +753,7 @@ static int set_time_and_mode(const char *lpath, time_t time, unsigned int mode) umask(mask); int r2 = chmod(lpath, mode & ~mask); - return r1 ? : r2; + return r1 ? r1 : r2; } static bool copy_remote_dir_local(SyncConnection& sc, std::string rpath, diff --git a/adb/sockets.cpp b/adb/sockets.cpp index f8c2f6419..eb0ce85fb 100644 --- a/adb/sockets.cpp +++ b/adb/sockets.cpp @@ -25,6 +25,8 @@ #include #include +#include + #if !ADB_HOST #include "cutils/properties.h" #endif diff --git a/adb/sysdeps.h b/adb/sysdeps.h index 173562745..9f4012ade 100644 --- a/adb/sysdeps.h +++ b/adb/sysdeps.h @@ -309,7 +309,12 @@ extern char* adb_getcwd(char* buf, int size); #define closedir adb_closedir #define rewinddir rewinddir_utf8_not_yet_implemented #define telldir telldir_utf8_not_yet_implemented -#define seekdir seekdir_utf8_not_yet_implemented +// Some compiler's C++ headers have members named seekdir, so we can't do the +// macro technique and instead cause a link error if seekdir is called. +inline void seekdir(DIR*, long) { + extern int seekdir_utf8_not_yet_implemented; + seekdir_utf8_not_yet_implemented = 1; +} #define utime adb_utime #define chmod adb_chmod diff --git a/adb/transport.cpp b/adb/transport.cpp index 9d5085491..406688999 100644 --- a/adb/transport.cpp +++ b/adb/transport.cpp @@ -26,6 +26,7 @@ #include #include +#include #include #include diff --git a/base/logging.cpp b/base/logging.cpp index 6bfaaec2c..248cd0617 100644 --- a/base/logging.cpp +++ b/base/logging.cpp @@ -212,8 +212,8 @@ void InitLogging(char* argv[]) { gInitialized = true; // Stash the command line for later use. We can use /proc/self/cmdline on - // Linux to recover this, but we don't have that luxury on the Mac, and there - // are a couple of argv[0] variants that are commonly used. + // Linux to recover this, but we don't have that luxury on the Mac/Windows, + // and there are a couple of argv[0] variants that are commonly used. if (argv != nullptr) { gProgramInvocationName.reset(new std::string(basename(argv[0]))); } @@ -264,11 +264,20 @@ void SetLogger(LogFunction&& logger) { gLogger = std::move(logger); } -// We can't use basename(3) because this code runs on the Mac, which doesn't -// have a non-modifying basename. static const char* GetFileBasename(const char* file) { + // We can't use basename(3) even on Unix because the Mac doesn't + // have a non-modifying basename. const char* last_slash = strrchr(file, '/'); - return (last_slash == nullptr) ? file : last_slash + 1; + if (last_slash != nullptr) { + return last_slash + 1; + } +#if defined(_WIN32) + const char* last_backslash = strrchr(file, '\\'); + if (last_backslash != nullptr) { + return last_backslash + 1; + } +#endif + return file; } // This indirection greatly reduces the stack impact of having lots of