# Copyright 2008 Castle Technology 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.
#
# Makefile fragment for C and C++ libraries for applications and modules
#
# $Id$
#
# This makefile provides the following phony targets:
#
#    all_libs  export_hdrs  export_libs
#
#
# This fragment uses the following macros set by the master makefile.
#
#
# COMPONENT          (the name of the component)
# TARGET       (opt) (the leafname of the primary target - otherwise ${COMPONENT})
# LIBRARY      (opt) (the leafname of the application library - otherwise ${TARGET})
# LIBRARYZM    (opt) (the leafname of the module library - otherwise ${LIBRARY}zm)
# LIBRARYD     (opt) (the leafname of the debug app library - otherwise ${LIBRARY}d)
# LIBRARYDZM   (opt) (the leafname of the debug module library - otherwise ${LIBRARY}dzm)
# LIBRARIES    (opt) (libraries to build/export - otherwise ${LIBRARY} and ${LIBRARYZM})
# LIBEXT       (opt) (subdir or filename extension for library files - otherwise o)
# EXPDIR       (opt) (the target directory - otherwise <Lib$Dir>.${TARGET})
# DIRS         (opt) (stamp object for directory creation - otherwise o._dirs)
# HDRS         (opt) (header files to export, no h. prefix - otherwise ${TARGET})
# ASMHDRS      (opt) (assembly header files to export, no Hdr. prefix - otherwise none)
# OBJS               (object files, no o. or oz. prefixes)
# DBG_OBJS     (opt) (debug build object files, no o. or oz. prefixes - otherwise ${OBJS})
# APP_OBJS     (opt) (object files for application version - otherwise derived from ${OBJS})
# APP_DBG_OBJS (opt) (object files for debug app version - otherwise derived from ${DBG_OBJS})
# MOD_OBJS     (opt) (object files for module version - otherwise derived from ${OBJS})
# MOD_DBG_OBJS (opt) (object files for debug module version - otherwise derived from ${DBG_OBJS})
# CUSTOMLIB    (opt) (set to "custom" to override the application library rule)
# CUSTOMLIBZM  (opt) (set to "custom" to override the module library rule)
# CUSTOMLIBD   (opt) (set to "custom" to override the debug application library rule)
# CUSTOMLIBDZM (opt) (set to "custom" to override the debug module library rule)
#
#
# It relies on the following from the build system:
#
#
# PHASE            (export phase discriminator)
#
#
# It relies on the following generic tool macros from the StdTools makefile
#
#
# CP + CPFLAGS     (copy, cp etc.)
# WIPE + WFLAGS    (recursive delete)
# RM               (non-recursive delete)
# AS + ASFLAGS     (assembler)
# MKDIR            (cdir/mkdir -p)
# ECHO
# AR + ARFLAGS     (libfile/ar)
# TOUCH            (create/touch)
# NOP
#
#
# It relies on the following from the StdRules makefile
#
#
# .c.o  .c.oz  .c++.o  .cpp.o  .c++.oz  .c++.oz  .s.o  .s.oz
#
#
# It relies on the following from the DbgRules makefile
#
#
# CDFLAGS  C++DFLAGS  ASDFLAGS
# .c.od  .c.odz  .c++.od  .cpp.od  .c++.odz  .cpp.odz  .s.od  .s.odz
#
#

LIBDIR        = <Lib$Dir>

TARGET       ?= ${COMPONENT}
LIBRARY      ?= ${TARGET}
LIBRARYZM    ?= ${LIBRARY}zm
LIBRARYD     ?= ${LIBRARY}d
LIBRARYDZM   ?= ${LIBRARY}dzm
LIBRARIES    ?= ${LIBRARYZM} ${LIBRARY}
LIBEXT       ?= o
EXPDIR       ?= ${LIBDIR}.${TARGET}
DIRS         ?= o._dirs
HDRS         ?= ${TARGET}
ASMHDRS      ?=
DBG_OBJS     ?= ${OBJS}
APP_OBJS     ?= $(addprefix o.,${OBJS})
APP_DBG_OBJS ?= $(addprefix od.,${DBG_OBJS})
MOD_OBJS     ?= $(addprefix oz.,${OBJS})
MOD_DBG_OBJS ?= $(addprefix odz.,${DBG_OBJS})

EXPORTING_HDRS    = $(addprefix exphdr.,${HDRS})
EXPORTING_ASMHDRS = $(addprefix expasm.,${ASMHDRS})
EXPORTING_LIBS    = $(addprefix explib.,${LIBRARIES})
TARGET_LIBS       = $(addprefix ${LIBEXT}.,${LIBRARIES})

