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