xmake.lua 5.4 KB

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