BUILD.bazel 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library")
  2. config_setting(
  3. name = "ios",
  4. constraint_values = ["@platforms//apple:ios"],
  5. )
  6. config_setting(
  7. name = "msvc",
  8. values = {
  9. "compiler": "msvc-cl",
  10. },
  11. )
  12. config_setting(
  13. name = "debug",
  14. values = {"compilation_mode": "dbg"},
  15. )
  16. config_setting(
  17. name = "release",
  18. values = {"compilation_mode": "opt"},
  19. )
  20. config_setting(
  21. name = "build_shared",
  22. define_values = {"BUILD_SHARED": "ON"},
  23. )
  24. config_setting(
  25. name = "build_static",
  26. define_values = {"BUILD_STATIC": "ON"}
  27. )
  28. config_setting(
  29. name = "build_examples",
  30. define_values = {"BUILD_EXAMPLES": "ON"},
  31. )
  32. config_setting(
  33. name = "build_unittest",
  34. define_values = {"BUILD_UNITTEST": "ON"}
  35. )
  36. config_setting(
  37. name = "with_protocol",
  38. define_values = {"WITH_PROTOCOL": "ON"}
  39. )
  40. config_setting(
  41. name = "with_evpp",
  42. define_values = {
  43. "WITH_EVPP": "ON",
  44. },
  45. visibility = [":__subpackages__"]
  46. )
  47. config_setting(
  48. name = "with_http",
  49. define_values = {
  50. "WITH_EVPP": "ON",
  51. "WITH_HTTP": "ON",
  52. },
  53. visibility = [":__subpackages__"]
  54. )
  55. config_setting(
  56. name = "with_http_server",
  57. define_values = {
  58. "WITH_EVPP": "ON",
  59. "WITH_HTTP": "ON",
  60. "WITH_HTTP_SERVER": "ON",
  61. },
  62. visibility = [":__subpackages__"]
  63. )
  64. config_setting(
  65. name = "with_http_client",
  66. define_values = {
  67. "WITH_EVPP": "ON",
  68. "WITH_HTTP": "ON",
  69. "WITH_HTTP_CLIENT": "ON",
  70. },
  71. visibility = [":__subpackages__"]
  72. )
  73. config_setting(
  74. name = "with_evpp_nghttp2",
  75. define_values = {
  76. "WITH_EVPP": "ON",
  77. "WITH_HTTP": "ON",
  78. "WITH_NGHTTP2": "ON",
  79. }
  80. )
  81. config_setting(
  82. name = "with_mqtt",
  83. define_values = {"WITH_MQTT": "ON"},
  84. visibility = [":__subpackages__"],
  85. )
  86. config_setting(
  87. name = "enable_uds",
  88. define_values = {"ENABLE_UDS": "ON"}
  89. )
  90. config_setting(
  91. name = "use_multimap",
  92. define_values = {"USE_MULTIMAP": "ON"}
  93. )
  94. config_setting(
  95. name = "with_curl",
  96. define_values = {"WITH_CURL": "ON"}
  97. )
  98. config_setting(
  99. name = "with_nghttp2",
  100. define_values = {"WITH_NGHTTP2": "ON"}
  101. )
  102. config_setting(
  103. name = "with_openssl",
  104. define_values = {"WITH_OPENSSL": "ON"}
  105. )
  106. config_setting(
  107. name = "with_gnutls",
  108. define_values = {"WITH_GNUTLS": "ON"}
  109. )
  110. config_setting(
  111. name = "with_mbedtls",
  112. define_values = {"WITH_MBEDTLS": "ON"}
  113. )
  114. config_setting(
  115. name = "with_kcp",
  116. define_values = {"WITH_KCP": "ON"}
  117. )
  118. config_setting(
  119. name = "with_wepoll",
  120. constraint_values = ["@platforms//os:windows"],
  121. define_values = {"WITH_WEPOLL": "ON"}
  122. )
  123. config_setting(
  124. name = "enable_windump",
  125. constraint_values = ["@platforms//os:windows"],
  126. define_values = {"ENABLE_WINDUMP": "ON"}
  127. )
  128. config_setting(
  129. name = "build_for_mt_dbg",
  130. constraint_values = ["@platforms//os:windows"],
  131. define_values = {
  132. "BUILD_FOR_MT": "ON",
  133. "compilation_mode": "dbg"
  134. }
  135. )
  136. config_setting(
  137. name = "build_for_mt_opt",
  138. constraint_values = ["@platforms//os:windows"],
  139. define_values = {
  140. "BUILD_FOR_MT": "ON",
  141. "compilation_mode": "opt"
  142. }
  143. )
  144. genrule(
  145. name = "config",
  146. outs = ["hconfig.h"],
  147. cmd = "($(execpath configure) && cp hconfig.h $@) || exit 1",
  148. tools = ["configure"],
  149. )
  150. HEADERS_DIRS = ["base", "ssl", "event"] + select({
  151. "with_wepoll": ["event/wepoll"],
  152. "//conditions:default": [],
  153. }) + select({
  154. "with_kcp": ["event/kcp"],
  155. "//conditions:default": [],
  156. }) + ["util"] + select({
  157. "with_protocol": ["protocol"],
  158. "//conditions:default": [],
  159. }) + select({
  160. "with_evpp": ["cpputil", "evpp"],
  161. "//conditions:default": [],
  162. }) + select({
  163. "with_http": ["http"],
  164. "//conditions:default": [],
  165. }) + select({
  166. "with_http_server": ["http/server"],
  167. "//conditions:default": [],
  168. }) + select({
  169. "with_http_client": ["http/client"],
  170. "//conditions:default": [],
  171. }) + select({
  172. "with_mqtt": ["mqtt"],
  173. "//conditions:default": [],
  174. })
  175. COPTS = select({
  176. "debug": ["-DDEBUG"],
  177. "release": ["-DNDEBUG"],
  178. "//conditions:default": [],
  179. }) + select({
  180. "enable_uds": ["-DENABLE_UDS"],
  181. "//conditions:default": [],
  182. }) + select({
  183. "use_multimap": ["-DUSE_MULTIMAP"],
  184. "//conditions:default": [],
  185. }) + select({
  186. "with_curl": ["-DWITH_CURL"],
  187. "//conditions:default": [],
  188. }) + select({
  189. "with_nghttp2": ["-DWITH_NGHTTP2"],
  190. "//conditions:default": [],
  191. }) + select({
  192. "with_openssl": ["-DWITH_OPENSSL"],
  193. "//conditions:default": [],
  194. }) + select({
  195. "with_gnutls": ["-DWITH_GNUTLS"],
  196. "//conditions:default": [],
  197. }) + select({
  198. "with_mbedtls": ["-DWITH_MBEDTLS"],
  199. "//conditions:default": [],
  200. }) + select({
  201. "@platforms//os:windows": ["-DWIN32_LEAN_AND_MEAN", "-D_CRT_SECURE_NO_WARNINGS", "-D_WIN32_WINNT=0x0600"],
  202. "//conditions:default": [],
  203. }) + select({
  204. "enable_windump": ["-DENABLE_WINDUMP"],
  205. "//conditions:default": [],
  206. }) + select({
  207. "build_for_mt_dbg": ["/MTd"],
  208. "build_for_mt_opt": ["/MT"],
  209. "//conditions:default": [],
  210. })
  211. LINKOPTS = select({
  212. "msvc": [],
  213. "//conditions:default": ["-pthread"],
  214. }) + select({
  215. "@platforms//os:linux": [
  216. "-lpthread",
  217. "-lm",
  218. "-ldl",
  219. ],
  220. "//conditions:default": [],
  221. }) + select({
  222. "@bazel_tools//tools/cpp:gcc": ["-lrt"],
  223. "//conditions:default": [],
  224. })
  225. BASE_HEADERS = [
  226. "base/hplatform.h",
  227. "base/hdef.h",
  228. "base/hatomic.h",
  229. "base/herr.h",
  230. "base/htime.h",
  231. "base/hmath.h",
  232. "base/hbase.h",
  233. "base/hversion.h",
  234. "base/hsysinfo.h",
  235. "base/hproc.h",
  236. "base/hthread.h",
  237. "base/hmutex.h",
  238. "base/hsocket.h",
  239. "base/hlog.h",
  240. "base/hbuf.h",
  241. "base/hmain.h",
  242. "base/hendian.h",
  243. ]
  244. SSL_HEADERS = [
  245. "ssl/hssl.h",
  246. ]
  247. EVENT_HEADERS = [
  248. "event/hloop.h",
  249. "event/nlog.h",
  250. ]
  251. UTIL_HEADERS = [
  252. "util/base64.h",
  253. "util/md5.h",
  254. "util/sha1.h",
  255. ]
  256. CPPUTIL_HEADERS = [
  257. "cpputil/hmap.h",
  258. "cpputil/hstring.h",
  259. "cpputil/hfile.h",
  260. "cpputil/hpath.h",
  261. "cpputil/hdir.h",
  262. "cpputil/hurl.h",
  263. "cpputil/hscope.h",
  264. "cpputil/hthreadpool.h",
  265. "cpputil/hasync.h",
  266. "cpputil/hobjectpool.h",
  267. "cpputil/ifconfig.h",
  268. "cpputil/iniparser.h",
  269. "cpputil/json.hpp",
  270. "cpputil/singleton.h",
  271. "cpputil/ThreadLocalStorage.h",
  272. ]
  273. EVPP_HEADERS = [
  274. "evpp/Buffer.h",
  275. "evpp/Channel.h",
  276. "evpp/Event.h",
  277. "evpp/EventLoop.h",
  278. "evpp/EventLoopThread.h",
  279. "evpp/EventLoopThreadPool.h",
  280. "evpp/Status.h",
  281. "evpp/TcpClient.h",
  282. "evpp/TcpServer.h",
  283. "evpp/UdpClient.h",
  284. "evpp/UdpServer.h",
  285. ]
  286. PROTOCOL_HEADERS = [
  287. "protocol/icmp.h",
  288. "protocol/dns.h",
  289. "protocol/ftp.h",
  290. "protocol/smtp.h",
  291. ]
  292. HTTP_HEADERS = [
  293. "http/httpdef.h",
  294. "http/wsdef.h",
  295. "http/http_content.h",
  296. "http/HttpMessage.h",
  297. "http/HttpParser.h",
  298. "http/WebSocketParser.h",
  299. "http/WebSocketChannel.h",
  300. ]
  301. HTTP2_HEADERS = [
  302. "http/http2def.h",
  303. "http/grpcdef.h",
  304. ]
  305. HTTP_CLIENT_HEADERS = [
  306. "http/client/HttpClient.h",
  307. "http/client/requests.h",
  308. "http/client/axios.h",
  309. "http/client/AsyncHttpClient.h",
  310. "http/client/WebSocketClient.h",
  311. ]
  312. HTTP_SERVER_HEADERS = [
  313. "http/server/HttpServer.h",
  314. "http/server/HttpService.h",
  315. "http/server/HttpContext.h",
  316. "http/server/HttpResponseWriter.h",
  317. "http/server/WebSocketServer.h",
  318. ]
  319. MQTT_HEADERS = [
  320. "mqtt/mqtt_protocol.h",
  321. "mqtt/mqtt_client.h",
  322. ]
  323. HEADERS = ["hv.h", ":config", "hexport.h"] + BASE_HEADERS + SSL_HEADERS + EVENT_HEADERS + UTIL_HEADERS + select({
  324. "with_protocol": PROTOCOL_HEADERS,
  325. "//conditions:default": [],
  326. }) + select({
  327. "with_evpp": CPPUTIL_HEADERS + EVPP_HEADERS,
  328. "//conditions:default": [],
  329. }) + select({
  330. "with_http": HTTP_HEADERS,
  331. "//conditions:default": [],
  332. }) + select({
  333. "with_evpp_nghttp2": HTTP2_HEADERS,
  334. "//conditions:default": [],
  335. }) + select({
  336. "with_http_server": HTTP_SERVER_HEADERS,
  337. "//conditions:default": [],
  338. }) + select({
  339. "with_http_client": HTTP_CLIENT_HEADERS,
  340. "//conditions:default": [],
  341. }) + select({
  342. "with_mqtt": MQTT_HEADERS,
  343. "//conditions:default": [],
  344. })
  345. CORE_SRCS = glob(
  346. ["*.h"], exclude = ["*_test.c"]
  347. ) + glob(
  348. ["base/*.h", "base/*.c", "base/*.cpp"], exclude = ["base/*_test.c"]
  349. ) + glob(
  350. ["ssl/*.h", "ssl/*.c", "ssl/*.cpp"], exclude = ["ssl/*_test.c"]
  351. ) + glob(
  352. ["event/*.h", "event/*.c", "event/*.cpp"], exclude = ["event/*_test.c"]
  353. ) + select({
  354. "with_wepoll": glob(["event/wepoll/*.h", "event/wepoll/*.c", "event/wepoll/*.cpp"], exclude = ["event/wepoll/*_test.c"]),
  355. "//conditions:default": [],
  356. }) + select({
  357. "with_kcp": glob(["event/kcp/*.h", "event/kcp/*.c", "event/kcp/*.cpp"], exclude = ["event/kcp/*_test.c"]),
  358. "//conditions:default": [],
  359. })
  360. SRCS = CORE_SRCS + glob(["util/*.h", "util/*.c", "util/*.cpp"], exclude = ["util/*_test.c"]) + select({
  361. "with_protocol": glob(["protocol/*.h", "protocol/*.c", "protocol/*.cpp"], exclude = ["protocol/*_test.c"]),
  362. "//conditions:default": [],
  363. }) + select({
  364. "with_evpp": glob(["cpputil/*.h", "cpputil/*.c", "cpputil/*.cpp", "evpp/*.h", "evpp/*.c", "evpp/*.cpp"], exclude = ["cpputil/*_test.c", "evpp/*_test.c", "evpp/*_test.cpp"]),
  365. "//conditions:default": [],
  366. }) + select({
  367. "with_http": glob(["http/*.h", "http/*.c", "http/*.cpp"], exclude = ["http/*_test.c"]),
  368. "//conditions:default": [],
  369. }) + select({
  370. "with_http_server": glob(["http/server/*.h", "http/server/*.c", "http/server/*.cpp"], exclude = ["http/server/*_test.c"]),
  371. "//conditions:default": [],
  372. }) + select({
  373. "with_http_client": glob(["http/client/*.h", "http/client/*.c", "http/client/*.cpp"], exclude = ["http/client/*_test.c"]),
  374. "//conditions:default": [],
  375. }) + select({
  376. "with_mqtt": glob(["mqtt/*.h", "mqtt/*.c", "mqtt/*.cpp"], exclude = ["mqtt/*_test.c"]),
  377. "//conditions:default": [],
  378. })
  379. cc_library(
  380. name = "hv_static",
  381. srcs = SRCS,
  382. hdrs = HEADERS,
  383. includes = HEADERS_DIRS,
  384. defines = ["HV_STATICLIB"],
  385. copts = COPTS,
  386. linkstatic = True,
  387. linkopts = LINKOPTS,
  388. )
  389. cc_library(
  390. name = "hv",
  391. srcs = SRCS,
  392. hdrs = HEADERS,
  393. includes = HEADERS_DIRS,
  394. defines = ["HV_DYNAMICLIB"],
  395. copts = COPTS,
  396. linkopts = LINKOPTS,
  397. visibility = ["//visibility:public"]
  398. )
  399. filegroup(
  400. name = "libhv",
  401. srcs = select({
  402. "build_shared": [":hv"],
  403. "//conditions:default": [],
  404. }) + select({
  405. "build_static": [":hv_static"],
  406. "//conditions:default": [],
  407. }) + select({
  408. "build_examples": ["//examples:examples"],
  409. "//conditions:default": [],
  410. })
  411. )