#! /usr/bin/perl
#
#  TOPPERS/JSP Kernel
#      Toyohashi Open Platform for Embedded Real-Time Systems/
#      Just Standard Profile Kernel
# 
#  Copyright (C) 2000-2003 by Embedded and Real-Time Systems Laboratory
#                              Toyohashi Univ. of Technology, JAPAN
#  Copyright (C) 2004-2005 by Embedded and Real-Time Systems Laboratory
#              Graduate School of Information Science, Nagoya Univ., JAPAN
# 
#  L쌠҂́Cȉ (1)`(4) ̏CFree Software Foundation 
#  ɂČ\Ă GNU General Public License  Version 2 ɋL
#  qĂ𖞂ꍇɌC{\tgEFAi{\tgEFA
#  ς̂܂ށDȉjgpEEρEĔzziȉC
#  pƌĂԁj邱Ƃ𖳏ŋD
#  (1) {\tgEFA\[XR[ȟ`ŗpꍇɂ́CL̒
#      \C̗pщL̖ۏ؋K肪Ĉ܂܂̌`Ń\[
#      XR[hɊ܂܂Ă邱ƁD
#  (2) {\tgEFACCu`ȂǁC̃\tgEFAJɎg
#      pł`ōĔzzꍇɂ́CĔzzɔhLgip
#      ҃}jAȂǁjɁCL̒쌠\C̗pщL
#      ̖ۏ؋Kfڂ邱ƁD
#  (3) {\tgEFAC@ɑgݍނȂǁC̃\tgEFAJɎg
#      płȂ`ōĔzzꍇɂ́Ĉꂩ̏𖞂
#      ƁD
#    (a) ĔzzɔhLgip҃}jAȂǁjɁCL̒
#        쌠\C̗pщL̖ۏ؋Kfڂ邱ƁD
#    (b) Ĕzž`ԂCʂɒ߂@ɂāCTOPPERSvWFNg
#        񍐂邱ƁD
#  (4) {\tgEFA̗pɂ蒼ړI܂͊ԐړIɐ邢Ȃ鑹
#      QCL쌠҂TOPPERSvWFNgƐӂ邱ƁD
# 
#  {\tgEFÁCۏ؂Œ񋟂Ă̂łDL쌠҂
#  TOPPERSvWFNǵC{\tgEFAɊւāC̓Kp\
#  ܂߂āCȂۏ؂sȂD܂C{\tgEFA̗pɂ蒼
#  ړI܂͊ԐړIɐȂ鑹QɊւĂC̐ӔC𕉂ȂD
# 
#  @(#) $Id: makedep,v 1.2 2008/06/17 01:07:44 suikan Exp $
# 

require "getopt.pl";

#  IvV̒`
#
#  -C <cc_path>	CRpC̃R}h
#  -O <cc_opts>	CRpC/CPPɓnIvV
#
#  -X		\[Xt@CICwb_t@CƌȂ
#
#  -T <target>	^[Qbg̃t@C
#  -D <t_dir>	^[Qbg̃fBNgw肷
#  -d		^[Qbg̃fBNgێ
#  -s		^[Qbg̃TtBbNX ".s" ɂiftHg ".o"j

#
#  IvV̏
#
do Getopt("COTD");

$cc_path = $opt_C;
$cc_opts = $opt_O;

$suffix = $opt_s ? "s" : "o";
if ($opt_T) {
	$target_file = $opt_T;
}
elsif ($opt_D) {
	$target_dir = $opt_D;
}
elsif (!$opt_d) {
	$target_dir = "";
}

#
# CodeSourcery G++ Lite + Cygwin p
#
#   ϐ CSL_JSP_CYGPATH ZbgĂꍇ͓ꏈ{܂B
#   Codesourcery G++ Lite 2008q1-126 for arm-none-eabi 
#   CygwinƋɎgpꍇAMakefile.depend 
#
#     c:\\codesourcery\\gxxlite\\bin\\../lib/gcc/arm-none-eabi
#
#   ̂悤windows type  unix type ݂̕ςȃpX񂪏o邽߂łB
#
#   CSL_JSP_CYGPATH ɂ cygwin 猩 bin tH_ւ̃pXĉB
#   ̍ۃpX̏I['/'ŏIĉB
#
#   :
#     bash $ export CSL_JSP_CYGPATH="/opt/codesourcery/bin/"
#
if ($ENV{'CSL_JSP_CYGPATH'}) {
	$csl_cygpath = $ENV{'CSL_JSP_CYGPATH'};
	$csl_winpath = `cygpath -w $csl_cygpath`;
	$csl_winpath =~ s/\n//;			#Why cygpath output has newline?
	$csl_winpath =~ tr/A-Z/a-z/;		#CSL G++ ̃pXȂɂȂĂ܂
	$csl_winpath =~ s/\\/\\\\\\\\/g;	#perl̕ϐWJ̊֌W'\'𑝗
}

#
#  %dependlist ɍꂽˑ֌Wo͂
#
sub output_dependlist {
	local($file) = @_;
	local($target, $column, $len);

	if ($target_file) {
		$target = $target_file;
	}
	else {
		$target = $file;
		$target =~ s/(.*)\.(.)/$1.$suffix/;
	}	
	if (defined($target_dir)) {
		$target =~ s/^.*\/([^\/]+)$/$1/;
		if ($target_dir) {
			$target = $target_dir."/".$target;
		}
	}
	print $target, ": ";
	$column = length($target) + 2;
    
	foreach $file (keys(%dependlist)) {
		$len = length($file) + 1;
		if ($column > 8 && $column + $len >= 70) {
			print "\\\n\t";
			$column = 8;
		}
		$column += $len;
		$file =~ s#$csl_winpath#$csl_cygpath# if ($csl_cygpath);	#Convert CSL G++'s bad path
		print "$file ";
	}
	print "\n";
}

#
#  $file ̈ˑ֌W %dependlist ɍ
#
sub makedepend_one {
	local($file) = @_;
	local($command, $input, $dir, $filename);

	$command = "$cc_path -E $cc_opts";
	if ($opt_X) {
		$command .= " -x c-header";
	}
	unless (open(INPUT, "$command $file |")) {
		print STDERR "makedep: can't open $file\n";
		exit(1);
	}
	while ($line = <INPUT>) {
		if ($line =~ /^\#\s*([0-9]+)\s*\"([^\"]+)\"/) {
			$filename = $2;
			$filename =~ s/ /\\ /;
			if ($filename !~ /^\<.*\>$/ && $filename !~ /\/$/) {
				$dependlist{$filename} = 1;
			}
		}
	}
	unless (close(INPUT)) {
		print STDERR "makedep: can't execute $command\n";
		exit(1);
	}
}

#
#  C[`
#
foreach $file (@ARGV) {
	%dependlist = ();
	do makedepend_one($file);
	do output_dependlist($file) if (%dependlist);
}
