#
# Copyright (c) 2016 Alibek Omarov
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#

cmake_minimum_required(VERSION 3.10)
project (CLDLL)

set (CLDLL_LIBRARY client)

add_definitions(-DCLIENT_DLL)

if(NOT MSVC)
	add_compile_options(-fno-exceptions) # GCC/Clang flag
	add_compile_options(-fno-rtti) # GCC/Clang flag
	add_compile_options(-fvisibility=hidden) # GCC/Clang flag
	add_definitions(-D_LINUX -DLINUX) # It seems enough for all non-Win32 systems
	add_definitions(-Dstricmp=strcasecmp -Dstrnicmp=strncasecmp)
	if(NOT MINGW)
		add_definitions(-D_snprintf=snprintf -D_vsnprintf=vsnprintf)
	endif()
	if(CMAKE_BUILD_TYPE MATCHES "Release"
		OR (CMAKE_BUILD_TYPE MATCHES "RelWithDebInfo"
		AND CMAKE_SYSTEM_NAME STREQUAL "Android"))
		add_compile_options(-fno-unwind-tables -fno-asynchronous-unwind-tables) # GCC/Clang flag
		add_compile_options(-fomit-frame-pointer) # GCC/Clang flag
		add_compile_options(-ftree-vectorize) # GCC/Clang flag
		add_compile_options(-funsafe-math-optimizations) # GCC/Clang flag
	endif()
else()
	add_definitions(-D_CRT_SECURE_NO_WARNINGS -D_CRT_NONSTDC_NO_DEPRECATE)
endif()

if (GOLDSOURCE_SUPPORT)
	add_definitions(-DGOLDSOURCE_SUPPORT)
endif()

if (USE_STATIC_FREEVGUI)
	add_definitions(-DINTERNAL_VGUI_SUPPORT)
endif()

set (CLDLL_SOURCES
	../dlls/dod_shared.cpp
	../dlls/wpn_shared/30cal.cpp
	../dlls/wpn_shared/amer_knife.cpp
	../dlls/wpn_shared/bar.cpp
	../dlls/wpn_shared/bazooka.cpp
	../dlls/wpn_shared/bipod.cpp
	../dlls/wpn_shared/bren.cpp
	../dlls/wpn_shared/colt.cpp
	../dlls/wpn_shared/enfield.cpp
	../dlls/wpn_shared/fg42.cpp
	../dlls/wpn_shared/garand.cpp
	../dlls/wpn_shared/ger_knife.cpp
	../dlls/wpn_shared/greasegun.cpp
	../dlls/wpn_shared/handgrenade.cpp
	../dlls/wpn_shared/handgrenade_ex.cpp
	../dlls/wpn_shared/k43.cpp
	../dlls/wpn_shared/kar.cpp
	../dlls/wpn_shared/knife.cpp
	../dlls/wpn_shared/luger.cpp
	../dlls/wpn_shared/m1carbine.cpp
	../dlls/wpn_shared/mg34.cpp
	../dlls/wpn_shared/mg42.cpp
	../dlls/wpn_shared/mp40.cpp
	../dlls/wpn_shared/mp44.cpp
	../dlls/wpn_shared/piat.cpp
	../dlls/wpn_shared/pistol.cpp
	../dlls/wpn_shared/pschreck.cpp
	../dlls/wpn_shared/scoped98k.cpp
	../dlls/wpn_shared/spade.cpp
	../dlls/wpn_shared/spring.cpp
	../dlls/wpn_shared/sten.cpp
	../dlls/wpn_shared/stickgrenade.cpp
	../dlls/wpn_shared/stickgrenade_ex.cpp
	../dlls/wpn_shared/thompson.cpp
	../dlls/wpn_shared/webley.cpp
	ev_dod.cpp
	dod/dod_baseentity.cpp
	dod/dod_events.cpp
	dod/dod_objects.cpp
	dod/dod_weapons.cpp
	ammo.cpp
	cdll_int.cpp
	CClientEnvModel.cpp
	com_weapons.cpp
	death.cpp
	demo.cpp
	dod_camera.cpp
	dod_common.cpp
	dod_crosshair.cpp
	dod_fog.cpp
	dod_icons.cpp
	dod_map.cpp
	dod_objectives.cpp
	mortarhud.cpp
	ParticleShooter.cpp
	entity.cpp
	ev_common.cpp
	events.cpp
	GameStudioModelRenderer.cpp
	hud.cpp
	hud_msg.cpp
	hud_redraw.cpp
	hud_spectator.cpp
	hud_update.cpp
	hud_safearea.cpp
	hud_vgui2print.cpp
	in_camera.cpp
	input.cpp
	input_goldsource.cpp
	input_mouse.cpp
	input_xash3d.cpp
	menu.cpp
	message.cpp
	MOTD.cpp
	parsemsg.cpp
	../pm_shared/pm_debug.cpp
	../pm_shared/pm_math.cpp
	../pm_shared/pm_shared.cpp
	saytext.cpp
	scope.cpp
	scoreboard.cpp
	status_icons.cpp
	statusbar.cpp
	studio_util.cpp
	StudioModelRenderer.cpp
	text_message.cpp
	train.cpp
	tri.cpp
	weather.cpp
	util.cpp
	view.cpp
	vgui/dod_localize.cpp
	vgui/dodviewport.cpp
	vgui/dodviewport_interface.cpp
	../public/safe_snprintf.c
	../particleman/pman_frustum.cpp
	../particleman/pman_main.cpp
	../particleman/pman_particlemem.cpp
	../particleman/pman_triangleeffect.cpp
	../3rdparty/MiniUTL/bitstring.cpp
	../3rdparty/MiniUTL/generichash.cpp
	../3rdparty/MiniUTL/strtools.cpp
	../3rdparty/MiniUTL/utlbuffer.cpp
	../3rdparty/MiniUTL/utlmemory.cpp
	../3rdparty/MiniUTL/utlstring.cpp
)

