xmake.lua 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package("box2d")
  2. set_homepage("https://box2d.org")
  3. set_description("A 2D Physics Engine for Games")
  4. set_license("MIT")
  5. set_urls("https://github.com/erincatto/box2d/archive/refs/tags/$(version).tar.gz",
  6. "https://github.com/erincatto/box2d.git")
  7. add_versions("v3.1.1", "fb6ef914b50f4312d7d921a600eabc12318bb3c55a0b8c0b90608fa4488ef2e4")
  8. add_versions("v3.1.0", "7fac19801485efb31ee3745b2284d9d4601f9e8138a3383a7b0df6d788ea5785")
  9. add_versions("v3.0.0", "64ad759006cd2377c99367f51fb36942b57f0e9ad690ed41548dd620e6f6c8b1")
  10. add_versions("v2.4.2", "85b9b104d256c985e6e244b4227d447897fac429071cc114e5cc819dae848852")
  11. add_patches("3.1.0", "patches/3.1.0/cmake.patch", "22200ef3af453ef3a81735ad3700249110e3f26a7a68d712dbb3ad96309ce02b")
  12. add_configs("avx2", {description = "Enable AVX2.", default = false, type = "boolean"})
  13. if is_plat("linux", "bsd") then
  14. add_syslinks("pthread")
  15. end
  16. add_deps("cmake")
  17. if on_check then
  18. on_check(function (package)
  19. if package:gitref() or package:version():ge("3.0.0") then
  20. if package:check_sizeof("void*") == "4" then
  21. raise("package(box2d >=3.0.0) unsupported 32-bit")
  22. end
  23. if package:is_plat("windows") then
  24. assert(not package:is_arch("arm64.*"), "package(box2d/arm >=3.0.0) Unsupported architecture.")
  25. end
  26. if package:is_plat("android") then
  27. local ndk = package:toolchain("ndk")
  28. local ndk_sdkver = ndk:config("ndk_sdkver")
  29. assert(ndk_sdkver and tonumber(ndk_sdkver) >= 28, "package(box2d >=3.0.0) requires ndk api level >= 28")
  30. end
  31. local configs = {languages = "c11"}
  32. if package:has_tool("cc", "cl") then
  33. configs.cflags = "/experimental:c11atomics"
  34. end
  35. assert(package:has_cincludes("stdatomic.h", {configs = configs}),
  36. "package(box2d >=3.0.0) Requires at least C11 and stdatomic.h")
  37. end
  38. end)
  39. end
  40. on_install("!bsd", function (package)
  41. if package:config("shared") then
  42. package:add("defines", "B2_SHARED")
  43. end
  44. if package:is_plat("windows") and package:is_debug() then
  45. package:add("defines", "B2_ENABLE_ASSERT")
  46. end
  47. io.replace("CMakeLists.txt", [[set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")]], "", {plain = true})
  48. local configs = {
  49. "-DBOX2D_BUILD_UNIT_TESTS=OFF",
  50. "-DBOX2D_BUILD_TESTBED=OFF",
  51. "-DBOX2D_SAMPLES=OFF",
  52. "-DBOX2D_UNIT_TESTS=OFF",
  53. "-DBOX2D_VALIDATE=OFF",
  54. "--compile-no-warning-as-error",
  55. }
  56. table.insert(configs, "-DCMAKE_BUILD_TYPE=" .. (package:is_debug() and "Debug" or "Release"))
  57. table.insert(configs, "-DBUILD_SHARED_LIBS=" .. (package:config("shared") and "ON" or "OFF"))
  58. table.insert(configs, "-DBOX2D_SANITIZE=" .. (package:config("asan") and "ON" or "OFF"))
  59. table.insert(configs, "-DBOX2D_AVX2=" .. (package:config("avx2") and "ON" or "OFF"))
  60. os.mkdir(path.join(package:buildir(), "src/pdb"))
  61. import("package.tools.cmake").install(package, configs)
  62. if package:gitref() or package:version():ge("3.0.0") then
  63. os.cp("include", package:installdir())
  64. end
  65. if package:config("shared") then
  66. os.trycp(path.join(package:buildir(), "bin/box2d.pdb"), package:installdir("bin"))
  67. else
  68. os.trycp(path.join(package:buildir(), "bin/box2d.pdb"), package:installdir("lib"))
  69. end
  70. end)
  71. on_test(function (package)
  72. if package:gitref() or package:version():ge("3.0.0") then
  73. assert(package:check_csnippets({test = [[
  74. void test(int argc, char** argv) {
  75. b2WorldDef worldDef = b2DefaultWorldDef();
  76. b2WorldId worldId = b2CreateWorld(&worldDef);
  77. b2BodyDef bodyDef = b2DefaultBodyDef();
  78. b2BodyId bodyId = b2CreateBody(worldId, &bodyDef);
  79. b2Polygon box = b2MakeBox(1.0f, 1.0f);
  80. b2ShapeDef shapeDef = b2DefaultShapeDef();
  81. b2CreatePolygonShape(bodyId, &shapeDef, &box);
  82. float timeStep = 1.0f / 60.0f;
  83. int subStepCount = 4;
  84. b2World_Step(worldId, timeStep, subStepCount);
  85. b2Vec2 position = b2Body_GetPosition(bodyId);
  86. b2Rot rotation = b2Body_GetRotation(bodyId);
  87. b2DestroyWorld(worldId);
  88. }
  89. ]]}, {configs = {languages = "c11"}, includes = "box2d/box2d.h"}))
  90. else
  91. assert(package:check_cxxsnippets({test = [[
  92. void test(int argc, char** argv) {
  93. b2World world(b2Vec2(0.0f, -10.0f));
  94. b2BodyDef bodyDef;
  95. b2Body* body = world.CreateBody(&bodyDef);
  96. b2PolygonShape box;
  97. box.SetAsBox(1.0f, 1.0f);
  98. b2FixtureDef fixtureDef;
  99. fixtureDef.shape = &box;
  100. body->CreateFixture(&fixtureDef);
  101. float timeStep = 1.0f / 60.0f;
  102. int32 velocityIterations = 6;
  103. int32 positionIterations = 2;
  104. world.Step(timeStep, velocityIterations, positionIterations);
  105. b2Vec2 position = body->GetPosition();
  106. float angle = body->GetAngle();
  107. }
  108. ]]}, {configs = {languages = "c++11"}, includes = "box2d/box2d.h"}))
  109. end
  110. end)