X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;ds=sidebyside;f=folly%2FVersionCheck.h;h=9a3ce67727851ff93a895717acb584be2207f2e6;hb=8d04c40413bbce86408b05814de44d06fe1f8723;hp=94be9e56be5cf398b7ec77ba1132f5836e796fa5;hpb=a42878e365ad97ef653a823ac5d5b62f91f63663;p=folly.git diff --git a/folly/VersionCheck.h b/folly/VersionCheck.h index 94be9e56..9a3ce677 100644 --- a/folly/VersionCheck.h +++ b/folly/VersionCheck.h @@ -1,5 +1,5 @@ /* - * Copyright 2014 Facebook, Inc. + * Copyright 2016 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 #include @@ -68,11 +67,26 @@ * ... 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", \ @@ -92,5 +106,3 @@ } \ } \ } - -#endif /* FOLLY_VERSIONCHECK_H_ */