list(APPEND CLDLL_SOURCES
	vgui/vgui_int.cpp
	vgui/vgui_CustomObjects.cpp
	vgui/vgui_SchemeManager.cpp
	vgui/vgui_TeamFortressViewport.cpp
	vgui/dodbutton.cpp
	vgui/dodclassmenu.cpp
	vgui/dodclientscoreboard.cpp
	vgui/dodkeyvalues.cpp
	vgui/dodmenu.cpp
	vgui/dodhtml.cpp
	vgui/dodmotd.cpp
	vgui/dodtextwindow.cpp
	vgui/hintpanel.cpp
	vgui/mortarpanel.cpp
	vgui/dodrespage.cpp
	vgui/dodscrollpanel.cpp
	vgui/dodspectatorgui.cpp
	vgui/dodteammenu.cpp
	voice_status.cpp
	../game_shared/vgui_checkbutton2.cpp
	../game_shared/vgui_grid.cpp
	../game_shared/vgui_helpers.cpp
	../game_shared/vgui_listbox.cpp
	../game_shared/vgui_loadtga.cpp
	../game_shared/vgui_scrollbar2.cpp
	../game_shared/vgui_slider2.cpp
	../game_shared/voice_banmgr.cpp
)

if (HAVE_STRLCPY)
	add_definitions(-DHAVE_STRLCPY)
else()
	list(APPEND CLDLL_SOURCES
		../external/openbsd/strlcpy.c)
endif()

if (HAVE_STRLCAT)
	add_definitions(-DHAVE_STRLCAT)
else()
	list(APPEND CLDLL_SOURCES
		../external/openbsd/strlcat.c)
endif()

include_directories (. dod/ ../dlls ../dlls/wpn_shared ../common ../engine ../pm_shared ../game_shared ../public ../particleman ../3rdparty/MiniUTL)

if (GOLDSOURCE_SUPPORT)
	include_directories (../external/)
endif()


include_directories(../utils/vgui/include)

add_library (${CLDLL_LIBRARY} SHARED ${CLDLL_SOURCES})
target_link_libraries(${CLDLL_LIBRARY} vcs_info)

if (GOLDSOURCE_SUPPORT)
	target_link_libraries( ${CLDLL_LIBRARY} ${CMAKE_DL_LIBS} )
endif()

target_link_libraries(${CLDLL_LIBRARY} vgui)

# freevgui draws system fonts only on Windows: platform/posix/font.cpp makes none and every font becomes the debugging font with boxes
# and hex codes. vgui/dod_fontplat.cpp is a backend on stb_truetype and the TrueType files of the extras of the engine. Its objects
# go to the client, which is linked before libvgui.a, so the linker never takes the stub of freevgui. It is compiled apart from the
# client: the headers of freevgui and the original ones of VGUI cannot be included together. A freevgui that is a shared library
# keeps its own stub.
if (USE_STATIC_FREEVGUI AND NOT WIN32)
	add_library(dodfont OBJECT vgui/dod_fontplat.cpp)
	target_compile_definitions(dodfont PRIVATE DOD_FONTPLAT)
	set_target_properties(dodfont PROPERTIES
		POSITION_INDEPENDENT_CODE 1
		INCLUDE_DIRECTORIES "${CMAKE_CURRENT_SOURCE_DIR}/../freevgui;${CMAKE_CURRENT_SOURCE_DIR}/../3rdparty/mainui_cpp/font")
	target_sources(${CLDLL_LIBRARY} PRIVATE $<TARGET_OBJECTS:dodfont>)
	target_compile_definitions(${CLDLL_LIBRARY} PRIVATE DOD_FONTPLAT)
endif()

if(WIN32)
	target_link_libraries( ${CLDLL_LIBRARY} user32.lib )
	if (GOLDSOURCE_SUPPORT)
		target_link_libraries( ${CLDLL_LIBRARY} winmm.lib )
	endif()
endif()

set_target_properties (${CLDLL_LIBRARY} PROPERTIES
	POSITION_INDEPENDENT_CODE 1)

set_target_postfix(${CLDLL_LIBRARY})

if(HAVE_LTO)
	set_property(TARGET ${CLDLL_LIBRARY} PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)
endif()

install(TARGETS ${CLDLL_LIBRARY}
	RUNTIME DESTINATION "${GAMEDIR}/${CLIENT_INSTALL_DIR}/"
	LIBRARY DESTINATION "${GAMEDIR}/${CLIENT_INSTALL_DIR}/"
	PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE
		GROUP_READ GROUP_EXECUTE
		WORLD_READ WORLD_EXECUTE)

if(CMAKE_BUILD_TYPE MATCHES "Release" AND NOT WIN32)
	if(APPLE)
		# Apple strip treats -s as a symbol list file, -x keeps exported symbols
		set(STRIP_FLAGS -x)
	else()
		set(STRIP_FLAGS -s)
	endif()
	add_custom_command(TARGET ${CLDLL_LIBRARY}
			POST_BUILD DEPENDS ${CLDLL_LIBRARY}
			COMMAND ${CMAKE_STRIP} ${STRIP_FLAGS} $<TARGET_FILE:${CLDLL_LIBRARY}>)
endif()
