SlideShare a Scribd company logo
Simon Kuenzer <simon.kuenzer@neclab.eu>
Senior Researcher, NEC Laboratories Europe GmbH
Xen Summit 2019, Chicago
A Journey through Unikraft’s Build System
This work has received funding from the European Union’s Horizon 2020 research and innovation
program under grant agreements no. 675806 (“5G CITY”). This work reflects only the author’s views
and the European Commission is not responsible for any use that may be made of the information it
contains.
XPDDS19: A Journey through Unikraft's Build System
Unikraft‘s Build System
Overview
4 © NEC Corporation 2019
Unikraft
KConfig
Makefile System
Internal Library
Config.uk
Makefile.uk
Sources
Build System: Loading & Parsing
Your Application
Config.uk
Makefile.uk
Sources
Makefile
External Library
Config.uk
Makefile.uk
Sources
5 © NEC Corporation 2019
Unikraft’s 3 Build Stages
(1) Fetch
Download and decompress external sources
e.g., a library hosted on GitHub, Sourceforge
Patch downloaded files
(2) Prepare
Further preparation steps to the sources, for instance:
• Call ./configure of downloaded library sources
• Generate further sources or headers required for building
(3) Compile & Link
Compile sources
Link libraries
Link final images
6 © NEC Corporation 2019
*_SRCS-y *_OBJS-y UK_ALIBS-y
UK_OLIBS-y
UK_IMAGES-y
Build Stage 3: Compiling and Linking
libA_a.c
libA_b.S libA.ld.o libA.o
... libB.o
Images
prebuilt.o
...
staticA.a
...
exportsyms.uk
libA_a.o
libA_b.o
......
extA.o
...
7 © NEC Corporation 2019
Make parameters
▌Unikraft’s base Makefile
$ make A=[APP] L=[LIBRARIES] P=[PLATFORMS] V=[1/0] [target]
Parameter Description
A=[APP] Path to application directory
L=[LIBRARIES] Colon-separated list of paths to external libraries
P=[PLATFORMS] Colon-separated list of paths to external platform libs
V=[1/0] Verbose mode (on/off)
[target] Build target
help Show overview of targets
menuconfig Configure and select target images
(default when there is no .config)
all/images Build everything (default) +libs
libs Build libraries +prepare
prepare Run preparations steps +fetch
fetch Download, extract, and patch external code
Unikraft Libraries
Integrate own libraries and applications
9 © NEC Corporation 2019
Libraries/Applications/Platforms: Necessary files
▌Makefile *applications only
 Invoke Unikraft build for simplification
▌Config.uk
 Configuration options
• Settings saved as part of .config
 Specifying library dependencies
and depending options
▌Makefile.uk
 Registration to the build system
 Specification of source files
 Extra custom Make rules
• For instance for preparing the sources
▌exportsyms.uk
 Masking of symbols
▌Linker.uk *platform libraries only
 Platform-dependent rules for linking final image
