From b2d4619125683aac30a78dc1581782591190551a Mon Sep 17 00:00:00 2001 From: Johannes Altmanninger Date: Wed, 3 Jan 2024 20:02:32 +0100 Subject: [PATCH] Link against curses in build.rs too This will allow to use "cargo test" for unit tests that depend on our curses.rs. This means that Rust.cmake depends on ConfigureChecks, so move that one to the front. --- CMakeLists.txt | 5 +++-- cmake/Rust.cmake | 3 +++ fish-rust/build.rs | 40 +++++++++++++++++++++++++++++++++++----- 3 files changed, 41 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index fa686cc14..ea2f70a3e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,6 +24,9 @@ if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) set(CMAKE_BUILD_TYPE "${DEFAULT_BUILD_TYPE}") endif() +include(cmake/ConfigureChecks.cmake) +include(cmake/gettext.cmake) + include(cmake/Rust.cmake) # Error out when linking statically, it doesn't work. @@ -123,8 +126,6 @@ set(FISH_SRCS file(GLOB FISH_HEADERS src/*.h) # Set up config.h -include(cmake/ConfigureChecks.cmake) -include(cmake/gettext.cmake) configure_file(${CMAKE_CURRENT_SOURCE_DIR}/config_cmake.h.in ${CMAKE_CURRENT_BINARY_DIR}/config.h) include_directories(${CMAKE_CURRENT_BINARY_DIR}) diff --git a/cmake/Rust.cmake b/cmake/Rust.cmake index 45cf1c882..5e5dbb94e 100644 --- a/cmake/Rust.cmake +++ b/cmake/Rust.cmake @@ -62,6 +62,8 @@ endif() # CMAKE_BINARY_DIR can include symlinks, since we want to compare this to the dir fish is executed in we need to canonicalize it. file(REAL_PATH "${CMAKE_BINARY_DIR}" fish_binary_dir) +string(JOIN "," CURSES_LIBRARY_LIST ${CURSES_LIBRARY} ${CURSES_EXTRA_LIBRARY}) + # Tell Cargo where our build directory is so it can find config.h. corrosion_set_env_vars(${fish_rust_target} "FISH_BUILD_DIR=${fish_binary_dir}" @@ -75,6 +77,7 @@ corrosion_set_env_vars(${fish_rust_target} "SYSCONFDIR=${CMAKE_INSTALL_FULL_SYSCONFDIR}" "BINDIR=${CMAKE_INSTALL_FULL_BINDIR}" "LOCALEDIR=${CMAKE_INSTALL_FULL_LOCALEDIR}" + "CURSES_LIBRARY_LIST=${CURSES_LIBRARY_LIST}" ) # this needs an extra fish-rust due to the poor source placement diff --git a/fish-rust/build.rs b/fish-rust/build.rs index e16a17492..dc94f9072 100644 --- a/fish-rust/build.rs +++ b/fish-rust/build.rs @@ -1,6 +1,7 @@ use rsconf::{LinkType, Target}; use std::env; use std::error::Error; +use std::path::{Path, PathBuf}; use std::process::Stdio; fn main() { @@ -11,13 +12,42 @@ fn main() { } } + let build_dir = PathBuf::from( + std::env::var("FISH_BUILD_DIR") + // Add our default to enable tools that don't go through CMake, like "cargo test" + // and the language server. + .unwrap_or("./build".into()), + ); + + let cached_curses_libnames = Path::join(&build_dir, "cached-curses-libnames"); + let curses_libnames: Vec = if let Ok(lib_path_list) = env::var("CURSES_LIBRARY_LIST") { + let lib_paths = lib_path_list + .split(',') + .filter(|s| !s.is_empty()) + .map(|s| s.to_string()); + let curses_libnames: Vec<_> = lib_paths + .map(|libpath| { + let stem = Path::new(&libpath).file_stem().unwrap().to_str().unwrap(); + // Ubuntu-32bit-fetched-pcre2's ncurses doesn't have the "lib" prefix. + stem.strip_prefix("lib").unwrap_or(stem).to_owned() + }) + .collect(); + std::fs::write(cached_curses_libnames, curses_libnames.join("\n") + "\n").unwrap(); + curses_libnames + } else { + let lib_cache = std::fs::read(cached_curses_libnames).unwrap_or_default(); + let lib_cache = String::from_utf8(lib_cache).unwrap(); + lib_cache + .split('\n') + .filter(|s| !s.is_empty()) + .map(|s| s.to_owned()) + .collect() + }; + rsconf::link_libraries(&curses_libnames, LinkType::Default); + cc::Build::new() .file("fish-rust/src/compat.c") - .include( - &std::env::var("FISH_BUILD_DIR") - // Add our default to potentially help tools that don't go through CMake. - .unwrap_or("./build".into()), - ) + .include(&build_dir) .compile("libcompat.a"); if compiles("fish-rust/src/cfg/w_exitcode.cpp") {