# The contents of this file are subject to the Mozilla Public License Version
# 1.1 (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 Original Code is the Python XPCOM language bindings.
#
# The Initial Developer of the Original Code is ActiveState Tool Corp.
# Portions created by ActiveState Tool Corp. are Copyright (C) 2000, 2001
# ActiveState Tool Corp.  All Rights Reserved.
#
# Contributor(s): Trent Mick <TrentM@ActiveState.com> (original author)
#                 Mark Hammond <MarkH@ActiveState.com>
#

# Dumb makefile to build PyXPCOM on linux
#
# The build is by no means clean. I just kept shoving in compiler and linker
# options until it worked. :)
#
# USAGE:
#	1. edit top section as appropriate
#	2. build, install, and test PyXPCOM
#       > make -f makefile.stupid.linux DEBUG=1
#       > make -f makefile.stupid.linux DEBUG=1 install
#       > make -f makefile.stupid.linux DEBUG=1 test
#      A debug build is suggested (DEBUG=1) by default because a default
#      mozilla build is a debug build. Exclude the "DEBUG=1" to build for a
#      release build of mozilla.
#

#-------------------------
# You must edit the variables in this section as appropriate for your machine
# The most common edits will just be:
#    MOZ_SRC, INSTALLDIR, and PYTHON_SRC
# =========== START OF SECTION FOR COMMON EDITS =====================

# this will have an "xpcom" subdir on install
INSTALLDIR=/usr/local/ActivePython-2.0/lib/python2.0/site-packages

PYTHON_SRC=/usr/local/ActivePython-2.0

# =========== START OF SECTION FOR COMMON EDITS =====================
# We expect a "mozilla" directory under this
MOZ_SRC=../../../..

MOZCOMPONENTSDIR=$(MOZ_SRC)/mozilla/dist/bin/components
MOZINCLUDES=-I$(MOZ_SRC)/mozilla/dist/include -I$(MOZ_SRC)/mozilla/dist/include/nspr -I$(MOZ_SRC)/mozilla/dist/include/string -I$(MOZ_SRC)/mozilla/dist/include/xpcom
MOZLIBDIR=$(MOZ_SRC)/mozilla/dist/lib

# this is setup to use the *installed* Python directory structure
#  - To use the development Python dir structure some changes are
#    necessary here *and* below (below, because there two lib dir
#    to include in LDFLAGS for the dev dir structure)
PYTHON_SRC=/usr/local/ActivePython-2.0
PYTHONINCLUDES=-I$(PYTHON_SRC)/include/python2.0
PYTHONLIBDIR=$(PYTHON_SRC)/lib/python2.0/config

XPIDL=$(MOZ_SRC)/mozilla/dist/bin/xpidl
IDLINCLUDES=-I$(MOZ_SRC)/mozilla/dist/idl


#-------------------------
# You should not need to edit anything beyond this point.
#

# the main PyXPCOM engine library
ENGINE=src/_xpcommodule.so
# the PyXPCOM loader
LOADER=src/loader/libpyloader.so

all:: $(ENGINE) $(LOADER)


#---- build the PyXPCOM loader

LOADER_CFLAGS=-fpic -fno-rtti -fno-exceptions -Wconversion \
	-Wpointer-arith -Wbad-function-cast -Wcast-align -Woverloaded-virtual \
	-Wsynth -pedantic -Wno-long-long -pthread -DTRIMMED
ifdef DEBUG
LOADER_CFLAGS += -DDEBUG
endif

# NOTE: not sure if using -rpath is the best way to do this
LOADER_LDFLAGS=-Xlinker -rpath -Xlinker $(MOZLIBDIR) \
	-Xlinker -rpath -Xlinker $(MOZLIBDIR)/components \
	-Xlinker -rpath -Xlinker $(PYTHONLIBDIR) \
	-L$(MOZLIBDIR) -L$(PYTHONLIBDIR)
# NOTE: can't remember if "-shared" is necessary here
LOADER_LIBS=-lpython2.0 -ldl -lpthread -lutil -shared -lxpcom
$(LOADER): src/loader/pyloader.cpp
	c++ $(LOADER_CFLAGS) $(MOZINCLUDES) $(PYTHONINCLUDES) \
		-c src/loader/pyloader.cpp \
		-o src/loader/pyloader.o
	c++ $(LOADER_CFLAGS) $(LOADER_LDFLAGS) $(LOADER_LIBS) \
		-o $(LOADER) \
		src/loader/pyloader.o


#---- build the PyXPCOM engine

ENGINE_CFLAGS=-fpic -fno-rtti -fno-exceptions -Wconversion \
	-Wpointer-arith -Wbad-function-cast -Wcast-align -Woverloaded-virtual \
	-Wsynth -pedantic -Wno-long-long -pthread -DTRIMMED -nostdlib
ifdef DEBUG
ENGINE_CFLAGS += -DDEBUG
endif

