Skip to content

Commit 4de8818

Browse files
committed
feat: Refactor to a header only library
wip: Add wamr samples Signed-off-by: Gordon Smith <GordonJSmith@gmail.com>
1 parent 22c2ac4 commit 4de8818

25 files changed

Lines changed: 570 additions & 593 deletions

.vscode/launch.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@
1414
"preLaunchTask": "CMake: build"
1515
},
1616
{
17-
"name": "(win) component-model-tests",
18-
"type": "cppvsdbg",
17+
"name": "(gdb) wamr-sample",
18+
"type": "cppdbg",
1919
"request": "launch",
20-
"program": "${workspaceFolder}/build/test/Debug/component-model-test.exe",
20+
"program": "${workspaceFolder}/build/samples/wamr/wamr",
2121
"args": [],
22-
"console": "integratedTerminal",
2322
"stopAtEntry": false,
2423
"cwd": "${workspaceFolder}",
2524
"environment": [],
25+
"externalConsole": false,
2626
"preLaunchTask": "CMake: build"
27-
}
27+
},
2828
]
2929
}

CMakeLists.txt

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,41 @@
11
cmake_minimum_required(VERSION 3.5...3.16)
22

3-
project(cmcpp)
3+
project(cmcpp LANGUAGES CXX)
44

5-
set(CMAKE_CXX_STANDARD 20)
6-
set(CMAKE_CXX_STANDARD_REQUIRED ON)
5+
find_package(Boost REQUIRED)
76

8-
find_package(Boost)
7+
add_library(cmcpp INTERFACE)
98

