--
-- Premake5 script for creating a Visual Studio, XCode or CodeLite workspace for TurtleBrains project.
--   Requires Premake5 from: http://industriousone.com/
--
-- <!-- Copyright (c) 2021 Tim Beaudet - All Rights Reserved -->
-----------------------------------------------------------------------------------------------------------------------

----------------------------------------
newoption {
	trigger = "build-version",
	description = "Version being built, expecting major.minor.patch; --build-version=1.2.3",
	value = "0.0.0",
}

newoption {
	trigger = "web",
	description = "Chosen build system to override for web.",
	value = "",
}

BUILD_VERSION = "0.0.0"
if _OPTIONS["build-version"] ~= nil and _OPTIONS["build-version"] ~= '' then
	BUILD_VERSION = _OPTIONS["build-version"]
end

BUILD_VERSION_MAJOR, BUILD_VERSION_MINOR, BUILD_VERSION_PATCH =  string.match(BUILD_VERSION, "(%d+).(%d+).(%d+)")
print("TurtleBrains Version: " .. BUILD_VERSION_MAJOR .. "." .. BUILD_VERSION_MINOR .. "." .. BUILD_VERSION_PATCH)
------------------------------------------

--Documented at: http://industriousone.com/osget
local WINDOWS_SYSTEM_NAME = "windows"
local LINUX_SYSTEM_NAME = "linux"
local MACOSX_SYSTEM_NAME = "macosx"
local WEB_SYSTEM_NAME = "web"
--local MACIOS_SYSTEM_NAME = "macios"
--local ANDROID_SYSTEM_NAME = "android"

local PROJECT_NAME = "turtle_brains"
local SYSTEM_NAME = os.target() --os.get was deprecated, should look into os.target() and os.host() to see what is desired.
if _OPTIONS["web"] then
	SYSTEM_NAME = WEB_SYSTEM_NAME
end

if _ACTION == "clean" then
	os.rmdir("../build/" .. WINDOWS_SYSTEM_NAME)
	os.rmdir("../build/" .. LINUX_SYSTEM_NAME)
	os.rmdir("../build/" .. MACOSX_SYSTEM_NAME)
	os.rmdir("../build/" .. WEB_SYSTEM_NAME)
end

local SCRIPT_EXTENSION = ".sh"
local TB_PLATFORM_DEFINE = ""

--TODO: TurtleBrains: BuildSystem: It is possible we could combine the SYSTEM_NAME with the TB_PLATFORM_DEFINE and remove
--   the tb_ from the checks within scripts/post_build.sh as these two variables are very similar in nature, though be
--   aware of macos vs macosx issues that remain, and perhaps should one day be cleaned up as well. 2021-11-06
if (WINDOWS_SYSTEM_NAME == SYSTEM_NAME) then
	SCRIPT_EXTENSION = ".bat"
	TB_PLATFORM_DEFINE = "tb_windows"
elseif (MACOSX_SYSTEM_NAME == SYSTEM_NAME) then
	TB_PLATFORM_DEFINE = "tb_macos"
elseif (LINUX_SYSTEM_NAME == SYSTEM_NAME) then
	TB_PLATFORM_DEFINE = "tb_linux"
elseif (WEB_SYSTEM_NAME == SYSTEM_NAME) then
	TB_PLATFORM_DEFINE = "tb_web"
end

solution(PROJECT_NAME)
	location ("../build/" .. SYSTEM_NAME)
	
configurations { "debug", "release", "public" }
project (PROJECT_NAME)
	location ("../build/" .. SYSTEM_NAME)
	language ("C++")
	kind     ("StaticLib")
	warnings ("Extra")
	flags { "FatalCompileWarnings" }
	files { "../source/**.hpp", "../source/**.cpp", "../source/**.mm", "../source/**.c" }
	excludes { "../**/doxygen/**" }
	includedirs { "../external_libraries/includes/", "../source/" }

