How can I build a Swift Package for iOS over command line? - swift

In Xcode, I can select my destination as a "generic iOS device" or any iOS simulator, and my package will build platform-specific code for ios.
Via command line "swift build" only builds my target for macOS.
I want to build the target for iOS for CI purposes. The problem with building for macOS is that UIKit-specific code won't be built.
For example:
#if canImport(UIKit)
// some invalid code
#endif
The invalid code will not be noticed and will pass the build phase.
Ideally, I could say something like swift build -platform iOS. Is there a way to do something like this?

At time of writing (Feb 16, 2019), a working solution is:
swift build -v \
-Xswiftc "-sdk" \
-Xswiftc "`xcrun --sdk iphonesimulator --show-sdk-path`" \
-Xswiftc "-target" \
-Xswiftc "x86_64-apple-ios13.0-simulator"
This command uses -Xswiftc to workaround the issue by overriding the sdk from macOS to iphonesimulator.
Strictly we add these flags so developers can work around issues, but they also should report a bug so that we can provide a proper solution for their needs.
Source
So I'm guessing there will be a more elegant solution in the future.

Starting with Xcode 11, xcodebuild supports SwiftPM packages out of the box.
An example invocation would look like this:
xcodebuild -scheme Foo \
-destination 'platform=iOS Simulator,OS=13.5,name=iPhone 11 Pro'
where Foo would be the name of the library product you're trying to build. You can
get the full list of available schemes for you SwiftPM package with xcodebuild -list.
You can get the list of available destinations for a given scheme with this invocation:
xcodebuild -showdestinations -scheme Foo

Related

ObjectMapper: Module compiled with Swift 4.2 cannot be imported in Swift 4.1.2:

I am facing this issue on Xcode 9.4.1
I tried with
github "Hearst-DD/ObjectMapper" ~> 3.1.0 //(tried with 3.3.0)
but this error.
Module compiled with Swift 4.2 cannot be imported in Swift 4.1.2 comes every-time.
I tried below things:
Deleting the .resolved file
Deleted the build and checkout folder
Deleted the framework folder from builds inside cathage
tried with carthage bootstrap --platform iOS.
Updated: Below error keep on coming.
Build Failed Task failed with exit code 65: /usr/bin/xcrun
xcodebuild -workspace
/Users/xx/Documents/tal1.14.1/Carthage/Checkouts/mixpanel-swift/MixpanelDemo/MixpanelDemo.xcworkspace
-scheme Mixpanel -configuration Release -derivedDataPath /Users/xx/Library/Caches/org.carthage.CarthageKit/DerivedData/9.4.1_9F2000/mixpanel-swift/v2.5.4
-sdk iphoneos ONLY_ACTIVE_ARCH=NO CODE_SIGNING_REQUIRED=NO CODE_SIGN_IDENTITY= CARTHAGE=YES archive -archivePath
/var/folders/82/_m5xwhxx4795wyxlhtw40ch509_hqq/T/mixpanel-swift
SKIP_INSTALL=YES GCC_INSTRUMENT_PROGRAM_FLOW_ARCS=NO
CLANG_ENABLE_CODE_COVERAGE=NO STRIP_INSTALLED_PRODUCT=NO (launched in
/Users/xx/Documents/tal1.14.1/Carthage/Checkouts/mixpanel-swift)
This usually indicates that project itself failed to compile. Please
check the xcodebuild log for more details:
/var/folders/82/_m5xwhxx4795wyxlhtw40ch509_hqq/T/carthage-xcodebuild.2MxUDl.log
BLDM3295-MAC:tal xx$
You need to do carthage bootstrap --no-use-binaries to make Carthage compile the frameworks locally, using the Swift 4.2 compiler that your project uses.
To fix what I did, I was having success build in Jenkins Server I kept the the same version and copied the build project and framework for the same. then run
carthage bootstrap platform iOS
I know this something hack, but this is how I am able to solve.

Swift private Framework with backward xcode version compatibility

