Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 35 additions & 9 deletions alvr/filesystem/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ pub struct Layout {
// (linux only) directory where the vulkan layer manifest is saved
pub vulkan_layer_manifest_dir: PathBuf,
pub launcher_root: Option<PathBuf>,

pub platform: Option<&'static str>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why should this be an Option?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my comment bellow. There is no valid reason, I just wanted to keep the existing build system untouched.

In this specific case, it allows me to override the defaut behavior of openvr_driver_lib_dir() with a new constructor new_cross_windows().

}

impl Layout {
Expand Down Expand Up @@ -154,6 +156,7 @@ impl Layout {
.and_then(|p| p.parent())
.and_then(|p| p.parent())
.map(|p| p.to_owned()),
platform: None,
}
}
#[cfg(not(target_os = "linux"))]
Expand All @@ -170,6 +173,25 @@ impl Layout {
ufw_config_dir: root.to_owned(),
vulkan_layer_manifest_dir: root.to_owned(),
launcher_root: root.parent().and_then(|p| p.parent()).map(|p| p.to_owned()),
platform: None,
}
}

pub fn new_cross_windows(root: &Path) -> Self {
Self {
executables_dir: root.to_owned(),
libraries_dir: root.to_owned(),
static_resources_dir: root.to_owned(),
config_dir: root.to_owned(),
log_dir: root.to_owned(),
openvr_driver_root_dir: root.to_owned(),
vrcompositor_wrapper_dir: root.to_owned(),
firewall_script_dir: root.to_owned(),
firewalld_config_dir: root.to_owned(),
ufw_config_dir: root.to_owned(),
vulkan_layer_manifest_dir: root.to_owned(),
launcher_root: root.parent().and_then(|p| p.parent()).map(|p| p.to_owned()),
platform: Some("win64"),
}
}

Expand Down Expand Up @@ -228,15 +250,19 @@ impl Layout {
}

pub fn openvr_driver_lib_dir(&self) -> PathBuf {
let platform = if cfg!(windows) {
"win64"
} else if cfg!(target_os = "linux") {
"linux64"
} else if cfg!(target_os = "macos") {
"macos"
} else {
unimplemented!()
};
let platform = self.platform.unwrap_or_else(|| {
// Those tests aren't "working" while cross compiling
// since this is called in build.rs, which is a host tool
if cfg!(windows) {
"win64"
} else if cfg!(target_os = "linux") {
"linux64"
} else if cfg!(target_os = "macos") {
"macos"
} else {
unimplemented!()
}
});

self.openvr_driver_root_dir.join("bin").join(platform)
}
Expand Down
82 changes: 82 additions & 0 deletions alvr/server_openvr/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,90 @@ fn get_linux_x264_path() -> PathBuf {
alvr_filesystem::deps_dir().join("linux/x264/alvr_build")
}

fn cross_windows_build() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if possible I would like to not duplicate the build code

let platform_name = "windows";
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());

let platform_subpath = "cpp/platform/win32";

let common_iter = walkdir::WalkDir::new("cpp")
.into_iter()
.filter_entry(|entry| {
entry.file_name() != "tools"
&& entry.file_name() != "platform"
&& (platform_name != "macos" || entry.file_name() != "amf")
&& (platform_name != "linux" || entry.file_name() != "amf")
});

let platform_iter = walkdir::WalkDir::new(platform_subpath).into_iter();

let cpp_paths = common_iter
.chain(platform_iter)
.filter_map(|maybe_entry| maybe_entry.ok())
.map(|entry| entry.into_path())
.collect::<Vec<_>>();

let source_files_paths = cpp_paths.iter().filter(|path| {
path.extension()
.filter(|ext| {
let ext_str = ext.to_string_lossy();
ext_str == "c" || ext_str == "cpp"
})
.is_some()
});

let mut build = cc::Build::new();
build
.cpp(true)
.std("c++17")
.files(source_files_paths)
.include(alvr_filesystem::workspace_dir().join("openvr/headers"))
.include("cpp")
.flag("/MD")
.flag("/EHsc")
.debug(false) // This is because we cannot link to msvcrtd (see below)
.flag("/permissive-")
.define("NOMINMAX", None)
.define("_WINSOCKAPI_", None)
.define("_MBCS", None)
.define("_MT", None);

#[cfg(debug_assertions)]
build.define("ALVR_DEBUG_LOG", None);

