objc4源码编译

331 阅读3分钟

编译环境:macOS Monterey 12.6,XCode 14.0

本次编译源码版本为objc4-818.2:opensource.apple.com/tarballs/ob…

下载解压后打开,二话不说先运行,迎接我们的第一个错误:

unable to find sdk 'macosx.internal'

解决:将PROJECT和TARGETS中的Base SDK都改为macOS image.png

修改完成之后继续编译,接着报错,找不到头文件:

'sys/reason.h' file not found

因为接下来还会有一堆类似这样找不到头文件的错误,而缺失的这些文件都需要我们去苹果源码网站opensource.apple.com去下载对应的库找到所需要的文件,会需要下载几个库,这里一次性全列举出来,按需下载需要的版本:

xnu库:opensource.apple.com/tarballs/xn…

dyld库:opensource.apple.com/tarballs/dy…

libplatform库:opensource.apple.com/tarballs/li…

libpthread库:opensource.apple.com/tarballs/li…

Libc库:opensource.apple.com/tarballs/Li…

libclosure库:opensource.apple.com/tarballs/li…

下载这些库之后找到我们需要的文件即可,这里针对报的错贴出对应的文件所在路径:

'sys/reason.h' file not found

reason.h文件路径:xnu-7195.81.3/bsd/sys/reason.h

找到reason.h文件,将其复制出来,放在我自己在objc4源码文件根目录下新建的common/sys/路径下:

image.png

然后在Build Settings - Header Search Paths中添加其路径:

image.png

接着编译还会继续报类似找不到头文件的错误,我们继续依照上面的方法,在各苹果源码库中找到对应的文件,复制到自建的common下(记得创建其上层路径文件夹),下面一一列举此类报错和对应的下载地址和我们所需的文件所在路径。

'mach-o/dyld_priv.h' file not found

dyld_priv.h文件路径:dyld-852.2/include/mach-o/dyld_priv.h

'os/lock_private.h' file not found

lock_private.h文件路径:libplatform-254.80.2/private/os/lock_private.h

'os/base_private.h' file not found

base_private.h文件路径:xnu-7195.81.3/libkern/os/base_private.h

  • 'pthread/tsd_private.h' file not found
  • 'pthread/spinlock_private.h' file not found

tsd_private.h文件路径:libpthread-454.80.2/private/pthread/tsd_private.h

spinlock_private.h文件路径:libpthread-454.80.2/private/pthread/spinlock_private.h

'System/machine/cpu_capabilities.h' file not found

cpu_capabilities.h文件路径:xnu-7195.81.3/osfmk/machine/cpu_capabilities.h

'os/tsd.h' file not found

tsd.h文件地址:xnu-7195.81.3/libsyscall/os/tsd.h

'CrashReporterClient.h' file not found

CrashReporterClient.h文件地址:Libc-825.40.1/include/CrashReporterClient.h 导入CrashReporterClient.h文件之后还是会报错,还需要在Build Settings - Preprocessor Macros中添加"LIBC_NO_LIBCRASHREPORTERCLIENT"

image.png

Library not found for -lCrashReporterClient

在Build Settings - Linking - Other Linker Flags - Any macOS SDK中将'lCrashReporterClient'移除。

  • Typedef redefinition with different types ('int' vs 'volatile OSSpinLock' (aka 'volatile int'))
  • Static declaration of '_pthread_has_direct_tsd' follows non-static declaration
  • Static declaration of '_pthread_getspecific_direct' follows non-static declaration

重复定义的都注释掉: image.png

Expected ','bridgeos(3.0)报错, bridgeos(4.0)报错

把bridgeos(3.0)、bridgeos(4.0)从代码中删掉: image.png

image.png

'os/feature_private.h' file not found image.png image.png

Mismatch in debug-ness macros

注释掉: image.png

Use of undeclared identifier 'dyld_fall_2020_os_versions'

注释掉: image.png

  • Unknown type name 'uint32_t'
  • Use of undeclared identifier 'INT64_C'

导入"#include <stdint.h>" image.png

Use of undeclared identifier 'dyld_platform_version_macOS_10_11'

这一段注释掉: image.png

Use of undeclared identifier 'dyld_fall_2018_os_versions'

image.png

'objc-bp-assist.h' file not found

注释这行: image.png

Use of undeclared identifier 'dyld_platform_version_macOS_10_13'

注释掉这一段代码: image.png

'objc-shared-cache.h' file not found

objc-shared-cache.h文件地址:dyld-852.2/include/objc-shared-cache.h

'_simple.h' file not found

_simple.h文件地址:libplatform-254.80.2/private/_simple.h

  • 'Cambria/Traps.h' file not found
  • 'Cambria/Cambria.h' file not found

注释掉这两行: image.png

'Block_private.h' file not found

Block_private.h文件地址:libclosure-79/Block_private.h

'kern/restartable.h' file not found

restartable.h文件地址:xnu-7195.81.3/osfmk/kern/restartable.h

'os/linker_set.h' file not found

linker_set.h文件地址:xnu-7195.81.3/bsd/sys/linker_set.h

  • Use of undeclared identifier 'oah_is_current_process_translated'
  • Use of undeclared identifier 'objc_thread_get_rip'

注释掉这一段: image.png

'os/reason_private.h' file not found

reason_private.h文件地址:xnu-7195.81.3/libkern/os/reason_private.h

'os/variant_private.h' file not found

variant_private.h文件地址:Libc-1439.40.11/os/variant_private.h

  • Use of undeclared identifier 'dyld_platform_version_bridgeOS_2_0'
  • Use of undeclared identifier 'dyld_platform_version_iOS_10_0'
  • Use of undeclared identifier 'dyld_platform_version_macOS_10_12'
  • Use of undeclared identifier 'dyld_platform_version_tvOS_10_0'
  • Use of undeclared identifier 'dyld_platform_version_watchOS_3_0'

注释掉这一段: image.png

Can't open order file: /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/AppleInternal/OrderFiles/libobjc.order

在Build Settings - Linking - Order File中添加.order文件的路径:$(SRCROOT)/libobjc.order image.png

Library not found for -loah

在Build Settings - Linking - Other Linker Flags - Any macOS SDK中将'-loah'移除。

'_static_assert' declared as an array with a negative size

直接注释: image.png

  • SDK "macosx.internal" cannot be located.
  • xcrun:1:1: error build: sh -c '/Applications/Xcode. app/Contents/Developer/usr/bin/xcodebuild -sdk macosx.internal -find clang++ 2> /dev/null' failed with exit code 16384: (null) (errno=No such file or directory)
  • xcrun:1:1: error build: unable to find utility "clang++", not a developer tool or in PATH

将Build Phases - Run Script(markgc)中的'macosx.internal'改为'macosx'。 image.png

至此,编译就通过了。