# 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)
CC:=$(NACL_TOOLCHAIN_DIR)/bin/$(CC)
LD:=$(NACL_TOOLCHAIN_DIR)/bin/$(LD)
NM:=$(NACL_TOOLCHAIN_DIR)/bin/$(NM)
AR:=$(NACL_TOOLCHAIN_DIR)/bin/$(AR)
AS:=$(NACL_TOOLCHAIN_DIR)/bin/$(AS)
RANLIB:=$(NACL_TOOLCHAIN_DIR)/bin/$(RANLIB)
OBJDUMP:=$(NACL_TOOLCHAIN_DIR)/bin/$(OBJDUMP)
OBJCOPY:=$(NACL_TOOLCHAIN_DIR)/bin/$(OBJCOPY)
PYTHON=@PYTHON@

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

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

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

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
ruby.$(OBJEXT):
	@$(ECHO) compiling $<
	$(Q) $(CC) $(CFLAGS) $(XCFLAGS) $(CPPFLAGS) $(COUTFLAG)$@.tmp -c $<
	$(Q) $(OBJCOPY) --weaken-symbol=rb_load_file $@.tmp $@
	@-$(RM) $@.tmp
file.$(OBJEXT):
	@$(ECHO) compiling $<
	$(Q) $(CC) $(CFLAGS) $(XCFLAGS) $(CPPFLAGS) $(COUTFLAG)$@.tmp -c $<
	$(Q) $(OBJCOPY) --weaken-symbol=rb_file_load_ok $@.tmp $@
	@-$(RM) $@.tmp

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