This is my first framework using Swift language and it's private framework. It's worked as expected until new Xcode 9.3 release. After upgrading newer Xcode got an issue like this Module compiled with Swift 4.0.3 cannot be imported in Swift 4.1.
After that only I realized framework doesn't support auto version (Xcode/Swift) upgrade, Did some search found there is a Universal or Fat Binary Framework single build instead of separate build for real device and simulator which I missed in previously.
UNIVERSAL_OUTPUTFOLDER=${BUILD_DIR}/${CONFIGURATION}-universal
# Make sure the output directory exists
mkdir -p "${UNIVERSAL_OUTPUTFOLDER}"
# Next, work out if we're in SIM or DEVICE
xcodebuild -target "${PROJECT_NAME}" -configuration ${CONFIGURATION} -sdk iphonesimulator ONLY_ACTIVE_ARCH=NO BUILD_DIR="${BUILD_DIR}" BUILD_ROOT="${BUILD_ROOT}" clean build
xcodebuild -target "${PROJECT_NAME}" ONLY_ACTIVE_ARCH=NO -configuration ${CONFIGURATION} -sdk iphoneos BUILD_DIR="${BUILD_DIR}" BUILD_ROOT="${BUILD_ROOT}" clean build
# Step 2. Copy the framework structure (from iphoneos build) to the universal folder
cp -R "${BUILD_DIR}/${CONFIGURATION}-iphoneos/${PROJECT_NAME}.framework" "${UNIVERSAL_OUTPUTFOLDER}/"
# Step 3. Copy Swift modules from iphonesimulator build (if it exists) to the copied framework directory
BUILD_PRODUCTS="${SYMROOT}/../../../../Products"
cp -R "${BUILD_PRODUCTS}/Debug-iphonesimulator/${PROJECT_NAME}.framework/Modules/${PROJECT_NAME}.swiftmodule/." "${UNIVERSAL_OUTPUTFOLDER}/${PROJECT_NAME}.framework/Modules/${PROJECT_NAME}.swiftmodule"
# Step 4. Create universal binary file using lipo and place the combined executable in the copied framework directory
lipo -create -output "${UNIVERSAL_OUTPUTFOLDER}/${PROJECT_NAME}.framework/${PROJECT_NAME}" "${BUILD_PRODUCTS}/Debug-iphonesimulator/${PROJECT_NAME}.framework/${PROJECT_NAME}" "${BUILD_DIR}/${CONFIGURATION}-iphoneos/${PROJECT_NAME}.framework/${PROJECT_NAME}"
# Step 5. Convenience step to copy the framework to the project's directory
cp -R "${UNIVERSAL_OUTPUTFOLDER}/${PROJECT_NAME}.framework" "${PROJECT_DIR}"
# Step 6. Convenience step to open the project's directory in Finder
open "${PROJECT_DIR}"
fi
At Edit Scheme -> Archive -> Post-Action -> Run Script
This time I am used Swift Compiler - Language -> Swift 3.3
And I got expected output this time too in Xcode 9.3 with Swift 4.1
When I try to run the project with this framework at Xcode 9.2 it's show error like this
Module compiled with Swift 4.1 cannot be imported in Swift 3.2.3:
Try to resolve with backward compatibility and I didn't find. How can I run this framework in Xcode 9.2
Framework support: Swift and Objective C, Min OS iOS 8.0
**Update**
Also got something like this error
compiled with newer version of Swift language (unknown ABI version 0x06) than previous files (4.0) file 'MAC location' for architecture armv7
And tried this to my project

xcodebuild command for Xcode 8 beta to set provisioning profile and team ID

I am not able to build a project from command line in the latest Xcode 8 beta version. Can you please let me know the "xcodebuild" commands for Xcode 8 beta version to build a project through command line. previous commands which we used in previous versions of Xcode does not work now. Every time it throws and error : "Check dependencies
Signing for MyTestApp3 requires a development team. Select a development team in the Target Editor.
Code signing is required for product type 'Application' in SDK 'iOS 10.0'"
Command i used to build - xcodebuild -project MyTestApp3.xcodeproj -sdk iphoneos -configuration Release clean build DEBUG_INFORMATION_FORMAT=dwarf-with-dsym DSTROOT=build GCC_GENERATE_DEBUGGING_SYMBOLS=YES SHARED_PRECOMPS_DIR=build OBJROOT=build SYMROOT=build PROVISIONING_PROFILE_SPECIFIER= CODE_SIGN_IDENTITY="iPhone Distribution: Our Distribution Certificate".
Did you try DEVELOPMENT_TEAM command line parameter?
It can also be set in Xcode (General and Build Settings tabs).
I found the solution by this answer!
Mybe you also want to know how to get the 10-digit team id. Well, I found it in the output of command
xcodebuild -showBuildSettings -project <YourProject> |grep DEVELOPMENT_TEAM

Swift fat framework w/Objective-C Cocoapod

