xref: /aosp_15_r20/external/boringssl/src/rust/bssl-sys/CMakeLists.txt (revision 8fb009dc861624b67b6cdb62ea21f0f22d0c584b)
1# Generate architecture-specific wrappers. bindgen must be called from
2# ${CMAKE_BINARY_DIR}, with the output path as a relative path. bindgen writes
3# the depfile using the same syntax as the command-line argument, and ninja
4# requires a path relative to the top-level build directory.
5set(wrapper_rs wrapper_${RUST_BINDINGS}.rs)
6binary_dir_relative_path(${wrapper_rs} wrapper_rs_relative)
7binary_dir_relative_path(${wrapper_rs}.d depfile_relative)
8
9add_custom_command(
10  OUTPUT ${wrapper_rs} wrapper.c
11  COMMAND ${BINDGEN_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/wrapper.h
12          -o ${wrapper_rs_relative}
13          --depfile=${depfile_relative}
14          --no-derive-default
15          --enable-function-attribute-detection
16          --use-core
17          --default-macro-constant-type=signed
18          --rustified-enum=point_conversion_form_t
19          # These regexes need to accept both / and \ to handle Windows file
20          # path differences, due a bindgen issue. See
21          # https://crbug.com/boringssl/595. Ideally, we would write [/\\], but
22          # there are many layers of escaping here. First, CMake interprets
23          # backslashes. Then CMake generates a Ninja or Make file. That, in
24          # turn, uses the shell on POSIX, and does something else on Windows.
25          #
26          # It is unlikely that every layer here has sufficiently well-defined
27          # escaping and correctly handled the next layer's escaping. On top of
28          # that, we'd likely need to detect Windows vs POSIX hosts and change
29          # the input. Instead, just use [[:punct:]] which is more permissive
30          # than necessary, but we only need to exclude unwanted libc headers.
31          #
32          # If bindgen ever supports some file-based config (see
33          # https://github.com/rust-lang/rust-bindgen/issues/2508), we can
34          # switch to that.
35          --allowlist-file=".*[[:punct:]]include[[:punct:]]openssl[[:punct:]].*\\.h"
36          --experimental
37          --wrap-static-fns
38          --wrap-static-fns-path="${CMAKE_CURRENT_BINARY_DIR}/wrapper.c"
39          -- # these are LLVM arg passthroughs
40          -I${PROJECT_SOURCE_DIR}/include
41          # https://doc.rust-lang.org/nightly/rustc/platform-support.html
42          --target=${RUST_BINDINGS}
43  DEPENDS wrapper.h
44  DEPFILE ${CMAKE_CURRENT_BINARY_DIR}/${wrapper_rs}.d
45  WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
46)
47
48add_library(rust_wrapper STATIC wrapper.c)
49if(CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_COMPILER_IS_GNUCXX)
50  target_compile_options(rust_wrapper PRIVATE "-Wno-missing-prototypes")
51endif()
52target_include_directories(rust_wrapper PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
53target_link_libraries(rust_wrapper crypto ssl)
54
55add_custom_target(bssl_sys ALL DEPENDS ${wrapper_rs} rust_wrapper)
56