.config
10 © NEC Corporation 2019
Makefile *applications only
▌Simplify application building
▌Changes to Unikraft base directory and invokes make
UK_ROOT ?= $(PWD)/../../unikraft
UK_LIBS ?= $(PWD)/../../libs
LIBS := $(UK_LIBS)/libA:$(UK_LIBS)/libB
all:
@$(MAKE) -C $(UK_ROOT) A=$(PWD) L=$(LIBS)
$(MAKECMDGOALS):
@$(MAKE) -C $(UK_ROOT) A=$(PWD) L=$(LIBS) (MAKECMDGOALS)
11 © NEC Corporation 2019
Config.uk
▌KConfig syntax1
▌Structure:
Title and Dependencies
Configuration parameters
(optional)
[1] https://www.kernel.org/doc/Documentation/kbuild/kconfig-language.txt
12 © NEC Corporation 2019
Config.uk: Title and Dependencies for Libraries
menuconfig defines a submenu for the following configuration options
• Note: Use just config without following if-block when the library does not
have any configuration parameters
select keyword is used to describe dependencies to other libraries
• Supports conditional expressions1
• Multiple select lines are possible for a single configuration
menuconfig LIBMYLIB
bool "ukmylib: my scheduler“
### libraries are off as default
default n
### dependencies
select LIBNOLIBC if !HAVE_LIBC
if LIBMYLIB
### list of configuration parameters goes here
endif
13 © NEC Corporation 2019
Config.uk: Dependencies for Applications
Invisible bool config to select dependencies
• Applications are not defining an own submenu. Because just a single
application can currently be selected for one Unikernel build, applications are
enabled as default
Like for libraries, select keyword is used to describe dependencies to
libraries
### Invisible option (no description) that is set as
### default to ‚y‘ in order to select dependencies
config LIBMYAPP
bool
default y
select LIBNOLIBC if !HAVE_LIBC
### list of configuration parameters goes here
14 © NEC Corporation 2019
Config.uk: Configuration Parameter
Name-space configuration options!
• Prepend library name in front of parameters: here: LIBMYLIB_
Configurations will appear as CONFIG_[CONFIGNAME]
in the build system and in the sources (include uk/config.h)
(here: CONFIG_LIBMYLIB_SETTING)
[type] can be one of bool, int (unsigned), hex, string:
Advanced options, like choice lists, are documented at:
https://www.kernel.org/doc/Documentation/kbuild/kconfig-language.txt
config LIBMYLIB_SETTING
[type] "[description]"
default [value]
select LIBOTHER
Makefile.uk #include <uk/config.h>
bool y/n „y“ is defined as 1
„n“ is not defined
int Hexadecimal Defined as hexadecimal
hex Hexadecimal Defined as hexadecimal
string String Defined as String
15 © NEC Corporation 2019
Makefile.uk
▌Makefile syntax
Unikraft provides helper functions and variables
Unikraft expects specific variables to be filled
▌Structure
Registration
Source files and build flags
Custom prepare rules
External sources (optional)
16 © NEC Corporation 2019
Makefile.uk: Registration
▌Registration with addlib / addlib_s helpers1
The first thing that has to be done in a Makefile.uk
Libraries (depending on being enabled)
Replace [libmylib] and CONFIG_LIBMYLIB accordingly:
Applications
Replace [libmyapp] accordingly:
The namespace for variables is defined by application/library name
here: prefixed in uppercase: LIBMYLIB_, LIBMYAPP_
The following variables are populated after the call
The following overrides are available after the call
$(eval $(call addlib_s,[libmylib],$(CONFIG_LIBMYLIB)))
[1] support/build/Makefile.rules
*_BASE Path to library folder
*_BUILD Path to library‘s output/build folder
Note: Place all generated files during building in here, never in the base
$(eval $(call addlib,[libmyapp]))
*_EXPORTS Path to an alternative exportsyms.uk (optional)
17 © NEC Corporation 2019
Makefile.uk: External sources (optional)
▌Download one archive with (additional) sources and extract them
with fetch / fetchas helpers1
Example with lwIP:
.tar.gz, .tgz, .tar.xz, .txz, and .zip are currently supported1
The following variables are populated after this call
▌If some downloaded source files need to be patched, use
patch helper1:
Example with lwIP:
This command applies all patches found in $(LIBLWIP_PATCHDIR) to the sub-
directory $(LIBLWIP_ZIPNAME) of the previously extracted sources
LIBLWIP_ZIPNAME=lwip-2.1.2
LIBLWIP_URL=http://download.savannah.nongnu.org/releases/lwip/$(LIBLWIP_ZIPNAME).zip
$(eval $(call fetch,liblwip,$(LIBLWIP_URL)))
[1] support/build/Makefile.rules
*_ORIGIN Path to folder containing extracted archive files
LIBLWIP_PATCHDIR=$(LIBLWIP_BASE)/patches
$(eval $(call patch,liblwip,$(LIBLWIP_PATCHDIR),$(LIBLWIP_ZIPNAME)))
18 © NEC Corporation 2019
Makefile.uk: Source files
▌Full paths to source files are added to the *_SRCS-y list:
Compile rule and target is automatically generated by build system
based on file extension:
• Currently supported: .S, .sx, .s, .c, .C, .cc, .cp, .cxx, .cpp, .CPP, .c++, .lds.S
• .o-binary is created within the library output directory based on the source filename:
– Note: Only the filename without extension matters for the target file name. The source file extension
and path is irrelevant. In conflicting cases, use variants (see next slide).
The following include and build flag lists apply for a source file:
# Source file from library directory
LIBMYLIB_SRCS-y += $(LIBMYLIB_BASE)/source.c
# Source file from extracted archive
LIBMYLIB_SRCS-y += $(LIBMYLIB_ORIGIN)/another_src.c
CFLAGS-y Global build flags (here: C sources, equivalents for other types exist)
CINCLUDES-y Global includes (here: C sources, equivalents for other types exist)
*_CFLAGS-y Library-internal build flags (here: C sources, equivalents exist)
*_CINCLUDES-y Library-internal includes (here: C sources, equivalents exist)
*_[FILENAME]_FLAGS Source file specific build flags
*_[FILENAME]_INCLUDES Source file specific includes
$(LIBMYLIB_BASE)/source.c.  $(LIBMYLIB_BUILD)/source.o
$(LIBMYLIB_ORIGIN)/another_src.c  $(LIBMYLIB_BUILD)/another_src.o
19 © NEC Corporation 2019
Makefile.uk: Source File Variants
▌Variants exist because of two reasons:
Conflicting output file names (previous slide)
Necessity to compile a single source file multiple times with different flags
(e.g., newlib *scanf() variants)
▌Variant names are added with a pipe symbol after source:
…produces:
Variants have their own specific build flags and includes:
LIBMYLIB_SRCS-y += $(LIBMYLIB_BASE)/source.c
LIBMYLIB_SRCS-y += $(LIBMYLIB_BASE)/source.c|variant
LIBMYLIB_SRCS-y += $(LIBMYLIB_ORIGIN)/source.c|origin
$(LIBMYLIB_BASE)/source.c  $(LIBMYLIB_BUILD)/source.o
$(LIBMYLIB_BASE)/source.c|variant  $(LIBMYLIB_BUILD)/source.variant.o
$(LIBMYLIB_ORIGIN)/source.c|origin  $(LIBMYLIB_BUILD)/source.origin.o
*_[FILENAME]_FLAGS Build flags for source file without variant specification
*_[FILENAME]_INCLUDES Includes for source file without variant specification
*_[FILENAME]_[VARIANT]_FLAGS Build flags for source file for variant [VARIANT]
*_[FILENAME]_[VARIANT]_INCLUDES Includes for source file for variant [VARIANT]
20 © NEC Corporation 2019
Makefile.uk: Externally Compiled Sources
▌Unikraft supports including externally compiled sources
Cases where it may happen:
Code only available as binary form
Compiling is done by different build system
(e.g., invoked by custom prepare rules)
▌Depending on the type, various places exist to add them:
.o-object files are added to the *_OBJS-y list:
.o-libraries are registered to the global list UK_OLIBS
(remember to use the library switch CONFIG_*):
Static libraries are registered to the global list UK_ALIBS
(remember to use the library switch CONFIG_*):
Note: Shared libraries (.so) are currently not supported
LIBMYLIB_OBJS-y += $(LIBMYLIB_BASE)/prebuilt.o
UK_ALIBS-$(CONIG_LIBMYLIB) += $(LIBMYLIB_BASE)/static_lib.a
UK_OLIBS-$(CONFIG_LIBMYLIB) += $(LIBMYLIB_BASE)/prebuilt_lib.o
21 © NEC Corporation 2019
Makefile.uk: Scope of Headers (Includes)
▌Global headers (e.g., library API)
(remember to use the library switch CONFIG_*):
▌Library-Internal headers
▌File-specific headers
▌Equivalent to this, you can set build flags within a specific scope
CFLAGS-$(CONFIG_LIBMYLIB), LIBMYLIB_CFLAGS-y,
LIBMYLIB_MYSRC_FLAGS, LIBMYLIB_MYSRC_VARIANT_FLAGS
CINCLUDES-$(CONFIG_LIBMYLIB) += -I$(LIBMYLIB_BASE)/include/api
LIBMYLIB_CINCLUDES-y += -I$(LIBMYLIB_BASE)/include/internal
# mysrc.c
LIBMYLIB_MYSRC_INCLUDES += -I$(LIBMYLIB_BASE)/include/mysrc
# Variant var0 of mysrc.c: mysrc.c|var0
LIBMYLIB_MYSRC_VAR0_INCLUDES += -I$(LIBMYLIB_BASE)/include/mysrc
22 © NEC Corporation 2019
Makefile.uk: Custom Prepare Rules
▌Reason
Generate files (headers, sources) needed for build
Invoke parts of ported library build system, like ./configure
▌Defined as custom Make rules
Use build_cmd to prettify the output1
(in cases where build_cmd is not applicable use verbose_cmd)
▌If used, set marker of fetch stage as dependency
Download marker: $(LIBMYLIB_BUILD)/.origin
Patched marker: $(LIBMYLIB_BUILD)/.patched
▌Register generated files to prepare stage
UK_PREPARE-$(CONFIG_LIBMYLIB) += [generated file/phony]
$(LIBMYLIB_BUILD)/generated.h: [dependencies]
$(call build_cmd,NM,libmylib,$@,$(NM) -n $(LIBMYLIB_BASE)/symtab.in > $@)
[1] support/build/Makefile.rules
23 © NEC Corporation 2019
exportsyms.uk
▌Re-masks the scope of each symbol of a library
 Re-defines for each symbol if it is available for final linking
 Intended to reduce potential clashing of symbols
