PageRenderTime 87ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/hphp/runtime/ext_zend_compat/import.php

http://github.com/facebook/hiphop-php
PHP | 138 lines | 120 code | 13 blank | 5 comment | 14 complexity | bcd5e61e95bb2d508b205f815f86fc24 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, MIT, LGPL-2.0, Apache-2.0
  1. <?php
  2. /** Imports a zend extension into HHVM **/
  3. if ($argc != 3) {
  4. die("Usage: $argv[0] <dir_to_extension> <name_of_extension>\n");
  5. }
  6. $source_root = realpath($argv[1]);
  7. $ext_name = $argv[2];
  8. $systemlib_name = 'ext_'.$ext_name.'.php';
  9. $dest_root = __DIR__.'/'.$ext_name;
  10. $test_root = __DIR__.'/../../test';
  11. print "Importing from $source_root in $dest_root\n";
  12. $objects = new RecursiveIteratorIterator(
  13. new RecursiveDirectoryIterator($source_root),
  14. RecursiveIteratorIterator::SELF_FIRST
  15. );
  16. $cpp_files = array();
  17. foreach($objects as $name => $object){
  18. if (!is_file($name)) {
  19. continue;
  20. }
  21. $dest_dir = $dest_root.'/'.substr($object->getPath(),
  22. strlen($source_root) + 1);
  23. if (substr($name, -2, 2) == '.h') {
  24. install_file($name, $dest_dir.'/'.$object->getFilename());
  25. } elseif (preg_match("/ext_{$ext_name}.php$/", $name)) {
  26. install_file($name, $dest_dir.'/'.$object->getFilename());
  27. } elseif (substr($name, -2, 2) == '.c') {
  28. $sub_path = substr($dest_dir, strlen($dest_root) + 1);
  29. $dest_file = ($sub_path ? $sub_path.'/' : '').$object->getFilename().'pp';
  30. install_file($name, $dest_root.'/'.$dest_file);
  31. $cpp_files[] = $dest_file;
  32. } elseif (substr($name, -5, 5) == '.phpt') {
  33. $dest_base_name = $test_root.'/slow/ext_'.$ext_name.'/'.
  34. substr($object->getFilename(), 0, -1);
  35. $sections = parse_phpt($name);
  36. foreach ($sections as $section_name => $section_text) {
  37. if ($section_name === 'file') {
  38. install_file_contents($dest_base_name, $section_text);
  39. } elseif (in_array($section_name, array('expect', 'expectf',
  40. 'expectregex', 'skipif'))) {
  41. install_file_contents($dest_base_name.'.'.$section_name, $section_text);
  42. }
  43. }
  44. }
  45. }
  46. $srcs = '';
  47. asort($cpp_files);
  48. foreach ($cpp_files as $file) {
  49. $file = str_replace('//', '/', $file);
  50. $srcs .= " '$file',\n";
  51. }
  52. $srcs = trim($srcs);
  53. $capital_name = strtoupper($ext_name);
  54. install_file_contents($dest_root.'/TARGETS', <<<END
  55. # -*- mode: python -*-
  56. cpp_library(
  57. name='$ext_name',
  58. srcs=[
  59. $srcs
  60. ],
  61. deps=[
  62. '@/hphp/runtime:hphp_runtime',
  63. '@/hphp/runtime/ext_zend_compat:ext_zend_compat_lib',
  64. ],
  65. preprocessor_flags=[
  66. "-Ihphp/runtime/ext_zend_compat/php-src/main/",
  67. "-Ihphp/runtime/ext_zend_compat/php-src/Zend/",
  68. "-Ihphp/runtime/ext_zend_compat/php-src/TSRM/",
  69. "-Ihphp/runtime/ext_zend_compat/php-src/",
  70. ],
  71. )
  72. END
  73. );
  74. // Write link it into the existing TARGETS
  75. // TODO sort these
  76. install_file_contents(
  77. __DIR__.'/TARGETS',
  78. str_replace(
  79. // Try to be idempotent
  80. " '@/hphp/runtime/ext_zend_compat/{$ext_name}',\n".
  81. " '@/hphp/runtime/ext_zend_compat/{$ext_name}',",
  82. " '@/hphp/runtime/ext_zend_compat/{$ext_name}',",
  83. str_replace(
  84. " '@/hphp/runtime/ext_zend_compat/calendar',",
  85. " '@/hphp/runtime/ext_zend_compat/{$ext_name}',\n".
  86. " '@/hphp/runtime/ext_zend_compat/calendar',",
  87. file_get_contents(__DIR__.'/TARGETS')
  88. )
  89. )
  90. );
  91. function install_dir($dir) {
  92. if (!is_dir($dir)) {
  93. if (!mkdir($dir, 0777, true)) {
  94. print "Unable to create directory \"$dir\"\n";
  95. exit(1);
  96. }
  97. }
  98. }
  99. function install_file($source, $dest) {
  100. install_dir(dirname($dest));
  101. if (!copy($source, $dest)) {
  102. print "Unable to create file \"$dest\"\n";
  103. exit(1);
  104. }
  105. }
  106. function install_file_contents($dest, $text) {
  107. install_dir(dirname($dest));
  108. if (file_put_contents($dest, $text) === false) {
  109. print "Unable to create file \"$dest\"\n";
  110. exit(1);
  111. }
  112. }
  113. function parse_phpt($fileName) {
  114. $contents = file_get_contents($fileName);
  115. if ($contents === false) {
  116. print "Unable to read file \"$fileName\"\n";
  117. }
  118. $bits = preg_split('/^--([_A-Z]+)--\s*\n/m', $contents, -1,
  119. PREG_SPLIT_DELIM_CAPTURE);
  120. $sections = array();
  121. for ( $i = 1; $i < count($bits) - 1; $i += 2 ) {
  122. $sections[strtolower($bits[$i])] = $bits[$i+1];
  123. }
  124. return $sections;
  125. }