Building Apple Swift on ppc64le for ubuntu 16.04 - swift

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

Related

XCTestManifests in generated swift package

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.

Swift Package Manager (package successfully added, but Module not found)

I'm new in Swift. I want to create iOS app that can connect to PostgreSQL database. First I found library https://github.com/vapor/postgresql.git that should be added to my project via Swift Package Manager. Using tutorial I added required library to my project successfully (File -> Swift Packages -> Add Package Dependency):
list of added packages from SPM
But when I try to import this module into my view controller, Xcode shows error that module is not found:
not found
I tried several times to rebuild my project, created new project just for testing this issue. Also I found information about build phases and added this lib as a dependency:
build phases
But I still get error: "No such module PostgreSQL".
Can anyone help me?
I found solution by myself. I compiled C static library "libpq" (can be found in PostgreSQL sources) and added it to my swift project. Included this library by adding special bridging header file. Finally I got what I wanted.
P.S. If someone what to repeat, he or she should know: static C library must be compiled for iOS device architecture (and also in iOS simulator architecture which differs from iOS device arch.).

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

"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.

Build and guard against missing library using Objective-C

I have a library (lib.a) and a header file (lib1.h). The problem is the library is too big and only a relatively small subset of users need it.
Would it be possible to build my Xcode project without the library? Currently I get:
ld: library not found for -llib
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Would it be possible to guard against usage of the library not existing inside the Xcode project? I found the following, but I'm not sure if it's sufficient (assuming I can get the project to build without the lib):
if ([MyLibABC class]) { ... } else { NSLog(#"Add Lib.a"); }
Side note:
In Java I would use reflection to call methods implemented in the library (lib.jar) and if the library is not present I would catch the ClassNotFoundException and show an error message or something (lib.jar missing, please consult the documentation).
I'm targeting >= iOS5.
Unlike desktop operating systems which support dynamic libraries, iOS supports only static libraries. The only way around this would be building two targets - one that uses the library, and the other one that does not.
You can build both targets from the same set of sources by using conditional compilation. In the version that conditionally compiles out the references to the "big library" there would be no references to it, so the linker would not complain about missing references.