all_libs: ${TARGET_LIBS}
        @${ECHO} ${COMPONENT}: library built

${DIRS} ::
        ${MKDIR} o
        ${MKDIR} oz
        ${MKDIR} od
        ${MKDIR} odz
        ${MKDIR} ${LIBEXT}
        ${TOUCH} $@

clean ::
        @IfThere o         Then ${ECHO} ${WIPE} o ${WFLAGS}
        @IfThere o         Then ${WIPE} o ${WFLAGS}
        @IfThere oz        Then ${ECHO} ${WIPE} oz ${WFLAGS}
        @IfThere oz        Then ${WIPE} oz ${WFLAGS}
        @IfThere od        Then ${ECHO} ${WIPE} od ${WFLAGS}
        @IfThere od        Then ${WIPE} od ${WFLAGS}
        @IfThere odz       Then ${ECHO} ${WIPE} odz ${WFLAGS}
        @IfThere odz       Then ${WIPE} odz ${WFLAGS}
        @IfThere ${LIBEXT} Then ${ECHO} ${WIPE} ${LIBEXT} ${WFLAGS}
        @IfThere ${LIBEXT} Then ${WIPE} ${LIBEXT} ${WFLAGS}
        @${ECHO} ${COMPONENT}: cleaned

export: export_${PHASE}
        ${NOP}

export_: export_libs export_hdrs
        ${NOP}

create_exp_hdr_dirs:
        ${MKDIR} ${EXPDIR}.h
        ${MKDIR} ${EXPDIR}.Hdr

create_exp_lib_dir:
        ${MKDIR} ${EXPDIR}.${LIBEXT}

.SUFFIXES: .exphdr .expasm .explib .h .Hdr .${LIBEXT}
.h.exphdr:;         ${CP} $< ${EXPDIR}.$< ${CPFLAGS}
.Hdr.expasm:;       ${CP} $< ${EXPDIR}.$< ${CPFLAGS}
.${LIBEXT}.explib:; ${CP} $< ${EXPDIR}.$< ${CPFLAGS}

export_hdrs: ${EXPORTING_ASMHDRS} ${EXPORTING_HDRS} ${DIRS} create_exp_hdr_dirs
        @IfThere VersionNum then ${ECHO} ${CP} VersionNum ${EXPDIR}.LibVersion ${CPFLAGS}
        @IfThere VersionNum then ${CP} VersionNum ${EXPDIR}.LibVersion ${CPFLAGS}
        @IfThere ${EXPDIR}.h.*   then else ${RM} ${EXPDIR}.h
        @IfThere ${EXPDIR}.Hdr.* then else ${RM} ${EXPDIR}.Hdr
        @${ECHO} ${COMPONENT}: header export complete

export_libs: ${EXPORTING_LIBS} ${TARGET_LIBS} ${DIRS} create_exp_lib_dir
        @IfThere VersionNum then ${ECHO} ${CP} VersionNum ${EXPDIR}.LibVersion ${CPFLAGS}
        @IfThere VersionNum then ${CP} VersionNum ${EXPDIR}.LibVersion ${CPFLAGS}
        @IfThere ${EXPDIR}.${LIBEXT}.* then else ${RM} ${EXPDIR}.${LIBEXT}
        @${ECHO} ${COMPONENT}: library export complete

${LIBEXT}.${LIBRARY}${CUSTOMLIB}: ${APP_OBJS} ${DIRS}
       ${AR} ${ARFLAGS} -o ${LIBEXT}.${LIBRARY} ${APP_OBJS}

${LIBEXT}.${LIBRARYZM}${CUSTOMLIBZM}: ${MOD_OBJS} ${DIRS}
       ${AR} ${ARFLAGS} -o ${LIBEXT}.${LIBRARYZM} ${MOD_OBJS}

${LIBEXT}.${LIBRARYD}${CUSTOMLIBD}: ${APP_DBG_OBJS} ${DIRS}
       ${AR} ${ARFLAGS} -o ${LIBEXT}.${LIBRARYD} ${APP_DBG_OBJS}

${LIBEXT}.${LIBRARYDZM}${CUSTOMLIBDZM}: ${MOD_DBG_OBJS} ${DIRS}
       ${AR} ${ARFLAGS} -o ${LIBEXT}.${LIBRARYDZM} ${MOD_DBG_OBJS}

# EOF