# Build recipe for ninja.
#
# Copyright (c) 2018-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=ninja
version=1.11.1
release=1

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

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

# Remote source(s)
fetch=https://github.com/ninja-build/ninja/archive/v${version}/$tarname

description="
A small build system with a focus on speed.

Ninja is a small build system with a focus on speed.  It differs from
other build systems in two major respects: it is designed to have its
input files generated by a higher-level build system, and it is
designed to run builds as fast as possible.
"

homepage=https://ninja-build.org
license="Apache License v2.0"

# Source documentation
docs="COPYING doc/manual.asciidoc"
docsdir="${docdir}/${program}-${version}"

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

    cd "$srcdir"

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

    python3 configure.py --bootstrap

    # Run tests
    #./ninja ninja_test
    #./ninja_test --gtest_filter=-SubprocessTest.SetWithLots

    mkdir -p "${destdir}/usr/bin"
    cp -p ninja "${destdir}/usr/bin/"
    chmod 755 "${destdir}/usr/bin/ninja"

    strip --strip-unneeded "${destdir}/usr/bin/ninja"

    # Add completions

    mkdir -p "${destdir}/usr/share/bash-completion/completions/ninja" \
             "${destdir}/usr/share/zsh/site-functions/_ninja"

    cp -p misc/bash-completion \
     "${destdir}/usr/share/bash-completion/completions/ninja"

    cp -p misc/zsh-completion \
     "${destdir}/usr/share/zsh/site-functions/_ninja"

    pylib=$( python3 -c 'import sysconfig; print(sysconfig.get_path("platlib"))' )
    mkdir -p "${destdir}/${pylib}"
    cp -p misc/ninja_syntax.py "${destdir}/${pylib}"

    chmod 644 \
     "${destdir}/usr/share/bash-completion/completions/ninja/bash-completion" \
     "${destdir}/usr/share/zsh/site-functions/_ninja/zsh-completion" \
     "${destdir}/${pylib}/ninja_syntax.py"

    unset -v pylib

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

