Skip to content

Commit 3f4a242

Browse files
committed
Improve rust builds, added better dependency management for rustc runtime, fixed nightly version, mitigated link errors in rust test.
1 parent 5b0e97c commit 3f4a242

File tree

6 files changed

+114
-61
lines changed

6 files changed

+114
-61
lines changed

cmake/FindRust.cmake

Lines changed: 97 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -19,105 +19,153 @@
1919

2020
# Find Rust executables and paths
2121
#
22-
# RUST_FOUND - True if rust was found
23-
# CARGO_HOME - Cargo home folder
24-
# CARGO_EXECUTABLE - Cargo package manager executable path
25-
# RUSTC_EXECUTABLE - Rust compiler executable path
26-
# RUSTC_SYSROOT - Rust compiler root location (includes binaries and libraries)
27-
# RUSTC_LIBRARIES - Rust compiler runtime library list
28-
# RUSTDOC_EXECUTABLE - Rust doc executable plath
29-
# RUSTUP_EXECUTABLE - Rustup executable path
30-
# RUST_GDB_EXECUTABLE - Rust GDB debugger executable path
31-
# RUST_LLDB_EXECUTABLE - Rust LLDB debugger executable path
22+
# Rust_FOUND - True if rust was found
23+
# Rust_CARGO_HOME - Cargo home folder
24+
# Rust_CARGO_EXECUTABLE - Cargo package manager executable path
25+
# Rust_RUSTC_EXECUTABLE - Rust compiler executable path
26+
# Rust_RUSTC_VERSION - Rust compiler vesion string
27+
# Rust_RUSTC_SYSROOT - Rust compiler root location (includes binaries and libraries)
28+
# Rust_RUSTC_LIBRARIES - Rust compiler runtime library list
29+
# Rust_RUSTDOC_EXECUTABLE - Rust doc executable plath
30+
# Rust_RUSTUP_EXECUTABLE - Rustup executable path
31+
# Rust_GDB_EXECUTABLE - Rust GDB debugger executable path
32+
# Rust_LLDB_EXECUTABLE - Rust LLDB debugger executable path
3233

3334
if(WIN32)
3435
set(USER_HOME "$ENV{USERPROFILE}")
3536
else()
3637
set(USER_HOME "$ENV{HOME}")
3738
endif()
3839

39-
if(NOT DEFINED CARGO_HOME)
40-
if("$ENV{CARGO_HOME}" STREQUAL "")
41-
set(CARGO_HOME "${USER_HOME}/.cargo")
40+
if(NOT DEFINED Rust_CARGO_HOME)
41+
if("$ENV{Rust_CARGO_HOME}" STREQUAL "")
42+
set(Rust_CARGO_HOME "${USER_HOME}/.cargo")
4243
else()
43-
set(CARGO_HOME "$ENV{CARGO_HOME}")
44+
set(Rust_CARGO_HOME "$ENV{Rust_CARGO_HOME}")
4445
endif()
4546
endif()
4647

47-
set(CARGO_HOME "${CARGO_HOME}" CACHE PATH "Rust Cargo Home")
48+
set(Rust_CARGO_HOME "${Rust_CARGO_HOME}" CACHE PATH "Rust Cargo Home")
4849

