/*
 * Imake rules for building libraries, programs, and other things. 
 * These rules are created specifically for RCCL/RCI, by John Lloyd,
 * McGill University, November, 1990
 */

/*
 *		   MACHINE-INDEPENDENT RULES; DO NOT MODIFY
 *
 * Warning, when defining rules: make sure you don't include both a trailing
 * backslash and double ampersand backslash or else you will get an extra
 * backslash in the Makefile.
 *
 */

/* This is a real hack ... but we can't assume CC -M will work for .s files */
#if defined(VaxArch)
#define DependFiles *.[cs]
#else
#define DependFiles *.c
#endif

#ifdef RanlibCmd
#define DoRanlib(lib)RanlibCmd lib
#else
#define DoRanlib(lib)/bin/true
#endif

#if SetUidOnRciBins
#define SetUidIfNecessary(file)chmod u+s file
#else
#define SetUidIfNecessary(file)# chmod u+s file
#endif

#if StripInstalledPrograms
#define StripIfNecessary(file)strip file
#else
#define StripIfNecessary(file)# strip file
#endif

#if !VxWorksConfig

#ifndef SgiArch
#define doGenLib(lib,mods,objs,targ,stub).PHONY: targ			@@\
targ:	mods lib(__.SYMDEF)						@@\
lib(__.SYMDEF): objs							@@\
	DoRanlib(lib)
#else
#define doGenLib(lib,mods,objs,targ,stub).PHONY: targ			@@\
targ:	mods
#endif

#define rciUserModule(mod)						@@\
ULIST := $(ULIST) $(LUSER)(mod.o)					@@\
UOBJS := $(UOBJS) mod.o

#define maintainRciUser(targ)doGenLib($(LUSER),$(ULIST),$(UOBJS),targ,rciUser)

#define rciCtrlModule(mod)						@@\
CLIST := $(CLIST) $(LCTRL)(mod.o)					@@\
COBJS := $(COBJS) mod.o							@@\
mod.o:  mod.c								@@\
	$(RCC) $(CFLAGS) -c CTRL $<

#define rciCppCtrlModule(mod)						@@\
CLIST := $(CLIST) $(LCTRL)(mod.o)					@@\
COBJS := $(COBJS) mod.o							@@\
mod.o:  mod.cpp								@@\
	gcc $(CFLAGS) -c $<

#define rciCtrlModuleDep(mod)						@@\
CLIST := $(CLIST) $(LCTRL)(mod.o)					@@\
COBJS := $(COBJS) mod.o

#define maintainRciCtrl(targ)doGenLib($(LCTRL),$(CLIST),$(COBJS),targ,rciCtrl)

#define libPumaModule(mod)						@@\
PLIST := $(PLIST) $(LPUMA)(mod.o)					@@\
POBJS := $(POBJS) mod.o							@@\
mod.o:  mod.c								@@\
	$(RCC) $(CFLAGS) -c CTRL $<

#define maintainLibPuma(targ)doGenLib($(LPUMA),$(PLIST),$(POBJS),targ,libPuma)

#define basicProgram(pname,objs,libs)pname:  objs			@@\
	$(CC) $(CFLAGS) -o $@ objs libs $(CLIBS)			@@\

#define rccProgram(prog,uo,co,libs)					@@\
prog:  $(VOLATILE_RCILIBS)						@@\
prog:  uo co								@@\
	$(RCC) $(CFLAGS) -o $@ uo CTRL co $(CLIBS) libs			@@\
	SetUidIfNecessary($@)

#define rccTestProgram(prog,uo,co,libs)					@@\
prog:	$(VOLATILE_RCILIBS)						@@\
prog:  uo co $(OU) $(OC)						@@\
	$(RCC) $(CFLAGS) -o $@ uo $(OU) CTRL co $(OC) $(CLIBS) libs	@@\
	SetUidIfNecessary($@)

#define rccUtilityProgram(prog,uo,co,libs)UTARGS := $(UTARGS) $(BDIR)/prog @@\
rccProgram($(BDIR)/prog,uo,co,libs)					@@\
	StripIfNecessary($@)						@@\

