Free Pascal on OSv: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: Free Pascal on OSv
| Old | New | Line | |
|---|---|---|---|
| 1 | + | # OSv | |
| 2 | + | ||
| 3 | + | OSv is an operating system based on Linux optimized for the cloud. [Official page](http://osv.io/) It runs a single application together with a small kernel and all needed libraries in a virtual machine. That means to run OSv applications you need to install a hypervisor such as QEMU/KVM or VirtualBox. | |
| 4 | + | ||
| 5 | + | # Installation | |
| 6 | + | ||
| 7 | + | ## Install OSv on Linux | |
| 8 | + | ||
| 9 | + | This worked on 3rd Mar 2015 on Ubuntu 14.10 and on 3rd Sep 2016 on Ubuntu 16.04: | |
| 10 | + | ||
| 11 | + | Please read <http://osv.io/run-locally/>. Especially about the "Data collection". | |
| 12 | + | ||
| 13 | + | Downloading capstan to ~/bin/capstan: | |
| 14 | + | ||
| 15 | + | `curl `[`https://raw.githubusercontent.com/cloudius-systems/capstan/master/scripts/download`](https://raw.githubusercontent.com/cloudius-systems/capstan/master/scripts/download)` | bash` | |
| 16 | + | ||
| 17 | + | Install needed qemu-kvm (VirtualBox is also possible, but not handled in this document) | |
| 18 | + | ||
| 19 | + | `sudo apt-get install qemu-kvm` | |
| 20 | + | ||
| 21 | + | # Building an instance with capstan | |
| 22 | + | ||
| 23 | + | Capstan takes a basic image and adds your libraries to create a VM image. It then starts the VM and runs the 'main' function of your library. At the moment only Linux/amd64 dynamic libraries with PIC are supported. | |
| 24 | + | ||
| 25 | + | ## Capstanfile | |
| 26 | + | ||
| 27 | + | Capstan needs a config file named *Capstanfile*. Read [here](https://github.com/cloudius-systems/capstan/blob/master/Documentation/Capstanfile.md) for details. | |
| 28 | + | ||
| 29 | + | Create a directory for the example. For instance ~/osv_fpc_test. | |
| 30 | + | ||
| 31 | + | Here is an example config for a hello world program (~/osv_fpc_test/Capstanfile): | |
| 32 | + | ||
| 33 | + | # Name of the base image. Capstan will download this automatically from Capstan repository. | |
| 34 | + | base: cloudius/osv-base | |
| 35 | + | ||
| 36 | + | # The command line passed to OSv to start up the application. | |
| 37 | + | cmdline: /tools/hello.so | |
| 38 | + | ||
| 39 | + | # The command used to build the application. | |
| 40 | + | build: ./compile.sh | |
| 41 | + | ||
| 42 | + | # List of files that are included in the generated image. | |
| 43 | + | files: | |
| 44 | + | /tools/hello.so: hello.so | |
| 45 | + | ||
| 46 | + | ## Pascal Library | |
| 47 | + | ||
| 48 | + | The small Linux kernel does not support all syscalls needed by the default FPC Linux System unit. | |
| 49 | + | ||
| 50 | + | See here for some details what OSv does not support: <https://github.com/cloudius-systems/osv/wiki/Porting-native-applications-to-OSv> | |
| 51 | + | ||
| 52 | + | You need to build the System unit using Libc (tested with fpc trunk revision 34418): | |
| 53 | + | ||
| 54 | + | `cd fpc_src_3.1.1`\ | |
| 55 | + | `make clean all OPT='-dFPC_USE_LIBC -Fl/usr/lib/gcc/x86_64-linux-gnu/5'`\ | |
| 56 | + | `sudo make install INSTALL_PREFIX=/usr` | |
| 57 | + | ||
| 58 | + | ## hello.pas | |
| 59 | + | ||
| 60 | + | ``` pascal | |
| 61 | + | library hello; | |
| 62 | + | ||
| 63 | + | // start function for OSv | |
| 64 | + | function main: longint; cdecl; | |
| 65 | + | begin | |
| 66 | + | Writeln('It works!'); | |
| 67 | + | main:=0; | |
| 68 | + | end; | |
| 69 | + | ||
| 70 | + | exports main name 'main'; // OSv searches for 'main' in the library | |
| 71 | + | ||
| 72 | + | end. | |
| 73 | + | ``` | |
| 74 | + | ||
| 75 | + | Here is the *~/osv_fpc_test/compile.sh*: | |
| 76 | + | ||
| 77 | + | #!/bin/bash | |
| 78 | + | # create a dynamically linked library hello.so | |
| 79 | + | /usr/lib/fpc/3.1.1/ppcx64 -fPIC -XD -Xc -gw2 -ohello.so hello.pas | |
| 80 | + | ||
| 81 | + | ## Building | |
| 82 | + | ||
| 83 | + | **Note**: The first time you build, capstan will download the base VM image "cloudius/osv-base" (~/.capstan/). | |
| 84 | + | ||
| 85 | + | []$ cd ~/osv_fpc_test | |
| 86 | + | []$ ~/bin/capstan build | |
| 87 | + | Building osv... | |
| 88 | + | Uploading files... | |
| 89 | + | 1 / 1 [===================================] 100.00 % | |
| 90 | + | ||
| 91 | + | ## Running | |
| 92 | + | ||
| 93 | + | []$ ~/bin/capstan run | |
| 94 | + | Created instance: helloworld | |
| 95 | + | OSv v0.18 | |
| 96 | + | eth0: 192.168.122.15 | |
| 97 | + | It works! | |
| 98 | + | ||
| 99 | + | # Pascal Library without using the System unit | |
| 100 | + | ||
| 101 | + | As demonstration here is a library, that does not call the system unit (works the normal FPC system unit): | |
| 102 | + | ||
| 103 | + | ## hello.pas | |
| 104 | + | ||
| 105 | + | Here is the *~/osv_fpc_test/hello.pas*: | |
| 106 | + | ||
| 107 | + | ``` pascal | |
| 108 | + | library hello; | |
| 109 | + | ||
| 110 | + | uses | |
| 111 | + | unixtype; | |
| 112 | + | ||
| 113 | + | // use the C function 'write' | |
| 114 | + | function CWrite(fd : cInt; buf:pChar; nbytes : unixtype.TSize): TSsize; external name 'write'; | |
| 115 | + | ||
| 116 | + | // start function for OSv | |
| 117 | + | function main: longint; cdecl; | |
| 118 | + | const | |
| 119 | + | MyText: PChar = 'It works!'#10; | |
| 120 | + | begin | |
| 121 | + | CWrite(StdOutputHandle,MyText,strlen(MyText)); | |
| 122 | + | main:=0; | |
| 123 | + | end; | |
| 124 | + | ||
| 125 | + | exports main name 'main'; // OSv searches for 'main' in the library | |
| 126 | + | ||
| 127 | + | end. | |
| 128 | + | ``` | |
| 129 | + | ||
| 130 | + | ## Compile a library that does not call the system init | |
| 131 | + | ||
| 132 | + | Here is the *~/osv_fpc_test/compile.sh*: | |
| 133 | + | ||
| 134 | + | #!/bin/bash | |
| 135 | + | ||
| 136 | + | # compile without linking, create a dynamically linked library hello.so | |
| 137 | + | fpc -fPIC -XD -Xc -g -s -ohello.so hello.pas | |
| 138 | + | # link without -init FPC_SHARED_LIB_START -fini FPC_LIB_EXIT | |
| 139 | + | /usr/bin/ld -b elf64-x86-64 -m elf_x86_64 -soname hello.so -shared -L. -o hello.so link.res | |
| 140 | + | ||
| 141 | + | ## Building | |
| 142 | + | ||
| 143 | + | **Note**: The first time you build, capstan will download the base VM image "cloudius/osv-base" (~/.capstan/). | |
| 144 | + | ||
| 145 | + | []$ cd ~/osv_fpc_test | |
| 146 | + | []$ ~/bin/capstan build | |
| 147 | + | Building osv... | |
| 148 | + | Uploading files... | |
| 149 | + | 1 / 1 [===================================] 100.00 % | |
| 150 | + | ||
| 151 | + | ## Running | |
| 152 | + | ||
| 153 | + | []$ cd ~/osv_fpc_test | |
| 154 | + | []$ ~/bin/capstan run | |
| 155 | + | Created instance: osv | |
| 156 | + | OSv v0.18 | |
| 157 | + | eth0: 192.168.122.15 | |
| 158 | + | It works! | |
| 159 | + | ||
| 160 | + | --- | |
| 161 | + | *From the Free Pascal wiki, page "Free Pascal on OSv", as it stood on 2020-02-16 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* | |
| 162 | + | Category: Linux |
