Skip to content

Commit ab7a3f5

Browse files
Steven Rostedtrostedt
authored andcommitted
ktest: Let IF keyword take comparisons
Allow ==, !=, <=, >=, <, and > to be used in IF statements to compare if a section should be processed or not. For example: BITS := 32 DEFAULTS IF ${BITS} == 32 MIN_CONFIG = ${CONFIG_DIR}/config-32 ELSE MIN_CONFIG = ${CONFIG_DIR}/config-64 Signed-off-by: Steven Rostedt <rostedt@goodmis.org>
1 parent 45d73a5 commit ab7a3f5

2 files changed

Lines changed: 62 additions & 6 deletions

File tree

‎tools/testing/ktest/ktest.pl‎

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -361,11 +361,47 @@ sub set_variable {
361361
}
362362
}
363363

364+
sub process_compare {
365+
my ($lval, $cmp, $rval) = @_;
366+
367+
# remove whitespace
368+
369+
$lval =~ s/^\s*//;
370+
$lval =~ s/\s*$//;
371+
372+
$rval =~ s/^\s*//;
373+
$rval =~ s/\s*$//;
374+
375+
if ($cmp eq "==") {
376+
return $lval eq $rval;
377+
} elsif ($cmp eq "!=") {
378+
return $lval ne $rval;
379+
}
380+
381+
my $statement = "$lval $cmp $rval";
382+
my $ret = eval $statement;
383+
384+
# $@ stores error of eval
385+
if ($@) {
386+
return -1;
387+
}
388+
389+
return $ret;
390+
}
391+
364392
sub process_if {
365393
my ($name, $value) = @_;
366394

367395
my $val = process_variables($value);
368396

397+
if ($val =~ /(.*)(==|\!=|>=|<=|>|<)(.*)/) {
398+
my $ret = process_compare($1, $2, $3);
399+
if ($ret < 0) {
400+
die "$name: $.: Unable to process comparison\n";
401+
}
402+
return $ret;
403+
}
404+
369405
if ($val =~ /^\s*0\s*$/) {
370406
return 0;
371407
} elsif ($val =~ /^\s*\d+\s*$/) {
@@ -428,8 +464,8 @@ sub read_config {
428464
$repeat_tests{"$test_num"} = $repeat;
429465
}
430466

431-
if ($rest =~ /\sIF\s+(\S*)(.*)/) {
432-
$rest = $2;
467+
if ($rest =~ /\sIF\s+(.*)/) {
468+
$rest = "";
433469
if (process_if($name, $1)) {
434470
$if_set = 1;
435471
} else {
@@ -461,14 +497,14 @@ sub read_config {
461497
$skip = 0;
462498
}
463499

464-
if ($rest =~ /\sIF\s+(\S*)(.*)/) {
500+
if ($rest =~ /\sIF\s+(.*)/) {
465501
$if = 1;
466-
$rest = $2;
467502
if (process_if($name, $1)) {
468503
$if_set = 1;
469504
} else {
470505
$skip = 1;
471506
}
507+
$rest = "";
472508
} else {
473509
$if = 0;
474510
}
@@ -477,26 +513,32 @@ sub read_config {
477513
die "$name: $.: Gargbage found after DEFAULTS\n$_";
478514
}
479515

480-
} elsif (/^\s*ELSE(.*)$/) {
516+
} elsif (/^\s*ELSE\b(.*)$/) {
481517
if (!$if) {
482518
die "$name: $.: ELSE found with out matching IF section\n$_";
483519
}
484520
$rest = $1;
485521
if ($if_set) {
486522
$skip = 1;
523+
$rest = "";
487524
} else {
488525
$skip = 0;
489526

490-
if ($rest =~ /\sIF\s+(\S*)(.*)/) {
527+
if ($rest =~ /\sIF\s+(.*)/) {
491528
# May be a ELSE IF section.
492529
if (!process_if($name, $1)) {
493530
$skip = 1;
494531
}
532+
$rest = "";
495533
} else {
496534
$if = 0;
497535
}
498536
}
499537

538+
if ($rest !~ /^\s*$/) {
539+
die "$name: $.: Gargbage found after DEFAULTS\n$_";
540+
}
541+
500542
} elsif (/^\s*([A-Z_\[\]\d]+)\s*=\s*(.*?)\s*$/) {
501543

502544
next if ($skip);

‎tools/testing/ktest/sample.conf‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@
7272
# the same option name under the same test or as default
7373
# ktest will fail to execute, and no tests will run.
7474
#
75+
#
76+
#
7577
# Both TEST_START and DEFAULTS sections can also have the IF keyword
7678
# The value after the IF must evaluate into a 0 or non 0 positive
7779
# integer, and can use the config variables (explained below).
@@ -110,6 +112,18 @@
110112
# ELSE
111113
# BUILD_TYPE = useconfig:${CONFIG_DIR}/config-network
112114
#
115+
# The if statement may also have comparisons that will and for
116+
# == and !=, strings may be used for both sides.
117+
#
118+
# BOX_TYPE := x86_32
119+
#
120+
# DEFAULTS IF ${BOX_TYPE} == x86_32
121+
# BUILD_TYPE = useconfig:${CONFIG_DIR}/config-32
122+
# ELSE
123+
# BUILD_TYPE = useconfig:${CONFIG_DIR}/config-64
124+
#
125+
126+
113127

114128
#### Config variables ####
115129
#

0 commit comments

Comments
 (0)