xmake.lua 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package("verilator")
  2. set_kind("toolchain")
  3. set_homepage("https://verilator.org")
  4. set_description("Verilator open-source SystemVerilog simulator and lint system")
  5. set_license("LGPL-3.0")
  6. add_urls("https://github.com/verilator/verilator/archive/refs/tags/$(version).tar.gz",
  7. "https://github.com/verilator/verilator.git")
  8. add_versions("v5.016", "66fc36f65033e5ec904481dd3d0df56500e90c0bfca23b2ae21b4a8d39e05ef1")
  9. add_deps("cmake")
  10. if on_check then
  11. on_check(function (package)
  12. if is_subhost("msys") and xmake:version():lt("2.9.7") then
  13. raise("package(verilator) requires xmake >= 2.9.7 on msys")
  14. end
  15. end)
  16. end
  17. on_load(function (package)
  18. if not package:is_precompiled() then
  19. package:add("deps", "flex", {kind = "library"})
  20. package:add("deps", "bison")
  21. package:add("deps", "python 3.x", {kind = "binary"})
  22. end
  23. package:mark_as_pathenv("VERILATOR_ROOT")
  24. package:addenv("VERILATOR_ROOT", ".")
  25. end)
  26. on_install(function (package)
  27. import("package.tools.cmake")
  28. if is_subhost("msys") then
  29. io.replace("CMakeLists.txt", "if(WIN32)", "if(0)", {plain = true})
  30. end
  31. local version = package:version()
  32. if version then
  33. if version:ge("5.030") then
  34. io.replace("src/CMakeLists.txt", "MSVC_RUNTIME_LIBRARY MultiThreaded$<IF:$<CONFIG:Release>,,DebugDLL>", "", {plain = true})
  35. else
  36. io.replace("src/CMakeLists.txt", "MSVC_RUNTIME_LIBRARY MultiThreaded$<IF:$<CONFIG:Release>,,DebugDLL>", "", {plain = true})
  37. if version:lt("5.028") then
  38. if is_host("linux", "bsd") then
  39. io.replace("src/CMakeLists.txt", "install(TARGETS ${verilator})",
  40. "target_link_libraries(${verilator} PRIVATE pthread)\ninstall(TARGETS ${verilator})", {plain = true})
  41. end
  42. if version:lt("5.020") then
  43. if is_host("windows") and not package:has_tool("cxx", "cl") then
  44. io.replace("src/CMakeLists.txt", "INTERPROCEDURAL_OPTIMIZATION_RELEASE TRUE", "", {plain = true})
  45. io.replace("src/CMakeLists.txt", "/bigobj", "-Wa,-mbig-obj", {plain = true})
  46. io.replace("src/CMakeLists.txt", "YY_NO_UNISTD_H", "", {plain = true})
  47. io.replace("src/CMakeLists.txt", "/STACK:10000000", "-Wl,--stack,10000000 -mconsole -lcomctl32 -DWIN_32_LEAN_AND_MEAN", {plain = true})
  48. end
  49. end
  50. end
  51. end
  52. end
  53. local configs = {
  54. "-DOBJCACHE_ENABLED=OFF",
  55. "-DDEBUG_AND_RELEASE_AND_COVERAGE=OFF",
  56. }
  57. table.insert(configs, "-DCMAKE_BUILD_TYPE=" .. (package:is_debug() and "Debug" or "Release"))
  58. table.insert(configs, "-DBUILD_SHARED_LIBS=" .. (package:config("shared") and "ON" or "OFF"))
  59. if not is_host("linux") then
  60. table.insert(configs, "-DCMAKE_CXX_STANDARD=20")
  61. end
  62. local opt = {}
  63. opt.envs = cmake.buildenvs(package)
  64. local winflexbison = package:dep("winflexbison")
  65. if winflexbison then
  66. opt.envs.WIN_FLEX_BISON = winflexbison:installdir("include")
  67. else
  68. local flex = package:dep("flex")
  69. -- https://github.com/verilator/verilator/issues/3487
  70. if is_subhost("msys") or not flex:is_system() then
  71. local includedir = flex:installdir("include")
  72. if version and version:lt("5.026") then
  73. opt.cxflags = "-I" .. includedir
  74. else
  75. table.insert(configs, "-DFLEX_INCLUDE_DIR=" .. includedir)
  76. end
  77. end
  78. end
  79. cmake.install(package, configs, opt)
  80. if is_host("linux") then
  81. if package:is_debug() then
  82. local bindir = package:installdir("bin")
  83. os.ln(path.join(bindir, "verilator_bin_dbg"), path.join(bindir, "verilator_bin"))
  84. end
  85. elseif is_host("windows") then
  86. local bindir = package:installdir("bin")
  87. local verilator = path.join(bindir, "verilator.exe")
  88. if not os.isfile(verilator) then
  89. local verilator_bin = "verilator_bin"
  90. if package:is_debug() then
  91. verilator_bin = verilator_bin .. "_dbg"
  92. end
  93. verilator_bin = path.join(bindir, verilator_bin .. ".exe")
  94. os.trycp(verilator_bin, verilator)
  95. end
  96. end
  97. end)
  98. on_test(function (package)
  99. os.vrun("verilator --version")
  100. end)