#define maintainUtils(targ)targ:	$(UTARGS)

#define rccModule(mod)mod.o:  mod.c					@@\
	$(RCC) $(CFLAGS) -c $<						@@\

#define ctrlModule(mod)mod.o:  mod.c					@@\
	$(RCC) $(CFLAGS) -c CTRL $<					@@\

#define maintainVxwLibs(file,targ)
#define maintainVxwBins(file,targ)
#define vxwBinModule(mod)

#define utilityProgram(prog,objs,libs)UTARGS := $(UTARGS) $(BDIR)/prog	@@\
$(BDIR)/prog:  objs							@@\
	$(CC) $(CFLAGS) -o $(BDIR)/prog objs libs $(CLIBS)		@@\
	StripIfNecessary($(BDIR)/prog)					@@\

#define utilityManPage(dst,src)UTARGS := $(UTARGS) dst			@@\
dst:  src								@@\
	cp src $@							@@\
/* MS Remove the packdeps command */
#define makeDepend(opts)depend::					@@\
	/bin/ls *.c | sed -e 's/.c$$/.s/' | xargs rm -f			@@\
	@if [ -f DependFile ] ; then \					@@\
	   echo mv DependFile DependFile.BAK; \				@@\
	   mv DependFile DependFile.BAK; else exit 0; fi		@@\
	MakeDependCmd $(C_INCS) $(C_DEFS) opts DependFiles \		@@\
	> DependFile							@@\
									@@\
DependFile:								@@\
include DependFile

#else	/*************** vxWorks definitions *******************/

#define basicProgram(pname,objs,libs)pname:  objs			@@\
	$(LD) -r -o $@ objs						@@\

#define rccProgram(pname,uo,co,libs)pname:  uo co			@@\
	$(LD) -r -o $@ uo co						@@\

#define rccTestProgram(pname,uo,co,libs)pname:  uo co $(OU) $(OC)	@@\
	$(LD) -r -o $@ uo co $(OU) $(OC)				@@\

#define rciUserModule(mod)VXWLO := $(VXWLO) mod.o

#define rciCtrlModule(mod)VXWLO := $(VXWLO) mod.o

#define rciCtrlModuleDep(mod)VXWLO := $(VXWLO) mod.o

#define libPumaModule(mod)VXWLO := $(VXWLO) mod.o

#define vxwBinModule(mod)VXWBO := $(VXWBO) mod.o

#define maintainRciUser(targ)
#define maintainRciCtrl(targ)
#define maintainLibPuma(targ)
#define maintainUtils(targ)

#define maintainVxwLibs(file,targ).PHONY:	targ			@@\
targ:		$(LDIR)/VXW/file.o					@@\
$(LDIR)/vxw.o:	$(LDIR)/VXW/file.o					@@\
$(LDIR)/VXW/file.o: $(VXWLO)						@@\
		$(LD) -r -o $@ $(VXWLO)					@@\

#define maintainVxwBins(file,targ).PHONY:	targ			@@\
targ:		$(LDIR)/VXW/file.o					@@\
$(LDIR)/vxw.o:	$(LDIR)/VXW/file.o					@@\
$(LDIR)/VXW/file.o: $(VXWBO)						@@\
		$(LD) -r -o $@ $(VXWBO)					@@\

#define rccModule(mod)mod.o:  mod.c					@@\
	$(CC) $(CFLAGS) -c $<						@@\

#define ctrlModule(mod)mod.o:  mod.c					@@\
	$(CC) $(CFLAGS) -c $<						@@\

#define rccUtilityProgram(pname,uo,co,libs)
#define utilityProgram(prog,objs,libs)
#define utilityManPage(dep,dst,src)

#define makeDepend(opts)depend::					@@\
	/bin/ls "../"*.c | sed -e 's/.c$$/.s/' | xargs rm -f		@@\
	@if [ -f DependFile ] ; then \					@@\
	   echo mv DependFile DependFile.BAK; \				@@\
	   mv DependFile DependFile.BAK; else exit 0; fi		@@\
	MakeDependCmd $(C_INCS) $(C_DEFS) opts "../"DependFiles | \	@@\
	PackDependCmd > DependFile					@@\
									@@\
