objc824源码编译
前言
本人最近学习iOS底层原理,需要编译源码,在这里,记录一下遇到的问题以及处理的过程,因为我的系统是 11.3.1 的,所以我配置的是最新的 objc源码。

这里附上objc源码地址:https://opensource.apple.com/tarballs/objc4/
一、 准备
1、环境
系统:macOS Big Sur - 11.3.1
工具:Xcode 12.5
2、资料
- dyld-851.27
- launchd-106.10(需要去mac os 10.4.4中下载)
- libauto-187
- Libc-583(需要去macos 10.6.2中去下载)
- Libc-825.24(需要去macos10.8.1中去下载)
- Libc-1439.100.3
- libclosure-79
- libdispatch-1271.100.5
- libplatform-254.80.2
- libpthread-454.100.8
- xnu-7195.101.1
- objc4-824

以上是需要的材料,都可以在 https://opensource.apple.com/releases/ 这里下载。
我也有将这些文件上传到 git,下载地址:https://github.com/RegretSF/objc824
二、 编译
1、unable to find sdk 'macosx.internal'
把objc824源码下载下来之后编译,发现报错:

解决方法:
target → objc/objc-trampolines → Build Setting → Base SDK
将其改为macOS
,如图:
注意:objc和objc-trampolines的都要改,因为编译器报的错误是这两个。
2、'os/feature_private.h' file not found

解决方法:这个声明报错直接注释掉就好了。
3、'sys/reason.h' file not found

解决方法:项目目录新建文件夹 FNCommon
,这个文件夹的名字可以随便取,在 FNCommon
里新建 sys
文件夹,将 xnu-7195.101.1/bsd/sys/
路径下的 reason.h
文件放到新建的 FNCommon/sys/
目录下,如图:

但这个时候编译还是会报错,解决方法:给我们的工程设置文件检索路径,选择
target -> objc -> Build Settings
,在工程的 Header Serach Paths
中添加搜索路径 $(SRCROOT)/FNCommon
,如图:
4、'mach-o/dyld_priv.h' file not found

解决方法:其实方式和第3点一样,在
FNCommon
里新建 mach-o
文件夹,将 dyld-851.27/include/mach-o
路径下的 dyld_priv.h
文件放到新建的 FNCommon/mach-o/
目录下,如图:
添加完成了之后,这里在
dyld_priv.h
文件顶部加入一下宏:
#define DYLD_MACOSX_VERSION_10_11 0x000A0B00
#define DYLD_MACOSX_VERSION_10_12 0x000A0C00
#define DYLD_MACOSX_VERSION_10_13 0x000A0D00
#define DYLD_MACOSX_VERSION_10_14 0x000A0E00
如图:

5、'os/lock_private.h' file not found

解决方法:和第3点一样,在
FNCommon
里新建 os
文件夹,将 libplatform-254.80.2/private/os
路径下的 lock_private.h
文件放到新建的 FNCommon/os/
目录下,如图:
6、bridgeos - 1 报错

将有关
bridgeos
报错的代码,删除掉 bridgeos(3.0)
,如图:
到这里先编译,后面还会出现有关 bridgeos
报错的代码,到时候用一样的方式,删除掉 bridgeos
即可。
7、'os/base_private.h' file not found

解决方法:和第3点一样,将
xnu-7195.101.1/libkern/os
路径下的 base_private.h
文件放到新建的 FNCommon/os/
目录下,如图:
8、bridgeos - 2 报错

解决方法:删除掉代码中的 bridgeos(4.0)
,如图:

9、'pthread/tsd_private.h' file not found

解决方法:和第3点一样,在 FNCommon
里新建 pthread
文件夹,将 libpthread-454.100.8/private/pthread
路径下的 tsd_private.h
文件放到新建的 FNCommon/pthread/
目录下,如图:

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

解决方法:和第3点一样,在
FNCommon
里新建 System
文件夹并在System
里新建文件夹machine
,将 xnu-7195.101.1/osfmk/machine
路径下的 cpu_capabilities.h
文件放到新建的 FNCommon/System/machine/
目录下,如图:

11、'os/tsd.h' file not found

解决方法:和第5点一样,将 xnu-7195.101.1/libsyscall/os
路径下的 tsd.h
文件放到新建的 FNCommon/os/
目录下,如图:

12、'pthread/spinlock_private.h' file not found

