美文网首页
iOS底层原理02 -- objc4-781源码编译&调试

iOS底层原理02 -- objc4-781源码编译&调试

作者: 阿木小丸子 | 来源:发表于2020-11-28 19:31 被阅读0次

本文主要是通过将objc4-781的源码编译成功,然后在源码中进行调试,便于跟踪方法的底层实现流程

环境:
macOS Catalina 10.15.7
XCode 12.2
objc4-781

依赖文件下载:

其中除了其中 launchd-106.10 需要在Mac OS X 10.4.4下载,其他的均可以在最新的的版本macOS 10.15中检索到

一、首先下载源码https://opensource.apple.com/tarballs/objc4/,选择最新版本下载
二、编译源码

以下是源码报错问题及解决办法:

1、unable to find sdk 'macosx.internal'

选择 target -> objc -> Build Settings -> Base SDK -> 选择 macOS 【target中的 objc 和 obc-trampolines都需要更改】
2、文件找不到的报错问题
【1】'sys/reason.h' file not found
  • 在该路径下https://opensource.apple.com/source/xnu/xnu-6153.11.26/bsd/sys/reason.h,下载reason.h文件

  • 在 objc4-781 的根目录下创建WZCommon文件,同时在WZCommon文件夹中创建sys文件,将reson.h文件拷贝到sys文件中
  • 设置文件检索路径:选择 target -> objc -> Build Settings,在工程的 Header Serach Paths 中添加搜索路径 $(SRCROOT)/WZCommon
    【2】'mach-o/dyld_priv.h' file not found
  • 在WZCommon文件中创建mach-o文件

  • 找到下图中的文件
  • 将文件拷贝到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
  • 运行,lock_private.h dyld_priv.h中报错,去掉 bridgeos(3.0)

    【3】'os/lock_private.h' 、' base_private.h 'file not found
  • 找到下图文件
  • 在WZCommon中新建文件夹os,将lock_private.h、base_private.h拷贝进去

  • 运行,lock_private.h中报错,去掉bridgeos(4.0)
    【4】'pthread/tsd_private.h' file not found 和 'pthread/spinlock_private.h' file not found

  • 找到下图文件
  • 在WZCommon中新建文件夹pthread,将tsd_private.h、/spinlock_private.h拷贝进去

【5】'System/machine/cpu_capabilities.h' file not found

  • 找到下图文件
  • 在WZCommon中新建文件夹System/machine,将cpu_capabilities.h拷贝进去
    【6】os/tsd.h' file not found
  • 找到下图文件
  • 找到WZCommon中的os文件夹,将tsd.h拷贝进去
    【7】'System/pthread_machdep.h' file not found
  • 在这个地址https://opensource.apple.com/source/Libc/Libc-583/pthreads/pthread_machdep.h下载pthread_machdep.h文件
  • 将pthread_machdep.h拷贝到WZCommon中的System文件夹
  • 运行报错,可将报错部分隐藏

【8】'CrashReporterClient.h' file not found

  • 在该地址https://opensource.apple.com/source/Libc/Libc-825.24/include/CrashReporterClient.h下载CrashReporterClient.h文件
  • 将CrashReporterClient.h文件拷贝到WZCommon
    文件夹中
  • 运行报错,需要在 Build Settings -> Preprocessor Macros 中加入:LIBC_NO_LIBCRASHREPORTERCLIENT
  • 运行仍然报错,在BuildSetting --> Other Linker Flags 中去掉CrashReporterClient

    【9】'objc-shared-cache.h' file not found

  • 找到下图文件
  • 将objc-shared-cache.h文件拷贝到WZCommon中

    【10】 Mismatch in debug-ness macros
  • 注释掉报错的那句话
    【11】'_simple.h' file not found
  • 找到下图中的文件
  • 将_simple.h文件拷贝到WZCommon中
    【12】'kern/restartable.h' file not found
  • 找到下图中的文件
  • 在WZCommon中新建文件夹kern,将restartable.h拷贝进去
    【13】'Block_private.h' file not found
  • 找到下图中的文件
  • 将Block_private.h文件拷贝到WZCommon中
3、libobjc.order 路径问题

can't open order file: /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk/AppleInternal/OrderFiles/libobjc.order

  • 在工程的 Order File 中添加搜索路径 $(SRCROOT)/libobjc.order
4、Xcode 脚本编译问题

/xcodebuild:1:1: SDK "macosx.internal" cannot be located.

  • 把脚本文本 macosx.internal 改成 macosx

接下来就可以编译调试了!

新建Target
新建WZPreson,在main中调用,进行源码调试

相关文章

网友评论

      本文标题:iOS底层原理02 -- objc4-781源码编译&调试

      本文链接:https://www.haomeiwen.com/subject/thfcwktx.html