build-web-examples.pl 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. #!/usr/bin/perl -w
  2. # Simple DirectMedia Layer
  3. # Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
  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 handle_example_dir {
  66. my $category = shift;
  67. my $example = shift;
  68. my @files = ();
  69. my $files_str = '';
  70. opendir(my $dh, "$examples_dir/$category/$example") or die("Couldn't opendir '$examples_dir/$category/$example': $!\n");
  71. my $spc = '';
  72. while (readdir($dh)) {
  73. my $path = "$examples_dir/$category/$example/$_";
  74. next if not -f $path; # only care about files.
  75. push @files, $path if /\.[ch]\Z/; # add .c and .h files to source code.
  76. if (/\.c\Z/) { # only care about .c files for compiling.
  77. $files_str .= "$spc$path";
  78. $spc = ' ';
  79. }
  80. }
  81. closedir($dh);
  82. my $dst = "$output_dir/$category/$example";
  83. print("Building $category/$example ...\n");
  84. my $basefname = "$example";
  85. $basefname =~ s/\A\d+\-//;
  86. $basefname = "$category-$basefname";
  87. my $jsfname = "$basefname.js";
  88. my $wasmfname = "$basefname.wasm";
  89. my $jssrc = "$compile_dir/examples/$jsfname";
  90. my $wasmsrc = "$compile_dir/examples/$wasmfname";
  91. my $jsdst = "$dst/$jsfname";
  92. my $wasmdst = "$dst/$wasmfname";
  93. my $description = '';
  94. if (open(my $readmetxth, '<', "$examples_dir/$category/$example/README.txt")) {
  95. while (<$readmetxth>) {
  96. chomp;
  97. s/\A\s+//;
  98. s/\s+\Z//;
  99. $description .= "$_<br/>";
  100. }
  101. close($readmetxth);
  102. }
  103. do_mkdir($dst);
  104. do_copy($jssrc, $jsdst);
  105. do_copy($wasmsrc, $wasmdst);
  106. my $highlight_cmd = "highlight '--outdir=$dst' --style-outfile=highlight.css --fragment --enclose-pre --stdout --syntax=c '--plug-in=$examples_dir/highlight-plugin.lua'";
  107. print("$highlight_cmd\n");
  108. my $pid = open2(my $child_out, my $child_in, $highlight_cmd);
  109. my $htmlified_source_code = '';
  110. foreach (sort(@files)) {
  111. my $path = $_;
  112. open my $srccode, '<', $path or die("Couldn't open '$path': $!\n");
  113. my $fname = "$path";
  114. $fname =~ s/\A.*\///;
  115. print $child_in "/* $fname ... */\n\n";
  116. while (<$srccode>) {
  117. print $child_in $_;
  118. }
  119. print $child_in "\n\n\n";
  120. close($srccode);
  121. }
  122. close($child_in);
  123. while (<$child_out>) {
  124. $htmlified_source_code .= $_;
  125. }
  126. close($child_out);
  127. waitpid($pid, 0);
  128. my $html = '';
  129. open my $htmltemplate, '<', "$examples_dir/template.html" or die("Couldn't open '$examples_dir/template.html': $!\n");
  130. while (<$htmltemplate>) {
  131. s/\@project_name\@/$project/g;
  132. s/\@category_name\@/$category/g;
  133. s/\@example_name\@/$example/g;
  134. s/\@javascript_file\@/$jsfname/g;
  135. s/\@htmlified_source_code\@/$htmlified_source_code/g;
  136. s/\@description\@/$description/g;
  137. $html .= $_;
  138. }
  139. close($htmltemplate);
  140. open my $htmloutput, '>', "$dst/index.html" or die("Couldn't open '$dst/index.html': $!\n");
  141. print $htmloutput $html;
  142. close($htmloutput);
  143. }
  144. sub handle_category_dir {
  145. my $category = shift;
  146. print("Category $category ...\n");
  147. do_mkdir("$output_dir/$category");
  148. opendir(my $dh, "$examples_dir/$category") or die("Couldn't opendir '$examples_dir/$category': $!\n");
  149. while (readdir($dh)) {
  150. next if ($_ eq '.') || ($_ eq '..'); # obviously skip current and parent entries.
  151. next if not -d "$examples_dir/$category/$_"; # only care about subdirectories.
  152. handle_example_dir($category, $_);
  153. }
  154. closedir($dh);
  155. }
  156. # Mainline!
  157. foreach (@ARGV) {
  158. $project = $_, next if not defined $project;
  159. $emsdk_dir = $_, next if not defined $emsdk_dir;
  160. $compile_dir = $_, next if not defined $compile_dir;
  161. $cmake_flags = $_, next if not defined $cmake_flags;
  162. $output_dir = $_, next if not defined $output_dir;
  163. usage(); # too many arguments.
  164. }
  165. usage() if not defined $output_dir;
  166. print("Examples dir: $examples_dir\n");
  167. print("emsdk dir: $emsdk_dir\n");
  168. print("Compile dir: $compile_dir\n");
  169. print("CMake flags: $cmake_flags\n");
  170. print("Output dir: $output_dir\n");
  171. do_system("rm -rf '$output_dir'");
  172. do_mkdir($output_dir);
  173. build_latest();
  174. opendir(my $dh, $examples_dir) or die("Couldn't opendir '$examples_dir': $!\n");
  175. while (readdir($dh)) {
  176. next if ($_ eq '.') || ($_ eq '..'); # obviously skip current and parent entries.
  177. next if not -d "$examples_dir/$_"; # only care about subdirectories.
  178. handle_category_dir($_);
  179. }
  180. closedir($dh);
  181. print("All examples built successfully!\n");
  182. exit(0); # success!