▌List of symbol names that should be available globally for final
linking. Non-listed symbols become private to the library.
 Example (libnolibc):
▌Note: The build system will throw a warning when no exportsyms.uk
file is provided
 The scope of each library symbol stays unchanged in such a case
asprintf
vasprintf
# comments are ignored
opterr
optind
optopt
optreset
optarg
getopt
[...]
Best Practices
25 © NEC Corporation 2019
Best Practices
▌Porting existing libraries/applications is a challenging task
 Existing sources often only fit to their build and configuration system
 Often not intended to run on something else than Linux (assumptions to the OS)
▌If possible, compile all sources with Unikraft
 Including external build binaries is risky
• Build flags may be incompatible (e.g., register usage/calling convention, LTO)
• Mismatch of depending libraries (external vs. Unikraft‘s version)
▌Learn from existing build system
 Extract list of source files and build flags when compiling with original build system
 Study steps that generate files needed for the build
• Try to run ./configure with settings fitting to Unikraft environment
• It is also possible to call ./configure from Unikraft as prepare step
▌Provide initial stubs for missing symbols
 Completing compiling & linking (but not running) first,
helps to get an better overview of missing functionality in Unikraft
XPDDS19: A Journey through Unikraft's Build System

More Related Content

PDF
Internationalization with TYPO3
PPTX
Autotools pratical training
PPT
From gcc to the autotools
PDF
Gnubs pres-foss-cdac-sem
PDF
Gnubs-pres-foss-cdac-sem
PDF
Linux Internals Part - 2
PDF
Introduction to GNU Make Programming Language
PDF
OS_Compilation_Makefile_kt4jerb34834343553
Internationalization with TYPO3
Autotools pratical training
From gcc to the autotools
Gnubs pres-foss-cdac-sem
Gnubs-pres-foss-cdac-sem
Linux Internals Part - 2
Introduction to GNU Make Programming Language
OS_Compilation_Makefile_kt4jerb34834343553

