From c12bc067b10cc63ccaf5d0a8e7dc52d15f222513 Mon Sep 17 00:00:00 2001 From: star9029 Date: Wed, 16 Aug 2023 21:51:48 +0800 Subject: [PATCH] improve xmake --- 3rdParty/glfw/xmake.lua | 11 ++++-- 3rdParty/rtaudio/xmake.lua | 4 +-- 3rdParty/tinydbr/xmake.lua | 2 +- 3rdParty/zydis/xmake.lua | 4 +-- GPCS4/ImportLibs.cpp | 32 ------------------ Xmake/llvm.lua | 27 --------------- xmake.lua | 69 ++++++++++++++++++-------------------- xmake/option.lua | 5 +++ xmake/package.lua | 7 ++++ xmake/rule/debug.lua | 23 +++++++++++++ xmake/rule/module.lua | 47 ++++++++++++++++++++++++++ xmake/task.lua | 8 +++++ xmake/xmake.lua | 10 ++++++ 13 files changed, 146 insertions(+), 103 deletions(-) delete mode 100644 GPCS4/ImportLibs.cpp delete mode 100644 Xmake/llvm.lua create mode 100644 xmake/option.lua create mode 100644 xmake/package.lua create mode 100644 xmake/rule/debug.lua create mode 100644 xmake/rule/module.lua create mode 100644 xmake/task.lua create mode 100644 xmake/xmake.lua diff --git a/3rdParty/glfw/xmake.lua b/3rdParty/glfw/xmake.lua index 396f1f314..a79797b22 100644 --- a/3rdParty/glfw/xmake.lua +++ b/3rdParty/glfw/xmake.lua @@ -8,7 +8,9 @@ target("glfw") add_headerfiles("glfw3.h", "glfw3native.h") add_filegroups("include", {rootdir = "./", files = {"*.h"}}) - if is_os("windows") then + add_packages("vulkansdk") + + if is_plat("windows") then add_files("src/context.c", "src/egl_context.c", "src/init.c", @@ -25,10 +27,13 @@ target("glfw") "src/win32_window.c", "src/window.c") - add_syslinks("Shell32.lib", - "Gdi32.lib") + add_syslinks("user32", "shell32", "gdi32") else print("glfw TODO") + -- TODO: add wayland support + add_deps("libx11", "libxrandr", "libxrender", "libxinerama", "libxfixes", "libxcursor", "libxi", "libxext") + add_syslinks("dl", "pthread") + add_defines("_GLFW_X11") end -- add_filegroups("include", {rootdir = "./", files = {"src/*.h"}}) \ No newline at end of file diff --git a/3rdParty/rtaudio/xmake.lua b/3rdParty/rtaudio/xmake.lua index 7734f5ab4..6f9ea657a 100644 --- a/3rdParty/rtaudio/xmake.lua +++ b/3rdParty/rtaudio/xmake.lua @@ -8,11 +8,11 @@ target("rtaudio") add_files("*.cpp") add_filegroups("src", {rootdir = "./"}) - if is_os("windows") then + if is_plat("windows") then add_defines("__WINDOWS_WASAPI__", "_CRT_SECURE_NO_WARNINGS") - add_syslinks("Ole32.lib") + add_syslinks("ole32") else print("rtaudio TODO") end \ No newline at end of file diff --git a/3rdParty/tinydbr/xmake.lua b/3rdParty/tinydbr/xmake.lua index fc3388fbf..351a36a42 100644 --- a/3rdParty/tinydbr/xmake.lua +++ b/3rdParty/tinydbr/xmake.lua @@ -12,7 +12,7 @@ target("tinydbr") add_filegroups("src", {rootdir = "./"}) - if is_os("windows") then + if is_plat("windows") then add_headerfiles("windows/*.h") add_files("windows/*.cpp") else diff --git a/3rdParty/zydis/xmake.lua b/3rdParty/zydis/xmake.lua index 1d185f5ac..421183b47 100644 --- a/3rdParty/zydis/xmake.lua +++ b/3rdParty/zydis/xmake.lua @@ -10,7 +10,7 @@ target("zycore") add_filegroups("src", {rootdir = "dependencies/zycore/src/"}) add_filegroups("include", {rootdir = "dependencies/zycore/include/Zycore/"}) - if is_os("windows") then + if is_plat("windows") then add_defines("WINVER=0x0502", "_WIN32_WINNT=0x0502", "NTDDI_VERSION=0x05020000", @@ -39,7 +39,7 @@ target("zydis") add_files("src/**.c") add_filegroups("include", {rootdir = "include/Zydis/"}) - if is_os("windows") then + if is_plat("windows") then add_defines("WINVER=0x0502", "_WIN32_WINNT=0x0502", "NTDDI_VERSION=0x05020000", diff --git a/GPCS4/ImportLibs.cpp b/GPCS4/ImportLibs.cpp deleted file mode 100644 index 4e692a2ea..000000000 --- a/GPCS4/ImportLibs.cpp +++ /dev/null @@ -1,32 +0,0 @@ -#include "GPCS4Config.h" - -// links to static libs -// -// I don't do this in property settings page -// because I found it doesn't support relative path. -// e.g. "pthreads4w/libpthreadVCE3d.lib" -// thus I'll have to add a lib directory in that settings page -// every time I add a new lib. that's awful. - - -#ifdef GPCS4_WINDOWS -#ifdef _DEBUG - -//#pragma comment(lib, "../3rdParty/winpthreads/winpthreadsd.lib") -//#pragma comment(lib, "../3rdParty/zydis/Zydisd.lib") -//#pragma comment(lib, "../3rdParty/glfw/glfw3d.lib") -//#pragma comment(lib, "../3rdParty/rtaudio/rtaudiod.lib") -//#pragma comment(lib, "../3rdParty/tinydbr/TinyDBRd.lib") -//#pragma comment(lib, "../3rdParty/fmt/fmtd.lib") - -#else - -#pragma comment(lib, "../3rdParty/winpthreads/winpthreads.lib") -#pragma comment(lib, "../3rdParty/zydis/Zydis.lib") -#pragma comment(lib, "../3rdParty/glfw/glfw3.lib") -#pragma comment(lib, "../3rdParty/rtaudio/rtaudio.lib") -#pragma comment(lib, "../3rdParty/tinydbr/TinyDBR.lib") -#pragma comment(lib, "../3rdParty/fmt/fmt.lib") - -#endif //_DEBUG -#endif //GPCS4_WINDOWS diff --git a/Xmake/llvm.lua b/Xmake/llvm.lua deleted file mode 100644 index c2b945a84..000000000 --- a/Xmake/llvm.lua +++ /dev/null @@ -1,27 +0,0 @@ -import("core.project.project") -import("lib.detect.find_path") - -function _locate_clang_cl() - if os.is_host("windows") then - local pathes = path.splitenv(vformat("$(env PATH)")) - local clang_path = find_path("clang-cl.exe", pathes) - return clang_path - else - print("non-windows build not supported yet.") - return nil - end -end - -function find_include() - local clang_path = _locate_clang_cl() - if clang_path then - local llvm_path = path.directory(clang_path) - local lib_path = path.join(llvm_path, "lib", "clang") - local dir_table = os.dirs(lib_path .. "/*") - local include_parent = dir_table[1] - local include_path = path.join(include_parent, "include") - return include_path - else - return nil - end -end \ No newline at end of file diff --git a/xmake.lua b/xmake.lua index 670a17d49..c5c7a3745 100644 --- a/xmake.lua +++ b/xmake.lua @@ -1,14 +1,15 @@ set_project("GPCS4") +set_version("0.2.3") set_xmakever("2.8.1") add_rules("mode.release", "mode.debug") --- Only support x64 -set_plat(os.host()) -set_arch("x64") +set_allowedplats("windows") +set_allowedarchs("x64") +set_allowedmodes("debug", "release") -- Only support clang -if is_os("windows") then +if is_plat("windows") then set_toolchains("clang-cl") if is_mode("debug") then @@ -27,18 +28,19 @@ else end --- includes("3rdParty/glfw") --- includes("3rdParty/rtaudio") --- includes("3rdParty/zydis") --- includes("3rdParty/tinydbr") +includes("3rdParty/glfw") +includes("3rdParty/rtaudio") +includes("3rdParty/zydis") +includes("3rdParty/tinydbr") -if is_os("windows") then +if is_plat("windows") then includes("3rdParty/winpthreads") end +includes("xmake") target("GPCS4") - set_kind("binary") + add_rules("module.program") set_languages("c11", "cxx17") -- C/C++ Flags @@ -50,13 +52,13 @@ target("GPCS4") "-Wno-microsoft-enum-forward-reference", "-Wno-nonportable-include-path") if is_mode("release") then - add_cxxflags("-flto=thin") + -- add_cxxflags("-flto=thin") end -- C/C++ Defines add_defines("FMT_HEADER_ONLY") - if is_os("windows") then + if is_plat("windows") then add_defines("GPCS4_WINDOWS") add_defines("__PTW32_STATIC_LIB", "_CRT_SECURE_NO_WARNINGS") @@ -68,16 +70,6 @@ target("GPCS4") add_defines("GPCS4_DEBUG") end - -- Include and Souce Files - if is_os("windows") then - on_load(function (target) - import("xmake.llvm") - target:add("includedirs", llvm.find_include()) - end) - - add_includedirs("$(env VULKAN_SDK)/Include") - end - add_includedirs("GPCS4/", "GPCS4/Emulator", "GPCS4/Algorithm", @@ -96,25 +88,30 @@ target("GPCS4") add_headerfiles("GPCS4/**.h") add_files("GPCS4/**.cpp", "GPCS4/**.c") + local shader_path = "GPCS4/Graphics/Sce/Shaders" + add_rules("module.shader", {outputdir = path.join(os.projectdir(), shader_path)}) + add_files(path.join(shader_path, "*.vert")) + add_files(path.join(shader_path, "*.frag")) + -- linked libraries - if is_os("windows") then - add_linkdirs("$(env VULKAN_SDK)/Lib") - add_links("vulkan-1.lib") - add_syslinks("ksuser.lib", - "mfplat.lib", - "mfuuid.lib", - "wmcodecdspuuid.lib", - "legacy_stdio_definitions.lib", - "user32.lib") + if is_plat("windows") then + add_syslinks("ksuser", + "mfplat", + "mfuuid", + "wmcodecdspuuid", + "legacy_stdio_definitions", + "user32") else print("gpcs4 TODO") end - -- add_deps("glfw") - -- add_deps("rtaudio") - -- add_deps("zydis") - -- add_deps("tinydbr") + add_packages("vulkansdk") + + add_deps("glfw") + add_deps("rtaudio") + add_deps("zydis") + add_deps("tinydbr") - if is_os("windows") then + if is_plat("windows") then add_deps("winpthreads") end diff --git a/xmake/option.lua b/xmake/option.lua new file mode 100644 index 000000000..07df265d0 --- /dev/null +++ b/xmake/option.lua @@ -0,0 +1,5 @@ +option("test") + set_default(false) + set_showmenu(true) + set_description("Enable test (default: false)") +option_end() diff --git a/xmake/package.lua b/xmake/package.lua new file mode 100644 index 000000000..cf46ae675 --- /dev/null +++ b/xmake/package.lua @@ -0,0 +1,7 @@ +add_requires("glslang", {configs = {binaryonly = true}}) + +add_requires("vulkansdk") + +if is_mode("release") and is_plat("windows") then + add_requires("vc-ltl5") +end diff --git a/xmake/rule/debug.lua b/xmake/rule/debug.lua new file mode 100644 index 000000000..1ef9945fa --- /dev/null +++ b/xmake/rule/debug.lua @@ -0,0 +1,23 @@ +rule("debug.asan") + on_load(function (target) + if not is_mode("debug") then + return + end + + target:add("cxflags", "-fsanitize=address") + target:add("mxflags", "-fsanitize=address") + target:add("ldflags", "-fsanitize=address") + target:add("shflags", "-fsanitize=address") + + if not target:get("symbols") then + target:set("symbols", "debug") + end + + local msvc = target:toolchain("msvc") + if target:kind() == "binary" and msvc then + import("lib.detect.find_tool") + + local cl = assert(find_tool("cl", {envs = msvc:runenvs()}), "cl not found!") + target:add("runenvs", "Path", path.directory(cl.program)) + end + end) diff --git a/xmake/rule/module.lua b/xmake/rule/module.lua new file mode 100644 index 000000000..aeec9a88d --- /dev/null +++ b/xmake/rule/module.lua @@ -0,0 +1,47 @@ +rule("module.program") + on_load(function (target) + target:set("kind", "binary") + target:set("rundir", "$(projectdir)") + if target:is_plat("windows") and target:get("runtimes") == "MT" then + target:add("packages", "vc-ltl5") + end + end) + + after_link(function (target) + local enabled = target:extraconf("rules", "module.program", "upx") + if (not enabled) or (not is_mode("release")) then + return + end + + import("core.project.depend") + + local targetfile = target:targetfile() + depend.on_changed(function () + local file = path.join("build", path.filename(targetfile)) + local upx = assert(import("lib.detect.find_tool")("upx"), "upx not found!") + + os.tryrm(file) + os.vrunv(upx.program, {targetfile, "-o", file}) + end, {files = targetfile}) + end) + +rule("module.shader") + set_extensions(".vert", ".tesc", ".tese", ".geom", ".comp", ".frag", ".comp", ".mesh", ".task", ".rgen", ".rint", ".rahit", ".rchit", ".rmiss", ".rcall", ".glsl") + + before_buildcmd_file(function (target, batchcmds, sourcefile, opt) + import("lib.detect.find_tool") + + local glslc = assert(find_tool("glslc"), "glslc not found!") + local outputdir = target:extraconf("rules", "module.shader", "outputdir") or path.join(target:autogendir(), "rules", "module.shader") + local headerfile = path.join(outputdir, path.basename(sourcefile) .. ".h") + + batchcmds:show_progress(opt.progress, "${color.build.object}generating shader header %s", sourcefile) + batchcmds:mkdir(outputdir) + + batchcmds:vrunv(glslc.program, {"-mfmt=num", "-o", path(headerfile), path(sourcefile)}) + + -- add deps + batchcmds:add_depfiles(sourcefile) + batchcmds:set_depmtime(os.mtime(headerfile)) + batchcmds:set_depcache(target:dependfile(headerfile)) + end) diff --git a/xmake/task.lua b/xmake/task.lua new file mode 100644 index 000000000..e3599bf1d --- /dev/null +++ b/xmake/task.lua @@ -0,0 +1,8 @@ +task("test") + on_run(function () + os.exec("xmake f -m debug --test=y") + os.exec("xmake build -g test") + os.exec("xmake run -g test") + end) + + set_menu{} diff --git a/xmake/xmake.lua b/xmake/xmake.lua new file mode 100644 index 000000000..5a8498d82 --- /dev/null +++ b/xmake/xmake.lua @@ -0,0 +1,10 @@ +-- third party libraries +includes("package.lua") +-- project option +includes("option.lua") +-- unit test script +includes("task.lua") +-- project module config +includes("rule/module.lua") +-- project debug tool +includes("rule/debug.lua")