#	-DXPCOM_EXPORTS
XPCOM_SRC_OBJECTS = \
	src/ErrorUtils.o \
	src/PyGBase.o \
	src/PyGModule.o \
	src/PyGStub.o \
	src/PyGInputStream.o \
	src/PyGWeakReference.o \
	src/PyIClassInfo.o \
	src/PyIComponentManager.o \
	src/PyIInputStream.o \
	src/PyIEnumerator.o \
	src/PyIID.o \
	src/PyIInterfaceInfo.o \
	src/PyIInterfaceInfoManager.o \
	src/PyISimpleEnumerator.o \
	src/PyISupports.o \
	src/Pyxpt_info.o \
	src/TypeObject.o \
	src/VariantUtils.o \
	src/dllmain.o \
	src/xpcom.o

%.o: %.cpp
	c++ $(ENGINE_CFLAGS) $(MOZINCLUDES) $(PYTHONINCLUDES) \
		-c $< -o $@

# NOTE: not sure if using -rpath is the best way to do this
# NOTE: can't remember if "-shared" is necessary here
ENGINE_LDFLAGS=-Xlinker -rpath -Xlinker $(MOZLIBDIR) \
	-Xlinker -rpath -Xlinker $(MOZLIBDIR)/components \
	-Xlinker -rpath -Xlinker $(PYTHONLIBDIR) \
	-Xlinker -shared
ENGINE_LIBS=-lpython2.0 -ldl -lpthread -lutil -lxpcom \
	-lnspr4 -lgtk -lgdk -rdynamic -lgmodule -lglib -lm -lplc4 \
	-lpthread -lnsl -lresolv -lm -lc -lgcc

$(ENGINE): $(XPCOM_SRC_OBJECTS)
	c++ $(ENGINE_CFLAGS) $(ENGINE_LDFLAGS) -o $(ENGINE) \
		$(XPCOM_SRC_OBJECTS) -L$(MOZLIBDIR) -L$(PYTHONLIBDIR) $(ENGINE_LIBS)


#---- install PyXPCOM

XPCOM_PACKAGE_FILES = \
	__init__.py \
	components.py \
	file.py \
	nsError.py \
	register.py \
	xpcom_consts.py \
	xpt.py \
	client/__init__.py \
	server/__init__.py \
	server/enumerator.py \
	server/factory.py \
	server/loader.py \
	server/module.py \
	server/policy.py

# this is a cheasy install
#	- no attention to permissions
#	- doesn't explicitly use $(XPCOM_PACKAGE_FILES)
install:: all $(XPCOM_PACKAGE_FILES)
	mkdir -p $(INSTALLDIR)/xpcom/client
	mkdir -p $(INSTALLDIR)/xpcom/server
	cp -f *.py $(INSTALLDIR)/xpcom
	cp -f client/*.py $(INSTALLDIR)/xpcom/client
	cp -f server/*.py $(INSTALLDIR)/xpcom/server
	cp -f $(ENGINE) $(INSTALLDIR)/xpcom
	mkdir -p $(MOZCOMPONENTSDIR)
	cp -f $(LOADER) $(MOZCOMPONENTSDIR)
	( \
		export PYTHONPATH=$(INSTALLDIR):$(PYTHONPATH) ; \
		export MOZILLA_FIVE_HOME=$(MOZ_SRC)/mozilla/dist/bin ; \
		export LD_LIBRARY_PATH=$(MOZ_SRC)/mozilla/dist/bin ; \
		$(MOZ_SRC)/mozilla/dist/bin/regxpcom \
	)


#---- build and run the PyXPCOM test suite

all:: test/test_component/py_test_component.xpt

test/test_component/py_test_component.xpt: test/test_component/py_test_component.idl
	$(XPIDL) -m typelib -w $(IDLINCLUDES) -o test/test_component/py_test_component $<

install::
	mkdir -p $(MOZCOMPONENTSDIR)
	cp -f test/test_component/py_test_component.xpt $(MOZCOMPONENTSDIR)
	cp -f test/test_component/py_test_component.py $(MOZCOMPONENTSDIR)
	( \
		export PYTHONPATH=$(INSTALLDIR):$(PYTHONPATH) ; \
		export MOZILLA_FIVE_HOME=$(MOZ_SRC)/mozilla/dist/bin ; \
		export LD_LIBRARY_PATH=$(MOZ_SRC)/mozilla/dist/bin ; \
		$(MOZ_SRC)/mozilla/dist/bin/regxpcom \
	)

test:: install
	( \
		export PYTHONPATH=$(INSTALLDIR):$(PYTHONPATH) ; \
		export MOZILLA_FIVE_HOME=$(MOZ_SRC)/mozilla/dist/bin ; \
		export LD_LIBRARY_PATH=$(MOZ_SRC)/mozilla/dist/bin ; \
		python test/regrtest.py \
	)

runpython: install
	( \
		export PYTHONPATH=$(INSTALLDIR):$(PYTHONPATH) ; \
		export MOZILLA_FIVE_HOME=$(MOZ_SRC)/mozilla/dist/bin ; \
		export LD_LIBRARY_PATH=$(MOZ_SRC)/mozilla/dist/bin ; \
		python \
	)

clean:
	find . -name "*~" | xargs rm -f
	find . -name "*.o" | xargs rm -f
	find . -name "*.pyc" | xargs rm -f
	find . -name "*.so" | xargs rm -f

