From d6124948cc690bb2285b1904b485505fe4f07916 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Adjemian=20=28Charybdis=29?=
 <stepan@dynare.org>
Date: Tue, 10 Sep 2019 15:20:35 +0200
Subject: [PATCH] Added rules to build x13 binaries and produce a zip archive
 of the dseries class.

---
 .gitignore                 |   5 ++
 Makefile                   | 119 +++++++++++++++++++++++++++++++++++++
 patches/Makefile.osx.patch |  11 ++++
 patches/Makefile.patch     |  47 +++++++++++++++
 4 files changed, 182 insertions(+)
 create mode 100644 patches/Makefile.osx.patch
 create mode 100644 patches/Makefile.patch

diff --git a/.gitignore b/.gitignore
index e02dd85..809e979 100644
--- a/.gitignore
+++ b/.gitignore
@@ -6,3 +6,8 @@ m-unit-tests/*
 
 src/git.info
 src/git.last-commit-hash
+
+*.zip
+
+binaries/*
+osxcross/*
diff --git a/Makefile b/Makefile
index 0fb0d14..0fb4219 100644
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,14 @@
 OCTAVE=octave-cli
 MATLAB=`which matlab`
 
+ROOT_PATH := ${CURDIR}
+OSX_SDK_VERSION=10.11
+OSXCROSS_VERSION=d39ba022313f2d5a1f5d02caaa1efb23d07a559b
+SRC_REMOTE_ADDRESS=https://www.census.gov/ts/x13as/unix/
+SRC_REMOTE_FILE=x13assrc_V1.1_B39.tar.gz
+
+.PHONY: all osxcross check-octave check-matlab build windows linux osx clean-sources clean-binaries
+
 all: check-octave check-matlab
 
 m-unit-tests/src/mtest.m:
@@ -17,3 +25,114 @@ check-clean:
 	rm -f tests/*_test_*.m tests/*.csv tests/*.xls tests/*.xlsx tests/*.mat tests/failed tests/datafile_for_test
 	rm -f git.info git.last-commit-hash tests/pass tests/failed tests/*.spc
 	rm -rf m-unit-tests
+
+build: osxcross windows osx linux
+	rm -rf x13src
+
+windows: binaries/windows/64/x13as.exe binaries/windows/32/x13as.exe
+
+osx:
+	mv osxcross/target/x86_64-apple-darwin15/lib/libquadmath.dylib osxcross/target/x86_64-apple-darwin15/lib/libquadmath.dylib.cache
+	mv osxcross/target/x86_64-apple-darwin15/lib/libquadmath.0.dylib osxcross/target/x86_64-apple-darwin15/lib/libquadmath.0.dylib.cache
+	$(MAKE) binaries/osx/64/x13as
+	$(MAKE) binaries/osx/32/x13as
+	mv osxcross/target/x86_64-apple-darwin15/lib/libquadmath.dylib.cache osxcross/target/x86_64-apple-darwin15/lib/libquadmath.dylib
+	mv osxcross/target/x86_64-apple-darwin15/lib/libquadmath.0.dylib.cache osxcross/target/x86_64-apple-darwin15/lib/libquadmath.0.dylib
+
+linux: binaries/linux/64/x13as binaries/linux/32/x13as
+
+osxcross: osxcross/target/bin/x86_64-apple-darwin15-gfortran
+
+osxcross/target/bin/x86_64-apple-darwin15-gfortran: osxcross/README.md osxcross/tarballs/MacOSX$(OSX_SDK_VERSION).sdk.tar.xz
+	cd osxcross; UNATTENDED=1 ./build.sh
+	cd osxcross; GCC_VERSION=9.2.0 ENABLE_FORTRAN=1 ./build_gcc.sh
+
+osxcross/README.md: $(OSXCROSS_VERSION).zip
+	unzip $(OSXCROSS_VERSION).zip
+	mkdir -p osxcross
+	mv osxcross-$(OSXCROSS_VERSION)/* osxcross
+	rm -rf osxcross-$(OSXCROSS_VERSION)
+	touch osxcross/README.md
+
+osxcross/tarballs/MacOSX$(OSX_SDK_VERSION).sdk.tar.xz:
+	wget https://dynare.adjemian.eu/osx/$(OSX_SDK_VERSION)/sdk.tar.xz -O sdk.tar.xz
+	mkdir -p osxcross/tarballs/MacOSX
+	mv sdk.tar.xz $@
+
+$(OSXCROSS_VERSION).zip:
+	wget  https://github.com/tpoechtrager/osxcross/archive/$(OSXCROSS_VERSION).zip
+
+binaries/linux/32/x13as: x13src/Makefile
+	mkdir -p tmp/linux/32
+	cp x13src/* tmp/linux/32
+	cd tmp/linux/32; make -j4 FC=gfortran LINKER=gfortran FFLAGS="-O2 -m32" LDFLAGS="-s -m32" PROGRAM=x13as
+	mkdir -p binaries/linux/32
+	mv tmp/linux/32/x13as binaries/linux/32/x13as
+	rm -rf tmp/linux/32
+
+binaries/linux/64/x13as: x13src/Makefile
+	mkdir -p tmp/linux/64
+	cp x13src/* tmp/linux/64
+	cd tmp/linux/64; make -j4 FC=gfortran LINKER=gfortran FFLAGS="-O2 -m64" PROGRAM=x13as
+	mkdir -p binaries/linux/64
+	mv tmp/linux/64/x13as binaries/linux/64/x13as
+	rm -rf tmp/linux/64
+
+binaries/windows/32/x13as.exe: x13src/Makefile
+	mkdir -p tmp/windows/32
+	cp x13src/* tmp/windows/32
+	cd tmp/windows/32;  make FC=i686-w64-mingw32-gfortran LINKER=i686-w64-mingw32-gfortran FFLAGS="-O2" PROGRAM=x13as.exe
+	mkdir -p binaries/windows/32
+	mv tmp/windows/32/x13as.exe binaries/windows/32/x13as.exe
+	rm -rf tmp/windows/32
+
+binaries/windows/64/x13as.exe: x13src/Makefile
+	mkdir -p tmp/windows/64
+	cp x13src/* tmp/windows/64
+	cd tmp/windows/64;  make FC=x86_64-w64-mingw32-gfortran LINKER=x86_64-w64-mingw32-gfortran FFLAGS="-O2" PROGRAM=x13as.exe
+	mkdir -p binaries/windows/64
+	mv tmp/windows/64/x13as.exe binaries/windows/64/x13as.exe
+	rm -rf tmp/windows/64
+
+binaries/osx/64/x13as: x13src/Makefile
+	mkdir -p tmp/osx/64
+	cp x13src/* tmp/osx/64
+	cd tmp/osx/64; patch Makefile < ../../../patches/Makefile.osx.patch; export PATH=$(ROOT_PATH)/osxcross/target/bin:$(PATH);  make FC=x86_64-apple-darwin15-gfortran  FFLAGS="-m64" LINKER=x86_64-apple-darwin15-gfortran LDFLAGS="-static-libgcc -static-libgfortran"  PROGRAMM=x13as
+	mkdir -p binaries/osx/64
+	mv tmp/osx/64/x13as binaries/osx/64/x13as
+	rm -rf tmp/osx/64
+
+binaries/osx/32/x13as: x13src/Makefile
+	mkdir -p tmp/osx/32
+	cp x13src/* tmp/osx/32
+	cd tmp/osx/32; patch Makefile < ../../../patches/Makefile.osx.patch; export PATH=$(ROOT_PATH)/osxcross/target/bin:$(PATH);  make FC=x86_64-apple-darwin15-gfortran  FFLAGS="-m32" LINKER=x86_64-apple-darwin15-gfortran LDFLAGS="-m32 -static-libgcc -static-libgfortran"  PROGRAMM=x13as
+	mkdir -p binaries/osx/32
+	mv tmp/osx/32/x13as binaries/osx/32/x13as
+	rm -rf tmp/osx/32
+
+x13src/${SRC_REMOTE_FILE}:
+	mkdir -p x13src
+	cd x13src; wget ${SRC_REMOTE_ADDRESS}${SRC_REMOTE_FILE}
+
+x13src/Makefile: x13src/${SRC_REMOTE_FILE}
+	cd x13src; tar xvf ${SRC_REMOTE_FILE}; mv makefile.gf Makefile; patch Makefile < ../patches/Makefile.patch
+
+externals/x13/linux/64/x13as: build
+	rm -rf tmp
+	cp -r binaries/* externals/x13
+
+dseries.zip: externals/x13/linux/64/x13as
+	mkdir -p tmp/dseries
+	cp -r src tmp/dseries
+	cp -r externals tmp/dseries
+	cp -r tests tmp/dseries
+	cp LICENSE.md tmp/dseries
+	cp README.md tmp/dseries
+	cd tmp && zip -r dseries.zip dseries && mv dseries.zip ../
+	rm -rf tmp
+
+clean-sources:
+	rm x13src/*.i x13src/*.f x13src/*.prm x13src/*.cmn x13src/*.var x13src/Makefile
+
+clean-binaries:
+	rm -rf binaries
diff --git a/patches/Makefile.osx.patch b/patches/Makefile.osx.patch
new file mode 100644
index 0000000..ddf35b9
--- /dev/null
+++ b/patches/Makefile.osx.patch
@@ -0,0 +1,11 @@
+--- Makefile	2017-09-01 18:04:35.825494849 +0200
++++ Makefile	2017-09-02 07:54:49.040617509 +0200
+@@ -288,7 +288,7 @@
+                   getrevdec.f m2q.f chqsea.f
+ 
+ $(PROGRAM): $(OBJS) $(LIBS)
+-	$(LINKER) -static -o $@ $(OBJS) $(LDMAP) $(LIBS) $(LDFLAGS)
++	$(LINKER) -o $@ $(OBJS) $(LDMAP) $(LIBS) $(LDFLAGS)
+ 
+ clean:
+ 	@rm -f $(OBJS)
diff --git a/patches/Makefile.patch b/patches/Makefile.patch
new file mode 100644
index 0000000..eefa0ca
--- /dev/null
+++ b/patches/Makefile.patch
@@ -0,0 +1,47 @@
+--- src2/Makefile	2016-02-03 18:14:44.000000000 +0100
++++ src/Makefile	2017-02-22 15:21:23.620664965 +0100
+@@ -1,15 +1,15 @@
+-# MKMF template makefile for protected mode executables.
+-FC        = gfortran
+-LINKER    = gfortran
+-PROGRAM         = x13asv11b39
+-DEST      = .
+-EXTHDRS         =
+-FFLAGS    = -O0
+-HDRS            =
+-LDFLAGS   = -s
++FC        ?= gfortran
++LINKER    ?= gfortran
++PROGRAM   ?= x13as
++DEST      ?= .
++EXTHDRS   =
++FFLAGS    ?= -O0
++HDRS      =
++LDFLAGS   ?= -s
+ LDMAP     = 
+ LIBS      =
+ MAKEFILE  = Makefile
++
+ OBJS            = aaamain.o abend.o acf.o acfar.o acfdgn.o \
+                   acfhdr.o addadj.o addaic.o addate.o addeas.o \
+                   addfix.o addlom.o addmat.o addmul.o addotl.o \
+@@ -287,14 +287,16 @@
+                   compcrodiag.f phasegain.f altundovrtst.f \
+                   getrevdec.f
+ 
+-$(PROGRAM):     $(OBJS) $(LIBS)
++$(PROGRAM): $(OBJS) $(LIBS)
+ 	$(LINKER) -static -o $@ $(OBJS) $(LDMAP) $(LIBS) $(LDFLAGS)
+ 
+-clean:;         @rm -f $(OBJS)
++clean:
++	@rm -f $(OBJS)
+ 
+-install:   $(PROGRAM)
++install: $(PROGRAM)
+ 	@echo Installing $(PROGRAM) in $(DEST)
+ 	@if not $(DEST)x==.x copy $(PROGRAM) $(DEST)
++
+ ### OPUS MKMF:  Do not remove this line!  Automatic dependencies follow.
+ 
+ aaamain.o:  build.prm cchars.i chrt.cmn error.cmn hiddn.cmn lex.i \
-- 
GitLab