SDL

Форк
0
/
build-web-examples.pl 
222 строки · 6.9 Кб
1
#!/usr/bin/perl -w
2

3
# Simple DirectMedia Layer
4
# Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
5
#
6
# This software is provided 'as-is', without any express or implied
7
# warranty.  In no event will the authors be held liable for any damages
8
# arising from the use of this software.
9
#
10
# Permission is granted to anyone to use this software for any purpose,
11
# including commercial applications, and to alter it and redistribute it
12
# freely, subject to the following restrictions:
13
#
14
# 1. The origin of this software must not be misrepresented; you must not
15
#    claim that you wrote the original software. If you use this software
16
#    in a product, an acknowledgment in the product documentation would be
17
#    appreciated but is not required.
18
# 2. Altered source versions must be plainly marked as such, and must not be
19
#    misrepresented as being the original software.
20
# 3. This notice may not be removed or altered from any source distribution.
21

22
use warnings;
23
use strict;
24
use File::Basename;
25
use File::Copy;
26
use Cwd qw(abs_path);
27
use IPC::Open2;
28

29
my $examples_dir = abs_path(dirname(__FILE__) . "/../examples");
30
my $project = undef;
31
my $emsdk_dir = undef;
32
my $compile_dir = undef;
33
my $cmake_flags = undef;
34
my $output_dir = undef;
35

36
sub usage {
37
    die("USAGE: $0 <project_name> <emsdk_dir> <compiler_output_directory> <cmake_flags> <html_output_directory>\n\n");
38
}
39

40
sub do_system {
41
    my $cmd = shift;
42
    $cmd = "exec /bin/bash -c \"$cmd\"";
43
    print("$cmd\n");
44
    return system($cmd);
45
}
46

47
sub do_mkdir {
48
    my $d = shift;
49
    if ( ! -d $d ) {
50
        print("mkdir '$d'\n");
51
        mkdir($d) or die("Couldn't mkdir('$d'): $!\n");
52
    }
53
}
54

55
sub do_copy {
56
    my $src = shift;
57
    my $dst = shift;
58
    print("cp '$src' '$dst'\n");
59
    copy($src, $dst) or die("Failed to copy '$src' to '$dst': $!\n");
60
}
61

62
sub build_latest {
63
    # Try to build just the latest without re-running cmake, since that is SLOW.
64
    print("Building latest version of $project ...\n");
65
    if (do_system("EMSDK_QUIET=1 source '$emsdk_dir/emsdk_env.sh' && cd '$compile_dir' && ninja") != 0) {
66
        # Build failed? Try nuking the build dir and running CMake from scratch.
67
        print("\n\nBuilding latest version of $project FROM SCRATCH ...\n");
68
        if (do_system("EMSDK_QUIET=1 source '$emsdk_dir/emsdk_env.sh' && rm -rf '$compile_dir' && mkdir '$compile_dir' && cd '$compile_dir' && emcmake cmake -G Ninja -DCMAKE_BUILD_TYPE=MinSizeRel $cmake_flags '$examples_dir/..' && ninja") != 0) {
69
            die("Failed to build latest version of $project!\n");  # oh well.
70
        }
71
    }
72
}
73

