summaryrefslogtreecommitdiff
path: root/share/sfpm/build.sh
blob: 9f76d8e749cb5baba16612bc6ef478a321adfedf (plain) (blame)
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
required_keys=(
    name
    version
    release
)

function sfpm_CPUS() {
    local count=$(getconf _NPROCESSORS_ONLN)

    ((count--))
    if (( ${count} <= 0 )); then
        count=1
    fi

    echo ${count}
}

function sfpm_gen_srcdir() {
    local path="${sfpm_srcdir}/${name}-${version}-${release}"
    if [[ ! -d ${path} ]]; then
        mkdir -p ${path}
    fi
    echo ${path}
}


function sfpm_rm_srcdir() {
    local path="${sfpm_srcdir}/${name}-${version}-${release}"
    if [[ -d ${path} ]]; then
        rm -rf "${path}"
    fi
}


function sfpm_check_required_keys() {
    die=0
    for key in "${required_keys[@]}"
    do
        if [[ ! ${!key} ]]; then
            msg_error "Package '${key}' undefined!"
            die=1
        fi
    done

    if (( ${die} )); then
        exit 1
    fi
}


function sfpm_cmp_sha256() {
    # is a FILE
    local sum_file="${1}"
    if [[ ! -f ${sum_file} ]]; then
        msg_error "${sum_file} is not a file"
        exit 1
    fi

    # sha256 hashes
    local sum_left=$(sha256sum ${sum_file} | awk '{ print $1 }')
    local sum_right="${2}"

    # compare hashes
    if [[ ${sum_left} != ${sum_right} ]]; then
        return 1
    fi

    return 0
}

function sfpm_verify_gpg() {
    msg_warn "sfpm_verify_gpg(): Not implemented"
}

function prepare() {
    msg_warn "prepare() function undefined"
}


function build() {
    msg_warn "build() function undefined"
}

function check() {
    msg_warn "check() function undefined"
}

function package() {
    msg_error "package() function undefined"
    exit 1
}


function sources_fetch() {
    local path=$(sfpm_gen_srcdir)
    for src in "${sources[@]}"
    do
        fetch --checksum --skip-exists --redirect --output ${path} ${src}
    done
}


function sources_extract() {
    local destdir="${1}"
    if [[ ! ${destdir} ]]; then
        msg_error "sources_extract() destination undefined: ${destdir}"
        exit 1
    fi

    if [[ ! -d ${destdir} ]]; then
        mkdir -p "${destdir}"
    fi

    local srcdir="$(sfpm_gen_srcdir)"
    if [[ ! -d ${srcdir} ]]; then
        msg_error "${srcdir} does not exist!"
        exit 1
    fi

    archives_tar=$(find ${srcdir} -maxdepth 1 -type f \( -name "*.tar*" -o -name "*.t*" \) -and -not -name "*.sha256")
    archives_zip=$(find ${srcdir} -maxdepth 1 -type f \( -name "*.zip" \) -and -not -name "*.sha256")

    msg2 "Extracting"
    pushd "${srcdir}"
        if [[ ${archives_tar} ]]; then
            for archive in ${archives_tar}
            do
                msg3 "${archive}"
                tar xf "${archive}" -C "${destdir}"
            done
        fi

        if [[ ${archives_zip} ]]; then
            for archive in ${archives_zip}
            do
                msg3 "${archive}"
                unzip "${archive}" -d "${destdir}"
            done
        fi
    popd
}


