CC      ?= cc
CFLAGS  ?= -std=c11 -Wall -Wextra -O2
PREFIX  ?= /usr/local
CFLAGS  += -Iinclude -DCAELIX_STDLIB_DIR=\"$(PREFIX)/share/caelix/stdlib\"
LDLIBS  += -lm

# HTTPS: TLS=auto (default) uses OpenSSL when its development files are installed; TLS=0 or TLS=1 force it.
TLS      ?= auto
TLS_INC  ?=
TLS_LIBS ?= -lssl -lcrypto
ifeq ($(TLS),auto)
  TLS := $(if $(shell $(CC) tools/tls_probe.c -o .tlsprobe $(TLS_INC) $(TLS_LIBS) 2>/dev/null && rm -f .tlsprobe && echo 1),1,0)
endif
ifeq ($(TLS),1)
  CFLAGS += -DCAELIX_TLS $(TLS_INC)
  LDLIBS += $(TLS_LIBS)
endif
ifneq ($(MAKECMDGOALS),clean)
  $(info Caelix build: HTTPS $(if $(filter 1,$(TLS)),ENABLED (OpenSSL),disabled - install OpenSSL then 'make clean && make' to enable))
endif
SRC     := $(wildcard src/*.c)
OBJ     := $(SRC:.c=.o)

all: caelix

caelix: $(OBJ)
	$(CC) $(OBJ) -o $@ $(LDLIBS)

src/%.o: src/%.c include/caelix.h
	$(CC) $(CFLAGS) -c $< -o $@

test: caelix
	sh tests/run_tests.sh
	CAELIX_GC_STRESS=1 sh tests/run_tests.sh
	sh tests/repl_test.sh
	sh tests/clxpm_test.sh
	sh tests/http_test.sh
	sh tests/https_test.sh
	sh tests/network_test.sh
	sh tests/date_tz_test.sh
	sh tests/highlight_test.sh
	sh tests/pty_test.sh
	sh tests/pip_test.sh

install: caelix
	mkdir -p $(DESTDIR)$(PREFIX)/bin $(DESTDIR)$(PREFIX)/share/caelix/stdlib
	rm -f $(DESTDIR)$(PREFIX)/bin/caelix $(DESTDIR)$(PREFIX)/bin/clxpm
	cp caelix clxpm $(DESTDIR)$(PREFIX)/bin/
	chmod +x $(DESTDIR)$(PREFIX)/bin/clxpm
	cp stdlib/*.clx $(DESTDIR)$(PREFIX)/share/caelix/stdlib/

uninstall:
	rm -f $(DESTDIR)$(PREFIX)/bin/caelix $(DESTDIR)$(PREFIX)/bin/clxpm
	rm -rf $(DESTDIR)$(PREFIX)/share/caelix

clean:
	rm -f $(OBJ) caelix
	rm -rf build dist python/*.egg-info

.PHONY: all test clean install uninstall