Similar to XPDDS19: A Journey through Unikraft's Build System (20)

PDF
Build Systems with autoconf, automake and libtool [updated]
PDF
Embedded Systems: Lecture 13: Introduction to GNU Toolchain (Build Tools)
PDF
Compiler design notes phases of compiler
PDF
LOSS_C11- Programming Linux 20221006.pdf
PDF
Makefile
PDF
Autotools
PDF
Course 102: Lecture 22: Package Management
PDF
Don't Fear the Autotools
PPT
Autoconf&Automake
PDF
The Ring programming language version 1.10 book - Part 92 of 212
PDF
Buildroot easy embedded system
PDF
Embedded Linux - Building toolchain
PDF
cmake.pdf
PDF
CMake_Tutorial.pdf
PDF
Embedded Operating System - Linux
PDF
Dependencies Managers in C/C++. Using stdcpp 2014
ODP
Autotools
PDF
An Empirical Study of Unspecified Dependencies in Make-Based Build Systems
PDF
Effective CMake
Build Systems with autoconf, automake and libtool [updated]
Embedded Systems: Lecture 13: Introduction to GNU Toolchain (Build Tools)
Compiler design notes phases of compiler
LOSS_C11- Programming Linux 20221006.pdf
Makefile
Autotools
Course 102: Lecture 22: Package Management
Don't Fear the Autotools
Autoconf&Automake
The Ring programming language version 1.10 book - Part 92 of 212
Buildroot easy embedded system
Embedded Linux - Building toolchain
cmake.pdf
CMake_Tutorial.pdf
Embedded Operating System - Linux
Dependencies Managers in C/C++. Using stdcpp 2014
Autotools
An Empirical Study of Unspecified Dependencies in Make-Based Build Systems
Effective CMake
Ad

More from The Linux Foundation (20)

