-- license:BSD-3-Clause
-- copyright-holders:MAMEdev Team

---------------------------------------------------------------------------
--
--   sdl3.lua
--
--   Rules for the building with SDL
--
---------------------------------------------------------------------------

dofile("modules.lua")


function maintargetosdoptions(_target,_subtarget)
	osdmodulestargetconf()

	if _OPTIONS["USE_DISPATCH_GL"]~="1" and _OPTIONS["MESA_INSTALL_ROOT"] then
		libdirs {
			path.join(_OPTIONS["MESA_INSTALL_ROOT"],"lib"),
		}
		linkoptions {
			"-Wl,-rpath=" .. path.join(_OPTIONS["MESA_INSTALL_ROOT"],"lib"),
		}
	end

	if _OPTIONS["NO_X11"]~="1" then
		links {
			"X11",
			"Xinerama",
		}
	else
		if _OPTIONS["targetos"]=="linux" or _OPTIONS["targetos"]=="netbsd" or _OPTIONS["targetos"]=="openbsd" then
			links {
				"EGL",
			}
		end
	end

	if _OPTIONS["NO_USE_XINPUT"]~="1" then
		links {
			"Xext",
			"Xi",
		}
	end

	if BASE_TARGETOS=="unix" and _OPTIONS["targetos"]~="macosx" and _OPTIONS["targetos"]~="android" and _OPTIONS["targetos"]~="asmjs" then
		links {
			"SDL3_ttf",
		}
		local str = backtick(pkgconfigcmd() .. " --libs fontconfig")
		addlibfromstring(str)
		addoptionsfromstring(str)
	end

	if _OPTIONS["targetos"]=="windows" then
		if _OPTIONS["USE_LIBSDL"]~="1" then
			configuration { "mingw*"}
				links {
					"SDL3",
				}
			configuration { "vs*" }
				links {
					"SDL3",
					"imm32",
					"version",
				}
			configuration { }
		else
			local str = backtick(sdlconfigcmd() .. " --libs | sed 's/ -lSDLmain//'")
			addlibfromstring(str)
			addoptionsfromstring(str)
		end
		configuration { "x32", "vs*" }
			libdirs {
				path.join(_OPTIONS["SDL_INSTALL_ROOT"],"lib","x86")
			}
		configuration { "x64", "vs*" }
			libdirs {
				path.join(_OPTIONS["SDL_INSTALL_ROOT"],"lib","x64")
			}
		configuration { }

		links {
			"dinput8",
			"gdi32",
			"imm32",
			"ole32",
			"psapi",
			"setupapi",
			"uuid",
			"version",
		}
	elseif _OPTIONS["targetos"]=="haiku" then
		links {
			"network",
			"bsd",
		}
	end

	configuration { "mingw*" or "vs*" }
		targetprefix "sdl"
		links {
			"psapi",
			"ole32",
		}
	configuration { }
end


function sdlconfigcmd()
	if _OPTIONS["SDL_PKGCONFIG_PATH"] then
		return path.join(_OPTIONS["SDL_PKGCONFIG_PATH"],"pkg-config") .. " sdl3"
	elseif not _OPTIONS["SDL_INSTALL_ROOT"] then
		return pkgconfigcmd() .. " sdl3"
	else
		return "PKG_CONFIG_LIBDIR=" .. path.join(_OPTIONS["SDL_INSTALL_ROOT"],"lib","pkgconfig") .. " " .. pkgconfigcmd() .. " sdl3"
	end
end

local function sdlpkgconfigcmd()
	if _OPTIONS["SDL_PKGCONFIG_PATH"] then
		return path.join(_OPTIONS["SDL_PKGCONFIG_PATH"],"pkg-config")
	else
		return pkgconfigcmd()
	end
end

local function macosx_uses_libsdl_by_default()
	if _OPTIONS["targetos"]~="macosx" then
		return false
	elseif _OPTIONS["SDL_INSTALL_ROOT"] then
		return true
	else
		return os.execute(sdlpkgconfigcmd() .. " --exists sdl3")
	end
end


newoption {
	trigger = "MESA_INSTALL_ROOT",
	description = "link against specific GL-Library - also adds rpath to executable (overridden by USE_DISPATCH_GL)",
}

newoption {
	trigger = "SDL_INI_PATH",
	description = "Default search path for .ini files",
}

newoption {
	trigger = "NO_X11",
	description = "Disable use of X11",
	allowed = {
		{ "0",  "Enable X11"  },
		{ "1",  "Disable X11" },
	},
}