49-
set(RUST_PATHS
50+
set(Rust_PATHS
5051
/usr
5152
/usr/local
52-
${CARGO_HOME}
53+
${Rust_CARGO_HOME}
5354
)
5455

55-
find_program(CARGO_EXECUTABLE cargo
56-
HINTS ${RUST_PATHS}
56+
find_program(Rust_RUSTUP_EXECUTABLE rustup
57+
HINTS ${Rust_PATHS}
5758
PATH_SUFFIXES "bin"
5859
)
5960

60-
find_program(RUSTC_EXECUTABLE rustc
61-
HINTS ${RUST_PATHS}
61+
if(Rust_RUSTUP_EXECUTABLE AND Rust_FIND_COMPONENTS)
62+
# Install the required toolchain (only one allowed by now)
63+
list(GET Rust_FIND_COMPONENTS 0 Rust_TOOLCHAIN)
64+
65+
if(Rust_TOOLCHAIN)
66+
execute_process(
67+
COMMAND ${Rust_RUSTUP_EXECUTABLE} toolchain install ${Rust_TOOLCHAIN} --force
68+
)
69+
execute_process(
70+
COMMAND ${Rust_RUSTUP_EXECUTABLE} default ${Rust_TOOLCHAIN}
71+
)
72+
73+
# Obtain toolchain full name and triplet (not needed for now)
74+
# execute_process(
75+
# COMMAND ${Rust_RUSTUP_EXECUTABLE} default
76+
# OUTPUT_VARIABLE Rust_TOOLCHAIN_FULL_NAME
77+
# OUTPUT_STRIP_TRAILING_WHITESPACE
78+
# )
79+
80+
# string(REPLACE " " ";" Rust_TOOLCHAIN_FULL_NAME ${Rust_TOOLCHAIN_FULL_NAME})
81+
# list(GET Rust_TOOLCHAIN_FULL_NAME 0 Rust_TOOLCHAIN_FULL_NAME)
82+
# string(REPLACE "${Rust_TOOLCHAIN}-" "" Rust_TOOLCHAIN_TRIPLET ${Rust_TOOLCHAIN_FULL_NAME})
83+
84+
set(Rust_TOOLCHAIN_COMPONENT_LIST
85+
cargo
86+
clippy
87+
llvm-tools-preview
88+
rls
89+
rust-analysis
90+
rust-analyzer-preview
91+
rust-docs
92+
rust-std
93+
rustc
94+
rustc-dev
95+
rustfmt
96+
rust-src
97+
)
98+
99+
foreach(Rust_TOOLCHAIN_COMPONENT ${Rust_TOOLCHAIN_COMPONENT_LIST})
100+
execute_process(
101+
COMMAND ${Rust_RUSTUP_EXECUTABLE} toolchain install ${Rust_TOOLCHAIN} --component ${Rust_TOOLCHAIN_COMPONENT}
102+
)
103+
endforeach()
104+
endif()
105+
106+
endif()
107+
108+
find_program(Rust_CARGO_EXECUTABLE cargo
109+
HINTS ${Rust_PATHS}
62110
PATH_SUFFIXES "bin"
63111
)
64112

65-
find_program(RUSTDOC_EXECUTABLE rustdoc
66-
HINTS ${RUST_PATHS}
113+
find_program(Rust_RUSTC_EXECUTABLE rustc
114+
HINTS ${Rust_PATHS}
67115
PATH_SUFFIXES "bin"
68116
)
69117

70-
find_program(RUSTUP_EXECUTABLE rustup
71-
HINTS ${RUST_PATHS}
118+
find_program(Rust_RUSTDOC_EXECUTABLE rustdoc
119+
HINTS ${Rust_PATHS}
72120
PATH_SUFFIXES "bin"
73121
)
74122

75-
find_program(RUST_GDB_EXECUTABLE rust-gdb
76-
HINTS ${RUST_PATHS}
123+
find_program(Rust_GDB_EXECUTABLE rust-gdb
124+
HINTS ${Rust_PATHS}
77125
PATH_SUFFIXES "bin"
78126
)
79127

80-
find_program(RUST_LLDB_EXECUTABLE rust-lldb
81-
HINTS ${RUST_PATHS}
128+
find_program(Rust_LLDB_EXECUTABLE rust-lldb
129+
HINTS ${Rust_PATHS}
82130
PATH_SUFFIXES "bin"
83131
)
84132

85-
if(RUSTC_EXECUTABLE)
133+
if(Rust_RUSTC_EXECUTABLE)
86134
execute_process(
87-
COMMAND ${RUSTC_EXECUTABLE} --version
88-
OUTPUT_VARIABLE RUSTC_VERSION
135+
COMMAND ${Rust_RUSTC_EXECUTABLE} --version
136+
OUTPUT_VARIABLE Rust_RUSTC_VERSION
89137
OUTPUT_STRIP_TRAILING_WHITESPACE
90138
)
91-
string(REGEX REPLACE "rustc ([^ ]+) .*" "\\1" RUSTC_VERSION "${RUSTC_VERSION}")
139+
string(REGEX REPLACE "rustc ([^ ]+) .*" "\\1" Rust_RUSTC_VERSION "${Rust_RUSTC_VERSION}")
92140

93141
execute_process(
94-
COMMAND ${RUSTC_EXECUTABLE} --print sysroot
95-
OUTPUT_VARIABLE RUSTC_SYSROOT
142+
COMMAND ${Rust_RUSTC_EXECUTABLE} --print sysroot
143+
OUTPUT_VARIABLE Rust_RUSTC_SYSROOT
96144
OUTPUT_STRIP_TRAILING_WHITESPACE
97145
)
98146

99147
file(
100-
GLOB RUSTC_LIBRARIES
101-
${RUSTC_SYSROOT}/lib/*${CMAKE_SHARED_LIBRARY_SUFFIX}
148+
GLOB Rust_RUSTC_LIBRARIES
149+
${Rust_RUSTC_SYSROOT}/lib/*${CMAKE_SHARED_LIBRARY_SUFFIX}
102150
)
103151
endif()
104152

105153
include(FindPackageHandleStandardArgs)
106154

107155
find_package_handle_standard_args(Rust
108-
FOUND_VAR RUST_FOUND
109-
REQUIRED_VARS CARGO_EXECUTABLE RUSTC_EXECUTABLE RUSTC_LIBRARIES
110-
VERSION_VAR RUSTC_VERSION
156+
FOUND_VAR Rust_FOUND
157+
REQUIRED_VARS Rust_CARGO_EXECUTABLE Rust_RUSTC_EXECUTABLE Rust_RUSTC_LIBRARIES
158+
VERSION_VAR Rust_RUSTC_VERSION
111159
)
112160

113161
mark_as_advanced(
114-
RUST_FOUND
115-
CARGO_EXECUTABLE
116-
RUSTC_EXECUTABLE
117-
RUSTC_SYSROOT
118-
RUSTC_LIBRARIES
119-
RUSTUP_EXECUTABLE
120-
RUSTDOC_EXECUTABLE
121-
RUST_GDB_EXECUTABLE
122-
RUST_LLDB_EXECUTABLE
162+
Rust_FOUND
163+
Rust_CARGO_EXECUTABLE
164+
Rust_RUSTC_EXECUTABLE
165+
Rust_RUSTC_SYSROOT
166+
Rust_RUSTC_LIBRARIES
167+
Rust_RUSTUP_EXECUTABLE
168+
Rust_RUSTDOC_EXECUTABLE
169+
Rust_GDB_EXECUTABLE
170+
Rust_LLDB_EXECUTABLE
123171
)

source/loaders/rs_loader/CMakeLists.txt

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,6 @@ endif()
77
# External dependencies
88
#
99

10-
find_package(Rust)
11-
12-
if(NOT RUST_FOUND)
13-
message(STATUS "Rust not found")
14-
return()
15-
endif()
16-
1710
add_subdirectory(rust)
1811

1912
#

source/loaders/rs_loader/rust/CMakeLists.txt

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@ endif()
77
# Plugin name and options
88
#
99

10-
find_package(Rust)
10+
file(READ ${CMAKE_CURRENT_SOURCE_DIR}/rust-toolchain TARGET_TOOLCHAIN)
11+
string(STRIP "${TARGET_TOOLCHAIN}" TARGET_TOOLCHAIN)
1112

12-
if(NOT RUST_FOUND)
13+
find_package(Rust COMPONENTS ${TARGET_TOOLCHAIN})
14+
15+
if(NOT Rust_FOUND)
1316
message(STATUS "Rust not found")
1417
return()
1518
endif()
@@ -41,12 +44,17 @@ endif()
4144
set(TARGET_OUTPUT ${TARGET_OUTPUT_PATH}/${TARGET_OUTPUT_NAME})
4245

4346
add_custom_target(${target}_runtime
44-
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${RUSTC_LIBRARIES} ${TARGET_OUTPUT_PATH}
47+
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${Rust_RUSTC_LIBRARIES} ${TARGET_OUTPUT_PATH}
4548
)
4649

4750
add_custom_target(${target} ALL
4851
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
4952
COMMAND ${CARGO_EXECUTABLE} build ${TARGET_BUILD_TYPE}
53+
# TODO: $ORIGIN does not work, but even using absolute path, the library librustc_driver depends on libstd and libLLVM
54+
# but they have the rpath hardcoded to the rustup folder, for mitigating this, we are using LD_LIBRARY_PATH in the test
55+
# although it may cause problems in the future in the distributable or docker builds, this must be reviewed
56+
# COMMAND ${CMAKE_COMMAND} -E env RUSTFLAGS='-C link-arg=-Wl,-rpath,${TARGET_OUTPUT_PATH}' ${CARGO_EXECUTABLE} build ${TARGET_BUILD_TYPE}
57+
# COMMAND ${CMAKE_COMMAND} -E env RUSTFLAGS='-C link-arg=-Wl,-rpath,\$ORIGIN' ${CARGO_EXECUTABLE} build ${TARGET_BUILD_TYPE}
5058
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${TARGET_BUILD_PATH} ${TARGET_OUTPUT}
5159
DEPENDS ${target}_runtime
5260
)
@@ -68,7 +76,7 @@ install(FILES
6876

6977
# Rust Runtime (pack the runtime meanwhile Rust runtime is not distributed as a library)
7078
install(FILES
71-
${RUSTC_LIBRARIES}
79+
${Rust_RUSTC_LIBRARIES}
7280
DESTINATION ${INSTALL_LIB}
7381
COMPONENT runtime
7482
)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
parallel-compiler = true
1+
parallel-compiler = true
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
nightly-2021-10-09

source/tests/metacall_rust_test/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,7 @@ include(TestEnvironmentVariables)
144144
test_environment_variables(${target}
145145
""
146146
${TESTS_ENVIRONMENT_VARIABLES}
147+
# TODO: This mitigates a bug in the Rust runtime libraries at runtime link time
148+
# Delete this line when we solve it, review: source/loaders/rs_loader/rust/CMakeLists.txt
149+
LD_LIBRARY_PATH=${PROJECT_BINARY_DIR}
147150
)

0 commit comments

Comments
 (0)