code:
# Build recipe for libxcb.
#
# Copyright (c) 2017 Mateus P. Rodrigues <mprodrigues@dragora.org>.
# Copyright (c) 2017-2018, 2021 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Exit immediately on any error
set -e

program=libxcb
version=1.14
release=1

# Define a category for the output of the package name
pkgcategory=xorg_xcb

tarname=${program}-${version}.tar.gz

# Remote source(s)
fetch=https://www.x.org/releases/individual/xcb/$tarname

description="
X-protocol C binding.

Provides an interface to the X Window System protocol,
which replaces the current Xlib interface.
"

homepage=https://www.x.org
license="MIT X Consortium"

# Source documentation
docs="COPYING ChangeLog NEWS README.md"
docsdir="${docdir}/${program}-${version}"

build()
{
    unpack "${tardir}/$tarname"

    cd "$srcdir"

    # Set sane permissions
    chmod -R u+w,go-w,a+rX-s .

    ./configure CPPFLAGS="$QICPPFLAGS" CFLAGS="$QICFLAGS -Wno-error=format-extra-args" \
    LDFLAGS="$QILDFLAGS" \
     $configure_args \
     --libdir=/usr/lib${libSuffix} \
     --mandir=$mandir \
     --docdir=$docsdir \
     --enable-shared=yes \
     --enable-static=no \
     --enable-xevie=yes \
     --build="$(gcc -dumpmachine)"

    make -j${jobs} V=1
    make -j${jobs} DESTDIR="$destdir" install-strip

    # Compress and link man pages (if needed)
    if test -d "${destdir}/$mandir"
    then
        (
            cd "${destdir}/$mandir"
            find . -type f -exec lzip -9 {} +
            find . -type l | while read -r file
            do
                ln -sf "$(readlink -- "$file").lz" "${file}.lz"
                rm -- "$file"
            done
        )
    fi

    # Copy documentation
    mkdir -p "${destdir}/$docsdir"
    cp -p $docs "${destdir}/$docsdir"
}

path: recipes/xorg/xcb/libxcb/recipe


code:
# Build recipe for xcb-proto.
#
# Copyright (c) 2017 Mateus P. Rodrigues <mprodrigues@dragora.org>.
# Copyright (c) 2017-2018, 2021 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Exit immediately on any error
set -e

program=xcb-proto
version=1.14.1
arch=noarch
release=1

# Define a category for the output of the package name
pkgcategory=xorg_xcb

tarname=${program}-${version}.tar.xz

# Remote source(s)
fetch=https://www.x.org/releases/individual/xcb/$tarname

description="
XCB protocol descriptions.

Provides the XML-XCB protocol descriptions that libxcb uses
to generate the majority fo its code and API.
"

homepage=https://www.x.org
license="MIT X Consortium"

# Source documentation
docs="COPYING README.md TODO"
docsdir="${docdir}/${program}-${version}"

