tags:

views:

201

answers:

1

I want to create an Action to automate GCJ compilation. Since I couldn't make it work with Ant, I decided to try SBT. The docs say how to create an Action and how to run an external process. What I don't yet see is how to reuse the directory tree traversal which exists for java and scala compiler Actions. In this case my input files would be all the .class files under a certain root folder. I would also need to specify a specific classpath for GCJ. Any pointers for this would be appreciated too.

+4  A: 

I haven't used GCJ much at all and I'm still pretty new at SBT, but this is how I believe you could write a quick task to do exactly what you are looking for with SBT 0.7.1. You can use a PathFinder to grab all of the class files like so:

val allClasses = (outputPath ##) ** "*.class"

Using that PathFinder and the "compileClasspath" top level method, you can construct a task like this which will run gcj using the current project's classpath and compose all of the .class files into one gcjFile:

val gcj = "/usr/local/bin/gcj"
val gcjFile = "target/my_executable.o"

val allClasses = (outputPath ##) ** "*.class"

lazy val gcjCompile = execTask {
  <x>{gcj} --classpath={compileClasspath.get.map(_.absolutePath).mkString(":")}  -c {allClasses.get.map(_.absolutePath).mkString("-c ")} -o {gcjFile}</x>
} dependsOn(compile) describedAs("Create a GCJ executable object")
Aaron
I will try this... one more question, is this getting the classpath defined for scala compilation? I would like to use one specific for gcj.
Germán
This does use the classpath defined for Scala compilation. If you want to use a different one, then you can either hard code that or you could put all of the JARs into a certain directory and use a PathFinder to pull those all in.
Aaron
Great. I needed to import Process._ for the xml conversion to work, and I tweaked some more. The answer was great help, thanks.
Germán