FLAGS = $(TOPFLAGS)
VERSION = -g 
#LIB_TYPE = {static,shared}
LIB_TYPE = $(LIB)
CXX = $(COMPILER)

ifeq ($(LIB_TYPE),shared)
   LIB_EXT = so
else
   LIB_EXT = a
endif

OBJECTS = cweet_scanner.o FileFinder.o utils/result.o scanners/scanner.o scanners/cwe1280.o scanners/cwe1271.o scanners/cwe1234.o scanners/cwe1245.o scanners/cwe1314.o 
OBJ = cweet_scanner.o FileFinder.o result.o scanner.o cwe1280.o cwe1271.o cwe1234.o cwe1245.o cwe1314.o
ifneq (,$(findstring -DUSE_COMREAD,$(TOPFLAGS)))
    INCLUDE = commands
    LINKDIRS = commands
endif


INCLUDE += verilog
LINKDIRS += verilog


INCLUDE += hdl_file_sort
LINKDIRS += hdl_file_sort


INCLUDE += hier_tree
LINKDIRS += hier_tree


INCLUDE += verilog_nl
LINKDIRS += verilog_nl

INCLUDE += synlib
LINKDIRS += synlib




INCLUDE += database
LINKDIRS += database


INCLUDE += util containers
LINKDIRS += util containers

HEADERS = FileFinder.h utils/result.h scanners/scanner.h scanners/cwe1280.h scanners/cwe1271.h scanners/cwe1234.h scanners/cwe1245.h scanners/cwe1314.h

COMPILEDIRS = 

# 'libxnet' does not seem to be available on older SunOS5 systems.
# so use the finer set of many small .so files.
# 
# -pg version needs -ldl if -xnet is used only.
#
ifeq ($(shell uname),Darwin)
  OS = mac
  EXTLIBS = $(STATIC) -ldl -lm -lc
endif
ifeq ($(shell uname),SunOS)
   EXTLIBS = -ldl -lsocket -lnsl -lm -lc
   OS = solaris
endif
ifeq ($(shell uname),Linux)
   EXTLIBS = -ldl -lnsl -lm -lc
   OS = linux
endif
LINKTARGET = cweet_scanner-$(OS)

# Link against -lz if compile flag VERIFIC_ENABLE_ZLIB is enabled (util/VerificSystem.h)
ifneq ($(strip $(shell grep -l "^\#define VERIFIC_ENABLE_ZLIB" ../../util/VerificSystem.h)),)
    EXTLIBS += -lz
endif



##########################################################################
# Stable for each Makefile

CFLAGS = -Wall --std=c++17
CFLAGS += $(FLAGS)
CFLAGS += -w
CC = gcc
ifeq ($(CXX),)
    CXX = g++
endif

##########################################################################
# Now the make rules 

default: all

.SUFFIXES: .c .cpp .o 

.cpp.o:
	$(CXX) -c -g -I. $(patsubst %,-I../../%,$(INCLUDE)) $(VERSION) $(CFLAGS) $<


lib:
ifdef ($(COMPILEDIRS)
	for p in $(COMPILEDIRS); do \
		$(MAKE) -C ../$$p VERSION=$(VERSION)  lib ;\
	done
endif

$(LINKTARGET) : $(OBJECTS) lib 
	$(CXX) $(VERSION) -o $(LINKTARGET) $(OBJ) $(patsubst %,../../%/*-$(OS).$(LIB_EXT),$(LINKDIRS)) $(EXTLIBS)  $(CFLAGS) 

all : $(LINKTARGET) 

# Header file dependency : All my headers, and all included dir's headers
$(OBJECTS) : $(HEADERS) $(patsubst %,../../%/*.h,$(INCLUDE)) 
 
clean:
	rm -f $(LINKTARGET) $(OBJ)

