xref: /aosp_15_r20/external/bazelbuild-rules_rust/examples/third_party/openssl/BUILD.openssl.bazel (revision d4726bddaa87cc4778e7472feed243fa4b6c267f)
1# The code here was picked up from the `rules_foreign_cc` openssl example
2# https://github.com/bazelbuild/rules_foreign_cc/tree/0.5.1/examples/third_party/openssl
3
4load("@rules_foreign_cc//foreign_cc:defs.bzl", "configure_make", "configure_make_variant")
5
6# Read https://wiki.openssl.org/index.php/Compilation_and_Installation
7
8filegroup(
9    name = "all_srcs",
10    srcs = glob(["**"]),
11)
12
13CONFIGURE_OPTIONS = [
14    "no-comp",
15    "no-idea",
16    "no-weak-ssl-ciphers",
17    "no-shared",
18]
19
20LIB_NAME = "openssl"
21
22MAKE_TARGETS = [
23    "build_libs",
24    "install_dev",
25]
26
27config_setting(
28    name = "msvc_compiler",
29    flag_values = {
30        "@bazel_tools//tools/cpp:compiler": "msvc-cl",
31    },
32    visibility = ["//visibility:public"],
33)
34
35alias(
36    name = "openssl",
37    actual = select({
38        ":msvc_compiler": "openssl_msvc",
39        "//conditions:default": "openssl_default",
40    }),
41    visibility = ["//visibility:public"],
42)
43
44configure_make_variant(
45    name = "openssl_msvc",
46    build_data = [
47        "@nasm_windows//:nasm",
48        "@perl_windows//:perl",
49    ],
50    configure_command = "Configure",
51    configure_in_place = True,
52    configure_options = CONFIGURE_OPTIONS + [
53        "VC-WIN64A",
54        # Unset Microsoft Assembler (MASM) flags set by built-in MSVC toolchain,
55        # as NASM is unsed to build OpenSSL rather than MASM
56        "ASFLAGS=\" \"",
57    ],
58    configure_prefix = "$PERL",
59    env = {
60        # The Zi flag must be set otherwise OpenSSL fails to build due to missing .pdb files
61        "CFLAGS": "-Zi",
62        "PATH": "$(dirname $(execpath @nasm_windows//:nasm)):$PATH",
63        "PERL": "$(execpath @perl_windows//:perl)",
64    },
65    lib_name = LIB_NAME,
66    lib_source = ":all_srcs",
67    out_static_libs = [
68        "libssl.lib",
69        "libcrypto.lib",
70    ],
71    targets = MAKE_TARGETS,
72    toolchain = "@rules_foreign_cc//toolchains:preinstalled_nmake_toolchain",
73)
74
75configure_make(
76    name = "openssl_default",
77    configure_command = "config",
78    configure_in_place = True,
79    configure_options = CONFIGURE_OPTIONS,
80    env = select({
81        "@platforms//os:macos": {"AR": ""},
82        "//conditions:default": {},
83    }),
84    lib_name = LIB_NAME,
85    lib_source = ":all_srcs",
86    # Note that for Linux builds, libssl must come before libcrypto on the linker command-line.
87    # As such, libssl must be listed before libcrypto
88    out_static_libs = [
89        "libssl.a",
90        "libcrypto.a",
91    ],
92    targets = MAKE_TARGETS,
93)
94
95filegroup(
96    name = "gen_dir",
97    srcs = [":openssl"],
98    output_group = "gen_dir",
99    visibility = ["//visibility:public"],
100)
101