X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=folly%2FVersionCheck.h;h=049556ef2a804c8b855f1021c8eae51f5a940a0c;hb=0659842527913025d998860811fe135cc6170044;hp=a15675d7defdf8d3244a5e9c812e6f81c2e301d4;hpb=1274a688d51ee0b82c2eeb0e1f373e9e992722e6;p=folly.git diff --git a/folly/VersionCheck.h b/folly/VersionCheck.h index a15675d7..049556ef 100644 --- a/folly/VersionCheck.h +++ b/folly/VersionCheck.h @@ -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 #include @@ -67,6 +66,29 @@ * * ... 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", \ @@ -84,5 +106,3 @@ } \ } \ } - -#endif /* FOLLY_VERSIONCHECK_H_ */