Skip to content

Commit

Permalink
save linux work
Browse files Browse the repository at this point in the history
  • Loading branch information
youyuanwu committed Nov 19, 2023
1 parent f67bf95 commit a88df0a
Show file tree
Hide file tree
Showing 19 changed files with 321 additions and 80 deletions.
4 changes: 4 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[target.x86_64-unknown-linux-gnu]
rustflags = [
"-C", "link-arg=-fuse-ld=lld",
]
28 changes: 28 additions & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
FROM ubuntu:20.04

RUN apt-get update && apt-get upgrade

# install sf
RUN apt-get install apt-transport-https curl lsb-release wget gnupg2 software-properties-common debconf-utils -y

RUN wget -q https://packages.microsoft.com/config/ubuntu/$(lsb_release -rs)/packages-microsoft-prod.deb \
&& dpkg -i packages-microsoft-prod.deb \
&& curl -fsSL https://packages.microsoft.com/keys/msopentech.asc | apt-key add - \
&& curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add - \
&& add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" \
&& apt-get update

RUN echo "servicefabric servicefabric/accepted-eula-ga select true" | debconf-set-selections \
&& echo "servicefabricsdkcommon servicefabricsdkcommon/accepted-eula-ga select true" | debconf-set-selections \
&& apt-get install servicefabricsdkcommon -y

# install rust
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs > ./rustup.sh
RUN chmod u+x ./rustup.sh && ./rustup.sh -y

# more tools
RUN apt-get install cmake git -y
RUN apt-get install clang lld -y

# expose sf shared libs
ENV LD_LIBRARY_PATH "$LD_LIBRARY_PATH:/opt/microsoft/servicefabric/bin/Fabric/Fabric.Code:/workspaces/service-fabric-rs/bintemp"
33 changes: 33 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/cpp
{
"name": "C++",
"build": {
"dockerfile": "Dockerfile"
},

// "image" : "fabric-metadata:v1",

// Features to add to the dev container. More info: https://containers.dev/features.
// "features": {},

// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],

// Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "gcc -v",

// Configure tool-specific properties.
// "customizations": {},

"customizations": {
"vscode": {
"extensions": [
"ms-vscode.cpptools-extension-pack",
"rust-lang.rust-analyzer"]
}
}

// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
// "remoteUser": "root"
}
52 changes: 50 additions & 2 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
BUILD_TYPE: ["Debug"]
os: [ windows-latest ]
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4

- name: Get MSVC
uses: ilammy/msvc-dev-cmd@v1
Expand Down Expand Up @@ -46,4 +46,52 @@ jobs:
run: Powershell.exe -File .\build\_deps\service_fabric_cpp-src\scripts\check_cluster_online.ps1

- name: run echo example app
run: Powershell.exe -File .\tests\echo_script_test.ps1
run: Powershell.exe -File .\tests\echo_script_test.ps1

build-linux:
runs-on: ${{ matrix.os }}
strategy:
matrix:
BUILD_TYPE: ["Debug"]
os: [ ubuntu-20.04 ]
steps:
- uses: actions/checkout@v4
- name: apt-get
run: sudo apt-get update && sudo apt-get install apt-transport-https curl lsb-release wget gnupg2 software-properties-common debconf-utils clang lld -y

- name: install sf
run: |
wget -q https://packages.microsoft.com/config/ubuntu/$(lsb_release -rs)/packages-microsoft-prod.deb;
sudo dpkg -i packages-microsoft-prod.deb;
curl -fsSL https://packages.microsoft.com/keys/msopentech.asc | sudo apt-key add - ;
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - ;
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" ;
sudo apt-get update;
echo "servicefabric servicefabric/accepted-eula-ga select true" | sudo debconf-set-selections ;
echo "servicefabricsdkcommon servicefabricsdkcommon/accepted-eula-ga select true" | sudo debconf-set-selections ;
sudo apt-get install servicefabricsdkcommon -y
- name: Get specific version CMake, v3.21.2
uses: lukka/[email protected]

- name: Install rust stable
uses: actions-rs/toolchain@v1
with:
toolchain: stable

- name: run cmake
run: >
cmake . -DCMAKE_BUILD_TYPE=${{ matrix.BUILD_TYPE }} -B build
- name: run build
run: cmake --build build --config ${{ matrix.BUILD_TYPE }}


