1 use std::io::{self, Write};
2 #[cfg(windows)]
3 use std::os::windows::fs as windows;
4 use std::path::Path;
5 use std::process;
6 #[cfg(windows)]
7 use std::{env, fs};
8 
9 const MISSING: &str = "
10 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11 When building `cxx` from a git clone, git's symlink support needs
12 to be enabled on platforms that have it off by default (Windows).
13 Either use:
14 
15    $ git config --global core.symlinks true
16 
17 prior to cloning, or else use:
18 
19    $ git clone -c core.symlinks=true https://github.com/dtolnay/cxx
20 
21 for the clone.
22 
23 Symlinks are only required when compiling locally from a clone of
24 the git repository---they are NOT required when building `cxx` as
25 a Cargo-managed (possibly transitive) build dependency downloaded
26 through crates.io.
27 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
28 ";
29 
30 #[cfg(windows)]
31 const DENIED: &str = "
32 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
33 When building `cxx` from a git clone on Windows we need Developer
34 Mode enabled for symlink support.
35 
36 To enable Developer Mode: go under Settings to Update & Security,
37 then 'For developers', and turn on the toggle for Developer Mode.
38 
39 For 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 
43 Symlinks are only required when compiling locally from a clone of
44 the git repository---they are NOT required when building `cxx` as
45 a Cargo-managed (possibly transitive) build dependency downloaded
46 through crates.io.
47 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
48 ";
49 
main()50 fn 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