PageRenderTime 26ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/ext/ext_skel.php

https://github.com/php/php-src
PHP | 414 lines | 320 code | 50 blank | 44 comment | 36 complexity | 7d1787a009175b7e128b032b6aacdc41 MD5 | raw file
Possible License(s): BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, LGPL-2.1
  1. #!/usr/bin/env php
  2. <?php
  3. /*
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | https://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Kalle Sommer Nielsen <kalle@php.net> |
  16. +----------------------------------------------------------------------+
  17. */
  18. /* $Id: ae7a3a987c0a9a6a0d4d07cc79eed0afa15e79dc $ */
  19. /* {{{ error */
  20. function error($message) {
  21. printf('Error: %s%s', $message, PHP_EOL);
  22. exit;
  23. }
  24. /* }}} */
  25. /* {{{ print_help */
  26. function print_help() {
  27. if (PHP_OS_FAMILY != 'Windows') {
  28. $file_prefix = './';
  29. $make_prefix = '';
  30. } else {
  31. $file_prefix = '';
  32. $make_prefix = 'n';
  33. }
  34. echo <<<HELP
  35. WHAT IT IS
  36. It's a tool for automatically creating the basic framework for a PHP extension.
  37. HOW TO USE IT
  38. Very simple. First, change to the ext/ directory of the PHP sources. Then run
  39. the following
  40. php ext_skel.php --ext extension_name
  41. and everything you need will be placed in directory ext/extension_name.
  42. If you don't need to test the existence of any external header files,
  43. libraries or functions in them, the extension is ready to be compiled in PHP.
  44. To compile the extension run the following:
  45. cd extension_name
  46. phpize
  47. {$file_prefix}configure
  48. {$make_prefix}make
  49. Don't forget to run tests once the compilation is done:
  50. {$make_prefix}make test
  51. Alternatively, to compile extension in the PHP:
  52. cd /path/to/php-src
  53. {$file_prefix}buildconf
  54. {$file_prefix}configure --enable-extension_name
  55. {$make_prefix}make
  56. {$make_prefix}make test TESTS=ext/extension_name/tests
  57. The definition of PHP_extension_NAME_VERSION will be present in the
  58. php_extension_name.h and injected into the zend_extension_entry definition.
  59. This is required by the PECL website for the version string conformity checks
  60. against package.xml
  61. SOURCE AND HEADER FILE NAME
  62. The ext_skel.php script generates 'extension_name.c' and 'php_extension_name.h'
  63. as the main source and header files. Keep these names.
  64. extension functions (User functions) must be named
  65. extension_name_function()
  66. When you need to expose extension functions to other extensions, expose
  67. functions strictly needed by others. Exposed internal function must be named
  68. php_extension_name_function()
  69. See also CODING_STANDARDS.md.
  70. OPTIONS
  71. php ext_skel.php --ext <name> [--experimental] [--author <name>]
  72. [--dir <path>] [--std] [--onlyunix]
  73. [--onlywindows] [--help]
  74. --ext <name> The name of the extension defined as <name>
  75. --experimental Passed if this extension is experimental, this creates
  76. the EXPERIMENTAL file in the root of the extension
  77. --author <name> Your name, this is used if --std is passed and for the
  78. CREDITS file
  79. --dir <path> Path to the directory for where extension should be
  80. created. Defaults to the directory of where this script
  81. lives
  82. --std If passed, the standard header used in extensions that
  83. is included in the core, will be used
  84. --onlyunix Only generate configure scripts for Unix
  85. --onlywindows Only generate configure scripts for Windows
  86. --help This help
  87. HELP;
  88. exit;
  89. }
  90. /* }}} */
  91. /* {{{ task */
  92. function task($label, $callback) {
  93. printf('%s... ', $label);
  94. $callback();
  95. printf('done%s', PHP_EOL);
  96. }
  97. /* }}} */
  98. /* {{{ print_success */
  99. function print_success() {
  100. global $options;
  101. if (PHP_OS_FAMILY != 'Windows') {
  102. $file_prefix = './';
  103. $make_prefix = '';
  104. } else {
  105. $file_prefix = '';
  106. $make_prefix = 'n';
  107. }
  108. printf('%1$sSuccess. The extension is now ready to be compiled. To do so, use the%s', PHP_EOL);
  109. printf('following steps:%1$s%1$s', PHP_EOL);
  110. printf('cd %s%s%s', $options['dir'], $options['ext'], PHP_EOL);
  111. printf('phpize%s', PHP_EOL);
  112. printf('%sconfigure%s', $file_prefix, PHP_EOL);
  113. printf('%smake%2$s%2$s', $make_prefix, PHP_EOL);
  114. printf('Don\'t forget to run tests once the compilation is done:%s', PHP_EOL);
  115. printf('%smake test%2$s%2$s', $make_prefix, PHP_EOL);
  116. printf('Thank you for using PHP!%s', PHP_EOL);
  117. }
  118. /* }}} */
  119. /* {{{ process_args */
  120. function process_args($argv, $argc) {
  121. $options = [
  122. 'unix' => true,
  123. 'windows' => true,
  124. 'ext' => '',
  125. 'dir' => __DIR__ . DIRECTORY_SEPARATOR,
  126. 'skel' => __DIR__ . DIRECTORY_SEPARATOR . 'skeleton' . DIRECTORY_SEPARATOR,
  127. 'author' => false,
  128. 'experimental' => false,
  129. 'std' => false
  130. ];
  131. for($i = 1; $i < $argc; ++$i)
  132. {
  133. $val = $argv[$i];
  134. if($val[0] != '-' || $val[1] != '-')
  135. {
  136. continue;
  137. }
  138. switch($opt = strtolower(substr($val, 2)))
  139. {
  140. case 'help': {
  141. print_help();
  142. }
  143. case 'onlyunix': {
  144. $options['windows'] = false;
  145. }
  146. break;
  147. case 'onlywindows': {
  148. $options['unix'] = false;
  149. }
  150. break;
  151. case 'experimental': {
  152. $options['experimental'] = true;
  153. }
  154. break;
  155. case 'std': {
  156. $options['std'] = true;
  157. }
  158. break;
  159. case 'ext':
  160. case 'dir':
  161. case 'author': {
  162. if (!isset($argv[$i + 1]) || ($argv[$i + 1][0] == '-' && $argv[$i + 1][1] == '-')) {
  163. error('Argument "' . $val . '" expects a value, none passed');
  164. } else if ($opt == 'dir' && empty($argv[$i + 1])) {
  165. continue 2;
  166. }
  167. $options[$opt] = ($opt == 'dir' ? realpath($argv[$i + 1]) . DIRECTORY_SEPARATOR : $argv[$i + 1]);
  168. }
  169. break;
  170. default: {
  171. error('Unsupported argument "' . $val . '" passed');
  172. }
  173. }
  174. }
  175. if (empty($options['ext'])) {
  176. error('No extension name passed, use "--ext <name>"');
  177. } else if (!$options['unix'] && !$options['windows']) {
  178. error('Cannot pass both --onlyunix and --onlywindows');
  179. } else if (!is_dir($options['skel'])) {
  180. error('The skeleton directory was not found');
  181. }
  182. // Validate extension name
  183. if (!preg_match('/^[a-z][a-z0-9_]+$/i', $options['ext'])) {
  184. error('Invalid extension name. Valid names start with a letter,'
  185. .' followed by any number of letters, numbers, or underscores.'
  186. .' Using only lower case letters is preferred.');
  187. }
  188. $options['ext'] = str_replace(['\\', '/'], '', strtolower($options['ext']));
  189. return $options;
  190. }
  191. /* }}} */
  192. /* {{{ process_source_tags */
  193. function process_source_tags($file, $short_name) {
  194. global $options;
  195. $source = file_get_contents($file);
  196. if ($source === false) {
  197. error('Unable to open file for reading: ' . $short_name);
  198. }
  199. $source = str_replace('%EXTNAME%', $options['ext'], $source);
  200. $source = str_replace('%EXTNAMECAPS%', strtoupper($options['ext']), $source);
  201. if (strpos($short_name, '.c') !== false || strpos($short_name, '.h') !== false) {
  202. static $header;
  203. if (!$header) {
  204. if ($options['std']) {
  205. $author_len = strlen($options['author']);
  206. $credits = $options['author'] . ($author_len && $author_len <= 60 ? str_repeat(' ', 60 - $author_len) : '');
  207. $header = <<<"HEADER"
  208. /*
  209. +----------------------------------------------------------------------+
  210. | Copyright (c) The PHP Group |
  211. +----------------------------------------------------------------------+
  212. | This source file is subject to version 3.01 of the PHP license, |
  213. | that is bundled with this package in the file LICENSE, and is |
  214. | available through the world-wide-web at the following url: |
  215. | https://www.php.net/license/3_01.txt |
  216. | If you did not receive a copy of the PHP license and are unable to |
  217. | obtain it through the world-wide-web, please send a note to |
  218. | license@php.net so we can mail you a copy immediately. |
  219. +----------------------------------------------------------------------+
  220. | Author: $credits |
  221. +----------------------------------------------------------------------+
  222. */
  223. HEADER;
  224. } else {
  225. if ($options['author']) {
  226. $header = sprintf('/* %s extension for PHP (c) %d %s */', $options['ext'], date('Y'), $options['author']);
  227. } else {
  228. $header = sprintf('/* %s extension for PHP */', $options['ext']);
  229. }
  230. }
  231. }
  232. $source = str_replace('%HEADER%', $header, $source);
  233. }
  234. if (!file_put_contents($file, $source)) {
  235. error('Unable to save contents to file: ' . $short_name);
  236. }
  237. }
  238. /* }}} */
  239. /* {{{ copy_config_scripts */
  240. function copy_config_scripts() {
  241. global $options;
  242. $files = [];
  243. if ($options['unix']) {
  244. $files[] = 'config.m4';
  245. }
  246. if ($options['windows']) {
  247. $files[] = 'config.w32';
  248. }
  249. $files[] = '.gitignore';
  250. foreach($files as $config_script) {
  251. $new_config_script = $options['dir'] . $options['ext'] . DIRECTORY_SEPARATOR . $config_script;
  252. if (!copy($options['skel'] . $config_script . '.in', $new_config_script)) {
  253. error('Unable to copy config script: ' . $config_script);
  254. }
  255. process_source_tags($new_config_script, $config_script);
  256. }
  257. }
  258. /* }}} */
  259. /* {{{ copy_sources */
  260. function copy_sources() {
  261. global $options;
  262. $files = [
  263. 'skeleton.c' => $options['ext'] . '.c',
  264. 'skeleton.stub.php' => $options['ext'] . '.stub.php',
  265. 'php_skeleton.h' => 'php_' . $options['ext'] . '.h',
  266. 'skeleton_arginfo.h' => $options['ext'] . '_arginfo.h'
  267. ];
  268. foreach ($files as $src_file => $dst_file) {
  269. if (!copy($options['skel'] . $src_file, $options['dir'] . $options['ext'] . DIRECTORY_SEPARATOR . $dst_file)) {
  270. error('Unable to copy source file: ' . $src_file);
  271. }
  272. process_source_tags($options['dir'] . $options['ext'] . DIRECTORY_SEPARATOR . $dst_file, $dst_file);
  273. }
  274. }
  275. /* }}} */
  276. /* {{{ copy_tests */
  277. function copy_tests() {
  278. global $options;
  279. $test_files = glob($options['skel'] . 'tests/*', GLOB_MARK);
  280. if (!$test_files) {
  281. return;
  282. }
  283. foreach ($test_files as $test) {
  284. if (is_dir($test)) {
  285. continue;
  286. }
  287. $new_test = str_replace([$options['skel'], '/'], ['', DIRECTORY_SEPARATOR], $test);
  288. if (!copy($test, $options['dir'] . $options['ext'] . DIRECTORY_SEPARATOR . $new_test)) {
  289. error('Unable to copy file: ' . $new_test);
  290. }
  291. process_source_tags($options['dir'] . $options['ext'] . DIRECTORY_SEPARATOR . $new_test, $new_test);
  292. }
  293. }
  294. /* }}} */
  295. if (PHP_SAPI != 'cli') {
  296. error('This script is only suited for CLI');
  297. }
  298. if ($argc < 1) {
  299. print_help();
  300. exit;
  301. }
  302. $options = process_args($argv, $argc);
  303. if (!$options['dir'] || !is_dir($options['dir'])) {
  304. error('The selected output directory does not exist');
  305. } else if (is_dir($options['dir'] . $options['ext'])) {
  306. error('There is already a folder named "' . $options['ext'] . '" in the output directory');
  307. } else if (!mkdir($options['dir'] . $options['ext'])) {
  308. error('Unable to create extension directory in the output directory');
  309. } else if (!mkdir($options['dir'] . $options['ext'] . DIRECTORY_SEPARATOR . 'tests')) {
  310. error('Unable to create the tests directory');
  311. }
  312. if ($options['experimental']) {
  313. print('Creating EXPERIMENTAL... ');
  314. if (file_put_contents($options['dir'] . $options['ext'] . DIRECTORY_SEPARATOR . 'EXPERIMENTAL', '') === false) {
  315. error('Unable to create the EXPERIMENTAL file');
  316. }
  317. printf('done%s', PHP_EOL);
  318. }
  319. if (!empty($options['author'])) {
  320. print('Creating CREDITS... ');
  321. if (!file_put_contents($options['dir'] . $options['ext'] . DIRECTORY_SEPARATOR . 'CREDITS', $options['ext'] . PHP_EOL . $options['author'])) {
  322. error('Unable to create the CREDITS file');
  323. }
  324. printf('done%s', PHP_EOL);
  325. }
  326. date_default_timezone_set('UTC');
  327. task('Copying config scripts', 'copy_config_scripts');
  328. task('Copying sources', 'copy_sources');
  329. task('Copying tests', 'copy_tests');
  330. print_success();