From c912c0aa0bf17cbe2213cc482c99b2807298c071 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Sat, 28 Aug 2021 12:41:43 +0200 Subject: [PATCH 1/4] Beginning of Makefile --- .cargo/config | 3 +++ .gitignore | 2 +- Makefile | 24 ++++++++++++++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 .cargo/config create mode 100644 Makefile diff --git a/.cargo/config b/.cargo/config new file mode 100644 index 0000000..b086373 --- /dev/null +++ b/.cargo/config @@ -0,0 +1,3 @@ +# vim: ft=toml +[build] +target-dir = "out/target" diff --git a/.gitignore b/.gitignore index 4d7b925..e79787c 100644 --- a/.gitignore +++ b/.gitignore @@ -2,7 +2,7 @@ # Generated by Cargo # will have compiled files and executables debug/ -target/ +out/ # Remove Cargo.lock from gitignore if creating an executable, leave it for libraries # More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..b70edac --- /dev/null +++ b/Makefile @@ -0,0 +1,24 @@ +PQ_VER ?= 11.12 +SSL_VER ?= 1.1.1k + +OUT_DIR ?= out/deps + +export CC = musl-gcc -fPIC -pie -static + +# TODO check for header files (openssl-dev, libpq-dev) both for Arch & Ubuntu + + +all: openssl +.PHONY: all + + +# =====OPENSSL===== +# Download the source code +$(OUT_DIR)/openssl-$(SSL_VER)/Configure: + mkdir -p '$(OUT_DIR)' + curl -sSL "https://www.openssl.org/source/openssl-$(SSL_VER).tar.gz" | \ + tar -C "$(OUT_DIR)" -xz + +# Build OpenSSL +openssl: $(OUT_DIR)/openssl-$(SSL_VER)/Configure +.PHONY: openssl From 85cfe6290c48756bd5f9427c27b74c757ca31fce Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Sat, 28 Aug 2021 13:53:13 +0200 Subject: [PATCH 2/4] Almost working libpq --- Makefile | 55 +++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 49 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index b70edac..2ce27a2 100644 --- a/Makefile +++ b/Makefile @@ -1,24 +1,67 @@ PQ_VER ?= 11.12 SSL_VER ?= 1.1.1k -OUT_DIR ?= out/deps +# This is such a lovely oneliner +# NOTE: $(dir PATH) outputs a trailing slash +OUT_DIR ?= $(dir $(abspath $(lastword $(MAKEFILE_LIST))))out/deps + +# Generated variables for ease of use +PREFIX := $(OUT_DIR)/prefix +OPENSSL_DIR := $(OUT_DIR)/openssl-$(SSL_VER) +PQ_DIR := $(OUT_DIR)/postgresql-$(PQ_VER) +CORES != nproc + +export CC=musl-gcc -fPIC -pie -static +export LD_LIBRARY_PATH=$(PREFIX) +export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig -export CC = musl-gcc -fPIC -pie -static # TODO check for header files (openssl-dev, libpq-dev) both for Arch & Ubuntu +# Create the out dir +$(shell mkdir -p "$(PREFIX)") all: openssl .PHONY: all # =====OPENSSL===== -# Download the source code -$(OUT_DIR)/openssl-$(SSL_VER)/Configure: - mkdir -p '$(OUT_DIR)' +# Download the source code & configure the project +$(OPENSSL_DIR)/Configure: curl -sSL "https://www.openssl.org/source/openssl-$(SSL_VER).tar.gz" | \ tar -C "$(OUT_DIR)" -xz + cd "$(OPENSSL_DIR)" && \ + CC="$$CC -idirafter /usr/include" ./Configure \ + no-zlib \ + no-shared \ + --prefix="$(PREFIX)" \ + --openssldir="$(PREFIX)/ssl" \ + linux-x86_64 # Build OpenSSL -openssl: $(OUT_DIR)/openssl-$(SSL_VER)/Configure +openssl: $(OPENSSL_DIR)/Configure + C_INCLUDE_PATH="$(PREFIX)/include" $(MAKE) -C "$(OPENSSL_DIR)" depend + $(MAKE) -C "$(OPENSSL_DIR)" -j$(CORES) + $(MAKE) -C "$(OPENSSL_DIR)" install_sw .PHONY: openssl + + +# =====LIBPQ===== +# Download the source code & configure the project +$(PQ_DIR)/configure: + curl -sSL "https://ftp.postgresql.org/pub/source/v$(PQ_VER)/postgresql-$(PQ_VER).tar.gz" | \ + tar -C "$(OUT_DIR)" -xz + cd "$(PQ_DIR)" && \ + LDFLAGS="-L$(PREFIX)/lib" CFLAGS="-I$(PREFIX)/include" ./configure \ + --without-readline \ + --without-zlib \ + --with-openssl \ + --prefix="$(PREFIX)" \ + --host=x86_64-unknown-linux-musl + +libpq: $(PQ_DIR)/configure + make -C "$(PQ_DIR)/src/interfaces/libpq" -j$(CORES) all-static-lib + make -C "$(PQ_DIR)/src/interfaces/libpq" install install-lib-static + make -C "$(PQ_DIR)/src/bin/pg_config" -j $(CORES) + make -C "$(PQ_DIR)/src/bin/pg_config" install +.PHONY: libpq From 055d1f9e8da054e316ec84eb8819fe42403e4248 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Sat, 28 Aug 2021 14:20:51 +0200 Subject: [PATCH 3/4] Added dumb-init; changed some stuff --- Makefile | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/Makefile b/Makefile index 2ce27a2..d7bbb41 100644 --- a/Makefile +++ b/Makefile @@ -1,16 +1,20 @@ -PQ_VER ?= 11.12 +PQ_VER ?= 11.12 SSL_VER ?= 1.1.1k +DI_VER ?= 1.2.5 -# This is such a lovely oneliner -# NOTE: $(dir PATH) outputs a trailing slash -OUT_DIR ?= $(dir $(abspath $(lastword $(MAKEFILE_LIST))))out/deps # Generated variables for ease of use -PREFIX := $(OUT_DIR)/prefix +# This is such a lovely oneliner +# NOTE: $(dir PATH) outputs a trailing slash +OUT_DIR ?= $(dir $(abspath $(lastword $(MAKEFILE_LIST))))out +PREFIX := $(OUT_DIR)/prefix OPENSSL_DIR := $(OUT_DIR)/openssl-$(SSL_VER) -PQ_DIR := $(OUT_DIR)/postgresql-$(PQ_VER) +PQ_DIR := $(OUT_DIR)/postgresql-$(PQ_VER) +DI_DIR := $(OUT_DIR)/dumb-init-$(DI_VER) + CORES != nproc + export CC=musl-gcc -fPIC -pie -static export LD_LIBRARY_PATH=$(PREFIX) export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig @@ -21,8 +25,12 @@ export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig # Create the out dir $(shell mkdir -p "$(PREFIX)") -all: openssl .PHONY: all +all: build + +# libpq builds openssl as a dependency +.PHONY: build +build: libpq # =====OPENSSL===== @@ -39,11 +47,11 @@ $(OPENSSL_DIR)/Configure: linux-x86_64 # Build OpenSSL +.PHONY: openssl openssl: $(OPENSSL_DIR)/Configure C_INCLUDE_PATH="$(PREFIX)/include" $(MAKE) -C "$(OPENSSL_DIR)" depend $(MAKE) -C "$(OPENSSL_DIR)" -j$(CORES) $(MAKE) -C "$(OPENSSL_DIR)" install_sw -.PHONY: openssl # =====LIBPQ===== @@ -59,9 +67,19 @@ $(PQ_DIR)/configure: --prefix="$(PREFIX)" \ --host=x86_64-unknown-linux-musl -libpq: $(PQ_DIR)/configure +.PHONY: libpq +libpq: openssl $(PQ_DIR)/configure make -C "$(PQ_DIR)/src/interfaces/libpq" -j$(CORES) all-static-lib make -C "$(PQ_DIR)/src/interfaces/libpq" install install-lib-static make -C "$(PQ_DIR)/src/bin/pg_config" -j $(CORES) make -C "$(PQ_DIR)/src/bin/pg_config" install -.PHONY: libpq + + +# =====DUMB-INIT===== +# NOTE: this is only used inside the Docker image, but it's here for completeness. +$(DI_DIR)/Makefile: + curl -sSL "https://github.com/Yelp/dumb-init/archive/refs/tags/v$(DI_VER).tar.gz" | \ + tar -C "$(OUT_DIR)" -xz + +dumb-init: $(DI_DIR)/Makefile + make -C "$(DI_DIR)" build From de8be87036871e3ea3c181df7e7d16a19b40daa3 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Sat, 28 Aug 2021 16:37:32 +0200 Subject: [PATCH 4/4] Added some comments --- Makefile | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index d7bbb41..0f1146d 100644 --- a/Makefile +++ b/Makefile @@ -1,20 +1,27 @@ +# =====CONFIGURATION===== +# Version of postgresql to compile libpq from PQ_VER ?= 11.12 +# OpenSSL version SSL_VER ?= 1.1.1k +# Dumb-init version DI_VER ?= 1.2.5 -# Generated variables for ease of use +# =====AUTO-GENERATED VARIABLES===== # This is such a lovely oneliner # NOTE: $(dir PATH) outputs a trailing slash -OUT_DIR ?= $(dir $(abspath $(lastword $(MAKEFILE_LIST))))out +OUT_DIR ?= $(dir $(abspath $(lastword $(MAKEFILE_LIST))))out + PREFIX := $(OUT_DIR)/prefix OPENSSL_DIR := $(OUT_DIR)/openssl-$(SSL_VER) PQ_DIR := $(OUT_DIR)/postgresql-$(PQ_VER) DI_DIR := $(OUT_DIR)/dumb-init-$(DI_VER) -CORES != nproc +# Used in various make calls to specify parallel recipes +CORES != nproc +# =====ENVIRONMENT VARIABLES===== export CC=musl-gcc -fPIC -pie -static export LD_LIBRARY_PATH=$(PREFIX) export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig @@ -25,6 +32,8 @@ export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig # Create the out dir $(shell mkdir -p "$(PREFIX)") + +# ====RECIPES==== .PHONY: all all: build @@ -32,6 +41,11 @@ all: build .PHONY: build build: libpq +.PHONY: clean +clean: + @ echo "Note: this only cleans the C dependencies, not the Cargo cache." + rm -rf "$(PQ_DIR)" "$(OPENSSL_DIR)" "$(DI_DIR)" "$(PREFIX)" + # =====OPENSSL===== # Download the source code & configure the project @@ -62,8 +76,8 @@ $(PQ_DIR)/configure: cd "$(PQ_DIR)" && \ LDFLAGS="-L$(PREFIX)/lib" CFLAGS="-I$(PREFIX)/include" ./configure \ --without-readline \ - --without-zlib \ --with-openssl \ + --without-zlib \ --prefix="$(PREFIX)" \ --host=x86_64-unknown-linux-musl @@ -81,5 +95,6 @@ $(DI_DIR)/Makefile: curl -sSL "https://github.com/Yelp/dumb-init/archive/refs/tags/v$(DI_VER).tar.gz" | \ tar -C "$(OUT_DIR)" -xz +.PHONY: dumb-init dumb-init: $(DI_DIR)/Makefile make -C "$(DI_DIR)" build