Donnerstag, 14. November 2013

Create classpath while compiling Java with GNU Make

Compiling Java files with GNU Make can be a bit tricky. The problem is, that a class path in the command line must be specified with colons while a class path specified in a manifest file has to be specified with spaces. This makes it necessary to convert either colons to spaces or spaces to colons. The following shows how to convert spaces to colons.

The main problem is that is quite hard the specify a single space in a GNU Make file. This requires the following trick. First you have to define a variable with nothing in and after that you can define a space by delimiting it with the variable for nothing. After that you have a variable with a single space. The following part of a Make file shows how the class path conversion can be done with the previously defined space.

# Compile Java source
e :=
space := $(e) $(e)
$(CLASSES): $(BUILD)/%.class: src/ | $(BUILD)
	@$(ECHO) JAVAC: $@
	@$(JAVAC) -Xlint:unchecked \
		-cp .:$(subst $(space),:,$(addprefix jar/,$(LIBS))) \
		-sourcepath src:$(subst $(space),:,$(addprefix jar/,$(SRCS))) \
		-d $(BUILD) $(JFLAGS) $< \
		|| { $(RM) $@ && $(EXIT) 1 ; }

Keine Kommentare: