#    This file is part of MKF.
#
#    MKF is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    MKF is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with MKF; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#    Copyright (c) 2004 Alexandre Becoulet <alexandre.becoulet@epita.fr>

# look for available c complier if none was specified on the command line

_if_var_eq		cmd_c_compiler
 _if_in_path		gcc
  var_define		cmd_c_compiler		gcc
 _else
  var_define		cmd_c_compiler		cc
 _end_if
_end_if

_if_var_eq		use_c_compiler

 _if_in_path		{PVAR,cmd_c_compiler}

  _if_stdout_match	gcc		{PVAR,cmd_c_compiler} -v
   msg_info		GNU C compiler ({PVAR,cmd_c_compiler}) found
   var_define		use_c_compiler	gcc

   var_append		opt_c_compiler	-Wall -Wshadow -Wpointer-arith -Winline -pipe
   var_append		opt_c_compiler	-Wwrite-strings -Waggregate-return
   var_append		opt_c_compiler	-Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations

   _if_var_eq		debug		yes
    var_append		opt_c_compiler	-g -DDEBUG -fno-inline -gstabs
   _else
    var_append		opt_c_compiler	-O3
    var_append		opt_c_compiler	-fomit-frame-pointer
    var_append		opt_c_compiler	-march=athlon -mcpu=athlon
   _end_if

  _else

   _if_stdout_match	Intel		{PVAR,cmd_c_compiler} -V
    msg_info		Intel C compiler ({PVAR,cmd_c_compiler}) found
    var_define		use_c_compiler	intel

    var_append	opt_c_compiler	-axiM -tpp6

    _if_var_eq	debug		yes
     var_append	opt_c_compiler	-g
    _else
     var_append	opt_c_compiler	-O3
    _end_if

   _else

    _if_stdout_match	Sun|Solaris	{PVAR,cmd_c_compiler} -V
     msg_info		Sun C compiler ({PVAR,cmd_c_compiler}) found
     var_define		use_c_compiler	sun

     _if_var_eq	debug		yes
      var_append	opt_c_compiler	-g
     _else
      var_append	opt_c_compiler	-xO3
     _end_if

    _else

     msg_info		native C compiler ({PVAR,cmd_c_compiler}) found
     var_define		use_c_compiler	cc
    _end_if
   _end_if
  _end_if

 _else
  msg_error		invalid or no c complier specified
 _end_if
_end_if

_if_stdout_match	x86_64		uname -m
 msg_info		using -fPIC for compilation
 var_append		cmd_c_compiler	-fPIC
_end_if

_if_var_eq		mkf_pretty_print	yes
 command_add		c	o	echo "   CC               {SRC,.*\.c} -> {OUT}"
_end_if
command_add		c	o	{PVAR,cmd_c_compiler} -c {JVAR,opt_c_compiler} \
					-o {OUT} -I. {JLIST,-I,inc_dirs} {SRC,.*\.c}

_if_var_eq		mkf_pretty_print	yes
 command_add		S	o	echo "   CC (.S file)     {SRC,.*\.c} -> {OUT}"
_end_if
command_add		S	o	{PVAR,cmd_c_compiler} -c {JVAR,opt_c_compiler} \
					-o {OUT} -I. {JLIST,-I,inc_dirs} {SRC,.*\.S}

# check c compiler is working

_if_exec_ok		echo 'int main() { return (0);}' > {TEMP,1,.c} ; {PVAR,cmd_c_compiler} \
                        -o {TEMP,2,.bin} {TEMP,1,.c} && test -x {TEMP,2,.bin}

  msg_info		C compiler is working
_else
  msg_warning		C compiler failed to create executable file
_end_if

