问题
I'm building a Mac Desktop/Cocoa Application to archive my iOS project, the core implementation uses NSTask with xcodebuild command, followed by raywenderlich's this guide.
The root cause is the CoreData model file compile, I used this app to build my another project which didn't contain any CoreData files and no issue there. More interesting comparison is, I tried the same xcodebuild command in my terminal and it just succeed!
Then I start to diff the two xcodebuild output and found some differences,
In terminal,
DataModelCompile /Users/hanwei/Library/Developer/Xcode/DerivedData/youYue3xMaster-cjatyqmgblwyoyccarejxlozikdi/Build/Intermediates/ArchiveIntermediates/youYue3xMaster/InstallationBuildProductsLocation/Applications/youYue3xMaster.app/ youYue3xMaster/LogicBook.xcdatamodeld
cd /Users/hanwei/work/DragonSource/projects/ios_youyue3_xmaster/youYue3xMaster
export PATH="/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin:/Applications/Xcode.app/Contents/Developer/usr/bin:/opt/subversion/bin:/usr/local/bin:/opt/subversion/bin:/usr/local/bin:/opt/subversion/bin:/usr/local/bin:/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/git/bin"
/Applications/Xcode.app/Contents/Developer/usr/bin/momc --sdkroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS9.3.sdk --iphoneos-deployment-target 7.0 --module youYue3xMaster /Users/hanwei/work/DragonSource/projects/ios_youyue3_xmaster/youYue3xMaster/youYue3xMaster/LogicBook.xcdatamodeld /Users/hanwei/Library/Developer/Xcode/DerivedData/youYue3xMaster-cjatyqmgblwyoyccarejxlozikdi/Build/Intermediates/ArchiveIntermediates/youYue3xMaster/InstallationBuildProductsLocation/Applications/youYue3xMaster.app/
In my cocoa app's log
DataModelCompile /Users/hanwei/Library/Developer/Xcode/DerivedData/youYue3xMaster-cjatyqmgblwyoyccarejxlozikdi/Build/Intermediates/ArchiveIntermediates/youYue3xMaster/InstallationBuildProductsLocation/Applications/youYue3xMaster.app/ youYue3xMaster/MagazineModel.xcdatamodeld
cd /Users/hanwei/work/DragonSource/projects/ios_youyue3_xmaster/youYue3xMaster
export PATH="/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin:/Applications/Xcode.app/Contents/Developer/usr/bin:/Applications/Xcode.app/Contents/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin"
/Applications/Xcode.app/Contents/Developer/usr/bin/momc --sdkroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS9.3.sdk --iphoneos-deployment-target 7.0 --module youYue3xMaster /Users/hanwei/work/DragonSource/projects/ios_youyue3_xmaster/youYue3xMaster/youYue3xMaster/MagazineModel.xcdatamodeld /Users/hanwei/Library/Developer/Xcode/DerivedData/youYue3xMaster-cjatyqmgblwyoyccarejxlozikdi/Build/Intermediates/ArchiveIntermediates/youYue3xMaster/InstallationBuildProductsLocation/Applications/youYue3xMaster.app/
dyld: Symbol not found: _OBJC_CLASS_$_OS_object
Referenced from: /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator9.3.sdk/usr/lib/system/libxpc.dylib
Expected in: /usr/lib/system/introspection/libdispatch.dylib in /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator9.3.sdk/usr/lib/system/libxpc.dylib
2016-06-17 09:46:28.158 momc[76594:13533677] *** -[NSKeyedUnarchiver initForReadingWithData:]: data is empty; did you forget to send -finishEncoding to the NSKeyedArchiver?
cdtool invalid hashes:
/Users/hanwei/work/DragonSource/projects/ios_youyue3_xmaster/youYue3xMaster/youYue3xMaster/MagazineModel.xcdatamodeld/MagazineModel.xcdatamodel:: error: cdtool cannot compile
The main error log detail could be found in later part, the diff result is "export PATH=...". In terminal, the PATH contains the current user's ${PATH}, I through that should be the final cause, but no luck there after trying to export the same path to my cocoa app's shell script.
Here is my shell script,
command -v xcodebuild >/dev/null 2>&1 || { echo >&2 "It requires xcodebuild but it's not installed. Aborting."; exit 1; }
#export PATH=$PATH:/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin:/Applications/Xcode.app/Contents/Developer/usr/bin:/opt/subversion/bin:/usr/local/bin:/opt/subversion/bin:/usr/local/bin:/opt/subversion/bin:/usr/local/bin:/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/git/bin
#PATH=$PATH:/opt/subversion/bin:/usr/local/bin:/opt/subversion/bin:/usr/local/bin:/opt/subversion/bin:/usr/local/bin:/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/git/bin
echo ${PATH}
type xcodebuild
type xcpretty
if [ -x "$(command -v xcpretty)" ]; then
xcodebuild -workspace "${1}" -scheme "${2}" -sdk iphoneos -destination generic/platform=iOS -archivePath "${3}" archive | xcpretty
else
echo "xcpretty is not installed, suggest to install it and try again.";
xcodebuild -workspace "${1}" -scheme "${2}" -sdk iphoneos -destination generic/platform=iOS -archivePath "${3}" archive
fi
For the error "dyld: Symbol not found: _OBJC_CLASS_$_OS_object...", I googled it and found that _OBJC_CLASS_$_OS_object symbol built from libdispach, libdispatch should be built in Foundation.framework, then I tried to link Foundation.framework in my project explicitly, the same error exists.
For the "Referenced from:...Expected in:..."* statement, it references to iPhone simulator, then I guess there maybe something wrong about xcodebuild -destination, passing generic/platform=iOS or my iOS device (references from this cheetsheet) doesn't work, either.
For "error: cdtool cannot compile", I found this big question related in Xcode bug, .xcdatamodel: cdtool cannot compile, I tried the rename solution and no luck, that question background is for Xcode beta version.
Currently my environment is Xcode 7.3.1 (7D1014), 10.11.5 (15F34).
I've focus on this issue over 8 hours, If you have some good suspect points or solutions, please help me, great thanks in advance!
BTW, I found @MichalMoskala encountered the same problem and no solution there, Calling xcodebuild from Swift command line tool fails. He didn't statement the background it happened in Xcode-beta version.
If there is really no final solutions, I have to decide to transit CoreData to FMDB/Realm.
UPDATE
SOLUTION
After several days' investigation, I didn't find a real final solution for this, it should be bug in Xcode when compiling xcdatamodeld file in xcodebuild by hand. But why not appear in Xcode IDE? Maybe Apple developers made some patches in Xcode.
Here is my solution, copy the compiled xcdatamodeld files (*.momd) to project, remove the *.xcdatamodeld file from Build Phrases - Compile Sources in project, so that they will not be compiled when building/archiving. Add custom script to copy the .momd backup files to product directory, it's executed before Compile Sources, just like the build progress in Xcode.
Here is my script,
cp -r ${SRCROOT}/CoreDataFiles/*.momd ${BUILT_PRODUCTS_DIR}/${FULL_PRODUCT_NAME}/
Validation passed!
来源:https://stackoverflow.com/questions/37872557/xcodebuild-cdtool-cannot-compile-datamodelcompile-path-to-coredatamodel-xcdat