#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License, Version 1.0 only
# (the "License").  You may not use this file except in compliance
# with the License.
#
# You can obtain a copy of the license at COPYING
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at COPYING.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
# Copyright (c) 2013, Joyent, Inc.  All rights reserved.
#

MAJOR_VER =	0.8
VER =		node-v0.8.20

include ../Makefile.defs

NODE_ROOT =	$(DESTDIR)/usr/node/$(MAJOR_VER)
VERSIONJS =	$(NODE_ROOT)/node_modules/platform_node_version.js

CFLAGS +=	-Wno-unknown-pragmas

#
# Node's build system is super broken.  If we pass it LIBS at configure time,
# it ignores it.  If we pass it LIBS at make time, it completely replaces all
# libraries, including those the build system itself would normally add.
# So our only option is to add this to LDFLAGS, which has to be passed at
# build time... including for the install target, because the generated
# makefiles have missing dependencies and rebuild stuff when installing.
# This has to be the most complicated, baroque, pointlessly wrong way
# imaginable to build something with a few dozen source files.  The next time
# you are here reading this and trying to work around the next piece of
# Node build stupidity, just stop.  Rip all this out, write a simple makefile,
# and copy it in at unpack time like we do for bzip2.
#
LDFLAGS +=	-lumem

ifeq ($(STRAP),strap)
	LDFLAGS += $(SYSLIBDIRS:%=-R$(DESTDIR)/%)
endif

AUTOCONF_OPTS += \
	--with-dtrace \
	--openssl-use-sys \
	--openssl-libpath=$(DESTDIR)/lib \
	--openssl-includes=$(DESTDIR)/usr/include \
	--shared-openssl-libname=sunw_crypto,sunw_ssl \
	--shared-zlib \
	--shared-zlib-libpath=$(DESTDIR)/lib \
	--shared-zlib-includes=$(DESTDIR)/usr/include \
	--prefix=/usr/node/$(MAJOR_VER)

AUTOCONF_CFLAGS =	CFLAGS="$(CPPFLAGS) $(CFLAGS)"
AUTOCONF_LIBS =
AUTOCONF_ENV +=		CXXFLAGS="$(CPPFLAGS) $(CFLAGS)"

#
# Jump through hoops to get the locally-run build tools to build with the
# correct compiler and without referencing anything in the proto area.
# This is needed because the proto area may contain libraries that are not
# present or are of different versions on the build machine itself.  A
# patch to the python configure program is also required to make this work.
#
AUTOCONF_ENV +=		LDFLAGS.host="-Wl,-i"
AUTOCONF_ENV +=		CXX.host="$(BASE)/wrapper $(GXX.host)"
AUTOCONF_ENV +=		CC.host="$(BASE)/wrapper $(GCC.host)"
AUTOCONF_ENV +=		CXX_host="$(BASE)/wrapper $(GXX.host)"
AUTOCONF_ENV +=		CC_host="$(BASE)/wrapper $(GCC.host)"
AUTOCONF_ENV +=		LINK.host="$(BASE)/wrapper $(GXX.host)"

OVERRIDES +=	$(AUTOCONF_ENV)

AUTOCONF_OUT =	build/default/config.h

PATCHES =	Patches/*
CLEANFILES +=	genversionjs

all: all_autoconf

all_autoconf: wrapper

#
# We build a host tool against the installed header so that we can spew a
# JavaScript file containing the version of the installed node.js binary.
#
genversionjs: genversionjs.c install_autoconf
	$(GCC.host) -o $@ -include $(NODE_ROOT)/include/node/node_version.h $<

#
# Ready for another hack?  This wrapper "translates" target gcc options into
# ones suitable for running on the host during the build.  This is needed
# because node's build system doesn't have any way to pass different options
# for the host and taget, and tries to build host tools as part of the build.
# Hooray, node!
#
wrapper: wrapper.c
	$(GCC.host) -Wall -Wextra -Werror -O2 -o $@ $<

# - platform_node_version.js is autogenerated with the current node version
# - we move man pages as we want them in /usr/node/0.8/man
install: install_autoconf genversionjs
	mkdir -p $(NODE_ROOT)/node_modules
	./genversionjs > $(VERSIONJS)
	rm -rf $(NODE_ROOT)/man
	mv $(NODE_ROOT)/share/man $(NODE_ROOT)/

include ../Makefile.targ
