Skip to content

Commit

Permalink
Clean up Makefile
Browse files Browse the repository at this point in the history
  • Loading branch information
zachasme committed Jul 26, 2019
1 parent 9bb3f63 commit 81851ec
Showing 1 changed file with 66 additions and 58 deletions.
124 changes: 66 additions & 58 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,33 +1,63 @@
EXTENSION = h3

# extract extension version from .control file
EXTVERSION = $(shell grep default_version $(EXTENSION).control | \
sed -e "s/default_version[[:space:]]*=[[:space:]]*'\([^']*\)'/\1/")

# the version to clone
# h3 core library version to clone and statically link
LIBH3_VERSION = v3.5.0
# where to store cloned repo
LIBH3_DIR = libh3-${LIBH3_VERSION}

INSTALL_FILES = $(wildcard sql/install/*.sql)
UPDATE_FILES = $(wildcard sql/updates/*.sql)
FULLINSTALL_SQL = sql/$(EXTENSION)--$(EXTVERSION).sql
UPDATETEST_SQL = sql/$(EXTENSION)--updatetest.sql
TESTS = $(wildcard test/sql/*.sql)
# directory that h3 core repository is cloned into
LIBH3_DIR = libh3-$(LIBH3_VERSION)
# h3 static library location
LIBH3_FILE = $(LIBH3_DIR)/install/lib/h3.a
# sql files used for installation, update and testing
SQL_INSTALLS = $(wildcard sql/install/*.sql)
SQL_UPDATES = $(wildcard sql/updates/*.sql)
SQL_TESTS = $(wildcard test/sql/*.sql)
SQL_FULLINSTALL = sql/$(EXTENSION)--$(EXTVERSION).sql
SQL_UPDATETEST = sql/$(EXTENSION)--updatetest.sql

DATA = $(UPDATE_FILES) $(FULLINSTALL_SQL) $(UPDATETEST_SQL)
OBJS = $(patsubst %.c,%.o,$(wildcard src/*.c))
# a shared library to build from multiple source files (list object files in OBJS)
MODULE_big = $(EXTENSION)
SHLIB_LINK += -lh3 -L${LIBH3_DIR}/install/lib
PG_CPPFLAGS += -I${LIBH3_DIR}/install/include

# testing
REGRESS = $(basename $(notdir $(TESTS)))
# object files to be linked together
OBJS = $(patsubst %.c,%.o,$(wildcard src/*.c))
# random files to install into $PREFIX/share/$MODULEDIR
DATA = $(SQL_UPDATES) $(SQL_FULLINSTALL) $(SQL_UPDATETEST)
# will be added to MODULE_big link line
SHLIB_LINK += -lh3 -L$(LIBH3_DIR)/install/lib
# will be added to CPPFLAGS
PG_CPPFLAGS += -I$(LIBH3_DIR)/install/include
# list of regression test cases (without suffix)
REGRESS = $(basename $(notdir $(SQL_TESTS)))
# additional switches to pass to pg_regress
REGRESS_OPTS = --inputdir=test --outputdir=test --load-extension=postgis --load-extension=h3
# extra files to remove in make clean
EXTRA_CLEAN += \
$(LIBH3_DIR) \
$(SQL_FULLINSTALL) \
$(SQL_UPDATETEST) \
src/extension.h \
test/expected/install.out \
test/expected/binding-functions.out \
test/sql/binding-functions.sql \
test/regression.diffs test/regression.out test/results \
h3-*.zip

# PGXS boilerplate
PG_CONFIG = pg_config
PGXS := $(shell $(PG_CONFIG) --pgxs)
include $(PGXS)

# build and compile h3 as static lib if not already installed
${LIBH3_DIR}:
git clone --branch ${LIBH3_VERSION} --depth 1 https://github.com/uber/h3 ${LIBH3_DIR}
${LIBH3_DIR}/cmake: ${LIBH3_DIR}
cd ${LIBH3_DIR} && cmake \
# generate full installation sql (from uninstalled to latest)
$(SQL_FULLINSTALL): $(sort $(SQL_INSTALLS))
cat $^ > $@
install: $(SQL_FULLINSTALL)

# build and compile h3 as static lib
$(LIBH3_DIR):
git clone --branch $(LIBH3_VERSION) --depth 1 https://github.com/uber/h3 $(LIBH3_DIR)
$(LIBH3_DIR)/cmake: $(LIBH3_DIR)
cd $(LIBH3_DIR) && cmake \
-DCMAKE_C_FLAGS=-fPIC \
-DCMAKE_INSTALL_PREFIX=./install \
-DBUILD_TESTING=OFF \
Expand All @@ -36,69 +66,47 @@ ${LIBH3_DIR}/cmake: ${LIBH3_DIR}
-DENABLE_FORMAT=OFF \
-DENABLE_LINTING=OFF \
.
${LIBH3_DIR}/install/lib/h3.a: ${LIBH3_DIR}/cmake
cmake --build ${LIBH3_DIR} --target install
$(OBJS): ${LIBH3_DIR}/install/lib/h3.a
$(LIBH3_FILE): $(LIBH3_DIR)/cmake
cmake --build $(LIBH3_DIR) --target install
$(OBJS): $(LIBH3_FILE)

# generate header file
# generate header file with extension version baked in
src/extension.h: src/extension.in.h
sed -e 's/@EXTVERSION@/${EXTVERSION}/g' \
sed -e 's/@EXTVERSION@/$(EXTVERSION)/g' \
$< > $@
$(OBJS): src/extension.h

# generate full install sql file
$(FULLINSTALL_SQL): $(sort $(INSTALL_FILES))
cat $^ > $@
install: $(FULLINSTALL_SQL)

# rules for testing the update path against full install
$(UPDATETEST_SQL): $(sort $(UPDATE_FILES))
$(SQL_UPDATETEST): $(sort $(SQL_UPDATES))
cat $^ > $@
test/expected/install.out: $(UPDATE_FILES)
test/expected/install.out: $(SQL_UPDATES)
psql -c "DROP DATABASE IF EXISTS pg_regress;"
psql -c "CREATE DATABASE pg_regress;"
psql -d pg_regress -c "CREATE EXTENSION postgis;"
psql -d pg_regress -c "CREATE EXTENSION h3 VERSION 'updatetest';"
echo "\df h3*" > $@
psql -d pg_regress -c "\df h3*" >> $@
psql -c "DROP DATABASE pg_regress;"
install: $(UPDATETEST_SQL)
install: $(SQL_UPDATETEST)
installcheck: test/expected/install.out

# rules for checking we have the correct bindings
${LIBH3_DIR}/binding-functions: ${LIBH3_DIR}/cmake
cmake --build ${LIBH3_DIR} --target binding-functions
$(OBJS): ${LIBH3_DIR}/binding-functions
test/expected/binding-functions.out: ${LIBH3_DIR}/binding-functions scripts/excluded-functions
$(LIBH3_DIR)/binding-functions: $(LIBH3_DIR)/cmake
cmake --build $(LIBH3_DIR) --target binding-functions
test/expected/binding-functions.out: $(LIBH3_DIR)/binding-functions
psql -c "DROP DATABASE IF EXISTS pg_regress;"
psql -c "CREATE DATABASE pg_regress;"
echo "\echo '$(shell scripts/binding-functions.sh ${LIBH3_DIR})'" > $@
psql -d pg_regress -c "\echo '$(shell scripts/binding-functions.sh ${LIBH3_DIR})'" >> $@
echo "\echo '$(shell scripts/binding-functions.sh $(LIBH3_DIR))'" > $@
psql -d pg_regress -c "\echo '$(shell scripts/binding-functions.sh $(LIBH3_DIR))'" >> $@
psql -c "DROP DATABASE pg_regress;"
test/sql/binding-functions.sql: test/expected/install.out scripts/extra-functions
psql -c "DROP DATABASE IF EXISTS pg_regress;"
psql -c "CREATE DATABASE pg_regress;"
echo "\echo '$(shell scripts/binding-functions.sh)'" > $@
psql -c "DROP DATABASE pg_regress;"
installcheck: test/sql/binding-functions.sql test/expected/binding-functions.out
$(OBJS): $(LIBH3_DIR)/binding-functions

# zip up for distribution
distribute: clean
git archive --format zip --prefix=h3-${EXTVERSION}/ --output h3-${EXTVERSION}.zip master

# cleanup
EXTRA_CLEAN += \
${LIBH3_DIR} \
$(FULLINSTALL_SQL) \
$(UPDATETEST_SQL) \
src/extension.h \
test/expected/install.out \
test/expected/bindings-functions.out \
test/sql/bindings-functions.sql \
test/regression.diffs test/regression.out test/results \
h3-*.zip

# PGXS boilerplate
PG_CONFIG = pg_config
PGXS := $(shell $(PG_CONFIG) --pgxs)
include $(PGXS)
git archive --format zip --prefix=h3-$(EXTVERSION)/ --output h3-$(EXTVERSION).zip master

0 comments on commit 81851ec

Please sign in to comment.