summaryrefslogtreecommitdiff
path: root/tools/cargo_package.py
blob: 29f5e7fb5009200167a2c6dcab736cb494a9d89e (plain)
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/env python
# Because of limitations in Cargo, Deno must dynamically build temporary source
# directories in order to publish to crates.io.
# The "deno" crate corresponds to the //core/ directory and depends on a
# platform dependent crate binary crate containing pre-compiled libdeno
# https://crates.io/crates/deno
# https://crates.io/crates/deno-x86_64-pc-windows-msvc
# https://crates.io/crates/deno-x86_64-apple-darwin
# https://crates.io/crates/deno-x86_64-unknown-linux-gnu

import os
import sys
import re
import errno
from shutil import copytree, ignore_patterns, copyfile
from tempfile import mkdtemp
from string import Template
from util import root_path, run

if sys.platform == "linux2":
    llvm_target = "x86_64-unknown-linux-gnu"
    static_lib_suffix = ".a"
elif sys.platform == "darwin":
    llvm_target = "x86_64-apple-darwin"
    static_lib_suffix = ".a"
elif sys.platform == "win32":
    llvm_target = "x86_64-pc-windows-msvc"
    static_lib_suffix = ".lib"
else:
    assert (False)

lib_name = os.path.join(root_path, "target", "release", "obj",
                        "libdeno" + static_lib_suffix)


def get_version(toml_path):
    for line in open(toml_path):
        match = re.search('version = "(.*)"', line)
        if match:
            return match.group(1)


core_path = os.path.join(root_path, "core")
cargo_toml_path = os.path.join(core_path, "Cargo.toml")
version = get_version(cargo_toml_path)


def main():
    os.chdir(root_path)

    run([
        "cargo", "build", "-vv", "--manifest-path", cargo_toml_path, "--lib",
        "--release", "--locked"
    ])
    assert (os.path.exists(lib_name))

    root_temp = mkdtemp()
    print "cargo package temp dir", root_temp

    build_core(root_temp)
    build_platform_crate(root_temp)

    print "Now go into %s and run 'cargo publish' manually." % root_temp


def build_core(root_temp):
    core_temp = os.path.join(root_temp, "core")

    # Copy entire core tree into temp directory, excluding build.rs and libdeno
    # and unnecessary files.
    copytree(
        core_path,
        core_temp,
        ignore=ignore_patterns("build.rs", "libdeno", ".*", "*.gn", "*.orig"))

    cargo_toml_temp = os.path.join(core_temp, "Cargo.toml")

    t = cargo_toml_deps.substitute(VERSION=version)
    # Append deps to //core/Cargo.toml
    # This append is the entire reason we are copying the tree.
    with open(cargo_toml_temp, "a") as f:
        f.write(t)


def build_platform_crate(root_temp):
    platform_temp = os.path.join(root_temp, "platform")

    copy_static_lib(platform_temp)

    inputs = {"TARGET": llvm_target, "VERSION": version}

    generate(platform_temp, "build.rs", platform_build_rs.substitute(inputs))
    generate(platform_temp, "Cargo.toml",
             platform_cargo_toml.substitute(inputs))
    generate(platform_temp, "src/lib.rs", "")


def copy_static_lib(platform_temp):
    platform_lib = os.path.join(platform_temp, "lib/")
    mkdir_p(platform_lib)
    platform_lib_name = os.path.join(platform_lib, os.path.basename(lib_name))
    assert (os.path.exists(lib_name))
    copyfile(lib_name, platform_lib_name)


platform_build_rs = Template("""
fn main() {
  use std::env::var;
  use std::path::Path;
  if var("TARGET")
    .map(|target| target == "$TARGET")
    .unwrap_or(false)
  {
    let dir = var("CARGO_MANIFEST_DIR").unwrap();
    println!(
      "cargo:rustc-link-search=native={}",
      Path::new(&dir).join("lib").display()
    );
  }
}
""")

platform_cargo_toml = Template("""
[package]
name = "deno-$TARGET"
description = "Binary dependencies for the 'deno' crate"
authors = ["The deno authors <bertbelder@nodejs.org>"]
version = "$VERSION"
build = "build.rs"
include = ["src/*", "lib/*", "Cargo.toml", "build.rs"]
license = "MIT"
repository = "https://github.com/denoland/deno"
""")

cargo_toml_deps = Template("""
[target.x86_64-apple-darwin.dependencies]
deno-x86_64-apple-darwin = "=$VERSION"
  
[target.x86_64-pc-windows-msvc.dependencies]
deno-x86_64-pc-windows-msvc = "=$VERSION"

[target.x86_64-unknown-linux-gnu.dependencies]
deno-x86_64-unknown-linux-gnu = "=$VERSION"
""")


def mkdir_p(path):
    try:
        os.makedirs(path)
    except OSError as exc:
        if exc.errno == errno.EEXIST and os.path.isdir(path):
            pass
        else:
            raise


def generate(out_dir, filename, content):
    path = os.path.join(out_dir, filename)
    d = os.path.dirname(path)
    mkdir_p(d)
    with open(path, "w") as f:
        f.write(content)


if __name__ == '__main__':
    sys.exit(main())