CMakeLists.txt 3.85 KB
Newer Older
1 2 3 4 5 6 7 8 9
cmake_minimum_required(VERSION 3.10)
project(context_switcher
        VERSION 0.0.1
        DESCRIPTION "allows to execute functions and lambdas on a new stack and switch between them"
        LANGUAGES CXX ASM)

set(CMAKE_CXX_STANDARD 11)

# Platform Support - Edit this when porting the context switch facility.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
# Settings:
# CS_USE_BOOST              = ON/OFF        Use boost's fcontext as the assembly implementation for the context switch.
# CS_USE_FAST               = ON/OFF        Use custom implementation optimized for fast creation/return context switch.
# CMAKE_SYSTEM_PROCESSOR    = String        Target processor
# CMAKE_SYSTEM_NAME         = String        Target
#
# Typically cross compiling in cmake will set CMAKE_SYSTEM_PROCESSOR and CMAKE_SYSTEM_NAME.
# The library uses these two to select the correct assembly file for performing the context switch.
# We currently only include minimal system support in our config file. The fastest way to port the
# library is to see if one of the boost.context assembly files matches your target platform and adding
# that to tho selection script.
#
# By default we use CS_USE_FAST if available and fall back to CS_USE_BOOST if needed.
# When sanitizers are turned on (or other instrumentation) we recommend to use CS_USE_BOOST
# as its calling convention causes no problems with function entry/exit instrumentation.

message("-- Configure Context Switcher: ${CMAKE_SYSTEM_PROCESSOR} running ${CMAKE_SYSTEM_NAME}")
include(asm/cscontext/SelectAssemblyFiles.cmake)
include(asm/fcontext/SelectAssemblyFiles.cmake)

if (CS_CSCONTEXT_FOUND AND NOT THREAD_SANITIZER AND NOT CS_FORCE_FALLBACK)
    MESSAGE("-- Using cscontext implementation")
    SET(CS_CONTEXT_SWITCH_ASSEMBLY ${CS_CSCONTEXT_ASSEMBLY})
    SET(CS_USE_CSCONTEXT TRUE)
elseif (CS_FCONTEXT_FOUND)
    if (THREAD_SANITIZER)
        MESSAGE("-- Falling back to fcontext implementation (thread sanitizer active)")
    else ()
        MESSAGE("-- Falling back to fcontext implementation")
    endif ()
    SET(CS_CONTEXT_SWITCH_ASSEMBLY ${CS_FCONTEXT_ASSEMBLY})
    SET(CS_USE_CSCONTEXT FALSE)
42 43 44
else ()
    MESSAGE(FATAL_ERROR "Platform (${CMAKE_SYSTEM_PROCESSOR} on ${CMAKE_SYSTEM_NAME}) not supported! Please see Readme for instructions to port.")
endif ()
45
message("-- Configure Context Switcher: Configuration Done")
46 47

add_library(context_switcher STATIC
48
        ${CS_CONTEXT_SWITCH_ASSEMBLY}
49
        include/context_switcher/context_switcher.h src/context_switcher.cpp
50 51
        include/context_switcher/cscontext.h
        include/context_switcher/fcontext.h
52
        include/context_switcher/continuation.h
53
        include/context_switcher/context.h)
54 55 56

if (CS_USE_CSCONTEXT)
    target_compile_definitions(context_switcher PUBLIC CS_USE_CSCONTEXT)
57 58
else ()
    target_compile_definitions(context_switcher PUBLIC CS_USE_FCONTEXT)
59
endif ()
60 61 62 63 64 65 66

# Add everything in `./include` to be in the include path of this project
target_include_directories(context_switcher
        PUBLIC
        $<INSTALL_INTERFACE:include>
        $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
        PRIVATE
67
        ${CMAKE_CURRENT_SOURCE_DIR}/src # TODO: Set this up when we require private headers
68 69
        )

70 71
# Rules for installing the library on a system
# ...binaries
72
INSTALL(TARGETS context_switcher
73
        EXPORT context_switcher-targets
74 75 76 77 78
        LIBRARY DESTINATION lib/context_switcher
        ARCHIVE DESTINATION lib/context_switcher
        )
# ...all headers in `include`
INSTALL(
79
        DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/context_switcher
80 81 82
        DESTINATION include
        FILES_MATCHING PATTERN "*.h*"
)
83 84 85 86 87 88 89 90 91 92 93
# ...allow our project to be a cmake dependency
install(
        EXPORT context_switcher-targets
        FILE context_switcherTargets.cmake
        NAMESPACE context_switcher::
        DESTINATION lib/context_switcher
)
INSTALl(
        FILES context_switcher-config.cmake
        DESTINATION lib/context_switcher
)
94