# Makefile for nqc
#
# The contents of this file are subject to the Mozilla Public License
# Version 1.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.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS IS"
# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
# License for the specific language governing rights and limitations
# under the License.
#
# The Initial Developer of this code is David Baum.
# Portions created by David Baum are Copyright (C) 1999 David Baum.
# All Rights Reserved.
#
#   original author:
#		Dave Baum <dbaum@enteract.com>
#
#   other contributors:
#		Rodd Zurcher <rbz@enteract.com>
#		Patrick Nadeau <pnadeau@wave.home.com>
#		Jonathan W. Miner <jminer@fcmail.com>
#
#
.SUFFIXES: .cpp .c

#
# Pick your C++ compiler. 
#
CC=g++
#CC=gcc

#
# Pick your YACC processor
#
YACC = bison -y
# YACC = yacc

#
# Define the FLEX processor
#    note - lex will not work
#
FLEX = flex

# Link in any necessary C++ libraries
#
# LIBS = -lstdc++

#
# Use this to define the default device driver name
# for serial port connections.
#
DEFAULT_SERIAL_NAME = "/dev/ttyS0"

# installation information
BINDIR=/usr/local/bin
MANDIR=/usr/local/man/man1
MANEXT=1


# other commands
LD=$(CC)
CP=cp -f
MKDIR=mkdir
MV=mv -f
RM=rm -f

IFLAGS=-Iplatform -Ircxlib -Inqc -Icompiler
WFLAGS=-Wall -Wstrict-prototypes -Wmissing-prototypes
CFLAGS = -O6 -pipe $(IFLAGS) $(WFLAGS) -DDEFAULT_SERIAL_NAME='$(DEFAULT_SERIAL_NAME)'

OBJ = $(NQCOBJ) $(COBJ) $(RCXOBJ) $(POBJ)

RCXOBJ= rcxlib/RCX_Cmd.o rcxlib/RCX_Disasm.o rcxlib/RCX_Image.o \
	rcxlib/RCX_Link.o rcxlib/RCX_Log.o rcxlib/RCX_Target.o

POBJ= platform/PStream.o platform/PSerial_unix.o \
	platform/PHashTable.o platform/PListS.o

COBJ=	compiler/AsmStmt.o compiler/AssignStmt.o compiler/BlockStmt.o compiler/Bytecode.o \
	compiler/Conditional.o compiler/CondParser.o compiler/DoStmt.o \
	compiler/Expansion.o compiler/Fragment.o compiler/IfStmt.o compiler/JumpStmt.o \
	compiler/lexer.o compiler/Macro.o compiler/parse.o \
	compiler/PreProc.o compiler/Program.o compiler/RepeatStmt.o compiler/AssignMathStmt.o \
	compiler/Stmt.o compiler/Symbol.o compiler/TaskStmt.o compiler/WhileStmt.o compiler/Error.o \
	compiler/AutoFree.o compiler/parse_util.o compiler/Expr.o \
	compiler/GosubStmt.o compiler/Scope.o compiler/InlineStmt.o compiler/CallStmt.o \
	compiler/LabeledStmt.o compiler/SwitchStmt.o compiler/MonitorStmt.o compiler/AcquireStmt.o \
	compiler/DeclareStmt.o compiler/ScopeStmt.o compiler/ForStmt.o compiler/CatchStmt.o\
	compiler/Compiler.o compiler/Buffer.o compiler/Mapping.o \
	compiler/FunctionDef.o compiler/ExprStmt.o compiler/AtomExpr.o compiler/IncDecExpr.o \
	compiler/BinaryExpr.o compiler/UnaryExpr.o compiler/ValueExpr.o compiler/TypeExpr.o \
	compiler/UnaryBaseExpr.o compiler/ModExpr.o compiler/BinaryBaseExpr.o compiler/EventSrcExpr.o \
	compiler/ArrayExpr.o compiler/TaskIdExpr.o compiler/RelExpr.o compiler/LogicalExpr.o \
	compiler/NegateExpr.o compiler/IndirectExpr.o \
	compiler/VarAllocator.o compiler/VarTranslator.o 

