Using CMake from the contents of KasperskyOS Community Edition
To automate the process of preparing the solution image, you need to configure the CMake
build system. You can base this system on the build system parameters used in the examples from KasperskyOS Community Edition.
CMakeLists.txt
files use the standard CMake
syntax, and commands and macros from libraries provided in KasperskyOS Community Edition.
Recommended structure of project directories
When creating a KasperskyOS-based solution, it is recommended to use the following directory structure in a project to simplify the use of CMake
scripts:
- In the project root, create a CMakeLists.txt boot file containing the general build instructions for the entire solution.
- The source code of each program being developed should be placed into a separate directory within the
src
subdirectory. - Create CMakeLists.txt files for building each application in the corresponding directories.
- To generate the source code of the
Einit
program, you should create a separateeinit
directory containing thesrc
subdirectory in which you should put the init.yaml.in and security.psl.in templates.Any other files that need to be included in the solution image can also be put into this directory.
- Create a CMakeLists.txt file for building the
Einit
program in theeinit
directory. - The files of EDL, CDL and IDL descriptions should be put into the
resources
directory in the project root. - [Optional] Create a
cross-build.sh
build script containing the commands to start generating build files (cmake
command), to build the solution (make
command), and to start the solution.
Example structure of project directories
example$ tree
.
├── CMakeLists.txt
├── cross-build.sh
├── hello
│ ├── CMakeLists.txt
│ ├── src
│ │ ├── hello.c
├── einit
│ ├── CMakeLists.txt
│ ├── src
│ │ ├── init.yaml.in
│ │ ├── security.psl.in
│ │ ├── fstab
├── resources
│ ├── Hello.idl
│ ├── Hello.cdl
│ ├── Hello.edl
Building a project
To prepare for a build using the CMake
build system, the following is required:
- Prepare a CMakeLists.txt boot file containing the general build instructions for the entire solution.
- Prepare CMakeLists.txt files for each application to be built.
- Prepare a CMakeLists.txt file for the Einit program.
- Prepare the
init.yaml.in
andsecurity.psl.in
templates.
To perform cross-compilation using the CMake
build automation system, the following is required:
- Create a subdirectory for the build.
BUILD=$PWD/.build
mkdir -p $BUILD && cd $BUILD
- Prior to starting generation of build scripts (
cmake
command), set the following values for environment variables:export LANG=C
export PKG_CONFIG=""
export SDK_PREFIX="/opt/KasperskyOS-Community-Edition-<version>"
export PATH="$SDK_PREFIX/toolchain/bin:$PATH"
export INSTALL_PREFIX=$BUILD/../install
export TARGET="aarch64-kos"
- When starting generation of build scripts (
cmake
command), specify the following:-G "Unix Makefiles"
parameter- Path to the file with the build system extension (
toolchain.cmake
) in theCMAKE_TOOLCHAIN_FILE
variable.The file with the build system extension is located in the following directory:
/opt/KasperskyOS-Community-Edition-<version>/toolchain/share/toolchain-aarch64-kos.cmake
- Value of the
CMAKE_BUILD_TYPE:STRING=Debug
variable - Value of the
CMAKE_INSTALL_PREFIX:STRING=$INSTALL_PREFIX
variable - Path to the CMakeLists.txt boot file
- When starting the build (
make
command), specify one of the build targets.The target name must match the build target name passed to the solution build command in the CMakeLists.txt file for the Einit program.
Example cross-build.sh build script
cross-build.sh
#!/bin/bash
# Create a subdirectory for the build
BUILD=$PWD/.build
mkdir -p $BUILD && cd $BUILD
# Set the values of environment variables
export LANG=C
export PKG_CONFIG=""
export SDK_PREFIX="/opt/KasperskyOS-Community-Edition-<version>"
export PATH="$SDK_PREFIX/toolchain/bin:$PATH"
export INSTALL_PREFIX=$BUILD/../install
export TARGET="aarch64-kos"
# Start generating files for the build. The current directory is $BUILD,
# so the CMakeLists.txt boot file is in the parent directory
cmake -G "Unix Makefiles" \
-D CMAKE_BUILD_TYPE:STRING=Debug \
-D CMAKE_INSTALL_PREFIX:STRING=$BUILD/../.install \
-D CMAKE_TOOLCHAIN_FILE=$SDK_PREFIX/toolchain/share/toolchain-$TARGET.cmake \
../
# Start the build. Include the VERBOSE flag for Make and redirect the output to the build.log file
VERBOSE=1 make kos-qemu-image 2>&1 | tee build.log
# Run the built solution image in QEMU.
# -kernel $BUILD/einit/kos-qemu-image path to the built kernel image
$SDK_PREFIX/toolchain/bin/qemu-system-aarch64 \
-m 1024 \
-cpu core2duo \
-serial stdio \
-kernel $BUILD/einit/kos-qemu-image