Created
November 22, 2017 23:46
-
-
Save ljli/c5aca47ca4eb89bded7e5cd87144bd96 to your computer and use it in GitHub Desktop.
GHC add foreign object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE TemplateHaskell #-} | |
module Main where | |
import Data.Int | |
import System.Process | |
import Language.Haskell.TH.Syntax | |
foreign import ccall "add" add :: Int32 -> Int32 -> Int32 | |
$(let rsSource = | |
"#[no_mangle] pub extern \"C\" fn add(a: i32, b: i32) -> i32 { a + b }" | |
compileRust :: String -> FilePath -> IO () | |
compileRust src objPath = do | |
readProcess | |
"rustc" | |
["--crate-type", "cdylib", "--emit", "obj", "-o", objPath, "-"] | |
src | |
pure () | |
in do objPath <- addTempFile ".o" | |
runIO $ compileRust rsSource objPath | |
addForeignFilePath RawObject objPath | |
pure []) | |
main :: IO () | |
main = putStrLn $ "add 16 32 = " <> show (add 16 32) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment