/* Copyright (c) 2019-2024 Griefer@Work                                       *
 *                                                                            *
 * This software is provided 'as-is', without any express or implied          *
 * warranty. In no event will the authors be held liable for any damages      *
 * arising from the use of this software.                                     *
 *                                                                            *
 * Permission is granted to anyone to use this software for any purpose,      *
 * including commercial applications, and to alter it and redistribute it     *
 * freely, subject to the following restrictions:                             *
 *                                                                            *
 * 1. The origin of this software must not be misrepresented; you must not    *
 *    claim that you wrote the original software. If you use this software    *
 *    in a product, an acknowledgement (see the following) in the product     *
 *    documentation is required:                                              *
 *    Portions Copyright (c) 2019-2024 Griefer@Work                           *
 * 2. Altered source versions must be plainly marked as such, and must not be *
 *    misrepresented as being the original software.                          *
 * 3. This notice may not be removed or altered from any source distribution. *
 */

if (!OPT_ONLY_BUILD_LIBRARIES) {

#define DEFINE_SIMPLE_C_APPLICATION(name, ...)          \
	BEGIN GROUP("apps." name) { COMPILE, LINK, MCOPY }; \
		SET_LANGUAGE("c")                               \
		SET_OUTPUT("/bin/" name)                        \
		SOURCE({ name "/*.c" })                         \
		__VA_ARGS__                                     \
	END

#define DEFINE_SIMPLE_CXX_APPLICATION(name, ...)        \
	BEGIN GROUP("apps." name) { COMPILE, LINK, MCOPY }; \
		SET_OUTPUT("/bin/" name)                        \
		SOURCE({ name "/*.c" })                         \
		__VA_ARGS__                                     \
	END


DEFINE_SIMPLE_C_APPLICATION("init")
DEFINE_SIMPLE_C_APPLICATION("keymap", USELIB("libkeymap"))
DEFINE_SIMPLE_C_APPLICATION("getconf")
DEFINE_SIMPLE_C_APPLICATION("snake")
DEFINE_SIMPLE_C_APPLICATION("vconf")
DEFINE_SIMPLE_CXX_APPLICATION("gfx", USELIB("libvideo-gfx"))

if (TARGET_ARCH in ["i386", "x86_64"]) {
	DEFINE_SIMPLE_C_APPLICATION("scanpci", USELIB("libpciaccess"))
}

/* Super-simple, statically linked (freestanding) applications. */
#define DEFINE_FREESTANDING_C_APPLICATION(name)         \
	BEGIN GROUP("apps." name) { COMPILE, LINK, MCOPY }; \
		REMOVE_CC_FLAGS({ "-fnon-call-exceptions" })    \
		GCC_FLAGS({ "-fno-exceptions" })                \
		SET_NOSTDLIB()                                  \
		SET_LANGUAGE("c")                               \
		SET_OUTPUT("/bin/" name)                        \
		SOURCE({ "misc/" name ".c" })                   \
	END
DEFINE_FREESTANDING_C_APPLICATION("leaks")
DEFINE_FREESTANDING_C_APPLICATION("cc")
DEFINE_FREESTANDING_C_APPLICATION("true")
DEFINE_FREESTANDING_C_APPLICATION("false")
#undef DEFINE_FREESTANDING_C_APPLICATION



BEGIN GROUP("apps.insmod") { COMPILE, LINK, MCOPY }; SET_OUTPUT("/bin/insmod") SOURCE({ "insmod/insmod.c" }) END
BEGIN GROUP("apps.lsmod") { COMPILE, LINK, MCOPY }; SET_OUTPUT("/bin/lsmod") SOURCE({ "insmod/lsmod.c" }) END
BEGIN GROUP("apps.rmmod") { COMPILE, LINK, MCOPY }; SET_OUTPUT("/bin/rmmod") SOURCE({ "insmod/rmmod.c" }) END

}

BEGIN GROUP("apps.system-test") { COMPILE, LINK, MCOPY };
	DEFINE({ "__INSIDE_LIBTEST" : "1" })
	LINKER_SCRIPT("system-test/arch/" + XARCH + "/_linker" + SUFFIX + ".ld")
	SET_OUTPUT("/bin/system-test")

	/* Because libstdc++'s override for __gcc_personality_v0 makes it really
	 * hard to debug exception-related problems,  exclude it from the  link. */
	SET_NOSTDLIB()
	USELIB("libc")
	STATIC_USELIBGCC()
	STATIC_USELIB("libssp")
	OBJECT({ TARGET_BINPATH + DISK_LIBPATH + "crt0.o" });

	/* When building  the compatibility-mode  system libraries  for x86_64,  also
	 * include a regular 32-bit application /bin/system-test32 that is the 32-bit
	 * variant of /bin/system-test, but will have to be executed in compatibility
	 * mode, thus also testing the system's ability to run such applications!
	 * NOTE: Aside from having a different filename, `/bin/system-test32' is
	 *       exactly the same file as the /bin/system-test from an i386 disk
	 *       image! */
	if (TARGET_REAL_ARCH == "x86_64" && TARGET_ARCH == "i386") {
		SET_OUTPUT("/bin/system-test32")
	}

	USELIB("libdl")
	SOURCE({
		"system-test/*.c",

		/* Pull in test files from various other places. */
		"../libc/libc/*.ctest",
		"../libdl/*.ctest",
		"../libiconv/*.ctest",
		"../libinstrlen/*.ctest",
		"../libjson/*.ctest",
		"../libkeymap/*.ctest",
		"../libregex/*.ctest",

	})
END

BEGIN GROUP("apps.playground") { COMPILE, LINK, MCOPY };
	SET_OUTPUT("/bin/playground")
	if (TARGET_REAL_ARCH == "x86_64" && TARGET_ARCH == "i386") {
		SET_OUTPUT("/bin/playground32")
	}
	SOURCE({ "playground/*.c" })
END
