# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You 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.
#

# TODO:
# @@ get rid of unused vars
#

srcdir = @srcdir@
top_srcdir = @top_srcdir@
top_builddir = @top_builddir@
prefix = @prefix@
exec_prefix = @exec_prefix@
libdir = @libdir@
ICU_PREFIX = @ICU_PREFIX@
SHREXT = @SHREXT@
INSTALL = @INSTALL@
INSTALL_PROGRAM = @INSTALL_PROGRAM@
mkdir_p = @mkdir_p@

CC = @CC@
CXX = @CXX@
AR = @AR@
RANLIB = @RANLIB@
CPPFLAGS = @CPPFLAGS@
CFLAGS = @CFLAGS@
CXXFLAGS = @CXXFLAGS@
LIBTOOL = @LIBTOOL@
LDFLAGS = @LDFLAGS@

BUILD_SHARED = @BUILD_SHARED@
BUILD_STATIC = @BUILD_STATIC@

include $(top_srcdir)/version.incl

GENRB=$(ICU_PREFIX)/bin/genrb
PKGDATA=$(ICU_PREFIX)/bin/pkgdata

# Add -v if you want to see more verbose output.
#
PKGDATAOPTS=-d . -M '"CC=$(CC)" "CXX=$(CXX)" "AR=$(AR)" "RANLIB=$(RANLIB)" \
"CPPFLAGS=$(CPPFLAGS)" "CFLAGS=$(CFLAGS)" "CXXFLAGS=$(CXXFLAGS)" "LDFLAGS=$(LDFLAGS)"'

# Resource shortname
PKGNAME=xercesc_messages_$(INTERFACE_VER_U)

# Resource files.  Add new ones for additional locales here. Keep in sync
# with the file RESLIST and don't forget to add the object file below.
#
RESFILES=root.res

# list of resource bundles  - keep in sync with RESFILES
RESLIST=$(srcdir)/res-file-list.txt

#
#
.PHONY: all
all: xercesc_messages.lo

# ICU makefiles use .o and .ao for non-PIC object file extensions
# on different platforms. We need to account for that. Also on some
# platforms ICU produces root_res.o. Overall, this is one hairy hack
# to make ICU work with libtool. If you know of a better way to handle
# this do let us (c-dev@xerces.apache.org) know.
#
xercesc_messages.lo: $(RESFILES)
ifeq ($(BUILD_SHARED),yes)
	@rm -f $(PKGNAME)_dat.o root_res.o
	$(PKGDATA) --name $(PKGNAME) --mode dll $(PKGDATAOPTS) $(RESLIST)
	mv $(PKGNAME)_dat.o $(PKGNAME)_dat-pic.o
	@if test -f root_res.o; then mv root_res.o root_res-pic.o; fi
endif
ifeq ($(BUILD_STATIC),yes)
	@rm -f $(PKGNAME)_dat.o $(PKGNAME)_dat.ao root_res.o root_res.ao
	$(PKGDATA) --name $(PKGNAME) --mode static $(PKGDATAOPTS) $(RESLIST)
	@if test -f $(PKGNAME)_dat.ao; then mv $(PKGNAME)_dat.ao $(PKGNAME)_dat.o; fi
	@if test -f root_res.ao; then mv root_res.ao root_res.o; fi
endif
	@echo "# $@ - a libtool object file" >$@
	@echo "# Generated by `$(LIBTOOL) --version | \
sed -e 's/^\([^ ]*\) (GNU \(.*\)) \(.*\)$$/\1 - GNU \2 \3/' -e q`" >>$@
	@echo "" >>$@
ifeq ($(BUILD_SHARED),yes)
	@echo "pic_object='$(PKGNAME)_dat-pic.o'" >>$@
else
	@echo "pic_object=none" >>$@
endif
ifeq ($(BUILD_STATIC),yes)
	@echo "non_pic_object='$(PKGNAME)_dat.o'" >>$@
else
	@echo "non_pic_object=none" >>$@
