# Copyright 2012 Google Inc. All Rights Reserved.
# Author: yugui@google.com (Yugui Sonoda)

include Makefile
-include uncommon.mk

NACL_SDK_ROOT=@NACL_SDK_ROOT@
NACL_TOOLCHAIN=@NACL_TOOLCHAIN@
NACL_TOOLCHAIN_DIR=$(NACL_SDK_ROOT)/toolchain/$(NACL_TOOLCHAIN)
CXX=@CXX@

# Don't override CC/LD/etc if they are already set to absolute
# paths (this is the case when building in the naclports tree).
ifeq ($(dir $(CC)),./)
CC:=$(NACL_TOOLCHAIN_DIR)/bin/$(CC)
endif
ifeq ($(dir $(CXX)),./)
CXX:=$(NACL_TOOLCHAIN_DIR)/bin/$(CXX)
endif
ifeq ($(dir $(LD)),./)
LD:=$(NACL_TOOLCHAIN_DIR)/bin/$(LD)
endif
ifeq ($(dir $(NM)),./)
NM:=$(NACL_TOOLCHAIN_DIR)/bin/$(NM)
endif
ifeq ($(dir $(AR)),./)
AR:=$(NACL_TOOLCHAIN_DIR)/bin/$(AR)
endif
ifeq ($(dir $(AS)),./)
AS:=$(NACL_TOOLCHAIN_DIR)/bin/$(AS)
endif
ifeq ($(dir $(RANLIB)),./)
RANLIB:=$(NACL_TOOLCHAIN_DIR)/bin/$(RANLIB)
endif
ifeq ($(dir $(OBJDUMP)),./)
OBJDUMP:=$(NACL_TOOLCHAIN_DIR)/bin/$(OBJDUMP)
endif
ifeq ($(dir $(OBJCOPY)),./)
OBJCOPY:=$(NACL_TOOLCHAIN_DIR)/bin/$(OBJCOPY)
endif
PYTHON=@PYTHON@

PPROGRAM=pepper-$(PROGRAM)
PEPPER_LIBS=-lppapi -lnacl_io
PROGRAM_NMF=$(PROGRAM:$(EXEEXT)=.nmf)
PPROGRAM_NMF=$(PPROGRAM:$(EXEEXT)=.nmf)

GNUmakefile: $(srcdir)/nacl/GNUmakefile.in
$(PPROGRAM): $(PROGRAM) pepper_main.$(OBJEXT)
	$(Q)$(MAKE) $(MFLAGS) PROGRAM=$(PPROGRAM) MAINOBJ="pepper_main.$(OBJEXT)" LIBS="$(LIBS) $(PEPPER_LIBS)" CC="$(CXX)" program
$(PROGRAM_NMF) $(PPROGRAM_NMF): $(@:.nmf=$(EXEEXT)) nacl/create_nmf.rb

.PHONY: pprogram package show_naclflags
.SUFFIXES: $(EXEEXT) .nmf
$(EXEEXT).nmf:
	$(ECHO) generating manifest $@
	$(Q)$(MINIRUBY) $(srcdir)/nacl/create_nmf.rb --verbose=$(V) $(@:.nmf=$(EXEEXT)) $@

pepper_main.$(OBJEXT): $(srcdir)/nacl/pepper_main.c
	@$(ECHO) compiling nacl/pepper_main.c
	$(Q) $(CC) $(CFLAGS) $(XCFLAGS) $(CPPFLAGS) $(COUTFLAG)$@ -c $(srcdir)/nacl/pepper_main.c

.rbconfig.time:
	@$(MAKE) .rbconfig.raw.time RBCONFIG=.rbconfig.raw.time
	@sed \
	  -e 's!CONFIG\["CC"\] = .*!CONFIG\["CC"\] = "$(CC)"!' \
	  -e 's!CONFIG\["LD"\] = .*!CONFIG\["LD"\] = "$(LD)"!' \
	  -e 's!CONFIG\["NM"\] = .*!CONFIG\["NM"\] = "$(NM)"!' \
	  -e 's!CONFIG\["AR"\] = .*!CONFIG\["AR"\] = "$(AR)"!' \
	  -e 's!CONFIG\["AS"\] = .*!CONFIG\["AS"\] = "$(AS)"!' \
	  -e 's!CONFIG\["RANLIB"\] = .*!CONFIG\["RANLIB"\] = "$(RANLIB)"!' \
	  -e 's!CONFIG\["OBJDUMP"\] = .*!CONFIG\["OBJDUMP"\] = "$(OBJDUMP)"!' \
	  -e 's!CONFIG\["OBJCOPY"\] = .*!CONFIG\["OBJCOPY"\] = "$(OBJCOPY)"!' \
	-i.bak rbconfig.rb
	@touch .rbconfig.time

all: pprogram
main: $(PROGRAM_NMF)
pprogram: showflags $(PPROGRAM) $(PPROGRAM_NMF)
program: $(PROGRAM_NMF)
prog: pprogram

package: pprogram install-lib install-ext-comm install-ext-arch
	$(INSTALL_DATA) $(srcdir)/nacl/example.html $(prefix)
	$(ECHO) generating manifest $@
	$(Q)$(MINIRUBY) $(srcdir)/nacl/package.rb $(prefix)

showflags: show_naclflags

show_naclflags:
	@echo "        NACL_SDK_ROOT = $(NACL_SDK_ROOT)"
	@echo "        NM = $(NM)"
	@echo "        AR = $(AR)"
	@echo "        AS = $(AS)"
	@echo "        RANLIB = $(RANLIB)"
	@echo "        OBJDUMP = $(OBJDUMP)"
	@echo "        OBJCOPY = $(OBJCOPY)"

clean-local::
	-$(RM) $(PPROGRAM) pepper_main.$(OBJEXT) $(PROGRAM_NMF) $(PPRGORAM_NMF)
