2022-05-30 18:46:17 +03:00
|
|
|
module Commands.Compile where
|
|
|
|
|
2022-09-14 17:16:15 +03:00
|
|
|
import Commands.Base
|
|
|
|
import Commands.Compile.Options
|
2022-05-30 18:46:17 +03:00
|
|
|
import Data.ByteString qualified as BS
|
|
|
|
import Data.FileEmbed qualified as FE
|
|
|
|
import Data.Text.IO qualified as TIO
|
2022-09-14 17:16:15 +03:00
|
|
|
import Juvix.Compiler.Backend.C.Translation.FromInternal qualified as MiniC
|
2022-05-30 18:46:17 +03:00
|
|
|
import System.Environment
|
|
|
|
import System.Process qualified as P
|
|
|
|
|
2022-09-14 17:16:15 +03:00
|
|
|
runCommand :: Members '[Embed IO, App] r => CompileOptions -> Sem r ()
|
|
|
|
runCommand opts@CompileOptions {..} = do
|
|
|
|
root <- askRoot
|
|
|
|
miniC <- (^. MiniC.resultCCode) <$> runPipeline _compileInputFile upToMiniC
|
|
|
|
let inputFile = _compileInputFile ^. pathPath
|
|
|
|
result <- embed (runCompile root inputFile opts miniC)
|
|
|
|
case result of
|
|
|
|
Left err -> printFailureExit err
|
|
|
|
_ -> return ()
|
|
|
|
|
2022-07-08 14:59:45 +03:00
|
|
|
juvixBuildDir :: FilePath
|
|
|
|
juvixBuildDir = ".juvix-build"
|
2022-05-30 18:46:17 +03:00
|
|
|
|
2022-06-09 17:36:07 +03:00
|
|
|
inputCFile :: FilePath -> FilePath -> FilePath
|
2022-09-14 17:16:15 +03:00
|
|
|
inputCFile projRoot inputFileCompile =
|
2022-07-08 14:59:45 +03:00
|
|
|
projRoot </> juvixBuildDir </> outputMiniCFile
|
2022-05-30 18:46:17 +03:00
|
|
|
where
|
|
|
|
outputMiniCFile :: FilePath
|
2022-09-14 17:16:15 +03:00
|
|
|
outputMiniCFile = takeBaseName inputFileCompile <> ".c"
|
2022-05-30 18:46:17 +03:00
|
|
|
|
2022-06-09 17:36:07 +03:00
|
|
|
runCompile :: FilePath -> FilePath -> CompileOptions -> Text -> IO (Either Text ())
|
2022-09-14 17:16:15 +03:00
|
|
|
runCompile projRoot inputFileCompile o minic = do
|
2022-07-08 14:59:45 +03:00
|
|
|
createDirectoryIfMissing True (projRoot </> juvixBuildDir)
|
2022-09-14 17:16:15 +03:00
|
|
|
TIO.writeFile (inputCFile projRoot inputFileCompile) minic
|
2022-05-30 18:46:17 +03:00
|
|
|
prepareRuntime projRoot o
|
|
|
|
case o ^. compileTarget of
|
2022-09-14 17:16:15 +03:00
|
|
|
TargetWasm -> runM (runError (clangCompile projRoot inputFileCompile o))
|
2022-05-30 18:46:17 +03:00
|
|
|
TargetC -> return (Right ())
|
2022-09-14 17:16:15 +03:00
|
|
|
TargetNative -> runM (runError (clangNativeCompile projRoot inputFileCompile o))
|
2022-05-30 18:46:17 +03:00
|
|
|
|
|
|
|
prepareRuntime :: FilePath -> CompileOptions -> IO ()
|
|
|
|
prepareRuntime projRoot o = do
|
|
|
|
mapM_ writeRuntime runtimeProjectDir
|
|
|
|
where
|
2022-08-01 13:53:19 +03:00
|
|
|
wasiStandaloneRuntimeDir :: [(FilePath, BS.ByteString)]
|
2022-08-03 14:20:40 +03:00
|
|
|
wasiStandaloneRuntimeDir = $(FE.makeRelativeToProject "c-runtime/wasi-standalone" >>= FE.embedDir)
|
2022-08-01 13:53:19 +03:00
|
|
|
|
2022-05-30 18:46:17 +03:00
|
|
|
standaloneRuntimeDir :: [(FilePath, BS.ByteString)]
|
2022-08-03 14:20:40 +03:00
|
|
|
standaloneRuntimeDir = $(FE.makeRelativeToProject "c-runtime/standalone" >>= FE.embedDir)
|
2022-05-30 18:46:17 +03:00
|
|
|
|
2022-08-01 13:53:19 +03:00
|
|
|
wasiLibCRuntimeDir :: [(FilePath, BS.ByteString)]
|
2022-08-03 14:20:40 +03:00
|
|
|
wasiLibCRuntimeDir = $(FE.makeRelativeToProject "c-runtime/wasi-libc" >>= FE.embedDir)
|
2022-05-30 18:46:17 +03:00
|
|
|
|
2022-06-28 14:31:31 +03:00
|
|
|
builtinCRuntimeDir :: [(FilePath, BS.ByteString)]
|
2022-08-03 14:20:40 +03:00
|
|
|
builtinCRuntimeDir = $(FE.makeRelativeToProject "c-runtime/builtins" >>= FE.embedDir)
|
2022-06-28 14:31:31 +03:00
|
|
|
|
2022-08-01 13:53:19 +03:00
|
|
|
wallocDir :: [(FilePath, BS.ByteString)]
|
2022-08-03 14:20:40 +03:00
|
|
|
wallocDir = $(FE.makeRelativeToProject "c-runtime/walloc" >>= FE.embedDir)
|
2022-08-01 13:53:19 +03:00
|
|
|
|
2022-08-19 13:16:02 +03:00
|
|
|
libcRuntime :: [(FilePath, BS.ByteString)]
|
|
|
|
libcRuntime = wasiLibCRuntimeDir <> builtinCRuntimeDir
|
|
|
|
|
2022-05-30 18:46:17 +03:00
|
|
|
runtimeProjectDir :: [(FilePath, BS.ByteString)]
|
2022-08-19 13:16:02 +03:00
|
|
|
runtimeProjectDir = case o ^. compileTarget of
|
|
|
|
TargetNative -> libcRuntime
|
|
|
|
_ -> case o ^. compileRuntime of
|
|
|
|
RuntimeWasiStandalone -> wasiStandaloneRuntimeDir <> builtinCRuntimeDir <> wallocDir
|
|
|
|
RuntimeWasiLibC -> libcRuntime
|
|
|
|
RuntimeStandalone -> standaloneRuntimeDir <> builtinCRuntimeDir <> wallocDir
|
2022-05-30 18:46:17 +03:00
|
|
|
|
|
|
|
writeRuntime :: (FilePath, BS.ByteString) -> IO ()
|
|
|
|
writeRuntime (filePath, contents) =
|
2022-07-08 14:59:45 +03:00
|
|
|
BS.writeFile (projRoot </> juvixBuildDir </> takeFileName filePath) contents
|
2022-05-30 18:46:17 +03:00
|
|
|
|
2022-08-19 13:16:02 +03:00
|
|
|
clangNativeCompile ::
|
|
|
|
forall r.
|
|
|
|
Members '[Embed IO, Error Text] r =>
|
|
|
|
FilePath ->
|
|
|
|
FilePath ->
|
|
|
|
CompileOptions ->
|
|
|
|
Sem r ()
|
2022-09-14 17:16:15 +03:00
|
|
|
clangNativeCompile projRoot inputFileCompile o = runClang (nativeArgs outputFile inputFile)
|
2022-08-19 13:16:02 +03:00
|
|
|
where
|
|
|
|
outputFile :: FilePath
|
2022-09-14 17:16:15 +03:00
|
|
|
outputFile = maybe (takeBaseName inputFileCompile) (^. pathPath) (o ^. compileOutputFile)
|
2022-08-19 13:16:02 +03:00
|
|
|
|
|
|
|
inputFile :: FilePath
|
2022-09-14 17:16:15 +03:00
|
|
|
inputFile = inputCFile projRoot inputFileCompile
|
2022-08-19 13:16:02 +03:00
|
|
|
|
2022-08-01 13:53:19 +03:00
|
|
|
clangCompile ::
|
|
|
|
forall r.
|
|
|
|
Members '[Embed IO, Error Text] r =>
|
|
|
|
FilePath ->
|
|
|
|
FilePath ->
|
|
|
|
CompileOptions ->
|
|
|
|
Sem r ()
|
2022-09-14 17:16:15 +03:00
|
|
|
clangCompile projRoot inputFileCompile o = clangArgs >>= runClang
|
2022-05-30 18:46:17 +03:00
|
|
|
where
|
2022-08-01 13:53:19 +03:00
|
|
|
clangArgs :: Sem r [String]
|
|
|
|
clangArgs = case o ^. compileRuntime of
|
|
|
|
RuntimeStandalone ->
|
|
|
|
return (standaloneLibArgs projRoot outputFile inputFile)
|
|
|
|
RuntimeWasiStandalone -> wasiStandaloneArgs projRoot outputFile inputFile <$> sysrootEnvVar
|
|
|
|
RuntimeWasiLibC -> wasiLibcArgs outputFile inputFile <$> sysrootEnvVar
|
2022-05-30 18:46:17 +03:00
|
|
|
|
2022-08-01 13:53:19 +03:00
|
|
|
outputFile :: FilePath
|
2022-09-14 17:16:15 +03:00
|
|
|
outputFile = maybe (takeBaseName inputFileCompile <> ".wasm") (^. pathPath) (o ^. compileOutputFile)
|
2022-05-30 18:46:17 +03:00
|
|
|
|
2022-08-01 13:53:19 +03:00
|
|
|
inputFile :: FilePath
|
2022-09-14 17:16:15 +03:00
|
|
|
inputFile = inputCFile projRoot inputFileCompile
|
2022-05-30 18:46:17 +03:00
|
|
|
|
2022-08-01 13:53:19 +03:00
|
|
|
sysrootEnvVar :: Sem r String
|
|
|
|
sysrootEnvVar =
|
|
|
|
fromMaybeM (throw msg) (embed (lookupEnv "WASI_SYSROOT_PATH"))
|
|
|
|
where
|
|
|
|
msg :: Text
|
|
|
|
msg = "Missing environment variable WASI_SYSROOT_PATH"
|
2022-05-31 18:50:29 +03:00
|
|
|
|
2022-08-01 13:53:19 +03:00
|
|
|
commonArgs :: FilePath -> [String]
|
|
|
|
commonArgs wasmOutputFile =
|
2022-08-19 13:16:02 +03:00
|
|
|
[ "-std=c99",
|
2022-05-31 18:50:29 +03:00
|
|
|
"-Oz",
|
2022-05-30 18:46:17 +03:00
|
|
|
"-I",
|
2022-07-08 14:59:45 +03:00
|
|
|
juvixBuildDir,
|
2022-05-30 18:46:17 +03:00
|
|
|
"-o",
|
2022-05-31 18:50:29 +03:00
|
|
|
wasmOutputFile
|
2022-05-30 18:46:17 +03:00
|
|
|
]
|
|
|
|
|
2022-08-01 13:53:19 +03:00
|
|
|
standaloneLibArgs :: FilePath -> FilePath -> FilePath -> [String]
|
|
|
|
standaloneLibArgs projRoot wasmOutputFile inputFile =
|
|
|
|
commonArgs wasmOutputFile
|
|
|
|
<> [ "--target=wasm32",
|
2022-08-19 23:59:57 +03:00
|
|
|
"-nodefaultlibs",
|
2022-08-01 13:53:19 +03:00
|
|
|
"-nostartfiles",
|
|
|
|
"-Wl,--no-entry",
|
|
|
|
projRoot </> juvixBuildDir </> "walloc.c",
|
|
|
|
inputFile
|
|
|
|
]
|
|
|
|
|
|
|
|
wasiStandaloneArgs :: FilePath -> FilePath -> FilePath -> FilePath -> [String]
|
|
|
|
wasiStandaloneArgs projRoot wasmOutputFile inputFile sysrootPath =
|
|
|
|
wasiCommonArgs sysrootPath wasmOutputFile
|
|
|
|
<> [ projRoot </> juvixBuildDir </> "walloc.c",
|
|
|
|
inputFile
|
|
|
|
]
|
|
|
|
|
|
|
|
wasiLibcArgs :: FilePath -> FilePath -> FilePath -> [String]
|
|
|
|
wasiLibcArgs wasmOutputFile inputFile sysrootPath =
|
|
|
|
wasiCommonArgs sysrootPath wasmOutputFile
|
|
|
|
<> ["-lc", inputFile]
|
|
|
|
|
2022-08-19 13:16:02 +03:00
|
|
|
nativeArgs :: FilePath -> FilePath -> [String]
|
|
|
|
nativeArgs outputFile inputFile =
|
|
|
|
commonArgs outputFile <> [inputFile]
|
|
|
|
|
2022-08-01 13:53:19 +03:00
|
|
|
wasiCommonArgs :: FilePath -> FilePath -> [String]
|
|
|
|
wasiCommonArgs sysrootPath wasmOutputFile =
|
|
|
|
commonArgs wasmOutputFile
|
2022-08-19 13:16:02 +03:00
|
|
|
<> [ "-nodefaultlibs",
|
|
|
|
"--target=wasm32-wasi",
|
2022-08-01 13:53:19 +03:00
|
|
|
"--sysroot",
|
|
|
|
sysrootPath
|
|
|
|
]
|
|
|
|
|
2022-05-30 18:46:17 +03:00
|
|
|
runClang ::
|
2022-08-01 13:53:19 +03:00
|
|
|
Members '[Embed IO, Error Text] r =>
|
2022-05-30 18:46:17 +03:00
|
|
|
[String] ->
|
2022-08-01 13:53:19 +03:00
|
|
|
Sem r ()
|
2022-05-30 18:46:17 +03:00
|
|
|
runClang args = do
|
2022-08-01 13:53:19 +03:00
|
|
|
(exitCode, _, err) <- embed (P.readProcessWithExitCode "clang" args "")
|
2022-05-30 18:46:17 +03:00
|
|
|
case exitCode of
|
2022-08-01 13:53:19 +03:00
|
|
|
ExitSuccess -> return ()
|
|
|
|
_ -> throw (pack err)
|