if not _OPTIONS["NO_X11"] then
	if _OPTIONS["targetos"]=="windows" or _OPTIONS["targetos"]=="macosx" or _OPTIONS["targetos"]=="haiku" or _OPTIONS["targetos"]=="asmjs" or _OPTIONS["targetos"]=="android" then
		_OPTIONS["NO_X11"] = "1"
	else
		_OPTIONS["NO_X11"] = "0"
	end
end

newoption {
	trigger = "NO_USE_XINPUT",
	description = "Disable use of Xinput",
	allowed = {
		{ "0",  "Enable Xinput"  },
		{ "1",  "Disable Xinput" },
	},
}

if not _OPTIONS["NO_USE_XINPUT"] then
	if _OPTIONS["targetos"]=="windows" or _OPTIONS["targetos"]=="macosx" or _OPTIONS["targetos"]=="haiku" or _OPTIONS["targetos"]=="asmjs" or _OPTIONS["targetos"]=="android" then
		_OPTIONS["NO_USE_XINPUT"] = "1"
	else
		_OPTIONS["NO_USE_XINPUT"] = "0"
	end
end

newoption {
	trigger = "NO_USE_XINPUT_WII_LIGHTGUN_HACK",
	description = "Disable use of Xinput Wii Lightgun Hack",
	allowed = {
		{ "0",  "Enable Xinput Wii Lightgun Hack"  },
		{ "1",  "Disable Xinput Wii Lightgun Hack" },
	},
}

if not _OPTIONS["NO_USE_XINPUT_WII_LIGHTGUN_HACK"] then
	_OPTIONS["NO_USE_XINPUT_WII_LIGHTGUN_HACK"] = "1"
end

newoption {
	trigger = "SDL_INSTALL_ROOT",
	description = "Equivalent to the ./configure --prefix=<path>",
}

newoption {
	trigger = "SDL_PKGCONFIG_PATH",
	description = "Location of pkg-config command that knows about SDL.  Useful for non-default Homebrew installs.",
}

newoption {
	trigger = "SDL_FRAMEWORK_PATH",
	description = "Location of SDL framework for custom OS X installations",
}

local framework_path_default = (_OPTIONS["SDL_FRAMEWORK_PATH"] == nil)

-- SDL 3's framework now contains all Apple platforms in a single framework, so we need to
-- specifically ask for the macOS version.
if not _OPTIONS["SDL_FRAMEWORK_PATH"] then
	_OPTIONS["SDL_FRAMEWORK_PATH"] = "/Library/Frameworks/SDL3.xcframework/macos-arm64_x86_64/"
end

newoption {
	trigger = "USE_LIBSDL",
	description = "Use SDL library on OS (rather than framework/dll)",
	allowed = {
		{ "0",  "Use framework/dll"  },
		{ "1",  "Use library" },
	},
}

local use_libsdl_default = (_OPTIONS["USE_LIBSDL"] == nil)

if not _OPTIONS["USE_LIBSDL"] then
	_OPTIONS["USE_LIBSDL"] = "0"
end

-- Prefer pkg-config/library installs on macOS when available, as that is the
-- common Homebrew layout.  Preserve explicit framework and USE_LIBSDL choices.
if use_libsdl_default and framework_path_default and macosx_uses_libsdl_by_default() then
	_OPTIONS["USE_LIBSDL"] = "1"
end


BASE_TARGETOS       = "unix"
SDLOS_TARGETOS      = "unix"
if _OPTIONS["targetos"]=="windows" then
	BASE_TARGETOS       = "win32"
	SDLOS_TARGETOS      = "win32"
elseif _OPTIONS["targetos"]=="macosx" then
	SDLOS_TARGETOS      = "macosx"
end

if BASE_TARGETOS=="unix" then
	if _OPTIONS["targetos"]=="macosx" then
		local os_version = str_to_version(backtick("sw_vers -productVersion"))

		links {
			"Cocoa.framework",
		}
		linkoptions {
			"-framework QuartzCore",
			"-framework OpenGL",
			"-framework IOKit",
		}


		if os_version>=101100 then
			linkoptions {
				"-weak_framework Metal",
			}
		end
		if _OPTIONS["USE_LIBSDL"]~="1" then
			linkoptions {
				"-rpath " .. _OPTIONS["SDL_FRAMEWORK_PATH"],
				"-F" .. _OPTIONS["SDL_FRAMEWORK_PATH"],
			}
			links {
				"SDL3.framework",
			}
		else
			local str = backtick(sdlconfigcmd() .. " --libs --static | sed 's/-lSDLmain//'")
			addlibfromstring(str)
			addoptionsfromstring(str)
		end
	else
		if _OPTIONS["NO_X11"]=="1" then
			_OPTIONS["USE_QTDEBUG"] = "0"
		else
			libdirs {
				"/usr/X11/lib",
				"/usr/X11R6/lib",
				"/usr/openwin/lib",
			}
		end
		local str = backtick(sdlconfigcmd() .. " --libs")
		addlibfromstring(str)
		addoptionsfromstring(str)

		if _OPTIONS["targetos"]~="haiku" and _OPTIONS["targetos"]~="android" then
			links {
				"m",
				"pthread",
			}
			if _OPTIONS["targetos"]=="solaris" then
				links {
					"socket",
					"nsl",
				}
			elseif _OPTIONS["targetos"]~="asmjs" then
				links {
					"util",
				}
			end
		end
	end
