Skip to content

Commit 16534a0

Browse files
committed
librustc_trans and librustdoc: add license::print().
The license document is compressed by DEFLATE and then stored as pub const license::text::COPYRIGHT of type &'static [u8]. A helper script src/etc/license2rs.sh takes care of updating it. It is decompressed by flate::inflate_bytes() on runtime. Fix rust-lang#17690 Signed-off-by: NODA, Kai <[email protected]>
1 parent 3b01ad0 commit 16534a0

File tree

11 files changed

+2627
-4
lines changed

11 files changed

+2627
-4
lines changed

AUTHORS.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,3 +672,5 @@ xales <[email protected]>
672672
673673
674674
675+
676+
Run src/etc/copyright2rs.sh to embed this file to rustc.

COPYRIGHT

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,3 +478,5 @@ their own copyright notices and license terms:
478478
has chosen for the collective work, enumerated at the top
479479
of this file. The only difference is the retention of
480480
copyright itself, held by the contributor.
481+
482+
Run src/etc/copyright2rs.sh to embed this file to rustc.

mk/crates.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ DEPS_rustc := syntax flate arena serialize getopts rbml \
7373
DEPS_rustc_llvm := native:rustllvm libc std
7474
DEPS_rustc_back := std syntax rustc_llvm flate log libc
7575
DEPS_rustdoc := rustc rustc_trans native:hoedown serialize getopts \
76-
test time
76+
test time flate
7777
DEPS_flate := std native:miniz
7878
DEPS_arena := std
7979
DEPS_graphviz := std

src/etc/copyright2rs.sh

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
#! /bin/sh
2+
3+
# Copyright 2014 The Rust Project Developers. See the COPYRIGHT
4+
# file at the top-level directory of this distribution and at
5+
# http://rust-lang.org/COPYRIGHT.
6+
#
7+
# Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
8+
# http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
9+
# <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
10+
# option.
11+
12+
set -e
13+
14+
clean_tmp() {
15+
if [ -n "$g_outf" ]; then
16+
echo "ERROR: failed to create \"$g_outf\""
17+
fi
18+
if [ -d "$g_tmpdir" -a -O "$g_tmpdir" ]; then
19+
echo "Cleanup: rm -rf \"$g_tmpdir\""
20+
rm -rf "$g_tmpdir" && exit 0
21+
fi
22+
echo "WARNING: could not remove \"$g_tmpdir\""
23+
}
24+
25+
trap clean_tmp EXIT;
26+
27+
check_file() {
28+
infile=$1
29+
if [ ! -r "$infile" ]; then
30+
echo "ERROR: could not find file \"$infile\" which should be in the top directory."
31+
echo "Did you run this script in the top directory?"
32+
exit 10
33+
fi
34+
}
35+
36+
git_final_commit() {
37+
infile=$1
38+
echo "// git log -n1 '$infile'"
39+
# don't use "--" so that git returns an error
40+
git log -n1 "$infile" > /dev/null || { echo "ERROR: failed to run git" >&2 ; exit 30; }
41+
git log -n1 "$infile" | while read line; do
42+
if [ -n "$line" ]; then
43+
echo "// $line"
44+
else
45+
echo //
46+
fi
47+
done
48+
echo
49+
}
50+
51+
generate() {
52+
g_outf=$1
53+
54+
tmpoutf=$g_tmpdir/$g_outf
55+
tmpcomb=$g_tmpdir/combined.txt
56+
tmpdeflate=$g_tmpdir/deflate.dat
57+
tmpod=$g_tmpdir/deflate.od.txt
58+
tmparr=$g_tmpdir/deflate.od.sed.txt
59+
60+
mkdir -p ${tmpoutf%/*}
61+
62+
check_file COPYRIGHT
63+
check_file LICENSE-MIT
64+
check_file LICENSE-APACHE
65+
check_file AUTHORS.txt
66+
67+
# Open a temporary output file.
68+
# It will be (atomically) renamed to the final output file.
69+
exec 3>"$tmpoutf"
70+
cat >&3 <<EOS
71+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
72+
// file at the top-level directory of this distribution and at
73+
// http://rust-lang.org/COPYRIGHT.
74+
//
75+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
76+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
77+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
78+
// option.
79+
80+
EOS
81+
echo "// This file (text.rs) was generated by src/etc/copyright2rs.sh at" >&3
82+
echo "// $(date -R)" >&3
83+
echo >&3
84+
85+
git_final_commit COPYRIGHT >&3
86+
git_final_commit AUTHORS.txt >&3
87+
88+
echo "pub const COPYRIGHT: &'static [u8] = &[" >&3
89+
90+
# Process each step w/o pipe so that "-e" can catch an error in intermediate step.s
91+
cat > "$tmpcomb" <<EOS
92+
Four license documents COPYRIGHT, LICENSE-MIT, LICENSE-APACHE, and
93+
AUTHORS.txt combined as one:
94+
95+
EOS
96+
for f in COPYRIGHT LICENSE-MIT LICENSE-APACHE AUTHORS.txt; do
97+
echo "---- $f ----" >> "$tmpcomb"
98+
cat "$f" >> "$tmpcomb"
99+
echo >> "$tmpcomb"
100+
done
101+
python -c 'import sys,zlib; sys.stdout.write(zlib.compress(sys.stdin.read(), 9))' \
102+
< "$tmpcomb" > "$tmpdeflate"
103+
od -t x1 -A n < "$tmpdeflate" > "$tmpod"
104+
sed -re 's/ (\w+)/0x\1,/g' < "$tmpod" >&3
105+
echo "];" >&3
106+
exec 3<&-
107+
108+
mv "$tmpoutf" "$g_outf"
109+
echo "Successfully generated \"$g_outf\""
110+
g_outf=''
111+
}
112+
113+
main() {
114+
g_tmpdir=$(mktemp -d) || { echo "ERROR: mktemp(1) failed to create a temp directory"; exit 5; }
115+
echo "Created a temp dir \"$g_tmpdir\""
116+
117+
generate src/librustc_trans/driver/license/text.rs
118+
generate src/librustdoc/license/text.rs
119+
}
120+
121+
main

src/librustc/session/config.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,7 @@ pub fn build_target_config(opts: &Options, sp: &SpanHandler) -> Config {
572572
pub fn optgroups() -> Vec<getopts::OptGroup> {
573573
vec!(
574574
optflag("h", "help", "Display this message"),
575+
optflag("", "license", "Display license documents"),
575576
optmulti("", "cfg", "Configure the compilation environment", "SPEC"),
576577
optmulti("L", "", "Add a directory to the library search path", "PATH"),
577578
optmulti("l", "", "Link the generated crate(s) to the specified native
@@ -631,7 +632,7 @@ pub fn optgroups() -> Vec<getopts::OptGroup> {
631632
always = always colorize output;
632633
never = never colorize output", "auto|always|never"),
633634
optmulti("", "extern", "Specify where an external rust library is located",
634-
"NAME=PATH"),
635+
"NAME=PATH")
635636
)
636637
}
637638

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use std::io::{Writer, IoResult, IoError};
2+
use std::io::IoErrorKind::InvalidInput;
3+
use flate;
4+
5+
mod text;
6+
7+
pub fn print(out: &mut Writer) -> IoResult<()> {
8+
match flate::inflate_bytes_zlib(text::COPYRIGHT) {
9+
Some(dat) => out.write(dat.as_slice()),
10+
None => Err(IoError { kind: InvalidInput, desc: "decompression error of the COPYRIGHT file",
11+
detail: None })
12+
}
13+
}

0 commit comments

Comments
 (0)