----------------------------------------------------------------------- Windows Platform Specifics  
	if (WINDOWS_SYSTEM_NAME == SYSTEM_NAME) then
		libdirs {
			"../external_libraries/libraries/msvc/x32",
		}

		defines { "_WINDOWS", "WIN32", "_CRT_SECURE_NO_WARNINGS", "_WINSOCK_DEPRECATED_NO_WARNINGS" }
		links { "OpenGL32", "OpenAL32", "glew32", }
		staticruntime "On"
		toolset "v143"
		characterset ("MBCS")
		buildoptions "/MP20"
	end
	
----------------------------------------------------------------------- Mac OS X Platform Specifics
	if (MACOSX_SYSTEM_NAME == SYSTEM_NAME) then
		libdirs {
			"../external_libraries/libraries/macosx/"
		}

		buildoptions "-mmacosx-version-min=10.7"
		links { "AppKit.framework", "IOKit.framework", "OpenGL.framework", "OpenAL.framework", "glew" }
	end
	
----------------------------------------------------------------------- Linux Platform Specifics 
	if (LINUX_SYSTEM_NAME == SYSTEM_NAME) then
		libdirs {
			"../build/tb_external_libraries/libraries/linux/",
			"/opt/lib/"
		}
		includedirs {
			"/usr/includes/GL/"
		}
		filter "files:**.cpp"
			buildoptions { "-std=c++11" }
		filter {}
		links { "GL", "GLEW", "openal", "X11" }
		excludes { "../**/**.mm" }
	end

----------------------------------------------------------------------- Web (Emscripten) Platform Specifics 
	if (WEB_SYSTEM_NAME == SYSTEM_NAME) then
		filter "files:**.cpp"
			buildoptions { "-std=c++11" }
		filter {}
		linkoptions "-stdlib=libc++"
		defines { "tb_without_legacy_gl", "tb_without_threading" }
	end

--------------------------------------------------------------------- Build Configuration Specifics/Overrides  
	filter "debug*"
		targetdir ("../build/" .. SYSTEM_NAME .. "/debug")
		objdir ("../build/" .. SYSTEM_NAME .. "/debug/objects" )
		defines { TB_PLATFORM_DEFINE, "tb_debug_build", "tb_with_internal_tests", "_DEBUG", "DEBUG" }
		symbols ("On")
		debugdir "../run"
		postbuildcommands { "../scripts/post_build" .. SCRIPT_EXTENSION .. " --build debug --platform " .. TB_PLATFORM_DEFINE .. " --name " .. PROJECT_NAME }

	filter "release*"
		targetdir ("../build/" .. SYSTEM_NAME .. "/release")
		objdir ("../build/" .. SYSTEM_NAME .. "/release/objects" )
		defines { TB_PLATFORM_DEFINE, "tb_release_build", "tb_without_splash_screen", "tb_with_debug_set", "tb_with_internal_tests", "NDEBUG" }
		symbols ("On")
		optimize ("On")
		debugdir "../run"
		postbuildcommands { "../scripts/post_build" .. SCRIPT_EXTENSION .. " --build release --platform " .. TB_PLATFORM_DEFINE .. " --name " .. PROJECT_NAME }

	filter "public*"
		targetdir ("../build/" .. SYSTEM_NAME .. "/public")
		objdir ("../build/" .. SYSTEM_NAME .. "/public/objects" )
		defines { TB_PLATFORM_DEFINE, "tb_public_build", "tb_without_debug_set", "NDEBUG" }
		symbols ("On")
		optimize ("On")
		debugdir "../run"
		postbuildcommands { "../scripts/post_build" .. SCRIPT_EXTENSION .. " --build public --platform " .. TB_PLATFORM_DEFINE .. " --name " .. PROJECT_NAME }



