Can't compile Swift package with Kitura on Xcode 8 - swift

Trying Kitura from IBM. I made swift package using
swift package init --type executable
then I made xcode proj with
swift package generate-xcodeproj
My Packages.swift file is like this
import PackageDescription
let package = Package(
name: "testSwiftServer",
dependencies: [
.Package(url: "https://github.com/IBM-Swift/Kitura.git", majorVersion: 0, minor: 31)
]
)
and my main.swift file:
import Kitura
let router = Router()
router.get("/") {
request, response, next in
response.send("la la la")
next()
}
Kitura.addHTTPServer(onPort: 8090, with: router)
Kitura.run()
I'm using new XCode8. swift --version giving me this
Apple Swift version 3.0 (swiftlang-800.0.46.2 clang-800.0.38)
This works fine when I'm lunching it from command line with swift build
But when I'm trying to Run this from XCode I get error: missing required module 'CCurl'

It seems to be working for me. I followed the steps in a slightly different order, but was able to build and then run. I could see the expected la la la output in the browser.
Generate the SPM structure with the init command
Update Package.swift with your code above
Run the generate-xcodeproj command to create the Xcode project
Open the Xcode project
Updated main.swift with your code above
New step: Update scheme to set the Executable (Product > Scheme > Edit Scheme...)
Run the project
View http://localhost:8090 in browser
Swift version: Apple Swift version 3.0 (swiftlang-800.0.46.2 clang-800.0.38)
Xcode version: 8.0 (8A218a)

Related

How to use Swift Packages in Visual Studio Code?

I'm new with Swift and all this tools.
I've tried to look for guides but they didn't help me. I wanna try to work with packages in VS code.So I decided to add a SwiftyJSON package. This is my Package.swift file:
import PackageDescription
let package = Package(
name: "SwiftyJSON",
dependencies: [
.package(url: "https://github.com/SwiftyJSON/SwiftyJSON.git", from: "4.0.0"),
]
)
And I got an error
error: no such module 'PackageDescription' import PackageDescription
For VS Code I use an extension Swift and package dependencies.
It automatically detects a package but when I save my Package.swift file terminal gives me an error:
> Executing task: swift package resolve <
'swiftpackages': error: the manifest is missing a Swift tools version specification;
consider prepending to the manifest '// swift-tools-version: 5.6' to
specify the current Swift toolchain version as the lowest Swift version
supported by the project;
if such a specification already exists, consider moving it to the top
of the manifest, or prepending it with '//' to help Swift Package
Manager find it
The terminal process "/opt/homebrew/bin/zsh '-c', 'swift package
resolve'" failed to launch (exit code: 1).
How I can add packages in VS code?

How do you build iOS specific packages using SPM?

