# LAMMPS shared library multiple-machine Makefile

SHELL = /bin/sh

# Subtargets for makefile auto
SUBTARGET_PRE_DEBUG = $(@)
OPTIONSPOSTFIX_PRE_DEBUG =
ifneq ($(origin postfix), undefined)
    ifneq (${postfix}, )
        ifneq (,$(filter $(MAKECMDGOALS), auto clean-auto))
            ifeq (${postfix}, default)
                $(error A postfix value of "default" is not allowed)
            endif
            SUBTARGET_PRE_DEBUG = $@_${postfix}
            OPTIONSPOSTFIX_PRE_DEBUG = _${postfix}
        endif
    endif
endif
SUBTARGET = $(SUBTARGET_PRE_DEBUG)
OPTIONSPOSTFIX = $(OPTIONSPOSTFIX_PRE_DEBUG)
ifneq ($(origin debug), undefined)
    ifneq (${debug}, )
        ifneq (,$(filter $(MAKECMDGOALS), auto clean-auto))
            ifeq (,$(filter $(debug), ON FULL))
                $(error Only values for debug are "ON" and "FULL")
            endif
            ifeq (${debug},ON)
                SUBTARGET = $(SUBTARGET_PRE_DEBUG)-debug
                OPTIONSPOSTFIX = $(OPTIONSPOSTFIX_PRE_DEBUG)-debug
            endif
            ifeq (${debug},FULL)
                SUBTARGET = $(SUBTARGET_PRE_DEBUG)-fulldebug
                OPTIONSPOSTFIX = $(OPTIONSPOSTFIX_PRE_DEBUG)-fulldebug
            endif
        endif
    endif
endif

# Definitions

ROOT =	lmp
EXE =	lib$(ROOT)_$(SUBTARGET).so

SRC := $(filter-out main.cpp,$(wildcard *cpp))

INC := $(wildcard *.h)

OBJ =	$(SRC:.cpp=.o)

# Targets

help:
	@echo 'Type "make target" where target is one of:'
	@echo ''
	@files="`ls MAKE/Makefile.*`"; \
	for file in $$files; do head -1 $$file; done

clean:
	rm -rf Obj_*

.DEFAULT:
	@test -f MAKE/Makefile.$@
	@if [ ! -d Obj_$(SUBTARGET) ]; then mkdir Obj_$(SUBTARGET); fi
	@cp -p $(SRC) $(INC) Obj_$(SUBTARGET)
	@cp MAKE/Makefile.$@ Obj_$(SUBTARGET)/Makefile
	@if [ ! -e Makefile.package ]; \
	  then cp Makefile.package.empty Makefile.package; fi
	@if [ ! -e Makefile.package.settings ]; \
	  then cp Makefile.package.settings.empty Makefile.package.settings; fi
	@cp Makefile.package Makefile.package.settings Obj_$(SUBTARGET)
	@cd Obj_$(SUBTARGET); \
	$(MAKE) $(MFLAGS) "OBJ = $(OBJ)" \
          "INC = $(INC)" "EXE = ../$(EXE)" shlib
	@if [ -d Obj_$(SUBTARGET) ]; then cd Obj_$(SUBTARGET); \
          rm -f $(SRC) $(INC) Makefile*; fi
	@rm -rf libliggghts.so
	@ln -s $(EXE) libliggghts.so