NQCOBJ = nqc/nqc.o nqc/SRecord.o nqc/DirList.o nqc/CmdLine.o

# We need a 'bin' directory because the names of the binaries clash
# with existing directory names.

all : bin nqh nub bin/nqc

# Create the bin directory in the Makefile because it is not part
# of the original distribution.  This prevents the need to tell the user
# to do it.

bin:
	$(MKDIR) bin

bin/nqc : compiler/parse.cpp $(OBJ)
	$(LD) -o $@ $(OBJ) $(LIBS)

bin/mkdata : mkdata/mkdata.cpp nqc/SRecord.cpp
	$(LD) -o bin/mkdata -Inqc/ -Iplatform/ mkdata/mkdata.cpp nqc/SRecord.cpp

#
# clean up stuff
#
clean: clean-parser clean-lexer clean-obj clean-nqh clean-nub

clean-obj:
	$(RM) bin/*
	$(RM) */*.o


clean-parser:
	$(RM) compiler/parse.cpp compiler/parse.tab.h

clean-lexer:
	$(RM) compiler/lexer.cpp

clean-nqh:
	$(RM) compiler/rcx1_nqh.h compiler/rcx2_nqh.h

clean-nub:
	$(RM) rcxlib/rcxnub.nqh

#
# create the parser files (parse.cpp and parse.tab.h)
#
compiler/parse.cpp: compiler/parse.y
	(cd compiler ; $(YACC) -d parse.y ; \
	$(MV) y.tab.c parse.cpp ; $(MV) y.tab.h parse.tab.h )

#
# create the lexer file (lexer.cpp)
#
compiler/lexer.cpp: compiler/lex.l
	(cd compiler ; $(FLEX) lex.l )

#
# NQH files
#
nqh : compiler/rcx1_nqh.h compiler/rcx2_nqh.h

compiler/rcx1_nqh.h: compiler/rcx1.nqh bin/mkdata
	bin/mkdata compiler/rcx1.nqh compiler/rcx1_nqh.h rcx1_nqh

compiler/rcx2_nqh.h: compiler/rcx2.nqh bin/mkdata
	bin/mkdata compiler/rcx2.nqh compiler/rcx2_nqh.h rcx2_nqh

#
# rcxnub.h
#
nub: rcxlib/rcxnub.h

rcxlib/rcxnub.h: rcxlib/fastdl.srec bin/mkdata
	bin/mkdata -s rcxlib/fastdl.srec rcxlib/rcxnub.h rcxnub

#
# general rule for compiling
#
.cpp.o:
	$(CC) -c $(CFLAGS) $< -o $*.o 


#
# Use these targets to use the default parser/lexer files.  This is handy if
# your system does not have a suitable flex and/or yacc tool.
#
default-parser:
	$(CP) default/parse.cpp default/parse.tab.h nqcc

default-lexer:
	$(CP) default/lexer.cpp nqcc


#
# This is used to create a default parser, lexer, and nqh files for later use. 
# You shouldn't need to do this as part of a port.
#
DEF_FILES = compiler/parse.cpp compiler/lexer.cpp compiler/rcx1_nqh.h compiler/rcx2_nqh.h
default-snapshot: default $(DEF_FILES)
	$(CP) $(DEF_FILES) compiler/parse.tab.h default

default:
	$(MKDIR) default

#
# Installation of binary and man page
#
install: all
	test -d $(BINDIR) || mkdir -p $(BINDIR)
	cp -r bin/* $(BINDIR)
	test -d $(MANDIR)  || mkdir -p $(MANDIR)
	cp nqc-man-2.1r1-0.man $(MANDIR)/nqc.$(MANEXT)
