| 1 |
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 |
# SPDX-License-Identifier: Apache-2.0 OR ISC |
| 3 |
|
| 4 |
# Module for generating Rust bindings using bindgen-cli |
| 5 |
# |
| 6 |
# Background: Symbol Prefix Mechanism |
| 7 |
# ------------------------------------ |
| 8 |
# AWS-LC supports building with a symbol prefix (via BORINGSSL_PREFIX) to avoid |
| 9 |
# symbol collisions when multiple copies of the library are linked into one binary. |
| 10 |
# The C build achieves this by generating a boringssl_prefix_symbols.h header that |
| 11 |
# #define-renames every public symbol (e.g., #define SSL_new AWSLC_SSL_new), then |
| 12 |
# force-including it during compilation. |
| 13 |
# |
| 14 |
# For Rust bindings, we must NOT include that prefix header when invoking bindgen. |
| 15 |
# Instead, we use bindgen's --prefix-link-name option, which adds the prefix to |
| 16 |
# #[link_name] attributes (for the linker) while keeping the Rust function names |
| 17 |
# unprefixed. If we included the prefix header, bindgen would see already-prefixed |
| 18 |
# names and --prefix-link-name would double-prefix them, producing broken bindings. |
| 19 |
# |
| 20 |
# This produces bindings like: |
| 21 |
# extern "C" { |
| 22 |
# #[link_name = "AWSLC_SSL_new"] // Prefixed for linker |
| 23 |
# pub fn SSL_new(...) -> ...; // Unprefixed for Rust API |
| 24 |
# } |
| 25 |
|
| 26 |
# Headers to exclude from Rust bindings generation. |
| 27 |
# Each exclusion should have a comment explaining why. |
| 28 |
set(RUST_WRAPPER_EXCLUDED_HEADERS |
| 29 |
# Template files (not actual headers, used by configure_file) |
| 30 |
"base.h.in" |
| 31 |
"opensslv.h.in" |
| 32 |
|
| 33 |
# Prefix symbol headers must not be included. See module-level comment above. |
| 34 |
"boringssl_prefix_symbols.h" |
| 35 |
"boringssl_prefix_symbols_asm.h" |
| 36 |
"boringssl_prefix_symbols_nasm.inc" |
| 37 |
|
| 38 |
# Platform/architecture-specific internal headers (not part of public API) |
| 39 |
"arm_arch.h" |
| 40 |
"asm_base.h" |
| 41 |
"target.h" |
| 42 |
|
| 43 |
# SSL headers (conditionally included via AWS_LC_RUST_INCLUDE_SSL define) |
| 44 |
"ssl.h" |
| 45 |
"ssl3.h" |
| 46 |
"tls1.h" |
| 47 |
"dtls1.h" |
| 48 |
) |
| 49 |
|
| 50 |
# Discover all OpenSSL headers for dependency tracking |
| 51 |
# Uses CONFIGURE_DEPENDS on CMake 3.12+ for automatic reconfiguration |
| 52 |
function(discover_openssl_headers OUT_VAR OUT_EXPERIMENTAL_VAR) |
| 53 |
if(CMAKE_VERSION VERSION_GREATER_EQUAL "3.12") |
| 54 |
file(GLOB _all_headers |
| 55 |
CONFIGURE_DEPENDS |
| 56 |
"${AWSLC_SOURCE_DIR}/include/openssl/*.h") |
| 57 |
file(GLOB _experimental_headers |
| 58 |
CONFIGURE_DEPENDS |
| 59 |
"${AWSLC_SOURCE_DIR}/include/openssl/experimental/*.h") |
| 60 |
else() |
| 61 |
file(GLOB _all_headers "${AWSLC_SOURCE_DIR}/include/openssl/*.h") |
| 62 |
file(GLOB _experimental_headers "${AWSLC_SOURCE_DIR}/include/openssl/experimental/*.h") |
| 63 |
endif() |
| 64 |
|
| 65 |
set(${OUT_VAR} "${_all_headers}" PARENT_SCOPE) |
| 66 |
set(${OUT_EXPERIMENTAL_VAR} "${_experimental_headers}" PARENT_SCOPE) |
| 67 |
endfunction() |
| 68 |
|
| 69 |
# Generate rust_wrapper.h from template by discovering headers |
| 70 |
function(generate_rust_wrapper_header) |
| 71 |
discover_openssl_headers(_all_headers _experimental_headers) |
| 72 |
|
| 73 |
# Build the include list, filtering out excluded headers |
| 74 |
set(_includes "") |
| 75 |
foreach(_header ${_all_headers}) |
| 76 |
get_filename_component(_header_name "${_header}" NAME) |
| 77 |
list(FIND RUST_WRAPPER_EXCLUDED_HEADERS "${_header_name}" _excluded_idx) |
| 78 |
if(_excluded_idx EQUAL -1) |
| 79 |
string(APPEND _includes "#include <openssl/${_header_name}>\n") |
| 80 |
endif() |
| 81 |
endforeach() |
| 82 |
|
| 83 |
# Build the experimental include list |
| 84 |
set(_experimental_includes "") |
| 85 |
foreach(_header ${_experimental_headers}) |
| 86 |
get_filename_component(_header_name "${_header}" NAME) |
| 87 |
string(APPEND _experimental_includes "#include <openssl/experimental/${_header_name}>\n") |
| 88 |
endforeach() |
| 89 |
|
| 90 |
# Set variables for configure_file substitution |
| 91 |
set(RUST_WRAPPER_INCLUDES "${_includes}") |
| 92 |
set(RUST_WRAPPER_EXPERIMENTAL_INCLUDES "${_experimental_includes}") |
| 93 |
|
| 94 |
# Generate the header from template |
| 95 |
configure_file( |
| 96 |
"${AWSLC_SOURCE_DIR}/cmake/rust_wrapper.h.in" |
| 97 |
"${AWSLC_BINARY_DIR}/include/rust_wrapper.h" |
| 98 |
@ONLY |
| 99 |
) |
| 100 |
|
| 101 |
message(STATUS "Generated rust_wrapper.h with discovered headers") |
| 102 |
endfunction() |
| 103 |
|
| 104 |
# Determine the symbol prefix format based on target platform |
| 105 |
# |
| 106 |
# Different platforms have different symbol naming conventions: |
| 107 |
# - Apple platforms (macOS, iOS): Symbols have a leading underscore (_SSL_new) |
| 108 |
# - 32-bit Windows: Symbols have a leading underscore (_SSL_new) |
| 109 |
# - Other platforms (Linux, 64-bit Windows): No leading underscore (SSL_new) |
| 110 |
# |
| 111 |
# When we add our prefix, we need to account for this: |
| 112 |
# - Apple/Win32: _AWSLC_SSL_new (prefix goes after the leading underscore) |
| 113 |
# - Others: AWSLC_SSL_new |
| 114 |
# |
| 115 |
# The --prefix-link-name option prepends the given string to link names, |
| 116 |
# so we format it appropriately for each platform. |
| 117 |
function(get_symbol_prefix_format PREFIX OUT_VAR) |
| 118 |
# Use CMAKE_SYSTEM_NAME for cross-compilation support |
| 119 |
# (WIN32/APPLE variables reflect the HOST, not the TARGET) |
| 120 |
if(CMAKE_SYSTEM_NAME STREQUAL "Darwin" OR CMAKE_SYSTEM_NAME STREQUAL "iOS") |
| 121 |
set(${OUT_VAR} "_${PREFIX}_" PARENT_SCOPE) |
| 122 |
elseif(CMAKE_SYSTEM_NAME STREQUAL "Windows" AND CMAKE_SIZEOF_VOID_P EQUAL 4) |
| 123 |
set(${OUT_VAR} "_${PREFIX}_" PARENT_SCOPE) |
| 124 |
else() |
| 125 |
set(${OUT_VAR} "${PREFIX}_" PARENT_SCOPE) |
| 126 |
endif() |
| 127 |
endfunction() |
| 128 |
|
| 129 |
# Generate Rust bindings using bindgen-cli |
| 130 |
# |
| 131 |
# Arguments: |
| 132 |
# OUTPUT_FILE - Path where the generated .rs file will be written |
| 133 |
# INCLUDE_DIRS - List of include directories for clang |
| 134 |
# PREFIX - (Optional) Symbol prefix (e.g., "AWSLC") |
| 135 |
# INCLUDE_SSL - (Optional) Flag to include SSL headers |
| 136 |
function(generate_rust_bindings) |
| 137 |
set(options INCLUDE_SSL) |
| 138 |
set(oneValueArgs OUTPUT_FILE PREFIX) |
| 139 |
set(multiValueArgs INCLUDE_DIRS) |
| 140 |
cmake_parse_arguments(ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) |
| 141 |
|
| 142 |
# Discover headers for dependency tracking |
| 143 |
discover_openssl_headers(_all_openssl_headers _experimental_headers) |
| 144 |
|
| 145 |
# Build include path arguments for clang |
| 146 |
set(_clang_include_args "") |
| 147 |
foreach(_dir ${ARG_INCLUDE_DIRS}) |
| 148 |
list(APPEND _clang_include_args "-I${_dir}") |
| 149 |
endforeach() |
| 150 |
|
| 151 |
# Build bindgen arguments (based on aws-lc-rs/aws-lc-sys configuration) |
| 152 |
# |
| 153 |
# Note on regex escaping for --allowlist-file: |
| 154 |
# - CMake requires \\ to represent a single backslash in strings |
| 155 |
# - The regex itself needs \\ to match a literal backslash (for Windows paths) |
| 156 |
# - Combined: \\\\ in CMake -> \\ passed to bindgen -> \\ in regex -> matches one backslash |
| 157 |
# - The pattern matches paths like: .../openssl/ssl.h or ...\openssl\ssl.h |
| 158 |
set(_bindgen_args |
| 159 |
"--allowlist-file" ".*(/|\\\\)openssl((/|\\\\)[^/\\\\]+)+\\.h" |
| 160 |
"--allowlist-file" ".*(/|\\\\)rust_wrapper\\.h" |
| 161 |
"--rustified-enum" "point_conversion_form_t" |
| 162 |
"--default-macro-constant-type" "signed" |
| 163 |
"--with-derive-default" |
| 164 |
"--with-derive-partialeq" |
| 165 |
"--with-derive-eq" |
| 166 |
"--generate" "functions,types,vars,methods,constructors,destructors" |
| 167 |
"--rust-target" "${RUST_BINDINGS_TARGET_VERSION}" |
| 168 |
"--formatter" "rustfmt" |
| 169 |
) |
| 170 |
# Add symbol prefix if specified. See module-level comment for why we use |
| 171 |
# --prefix-link-name instead of including the prefix symbols header. |
| 172 |
if(ARG_PREFIX AND NOT ARG_PREFIX STREQUAL "") |
| 173 |
get_symbol_prefix_format("${ARG_PREFIX}" _sym_prefix) |
| 174 |
list(APPEND _bindgen_args "--prefix-link-name" "${_sym_prefix}") |
| 175 |
message(STATUS "Rust bindings will use symbol prefix: ${_sym_prefix}") |
| 176 |
endif() |
| 177 |
|
| 178 |
# Build clang arguments (prefix symbols header is intentionally excluded) |
| 179 |
set(_clang_args "") |
| 180 |
|
| 181 |
# Add SSL support if requested |
| 182 |
if(ARG_INCLUDE_SSL) |
| 183 |
list(APPEND _clang_args "-DAWS_LC_RUST_INCLUDE_SSL") |
| 184 |
message(STATUS "Rust bindings: SSL support enabled (INCLUDE_SSL=ON)") |
| 185 |
else() |
| 186 |
message(STATUS "Rust bindings: SSL support disabled (INCLUDE_SSL not set)") |
| 187 |
endif() |
| 188 |
|
| 189 |
# Add include paths (unprefixed headers only) |
| 190 |
list(APPEND _clang_args ${_clang_include_args}) |
| 191 |
|
| 192 |
# Ensure output directory exists |
| 193 |
get_filename_component(_output_dir "${ARG_OUTPUT_FILE}" DIRECTORY) |
| 194 |
|
| 195 |
message(STATUS "Rust bindings clang args: ${_clang_args}") |
| 196 |
message(STATUS "Rust bindings bindgen args: ${_bindgen_args}") |
| 197 |
|
| 198 |
# Create custom command for generating bindings |
| 199 |
# Dependencies include all OpenSSL headers to ensure regeneration when headers change |
| 200 |
# |
| 201 |
# Note: VERBATIM is used to ensure proper argument escaping across platforms, |
| 202 |
# especially important for the regex patterns on Windows. If debugging command-line |
| 203 |
# issues, be aware that VERBATIM affects how CMake quotes arguments. |
| 204 |
add_custom_command( |
| 205 |
OUTPUT "${ARG_OUTPUT_FILE}" |
| 206 |
COMMAND ${CMAKE_COMMAND} -E make_directory "${_output_dir}" |
| 207 |
COMMAND ${BINDGEN_EXECUTABLE} |
| 208 |
${_bindgen_args} |
| 209 |
"${AWSLC_BINARY_DIR}/include/rust_wrapper.h" |
| 210 |
"--output" "${ARG_OUTPUT_FILE}" |
| 211 |
"--" |
| 212 |
${_clang_args} |
| 213 |
DEPENDS |
| 214 |
"${AWSLC_BINARY_DIR}/include/rust_wrapper.h" |
| 215 |
"${AWSLC_SOURCE_DIR}/cmake/rust_wrapper.h.in" |
| 216 |
${_all_openssl_headers} |
| 217 |
${_experimental_headers} |
| 218 |
COMMENT "Generating Rust bindings with bindgen-cli" |
| 219 |
VERBATIM |
| 220 |
) |
| 221 |
|
| 222 |
# Create target for bindings generation (not part of ALL; built on-demand or via install) |
| 223 |
add_custom_target(rust_bindings DEPENDS "${ARG_OUTPUT_FILE}") |
| 224 |
endfunction() |