build()
{
    unpack "${tardir}/$tarname"

    cd "$srcdir"

    # Set sane permissions
    chmod -R u+w,go-w,a+rX-s .

    ./configure $configure_args \
     --libdir=/usr/lib${libSuffix} \
     --infodir=$infodir \
     --mandir=$mandir \
     --docdir=$docsdir \
     || true

    make -j${jobs} V=1
    make -j${jobs} DESTDIR="$destdir" install

    # Compress info documents deleting index file for the package
    if test -d "${destdir}/$infodir"
    then
        rm -f "${destdir}/${infodir}/dir"
        lzip -9 "${destdir}/${infodir}"/*
    fi

    # Compress and link man pages (if needed)
    if test -d "${destdir}/$mandir"
    then
        (
            cd "${destdir}/$mandir"
            find . -type f -exec lzip -9 {} +
            find . -type l | while read -r file
            do
                ln -sf "$(readlink -- "$file").lz" "${file}.lz"
                rm -- "$file"
            done
        )
    fi

    # Copy documentation
    mkdir -p "${destdir}/$docsdir"
    cp -p $docs "${destdir}/$docsdir"
}

repo_name: dustdfg/dragora

path: recipes/xorg/xcb/xcb-proto/recipe

license: mit


code:
# Build recipe for xcb-util-image.
#
# Copyright (c) 2017 Mateus P. Rodrigues <mprodrigues@dragora.org>.
# Copyright (c) 2017 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Exit immediately on any error
set -e

program=xcb-util-image
version=0.4.0
release=1

# Define a category for the output of the package name
pkgcategory=xorg_xcb

tarname=${program}-${version}.tar.bz2

# Remote source(s)
fetch=https://www.x.org/releases/individual/xcb/$tarname

description="
XCB util-image modules.

Provides a number of libraries which sit on top of libxcb.
"

homepage=https://www.x.org
license="MIT X Consortium"

# Source documentation
docs="COPYING ChangeLog NEWS README"
docsdir="${docdir}/${program}-${version}"

build()
{
    unpack "${tardir}/$tarname"

    cd "$srcdir"

    # Set sane permissions
    chmod -R u+w,go-w,a+rX-s .

    ./configure CPPFLAGS="$QICPPFLAGS" CFLAGS="$QICFLAGS" LDFLAGS="$QILDFLAGS" \
     $configure_args \
     --libdir=/usr/lib${libSuffix} \
     --infodir=$infodir \
     --mandir=$mandir \
     --docdir=$docsdir \
     || true

    make -j${jobs} V=1
    make -j${jobs} DESTDIR="$destdir" install

    # Compress info documents deleting index file for the package
    if test -d "${destdir}/$infodir"
    then
        rm -f "${destdir}/${infodir}/dir"
        lzip -9 "${destdir}/${infodir}"/*
    fi

    # Compress and link man pages (if needed)
    if test -d "${destdir}/$mandir"
    then
        (
            cd "${destdir}/$mandir"
            find . -type f -exec lzip -9 {} +
            find . -type l | while read -r file
            do
                ln -sf "$(readlink -- "$file").lz" "${file}.lz"
                rm -- "$file"
            done
        )
    fi

    # Copy documentation
    mkdir -p "${destdir}/$docsdir"

    for file in $docs
    do
        if test -e $file
        then
            cp -p $file "${destdir}/$docsdir"
        fi
    done
}

path: recipes/xorg/xcb/xcb-util-image/recipe


code:
# Build recipe for xcb-util-keysyms.
#
# Copyright (c) 2017 Mateus P. Rodrigues <mprodrigues@dragora.org>.
# Copyright (c) 2017 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Exit immediately on any error
set -e

program=xcb-util-keysyms
version=0.4.0
release=1

# Define a category for the output of the package name
pkgcategory=xorg_xcb

tarname=${program}-${version}.tar.bz2

# Remote source(s)
fetch=https://www.x.org/releases/individual/xcb/$tarname

description="
XCB util-keysyms modules.

Provides a number of libraries which sit on top of libxcb.
"

homepage=https://www.x.org
license="MIT X Consortium"

# Source documentation
docs="ChangeLog NEWS README"
docsdir="${docdir}/${program}-${version}"

build()
{
    unpack "${tardir}/$tarname"

    cd "$srcdir"

    # Set sane permissions
    chmod -R u+w,go-w,a+rX-s .

    ./configure CPPFLAGS="$QICPPFLAGS" CFLAGS="$QICFLAGS" LDFLAGS="$QILDFLAGS" \
     $configure_args \
     --libdir=/usr/lib${libSuffix} \
     --infodir=$infodir \
     --mandir=$mandir \
     --docdir=$docsdir \
     || true

    make -j${jobs} V=1
    make -j${jobs} DESTDIR="$destdir" install

    # Compress info documents deleting index file for the package
    if test -d "${destdir}/$infodir"
    then
        rm -f "${destdir}/${infodir}/dir"
        lzip -9 "${destdir}/${infodir}"/*
    fi

    # Compress and link man pages (if needed)
    if test -d "${destdir}/$mandir"
    then
        (
            cd "${destdir}/$mandir"
            find . -type f -exec lzip -9 {} +
            find . -type l | while read -r file
            do
                ln -sf "$(readlink -- "$file").lz" "${file}.lz"
                rm -- "$file"
            done
        )
    fi

    # Copy documentation
    mkdir -p "${destdir}/$docsdir"

    for file in $docs
    do
        if test -e $file
        then
            cp -p $file "${destdir}/$docsdir"
        fi
    done
}


code:
# Build recipe for xcb-util-renderutil.
#
# Copyright (c) 2017 Mateus P. Rodrigues <mprodrigues@dragora.org>.
# Copyright (c) 2017 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Exit immediately on any error
set -e

program=xcb-util-renderutil
version=0.3.9
release=1

# Define a category for the output of the package name
pkgcategory=xorg_xcb

tarname=${program}-${version}.tar.bz2

# Remote source(s)
fetch=https://www.x.org/releases/individual/xcb/$tarname

description="
XCB util-renderutil modules.

Provides a number of libraries which sit on top of libxcb.
"

homepage=https://www.x.org
license="MIT X Consortium"

# Source documentation
docs="COPYING ChangeLog NEWS README"
docsdir="${docdir}/${program}-${version}"

build()
{
    unpack "${tardir}/$tarname"

    cd "$srcdir"

    # Set sane permissions
    chmod -R u+w,go-w,a+rX-s .

    ./configure CPPFLAGS="$QICPPFLAGS" CFLAGS="$QICFLAGS" LDFLAGS="$QILDFLAGS" \
     $configure_args \
     --libdir=/usr/lib${libSuffix} \
     --infodir=$infodir \
     --mandir=$mandir \
     --docdir=$docsdir \
     || true

    make -j${jobs} V=1
    make -j${jobs} DESTDIR="$destdir" install

    # Compress info documents deleting index file for the package
    if test -d "${destdir}/$infodir"
    then
        rm -f "${destdir}/${infodir}/dir"
        lzip -9 "${destdir}/${infodir}"/*
    fi

    # Compress and link man pages (if needed)
    if test -d "${destdir}/$mandir"
    then
        (
            cd "${destdir}/$mandir"
            find . -type f -exec lzip -9 {} +
            find . -type l | while read -r file
            do
                ln -sf "$(readlink -- "$file").lz" "${file}.lz"
                rm -- "$file"
            done
        )
    fi

    # Copy documentation
    mkdir -p "${destdir}/$docsdir"

    for file in $docs
    do
        if test -e $file
        then
            cp -p $file "${destdir}/$docsdir"
        fi
    done
}


code:
# Build recipe for xcb-util-wm.
#
# Copyright (c) 2017 Mateus P. Rodrigues <mprodrigues@dragora.org>.
# Copyright (c) 2017 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Exit immediately on any error
set -e

program=xcb-util-wm
version=0.4.1
release=1

# Define a category for the output of the package name
pkgcategory=xorg_xcb

tarname=${program}-${version}.tar.bz2

# Remote source(s)
fetch=https://www.x.org/releases/individual/xcb/$tarname

description="
XCB util-wm modules.

Provides a number of libraries which sit on top of libxcb.
"

homepage=https://www.x.org
license="MIT X Consortium"

# Source documentation
docs="COPYING ChangeLog NEWS README"
docsdir="${docdir}/${program}-${version}"

build()
{
    unpack "${tardir}/$tarname"

    cd "$srcdir"

    # Set sane permissions
    chmod -R u+w,go-w,a+rX-s .

    ./configure CPPFLAGS="$QICPPFLAGS" CFLAGS="$QICFLAGS" LDFLAGS="$QILDFLAGS" \
     $configure_args \
     --libdir=/usr/lib${libSuffix} \
     --infodir=$infodir \
     --mandir=$mandir \
     --docdir=$docsdir \
     || true

    make -j${jobs} V=1
    make -j${jobs} DESTDIR="$destdir" install

    # Compress info documents deleting index file for the package
    if test -d "${destdir}/$infodir"
    then
        rm -f "${destdir}/${infodir}/dir"
        lzip -9 "${destdir}/${infodir}"/*
    fi

    # Compress and link man pages (if needed)
    if test -d "${destdir}/$mandir"
    then
        (
            cd "${destdir}/$mandir"
            find . -type f -exec lzip -9 {} +
            find . -type l | while read -r file
            do
                ln -sf "$(readlink -- "$file").lz" "${file}.lz"
                rm -- "$file"
            done
        )
    fi

    # Copy documentation
    mkdir -p "${destdir}/$docsdir"

    for file in $docs
    do
        if test -e $file
        then
            cp -p $file "${destdir}/$docsdir"
        fi
    done
}

path: recipes/xorg/xcb/xcb-util-wm/recipe


code:
# Build recipe for xcb-util.
#
# Copyright (c) 2017 Mateus P. Rodrigues <mprodrigues@dragora.org>.
# Copyright (c) 2017 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Exit immediately on any error
set -e

program=xcb-util
version=0.4.0
release=1

# Define a category for the output of the package name
pkgcategory=xorg_xcb

tarname=${program}-${version}.tar.bz2

# Remote source(s)
fetch=https://www.x.org/releases/individual/xcb/$tarname

description="
XCB util core interface.

Provides a number of libraries which sit on top of libxcb.
"

homepage=https://www.x.org
license="MIT X Consortium"

# Source documentation
docs="COPYING ChangeLog NEWS README"
docsdir="${docdir}/${program}-${version}"

build()
{
    unpack "${tardir}/$tarname"

    cd "$srcdir"

    # Set sane permissions
    chmod -R u+w,go-w,a+rX-s .

    ./configure CPPFLAGS="$QICPPFLAGS" CFLAGS="$QICFLAGS" LDFLAGS="$QILDFLAGS" \
     $configure_args \
     --libdir=/usr/lib${libSuffix} \
     --infodir=$infodir \
     --mandir=$mandir \
     --docdir=$docsdir \
     || true

    make -j${jobs} V=1
    make -j${jobs} DESTDIR="$destdir" install-strip

    # Compress info documents deleting index file for the package
    if test -d "${destdir}/$infodir"
    then
        rm -f "${destdir}/${infodir}/dir"
        lzip -9 "${destdir}/${infodir}"/*
    fi

    # Compress and link man pages (if needed)
    if test -d "${destdir}/$mandir"
    then
        (
            cd "${destdir}/$mandir"
            find . -type f -exec lzip -9 {} +
            find . -type l | while read -r file
            do
                ln -sf "$(readlink -- "$file").lz" "${file}.lz"
                rm -- "$file"
            done
        )
    fi

    # Copy documentation
    mkdir -p "${destdir}/$docsdir"

    for file in $docs
    do
        if test -e $file
        then
            cp -p $file "${destdir}/$docsdir"
        fi
    done
}

path: recipes/xorg/xcb/xcb-util/recipe


code:
mkdir -p etc/X11
ln -sf /usr/share/X11/xorg.conf.d etc/X11

path: recipes/xorg/xserver/post-install


code:
# Build recipe for xorg-server.
#
# Copyright (c) 2017 Mateus P. Rodrigues <mprodrigues@dragora.org>.
# Copyright (c) 2017-2019, 2021-2022 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Exit immediately on any error
set -e

program=xorg-server
version=1.20.14
release=2

# Define a category for the output of the package name
pkgcategory=xorg_xserver

tarname=${program}-${version}.tar.gz

# Remote source(s)
fetch=https://www.x.org/releases/individual/xserver/$tarname

description="
A portable, network-transparent Window System.

The X Window System is a network transparent window system which
runs on a wide range of computing and graphics machines.
"

homepage=https://www.x.org
license="MIT X Consortium"

# Source documentation
docs="COPYING ChangeLog README.md"
docsdir="${docdir}/${program}-${version}"

build()
{
    unpack "${tardir}/$tarname"

    cd "$srcdir"

    # Set sane permissions
    chmod -R u+w,go-w,a+rX-s .

    # Adjust headers
    sed -i 's#termio.h#termios.h#' hw/xfree86/os-support/xf86_OSlib.h

    # Apply a fix from "Alpine Linux"
    patch -p1 < "${worktree}/patches/xserver/link-libshadow.patch"

    rm -rf BUILD
    mkdir BUILD
    cd BUILD

    CPPFLAGS="$QICPPFLAGS -D_GNU_SOURCE -D__gid_t=gid_t -D__uid_t=uid_t" \
    CFLAGS="$QICFLAGS " LDFLAGS="$QILDFLAGS -Wl,-z,lazy" \
    meson setup $configure_args \
     --libdir=/usr/lib${libSuffix} \
     --mandir=$mandir \
     --buildtype=release \
     --strip \
     -Dxorg=true \
     -Dxnest=true \
     -Dglamor=true \
     -Dbuilder_addr='dragora-users@nongnu.org' \
     -Dos_vendor="Dragora GNU/Linux-Libre" \
     -Dmodule_dir=/usr/lib${libSuffix}/xorg/modules \
     -Ddefault_font_path="/usr/share/fonts/X11/misc,/usr/share/fonts/X11/Type1,/usr/share/fonts/X11/100dpi:unscaled,/usr/share/fonts/X11/75dpi:unscaled,/usr/share/fonts/X11/cyrillic,/usr/share/fonts/X11/TTF,/usr/share/fonts/X11/OTF" \
     -Dglx=true \
     -Dsecure-rpc=false \
     -Dipv6=true \
     -Dxkb_dir=/etc/X11/xkb \
     -Dxkb_output_dir=/var/lib/xkb \
     -Dlisten_tcp=false \
     -Dlisten_unix=true \
     -Dlisten_local=true \
     -Dint10=x86emu \
     -Dsuid_wrapper=true \
     -Dhal=false \
     -Dsystemd_logind=false \
     -Dxf86bigfont=true \
     -Dxcsecurity=true \
     -Dmitshm=true \
     -Ddri1=true \
     -Ddri2=true \
     -Ddri3=true \
     ..

    ninja -j${jobs}
    DESTDIR="$destdir" ninja -j${jobs} install

    cd ..

    # Required symlink
    ln -s Xorg "${destdir}"/usr/bin/X

    # Allow old behavior to terminate server keystroke
    cp -p "${worktree}/archive/xserver/00-keyboard.conf" \
          "${destdir}/usr/share/X11/xorg.conf.d"
    chmod 644 "${destdir}/usr/share/X11/xorg.conf.d/00-keyboard.conf"

    # Set the suid bit on the Xorg application.
    #
    # This will allow us to switch between tty1 and tty7 with the
    # Ctrl-Alt-F1 and Ctrl-Alt-F7 key combinations (see
    # recipes/xorg/app/xinit/recipe).  Thanks to BLFS!
    chmod 4755 "${destdir}/usr/libexec/Xorg"

    # Compress and link man pages (if needed)
    if test -d "${destdir}/$mandir"
    then
        (
            cd "${destdir}/$mandir"
            find . -type f -exec lzip -9 {} +
            find . -type l | while read -r file
            do
                ln -sf "$(readlink -- "$file").lz" "${file}.lz"
                rm -- "$file"
            done
        )
    fi

    # Copy documentation
    mkdir -p "${destdir}/$docsdir"
    cp -p $docs "${destdir}/$docsdir"
}

path: recipes/xorg/xserver/recipe


code:
# README

There are two ways to get the sources used by Dragora:

  1. Source list.
  2. RSYNC protocol.

  Normally we obtain the sources from original sites (upstream).

Full address of sources files are in the "SOURCELIST.txt" file.  A copy
of these files can be found via RSYNC at [rsync://rsync.dragora.org][1]

Please, [check our mirrors][2].

## Instructions

### 1. Source list.

Download all the sources from the list, one by one:

    wget -c -i SOURCELIST.txt

If your wget(1) implementation lacks '-i', try with:

    cat SOURCELIST.txt | wget -c -

### 2. RSYNC protocol.

    rsync -rvaiz --progress rsync://rsync.dragora.org/dragora/current/sources/ .

Note, if you are upgrading an (already) out-of-date sources directory
and want to make it clear, add the following options:

    rsync -rvaiz --progress --exclude=README.md --delete-delay \
     rsync://rsync.dragora.org/dragora/current/sources/ .

The command and options above will preserve the README.md file while deletes
any previous source.

The tarballs will be stored on the current working directory.

### Recommendation

Download all the sources from the list.

Use the RSYNC protocol to download missing sources.

## Check sums

Remember to check if everything is OK.  To check the integrity of all
the tarballs using sha256sum(1), type:

    sha256sum --quiet --check *.sha256

[1]: http://rsync.dragora.org "Browse via HTTP"
[2]: /wiki?name=Mirrors


code:
# Build script for binutils.
#
# Copyright (c) 2014-2022 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

version=2.38

cd -- "$TMPDIR"
rm -rf binutils-${version}
unpack "${worktree}/sources/binutils-${version}.tar.lz"

# Build instructions
cd binutils-${version}

# Apply patches from Fedora (Nick Clifton)

patch -Np1 -i "${worktree}/patches/binutils/2.38/binutils-export-demangle.h.patch"
patch -Np1 -i "${worktree}/patches/binutils/2.38/binutils-no-config-h-check.patch"
patch -Np1 -i "${worktree}/patches/binutils/2.38/binutils-filename-in-error-messages.patch"
patch -Np1 -i "${worktree}/patches/binutils/2.38/binutils-revert-PLT-elision.patch"
patch -Np1 -i "${worktree}/patches/binutils/2.38/binutils-readelf-other-sym-info.patch"
patch -Np1 -i "${worktree}/patches/binutils/2.38/binutils-2.27-aarch64-ifunc.patch"
patch -Np1 -i "${worktree}/patches/binutils/2.38/binutils-do-not-link-with-static-libstdc++.patch"
patch -Np1 -i "${worktree}/patches/binutils/2.38/binutils-special-sections-in-groups.patch"
patch -Np1 -i "${worktree}/patches/binutils/2.38/binutils-gold-mismatched-section-flags.patch"
patch -Np1 -i "${worktree}/patches/binutils/2.38/binutils-CVE-2019-1010204.patch"
patch -Np1 -i "${worktree}/patches/binutils/2.38/binutils-gold-warn-unsupported.patch"
patch -Np1 -i "${worktree}/patches/binutils/2.38/binutils-use-long-long.patch"
patch -Np1 -i "${worktree}/patches/binutils/2.38/binutils-gold-i386-gnu-property-notes.patch"
patch -Np1 -i "${worktree}/patches/binutils/2.38/binutils-autoconf-version.patch"
patch -Np1 -i "${worktree}/patches/binutils/2.38/gcc12-libtool-no-rpath.patch"
patch -Np1 -i "${worktree}/patches/binutils/2.38/binutils-section-type.patch"
patch -Np1 -i "${worktree}/patches/binutils/2.38/binutils-gas-loc-view.patch"
patch -Np1 -i "${worktree}/patches/binutils/2.38/binutils-do-not-use-debuginfod.patch"

patch -Np1 -i "${worktree}/patches/binutils/branch-updates.diff"

# Build in a separate directory
rm -rf ../binutils-build
mkdir ../binutils-build
cd ../binutils-build

../binutils-${version}/configure \
AR="ar" AS="as" CC="$BTCC" CXX="$BTCXX" \
CFLAGS="$BTCFLAGS" CXXFLAGS="$BTCXXFLAGS" LDFLAGS="$BTLDFLAGS" \
 --prefix="$crossdir" \
 --libdir="${crossdir}/lib${libSuffix}" \
 --host=$host \
 --target=$target \
 --enable-deterministic-archives \
 --enable-separate-code \
 --disable-compressed-debug-sections \
 --disable-generate-build-notes \
 --disable-werror \
 --disable-nls \
 --disable-ppl-version-check \
 --disable-cloog-version-check \
 --with-sysroot="${crossdir}/$target" \
 $multilib_options

make -j${jobs} V=1 MAKEINFO="true"
make -j${jobs} V=1 MAKEINFO="true" install

cd -- "$TMPDIR"

cleanup()
{
    rm -rf binutils-${version} binutils-build
}


code:
# Build script for linux-libre (headers).
#
# Copyright (c) 2014-2022 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

version=5.16.20

cd -- "$TMPDIR"
rm -rf linux-${version}
unpack "${worktree}/sources/linux-libre-${version}-gnu.tar.lz"

# Build instructions
cd linux-${version}

patch -p1 < "${worktree}/patches/linux-headers/revert-broken-uapi.patch"
patch -p1 < "${worktree}/patches/linux-headers/0003-remove-inclusion-of-sysinfo.h-in-kernel.h.patch"

make -j1 mrproper
make -j1 headers ARCH="$kernel_arch"
mkdir -p -- "${crossdir}/${target}/include"
cp -Rp usr/include/* "${crossdir}/${target}/include/"

# Remove cruft
find "${crossdir}/${target}/include" -name '.*' -exec rm -rf {} +
rm -f "${crossdir}/${target}/include/Makefile"

cleanup()
{
    cd -- "$TMPDIR" && rm -rf linux-${version}
}


code:
# Build script for gcc.
#
# Copyright (c) 2016-2022 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

version=11-20220429

# Prerequisites
gmp_version=6.2.1
mpfr_version=4.1.0
mpc_version=1.2.1
isl_version=0.24

cd -- "$TMPDIR"

rm -rf gcc-${version} gmp-${gmp_version} \
       mpfr-${mpfr_version} mpc-${mpc_version} isl-${isl_version}
unpack "${worktree}/sources/gcc-${version}.tar.xz" \
       "${worktree}/sources/gmp-${gmp_version}.tar.lz" \
       "${worktree}/sources/mpfr-${mpfr_version}.tar.bz2" \
       "${worktree}/sources/mpc-${mpc_version}.tar.gz" \
       "${worktree}/sources/isl-${isl_version}.tar.bz2"

# Build instructions
cd gcc-${version}

# Make symlinks for requisites
ln -s ../gmp-${gmp_version} gmp
ln -s ../mpfr-${mpfr_version} mpfr
ln -s ../mpc-${mpc_version} mpc
ln -s ../isl-${isl_version} isl

# Apply patches for MPFR
(
    cd mpfr || exit 1

    for file in "${worktree}"/patches/mpfr/*
    do
        if test -f "$file"
        then
            rm -f PATCHES
            patch -p1 < "$file"
        fi
    done
)

# Update detection for hosts based on musl

cp -f "${worktree}/archive/common/config.guess" gmp/configfsf.guess
cp -f "${worktree}/archive/common/config.sub" gmp/config.sub

cp -f "${worktree}/archive/common/config.guess" isl/config.guess
cp -f "${worktree}/archive/common/config.sub" isl/config.sub

# Apply specific patches for the support in musl.
# https://port70.net/~nsz/musl/gcc-11.1.0/

patch -Np1 -i "${worktree}/patches/gcc/11/0002-posix_memalign.patch"
patch -Np1 -i "${worktree}/patches/gcc/11/0003-j2.patch"
patch -Np1 -i "${worktree}/patches/gcc/11/0004-static-pie.patch"
patch -Np1 -i "${worktree}/patches/gcc/11/0005-m68k-sqrt.patch"
patch -Np1 -i "${worktree}/patches/gcc/11/extra-musl_libssp.patch"

patch -Np1 -i "${worktree}/patches/gcc/0008-Disable-ssp-on-nostdlib-nodefaultlibs-and-ffreestand.patch"

# Build in a separate directory
rm -rf ../gcc-build
mkdir ../gcc-build
cd ../gcc-build

../gcc-${version}/configure \
AR="ar" CC="$BTCC" CXX="$BTCXX" \
CFLAGS="$BTCFLAGS" CXXFLAGS="$BTCXXFLAGS" LDFLAGS="$BTLDFLAGS" \
 --prefix="$crossdir" \
 --libdir="${crossdir}/lib${libSuffix}" \
 --build=$host \
 --host=$host \
 --target=$target \
 --enable-languages=c \
 --enable-clocale=generic \
 --enable-initfini-array \
 --disable-shared \
 --disable-threads \
 --disable-decimal-float \
 --disable-libgomp \
 --disable-libssp \
 --disable-libatomic \
 --disable-libitm \
 --disable-libquadmath \
 --disable-libvtv \
 --disable-libcilkrts \
 --disable-libstdcxx \
 --disable-gnu-indirect-function \
 --disable-libsanitizer \
 --disable-libmpx \
 --disable-nls \
 --with-sysroot="${crossdir}/$target" \
 --with-newlib \
 --without-headers \
 --without-ppl \
 --without-cloog \
 $multilib_options \
 $gcc_options

make -j${jobs} all-gcc all-target-libgcc
make -j${jobs} install-gcc install-target-libgcc

cd ..

cleanup()
{
    cd -- "$TMPDIR" && rm -rf gcc-${version} gcc-build \
     gmp-${gmp_version} mpfr-${mpfr_version} \
     mpc-${mpc_version} isl-${isl_version}
}


code:
# Build script for musl.
#
# Copyright (c) 2014-2022 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

version=1.2.3

cd -- "$TMPDIR"
rm -rf musl-${version}
unpack "${worktree}/sources/musl-${version}.tar.gz"

# Build instructions
cd musl-${version}

#patch -Np1 -i "${worktree}/patches/musl/branch-updates.diff"

./configure CC="${target}-gcc" CROSS_COMPILE="${target}-" \
 CFLAGS="$BTCFLAGS" \
 --prefix= \
 --syslibdir=/lib \
 --enable-optimize \
 --enable-debug

make -j${jobs}
make -j${jobs} DESTDIR="${crossdir}/$target" install

# Provide minimal libssp_nonshared.a for the bootstrap process

${target}-gcc \
 -c "${worktree}/archive/gcc/__stack_chk_fail_local.c" \
 -o __stack_chk_fail_local.o

${target}-ar rc libssp_nonshared.a __stack_chk_fail_local.o
${target}-ranlib libssp_nonshared.a
cp -p libssp_nonshared.a "${crossdir}/${target}/lib/"

cleanup()
{
    cd -- "$TMPDIR" && rm -rf musl-${version}
}


code:
# Build script for gcc.
#
# Copyright (c) 2016-2022 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

version=11-20220429

# Prerequisites
gmp_version=6.2.1
mpfr_version=4.1.0
mpc_version=1.2.1
isl_version=0.24

cd -- "$TMPDIR"

rm -rf gcc-${version} gmp-${gmp_version} \
       mpfr-${mpfr_version} mpc-${mpc_version} isl-${isl_version}
unpack "${worktree}/sources/gcc-${version}.tar.xz" \
       "${worktree}/sources/gmp-${gmp_version}.tar.lz" \
       "${worktree}/sources/mpfr-${mpfr_version}.tar.bz2" \
       "${worktree}/sources/mpc-${mpc_version}.tar.gz" \
       "${worktree}/sources/isl-${isl_version}.tar.bz2"

# Build instructions
cd gcc-${version}

# Make symlinks for requisites
ln -s ../gmp-${gmp_version} gmp
ln -s ../mpfr-${mpfr_version} mpfr
ln -s ../mpc-${mpc_version} mpc
ln -s ../isl-${isl_version} isl

# Apply patches for MPFR
(
    cd mpfr || exit 1

    for file in "${worktree}"/patches/mpfr/*
    do
        if test -f "$file"
        then
            rm -f PATCHES
            patch -p1 < "$file"
        fi
    done
)

# Update detection for hosts based on musl

cp -f "${worktree}/archive/common/config.guess" gmp/configfsf.guess
cp -f "${worktree}/archive/common/config.sub" gmp/config.sub

cp -f "${worktree}/archive/common/config.guess" isl/config.guess
cp -f "${worktree}/archive/common/config.sub" isl/config.sub

# Apply specific patches for the support in musl.
# https://port70.net/~nsz/musl/gcc-11.1.0/

patch -Np1 -i "${worktree}/patches/gcc/11/0002-posix_memalign.patch"
patch -Np1 -i "${worktree}/patches/gcc/11/0003-j2.patch"
patch -Np1 -i "${worktree}/patches/gcc/11/0004-static-pie.patch"
patch -Np1 -i "${worktree}/patches/gcc/11/0005-m68k-sqrt.patch"
patch -Np1 -i "${worktree}/patches/gcc/11/extra-musl_libssp.patch"

patch -Np1 -i "${worktree}/patches/gcc/0008-Disable-ssp-on-nostdlib-nodefaultlibs-and-ffreestand.patch"

# Build in a separate directory
rm -rf ../gcc-build
mkdir ../gcc-build
cd ../gcc-build

option_list="
 --prefix="$crossdir" \
 --libdir="${crossdir}/lib${libSuffix}" \
 --build=$host \
 --host=$host \
 --target=$target \
 --enable-languages=c,c++ \
 --enable-clocale=generic \
 --enable-initfini-array \
 --enable-tls \
 --enable-libstdcxx-time \
 --enable-checking=release \
 --enable-fully-dynamic-string \
 --enable-libssp \
 --disable-symvers \
 --disable-gnu-indirect-function \
 --disable-libsanitizer \
 --disable-libmpx \
 --disable-nls \
 --with-sysroot="${crossdir}/$target" \
 $multilib_options \
 $gcc_options
"

if test -n "$IS_DARKCRUSADE"
then
    ../gcc-${version}/configure CC="$BTCC" CXX="$BTCXX" AR="ar" \
    CFLAGS="$BTCFLAGS" CXXFLAGS="$BTCXXFLAGS" LDFLAGS="$BTLDFLAGS" \
    $option_list
else
    ../gcc-${version}/configure AR="ar" \
    CFLAGS="$BTCFLAGS" CXXFLAGS="$BTCXXFLAGS" LDFLAGS="$BTLDFLAGS" \
    $option_list
fi

unset option_list IS_DARKCRUSADE

make -j${jobs} all \
 AS_FOR_TARGET="${target}-as" \
 LD_FOR_TARGET="${target}-ld"

make -j${jobs} install

# Insert again libssp_nonshared.a overwritten the recent library
${target}-gcc \
 -c "${worktree}/archive/gcc/__stack_chk_fail_local.c" \
 -o __stack_chk_fail_local.o

${target}-ar rc libssp_nonshared.a __stack_chk_fail_local.o
${target}-ranlib libssp_nonshared.a
cp -f libssp_nonshared.a "${crossdir}/${target}/lib/"

cd -- "$TMPDIR"

cleanup()
{
    rm -rf gcc-${version} gcc-build \
     gmp-${gmp_version} mpfr-${mpfr_version} \
     mpc-${mpc_version} isl-${isl_version}
}


code:
# Build script for the filesystem layout (Dragora).
#
# Copyright (c) 2014-2018, 2022 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# These steps are to satisfy "19-symlinks", the final
# steps are done from "recipes/data/hierarchyfs".

echo "Making minimal directory layout on 'rootdir' ..."

mkdir -p -- \
 "${rootdir}/etc"      \
 "${rootdir}/root"     \
 "${rootdir}/tmp"      \
 "${rootdir}/usr/bin"  \
 "${rootdir}/var/log"
(
    cd -- "${rootdir}"
    chmod 0750 root; chmod 1777 tmp
    if test ! -L bin
    then
        rm -f bin
        ln -s -f usr/bin bin
    fi
)

# Handle lib<equal> directory for the multi directory support
if test -n "$libSuffix"
then
    echo "Handling lib<equal> for ${rootdir}/usr/lib${libSuffix} ..."
    (
        cd -- "${rootdir}/usr"
        mkdir -p lib${libSuffix}
        if test ! -L lib
        then
            rm -f lib
            ln -s -f lib${libSuffix} lib
        fi
    )

    # Make symlink of compatibility for / at $rootdir
    cd -- "$rootdir"

    echo "Handling lib<equal> for ${rootdir}/lib${libSuffix} ..."
    if test ! -L "lib${libSuffix}"
    then
        rm -f lib${libSuffix}
        ln -s -f usr/lib${libSuffix} lib${libSuffix}
    fi

    echo "Handling lib<equal> for ${rootdir}/lib ..."
    if test ! -L lib
    then
        rm -f lib
        ln -s -f lib${libSuffix} lib
    fi

    echo "Making compatibility for ${rootdir}/tools/lib${libSuffix} ..."
    cd tools
    mkdir -p lib${libSuffix}
    if test ! -L lib
    then
        rm -f lib
        ln -s -f lib${libSuffix} lib
    fi
else
    mkdir -p -- "${rootdir}/lib" "${rootdir}/tools/lib" "${rootdir}/usr/lib"
fi

# Save the value of 'libSuffix'

if test -n "$libSuffix"
then
    mkdir -p /tools/etc
    echo "$libSuffix" > /tools/etc/stage1_is-multi-directory
fi


code:
# Build script for musl.
#
# Copyright (c) 2014-2022 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

version=1.2.3

cd -- "$TMPDIR"
rm -rf musl-${version}
unpack "${worktree}/sources/musl-${version}.tar.gz"

# Build instructions
cd musl-${version}

#patch -Np1 -i "${worktree}/patches/musl/branch-updates.diff"

./configure CROSS_COMPILE="${target}-" CFLAGS="$BTCFLAGS" \
 --prefix=/tools \
 --libdir=/tools/lib${libSuffix} \
 --syslibdir=/tools/lib \
 --build=$host \
 --host=$target \
 --enable-optimize

make -j${jobs}
make -j${jobs} DESTDIR="$rootdir" install

cleanup()
{
    cd -- "$TMPDIR" && rm -rf musl-${version}
}


code:
# Build script for linux-libre (headers).
#
# Copyright (c) 2014-2022 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

version=5.16.20

cd -- "$TMPDIR"
rm -rf linux-${version}
unpack "${worktree}/sources/linux-libre-${version}-gnu.tar.lz"

# Build instructions
cd linux-${version}

patch -p1 < "${worktree}/patches/linux-headers/revert-broken-uapi.patch"
patch -p1 < "${worktree}/patches/linux-headers/0003-remove-inclusion-of-sysinfo.h-in-kernel.h.patch"

make -j1 mrproper
make -j1 headers ARCH="$kernel_arch"
mkdir -p -- "${rootdir}/tools/include"
cp -Rp usr/include/* "${rootdir}/tools/include/"

# Remove cruft
find "${rootdir}/tools/include" -name '.*' -exec rm -rf {} +
rm -f "${rootdir}/tools/include/Makefile"

cleanup()
{
    cd -- "$TMPDIR" && rm -rf linux-${version}
}


code:
# Build script for fortify-headers.
#
# Copyright (c) 2021 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

version=1.1

cd -- "$TMPDIR"
rm -rf linux-${version}
unpack "${worktree}/sources/fortify-headers-${version}.tar.gz"

# Build instructions
cd fortify-headers-${version}

make -j1 PREFIX=/tools install

cleanup()
{
    cd -- "$TMPDIR" && rm -rf fortify-headers-${version}
}


code:
# Build script for binutils.
#
# Copyright (c) 2014-2022 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

version=2.38

cd -- "$TMPDIR"
rm -rf binutils-${version}
unpack "${worktree}/sources/binutils-${version}.tar.lz"

# Build instructions
cd binutils-${version}

# Apply patches from Fedora (Nick Clifton)

patch -Np1 -i "${worktree}/patches/binutils/2.38/binutils-export-demangle.h.patch"
patch -Np1 -i "${worktree}/patches/binutils/2.38/binutils-no-config-h-check.patch"
patch -Np1 -i "${worktree}/patches/binutils/2.38/binutils-filename-in-error-messages.patch"
patch -Np1 -i "${worktree}/patches/binutils/2.38/binutils-revert-PLT-elision.patch"
patch -Np1 -i "${worktree}/patches/binutils/2.38/binutils-readelf-other-sym-info.patch"
patch -Np1 -i "${worktree}/patches/binutils/2.38/binutils-2.27-aarch64-ifunc.patch"
patch -Np1 -i "${worktree}/patches/binutils/2.38/binutils-do-not-link-with-static-libstdc++.patch"
patch -Np1 -i "${worktree}/patches/binutils/2.38/binutils-special-sections-in-groups.patch"
patch -Np1 -i "${worktree}/patches/binutils/2.38/binutils-gold-mismatched-section-flags.patch"
patch -Np1 -i "${worktree}/patches/binutils/2.38/binutils-CVE-2019-1010204.patch"
patch -Np1 -i "${worktree}/patches/binutils/2.38/binutils-gold-warn-unsupported.patch"
patch -Np1 -i "${worktree}/patches/binutils/2.38/binutils-use-long-long.patch"
patch -Np1 -i "${worktree}/patches/binutils/2.38/binutils-gold-i386-gnu-property-notes.patch"
patch -Np1 -i "${worktree}/patches/binutils/2.38/binutils-autoconf-version.patch"
patch -Np1 -i "${worktree}/patches/binutils/2.38/gcc12-libtool-no-rpath.patch"
patch -Np1 -i "${worktree}/patches/binutils/2.38/binutils-section-type.patch"
patch -Np1 -i "${worktree}/patches/binutils/2.38/binutils-gas-loc-view.patch"
patch -Np1 -i "${worktree}/patches/binutils/2.38/binutils-do-not-use-debuginfod.patch"

patch -Np1 -i "${worktree}/patches/binutils/branch-updates.diff"

# Build in a separate directory
rm -rf ../binutils-build
mkdir ../binutils-build
cd ../binutils-build

# Import and export toolchain variables
. "${worktree}/stages/env.d/cross-staticenv"

../binutils-${version}/configure \
AR="$AR" AS="$AS" LD="$LD" RANLIB="$RANLIB" READELF="$READELF" STRIP="$STRIP" \
CC="$BTCC" CXX="$BTCXX" \
CFLAGS="$BTCFLAGS" CXXFLAGS="$BTCXXFLAGS" LDFLAGS="$BTLDFLAGS" \
 --prefix=/tools \
 --libdir=/tools/lib${libSuffix} \
 --build=$host \
 --host=$target \
 --target=$target \
 --enable-deterministic-archives \
 --enable-separate-code \
 --enable-relro \
 --enable-threads=auto \
 --enable-default-hash-style=gnu \
 --enable-64-bit-bfd \
 --enable-install-libiberty \
 --disable-compressed-debug-sections \
 --disable-generate-build-notes \
 --disable-nls \
 --disable-werror \
 --disable-ppl-version-check \
 --disable-cloog-version-check \
 --with-lib-path=/tools/lib \
 --with-pic \
 $multilib_options

make -j${jobs} V=1 MAKEINFO="true"
make -j${jobs} V=1 MAKEINFO="true" install

# Make a new ld(1) containing another search path,
# this is handled by "20-toolchain" (called from musl's recipe)
make -C ld clean
make -C ld LIB_PATH=/usr/lib${libSuffix}:/lib
cp -f ld/ld-new /tools/bin

# Unset some imported variables from file
unset AR AS LD RANLIB READELF STRIP

cd -- "$TMPDIR"

cleanup()
{
    rm -rf binutils-${version} binutils-build
}


code:
# Build script for gcc.
#
# Copyright (c) 2016-2022 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

version=11-20220429

# Prerequisites
gmp_version=6.2.1
mpfr_version=4.1.0
mpc_version=1.2.1
isl_version=0.24

cd -- "$TMPDIR"

rm -rf gcc-${version} gmp-${gmp_version} \
       mpfr-${mpfr_version} mpc-${mpc_version} isl-${isl_version}
unpack "${worktree}/sources/gcc-${version}.tar.xz" \
       "${worktree}/sources/gmp-${gmp_version}.tar.lz" \
       "${worktree}/sources/mpfr-${mpfr_version}.tar.bz2" \
       "${worktree}/sources/mpc-${mpc_version}.tar.gz" \
       "${worktree}/sources/isl-${isl_version}.tar.bz2"

# Build instructions
cd gcc-${version}

# Apply specific patches for the support in musl.
# https://port70.net/~nsz/musl/gcc-11.1.0/

patch -Np1 -i "${worktree}/patches/gcc/11/0002-posix_memalign.patch"
patch -Np1 -i "${worktree}/patches/gcc/11/0003-j2.patch"
patch -Np1 -i "${worktree}/patches/gcc/11/0004-static-pie.patch"
patch -Np1 -i "${worktree}/patches/gcc/11/0005-m68k-sqrt.patch"
patch -Np1 -i "${worktree}/patches/gcc/11/extra-musl_libssp.patch"

# Apply patches from "Alpine Linux" in order to improve the security (Thanks!)
echo "patch1:"
patch -Np1 -i "${worktree}/patches/gcc/0004-Turn-on-D_FORTIFY_SOURCE-2-by-default-for-C-C-ObjC-O.patch"
echo "patch2:"
patch -Np1 -i "${worktree}/patches/gcc/0005-On-linux-targets-pass-as-needed-by-default-to-the-li.patch"
echo "patch3:"
patch -Np1 -i "${worktree}/patches/gcc/0006-Enable-Wformat-and-Wformat-security-by-default.patch"
echo "patch4:"
patch -Np1 -i "${worktree}/patches/gcc/0007-Enable-Wtrampolines-by-default.patch"
echo "patch5:"
patch -Np1 -i "${worktree}/patches/gcc/0008-Disable-ssp-on-nostdlib-nodefaultlibs-and-ffreestand.patch"
echo "patch6:"
patch -Np1 -i "${worktree}/patches/gcc/0021-add-fortify-headers-paths.patch"

# Hard code default ld path to look at /tools

find . -type f -name '*.h' -exec grep -l "/lib/ld-" {} + | while read -r file
do
    test -f "$file" || continue;
    cp -f "$file" "${file}.orig"

    sed -e 's@/lib\(64\)\{0,1\}\(32\)\{0,1\}/ld@/tools&@g' \
        -e 's@/usr@/tools@g' "${file}.orig" > "$file"
    echo '
#undef STANDARD_STARTFILE_PREFIX_1
#undef STANDARD_STARTFILE_PREFIX_2
#define STANDARD_STARTFILE_PREFIX_1 "/tools/lib/"
#define STANDARD_STARTFILE_PREFIX_2 ""' >> "$file"
done

# Make symlinks for requisites
ln -s ../gmp-${gmp_version} gmp
ln -s ../mpfr-${mpfr_version} mpfr
ln -s ../mpc-${mpc_version} mpc
ln -s ../isl-${isl_version} isl

# Apply patches for MPFR
(
    cd mpfr || exit 1

    for file in "${worktree}"/patches/mpfr/*
    do
        if test -f "$file"
        then
            rm -f PATCHES
            patch -p1 < "$file"
        fi
    done
)

# Update detection for hosts based on musl

cp -f "${worktree}/archive/common/config.guess" gmp/configfsf.guess
cp -f "${worktree}/archive/common/config.sub" gmp/config.sub

cp -f "${worktree}/archive/common/config.guess" isl/config.guess
cp -f "${worktree}/archive/common/config.sub" isl/config.sub

# Build in a separate directory
rm -rf ../gcc-build
mkdir ../gcc-build
cd ../gcc-build

# Import and export toolchain variables
. "${worktree}/stages/env.d/cross-staticenv"

../gcc-${version}/configure \
AR="$AR" AS="$AS" LD="$LD" RANLIB="$RANLIB" READELF="$READELF" STRIP="$STRIP" \
CC="$BTCC" CXX="$BTCXX" \
CFLAGS="$BTCFLAGS" CXXFLAGS="$BTCXXFLAGS" LDFLAGS="$BTLDFLAGS" \
 --prefix=/tools \
 --libdir=/tools/lib${libSuffix} \
 --build=$host \
 --host=$target \
 --target=$target \
 --enable-languages=c,c++ \
 --enable-clocale=generic \
 --enable-cet=auto \
 --enable-initfini-array \
 --enable-tls \
 --enable-libstdcxx-time \
 --enable-fully-dynamic-string \
 --enable-default-ssp \
 --enable-default-pie \
 --enable-libssp \
 --disable-symvers \
 --disable-bootstrap \
 --disable-libstdcxx-pch \
 --disable-gnu-indirect-function \
 --disable-libsanitizer \
 --disable-nls \
 --disable-install-libiberty \
 --with-linker-hash-style=gnu \
 --with-local-prefix=/tools \
 --with-native-system-header-dir=/tools/include \
 $multilib_options \
 $gcc_options

make -j${jobs} all \
 AS_FOR_TARGET="$AS" \
 LD_FOR_TARGET="$LD"

make -j${jobs} install

# Provide minimal libssp_nonshared.a

$BTCC -c "${worktree}/archive/gcc/__stack_chk_fail_local.c" \
      -o __stack_chk_fail_local.o
$AR rc libssp_nonshared.a __stack_chk_fail_local.o
$RANLIB libssp_nonshared.a
cp -p libssp_nonshared.a /tools/lib${libSuffix}

# Unset some imported variables from file
unset AR AS LD RANLIB READELF STRIP

cd -- "$TMPDIR"

cleanup()
{
    rm -rf gcc-${version} gcc-build \
     gmp-${gmp_version} mpfr-${mpfr_version} \
     mpc-${mpc_version} isl-${isl_version}
}


code:
# Build script for file.
#
# Copyright (c) 2014-2022 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

version=5.41

cd -- "$TMPDIR"
rm -rf file-${version}
unpack "${worktree}/sources/file-${version}.tar.gz"

# Build instructions
cd file-${version}

# A local installation of "file" is needed in order
# to cross-compile "file" matching the same version
./configure \
 --prefix="$crossdir" \
 --disable-fsect-man5

make -j1 V=1
make -j1 install

make -j1 clean

# Import and export toolchain variables
. "${worktree}/stages/env.d/cross-staticenv"

./configure \
AR="$AR" AS="$AS" LD="$LD" RANLIB="$RANLIB" READELF="$READELF" STRIP="$STRIP" \
CC="$BTCC" CXX="$BTCXX" \
CFLAGS="$BTCFLAGS" CXXFLAGS="$BTCXXFLAGS" LDFLAGS="$BTLDFLAGS" \
 --prefix=/tools \
 --build=$host \
 --host=$target \
 --disable-fsect-man5

make -j1 V=1
make -j1 install

# Unset some imported variables from file
unset AR AS LD RANLIB READELF STRIP

cleanup()
{
    cd -- "$TMPDIR" && rm -rf file-${version}
}


code:
# Build script for make.
#
# Copyright (c) 2014-2017 Matias Fonzo, <selk@dragora.org>.
# Copyright (c) 2020 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

version=4.3

cd -- "$TMPDIR"
rm -rf make-${version}
unpack "${worktree}/sources/make-${version}.tar.lz"

# Build instructions
cd make-${version}

# Import and export toolchain variables
. "${worktree}/stages/env.d/cross-staticenv"

./configure \
AR="$AR" AS="$AS" LD="$LD" RANLIB="$RANLIB" READELF="$READELF" STRIP="$STRIP" \
CC="$BTCC" CXX="$BTCXX" \
CFLAGS="$BTCFLAGS" CXXFLAGS="$BTCXXFLAGS" LDFLAGS="$BTLDFLAGS" \
 --prefix=/tools \
 --build=$host \
 --host=$target \
 --disable-nls \
 --without-guile

make -j${jobs}
make -j${jobs} install

# Unset some imported variables from file
unset AR AS LD RANLIB READELF STRIP

cleanup()
{
    cd -- "$TMPDIR" && rm -rf make-${version}
}


code:
# Build script for GNU ed.
#
# Copyright (c) 2017-2022 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

version=1.18

cd -- "$TMPDIR"
rm -rf ed-${version}
unpack "${worktree}/sources/ed-${version}.tar.lz"

# Build instructions
cd ed-${version}

# Import and export toolchain variables
. "${worktree}/stages/env.d/cross-staticenv"

./configure \
AR="$AR" AS="$AS" LD="$LD" RANLIB="$RANLIB" READELF="$READELF" STRIP="$STRIP" \
CC="$BTCC" CXX="$BTCXX" \
CFLAGS="$BTCFLAGS" LDFLAGS="$BTLDFLAGS" \
 --prefix=/tools \
 --build=$host \
 --host=$target

make -j${jobs}
make -j${jobs} install

# Unset some imported variables from file
unset AR AS LD RANLIB READELF STRIP

cleanup()
{
    cd -- "$TMPDIR" && rm -rf ed-${version}
}


code:
# Build script for GNU patch.
#
# Copyright (c) 2017-2018 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

version=2.7.6

cd -- "$TMPDIR"
rm -rf patch-${version}
unpack "${worktree}/sources/patch-${version}.tar.bz2"

# Build instructions
cd patch-${version}

# Update for hosts based on musl
cp -f "${worktree}/archive/common/config.guess" build-aux/config.guess
cp -f "${worktree}/archive/common/config.sub" build-aux/config.sub

# Import and export toolchain variables
. "${worktree}/stages/env.d/cross-staticenv"

./configure \
AR="$AR" AS="$AS" LD="$LD" RANLIB="$RANLIB" READELF="$READELF" STRIP="$STRIP" \
CC="$BTCC" CXX="$BTCXX" \
CFLAGS="$BTCFLAGS" LDFLAGS="$BTLDFLAGS" \
 --prefix=/tools \
 --build=$host \
 --host=$target

make -j${jobs}
make -j${jobs} install

# Unset some imported variables from file
unset AR AS LD RANLIB READELF STRIP

cleanup()
{
    cd -- "$TMPDIR" && rm -rf patch-${version}
}


code:
# Build script for perl and perl-cross.
# Perl-cross URL: http://arsv.github.io/perl-cross/
#
# Copyright (c) 2014-2022 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

version=5.32.1
cross_ver=1.3.5

cd -- "$TMPDIR"
rm -rf perl-${version} perl-cross-${cross_ver}
unpack "${worktree}/sources/perl-${version}.tar.gz" \
       "${worktree}/sources/perl-cross-${cross_ver}.tar.gz"

# Copy perl-cross over Perl distribution
cp -f -r perl-cross-${cross_ver}/* perl-${version}/

# Build instructions
cd perl-${version}

./configure \
 --prefix=/tools \
 --target=$target \
 --all-static \
 -Dusethreads \
 -Dprivlib=/tools/lib${libSuffix}/perl5 \
 -Dsitelib=/tools/lib${libSuffix}/perl5/site_perl \
 -Dldflags="$BTLDFLAGS -static"

make -j1

mkdir -p -- "${rootdir}/tools/bin" "${rootdir}/tools/lib${libSuffix}/perl5"
cp -f perl cpan/podlators/scripts/pod2man "${rootdir}/tools/bin"
cp -R lib/* "${rootdir}/tools/lib${libSuffix}/perl5"

cleanup()
{
    cd -- "$TMPDIR" && rm -rf perl-${version} perl-cross-${cross_ver}
}


code:
# Build script for texinfo.
#
# Copyright (c) 2018-2019, 2021 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

version=6.8

cd -- "$TMPDIR"
rm -rf texinfo-${version}
unpack "${worktree}/sources/texinfo-${version}.tar.gz"

# Build instructions
cd texinfo-${version}

# https://lists.gnu.org/archive/html/bug-texinfo/2021-09/msg00011.html
patch -Np1 -i "${worktree}/patches/texinfo/headings-single.patch"

# Import and export toolchain variables
. "${worktree}/stages/env.d/cross-staticenv"

./configure \
AR="$AR" AS="$AS" LD="$LD" RANLIB="$RANLIB" READELF="$READELF" STRIP="$STRIP" \
CC="$BTCC" CXX="$BTCXX" \
CFLAGS="$BTCFLAGS" CXXFLAGS="$BTCXXFLAGS" LDFLAGS="$BTLDFLAGS" \
PERL=/tools/bin/perl \
 --prefix=/tools \
 --build=$host \
 --host=$target \
 --disable-nls

make -j${jobs}
make -j${jobs} install

# Unset some imported variables from file
unset AR AS LD RANLIB READELF STRIP

cleanup()
{
    cd -- "$TMPDIR" && rm -rf texinfo-${version}
}


code:
# Build script for graft.
#
# Copyright (c) 2015-2018 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

version=2.16

cd -- "$TMPDIR"
rm -rf graft-${version}
unpack "${worktree}/sources/graft-${version}.tar.gz"

# Build instructions
cd graft-${version}

# Adjust PACKAGEDIR and TARGETDIR:
#
# PACKAGEDIR = /usr/pkg
# TARGETDIR  = /
# PERL       = /tools/bin/perl
#
patch -Np0 -i "${worktree}/patches/graft/Makefile.dist-stage1.diff"

make -j1 -f Makefile.dist || true
make -j1

cat graft > /tools/bin/graft
chmod 755 /tools/bin/graft

cleanup()
{
    cd -- "$TMPDIR" && rm -rf graft-${version}
}


code:
# Build script for lzlib.
#
# Copyright (c) 2019-2022 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

version=1.13

cd -- "$TMPDIR"
rm -rf lzlib-${version}
unpack "${worktree}/sources/lzlib-${version}.tar.lz"

# Build instructions
cd lzlib-${version}

# Import and export toolchain variables
. "${worktree}/stages/env.d/cross-staticenv"

./configure \
AR="$AR" AS="$AS" LD="$LD" RANLIB="$RANLIB" READELF="$READELF" STRIP="$STRIP" \
CC="$BTCC" CXX="$BTCXX" \
CFLAGS="-Wall -W $BTCFLAGS -O3" LDFLAGS="$BTLDFLAGS" \
 --prefix=/tools \
 --libdir=/tools/lib${libSuffix} \
 --disable-ldconfig

make -j${jobs}
make -j${jobs} install

# Unset some imported variables from file
unset AR AS LD RANLIB READELF STRIP

cleanup()
{
    cd -- "$TMPDIR" && rm -rf lzlib-${version}
}


code:
# Build script for tarlz.
#
# Copyright (c) 2019-2022 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

version=0.22

cd -- "$TMPDIR"
rm -rf tarlz-${version}
unpack "${worktree}/sources/tarlz-${version}.tar.lz"

# Build instructions
cd tarlz-${version}

# Import and export toolchain variables
. "${worktree}/stages/env.d/cross-staticenv"

./configure \
AR="$AR" AS="$AS" LD="$LD" RANLIB="$RANLIB" READELF="$READELF" STRIP="$STRIP" \
CC="$BTCC" CXX="$BTCXX" \
CXXFLAGS="$BTCXXFLAGS" CPPFLAGS="-I/tools/include" \
LDFLAGS="$BTLDFLAGS -L/tools/lib${libSuffix}" \
 --prefix=/tools

make -j${jobs}
make -j${jobs} install

# Unset some imported variables from file
unset AR AS LD RANLIB READELF STRIP

cleanup()
{
    cd -- "$TMPDIR" && rm -rf tarlz-${version}
}


code:
# Build script for plzip (as lzip(1) installation).
#
# Copyright (c) 2019-2022 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

version=1.10

cd -- "$TMPDIR"
rm -rf plzip-${version}
unpack "${worktree}/sources/plzip-${version}.tar.lz"

# Build instructions
cd plzip-${version}

# Import and export toolchain variables
. "${worktree}/stages/env.d/cross-staticenv"

./configure \
AR="$AR" AS="$AS" LD="$LD" RANLIB="$RANLIB" READELF="$READELF" STRIP="$STRIP" \
CC="$BTCC" CXX="$BTCXX" \
CXXFLAGS="$BTCXXFLAGS" CPPFLAGS="-I/tools/include" \
LDFLAGS="$BTLDFLAGS -L/tools/lib${libSuffix}" \
 --prefix=/tools

make -j${jobs}
make -j${jobs} install-as-lzip

# Unset some imported variables from file
unset AR AS LD RANLIB READELF STRIP

cleanup()
{
    cd -- "$TMPDIR" && rm -rf plzip-${version}
}


code:
# Build script for qi.
#
# Copyright (c) 2015-2022 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

echo "Making Qi from ${worktree}/qi ..."

cd -- "${worktree}/qi"

make -j1 clean

./configure \
 --prefix=/tools \
 --packagedir=/usr/pkg \
 --targetdir=/ \
 --arch=$package_arch

make -j1 install

# Clean up the source repository

rm -f config.mak
make -j1 clean

# Make "usr/pkg" directory and output
# directory for packages available
mkdir -p -- "${rootdir}/usr/pkg" "${rootdir}/var/cache/qi/packages"

# Adjust prefixes in config for final system installation
sed -i \
 -e 's|/tools|/usr|g'   \
 -e 's|/usr/etc|/etc|'  \
 -e 's|/usr/var|/var|'  \
 /tools/etc/qirc

# Set default compiler options for Dragora
case $arch in
i586)
    sed -i \
     -e 's|#QICFLAGS=.*|QICFLAGS=\"-O2 -march=pentium-mmx -mtune=pentiumpro -fstack-clash-protection -fcf-protection=full -fstack-protector-strong --param=ssp-buffer-size=4 -pipe\"|' \
     -e 's|#QICXXFLAGS=.*|QICXXFLAGS=\"-O2 -march=pentium-mmx -mtune=pentiumpro -fstack-clash-protection -fcf-protection=full -fstack-protector-strong --param=ssp-buffer-size=4 -pipe\"|' \
     -e 's|#QILDFLAGS=.*|QILDFLAGS=\"-s\"|' \
     -e 's|#QICPPFLAGS=.*|QICPPFLAGS=\"-D_FORTIFY_SOURCE=2\"|' \
     /tools/etc/qirc
    ;;
x86_64)
    sed -i \
     -e 's|#QICFLAGS=.*|QICFLAGS=\"-O2 -march=x86-64 -fstack-clash-protection -fcf-protection=full -fstack-protector-strong --param=ssp-buffer-size=4 -pipe\"|' \
     -e 's|#QICXXFLAGS=.*|QICXXFLAGS=\"-O2 -march=x86-64 -fstack-clash-protection -fcf-protection=full -fstack-protector-strong --param=ssp-buffer-size=4 -pipe\"|' \
     -e 's|#QILDFLAGS=.*|QILDFLAGS=\"-s\"|' \
     -e 's|#QICPPFLAGS=.*|QICPPFLAGS=\"-D_FORTIFY_SOURCE=2\"|' \
     /tools/etc/qirc
    ;;
*)
    sed -i \
     -e 's|#QICFLAGS=.*|QICFLAGS=\"-O2 -fstack-protector-strong --param=ssp-buffer-size=4 -pipe\"|' \
     -e 's|#QICXXFLAGS=.*|QICXXFLAGS=\"-O2 -fstack-protector-strong --param=ssp-buffer-size=4 -pipe\"|' \
     -e 's|#QILDFLAGS=.*|QILDFLAGS=\"-s\"|' \
     -e 's|#QICPPFLAGS=.*|QICPPFLAGS=\"-D_FORTIFY_SOURCE=2\"|' \
     /tools/etc/qirc
    ;;
esac

# Include additional settings from targets/

# We prefix '--enable-multilib' for targets supporting it,
# the native GCC wants this option explicitly
if test -n "$multilib_options"
then
    case $multilib_options in
    *--disable-multilib* | *--enable-multilib* | *--with-multilib-list=)
        true
        ;;
    *)
        multilib_options="--enable-multilib $multilib_options"
        ;;
    esac
fi

cat << EOF >> /tools/etc/qirc

# Multilib options
multilib_options="$multilib_options"

# lib<equal> suffix (multilib, if any)
libSuffix="$libSuffix"

# GCC (arch-specific) options
gcc_options="$gcc_options"
EOF

chmod 644 /tools/etc/qirc


code:
# Build script for busybox.
#
# Copyright (c) 2014-2022 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

version=1.35.0

cd -- "$TMPDIR"
rm -rf busybox-${version}
unpack "${worktree}/sources/busybox-${version}.tar.bz2"

# Build instructions
cd busybox-${version}

# Import and export toolchain variables
. "${worktree}/stages/env.d/cross-staticenv"

make -j1 mrproper
cat "${worktree}/archive/busybox/busybox-config_stage1" > .config

make -j${jobs} busybox V=1 \
  ARCH="$kernel_arch" \
  CROSS_COMPILE="${target}-"

make -j${jobs} install CONFIG_PREFIX=/tools \
  ARCH="$kernel_arch" \
  CROSS_COMPILE="${target}-"

# Unset some imported variables from file
unset AR AS LD RANLIB READELF STRIP

cleanup()
{
    cd -- "$TMPDIR" && rm -rf busybox-${version}
}


code:
# Build script for essential symlinks.
#
# Copyright (c) 2014-2019 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Essential symbolic links for rootdir

# Binaries
for file in awk bash cat dd echo env file grep ln ls pwd rm sed sh stty
do
    ln -sf "/tools/bin/${file}" "${rootdir}/bin"
done

ln -sf gcc                 /tools/bin/cc
ln -sf /tools/bin/perl     "${rootdir}/usr/bin"
ln -sf /tools/bin/install  "${rootdir}/usr/bin"
ln -sf /proc/self/mounts   "${rootdir}/etc/mtab"

# Libraries
ln -sf /tools/lib${libSuffix}/libgcc_s.so         "${rootdir}/usr/lib${libSuffix}"
ln -sf /tools/lib${libSuffix}/libgcc_s.so.1       "${rootdir}/usr/lib${libSuffix}"

ln -sf /tools/lib${libSuffix}/libstdc++.so        "${rootdir}/usr/lib${libSuffix}"
ln -sf /tools/lib${libSuffix}/libstdc++.so.6      "${rootdir}/usr/lib${libSuffix}"
ln -sf /tools/lib${libSuffix}/libstdc++.a         "${rootdir}/usr/lib${libSuffix}"

ln -sf /tools/lib${libSuffix}/libssp.so           "${rootdir}/usr/lib${libSuffix}"
ln -sf /tools/lib${libSuffix}/libssp.so.0         "${rootdir}/usr/lib${libSuffix}"
ln -sf /tools/lib${libSuffix}/libssp.so.0.0.0     "${rootdir}/usr/lib${libSuffix}"
ln -sf /tools/lib${libSuffix}/libssp.a            "${rootdir}/usr/lib${libSuffix}"
ln -sf /tools/lib${libSuffix}/libssp_nonshared.a  "${rootdir}/usr/lib${libSuffix}"

# Strip temporary prefix
for file in libstdc++.la libssp.la libssp_nonshared.la
do
    sed -e 's/tools/usr/' /tools/lib${libSuffix}/$file \
     > "${rootdir}/usr/lib${libSuffix}/$file"
done
unset file


code:
# Build script for adjust the toolchain.
#
# Copyright (C) 2014-2017, 2020-2021 Matias Fonzo, <selk@dragora.org>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# This will be handled by libs/musl (post-install script):

cat << EOF > /tools/bin/adjust-toolchain
#! /bin/sh -

if test -r /tools/bin/ld-new
then
    set -e
    echo "\${0}: Adjusting toolchain ..."
    set -x

    CC=gcc
    TRIPLET="\$(\$CC -dumpmachine)"
    libSuffix=$libSuffix

    mv /tools/bin/ld /tools/bin/ld-old
    mv /tools/\${TRIPLET}/bin/ld /tools/\${TRIPLET}/bin/ld-old
    mv /tools/bin/ld-new /tools/bin/ld
    ln -sf /tools/bin/ld /tools/\${TRIPLET}/bin/ld

    \$CC -dumpspecs | sed \\
     -e 's@/tools@@g' \\
     -e "/\*startfile_prefix_spec:/{n;s@.*@/usr/lib\${libSuffix}/ @}" \\
     -e '/\*cpp:/{n;s@\$@ -isystem /usr/include@}' \\
      > "\$(dirname -- "\$(\$CC --print-libgcc-file-name)")"/specs
fi

EOF

chmod 700 /tools/bin/adjust-toolchain


code:
# Build script to insert the release script.
#
# Copyright (c) 2021 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

cp -f -p "${worktree}/archive/dragora-tools/build-for-release" \
 "${rootdir}/usr/bin"

chmod 700 "${rootdir}/usr/bin/build-for-release"


code:
# Build script for busybox (liveCD).
#
# Copyright (c) 2017-2022 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

version=1.35.0

cd -- "$TMPDIR"
rm -rf busybox-${version}
unpack "${worktree}/sources/busybox-${version}.tar.bz2"

# Build instructions
cd busybox-${version}

# Import and export toolchain variables
. "${worktree}/stages/env.d/cross-staticenv"

make mrproper
cat "${worktree}/archive/busybox/busybox-config_stage2" > .config

make -j${jobs} busybox V=1 \
  ARCH="$kernel_arch" \
  CROSS_COMPILE="${target}-"

make -j${jobs} install CONFIG_PREFIX="${rootdir}/initrd" \
  ARCH="$kernel_arch" \
  CROSS_COMPILE="${target}-"

# Unset some imported variables from file
unset AR AS LD RANLIB READELF STRIP

cleanup()
{
    cd -- "$TMPDIR" && rm -rf busybox-${version}
}


code:
# Build script for squashfs4.3+.
#
# Copyright (c) 2018-2020 Matias Fonzo, <selk@dragora.org>.
# Copyright (c) 2022 DustDFG, <dfgdust@gmail.com>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

version=4.5

cd -- "$TMPDIR"
rm -rf squashfs-tools-${version} \
       "${rootdir}/squashfs-tools_lzip"
unpack "${worktree}/sources/squashfs${version}.tar.gz"

# Build instructions
cd squashfs-tools-${version}

# Note:
# The build environment for cross-compilation is not imported,
# the squashfs-tools installation must work from the host system,
# this will produce the compressed squash-image

# Apply a patch to include LZIP support
patch -p1 < "${worktree}/patches/squashfs-tools/squashfs-tools-4.5_lzip-0.diff"

cd squashfs-tools

make -j${jobs} CC=cc LDFLAGS="" \
 GZIP_SUPPORT=0 \
 LZIP_SUPPORT=1 \
 XZ_SUPPORT=0 \
 LZO_SUPPORT=0 \
 LZ4_SUPPORT=0 \
 ZSTD_SUPPORT=0 \
 COMP_DEFAULT=lzip \
 XATTR_SUPPORT=1 \
 install INSTALL_DIR="${rootdir}/squashfs-tools_lzip"

cleanup()
{
    cd -- "$TMPDIR" && rm -rf squashfs-tools-${version}
}


code:
# Build script for the single-purpose initramfs (Live CD).
#
# Copyright (c) 2017-2020, 2022 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

mkdir -p -- "${rootdir}/initrd"
cd -- "${rootdir}/initrd"

# Create essential directories for the image

mkdir -p proc sys dev UP
chmod 755 proc sys dev UP

cat << "EOF" > init
#! /bin/busybox hush

set -x

/bin/busybox mount -n -o nosuid,noexec,nodev -t proc proc /proc
/bin/busybox mount -n -o nosuid,noexec,nodev -t sysfs sysfs /sys
/bin/busybox mount -n -t devtmpfs devtmpfs /dev
/bin/busybox sleep 5

# Mount tmpfs plus create directories
/bin/busybox mount -n -w -o size=100% -t tmpfs tmpfs /UP
/bin/busybox mkdir -p \
 /UP/cdrom /UP/squash /UP/rw /UP/workdir /UP/union

# Mount ISO and system image
/bin/busybox mount -n -r -t iso9660 LABEL="Dragora_@ARCH@" /UP/cdrom

# Be the system or be dead!
/bin/busybox mount -n -r -o loop -t squashfs \
 /UP/cdrom/dragora.sfs /UP/squash || /bin/busybox sleep 3323;

# Merge Squashfs with OverlayFS
/bin/busybox mount -n \
 -o lowerdir=/UP/squash,upperdir=/UP/rw,workdir=/UP/workdir \
 -t overlay overlay /UP/union

# Move virtual file systems prior to switch_root
/bin/busybox mount -n --move /dev   /UP/union/dev
/bin/busybox mount -n --move /proc  /UP/union/proc
/bin/busybox mount -n --move /sys   /UP/union/sys

: > /UP/union/fastboot
exec /bin/busybox switch_root /UP/union /sbin/init

EOF

sed -i "s/@ARCH@/${package_arch}/" init
chmod 750 init

# Make initramfs

rm -f ./initrd.img
find . | sed -e 's!^./!!' | cpio -oC512 -H newc | \
 lzip -s16 -0c > initrd.img


code:
# Build script to compose the Live CD image.
#
# Copyright (c) 2017 MMPG.
# Copyright (c) 2017-2022 Matias Fonzo <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# ISO file name
ISONAME="${ISONAME:-dragora-3.0_$(date +%Y%m%d)-${package_arch}}"

# Perform sanity checks (requirements)

if ! type xorriso > /dev/null
then
    echo "    xorriso(1) was not found in your PATH." 1>&2
    echo "GNU Xorriso is required to produce the ISO image(s)." 1>&2
    exit 2;
fi

if ! test -e "${rootdir}/squashfs-tools_lzip/mksquashfs"
then
    echo "Local \`${rootdir}/squashfs-tools_lzip/mksquashfs' does not exist." 1>&2
    exit 4;
fi

# This depends on the existence of the stage 1

if test ! -d "${output}/stage1/usr/pkg"
then
    echo "The stage number 1 does not exist or is incomplete." 1>&2
    echo "Build (completely) the stage 1 before processing '$stage'." 1>&2
    exit 1;
fi
if test ! -f "${rootdir}/initrd/initrd.img"
then
    echo "${rootdir}/initrd/initrd.img: No such initramfs file" 1>&2
    exit 4;
fi

STAGE1="${output}/stage1"
CDROM="${rootdir}/cdrom"

# Re-create CD tree

echo "Re-creating CD tree (${CDROM}) ..."
echo ""
rm -rf -- "$CDROM"
mkdir -p -- "${CDROM}/isolinux"

#
# Copy kernel installation to isolinux/

cd -- "${STAGE1}/boot"

for file in System.map-* config-* vmlinuz-*
do
    cp -p "${STAGE1}/boot/$file" "${CDROM}/isolinux/${file%%-*}"
done
unset file

# Copy local isolinux files and initramfs for ISOLINUX

cp -p "${worktree}"/archive/isolinux/* "${CDROM}/isolinux/"
cp -p "${rootdir}/initrd/initrd.img" "${CDROM}/isolinux/"

# Adjust things on the live side.
#
# Set auto-login:
sed \
 -e 's|c1:123:respawn:/sbin/agetty --noclear tty1 38400 linux|c1:123:respawn:/sbin/agetty --noclear --autologin root --noissue tty1 38400 linux|' \
 -e 's|c2:123:respawn:/sbin/agetty tty2 38400 linux|c2:123:respawn:/sbin/agetty --autologin root --login-pause --noissue tty2 38400 linux|' \
 -e 's|c3:12345:respawn:/sbin/agetty tty3 38400 linux|c3:12345:respawn:/sbin/agetty --autologin root --login-pause --noissue tty3 38400 linux|' \
 -i "${STAGE1}/etc/inittab"

# Add instructions for running the installer from boot menu option

cat << 'EOF' > "${STAGE1}/root/.profile"
#
# .profile - Personal shell-initialization file.
#
# The instructions here could serve to complement
# /etc/profile or to override default (previous)
# values.
#

# Add personal bin directory to the end of path
# and make it known to child process

PATH=${PATH}:${HOME}/bin
export PATH

# Run Dragora setup if the "parameter" in the boot was set

#
# Execute setup only on tty1/serial console

case "$(fgconsole)" in
1* | serial* )
    if grep -q -m 1 DRAGORA_SETUP /proc/cmdline
    then
        /sbin/dragora-keymap
        /sbin/dragora-mouse
        /sbin/dragora-installer
    fi
    ;;
esac

EOF

# Produce compressed images using mksquashfs(1)

cd -- "$STAGE1"
rm -f ./dragora.sfs

EXCLUDED_PATHS_COMMON="
 ./tools \
 ./root/.ash_history \
 ./*-log* \
 ./usr/src/qi \
 ./usr/bin/build-for-release
"
EXCLUDED_PATHS_CD="
 $EXCLUDED_PATHS_COMMON \
 ./var/cache/qi/packages \
 ./usr/pkg/kernel-buildtree-* \
 ./usr/src/linux-*
"

# For a DVD live-image with binary packages and kernel sources
echo "Creating DVD ISO image ..."
echo ""

echo "Binding ${worktree}/packages with ${rootdir}/var/cache/qi/packages ..."
mount -o bind "${worktree}/packages" ./var/cache/qi/packages
echo ""

"${rootdir}"/squashfs-tools_lzip/mksquashfs ./* "${CDROM}/dragora.sfs" \
 -comp lzip -b 1024K -Xcompression-level 1 -Xdict-size 100% \
 -noappend -e $EXCLUDED_PATHS_COMMON

echo ""
echo "Unmounting bind-mount ${worktree}/packages ..."
umount ./var/cache/qi/packages

# Produce hybrid ISO-9660 image (DVD)
cd -- "$CDROM"

rm -f ./${ISONAME}-dvd*
xorriso -as mkisofs -verbose \
         -o ${ISONAME}-dvd.iso                                    \
         -J -rational-rock -hide-rr-moved -iso-level 3            \
         -c "isolinux/boot.cat"                                   \
         -b "isolinux/isolinux.bin"                               \
            -partition_offset 16                                  \
            -no-emul-boot -boot-load-size 4 -boot-info-table      \
         -isohybrid-mbr "isolinux/isohdpfx.bin"                   \
         -publisher "Dragora GNU/Linux-Libre, https://www.dragora.org" \
         -volid "Dragora_${package_arch}"                         \
         .

echo "Creating .sha256 sum ..."
echo "sha256sum ${ISONAME}-dvd.iso > ${ISONAME}-dvd.iso.sha256"
sha256sum ${ISONAME}-dvd.iso > ${ISONAME}-dvd.iso.sha256

# ^ End Of 'For a DVD live-image with binary packages and kernel sources'

echo "Finishing ..."
echo ""
echo "***"
echo "The DVD ISO image has been produced at"
echo "    ${CDROM}/${ISONAME}-dvd.iso"
echo "***"
echo ""

exit 0


code:
#
# Cross toolchain variables for the environment
#

BTCC="${target}-gcc -static --static"
BTCXX="${target}-g++ -static --static"
AR="${target}-ar"
AS="${target}-as"
LD="${target}-ld"
RANLIB="${target}-ranlib"
READELF="${target}-readelf"
STRIP="${target}-strip"


code:
#
# aarch64: The 64-bit ARM architecture.
#

kernel_arch=arm64
libc_arch=aarch64

multilib_options="--disable-multilib --with-multilib-list="
gcc_options="--enable-fix-cortex-a53-835769 --enable-fix-cortex-a53-843419"

target=aarch64-${VENDOR}linux-musl

# Define architecture for the package manager
package_arch=arm64


code:
#
# armv7a_fp: ARM softfloat v7.
#

kernel_arch=arm
libc_arch=arm

multilib_options="--disable-multilib --with-multilib-list="
gcc_options="--with-arch=armv7-a --with-float=softfp"

target=arm-${VENDOR}linux-musleabi

# Define architecture for the package manager
package_arch=armfp


code:
#
# armv7a_hf: ARM hardfloat v7 (FPU vfpv3-d16).
#

kernel_arch=arm
libc_arch=arm

multilib_options="--disable-multilib --with-multilib-list="
gcc_options="--with-arch=armv7-a --with-float=hard --with-fpu=vfpv3-d16"

target=arm-${VENDOR}linux-musleabihf

# Define architecture for the package manager
package_arch=armhf


code:
#
# armv7hl: ARM hardfloat v7 (FPU neon).
#

kernel_arch=arm
libc_arch=arm

multilib_options="--disable-multilib --with-multilib-list="
gcc_options="--with-arch=armv7-a --with-float=hard --with-fpu=neon"

target=armv7hl-${VENDOR}linux-musleabihf

# Define architecture for the package manager
package_arch=armhl


code:
#
# i486: Intel 486 optimized for Intel 686+ processors.
#

kernel_arch=x86
libc_arch=i386

libSuffix=32

multilib_options="--with-multilib-list=m32"
gcc_options="--with-arch=i486 --with-tune=i686 --enable-cld"

target=i486-${VENDOR}linux-musl

# Define architecture for the package manager
package_arch=i486


code:
#
# i586: Intel Pentium MMX optimized for Pentium Pro+ processors.
#

kernel_arch=x86
libc_arch=i386

libSuffix=32

multilib_options="--with-multilib-list=m32"
gcc_options="--with-arch=pentium-mmx --with-tune=pentiumpro --enable-cld"

target=i586-${VENDOR}linux-musl

# Define architecture for the package manager
package_arch=i586


code:
#
# microblaze: Soft microprocessor core designed for Xilinx FPGAs (32-bit).
#

kernel_arch=microblaze
libc_arch=microblaze

multilib_options="--disable-multilib --with-multilib-list="
gcc_options=""

target=microblaze-${VENDOR}linux-musl

path: targets/microblaze


code:
#
# mips: MIPS r4k big endian.
#

kernel_arch=mips
libc_arch=mips

multilib_options="--disable-multilib --with-multilib-list="
gcc_options="--with-linker-hash-style=sysv"

target=mips-${VENDOR}linux-musl

path: targets/mips


code:
#
# powerpc: PowerPC32.
#

kernel_arch=powerpc
libc_arch=powerpc

multilib_options="--disable-multilib --with-multilib-list="
gcc_options="--enable-decimal-float=no --enable-secureplt"

target=powerpc-${VENDOR}linux-musl

path: targets/powerpc


code:
#
# powerpc64le: PowerPC64 (for Talos II systems).
#

kernel_arch=powerpc
libc_arch=powerpc64

multilib_options="--disable-multilib --with-multilib-list="
gcc_options="--enable-decimal-float=no --with-abi=elfv2"

target=powerpc64le-${VENDOR}linux-musl

# Define architecture for the package manager
package_arch=ppc64le


code:
#
# riscv64: "risk-five" (64-bit long and pointers, hard-float).
#

kernel_arch=riscv
libc_arch=riscv64

libSuffix=64

multilib_options="--enable-multilib --with-multilib-list=lp64"
gcc_options="--with-arch=rv64gc --with-abi=lp64d"

target=riscv64-${VENDOR}linux-musl


code:
#
# s390x: z/Arch 64-bit.
#

kernel_arch=s390
libc_arch=s390x

multilib_options="--disable-multilib --with-multilib-list="
gcc_options="--with-long-double-128"

target=s390x-${VENDOR}linux-musl


code:
#
# x32: x86-64 ABI using 32-bit integers, longs and pointers.
#

kernel_arch=x86_64
libc_arch=x32

libSuffix=x32

multilib_options="--with-multilib-list=mx32"
gcc_options="--with-abi=x32"

target=x86_64-x32-${VENDOR}linux-musl

# Define architecture for the package manager
package_arch=x32


code:
#
# x86_64: x86-64 (generic).
#

kernel_arch=x86_64
libc_arch=x86_64

libSuffix=64

multilib_options="--with-multilib-list=m64"
gcc_options="--with-arch=x86-64 --with-tune=generic"

target=x86_64-${VENDOR}linux-musl

# Define architecture for the package manager
package_arch=amd64


二次截取: <div class="module-items">&&mxonefoot

图片: data-src="&&"

标题: title="&&"

分类: 电影&电视剧&综艺&动漫

搜索二次截取: class="module-items&&mxonefoot

搜索图片: data-src="&&"

类型: 类型：&&</div>

导演: 导演：&&

主演: 主演：&&

简介: 剧情：&&

线路数组: data-dropdown-value=&&</div>

播放数组: <div class="sort-item&&

剧情

key: class

n: 全部

n: 喜剧

v: 喜剧

n: 爱情

v: 爱情

n: 恐怖

v: 恐怖

n: 动作

v: 动作

n: 科幻

v: 科幻

n: 剧情

v: 剧情

n: 战争

v: 战争

n: 警匪

v: 警匪

n: 犯罪

v: 犯罪

n: 动画

v: 动画

n: 奇幻

v: 奇幻

n: 武侠

v: 武侠

n: 冒险

v: 冒险

n: 枪战

v: 枪战

n: 悬疑

v: 悬疑

n: 惊悚

v: 惊悚

n: 经典

v: 经典

n: 青春

v: 青春

n: 文艺

v: 文艺

n: 微电影

v: 微电影

n: 古装

v: 古装

n: 历史

v: 历史

n: 运动

v: 运动

n: 农村

v: 农村

n: 儿童

v: 儿童

n: 网络电影

v: 网络电影

地区

key: area

n: 大陆

v: 大陆

n: 香港

v: 香港

n: 台湾

v: 台湾

n: 美国

v: 美国

n: 法国

v: 法国

n: 英国

v: 英国

n: 日本

v: 日本

n: 韩国

v: 韩国

n: 德国

v: 德国

n: 泰国

v: 泰国

n: 印度

v: 印度

n: 意大利

v: 意大利

n: 西班牙

v: 西班牙

n: 加拿大

v: 加拿大

n: 其他

v: 其他

年份

key: year


code:
{
    "规则名": "4KHDR",
    "规则作者": "",
    "请求头参数": "手机",
    "网页编码格式": "UTF-8",
    "图片是否需要代理": "1",
    "是否开启获取首页数据": "1",
    "首页推荐链接": "https://www.4khdr.cn/forum.php?mod=forumdisplay&fid=2&&page=1",
    "首页列表数组规则": "body&&#waterfall",
    "首页片单列表数组规则": "li",
    "首页片单是否Jsoup写法": "1",
    "分类起始页码": "1",
    "分类链接": "https://www.4khdr.cn/forum.php?mod=forumdisplay&fid=2&&filter=typeid&typeid={cateId}&page={catePg}",
    "分类名称": "4K电影&4K美剧&4K华语&4K动画&4K纪录片&4K日韩印&蓝光电影&蓝光美剧&蓝光华语&蓝光动画&蓝光日韩印",
    "分类名称替换词": "3&8&15&6&11&4&29&31&33&32&34",
    "筛选数据": {},
    //"筛选数据": "ext",
    //{cateId}
    "筛选子分类名称": "",
    "筛选子分类替换词": "",
    //{class}
    "筛选类型名称": "",
    "筛选类型替换词": "*",
    //{area}
    "筛选地区名称": "",
    "筛选地区替换词": "*",
    //{year}
    "筛选年份名称": "",
    "筛选年份替换词": "*",
    //{lang}
    "筛选语言名称": "",
    "筛选语言替换词": "*",
    //{by}
    "筛选排序名称": "时间&人气&评分",
    "筛选排序替换词": "time&hits&score",
    "分类截取模式": "1",
    "分类列表数组规则": "#waterfall&&li",
    "分类片单是否Jsoup写法": "1",
    "分类片单标题": "h3&&a&&Text",
    "分类片单链接": "h3&&a&&href",
    "分类片单图片": "img&&src",
    "分类片单副标题": "",
    "分类片单链接加前缀": "https://www.4khdr.cn/",
    "分类片单链接加后缀": "",
    "搜索请求头参数": "User-Agent$手机#Referer$https://cn.bing.com/",
    "搜索链接": "https://cn.bing.com/search?q={wd}+site:https://www.4khdr.cn&qs=ds&form=QBRE",
    "POST请求数据": "",
    "搜索截取模式": "1",
    "搜索列表数组规则": "ol#b_results&&li.b_algo",
    "搜索片单是否Jsoup写法": "1",
    "搜索片单图片": "",
    "搜索片单标题": "h2&&Text",
    "搜索片单链接": "a&&href",
    "搜索片单副标题": "",
    "搜索片单链接加前缀": "",
    "搜索片单链接加后缀": "",
    "链接是否直接播放": "0",
    "直接播放链接加前缀": "",
    "直接播放链接加后缀": "",
    "直接播放直链视频请求头": "",
    "详情是否Jsoup写法": "0",
    "类型详情": "",
    "年代详情": "",
    "地区详情": "",
    "演员详情": "",
    "简介详情": "",
    "线路列表数组规则": "",
    "线路标题": "",
    "播放列表数组规则": "body&&#postlist",
    "选集列表数组规则": "a[href^=https://www.aliyundrive.com/s/]||a[href^=magnet]",
    "选集标题链接是否Jsoup写法": "1",
    "选集标题": "a&&Text",
    "选集链接": "a&&href",
    "是否反转选集序列": "0",
    "选集链接加前缀": "",
    "选集链接加后缀": "",
    "分析MacPlayer": "0",
    "是否开启手动嗅探": "0",
    "手动嗅探视频链接关键词": ".mp4#.m3u8#.flv#video/tos",
    "手动嗅探视频链接过滤词": ".html#=http"
}

license: unknown


分类: 电影&电视剧&综艺&动漫

剧情

n: 全部

n: 喜剧

v: 喜剧

n: 爱情

v: 爱情

n: 恐怖

v: 恐怖

n: 动作

v: 动作

n: 科幻

v: 科幻

n: 剧情

v: 剧情

n: 战争

v: 战争

n: 警匪

v: 警匪

n: 犯罪

v: 犯罪

n: 动画

v: 动画

n: 奇幻

v: 奇幻

n: 武侠

v: 武侠

n: 冒险

v: 冒险

n: 枪战

v: 枪战

n: 悬疑

v: 悬疑

n: 惊悚

v: 惊悚

n: 经典

v: 经典

n: 青春

v: 青春

n: 文艺

v: 文艺

n: 微电影

v: 微电影

n: 古装

v: 古装

n: 历史

v: 历史

n: 运动

v: 运动

n: 农村

v: 农村

n: 儿童

v: 儿童


code:
// 一级筛选 数字验证
muban.mxone5.二级.title = 'h1&&Text;.video-info-aux&&Text';
muban.mxone5.二级.desc = '.video-info-items:eq(5)&&Text;;;.video-info-actor:eq(1)&&Text;.video-info-actor:eq(0)&&Text';
var rule={
    title:'8K影视',
    模板:'mxone5',
    host:'http://www.8kvod.com',
    url:'/t/fyclass-fypage/',
    class_name:'足球&篮球&网球&动作片&喜剧片&爱情片&科幻片&恐怖片&剧情片&战争片&惊悚片&犯罪片&冒险片&悬疑片&奇幻片&纪录片&动画片&其他片&国产剧&港台剧&日韩剧&欧美剧&海外剧&大陆综艺&港台综艺&国外综艺&歌曲MV&国产动漫&日本动漫&欧美动漫&其他动漫&早教&亲子&儿歌',
    class_url:'53&54&55&6&7&8&9&10&11&12&48&49&50&51&52&28&29&31&13&14&15&16&20&21&22&23&39&24&25&26&27&44&45&46',
    searchUrl:'/vse**/page/fypage/',
}


