
# -*- PIXIE-4 Makefile for Linux -*-
# PLX build and load scripts need bash
SHELL := /bin/bash

# Root directory
PIXIE_ROOT_DIR=$(shell pwd)

# The archiver
AR := ar
# C compiler
CC := gcc
# C++ compiler
CXX := g++
# C flags
CFLAGS := -DXIA_LINUX -DPLX_LINUX -fPIC
CFLAG_COMPILE := -c
CFLAG_OUT     := -o
# C++ flags
CXXFLAGS := $(shell root-config --cflags) $(CFLAGS)
# Include flags
INCFLAGS := -Iinc -I.
# This should be the linker
LINK := ld
# Linker flags
LINK_PRE_FLAGS := -L. -Llib
LINK_FLAG_OUT := -o
DYN_LIB_LINK_FLAGS := -shared
# System libraries
SYSTEM_LIBS := -lm -ldl
# Root libraries
ROOT_LIBS := $(shell root-config --glibs)
# PLX SDK root dir
PLX_SDK_DIR=$(PIXIE_ROOT_DIR)/Linux/PlxSdk
# Precompiled PLX library 
PLX_LIB := -lPlxApi
# Pixie library name
LIBNAME = Pixie4
# PIXIE-4 library object files to create
LIBOBJS = Boot.o \
          ua_par_io.o \
          eeprom.o \
          pixie_c.o \
          utilities.o \
          globals.o

NSCOPEOBJS = ../Root/AnalogSignal.o \
             ../Root/Baseline.o \
             ../Root/Detector.o \
             ../Root/displayEdit.o \
             ../Root/EnergyFilter.o \
             ../Root/ExpertMod.o \
             ../Root/Histogram.o \
             ../Root/Main.o \
             ../Root/MaxEvent.o \
             ../Root/nscope.o \
             ../Root/Popup.o \
             ../Root/PulseShape.o \
             ../Root/ScopedT.o \
             ../Root/Tau.o \
             ../Root/Table.o \
             ../Root/DSP.o \
             ../Root/TriggerFilter.o


SAMPLEOBJ = SamplePrograms/SampleBoot.o \
            SamplePrograms/SampleADCTrace.o \
            SamplePrograms/SampleMCARun.o \
            SamplePrograms/SampleAdjustOffsets.o \
            SamplePrograms/SampleListMode.o
            
# Targets
all: lib
.PHONY: all

.c.o: 
	$(CC) $(CFLAGS) $(INCFLAGS) $(CFLAG_COMPILE) $< $(CFLAG_OUT)$@

.cpp.o: 
	$(CXX) $(CXXFLAGS) $(INCFLAGS) $(CFLAG_COMPILE) $< $(CFLAG_OUT)$@

lib: $(LIBOBJS)
	$(AR) -rcv lib$(LIBNAME).a $(LIBOBJS)
.PHONY: lib


sample: lib plxlib $(SAMPLEOBJ)
	$(CC) $(LINK_PRE_FLAGS) $(LINK_FLAG_OUT)SamplePrograms/SampleBoot          SamplePrograms/SampleBoot.o          -l$(LIBNAME) $(PLX_LIB) $(SYSTEM_LIBS)
	$(CC) $(LINK_PRE_FLAGS) $(LINK_FLAG_OUT)SamplePrograms/SampleADCTrace      SamplePrograms/SampleADCTrace.o      -l$(LIBNAME) $(PLX_LIB) $(SYSTEM_LIBS)
	$(CC) $(LINK_PRE_FLAGS) $(LINK_FLAG_OUT)SamplePrograms/SampleMCARun        SamplePrograms/SampleMCARun.o        -l$(LIBNAME) $(PLX_LIB) $(SYSTEM_LIBS)
	$(CC) $(LINK_PRE_FLAGS) $(LINK_FLAG_OUT)SamplePrograms/SampleListMode      SamplePrograms/SampleListMode.o      -l$(LIBNAME) $(PLX_LIB) $(SYSTEM_LIBS)
	$(CC) $(LINK_PRE_FLAGS) $(LINK_FLAG_OUT)SamplePrograms/SampleAdjustOffsets SamplePrograms/SampleAdjustOffsets.o -l$(LIBNAME) $(PLX_LIB) $(SYSTEM_LIBS)
.PHONY: sample


nscope: lib plxlib $(NSCOPEOBJS)
	$(CXX) $(LINK_PRE_FLAGS) $(LINK_FLAG_OUT)../Root/nscope $(NSCOPEOBJS) $(ROOT_LIBS) -l$(LIBNAME) $(PLX_LIB)
.PHONY: nscope


plxdriver:
	@echo "Building PLX kernel module. Please wait ..."; \
	export PLX_SDK_DIR=$(PLX_SDK_DIR); \
	cd $(PLX_SDK_DIR)/Linux/Driver; \
	$(SHELL)./builddriver 9054 cleanall; \
	$(SHELL) ./builddriver 9054
.PHONY: plxdriver


loadplxdriver:
	@export PLX_SDK_DIR=$(PLX_SDK_DIR); \
	cd $(PLX_SDK_DIR)/Bin; \
	sudo -E $(SHELL) ./Plx_load 9054
.PHONY: loadplxdriver


unloadplxdriver:
	@export PLX_SDK_DIR=$(PLX_SDK_DIR); \
	cd $(PLX_SDK_DIR)/Bin; \
	sudo -E ./Plx_unload 9054
.PHONY: unloadplxdriver


plxlib:
	@echo "Building PLX library. Please wait ..."
	@export PLX_SDK_DIR=$(PLX_SDK_DIR); \
	cd $(PLX_SDK_DIR)/Linux/PlxApi; \
	make
	@export PLX_SDK_DIR=$(PLX_SDK_DIR); \
	cp $(PLX_SDK_DIR)/Linux/PlxApi/Library/PlxApi.a lib/libPlxApi.a
.PHONY: plxlib


clean:
	-rm $(LIBOBJS) lib$(LIBNAME).a $(SAMPLEOBJ) 
	-rm SamplePrograms/SampleBoot
	-rm SamplePrograms/SampleADCTrace
	-rm SamplePrograms/SampleMCARun
	-rm SamplePrograms/SampleListMode
	-rm SamplePrograms/SampleAdjustOffsets
	-rm $(NSCOPEOBJS) ../Root/nscope
	-rm -R $(PLX_SDK_DIR)/Linux/Driver/Plx9054
	-rm -R $(PLX_SDK_DIR)/Linux/PlxApi/Library
	-rm lib/libPlxApi.a
.PHONY: clean

