# To use this file, first run "makemake" and then run "make".
# (makemake is part of the JX distribution)

# Useful directories

JX_ROOT := ../..

CODEDIR  := .
JCOREDIR := ${JX_ROOT}/include/jcore
REGEXDIR := ${JX_ROOT}/misc/regex

JLIBDIR   := ${JX_ROOT}/lib
ACELIBDIR := ${JX_ROOT}/ACE

# Directories to search for header files

SEARCHDIRS := -I- \
              -I${CODEDIR} \
              -I${JCOREDIR} \
              -I${ACE_ROOT}

# other constants

SCRIPT_FILES := maketouch makecheck
DISTR_FILES  := README LICENSE makemake ${SCRIPT_FILES}

# makemake variables

include ${JX_ROOT}/include/make/jx_constants
include ${JX_ROOT}/include/make/jx_config

DEPENDFLAGS := ${J_COMPILER_DEPEND_FLAGS} \
               ${J_COMPILE_DEBUG} ${J_BUILD_SHARED_LIB} \
               -DJ_MAKE_BINARY_NAME=\"${firstword ${MAKE}}\" \
               ${J_RANLIB_VIA_AR_S} \
               ${SEARCHDIRS}

# make variables

LOADLIBES := ${J_ACE_LIBS} ${J_GCC_LIBS}
LDFLAGS   := 

# JMemoryManager files

ifdef J_ARRAY_NEW_OVERRIDABLE
  JMM_FILES := ${addprefix ${DJ_MM_DIR}/, \
                 jNew.o JMemoryManager.o JMMTable.o JMMArrayTable.o \
                 JMMHashTable.o JMMMonitor.o JMMErrorPrinter.o \
                 JMMRecord.o JArray-JMMRecord.o JHashTable-JMMRecord.o}
else
  JMM_FILES :=
endif

#####

.PHONY : default
default: regex makemake

.PHONY : Makefiles
Makefiles:

#
# libraries we have eaten
#

.PHONY : regex
regex:
	@cd ${REGEXDIR}; ${MAKE} for_jcore

#
# JX source distribution (DISTR_TAR_FILE)
#

SRC_FILE_DIR := ${JX}/programs/makemake

.PHONY : jxsource
jxsource:
	@cd ${J_DISTR_TAR_DIR}; \
     tar -rf ${DISTR_TAR_FILE} ${addprefix ${SRC_FILE_DIR}/, \
             README LICENSE Make.header Make.files Makefile.port \
             ${wildcard *.cc *.h} ${SCRIPT_FILES} }

#
# JX binary distributions (DISTR_TAR_FILE)
#

.PHONY : jxshared
jxshared: makemake
	@strip makemake
	@cd ${J_DISTR_TAR_DIR}; \
     tar -rf ${DISTR_TAR_FILE} ${addprefix ${SRC_FILE_DIR}/, ${DISTR_FILES}}

#
# install binaries
#

.PHONY : jxinstall
jxinstall: makemake
	@strip makemake
	@mv makemake ${JX_INSTALL_ROOT}/
	@chmod a+x ${SCRIPT_FILES}
	@cp ${SCRIPT_FILES} ${JX_INSTALL_ROOT}/

.PHONY : jxuninstall
jxuninstall:
	@${RM} ${addprefix ${JX_INSTALL_ROOT}/, makemake ${SCRIPT_FILES}}

#
# jcc binary distribution (JCC_TAR_DIR)
#

JCC_FILE_DIR := ${JCC_TAR_DIR}/makemake

.PHONY : jccshared
jccshared: debug distr_sub

.PHONY : jccstatic
jccstatic: regex staticACE
	@${RM} makemake
	@${MAKE} "LOADLIBES=-static ${LOADLIBES}" makemake
	@${MAKE} distr_sub

.PHONY : distr_sub
distr_sub:
	@strip makemake
	@mkdir ${JCC_FILE_DIR}
	@cp ${DISTR_FILES} ${JCC_FILE_DIR}/
	@cp Make.header ${JCC_FILE_DIR}/Make.header_sample
	@cp Make.files  ${JCC_FILE_DIR}/Make.files_sample

#
# debugging version
#

DEBUG_LOADLIBES := -L${JLIBDIR} \
                   -Xlinker -Bstatic \
                   ${J_ACE_LIBS} \
                   -Xlinker -Bdynamic \
                   ${J_GCC_LIBS}

.PHONY : debug
debug: regex staticACE
	@${RM} makemake
	@${MAKE} "LOADLIBES=${DEBUG_LOADLIBES}" makemake

.PHONY : staticACE
staticACE:
	@cd ${ACELIBDIR}; ${MAKE} static

#
# personal install
#

.PHONY : jafl
jafl: debug
	@strip makemake
	@mv makemake ${JX_INSTALL_ROOT}/