[User] 空

houzhui: .html

jiequqian: 空

jiequhou: 空

tupianqian: data-original="

biaotiqian: title="

lianjieqian: href="

jspic: pic

jsname: name

jsid: id

ssjiequqian: 空

ssjiequhou: 空

ssjiequshuzuqian: 空

ssjiequshuzuhou: 空

sstupianqian: 空

sstupianhou: 空

ssbiaotiqian: 空

ssbiaotihou: 空

sslianjieqian: 空

sslianjiehou: 空

bfjiequqian: 空

bfjiequhou: 空

bfjiequshuzuqian: class="stui-content__playlist clearfix

zhuangtaiqian: class="pic-text text-right">

zhuangtaihou: </span>

daoyanqian: 导演：

zhuyanqian: 主演：

juqingqian: stui-content__desc col-pd clearfix">

juqinghou: </div>

bfyjiequqian: 空

bfyjiequhou: 空

bfyjiequshuzuqian: <li

bflianjieqian: href="


jiequshuzuhou: </div>

sousuohouzhui: /voddetail/

zhuangtaiqian: 更新：

juqingqian: 简介：</span>

bfyjiequshuzuqian: <a


code:
//写法思路来自biubiu影院，海阔视界，xpath筛选。
//项目魔改至github开源仓库https://github.com/Tangsan99999/TvJar中的XBiubiu.java文件，

