#!/usr/bin/make -f

include /usr/share/cdbs/1/rules/debhelper.mk

DEB_HOST_ARCH ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)
DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

GCC_VERSION   := $(shell ls -l /usr/bin/gcj | sed 's/.*-> gcj-\(.*\)/\1/')

LIBGCJ_EXT    := $(shell ls -l /usr/lib/$(DEB_HOST_MULTIARCH)/libgcj_bc.so.1 | sed 's/.*\.//')
LIBGCJ_SOVER  = $(LIBGCJ_EXT)
REQ_VERSION   = 4.4.6

ant_version = 1.7
ant_version =
GCJ = gcj-$(GCC_VERSION)
GIJ = gij-$(GCC_VERSION)
GCJDBTOOL = gcj-dbtool-$(GCC_VERSION)
GCJSUBDIR = /usr/lib/gcj-$(GCC_VERSION)-$(LIBGCJ_SOVER)

# work around ecj1 having fallen out of the gcc search path
PATH := $(CURDIR):$(PATH)
export PATH

with_native := yes
with_rebuild := yes
with_sourcebuild := yes

ifneq (,$(filter $(DEB_HOST_ARCH), arm armel))
  with_rebuild := no
endif

ifneq (,$(filter $(DEB_HOST_ARCH), arm))
  with_sourcebuild := no
endif

DEB_DH_GENCONTROL_ARGS = -- \
	'-Vpkg:gij=gcj-$(GCC_VERSION)-jre-headless (>= $(REQ_VERSION))' \
	'-Vpkg:libgcjjar=gcj-$(GCC_VERSION)-jre-lib (>= $(REQ_VERSION))'

JAR = eclipse-ecj.jar

default: build

eclipse_root = $(CURDIR)/../eclipse

build/ecj:: build/stamp-bytecode build/stamp-nativecode

build/stamp-bytecode:
ifneq (,$(findstring $(DEB_HOST_ARCH),arm))
	@echo 'ecj is not built anymore on this architecture.'
	@echo 'People insist on 100% compatible packages without providing any help.'
	@echo 'The porter lists are quiet on the topic of better gij/gcj support for'
	@echo 'these architectures.'
	false
endif
	rm -rf build/bin
	mkdir -p build/bin

ifeq ($(with_sourcebuild),yes)
	cp -r src/org.eclipse.jdt.core/org build/bin/
	cat gcc.properties \
	    >> build/bin/org/eclipse/jdt/internal/compiler/batch/messages.properties
	rm -rf build/bin/org/eclipse/jdt/internal/compiler/apt
	rm -rf build/bin/org/eclipse/jdt/internal/compiler/tool

	find build/bin -name '*.java' > build/sourcefiles
	split -l 25 build/sourcefiles ecj-sources.
	ls -l
	mv ecj-sources.* build/bin
	ls -l . build/bin

	ln -sf /usr/lib/gcc/ecj1 $(CURDIR)/ecj1
	set -e; \
	for list in $$(find build/bin -name 'ecj-sources.*'); do \
	    echo "building files in $$list ..."; \
	    echo $(GCJ) -d build/bin -C -g \
		-I/usr/share/ant$(ant_version)/lib/ant.jar \
		-Ibuild/bin \
		$$(cat $$list); \
	    $(GCJ) -v -d build/bin -C -g \
		-I/usr/share/ant$(ant_version)/lib/ant.jar \
		-Ibuild/bin \
		$$(cat $$list); \
	done

	find build/bin -name 'sources.list' -exec rm -f {} \;
	find build/bin -name '*.java' -exec rm -f {} \;
	find build/bin -name '*.html' -exec rm -f {} \;

	mkdir -p build/bootstrap
	fastjar -c -C build/bin . -f build/bootstrap/$(JAR)

  ifeq ($(with_rebuild),yes)
	: # rebuild the compiler
	rm -rf build/bin
	mkdir -p build/bin
	cp -r src/org.eclipse.jdt.core/org build/bin/
	cat gcc.properties \
	    >> build/bin/org/eclipse/jdt/internal/compiler/batch/messages.properties
	rm -rf build/bin/org/eclipse/jdt/internal/compiler/apt
	rm -rf build/bin/org/eclipse/jdt/internal/compiler/tool

	time $(GIJ) \
	    -classpath build/bootstrap/$(JAR):/usr/share/ant$(ant_version)/lib/ant.jar \
	    org.eclipse.jdt.internal.compiler.batch.Main \
	    -bootclasspath /usr/share/java/libgcj-$(GCC_VERSION).jar \
	    build/bin

	find build/bin -name '*.java' -exec rm -f {} \;
	find build/bin -name '*.html' -exec rm -f {} \;

	mkdir -p build/dist
	fastjar -c -C build/bin . -f build/dist/$(JAR)
  else
	mkdir -p build/dist
	cp -p build/bootstrap/$(JAR) build/dist/$(JAR)
  endif