endif
	@echo "# root_res.lo - a libtool object file" >root_res.lo
	@echo "# Generated by `$(LIBTOOL) --version | \
sed -e 's/^\([^ ]*\) (GNU \(.*\)) \(.*\)$$/\1 - GNU \2 \3/' -e q`" >>root_res.lo
	@echo "" >>root_res.lo
ifeq ($(BUILD_SHARED),yes)
	@echo "pic_object='root_res-pic.o'" >>root_res.lo
else
	@echo "pic_object=none" >>root_res.lo
endif
ifeq ($(BUILD_STATIC),yes)
	@echo "non_pic_object='root_res.o'" >>root_res.lo
else
	@echo "non_pic_object=none" >>root_res.lo
endif
	@if test ! -f root_res-pic.o -a ! -f root_res.o; then \
$(LIBTOOL) --quiet --tag=CXX --mode=compile $(CXX) $(CPPFLAGS) $(CXXFLAGS) \
-c -o root_res.lo root_res_dummy.cpp; fi

%.res: $(srcdir)/%.txt
	$(GENRB) $^

# Let the corresponding tools (pkgdata, libtool) clean up their own
# files. pkgdata does not remove the .dat and README file for some
# reason.
#
clean:
	rm -f xercesc_messages.lo root_res.lo
ifeq ($(BUILD_SHARED),yes)
	$(PKGDATA) --name $(PKGNAME) --mode dll --clean $(PKGDATAOPTS) $(RESLIST)
	rm -f $(PKGNAME)_dat-pic.o root_res-pic.o
	rm -rf .libs
endif
ifeq ($(BUILD_STATIC),yes)
	$(PKGDATA) --name $(PKGNAME) --mode static --clean $(PKGDATAOPTS) $(RESLIST)
	rm -f README_$(PKGNAME).txt root_res.o
endif
	rm -f $(RESFILES)
	rm -f $(PKGNAME).dat

# Distclean is invoked even if we are not building with ICU.
# As a result, we cannot use pkgdata to clean up.
#
distclean:
	rm -f xercesc_messages.lo root_res.lo
	rm -f lib$(PKGNAME).so
	rm -f lib$(PKGNAME).a
	rm -f $(PKGNAME)_dat*.o root_res*.o
	rm -f $(PKGNAME)*.lst
	rm -f $(PKGNAME)*.mak
	rm -f $(RESFILES)
	rm -f $(PKGNAME).dat
	rm -f README_$(PKGNAME).txt
	rm -rf .libs

#
#
.PHONY: check
check: all

# Support for "make dist"
#
distdir:
	@srcdirstrip=`echo "$(srcdir)" | sed 's/[].[^$$\\*]/\\\\&/g'`; \
	topsrcdirstrip=`echo "$(top_srcdir)" | sed 's/[].[^$$\\*]/\\\\&/g'`; \
	list='res-file-list.txt root.txt Makefile.in resources.mak root_res_dummy.cpp'; \
	  dist_files=`for file in $$list; do echo $$file; done | \
	  sed -e "s|^$$srcdirstrip/||;t" \
	      -e "s|^$$topsrcdirstrip/|$(top_builddir)/|;t"`; \
	case $$dist_files in \
	  */*) $(MKDIR_P) `echo "$$dist_files" | \
			   sed '/\//!d;s|^|$(distdir)/|;s,/[^/]*$$,,' | \
			   sort -u` ;; \
	esac; \
	for file in $$dist_files; do \
	  if test -f $$file || test -d $$file; then d=.; else d=$(srcdir); fi; \
	  if test -d $$d/$$file; then \
	    dir=`echo "/$$file" | sed -e 's,/[^/]*$$,,'`; \
	    if test -d $(srcdir)/$$file && test $$d != $(srcdir); then \
	      cp -pR $(srcdir)/$$file $(distdir)$$dir || exit 1; \
	    fi; \
	    cp -pR $$d/$$file $(distdir)$$dir || exit 1; \
	  else \
	    test -f $(distdir)/$$file \
	    || cp -p $$d/$$file $(distdir)/$$file \
	    || exit 1; \
	  fi; \
	done


# Nothing to do for these targets.
#
install: ; @:
uninstall: ; @:
