Skip to content

Instantly share code, notes, and snippets.

@Jens0512
Last active February 15, 2024 16:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jens0512/aa5da9465e2aa6bf246d7d2ade4b44ac to your computer and use it in GitHub Desktop.
Save Jens0512/aa5da9465e2aa6bf246d7d2ade4b44ac to your computer and use it in GitHub Desktop.
General Makefile for Crystal programs.
#!/usr/bin/env make -f
#
# General Makefile for a crystal program.
# For it to work, you want to change PROG_NAME to your programs name.
# You should also ensure PROG_TARGET and SPEC_TARGET
# are respectively the programs entry file, and the specs entry file.
.POSIX:
.ONESHELL:
SHELL := sh
CRYSTAL := crystal
SHARDS := shards
CRFLAGS :=
FIND := find
MAKE += --no-print-directory
DESTDIR =
PREFIX := /usr/local
BINDIR := $(DESTDIR)$(PREFIX)/bin
SOURCES := $(shell $(FIND) src -type f -name '*.cr' 2>&-||:)
SPECS := $(shell $(FIND) spec -type f -name '*.cr' 2>&-||:)
SPEC_NAME := spec
PROG_NAME := program_name
PROG_TARGET := src/$(PROG_NAME).cr
SPEC_TARGET := spec/$(PROG_NAME)_spec.cr
OUT := bin
PROG_BIN := $(OUT)/$(PROG_NAME)
SPEC_BIN := $(OUT)/$(SPEC_NAME)
# These are set empty so commandline completion (if installed)
# detects them; E.g. '$ make v<tab>' completes to 'verbose='
stats ?= ### Enable statistics output
debug ?= ### Add symbolic debug info
static ?= ### Enable static linking
threads ?= ### Maximum number of threads to use
verbose ?= ### Run specs in verbose mode
release ?= ### Compile in release mode
no_debug ?= ### Skip any symbolic debug info
progress ?= ### Enable progress output
junit_out ?= ### Directory to output junit results
override CRFLAGS += $(if $(release),--release )$(if $(stats),--stats )$(if $(progress),--progress)
override CRFLAGS += $(if $(debug),-d )$(if $(no_debug),--no-debug)
override CRFLAGS += $(if $(static),--static )$(if $(threads),--threads $(threads))
override CRFLAGS := $(strip $(CRFLAGS))
override SPEC_FLAGS += $(strip $(if $(verbose),-v )$(if $(junit_out),--junit_output $(junit_out)))
all: $(PROG_BIN)
install: phony
install: export release := 1
install: export no_debug := 1
install: all_with_env
chmod 755 $(PROG_BIN)
cp $(PROG_BIN) $(BINDIR)
uninstall: phony
rm -f $(BINDIR)/$(PROG_NAME)
cr_build = $(strip $(CRYSTAL) build $(CRFLAGS) -o $@)
ifdef CRFLAGS
$(PROG_BIN): phony
endif
$(PROG_BIN): $(SOURCES) lib
$(SHARDS) install
mkdir -p $(@D)
$(cr_build) $(PROG_TARGET)
test: spec
spec: $(SPEC_BIN)
$(SPEC_BIN)
ifdef SPEC_FLAGS
$(SPEC_BIN): phony
endif
$(SPEC_BIN): $(SOURCES) $(SPECS) lib
$(SHARDS) install
mkdir -p $(@D)
$(cr_build) $(SPEC_FLAGS) $(SPEC_TARGET)
ifneq ($(shell [ -f shard.lock ] && echo exists),)
lib: shard.lock
endif
lib: shard.yml $(DEPS)
$(SHARDS) install
mkdir -p lib/
shard.lock: phony
shards: phony
$(SHARDS) install
clean: phony
rm -f $(PROG_NAME) 2>&-||:
rm -f $(SPEC_NAME) 2>&-||:
touch:
touch $(PROG_TARGET)
# For order of things
all_with_env:
$(MAKE) all
phony:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment