Skip to content

Truly independent web browser

2.0 KB
Raw Blame
#[[
 * Copyright (c) 2026-present, the Ladybird developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
]]

if (NOT DEFINED CRATE_NAME OR NOT DEFINED CARGO_BUILD_SCRIPT_DIR OR NOT DEFINED FFI_HEADER OR NOT DEFINED FFI_OUTPUT_DIR)
    message(FATAL_ERROR "sync_rust_ffi_header.cmake requires CRATE_NAME, CARGO_BUILD_SCRIPT_DIR, FFI_HEADER, and FFI_OUTPUT_DIR")
endif()

if (FFI_HEADER STREQUAL "")
    return()
endif()

# Cargo writes a build script's output under <crate>-<hash>/ up to 1.98, and under <crate>/<hash>/run/ after that. A
# build directory reused across a Cargo upgrade holds both, so the newest root-output file decides.
file(GLOB root_output_files
    "${CARGO_BUILD_SCRIPT_DIR}/${CRATE_NAME}-*/root-output"
    "${CARGO_BUILD_SCRIPT_DIR}/${CRATE_NAME}/*/run/root-output")

set(latest_source_header "")
set(latest_root_output_file "")

foreach(root_output_file IN LISTS root_output_files)
    file(READ "${root_output_file}" out_dir)
    string(STRIP "${out_dir}" out_dir)

    set(source_header "${out_dir}/${FFI_HEADER}")
    if (NOT EXISTS "${source_header}")
        continue()
    endif()

    # IS_NEWER_THAN compares the full modification times, and holds for equal ones, so a tie goes to the file that
    # sorts later: the newer layout's, since "/" sorts after "-".
    if (latest_source_header STREQUAL "" OR "${root_output_file}" IS_NEWER_THAN "${latest_root_output_file}")
        set(latest_source_header "${source_header}")
        set(latest_root_output_file "${root_output_file}")
    endif()
endforeach()

if (NOT latest_source_header STREQUAL "")
    get_filename_component(destination_header_directory "${FFI_OUTPUT_DIR}/${FFI_HEADER}" DIRECTORY)
    file(MAKE_DIRECTORY "${destination_header_directory}")
    execute_process(
        COMMAND "${CMAKE_COMMAND}" -E copy_if_different "${latest_source_header}" "${FFI_OUTPUT_DIR}/${FFI_HEADER}"
        COMMAND_ERROR_IS_FATAL ANY
    )
    return()
endif()

message(FATAL_ERROR "Failed to find ${FFI_HEADER} in Cargo build output for ${CRATE_NAME}")