build: cmake: build .wat from source files

we compile .wat files from .rs and .c source files since
6d89d718d9.
these .wat are used by test/cql-pytest/test_wasm.py . let's update
the CMake building system accordingly so these .wat files can also
be generated using the "wasm" target. since the ctest system is
not used. this change should allow us to perform this test manually.

Signed-off-by: Kefu Chai <kefu.chai@scylladb.com>

Closes #14126
This commit is contained in:
Kefu Chai
2023-06-04 16:11:45 +08:00
committed by Nadav Har'El
parent a12d2d5f16
commit 3cd9aa1448
4 changed files with 124 additions and 0 deletions

View File

@@ -76,4 +76,5 @@ if(BUILD_TESTING)
add_subdirectory(boost)
add_subdirectory(manual)
add_subdirectory(unit)
add_subdirectory(resource/wasm)
endif()

View File

@@ -0,0 +1,21 @@
find_program(WASM2WAT wasm2wat)
if(NOT WASM2WAT)
message(FATAL_ERROR "wasm2wat is required")
endif()
function(wasm2wat input)
cmake_parse_arguments(parsed_args "" "WAT;OUT_DIR" "" ${ARGN})
get_filename_component(basename ${input} NAME_WE)
set(output_dir "${parsed_args_OUT_DIR}")
set(output "${output_dir}/${basename}.wat")
add_custom_command(
OUTPUT ${output}
COMMAND ${WASM2WAT} ${input} --output=${output}
DEPENDS ${input}
COMMENT "Generating WebAssembly Text ${output}")
set(${parsed_args_WAT} ${output} PARENT_SCOPE)
endfunction()
add_custom_target(wasm)
add_subdirectory(c)
add_subdirectory(rust)

View File

@@ -0,0 +1,46 @@
find_program(CLANG clang)
if(NOT CLANG)
message(FATAL_ERROR "clang is required")
endif()
function(compile_c_to_wasm input)
cmake_parse_arguments(parsed_args "" "WASM;OUT_DIR" "" ${ARGN})
get_filename_component(basename ${input} NAME_WE)
set(input "${CMAKE_CURRENT_SOURCE_DIR}/${input}")
set(output_dir "${parsed_args_OUT_DIR}")
set(output "${output_dir}/${basename}.wasm")
set(target "wasm32")
add_custom_command(
OUTPUT ${output}
COMMAND ${CLANG} ${input}
--target=wasm32
--no-standard-libraries
-Wl,--export-all -Wl,--no-entry
-o ${output}
DEPENDS ${input}
COMMENT "Building WASM ${output}")
set(${parsed_args_WASM} ${output} PARENT_SCOPE)
endfunction(compile_c_to_wasm)
set(c_srcs
test_UDA_final.c
test_UDA_scalar.c
test_fib_called_on_null.c
test_mem_grow.c
test_pow.c
test_word_double.c)
foreach(c_src ${c_srcs})
compile_c_to_wasm(${c_src}
OUT_DIR "${CMAKE_BINARY_DIR}/wasm"
WASM wasm)
wasm2wat(${wasm}
OUT_DIR "${CMAKE_BINARY_DIR}/wasm"
WAT wat)
list(APPEND wasms ${wat})
endforeach()
add_custom_target(wasm_c
DEPENDS ${wasms})
add_dependencies(wasm
wasm_c)

View File

@@ -0,0 +1,56 @@
find_program(CARGO cargo)
if(NOT CARGO)
message(FATAL_ERROR "cargo is required")
endif()
function(compile_rust_to_wasm input)
cmake_parse_arguments(parsed_args "" "WASM;OUT_DIR" "" ${ARGN})
get_filename_component(basename ${input} NAME_WE)
set(input "${CMAKE_CURRENT_SOURCE_DIR}/${input}")
set(output_dir "${parsed_args_OUT_DIR}")
set(output "${output_dir}/${basename}.wasm")
set(target "wasm32-wasi")
set(mode "debug")
set(package "examples")
add_custom_command(
OUTPUT ${output}
COMMAND
${CARGO} build
--target=${target}
--example=${basename}
--locked
--manifest-path=Cargo.toml
--target-dir=${CMAKE_CURRENT_BINARY_DIR}
COMMAND "wasm-opt"
"${CMAKE_CURRENT_BINARY_DIR}/${target}/${profile}/${mode}/${package}/${basename}.wasm"
"-Oz"
"-o" "${output}"
COMMAND "wasm-strip" "${output}"
DEPENDS ${input} Cargo.lock
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
COMMENT "Building WASM ${output}"
VERBATIM)
set(${parsed_args_WASM} ${output} PARENT_SCOPE)
endfunction(compile_rust_to_wasm)
set(rust_srcs
return_input.rs
test_complex_null_values.rs
test_functions_with_frozen_types.rs
test_short_ints.rs
test_types_with_and_without_nulls.rs)
foreach(rust_src ${rust_srcs})
compile_rust_to_wasm(${rust_src}
OUT_DIR "${CMAKE_BINARY_DIR}/wasm"
WASM wasm)
wasm2wat(${wasm}
OUT_DIR "${CMAKE_BINARY_DIR}/wasm"
WAT wat)
list(APPEND wasms ${wat})
endforeach()
add_custom_target(wasm_rust
DEPENDS ${wasms})
add_dependencies(wasm
wasm_rust)