/hphp/system/lib/gen_systemlib.php

https://bitbucket.org/gnanakeethan/hiphop-php · PHP · 99 lines · 85 code · 10 blank · 4 comment · 11 complexity · 3a5a8e8456bca56097944b4274d79317 MD5 · raw file

  1. <?php
  2. global $scriptPath, $outputPath;
  3. $scriptPath = dirname(__FILE__);
  4. if (!isset($argv[1])) {
  5. throw new Exception("Usage: gen_systemlib.php <outputPath> [input files]");
  6. }
  7. $outputPath = $argv[1];
  8. function processPhpFile($phpfile, $systemlib_php) {
  9. $firstchar = true;
  10. $contents = file_get_contents($phpfile);
  11. if (preg_match('/\.hhas$/', $phpfile)) {
  12. $k = 0;
  13. } else {
  14. $k = strpos($contents, "\n") + 1;
  15. $header = trim(substr($contents, 0, $k));
  16. if ($header !== "<?php") {
  17. echo "ERROR: Unexpected header in file $phpfile\n";
  18. throw new Exception("Unexpected header in file $phpfile");
  19. }
  20. }
  21. fwrite($systemlib_php, substr($contents, $k));
  22. }
  23. function populatePhpFiles($input_files) {
  24. $php_files = array();
  25. foreach ($input_files as $file) {
  26. $key = strtolower(basename($file));
  27. if (!preg_match('/\.(php|hhas)$/', $file)) {
  28. $errMsg = "ERROR: Encountered non-php file ($file)";
  29. echo $errMsg . "\n";
  30. throw new Exception($errMsg);
  31. }
  32. if (isset($php_files[$key])) {
  33. $errMsg = "ERROR: Encountered multiple php files with the same name (" .
  34. $file . " vs " . $php_files[$key] . ")";
  35. echo $errMsg . "\n";
  36. throw new Exception($errMsg);
  37. }
  38. // calling Makefile machinery will use the truncated path, full is expected
  39. $php_files[$key] = $_ENV['FBCODE_DIR'].'/'.$file;
  40. }
  41. return $php_files;
  42. }
  43. function genSystemlib($input_files) {
  44. global $scriptPath;
  45. global $outputPath;
  46. $systemlib_php_tempnam = null;
  47. $systemlib_php = null;
  48. try {
  49. $systemlib_php_tempnam = tempnam('/tmp', 'systemlib.php.tmp');
  50. $systemlib_php = fopen($systemlib_php_tempnam, 'w');
  51. $phpfiles = populatePhpFiles($input_files);
  52. fwrite($systemlib_php, "<?hh\n");
  53. fwrite($systemlib_php, '// @' . 'generated' . "\n\n");
  54. // There are some dependencies between files, so we output
  55. // the classes in a certain order so that all classes can be
  56. // hoisted.
  57. $initialFiles = array('stdclass.php', 'exception.php', 'arrayaccess.php',
  58. 'iterator.php', 'splfile.php');
  59. foreach ($initialFiles as $initialFile) {
  60. if (isset($phpfiles[$initialFile])) {
  61. processPhpFile($phpfiles[$initialFile], $systemlib_php);
  62. unset($phpfiles[$initialFile]);
  63. }
  64. }
  65. foreach ($phpfiles as $key => $phpfile) {
  66. if (preg_match('/\.php$/', $phpfile)) {
  67. processPhpFile($phpfile, $systemlib_php);
  68. unset($phpfiles[$key]);
  69. }
  70. }
  71. fwrite($systemlib_php, "\n");
  72. if (count($phpfiles)) {
  73. fwrite($systemlib_php, "\n\n<?hhas\n\n");
  74. foreach ($phpfiles as $key => $phpfile) {
  75. processPhpFile($phpfile, $systemlib_php);
  76. unset($phpfiles[$key]);
  77. }
  78. }
  79. fclose($systemlib_php);
  80. $systemlib_php = null;
  81. chmod($systemlib_php_tempnam, 0644);
  82. `mkdir -p $outputPath`;
  83. `mv -f $systemlib_php_tempnam $outputPath/systemlib.php`;
  84. } catch (Exception $e) {
  85. if ($systemlib_php) fclose($systemlib_php);
  86. if ($systemlib_php_tempnam) `rm -rf $systemlib_php_tempnam`;
  87. }
  88. }
  89. genSystemlib(array_slice($argv,2));