make-preload.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. $cwd = dirname($argv[0]) . "/../../../src";
  3. chdir($cwd);
  4. $cmd = "grep -r -l 'Generated by the protocol buffer' * | grep 'php$' | grep -v Internal";
  5. $handle = popen($cmd, 'r');
  6. $filenames = explode("\n", stream_get_contents($handle));
  7. array_pop($filenames); // empty string after last '\n'
  8. $filenames[] = "Google/Protobuf/DescriptorPool.php";
  9. $output = "../ext/google/protobuf2/bundled_php.c";
  10. function stripSuffix($str, $suffix) {
  11. return substr($str, 0, strlen($str) - strlen($suffix));
  12. }
  13. function toClassName($filename) {
  14. # Google/Protobuf/BoolValue.php -> Google\\Protobuf\\BoolValue
  15. $ret = stripSuffix($filename, ".php");
  16. return str_replace("/", "\\\\", $ret);
  17. }
  18. function toCSymbolName($filename) {
  19. # Google/Protobuf/BoolValue.php -> Google__Protobuf__BoolValue
  20. $ret = stripSuffix($filename, ".php");
  21. return str_replace("/", "__", $ret);
  22. }
  23. $f = fopen($output, "w");
  24. fwrite($f, "#include \"bundled_php.h\"\n");
  25. fwrite($f, "#include \"stdlib.h\"\n");
  26. foreach ($filenames as $filename) {
  27. print("Reading $filename...\n");
  28. $contents = file_get_contents($filename);
  29. $contents = substr($contents, 5); // Strip <?php
  30. $c_symbol_name = toCSymbolName($filename);
  31. fwrite($f, "static const char {$c_symbol_name}[] = {");
  32. for ($i = 0; $i < strlen($contents); $i++) {
  33. if ($i % 10 == 0) {
  34. fwrite($f, "\n");
  35. }
  36. fprintf($f, " 0x%02x,", ord($contents[$i]));
  37. }
  38. fwrite($f, "0};\n");
  39. }
  40. fwrite($f, "static BundledPhp_File php[] = {\n");
  41. foreach ($filenames as $filename) {
  42. $class_name = toClassName($filename);
  43. $c_symbol_name = toCSymbolName($filename);
  44. fwrite($f, " {\"$class_name\", $c_symbol_name},\n");
  45. }
  46. fwrite($f, " {NULL, NULL}\n");
  47. fwrite($f, "};\n");
  48. fwrite($f, "BundledPhp_File *bundled_files = &php[0];\n");
  49. fclose($f);
  50. print("Wrote $output\n");
  51. ?>