Skip to content

Commit

Permalink
chore(bindings/haskell): add rpath to haskell linker option (apache#3128
Browse files Browse the repository at this point in the history
)

Signed-off-by: silver-ymz <[email protected]>
  • Loading branch information
silver-ymz authored and Young-Flash committed Sep 19, 2023
1 parent 62c1361 commit eecc0b3
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 48 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/bindings_haskell.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ jobs:
- name: Build & Test
working-directory: "bindings/haskell"
run: |
LD_LIBRARY_PATH=../../target/debug cabal test
cabal test
- name: Save haskell cache
uses: actions/cache/save@v3
with:
Expand Down
28 changes: 5 additions & 23 deletions bindings/haskell/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,16 @@ To verify that everything is working properly, run `ghc -V` and `cabal -V`:

```shell
> ghc -V
The Glorious Glasgow Haskell Compilation System, version 9.6.1
The Glorious Glasgow Haskell Compilation System, version 9.2.8
> cabal -V
cabal-install version 3.10.1.0
compiled using version 3.10.1.0 of the Cabal library
cabal-install version 3.6.2.0
compiled using version 3.6.2.0 of the Cabal library
```

## Build

```shell
cargo build
LIBRARY_PATH=../../target/debug cabal build
cabal build
```

To clean up the build:
Expand All @@ -64,7 +63,7 @@ cabal clean
We use [`tasty`](https://hackage.haskell.org/package/tasty) as the test framework. To run the tests:

```shell
LD_LIBRARY_PATH=../../target/debug cabal test
cabal test
```

```text
Expand All @@ -85,20 +84,3 @@ cabal haddock
```

If your `cabal` version is greater than `3.8`, you can use `cabal haddock --open` to open the documentation in your browser. Otherwise, you can visit the documentation from `dist-newstyle/build/$ARCH/ghc-$VERSION/opendal-$VERSION/doc/html/opendal/index.html`.

## Misc

If you don't want to specify `LIBRARY_PATH` and `LD_LIBRARY_PATH` every time, you can use [`direnv`](https://direnv.net/) to set the environment variable automatically. Add the following to your `.envrc`:

```shell
export LIBRARY_PATH=../../target/debug:$LIBRARY_PATH
export LD_LIBRARY_PATH=../../target/debug:$LD_LIBRARY_PATH
```

If you are using [`Haskell`](https://marketplace.visualstudio.com/items?itemName=haskell.haskell) in VSCode, you may need to add the following configuration to your `settings.json`:

```json
"haskell.serverEnvironment": {
"LIBRARY_PATH": "../../target/debug:$LIBRARY_PATH"
},
```
14 changes: 2 additions & 12 deletions bindings/haskell/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,14 @@ main = do

## Build

1. Build OpenDAL Haskell Interface

```bash
cargo build --package opendal-hs
```

2. Build Haskell binding

If you don't want to install `libopendal_hs`, you need to specify library path manually by `LIBRARY_PATH=${OPENDAL_ROOT}/target/debug`.

```bash
LIBRARY_PATH=... cabal build
cabal build
```

## Test

```bash
LD_LIBRARY_PATH=... cabal test
cabal test
```

## Doc
Expand Down
16 changes: 4 additions & 12 deletions bindings/haskell/Setup.hs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ rustConfHook (description, buildInfo) flags = do
library
{ PD.libBuildInfo =
libraryBuildInfo
{ PD.extraLibDirs = dir : PD.extraLibDirs libraryBuildInfo
{ PD.extraLibDirs = dir : PD.extraLibDirs libraryBuildInfo,
PD.ldOptions = ("-Wl,-rpath," ++ dir) : (PD.ldOptions libraryBuildInfo)
}
}
}
Expand All @@ -60,27 +61,18 @@ rustConfHook (description, buildInfo) flags = do
rustBuildHook :: PD.PackageDescription -> LocalBuildInfo -> UserHooks -> BuildFlags -> IO ()
rustBuildHook pkg_descr lbi hooks flags = do
putStrLn "Building Rust code..."
let isRelease = withProfLib lbi
let cargoArgs = if isRelease then ["build", "--release"] else ["build"]
rawSystemExit (fromFlag $ buildVerbosity flags) "cargo" cargoArgs
createHSLink
putStrLn "Build Rust code success!"
buildHook simpleUserHooks pkg_descr lbi hooks flags
where
createHSLink = do
dir <- getLibDir isRelease
ghcVersion <- init <$> readProcess "ghc" ["--numeric-version"] ""
let srcPath = dir ++ "/libopendal_hs." ++ getDynamicLibExtension lbi
let destPath = dir ++ "/libopendal_hs-ghc" ++ ghcVersion ++ "." ++ getDynamicLibExtension lbi
exist <- doesFileExist destPath
when (not exist) $ createFileLink srcPath destPath
isRelease = withProfLib lbi

getLibDir :: Bool -> IO String
getLibDir isRelease = do
cargoPath <- readProcess "cargo" ["locate-project", "--workspace", "--message-format=plain"] ""
let dir = take (length cargoPath - 11) cargoPath -- <dir>/Cargo.toml -> <dir>
let targetDir = if isRelease then "release" else "debug"
return $ dir ++ "/target/" ++ targetDir
return $ dir ++ "target/" ++ targetDir

getDynamicLibExtension :: LocalBuildInfo -> String
getDynamicLibExtension lbi =
Expand Down

0 comments on commit eecc0b3

Please sign in to comment.