function sfpm_sources_cmp_sha256() {
    source_count=${#sources[@]}
    sha256_count=${#sha256sums[@]}

    if (( ! ${sha256_count} )); then
        return 0
    fi

    msg2 "Comparing sha256 checksums"
    if [[ ${source_count} != ${sha256_count} ]]; then
        msg_error "Total sources (${source_count}) does not match total of hashes (${sha256_count})" \
                  "HINT: Place 'null' for each source without a hash."
        exit 1
    fi

    for url in "${sources[@]}"
    do
        for sha in "${sha256sums[@]}"
        do
            if [[ ${sha} == null ]] || [[ ${sha} == NULL ]]; then
                continue
            fi

            archive="${sfpm_srcdir}/${name}-${version}-${release}/$(basename ${url})"
            sfpm_cmp_sha256 ${archive} ${sha}
            if (( ${?} )); then
                msg_error "${sha} does not match $(basename ${archive})"
                exit 1
            fi
        done
    done
}

function sfpm_gen_buildroot() {
    export buildroot=$(mktemp -d ${TMPDIR}/sfpm.buildroot.XXXXXX)
    if [[ ! -d ${buildroot} ]]; then
        msg_error "Failed to create buildroot: ${buildroot}"
        exit 1
    fi

    echo ${buildroot}
}


function sfpm_rm_buildroot() {
    if [[ ${buildroot} == *${sfpm_tmpdir}* ]]; then
        msg "Removing ${buildroot}"
        rm -rf "${buildroot}"
    fi
}

function sfpm_build_env_do_depends() {
    local env_name="${1}"
    if [[ ${depends} ]]; then
        for dep in "${depends[@]}"
        do
            local pkg=$(sfpm_index_search ${dep})
            sfpm_install "${env_name}" "${pkg}"
        done
    fi
}

function sfpm_gen_package_manifest() {
    pushd "${pkgdir}"
        find . -type f -not -name ".SFPM-*" | xargs -I'{}' sha256sum "{}" > .SFPM-MANIFEST
    popd
}


# Don't use this. Getting rid of it.
function sfpm_gen_package_sizes() {
    pushd "${pkgdir}"
        find . -type f -not -name ".SFPM-*" | xargs -I'{}' -n1 du -b "{}" > .SFPM-SIZES
    popd
}

function sfpm_gen_package_depends_manifest() {
    pushd "${pkgdir}"
        >.SFPM-DEPENDS
        for dep in "${depends[@]}"
        do
            echo "${dep}" >> .SFPM-DEPENDS
        done
    popd
}

function sfpm_gen_package_rpath() {
    pushd "${pkgdir}"
        local rpath_orig
        local rpath_new
        local rpath_cache="$(mktemp ${TMPDIR}/sfpm.rpath_cache.XXXXXX)"

        # Assimilate all file paths that contain an RPATH
        for path in $(find . -type f -not -name '.SFPM-*')
        do
            readelf -d "${path}" 2>/dev/null | grep RPATH &>/dev/null
            if (( $? )); then
                continue
            fi
            echo "${path}" >> "${rpath_cache}"
        done

        msg2 "Adjusting depth of RPATHs"
        while read line
        do
            rpath_orig="$(readelf -d ${line} | grep RPATH | awk -F'[][]' '{ print $2 }')"
            rpath_new='$ORIGIN/'"$(sfpm_rpath_nearest ${line})"
            msg3 "${line}: ${rpath_orig} -> ${rpath_new}"
            patchelf --set-rpath "${rpath_new}" "${line}"
        done < "${rpath_cache}"
        [[ -f "${rpath_cache}" ]] && rm -f "${rpath_cache}"
    popd
}

function sfpm_gen_package_prefixes() {
    msg "Generating build prefix manifest"
    pushd "${pkgdir}"
        # Create record files
        >.SFPM-PREFIX-TEXT
        >.SFPM-PREFIX-BIN

        # Assimilate file path for anything containing our prefix
        local count_text=0
        local count_bin=0
        local count_total=0

        for path in $(find . -type f -not -name ".SFPM-*")
        do
            # Check for prefix
            grep -l "${sfpm_build_prefix}" "${path}" &>/dev/null

            # Prefix present? (0: yes, 1: no)
            if (( $? )); then
                continue
            fi

            # Get file type
            local mimetype="$(file -i ${path} | awk -F': ' '{ print $2 }')"
            local outfile

            # Record prefix data
            if [[ ${mimetype} = *text/* ]]; then
                outfile=.SFPM-PREFIX-TEXT
                (( count_text++ ))
            else
                outfile=.SFPM-PREFIX-BIN
                (( count_bin++ ))
            fi

            echo "${path}" >> "${outfile}"

        done

        count_total=$(( count_text + count_bin ))
        if (( ${count_total} )); then
            msg2 "Text: ${count_text}"
            msg2 "Binary: ${count_bin}"
            msg2 "Total: ${count_total}"
        else
            msg2 "No prefixes detected"
        fi
    popd
}

function sfpm_gen_packages() {
    local funcs=$(compgen -A function | grep ^package)
    local name_old="${name}"
    local pkgdir_old="${pkgdir}"
    for fn in ${funcs}
    do
        # Don't modify main package() behavior
        if [[ ${fn} != package ]]; then
            name=${fn#package_}
            pkgdir_child=$(mktemp -d ${TMPDIR}/sfpm.pkgdir_child.XXXXXX)
            pkgdir="${pkgdir_child}"
        fi

        ${fn}
        sfpm_gen_package

        if [[ -d ${pkgdir_child} ]]; then
            rm -rf "${pkgdir_child}"
            name="${name_old}"
            pkgdir="${pkgdir_old}"
        fi
    done
}

function sfpm_gen_package() {
    if [[ ! ${sfpm_build_scriptdir} ]]; then
        msg_error "Refusing to generate package outside of sfpm_makepkg"
        exit 1
    fi

    archive=${name}-${version}-${release}.tar.bz2
    pushd "${pkgdir}"
        msg "Generating ${archive}"
        sfpm_gen_package_manifest
        sfpm_gen_package_depends_manifest
        sfpm_gen_package_prefixes
        sfpm_gen_package_rpath
        tar cfj "${sfpm_build_scriptdir}/${archive}" .SFPM-* *
    popd
}