#!/hint/bash
# shellcheck disable=2034

# Configuration
## Packager
## Leave both empty to disable, leave $_packager_email empty to disable
_packager="JeremyStarTM"
_packager_email="jeremystartm@staropensource.de"

## GPG Key
## Leave empty to disable
_gpgkey=""

## March
## to get a list of all march values by executing "gcc --target-help" and search for -march=
_march="native"

## Mtune
## to get a list of all mtune values by executing "gcc --target-help" and search for -mtune=
_mtune="native"

## CPU flags
## Define your cpu flags here (get values by installing cpuid2cpuflags from the AUR and running "cpuid2cpuflags")
_cpuflags="aes avx avx2 f16c fma3 mmx mmxext pclmul popcnt rdrand sha sse sse2 sse3 sse4_1 sse4_2 sse4a ssse3 vpclmulqdq"

## Streaming SIMD
### SSE1 and SSE2 are default on amd64, use "cat /proc/cpuinfo" to get info for all others
### Different cpuflags names: sse3=pni mmx=mmxext
_sse1="true"
_sse2="true"
_sse3="true"
_sse4="true"
_sse4dot1="true"
_sse4dot2="true"
_sse4a="true"
_mmx="true"
_3dnow="false"

## Safe CFLAGS -> see https://wiki.gentoo.org/wiki/Safe_CFLAGS
_safecflags=""

## Compilation caches
_ccache="false"
_sccache="false"

## Replacements
_mold="true"


##########################################
## Modify things below for more control ##
##########################################


# Architecture and host triple
CARCH="x86_64"
CHOST="x86_64-pc-linux-gnu"


# Downloaders
DLAGENTS=('file::/usr/bin/curl -qgC - -o %o %u'
          'ftp::/usr/bin/curl -qgfC - --ftp-pasv --retry 3 --retry-delay 3 -o %o %u'
          'http::/usr/bin/curl -qgb "" -fLC - --retry 3 --retry-delay 3 -o %o %u'
          'https::/usr/bin/curl -qgb "" -fLC - --retry 3 --retry-delay 3 -o %o %u'
          'rsync::/usr/bin/rsync --no-motd -z %u %o'
          'scp::/usr/bin/scp -C %u %o')


# Source control
VCSCLIENTS=('bzr::breezy'
            'fossil::fossil'
            'git::git'
            'hg::mercurial'
            'svn::subversion')


# Tool flags
## GCC (C)
CPU_FLAGS_X86="${_cpuflags}"
CFLAGS="-march=${_march} -mtune=${_mtune} -O2 -ftree-vectorize -pipe -fomit-frame-pointer -fno-plt -fexceptions -Wp,-D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security -fstack-clash-protection -fcf-protection"

## Streaming SIMD
if [ "${_sse1}" == "true" ]; then
        export "CFLAGS=${CFLAGS} -msse"
fi
if [ "${_sse2}" == "true" ]; then
        export "CFLAGS=${CFLAGS} -msse2"
fi
if [ "${_sse3}" == "true" ]; then
        export "CFLAGS=${CFLAGS} -msse3"
fi
if [ "${_sse4}" == "true" ]; then
        export "CFLAGS=${CFLAGS} -msse4"
fi
if [ "${_sse4dot1}" == "true" ]; then
        export "CFLAGS=${CFLAGS} -msse4.1"
fi
if [ "${_sse4dot2}" == "true" ]; then
        export "CFLAGS=${CFLAGS} -msse4.2"
fi
if [ "${_sse4a}" == "true" ]; then
        export "CFLAGS=${CFLAGS} -msse4a"
fi
if [ "${_mmx}" == "true" ]; then
        export "CFLAGS=${CFLAGS} -mmmx"
fi
if [ "${_3dnow}" == "true" ]; then
        export "CFLAGS=${CFLAGS} -m3dnow"
fi
if [ -n "${_safecflags}" ]; then
        export "CFLAGS=${CFLAGS} ${_safecflags}"
fi

## GCC (C++)
CXXFLAGS="${CFLAGS} -Wp,-D_GLIBCXX_ASSERTIONS"
CPPFLAGS="${CXXFLAGS}"

## LD
LDFLAGS="-Wl,-O1,--sort-common,--as-needed,-z,relro,-z,now"
if [ "${_mold}" == "true" ]; then
        export "LDFLAGS=${LDFLAGS},-fuse-ld=mold"
fi
LTOFLAGS="-flto=auto"

## Rust
RUSTFLAGS="-C target-cpu=native"
if [ "${_mold}" == "true" ]; then
        export "RUSTFLAGS=${RUSTFLAGS} -C link-arg=-fuse-ld=mold"
fi

## Make
MAKEFLAGS="-j$(nproc)"

## Debug flags
### GCC (C)
DEBUG_CFLAGS="-g"

### GCC (C++)
DEBUG_CXXFLAGS="$DEBUG_CFLAGS"
DEBUG_CPPFLAGS="${DEBUG_CXXFLAGS}"