{
    //规则名
    "title": "无插件直播",
    //作者
    "author": "聆听优美旋律",
    //请求头UA,不填则默认okhttp/3.12.11，可填MOBILE_UA或PC_UA使用内置的手机版或电脑版UA
    //习惯查看手机源码写建议用手机版UA，习惯查看PC版源码写建议用电脑版UA
    "UserAgent":"Mozilla/5.0 (Linux; Android 12; V2049A Build/SP1A.210812.003; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/103.0.5060.129 Mobile Safari/537.36",
    //请求头Referer参数，如果只是播放页需要，填WebView即可引用播放页的链接作嗅探Referer参数。
    //一般很少用，留空或删除
    "Referer":"",
    //是否开启获取首页数据，0关闭，1开启
    "homeContent":"0",
    //分类链接起始页码,禁止负数和含小数点。
    "firstpage": "1",
    //分类链接,{cateId}是分类，{catePg}是页码,第一页没有页码的可以这样写 第二页链接[firstPage=第一页的链接]
    "class_url": "http://www.17klive.com:8866/m.html{cateId}",
    //分类名，分类1&分类2&分类3
    "class_name": "无插件直播",
    //分类名替换词，替换词1&替换词2&替换词3，替换词包含英文&的用两个中文＆＆代替，示例：＆＆id=0&＆＆id=1
    "class_value": "",
    //筛选数据，json格式，参考xpath的筛选写法
    "filterdata":{},
    //分类是否二次截取
    "cat_YN_twice": "0",
    //分类二次截取前
    "cat_twice_pre": "<div class=\"list_content",
    //分类二次截取后
    "cat_twice_suf": "
&&<a",
    //分类截取片单副标题，截取前缀&&截取后缀
    "cat_subtitle": "",
    //分类截取片单链接，截取前缀&&截取后缀
    "cat_url": "href=\"http&&\"",
    //分类片单链接补前缀
    "cat_prefix": "http",
    //分类片单链接补后缀
    "cat_suffix": "",
    "force_play": "1",
    //是否开启手动嗅探，只对网页嗅探有效，0否，1是
    "ManualSniffer":"1",
    //手动嗅探视频链接关键字，每个用#隔开
    "VideoFormat":"",
    //手动嗅探视频链接过滤关键词,每个用#隔开
    "VideoFilter":".html#=http"
}


