Skip to content

Commit 0599dc2

Browse files
committed
WIP
1 parent 39a282e commit 0599dc2

32 files changed

+2977
-407
lines changed

CMakeLists.txt

Lines changed: 30 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,22 @@ project(cmcpp
77
LANGUAGES CXX
88
)
99

10+
# ============================================================================
11+
# CMake Module Path
12+
# ============================================================================
13+
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
14+
15+
# ============================================================================
16+
# Build Options
17+
# ============================================================================
1018
# Determine default for BUILD_SAMPLES: OFF on Windows, ON elsewhere
11-
set(_BUILD_SAMPLES_DEFAULT ON)
12-
if (WIN32)
13-
set(_BUILD_SAMPLES_DEFAULT ON)
14-
endif()
15-
option(BUILD_SAMPLES "Build samples" ${_BUILD_SAMPLES_DEFAULT})
19+
option(BUILD_SAMPLES "Build samples" ON)
1620
option(BUILD_TESTING "Build tests" ON)
17-
option(WIT_CODEGEN "Generate code from ANTLR grammar" ON)
21+
option(BUILD_WIT_CODEGEN "Generate code from ANTLR grammar" ON)
1822

23+
# ============================================================================
24+
# Core Library Target
25+
# ============================================================================
1926
add_library(cmcpp INTERFACE)
2027

