BUILD.openssl.bazel 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. """
  16. A build file for OpenSSL that is compatible with the Rust crate openssl-sys.
  17. Adapted from the OpenSSL build file in the `rules_rust` examples:
  18. https://github.com/bazelbuild/rules_rust/blob/21eed19188c0359d72f9a508a0c0e7040ff20070/examples/crate_universe/multi_package/3rdparty/BUILD.openssl.bazel
  19. """
  20. load("@aspect_bazel_lib//lib:copy_file.bzl", "copy_file")
  21. load("@aspect_bazel_lib//lib:copy_to_directory.bzl", "copy_to_directory")
  22. load("@bazel_skylib//rules:select_file.bzl", "select_file")
  23. load("@rules_foreign_cc//foreign_cc:defs.bzl", "configure_make")
  24. # Read https://wiki.openssl.org/index.php/Compilation_and_Installation
  25. filegroup(
  26. name = "all_srcs",
  27. srcs = glob(
  28. include = ["**"],
  29. exclude = ["*.bazel"],
  30. ),
  31. )
  32. # Copied from `openssl-src-rs`
  33. #
  34. # See https://github.com/alexcrichton/openssl-src-rs/blob/599f48490e62bcd03ba02d8c9357c9a1fed1c255/src/lib.rs#L179
  35. #
  36. # TODO(parkmycar): Lint that these options are the same ones enabled by `openssl-src`.
  37. CONFIGURE_OPTIONS = [
  38. # No shared objects, only static libraries.
  39. "no-dso",
  40. "no-shared",
  41. # Should be off by default, but let's be extra sure.
  42. "no-ssl3",
  43. # No need to build tests, we don't run them.
  44. "no-tests",
  45. # Nothing related to zlib.
  46. "no-comp",
  47. "no-zlib",
  48. "no-zlib-dynamic",
  49. # Avoid multilib-postfix for build targets that specify it
  50. "--libdir=lib",
  51. # No weak crypto.
  52. "no-md2",
  53. "no-rc5",
  54. "no-weak-ssl-ciphers",
  55. # Additional flags.
  56. "no-camellia",
  57. "no-idea",
  58. "no-seed",
  59. ]
  60. LIB_NAME = "openssl"
  61. MAKE_TARGETS = [
  62. "build_libs",
  63. "install_dev",
  64. ]
  65. configure_make(
  66. name = "openssl",
  67. # TODO(parkmycar): Figure out how to dynamically provide a number of jobs to foreign_cc rules.
  68. args = ["-j8"],
  69. # darwin/macOS defaults to using libtool but openSSL3 fails linking with
  70. # the generated archive, so we switch to `llvm-ar`.
  71. build_data = select({
  72. "@platforms//os:macos": ["@llvm_toolchain_llvm//:ar"],
  73. "//conditions:default": {},
  74. }),
  75. configure_command = "config",
  76. configure_in_place = True,
  77. configure_options = CONFIGURE_OPTIONS + select(
  78. {
  79. "@//misc/bazel/platforms:linux_arm": ["linux-aarch64"],
  80. "@//misc/bazel/platforms:linux_x86_64": ["linux-x86_64"],
  81. "@//misc/bazel/platforms:macos_arm": ["darwin64-arm64-cc"],
  82. "@//misc/bazel/platforms:macos_x86_64": ["darwin64-x86_64-cc"],
  83. },
  84. no_match_error = "The specified platform is not supported.",
  85. ) + select({
  86. "@platforms//os:macos": ["ARFLAGS=r"],
  87. "//conditions:default": [],
  88. }) + select(
  89. {
  90. "@//misc/bazel/platforms:sanitizer_none": [],
  91. "@//misc/bazel/platforms:sanitizer_address": [],
  92. # When we build with the hardware assisted address sanitizer
  93. # linking fails with some ASM instructions being out of range.
  94. "@//misc/bazel/platforms:sanitizer_hwaddress": ["no-asm"],
  95. },
  96. no_match_error = "Please configure OpenSSL to build with this sanitizer.",
  97. ),
  98. env = select({
  99. "@platforms//os:macos": {"AR": "$(execpath @llvm_toolchain_llvm//:ar)"},
  100. "//conditions:default": {},
  101. }),
  102. lib_name = LIB_NAME,
  103. lib_source = ":all_srcs",
  104. # Note that for Linux builds, libssl must come before libcrypto on the linker command-line.
  105. # As such, libssl must be listed before libcrypto
  106. out_static_libs = [
  107. "libssl.a",
  108. "libcrypto.a",
  109. ],
  110. targets = MAKE_TARGETS,
  111. visibility = ["//visibility:public"],
  112. )
  113. # Captures all of the outputs of the "openssl" rule into a single group.
  114. filegroup(
  115. name = "out_dir",
  116. srcs = [":openssl"],
  117. visibility = ["//visibility:public"],
  118. )
  119. # There are two nuances here we're accounting for:
  120. #
  121. # 1. The Rust crate openssl-sys expects the static libs to be in a single
  122. # directory, specified with `OPENSSL_LIB_DIR`. We use `copy_to_directory` to
  123. # group everything together.
  124. # 2. `rustc` (or the linker?) can't find the static libs through a symlink, so
  125. # we explicitly copy the files into the aforementioned directory.
  126. # `copy_file` + `copy_to_directory` forces bazel to produce real files in
  127. # the right place, not just symlinks.
  128. #
  129. select_file(
  130. name = "libssl",
  131. srcs = ":out_dir",
  132. subpath = "libssl.a",
  133. )
  134. copy_file(
  135. name = "libssl_copy",
  136. src = ":libssl",
  137. out = "libssl.a",
  138. allow_symlink = False,
  139. )
  140. select_file(
  141. name = "libcrypto",
  142. srcs = ":out_dir",
  143. subpath = "libcrypto.a",
  144. )
  145. copy_file(
  146. name = "libcrypto_copy",
  147. src = ":libcrypto",
  148. out = "libcrypto.a",
  149. allow_symlink = False,
  150. )
  151. copy_to_directory(
  152. name = "openssl_lib",
  153. srcs = [
  154. ":libcrypto_copy",
  155. ":libssl_copy",
  156. ],
  157. visibility = ["//visibility:public"],
  158. )
  159. # Select the include folder so we can specify `OPENSSL_INCLUDE_DIR`
  160. select_file(
  161. name = "openssl_include",
  162. srcs = ":out_dir",
  163. subpath = "include",
  164. visibility = ["//visibility:public"],
  165. )
  166. copy_to_directory(
  167. name = "openssl_root",
  168. srcs = [
  169. ":libcrypto_copy",
  170. ":libssl_copy",
  171. ":openssl_include",
  172. ],
  173. root_paths = [
  174. ".",
  175. "openssl",
  176. ],
  177. visibility = ["//visibility:public"],
  178. )