end

project ("qtdbg_" .. _OPTIONS["osd"])
	uuid (os.uuid("qtdbg_" .. _OPTIONS["osd"]))
	kind (LIBTYPE)

	dofile("sdl3_cfg.lua")
	includedirs {
		MAME_DIR .. "src/emu",
		MAME_DIR .. "src/devices", -- accessing imagedev from debugger
		MAME_DIR .. "src/osd",
		MAME_DIR .. "src/lib",
		MAME_DIR .. "src/lib/util",
		MAME_DIR .. "src/osd/modules/render",
		MAME_DIR .. "3rdparty",
	}
	configuration { "linux-* or freebsd" }
		buildoptions {
			"-fPIC",
		}
	configuration { }

	qtdebuggerbuild()

project ("osd_" .. _OPTIONS["osd"])
	uuid (os.uuid("osd_" .. _OPTIONS["osd"]))
	kind (LIBTYPE)

	dofile("sdl3_cfg.lua")
	osdmodulesbuild()

	includedirs {
		MAME_DIR .. "src/emu",
		MAME_DIR .. "src/devices", -- accessing imagedev from debugger
		MAME_DIR .. "src/osd",
		MAME_DIR .. "src/lib",
		MAME_DIR .. "src/lib/util",
		MAME_DIR .. "src/osd/modules/file",
		MAME_DIR .. "src/osd/modules/render",
		MAME_DIR .. "3rdparty",
		MAME_DIR .. "src/osd/sdl3",
	}
	addincludesfromstring(backtick(sdlconfigcmd() .. " --cflags"))

	if _OPTIONS["targetos"]=="macosx" then
		files {
			MAME_DIR .. "src/osd/modules/debugger/debugosx.mm",
			MAME_DIR .. "src/osd/modules/debugger/osx/breakpointsview.mm",
			MAME_DIR .. "src/osd/modules/debugger/osx/breakpointsview.h",
			MAME_DIR .. "src/osd/modules/debugger/osx/consoleview.mm",
			MAME_DIR .. "src/osd/modules/debugger/osx/consoleview.h",
			MAME_DIR .. "src/osd/modules/debugger/osx/debugcommandhistory.mm",
			MAME_DIR .. "src/osd/modules/debugger/osx/debugcommandhistory.h",
			MAME_DIR .. "src/osd/modules/debugger/osx/debugconsole.mm",
			MAME_DIR .. "src/osd/modules/debugger/osx/debugconsole.h",
			MAME_DIR .. "src/osd/modules/debugger/osx/debugview.mm",
			MAME_DIR .. "src/osd/modules/debugger/osx/debugview.h",
			MAME_DIR .. "src/osd/modules/debugger/osx/debugwindowhandler.mm",
			MAME_DIR .. "src/osd/modules/debugger/osx/debugwindowhandler.h",
			MAME_DIR .. "src/osd/modules/debugger/osx/deviceinfoviewer.mm",
			MAME_DIR .. "src/osd/modules/debugger/osx/deviceinfoviewer.h",
			MAME_DIR .. "src/osd/modules/debugger/osx/devicesviewer.mm",
			MAME_DIR .. "src/osd/modules/debugger/osx/devicesviewer.h",
			MAME_DIR .. "src/osd/modules/debugger/osx/disassemblyview.mm",
			MAME_DIR .. "src/osd/modules/debugger/osx/disassemblyviewer.mm",
			MAME_DIR .. "src/osd/modules/debugger/osx/disassemblyviewer.h",
			MAME_DIR .. "src/osd/modules/debugger/osx/errorlogview.mm",
			MAME_DIR .. "src/osd/modules/debugger/osx/errorlogview.h",
			MAME_DIR .. "src/osd/modules/debugger/osx/exceptionpointsview.mm",
			MAME_DIR .. "src/osd/modules/debugger/osx/exceptionpointsview.h",
			MAME_DIR .. "src/osd/modules/debugger/osx/disassemblyview.h",
			MAME_DIR .. "src/osd/modules/debugger/osx/errorlogviewer.mm",
			MAME_DIR .. "src/osd/modules/debugger/osx/errorlogviewer.h",
			MAME_DIR .. "src/osd/modules/debugger/osx/memoryview.mm",
			MAME_DIR .. "src/osd/modules/debugger/osx/memoryview.h",
			MAME_DIR .. "src/osd/modules/debugger/osx/memoryviewer.mm",
			MAME_DIR .. "src/osd/modules/debugger/osx/memoryviewer.h",
			MAME_DIR .. "src/osd/modules/debugger/osx/pointsviewer.mm",
			MAME_DIR .. "src/osd/modules/debugger/osx/pointsviewer.h",
			MAME_DIR .. "src/osd/modules/debugger/osx/registerpointsview.mm",
			MAME_DIR .. "src/osd/modules/debugger/osx/registerpointsview.h",
			MAME_DIR .. "src/osd/modules/debugger/osx/registersview.mm",
			MAME_DIR .. "src/osd/modules/debugger/osx/registersview.h",
			MAME_DIR .. "src/osd/modules/debugger/osx/watchpointsview.mm",
			MAME_DIR .. "src/osd/modules/debugger/osx/watchpointsview.h",
			MAME_DIR .. "src/osd/modules/debugger/osx/debugosx.h",
		}
	end

	files {
		MAME_DIR .. "src/osd/osdepend.h",
		MAME_DIR .. "src/osd/modules/osdwindow.cpp",
		MAME_DIR .. "src/osd/modules/osdwindow.h",
		MAME_DIR .. "src/osd/sdl3/osdsdl.cpp",
		MAME_DIR .. "src/osd/sdl3/osdsdl.h",
		MAME_DIR .. "src/osd/sdl3/sdlmain.cpp",
		MAME_DIR .. "src/osd/sdl3/sdlopts.cpp",
		MAME_DIR .. "src/osd/sdl3/sdlopts.h",
		MAME_DIR .. "src/osd/sdl3/sdlprefix.h",
		MAME_DIR .. "src/osd/sdl3/video.cpp",
		MAME_DIR .. "src/osd/sdl3/window.cpp",
		MAME_DIR .. "src/osd/sdl3/window.h",
	}

