IOS了解底层代码非常重要, 无论是我们 了解OC原理或者应付大厂面试等, 那么第一步肯定是能运行起来苹果源码
这边主要介绍下苹果开源源码的编译以及调试
源码下载地址:
由于目前这边最新的就是 objc4-781, 所以就拿它开刀吧 :)

下载之后解压得到

运行objc.xcodeproj, 因为我们目的是源码上面我们选择objc, 得到我们第一个错误
问题1: unable to find sdk 'macosx.internal'

- 解决方法
target → objc → Build Setting → Base SDK → macOS
因为默认的是 macosx.internal 但是这个SDK是找不到的, 所以换成macOS的 - 注意:
objc , obc-trampolines这两个必须改, 但其实剩余objc-simulator, objc4_tests 也是macosx.internal找不到的, 所以我建议一起都改下

解决完继续运行, 得到我们第二个错误, 文件找不到问题
问题2: 'sys/reason.h' file not found

- 解决方法
既然提示找不到, 那我们就造一个, 项目里面创建文件夹, SACommon(名字随便起)
统一存放找不到文件, 后面还有几个提示文件找不到的问题
- SACommon下建立sys文件夹, 然后去找reason.h文件

找到 10.15, 当然依旧下最新的10.15.6, 别问为什么, 老渣男了, 喜欢喜新厌旧

找到xnu这里面有我们想要的reason.h, 可以 command + f 模糊搜索xnu , 找到后点击下载

(先别着急关页面, 我们之后还会用到下载其他资源)
- xnu 中找到
bsd → sys → reason.h放入我们刚才建立的SACommon → sys文件夹内


- 配置文件检索路径:
target → objc → Build Setting → Header Serach Paths中添加搜索路径$(SRCROOT)/SACommon

这个问题解决, 我们再运行, 得到我们第三个错误
问题3: mach-o/dyld_priv.h' file not found

- 照旧 SACommon下新建mach-o文件夹, 然后去找dyld_priv.h文件
Apple source10.15.6: https://opensource.apple.com/release/macos-10156.html
2.找到dyld里面有我们想要的dyld_priv.h, command + f 模糊搜索dyld , 找到后点击下载

- dyld 中找到
include → mach-o → dyld_priv.h放入我们刚才建立的文件夹内SACommon → mach-o → dyld_priv.h


- 放入之后, 还需要修改 dyld_priv.h ,在 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
这个问题解决, 我们再运行, 得到我们第四个错误
问题4: 'os/lock_private.h' file not found , dyld_priv.h中 bridgeos(3.0) 报错


-
先解决这个bridgeos 3.0这个, Bridge OS是Apple独立的T2安全芯片使用的嵌入式操作系统, 而在这里我们用不到这个系统所以删掉即可
问题4-bridgeos(3.0)修改 照旧 SACommon下新建os文件夹, 然后去找lock_private.h文件
Apple source10.15.6: https://opensource.apple.com/release/macos-10156.html
3.找到libplatform里面有我们想要的lock_private.h, command + f 模糊搜索libplatform , 找到后点击下载

- libplatform 中找到
private → os → lock_private.h放入我们刚才建立的文件夹内SACommon → os → lock_private.h


这个问题解决, 我们再运行, 得到我们第五个错误
问题5: 'os/base_private.h' file not found
其实5跟4可以放一起解决, 因为都在 libplatform里面
libplatform 中找到private → os → base_private.h 放入我们刚才建立的文件夹内 SACommon → os → base_private.h


同样之后运行lock_private也会报bridgeos这个错误, 去掉即可

这个问题解决, 我们再运行, 得到我们第六个错误
问题6: 'pthread/tsd_private.h' file not found

- 照旧 SACommon下新建pthread文件夹, 然后去找tsd_private.h文件
Apple source10.15.6: https://opensource.apple.com/release/macos-10156.html
2.找到libpthread里面有我们想要的tsd_private.h, command + f 模糊搜索libpthread , 找到后点击下载

- libpthread 中找到
private → tsd_private.h放入我们刚才建立的文件夹内SACommon → pthread → tsd_private.h

这个问题解决, 我们再运行, 得到我们第七个错误
问题7: 'System/machine/cpu_capabilities.h' file not found

- 照旧 SACommon下新建System文件夹,进入System在创建machine文件夹, 然后去找cpu_capabilities.h文件
Apple source10.15.6: https://opensource.apple.com/release/macos-10156.html
2.找到xnu们想要的cpu_capabilities.h, command + f 模糊搜索xnu , 找到后点击下载

- xnu 中找到
osfmk → machine → cpu_capabilities.h放入我们刚才建立的文件夹内SACommon → System → machine → cpu_capabilities.h

这个问题解决, 我们再运行, 得到我们第八个错误
问题8: 'os/tsd.h' file not found

