Make folly::getCurrentThreadId() return a thread ID on OSX
[folly.git] / folly / VersionCheck.h
index 94be9e56be5cf398b7ec77ba1132f5836e796fa5..049556ef2a804c8b855f1021c8eae51f5a940a0c 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2014 Facebook, Inc.
+ * Copyright 2017 Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -14,8 +14,7 @@
  * limitations under the License.
  */
 
-#ifndef FOLLY_VERSIONCHECK_H_
-#define FOLLY_VERSIONCHECK_H_
+#pragma once
 
 #include <cstdio>
 #include <cstdlib>
  * ... and then commpile your file with -DMYLIB_VERSION=\"1\"
  */
 
-#ifdef __APPLE__
+#if defined(_MSC_VER)
+// MSVC doesn't support constructor priorities. Just pray it works, I guess.
+// We could implement a link-time mechanism for MSVC,
+// via #pragma detect_mismatch but that would only handle
+// static library linking.
+# define FOLLY_VERSION_CHECK_PRIORITY(Ret, name) \
+    __pragma(section(".CRT$XCU",read)) \
+    static Ret __cdecl name(void); \
+    __declspec(allocate(".CRT$XCU")) \
+    Ret (__cdecl*name##_)(void) = name; \
+    Ret __cdecl name()
+
+#elif defined(__APPLE__)
 // OS X doesn't support constructor priorities. Just pray it works, I guess.
-#define FOLLY_VERSION_CHECK_PRIORITY __attribute__((__constructor__))
+# define FOLLY_VERSION_CHECK_PRIORITY(Ret, name) \
+  __attribute__((__constructor__)) Ret name()
+
 #else
-#define FOLLY_VERSION_CHECK_PRIORITY __attribute__((__constructor__(101)))
+# define FOLLY_VERSION_CHECK_PRIORITY(Ret, name) \
+  __attribute__((__constructor__(101))) Ret name()
 #endif
 
 // Note that this is carefully crafted: PRODUCT##Version must have external
@@ -83,7 +97,7 @@
 #define FOLLY_VERSION_CHECK(PRODUCT, VERSION) \
   const char* PRODUCT##Version = VERSION; \
   namespace { \
-  FOLLY_VERSION_CHECK_PRIORITY void versionCheck() { \
+  FOLLY_VERSION_CHECK_PRIORITY(void, versionCheck) { \
     if (strcmp(PRODUCT##Version, VERSION)) { \
       fprintf(stderr, \
               "Invalid %s version: desired [%s], currently loaded [%s]\n", \
     } \
   } \
   }
-
-#endif /* FOLLY_VERSIONCHECK_H_ */