I've built a framework in Swift. The framework uses Cocoapods, one of the pods is written in Objective C.
I also use a custom script to make the framework a fat framework so it supports 32/64 bit systems. (This runs in a separate target on the project and I'm wondering if that has something to do with it?)
UNIVERSAL_OUTPUTFOLDER=${BUILD_DIR}/${CONFIGURATION}-universal
if [ "true" == ${ALREADYINVOKED:-false} ]
then
echo "RECURSION: Detected, stopping"
else
export ALREADYINVOKED="true"
// Step 1. Build Device and Simulator versions
xcodebuild -target "${PROJECT_NAME}" ONLY_ACTIVE_ARCH=NO -configuration ${CONFIGURATION} -sdk iphoneos BUILD_DIR="${BUILD_DIR}" BUILD_ROOT="${BUILD_ROOT}" clean build
xcodebuild -target "${PROJECT_NAME}" -configuration ${CONFIGURATION} -sdk iphonesimulator ONLY_ACTIVE_ARCH=NO BUILD_DIR="${BUILD_DIR}" BUILD_ROOT="${BUILD_ROOT}" clean build
//Step 2. Copy the framework structure (from iphoneos build) to the universal folder
cp -R "${BUILD_DIR}/${CONFIGURATION}-iphoneos/${PROJECT_NAME}.framework" "${UNIVERSAL_OUTPUTFOLDER}/"
// Step 3. Copy Swift modules (from iphonesimulator build) to the copied framework directory
cp -R "${BUILD_DIR}/${CONFIGURATION}-iphonesimulator/${PROJECT_NAME}.framework/Modules/${PROJECT_NAME}.swiftmodule/." "${UNIVERSAL_OUTPUTFOLDER}/${PROJECT_NAME}.framework/Modules/${PROJECT_NAME}.swiftmodule"
// Step 4. Create universal binary file using lipo and place the combined executable in the copied framework directory
lipo -create -output "${UNIVERSAL_OUTPUTFOLDER}/${PROJECT_NAME}.framework/${PROJECT_NAME}" "${BUILD_DIR}/${CONFIGURATION}-iphonesimulator/${PROJECT_NAME}.framework/${PROJECT_NAME}" "${BUILD_DIR}/${CONFIGURATION}-iphoneos/${PROJECT_NAME}.framework/${PROJECT_NAME}"
// Step 5. Convenience step to copy the framework to the project's directory
cp -R "${UNIVERSAL_OUTPUTFOLDER}/${PROJECT_NAME}.framework" "${PROJECT_DIR}"
// Step 6. Convenience step to open the project's directory in Finder
open "${PROJECT_DIR}"
fi
When I create this fat framework and put it into a project I'd like to use it on the compiler always fails b/c the project can't see the objective C cocoa pod module.
clang: error: linker command failed with exit code 1 (use -v to see invocation)
ld: framework not found Pusher for architecture x86_64
It is not found for any architecture when I change the build platform.
Any solution where I can produce a framework that I can use in a separate Xcode project (for all iOS or OS X) would be awesome.
You can't run xcodebuild with -target when using CocoaPods. When you use -target, Xcode will only consider the active project and fail to pull in the Pod dependencies, similar to if you just opened the project file in Xcode and tried building.
You should be running xcodebuild -workspace "${PROJECT_NAME}.xcworkspace" -scheme "${PROJECT_NAME}" ..., assuming that CocoaPods generated the workspace and Xcode generated a scheme using the target name. You will also need to make sure your scheme is marked as shared if running this on another device.
Once your framework is built you will need to include it and the frameworks it depends on in apps that will be using it. For your framework, that means including it in General > Embedded Binaries and General > Linked Frameworks and Libraries. For frameworks you depend on, (e.g. AlamoFire), you could instruct users to include it in their Podfile, you could package it and ship it along with your framework, or you could do both and let the user do what works for them.
Apparently you are missing the 64bit architecture for Simulator.
When you build a target from Xcode, depending on what kind of simulator you have selected - the produced library will contains i386 or x86_64 respectively for selected 32bit or 64bit version of the simulator.
I guess that the cli build is producing only i386 version.
You can try to set the architectures in the script:
xcodebuild -target "${PROJECT_NAME}" ARCHS="i386 x86_64" -configuration ${CONFIGURATION} -sdk iphonesimulator ONLY_ACTIVE_ARCH=NO BUILD_DIR="${BUILD_DIR}" BUILD_ROOT="${BUILD_ROOT}" clean build
As an alternative You can try to build by hand using 64bit simulator (iPhone 5S +), then extract the missing architecture and then put it into the final library using lipo command.

How do I catch OCUnit's test fail/success in command line?

My Expect: # Build Successed # and # Test Failed #
Actual Result: only # Build Succeeded #
How do I get fail/success in command line? [iphone, objective-c, xcode4, OCUnit]
I'm newbe of OCUnit and Xcode4. I create project:
https://github.com/sanemat/HogeHoge
This is scafold code by xcode4. I run test in xcode gui, #Test Failed# appear in successfully. In commandline (I want to use jenkins),I use following command.
xcodebuild -target HogeHogeTests -configuration Debug -sdk iphonesimulator4.3 clean build
I see # Build Succeeded # and then completed. I want run test and get it's result. How?
Edited:
I found BPOCUnitXMLReporter. This is for MacOS. I forked and adjusted iOS.
I found BPOCUnitXMLReporter. This is for MacOS. I forked and adjusted iOS.
Try the Text Finder Plugin It allows you to search a log for a string, and set pass or fail based on that.
You can integrate a custom test logger with OCUnit. See my answer here:
How do I trap OCUnit test pass/failure messages/events
what you're looking for is this undocumented argument (you do need sdk and target too) to run your OCUnit Tests from the terminal
xcodebuild -target MyTarget -sdk iphonesimulator TEST_AFTER_BUILD=YES