forked from bytecodealliance/wasmtime
-
Notifications
You must be signed in to change notification settings - Fork 1
/
CMakeLists.txt
141 lines (129 loc) · 5.38 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
cmake_minimum_required(VERSION 3.10)
set(WASMTIME_USER_CARGO_BUILD_OPTIONS "" CACHE STRING "Additional cargo flags (such as --features) to apply to the build command")
option(BUILD_SHARED_LIBS "Build using shared libraries" OFF)
option(WASMTIME_ALWAYS_BUILD "If cmake should always invoke cargo to build wasmtime" ON)
if(CMAKE_BUILD_TYPE STREQUAL "Release")
set(WASMTIME_BUILD_TYPE_FLAG "--release")
set(WASMTIME_BUILD_TYPE "release")
else()
set(WASMTIME_BUILD_TYPE "debug")
endif()
if(BUILD_SHARED_LIBS)
# Copy shared library into build directory
if(WIN32)
set(WASMTIME_INSTALL_COMMAND ${CMAKE_COMMAND} -E copy_if_different
${CMAKE_CURRENT_SOURCE_DIR}/../../target/${WASMTIME_BUILD_TYPE}/wasmtime.dll
${CMAKE_BINARY_DIR})
elseif(APPLE)
set(WASMTIME_INSTALL_COMMAND ${CMAKE_COMMAND} -E copy_if_different
${CMAKE_CURRENT_SOURCE_DIR}/../../target/${WASMTIME_BUILD_TYPE}/libwasmtime.dylib
${CMAKE_BINARY_DIR})
else()
set(WASMTIME_INSTALL_COMMAND ${CMAKE_COMMAND} -E copy_if_different
${CMAKE_CURRENT_SOURCE_DIR}/../../target/${WASMTIME_BUILD_TYPE}/libwasmtime.so
${CMAKE_BINARY_DIR})
endif()
endif()
if(ANDROID)
# TODO wasmtime only supports arm64-v8a right now
if(ANDROID_ABI STREQUAL "armeabi-v7a")
set(ANDROID_TARGET "armv7-linux-androideabi")
set(ANDROID_ARCH_SHORT "arm")
elseif(ANDROID_ABI STREQUAL "arm64-v8a")
set(ANDROID_TARGET "aarch64-linux-android")
set(ANDROID_ARCH_SHORT "aarch64")
elseif(ANDROID_ABI STREQUAL "x86")
set(ANDROID_TARGET "i686-linux-android")
set(ANDROID_ARCH_SHORT "i386")
elseif(ANDROID_ABI STREQUAL "x86_64")
set(ANDROID_TARGET "x86_64-linux-android")
set(ANDROID_ARCH_SHORT "x86_64")
endif()
set(WASMTIME_BUILD_TARGET "--target=${ANDROID_TARGET}")
endif()
if (BUILD_SHARED_LIBS AND ANDROID)
message(FATAL_ERROR "Wasmtime cannot be built with BUILD_SHARED_LIBS on Android")
endif()
if(BUILD_SHARED_LIBS)
if(WIN32)
set(WASMTIME_BUILD_PRODUCT
${CMAKE_CURRENT_SOURCE_DIR}/../../target/${WASMTIME_BUILD_TYPE}/wasmtime.dll.lib)
elseif(APPLE)
set(WASMTIME_BUILD_PRODUCT
${CMAKE_CURRENT_SOURCE_DIR}/../../target/${WASMTIME_BUILD_TYPE}/libwasmtime.dylib)
else()
set(WASMTIME_BUILD_PRODUCT
${CMAKE_CURRENT_SOURCE_DIR}/../../target/${WASMTIME_BUILD_TYPE}/libwasmtime.so)
endif()
else()
if(WIN32)
set(WASMTIME_BUILD_PRODUCT
${CMAKE_CURRENT_SOURCE_DIR}/../../target/${WASMTIME_BUILD_TYPE}/wasmtime.lib)
elseif(ANDROID)
set(WASMTIME_BUILD_PRODUCT
${CMAKE_CURRENT_SOURCE_DIR}/../../target/${ANDROID_TARGET}/${WASMTIME_BUILD_TYPE}/libwasmtime.a)
else()
set(WASMTIME_BUILD_PRODUCT
${CMAKE_CURRENT_SOURCE_DIR}/../../target/${WASMTIME_BUILD_TYPE}/libwasmtime.a)
endif()
endif()
if(ANDROID)
# Rust attempts to use libgcc.a on NDK versions r23-beta3 and up
# but it has been replaced with libunwind.a (rust-lang/rust#85806)
file(WRITE ${CMAKE_BINARY_DIR}/libgcc.a "INPUT(-lunwind)")
# The version of the clang compiler is part of the libunwind.a path
file(STRINGS ${ANDROID_TOOLCHAIN_ROOT}/AndroidVersion.txt CLANG_VERSION_FILE)
list(GET CLANG_VERSION_FILE 0 CLANG_VERSION)
# Some crates use the compiler directly, environment variables
# are set to make them use the Android compiler
set(WASMTIME_PREBUILD_COMMAND ${CMAKE_COMMAND} -E env
CC=${ANDROID_TOOLCHAIN_ROOT}/bin/clang
AR=${ANDROID_TOOLCHAIN_ROOT}/bin/llvm-ar
"RUSTFLAGS=-L ${CMAKE_SYSROOT}/usr/lib/${ANDROID_TARGET}/${ANDROID_NATIVE_API_LEVEL} \
-L ${ANDROID_TOOLCHAIN_ROOT}/lib64/clang/${CLANG_VERSION}/lib/linux/${ANDROID_ARCH_SHORT} \
-L ${CMAKE_BINARY_DIR} -C linker=${ANDROID_TOOLCHAIN_ROOT}/bin/ld")
endif()
include(ExternalProject)
find_program(WASMTIME_CARGO_BINARY cargo)
ExternalProject_Add(
wasmtime-crate
DOWNLOAD_COMMAND ""
CONFIGURE_COMMAND ""
INSTALL_COMMAND "${WASMTIME_INSTALL_COMMAND}"
BUILD_COMMAND ${WASMTIME_PREBUILD_COMMAND} ${WASMTIME_CARGO_BINARY} build ${WASMTIME_BUILD_TYPE_FLAG} ${WASMTIME_USER_CARGO_BUILD_OPTIONS} ${WASMTIME_BUILD_TARGET}
BINARY_DIR ${CMAKE_CURRENT_SOURCE_DIR}/artifact
BUILD_ALWAYS ${WASMTIME_ALWAYS_BUILD}
BUILD_BYPRODUCTS ${WASMTIME_BUILD_PRODUCT})
add_library(wasmtime INTERFACE)
add_dependencies(wasmtime wasmtime-crate)
if(BUILD_SHARED_LIBS)
if(NOT WIN32)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-rpath='$ORIGIN'")
endif()
target_link_libraries(wasmtime INTERFACE ${WASMTIME_BUILD_PRODUCT})
else()
if(WIN32)
target_compile_options(wasmtime INTERFACE -DWASM_API_EXTERN= -DWASI_API_EXTERN=)
target_link_libraries(wasmtime INTERFACE ${WASMTIME_BUILD_PRODUCT}
ws2_32 advapi32 userenv ntdll shell32 ole32 bcrypt)
elseif(APPLE OR ANDROID)
target_link_libraries(wasmtime INTERFACE ${WASMTIME_BUILD_PRODUCT})
else()
target_link_libraries(wasmtime INTERFACE ${WASMTIME_BUILD_PRODUCT}
pthread dl m)
endif()
endif()
target_include_directories(wasmtime INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/include
${CMAKE_CURRENT_SOURCE_DIR}/wasm-c-api/include)
include(GNUInstallDirs)
install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/wasmtime.h
${CMAKE_CURRENT_SOURCE_DIR}/include/wasi.h
${CMAKE_CURRENT_SOURCE_DIR}/include/doc-wasm.h
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/wasm-c-api/include/wasm.h
${CMAKE_CURRENT_SOURCE_DIR}/wasm-c-api/include/wasm.hh
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/wasmtime
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
install(FILES ${WASMTIME_BUILD_PRODUCT}
DESTINATION ${CMAKE_INSTALL_LIBDIR})