XCTestManifests in generated swift package - swift

I am currently creating a Swift Package. By default, the package generated by Xcode contains the XCTestManifests file with the following code:
#if !canImport(ObjectiveC)
public func allTests() -> [XCTestCaseEntry] {
return [
testCase(TestPkgTests.allTests),
]
}
#endif
As you can see, this is the function that returns all test cases. However, I am curious when it will be called and in what case the condition !CanImport (ObjectiveC) will be satisfied.

This is needed for running tests on platforms other than macOS.
On macOS the test runner relies on the ObjectiveC runtime which is absent on other platforms, hence canImport(ObjectiveC) is false. The allTests() function is called from LinuxMain.swift file, you can generate it on macOS by running swift test --generate-linuxmain.
In newer SwiftPM releases there is an alternative option for running tests on those platforms: swift test --enable-test-discovery. You can read more about it here: https://forums.swift.org/t/pitch-enable-test-discovery-by-default/36619

Swift 5.4: Automatic Test Discovery Included
Swift 5.4 (April 26, 2021 release) includes test discovery by default.
Automatic test discovery is now the default on all platforms
swift test --enable-test-discovery is no longer needed.
The Swift 5.4+ swift package init template does not generate allTests and XCTestManifests.swift.

Related

How to expose an Objective-C Module properly using Bazel?

I'm trying to build RxSwift using Bazel in order to be able to use it in a dummy project as a dependency, for the sake of getting to know it a bit.
We don't use the latest RxSwift version at the moment, but version 4.4.2.
After reading through the docs, I've come to the point where I successfully add the specific release to my workspace as an http_archive, and supply my own BUILD file to build that external dependency RxSwift.
You can check out the BUILD file and WORKSPACE file here:
https://gist.github.com/daneov/487444109c703087862d830a3445ee86
Running
bazel build #rx_swift//:RxSwift
yields a Swift compilation error:
No such module: RxAtomic
So, this shows that my I'm missing something with regards to exposing the Objective-C module to a Swift library.
I currently supply these options to the objc_framework:
enable_modules = 1,
alwayslink = 1,
module_name = "RxAtomic"
Thanks in advance for any thoughts/pointers!
You should call bazel build with --experimental_objc_enable_module_maps option

Building Apple Swift on ppc64le for ubuntu 16.04

We are trying to build Apple Swift on ppc64le Ubuntu16.04. We built it on ppc64le using a build script at the following link:- https://github.com/ppc64le/build-scripts/blob/60b7885f4f0915a8671b3cfb86ddbb65f9b988a8/swift/swift41_ubuntu_16.04.sh
With this we were able to run the 'swiftc' and REPL environment for some basic "Hello World" type of code. However for complex codes, like import Foundation, it fails(probably due to Package manager issues). When we use "import Foundation" with swift, compile error occurs as below is seen:
(swift) import Foundation
<REPL Input>:1:8: error: no such module 'Foundation'
import Foundation
^
We are trying to build Swift 4.2(since Swift v4.1 has been released now and no further development is happening on this branch.) on Power8/LE (ppc64le) using the build-toolchain, in order to build the Swift package manager and other tools.
We are seeing a crash which is traceable till following function in HeapObject.cpp file:
static HeapObject *_swift_retain_(HeapObject *object) {
SWIFT_RT_TRACK_INVOCATION(object, swift_retain);
if (isValidPointerForNativeRetain(object))
object->refCounts.increment(1);
return object;
}
Beyond this debugging using simple print statements is tricky and the code flow is unclear. Breakpoints cannot be set since building code in debug mode is not possible as it runs out of resources and hangs. Any help here would be greatly appreciated. Any more outputs, errors can be shared.
Looking forward to port Apple Swift on Ubuntu16.04.
Regards,
Sarvesh Tamba

Framework method crash in nesting framework

I have my custom framework, let's call it TestFramework, and project, in which I want to include that framework, lets call it TestProject.
TestFramework use libraries, such as Alamofire, which I added to TestFramework using Carthage. When I added TestFramework to TestProject, the project wouldn't compile until I added Alamofire (and the other libraries that exist in TestFramework) using Cocoapods to TestProject. This worked until I updated Xcode to version 10. Now, when I try to access libraries methods in TestFramework, I get this:
No error description provided, and I am using latest version of Alamofire.

How to determine configuration in Swift code using Swift Package Manager

I'm creating an application using Swift Package Manager and I need to know the configuration of which the project was built under, i.e Debug or Release. I'm trying to stay away from using the .xcodeproj file. Please, someone let me know if this is possible. I'll paste a simple example below, but just know if there's a better way to handling configuration besides the code below, please submit as answer.
Example:
// main.swift
#if DEBUG
print("Im in Debug!!")
#else
print("Im in Release!!")
#endif
As of Swift 3.1, the Package Manager doesn't offer the option to customize build settings in the Package.swift file (this feature is part of the team's roadmap for Swift 4). However, you can use the -Xswiftc flag to pass custom build settings to the compiler when you invoke swift build or swift test.
To set the DEBUG flag in debug mode, invoke swift build like this:
swift build --configuration debug -Xswiftc "-D" -Xswiftc "DEBUG"
And for release builds you would do the usual:
swift build --configuration release
There is also -Xlinker and Xcc to pass flags to the linker and C compiler, respectively.

"Module was not compiled for testing" error when using Swift Package Manager

I created a Swift library with swift package init --type library and generated an Xcode project with swift package generate-xcodeproj.
Now I'm trying to run the Test scheme in Xcode. It prints following error:
Module '<title>' was not compiled for testing
However when I run swift build and swift test in terminal, it works fine.
I have ENABLE_TESTABILITY set to YES in all of the targets. I didn't change anything in the project except this. How can I make Xcode perform unit testing?
You need to set the "Enable Testability" to Yes in build setting over your "Main Target"
I was having this issue today, it seems like #testable cannot be used with projects generated by Swift Package Manager.
Removing #testable from my import statements solved this issue. Of course, this means we can only test the public interface of our modules.
Xcode -> Product -> Scheme -> Edit Scheme
Select the Info tab.
Set the following -
Build Configuration: Debug
Add a check mark to Debug executable
Tested with Xcode 12.4(12D4e) and iOS 14.1 deployment target.