Path: blob/main_old/src/common/apple/SoftLinking.h
1693 views
//1// Copyright 2020 The ANGLE Project Authors. All rights reserved.2// Use of this source code is governed by a BSD-style license that can be3// found in the LICENSE file.4//56// SoftLinking.h: Macros for soft-linking Frameworks and Functions.78#ifndef SOFT_LINKING_APPLE_H_9#define SOFT_LINKING_APPLE_H_1011#include "common/platform.h"1213#if defined(ANGLE_PLATFORM_APPLE)1415# include "common/debug.h"1617# import <dispatch/dispatch.h>18# import <dlfcn.h>19# import <objc/runtime.h>2021# define RELEASE_ASSERT(expression, message) \22(expression \23? static_cast<void>(0) \24: (FATAL() << "\t! Assert failed in " << __FUNCTION__ << " (" << __FILE__ << ":" \25<< __LINE__ << "): " << #expression << "\n\t! Message: " << message))2627# ifdef __cplusplus28# define EXTERN_C_BEGIN extern "C" {29# define EXTERN_C_END }30# else31# define EXTERN_C_BEGIN32# define EXTERN_C_END33# endif3435# define SOFT_LINK_FRAMEWORK_HEADER(framework) extern void *framework##Library();3637# define SOFT_LINK_FRAMEWORK_SOURCE(framework) \38void *framework##Library() \39{ \40static dispatch_once_t once = 0; \41static void *frameworkLibrary = NULL; \42dispatch_once(&once, ^{ \43frameworkLibrary = dlopen( \44"/System/Library/Frameworks/" #framework ".framework/" #framework, RTLD_NOW); \45RELEASE_ASSERT(frameworkLibrary, "Unable to load " #framework ".framework"); \46}); \47return frameworkLibrary; \48}4950# define SOFT_LINK_FUNCTION_HEADER(framework, functionName, resultType, parameterDeclarations, \51parameterNames) \52EXTERN_C_BEGIN \53resultType functionName parameterDeclarations; \54EXTERN_C_END \55extern resultType init##framework##functionName parameterDeclarations; \56extern resultType(*softLink##framework##functionName) parameterDeclarations; \57inline __attribute__((__always_inline__)) resultType functionName parameterDeclarations \58{ \59return softLink##framework##functionName parameterNames; \60}6162# define SOFT_LINK_FUNCTION_SOURCE(framework, functionName, resultType, parameterDeclarations, \63parameterNames) \64resultType(*softLink##framework##functionName) parameterDeclarations = \65init##framework##functionName; \66resultType init##framework##functionName parameterDeclarations \67{ \68static dispatch_once_t once; \69dispatch_once(&once, ^{ \70softLink##framework##functionName = \71(resultType(*) parameterDeclarations)dlsym(framework##Library(), #functionName); \72}); \73return softLink##framework##functionName parameterNames; \74}7576# define SOFT_LINK_CLASS_HEADER(className) \77@class className; \78extern Class (*get##className##Class)(); \79className *alloc##className##Instance(); \80inline className *alloc##className##Instance() { return [get##className##Class() alloc]; }8182# define SOFT_LINK_CLASS(framework, className) \83@class className; \84static Class init##className(); \85Class (*get##className##Class)() = init##className; \86static Class class##className; \87\88static Class className##Function() { return class##className; } \89\90static Class init##className() \91{ \92static dispatch_once_t once; \93dispatch_once(&once, ^{ \94framework##Library(); \95class##className = objc_getClass(#className); \96RELEASE_ASSERT(class##className, "objc_getClass failed for " #className); \97get##className##Class = className##Function; \98}); \99return class##className; \100} \101_Pragma("clang diagnostic push") \102_Pragma("clang diagnostic ignored \"-Wunused-function\"") static className \103*alloc##className##Instance() \104{ \105return [get##className##Class() alloc]; \106} \107_Pragma("clang diagnostic pop")108109#endif // defined(ANGLE_PLATFORM_APPLE)110111#endif // SOFT_LINKING_APPLE_H_112113114