#
# on windows we have to link with the ws2_32 (winsock2) library as it is no longer added
# to the omnetpp system libraries by default (as of OMNeT++ 5.1)
#
ifeq ($(PLATFORM),win32.x86_64)
  LIBS += -lws2_32
  DEFINES += -DINET_EXPORT
  ENABLE_AUTO_IMPORT=-Wl,--enable-auto-import
  LDFLAGS := $(filter-out $(ENABLE_AUTO_IMPORT), $(LDFLAGS))
endif

#
# TCP implementaion using the Network Simulation Cradle (TCP_NSC feature)
#
WITH_TCP_NSC := $(shell (cd .. && ./inet_featuretool -q isenabled TCP_NSC && echo enabled) )
ifeq ($(WITH_TCP_NSC), enabled)
  NSC_VERSION= $(shell ls -d ../3rdparty/nsc* 2>/dev/null | sed 's/^.*-//')
  ifneq ($(NSC_VERSION),)
    INCLUDE_PATH += -I../3rdparty/nsc-$(NSC_VERSION)/sim
    LIBS += -Wl,-rpath,$(abspath ../3rdparty/nsc-$(NSC_VERSION))
  else
    $(error Please install NSC or disable 'TCP_NSC' feature)
  endif
endif

#
# TCP implementation using the lwIP stack
#
WITH_TCP_LWIP := $(shell (cd .. && ./inet_featuretool -q isenabled TCP_lwIP && echo enabled) )
ifeq ($(WITH_TCP_LWIP), enabled)
  INCLUDE_PATH += -Iinet/transportlayer/tcp_lwip/lwip/include -Iinet/transportlayer/tcp_lwip/lwip/include/ipv4 -Iinet/transportlayer/tcp_lwip/lwip/include/ipv6
endif

#
# pkg-config:
#
HAVE_PKGCFG := $(shell pkg-config --version 2>/dev/null)
ifeq ($(strip $(HAVE_PKGCFG)),)
  HAVE_PKGCFG := no
else
  HAVE_PKGCFG := yes
  PKGCFG := $(shell which pkg-config)
endif

#
# VoipStream feature:
#
WITH_VOIPSTREAM := $(shell (cd .. && ./inet_featuretool -q isenabled VoIPStream && echo enabled) )
ifeq ($(WITH_VOIPSTREAM), enabled)
  ifeq ($(HAVE_PKGCFG), yes)
    HAVE_FFMPEG := $(shell $(PKGCFG) --exists libavcodec libavformat libavutil && echo yes || echo no)
    ifeq ($(HAVE_FFMPEG), yes)
      LIBS += $(shell $(PKGCFG) --libs libavcodec libavformat libavutil)
      CFLAGS += $(shell $(PKGCFG) --cflags libavcodec libavformat libavutil) -DHAVE_FFMPEG
    endif
    HAVE_FFMPEG_AVRESAMPLE := $(shell $(PKGCFG) --exists libavresample && echo yes || echo no)
    ifeq ($(HAVE_FFMPEG_AVRESAMPLE), yes)
      LIBS += $(shell $(PKGCFG) --libs libavresample)
      CFLAGS += $(shell $(PKGCFG) --cflags libavresample) -DHAVE_FFMPEG_AVRESAMPLE
    endif
  endif
endif

#
# visualization feature requires (optionally) some extra osg and osgEarth libraries
#
WITH_VISUALIZERS := $(shell (cd .. && ./inet_featuretool -q isenabled visualization && echo enabled) )
ifeq ($(WITH_VISUALIZERS), enabled)
  ifeq ($(WITH_OSGEARTH), yes)
      OMNETPP_LIBS += -lOpenThreads -losg -losgText -losgDB -losgEarth -losgEarthUtil
  endif
endif

# uncomment this if you want to run the NS3 vs INET 802.11 cross validation tests in the 'tests/misc/ns3' folder.
# CFLAGS += -DNS3_VALIDATION

# disable anoying "... hides overloaded virtual function" warning
CFLAGS += -Wno-overloaded-virtual

#########################################################################
# precompiled header support for GCC and CLANG

ifeq ($(CC),gcc)
PRECOMPILED_EXT=gch
else ifeq ($(CC),clang)
PRECOMPILED_EXT=pch
else
PRECOMPILED_EXT=
endif

PRECOMPILED_HEADER=inet/common/precompiled_$(MODE).h
PRECOMPILED_HEADER_PCH=$(PRECOMPILED_HEADER).$(PRECOMPILED_EXT)
PRECOMPILED_HEADER_D=$(PRECOMPILED_HEADER_PCH:%.$(PRECOMPILED_EXT)=%.d)
CFLAGS += -include $(PRECOMPILED_HEADER)

# Main target
all-pch: | pch msgheaders all

.PHONY: pch clean-pch

pch : $(PRECOMPILED_HEADER_PCH)

$(PRECOMPILED_HEADER_PCH): $(PRECOMPILED_HEADER)
ifneq ("$(PRECOMPILED_EXT)","")
	@echo Creating precompiled header for $(CC)...
	$(Q)$(CXX) -x c++-header $(CXXFLAGS) $(filter-out -MMD -include $(PRECOMPILED_HEADER),$(COPTS)) -MD -o $@ $<
endif

clean: clean-pch clean-defines

clean-pch:
	$(Q)-rm -f $(PRECOMPILED_HEADER_D) $(PRECOMPILED_HEADER).pch $(PRECOMPILED_HEADER).gch

-include $(PRECOMPILED_HEADER_D)

# Create opp_defines.h so important WITH_* macros from OMNeT++ can be included as macros from a header file
# This helps the IDE to properly enable/disable conditional code in the editor
DEFINES_FILE=inet/opp_defines.h

msgheaders: $(DEFINES_FILE)

clean-defines:
	$(Q)-rm -f $(DEFINES_FILE)

$(DEFINES_FILE) : $(COPTS_FILE)
	@echo "// Generated file, do not edit" >$(DEFINES_FILE)
ifeq ($(WITH_OSG),yes)
	@echo "#ifndef WITH_OSG" >>$(DEFINES_FILE)
	@echo "#define WITH_OSG" >>$(DEFINES_FILE)
	@echo "#endif" >>$(DEFINES_FILE)
endif
ifeq ($(WITH_OSGEARTH),yes)
	@echo "#ifndef WITH_OSGEARTH" >>$(DEFINES_FILE)
	@echo "#define WITH_OSGEARTH" >>$(DEFINES_FILE)
	@echo "#endif" >>$(DEFINES_FILE)
endif

# dump out the actual compiler and linker command line for easier debugging
ifneq ($(MAKECMDGOALS),clean)
$(info *** COMPILING with:)
$(info $(CXX) -c $(CXXFLAGS) $(COPTS))
$(info *** LINKING with:)
$(info $(SHLIB_LD) -o $O/$(TARGET) $(AS_NEEDED_OFF) $(WHOLE_ARCHIVE_ON) $(LIBS) $(WHOLE_ARCHIVE_OFF) $(OMNETPP_LIBS) $(LDFLAGS))
$(info Building...)
endif
