Skip to content

Commit

Permalink
Add option to record compiler time
Browse files Browse the repository at this point in the history
  • Loading branch information
dspinellis committed Sep 4, 2024
1 parent 03023d0 commit 87e7210
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
9 changes: 7 additions & 2 deletions man/csmake.1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.TH CSMAKE 1 "2 May 2024"
.TH CSMAKE 1 "5 September 2024"
.\"
.\" (C) Copyright 2020-2024 Diomidis Spinellis
.\"
Expand Down Expand Up @@ -29,6 +29,7 @@ csmake \- CScout make
[\fB\-s\fP \fIcs-files-directory\fP]
[\fB\-A\fP]
[\fB\-T\fP \fItemporary-directory\fP]
[\fB\-t\fP \fItime-file\fP]
[\fB\-N\fP \fIrules-file\fP]
--
]
Expand Down Expand Up @@ -82,6 +83,10 @@ Run on an existing rules file.
Set temporary directory.
Use \fItemporary-directory\fP as temporary directory,
remember to clear it if you use it multiple times.
.IP "\fB\-t\fP \fItime-file\fP"
Record C compiler timing information in the specified file.
The information recorded is the output of \fItime\fP(1) with
format \fC'%D %e %F %I %K %M %O %S %U\fP.
.PP
.SH EXAMPLES
The following commands are often the only ones required to process
Expand Down Expand Up @@ -142,6 +147,6 @@ CSMAKEFLAGS='-T tmp_dir -A -s cscout_projects -k --' \\
.DE
.PP
.SH "SEE ALSO"
cscout(1)
\fIcscout\fP(1), \fItime\fP(1)
.SH AUTHOR
(C) Copyright 2006-2024 Diomidis Spinellis.
32 changes: 27 additions & 5 deletions src/csmake.pl
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
@ARGV = split(/\s+/, $ENV{'CSMAKEFLAGS'});
}
my %options=(); # csmake options
my $csmake_opts = "AdhkN:o:s:T:";
my $csmake_opts = "AdhkN:o:s:T:t:";
# Parse options from CSMAKEFLAGS
getopts($csmake_opts, \%options);

Expand All @@ -91,7 +91,7 @@

if (defined $options{h}) {
print <<HELP;
usage: csmake [ [-A] [-d] [-k] [-o output] [-s cs_files_directory] [-T temporary_directory ] [-N rules_file] [-h] -- ] [make(1) options]
usage: csmake [ [-A] [-d] [-k] [-o output] [-s cs_files_directory] [-T temporary_directory ] [-t time_file] [-N rules_file] [-h] -- ] [make(1) options]
-d Run in debug mode (it also keeps spy directory in place).
-h Print help message.
-k Keep temporary directory in place.
Expand All @@ -100,6 +100,7 @@
-A Generate cs projects for static libraries.
-N rules_file Run on an existing rules file.
-T temporary_dir Set temporary directory.
-t time_file Set file to save command timing information.
HELP
exit();
}
Expand Down Expand Up @@ -166,6 +167,13 @@
next;
}
}

my $time_file;
if (defined $options{t}) {
open($time_file, '>', $options{t}) || die "Unable to open timing file $options{t}: $!\n";
}


seek (IN, 0, 0);
# Read a spy-generated rules file
while (<IN>) {
Expand All @@ -174,6 +182,8 @@
($dummy, $old, $new) = split;
$old{$new} =$old;
next;
} elsif (/^COMMENT (.*)/ && defined $options{t}) {
print $time_file "$1\n";
} elsif (/^BEGIN COMPILE/) {
$state = 'COMPILE';
undef @rules;
Expand Down Expand Up @@ -690,12 +700,24 @@ sub ancestor
$rules .= "END LINK\n";
}

# Finally, execute the real gcc keeping time information
my $tmpdir = tempdir('time-out-XXXX', DIR => $ENV{CSCOUT_SPY_TMPDIR});
my $time_out = "$tmpdir/out.txt";
$rules .= "COMMENT COMPILE $real " . join(' ', @ARGV) . "\n";
unshift(@ARGV, '-f', '%D %e %F %I %K %M %O %S %U', '-o', $time_out, $real);
print STDERR "Finally run (/usr/bin/time @ARGV))\n" if ($debug);
my $exit_code = system(('/usr/bin/time', @ARGV)) / 256;
open(IN, '<', $time_out) || die "Unable to read time output: $!\n";
my $time = <IN>;
$rules .= "COMMENT TIME $time";
close(IN);
unlink($time_out);
rmdir($tmpdir);

syswrite(RULES, $rules);
close(RULES);

# Finally, execute the real gcc
print STDERR "Finally run ($real @ARGV))\n" if ($debug);
exit system(($real, @ARGV)) / 256;
exit $exit_code;

# Return the absolute file name of a file, if the file exists in the
# current directory
Expand Down

0 comments on commit 87e7210

Please sign in to comment.