build-web-examples.pl 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. #!/usr/bin/perl -w
  2. # Simple DirectMedia Layer
  3. # Copyright (C) 1997-2024 Sam Lantinga <[email protected]>
  4. #
  5. # This software is provided 'as-is', without any express or implied
  6. # warranty. In no event will the authors be held liable for any damages
  7. # arising from the use of this software.
  8. #
  9. # Permission is granted to anyone to use this software for any purpose,
  10. # including commercial applications, and to alter it and redistribute it
  11. # freely, subject to the following restrictions:
  12. #
  13. # 1. The origin of this software must not be misrepresented; you must not
  14. # claim that you wrote the original software. If you use this software
  15. # in a product, an acknowledgment in the product documentation would be
  16. # appreciated but is not required.
  17. # 2. Altered source versions must be plainly marked as such, and must not be
  18. # misrepresented as being the original software.
  19. # 3. This notice may not be removed or altered from any source distribution.
  20. use warnings;
  21. use strict;
  22. use File::Basename;
  23. use File::Copy;
  24. use Cwd qw(abs_path);
  25. use IPC::Open2;
  26. my $examples_dir = abs_path(dirname(__FILE__) . "/../examples");
  27. my $project = undef;
  28. my $emsdk_dir = undef;
  29. my $compile_dir = undef;
  30. my $cmake_flags = undef;
  31. my $output_dir = undef;
  32. sub usage {
  33. die("USAGE: $0 <project_name> <emsdk_dir> <compiler_output_directory> <cmake_flags> <html_output_directory>\n\n");
  34. }
  35. sub do_system {
  36. my $cmd = shift;
  37. $cmd = "exec /bin/bash -c \"$cmd\"";
  38. print("$cmd\n");
  39. return system($cmd);
  40. }
  41. sub do_mkdir {
  42. my $d = shift;
  43. if ( ! -d $d ) {
  44. print("mkdir '$d'\n");
  45. mkdir($d) or die("Couldn't mkdir('$d'): $!\n");
  46. }
  47. }
  48. sub do_copy {
  49. my $src = shift;
  50. my $dst = shift;
  51. print("cp '$src' '$dst'\n");
  52. copy($src, $dst) or die("Failed to copy '$src' to '$dst': $!\n");
  53. }
  54. sub build_latest {
  55. # Try to build just the latest without re-running cmake, since that is SLOW.
  56. print("Building latest version of $project ...\n");
  57. if (do_system("EMSDK_QUIET=1 source '$emsdk_dir/emsdk_env.sh' && cd '$compile_dir' && ninja") != 0) {
  58. # Build failed? Try nuking the build dir and running CMake from scratch.
  59. print("\n\nBuilding latest version of $project FROM SCRATCH ...\n");
  60. 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) {
  61. die("Failed to build latest version of $project!\n"); # oh well.
  62. }
  63. }
  64. }
  65. sub get_categories {
  66. my @categories = ();
  67. opendir(my $dh, $examples_dir) or die("Couldn't opendir '$examples_dir': $!\n");
  68. foreach my $dir (sort readdir $dh) {
  69. next if ($dir eq '.') || ($dir eq '..'); # obviously skip current and parent entries.
  70. next if not -d "$examples_dir/$dir"; # only care about subdirectories.
  71. push @categories, $dir;
  72. }
  73. closedir($dh);
  74. return @categories;
  75. }
  76. sub get_examples_for_category {
  77. my $category = shift;
  78. my @examples = ();
  79. opendir(my $dh, "$examples_dir/$category") or die("Couldn't opendir '$examples_dir/$category': $!\n");
  80. foreach my $dir (sort readdir $dh) {
  81. next if ($dir eq '.') || ($dir eq '..'); # obviously skip current and parent entries.
  82. next if not -d "$examples_dir/$category/$dir"; # only care about subdirectories.
  83. push @examples, $dir;
  84. }
  85. closedir($dh);
  86. return @examples;
  87. }
  88. sub handle_example_dir {
  89. my $category = shift;
  90. my $example = shift;
  91. my @files = ();
  92. my $files_str = '';
  93. opendir(my $dh, "$examples_dir/$category/$example") or die("Couldn't opendir '$examples_dir/$category/$example': $!\n");
  94. my $spc = '';
  95. while (readdir($dh)) {
  96. my $path = "$examples_dir/$category/$example/$_";
  97. next if not -f $path; # only care about files.
  98. push @files, $path if /\.[ch]\Z/; # add .c and .h files to source code.
  99. if (/\.c\Z/) { # only care about .c files for compiling.
  100. $files_str .= "$spc$path";
  101. $spc = ' ';
  102. }
  103. }
  104. closedir($dh);
  105. my $dst = "$output_dir/$category/$example";
  106. print("Building $category/$example ...\n");
  107. my $basefname = "$example";
  108. $basefname =~ s/\A\d+\-//;
  109. $basefname = "$category-$basefname";
  110. my $jsfname = "$basefname.js";
  111. my $wasmfname = "$basefname.wasm";
  112. my $jssrc = "$compile_dir/examples/$jsfname";
  113. my $wasmsrc = "$compile_dir/examples/$wasmfname";
  114. my $jsdst = "$dst/$jsfname";
  115. my $wasmdst = "$dst/$wasmfname";
  116. my $description = '';
  117. if (open(my $readmetxth, '<', "$examples_dir/$category/$example/README.txt")) {
  118. while (<$readmetxth>) {
  119. chomp;
  120. s/\A\s+//;
  121. s/\s+\Z//;
  122. $description .= "$_<br/>";
  123. }
  124. close($readmetxth);
  125. }
  126. do_mkdir($dst);
  127. do_copy($jssrc, $jsdst);
  128. do_copy($wasmsrc, $wasmdst);
  129. my $highlight_cmd = "highlight '--outdir=$dst' --style-outfile=highlight.css --fragment --enclose-pre --stdout --syntax=c '--plug-in=$examples_dir/highlight-plugin.lua'";
  130. print("$highlight_cmd\n");
  131. my $pid = open2(my $child_out, my $child_in, $highlight_cmd);
  132. my $htmlified_source_code = '';
  133. foreach (sort(@files)) {
  134. my $path = $_;
  135. open my $srccode, '<', $path or die("Couldn't open '$path': $!\n");
  136. my $fname = "$path";
  137. $fname =~ s/\A.*\///;
  138. print $child_in "/* $fname ... */\n\n";
  139. while (<$srccode>) {
  140. print $child_in $_;
  141. }
  142. print $child_in "\n\n\n";
  143. close($srccode);
  144. }
  145. close($child_in);
  146. while (<$child_out>) {
  147. $htmlified_source_code .= $_;
  148. }
  149. close($child_out);
  150. waitpid($pid, 0);
  151. my $other_examples_html = "<ul>";
  152. foreach my $example (get_examples_for_category($category)) {
  153. $other_examples_html .= "<li><a href='/$category/$example'>$category/$example</a></li>";
  154. }
  155. $other_examples_html .= "</ul>";
  156. my $html = '';
  157. open my $htmltemplate, '<', "$examples_dir/template.html" or die("Couldn't open '$examples_dir/template.html': $!\n");
  158. while (<$htmltemplate>) {
  159. s/\@project_name\@/$project/g;
  160. s/\@category_name\@/$category/g;
  161. s/\@example_name\@/$example/g;
  162. s/\@javascript_file\@/$jsfname/g;
  163. s/\@htmlified_source_code\@/$htmlified_source_code/g;
  164. s/\@description\@/$description/g;
  165. s/\@other_examples_html\@/$other_examples_html/g;
  166. $html .= $_;
  167. }
  168. close($htmltemplate);
  169. open my $htmloutput, '>', "$dst/index.html" or die("Couldn't open '$dst/index.html': $!\n");
  170. print $htmloutput $html;
  171. close($htmloutput);
  172. }
  173. sub handle_category_dir {
  174. my $category = shift;
  175. print("Category $category ...\n");
  176. do_mkdir("$output_dir/$category");
  177. opendir(my $dh, "$examples_dir/$category") or die("Couldn't opendir '$examples_dir/$category': $!\n");
  178. while (readdir($dh)) {
  179. next if ($_ eq '.') || ($_ eq '..'); # obviously skip current and parent entries.
  180. next if not -d "$examples_dir/$category/$_"; # only care about subdirectories.
  181. handle_example_dir($category, $_);
  182. }
  183. closedir($dh);
  184. my $examples_list_html = "";
  185. foreach my $example (get_examples_for_category($category)) {
  186. # !!! FIXME: image
  187. my $example_image_url = "https://placehold.co/600x400/png";
  188. $examples_list_html .= "
  189. <a href='/$category/$example'>
  190. <div>
  191. <img src='$example_image_url' />
  192. <div>$category/$example</div>
  193. </div>
  194. </a>";
  195. }
  196. # write category page
  197. my $dst = "$output_dir/$category";
  198. my $html = '';
  199. open my $htmltemplate, '<', "$examples_dir/template-category.html" or die("Couldn't open '$examples_dir/template-category.html': $!\n");
  200. while (<$htmltemplate>) {
  201. s/\@project_name\@/$project/g;
  202. s/\@category_name\@/$category/g;
  203. s/\@examples_list_html\@/$examples_list_html/g;
  204. $html .= $_;
  205. }
  206. close($htmltemplate);
  207. open my $htmloutput, '>', "$dst/index.html" or die("Couldn't open '$dst/index.html': $!\n");
  208. print $htmloutput $html;
  209. close($htmloutput);
  210. }
  211. # Mainline!
  212. foreach (@ARGV) {
  213. $project = $_, next if not defined $project;
  214. $emsdk_dir = $_, next if not defined $emsdk_dir;
  215. $compile_dir = $_, next if not defined $compile_dir;
  216. $cmake_flags = $_, next if not defined $cmake_flags;
  217. $output_dir = $_, next if not defined $output_dir;
  218. usage(); # too many arguments.
  219. }
  220. usage() if not defined $output_dir;
  221. print("Examples dir: $examples_dir\n");
  222. print("emsdk dir: $emsdk_dir\n");
  223. print("Compile dir: $compile_dir\n");
  224. print("CMake flags: $cmake_flags\n");
  225. print("Output dir: $output_dir\n");
  226. do_system("rm -rf '$output_dir'");
  227. do_mkdir($output_dir);
  228. build_latest();
  229. do_copy("$examples_dir/template.css", "$output_dir/examples.css");
  230. opendir(my $dh, $examples_dir) or die("Couldn't opendir '$examples_dir': $!\n");
  231. while (readdir($dh)) {
  232. next if ($_ eq '.') || ($_ eq '..'); # obviously skip current and parent entries.
  233. next if not -d "$examples_dir/$_"; # only care about subdirectories.
  234. # !!! FIXME: this needs to generate a preview page for all the categories.
  235. handle_category_dir($_);
  236. }
  237. closedir($dh);
  238. # write homepage
  239. my $homepage_list_html = "";
  240. foreach my $category (get_categories()) {
  241. $homepage_list_html .= "<h2>$category</h2>";
  242. $homepage_list_html .= "<div class='list'>";
  243. foreach my $example (get_examples_for_category($category)) {
  244. # !!! FIXME: image
  245. my $example_image_url = "https://placehold.co/600x400/png";
  246. $homepage_list_html .= "
  247. <a href='/$category/$example'>
  248. <div>
  249. <img src='$example_image_url' />
  250. <div>$category/$example</div>
  251. </div>
  252. </a>";
  253. }
  254. $homepage_list_html .= "</div>";
  255. }
  256. my $dst = "$output_dir/";
  257. my $html = '';
  258. open my $htmltemplate, '<', "$examples_dir/template-homepage.html" or die("Couldn't open '$examples_dir/template-category.html': $!\n");
  259. while (<$htmltemplate>) {
  260. s/\@project_name\@/$project/g;
  261. s/\@homepage_list_html\@/$homepage_list_html/g;
  262. $html .= $_;
  263. }
  264. close($htmltemplate);
  265. open my $htmloutput, '>', "$dst/index.html" or die("Couldn't open '$dst/index.html': $!\n");
  266. print $htmloutput $html;
  267. close($htmloutput);
  268. print("All examples built successfully!\n");
  269. exit(0); # success!