xmake.lua 5.6 KB

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