# 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

INCLUDED_CLIBRARY = YES

#
# $Id$
#
# This makefile provides the following phony targets:
#
#    all_libs  export_hdrs  export_libs  install
#
#
# 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)
# LIB_DEPENDS  (opt) (any extra dependencies to assert before making LIBRARIES)
# EXPDIR       (opt) (the target directory - otherwise <Lib$Dir>.${TARGET} or
#                                  for a disc install ${INSTDIR}.${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)
# EXPORTS      (opt) (dependencies for export_hdrs phase - otherwise deduced from ${HDRS} and ${ASMHDRS})
# 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) (release application object files, no o. or oz. prefixes - otherwise ${OBJS})
# APP_DBG_OBJS (opt) (debug application object files, no o. or oz. prefixes - otherwise ${DBG_OBJS})
# MOD_OBJS     (opt) (release module object files, no o. or oz. prefixes - otherwise ${OBJS})
# MOD_DBG_OBJS (opt) (debug module object files, no o. or oz. prefixes - otherwise ${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
DIRS         ?= o._dirs
HDRS         ?= ${TARGET}
ASMHDRS      ?=
DBG_OBJS     ?= ${OBJS}
APP_OBJS     ?= ${OBJS}
APP_DBG_OBJS ?= ${DBG_OBJS}
MOD_OBJS     ?= ${OBJS}
MOD_DBG_OBJS ?= ${DBG_OBJS}
APP_OBJS_     = $(addprefix o.,${APP_OBJS})
APP_DBG_OBJS_ = $(addprefix od.,${APP_DBG_OBJS})
MOD_OBJS_     = $(addprefix oz.,${MOD_OBJS})
MOD_DBG_OBJS_ = $(addprefix odz.,${MOD_DBG_OBJS})

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

ifeq ($(filter install%,${MAKECMDGOALS}),)
EXPDIR       ?= ${LIBDIR}.${TARGET}
else
EXPDIR       ?= ${INSTDIR}.${TARGET}
endif

ifeq ("${INCLUDED_STDTOOLS}","")
include StdTools
endif

ifeq ("${INCLUDED_STDRULES}","")
include StdRules
endif
ifeq ("${INCLUDED_DBGRULES}","")
include DbgRules
endif

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}

install 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: ${EXPORTS} ${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} ${LIBDEPENDS}
       ${AR} ${ARFLAGS} ${LIBEXT}.${LIBRARY} ${APP_OBJS_}

${LIBEXT}.${LIBRARYZM}${CUSTOMLIBZM}: ${MOD_OBJS_} ${DIRS} ${LIBDEPENDS}
       ${AR} ${ARFLAGS} ${LIBEXT}.${LIBRARYZM} ${MOD_OBJS_}

${LIBEXT}.${LIBRARYD}${CUSTOMLIBD}: ${APP_DBG_OBJS_} ${DIRS} ${LIBDEPENDS}
       ${AR} ${ARFLAGS} ${LIBEXT}.${LIBRARYD} ${APP_DBG_OBJS_}

${LIBEXT}.${LIBRARYDZM}${CUSTOMLIBDZM}: ${MOD_DBG_OBJS_} ${DIRS} ${LIBDEPENDS}
       ${AR} ${ARFLAGS} ${LIBEXT}.${LIBRARYDZM} ${MOD_DBG_OBJS_}

# EOF