sourcegear_bridge_build/
lib.rs1
2use std::path::Path;
3use std::process::Command;
4
5const INITIAL_CSPROJ : &str =
6r#"
7<Project Sdk="Microsoft.NET.Sdk">
8
9 <PropertyGroup>
10 <TargetFramework>net8.0</TargetFramework>
11 <TrimmerSingleWarn>false</TrimmerSingleWarn>
12 <PublishAOT>True</PublishAOT>
13 </PropertyGroup>
14
15 <ItemGroup>
16 <PackageReference Include="sourcegear.bridge.nativeaot.rust" Version="[__SGBRIDGE_VERSION__]" />
17 </ItemGroup>
18
19 <Import Project="$(sgbridge_dir_for_crate)\deps.proj" Condition="exists('$(sgbridge_dir_for_crate)\deps.proj')" />
20
21</Project>
22"#;
23
24#[derive(Debug)]
25pub enum MyError {
26 IO(std::io::Error),
27 Cmd(std::process::ExitStatus),
28}
29
30impl From<std::io::Error> for MyError {
31 fn from(err: std::io::Error) -> MyError {
32 MyError::IO(err)
33 }
34}
35
36pub fn run() -> Result<(),MyError>
37{
38 let cur_dir = std::env::current_dir()?;
39 let sgbridge_dir_for_crate = Path::new(&cur_dir).join("sgbridge");
40
41 let out_dir = std::env::var_os("OUT_DIR").unwrap();
42 let os = std::env::var("CARGO_CFG_TARGET_OS").unwrap();
43 let arch = std::env::var("CARGO_CFG_TARGET_ARCH").unwrap();
44
45 let path_out_csproj = Path::new(&out_dir).join("dotnet.csproj");
50 let csproj_content = INITIAL_CSPROJ.replace("__SGBRIDGE_VERSION__", env!("CARGO_PKG_VERSION"));
51 std::fs::write(&path_out_csproj, csproj_content)?;
52
53 let rid =
54 if (os == "windows") && (arch == "x86_64")
55 {
56 "win-x64"
57 }
58 else if (os == "linux") && (arch == "x86_64")
59 {
60 "linux-x64"
61 }
62 else if (os == "macos") && (arch == "x86_64")
63 {
64 "osx-x64"
65 }
66 else if (os == "macos") && (arch == "aarch64")
67 {
68 "osx-arm64"
69 }
70 else
71 {
72 "TODO"
73 };
74
75 println!("cargo:rerun-if-changed=build.rs");
76
77 let path_deps = Path::new(&sgbridge_dir_for_crate).join("deps.proj");
78 let path_config = Path::new(&sgbridge_dir_for_crate).join("config.xml");
79
80 println!("cargo:rerun-if-changed={}",path_deps.display());
81 println!("cargo:rerun-if-changed={}",path_config.display());
82
83 let dir_prop = format!("sgbridge_dir_for_crate={}", sgbridge_dir_for_crate.display());
84 let status = Command::new("dotnet")
85 .args(&["publish", "--property", &dir_prop, "--property", "NativeLib=Static", "-r", rid])
86 .current_dir(&out_dir)
87 .status()?;
88 if status.success() {
89 return Ok(());
90 } else {
91 return Err(MyError::Cmd(status));
92 }
93
94}
95