2128
target_include_directories(cmcpp INTERFACE
@@ -25,45 +32,37 @@ target_include_directories(cmcpp INTERFACE
2532

2633
target_compile_features(cmcpp INTERFACE cxx_std_20)
2734

35+
# ============================================================================
36+
# Testing Setup
37+
# ============================================================================
2838
# Enable testing early so subdirectories (including samples) can register tests
29-
if (BUILD_TESTING)
39+
if(BUILD_TESTING)
3040
enable_testing()
3141
endif()
3242

33-
if (BUILD_SAMPLES AND EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/samples/CMakeLists.txt")
34-
if (NOT DEFINED WASI_SDK_PREFIX OR WASI_SDK_PREFIX STREQUAL "")
35-
set(_wasi_sdk_candidates)
36-
if (DEFINED VCPKG_TARGET_TRIPLET)
37-
list(APPEND _wasi_sdk_candidates "${CMAKE_CURRENT_BINARY_DIR}/vcpkg_installed/${VCPKG_TARGET_TRIPLET}/wasi-sdk")
38-
endif()
39-
file(GLOB _wasi_sdk_glob "${CMAKE_CURRENT_BINARY_DIR}/vcpkg_installed/*/wasi-sdk")
40-
list(APPEND _wasi_sdk_candidates ${_wasi_sdk_glob})
41-
list(FILTER _wasi_sdk_candidates EXCLUDE REGEX "^$")
42-
list(REMOVE_DUPLICATES _wasi_sdk_candidates)
43-
if (_wasi_sdk_candidates)
44-
list(GET _wasi_sdk_candidates 0 _wasi_sdk_default)
45-
set(WASI_SDK_PREFIX "${_wasi_sdk_default}" CACHE PATH "Path to the WASI SDK used by the samples" FORCE)
46-
else()
47-
set(WASI_SDK_PREFIX "" CACHE PATH "Path to the WASI SDK used by the samples" FORCE)
48-
endif()
49-
endif()
50-
message ("WASI_SDK_PREFIX: ${WASI_SDK_PREFIX}")
43+
# ============================================================================
44+
# Subdirectories
45+
# ============================================================================
46+
# Samples
47+
if(BUILD_SAMPLES)
5148
add_subdirectory(samples)
5249
endif()
5350

51+
# Grammar and code generation tools
5452
# Grammar must be added before tools/test so the generate-grammar target is available
55-
if (WIT_CODEGEN AND EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/grammar/CMakeLists.txt")
53+
if(BUILD_WIT_CODEGEN)
5654
add_subdirectory(grammar)
57-
# Add tools that use the grammar
58-
if (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/tools/wit-codegen/CMakeLists.txt")
59-
add_subdirectory(tools/wit-codegen)
60-
endif()
55+
add_subdirectory(tools/wit-codegen)
6156
endif()
6257

63-
if (BUILD_TESTING AND EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/test/CMakeLists.txt")
58+
# Tests
59+
if(BUILD_TESTING)
6460
add_subdirectory(test)
6561
endif()
6662

63+
# ============================================================================
64+
# Installation
65+
# ============================================================================
6766
include(GNUInstallDirs)
6867
include(CMakePackageConfigHelpers)
6968

@@ -105,44 +104,6 @@ install(FILES
105104
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/cmcpp
106105
)
107106

108-
# Install wit-codegen tool if it was built
109-
if(WIT_CODEGEN AND TARGET wit-codegen)
110-
install(TARGETS wit-codegen
111-
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
112-
COMPONENT tools
113-
)
114-
115-
# Install ANTLR4 runtime DLL on Windows
116-
if(WIN32)
117-
# Find and install the ANTLR4 runtime DLL
118-
set(ANTLR4_DLL_PATH "${CMAKE_BINARY_DIR}/vcpkg_installed/${VCPKG_TARGET_TRIPLET}/bin/antlr4-runtime.dll")
119-
if(EXISTS "${ANTLR4_DLL_PATH}")
120-
install(FILES "${ANTLR4_DLL_PATH}"
121-
DESTINATION ${CMAKE_INSTALL_BINDIR}
122-
COMPONENT tools
123-
)
124-
message(STATUS "Will install ANTLR4 runtime DLL: ${ANTLR4_DLL_PATH}")
125-
else()
126-
message(WARNING "ANTLR4 runtime DLL not found at: ${ANTLR4_DLL_PATH}")
127-
endif()
128-
elseif(UNIX AND TARGET antlr4_shared)
129-
# On Linux/macOS, try to find and install the shared library
130-
if(APPLE)
131-
set(ANTLR4_LIB_PATTERN "libantlr4-runtime*.dylib")
132-
else()
133-
set(ANTLR4_LIB_PATTERN "libantlr4-runtime.so*")
134-
endif()
135-
136-
file(GLOB ANTLR4_LIBS "${CMAKE_BINARY_DIR}/vcpkg_installed/${VCPKG_TARGET_TRIPLET}/lib/${ANTLR4_LIB_PATTERN}")
137-
if(ANTLR4_LIBS)
138-
install(FILES ${ANTLR4_LIBS}
139-
DESTINATION ${CMAKE_INSTALL_LIBDIR}
140-
COMPONENT tools
141-
)
142-
endif()
143-
endif()
144-
endif()
145-
146107
# CPack configuration for package generation
147108
set(CPACK_PACKAGE_NAME "cmcpp")
148109
set(CPACK_PACKAGE_VENDOR "GordonSmith")

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,14 @@ ctest --test-dir build -C Debug --output-on-failure
8585
```
8686
Header-only packaging (no tests/samples/wit-codegen):
8787
```bash
88-
cmake -DBUILD_TESTING=OFF -DBUILD_SAMPLES=OFF -DWIT_CODEGEN=OFF -S . -B build
88+
cmake -DBUILD_TESTING=OFF -DBUILD_SAMPLES=OFF -DBUILD_WIT_CODEGEN=OFF -S . -B build
8989
cmake --build build
9090
```
9191

9292
#### Default Options
9393
- `BUILD_TESTING` (ON)
9494
- `BUILD_SAMPLES` (ON)
95-
- `WIT_CODEGEN` (ON)
95+
- `BUILD_WIT_CODEGEN` (ON)
9696

9797
#### Preset Build (recommended)
9898

cmake/CompilerOptions.cmake

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# CompilerOptions.cmake
2+
# Centralized compiler flags and options for the project
3+
4+
# Function to add coverage support to a target
5+
function(add_coverage_support target_name)
6+
if(CMAKE_COMPILER_IS_GNUCXX)
7+
target_compile_options(${target_name} PRIVATE --coverage)
8+
target_link_options(${target_name} PRIVATE --coverage)
9+
elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
10+
target_compile_options(${target_name} PRIVATE -fprofile-instr-generate -fcoverage-mapping)
11+
target_link_options(${target_name} PRIVATE -fprofile-instr-generate)
12+
endif()
13+
endfunction()
14+
15+
# Function to suppress WebAssembly narrowing warnings
16+
function(suppress_wasm_narrowing_warnings target_name)
17+
if(MSVC)
18+
target_compile_options(${target_name} PRIVATE
19+
/wd4244 # conversion from 'type1' to 'type2', possible loss of data
20+
/wd4267 # conversion from 'size_t' to 'type', possible loss of data
21+
/wd4305 # truncation from 'type1' to 'type2'
22+
/wd4309 # truncation of constant value
23+
)
24+
endif()
25+
endfunction()
26+
27+
# Function to set WAMR-specific compile options
28+
function(add_wamr_compile_options target_name)
29+
get_target_property(_target_type ${target_name} TYPE)
30+
if(_target_type STREQUAL "INTERFACE_LIBRARY")
31+
set(_scope INTERFACE)
32+
else()
33+
set(_scope PRIVATE)
34+
endif()
35+
36+
if(MSVC)
37+
target_compile_options(${target_name} ${_scope}
38+
/EHsc
39+
/permissive-
40+
/wd4244 # conversion warnings
41+
/wd4267 # size_t conversion warnings
42+
/wd4305 # truncation warnings
43+
/wd4309 # constant truncation warnings
44+
)
45+
else()
46+
target_compile_options(${target_name} ${_scope}
47+
-Wno-error=maybe-uninitialized
48+
-Wno-error=jump-misses-init
49+
)
50+
endif()
51+
endfunction()

cmake/FindWAMR.cmake

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
# FindWAMR.cmake
2+
# Locates and validates WAMR runtime libraries
3+
#
4+
# Provides:
5+
# WAMR_FOUND - TRUE if WAMR was found
6+
# WAMR_INCLUDE_DIRS - WAMR include directories
7+
# WAMR_LIBRARIES - WAMR libraries to link against
8+
# wamr::runtime - Imported interface target
9+
10+
if(NOT DEFINED VCPKG_INSTALLED_DIR OR NOT DEFINED VCPKG_TARGET_TRIPLET)
11+
set(WAMR_FOUND FALSE)
12+
return()
13+
endif()
14+
15+
# Find WAMR headers
16+
find_path(WAMR_INCLUDE_DIRS
17+
NAMES wasm_export.h
18+
PATHS ${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/include
19+
NO_DEFAULT_PATH
20+
)
21+
22+
# Find WAMR libraries
23+
set(_wamr_lib_dirs
24+
${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/lib
25+
${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/lib/manual-link
26+
${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/bin
27+
)
28+
set(_wamr_lib_dirs_debug
29+
${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/debug/lib
30+
${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/debug/lib/manual-link
31+
${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/debug/bin
32+
)
33+
34+
set(_wamr_iwasm_names iwasm libiwasm iwasm_static)
35+
set(_wamr_vmlib_names vmlib libvmlib vmlib_static)
36+
37+
find_library(WAMR_IWASM_LIB_RELEASE NAMES ${_wamr_iwasm_names}
38+
PATHS ${_wamr_lib_dirs}
39+
NO_DEFAULT_PATH
40+
)
41+
find_library(WAMR_IWASM_LIB_DEBUG NAMES ${_wamr_iwasm_names}
42+
PATHS ${_wamr_lib_dirs_debug}
43+
NO_DEFAULT_PATH
44+
)
45+
46+
set(WAMR_IWASM_LIB "")
47+
if(WAMR_IWASM_LIB_RELEASE)
48+
set(WAMR_IWASM_LIB ${WAMR_IWASM_LIB_RELEASE})
49+
elseif(WAMR_IWASM_LIB_DEBUG)
50+
set(WAMR_IWASM_LIB ${WAMR_IWASM_LIB_DEBUG})
51+
endif()
52+
53+
find_library(WAMR_VMLIB_LIB_RELEASE NAMES ${_wamr_vmlib_names}
54+
PATHS ${_wamr_lib_dirs}
55+
NO_DEFAULT_PATH
56+
)
57+
find_library(WAMR_VMLIB_LIB_DEBUG NAMES ${_wamr_vmlib_names}
58+
PATHS ${_wamr_lib_dirs_debug}
59+
NO_DEFAULT_PATH
60+
)
61+
62+
set(WAMR_VMLIB_LIB "")
63+
if(WAMR_VMLIB_LIB_RELEASE)
64+
set(WAMR_VMLIB_LIB ${WAMR_VMLIB_LIB_RELEASE})
65+
elseif(WAMR_VMLIB_LIB_DEBUG)
66+
set(WAMR_VMLIB_LIB ${WAMR_VMLIB_LIB_DEBUG})
67+
endif()
68+
69+
# Determine if WAMR was found
70+
if(WAMR_INCLUDE_DIRS AND WAMR_IWASM_LIB)
71+
set(WAMR_FOUND TRUE)
72+
73+
# Build library list
74+
set(WAMR_LIBRARIES ${WAMR_IWASM_LIB})
75+
if(WAMR_VMLIB_LIB)
76+
list(APPEND WAMR_LIBRARIES ${WAMR_VMLIB_LIB})
77+
endif()
78+
if(NOT WIN32)
79+
list(APPEND WAMR_LIBRARIES m)
80+
endif()
81+
82+
# Create imported target
83+
if(NOT TARGET wamr::runtime)
84+
add_library(wamr::runtime INTERFACE IMPORTED)
85+
set_target_properties(wamr::runtime PROPERTIES
86+
INTERFACE_INCLUDE_DIRECTORIES "${WAMR_INCLUDE_DIRS}"
87+
INTERFACE_LINK_LIBRARIES "${WAMR_LIBRARIES}"
88+
)
89+
endif()
90+
91+
if(NOT WAMR_FIND_QUIETLY)
92+
message(STATUS "Found WAMR:")
93+
message(STATUS " Include dirs: ${WAMR_INCLUDE_DIRS}")
94+
message(STATUS " IWASM library: ${WAMR_IWASM_LIB}")
95+
if(WAMR_VMLIB_LIB)
96+
message(STATUS " VMLIB library: ${WAMR_VMLIB_LIB}")
97+
endif()
98+
endif()
99+
else()
100+
set(WAMR_FOUND FALSE)
101+
if(NOT WAMR_FIND_QUIETLY)
102+
if(WAMR_FIND_REQUIRED)
103+
message(FATAL_ERROR "WAMR not found. Install via: vcpkg install wasm-micro-runtime")
104+
else()
105+
message(STATUS "WAMR not found")
106+
endif()
107+
endif()
108+
endif()
109+
110+
# Standard CMake find package handling
111+
include(FindPackageHandleStandardArgs)
112+
find_package_handle_standard_args(WAMR
113+
REQUIRED_VARS WAMR_INCLUDE_DIRS WAMR_IWASM_LIB
114+
FAIL_MESSAGE "WAMR not found. Install via: vcpkg install wasm-micro-runtime"
115+
)
116+
117+
mark_as_advanced(
118+
WAMR_INCLUDE_DIRS
119+
WAMR_IWASM_LIB_RELEASE
120+
WAMR_IWASM_LIB_DEBUG
121+
WAMR_VMLIB_LIB_RELEASE
122+
WAMR_VMLIB_LIB_DEBUG
123+
)

cmake/FindWASISDK.cmake

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# FindWASISDK.cmake
2+
# Locates WASI SDK for WebAssembly guest compilation
3+
#
4+
# Provides:
5+
# WASI_SDK_FOUND - TRUE if WASI SDK was found
6+
# WASI_SDK_PREFIX - Path to WASI SDK installation
7+
8+
function(find_wasi_sdk)
9+
if(NOT DEFINED WASI_SDK_PREFIX OR WASI_SDK_PREFIX STREQUAL "")
10+
set(_wasi_sdk_candidates)
11+
12+
# Check vcpkg installation
13+
if(DEFINED VCPKG_TARGET_TRIPLET)
14+
list(APPEND _wasi_sdk_candidates
15+
"${CMAKE_BINARY_DIR}/vcpkg_installed/${VCPKG_TARGET_TRIPLET}/wasi-sdk")
16+
endif()
17+
18+
# Glob for any vcpkg installation
19+
file(GLOB _wasi_sdk_glob "${CMAKE_BINARY_DIR}/vcpkg_installed/*/wasi-sdk")
20+
list(APPEND _wasi_sdk_candidates ${_wasi_sdk_glob})
21+
22+
# Clean up list
23+
list(FILTER _wasi_sdk_candidates EXCLUDE REGEX "^$")
24+
list(REMOVE_DUPLICATES _wasi_sdk_candidates)
25+
26+
if(_wasi_sdk_candidates)
27+
list(GET _wasi_sdk_candidates 0 _wasi_sdk_default)
28+
set(WASI_SDK_PREFIX "${_wasi_sdk_default}" CACHE PATH
29+
"Path to the WASI SDK installation" FORCE)
30+
set(WASI_SDK_FOUND TRUE PARENT_SCOPE)
31+
else()
32+
set(WASI_SDK_PREFIX "" CACHE PATH
33+
"Path to the WASI SDK installation" FORCE)
34+
set(WASI_SDK_FOUND FALSE PARENT_SCOPE)
35+
endif()
36+
else()
37+
# WASI_SDK_PREFIX was already set
38+
if(EXISTS "${WASI_SDK_PREFIX}")
39+
set(WASI_SDK_FOUND TRUE PARENT_SCOPE)
40+
else()
41+
set(WASI_SDK_FOUND FALSE PARENT_SCOPE)
42+
endif()
43+
endif()
44+
45+
# Propagate to parent scope
46+
set(WASI_SDK_PREFIX "${WASI_SDK_PREFIX}" PARENT_SCOPE)
47+
48+
if(NOT WASI_SDK_FIND_QUIETLY)
49+
if(WASI_SDK_FOUND)
50+
message(STATUS "Found WASI SDK: ${WASI_SDK_PREFIX}")
51+
elseif(WASI_SDK_FIND_REQUIRED)
52+
message(FATAL_ERROR "WASI SDK not found. Install via: vcpkg install wasi-sdk")
53+
else()
54+
message(STATUS "WASI SDK not found")
55+
endif()
56+
endif()
57+
endfunction()
58+
59+
# Execute the search
60+
find_wasi_sdk()
61+
62+
# Standard CMake find package handling
63+
include(FindPackageHandleStandardArgs)
64+
find_package_handle_standard_args(WASISDK
65+
REQUIRED_VARS WASI_SDK_PREFIX
66+
FAIL_MESSAGE "WASI SDK not found. Install via: vcpkg install wasi-sdk"
67+
)
68+
69+
mark_as_advanced(WASI_SDK_PREFIX)

0 commit comments

Comments
 (0)