-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcltoh.pl
73 lines (61 loc) · 1.71 KB
/
cltoh.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/perl
# Convert OpenCL code to a C-style string.
# I, the author, Ken Brazier, place this Perl code in the Public Domain.
use strict;
use warnings;
use File::Basename;
my $file_name = substr($ARGV[0], 0, length($ARGV[0]) - 3);
my ($file,$dir,$ext) = fileparse($file_name, qr'\.[^\.]*');
my $func_name = $file;
my $def_name = uc $func_name;
# Load the code in C++ only (to avoid app.c) and start the string!
print "#ifdef __cplusplus\n";
print "#ifndef _" . $def_name . "_CL\n";
print "#define _" . $def_name . "_CL\n";
print "const char *" . $func_name . "= \\\n";
my $in_ifdef = 0;
my $in_blockquote = 0;
while(<>) {
# Chomp.
s/[\r\n]*$//;
# Remove C++ comments - can be dangerous but makes smaller code!
# The unless makes it safer, but possibly less effective.
s/\/\/.*$// unless /".*\/\/.*"/;
# Remove a few C comments as well.
s/\/\*.*\*\///g;
# Remove C block comments. Dangerous, but perhaps not as dangerous as not removing them!
if($in_blockquote) {
if(s/^.*\*\///) { $in_blockquote = 0; }
else { next; }
}
if(s/\/\*.*$//) {
++$in_blockquote;
}
# Fix \'s not at the ends of lines.
s/\\(.)/\\\\$1/g;
# Remove (some) whitespace.
s/^[ ]*//;
s/[ ]*$//;
next if(/^$/);
# Remove ifdefs and everything in them.
# Dangerous if you #define anything!
# Also Dangerous if you use #else.
#if(/^#if/) {
#$in_ifdef++;
#next;
#}
#if($in_ifdef > 0) {
#--$in_ifdef if(/^#endif$/);
#next;
#}
# Escape quotes - vital!
s/"/\\"/g;
# Print the line in a string. Adjacent strings are concatenated.
if(s/\\$//) {
# If string ended in an escaped newline, just print the string without a newline.
print '"', $_, "\" \\\n";
} else {
print '"', $_, "\\n\" \\\n";
}
}
print ";\n#endif\n#endif\n";