# - uses: actions/checkout@v2

# - name: apt-get
# run: sudo apt-get install apt-transport-https curl lsb-release wget gnupg2 software-properties-common debconf-utils -y

# - name: Install rust stable
# uses: actions-rs/toolchain@v1
# with:
# toolchain: stable
23 changes: 19 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ endif()
# generate rust code
find_program (
cargo_exe
NAMES cargo.exe
NAMES cargo.exe cargo
REQUIRED
)

Expand All @@ -48,7 +48,7 @@ add_custom_target(build_rust
add_custom_target(build_rust_sample_client ALL
COMMAND ${cargo_exe} build -p samples_client
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
DEPENDS generate_import_libs
#DEPENDS generate_import_libs
)

add_custom_target(run_rust_sample_client
Expand All @@ -65,12 +65,27 @@ add_custom_target(force_clean
add_custom_target(build_rust_sample_echomain ALL
COMMAND ${cargo_exe} build -p samples_echomain
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
DEPENDS generate_import_libs
DEPENDS build_fabric_rust_pal
)

add_custom_target(build_fabric_rust_pal ALL
COMMAND ${cargo_exe} build -p pal
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)

set(_pkg_root ${CMAKE_BINARY_DIR}/echoapp_root)
set(_pkg_src ${CMAKE_CURRENT_SOURCE_DIR}/crates/samples/echomain)
set(_pkg_exe ${CMAKE_CURRENT_SOURCE_DIR}/target/debug/samples_echomain.exe)
if(WIN32)
set(_pkg_exe ${CMAKE_CURRENT_SOURCE_DIR}/target/debug/samples_echomain.exe)
else()
set(_pkg_exe ${CMAKE_CURRENT_SOURCE_DIR}/target/debug/samples_echomain)
set(_pal_so ${CMAKE_CURRENT_SOURCE_DIR}/target/debug/libfabric_rust_pal.so)
add_custom_command(TARGET build_rust_sample_echomain POST_BUILD
COMMAND ${CMAKE_COMMAND} -E make_directory ${_pkg_root}
COMMAND ${CMAKE_COMMAND}
-E copy_if_different ${_pal_so} ${_pkg_root}/EchoAppServicePackage/Code/libfabric_rust_pal.so
)
endif(WIN32)
add_custom_command(TARGET build_rust_sample_echomain POST_BUILD
COMMAND ${CMAKE_COMMAND} -E make_directory ${_pkg_root}
COMMAND ${CMAKE_COMMAND}
Expand Down
10 changes: 9 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ members = [
]

[package.metadata.docs.rs]
default-target = "x86_64-pc-windows-msvc"
# default-target = "x86_64-pc-windows-msvc"
targets = []

[dependencies]
fabric-metadata = { git = "https://github.com/youyuanwu/fabric-metadata.git", rev = "9d62ca15573a0716fb9828dce03a489ae4b8a4a7"}
fabric-metadata = { git = "https://github.com/youyuanwu/fabric-metadata.git", rev = "2e6a471c7752148c4dc8d0968c0f1161c2a6c76a"}
pal = { path = "./crates/fabric/pal" }
windows-core = "0.51"

[dependencies.windows]
Expand Down
19 changes: 10 additions & 9 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
//use std::{path::Path, env};
use std::path::Path;

fn main() {
// add link dir for fabric support libs. This is propagated to downstream targets
// let dir = String::from("build\\_deps\\fabric_metadata-build\\src");
// let package_root = env::var("CARGO_MANIFEST_DIR").unwrap();
// let abs_dir = package_root + &dir;
// println!(
// "cargo:rustc-link-search=native={}",
// Path::new(&abs_dir).display()
// );
if cfg!(unix) {
// Add link dir for fabric libs on linux.
let dir = String::from("/opt/microsoft/servicefabric/bin/Fabric/Fabric.Code/");
println!("cargo:rustc-link-search={}", Path::new(&dir).display());

// On linux, for windows-rs to work we need have a pal shared lib.
// No need to have search dir since it is in the target dir
println!("cargo:rustc-link-lib=dylib=fabric_rust_pal");
}
}
20 changes: 7 additions & 13 deletions crates/fabric/ext/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,8 @@

pub mod fasync;

use std::os::windows::prelude::OsStrExt;
use std::{
ffi::OsString,
sync::{Arc, Condvar, Mutex},
};
//use std::os::windows::prelude::OsStrExt;
use std::sync::{Arc, Condvar, Mutex};

use log::info;
use service_fabric_rs::FabricCommon::{
Expand All @@ -21,7 +18,7 @@ use service_fabric_rs::FabricCommon::{
IFabricAsyncOperationContext_Impl, IFabricStringResult, IFabricStringResult_Impl,
};
use windows::core::implement;
use windows_core::PCWSTR;
use windows_core::{HSTRING, PCWSTR};

// Interface for waitable async callback.
// This is a common use case to combine fabric Begin* and End* apis.
Expand Down Expand Up @@ -144,13 +141,10 @@ pub struct StringResult {
}

impl StringResult {
pub fn new(data: OsString) -> StringResult {
let data_vec = data
.as_os_str()
.encode_wide()
.chain(Some(0))
.collect::<Vec<_>>();
let ret = StringResult { vec_: data_vec };
pub fn new(data: HSTRING) -> StringResult {
let ret = StringResult {
vec_: data.as_wide().to_vec(),
};
return ret;
}
}
Expand Down
19 changes: 19 additions & 0 deletions crates/fabric/pal/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
name = "pal"
version = "0.1.0"
edition = "2021"


[lib]
name = "fabric_rust_pal"
path = "src/lib.rs"
crate-type = ["cdylib"]


# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies.windows]
version = "0.51"
features = [
"Win32_Foundation"
]
2 changes: 2 additions & 0 deletions crates/fabric/pal/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#[cfg(unix)]
pub mod pal;
83 changes: 83 additions & 0 deletions crates/fabric/pal/src/pal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#[allow(non_camel_case_types, non_snake_case, dead_code)]
use std::ffi::c_void;

use windows::core::imp::LOAD_LIBRARY_FLAGS;
use windows::{
core::{HRESULT, PCSTR, PWSTR},
Win32::Foundation::{HANDLE, HMODULE},
};

#[no_mangle]
pub unsafe extern "system" fn GetLastError() -> u32 {
0
}

#[no_mangle]
pub unsafe extern "system" fn GetProcessHeap() -> isize {
0
}

#[no_mangle]
pub unsafe extern "system" fn HeapAlloc(_heap: isize, _flags: u32, _len: usize) -> *mut c_void {
std::ptr::null_mut()
}

#[no_mangle]
pub unsafe extern "system" fn HeapFree(_heap: isize, _flags: u32, _ptr: *const c_void) -> i32 {
0
}

#[no_mangle]
pub unsafe extern "system" fn GetErrorInfo(_reserved: u32, _info: *mut *mut c_void) -> HRESULT {
HRESULT(0)
}

#[no_mangle]
pub unsafe extern "system" fn SetErrorInfo(_reserved: u32, _info: *const c_void) -> HRESULT {
HRESULT(0)
}

#[no_mangle]
pub unsafe extern "system" fn LoadLibraryA(_name: PCSTR) -> isize {
0
}

#[no_mangle]
pub unsafe extern "system" fn LoadLibraryExA(
_lplibfilename: PCSTR,
_hfile: HANDLE,
_dwflags: LOAD_LIBRARY_FLAGS,
) -> HMODULE {
windows::Win32::Foundation::HMODULE(0)
}

#[no_mangle]
pub unsafe extern "system" fn FreeLibrary(_library: isize) -> i32 {
0
}

#[no_mangle]
pub unsafe extern "system" fn GetProcAddress(_library: isize, _name: PCSTR) -> *const c_void {
std::ptr::null()
}

#[no_mangle]
pub unsafe extern "system" fn SysFreeString(_bstr: *const u16) {}

#[no_mangle]
pub unsafe extern "system" fn SysStringLen(_bstr: *const u16) -> u32 {
0
}

#[no_mangle]
pub unsafe extern "system" fn FormatMessageW(
_flags: u32,
_source: *const c_void,
_code: u32,
_lang: u32,
_buffer: PWSTR,
_len: u32,
_args: *const *const i8,
) -> u32 {
0
}
Loading

0 comments on commit a88df0a

Please sign in to comment.