Skip to content

Commit 931f028

Browse files
committed
Add base for integrating rs_loader with CMake.
1 parent a713231 commit 931f028

File tree

8 files changed

+170
-4
lines changed

8 files changed

+170
-4
lines changed

cmake/FindRust.cmake

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#
2+
# CMake Find Rust by Parra Studios
3+
# CMake script to find Rust compiler and tools.
4+
#
5+
# Copyright (C) 2016 - 2021 Vicente Eduardo Ferrer Garcia <[email protected]>
6+
#
7+
# Licensed under the Apache License, Version 2.0 (the "License");
8+
# you may not use this file except in compliance with the License.
9+
# You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS,
15+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
# See the License for the specific language governing permissions and
17+
# limitations under the License.
18+
#
19+
20+
# Find Rust executables and paths
21+
#
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+
# RUSTDOC_EXECUTABLE - Rust doc executable plath
27+
# RUSTUP_EXECUTABLE - Rustup executable path
28+
# RUST_GDB_EXECUTABLE - Rust GDB debugger executable path
29+
# RUST_LLDB_EXECUTABLE - Rust LLDB debugger executable path
30+
31+
if(WIN32)
32+
set(USER_HOME "$ENV{USERPROFILE}")
33+
else()
34+
set(USER_HOME "$ENV{HOME}")
35+
endif()
36+
37+
if(NOT DEFINED CARGO_HOME)
38+
if("$ENV{CARGO_HOME}" STREQUAL "")
39+
set(CARGO_HOME "${USER_HOME}/.cargo")
40+
else()
41+
set(CARGO_HOME "$ENV{CARGO_HOME}")
42+
endif()
43+
endif()
44+
45+
set(RUST_PATHS
46+
/usr
47+
/usr/local
48+
${CARGO_HOME}
49+
)
50+
51+
find_program(CARGO_EXECUTABLE cargo
52+
HINTS ${RUST_PATHS}
53+
PATH_SUFFIXES "bin"
54+
)
55+
56+
find_program(RUSTC_EXECUTABLE rustc
57+
HINTS ${RUST_PATHS}
58+
PATH_SUFFIXES "bin"
59+
)
60+
61+
find_program(RUSTDOC_EXECUTABLE rustdoc
62+
HINTS ${RUST_PATHS}
63+
PATH_SUFFIXES "bin"
64+
)
65+
66+
find_program(RUSTUP_EXECUTABLE rustup
67+
HINTS ${RUST_PATHS}
68+
PATH_SUFFIXES "bin"
69+
)
70+
71+
find_program(RUST_GDB_EXECUTABLE rust-gdb
72+
HINTS ${RUST_PATHS}
73+
PATH_SUFFIXES "bin"
74+
)
75+
76+
find_program(RUST_LLDB_EXECUTABLE rust-lldb
77+
HINTS ${RUST_PATHS}
78+
PATH_SUFFIXES "bin"
79+
)
80+
81+
if(CARGO_EXECUTABLE AND RUSTC_EXECUTABLE AND RUSTDOC_EXECUTABLE)
82+
set(CARGO_HOME "${CARGO_HOME}" CACHE PATH "Rust Cargo Home")
83+
execute_process(
84+
COMMAND ${RUSTC_EXECUTABLE} --version
85+
OUTPUT_VARIABLE RUSTC_VERSION
86+
OUTPUT_STRIP_TRAILING_WHITESPACE
87+
)
88+
string(REGEX REPLACE "rustc ([^ ]+) .*" "\\1" RUSTC_VERSION "${RUSTC_VERSION}")
89+
endif()
90+
91+
include(FindPackageHandleStandardArgs)
92+
93+
find_package_handle_standard_args(Rust
94+
FOUND_VAR RUST_FOUND
95+
REQUIRED_VARS CARGO_EXECUTABLE RUSTC_EXECUTABLE
96+
VERSION_VAR RUSTC_VERSION
97+
)
98+
99+
mark_as_advanced(
100+
RUST_FOUND
101+
CARGO_EXECUTABLE
102+
RUSTC_EXECUTABLE
103+
RUSTUP_EXECUTABLE
104+
RUSTDOC_EXECUTABLE
105+
RUST_GDB_EXECUTABLE
106+
RUST_LLDB_EXECUTABLE
107+
)

