From c6964a7e611b8f9eeb365dfd1eac05d9111eb671 Mon Sep 17 00:00:00 2001 From: Fred Emmott Date: Wed, 1 Sep 2021 18:07:55 -0700 Subject: [PATCH] Never build rust FFI in tree for open source hack 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 --- hphp/hack/CMakeLists.txt | 26 ++++++++++++++++++-------- hphp/hack/dev_env_common.sh.in | 2 +- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/hphp/hack/CMakeLists.txt b/hphp/hack/CMakeLists.txt index 8c812bdaf25dc..ead48b27f84ed 100644 --- a/hphp/hack/CMakeLists.txt +++ b/hphp/hack/CMakeLists.txt @@ -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") @@ -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 ) @@ -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 ` + target_compile_options("${NAME}_ffi_bridge" INTERFACE "-iquote" "${RUST_FFI_BUILD_ROOT}") endfunction() build_cxx_bridge(rust_facts "src/facts/ffi_bridge") diff --git a/hphp/hack/dev_env_common.sh.in b/hphp/hack/dev_env_common.sh.in index 0f702ab641d80..5d2ad265d4edd 100644 --- a/hphp/hack/dev_env_common.sh.in +++ b/hphp/hack/dev_env_common.sh.in @@ -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@"