Generating file from Scala project to be used in building C program from within SBT

Viewed 45

I have an sbt project where the main class can be used to both generate a C header file and depends on compiled C program to run. In order to build this in sbt, I tried adding in a task to generate the header file:

lazy val genHeader = taskKey[Unit]("Generate header")
genHeader := Command.process("run gen", state.value)

And a task to compile the program that uses the header:

lazy val buildRunner = taskKey[Unit]("Build runner code")
buildRunner := ("gcc -I target -o target/hello src/main/c/runner.c" !)

Separately these tasks work and do what I expect. Unfortunately, if I try to wire these together using triggers and dependencies:

buildRunner := (buildRunner dependsOn genHeader).value
buildRunner := (buildRunner triggeredBy (compile in Compile)).value

The build becomes an infinite loop, because compile triggers buildRunner which depends on genHeader, which uses run, which depends on compile, which triggers buildRunner ...

I've tried to directly call the main function in my GenAndRun object, but within the build.sbt it cannot be imported (since it doesn't exist when the build is first created), and running it externally through java using ! is complicated by trying to determine the classpath to specify with all of the sbt managed dependencies.

Any suggestions on how I can break the loop?

0 Answers
Related