Re-work the OpenSSL portability header to be a portability header
[folly.git] / folly / VersionCheck.h
index a15675d7defdf8d3244a5e9c812e6f81c2e301d4..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\"
  */
+
+#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(Ret, name) \
+  __attribute__((__constructor__)) Ret name()
+
+#else
+# define FOLLY_VERSION_CHECK_PRIORITY(Ret, name) \
+  __attribute__((__constructor__(101))) Ret name()
+#endif
+
 // Note that this is carefully crafted: PRODUCT##Version must have external
 // linkage (so it collides among versions), versionCheck must have internal
 // linkage (so it does NOT collide between versions); if we're trying to have
@@ -75,7 +97,7 @@
 #define FOLLY_VERSION_CHECK(PRODUCT, VERSION) \
   const char* PRODUCT##Version = VERSION; \
   namespace { \
-  __attribute__((constructor(101))) 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_ */