Free Pascal development for Ubuntu Phone


Introduction 

Freepascal compiler (see: freepascal.org) has long been available for both Linux and ARM platforms. It seemed to me that it should be straightforward to use if on Ubuntu phone, to say at least. And I was not interested in any cross-compiling type of setup. I wanted a true native way of writing, compiling, running and debugging code for Ubuntu phone, on the Ubuntu phone itself. This article will show, that this is in fact possible and painfully straightforward - yay!

There are however, few culprits. Everything is as easy as possible as long as you stay inside Ubuntu Phone's shell. As you use terminal environment to write code, and the code you write is a terminal application. Culprits will arise, however, when you go about using code editor in GUI mode, or going about writing a GUI application. This article will introduce and get into detail of those aspects in a rather shallow fashion, because GUI aspect of developing for Ubuntu Phone with Freepascal will be covered separately in the article "Lazarus development for Ubuntu Phone".
Or if you want to read about general scenario of running GUI X applications on the phone, read my X applications on ubuntu phone post.

All of the examples in this article have been tested on a Meizu MX4 Ubuntu Edition phone.

Pure shell mode: coding and running programs in terminal

This is the most straightforward aspect of developing on and for Ubuntu Phone with Freepascal. It is in fact just identical with how you would go about it on your Ubuntu desktop. With only one minuscule difference - the system image on the phone, unlike on a standard desktop environment, is read-only. This however, so far, is not an obstacle at all, since all the Ubuntu Touch devices that came out so far, that I am aware off, grants user full root access. It means you can at wish toggle the system image from read-only to read-write and back. Long story short, here is how you get Freepascal compiler up and running on your Ubuntu Phone:

  $ #set system image to read-write:
  $ sudo mount -o remount,rw /  
  $ #install Freepascal right from the repos:
  $ sudo apt-get install fpc
  $ #just if you got any errors (I have not):
  $ sudo apt-get install -f
  $ #set system image to read-only again:
  $ sudo mount -o remount,ro /

Now, there is one more step though, not mentioned above, that determines whether or not above shell commands actually all are executed smoothly: will fpc package be found in your repository.
Easy answer is: make sure the Universe repository is enabled. If you, however, are not sure what it means, then this paragraph on. On your phone, there is a file: /etc/apt/sources.list, where you find list of repositories being searched for packages when you try to install them via apt-get command. You can by hand edit this file and add extra repositories in there. You can edit the file with vi editor, which is one I do not like using and find it user-unfriendly. If you do too, I recommend using mcedit from mc package, to install it, just install the mc package, which will get you both the mcedit and the awesome Midnight Commander file browser for terminal:

 $ sudo apt-get install mc

For your reference, in my phone, the /etc/apt/sources.list file looks like this:

  deb http://ports.ubuntu.com/ubuntu-ports/ vivid-security main restricted
  deb-src http://ports.ubuntu.com/ubuntu-ports/ vivid-security main restricted
  deb http://ports.ubuntu.com/ubuntu-ports/ vivid-security universe
  deb-src http://ports.ubuntu.com/ubuntu-ports/ vivid-security universe

So if yours looks the same, you should now be able to install Freepascal and then run it. In my case, I have Free Pascal Compiler version 2.6.4+dfsg-4 [2014/10/24] installed, which is not the newest one, but a recent enough! It works great, and I can even enjoy terminal IDE for coding and debugging, supplied as a part of fpc package, avalable under command fp.

GUI coding

As long as we are still within the aspect of writing console-mode applications, we might prefer to use GUI code editor. I will go ahead with Geany (see: geany.org) as my choice for the editor. It is quite mature, lightweight, and recognizes multiple compilers, including Free Pascal. So far, there is no port of Geany for Ubuntu Phone with QT interface, unless I don't know about it. But there is actually a GTK version of it available in the repos. However, the size of geany package along with all dependencies is somewhat heavy, especially if we have already installed fpc and really running out of space on the root partition. There are at least 3 options for getting Geany on the phone, however the fisrt option might just not be viable one because of the low avaliable disk space mentionned:
  1. sudo apt-get install geany (might need some extra dependencies)
  2. Installing Geany inside Libertine container
  3. Installing custom-compiled Geany binary under home partition
I will not be covering the 1st option here at all. As for the 2nd option, as long as you have Libertine installed, just go ahead and install geany package inside it, and enjoy! Now, the 3rd option, is also nice, plus it does not requie your device to have Libertine installed at all. There is a limitation to this method though. Geany requires a bunch of files to be deployed under /usr/share/geany folder. And we already said that we do not want to use up space in the root partition. Also, Geany seems to have some dependencies, that at the end prevents it to run on the phone directly. So far, I have not figured all of those dependencies out. What I am doing though, I am using locally installed Geany via SSH session with X forwarding, so I run it off the phone, but onto my Ubuntu Desktop. Given that your device is compatible with my Meixu MX4 in terms of CPU, go ahead and download geany-arm.7z onto your phone. Extract it somewhere under your home directory, ie. ~/.local/geany-arm, then you will see a fiele geany-arm and a folder geany inside. Ensure the binary is executable, and make symbolic link under /usr/share

 $ sudo mount -o remount,rw /
 $ sudo chmod a+x ~/.local/geany/geany
 $ sudo ln -s /home/phablet/.local/geany/geany /usr/share/geany
 $ sudo mount -o remount,ro /

Now, just connect over SSH (with X forwarding!) to your phone, and you can enjoy the Geany running directly off your phone! I prefer using -Y switch instead of -X, so:

 $ ssh -Y phablet@192.168.1.31
 $ ~/.local/geany/geany-arm
 $ #optionally: sudo ln-s /home/phablet/.local/geany/geany-arm /usr/bin/geany-arm

This is screen of Geany on the screen of my Ubuntu Desktop, however, it is running inside my Ubuntu Phone! You could off course just put Lazarus in your phone, and hat would seem to be the ultimate way to enjoy Pascal development on the Ubuntu Phone, but... no, there's no but, you can do just that as well, and this is going to be covered in detail in my next article "Lazarus development for Ubuntu Phone".


Proof of concept

So without further do, below are some screens from a terminal session on my Meizu MX4 Ubuntu Edition phone. I am using my Ubuntu Desktop, and I connect over SSH into the phone with my standard gnome-terminal:






So as you can see I have used Free Pascal console-mode IDE (fp) to write and compile hello.pas file, then I ran hello in the shell, and finally I stripped the binary off the embedded debugger information to have a smaller executable size!

Lastly, it is naturally possible to write code on the phone itself. Just keep in mind, that the screen facility is not very convenient to work with in case of a smartphone. There is a native Terminal app for Ubuntu Phones that you can install from Ubuntu Store. And inside it, you can use Free Pascal as well, and below are screen captures from my phone:










inside the fp IDE there are lots of keyboard shortcuts for navigation. It is not very easy to use these on the phone with the OSK (on-screen keyboard), but possible. The Terminal app allows us to input things like F1..F12 keys, or keyboard arrow keys. This problem can be handled entirely though, if you connect BT keyboard and mouse to your phone, which I have tried and it is quite fun!



Comments

Popular posts from this blog

Delphi VCL/FMX for Linux before the official platform support

How I use Linux to write software for multiple target platforms

Easy containers on Ubuntu Touch with qemu-debootstrap