""" SCons build recipe for the GPSD project

Important targets:

build      - build the software (default)
dist       - make distribution tarballs .gz and .xz, plus .zip
zip        - make distribution zip
install    - install programs, libraries, and manual pages
uninstall  - undo an install

check      - run regression and unit tests.
audit      - run code-auditing tools
testbuild  - test-build the code from a tarball
website    - refresh the website
release    - ship a release

--clean    - clean all normal build targets
-c         - clean all normal build targets

Useful configuration options:
debug=yes     -- Compile and link with debugging options. Turns off
                 optimizations,
debug_opt=yes -- Compile and link with debugging options. Leaves on
                 optimizations,

Useful scons options:
--warn=all -- to see scons warnings

Setting the DESTDIR environment variable will prefix the install destinations
without changing the --prefix prefix.

Pretty much all this file does is create the variant_dir, and call SConscript.
"""

# Unfinished items:
# * Coveraging mode: gcc "-coverage" flag requires a hack
#   for building the python bindings

# This file is Copyright 2010 by the GPSD project
# SPDX-License-Identifier: BSD-2-clause
#
# This code runs compatibly under Python 2 and 3.x for x >= 2.
# Preserve this property!
from __future__ import print_function

import atexit      # for atexit.register()
import os
import SCons

def SConsVersionTuple():
    """Return (major, minor, micro) from SCons.__version__.

    A release is a string like "4.11.0".  Very old SCons used a tuple.
    A development build may append trailing junk ("4.11.0.abc").
    """
    version = SCons.__version__
    if isinstance(version, tuple):
        return tuple(version[:3])
    parts = []
    for part in version.split('.')[:3]:
        digits = ''
        for char in part:
            if char.isdigit():
                digits += char
            else:
                break
        parts.append(int(digits or '0'))
    while len(parts) < 3:
        parts.append(0)
    return tuple(parts)

# gpsd needs Scons version at least 2.3
EnsureSConsVersion(2, 3, 0)
# SCons 4.11.0 crashes while reading a missing .scons-option-cache.
# 4.10.1 and 4.11.1 work.  See #412.
if SConsVersionTuple() == (4, 11, 0):
    print('SCons 4.11.0 cannot build gpsd. SCons 4.10.1 and 4.11.1 can.')
    Exit(1)
# gpsd needs Python version at least 2.6
EnsurePythonVersion(2, 6)

# package version
gpsd_version = "3.28"
# name 'build' is already taken, put stuff in gpsd-$VERSION
# it makes tar simple
variantdir = 'gpsd-' + gpsd_version

# one touch clean!
# also, for brutal effect, you can do: git clean -dfx
if GetOption('clean'):
    # .pages and .public are GitLab "pages".
    clean_targets = '%s buildtmp .public .pages testbuild' % variantdir
    atexit.register(lambda: os.system('rm -rf ' + clean_targets))

# Not everything respects this  chdir()
SConscriptChdir(1)
SConscript('SConscript',
           duplicate=1,
           exports=['gpsd_version', 'variantdir'],
           must_exit=True,
           variant_dir=variantdir,
           )
# VariantDir('buildtmp', '.')
# SConscript('buildtmp/SConscript', must_exit=True, duplicate=1)