74
sub handle_example_dir {
75
    my $category = shift;
76
    my $example = shift;
77

78
    my @files = ();
79
    my $files_str = '';
80
    opendir(my $dh, "$examples_dir/$category/$example") or die("Couldn't opendir '$examples_dir/$category/$example': $!\n");
81
    my $spc = '';
82
    while (readdir($dh)) {
83
        my $path = "$examples_dir/$category/$example/$_";
84
        next if not -f $path;    # only care about files.
85
        push @files, $path if /\.[ch]\Z/;  # add .c and .h files to source code.
86
        if (/\.c\Z/) {  # only care about .c files for compiling.
87
            $files_str .= "$spc$path";
88
            $spc = ' ';
89
        }
90
    }
91
    closedir($dh);
92

93
    my $dst = "$output_dir/$category/$example";
94

95
    print("Building $category/$example ...\n");
96

97
    my $basefname = "$example";
98
    $basefname =~ s/\A\d+\-//;
99
    $basefname = "$category-$basefname";
100
    my $jsfname = "$basefname.js";
101
    my $wasmfname = "$basefname.wasm";
102
    my $jssrc = "$compile_dir/examples/$jsfname";
103
    my $wasmsrc = "$compile_dir/examples/$wasmfname";
104
    my $jsdst = "$dst/$jsfname";
105
    my $wasmdst = "$dst/$wasmfname";
106

107
    my $description = '';
108
    if (open(my $readmetxth, '<', "$examples_dir/$category/$example/README.txt")) {
109
        while (<$readmetxth>) {
110
            chomp;
111
            s/\A\s+//;
112
            s/\s+\Z//;
113
            $description .= "$_<br/>";
114
        }
115
        close($readmetxth);
116
    }
117

118
    do_mkdir($dst);
119
    do_copy($jssrc, $jsdst);
120
    do_copy($wasmsrc, $wasmdst);
121

122
    my $highlight_cmd = "highlight '--outdir=$dst' --style-outfile=highlight.css --fragment --enclose-pre --stdout --syntax=c '--plug-in=$examples_dir/highlight-plugin.lua'";
123
    print("$highlight_cmd\n");
124
    my $pid = open2(my $child_out, my $child_in, $highlight_cmd);
125

126
    my $htmlified_source_code = '';
127
    foreach (sort(@files)) {
128
        my $path = $_;
129
        open my $srccode, '<', $path or die("Couldn't open '$path': $!\n");
130
        my $fname = "$path";
131
        $fname =~ s/\A.*\///;
132
        print $child_in "/* $fname ... */\n\n";
133
        while (<$srccode>) {
134
            print $child_in $_;
135
        }
136
        print $child_in "\n\n\n";
137
        close($srccode);
138
    }
139

140
    close($child_in);
141

142
    while (<$child_out>) {
143
        $htmlified_source_code .= $_;
144
    }
145
    close($child_out);
146

147
    waitpid($pid, 0);
148

149

150
    my $html = '';
151
    open my $htmltemplate, '<', "$examples_dir/template.html" or die("Couldn't open '$examples_dir/template.html': $!\n");
152
    while (<$htmltemplate>) {
153
        s/\@project_name\@/$project/g;
154
        s/\@category_name\@/$category/g;
155
        s/\@example_name\@/$example/g;
156
        s/\@javascript_file\@/$jsfname/g;
157
        s/\@htmlified_source_code\@/$htmlified_source_code/g;
158
        s/\@description\@/$description/g;
159
        $html .= $_;
160
    }
161
    close($htmltemplate);
162

163
    open my $htmloutput, '>', "$dst/index.html" or die("Couldn't open '$dst/index.html': $!\n");
164
    print $htmloutput $html;
165
    close($htmloutput);
166
}
167

168
sub handle_category_dir {
169
    my $category = shift;
170

171
    print("Category $category ...\n");
172

173
    do_mkdir("$output_dir/$category");
174

175
    opendir(my $dh, "$examples_dir/$category") or die("Couldn't opendir '$examples_dir/$category': $!\n");
176

177
    while (readdir($dh)) {
178
        next if ($_ eq '.') || ($_ eq '..');  # obviously skip current and parent entries.
179
        next if not -d "$examples_dir/$category/$_";   # only care about subdirectories.
180
        handle_example_dir($category, $_);
181
    }
182

183
    closedir($dh);
184
}
185

186

187
# Mainline!
188

189
foreach (@ARGV) {
190
    $project = $_, next if not defined $project;
191
    $emsdk_dir = $_, next if not defined $emsdk_dir;
192
    $compile_dir = $_, next if not defined $compile_dir;
193
    $cmake_flags = $_, next if not defined $cmake_flags;
194
    $output_dir = $_, next if not defined $output_dir;
195
    usage();  # too many arguments.
196
}
197

198
usage() if not defined $output_dir;
199

200
print("Examples dir: $examples_dir\n");
201
print("emsdk dir: $emsdk_dir\n");
202
print("Compile dir: $compile_dir\n");
203
print("CMake flags: $cmake_flags\n");
204
print("Output dir: $output_dir\n");
205

206
do_system("rm -rf '$output_dir'");
207
do_mkdir($output_dir);
208

209
build_latest();
210

211
opendir(my $dh, $examples_dir) or die("Couldn't opendir '$examples_dir': $!\n");
212

213
while (readdir($dh)) {
214
    next if ($_ eq '.') || ($_ eq '..');  # obviously skip current and parent entries.
215
    next if not -d "$examples_dir/$_";   # only care about subdirectories.
216
    handle_category_dir($_);
217
}
218

219
closedir($dh);
220

221
print("All examples built successfully!\n");
222
exit(0);  # success!
223

224

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.