else
	mkdir -p build/bin
	unzip -d build/bin -q /usr/share/java/eclipse-ecj.jar
	mkdir -p build/dist
	cp -p /usr/share/java/eclipse-ecj.jar build/dist/$(JAR)
endif
	mkdir -p build/exe
	cp build/dist/$(JAR) build/exe/ecj-standalone.jar
	zip -d build/exe/ecj-standalone.jar \
		'org/eclipse/jdt/core/JDTCompilerAdapter*'

	touch build/stamp-bytecode

build/stamp-nativecode: build/stamp-bytecode
	find build/dist -name '*.jar.*.jar' | xargs -r rm -f
ifeq ($(with_native),yes)
	: # $(JAR).so

	cd build/bin && find -name '*.rsc' -o -name '*.properties' -o -name '*.props' \
	    | fastjar -c -@ - -f $(CURDIR)/build/resources.jar
	$(GCJ) \
	    -c -O2 -g -fPIC -fjni -findirect-dispatch \
	    -o build/dist/resources.o build/resources.jar
	cp -p build/dist/resources.o build/exe/

	PYTHONPATH=$(CURDIR)/debian \
	RPATH=-Wl,-rpath,$(GCJSUBDIR) \
	  time python debian/aot-compile \
	    --gcj=$(GCJ) --dbtool=$(GCJDBTOOL) \
	    -L /usr/lib/gcj build/dist build/dist

	PYTHONPATH=$(CURDIR)/debian \
	RPATH=-Wl,-rpath,$(GCJSUBDIR) \
	  time python debian/aot-compile \
	    --gcj=$(GCJ) --dbtool=$(GCJDBTOOL) \
	    -L /usr/lib/gcj build/exe build/exe

  ifeq (0,1)
	: # ecj-gcj
	time $(GCJ) \
	    -O1 -g -Wl,-O1 -Wl,-rpath,$(GCJSUBDIR) -Wl,-z,relro \
	    --main=org.eclipse.jdt.internal.compiler.batch.Main \
	    -o build/dist/ecj-gcj build/exe/ecj-standalone.jar
	: # ecj1
	time $(GCJ) \
	    -O1 -g -Wl,-O1 -Wl,-rpath,$(GCJSUBDIR) -Wl,-z,relro \
	    --main=org.eclipse.jdt.internal.compiler.batch.GCCMain \
	    -o build/dist/ecj1 build/exe/ecj-standalone.jar
  endif
endif
	touch build/stamp-nativecode


install/libecj-java::
	mh_installpoms -plibecj-java
	mh_installjar -plibecj-java -l debian/poms/ecj.pom build/dist/$(JAR) \
		--usj-name=eclipse-ecj

install/ecj::
	mkdir -p debian/tmp/usr/bin
	sed 's/@ver@/$(GCC_VERSION)/g' debian/ecj.in \
	  > debian/tmp/usr/bin/ecj
	chmod 755 debian/tmp/usr/bin/ecj