### Rust
DEBUG_RUSTFLAGS="-C debuginfo=2"


# Makepkg settings
## Build environment
## Default: BUILDENV=(!distcc !color !ccache check !sign)
## Executes:
##  -> distcc: Use the Distributed C/C++/ObjC compiler
##  -> color:  Colorize output messages
##  -> ccache: Use ccache to cache compilation
##  -> check:  Run the check() function if present in the PKGBUILD
##  -> sign:   Generate PGP signature file
BUILDENV="!distcc color"
if [ "${_ccache}" == "true" ]; then
	export "BUILDENV=${BUILDENV} ccache"
else
	export "BUILDENV=${BUILDENV} !ccache"
fi
export BUILDENV="${BUILDENV} check"
if [ -n "${_gpgkey}" ]; then
	export "BUILDENV=${BUILDENV} sign"
else
	export "BUILDENV=${BUILDENV} !sign"
fi
export BUILDENV=(${BUILDENV})

## Build directory
## Default: BUILDDIR=<unset>
BUILDDIR=/tmp/makepkg

## Global package options
## Default: OPTIONS=(!strip docs libtool staticlibs emptydirs !zipman !purge !debug !lto)
## Executes:
##  -> strip:      Strip symbols from binaries/libraries
##  -> docs:       Save doc directories specified by DOC_DIRS
##  -> libtool:    Leave libtool (.la) files in packages
##  -> staticlibs: Leave static library (.a) files in packages
##  -> emptydirs:  Leave empty directories in packages
##  -> zipman:     Compress manual (man and info) pages in MAN_DIRS with gzip
##  -> purge:      Remove files specified by PURGE_TARGETS
##  -> debug:      Add debugging flags as specified in DEBUG_* variables
##  -> lto:        Add compile flags for building with link time optimization
OPTIONS=(strip docs !libtool !staticlibs emptydirs zipman purge debug lto)

## File integrity checks to use
## Default: INTEGRITY_CHECK=(sha256)
## Valid: md5, sha1, sha224, sha256, sha384, sha512, b2
INTEGRITY_CHECK=(sha256)

## Options for stripping binaries (see "man strip")
## Default: STRIP_BINARIES="--strip-all"
STRIP_BINARIES="--strip-all"

## Options for stripping shared libraries (see "man strip")
## Default: STRIP_SHARED="--strip-unneeded"
STRIP_SHARED="--strip-unneeded"

## Options for stripping static librarie (see "man strip")
STRIP_STATIC="--strip-debug"

## Manual directories (man & info) to compress
## Default: MAN_DIRS=({usr{,/local}{,/share},opt/*}/{man,info})
MAN_DIRS=({usr{,/local}{,/share},opt/*}/{man,info})

## Documentation directories to remove
## Default: DOC_DIRS=(usr/{,local/}{,share/}{doc,gtk-doc} opt/*/{doc,gtk-doc})
DOC_DIRS=(usr/{,local/}{,share/}{doc,gtk-doc} opt/*/{doc,gtk-doc})

## Files to remove from all packages
## Default: PURGE_TARGETS=(usr/{,share}/info/dir .packlist *.pod)
PURGE_TARGETS=(usr/{,share}/info/dir .packlist *.pod)

## Directories to store source code in (debug packages)
## Default: DBGSRCDIR="/usr/src/debug"
DBGSRCDIR="/usr/src/debug"


# Package output
## Where all packages will be placed
## Default: PKGDEST=<unset>

## Where source files will be cached
## Default: SRCDEST=<unset>

## Where source packages will be placed
## Default: SRCPKGDEST=<unset>

## Where log files will be placed
## Default: LOGDEST=<unset>

## Packager
## Default: <unset>
if [ -n "${_packager}" ] && [ -n "${_packager_email}" ]; then
        export PACKAGER="${_packager} <${_packager_email}>"
elif [ -n "${_packager}" ] && [ -z "${_packager_email}" ]; then
        export PACKAGER="${_packager}"
else
        export PACKAGER=""
fi

## GPG Key
## Default: <unset>
if [ -n "${_gpgkey}" ]; then
        export GPGKEY="${_gpgkey}"
else
        export GPGKEY=""
fi

## Compression commands
COMPRESSGZ=(gzip -c -f -n)
COMPRESSBZ2=(bzip2 -c -f)
COMPRESSXZ=(xz -c -z -)
COMPRESSZST=(zstd -c -T0 --ultra -20 -)
COMPRESSLRZ=(lrzip -q)
COMPRESSLZO=(lzop -q)
COMPRESSZ=(compress -c -f)
COMPRESSLZ4=(lz4 -q)
COMPRESSLZ=(lzip -c -f)

## Extension defaults
PKGEXT='.pkg.tar.zst'
SRCEXT='.src.tar.gz'


# Other
## Default: PACMAN_AUTH=<unset>

# vim: set ft=sh ts=2 sw=2 et: