简体   繁体   English

在编译期间检测是否正在使用给定扩展名编译PHP

[英]Detect during compilation whether PHP is being compiled with a given extension

I am trying to build an extension for PHP. 我正在尝试为PHP建立扩展。 After following Sara Golemon's book I have a basic extension which I can compile as a shared module and, in addition, I can compile it statically along PHP itself. 遵循Sara Golemon的书之后,我有了一个基本扩展,可以将其编译为共享模块,此外,还可以沿PHP本身静态地对其进行编译。

Now I want to modify the PHP interpreter in order to intercept particular internal function invocations and communicate these calls to my extension. 现在,我想修改PHP解释器,以拦截特定的内部函数调用并将这些调用传达给我的扩展程序。 I want to do this only when my extension is statically compiled with PHP---the interpreter build process should otherwise generate an unmodified PHP binary. 我只想在我的扩展使用PHP静态编译时才执行此操作-否则解释器生成过程应生成未修改的PHP二进制文件。 My understanding is that I should use the C preprocessor. 我的理解是我应该使用C预处理程序。 However, to achieve my goal I need a preprocessor flag that will only be raised when PHP is configured to compile with my extension (ie ./configure --enable-myextension). 但是,要实现我的目标, 我需要一个预处理器标志,只有在将PHP配置为使用我的扩展名进行编译时 (即./configure --enable-myextension) ,才会出现该标志 Unfortunately, I cannot find such a flag nor one seems to be set by the configure script. 不幸的是,我找不到这样的标志,也似乎没有由configure脚本设置。

I should say here that I have tried setting preprossessor flags within my extension's code but this will not work. 我在这里应该说,我已经尝试在扩展程序的代码中设置preprosssor标志,但这将不起作用。 My extension is first touched late in the build process (ie roughly after the core of the interpreter) and the flags I set there are not active when the bulk of interpreter code is being compiled. 我的扩展名首先在构建过程的后期(即大约在解释器的核心之后)接触到,并且在编译大量解释器代码时,我在其中设置的标志未激活。

Any thoughts? 有什么想法吗? Do the above sound reasonable? 以上听起来合理吗?

My understanding is that I should use the C preprocessor. 我的理解是我应该使用C预处理程序。

Nope, you don't need that. 不,您不需要那。

I need a preprocessor flag that will only be raised when PHP is configured to compile with my extension 我需要一个预处理器标志,该标志仅在将PHP配置为与扩展一起编译时才会出现

Why would you want that? 你为什么要那个? It would basically limit the functionality of your extension artificially, although it's possible to hook function calls no matter how your extension is compiled. 尽管可以不管扩展的编译方式如何挂接函数调用,但基本上可以人为地限制扩展的功能。

Do the above sound reasonable? 以上听起来合理吗?

In my opinion, it's not reasonable. 我认为这是不合理的。 Please have a look at how AOP hooks function calls: https://github.com/AOP-PHP/AOP 请看一下AOP挂钩函数调用的方式: https : //github.com/AOP-PHP/AOP

If you need to hook more than just function calls, you need to reach down at the lowest level, the opcodes, by using zend_set_user_opcode_handler() . 如果您不仅需要挂钩函数调用,还需要使用zend_set_user_opcode_handler()来达到最低级别的操作码。 Please use lxr.php.net or similar tools (fgrep, etc) to find out where and how such handlers are used. 请使用lxr.php.net或类似工具(fgrep等)来查找在何处以及如何使用此类处理程序。 I know laruence was working hard on an interesting extension last year here: http://svn.php.net/viewvc/pecl/taint/trunk/taint.c?view=markup so I would take that as the most "up to date" way of doing things as a reference, if anything has changed in the meanwhile. 我知道laruence去年在这里进行了有趣的扩展: http ://svn.php.net/viewvc/pecl/taint/trunk/taint.c?view=markup,所以我认为这是最“日期”作为参考,如果与此同时发生任何变化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM