540 lines
18 KiB
Lua
540 lines
18 KiB
Lua
-- ==============================================
|
||
-- Extra2D for Nintendo Switch - Xmake Build Script
|
||
-- Purpose: Build Extra2D static library and Switch demo programs
|
||
-- Platform: Nintendo Switch (ARM64)
|
||
-- Graphics: Desktop OpenGL 3.3+ via Mesa EGL
|
||
-- Audio: SDL2_mixer
|
||
-- ==============================================
|
||
|
||
set_project("Extra2D")
|
||
set_version("3.1.0")
|
||
set_languages("c++17")
|
||
set_encodings("utf-8")
|
||
add_rules("mode.debug", "mode.release")
|
||
|
||
-- ==============================================
|
||
-- 平台检测
|
||
-- ==============================================
|
||
local is_switch = is_plat("switch")
|
||
local is_mingw = is_plat("mingw")
|
||
local is_windows = is_plat("windows")
|
||
local is_pc = is_mingw or is_windows
|
||
|
||
-- ==============================================
|
||
-- PC 平台依赖包 (使用 xmake 包管理)
|
||
-- ==============================================
|
||
if is_pc then
|
||
-- 使用 xmake 包管理安装 SDL2 和 SDL2_mixer
|
||
-- 包名在 xmake-repo 中为 libsdl2 和 libsdl2_mixer
|
||
add_requires("libsdl2")
|
||
end
|
||
|
||
-- ==============================================
|
||
-- Nintendo Switch 工具链定义
|
||
-- ==============================================
|
||
toolchain("switch")
|
||
set_kind("standalone")
|
||
set_description("Nintendo Switch devkitA64 toolchain")
|
||
|
||
-- 检查 DEVKITPRO 环境变量(Windows 上使用 C:/devkitPro)
|
||
local devkitPro = "C:/devkitPro"
|
||
local devkitA64 = path.join(devkitPro, "devkitA64")
|
||
|
||
-- 设置工具链路径
|
||
set_toolset("cc", path.join(devkitA64, "bin/aarch64-none-elf-gcc.exe"))
|
||
set_toolset("cxx", path.join(devkitA64, "bin/aarch64-none-elf-g++.exe"))
|
||
set_toolset("ld", path.join(devkitA64, "bin/aarch64-none-elf-g++.exe"))
|
||
set_toolset("ar", path.join(devkitA64, "bin/aarch64-none-elf-gcc-ar.exe"))
|
||
set_toolset("strip", path.join(devkitA64, "bin/aarch64-none-elf-strip.exe"))
|
||
|
||
-- 架构标志
|
||
local arch_flags = "-march=armv8-a+crc+crypto -mtune=cortex-a57 -mtp=soft -fPIE"
|
||
add_cxflags(arch_flags)
|
||
-- 使用修复后的 switch_fix.specs 文件(使用 Windows 路径)
|
||
add_ldflags("-specs=switch_fix.specs", "-g", arch_flags)
|
||
|
||
-- 定义 Switch 平台宏
|
||
add_defines("__SWITCH__", "__NX__", "MA_SWITCH", "PFD_SWITCH")
|
||
|
||
-- SimpleIni 配置:不使用 Windows API
|
||
add_defines("SI_NO_CONVERSION")
|
||
|
||
-- libnx 路径 - 必须在工具链级别添加
|
||
add_includedirs(path.join(devkitPro, "libnx/include"))
|
||
add_linkdirs(path.join(devkitPro, "libnx/lib"))
|
||
|
||
-- portlibs 路径(EGL + 桌面 OpenGL + SDL2)
|
||
add_includedirs(path.join(devkitPro, "portlibs/switch/include"))
|
||
add_includedirs(path.join(devkitPro, "portlibs/switch/include/SDL2"))
|
||
add_linkdirs(path.join(devkitPro, "portlibs/switch/lib"))
|
||
|
||
add_syslinks("nx", "m")
|
||
|
||
-- ==============================================
|
||
-- MinGW 工具链定义 (PC 版本)
|
||
-- ==============================================
|
||
toolchain("mingw")
|
||
set_kind("standalone")
|
||
set_description("MinGW-w64 toolchain for Windows")
|
||
|
||
-- 检测 MinGW 安装路径
|
||
local mingw_path = os.getenv("MINGW_PATH") or "C:/msys64/mingw64"
|
||
|
||
-- 设置工具链路径
|
||
set_toolset("cc", path.join(mingw_path, "bin/gcc.exe"))
|
||
set_toolset("cxx", path.join(mingw_path, "bin/g++.exe"))
|
||
set_toolset("ld", path.join(mingw_path, "bin/g++.exe"))
|
||
set_toolset("ar", path.join(mingw_path, "bin/ar.exe"))
|
||
set_toolset("strip", path.join(mingw_path, "bin/strip.exe"))
|
||
|
||
-- 定义 PC 平台宏(用于替代 __SWITCH__)
|
||
add_defines("__PC_PLATFORM__", "__WINDOWS__", "_WINDOWS")
|
||
|
||
-- SimpleIni 配置
|
||
add_defines("SI_NO_CONVERSION")
|
||
|
||
-- 核心路径定义
|
||
local SRC_DIR = "Extra2D/src"
|
||
local INC_DIR = "Extra2D/include"
|
||
|
||
-- ==============================================
|
||
-- 1. Extra2D 静态库 (多平台支持)
|
||
-- ==============================================
|
||
target("extra2d")
|
||
set_kind("static")
|
||
set_basename(is_mode("debug") and "libeasy2dd" or "libeasy2d")
|
||
|
||
-- 引擎源文件
|
||
add_files(path.join(SRC_DIR, "**.cpp"))
|
||
add_files(path.join(SRC_DIR, "glad/glad.c"))
|
||
|
||
-- Squirrel 3.2 源文件
|
||
add_files("squirrel/squirrel/*.cpp")
|
||
add_files("squirrel/sqstdlib/*.cpp")
|
||
|
||
-- 公开头文件目录
|
||
add_includedirs(INC_DIR, {public = true})
|
||
|
||
-- 第三方头文件目录
|
||
add_includedirs("squirrel/include", {public = true})
|
||
|
||
-- ==============================================
|
||
-- 平台特定配置
|
||
-- ==============================================
|
||
|
||
if is_switch then
|
||
-- ==============================================
|
||
-- Nintendo Switch 平台配置
|
||
-- ==============================================
|
||
set_plat("switch")
|
||
set_arch("arm64")
|
||
set_toolchains("switch")
|
||
|
||
-- devkitPro mesa 路径(EGL + 桌面 OpenGL)
|
||
local devkitPro = "C:/devkitPro"
|
||
add_includedirs(path.join(devkitPro, "portlibs/switch/include"), {public = true})
|
||
add_linkdirs(path.join(devkitPro, "portlibs/switch/lib"))
|
||
|
||
-- 使用系统 GLES3.2 头文件 (位于 devkitPro/portlibs/switch/include)
|
||
|
||
-- 链接 EGL、OpenGL ES 3.0(mesa)和 SDL2 音频
|
||
-- 注意:链接顺序很重要!被依赖的库必须放在后面
|
||
-- 依赖链:SDL2 -> EGL -> drm_nouveau
|
||
-- GLESv2 -> glapi -> drm_nouveau
|
||
add_syslinks("SDL2_mixer", "SDL2",
|
||
"opusfile", "opus", "vorbisidec", "ogg",
|
||
"modplug", "mpg123", "FLAC",
|
||
"GLESv2",
|
||
"EGL",
|
||
"glapi",
|
||
"drm_nouveau",
|
||
{public = true})
|
||
|
||
-- 添加 Switch 兼容性头文件路径
|
||
add_includedirs(path.join(INC_DIR, "extra2d/platform"), {public = true})
|
||
|
||
elseif is_pc then
|
||
-- ==============================================
|
||
-- PC 平台配置 (MinGW/Windows)
|
||
-- ==============================================
|
||
if is_mingw then
|
||
set_plat("mingw")
|
||
set_toolchains("mingw")
|
||
end
|
||
|
||
-- 使用 xmake 包管理的 SDL2 和 SDL2_mixer
|
||
add_packages("libsdl2", {public = true})
|
||
|
||
-- 链接 PC 平台的库
|
||
-- OpenGL
|
||
add_syslinks("opengl32", "glu32", {public = true})
|
||
|
||
-- Windows 系统库
|
||
add_syslinks("winmm", "imm32", "version", "setupapi", "gdi32", "user32", "kernel32", "shell32", {public = true})
|
||
|
||
-- 添加 PC 平台兼容性定义
|
||
add_includedirs(path.join(INC_DIR, "extra2d/platform"), {public = true})
|
||
end
|
||
|
||
-- 注意:pfd (portable-file-dialogs) 暂时禁用,需要进一步修复
|
||
-- add_files(path.join(INC_DIR, "pfd/pfd_switch.cpp"))
|
||
|
||
-- Switch 特定编译标志
|
||
-- 注意:Squirrel 脚本绑定使用 dynamic_cast,需要 RTTI 支持
|
||
-- add_cxflags("-fno-rtti", {force = true})
|
||
add_cxflags("-Wno-unused-variable", "-Wno-unused-function", {force = true})
|
||
|
||
-- Squirrel 第三方库警告抑制
|
||
add_cxflags("-Wno-deprecated-copy", "-Wno-strict-aliasing", "-Wno-implicit-fallthrough", "-Wno-class-memaccess", {force = true})
|
||
|
||
-- ==============================================
|
||
-- 头文件安装配置
|
||
-- ==============================================
|
||
add_headerfiles(path.join(INC_DIR, "extra2d/**.h"), {prefixdir = "extra2d"})
|
||
-- 使用 devkitPro 的 switch-glm 替代项目自带的 GLM
|
||
-- add_headerfiles(path.join(INC_DIR, "glm/**.hpp"), {prefixdir = "glm"})
|
||
add_headerfiles(path.join(INC_DIR, "stb/**.h"), {prefixdir = "stb"})
|
||
add_headerfiles(path.join(INC_DIR, "simpleini/**.h"), {prefixdir = "simpleini"})
|
||
|
||
-- 编译器配置
|
||
add_cxxflags("-Wall", "-Wextra", {force = true})
|
||
add_cxxflags("-Wno-unused-parameter", {force = true})
|
||
if is_mode("debug") then
|
||
add_defines("E2D_DEBUG", "_DEBUG", {public = true})
|
||
add_cxxflags("-O0", "-g", {force = true})
|
||
else
|
||
add_defines("NDEBUG", {public = true})
|
||
add_cxxflags("-O2", {force = true})
|
||
end
|
||
target_end()
|
||
|
||
-- ==============================================
|
||
-- Switch 示例程序目标
|
||
-- ==============================================
|
||
if is_switch then
|
||
|
||
-- ============================================
|
||
-- Switch 简单测试程序
|
||
-- ============================================
|
||
target("hello_world")
|
||
set_kind("binary")
|
||
set_plat("switch")
|
||
set_arch("arm64")
|
||
set_toolchains("switch")
|
||
set_targetdir("build/switch")
|
||
|
||
-- 应用信息
|
||
local appTitle = "Extra2D hello_world"
|
||
local appAuthor = "Extra2D hello_world"
|
||
local appVersion = "1.0.0"
|
||
|
||
-- 添加源文件
|
||
add_files("Extra2D/examples/hello_world/main.cpp")
|
||
|
||
-- 添加头文件路径
|
||
add_includedirs("Extra2D/include")
|
||
|
||
-- 链接 extra2d 库
|
||
add_deps("extra2d")
|
||
|
||
|
||
-- 构建后生成 .nro 文件(包含 RomFS)
|
||
after_build(function (target)
|
||
local devkitPro = "C:/devkitPro"
|
||
local elf_file = target:targetfile()
|
||
local output_dir = path.directory(elf_file)
|
||
local nacp_file = path.join(output_dir, "hello_world.nacp")
|
||
local nro_file = path.join(output_dir, "hello_world.nro")
|
||
local romfs_dir = "Extra2D/examples/hello_world/romfs"
|
||
local nacptool = path.join(devkitPro, "tools/bin/nacptool.exe")
|
||
local elf2nro = path.join(devkitPro, "tools/bin/elf2nro.exe")
|
||
|
||
if not os.isfile(nacptool) then
|
||
print("Warning: nacptool not found at " .. nacptool)
|
||
return
|
||
end
|
||
if not os.isfile(elf2nro) then
|
||
print("Warning: elf2nro not found at " .. elf2nro)
|
||
return
|
||
end
|
||
|
||
-- 生成 .nacp 文件
|
||
os.vrunv(nacptool, {"--create", appTitle, appAuthor, appVersion, nacp_file})
|
||
print("Built " .. path.filename(nacp_file))
|
||
|
||
-- 生成 .nro 文件(包含 RomFS)
|
||
local romfs_absolute = path.absolute(romfs_dir)
|
||
if os.isdir(romfs_absolute) then
|
||
print("Packing RomFS from: " .. romfs_absolute)
|
||
os.vrunv(elf2nro, {elf_file, nro_file, "--nacp=" .. nacp_file, "--romfsdir=" .. romfs_absolute})
|
||
print("Built " .. path.filename(nro_file) .. " (with RomFS)")
|
||
else
|
||
os.vrunv(elf2nro, {elf_file, nro_file, "--nacp=" .. nacp_file})
|
||
print("Built " .. path.filename(nro_file))
|
||
end
|
||
end)
|
||
target_end()
|
||
|
||
-- ============================================
|
||
-- 引擎空间索引演示(1000个节点)
|
||
-- ============================================
|
||
target("spatial_index_demo")
|
||
set_kind("binary")
|
||
set_plat("switch")
|
||
set_arch("arm64")
|
||
set_toolchains("switch")
|
||
set_targetdir("build/switch")
|
||
|
||
-- 应用信息
|
||
local appTitle = "Extra2D Spatial Index Demo"
|
||
local appAuthor = "Extra2D Team"
|
||
local appVersion = "1.0.0"
|
||
|
||
-- 添加源文件
|
||
add_files("Extra2D/examples/spatial_index_demo/main.cpp")
|
||
|
||
-- 添加头文件路径
|
||
add_includedirs("Extra2D/include")
|
||
|
||
-- 链接 extra2d 库
|
||
add_deps("extra2d")
|
||
|
||
-- 生成 map 文件用于调试
|
||
add_ldflags("-Wl,-Map=build/switch/spatial_index_demo.map", {force = true})
|
||
|
||
-- 构建后生成 .nro 文件(包含 RomFS)
|
||
after_build(function (target)
|
||
local devkitPro = "C:/devkitPro"
|
||
local elf_file = target:targetfile()
|
||
local output_dir = path.directory(elf_file)
|
||
local nacp_file = path.join(output_dir, "spatial_index_demo.nacp")
|
||
local nro_file = path.join(output_dir, "spatial_index_demo.nro")
|
||
local romfs_dir = "Extra2D/examples/spatial_index_demo/romfs"
|
||
local nacptool = path.join(devkitPro, "tools/bin/nacptool.exe")
|
||
local elf2nro = path.join(devkitPro, "tools/bin/elf2nro.exe")
|
||
|
||
if not os.isfile(nacptool) then
|
||
print("Warning: nacptool not found at " .. nacptool)
|
||
return
|
||
end
|
||
if not os.isfile(elf2nro) then
|
||
print("Warning: elf2nro not found at " .. elf2nro)
|
||
return
|
||
end
|
||
|
||
-- 生成 .nacp 文件
|
||
os.vrunv(nacptool, {"--create", appTitle, appAuthor, appVersion, nacp_file})
|
||
print("Built " .. path.filename(nacp_file))
|
||
|
||
-- 生成 .nro 文件(包含 RomFS)
|
||
local romfs_absolute = path.absolute(romfs_dir)
|
||
if os.isdir(romfs_absolute) then
|
||
print("Packing RomFS from: " .. romfs_absolute)
|
||
os.vrunv(elf2nro, {elf_file, nro_file, "--nacp=" .. nacp_file, "--romfsdir=" .. romfs_absolute})
|
||
print("Built " .. path.filename(nro_file) .. " (with RomFS)")
|
||
else
|
||
os.vrunv(elf2nro, {elf_file, nro_file, "--nacp=" .. nacp_file})
|
||
print("Built " .. path.filename(nro_file))
|
||
end
|
||
end)
|
||
target_end()
|
||
|
||
|
||
-- ============================================
|
||
-- 碰撞检测演示程序
|
||
-- ============================================
|
||
target("collision_demo")
|
||
set_kind("binary")
|
||
set_plat("switch")
|
||
set_arch("arm64")
|
||
set_toolchains("switch")
|
||
set_targetdir("build/switch")
|
||
|
||
-- 应用信息
|
||
local appTitle = "Extra2D Collision Demo"
|
||
local appAuthor = "Extra2D Team"
|
||
local appVersion = "1.0.0"
|
||
|
||
-- 添加源文件
|
||
add_files("Extra2D/examples/collision_demo/main.cpp")
|
||
|
||
-- 添加头文件路径
|
||
add_includedirs("Extra2D/include")
|
||
|
||
-- 链接 extra2d 库
|
||
add_deps("extra2d")
|
||
|
||
-- 构建后生成 .nro 文件(包含 RomFS)
|
||
after_build(function (target)
|
||
local devkitPro = "C:/devkitPro"
|
||
local elf_file = target:targetfile()
|
||
local output_dir = path.directory(elf_file)
|
||
local nacp_file = path.join(output_dir, "collision_demo.nacp")
|
||
local nro_file = path.join(output_dir, "collision_demo.nro")
|
||
local romfs_dir = "Extra2D/examples/collision_demo/romfs"
|
||
local nacptool = path.join(devkitPro, "tools/bin/nacptool.exe")
|
||
local elf2nro = path.join(devkitPro, "tools/bin/elf2nro.exe")
|
||
|
||
if not os.isfile(nacptool) then
|
||
print("Warning: nacptool not found at " .. nacptool)
|
||
return
|
||
end
|
||
if not os.isfile(elf2nro) then
|
||
print("Warning: elf2nro not found at " .. elf2nro)
|
||
return
|
||
end
|
||
|
||
-- 生成 .nacp 文件
|
||
os.vrunv(nacptool, {"--create", appTitle, appAuthor, appVersion, nacp_file})
|
||
print("Built " .. path.filename(nacp_file))
|
||
|
||
-- 生成 .nro 文件(包含 RomFS)
|
||
local romfs_absolute = path.absolute(romfs_dir)
|
||
if os.isdir(romfs_absolute) then
|
||
print("Packing RomFS from: " .. romfs_absolute)
|
||
os.vrunv(elf2nro, {elf_file, nro_file, "--nacp=" .. nacp_file, "--romfsdir=" .. romfs_absolute})
|
||
print("Built " .. path.filename(nro_file) .. " (with RomFS)")
|
||
else
|
||
os.vrunv(elf2nro, {elf_file, nro_file, "--nacp=" .. nacp_file})
|
||
print("Built " .. path.filename(nro_file))
|
||
end
|
||
end)
|
||
target_end()
|
||
|
||
-- ==============================================
|
||
-- PC 示例程序目标 (MinGW)
|
||
-- ==============================================
|
||
elseif is_pc then
|
||
|
||
-- ============================================
|
||
-- PC 简单测试程序
|
||
-- ============================================
|
||
target("hello_world")
|
||
set_kind("binary")
|
||
if is_mingw then
|
||
set_plat("mingw")
|
||
set_toolchains("mingw")
|
||
end
|
||
set_targetdir("build/pc")
|
||
|
||
-- 添加源文件
|
||
add_files("Extra2D/examples/hello_world/main.cpp")
|
||
|
||
-- 添加头文件路径
|
||
add_includedirs("Extra2D/include")
|
||
|
||
-- 链接 extra2d 库
|
||
add_deps("extra2d")
|
||
|
||
-- 使用 xmake 包管理的 SDL2
|
||
add_packages("libsdl2")
|
||
|
||
-- 复制资源文件到输出目录
|
||
after_build(function (target)
|
||
local output_dir = path.directory(target:targetfile())
|
||
local romfs_dir = "Extra2D/examples/hello_world/romfs"
|
||
|
||
-- 创建 assets 目录并复制资源
|
||
local assets_dir = path.join(output_dir, "assets")
|
||
os.mkdir(assets_dir)
|
||
|
||
local font_src = path.join(romfs_dir, "assets/font.ttf")
|
||
local font_dst = path.join(assets_dir, "font.ttf")
|
||
|
||
if os.isfile(font_src) then
|
||
os.cp(font_src, font_dst)
|
||
print("Copied font to: " .. font_dst)
|
||
end
|
||
|
||
print("Built " .. path.filename(target:targetfile()))
|
||
end)
|
||
target_end()
|
||
|
||
-- ============================================
|
||
-- PC 空间索引演示
|
||
-- ============================================
|
||
target("spatial_index_demo")
|
||
set_kind("binary")
|
||
if is_mingw then
|
||
set_plat("mingw")
|
||
set_toolchains("mingw")
|
||
end
|
||
set_targetdir("build/pc")
|
||
|
||
-- 添加源文件
|
||
add_files("Extra2D/examples/spatial_index_demo/main.cpp")
|
||
|
||
-- 添加头文件路径
|
||
add_includedirs("Extra2D/include")
|
||
|
||
-- 链接 extra2d 库
|
||
add_deps("extra2d")
|
||
|
||
-- 使用 xmake 包管理的 SDL2
|
||
add_packages("libsdl2")
|
||
|
||
-- 复制资源文件
|
||
after_build(function (target)
|
||
local output_dir = path.directory(target:targetfile())
|
||
local romfs_dir = "Extra2D/examples/spatial_index_demo/romfs"
|
||
|
||
local assets_dir = path.join(output_dir, "assets")
|
||
os.mkdir(assets_dir)
|
||
|
||
local font_src = path.join(romfs_dir, "assets/font.ttf")
|
||
local font_dst = path.join(assets_dir, "font.ttf")
|
||
|
||
if os.isfile(font_src) then
|
||
os.cp(font_src, font_dst)
|
||
print("Copied font to: " .. font_dst)
|
||
end
|
||
|
||
print("Built " .. path.filename(target:targetfile()))
|
||
end)
|
||
target_end()
|
||
|
||
-- ============================================
|
||
-- PC 碰撞检测演示
|
||
-- ============================================
|
||
target("collision_demo")
|
||
set_kind("binary")
|
||
if is_mingw then
|
||
set_plat("mingw")
|
||
set_toolchains("mingw")
|
||
end
|
||
set_targetdir("build/pc")
|
||
|
||
-- 添加源文件
|
||
add_files("Extra2D/examples/collision_demo/main.cpp")
|
||
|
||
-- 添加头文件路径
|
||
add_includedirs("Extra2D/include")
|
||
|
||
-- 链接 extra2d 库
|
||
add_deps("extra2d")
|
||
|
||
-- 使用 xmake 包管理的 SDL2
|
||
add_packages("libsdl2")
|
||
|
||
-- 复制资源文件
|
||
after_build(function (target)
|
||
local output_dir = path.directory(target:targetfile())
|
||
local romfs_dir = "Extra2D/examples/collision_demo/romfs"
|
||
|
||
local assets_dir = path.join(output_dir, "assets")
|
||
os.mkdir(assets_dir)
|
||
|
||
local font_src = path.join(romfs_dir, "assets/font.ttf")
|
||
local font_dst = path.join(assets_dir, "font.ttf")
|
||
|
||
if os.isfile(font_src) then
|
||
os.cp(font_src, font_dst)
|
||
print("Copied font to: " .. font_dst)
|
||
end
|
||
|
||
print("Built " .. path.filename(target:targetfile()))
|
||
end)
|
||
target_end()
|
||
|
||
end -- end of platform-specific targets
|