2022-12-06 13:33:20 +03:00
|
|
|
module Commands.Extra.Compile where
|
|
|
|
|
|
|
|
import Commands.Base
|
|
|
|
import Commands.Extra.Compile.Options
|
|
|
|
import Data.ByteString qualified as BS
|
|
|
|
import Data.FileEmbed qualified as FE
|
|
|
|
import Juvix.Extra.Paths
|
|
|
|
import System.Environment
|
|
|
|
import System.Process qualified as P
|
|
|
|
|
Update CI to install Smoke, Github actions, and Makefile fixes (#1735)
This PR adds some maintenance at different levels to the CI config, the
Make file, and formatting.
- Most of the actions used by the CI related to haskell, ormolu, hlint
and pre-commit have been updated because Github requires NodeJS 16. This
change removes all the old warnings related to nodeJs.
In the case of ormolu, the new version makes us format some files that
were not formatted before, similarly with hlint.
- The CI has been updated to use the latest version of the Smoke testing
framework, which introduced installation of the dependencies for Linux
(libicu66) and macOS (icu4c) in the CI. In the case of macOS, the CI
uses a binary for smoke. For Linux, we use stack to build smoke from the
source. The source here is in a fork of [the official Smoke
repo](https://github.com/SamirTalwar/smoke). Such includes some
features/changes that are not yet in the official repo.
- The Makefile runs the ormolu and hlint targets using as a path for the
binaries the environment variables ORMOLU and HLINT. Thus, export those
variables in your environment before running `make check,` `make format`
or `make hlint`. Otherwise, the Makefile will use the binaries provided
by `stack`.
Co-authored-by: Paul Cadman <git@paulcadman.dev>
2023-01-24 13:50:23 +03:00
|
|
|
runCommand :: forall r. (Members '[Embed IO, App] r) => CompileOptions -> Sem r ()
|
2022-12-06 13:33:20 +03:00
|
|
|
runCommand opts = do
|
2023-04-19 17:56:48 +03:00
|
|
|
inputFile <- fromAppPathFile (opts ^. compileInputFile)
|
2023-01-06 19:54:13 +03:00
|
|
|
result <- runCompile inputFile opts
|
2022-12-06 13:33:20 +03:00
|
|
|
case result of
|
|
|
|
Left err -> printFailureExit err
|
|
|
|
_ -> return ()
|
|
|
|
|
2023-01-06 19:54:13 +03:00
|
|
|
runCompile ::
|
Update CI to install Smoke, Github actions, and Makefile fixes (#1735)
This PR adds some maintenance at different levels to the CI config, the
Make file, and formatting.
- Most of the actions used by the CI related to haskell, ormolu, hlint
and pre-commit have been updated because Github requires NodeJS 16. This
change removes all the old warnings related to nodeJs.
In the case of ormolu, the new version makes us format some files that
were not formatted before, similarly with hlint.
- The CI has been updated to use the latest version of the Smoke testing
framework, which introduced installation of the dependencies for Linux
(libicu66) and macOS (icu4c) in the CI. In the case of macOS, the CI
uses a binary for smoke. For Linux, we use stack to build smoke from the
source. The source here is in a fork of [the official Smoke
repo](https://github.com/SamirTalwar/smoke). Such includes some
features/changes that are not yet in the official repo.
- The Makefile runs the ormolu and hlint targets using as a path for the
binaries the environment variables ORMOLU and HLINT. Thus, export those
variables in your environment before running `make check,` `make format`
or `make hlint`. Otherwise, the Makefile will use the binaries provided
by `stack`.
Co-authored-by: Paul Cadman <git@paulcadman.dev>
2023-01-24 13:50:23 +03:00
|
|
|
(Members '[App, Embed IO] r) =>
|
2023-01-06 19:54:13 +03:00
|
|
|
Path Abs File ->
|
|
|
|
CompileOptions ->
|
|
|
|
Sem r (Either Text ())
|
|
|
|
runCompile inputFile o = do
|
|
|
|
buildDir <- askBuildDir
|
|
|
|
ensureDir buildDir
|
|
|
|
ensureDir (juvixIncludeDir buildDir)
|
|
|
|
prepareRuntime buildDir o
|
2022-12-06 13:33:20 +03:00
|
|
|
case o ^. compileTarget of
|
2022-12-20 15:05:40 +03:00
|
|
|
TargetWasm32Wasi -> runError (clangWasmWasiCompile inputFile o)
|
|
|
|
TargetNative64 -> runError (clangNativeCompile inputFile o)
|
2023-02-01 14:04:05 +03:00
|
|
|
TargetGeb -> return $ Right ()
|
2023-05-19 15:43:45 +03:00
|
|
|
TargetVampIR -> return $ Right ()
|
2023-03-14 18:24:07 +03:00
|
|
|
TargetCore -> return $ Right ()
|
|
|
|
TargetAsm -> return $ Right ()
|
2022-12-06 13:33:20 +03:00
|
|
|
|
Update CI to install Smoke, Github actions, and Makefile fixes (#1735)
This PR adds some maintenance at different levels to the CI config, the
Make file, and formatting.
- Most of the actions used by the CI related to haskell, ormolu, hlint
and pre-commit have been updated because Github requires NodeJS 16. This
change removes all the old warnings related to nodeJs.
In the case of ormolu, the new version makes us format some files that
were not formatted before, similarly with hlint.
- The CI has been updated to use the latest version of the Smoke testing
framework, which introduced installation of the dependencies for Linux
(libicu66) and macOS (icu4c) in the CI. In the case of macOS, the CI
uses a binary for smoke. For Linux, we use stack to build smoke from the
source. The source here is in a fork of [the official Smoke
repo](https://github.com/SamirTalwar/smoke). Such includes some
features/changes that are not yet in the official repo.
- The Makefile runs the ormolu and hlint targets using as a path for the
binaries the environment variables ORMOLU and HLINT. Thus, export those
variables in your environment before running `make check,` `make format`
or `make hlint`. Otherwise, the Makefile will use the binaries provided
by `stack`.
Co-authored-by: Paul Cadman <git@paulcadman.dev>
2023-01-24 13:50:23 +03:00
|
|
|
prepareRuntime :: forall r. (Members '[App, Embed IO] r) => Path Abs Dir -> CompileOptions -> Sem r ()
|
2023-01-06 19:54:13 +03:00
|
|
|
prepareRuntime buildDir o = do
|
2022-12-06 13:33:20 +03:00
|
|
|
mapM_ writeHeader headersDir
|
|
|
|
case o ^. compileTarget of
|
|
|
|
TargetWasm32Wasi | o ^. compileDebug -> writeRuntime wasiDebugRuntime
|
|
|
|
TargetWasm32Wasi -> writeRuntime wasiReleaseRuntime
|
|
|
|
TargetNative64 | o ^. compileDebug -> writeRuntime nativeDebugRuntime
|
|
|
|
TargetNative64 -> writeRuntime nativeReleaseRuntime
|
2023-02-01 14:04:05 +03:00
|
|
|
TargetGeb -> return ()
|
2023-05-19 15:43:45 +03:00
|
|
|
TargetVampIR -> return ()
|
2023-03-14 18:24:07 +03:00
|
|
|
TargetCore -> return ()
|
|
|
|
TargetAsm -> return ()
|
2022-12-06 13:33:20 +03:00
|
|
|
where
|
|
|
|
wasiReleaseRuntime :: BS.ByteString
|
|
|
|
wasiReleaseRuntime = $(FE.makeRelativeToProject "runtime/_build.wasm32-wasi/libjuvix.a" >>= FE.embedFile)
|
|
|
|
|
|
|
|
nativeReleaseRuntime :: BS.ByteString
|
|
|
|
nativeReleaseRuntime = $(FE.makeRelativeToProject "runtime/_build.native64/libjuvix.a" >>= FE.embedFile)
|
|
|
|
|
|
|
|
wasiDebugRuntime :: BS.ByteString
|
|
|
|
wasiDebugRuntime = $(FE.makeRelativeToProject "runtime/_build.wasm32-wasi-debug/libjuvix.a" >>= FE.embedFile)
|
|
|
|
|
|
|
|
nativeDebugRuntime :: BS.ByteString
|
|
|
|
nativeDebugRuntime = $(FE.makeRelativeToProject "runtime/_build.native64-debug/libjuvix.a" >>= FE.embedFile)
|
|
|
|
|
2022-12-20 15:05:40 +03:00
|
|
|
writeRuntime :: BS.ByteString -> Sem r ()
|
2022-12-06 13:33:20 +03:00
|
|
|
writeRuntime =
|
2022-12-20 15:05:40 +03:00
|
|
|
embed
|
2023-01-06 19:54:13 +03:00
|
|
|
. BS.writeFile (toFilePath (buildDir <//> $(mkRelFile "libjuvix.a")))
|
2022-12-06 13:33:20 +03:00
|
|
|
|
2022-12-20 15:05:40 +03:00
|
|
|
headersDir :: [(Path Rel File, BS.ByteString)]
|
|
|
|
headersDir = map (first relFile) $(FE.makeRelativeToProject "runtime/include" >>= FE.embedDir)
|
|
|
|
|
2023-01-06 19:54:13 +03:00
|
|
|
includeDir :: Path Abs Dir
|
|
|
|
includeDir = juvixIncludeDir buildDir
|
|
|
|
|
2022-12-20 15:05:40 +03:00
|
|
|
writeHeader :: (Path Rel File, BS.ByteString) -> Sem r ()
|
|
|
|
writeHeader (filePath, contents) = embed $ do
|
2023-01-06 19:54:13 +03:00
|
|
|
ensureDir (includeDir <//> parent filePath)
|
|
|
|
BS.writeFile (toFilePath (includeDir <//> filePath)) contents
|
2022-12-06 13:33:20 +03:00
|
|
|
|
2023-04-13 20:57:49 +03:00
|
|
|
outputFile :: forall r. Member App r => CompileOptions -> Path Abs File -> Sem r (Path Abs File)
|
2023-02-08 12:36:22 +03:00
|
|
|
outputFile opts inputFile =
|
2023-04-19 17:56:48 +03:00
|
|
|
maybe defaultOutputFile fromAppPathFile (opts ^? compileOutputFile . _Just)
|
2023-02-08 12:36:22 +03:00
|
|
|
where
|
2023-04-13 20:57:49 +03:00
|
|
|
defaultOutputFile :: Sem r (Path Abs File)
|
|
|
|
defaultOutputFile = do
|
|
|
|
invokeDir <- askInvokeDir
|
|
|
|
let baseOutputFile = invokeDir <//> filename inputFile
|
|
|
|
return $ case opts ^. compileTarget of
|
|
|
|
TargetNative64 ->
|
|
|
|
if
|
|
|
|
| opts ^. compileCOutput -> replaceExtension' ".c" inputFile
|
|
|
|
| opts ^. compilePreprocess -> addExtension' ".c" (addExtension' ".out" (removeExtension' inputFile))
|
|
|
|
| opts ^. compileAssembly -> replaceExtension' ".s" inputFile
|
|
|
|
| otherwise -> removeExtension' baseOutputFile
|
|
|
|
TargetWasm32Wasi ->
|
|
|
|
if
|
|
|
|
| opts ^. compileCOutput -> replaceExtension' ".c" inputFile
|
|
|
|
| opts ^. compilePreprocess -> addExtension' ".c" (addExtension' ".out" (removeExtension' inputFile))
|
|
|
|
| opts ^. compileAssembly -> replaceExtension' ".wat" inputFile
|
|
|
|
| otherwise -> replaceExtension' ".wasm" baseOutputFile
|
|
|
|
TargetGeb ->
|
|
|
|
if
|
|
|
|
| opts ^. compileTerm -> replaceExtension' ".geb" inputFile
|
|
|
|
| otherwise -> replaceExtension' ".lisp" baseOutputFile
|
2023-05-19 15:43:45 +03:00
|
|
|
TargetVampIR ->
|
|
|
|
replaceExtension' ".pir" baseOutputFile
|
2023-04-13 20:57:49 +03:00
|
|
|
TargetCore ->
|
|
|
|
replaceExtension' ".jvc" baseOutputFile
|
|
|
|
TargetAsm ->
|
|
|
|
replaceExtension' ".jva" baseOutputFile
|
2023-02-08 12:36:22 +03:00
|
|
|
|
2022-12-06 13:33:20 +03:00
|
|
|
clangNativeCompile ::
|
|
|
|
forall r.
|
Update CI to install Smoke, Github actions, and Makefile fixes (#1735)
This PR adds some maintenance at different levels to the CI config, the
Make file, and formatting.
- Most of the actions used by the CI related to haskell, ormolu, hlint
and pre-commit have been updated because Github requires NodeJS 16. This
change removes all the old warnings related to nodeJs.
In the case of ormolu, the new version makes us format some files that
were not formatted before, similarly with hlint.
- The CI has been updated to use the latest version of the Smoke testing
framework, which introduced installation of the dependencies for Linux
(libicu66) and macOS (icu4c) in the CI. In the case of macOS, the CI
uses a binary for smoke. For Linux, we use stack to build smoke from the
source. The source here is in a fork of [the official Smoke
repo](https://github.com/SamirTalwar/smoke). Such includes some
features/changes that are not yet in the official repo.
- The Makefile runs the ormolu and hlint targets using as a path for the
binaries the environment variables ORMOLU and HLINT. Thus, export those
variables in your environment before running `make check,` `make format`
or `make hlint`. Otherwise, the Makefile will use the binaries provided
by `stack`.
Co-authored-by: Paul Cadman <git@paulcadman.dev>
2023-01-24 13:50:23 +03:00
|
|
|
(Members '[App, Embed IO, Error Text] r) =>
|
2022-12-20 15:05:40 +03:00
|
|
|
Path Abs File ->
|
2022-12-06 13:33:20 +03:00
|
|
|
CompileOptions ->
|
|
|
|
Sem r ()
|
2022-12-20 15:05:40 +03:00
|
|
|
clangNativeCompile inputFile o = do
|
2023-02-08 12:36:22 +03:00
|
|
|
outputFile' <- outputFile o inputFile
|
2023-01-06 19:54:13 +03:00
|
|
|
buildDir <- askBuildDir
|
2023-02-08 12:36:22 +03:00
|
|
|
if
|
|
|
|
| o ^. compileCOutput ->
|
|
|
|
copyFile inputFile outputFile'
|
|
|
|
| otherwise ->
|
|
|
|
runClang (native64Args buildDir o outputFile' inputFile)
|
2022-12-06 13:33:20 +03:00
|
|
|
|
|
|
|
clangWasmWasiCompile ::
|
|
|
|
forall r.
|
Update CI to install Smoke, Github actions, and Makefile fixes (#1735)
This PR adds some maintenance at different levels to the CI config, the
Make file, and formatting.
- Most of the actions used by the CI related to haskell, ormolu, hlint
and pre-commit have been updated because Github requires NodeJS 16. This
change removes all the old warnings related to nodeJs.
In the case of ormolu, the new version makes us format some files that
were not formatted before, similarly with hlint.
- The CI has been updated to use the latest version of the Smoke testing
framework, which introduced installation of the dependencies for Linux
(libicu66) and macOS (icu4c) in the CI. In the case of macOS, the CI
uses a binary for smoke. For Linux, we use stack to build smoke from the
source. The source here is in a fork of [the official Smoke
repo](https://github.com/SamirTalwar/smoke). Such includes some
features/changes that are not yet in the official repo.
- The Makefile runs the ormolu and hlint targets using as a path for the
binaries the environment variables ORMOLU and HLINT. Thus, export those
variables in your environment before running `make check,` `make format`
or `make hlint`. Otherwise, the Makefile will use the binaries provided
by `stack`.
Co-authored-by: Paul Cadman <git@paulcadman.dev>
2023-01-24 13:50:23 +03:00
|
|
|
(Members '[App, Embed IO, Error Text] r) =>
|
2022-12-20 15:05:40 +03:00
|
|
|
Path Abs File ->
|
2022-12-06 13:33:20 +03:00
|
|
|
CompileOptions ->
|
|
|
|
Sem r ()
|
2023-02-08 12:36:22 +03:00
|
|
|
clangWasmWasiCompile inputFile o = do
|
|
|
|
outputFile' <- outputFile o inputFile
|
|
|
|
buildDir <- askBuildDir
|
|
|
|
if
|
|
|
|
| o ^. compileCOutput ->
|
|
|
|
copyFile inputFile outputFile'
|
|
|
|
| otherwise -> do
|
|
|
|
clangArgs <- wasiArgs buildDir o outputFile' inputFile <$> sysrootEnvVar
|
|
|
|
runClang clangArgs
|
2022-12-06 13:33:20 +03:00
|
|
|
where
|
2022-12-20 15:05:40 +03:00
|
|
|
sysrootEnvVar :: Sem r (Path Abs Dir)
|
2022-12-06 13:33:20 +03:00
|
|
|
sysrootEnvVar =
|
2022-12-20 15:05:40 +03:00
|
|
|
absDir
|
|
|
|
<$> fromMaybeM (throw msg) (embed (lookupEnv "WASI_SYSROOT_PATH"))
|
2022-12-06 13:33:20 +03:00
|
|
|
where
|
|
|
|
msg :: Text
|
|
|
|
msg = "Missing environment variable WASI_SYSROOT_PATH"
|
|
|
|
|
2023-01-06 19:54:13 +03:00
|
|
|
commonArgs :: Path Abs Dir -> CompileOptions -> Path Abs File -> [String]
|
2023-02-08 12:36:22 +03:00
|
|
|
commonArgs buildDir o outfile =
|
2022-12-06 13:33:20 +03:00
|
|
|
["-E" | o ^. compilePreprocess]
|
|
|
|
<> ["-S" | o ^. compileAssembly]
|
|
|
|
<> (if o ^. compileDebug then ["-DDEBUG"] else ["-DNDEBUG"])
|
|
|
|
<> [ "-W",
|
|
|
|
"-Wall",
|
|
|
|
"-Wno-unused-parameter",
|
|
|
|
"-Wno-unused-label",
|
|
|
|
"-Werror",
|
|
|
|
"-std=c11",
|
|
|
|
"-I",
|
2023-01-06 19:54:13 +03:00
|
|
|
toFilePath (juvixIncludeDir buildDir),
|
2022-12-06 13:33:20 +03:00
|
|
|
"-o",
|
2023-02-08 12:36:22 +03:00
|
|
|
toFilePath outfile
|
2022-12-06 13:33:20 +03:00
|
|
|
]
|
|
|
|
<> ( if
|
|
|
|
| not (o ^. compilePreprocess || o ^. compileAssembly) ->
|
|
|
|
[ "-L",
|
2023-01-06 19:54:13 +03:00
|
|
|
toFilePath buildDir
|
2022-12-06 13:33:20 +03:00
|
|
|
]
|
|
|
|
| otherwise -> []
|
|
|
|
)
|
|
|
|
|
2023-01-06 19:54:13 +03:00
|
|
|
native64Args :: Path Abs Dir -> CompileOptions -> Path Abs File -> Path Abs File -> [String]
|
2023-02-08 12:36:22 +03:00
|
|
|
native64Args buildDir o outfile inputFile =
|
|
|
|
commonArgs buildDir o outfile
|
2022-12-06 13:33:20 +03:00
|
|
|
<> [ "-DARCH_NATIVE64",
|
|
|
|
"-DAPI_LIBC",
|
|
|
|
"-m64",
|
|
|
|
"-O3",
|
2022-12-20 15:05:40 +03:00
|
|
|
toFilePath inputFile
|
2022-12-06 13:33:20 +03:00
|
|
|
]
|
|
|
|
<> ( if
|
|
|
|
| not (o ^. compilePreprocess || o ^. compileAssembly) ->
|
|
|
|
["-ljuvix"]
|
|
|
|
| otherwise -> []
|
|
|
|
)
|
|
|
|
|
2023-01-06 19:54:13 +03:00
|
|
|
wasiArgs :: Path Abs Dir -> CompileOptions -> Path Abs File -> Path Abs File -> Path Abs Dir -> [String]
|
2023-02-08 12:36:22 +03:00
|
|
|
wasiArgs buildDir o outfile inputFile sysrootPath =
|
|
|
|
commonArgs buildDir o outfile
|
2022-12-06 13:33:20 +03:00
|
|
|
<> [ "-DARCH_WASM32",
|
|
|
|
"-DAPI_WASI",
|
|
|
|
"-Os",
|
|
|
|
"-nodefaultlibs",
|
|
|
|
"--target=wasm32-wasi",
|
|
|
|
"--sysroot",
|
2022-12-20 15:05:40 +03:00
|
|
|
toFilePath sysrootPath,
|
|
|
|
toFilePath inputFile
|
2022-12-06 13:33:20 +03:00
|
|
|
]
|
|
|
|
<> ( if
|
|
|
|
| not (o ^. compilePreprocess || o ^. compileAssembly) ->
|
|
|
|
["-ljuvix"]
|
|
|
|
| otherwise -> []
|
|
|
|
)
|
|
|
|
|
|
|
|
runClang ::
|
Update CI to install Smoke, Github actions, and Makefile fixes (#1735)
This PR adds some maintenance at different levels to the CI config, the
Make file, and formatting.
- Most of the actions used by the CI related to haskell, ormolu, hlint
and pre-commit have been updated because Github requires NodeJS 16. This
change removes all the old warnings related to nodeJs.
In the case of ormolu, the new version makes us format some files that
were not formatted before, similarly with hlint.
- The CI has been updated to use the latest version of the Smoke testing
framework, which introduced installation of the dependencies for Linux
(libicu66) and macOS (icu4c) in the CI. In the case of macOS, the CI
uses a binary for smoke. For Linux, we use stack to build smoke from the
source. The source here is in a fork of [the official Smoke
repo](https://github.com/SamirTalwar/smoke). Such includes some
features/changes that are not yet in the official repo.
- The Makefile runs the ormolu and hlint targets using as a path for the
binaries the environment variables ORMOLU and HLINT. Thus, export those
variables in your environment before running `make check,` `make format`
or `make hlint`. Otherwise, the Makefile will use the binaries provided
by `stack`.
Co-authored-by: Paul Cadman <git@paulcadman.dev>
2023-01-24 13:50:23 +03:00
|
|
|
(Members '[Embed IO, Error Text] r) =>
|
2022-12-06 13:33:20 +03:00
|
|
|
[String] ->
|
|
|
|
Sem r ()
|
|
|
|
runClang args = do
|
|
|
|
(exitCode, _, err) <- embed (P.readProcessWithExitCode "clang" args "")
|
|
|
|
case exitCode of
|
|
|
|
ExitSuccess -> return ()
|
|
|
|
_ -> throw (pack err)
|