PDF
ELC2019: Static Partitioning Made Simple
PDF
XPDDS19: How TrenchBoot is Enabling Measured Launch for Open-Source Platform ...
PDF
XPDDS19 Keynote: Xen in Automotive - Artem Mygaiev, Director, Technology Solu...
PDF
XPDDS19 Keynote: Xen Project Weather Report 2019 - Lars Kurth, Director of Op...
PDF
XPDDS19 Keynote: Unikraft Weather Report
PDF
XPDDS19 Keynote: Secret-free Hypervisor: Now and Future - Wei Liu, Software E...
PDF
XPDDS19 Keynote: Xen Dom0-less - Stefano Stabellini, Principal Engineer, Xilinx
PDF
XPDDS19 Keynote: Patch Review for Non-maintainers - George Dunlap, Citrix Sys...
PDF
XPDDS19: Memories of a VM Funk - Mihai Donțu, Bitdefender
PPTX
OSSJP/ALS19: The Road to Safety Certification: Overcoming Community Challeng...
PPTX
OSSJP/ALS19: The Road to Safety Certification: How the Xen Project is Making...
PDF
XPDDS19: Speculative Sidechannels and Mitigations - Andrew Cooper, Citrix
PDF
XPDDS19: Keeping Coherency on Arm: Reborn - Julien Grall, Arm ltd
PDF
XPDDS19: QEMU PV Backend 'qdevification'... What Does it Mean? - Paul Durrant...
PDF
XPDDS19: Status of PCI Emulation in Xen - Roger Pau Monné, Citrix Systems R&D
PDF
XPDDS19: [ARM] OP-TEE Mediator in Xen - Volodymyr Babchuk, EPAM Systems
PDF
XPDDS19: Bringing Xen to the Masses: The Story of Building a Community-driven...
PDF
XPDDS19: Will Robots Automate Your Job Away? Streamlining Xen Project Contrib...
PDF
XPDDS19: Client Virtualization Toolstack in Go - Nick Rosbrook & Brendan Kerr...
PDF
XPDDS19: Core Scheduling in Xen - Jürgen Groß, SUSE
ELC2019: Static Partitioning Made Simple
XPDDS19: How TrenchBoot is Enabling Measured Launch for Open-Source Platform ...
XPDDS19 Keynote: Xen in Automotive - Artem Mygaiev, Director, Technology Solu...
XPDDS19 Keynote: Xen Project Weather Report 2019 - Lars Kurth, Director of Op...
XPDDS19 Keynote: Unikraft Weather Report
XPDDS19 Keynote: Secret-free Hypervisor: Now and Future - Wei Liu, Software E...
XPDDS19 Keynote: Xen Dom0-less - Stefano Stabellini, Principal Engineer, Xilinx
XPDDS19 Keynote: Patch Review for Non-maintainers - George Dunlap, Citrix Sys...
XPDDS19: Memories of a VM Funk - Mihai Donțu, Bitdefender
OSSJP/ALS19: The Road to Safety Certification: Overcoming Community Challeng...
OSSJP/ALS19: The Road to Safety Certification: How the Xen Project is Making...
XPDDS19: Speculative Sidechannels and Mitigations - Andrew Cooper, Citrix
XPDDS19: Keeping Coherency on Arm: Reborn - Julien Grall, Arm ltd
XPDDS19: QEMU PV Backend 'qdevification'... What Does it Mean? - Paul Durrant...
XPDDS19: Status of PCI Emulation in Xen - Roger Pau Monné, Citrix Systems R&D
XPDDS19: [ARM] OP-TEE Mediator in Xen - Volodymyr Babchuk, EPAM Systems
XPDDS19: Bringing Xen to the Masses: The Story of Building a Community-driven...
XPDDS19: Will Robots Automate Your Job Away? Streamlining Xen Project Contrib...
XPDDS19: Client Virtualization Toolstack in Go - Nick Rosbrook & Brendan Kerr...
XPDDS19: Core Scheduling in Xen - Jürgen Groß, SUSE
Ad

Recently uploaded (20)

PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
PPTX
Group 1 Presentation -Planning and Decision Making .pptx
PDF
August Patch Tuesday
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
Chapter 5: Probability Theory and Statistics
PDF
A novel scalable deep ensemble learning framework for big data classification...
PPTX
SOPHOS-XG Firewall Administrator PPT.pptx
PDF
Web App vs Mobile App What Should You Build First.pdf
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
1 - Historical Antecedents, Social Consideration.pdf
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
Enhancing emotion recognition model for a student engagement use case through...
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
PDF
Accuracy of neural networks in brain wave diagnosis of schizophrenia
PDF
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
PDF
Zenith AI: Advanced Artificial Intelligence
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
A Presentation on Artificial Intelligence
MIND Revenue Release Quarter 2 2025 Press Release
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
Group 1 Presentation -Planning and Decision Making .pptx
August Patch Tuesday
Encapsulation_ Review paper, used for researhc scholars
Chapter 5: Probability Theory and Statistics
A novel scalable deep ensemble learning framework for big data classification...
SOPHOS-XG Firewall Administrator PPT.pptx
Web App vs Mobile App What Should You Build First.pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
1 - Historical Antecedents, Social Consideration.pdf
A comparative analysis of optical character recognition models for extracting...
Enhancing emotion recognition model for a student engagement use case through...
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
Accuracy of neural networks in brain wave diagnosis of schizophrenia
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
Zenith AI: Advanced Artificial Intelligence
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
A Presentation on Artificial Intelligence