10-
add_library(cmcpp STATIC
11-
src/context.cpp
12-
src/util.cpp
9+
target_include_directories(cmcpp INTERFACE
10+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
11+
$<INSTALL_INTERFACE:include>
1312
)
1413

15-
target_include_directories(cmcpp
16-
INTERFACE include
17-
PRIVATE ${Boost_INCLUDE_DIR}
18-
)
19-
20-
target_link_libraries(cmcpp
21-
)
14+
target_compile_features(cmcpp INTERFACE cxx_std_20)
2215

23-
include_directories(
24-
include
25-
)
16+
if (BUILD_SAMPLES AND EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/samples/CMakeLists.txt")
17+
set(WASI_SDK_PREFIX ${CMAKE_CURRENT_BINARY_DIR}/vcpkg_installed/x64-linux/wasi-sdk)
18+
add_subdirectory(samples)
19+
endif()
2620

2721
if (BUILD_TESTING AND EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/test/CMakeLists.txt")
2822
enable_testing()
2923
add_subdirectory(test)
3024
endif()
25+
26+
include(GNUInstallDirs)
27+
28+
install(DIRECTORY include/
29+
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
30+
)
31+
32+
install(TARGETS cmcpp
33+
EXPORT cmcppTargets
34+
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
35+
)
36+
37+
install(EXPORT cmcppTargets
38+
FILE cmcppTargets.cmake
39+
NAMESPACE cmcpp::
40+
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/cmcpp
41+
)

CMakePresets.json

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,31 +26,35 @@
2626
"installDir": "${sourceDir}/build/stage/Debug",
2727
"cacheVariables": {
2828
"CMAKE_BUILD_TYPE": "Debug",
29-
"BUILD_TESTING": "ON"
29+
"BUILD_TESTING": "ON",
30+
"BUILD_SAMPLES": "ON"
3031
},
3132
"hidden": true
3233
},
3334
{
3435
"name": "Release",
3536
"installDir": "${sourceDir}/build/stage/Release",
3637
"cacheVariables": {
37-
"CMAKE_BUILD_TYPE": "Release"
38+
"CMAKE_BUILD_TYPE": "Release",
39+
"BUILD_SAMPLES": "ON"
3840
},
3941
"hidden": true
4042
},
4143
{
4244
"name": "RelWithDebInfo",
4345
"installDir": "${sourceDir}/build/stage/RelWithDebInfo",
4446
"cacheVariables": {
45-
"CMAKE_BUILD_TYPE": "RelWithDebInfo"
47+
"CMAKE_BUILD_TYPE": "RelWithDebInfo",
48+
"BUILD_SAMPLES": "ON"
4649
},
4750
"hidden": true
4851
},
4952
{
5053
"name": "MinSizeRel",
5154
"installDir": "${sourceDir}/build/stage/MinSizeRel",
5255
"cacheVariables": {
53-
"CMAKE_BUILD_TYPE": "MinSizeRel"
56+
"CMAKE_BUILD_TYPE": "MinSizeRel",
57+
"BUILD_SAMPLES": "ON"
5458
},
5559
"hidden": true
5660
},

include/cmcpp/context.hpp

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
#ifndef CMCPP_CONTEXT_HPP
22
#define CMCPP_CONTEXT_HPP
33

4+
#include "traits.hpp"
5+
6+
#include <functional>
7+
#include <memory>
8+
#include <optional>
49
#if __has_include(<span>)
510
#include <span>
611
#else
712
#include <string>
813
#include <sstream>
914
#endif
1015

11-
#include "traits.hpp"
12-
13-
#include <functional>
14-
#include <memory>
15-
#include <optional>
16-
1716
namespace cmcpp
1817
{
1918
using HostTrap = std::function<void(const char *msg) noexcept(false)>;
@@ -40,10 +39,27 @@ namespace cmcpp
4039
HostTrap trap;
4140
HostUnicodeConversion convert;
4241
GuestRealloc realloc;
43-
std::unique_ptr<CallContext> createCallContext(const GuestMemory &memory, const Encoding &encoding = Encoding::Utf8, const GuestPostReturn &post_return = nullptr);
42+
std::unique_ptr<CallContext> createCallContext(const GuestMemory &memory, const Encoding &guest_encoding = Encoding::Utf8, const GuestPostReturn &post_return = nullptr)
43+
{
44+
auto retVal = std::make_unique<CallContext>();
45+
retVal->trap = trap;
46+
retVal->convert = convert;
47+
retVal->realloc = realloc;
48+
retVal->memory = memory;
49+
retVal->guest_encoding = guest_encoding;
50+
retVal->post_return = post_return;
51+
return retVal;
52+
}
4453
};
4554

46-
std::unique_ptr<InstanceContext> createInstanceContext(const HostTrap &trap, HostUnicodeConversion convert, const GuestRealloc &realloc);
55+
inline std::unique_ptr<InstanceContext> createInstanceContext(const HostTrap &trap, HostUnicodeConversion convert, const GuestRealloc &realloc)
56+
{
57+
auto retVal = std::make_unique<InstanceContext>();
58+
retVal->trap = trap;
59+
retVal->convert = convert;
60+
retVal->realloc = realloc;
61+
return retVal;
62+
}
4763
}
4864

4965
#endif

include/cmcpp/util.hpp

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,39 +7,65 @@ namespace cmcpp
77
{
88
const bool DETERMINISTIC_PROFILE = false;
99

10-
void trap_if(const CallContext &cx, bool condition, const char *message = nullptr) noexcept(false);
10+
inline void trap_if(const CallContext &cx, bool condition, const char *message = nullptr) noexcept(false)
11+
{
12+
if (condition)
13+
{
14+
cx.trap(message);
15+
}
16+
}
17+
18+
inline bool_t convert_int_to_bool(uint8_t i)
19+
{
20+
return i > 0;
21+
}
1122

12-
bool_t convert_int_to_bool(uint8_t i);
23+
inline char_t convert_i32_to_char(const CallContext &cx, int32_t i)
24+
{
25+
trap_if(cx, i >= 0x110000);
26+
trap_if(cx, 0xD800 <= i && i <= 0xDFFF);
27+
return i;
28+
}
29+
30+
inline int32_t char_to_i32(const CallContext &cx, const char_t &v)
31+
{
32+
uint32_t retVal = v;
33+
trap_if(cx, retVal >= 0x110000);
34+
trap_if(cx, 0xD800 <= retVal && retVal <= 0xDFFF);
35+
return retVal;
36+
}
1337

14-
char_t convert_i32_to_char(const CallContext &cx, int32_t i);
15-
int32_t char_to_i32(const CallContext &cx, const char_t &v);
38+
inline int32_t wrap_i64_to_i32(int64_t x)
39+
{
40+
if (x < std::numeric_limits<int32_t>::lowest() || x > std::numeric_limits<int32_t>::max())
41+
{
42+
return std::numeric_limits<int32_t>::lowest();
43+
}
44+
return static_cast<int32_t>(x);
45+
}
1646

1747
class CoreValueIter
1848
{
1949
mutable WasmValVector::const_iterator it;
2050
WasmValVector::const_iterator end;
2151

2252
public:
23-
CoreValueIter(const WasmValVector &v);
53+
CoreValueIter(const WasmValVector &v) : it(v.begin()), end(v.end())
54+
{
55+
}
2456

2557
template <FlatValue T>
2658
T next() const
2759
{
2860
return std::get<T>(next(WasmValTrait<T>::type));
2961
}
30-
virtual WasmVal next(const WasmValType &t) const;
62+
virtual WasmVal next(const WasmValType &t) const
63+
{
64+
assert(it != end);
65+
return *it++;
66+
}
3167
};
3268

33-
class CoerceValueIter : public CoreValueIter
34-
{
35-
const CoreValueIter &vi;
36-
WasmValTypeVector &flat_types;
37-
38-
public:
39-
CoerceValueIter(const CoreValueIter &vi, WasmValTypeVector &flat_types);
40-
41-
virtual WasmVal next(const WasmValType &t) const override;
42-
};
4369
}
4470

4571
#endif

include/cmcpp/variant.hpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,45 @@
1212

1313
namespace cmcpp
1414
{
15+
class CoerceValueIter : public CoreValueIter
16+
{
17+
const CoreValueIter &vi;
18+
WasmValTypeVector &flat_types;
19+
20+
public:
21+
CoerceValueIter(const CoreValueIter &vi, WasmValTypeVector &flat_types) : CoreValueIter({}), vi(vi), flat_types(flat_types)
22+
{
23+
}
24+
25+
virtual WasmVal next(const WasmValType &want) const override
26+
{
27+
auto have = flat_types.front();
28+
flat_types.erase(flat_types.begin());
29+
auto x = vi.next(have);
30+
if (have == WasmValType::i32 && want == WasmValType::f32)
31+
{
32+
return float_::decode_i32_as_float(std::get<int32_t>(x));
33+
}
34+
else if (have == WasmValType::i64 && want == WasmValType::i32)
35+
{
36+
return wrap_i64_to_i32(std::get<int64_t>(x));
37+
}
38+
else if (have == WasmValType::i64 && want == WasmValType::f32)
39+
{
40+
return float_::decode_i32_as_float(wrap_i64_to_i32(std::get<int64_t>(x)));
41+
}
42+
else if (have == WasmValType::i64 && want == WasmValType::f64)
43+
{
44+
return float_::decode_i64_as_float(std::get<int64_t>(x));
45+
}
46+
else
47+
{
48+
assert(have == want);
49+
return x;
50+
}
51+
}
52+
};
53+
1554
namespace variant
1655
{
1756
template <size_t N, Variant T>

samples/CMakeLists.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
include(ExternalProject)
2+
3+
ExternalProject_Add(wasm
4+
SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/wasm
5+
BUILD_ALWAYS TRUE
6+
PREFIX ${CMAKE_CURRENT_BINARY_DIR}/wasm
7+
CMAKE_ARGS
8+
-DHPCC_WASM_ROOT=${HPCC_WASM_ROOT}
9+
-DWASI_SDK_PREFIX=${WASI_SDK_PREFIX}
10+
-DCMAKE_TOOLCHAIN_FILE=${WASI_SDK_PREFIX}/share/cmake/wasi-sdk.cmake
11+
-DCMAKE_SYSROOT=${WASI_SDK_PREFIX}/share/wasi-sysroot
12+
-DCMAKE_INSTALL_PREFIX=${CMAKE_CURRENT_BINARY_DIR}
13+
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
14+
)
15+
16+
add_subdirectory(wamr)

samples/wamr/CMakeLists.txt

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
project(wamr)
2+
3+
find_path(WAMR_INCLUDE_DIRS "wasm_export.h"
4+
PATHS ${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}
5+
)
6+
if (WIN32)
7+
find_library(WAMR_LIB NAMES iwasm.dll vmlib.dll
8+
PATHS ${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}
9+
)
10+
else()
11+
find_library(IWASM_LIB NAMES iwasm
12+
PATHS ${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}
13+
)
14+
find_library(VMLIB_LIB NAMES vmlib
15+
PATHS ${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}
16+
)
17+
endif()
18+
19+
set(CMAKE_CXX_STANDARD 20)
20+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
21+
22+
include_directories(
23+
${CMAKE_CURRENT_LIST_DIR}/../../include
24+
)
25+
26+
add_executable(wamr
27+
main.cpp
28+
)
29+
30+
target_include_directories(wamr
31+
PRIVATE ${WAMR_INCLUDE_DIRS}
32+
)
33+
34+
target_link_libraries(wamr
35+
PRIVATE ${IWASM_LIB}
36+
PRIVATE ${VMLIB_LIB}
37+
)
38+

0 commit comments

Comments
 (0)