2

I'm relatively new to Swift (I've tinkered with Python and HTML in the past)- but I'm currently working on an app with a group.

This app already exists as a desktop program written in Ada and we were hoping to port it over to Swift and put out an iPhone app.

My understanding is that I can (likely?) turn the Ada into C with a converter, then reference the C in Swift? Is this correct, or have I overlooked something due to not being familiar with the languages?

If correct, how would I go about actually executing this since I have little experience in Swift and C?

This is a pretty loaded question, so thank you for any help or insight! edit: said desktop application is open source and available for all use- I do not intend on stealing anyone's work

NFIO
  • 21
  • 2
  • The Ada community still has plenty of Mac users, so Gnat (and therefore gcc) are still usable on Mac. Simon Wright's blog has a lot of info https://forward-in-code.blogspot.com/2017/02/gdb-vs-macos-sierra.html How to link with Swift ... don't know of specific resources on that, sorry. –  Jun 02 '18 at 10:47

4 Answers4

2

I'm assuming that you can easily pull out the parts of the Ada program that you want to use in the iPhone program. If the code that you want to port over has other dependencies, then you have to make sure that you can get all of these dependencies for iOS as well. This may be extremely difficult for UI toolkits, if you use one.

To call foreign functions in Swift, you usually need to use a bridging header. Your target's bridging header contains C and Objective-C declarations of classes, functions and variables that Swift should be able to call into. As long as the language that you want to call into can export C-compatible bindings (which should be the case of Ada), then you will be able to call these bindings by declaring them in your bridging header.

If the part of the desktop Ada program that you want to use can easily be isolated in a library (static or dynamic), and you are capable of building that library as an ARM Mach-O library, then it's simply a matter of declaring the functions that you want to use from the library in your app's bridging header. You can refer to GNAT User's Guide for Native Platforms: Interfacing to C for the directives to use to expose an Ada function to C, and by exposing it to C, you're also exposing it to Swift. I'd try this first, as maintaining the almost-unmodified Ada source is almost certainly going to be easier than maintaining a mechanically-translated C version of the Ada source.

If that doesn't work, then yes, translating the Ada source to C code will also allow you to reference it from Swift. You will also have to ensure that the functions that you need to call are declared in your bridging header.

zneak
  • 134,922
  • 42
  • 253
  • 328
  • 1
    To emphasize: the `Interfaces.C` packages and `pragma Export(C, ada_method, "my_service")` are the main tools to achieve C compatibility of an Ada code. I had to do a similar work a while ago with java. The process is similar with Swift (provided you have the correct compiler and the ability to produce .a/.dll as stated above ). You can check how I did it here: https://stackoverflow.com/questions/42938883/interface-ada-dynamic-library-with-java-using-jna-and-adas-interface-c-packages. You could try it with very simple ada lib (hello_world) into your swift, and post it as an answer here – LoneWanderer Jun 06 '18 at 18:32
0

It looks like it used to be possible to compile Ada in Xcode when Xcode still supported the GCC compiler. That was removed a while ago.

If there are translators that will translate Ada to C then you could use C natively in an Objective-C iOS app. (Objective-C is a pure superset of C, meaning that all C code is also legal objective-C.)

Swift "plays nice" with C and Objective-C as long as you limit yourself to the types and classes that C and Objective-C understand, but inter-operating between Swift and C/Objective-C is somewhat tricky and tedious. You would have a fair amount of reading and study to do in order to learn how to build an app that uses both, on top of everything else you would need to learn.

Unless you're dealing with many thousands of lines of Ada code it might be better to find somebody who knows both Ada and Swift to translate the code for you, or for you to learn Swift and then translate it yourself.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
0

Unfortunately, There is, at this time, no Ada compiler which targets iOS available to the public.

If you have cash to spend you could contact AdaCore for one, don’t expect it to be cheap though.

Believe me, there are some of us who would like to target iOS with Ada.

Luke A. Guest
  • 556
  • 5
  • 8
0

I am only aware of two Ada to C compilers, the most powerful one (AdaMagic) is now known as MapuSoft Ada-to-C/C++ changer. You'll need either Windows or Linux execution environment to run translator (Wine, Docker, etc. on macOS). Also, you'll need to limit yourself to Ada 95. AdaMagic is said to support a "subset of Ada 2005", but I am yet to stumble upon this subset. Also you'll need to port the runtime or a part of it. If you disable runtime checks, the amount of runtime required to port becomes lower, so it is up to you how much time you are willing to spend on it.

It has two modes of operation, either targeting C, or C++, I recommend C++ because throw-catch is a better match with modern Objective-C runtime (utilizing C++ ABI behind the scenes) than C setjmp/longjmp.

With some effort you'll get something up and running.

It is a pity Ada developers are so unaware of this option. Maybe somebody could find time to port AdaMagic to EmScripten, iOS, Elbrus or whatever essential target the humanity is missing to start mass Ada adoption.

Others advised producing Swift bindings from bridging headers, but IMO the winning strategy would be to write glue code in Objective-C++ hybrid. Objective-C++ will have access to both Swift code (viewed as external Objective-C classes) and Ada-to-C++ translated code. For instance, it will be able to catch C++ exceptions from Ada and throw Objective-C ones or vice versa.

Another compiler is GNAT CCG aka SPARK2C. It is designed to target embedded devices having no other Ada compiler. It supports recent Ada standards, but is very limited with regards to features requiring runtime. Basicly, there is no runtime. No runtime, no problem. Nothing to port. In this compiler one can return limited record from function (Ada 2005+ feature), but cannot have RAII, tasking, etc. I guess, that are far more strict requirements than Ada 95. GNAT CCG is not yet publicly available, but I expect it to appear before annual Make with Ada, otherwise that would be a very stupid situation.

Another options are Ada-to-Java or Ada-to-.NET, maybe they can work for your application better than others.

OCTAGRAM
  • 618
  • 6
  • 12
  • People are aware of AdaMagic but not the other. The reason anyone is not aware might be b cause they’re commercial transpilers and most likely out of reach of people financially. – Luke A. Guest Jun 19 '18 at 16:16
  • Thankfully, AdaMagic's protection is virtually nonexistent. At least, I guess, anybody able to mix Ada and Swift has enough low-level expertise to get AdaMagic work as well. – OCTAGRAM Jun 20 '18 at 17:00
  • The issue is that AdaMagic isn’t publicly available afaik. – Luke A. Guest Jun 20 '18 at 17:07
  • That is the problem, unawareness. I had no notable problems obtaining it. Had to give away my email address to download trial, and got download links (actually usable without registration, mere wget can handle them). – OCTAGRAM Jun 21 '18 at 19:40
  • and what limitations are on the trial? – Luke A. Guest Jun 21 '18 at 19:54
  • The trial contains compiler, binder, opt etc. tools; and also full sources of Ada RTL for Windows and Linux, and what they call OS Abstractor. Technically, Ada compiler should not work in trial mode. This is achieved by encrypting libadartl.a and barely obscuring Ada tools from running. The simplest way to run Ada tools is to look for demo videos, see that IDE generates key argument for Ada tools (look for console output). And take at look at test_key.exe near the Ada tools. Guess what is it needed for? All the other protection there is equally "strong". – OCTAGRAM Jun 22 '18 at 13:43