DependFile:								@@\
include DependFile

#endif	/*************** end of vxWorks definitions *******************/

#define basicModule(mod)mod.o:  mod.c					@@\
	$(CC) $(CFLAGS) -c $<						@@\

#define buildLibrary(lib,objs)lib:  objs				@@\
	ar rv lib objs							@@\
	DoRanlib(lib)							@@\

#define compileShadow(src,obj,dst,cmd)@if [ -f obj ] ; then \		@@\
	   tmp=\#obj_tmp$$$$; mv obj $$tmp; fi; \			@@\
	echo cmd src '[' -o dst ']' ; cmd src; mv obj dst; \		@@\
	if [ "$$tmp" != "" ] ; then mv $$tmp obj; else exit 0; fi

#define shadowModule(mod,src,opt)mod:  src.c				@@\
	compileShadow($^,src.o,mod,$(CC) $(CFLAGS) -c opt)		@@\

#define shadowCtrlModule(mod,src,opt)mod:  src.c			@@\
	compileShadow($^,src.o,mod,$(RCC) $(CFLAGS) -c CTRL opt)	@@\

#define asmCppModule(mod,deps)mod.o:	mod.c				@@\
mod.o:  deps								@@\
	$(CC) -S $<							@@\
	/lib/cpp -I. mod.s > _mod.tmp					@@\
	as _mod.tmp -o mod.o						@@\
	$(RMCMD) mod.s _mod.tmp						@@\

#define notSupported(targ)targ:						@@\
	echo $@ is not supported in this version of the system		@@\

#define makeClean(files)clean::						@@\
	$(RMCMD) $(JUNKFILES) *.o *.to *.tO				@@\
	$(RMCMD) files							@@\

#if 0
#define makeDepend()depend::						@@\
	cp Makefile Makefile.bak					@@\
	/bin/ls *.c | sed -e 's/.c$$/.s/' | xargs rm -f			@@\
	( echo '/^# XXX line used by make depend/,$$c' ; \		@@\
	  echo '# XXX line used by make depend - DO NOT DELETE XXX' ; \	@@\
	  echo '' ; \							@@\
	MakeDependCmd -I$$RCCL/h $(C_INCS) $(TARGET_DEFS) *.[cs] | \	@@\
	  PackDependCmd ; \ 						@@\
	  echo . ; \							@@\
	  echo w ; \							@@\
	  echo q )  | ed - Makefile
#endif

#if VxWorksSupport && !VxWorksConfig
#define maintainVxwSubdir()						@@\
clean::  VXW/Makefile							@@\
	cd VXW; $(MAKE) $(MFLAGS) clean					@@\
									@@\
depend:: VXW/Makefile							@@\
	cd VXW; $(MAKE) $(MFLAGS) depend				@@\
									@@\
vxlibs: VXW/Makefile							@@\
	cd VXW; $(MAKE) $(MFLAGS) vxlibs				@@\
									@@\
vxbins: VXW/Makefile							@@\
	cd VXW; $(MAKE) $(MFLAGS) vxbins				@@\
									@@\
vxw:    VXW/Makefile							@@\
	cd VXW; $(MAKE) $(MFLAGS) vxw					@@\
									@@\
VXW/Makefile: Imakefile							@@\
	@ $(CheckForVXWDir)						@@\
	$(BDIR)/imake -I../conf -DRCCLRootDir=/**/RCCLRootDir \ 	@@\
		-DVxWorksConfig -s VXW/Makefile				@@\

#else
#define maintainVxwSubdir()						@@\
vpath %.h ..								@@\
vpath %.c ..								@@\
vpath %.s ..								@@\
vxw:		$(LDIR)/vxw.o						@@\
$(LDIR)/vxw.o:								@@\
		(cd $(LDIR); $(MAKE) $(MFLAGS) vxw)			@@\

#endif
