2022-05-30 18:46:17 +03:00
|
|
|
module Commands.Compile where
|
|
|
|
|
|
|
|
import Data.ByteString qualified as BS
|
|
|
|
import Data.FileEmbed qualified as FE
|
|
|
|
import Data.Text.IO qualified as TIO
|
|
|
|
import MiniJuvix.Prelude hiding (Doc)
|
|
|
|
import Options.Applicative
|
|
|
|
import System.Environment
|
|
|
|
import System.Process qualified as P
|
|
|
|
|
|
|
|
minijuvixBuildDir :: FilePath
|
|
|
|
minijuvixBuildDir = ".minijuvix-build"
|
|
|
|
|
|
|
|
data CompileTarget = TargetC | TargetWasm
|
|
|
|
deriving stock (Show)
|
|
|
|
|
|
|
|
data CompileRuntime = RuntimeStandalone | RuntimeLibC
|
|
|
|
deriving stock (Show)
|
|
|
|
|
|
|
|
data CompileOptions = CompileOptions
|
2022-06-09 17:36:07 +03:00
|
|
|
{ _compileTarget :: CompileTarget,
|
2022-05-30 18:46:17 +03:00
|
|
|
_compileRuntime :: CompileRuntime,
|
|
|
|
_compileOutputFile :: Maybe FilePath
|
|
|
|
}
|
|
|
|
|
|
|
|
makeLenses ''CompileOptions
|
|
|
|
|
|
|
|
parseCompile :: Parser CompileOptions
|
|
|
|
parseCompile = do
|
|
|
|
_compileTarget <-
|
|
|
|
option
|
|
|
|
(eitherReader parseTarget)
|
|
|
|
( long "target"
|
|
|
|
<> short 't'
|
|
|
|
<> metavar "TARGET"
|
|
|
|
<> value TargetWasm
|
|
|
|
<> showDefaultWith targetShow
|
|
|
|
<> help "select a target: wasm, c"
|
|
|
|
)
|
|
|
|
|
|
|
|
_compileRuntime <-
|
|
|
|
option
|
|
|
|
(eitherReader parseRuntime)
|
|
|
|
( long "runtime"
|
|
|
|
<> short 'r'
|
|
|
|
<> metavar "RUNTIME"
|
|
|
|
<> value RuntimeStandalone
|
|
|
|
<> showDefaultWith runtimeShow
|
|
|
|
<> help "select a runtime: standalone, libc"
|
|
|
|
)
|
|
|
|
|
|
|
|
_compileOutputFile <-
|
|
|
|
optional $
|
|
|
|
option
|
|
|
|
str
|
|
|
|
( long "output"
|
|
|
|
<> short 'o'
|
|
|
|
<> metavar "OUTPUT_FILE"
|
|
|
|
<> help "Path to output file"
|
|
|
|
<> action "file"
|
|
|
|
)
|
|
|
|
pure CompileOptions {..}
|
|
|
|
where
|
|
|
|
parseTarget :: String -> Either String CompileTarget
|
|
|
|
parseTarget = \case
|
|
|
|
"wasm" -> Right TargetWasm
|
|
|
|
"c" -> Right TargetC
|
|
|
|
s -> Left $ "unrecognised target: " <> s
|
|
|
|
|
|
|
|
targetShow :: CompileTarget -> String
|
|
|
|
targetShow = \case
|
|
|
|
TargetC -> "c"
|
|
|
|
TargetWasm -> "wasm"
|
|
|
|
|
|
|
|
parseRuntime :: String -> Either String CompileRuntime
|
|
|
|
parseRuntime = \case
|
|
|
|
"standalone" -> Right RuntimeStandalone
|
|
|
|
"libc" -> Right RuntimeLibC
|
|
|
|
s -> Left $ "unrecognised runtime: " <> s
|
|
|
|
|
|
|
|
runtimeShow :: CompileRuntime -> String
|
|
|
|
runtimeShow = \case
|
|
|
|
RuntimeStandalone -> "standalone"
|
|
|
|
RuntimeLibC -> "libc"
|
|
|
|
|
2022-06-09 17:36:07 +03:00
|
|
|
inputCFile :: FilePath -> FilePath -> FilePath
|
|
|
|
inputCFile projRoot compileInputFile =
|
|
|
|
projRoot </> minijuvixBuildDir </> outputMiniCFile
|
2022-05-30 18:46:17 +03:00
|
|
|
where
|
|
|
|
outputMiniCFile :: FilePath
|
2022-06-09 17:36:07 +03:00
|
|
|
outputMiniCFile = takeBaseName compileInputFile <> ".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 ())
|
|
|
|
runCompile projRoot compileInputFile o minic = do
|
2022-05-30 18:46:17 +03:00
|
|
|
createDirectoryIfMissing True (projRoot </> minijuvixBuildDir)
|
2022-06-09 17:36:07 +03:00
|
|
|
TIO.writeFile (inputCFile projRoot compileInputFile) minic
|
2022-05-30 18:46:17 +03:00
|
|
|
prepareRuntime projRoot o
|
|
|
|
case o ^. compileTarget of
|
2022-06-09 17:36:07 +03:00
|
|
|
TargetWasm -> clangCompile projRoot compileInputFile o
|
2022-05-30 18:46:17 +03:00
|
|
|
TargetC -> return (Right ())
|
|
|
|
|
|
|
|
prepareRuntime :: FilePath -> CompileOptions -> IO ()
|
|
|
|
prepareRuntime projRoot o = do
|
|
|
|
mapM_ writeRuntime runtimeProjectDir
|
|
|
|
where
|
|
|
|
standaloneRuntimeDir :: [(FilePath, BS.ByteString)]
|
|
|
|
standaloneRuntimeDir = $(FE.makeRelativeToProject "minic-runtime/standalone" >>= FE.embedDir)
|
|
|
|
|
|
|
|
libCRuntimeDir :: [(FilePath, BS.ByteString)]
|
|
|
|
libCRuntimeDir = $(FE.makeRelativeToProject "minic-runtime/libc" >>= FE.embedDir)
|
|
|
|
|
2022-06-28 14:31:31 +03:00
|
|
|
builtinCRuntimeDir :: [(FilePath, BS.ByteString)]
|
|
|
|
builtinCRuntimeDir = $(FE.makeRelativeToProject "minic-runtime/builtins" >>= FE.embedDir)
|
|
|
|
|
2022-05-30 18:46:17 +03:00
|
|
|
runtimeProjectDir :: [(FilePath, BS.ByteString)]
|
|
|
|
runtimeProjectDir = case o ^. compileRuntime of
|
2022-06-28 14:31:31 +03:00
|
|
|
RuntimeStandalone -> standaloneRuntimeDir <> builtinCRuntimeDir
|
|
|
|
RuntimeLibC -> libCRuntimeDir <> builtinCRuntimeDir
|
2022-05-30 18:46:17 +03:00
|
|
|
|
|
|
|
writeRuntime :: (FilePath, BS.ByteString) -> IO ()
|
|
|
|
writeRuntime (filePath, contents) =
|
|
|
|
BS.writeFile (projRoot </> minijuvixBuildDir </> takeFileName filePath) contents
|
|
|
|
|
2022-06-09 17:36:07 +03:00
|
|
|
clangCompile :: FilePath -> FilePath -> CompileOptions -> IO (Either Text ())
|
|
|
|
clangCompile projRoot compileInputFile o = do
|
2022-05-30 18:46:17 +03:00
|
|
|
v <- sysrootEnvVar
|
|
|
|
case v of
|
|
|
|
Left s -> return (Left s)
|
|
|
|
Right sysrootPath -> withSysrootPath sysrootPath
|
|
|
|
where
|
|
|
|
sysrootEnvVar :: IO (Either Text String)
|
2022-06-09 17:36:07 +03:00
|
|
|
sysrootEnvVar =
|
|
|
|
maybeToEither "Missing environment variable WASI_SYSROOT_PATH"
|
|
|
|
<$> lookupEnv "WASI_SYSROOT_PATH"
|
2022-05-30 18:46:17 +03:00
|
|
|
|
|
|
|
withSysrootPath :: String -> IO (Either Text ())
|
|
|
|
withSysrootPath sysrootPath = runClang clangArgs
|
|
|
|
where
|
|
|
|
clangArgs :: [String]
|
|
|
|
clangArgs = case o ^. compileRuntime of
|
2022-07-04 18:53:06 +03:00
|
|
|
RuntimeStandalone -> standaloneArgs projRoot sysrootPath outputFile inputFile
|
2022-05-30 18:46:17 +03:00
|
|
|
RuntimeLibC -> libcArgs sysrootPath outputFile inputFile
|
|
|
|
|
|
|
|
outputFile :: FilePath
|
2022-06-09 17:36:07 +03:00
|
|
|
outputFile = fromMaybe (takeBaseName compileInputFile <> ".wasm") (o ^. compileOutputFile)
|
2022-05-30 18:46:17 +03:00
|
|
|
|
|
|
|
inputFile :: FilePath
|
2022-06-09 17:36:07 +03:00
|
|
|
inputFile = inputCFile projRoot compileInputFile
|
2022-05-30 18:46:17 +03:00
|
|
|
|
2022-07-04 18:53:06 +03:00
|
|
|
standaloneArgs :: FilePath -> FilePath -> FilePath -> FilePath -> [String]
|
|
|
|
standaloneArgs projRoot sysrootPath wasmOutputFile inputFile =
|
2022-05-31 18:50:29 +03:00
|
|
|
commonArgs sysrootPath wasmOutputFile
|
2022-07-04 18:53:06 +03:00
|
|
|
<> [projRoot </> minijuvixBuildDir </> "walloc.c", inputFile]
|
2022-05-30 18:46:17 +03:00
|
|
|
|
|
|
|
libcArgs :: FilePath -> FilePath -> FilePath -> [String]
|
|
|
|
libcArgs sysrootPath wasmOutputFile inputFile =
|
2022-05-31 18:50:29 +03:00
|
|
|
commonArgs sysrootPath wasmOutputFile
|
|
|
|
<> ["-lc", inputFile]
|
|
|
|
|
|
|
|
commonArgs :: FilePath -> FilePath -> [String]
|
|
|
|
commonArgs sysrootPath wasmOutputFile =
|
2022-05-30 18:46:17 +03:00
|
|
|
[ "-nodefaultlibs",
|
2022-05-31 18:50:29 +03:00
|
|
|
"-Oz",
|
2022-05-30 18:46:17 +03:00
|
|
|
"-I",
|
|
|
|
minijuvixBuildDir,
|
|
|
|
"--target=wasm32-wasi",
|
|
|
|
"--sysroot",
|
|
|
|
sysrootPath,
|
|
|
|
"-o",
|
2022-05-31 18:50:29 +03:00
|
|
|
wasmOutputFile
|
2022-05-30 18:46:17 +03:00
|
|
|
]
|
|
|
|
|
|
|
|
runClang ::
|
|
|
|
[String] ->
|
|
|
|
IO (Either Text ())
|
|
|
|
runClang args = do
|
|
|
|
(exitCode, _, err) <- P.readProcessWithExitCode "clang" args ""
|
|
|
|
case exitCode of
|
|
|
|
ExitSuccess -> return (Right ())
|
|
|
|
_ -> return (Left (pack err))
|