project (PROJECT_NAME .. "_headless")
	location ("../build/" .. SYSTEM_NAME)
	language ("C++")
	kind     ("StaticLib")
	warnings ("Extra")
	--defines { "tb_without_input_devices", "tb_without_networking", "tb_with_internal_tests" }
	defines { "tb_without_audio" }
	files { "../source/**.hpp", "../source/**.cpp", "../source/**.mm", "../source/**.c" }
	excludes { "../**/doxygen/**", "../**/turtle_brains/graphics/**", "../**/turtle_brains/game/**", "../**/turtle_brains/audio/**",
		"../**/turtle_brains/tests/**", "../**/turtle_brains/express/**", "../**/turtle_brains/application/**", "../**kit.hpp" }
	files { "../source/turtle_brains/game/tb_game_timer.hpp", "../source/turtle_brains/game/tb_game_timer.cpp" }
	includedirs { "../external_libraries/includes/", "../source/" }

	--TODO: TIM: Cleanup: tb_windows, tb_macos and tb_linux defines should be removed and placed in tb_configuration.hpp
	
----------------------------------------------------------------------- Windows Platform Specifics
	if (WINDOWS_SYSTEM_NAME == SYSTEM_NAME) then
		libdirs {
			"../external_libraries/libraries/msvc/x32",
		}

		defines { "_WINDOWS", "WIN32", "_CRT_SECURE_NO_WARNINGS", "_WINSOCK_DEPRECATED_NO_WARNINGS" }
		links { }
		staticruntime "On"
		toolset "v143"
		characterset ("MBCS")
		buildoptions "/MP20"
	end
	
----------------------------------------------------------------------- Mac OS X Platform Specifics
	if (MACOSX_SYSTEM_NAME == SYSTEM_NAME) then
		libdirs {
			"../external_libraries/libraries/macosx/"
		}

		buildoptions "-mmacosx-version-min=10.7"
		links { }
	end
	
----------------------------------------------------------------------- Linux Platform Specifics
	if (LINUX_SYSTEM_NAME == SYSTEM_NAME) then
		libdirs {
			"../build/tb_external_libraries/libraries/linux/",
			"/opt/lib/"
		}
		includedirs {
			"/usr/includes/GL/"
		}
		buildoptions "-std=c++11"
		links { }
		excludes { "../**/**.mm" }
	end

----------------------------------------------------------------------- Web (Emscripten) Platform Specifics
	if (WEB_SYSTEM_NAME == SYSTEM_NAME) then
		buildoptions "-std=c++11"
		linkoptions "-stdlib=libc++"
		defines { "tb_without_networking" }
	end

--------------------------------------------------------------------- Build Configuration Specifics/Overrides
	filter "debug*"
		targetdir ("../build/" .. SYSTEM_NAME .. "/debug")
		objdir ("../build/" .. SYSTEM_NAME .. "/debug/objects" )
		defines { TB_PLATFORM_DEFINE, "tb_debug_build", "tb_with_internal_tests", "_DEBUG", "DEBUG" }
		symbols ("On")
		postbuildcommands { "../scripts/post_build" .. SCRIPT_EXTENSION .. " --build debug --platform " .. TB_PLATFORM_DEFINE .. " --name " .. PROJECT_NAME .. "_headless" }

	filter "release*"
		targetdir ("../build/" .. SYSTEM_NAME .. "/release")
		objdir ("../build/" .. SYSTEM_NAME .. "/release/objects" )
		defines { TB_PLATFORM_DEFINE, "tb_release_build", "tb_without_splash_screen", "tb_with_debug_set", "tb_with_internal_tests", "NDEBUG" }
		symbols ("On")
		optimize ("On")
		postbuildcommands { "../scripts/post_build" .. SCRIPT_EXTENSION .. " --build release --platform " .. TB_PLATFORM_DEFINE .. " --name " .. PROJECT_NAME .. "_headless" }

	filter "public*"
		targetdir ("../build/" .. SYSTEM_NAME .. "/public")
		objdir ("../build/" .. SYSTEM_NAME .. "/public/objects" )
		defines { TB_PLATFORM_DEFINE, "tb_public_build", "tb_without_debug_set", "NDEBUG" }
		symbols ("Off")
		optimize ("On")
		postbuildcommands { "../scripts/post_build" .. SCRIPT_EXTENSION .. " --build public --platform " .. TB_PLATFORM_DEFINE .. " --name " .. PROJECT_NAME .. "_headless" }