1
1
mirror of https://github.com/anoma/juvix.git synced 2024-12-14 17:32:00 +03:00
juvix/test/Runtime/Base.hs

106 lines
3.2 KiB
Haskell
Raw Normal View History

2022-11-03 11:38:09 +03:00
module Runtime.Base where
import Base
import Data.FileEmbed
import Data.Text.IO qualified as TIO
import System.Process qualified as P
clangCompile ::
2022-12-20 15:05:40 +03:00
(Path Abs File -> Path Abs File -> [String]) ->
Path Abs File ->
Path Rel File ->
(Path Abs File -> IO Text) ->
2022-11-03 11:38:09 +03:00
(String -> IO ()) ->
IO Text
clangCompile mkClangArgs inputFile outputFile execute step =
2022-12-20 15:05:40 +03:00
withTempDir'
2022-11-03 11:38:09 +03:00
( \dirPath -> do
2022-12-20 15:05:40 +03:00
let outputFile' = dirPath <//> outputFile
2022-11-03 11:38:09 +03:00
step "C compilation"
P.callProcess
"clang"
(mkClangArgs outputFile' inputFile)
step "Execution"
execute outputFile'
)
2022-12-20 15:05:40 +03:00
clangAssertion :: Path Abs File -> Path Abs File -> Text -> ((String -> IO ()) -> Assertion)
2022-11-03 11:38:09 +03:00
clangAssertion inputFile expectedFile stdinText step = do
step "Check clang and wasmer are on path"
2022-12-20 15:05:40 +03:00
assertCmdExists $(mkRelFile "clang")
assertCmdExists $(mkRelFile "wasmer")
2022-11-03 11:38:09 +03:00
step "Lookup WASI_SYSROOT_PATH"
2023-01-05 19:48:26 +03:00
sysrootPath :: Path Abs Dir <- getWasiSysrootPath
2022-11-03 11:38:09 +03:00
2022-12-20 15:05:40 +03:00
expected <- TIO.readFile (toFilePath expectedFile)
2022-11-03 11:38:09 +03:00
2022-12-20 15:05:40 +03:00
let executeWasm :: Path Abs File -> IO Text
executeWasm outputFile = pack <$> P.readProcess "wasmer" [toFilePath outputFile] (unpack stdinText)
2022-11-03 11:38:09 +03:00
2022-12-20 15:05:40 +03:00
let executeNative :: Path Abs File -> IO Text
executeNative outputFile = pack <$> P.readProcess (toFilePath outputFile) [] (unpack stdinText)
2022-11-03 11:38:09 +03:00
step "Compile C to WASM32-WASI"
2022-12-20 15:05:40 +03:00
actualWasm <- clangCompile (wasiArgs sysrootPath) inputFile $(mkRelFile "Program.wasm") executeWasm step
2022-11-03 11:38:09 +03:00
step "Compare expected and actual program output"
2022-12-20 15:05:40 +03:00
assertEqDiff ("check: WASM output = " <> toFilePath expectedFile) actualWasm expected
2022-11-03 11:38:09 +03:00
step "Compile C to native 64-bit code"
2022-12-20 15:05:40 +03:00
actualNative <- clangCompile native64Args inputFile $(mkRelFile "Program") executeNative step
2022-11-03 11:38:09 +03:00
step "Compare expected and actual program output"
2022-12-20 15:05:40 +03:00
assertEqDiff ("check: native output = " <> toFilePath expectedFile) actualNative expected
2022-11-03 11:38:09 +03:00
2022-12-20 15:05:40 +03:00
commonArgs :: Path Abs File -> [String]
2022-11-03 11:38:09 +03:00
commonArgs outputFile =
[ "-DDEBUG",
"-W",
"-Wall",
"-Wno-unused-parameter",
"-Wno-unused-label",
"-Werror",
"-std=c11",
"-I",
runtimeInclude,
"-o",
2022-12-20 15:05:40 +03:00
toFilePath outputFile
2022-11-03 11:38:09 +03:00
]
where
runtimeInclude :: FilePath
runtimeInclude = $(makeRelativeToProject "runtime/include" >>= strToExp)
2022-12-20 15:05:40 +03:00
native64Args :: Path Abs File -> Path Abs File -> [String]
2022-11-03 11:38:09 +03:00
native64Args outputFile inputFile =
commonArgs outputFile
<> [ "-DARCH_NATIVE64",
"-DAPI_LIBC",
"-m64",
"-O3",
"-L",
juvixLibraryDir,
2022-12-20 15:05:40 +03:00
toFilePath inputFile,
2022-11-03 11:38:09 +03:00
"-ljuvix"
]
where
juvixLibraryDir :: FilePath
juvixLibraryDir = $(makeRelativeToProject "runtime/_build.native64-debug" >>= strToExp)
2022-12-20 15:05:40 +03:00
wasiArgs :: Path Abs Dir -> Path Abs File -> Path Abs File -> [String]
2022-11-03 11:38:09 +03:00
wasiArgs sysrootPath outputFile inputFile =
commonArgs outputFile
<> [ "-DARCH_WASM32",
"-DAPI_WASI",
"-Os",
"-nodefaultlibs",
"--target=wasm32-wasi",
"--sysroot",
2022-12-20 15:05:40 +03:00
toFilePath sysrootPath,
2022-11-03 11:38:09 +03:00
"-L",
2022-12-20 15:05:40 +03:00
toFilePath juvixLibraryDir,
toFilePath inputFile,
2022-11-03 11:38:09 +03:00
"-ljuvix"
]
where
2022-12-20 15:05:40 +03:00
juvixLibraryDir :: Path Abs Dir
juvixLibraryDir = absDir $(makeRelativeToProject "runtime/_build.wasm32-wasi-debug" >>= strToExp)