|
| 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 |
0 commit comments