# -*- Makefile -*-
#*****************************************************************************
#
# DESCRIPTION: Verilator: Makefile for verilog source
#
# Code available from: http://www.veripool.org/verilator
#
#*****************************************************************************
#
# Copyright 2003-2014 by Wilson Snyder.  This program is free software; you can
# redistribute it and/or modify it under the terms of either the GNU
# Lesser General Public License Version 3 or the Perl Artistic License
# Version 2.0.
#
# Verilator is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
#****************************************************************************/

#### Start of system configuration section. ####

srcdir = @srcdir@
VPATH = @srcdir@
PERL = @PERL@
EXEEXT = @EXEEXT@

#### End of system configuration section. ####


default: dbg opt
debug: dbg
optimize: opt

ifneq ($(words $(CURDIR)),1)
 $(error Unsupported: GNU Make cannot build in directories containing spaces, build elsewhere: '$(CURDIR)')
endif

UNDER_GIT = $(wildcard ${srcdir}/../.git/logs/HEAD)

#*********************************************************************

obj_opt:
	mkdir $@
obj_dbg:
	mkdir $@

.PHONY: ../verilator_bin ../verilator_bin_dbg

opt: ../verilator_bin
ifeq ($(VERILATOR_NO_OPT_BUILD),1)	# Faster laptop development... One build
../verilator_bin: ../verilator_bin_dbg
	-rm -rf $@ $@.exe
	-cp -p $<$(EXEEXT) $@$(EXEEXT)
else
../verilator_bin: obj_opt prefiles
	cd obj_opt && $(MAKE) -j 1  TGT=../$@ -f ../Makefile_obj serial
	cd obj_opt && $(MAKE)       TGT=../$@ -f ../Makefile_obj
endif

dbg: ../verilator_bin_dbg
../verilator_bin_dbg: obj_dbg prefiles
	cd obj_dbg && $(MAKE) -j 1  TGT=../$@ VL_DEBUG=1 -f ../Makefile_obj serial
	cd obj_dbg && $(MAKE)       TGT=../$@ VL_DEBUG=1 -f ../Makefile_obj

prefiles::
prefiles:: config_rev.h
ifneq ($(UNDER_GIT),)	# If local git tree... Else don't burden users
# This output goes into srcdir if locally configured, as we need to distribute it as part of the kit.
config_rev.h:	${srcdir}/config_rev.pl ${srcdir}/../.git/logs/HEAD
	$(PERL) ${srcdir}/config_rev.pl ${srcdir} >$@
else
config_rev.h:	${srcdir}/config_rev.pl
	$(PERL) ${srcdir}/config_rev.pl ${srcdir} >$@
endif

maintainer-copy::
clean mostlyclean distclean maintainer-clean::
	-rm -rf obj_* *.log *.dmp *.vpd core
	-rm -f *.o *.d perlxsi.c *_gen_*
	-rm -f *__gen*
	-rm -f .objcache*

distclean maintainer-clean::
	-rm -f Makefile Makefile_obj config_build.h

maintainer-clean::
	-rm -f config_rev.h