project ("ocore_" .. _OPTIONS["osd"])
	uuid (os.uuid("ocore_" .. _OPTIONS["osd"]))
	kind (LIBTYPE)

	removeflags {
		"SingleOutputDir",
	}

	dofile("sdl3_cfg.lua")

	includedirs {
		MAME_DIR .. "src/emu",
		MAME_DIR .. "src/osd",
		MAME_DIR .. "src/lib",
		MAME_DIR .. "src/lib/util",
		MAME_DIR .. "src/osd/sdl3",
	}
	addincludesfromstring(backtick(sdlconfigcmd() .. " --cflags"))

	files {
		MAME_DIR .. "src/osd/osdcore.cpp",
		MAME_DIR .. "src/osd/osdcore.h",
		MAME_DIR .. "src/osd/osdfile.h",
		MAME_DIR .. "src/osd/strconv.cpp",
		MAME_DIR .. "src/osd/strconv.h",
		MAME_DIR .. "src/osd/osdsync.cpp",
		MAME_DIR .. "src/osd/osdsync.h",
		MAME_DIR .. "src/osd/modules/osdmodule.cpp",
		MAME_DIR .. "src/osd/modules/osdmodule.h",
		MAME_DIR .. "src/osd/modules/lib/osdlib_" .. SDLOS_TARGETOS .. ".cpp",
		MAME_DIR .. "src/osd/modules/lib/osdlib.h",
	}

	if BASE_TARGETOS=="unix" then
		files {
			MAME_DIR .. "src/osd/modules/file/posixdir.cpp",
			MAME_DIR .. "src/osd/modules/file/posixfile.cpp",
			MAME_DIR .. "src/osd/modules/file/posixfile.h",
			MAME_DIR .. "src/osd/modules/file/posixptty.cpp",
			MAME_DIR .. "src/osd/modules/file/posixsocket.cpp",
		}
	elseif BASE_TARGETOS=="win32" then
		includedirs {
			MAME_DIR .. "src/osd/windows",
		}
		files {
			MAME_DIR .. "src/osd/modules/file/windir.cpp",
			MAME_DIR .. "src/osd/modules/file/winfile.cpp",
			MAME_DIR .. "src/osd/modules/file/winfile.h",
			MAME_DIR .. "src/osd/modules/file/winptty.cpp",
			MAME_DIR .. "src/osd/modules/file/winsocket.cpp",
			MAME_DIR .. "src/osd/windows/winutil.cpp", -- FIXME put the necessary functions somewhere more appropriate?
			MAME_DIR .. "src/osd/windows/winutil.h",
		}
	else
		files {
			MAME_DIR .. "src/osd/modules/file/stdfile.cpp",
		}
	end
