i already faced that problem quite a few times…and it drives me crazy…
it seems like it is impossible to create a xcode project which is intended to build a static library for kernel extension projects. for me this is crucial as i see a high demand to modularise my projects in a efficient way.
so i tried a lot, i read all the documentation but i was not able to figure out how it can be done in xcode directly. and sadly i have to admit: LOOKS LIKE THERE IS REALLY NO WAY TO DO SO!!!
it was quite depressing to me, as i cannot see any reason not to have such feature in xcode, anyway….
from a coding point of view building static libraries for user mode applications shouldn’t be different than doing the same for kernel mode application.
so i had to start somewhere and i went for a plain, straight forward kext project. if you build it you can have a look at the build command executed in the background. you will see that for kext project two c files are compiled. one is the entrypoint of your kernel extension and the other one is autogenerated stuff build by xcode. as we want to build our own library we don’t care about autogen code so lets have a look at our entry point c file:
clang -x c -arch x86_64 -fmessage-length=0 -fdiagnostics-show-note-include-stack -fmacro-backtrace-limit=0 -nostdinc -std=gnu99 -fmodules -fno-builtin -Wno-trigraphs -msoft-float -O0 -fno-common -mkernel -Wno-missing-field-initializers -Wno-missing-prototypes -Werror=return-type -Werror=deprecated-objc-isa-usage -Werror=objc-root-class -Wno-missing-braces -Wparentheses -Wswitch -Wunused-function -Wno-unused-label -Wno-unused-parameter -Wunused-variable -Wunused-value -Wempty-body -Wconditional-uninitialized -Wno-unknown-pragmas -Wno-shadow -Wno-four-char-constants -Wno-conversion -Wconstant-conversion -Wint-conversion -Wbool-conversion -Wenum-conversion -Wshorten-64-to-32 -Wpointer-sign -Wno-newline-eof -DDEBUG=1 -DKERNEL -DKERNEL_PRIVATE -DDRIVER_PRIVATE -DAPPLE -DNeXT -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk -fasm-blocks -fstrict-aliasing -Wdeprecated-declarations -mmacosx-version-min=10.9 -g -Wno-sign-conversion -iquote /tmp/plainnstraightkext/Build/Intermediates/plainnstraightkext.build/Debug/plainnstraightkext.build/plainnstraightkext-generated-files.hmap -I/tmp/plainnstraightkext/Build/Intermediates/plainnstraightkext.build/Debug/plainnstraightkext.build/plainnstraightkext-own-target-headers.hmap -I/tmp/plainnstraightkext/Build/Intermediates/plainnstraightkext.build/Debug/plainnstraightkext.build/plainnstraightkext-all-target-headers.hmap -iquote /tmp/plainnstraightkext/Build/Intermediates/plainnstraightkext.build/Debug/plainnstraightkext.build/plainnstraightkext-project-headers.hmap -I/tmp/plainnstraightkext/Build/Products/Debug/include -I/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include -I/System/Library/Frameworks/Kernel.framework/PrivateHeaders -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/System/Library/Frameworks/Kernel.framework/Headers -I/tmp/plainnstraightkext/Build/Intermediates/plainnstraightkext.build/Debug/plainnstraightkext.build/DerivedSources/x86_64 -I/tmp/plainnstraightkext/Build/Intermediates/plainnstraightkext.build/Debug/plainnstraightkext.build/DerivedSources -F/tmp/plainnstraightkext/Build/Products/Debug -include /tmp/plainnstraightkext/Build/Intermediates/PrecompiledHeaders/plainnstraightkext-Prefix-fdizaqqfczfgiycmwsbpnwwimbju/plainnstraightkext-Prefix.pch -MMD -MT dependencies -MF /tmp/plainnstraightkext/Build/Intermediates/plainnstraightkext.build/Debug/plainnstraightkext.build/Objects-normal/x86_64/plainnstraightkext.d –serialize-diagnostics /tmp/plainnstraightkext/Build/Intermediates/plainnstraightkext.build/Debug/plainnstraightkext.build/Objects-normal/x86_64/plainnstraightkext.dia -c /tmp/plainnstraightkext/plainnstraightkext/plainnstraightkext.c -o /tmp/plainnstraightkext/Build/Intermediates/plainnstraightkext.build/Debug/plainnstraightkext.build/Objects-normal/x86_64/plainnstraightkext.o
if you execute that command you can in a terminal you will end up with an object file build from your c file!! it is a good point to start. i tried to reduce the command line a bit, so look here:
clang -x c -arch x86_64 -fmessage-length=0 -fdiagnostics-show-note-include-stack -fmacro-backtrace-limit=0 -nostdinc -std=gnu99 -fno-builtin -msoft-float -O0 -fno-common -mkernel -Wno-conversion -Wno-newline-eof -DDEBUG=1 -DKERNEL -DKERNEL_PRIVATE -DDRIVER_PRIVATE -DAPPLE -DNeXT -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk -fasm-blocks -fstrict-aliasing -mmacosx-version-min=10.9 -g -I/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include -I/System/Library/Frameworks/Kernel.framework/PrivateHeaders -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/System/Library/Frameworks/Kernel.framework/Headers -MMD -MT dependencies -c test.c -o test.o
if change your source accordingly you if will work as well. i removed all the warning flags…you can add them again if you need it, but for now this is quite ok.
still we have no static library!!
Ok, what i did then was i create another plain project but this time for static library. it is more or less the same build process beside an additional tool called “libtool” is called. you can craft following line and it will create a static library out of your object file.
libtool -static -arch_only x86_64 -syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk -filelist test.LinkFileList -o test.a
you will realize that the object file is not explicitly declared in the command line. nevermind, take a look at the switch -fileList. it specifies a file which contains all object files ( path + name of object file ) seperated by a new line.
execute that snippet and tada!!!!! you static kernel library is ready to be linked against.
i really hope that it helps some of you guys as I had a lot of trouble with it.
maybe I’m simply to stupid the use the “official” way 😉 but for me this is right now the one and only way to build my static kernel libraries.
i will continue searching a better and post it as soon as I find something.
mitp0sh of [PDX]