source/loaders/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ option(OPTION_BUILD_LOADERS_MOCK "Build mock loader loader plugin." ON)
2020
option(OPTION_BUILD_LOADERS_NODE "Build NodeJS v12.21.0 JavaScript Runtime loader plugin." OFF)
2121
option(OPTION_BUILD_LOADERS_PY "Build Python 3.9 C API loader plugin." OFF)
2222
option(OPTION_BUILD_LOADERS_RB "Build Ruby 2.7 C API loader plugin." OFF)
23+
option(OPTION_BUILD_LOADERS_RS "Build Rust 1.55.0 loader plugin." OFF)
2324
option(OPTION_BUILD_LOADERS_RPC "Build cURL Remote Procedure Call loader plugin." OFF)
2425
option(OPTION_BUILD_LOADERS_TS "Build TypeScript 3.9.7 Runtime loader plugin." OFF)
2526
option(OPTION_BUILD_LOADERS_WASM "Build WebAssembly Virtual Machine loader plugin." OFF)
@@ -41,6 +42,7 @@ add_subdirectory(mock_loader) # Mock loader plugin without dependencies (core te
4142
add_subdirectory(node_loader) # NodeJS v12.21.0 JavaScript Runtime
4243
add_subdirectory(py_loader) # Python 3.9 C API
4344
add_subdirectory(rb_loader) # Ruby 2.7 C API
45+
add_subdirectory(rs_loader) # Rust 1.55.0
4446
add_subdirectory(rpc_loader) # cURL Remote Procedure Call
4547
add_subdirectory(ts_loader) # TypeScript 3.9.7
4648
add_subdirectory(wasm_loader) # WebAssembly Virtual Machine
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Check if this loader is enabled
2+
if(NOT OPTION_BUILD_LOADERS OR NOT OPTION_BUILD_LOADERS_RS)
3+
return()
4+
endif()
5+
6+
#
7+
# Plugin name and options
8+
#
9+
10+
find_package(Rust)
11+
12+
if(NOT RUST_FOUND)
13+
message(STATUS "Rust not found")
14+
return()
15+
endif()
16+
17+
# Target name
18+
set(target rs_loader)
19+
20+
# Exit here if required dependencies are not met
21+
message(STATUS "Plugin ${target}")
22+
23+
set(TARGET_BUILD_NAME "${CMAKE_SHARED_LIBRARY_PREFIX}rs_loader${CMAKE_SHARED_LIBRARY_SUFFIX}")
24+
25+
if(CMAKE_BUILD_TYPE STREQUAL "Release")
26+
set(TARGET_BUILD_TYPE "--release")
27+
set(TARGET_BUILD_PATH "${CMAKE_CURRENT_SOURCE_DIR}/target/release/${TARGET_BUILD_NAME}")
28+
set(TARGET_OUTPUT_NAME "${CMAKE_SHARED_LIBRARY_PREFIX}rs_loader${CMAKE_SHARED_LIBRARY_SUFFIX}")
29+
else()
30+
set(TARGET_BUILD_TYPE)
31+
set(TARGET_BUILD_PATH "${CMAKE_CURRENT_SOURCE_DIR}/target/debug/${TARGET_BUILD_NAME}")
32+
set(TARGET_OUTPUT_NAME "${CMAKE_SHARED_LIBRARY_PREFIX}rs_loaderd${CMAKE_SHARED_LIBRARY_SUFFIX}")
33+
endif()
34+
35+
if(MSVC)
36+
set(TARGET_OUTPUT_PATH "${PROJECT_BINARY_DIR}/${CMAKE_BUILD_TYPE}")
37+
else()
38+
set(TARGET_OUTPUT_PATH "${PROJECT_BINARY_DIR}")
39+
endif()
40+
41+
# Build without internet access
42+
add_custom_target(${target}
43+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
44+
COMMAND ${CARGO_EXECUTABLE} build ${TARGET_BUILD_TYPE}
45+
COMMAND ${CMAKE_COMMAND} -E copy ${TARGET_BUILD_PATH} ${TARGET_OUTPUT_PATH}/${TARGET_OUTPUT_NAME}
46+
)
47+
48+
#
49+
# Deployment
50+
#
51+
52+
# Files
53+
install(FILES
54+
${TARGET_OUTPUT_PATH}/${TARGET_OUTPUT_NAME}
55+
DESTINATION ${INSTALL_LIB}
56+
COMPONENT runtime
57+
)

source/loaders/rs_loader/Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

source/loaders/rs_loader/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ rustc_private = true
1515
opt-level = "z"
1616

1717
[workspace]
18-
members = ["functions_extractor"]
18+
members = ["parser"]

source/loaders/rs_loader/functions_extractor/Cargo.toml renamed to source/loaders/rs_loader/parser/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[package]
2-
name = "functions_extractor"
2+
name = "parser"
33
version = "0.1.0"
44
edition = "2018"
55

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub struct ParsedRustFunction {
3232
arguments: Vec<ParsedRustFunctionArguments>,
3333
}
3434

35-
pub fn functions_extractor(parse_source: Crate) -> Vec<ParsedRustFunction> {
35+
pub fn parse(parse_source: Crate) -> Vec<ParsedRustFunction> {
3636
let mut parsed_rust_functions: Vec<ParsedRustFunction> = Vec::new();
3737

3838
for parsed_item in parse_source.items.iter() {

0 commit comments

Comments
 (0)