# General-purpose Makefile for PLINK 1.90
#
# Compilation options:
#   Do not link to LAPACK                    NO_LAPACK

# Leave blank after "=" to disable; put "= 1" to enable
# (when enabled, "#define NOLAPACK" must be uncommented in plink_common.h)
NO_LAPACK =

# Variable that allows additional linker flags (e.g., "-L/path/to/libs") to be
# passed into the linker; to use this, invoke 'make LINKFLAGS_EXTRA="<opts>"'.
LINKFLAGS_EXTRA =

.PHONY: clean

# should autodetect system
SYS = UNIX
ifdef SystemRoot
  SYS = WIN
else
  UNAME := $(shell uname)
  ifeq ($(UNAME), Darwin)
    SYS = MAC
  endif
endif

CFLAGS=-Wall -O2
BLASFLAGS=-L/usr/lib64/atlas -llapack -lcblas -latlas
LINKFLAGS=-lm -lpthread -ldl
ZLIB=../zlib-1.2.11/libz.so.1.2.11

ifeq ($(SYS), MAC)
  GCC_GTEQ_43 := $(shell expr `g++ -dumpversion | sed -e 's/\.\([0-9][0-9]\)/\1/g' -e 's/\.\([0-9]\)/0\1/g' -e 's/^[0-9]\{3,4\}$$/&00/'` \>= 40300)
  ifeq "$(GCC_GTEQ_43)" "1"
    CFLAGS=-Wall -O2 -flax-vector-conversions
  endif
  BLASFLAGS=-framework Accelerate
  LINKFLAGS=-ldl
  ZLIB=../zlib-1.2.11/libz.1.2.11.dylib
endif

ifeq ($(SYS), WIN)
# Note that, unlike the Linux and Mac build processes, this STATICALLY links
# LAPACK, since we have not gotten around to trying dynamically-linked LAPACK
# on Windows.
# If you don't already have LAPACK built, you'll probably want to turn on
# NO_LAPACK.
  BLASFLAGS=-L. lapack/liblapack.a -L. lapack/librefblas.a
  LINKFLAGS=-lm -static-libgcc
  ZLIB=../zlib-1.2.11/libz.a
endif

ifdef NO_LAPACK
  BLASFLAGS=
endif

OBJS = plink.o plink_assoc.o plink_calc.o plink_cluster.o plink_cnv.o plink_common.o plink_data.o plink_dosage.o plink_family.o plink_filter.o plink_glm.o plink_help.o plink_homozyg.o plink_lasso.o plink_ld.o plink_matrix.o plink_misc.o plink_perm.o plink_rserve.o plink_set.o plink_stats.o SFMT.o dcdflib.o pigz.o yarn.o Rconnection.o hfile.o bgzf.o

# In the event that you are still concurrently using PLINK 1.07, we suggest
# renaming that binary to "plink107", and this one to "plink1".

plink: $(OBJS)
	g++ $^ $(LINKFLAGS_EXTRA) $(BLASFLAGS) $(LINKFLAGS) -L. $(ZLIB) -o $@

plinkw: $(OBJS)
	gfortran -O2 $^ $(LINKFLAGS_EXTRA) -Wl,-Bstatic $(BLASFLAGS) $(LINKFLAGS) -L. $(ZLIB) -o $@

clean:
	rm -f $(OBJS) plink plinkw


# Pattern-based rules for compiling object (.o) files; basically identical to
# GNU make's built-in rules, except we explicitly use "g++" instead of $(CC).

%.o: %.c
	g++ -c $(CFLAGS) $< -o $@

%.o: %.cc
	g++ -x c++ -c $(CFLAGS) $< -o $@
