|
| 1 | +# Add Ocre Runtime to a custom Linux Application |
| 2 | + |
| 3 | +This document will guide you through the process of embedding Ocre Runtime into a custom Linux application. For integrating Ocre Runtime into a Zephyr application, please refer to the [Custom Zephyr Application](CustomZephyrApplication.md) documentation. |
| 4 | + |
| 5 | +We will provide instructions for a CMake project. Other build systems can be used but are not documented here. |
| 6 | + |
| 7 | +Note that this is not the only way to achieve the integration of Ocre Runtime into a Linux application. As Ocre Runtime is provided as a library, we recommend you just link it statically to your application, as this guide will show. |
| 8 | + |
| 9 | +Alternatives range from building the Ocre Runtime as an ExternalProject and then linking it statically or dynamically to your application. |
| 10 | + |
| 11 | +## Prerequisites |
| 12 | + |
| 13 | +Use the [Devcontainer](Devcontainers.md), follow the [Get started with Linux](GetStartedLinux.md) guide or set up a [Native Build](NativeBuild.md) development environment so you can compile Ocre Runtime. |
| 14 | + |
| 15 | +## CMake Project Integration |
| 16 | + |
| 17 | +Say we have any CMake project with a structure like: |
| 18 | + |
| 19 | +``` |
| 20 | +. |
| 21 | +├── CMakeLists.txt |
| 22 | +└── main.c |
| 23 | +``` |
| 24 | + |
| 25 | +The contents of `CMakeLists.txt` are as follows: |
| 26 | + |
| 27 | +```cmake |
| 28 | +cmake_minimum_required(VERSION 3.20) |
| 29 | +
|
| 30 | +project(my_project) |
| 31 | +
|
| 32 | +add_executable(app main.c) |
| 33 | +``` |
| 34 | + |
| 35 | +And the contents of `main.c` are as follows: |
| 36 | + |
| 37 | +```c |
| 38 | +#include <stdio.h> |
| 39 | + |
| 40 | +int main() { |
| 41 | + printf("Hello, World!\n"); |
| 42 | + return 0; |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +`Hello, World!` is being printed to the console from native code. |
| 47 | + |
| 48 | +To add Ocre runtime to this project, if you are in a Git repository, we can add it as a git submodule: |
| 49 | + |
| 50 | +``` |
| 51 | +git submodule add https://github.com/project-ocre/ocre-runtime.git |
| 52 | +git submodule update --init --recursive |
| 53 | +``` |
| 54 | + |
| 55 | +Or if you are not in a Git repository, we can just clone the repository: |
| 56 | + |
| 57 | +```sh |
| 58 | +git clone --recursive https://github.com/project-ocre/ocre-runtime.git |
| 59 | +``` |
| 60 | + |
| 61 | +So we have a directory structure like: |
| 62 | + |
| 63 | +``` |
| 64 | +. |
| 65 | +├── CMakeLists.txt |
| 66 | +├── main.c |
| 67 | +└── ocre-runtime |
| 68 | + └── ... |
| 69 | +``` |
| 70 | + |
| 71 | +Then, we can add the following lines to `CMakeLists.txt`: |
| 72 | + |
| 73 | +```cmake |
| 74 | +add_subdirectory(ocre-runtime) |
| 75 | +
|
| 76 | +target_link_libraries(app PRIVATE OcreCore) |
| 77 | +``` |
| 78 | + |
| 79 | +And now just try to build the project: |
| 80 | + |
| 81 | +```sh |
| 82 | +mkdir build |
| 83 | +cd build |
| 84 | +cmake .. |
| 85 | +make |
| 86 | +``` |
| 87 | + |
| 88 | +If this works, we can successfully build Ocre Runtime library. We can now proceed to the next section about using Ocre from your application. |
| 89 | + |
| 90 | +## Using Ocre from your application |
| 91 | + |
| 92 | +Now that we have a build working, and we can successfully build Ocre Runtime library and our application, replace the `main.c` contents with: |
| 93 | + |
| 94 | +```c |
| 95 | +#include <stdio.h> |
| 96 | +#include <ocre/ocre.h> |
| 97 | + |
| 98 | +int main() { |
| 99 | + int rc = ocre_initialize(NULL); |
| 100 | + if (rc) { |
| 101 | + fprintf(stderr, "Failed to initialize runtimes\n"); |
| 102 | + return 1; |
| 103 | + } |
| 104 | + |
| 105 | + struct ocre_context *ocre = ocre_create_context("/tmp/ocre"); |
| 106 | + if (!ocre) { |
| 107 | + fprintf(stderr, "Failed to create ocre context\n"); |
| 108 | + return 1; |
| 109 | + } |
| 110 | + |
| 111 | + struct ocre_container *hello = |
| 112 | + ocre_context_create_container(ocre, "hello.wasm", "wamr/wasip1", NULL, false, NULL); |
| 113 | + |
| 114 | + if (!hello) { |
| 115 | + fprintf(stderr, "Failed to create container\n"); |
| 116 | + return 1; |
| 117 | + } |
| 118 | + |
| 119 | + rc = ocre_container_start(hello); |
| 120 | + if (rc) { |
| 121 | + fprintf(stderr, "Failed to start container\n"); |
| 122 | + return 1; |
| 123 | + } |
| 124 | + |
| 125 | + return 0; |
| 126 | +} |
| 127 | +``` |
| 128 | + |
| 129 | +This code will create a new Ocre context with `/tmp/ocre` as the working directory, and then will start a container with the image `hello.wasm`, which we will copy from Ocre SDK. |
| 130 | + |
| 131 | +To use your own container image, please refer to the [Create and Run custom containers](CreateAndRunCustomContainers.md) documentation. |
| 132 | + |
| 133 | +We can now proceed with the build process by running `make`, possibly in the build directory. |
| 134 | + |
| 135 | +We will now create the working directory `/tmp/ocre` and copy the `hello.wasm` file into it: |
| 136 | + |
| 137 | +You can use the following commands from the build directory: |
| 138 | + |
| 139 | +```sh |
| 140 | +mkdir -p /tmp/ocre/images |
| 141 | +cp ./ocre-runtime/src/ocre/OcreSampleContainers/build/hello-world.wasm /tmp/ocre/images/hello.wasm |
| 142 | +``` |
| 143 | + |
| 144 | +Note that we "renamed" the `hello-world.wasm` file to `hello.wasm` for simplicity. |
| 145 | + |
| 146 | +Now you can run your application: |
| 147 | + |
| 148 | +```sh |
| 149 | +./app |
| 150 | +``` |
| 151 | + |
| 152 | +And it should run the `hello.wasm` container. |
| 153 | + |
| 154 | +For more information, check the [Linux Build system](BuildSystemLinux.md) documentation. |
0 commit comments