# 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>
#
#
.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

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


# 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 

POBJ=	platform/PArray.o 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/Condition.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/ReturnStmt.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/Compiler.o compiler/Buffer.o compiler/Mapping.o compiler/VarPool.o \
	compiler/Function.o compiler/ExprStmt.o compiler/AtomExpr.o compiler/IncDecExpr.o \
	compiler/BinaryExpr.o compiler/UnaryExpr.o compiler/ValueExpr.o

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

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

all : bin nqh 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)

bin/mkdata : mkdata/mkdata.c bin
	$(LD) -o bin/mkdata mkdata/mkdata.c

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

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
#
# 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

#
# 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 and lexer for later use.  You shouldn't need
# to do this as part of the port.
#
default-snapshot: default compiler/parse.cpp compiler/lexer.cpp
	$(CP) compiler/parse.cpp compiler/parse.tab.h compiler/lexer.cpp default

default:
	$(MKDIR) default