install/libecj-java-gcj::
ifeq ($(with_native),yes)
	mkdir -p debian/tmp/usr/lib/gcj
	install -m 644 build/dist/$(JAR).so debian/tmp/usr/lib/gcj

	mkdir -p debian/tmp/usr/share/gcj/classmap.d
	install -m 644 build/dist/*.db \
		debian/tmp/usr/share/gcj/classmap.d/
endif

install/ecj1::
ifeq ($(with_native),yes)
	mkdir -p debian/ecj1/usr/lib/$(DEB_HOST_MULTIARCH)/gcc
	install -m 755 build/exe/ecj1 debian/ecj1/usr/lib/$(DEB_HOST_MULTIARCH)/gcc/
	dh_link -p ecj1 \
	  /usr/lib/$(DEB_HOST_MULTIARCH)/gcc/ecj1 /usr/lib/gcc/ecj1
endif

install/ecj-gcj::
ifeq ($(with_native),yes)
	mkdir -p debian/tmp/usr/bin
	install -m 755 build/exe/ecj-gcj debian/tmp/usr/bin/
endif
	mkdir -p debian/ecj-gcj/usr/bin

	mkdir -p debian/ecj-gcj/usr/share/man/man1
	ln -sf ecj.1.gz \
	  debian/ecj-gcj/usr/share/man/man1/ecj-gcj.1.gz

	mkdir -p debian/ecj-gcj/usr/share/lintian/overrides
	cp -p debian/ecj-gcj.overrides \
	  debian/ecj-gcj/usr/share/lintian/overrides/ecj-gcj

binary-predeb/ecj1::
#	sed -i 's/libgcj[1-9][^,]*//;s/,,/,/' debian/ecj1.substvars

binary-predeb/ecj-gcj::
#	sed -i 's/libgcj[1-9][^,]*//;s/,,/,/' debian/ecj-gcj.substvars

clean::
	rm -rf build
	rm -f debian/*.pyc ecj1
	rm -rf debian/.mh

RR=../cvs/R3_5_maintenance/
RR=../cvs/R3_5_1/
RR=R4_3_1/
tag = R4_3_1

MAVEN_REPO  := http://repo1.maven.org/maven2
PACKAGE := ecj

get-orig-pom:
	mkdir -p debian/poms
	wget  -U NoSuchBrowser/1.0 -O debian/poms/$(PACKAGE).pom \
        $(MAVEN_REPO)/org/eclipse/jdt/core/compiler/$(PACKAGE)/$(DEB_UPSTREAM_VERSION)/$(PACKAGE)-$(DEB_UPSTREAM_VERSION).pom

get-source:
	rm -rf org.eclipse.jdt.core
	#wget http://git.eclipse.org/c/jdt/eclipse.jdt.core.git/snapshot/R4_3_1.tar.gz
	#git clone -b R3_8_maintenance git://git.eclipse.org/gitroot/jdt/eclipse.jdt.core.git
	tar xf ../R4_3_1.tar.gz
	rm -rf tmp-src
	mkdir -p tmp-src/org.eclipse.jdt.core
	cp -p $(RR)org.eclipse.jdt.core/scripts/about.html \
		tmp-src/org.eclipse.jdt.core/
	cp -p $(RR)org.eclipse.jdt.core/scripts/build.xml \
		tmp-src/org.eclipse.jdt.core/
	tar -c -f - -C $(RR)org.eclipse.jdt.core/compiler org \
	    | tar -x -f - -C tmp-src/org.eclipse.jdt.core/

	tar -c -f - -C $(RR)org.eclipse.jdt.core/antadapter org META-INF \
	    | tar -x -f - -C tmp-src/org.eclipse.jdt.core/
	find tmp-src -name CheckDebugAttributes.java | xargs -r rm -f
	find tmp-src -name BuildJarIndex.java | xargs -r rm -f

	tar -c -f - -C $(RR)org.eclipse.jdt.core/batch org \
	    | tar -x -f - -C tmp-src/org.eclipse.jdt.core/

	mkdir -p tmp-src/org.eclipse.jdt.core/META-INF/services
#	cp -p $(RR)org.eclipse.jdt.core/scripts/META-INF/MANIFEST.MF \
#	    tmp-src/org.eclipse.jdt.core/META-INF/
	printf 'org.eclipse.jdt.internal.compiler.tool.EclipseCompiler #Eclipse compiler' \
	    > tmp-src/org.eclipse.jdt.core/META-INF/services/javax.tools.JavaCompiler

