10

I was working on a project that requires loading of native libraries, and so far, all development was restricted to Linux. In order to run my project, I could simply enable forking and modify java.library.path as follows:

javaOptions in run += "-Djava.library.path=some/common/path:lib/native/linux"

My question is: How can I do the same in a cross-platform way, so that I can share my build.sbt with a Windows-based developer. There are in particular three things that I couldn't figure out so far:

  • I know that SBT allows to construct platform-independent paths like "dir1" / "dir2", but I'm not aware of a cross-platform way to join multiple paths (since it is : on Linux and ; on Windows).
  • Is it possible to append either lib/native/linux or lib/native/windows dependent on the platform?
  • My approach above overwrites java.library.path -- is it possible to append instead?
bluenote10
  • 23,414
  • 14
  • 122
  • 178

1 Answers1

6

Since you can use any Scala code, you can of course do

val folderName =
  if (System.getProperty("os.name").startsWith("Windows")) "windows" else "linux"

val libPath = Seq("some/common/path", s"lib/native/$folderName").mkString(java.io.File.pathSeparator)

javaOptions in run += s"-Djava.library.path=$libPath"

though this doesn't answer your last question.

Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487
  • This is looking good. I forgot to mention that I'm still using SBT 0.12 and, in general, I was hoping for a version agnostic solution. But I think this idea works in 0.12 as well. I still have a hard time to use SBT's `/` syntax though, since there is no implicit conversion from String to (I guess) File. But obviously there is always the work-around of doing it manually with `separatorChar`. And for debugging purposes: Any idea why `show java-options` always just returns `List()`? – bluenote10 Aug 27 '14 at 14:32
  • Yes, this should work fine in 0.12. Except, IIRC, it uses Scala 2.9 for build configuration, so string interpolation should be replaced with `+`. For the last question, I don't know. Maybe ask it separately? – Alexey Romanov Aug 28 '14 at 05:33
  • 2
    What about `System.getProperty("java.library.path")` and do the concatenation yourself? – Jacek Laskowski Aug 28 '14 at 19:32
  • 1
    I guess my mistake was to assume that SBT must have a nice idiomatic solution for all that (like with modifying the classpath for instance). The idea of doing this manually just felt wrong to me. But overall: Problem solved. – bluenote10 Aug 28 '14 at 20:19