{-# LANGUAGE FlexibleContexts #-}
-- | @futhark pyopencl@
module Futhark.CLI.PyOpenCL (main) where

import Control.Monad.IO.Class
import System.FilePath
import System.Directory

import Futhark.Passes
import qualified Futhark.CodeGen.Backends.PyOpenCL as PyOpenCL
import Futhark.Compiler.CLI

-- | Run @futhark pyopencl@.
main :: String -> [String] -> IO ()
main :: String -> [String] -> IO ()
main = ()
-> [CompilerOption ()]
-> String
-> String
-> Pipeline SOACS KernelsMem
-> (() -> CompilerMode -> String -> Prog KernelsMem -> FutharkM ())
-> String
-> [String]
-> IO ()
forall cfg lore.
cfg
-> [CompilerOption cfg]
-> String
-> String
-> Pipeline SOACS lore
-> (cfg -> CompilerMode -> String -> Prog lore -> FutharkM ())
-> String
-> [String]
-> IO ()
compilerMain () []
       String
"Compile PyOpenCL" String
"Generate Python + OpenCL code from optimised Futhark program."
       Pipeline SOACS KernelsMem
gpuPipeline ((() -> CompilerMode -> String -> Prog KernelsMem -> FutharkM ())
 -> String -> [String] -> IO ())
-> (() -> CompilerMode -> String -> Prog KernelsMem -> FutharkM ())
-> String
-> [String]
-> IO ()
forall a b. (a -> b) -> a -> b
$ \() CompilerMode
mode String
outpath Prog KernelsMem
prog -> do
          let class_name :: Maybe String
class_name =
                case CompilerMode
mode of CompilerMode
ToLibrary -> String -> Maybe String
forall a. a -> Maybe a
Just (String -> Maybe String) -> String -> Maybe String
forall a b. (a -> b) -> a -> b
$ String -> String
takeBaseName String
outpath
                             CompilerMode
ToExecutable -> Maybe String
forall a. Maybe a
Nothing
          String
pyprog <- Maybe String -> Prog KernelsMem -> FutharkM String
forall (m :: * -> *).
MonadFreshNames m =>
Maybe String -> Prog KernelsMem -> m String
PyOpenCL.compileProg Maybe String
class_name Prog KernelsMem
prog

          case CompilerMode
mode of
            CompilerMode
ToLibrary ->
              IO () -> FutharkM ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> FutharkM ()) -> IO () -> FutharkM ()
forall a b. (a -> b) -> a -> b
$ String -> String -> IO ()
writeFile (String
outpath String -> String -> String
`addExtension` String
"py") String
pyprog
            CompilerMode
ToExecutable -> IO () -> FutharkM ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> FutharkM ()) -> IO () -> FutharkM ()
forall a b. (a -> b) -> a -> b
$ do
              String -> String -> IO ()
writeFile String
outpath String
pyprog
              Permissions
perms <- IO Permissions -> IO Permissions
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Permissions -> IO Permissions)
-> IO Permissions -> IO Permissions
forall a b. (a -> b) -> a -> b
$ String -> IO Permissions
getPermissions String
outpath
              String -> Permissions -> IO ()
setPermissions String
outpath (Permissions -> IO ()) -> Permissions -> IO ()
forall a b. (a -> b) -> a -> b
$ Bool -> Permissions -> Permissions
setOwnerExecutable Bool
True Permissions
perms