在项目开发中,我们很少能进到底层看iOS源码,这样感觉不是很爽,那么我们有什么办法可以看到源码呢?
其实在https://opensource.apple.com/tarballs/objc4/
可以看到objc的源码,这里我们就开始最新的objc4-781的源码编译与调试。
准备工作
- Mac版本系统macOS Catalina 10.15.7
- Xcode 12.2
- objc4-781源码(https://opensource.apple.com/tarballs/objc4/)
首先直接编译objc源码
此时会出现问题1Showing All Messages unable to find sdk 'macosx.internal',选择Build Setting ->Base SDK 为macOS。
maxosx错误.png
再编译调试,报错问题2sys/reason.h' file not found
缺少reason.h.png
这是缺少objc4的依赖库。
- 下载地址为https://opensource.apple.com/release/macos-10156.html。
- 定位reason.h
reason.h site:opensource.apple.com
下载xnu文件
解压后
新建一个group->sys,添加复制上图中的reason.h文件。
配置文件路径:target->objc->System Header Search Paths添加$(SRCROOT)/LostHeader
配置reason文件路径.png
接下来再运行,发现报错问题3mach-o/dyld_priv.h' file not found
再使用dyld_priv.h site:opensource.apple.com定位这个文件是在那个文件夹的中
dyld_priv.h
表示在dylb文件中,此时先进入https://opensource.apple.com
点击进去来到https://opensource.apple.com/release/macos-10156.html
搜索dylb
下载解压
dyld-750.6文件搜索
dyld_priv.h文件找到此文件,复制拷贝到LostHeader->mach-o文件中
运行会报错问题4
问题4.png
bridgeos(3.0)好像与搭载了T2 芯片有关,但是目前只有 Pro 和 2018 款 Pro 搭载了 T2 芯片,可以把这相关的代码屏蔽掉。
再运行报错问题5
os/lock_private.h' file not found此刻就按照上面出现
mach-o/dyld_priv.h' file not found这样的错误,定位文件lock_private.h,下载对应的文件libplatform-220.100.1,找到lock_private.h复制到LostHeader->os文件中
lock_private
类似的问题:
- 1
sys/reason.h file not found下载对应文件xnu-6153.141.1 - 2
mach-o/dyld_priv.h file not found下载对应文件dyld-750.6 - 3
os/lock_private.h file not found下载对应文件libplatform-220.100.1 - 4
os/base_private.h file not found下载对应文件libplatform-220.100.1
再运行出现问题6还是bridgeos问题,可以直接屏蔽掉。
问题6
问题7报四个错误
问题7
前面三个问题跟前面问题6屏蔽相关联,目前先屏蔽后面再看看有什么报错没有。
再运行出现pthread/base_private.h file not found,这个直接在https://opensource.apple.com/release/macos-10156.html搜索libdispatch
下载libdispatch-1173.100.2找到private->tsd_private.h复制粘贴到objc源码中。
运行报错 'System/machine/cpu_capabilities.h' file not found。
此时可以在本地电脑搜索下cpu_capabilities.h,找到文件。
复制添加到objcLostHeader->System->machine中。再运行.
报错'os/tsd.h' file not found
在xnu-6153.141.1->libsyscall->os中可以找到
复制添加到objcLostHeader->os中。再运行.
报错'pthread/spinlock_private.h' file not found
在libpthread-416.100.3->private中可以找到
复制添加到objcLostHeader->pthread中。再运行.
报错''System/pthread_machdep.h' file not found
找pthread_machdep.h,下载地址https://opensource.apple.com/tarballs/Libc/。 此时最新的Lib中没有pthread_machdep.h文件,需要下载583这个的Lib
在Libc->pthreads找到文件,
control+c,然后control+v到objcLostHeader->System中
16079261593571.jpg
再运行报错
'CrashReporterClient.h' file not found
下载地址https://opensource.apple.com/tarballs/Libc/。 此时最新的Lib中没有CrashReporterClient.h文件,需要下载Libc-825.24
在Libc-825.24中找到include找到CrashReporterClient.h
control+c,然后control+v到objc
LostHeader中。
此时运行,报错'objc-shared-cache.h' file not found
在dyld-750.6中include中找到objc-shared-cache.h
control+c,control+v到objc中LostHeader文件里面
接着运行,报错三个错误
Typedef redefinition with different types ('int' vs 'volatile OSSpinLock' (aka 'volatile int'))Static declaration of '_pthread_getspecific_direct' follows non-static declarationStatic declaration of '_pthread_has_direct_tsd' follows non-static declaration
把三个部分注释掉,一个自定义类型,两个函数都注释完全。别漏掉,不然会报错哟。
再运行,报错'_simple.h' file not found
在之前下载的libplatform文件中private可以找到
control+c,control+v到objc中LostHeader文件里面
再运行,报错'kern/restartable.h' file not found' file not found
在之前下载的xnu-6153.141.1文件中osfmk->kern可以找到
control+c,control+v到objc中LostHeader->kern文件里面
再运行,报错Mismatch in debug-ness macros
注释掉#error mismatch in debug-ness macros
再运行,报错'Block_private.h' file not found
链接地址https://opensource.apple.com/release/macos-10156.html。搜索libclosure
下载libclosure-74文件
control+c,control+v到objc中LostHeader文件里面
再运行报错ld: can't open order file: /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.0.sdk/AppleInternal/OrderFiles/libobjc.order
targets → Build Settings → Order File 添加 $(SRCROOT)/libobjc.order
再运行报错library not found for -lCrashReporterClient
在build Settings -> Other Linker Flags 删除-lCrashReporterClient
运行,报错'_static_assert' declared as an array with a negative size
注释掉这行。
再运行,出现三个错误
/Users/mac/Desktop/MartinDemo/objc4-781/xcodebuild:1:1: SDK "macosx.internal" cannot be located./Users/mac/Desktop/MartinDemo/objc4-781/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)/Users/mac/Desktop/MartinDemo/objc4-781/xcrun:1:1: unable to find utility "clang++", not a developer tool or in PATH
第一个错误target->Build Phases -> Run Script 将macosx.internal 改成 macosx
解决以上问题,再运行,没有报错了,接下来可以添加下main.m文件编译调试下。
编译调试
添加MTObjc运行端
MTObjc绑定关系 MTObjc->Build Phases-> Dependencies,绑定objc
16079428223825.jpg
Link Binary With Libraries → libobjc.A.dylib
添加main.m文件
再运行,就成功啦。
所需要依赖下载的项目
- xnu-6153.141.1
- libpthread-416.100.3
- libplatform-220.100.1
- libdispatch-1173.100.2
- libclosure-74
- Libc-825.24
- Libc-583
- dyld-750.6













网友评论