1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
use std::env;
use std::path::Path;
use std::path::PathBuf;
use target_info;

/// The operating system
#[derive(Debug, Clone, Eq, PartialEq)]
enum OS {
    Linux,
    Windows,
    Macos,
}

/// The part of the current platform that is relevant when building wheels and is supported
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Target {
    os: OS,
    is_64_bit: bool,
}

impl Target {
    /// Returns the target pyo3-pack was compiled for
    pub fn current() -> Self {
        let os = match target_info::Target::os() {
            "linux" => OS::Linux,
            "windows" => OS::Windows,
            "macos" => OS::Macos,
            unsupported => panic!("The platform {} is not supported", unsupported),
        };

        let is_64_bit = match target_info::Target::pointer_width() {
            "64" => true,
            "32" => false,
            unsupported => panic!("The pointer width {} is not supported ಠ_ಠ", unsupported),
        };

        Target { os, is_64_bit }
    }

    /// Returns whether the platform is 64 bit or 32 bit
    pub fn pointer_width(&self) -> usize {
        if self.is_64_bit {
            64
        } else {
            32
        }
    }

    /// Returns true if the current platform is linux or mac os
    pub fn is_unix(&self) -> bool {
        self.os != OS::Windows
    }

    /// Returns true if the current platform is linux
    pub fn is_linux(&self) -> bool {
        self.os == OS::Linux
    }

    /// Returns true if the current platform is mac os
    pub fn is_macos(&self) -> bool {
        self.os == OS::Macos
    }

    /// Returns true if the current platform is windows
    pub fn is_windows(&self) -> bool {
        self.os == OS::Windows
    }

    /// Returns the platform part of the tag for the wheel name for cffi wheels
    pub fn get_platform_tag(&self) -> &'static str {
        match (&self.os, self.is_64_bit) {
            (&OS::Linux, true) => "manylinux1_x86_64",
            (&OS::Linux, false) => "manylinux1_i686",
            (&OS::Windows, true) => "win_amd64",
            (&OS::Windows, false) => "win32",
            (&OS::Macos, true) => "macosx_10_11_x86_64",
            (&OS::Macos, false) => "macosx_10_11_i686", // TODO: Is that one correct?
        }
    }

    /// Returns the tags for the WHEEL file for cffi wheels
    pub fn get_cffi_tags(&self) -> &[&'static str] {
        if self.is_64_bit {
            &["py2-none-linux_x86_64", "py3-none-linux_x86_64"]
        } else {
            &["py2-none-linux_i686", "py3-none-linux_i686"]
        }
    }

    /// Returns the platform for the tag in the shared libaries file name
    pub fn get_shared_platform_tag(&self) -> &'static str {
        match self.os {
            OS::Linux => {
                if target_info::Target::env() != "gnu" {
                    panic!("Expected a gnu target, musl is not supported");
                }
                if self.is_64_bit {
                    "x86_64-linux-gnu"
                } else {
                    "x86-linux-gnu"
                }
            }
            OS::Macos => "darwin",
            OS::Windows => if self.is_64_bit {
                "win_amd64"
            } else {
                "win32"
            },
        }
    }

    /// Returns the path to the python executable inside a venv
    pub fn get_venv_python(&self, venv_base: impl AsRef<Path>) -> PathBuf {
        let message = "expected python to be the venv";
        if self.is_windows() {
            venv_base
                .as_ref()
                .join("Scripts")
                .join("python.exe")
                .canonicalize()
                .expect(message)
        } else {
            venv_base
                .as_ref()
                .join("bin")
                .join("python")
                .canonicalize()
                .expect(message)
        }
    }

    /// Returns the path to the python executable
    ///
    /// For windows it's always python.exe for unix it's 1. venv's python 2. python3
    pub fn get_python(&self) -> PathBuf {
        if self.is_windows() {
            PathBuf::from("python.exe")
        } else if env::var_os("VIRTUAL_ENV").is_some() {
            PathBuf::from("python")
        } else {
            PathBuf::from("python3")
        }
    }
}