summaryrefslogtreecommitdiffstats
path: root/chromium/tools/crates/lib/consts.py
blob: 4aaa300aa05db203b98f3ab75a8428d63325ff8a (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
# python3
# Copyright 2021 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import os
import re

CRATES_IO_VIEW = "https://crates.io/crates/{crate}"
CRATES_IO_DOWNLOAD = "https://static.crates.io/crates/{crate}/{crate}-{version}.crate"

# Allowed licenses, in the format they are specified in Cargo.toml files from
# crates.io, and the format to write to README.chromium.
ALLOWED_LICENSES = [
    # ("Cargo.toml string", "License for README.chromium")
    ("Apache-2.0", "Apache 2.0"),
    ("MIT OR Apache-2.0", "Apache 2.0"),
    ("MIT/Apache-2.0", "Apache 2.0"),
    ("Apache-2.0 / MIT", "Apache 2.0"),
    ("Apache-2.0 OR MIT", "Apache 2.0"),
    ("Apache-2.0/MIT", "Apache 2.0"),
    ("MIT", "MIT"),
    ("Unlicense OR MIT", "MIT"),
    ("Unlicense/MIT", "MIT"),
    ("Apache-2.0 OR BSL-1.0", "Apache 2.0"),
    ("BSD-3-Clause", "BSD 3-Clause"),
    ("ISC", "ISC"),
    ("MIT OR Zlib OR Apache-2.0", "Apache 2.0"),
    ("0BSD OR MIT OR Apache-2.0", "Apache 2.0"),
]

# The subdirectory where crates are found, relative to the current working
# directory where the tool is run (i.e. `os.getcwd()`).
THIRD_PARTY = ["third_party", "rust"]

# Where to place the extracted crate inside the version epoch directory. If
# empty, it will be extracted directly to the epoch directory.
CRATE_INNER_DIR = ["crate"]

# Template for generating README.chromium files.
README_CHROMIUM = """Name: {crate_name}
URL: {url}
Description: {description}
Version: {version}
Security Critical: {security}
License: {license}
"""

# Crates that can not be depended one. Dependencies should be removed from
# Cargo.toml files. Each one comes with a reason.
BLOCKED_CRATES = {
    "cc":
    "C/C++ code should be build by a GN rule, not from Rust code directly. See "
    + os.path.join(*(THIRD_PARTY + ["cc", "README.md"])),
}

# A Regex for parsing the output of `cargo tree`. This matches the dependencies
# and reports their name, version, if they are a proc macro, and their enabled
# features.
_CARGO_DEPS = \
    r"(?:├──|└──) (?P<name>.*?) v(?P<version>[0-9]+.[0-9]+.[0-9]+)" \
    r"(?P<isprocmacro> \(proc-macro\))?" \
    r"(?: \((?P<path>[\/].*?)\))?" \
    r"(?: (?P<features>[^( ][^ ]*))?(?: \(\*\))?"
CARGO_DEPS_REGEX = re.compile(_CARGO_DEPS)

FAKE_EMPTY_CARGO_TOML = """[package]
name = "fake"
version = "0.0.0"
"""

# Header at the top of BUILD.gn files. The {year} is substituted with the
# appropriate year.
GN_HEADER = \
"""# Copyright {year} The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import("//build/rust/cargo_crate.gni")

"""
_GN_HEADER_YEAR = r"^# Copyright( \(c\))? (?P<year>[0-9]+) " \
    r"The Chromium Authors\. All rights reserved\."
GN_HEADER_YEAR_REGEX = re.compile(_GN_HEADER_YEAR)

# Comment on the build_native_rust_unit_tests field in BUILD.gn file output.
GN_TESTS_COMMENT = \
"""# Unit tests skipped. Generate with --with-tests to include them."""

# Comment on the visibility field in BUILD.gn file output.
GN_VISIBILITY_COMMENT = \
"""# Only for usage from third-party crates. Add the crate to
# third_party.toml to use it from first-party code."""