JX_ROOT  := ..

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

ACE_MAKE_FLAGS :=

ifdef J_COMPILE_DEBUG
  ACE_MAKE_FLAGS += debug=1
else
  ACE_MAKE_FLAGS += debug=0
endif

#
# ACE library
#

.PHONY : jxinstall
ifdef J_FORCE_ACE_STATIC
  jxinstall: static
	@${RM} -r ACE_wrappers/ace/libACE.so ACE_wrappers/ace/.shobj/
else
ifdef J_BUILD_SHARED_LIB
  jxinstall: shared
else
  jxinstall: static
endif
endif
  ifeq ($(shell whoami),root)
	@if { test -f ACE_wrappers/ace/libACE.a; } then \
         { \
         cp ACE_wrappers/ace/libACE.a ${JX_LIB_ROOT}/libACE-${ACE_LIB_VERSION}.a; \
         } \
     fi
	@if { test -f ACE_wrappers/ace/libACE.so; } then \
         { \
         cp ACE_wrappers/ace/libACE.so ${JX_LIB_ROOT}/libACE-${ACE_LIB_VERSION}.so; \
         } \
     fi
  endif

.PHONY : shared
shared:
	@cd ACE_wrappers/ace; ${MAKE} debug=0 shared_libs=1 static_libs=0

.PHONY : static
static:
	@cd ACE_wrappers/ace; ${MAKE} ${ACE_MAKE_FLAGS} shared_libs=0 static_libs=1

#
# JX source distribution (DISTR_TAR_FILE)
#

SRC_FILE_DIR := ${JX}/ACE

EXTRA_ACE_FILES := VERSION ACE-INSTALL* docs/ \
                   BIBLIOGRAPHY FAQ README COPYING PROBLEM-REPORT-FORM \
                   bin/ace_components etc/

.PHONY : jxsource
jxsource:
	@cd ${J_DISTR_TAR_DIR}; \
     tar -rf ${DISTR_TAR_FILE} \
             ${SRC_FILE_DIR}/README \
             ${SRC_FILE_DIR}/Makefile \
             ${JX}/lib/libACE-${ACE_LIB_VERSION}.* \
             ${addprefix ${SRC_FILE_DIR}/ACE_wrappers/, ${EXTRA_ACE_FILES} }
	@cd ${J_DISTR_TAR_DIR}; \
     tar -rf ${DISTR_TAR_FILE} ${filter-out %.a %.so %~ %/ace/config.h, \
                                 ${shell cd ${J_DISTR_TAR_DIR}; echo \
             ${SRC_FILE_DIR}/ACE_wrappers/ace/* \
             ${SRC_FILE_DIR}/ACE_wrappers/include/makeinclude/* }};
	@cd test; ${MAKE} jxsource

#
# JX binary distributions (DISTR_TAR_FILE)
#

.PHONY : jxshared
jxshared: jx_bin_distr
  ifndef J_FORCE_ACE_STATIC
	@cd ${J_DISTR_TAR_DIR}; \
     tar -rhf ${DISTR_TAR_FILE} ${JX}/lib/libACE-${ACE_LIB_VERSION}.so
  endif
	@cd test; ${MAKE} jxshared

.PHONY : jxlink
jxlink: jx_bin_distr
	@cd test; ${MAKE} jxlink

.PHONY : jx_bin_distr
jx_bin_distr:
	@strip -g ACE_wrappers/ace/libACE.*
	@cd ${J_DISTR_TAR_DIR}; \
     tar -rhf ${DISTR_TAR_FILE} \
              ${SRC_FILE_DIR}/README \
              ${JX}/lib/libACE-${ACE_LIB_VERSION}.a \
              ${addprefix ${SRC_FILE_DIR}/ACE_wrappers/, ${EXTRA_ACE_FILES} }
	@cd ${J_DISTR_TAR_DIR}; \
     tar -rf ${DISTR_TAR_FILE} ${filter-out Makefile %.a %.so %~, \
                                 ${shell cd ${J_DISTR_TAR_DIR}; echo \
             ${SRC_FILE_DIR}/ACE_wrappers/ace/* }};

#
# remove object files
#

ACE_TIDY      := cd ACE_wrappers/ace; ${MAKE} clean shared_libs=1 static_libs=1
ACE_CLEAN     := cd ACE_wrappers/ace; ${MAKE} realclean shared_libs=1 static_libs=1
ACE_UNINSTALL := ${RM} ${JX_LIB_ROOT}/libACE-${ACE_LIB_VERSION}.*

.PHONY : tidy
tidy::
	@if { test ! -L ACE_wrappers; } \
     then { ${ACE_TIDY}; } fi
	@cd test; ${MAKE} tidy

.PHONY : clean
clean::
	@if { test ! -L ACE_wrappers; } \
     then { ${ACE_CLEAN}; } fi
	@cd test; ${MAKE} clean

.PHONY : jxuninstall
jxuninstall:
  ifeq ($(shell whoami),root)
	@if { test ! -L ACE_wrappers; } \
     then { ${ACE_UNINSTALL}; } fi
  endif
