From 0428fa0cacd9285fa023cc922bda688b6f24f02a Mon Sep 17 00:00:00 2001 From: Artoria2e5 Date: Fri, 27 May 2022 10:04:05 +0800 Subject: [PATCH] Make target-conf emit single line target.conf currently causes three lines to be emitted. With some preprocessor defines we can make it just one, plus header. --- README.md | 10 +--------- make-fmv-patch.pl | 28 +++++++++++++--------------- targets.conf.txt | 6 ++++-- 3 files changed, 18 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index b8ee8b4..7c7f1d9 100644 --- a/README.md +++ b/README.md @@ -77,15 +77,7 @@ The command generates the following example.c.patch patch: int i,x; ``` -If you want to use a different target for your code you can create a file targets.conf - -For example - -``` -#if defined(i386__) || defined(__i386__) || defined(_M_IX86) || defined(_M_IX86) || defined(__X86__) || defined(_X86_) || defined(__I86__) || defined(__INTEL__) || defined(__amd64__) || defined(__amd64) || defined(__x86_64__) || defined(__x86_64) || defined(_M_AMD64) - __attribute__((target_clones("avx2","arch=haswell","default"))) -#endif -``` +If you want to use a different target for your code you can create a file `targets.conf`. See `targets.conf.txt` for an example. ### 3. Apply the patch Using the example.c.patch file, apply the patch to example.c: diff --git a/make-fmv-patch.pl b/make-fmv-patch.pl index 6e7b692..f09d24d 100644 --- a/make-fmv-patch.pl +++ b/make-fmv-patch.pl @@ -20,7 +20,6 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. - # This program will make its best effort on finding the candidates sources for FMV # patch them and create a bunch of ready to apply patches. @@ -32,39 +31,37 @@ my ($log_file, $source_path) = @ARGV; sub patch_function { - my $attribute = ""; + my $attribute = '__attribute__((target_clones(FMV_CLONE_TARGETS "default")))' . "\n"; + my $head = '#define FMV_CLONE_TARGETS "avx2","arch=atom",' . "\n"; my $target_clones = 'targets.conf'; if (-e $target_clones) { - open(my $fh, '<:encoding(UTF-8)', $target_clones) - or die "Could not open file '$target_clones' $!"; + open(my $fh, '<:encoding(UTF-8)', $target_clones) or die "Could not open file '$target_clones' $!"; while (my $row = <$fh>) { chomp $row; - $attribute= $attribute.$row."\n"; + $head = $head . $row . "\n"; } - - }else { - $attribute = '__attribute__((target_clones("avx2","arch=atom","default")))'."\n"; } - my ($file,@patch_line) = @_; - my $patch_file = "$file"."~"; + my ($file, @patch_line) = @_; + my $patch_file = "$file" . "~"; print "patching $file @ lines \(@patch_line\)\n"; open(my $in, "<", $file) or die "$! - $file\n"; - open(my $out, ">","$patch_file") or die __LINE__," - $!\n"; + open(my $out, ">", "$patch_file") or die __LINE__, " - $!\n"; + print $head; foreach (@patch_line) { - my $line_num = $_; + my $line_num = $_; - while( <$in> ) { + while (<$in>) { print $out $_; last if $. == ($line_num - 1); } print $out $attribute; } - while( <$in> ) { + while (<$in>) { print $out $_; } @@ -72,11 +69,12 @@ sub patch_function { close $in; my $diff = `diff -su $file $patch_file`; - open (my $d,">","$file.patch") or die __LINE__," - $!\n"; + open(my $d, ">", "$file.patch") or die __LINE__, " - $!\n"; print $d $diff; close($d); `rm $patch_file`; } + sub find_file { my ($file) = @_; diff --git a/targets.conf.txt b/targets.conf.txt index ab742fd..f684c20 100644 --- a/targets.conf.txt +++ b/targets.conf.txt @@ -1,3 +1,5 @@ -#if defined(i386__) || defined(__i386__) || defined(_M_IX86) || defined(_M_IX86) || defined(__X86__) || defined(_X86_) || defined(__I86__) || defined(__INTEL__) || defined(__amd64__) || defined(__amd64) || defined(__x86_64__) || defined(__x86_64) || defined(_M_AMD64) - __attribute__((target_clones("avx2","arch=haswell","default"))) +#if defined(__i386__) || defined(__x86_64__) + #define FMV_CLONE_TARGETS "avx2","arch=haswell", +#else + #define FMV_CLONE_TARGETS #endif