tsd.h也是在问题7 xnu 中,
xnu 中找到
libsysycall → os → tsd.h放入之前建立的文件夹内SACommon → os → tsd.h

这个问题解决, 我们再运行, 得到我们第九个错误
问题9: 'pthread/spinlock_private.h' file not found

照旧 去找spinlock_private.h文件, 这个跟问题6一样都在libpthread里面
libpthread 中找到
private → spinlock_private.h放入之前建立的文件夹内SACommon → pthread → spinlock_private.h(图片箭头标错了, 应该指向spinlock_private.h :) )

这个问题解决, 我们再运行, 得到我们第十个错误
问题10: 'System/pthread_machdep.h' file not found
- 照旧 去找pthread_machdep.h文件, 这个在 Libc中
Apple source Libc: https://opensource.apple.com/tarballs/Libc/
2.这里留意下Libc不能下最新的, 里面没有这个spinlock_private.h文件, 这个里面要下583的这个command + f 模糊搜索Libc-583 找到后点击下载

- Libc-583 中找到
pthreads → os → pthread_machdep.h放入之前建立的System内SACommon → System → pthread_machdep.h

这个问题解决, 我们再运行, 得到我们第十一个错误
问题11: 'CrashReporterClient.h' file not found

- 照旧 去找'CrashReporterClient.h文件, 这个在也在Libc中, 这里也是留意下Libc不能下最新的, 也是没有这个 CrashReporterClient.h文件, 在 Libc-825.24中
Apple source Libc: https://opensource.apple.com/tarballs/Libc/

- Libc-825.24 中找到
include → CrashReporterClient.h放入SACommon内SACommon → CrashReporterClient.h

3.引入之后CrashReporterClient.h 还是报找不到问题

方法① 需要在 Build Settings -> Preprocessor Macros 中加入LIBC_NO_LIBCRASHREPORTERCLIENT

如果还是报错(网上看到的)
方法② 直接更改了里面的宏信息 #define LIBC_NO_LIBCRASHREPORTERCLIENT
方法③ 如果还是报错CrashReporterClient 的问题,解决方法是 在BuildSetting --> Other Linker Flags 中去掉CrashReporterClient .
因为我这边只是方法① 就已经修复好, 方法②, 方法③我这边没试 :)
这个问题解决, 我们再运行, 得到我们第十二个错误
问题12: pthread_machdep.h 中 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

把报错的地方注释掉, 这里注意下一定要注释全, 别漏代码

这个问题解决, 我们再运行, 得到我们第十三个错误
问题13: 'objc-shared-cache.h' file not found

- 在之前问题3下载的dyld里面找到
include → objc-shared-cache.h放入SACommon内SACommon → objc-shared-cache.h

这个问题解决, 我们再运行, 得到我们第十四个错误
问题14: objc-errors.mm中报错 '_simple.h' file not found
- 在之前问题4下载的libplatform里面找到
private → _simple.h放入SACommon内SACommon → _simple.h

这个问题解决, 我们再运行, 得到我们第十五个错误
问题15: kern/restartable.h

- SACommon 中新建kern文件夹, 在之前问题2下载的xnu里面找到
osfmk → kern放入SACommon内SACommon → kern → restartable.h

这个问题解决, 我们再运行, 得到我们第十六个错误
问题16: 'Block_private.h' file not found

Apple source : https://opensource.apple.com/release/macos-1015.html
1.command + f 模糊搜索libclosure 找到后点击下载, 里面有我们想要的Block_private.h

- 在刚才下载的 libclosure 找到
Block_private.h放入SACommon内SACommon → Block_private.h

这个问题解决, 我们再运行, 得到我们第十七个错误
问题17: Mismatch in debug-ness macros

这个简单, 注释掉objc-runtime.mm中的#error mismatch in debug-ness macros即可
这个问题解决, 我们再运行, 得到我们第十八个错误
问题18: libobjc.order 路径问题

targets → Build Settings → Order File 添加 $(SRCROOT)/libobjc.order

这个问题解决, 我们再运行, 得到我们第十九个错误
问题19: /xcodebuild:1:1: SDK "macosx.internal" cannot be located. 编译脚本问题

targets → Build Phases → Run Script 中macosx.internal 改成 macosx

这个问题解决, 我们再运行, OK, 没有再报错, 那么接下来我们就可以加些东西来进行编译调试
编译调试环节
targets 点击 "+" 新建 target: SATest



绑定依赖关系 Dependencies → objc, Link Binary With Libraries → libobjc.A.dylib



源码调试环节
在之前建立的SATest中 创建 SAPerson对象(名字随便取)

targets → SATest → Build Phases → Compile Sources 中main.m 放在最上面方便断点调试

接下来main.m 中引入 SAPerson.h头文件, 运行项目选择SATest 就可以愉快的进行源码调试

需要总共下载项目

