xref: /third_party/rust/crates/cxx/tools/cargo/build.rs (revision 33d722a9)
1use std::io::{self, Write};
2#[cfg(windows)]
3use std::os::windows::fs as windows;
4use std::path::Path;
5use std::process;
6#[cfg(windows)]
7use std::{env, fs};
8
9const MISSING: &str = "
10~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11When building `cxx` from a git clone, git's symlink support needs
12to be enabled on platforms that have it off by default (Windows).
13Either use:
14
15   $ git config --global core.symlinks true
16
17prior to cloning, or else use:
18
19   $ git clone -c core.symlinks=true https://github.com/dtolnay/cxx
20
21for the clone.
22
23Symlinks are only required when compiling locally from a clone of
24the git repository---they are NOT required when building `cxx` as
25a Cargo-managed (possibly transitive) build dependency downloaded
26through crates.io.
27~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
28";
29
30#[cfg(windows)]
31const DENIED: &str = "
32~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
33When building `cxx` from a git clone on Windows we need Developer
34Mode enabled for symlink support.
35
36To enable Developer Mode: go under Settings to Update & Security,
37then 'For developers', and turn on the toggle for Developer Mode.
38
39For more explanation of symlinks in Windows, see these resources:
40> https://blogs.windows.com/windowsdeveloper/2016/12/02/symlinks-windows-10/
41> https://docs.microsoft.com/windows/uwp/get-started/enable-your-device-for-development
42
43Symlinks are only required when compiling locally from a clone of
44the git repository---they are NOT required when building `cxx` as
45a Cargo-managed (possibly transitive) build dependency downloaded
46through crates.io.
47~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
48";
49
50fn main() {
51    if Path::new("src/syntax/mod.rs").exists() {
52        return;
53    }
54
55    #[allow(unused_mut)]
56    let mut message = MISSING;
57
58    #[cfg(windows)]
59    if let Some(out_dir) = env::var_os("OUT_DIR") {
60        let parent_dir = Path::new(&out_dir).join("symlink");
61        let original_dir = parent_dir.join("original");
62        let link_dir = parent_dir.join("link");
63        if fs::create_dir_all(&original_dir).is_ok()
64            && (!link_dir.exists() || fs::remove_dir(&link_dir).is_ok())
65            && windows::symlink_dir(&original_dir, &link_dir).is_err()
66        {
67            message = DENIED;
68        }
69    }
70
71    let _ = io::stderr().write_all(message.as_bytes());
72    process::exit(1);
73}
74