I am using Swift Package Manager that doesn't having a xcodeproject file associated with it and I get an error when building through he terminal. When I call the swift build command I get an error that the MacOS build failed. The package I'm building doesn't support MacOS (It uses UIKit), but only iOS. I can't figure out a way to call the command to only specify that the build is targeted for iOS. I've Google searched around with no luck. Does anybody know the correct syntax if it exists to build an SPM package for iOS from the terminal?
The version of Swift I'm using is: "Apple Swift version 5.2.4 (swiftlang-1103.0.32.9 clang-1103.0.32.53)"
I have specified the platform in the Package.swift file
let package = Package(
name: "Package",
platforms: [.iOS(.v10), ],
products: [
...
I have created a sample project on github https://github.com/mike011/Swift-Package-Manager-Example. When I run swift build it fails with
/git/Swift-Package-Manager-Example/Sources/Swift-Package-Manager-Example/iOSSpecificFile.swift:9:8: error: no such module 'UIKit'
import UIKit
^
/git/Swift-Package-Manager-Example/Sources/Swift-Package-Manager-Example/iOSSpecificFile.swift:9:8: error: no such module 'UIKit'
import UIKit
^
I "solved" this by wrapping all my files in #if !os(macOS) #endif blocks. So the package builds on a Mac, but it doesn't have any content.
SwiftPM doesn't currently have a way to disallow building for a specific platform but if you want you can take advantage of the minimum build version to cause compile time errors on platforms you don't support.
For example if you don't want to allow building on macOS you can use the platform version: .macOS("99.0") in your platforms section of your manifest and you will be given the compilation warnings and errors similar to this when building in Xcode:
The macOS deployment target 'MACOSX_DEPLOYMENT_TARGET' is set to 99.0, but the range of supported deployment target versions is 10.8 to 10.16.99
Invalid Darwin version number: macos99.0
Invalid version number in 'target x86_64-apple-macos99.0'

Swift Package Manager executable app, set deployment target

I'm using Swift Package Manager to create a macOS executable. When I use things that aren't available in all macOS versions I get compile errors. Two big examples are URL(fileURLWithPath: filePath, relativeTo: directoryToSearch) and url.hasDirectoryPath.
When building with swift build I get error: 'init(fileURLWithPath:relativeTo:)' is only available on OS X 10.11 or newer errors. I don't care about any old OS versions, as it's just a personal tool. How can I set the deployment target to be 10.14 so I don't have to sprinkle checks all through out my code?
I found https://hirschmann.io/swift-package-manager/ which talks about this issue. However it's solution is creating an xcconfig file with the deployment target set and using swift package generate-xcodeproj --xcconfig-overrides ./main.xcconfig to apply it to the generated Xcode project. While it does work, it only works for the Xcode project, so if I just want to do swift build to get the free standing executable to use outside of Xcode then I can't.
My package file was auto generated by swift package init --type executable and hasn't been changed.
// swift-tools-version:4.2
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "swift_converter",
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
.target(
name: "swift_converter",
dependencies: []),
.testTarget(
name: "swift_converterTests",
dependencies: ["swift_converter"]),
]
)
This may not help you right now, but the upcoming Swift 5.0 will include the ability to specify the deployment target in the package manifest, using a syntax like this:
...
platforms: [
.macOS(.v10_13), .iOS(.v12),
],
...
(The same is true for some other common build settings.)
Until then, you can override the default deployment target via command line arguments like this:
$ swift build -Xswiftc "-target" -Xswiftc "x86_64-apple-macosx10.14"
You'll have to include these arguments in every call to swift build, swift run, swift test.

`swift package generate-xcodeproj` fails with "Swift does not support the SDK"

I'm having trouble running swift package generate-xcodeproj. I created my package like this:
$ /Applications/Xcode9.4.1.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift package init --type executable
(as I have many Xcode versions installed I explicitly targeted a swift binary when running the command so that I don't need to xcode-select all the time)
This created a Package.swift with the version header // swift-tools-version:4.0.
Now, when I run swift package generate-xcodeproj I get a fatal error:
$ /Applications/Xcode9.4.1.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift package generate-xcodeproj
/Users/max.chuquimia/Desktop/xcode/MyPackage: error: manifest parse error(s):
<unknown>:0: error: Swift does not support the SDK 'MacOSX10.12.sdk'
No .xcodeproj is generated. Why is this occurring?
It seems the problem is the $DEVELOPER_DIR environment variable is wrong - it should also be made to point to the Xcode version that the swift binary resides in.
$ DEVELOPER_DIR=/Applications/Xcode9.4.1.app/Contents/Developer /Applications/Xcode9.4.1.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift package generate-xcodeproj

Swift Package Manager Not able to resolve Kitura version above 1.4

I am trying with a sample Swift on server (using Kitura) app and using swift package manager to resolve the dependencies.
Here is my sample Package.swift file.
import PackageDescription
let package = Package(
name: "Kitura_Swift_Server",
dependencies: [
.Package(url: "https://github.com/IBM-Swift/Kitura.git", majorVersion: 1, minor: 4)
])
My problem is if I change the minor version of Kitura framework to anything above 4, I get the error error: unsatisfiable. Although 1.7.1 is the latest Kitura version but swift build is not resolving it.
Anyone have any idea why it could behave so?
I've seen this issue before when you've already run swift build and the .build directory is created, then you change something in your Package.swift. That said, I've had no issue using Kitura 1.7.x as my lone dependency.
I recommend you change your Kitura version to 1.7.x and run swift package update (in Swift 3.1.1) if you want your dependencies updated.
If that doesn't work, it doesn't hurt to start fresh and delete the .build directory, then run swift build.