1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ex: set tabstop=8 softtabstop=2 shiftwidth=2 expandtab: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
9 #include "nsCUPSShim.h"
10 #include "mozilla/ArrayUtils.h"
11 #include "mozilla/Logging.h"
14 #ifdef CUPS_SHIM_RUNTIME_LINK
16 mozilla::LazyLogModule
gCupsLinkLog("CupsLink");
18 # define DEBUG_LOG(...) \
19 MOZ_LOG(gCupsLinkLog, mozilla::LogLevel::Debug, (__VA_ARGS__))
21 // TODO: This is currently pointless as we always use the compile-time linked
22 // version of CUPS, but in the future this may become a configure option.
23 // We also cannot use NSPR's library suffix support, since that cannot handle
24 // version number suffixes.
26 static const char gCUPSLibraryName
[] = "libcups.2.dylib";
28 static const char gCUPSLibraryName
[] = "libcups.so.2";
31 static bool LoadCupsFunc(PRLibrary
* aLib
, void** aDest
, const char* const aName
,
32 nsCUPSShim::Optional aOptional
) {
33 *aDest
= PR_FindSymbol(aLib
, aName
);
35 DEBUG_LOG("%s not found in CUPS library", aName
);
36 return bool(aOptional
);
41 nsCUPSShim::nsCUPSShim() {
42 mCupsLib
= PR_LoadLibrary(gCUPSLibraryName
);
44 DEBUG_LOG("CUPS library not found");
50 // This is a macro so that it could also load from libcups if we are
51 // configured to use it as a compile-time dependency.
53 // We try to load all functions even if some fail so that we get the debug log
55 # define CUPS_SHIM_LOAD(opt_, fn_) \
57 LoadCupsFunc(mCupsLib, reinterpret_cast<void**>(&fn_), #fn_, opt_);
58 CUPS_SHIM_ALL_FUNCS(CUPS_SHIM_LOAD
)
59 # undef CUPS_SHIM_LOAD
63 // With TSan, we cannot unload libcups once we have loaded it because
64 // TSan does not support unloading libraries that are matched from its
65 // suppression list. Hence we just keep the library loaded in TSan builds.
66 PR_UnloadLibrary(mCupsLib
);
72 // Set mInitOkay only if all cups functions are loaded successfully.