How to compile Swift programs for testing on the command line - swift

I am trying to set up a Swift project on the command line, preferably for both OS X and Linux. I managed to compile Swift source to executable binaries, dynamic libraries and Swift modules on OS X. Now I am trying to get testing to work by using the XCTest framework. When I am trying to compile the testing module, the compiler tells me that my module was not "compiled for testing". While there are a lot of hints about "enabling testability" in Xcode, I couldn't find any pointers about doing this with the Swift command line compiler. So, how can do this?

Related

Armadillo C++ library - C++11 compiler required error

I'm using MacOS 10.15.7 with Xcode 12.4, I want to use Armadillo library in my iOS Swift project. So first I installed Armadillo through Homebrew.
Armadillo was installed in path /usr/local/Cellar/armadillo/
I found header files in path /usr/local/Cellar/armadillo/10.5.1/include and library files in path /usr/local/Cellar/armadillo/10.5.1/lib
In Xcode project, Build settings I provided the above paths in Header Search path and Library search path.
I'm getting below three errors.
/usr/local/Cellar/armadillo/10.5.1/include/armadillo_bits/compiler_check.hpp:50:4: error: "*** C++11 compiler required; enable C++11 mode in your compiler, or use an earlier version of Armadillo"
/usr/local/Cellar/armadillo/10.5.1/include/armadillo:23:10: error: 'cstdlib' file not found #include <cstdlib>
<unknown>:0: error: failed to emit precompiled header
Not sure how to proceed.. Any suggestions ???
You will not be able to do this easily. Swift does not yet support C++ interop, but this is a long term goal. You can read about in the C++ Interop Manifesto in the Swift git repo. The best you will be able to do right now is write an extern "C" wrapper in C++ around any C++ functions you want to call, and then import your wrapper into Swift. Since you're using an Xcode project, I would recommend trying something like this.
Alternatively, depending on what you need Armadillo for, you might be able to by away without it. If you just need to do linear algebra, Apple includes LAPACK (see here) and BLAS (see here) inside Accelerate, which is available on all Apple platforms without installing anything. This might even be familiar to you because Armadillo does its matrix decompositions through LAPACK. There's also Quadrature (see here) if you are looking for integration. I've had good experiences with each of these.

How to build a Swift program as a static executable?

I do not have any familiarity with the Swift Package Manager, but I have to compile a Swift program (https://github.com/ltentrup/SafetySynth, if it matters) as a statically linked executable (on Linux).
First of all. Is it possible?
The project uses the Swift Package Manager to build the executable, through the swift build command.
Is there any command line flags to tell the SPM to make a static executable?
This question specifically concerns the Swift Package Manager, because I'm on Linux, so I'm not using Xcode (and the project does not use Xcode neither).

How can I build and test a Swift/C library from the Linux command line

I have Swift code that wraps a C-library.
I want to build and test it under Linux.
How should I build the library, associated tests and run the tests?
I was tempted by the idea of creating a Swift Package but I'm not sure if I can use a C-library as part of the package if I do that.

Using distutils to compile plain C code with unmangled output name

I have a little benchmark suite, which compares different approaches to writing and executing some simple toy Python code, mainly for the purpose of illustrating Cython.
One of the approaches I used to have, was to write the test function in pure C, compile it with a C compiler (getting distutils to find and invoke the compiler for me) then load the library into Python with ctypes, and execute the function.
This worked swimmingly until distutils started embedding the python version and the platform in the name of the file it generates.
In the past I could persuade distutils to compile a C source file called C.c into a library called C.so in the working directory: recent versions of distutils insist on mangling the output name into something of the form build/temp.linux-x86_64-3.6 or (depending on exactly what distutils features I use) some other variaton on that theme.
Is there any way to persuade current versions of distutils to simply call the output C.so in the current working directory? (Alternatively, is there some way to reliably and easily tell ctypes where the library produced by distutils resides?)
[Bonus: How can this idea be expressed portably across Linux, OS X and Windows?]
=================================================================
Edit: for completeness, here is how I used to do it successfully in the past:
pure_C_setup.py:
from distutils.core import setup, Extension
setup(name="ctypes-test",
ext_modules = [Extension("C", ["C.c"])])
With the above setup file, the command python pure_C_setup.py install --install-lib=. used to produce C.so in the working directory; today it produces build/lib.linux-x86_64-3.6/C.cpython-36m-x86_64-linux-gnu.so
Not platform, not even compiler independent, but you might be able to use Extension's extra_link_args to achieve what you want.
For VCPP 2015:
setup(name="ctypes-test",
ext_modules = [Extension("C", ["C.c"], extra_link_args="/OUT:C.pyd")])

Swift Linux. Error: no such module 'Dispatch'

I use Ubuntu 15.10
I compiled swift-corelibs-libdispatch, get file libdispatch.so
But still if I use "import Dispatch" get an error "no such module 'Dispatch'"
How can I add this module to Swift?
One way to accomplish this would be to set up a system module for libdispatch and use swift build. See https://github.com/apple/swift-package-manager/blob/13d682a63ea01246dd119cd4cf5c8d90c030566d/Documentation/SystemModules.md on how to use system modules. This quesion, Importing a Swift module using a C library, may also come in handy.
I'm sure there are other ways, too. You should be able to use swift interpreter or swift compiler (swiftc), but I can't think of a way to do that off the top of my head.