1# pgloader build tool
2APP_NAME   = pgloader
3VERSION    = 3.6.2
4
5# use either sbcl or ccl
6CL	   = sbcl
7
8# default to 4096 MB of RAM size in the image
9DYNSIZE    = 4096
10
11LISP_SRC   = $(wildcard src/*lisp)         \
12             $(wildcard src/monkey/*lisp)  \
13             $(wildcard src/utils/*lisp)   \
14             $(wildcard src/load/*lisp)    \
15             $(wildcard src/parsers/*lisp) \
16             $(wildcard src/pg-copy/*lisp) \
17             $(wildcard src/pgsql/*lisp)   \
18             $(wildcard src/sources/*lisp) \
19             pgloader.asd
20
21BUILDDIR   = build
22LIBS       = $(BUILDDIR)/libs.stamp
23QLDIR      = $(BUILDDIR)/quicklisp
24MANIFEST   = $(BUILDDIR)/manifest.ql
25LATEST     = $(BUILDDIR)/pgloader-latest.tgz
26
27BUNDLEDIST = 2020-02-18
28BUNDLENAME = pgloader-bundle-$(VERSION)
29BUNDLEDIR  = $(BUILDDIR)/bundle/$(BUNDLENAME)
30BUNDLE     = $(BUILDDIR)/$(BUNDLENAME).tgz
31BUNDLETESTD= $(BUILDDIR)/bundle/test
32
33ifneq ($(shell getconf LONG_BIT),64)
34# Assuming 32 bit FreeBSD and setting DYNSIZE down to 1 GiB...
35# (Will be used by SBCL for its GC/heap, 32 bit systems can only
36# allocate 4 GiB max).
37DYNSIZE = 1024
38endif
39
40ifeq ($(OS),Windows_NT)
41EXE           = .exe
42COMPRESS_CORE = no
43DYNSIZE       = 1024		# support for windows 32 bits
44else
45EXE =
46endif
47
48PGLOADER   = $(BUILDDIR)/bin/$(APP_NAME)$(EXE)
49BUILDAPP_CCL  = $(BUILDDIR)/bin/buildapp.ccl$(EXE)
50BUILDAPP_SBCL = $(BUILDDIR)/bin/buildapp.sbcl$(EXE)
51
52ifeq ($(CL),sbcl)
53BUILDAPP      = $(BUILDAPP_SBCL)
54BUILDAPP_OPTS = --require sb-posix                      \
55                --require sb-bsd-sockets                \
56                --require sb-rotate-byte
57CL_OPTS    = --noinform --no-sysinit --no-userinit
58else
59BUILDAPP   = $(BUILDAPP_CCL)
60CL_OPTS    = --no-init
61endif
62
63ifeq ($(CL),sbcl)
64COMPRESS_CORE ?= $(shell $(CL) --noinform \
65                               --quit     \
66                               --eval '(when (member :sb-core-compression cl:*features*) (write-string "yes"))')
67
68endif
69
70# note: on Windows_NT, we never core-compress; see above.
71ifeq ($(COMPRESS_CORE),yes)
72COMPRESS_CORE_OPT = --compress-core
73endif
74
75DEBUILD_ROOT = /tmp/pgloader
76
77all: $(PGLOADER)
78
79clean:
80	rm -rf $(LIBS) $(QLDIR) $(MANIFEST) $(BUILDAPP) $(PGLOADER) docs/_build
81
82$(QLDIR)/local-projects/qmynd:
83	git clone --depth 1 https://github.com/qitab/qmynd.git $@
84
85$(QLDIR)/local-projects/cl-ixf:
86	git clone --depth 1 https://github.com/dimitri/cl-ixf.git $@
87
88$(QLDIR)/local-projects/cl-db3:
89	git clone --depth 1 https://github.com/dimitri/cl-db3.git $@
90
91$(QLDIR)/local-projects/cl-csv:
92	git clone --depth 1 https://github.com/AccelerationNet/cl-csv.git $@
93
94$(QLDIR)/setup.lisp:
95	mkdir -p $(BUILDDIR)
96#	curl -o $(BUILDDIR)/quicklisp.lisp http://beta.quicklisp.org/quicklisp.lisp
97	$(CL) $(CL_OPTS) --load $(BUILDDIR)/quicklisp.lisp                        \
98             --load src/getenv.lisp                                               \
99             --eval '(quicklisp-quickstart:install :path "$(BUILDDIR)/quicklisp" :proxy (getenv "http_proxy"))' \
100             --eval '(quit)'
101
102quicklisp: $(QLDIR)/setup.lisp ;
103
104clones: $(QLDIR)/local-projects/cl-ixf \
105        $(QLDIR)/local-projects/cl-db3 \
106        $(QLDIR)/local-projects/cl-csv \
107        $(QLDIR)/local-projects/qmynd ;
108
109$(LIBS): $(QLDIR)/setup.lisp
110	$(CL) $(CL_OPTS) --load $(QLDIR)/setup.lisp                   \
111             --eval '(push :pgloader-image *features*)'               \
112             --eval '(setf *print-circle* t *print-pretty* t)'        \
113             --eval '(push "$(PWD)/" ql:*local-project-directories*)' \
114             --eval '(ql:quickload "pgloader")'                       \
115             --eval '(quit)'
116	touch $@
117
118libs: $(LIBS) ;
119
120$(MANIFEST): $(LIBS)
121	$(CL) $(CL_OPTS) --load $(QLDIR)/setup.lisp                \
122             --eval '(ql:write-asdf-manifest-file "$(MANIFEST)")'  \
123             --eval '(quit)'
124
125manifest: $(MANIFEST) ;
126
127$(BUILDAPP_CCL): $(QLDIR)/setup.lisp
128	mkdir -p $(BUILDDIR)/bin
129	$(CL) $(CL_OPTS) --load $(QLDIR)/setup.lisp               \
130             --eval '(ql:quickload "buildapp")'                   \
131             --eval '(buildapp:build-buildapp "$@")'              \
132             --eval '(quit)'
133
134$(BUILDAPP_SBCL): $(QLDIR)/setup.lisp
135	mkdir -p $(BUILDDIR)/bin
136	cd $(BUILDDIR)/quicklisp/dists/quicklisp/software/buildapp-1.5.6/ && LISP=$(CL) gmake
137	mv $(BUILDDIR)/quicklisp/dists/quicklisp/software/buildapp-1.5.6/buildapp $(BUILDDIR)/bin/buildapp.sbcl
138#$(CL) $(CL_OPTS) --load $(QLDIR)/setup.lisp               \
139             --eval '(ql:quickload "buildapp")'                   \
140             --eval '(buildapp:build-buildapp "$@")'              \
141             --eval '(quit)'
142
143buildapp: $(BUILDAPP) ;
144
145$(PGLOADER): $(MANIFEST) $(BUILDAPP) $(LISP_SRC)
146	mkdir -p $(BUILDDIR)/bin
147	$(BUILDAPP)      --logfile /tmp/build.log                \
148                         $(BUILDAPP_OPTS)                        \
149                         --sbcl $(CL)                            \
150                         --asdf-path .                           \
151                         --asdf-tree $(QLDIR)/local-projects     \
152                         --manifest-file $(MANIFEST)             \
153                         --asdf-tree $(QLDIR)/dists              \
154                         --asdf-path .                           \
155                         --load-system cffi                      \
156                         --load-system cl+ssl                    \
157                         --load-system mssql                     \
158                         --load src/hooks.lisp                   \
159                         --load-system $(APP_NAME)               \
160                         --entry pgloader:main                   \
161                         --dynamic-space-size $(DYNSIZE)         \
162                         $(COMPRESS_CORE_OPT)                    \
163                         --output $@.tmp
164	# that's ugly, but necessary when building on Windows :(
165	mv $@.tmp $@
166
167pgloader: $(PGLOADER) ;
168
169pgloader-standalone:
170	$(BUILDAPP)    $(BUILDAPP_OPTS)                        \
171                       --sbcl $(CL)                            \
172                       --load-system $(APP_NAME)               \
173                       --load src/hooks.lisp                   \
174                       --entry pgloader:main                   \
175                       --dynamic-space-size $(DYNSIZE)         \
176                       $(COMPRESS_CORE_OPT)                    \
177                       --output $(PGLOADER)
178test: $(PGLOADER)
179	$(MAKE) PGLOADER=$(realpath $(PGLOADER)) CL=$(CL) -C test regress
180
181save: ./src/save.lisp $(LISP_SRC)
182	$(CL) $(CL_OPTS) --load ./src/save.lisp
183
184check-saved:
185	$(MAKE) PGLOADER=$(realpath $(PGLOADER)) CL=$(CL) -C test regress
186
187clean-bundle:
188	rm -rf $(BUNDLEDIR)
189	rm -rf $(BUNDLETESTD)/$(BUNDLENAME)/*
190
191$(BUNDLETESTD):
192	mkdir -p $@
193
194$(BUNDLEDIR):
195	mkdir -p $@
196	$(CL) $(CL_OPTS) --load $(QLDIR)/setup.lisp      \
197             --eval '(defvar *bundle-dir* "$@")'         \
198             --eval '(defvar *pwd* "$(PWD)/")'           \
199             --eval '(defvar *ql-dist* "$(BUNDLEDIST)")' \
200             --load bundle/ql.lisp
201
202$(BUNDLEDIR)/version.sexp: $(BUNDLEDIR)
203	echo "\"$(VERSION)\"" > $@
204
205$(BUNDLE): $(BUNDLEDIR) $(BUNDLEDIR)/version.sexp
206	cp bundle/README.md $(BUNDLEDIR)
207	cp bundle/save.lisp $(BUNDLEDIR)
208	sed -e s/%VERSION%/$(VERSION)/ < bundle/Makefile > $(BUNDLEDIR)/Makefile
209	git archive --format=tar --prefix=pgloader-$(VERSION)/ master \
210	     | tar -C $(BUNDLEDIR)/local-projects/ -xf -
211#	make QLDIR=$(BUNDLEDIR) clones
212	tar -C build/bundle 		    \
213            --exclude bin   		    \
214            --exclude test/sqlite           \
215            -czf $@ $(BUNDLENAME)
216
217bundle: clean-bundle $(BUNDLE) $(BUNDLETESTD)
218	tar -C $(BUNDLETESTD) -xf $(BUNDLE)
219	make -C $(BUNDLETESTD)/$(BUNDLENAME)
220	$(BUNDLETESTD)/$(BUNDLENAME)/bin/pgloader --version
221
222test-bundle:
223	$(MAKE) -C $(BUNDLEDIR) test
224
225
226deb:
227	# intended for use on a debian system
228	mkdir -p $(DEBUILD_ROOT) && rm -rf $(DEBUILD_ROOT)/*
229	rsync -Ca --exclude 'build'                      		  \
230		  --exclude '.vagrant'                   		  \
231              ./ $(DEBUILD_ROOT)/
232	cd $(DEBUILD_ROOT) && make -f debian/rules orig
233	cd $(DEBUILD_ROOT) && debuild -us -uc -sa
234	cp -a /tmp/pgloader_* /tmp/cl-pgloader* build/
235
236rpm:
237	# intended for use on a CentOS or other RPM based system
238	mkdir -p $(DEBUILD_ROOT) && rm -rf $(DEBUILD_ROOT)
239	rsync -Ca --exclude=build/* ./ $(DEBUILD_ROOT)/
240	cd /tmp && tar czf $(HOME)/rpmbuild/SOURCES/pgloader-$(VERSION).tar.gz pgloader
241	cd $(DEBUILD_ROOT) && rpmbuild -ba pgloader.spec
242	cp -a $(HOME)/rpmbuild/SRPMS/*rpm build
243	cp -a $(HOME)/rpmbuild/RPMS/x86_64/*rpm build
244
245pkg:
246	# intended for use on a MacOSX system
247	mkdir -p $(DEBUILD_ROOT) && rm -rf $(DEBUILD_ROOT)/*
248	mkdir -p $(DEBUILD_ROOT)/usr/local/bin/
249	mkdir -p $(DEBUILD_ROOT)/usr/local/share/man/man1/
250	cp ./pgloader.1 $(DEBUILD_ROOT)/usr/local/share/man/man1/
251	cp ./build/bin/pgloader $(DEBUILD_ROOT)/usr/local/bin/
252	pkgbuild --identifier org.tapoueh.pgloader \
253	         --root $(DEBUILD_ROOT)            \
254	         --version $(VERSION)              \
255                 ./build/pgloader-$(VERSION).pkg
256
257latest:
258	git archive --format=tar --prefix=pgloader-$(VERSION)/ v$(VERSION) \
259        | gzip -9 > $(LATEST)
260
261check: test ;
262
263.PHONY: test pgloader-standalone docs bundle
264