build.compile("bindings");

bindgen::builder()
.clang_arg("-xc++")
.header("cpp/alvr_server/bindings.h")
.derive_default(true)
.generate()
.unwrap()
.write_to_file(out_dir.join("bindings.rs"))
.unwrap();

println!(
"cargo:rustc-link-search=native={}",
alvr_filesystem::workspace_dir()
.join("openvr/lib/win64")
.to_string_lossy()
);
println!("cargo:rustc-link-lib=openvr_api");

for path in cpp_paths {
println!("cargo:rerun-if-changed={}", path.to_string_lossy());
}
}

fn main() {
let platform_name = env::var("CARGO_CFG_TARGET_OS").unwrap();

let cross_to_windows = cfg!(not(target_os = "windows")) && platform_name == "windows";
if cross_to_windows {
cross_windows_build();
return;
}

let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());

let platform_subpath = match platform_name.as_str() {
Expand Down
10 changes: 5 additions & 5 deletions alvr/server_openvr/cpp/alvr_server/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
#include <chrono>
#ifdef _WIN32
#pragma warning(disable : 4005)
#include <WinSock2.h>
#include <winsock2.h>
#pragma warning(default : 4005)
#include <WS2tcpip.h>
#include <WinInet.h>
#include <Windows.h>
#include <d3d11.h>
#include <delayimp.h>
#include <stdint.h>
#include <string>
#include <vector>
#include <windows.h>
#include <wininet.h>
#include <ws2tcpip.h>
#define _USE_MATH_DEFINES
#include <VersionHelpers.h>
#include <versionhelpers.h>
#else
#include <arpa/inet.h>
#include <netinet/in.h>
Expand Down
2 changes: 1 addition & 1 deletion alvr/server_openvr/cpp/platform/win32/CrashHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

#include "../../alvr_server/Logger.h"
#include "../../shared/backward.hpp"
#include <Windows.h>
#include <ostream>
#include <windows.h>

static LONG WINAPI handler(PEXCEPTION_POINTERS ptrs) {
backward::StackTrace stacktrace;
Expand Down
2 changes: 1 addition & 1 deletion alvr/server_openvr/cpp/platform/win32/FrameRender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,7 @@ bool FrameRender::RenderFrame(
m_pD3DRender->GetContext()->OMSetBlendState(m_pBlendState.Get(), NULL, 0xffffffff);
}

int inputColorAdjust = 0;
uint32_t inputColorAdjust = 0;
if (Settings::Instance().m_enableHdr) {
if (SRVDesc.Format == DXGI_FORMAT_R8G8B8A8_UNORM_SRGB
|| SRVDesc.Format == DXGI_FORMAT_B8G8R8A8_UNORM_SRGB
Expand Down
1 change: 0 additions & 1 deletion alvr/server_openvr/cpp/platform/win32/NvEncoderD3D11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#include <dlfcn.h>
#endif
#include "NvEncoderD3D11.h"
#include <D3D9Types.h>

#ifndef MAKEFOURCC
#define MAKEFOURCC(a,b,c,d) (((unsigned int)a) | (((unsigned int)b)<< 8) | (((unsigned int)c)<<16) | (((unsigned int)d)<<24) )
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "RenderUtils.h"

#include <D3d11_4.h>
#include <d3d11_4.h>

using namespace std::string_literals;
using Microsoft::WRL::ComPtr;
Expand Down
8 changes: 4 additions & 4 deletions alvr/server_openvr/cpp/platform/win32/shared/d3drender.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
//===================== Copyright (c) Valve Corporation. All Rights Reserved. ======================
#include "d3drender.h"
#include <D3d11_4.h>
#include <Evntprov.h>
#include <d3d11_4.h>
#include <evntprov.h>

#pragma comment( lib, "dxgi.lib" )
#pragma comment( lib, "d3d11.lib" )
#pragma comment( lib, "Rpcrt4.lib" )
#pragma comment( lib, "rpcrt4.lib" )

#define Log( ... )

Expand Down Expand Up @@ -90,7 +90,7 @@ namespace
public:
CEventHelper()
{
UuidFromString( ( RPC_CSTR ) "8c8f13b1-60eb-4b6a-a433-de86104115ac", &guid );
UuidFromStringA( ( RPC_CSTR ) "8c8f13b1-60eb-4b6a-a433-de86104115ac", &guid );
EventRegister( &guid, nullptr, nullptr, &handle );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ amf_pts AMF_CDECL_CALL amf_high_precision_clock()
#endif
}
//-------------------------------------------------------------------------------------------------
#pragma comment (lib, "Winmm.lib")
#pragma comment (lib, "winmm.lib")
static amf_uint32 timerPrecision = 1;

void AMF_CDECL_CALL amf_increase_timer_precision()
Expand Down
128 changes: 127 additions & 1 deletion alvr/xtask/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{
env,
fmt::{self, Display, Formatter},
fs,
path::PathBuf,
path::{Path, PathBuf},
vec,
};
use xshell::{cmd, Shell};
Expand Down Expand Up @@ -71,6 +71,132 @@ pub fn build_server_lib(profile: Profile, root: Option<String>, reproducible: bo
cmd!(sh, "cbindgen --output {out}").run().unwrap();
}

pub fn cross_build_windows_streamer(
profile: Profile,
root: Option<String>,
reproducible: bool,
profiling: bool,
keep_config: bool,
) {
let sh = Shell::new().unwrap();

let cargo_target = "x86_64-pc-windows-msvc";
let target_dir = afs::target_dir().join(cargo_target);

println!("Cross compiling for Windows using target {cargo_target}");
println!("Target dir: {}", target_dir.display());

let mut common_flags = vec!["--target", cargo_target];
match profile {
Profile::Distribution => {
common_flags.push("--profile");
common_flags.push("distribution");
}
Profile::Release => common_flags.push("--release"),
Profile::Debug => (),
}
if reproducible {
common_flags.push("--locked");
}

let streamer_build_dir = afs::build_dir().join("alvr_streamer_windows");
println!("Streamer build dir: {}", streamer_build_dir.display());
let build_layout = Layout::new_cross_windows(&streamer_build_dir);

let artifacts_dir = afs::target_dir()
.join(cargo_target)
.join(profile.to_string());
println!("Artifacts dir: {}", artifacts_dir.display());

let common_flags_ref = &common_flags;

let maybe_config = if keep_config {
fs::read_to_string(build_layout.session()).ok()
} else {
None
};

sh.remove_path(afs::streamer_build_dir()).ok();
sh.create_dir(build_layout.openvr_driver_lib_dir()).unwrap();
sh.create_dir(&build_layout.executables_dir).unwrap();

if let Some(config) = maybe_config {
fs::write(build_layout.session(), config).ok();
}

if let Some(root) = root {
sh.set_var("ALVR_ROOT_DIR", root);
}

// build server
{
let profiling_flag = if profiling {
vec!["--features", "alvr_server_core/trace-performance"]
} else {
vec![]
};

let _push_guard = sh.push_dir(afs::crate_dir("server_openvr"));
cmd!(sh, "cargo build {common_flags_ref...} {profiling_flag...}")
.run()
.unwrap();

sh.copy_file(
artifacts_dir.join("alvr_server_openvr.dll"),
build_layout
.openvr_driver_lib_dir()
.join("driver_alvr_server.dll"),
)
.unwrap();

sh.copy_file(
artifacts_dir.join("alvr_server_openvr.pdb"),
build_layout
.openvr_driver_lib_dir()
.join("alvr_server_openvr.pdb"),
)
.unwrap();

sh.copy_file(
afs::workspace_dir().join("openvr/bin/win64/openvr_api.dll"),
build_layout.openvr_driver_lib_dir(),
)
.unwrap();

// Bring along the c++ runtime
command::copy_recursive(
&sh,
&afs::crate_dir("server_openvr").join("cpp/bin/windows"),
&build_layout.openvr_driver_lib_dir(),
)
.unwrap();
}

// Build dashboard
{
let _push_guard = sh.push_dir(afs::crate_dir("dashboard"));
cmd!(sh, "cargo build {common_flags_ref...}").run().unwrap();

let dashboard_fname = "ALVR Dashboard.exe";

sh.copy_file(
artifacts_dir.join("alvr_dashboard.exe"),
build_layout.executables_dir.join(dashboard_fname),
)
.unwrap();
}

// copy static resources
{
// copy driver manifest
sh.copy_file(
afs::crate_dir("xtask").join("resources/driver.vrdrivermanifest"),
build_layout.openvr_driver_manifest(),
)
.unwrap();
}
}

pub fn build_streamer(
profile: Profile,
gpl: bool,
Expand Down
Loading