diff options
| author | Emīls Piņķis <emils@mullvad.net> | 2018-04-03 18:16:47 +0100 |
|---|---|---|
| committer | Emīls Piņķis <emils@mullvad.net> | 2018-04-13 11:18:46 +0100 |
| commit | 2397bab569521dcd6d4b9670cd546e29f461a85b (patch) | |
| tree | f821730e0634ca2b9010fd9f9d1c6bee909e625c | |
| parent | 692671f73f8c4a03588359f1ab0f4e459fed1afa (diff) | |
| download | mullvadvpn-2397bab569521dcd6d4b9670cd546e29f461a85b.tar.xz mullvadvpn-2397bab569521dcd6d4b9670cd546e29f461a85b.zip | |
Add build.rs to build wfpctl.dll
| -rw-r--r-- | build_wfp.sh | 149 | ||||
| -rw-r--r-- | talpid-core/build.rs | 60 |
2 files changed, 209 insertions, 0 deletions
diff --git a/build_wfp.sh b/build_wfp.sh new file mode 100644 index 0000000000..8ffa6f9d6c --- /dev/null +++ b/build_wfp.sh @@ -0,0 +1,149 @@ +set -eu + +# List of solutions to build +WFP_SOLUTIONS=${WFP_SOLUTIONS:-"wfpctl"} + +# Override this variable to set your own list of build configurations for +# wfpctl +WFP_BUILD_MODES=${WFP_BUILD_MODES:-"Debug Release"} +# Override this variable to set different target platforms for wfpctl +WFP_BUILD_TARGETS=${WFP_BUILD_TARGETS:-"x86 x64"} +# Override this to set a different cargo target directory +CARGO_TARGET_DIR=${CARGO_TARGET_DIR:-"./target/"} + +# Builds all 4 variations of the wfpctl.dll library. Takes an argument that is +# the root of the WFP repository. +function build_wfpctl +{ + local path="$1/wfpctl.sln" + set -x + for mode in $WFP_BUILD_MODES; do + for target in $WFP_BUILD_TARGETS; do + msbuild.exe "$(to_win_path $path)" \ + //p:Configuration=$mode \ + //p:Platform=$target \ + //t:$WFP_SOLUTIONS + done + done + set +x +} + +function to_win_path +{ + echo $1 | sed -e 's/^\///' -e 's/\//\\/g' -e 's/^./\0:/' +} + +function copy_outputs +{ + local wfp_root_path=$1 + + for mode in $WFP_BUILD_MODES; do + for target in $WFP_BUILD_TARGETS; do + local dll_path=$(get_wfp_output_path $wfp_root_path $target $mode) + local cargo_target=$(get_cargo_target_dir $target $mode) + mkdir -p $cargo_target + cp "$dll_path/wfpctl.dll" $cargo_target + done + done + +} + +function get_wfp_output_path +{ + local wfp_root=$1 + local build_target=$2 + local build_mode=$3 + + case $build_target in + "x86") + echo "$wfp_root/bin/Win32-$build_mode" + ;; + "x64") + echo "$wfp_root/bin/x64-$build_mode" + ;; + *) + echo Unkown build target $build_target + exit 1 + ;; + esac +} + +# builds an appropriate cargo target path for the specified build target and +# build mode +function get_cargo_target_dir +{ + local build_target=$1 + local build_mode=$2 + + local host_arch=$(rustc_host_arch) + local host_target_arch=$(arch_from_build_target $host_arch) + local build_target_arch=$(arch_from_build_target $build_target) + # if the target is the same as the host, cargo omits the platform triplet + if [ "$host_target_arch" == "$build_target_arch" ]; then + platform_triplet="" + # otherwise, the cargo target path is build with the platform triplet + else + platform_triplet="$build_target_arch-pc-windows-msvc" + fi + + echo "$CARGO_TARGET_DIR/$platform_triplet/${build_mode,,}" +} + +# Rust isn't internally consistent w.r.t. architecture names - for build +# targets 32 bit x86 is calles i686, for hosts, it's x86. Hence these need to +# be converted. +function host_arch_to_target_arch +{ + local host_arch=$1 + + case $host_arch in + "x86") + echo "i686" + ;; + "x64") + echo "x86_64" + ;; + *) + echo $build_target + ;; + esac +} + +# Since Microsoft likes to name their architectures differently from Rust, this +# function tries to match microsoft names to Rust names. +function arch_from_build_target +{ + local build_target=$1 + + case $build_target in + "x86") + echo "i686" + ;; + "x64") + echo "x86_64" + ;; + *) + echo $build_target + ;; + esac +} + +function rustc_host_arch +{ + rustc --print cfg \ + | grep '^target_arch=' \ + | cut -d'=' -f2 \ + | tr -d '"' +} + + +function main +{ + + local wfp_root_path=${WFP_ROOT_PATH:-"$(pwd)/wfpctl"} + + build_wfpctl $wfp_root_path + copy_outputs $wfp_root_path +} + +main diff --git a/talpid-core/build.rs b/talpid-core/build.rs new file mode 100644 index 0000000000..0711ec2546 --- /dev/null +++ b/talpid-core/build.rs @@ -0,0 +1,60 @@ +#[cfg(windows)] +mod win { + use std::env; + use std::path::PathBuf; + + static WFP_BUILD_DIR: &'static str = "..\\wfpctl\\bin"; + + pub fn default_wfpctl_output_dir() -> PathBuf { + let target = env::var("TARGET").expect("TARGET env var not set"); + + let target_dir = match target.as_str() { + "i686-pc-windows-msvc" => format!("Win32-{}", get_build_mode()), + "x86_64-pc-windows-msvc" => format!("x64-{}", get_build_mode()), + _ => panic!("uncrecognized target: {}", target), + }; + + let mut lib_dir = manifest_dir(); + lib_dir.push(WFP_BUILD_DIR); + lib_dir.push(&target_dir); + + lib_dir + } + + fn manifest_dir() -> PathBuf { + env::var("CARGO_MANIFEST_DIR") + .map(PathBuf::from) + .expect("CARGO_MANIFEST_DIR env var not set") + } + + fn get_build_mode() -> &'static str { + let profile = env::var("PROFILE").expect("PROFILE env var not set"); + if profile == "release" { + "Release" + } else { + "Debug" + } + } +} + +#[cfg(windows)] +fn main() { + use win::*; + use std::env; + use std::path::PathBuf; + + let wfpctl_dir = env::var_os("WFPCTL_INCLUDE_DIR") + .map(PathBuf::from) + .unwrap_or_else(default_wfpctl_output_dir); + + println!( + "cargo:rustc-link-search={}", + wfpctl_dir + .to_str() + .expect("failed to construct path for wfpctl include directory") + ); + println!("cargo:rustc-link-lib=dylib=wfpctl"); +} + +#[cfg(not(windows))] +fn main() {} |
