# Copyright 1996-2022 Cyberbotics Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

### Generic Webots Makefile system for Java controllers
###
### Platforms: Windows, macOS, Linux
### Languages: Java
###
###-----------------------------------------------------------------------------
###
### This file is meant to be included from the Makefile_java files located in
### the Webots controllers directories. It will automatically compile all the
### .java source files located in the controllers folder.
### It is possible to set the compilation options and the resulting .jar file
### name setting the following variables:
###
### CLASSPATH, JAVAC_OPTS, and JAR_FILE
###

# WEBOTS_HOME is a sine qua non condition to run this Makefile
ifndef WEBOTS_HOME
 $(error The WEBOTS_HOME environment variable is not defined.)
endif

# WEBOTS_HOME_PATH is commonly defined in the caller Makefile
# but as it is not necessary on non-windows OS, it seems safer to reconstruct it there if required
ifndef WEBOTS_HOME_PATH
 null :=
 space := $(null) $(null)
 WEBOTS_HOME_PATH=$(subst $(space),\ ,$(strip $(subst \,/,$(WEBOTS_HOME))))
endif


###-----------------------------------------------------------------------------
### OS determination and OS specific commands
###-----------------------------------------------------------------------------

include $(WEBOTS_HOME_PATH)/resources/Makefile.os.include

###-----------------------------------------------------------------------------
### Paths
###-----------------------------------------------------------------------------

ifeq ($(OSTYPE),darwin)
 WEBOTS_JAVA_LIB=$(WEBOTS_HOME_PATH)/Contents/lib/controller/java
else
 WEBOTS_JAVA_LIB=$(WEBOTS_HOME_PATH)/lib/controller/java
endif

# compute the current directory, the CURDIR variable may be already set
ifndef CURDIR
 CURDIR = $(shell pwd)
endif

# compute the name of the controller from its directory (i.e. braiten)
BAD_NAME = $(basename $(notdir $(CURDIR)))

# this is a hack to work around a problem with spaces in dir names
NAME = $(word $(words $(BAD_NAME)),$(BAD_NAME))

# set jar file name
ifndef JAR_FILE
  JAR_FILE = $(NAME).jar
endif

###-----------------------------------------------------------------------------
### Compilation options
###-----------------------------------------------------------------------------

# set the Java compiler options
ifneq ($(strip $(CLASSPATH)),)
  ifeq ($(OSTYPE),windows)
   JAVAC_OPTS += -Xlint -classpath "$(WEBOTS_CONTROLLER_LIB_PATH)/java/Controller.jar;$(CLASSPATH);." --release 8
  else
   JAVAC_OPTS += -Xlint:-options -Xlint -classpath "$(WEBOTS_CONTROLLER_LIB_PATH)/java/Controller.jar:$(CLASSPATH):." --release 8
  endif
 else
  ifeq ($(OSTYPE),windows)
   JAVAC_OPTS += -Xlint -classpath "$(WEBOTS_CONTROLLER_LIB_PATH)/java/Controller.jar;." --release 8
  else
   JAVAC_OPTS += -Xlint:-options -Xlint -classpath "$(WEBOTS_CONTROLLER_LIB_PATH)/java/Controller.jar":. --release 8
  endif
endif

###-----------------------------------------------------------------------------
### Makefile setup and rules
###-----------------------------------------------------------------------------

.PHONY: all clean release debug profile

ifeq ($(JAVA_HOME),)
release debug profile:
	@echo -e "# \033[0;33mJava not installed or 'JAVA_HOME' not set, skipping Java API\033[0m"
else ifeq (,$(wildcard $(WEBOTS_JAVA_LIB)))
release debug profile:
	@echo -e "# \033[0;33mJava library not compiled, skipping Java API\033[0m"
else
release debug profile:
	@javac $(JAVAC_OPTS) *.java

$(JAR_FILE):
	@javac $(JAVAC_OPTS) *.java
	@jar cf $@ *.class

jar: $(JAR_FILE)
endif

clean:
	@rm -fr *.class  *.jar > /dev/null 2>&1 || :
