2

I have in my makefile currently this implicit rule:

.java.class:
    $(JAVAC) -classpath . $<

What I need to achieve is not only to use this rule on .java files but also on .sqlj files.

I need something like this:

(.java.sqlj).class:
    $(SQLJC) -classpath . $<

The reason for that is I have a circular dependency between one of my java files and one of my sqlj files that only gets resolved when compiling both the .java files and the .sqlj files in one step. As far as I tried it the sqlj translator can also compile .java files so this should be no problem.

jteichert
  • 527
  • 1
  • 4
  • 20

1 Answers1

1

You can't use Old-Fashioned Suffix Rules for this.

You need to use Implicit Rules for this.

So you would write

%.class: %.java %.sqlj
        $(SQLJC) -classpath . $<

Assuming the %.java file was the main input. If $(SQLJC) needs the .sqlj file instead then using

%.class: %.sqlj %.java
        $(SQLJC) -classpath . $<

would do that.

If you need to pass both the .sqlj and .java files to $(SQLJC) at the same time then replace $< with $^ in either of those examples.

To compile all .java and .sqlj files together at once you want something more like this:

# Assumes files are all in the current directory.
FILES=$(wildcard *.java) $(wildcard *.sqlj)

tgt.class: $(FILES)
        $(SQLJC) -classpath . $^

But assuming you can actually compile any of your .java or .sqlj files to some intermediate format (.class?) individually doing things this way will lose you the benefit of only needing to do that for each input file when it changes. This will recompile every file every time any of them changes.

Etan Reisner
  • 77,877
  • 8
  • 106
  • 148
  • Thanks for the answer, Would this translate to `%.class: A.sqlj B.java` or to `%.class: A.sqlj A.java` ? – jteichert Dec 02 '15 at 08:34
  • The rule should take all .java and .sqlj files and compile them with `$(SQLJC)` in one go. – jteichert Dec 02 '15 at 09:26
  • *All* `.java` and `.sqlj` files? Not just paired `.java`/`.sqlj` files? Because your original wasn't doing anything of that sort in either the `.java.class` or your attempted `(.java.sqlj).class` versions. – Etan Reisner Dec 02 '15 at 13:47
  • Yes all of them, I just realized I had no idea how makefiles are doing their thing. gmake takes each file that matches and passes it to the compile command seperately, right? – jteichert Dec 02 '15 at 14:19
  • make does whatever you tell it to. If you give it a suffix or pattern rule it applies that to files "individually" (for whatever definition of individually matches the suffix/patterns). – Etan Reisner Dec 02 '15 at 14:21