解决方法:和第9点一样,将
libpthread-454.100.8/private/pthread
路径下的 spinlock_private.h
文件放到新建的 FNCommon/pthread/
目录下,如图:
13、'System/pthread_machdep.h' file not found

解决方法:和第10点一样,将 Libc-825.24/pthreads
路径下的 pthread_machdep.h
文件放到新建的 FNCommon/System/
目录下,如图:

添加完成后,重新编译,会出现几个报错,如图:

我们直接把报错的代码注释掉,重新编译就好了。
14、'CrashReporterClient.h' file not found

解决方法:将 Libc-825.24/include
路径下的 CrashReporterClient.h
文件放到新建的 FNCommon/
目录下,如图:

15、'_simple.h' file not found

解决方法:将 libplatform-254.80.2/private
路径下的 _simple.h
文件放到新建的 FNCommon/
目录下,如图:

16、'Cambria/Traps.h' file not found, 'Cambria/Cambria.h' file not found

解决方法:这两个声明直接注释掉就好了。
17、'os/linker_set.h' file not found

解决方法:将 Libc-1439.100.3/os
路径下的 linker_set.h
文件放到新建的 FNCommon/os/
目录下,如图:

18、'kern/restartable.h' file not found

解决方法:和第3点一样,在 FNCommon
里新建 kern
文件夹,将 xnu-7195.101.1/osfmk/kern
路径下的 restartable.h
文件放到新建的 FNCommon/kern/
目录下,如图:

19、Use of undeclared identifier 'CRGetCrashLogMessage',Use of undeclared identifier 'CRSetCrashLogMessage'

解决方法:将代码注释掉即可,如图:

20、Use of undeclared identifier 'oah_is_current_process_translated',

解决方法:将代码注释掉即可,如图:

21、Use of undeclared identifier 'dyld_fall_2020_os_versions'

解决方法:将代码注释掉即可,如图:


22、'Block_private.h' file not found

解决方法:将 ``libclosure-79/路径下的
Block_private.h 文件放到新建的
FNCommon/``` 目录下,如图:

23、Use of undeclared identifier 'dyld_platform_version_macOS_10_13',Use of undeclared identifier 'dyld_platform_version_macOS_10_11',Use of undeclared identifier 'dyld_fall_2018_os_versions'

解决方法:将代码注释掉即可,如图:



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

解决方法:将 dyld-851.27/include
路径下的 objc-shared-cache.h
文件放到新建的 FNCommon/
目录下,如图:

25、'os/reason_private.h' file not found, 'os/variant_private.h' file not found

解决方法:将 xnu-7195.101.1/libkern/os
路径下的 reason_private.h
文件和Libc-1439.100.3/os
路径下的 reason_private.h
文件放到新建的 FNCommon/os/
目录下,如图:

26、bridgeos - 3 报错

解决方法:将 bridgeos
删掉即可,其他代码有也一样,如图:

27、Use of undeclared identifier 'dyld_platform_version_bridgeOS_2_0'...

解决方法:将有关的代码注释掉即可,如图:

28、'_static_assert' declared as an array with a negative size

解决方法:将有关的代码注释掉即可,如图:

29、ld: can't open order file: /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.3.sdk/AppleInternal/OrderFiles/libobjc.order

libobjc.order 路径问题。
解决方法:选择 target -> objc -> Build Settings
,在工程的 Order File
中添加搜索路径 $(SRCROOT)/libobjc.order
,如图:

30、library not found for -lCrashReporterClient

解决方法:需要在 TARGETS -> objc -> Build Settings -> Preprocessor Macros
中加入: LIBC_NO_LIBCRASHREPORTERCLIENT
,如图:

如果还是报错,找到TARGETS -> objc -> Build Setting -> Other Link Flags
,删除-lCrashReporterClient
和-loah
,如图:

31、xcodebuild:1:1: SDK "macosx.internal" cannot be located.,/xcrun:1:1: 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: unable to find utility "clang++", not a developer tool or in PATH

解决方法:选择 target -> objc -> Build Phases -> Run Script(markgc)
把脚本文本macosx.internal
改成 macosx
,如图:

到这里,就编译成功啦!
我已经老血吐了一地了!的确配置过程还是相对来说有点恶心,尤其是文件的查漏补缺。
三、 objc 编译调试
1、新建一个 Target : FNTest,如图:



2、绑定二进制依赖关系

运行代码进入源码,可以自由编译调试!