XPDDS19: A Journey through Unikraft's Build System

  • 1. Simon Kuenzer <simon.kuenzer@neclab.eu> Senior Researcher, NEC Laboratories Europe GmbH Xen Summit 2019, Chicago A Journey through Unikraft’s Build System This work has received funding from the European Union’s Horizon 2020 research and innovation program under grant agreements no. 675806 (“5G CITY”). This work reflects only the author’s views and the European Commission is not responsible for any use that may be made of the information it contains.
  • 4. 4 © NEC Corporation 2019 Unikraft KConfig Makefile System Internal Library Config.uk Makefile.uk Sources Build System: Loading & Parsing Your Application Config.uk Makefile.uk Sources Makefile External Library Config.uk Makefile.uk Sources
  • 5. 5 © NEC Corporation 2019 Unikraft’s 3 Build Stages (1) Fetch Download and decompress external sources e.g., a library hosted on GitHub, Sourceforge Patch downloaded files (2) Prepare Further preparation steps to the sources, for instance: • Call ./configure of downloaded library sources • Generate further sources or headers required for building (3) Compile & Link Compile sources Link libraries Link final images
  • 6. 6 © NEC Corporation 2019 *_SRCS-y *_OBJS-y UK_ALIBS-y UK_OLIBS-y UK_IMAGES-y Build Stage 3: Compiling and Linking libA_a.c libA_b.S libA.ld.o libA.o ... libB.o Images prebuilt.o ... staticA.a ... exportsyms.uk libA_a.o libA_b.o ...... extA.o ...
  • 7. 7 © NEC Corporation 2019 Make parameters ▌Unikraft’s base Makefile $ make A=[APP] L=[LIBRARIES] P=[PLATFORMS] V=[1/0] [target] Parameter Description A=[APP] Path to application directory L=[LIBRARIES] Colon-separated list of paths to external libraries P=[PLATFORMS] Colon-separated list of paths to external platform libs V=[1/0] Verbose mode (on/off) [target] Build target help Show overview of targets menuconfig Configure and select target images (default when there is no .config) all/images Build everything (default) +libs libs Build libraries +prepare prepare Run preparations steps +fetch fetch Download, extract, and patch external code
  • 8. Unikraft Libraries Integrate own libraries and applications
  • 9. 9 © NEC Corporation 2019 Libraries/Applications/Platforms: Necessary files ▌Makefile *applications only  Invoke Unikraft build for simplification ▌Config.uk  Configuration options • Settings saved as part of .config  Specifying library dependencies and depending options ▌Makefile.uk  Registration to the build system  Specification of source files  Extra custom Make rules • For instance for preparing the sources ▌exportsyms.uk  Masking of symbols ▌Linker.uk *platform libraries only  Platform-dependent rules for linking final image .config
  • 10. 10 © NEC Corporation 2019 Makefile *applications only ▌Simplify application building ▌Changes to Unikraft base directory and invokes make UK_ROOT ?= $(PWD)/../../unikraft UK_LIBS ?= $(PWD)/../../libs LIBS := $(UK_LIBS)/libA:$(UK_LIBS)/libB all: @$(MAKE) -C $(UK_ROOT) A=$(PWD) L=$(LIBS) $(MAKECMDGOALS): @$(MAKE) -C $(UK_ROOT) A=$(PWD) L=$(LIBS) (MAKECMDGOALS)
  • 11. 11 © NEC Corporation 2019 Config.uk ▌KConfig syntax1 ▌Structure: Title and Dependencies Configuration parameters (optional) [1] https://www.kernel.org/doc/Documentation/kbuild/kconfig-language.txt
  • 12. 12 © NEC Corporation 2019 Config.uk: Title and Dependencies for Libraries menuconfig defines a submenu for the following configuration options • Note: Use just config without following if-block when the library does not have any configuration parameters select keyword is used to describe dependencies to other libraries • Supports conditional expressions1 • Multiple select lines are possible for a single configuration menuconfig LIBMYLIB bool "ukmylib: my scheduler“ ### libraries are off as default default n ### dependencies select LIBNOLIBC if !HAVE_LIBC if LIBMYLIB ### list of configuration parameters goes here endif
  • 13. 13 © NEC Corporation 2019 Config.uk: Dependencies for Applications Invisible bool config to select dependencies • Applications are not defining an own submenu. Because just a single application can currently be selected for one Unikernel build, applications are enabled as default Like for libraries, select keyword is used to describe dependencies to libraries ### Invisible option (no description) that is set as ### default to ‚y‘ in order to select dependencies config LIBMYAPP bool default y select LIBNOLIBC if !HAVE_LIBC ### list of configuration parameters goes here
  • 14. 14 © NEC Corporation 2019 Config.uk: Configuration Parameter Name-space configuration options! • Prepend library name in front of parameters: here: LIBMYLIB_ Configurations will appear as CONFIG_[CONFIGNAME] in the build system and in the sources (include uk/config.h) (here: CONFIG_LIBMYLIB_SETTING) [type] can be one of bool, int (unsigned), hex, string: Advanced options, like choice lists, are documented at: https://www.kernel.org/doc/Documentation/kbuild/kconfig-language.txt config LIBMYLIB_SETTING [type] "[description]" default [value] select LIBOTHER Makefile.uk #include <uk/config.h> bool y/n „y“ is defined as 1 „n“ is not defined int Hexadecimal Defined as hexadecimal hex Hexadecimal Defined as hexadecimal string String Defined as String
  • 15. 15 © NEC Corporation 2019 Makefile.uk ▌Makefile syntax Unikraft provides helper functions and variables Unikraft expects specific variables to be filled ▌Structure Registration Source files and build flags Custom prepare rules External sources (optional)
  • 16. 16 © NEC Corporation 2019 Makefile.uk: Registration ▌Registration with addlib / addlib_s helpers1 The first thing that has to be done in a Makefile.uk Libraries (depending on being enabled) Replace [libmylib] and CONFIG_LIBMYLIB accordingly: Applications Replace [libmyapp] accordingly: The namespace for variables is defined by application/library name here: prefixed in uppercase: LIBMYLIB_, LIBMYAPP_ The following variables are populated after the call The following overrides are available after the call $(eval $(call addlib_s,[libmylib],$(CONFIG_LIBMYLIB))) [1] support/build/Makefile.rules *_BASE Path to library folder *_BUILD Path to library‘s output/build folder Note: Place all generated files during building in here, never in the base $(eval $(call addlib,[libmyapp])) *_EXPORTS Path to an alternative exportsyms.uk (optional)
  • 17. 17 © NEC Corporation 2019 Makefile.uk: External sources (optional) ▌Download one archive with (additional) sources and extract them with fetch / fetchas helpers1 Example with lwIP: .tar.gz, .tgz, .tar.xz, .txz, and .zip are currently supported1 The following variables are populated after this call ▌If some downloaded source files need to be patched, use patch helper1: Example with lwIP: This command applies all patches found in $(LIBLWIP_PATCHDIR) to the sub- directory $(LIBLWIP_ZIPNAME) of the previously extracted sources LIBLWIP_ZIPNAME=lwip-2.1.2 LIBLWIP_URL=http://download.savannah.nongnu.org/releases/lwip/$(LIBLWIP_ZIPNAME).zip $(eval $(call fetch,liblwip,$(LIBLWIP_URL))) [1] support/build/Makefile.rules *_ORIGIN Path to folder containing extracted archive files LIBLWIP_PATCHDIR=$(LIBLWIP_BASE)/patches $(eval $(call patch,liblwip,$(LIBLWIP_PATCHDIR),$(LIBLWIP_ZIPNAME)))
  • 18. 18 © NEC Corporation 2019 Makefile.uk: Source files ▌Full paths to source files are added to the *_SRCS-y list: Compile rule and target is automatically generated by build system based on file extension: • Currently supported: .S, .sx, .s, .c, .C, .cc, .cp, .cxx, .cpp, .CPP, .c++, .lds.S • .o-binary is created within the library output directory based on the source filename: – Note: Only the filename without extension matters for the target file name. The source file extension and path is irrelevant. In conflicting cases, use variants (see next slide). The following include and build flag lists apply for a source file: # Source file from library directory LIBMYLIB_SRCS-y += $(LIBMYLIB_BASE)/source.c # Source file from extracted archive LIBMYLIB_SRCS-y += $(LIBMYLIB_ORIGIN)/another_src.c CFLAGS-y Global build flags (here: C sources, equivalents for other types exist) CINCLUDES-y Global includes (here: C sources, equivalents for other types exist) *_CFLAGS-y Library-internal build flags (here: C sources, equivalents exist) *_CINCLUDES-y Library-internal includes (here: C sources, equivalents exist) *_[FILENAME]_FLAGS Source file specific build flags *_[FILENAME]_INCLUDES Source file specific includes $(LIBMYLIB_BASE)/source.c.  $(LIBMYLIB_BUILD)/source.o $(LIBMYLIB_ORIGIN)/another_src.c  $(LIBMYLIB_BUILD)/another_src.o
  • 19. 19 © NEC Corporation 2019 Makefile.uk: Source File Variants ▌Variants exist because of two reasons: Conflicting output file names (previous slide) Necessity to compile a single source file multiple times with different flags (e.g., newlib *scanf() variants) ▌Variant names are added with a pipe symbol after source: …produces: Variants have their own specific build flags and includes: LIBMYLIB_SRCS-y += $(LIBMYLIB_BASE)/source.c LIBMYLIB_SRCS-y += $(LIBMYLIB_BASE)/source.c|variant LIBMYLIB_SRCS-y += $(LIBMYLIB_ORIGIN)/source.c|origin $(LIBMYLIB_BASE)/source.c  $(LIBMYLIB_BUILD)/source.o $(LIBMYLIB_BASE)/source.c|variant  $(LIBMYLIB_BUILD)/source.variant.o $(LIBMYLIB_ORIGIN)/source.c|origin  $(LIBMYLIB_BUILD)/source.origin.o *_[FILENAME]_FLAGS Build flags for source file without variant specification *_[FILENAME]_INCLUDES Includes for source file without variant specification *_[FILENAME]_[VARIANT]_FLAGS Build flags for source file for variant [VARIANT] *_[FILENAME]_[VARIANT]_INCLUDES Includes for source file for variant [VARIANT]
  • 20. 20 © NEC Corporation 2019 Makefile.uk: Externally Compiled Sources ▌Unikraft supports including externally compiled sources Cases where it may happen: Code only available as binary form Compiling is done by different build system (e.g., invoked by custom prepare rules) ▌Depending on the type, various places exist to add them: .o-object files are added to the *_OBJS-y list: .o-libraries are registered to the global list UK_OLIBS (remember to use the library switch CONFIG_*): Static libraries are registered to the global list UK_ALIBS (remember to use the library switch CONFIG_*): Note: Shared libraries (.so) are currently not supported LIBMYLIB_OBJS-y += $(LIBMYLIB_BASE)/prebuilt.o UK_ALIBS-$(CONIG_LIBMYLIB) += $(LIBMYLIB_BASE)/static_lib.a UK_OLIBS-$(CONFIG_LIBMYLIB) += $(LIBMYLIB_BASE)/prebuilt_lib.o
  • 21. 21 © NEC Corporation 2019 Makefile.uk: Scope of Headers (Includes) ▌Global headers (e.g., library API) (remember to use the library switch CONFIG_*): ▌Library-Internal headers ▌File-specific headers ▌Equivalent to this, you can set build flags within a specific scope CFLAGS-$(CONFIG_LIBMYLIB), LIBMYLIB_CFLAGS-y, LIBMYLIB_MYSRC_FLAGS, LIBMYLIB_MYSRC_VARIANT_FLAGS CINCLUDES-$(CONFIG_LIBMYLIB) += -I$(LIBMYLIB_BASE)/include/api LIBMYLIB_CINCLUDES-y += -I$(LIBMYLIB_BASE)/include/internal # mysrc.c LIBMYLIB_MYSRC_INCLUDES += -I$(LIBMYLIB_BASE)/include/mysrc # Variant var0 of mysrc.c: mysrc.c|var0 LIBMYLIB_MYSRC_VAR0_INCLUDES += -I$(LIBMYLIB_BASE)/include/mysrc
  • 22. 22 © NEC Corporation 2019 Makefile.uk: Custom Prepare Rules ▌Reason Generate files (headers, sources) needed for build Invoke parts of ported library build system, like ./configure ▌Defined as custom Make rules Use build_cmd to prettify the output1 (in cases where build_cmd is not applicable use verbose_cmd) ▌If used, set marker of fetch stage as dependency Download marker: $(LIBMYLIB_BUILD)/.origin Patched marker: $(LIBMYLIB_BUILD)/.patched ▌Register generated files to prepare stage UK_PREPARE-$(CONFIG_LIBMYLIB) += [generated file/phony] $(LIBMYLIB_BUILD)/generated.h: [dependencies] $(call build_cmd,NM,libmylib,$@,$(NM) -n $(LIBMYLIB_BASE)/symtab.in > $@) [1] support/build/Makefile.rules
  • 23. 23 © NEC Corporation 2019 exportsyms.uk ▌Re-masks the scope of each symbol of a library  Re-defines for each symbol if it is available for final linking  Intended to reduce potential clashing of symbols ▌List of symbol names that should be available globally for final linking. Non-listed symbols become private to the library.  Example (libnolibc): ▌Note: The build system will throw a warning when no exportsyms.uk file is provided  The scope of each library symbol stays unchanged in such a case asprintf vasprintf # comments are ignored opterr optind optopt optreset optarg getopt [...]
  • 25. 25 © NEC Corporation 2019 Best Practices ▌Porting existing libraries/applications is a challenging task  Existing sources often only fit to their build and configuration system  Often not intended to run on something else than Linux (assumptions to the OS) ▌If possible, compile all sources with Unikraft  Including external build binaries is risky • Build flags may be incompatible (e.g., register usage/calling convention, LTO) • Mismatch of depending libraries (external vs. Unikraft‘s version) ▌Learn from existing build system  Extract list of source files and build flags when compiling with original build system  Study steps that generate files needed for the build • Try to run ./configure with settings fitting to Unikraft environment • It is also possible to call ./configure from Unikraft as prepare step ▌Provide initial stubs for missing symbols  Completing compiling & linking (but not running) first, helps to get an better overview of missing functionality in Unikraft