#!/usr/bin/python
# vim:ts=4:sts=4:sw=4:et:tw=120
# Explicitly using /usr/bin/python, not /usr/bin/env python here.

##########
# README #
##########
# This script wraps clang for GHC. GHC won't compile with clang's preprocessor unless the flags
#   -Wno-invalid-pp-token -Wno-unicode -Wno-trigraphs
# are passed (in addition to -E -undef -traditional, which are already passed in by GHC). Furthermore, if a compilation
# language is passed and it is C (i.e., -x c), it is replaced with -x assembler-with-cpp.
#
# There is a popular script fulfilling the same purpose on Github, but the script does not specify a license and can
# thus not be re-distributed and used in MacPorts. To avoid the same problem with this script further down the road:
#
###########
# LICENSE #
###########
#
# SPDX-License-Identifier: BSD-2-Clause
#
# Copyright (c) 2014, Clemens Lang <cal@macports.org>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
# following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
#    disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
#    disclaimer in the documentation and/or other materials provided with the distribution.
#
#    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
#    INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
#    DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#    SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
#    SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#    WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
#    USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import sys
import os

# To be reinplaced on installation, should hold the absolute path to clang
__clang_path = '@clang@'

# The set of flags to be added in preprocessor mode
__clang_cpp_flags = ['-Wno-invalid-pp-token', '-Wno-unicode', '-Wno-trigraphs']

def filterLanguageFlag(args):
    lastArgWasLanguage = False
    # Start at 1, because index 0 is the program basename, and we don't want to check that for -x
    for idx in range(1, len(args)):
        if lastArgWasLanguage and args[idx].lower() == 'c':
            args[idx] = 'assembler-with-cpp'
        lastArgWasLanguage = args[idx] == '-x'
    return args

# copy argv for modification
args = sys.argv[:]

# Don't check for -E, -undef, -traditional in sys.argv[0]
# But check for the presence of all -E, -undef, and -traditional in the other flags
if all(needle in sys.argv[1:] for needle in ['-E', '-undef', '-traditional']):
    # We're in preprocessor mode, replace "-x c" with "-x assembler-with-cpp" and append __clang_cpp_flags
    args = filterLanguageFlag(args)
    args.extend(__clang_cpp_flags) 

# Replace args[0] with the real path to clang and exec that instead
args[0] = __clang_path
#print >> sys.stderr, "Executing: %s" % repr(args)
os.execv(args[0], args)
