-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathbuild-script.ts
More file actions
107 lines (88 loc) · 3.11 KB
/
build-script.ts
File metadata and controls
107 lines (88 loc) · 3.11 KB
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
import { hooks, PackageRequirement, utils, Path } from "pkgx"
import { find_in_PATH } from "brewkit/utils.ts"
import { Config } from "brewkit/config.ts"
import undent from "outdent"
const { usePantry, useConfig } = hooks
const { host } = utils
export default async function(config: Config, PATH?: Path): Promise<string> {
const depset = new Set(config.deps.gas.map(x => x.pkg.project))
const depstr = (deps: PackageRequirement[]) => deps.map(x => `"+${utils.pkg.str(x)}"`).join(' ')
let env_plus = `${depstr(config.deps.dry.runtime)} ${depstr(config.deps.dry.build)}`.trim()
// if no compiler is an explicit dep, add llvm as default (was previously done per-invocation in the shim)
if (host().platform != 'darwin' && !depset.has('llvm.org') && !depset.has('gnu.org/gcc')) {
env_plus = `${env_plus} "+llvm.org"`.trim()
}
const user_script = await usePantry().getScript(config.pkg, 'build', config.deps.gas, config)
const pkgx = find_in_PATH('pkgx')
const bash = find_in_PATH('bash')
const gum = find_in_PATH('gum')
const brewkitd = new Path(new URL(import.meta.url).pathname).parent().parent().parent()
const brewkit_PATHs = [
brewkitd.join("libexec"),
PATH
].compact(x => x?.string).join(':')
const FLAGS = flags()
if (host().platform == 'darwin') {
FLAGS.push("export MACOSX_DEPLOYMENT_TARGET=11.0")
// gcc needs to use Apple’s ar/ranlib combo on darwin almost always or link failure occurs
// see https://github.com/pkgxdev/brewkit/pull/285
if (depset.has('gnu.org/binutils')) {
FLAGS.push("export AR=/usr/bin/ar")
FLAGS.push("export RANLIB=/usr/bin/ranlib")
}
}
const tmp = (() => {
switch (host().platform) {
case 'darwin':
case 'linux':
return `export TMPDIR="$HOME/tmp"; mkdir -p "$TMPDIR"`
case 'windows':
return `export TMP="$HOME/tmp"; export TEMP="$HOME/tmp"; mkdir -p "$TMP"`
}
})
return undent`
#!/${bash}
set -eo pipefail
${gum} format "## env"
export PKGX_HOME="$HOME"
set -a
${env_plus ? `eval "$(CLICOLOR_FORCE=1 ${pkgx} ${env_plus})"` : ''}
set +a
export PATH="${brewkit_PATHs}:$PATH"
export PKGX="${pkgx}"
export HOME=${config.path.home.string}
export SRCROOT=${config.path.build.string}
${tmp()}
if [ -n "$CI" ]; then
export FORCE_UNSAFE_CONFIGURE=1
fi
mkdir -p $HOME
${FLAGS.join('\n ')}
env -u GH_TOKEN -u GITHUB_TOKEN
${gum} format "## pantry script start"
set -x
cd ${config.path.build}
${user_script}
`
}
function flags(): string[] {
const {platform, arch} = host()
const is_linux_x86_64 = platform == 'linux' && arch == 'x86-64'
const LDFLAGS = (() => {
switch (platform) {
case 'darwin':
return `-Wl,-rpath,${useConfig().prefix.string}`
case 'linux':
if (arch != 'x86-64') return
return '-pie'
}
})()
const rv: [string, string][] = []
if (LDFLAGS) {
rv.push(['LDFLAGS', LDFLAGS])
}
if (is_linux_x86_64) {
rv.push(['CFLAGS', '-fPIC'], ['CXXFLAGS', '-fPIC'])
}
return rv.map(([key, value]) => `export ${key}="${value} $${key}"`)
}