rust_cxx_bridge.bzl 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # Copyright Materialize, Inc. and contributors. All rights reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License in the LICENSE file at the
  6. # root of this repository, or online at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. load("@bazel_skylib//rules:run_binary.bzl", "run_binary")
  16. load("@rules_cc//cc:defs.bzl", "cc_library")
  17. """
  18. Ripped from the `cxx` repository.
  19. <https://github.com/dtolnay/cxx/blob/ea34e6350c7ff6528e9aebec1be7918dd2a2b608/tools/bazel/rust_cxx_bridge.bzl>
  20. Please see the end of the file for the license.
  21. """
  22. def rust_cxx_bridge(name, src, deps = [], headers_prefix = ""):
  23. """A macro defining a cxx bridge library
  24. Args:
  25. name (string): The name of the new target
  26. src (string): The rust source file to generate a bridge for
  27. deps (list, optional): A list of dependencies for the underlying cc_library. Defaults to [].
  28. headers_prefix (string, optional): An `include_prefix` for the generated headers.
  29. """
  30. native.alias(
  31. name = "%s/header" % name,
  32. actual = src + ".h",
  33. )
  34. native.alias(
  35. name = "%s/source" % name,
  36. actual = src + ".cc",
  37. )
  38. run_binary(
  39. name = "%s/generated" % name,
  40. srcs = [src],
  41. outs = [
  42. src + ".h",
  43. src + ".cc",
  44. ],
  45. args = [
  46. "$(location %s)" % src,
  47. "-o",
  48. "$(location %s.h)" % src,
  49. "-o",
  50. "$(location %s.cc)" % src,
  51. ],
  52. tool = "@cxxbridge-cmd//:cxxbridge-cmd",
  53. )
  54. cc_library(
  55. name = name,
  56. srcs = [src + ".cc"],
  57. deps = deps + [":%s/include" % name],
  58. )
  59. cc_library(
  60. name = "%s/include" % name,
  61. hdrs = [src + ".h"],
  62. include_prefix = headers_prefix,
  63. )
  64. """
  65. `cxx/LICENSE-MIT`
  66. Permission is hereby granted, free of charge, to any
  67. person obtaining a copy of this software and associated
  68. documentation files (the "Software"), to deal in the
  69. Software without restriction, including without
  70. limitation the rights to use, copy, modify, merge,
  71. publish, distribute, sublicense, and/or sell copies of
  72. the Software, and to permit persons to whom the Software
  73. is furnished to do so, subject to the following
  74. conditions:
  75. The above copyright notice and this permission notice
  76. shall be included in all copies or substantial portions
  77. of the Software.
  78. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
  79. ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  80. TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  81. PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
  82. SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  83. CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  84. OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
  85. IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  86. DEALINGS IN THE SOFTWARE.
  87. """