~ach/hermes

hermes/do -rwxr-xr-x 5.7 KiB
b2599991Andrew Chambers Add pkg_tree builtin. 3 years ago
                                                                                
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#! /bin/sh

set -eu

# Instead of using $3, the .do script may also write the produced data to stdout.
# Here we disable stdout that and use stderr for logging.
exec 1>&2

fail () {
  echo "$@"
  exit 1
}

vcmd () {
  echo "> $@" 1>&2
  if ! "$@"
  then
    fail "command $@ failed"
  fi
}

build_go_bin () {
  unset GOPATH
  out="$(pwd)/$2/$1"
  pushd "src/cmd/$1" > /dev/null
  vcmd go build $HERMES_GO_EXTRA_BUILD_FLAGS -mod=vendor -ldflags "-linkmode=external -extldflags -static" -o "$out"
  popd > /dev/null
}

test -f "config.inc" || fail "please run ./configure first."
. ./config.inc

case "$1" in
  
  build)
    if test -e ./build
    then
      rm -rf ./build
    fi

    mkdir -p build/builtins
    mkdir -p build/sysroot/bin
    mkdir -p build/sysroot/libexec/hermes
    
    build_go_bin builtins build/builtins

    for cmd in fetch unpack writefile runcmds mkdir copy
    do
       (cd "build/builtins" && ln -s ./builtins "$cmd")
    done
    echo "building builtins.tar.gz..."
    tar -C "build/builtins" -c -f - . | gzip -n -9 > "build/sysroot/libexec/hermes/builtins.tar.gz"

    cat <<EOF > "src/bindata/bindata_generated.go"
package bindata

var BundledBuiltinsSha256 string = "$(sha256sum build/sysroot/libexec/hermes/builtins.tar.gz | cut -d ' ' -f 1-1)"
EOF
    
    build_go_bin hermes build/sysroot/bin
    build_go_bin hermes-pkgstore build/sysroot/libexec/hermes
    ${CC} ${CFLAGS} ${LDFLAGS} -o build/sysroot/libexec/hermes/hermes-linux-namespace-sandbox csrc/hermes-linux-namespace-sandbox.c
  ;;
  
  install)

    test -e ./build || fail "run './do build' first"
    echo "installing to $PREFIX..."
    vcmd mkdir -p "$PREFIX/bin/"
    vcmd mkdir -p "$PREFIX/libexec/hermes"
    vcmd cp build/sysroot/bin/hermes "$PREFIX/bin/"
    vcmd cp build/sysroot/libexec/hermes/hermes-pkgstore "$PREFIX/libexec/hermes/hermes-pkgstore-nosetuid"
    vcmd cp build/sysroot/libexec/hermes/hermes-pkgstore "$PREFIX/libexec/hermes"
    vcmd cp build/sysroot/libexec/hermes/hermes-linux-namespace-sandbox "$PREFIX/libexec/hermes"
    vcmd cp build/sysroot/libexec/hermes/builtins.tar.gz "$PREFIX/libexec/hermes"

    if test "$(whoami)" = root
    then
      vcmd  chmod u+s "$PREFIX/libexec/hermes/hermes-pkgstore"
    else
      echo ""
      echo "NOTE:"
      echo "  Installed hermes without setuid (installer wasn't run as root)"
      echo "  you may not have permissions to run builds with all sandbox modes."
    fi
    echo ""
    echo "NOTE:"
    echo "  Before you can use hermes, you must initialize the package store with:"
    echo "    sudo hermes init"
    echo "  or for an unprivileged install:"
    echo "    hermes init --single-user --store \$HOME/hermes'"
    echo "    echo \"store_path=\\\"\$HOME/hermes\\\"\" > \$HOME/.config/hermes/hermes.cfg"
    echo ""
  ;;

  uninstall)
    vcmd rm -f "$PREFIX/bin/hermes"
    vcmd rm -rf "$PREFIX/libexec/hermes"
  ;;

  installer)
    ./do build
    mkdir ./build/installer
    # XXX TODO single user install...
    cat <<'EOF' > build/installer/install
#! /bin/sh
set -eu
if ! test "$(id -u)" = 0
then
  echo "please run the installer as root."
  exit 1
fi
echo "installing into '/hermes'..."
set -x
mkdir /hermes
cp -r ./sysroot/bin /hermes
cp -r ./sysroot/libexec /hermes
cp /hermes/libexec/hermes/hermes-pkgstore /hermes/libexec/hermes/hermes-pkgstore-nosetuid
chmod 755 -R /hermes
chmod 555 /hermes/libexec/hermes/hermes-*
chmod 555 /hermes/bin/*
chmod u+s /hermes/libexec/hermes/hermes-pkgstore
umask 022
/hermes/bin/hermes init
set +x
echo ""
echo "You need have a 'hermes_build_user' account on your system."
echo ""
echo "Create one with:"
echo "  $ useradd --system --no-create-home --home /homeless hermes_build_user"
echo ""
echo "Please add user accounts to authorized_users to /hermes/store.cfg"
echo "to allow them to build and install package using hermes."
echo ""
echo "Please add hermes to your path, e.g. add \"export PATH=\$PATH:/hermes/bin\""
echo "to your shell profile."

EOF
    chmod +x build/installer/install
    cp -r build/sysroot build/installer
    makeself build/installer build/hermes-x86_64-linux-installer.run "Hermes - build, package, deploy installer." ./install
  ;;

  vendor-hscript)
    # We rename starlark to hscript so that user facing errors that mention
    # types show hscript instead of starlark, its less than ideal and we
    # should somehow revisit this in the future.
    #
    rm -rf ./src/hscript

    ref="111e028f1bdd7e69c52fe75eea003c064df83068"
    
    tmpdir="$(mktemp -d)"
    curl -o "$tmpdir/hscript.tar.gz" "https://git.sr.ht/~ach/hscript/archive/${ref}.tar.gz"
    (cd "$tmpdir" && tar xzf "$tmpdir/hscript.tar.gz")
    mv "$tmpdir/hscript-${HSCRIPT_REF}" src/hscript
    rm -rf "$tmpdir"

    mv src/hscript/starlark src/hscript/hscript
    mv src/hscript/starlarkstruct src/hscript/hscriptstruct
    mv src/hscript/starlarktest src/hscript/hscripttest
    mv src/hscript/cmd/starlark src/hscript/cmd/hscript
    mv src/hscript/cmd/hscript/starlark.go src/hscript/cmd/hscript/hscript.go 

    sed -i s@go.starlark.net@github.com/andrewchambers/hermes/hscript@g $(find ./src/hscript -name "*.go")
    sed -i s@starlark@hscript@g $(find ./src/hscript -name "*.go")
    sed -i s@Starlark@Hscript@g $(find ./src/hscript -name "*.go")

    echo "AUTOMATICALLY CLONED FROM THE HSCRIPT PROJECT" > ./src/hscript/DO_NOT_EDIT

    git add src/hscript
  ;;

  fmt-src)
    vcmd go fmt ./src/...
    vcmd clang-format -i ./csrc/*.c
    # XXX we need a patched version of black.
    # or another formatter, maybe google has a go one?
    # black $(find  ./pkgs/ -name "*.hermes")
    # XXX format C code.
  ;;

  fmt-doc)
      for md in README.md $(ls book/src/*.md)
      do
          vcmd pandoc -f gfm -t gfm $md > $md.fmt
          mv $md.fmt $md
      done
  ;;

  *)
    fail "don't know how to do $1"
  ;;
esac