code:
{
  "name": "23影院",
  "url": "https://www.23wm.net",
  "User": "空",
  "shouye": "1",
  //"fenlei": "电影$/vodshow/1--------1---/page/#剧集$/vodshow/2--------1---/page/#综艺$/vodshow/3--------1---/page/#动漫$/vodshow/4--------1---/page/#纪录片$/vodshow/20--------1---/page/#国产剧$/vodshow/13--------1---/page/#港台剧$/vodshow/14--------1---/page/#日韩剧$/vodshow/15--------1---/page/#欧美剧$/vodshow/16--------1---/page/#哔哩哔哩$/vodshow/39--------1---/page/#喜剧片$/vodshow/7--------1---/page/#爱情片$/vodshow/8--------1---/page/#科幻片$/vodshow/9--------1---/page/#恐怖片$/vodshow/10--------1---/page/#剧情片$/vodshow/11--------1---/page/#",
  "fenlei": "电影$/vodshow/1--------#剧集$/vodshow/2--------#综艺$/vodshow/3--------#动漫$/vodshow/4--------#纪录片$/vodshow/20--------#国产剧$/vodshow/13--------#港台剧$/vodshow/14--------#韩剧$/vodshow/15-韩国-------#日剧$/vodshow/15-日本-------#欧美剧$/vodshow/16--------#泰国剧$/vodshow/25--------#哔哩哔哩$/vodshow/39--------#海外剧$/vodshow/51--------#国产动漫$/vodshow/4-国产-------#日本动漫$/vodshow/4-日本-------#欧美动漫$/vodshow/4-欧美-------#其他动漫$/vodshow/4-其他-------#喜剧片$/vodshow/7--------#爱情片$/vodshow/8--------#科幻片$/vodshow/9--------#恐怖片$/vodshow/10--------#剧情片$/vodshow/11--------#战争片$/vodshow/12--------#动作片$/vodshow/6--------#奇幻片$/vodshow/52--------#犯罪片$/vodshow/53--------",
  "houzhui": "---.html",
  "shifouercijiequ": "0",
  "jiequqian": "空",
  "jiequhou": "空",
  "jiequshuzuqian": "class=\"pack-ykpack\"",
  "jiequshuzuhou": "
",
  "tupianqian": "data-original=\"",
  "tupianhou": "\"",
  "biaotiqian": "title=\"",
  //"biaotihou": "\"",
  "biaotihou": "23影视网-",
  "lianjieqian": "href=\"",
  "lianjiehou": "\"",
  "sousuoqian": "/index.php/ajax/suggest?mid=1&wd=",
  "sousuohou": "&limit=500",
  "sousuohouzhui": "/voddetail/",
  "ssmoshi": "0",
  "sousuoshifouercijiequ": "0",
  "jspic": "pic",
  "jsname": "name",
  "jsid": "id",
  "ssjiequqian": "空",
  "ssjiequhou": "空",
  "ssjiequshuzuqian": "空",
  "ssjiequshuzuhou": "空",
  "sstupianqian": "空",
  "sstupianhou": "空",
  "ssbiaotiqian": "空",
  "ssbiaotihou": "空",
  "sslianjieqian": "空",
  "sslianjiehou": "空",
  "bfshifouercijiequ": "0",
  "bfjiequqian": "空",
  "bfjiequhou": "空",
  "bfjiequshuzuqian": "class=\"content_playlist",
  "bfjiequshuzuhou": "
",
  "zhuangtaiqian": "类型：",
  "zhuangtaihou": "
",
  "daoyanqian": "导演：",
  "daoyanhou": "
",
  "zhuyanqian": "演员：",
  "zhuyanhou": "
",
  "juqingqian": "简介：",
  "juqinghou": "
",
  "bfyshifouercijiequ": "0",
  "bfyjiequqian": "空",
  "bfyjiequhou": "空",
  "bfyjiequshuzuqian": "",
  "bfbiaotiqian": ">",
  "bfbiaotihou": "<",
  "bflianjieqian": "href=\"",
  "bflianjiehou": "\""
}


31看影视

author: dj

jiequqian: <ul class="myui-vodlist

jiequshuzuhou: </span>

tupianqian: original="

bfjiequshuzuqian: id="playlist

zhuangtaiqian: 更新：</span>

daoyanqian: 导演：</span>

zhuyanqian: 主演：</span>


348电影网

jiequshuzuqian: class="pack-ykpack

jspic: 空

jsname: 空

jsid: 空

ssjiequshuzuqian: class="thumb

sstupianqian: data-original="

ssbiaotiqian: title="

sslianjieqian: href="

bfjiequshuzuqian: <ul class="content_playlist

zhuangtaiqian: 状态：</span>

juqingqian: 详情：</span>