Skip to content

Commit

Permalink
Never build rust FFI in tree for open source hack
Browse files Browse the repository at this point in the history
Summary:
Similar to dune, we *must* have an out of source build as far as cargo/dune are concerned, even if we don't have one as far as top-level/cmake are concerned.

Stick with .rs rather than #ifdef FACEBOOK for .h vs .rs, as with a separate build dir, we shouldn't need it any more.

Might as well hide cargo_home out the way too while I'm here.

Reviewed By: kavoor

Differential Revision: D30712601

fbshipit-source-id: 5ac37df9d395bf52853c7f57c3949bdeb3989258
  • Loading branch information
fredemmott authored and facebook-github-bot committed Sep 2, 2021
1 parent 4b3a784 commit c6964a7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
26 changes: 18 additions & 8 deletions hphp/hack/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,12 @@ endif()
if ("${CMAKE_CURRENT_SOURCE_DIR}" STREQUAL "${CMAKE_CURRENT_BINARY_DIR}")
set(DUNE_BUILD_DIR "${CMAKE_CURRENT_SOURCE_DIR}/_build")
set(OPAM_STAMP_FILE "_build/opam.stamp")
set(RUST_FFI_BUILD_ROOT "${DUNE_BUILD_DIR}/rust_ffi")
set(CARGO_HOME "${DUNE_BUILD_DIR}/cargo_home")
else()
set(DUNE_BUILD_DIR "${CMAKE_CURRENT_BINARY_DIR}")
set(RUST_FFI_BUILD_ROOT "${CMAKE_CURRENT_BINARY_DIR}")
set(CARGO_HOME "${CMAKE_CURRENT_BINARY_DIR}/cargo_home")
set(OPAM_STAMP_FILE "opam.stamp")
endif()
set(HACK_BUILD_ROOT "${DUNE_BUILD_DIR}/default")
Expand Down Expand Up @@ -141,18 +145,21 @@ endif()

function(build_cxx_bridge NAME FFI_BRIDGE_DIR)
set(FFI_BRIDGE_SRC "${CMAKE_CURRENT_SOURCE_DIR}/${FFI_BRIDGE_DIR}")
set(FFI_BRIDGE_BIN "${CMAKE_CURRENT_BINARY_DIR}/${FFI_BRIDGE_DIR}")
set(FFI_BRIDGE_BIN "${RUST_FFI_BUILD_ROOT}/hphp/hack/${FFI_BRIDGE_DIR}")

set(RUST_PART_LIB "${FFI_BRIDGE_BIN}/${PROFILE}/lib${NAME}_ffi_bridge${CMAKE_STATIC_LIBRARY_SUFFIX}")
set(RUST_PART_LIB "${FFI_BRIDGE_BIN}/${PROFILE}/${CMAKE_STATIC_LIBRARY_PREFIX}${NAME}_ffi_bridge${CMAKE_STATIC_LIBRARY_SUFFIX}")
set(RUST_PART_CXX "${FFI_BRIDGE_BIN}/${NAME}_ffi_bridge.cpp")
set(RUST_PART_HEADER "${FFI_BRIDGE_BIN}/${NAME}_ffi_bridge.rs")
set(GENERATED "${FFI_BRIDGE_BIN}/cxxbridge/${NAME}_ffi_bridge/${NAME}_ffi_bridge")

add_custom_command(
OUTPUT ${RUST_PART_CXX}
COMMAND . "${CMAKE_CURRENT_BINARY_DIR}/dev_env_rust_only.sh" &&
${CARGO_BUILD} "${NAME}_ffi_bridge" "${NAME}_ffi_bridge" --cxx ${FFI_BRIDGE_BIN}
COMMAND cp "${GENERATED}.rs.cc" ${RUST_PART_CXX}
COMMAND cp "${GENERATED}.rs.h" "${FFI_BRIDGE_BIN}/${NAME}_ffi_bridge.rs"
COMMAND
${CMAKE_COMMAND} -E make_directory "${FFI_BRIDGE_BIN}" &&
. "${CMAKE_CURRENT_BINARY_DIR}/dev_env_rust_only.sh" &&
${CARGO_BUILD} "${NAME}_ffi_bridge" "${NAME}_ffi_bridge" --cxx "${FFI_BRIDGE_BIN}" &&
${CMAKE_COMMAND} -E copy "${GENERATED}.rs.cc" "${RUST_PART_CXX}" &&
${CMAKE_COMMAND} -E copy "${GENERATED}.rs.h" "${RUST_PART_HEADER}"
WORKING_DIRECTORY ${FFI_BRIDGE_SRC}
DEPENDS rustc cargo
)
Expand All @@ -161,10 +168,13 @@ function(build_cxx_bridge NAME FFI_BRIDGE_DIR)
DEPENDS ${RUST_PART_CXX}
)

include_directories(${FFI_BRIDGE_BIN})
add_library("${NAME}_ffi_bridge" STATIC ${RUST_PART_CXX})
add_dependencies("${NAME}_ffi_bridge" rustc cargo "${NAME}_cxx")
target_link_libraries("${NAME}_ffi_bridge" ${RUST_PART_LIB})
target_link_libraries("${NAME}_ffi_bridge" PUBLIC ${RUST_PART_LIB})
# `-iquote` is like `-I` (or target_include_directories()`), except:
# - it takes precedence over `-I`
# - it only applies to `#include "foo"`, not `#include <foo>`
target_compile_options("${NAME}_ffi_bridge" INTERFACE "-iquote" "${RUST_FFI_BUILD_ROOT}")
endfunction()

build_cxx_bridge(rust_facts "src/facts/ffi_bridge")
Expand Down
2 changes: 1 addition & 1 deletion hphp/hack/dev_env_common.sh.in
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export CMAKE_INSTALL_FULL_BINDIR="@CMAKE_INSTALL_FULL_BINDIR@"
export HACK_NO_CARGO_VENDOR=true
export OPAMROOT="@OPAMROOT@"
export PYTHONPATH="@HPHP_HOME@" # needed for verify.py for `hack_dune_test`
export CARGO_HOME="@CMAKE_CURRENT_BINARY_DIR@/cargo_home"
export CARGO_HOME="@CARGO_HOME@"
export RUSTC="@RUSTC_BIN_DIR@/rustc"
export DUNE_BUILD_DIR="@DUNE_BUILD_DIR@"
export HACK_SOURCE_ROOT="@CMAKE_CURRENT_SOURCE_DIR@"
Expand Down

0 comments on commit c6964a7

Please sign in to comment.