ExtUtils-CChecker-0.10000755001750001750 012532126355 13405 5ustar00leoleo000000000000ExtUtils-CChecker-0.10/LICENSE000444001750001750 4376212532126355 14603 0ustar00leoleo000000000000This software is copyright (c) 2015 by Paul Evans . This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. Terms of the Perl programming language system itself a) the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version, or b) the "Artistic License" --- The GNU General Public License, Version 1, February 1989 --- This software is Copyright (c) 2015 by Paul Evans . This is free software, licensed under: The GNU General Public License, Version 1, February 1989 GNU GENERAL PUBLIC LICENSE Version 1, February 1989 Copyright (C) 1989 Free Software Foundation, Inc. 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The license agreements of most software companies try to keep users at the mercy of those companies. By contrast, our General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. The General Public License applies to the Free Software Foundation's software and to any other program whose authors commit to using it. You can use it for your programs, too. When we speak of free software, we are referring to freedom, not price. Specifically, the General Public License is designed to make sure that you have the freedom to give away or sell copies of free software, that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of a such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must tell them their rights. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License Agreement applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any work containing the Program or a portion of it, either verbatim or with modifications. Each licensee is addressed as "you". 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this General Public License and to the absence of any warranty; and give any other recipients of the Program a copy of this General Public License along with the Program. You may charge a fee for the physical act of transferring a copy. 2. You may modify your copy or copies of the Program or any portion of it, and copy and distribute such modifications under the terms of Paragraph 1 above, provided that you also do the following: a) cause the modified files to carry prominent notices stating that you changed the files and the date of any change; and b) cause the whole of any work that you distribute or publish, that in whole or in part contains the Program or any part thereof, either with or without modifications, to be licensed at no charge to all third parties under the terms of this General Public License (except that you may choose to grant warranty protection to some or all third parties, at your option). c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the simplest and most usual way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this General Public License. d) You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. Mere aggregation of another independent work with the Program (or its derivative) on a volume of a storage or distribution medium does not bring the other work under the scope of these terms. 3. You may copy and distribute the Program (or a portion or derivative of it, under Paragraph 2) in object code or executable form under the terms of Paragraphs 1 and 2 above provided that you also do one of the following: a) accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Paragraphs 1 and 2 above; or, b) accompany it with a written offer, valid for at least three years, to give any third party free (except for a nominal charge for the cost of distribution) a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Paragraphs 1 and 2 above; or, c) accompany it with the information you received as to where the corresponding source code may be obtained. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form alone.) Source code for a work means the preferred form of the work for making modifications to it. For an executable file, complete source code means all the source code for all modules it contains; but, as a special exception, it need not include source code for modules which are standard libraries that accompany the operating system on which the executable file runs, or for standard header files or definitions files that accompany that operating system. 4. You may not copy, modify, sublicense, distribute or transfer the Program except as expressly provided under this General Public License. Any attempt otherwise to copy, modify, sublicense, distribute or transfer the Program is void, and will automatically terminate your rights to use the Program under this License. However, parties who have received copies, or rights to use copies, from you under this General Public License will not have their licenses terminated so long as such parties remain in full compliance. 5. By copying, distributing or modifying the Program (or any work based on the Program) you indicate your acceptance of this license to do so, and all its terms and conditions. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. 7. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies a version number of the license which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the license, you may choose any version ever published by the Free Software Foundation. 8. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 9. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 10. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS Appendix: How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to humanity, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) 19yy This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA Also add information on how to contact you by electronic and paper mail. If the program is interactive, make it output a short notice like this when it starts in an interactive mode: Gnomovision version 69, Copyright (C) 19xx name of author Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, the commands you use may be called something other than `show w' and `show c'; they could even be mouse-clicks or menu items--whatever suits your program. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the program, if necessary. Here a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision' (a program to direct compilers to make passes at assemblers) written by James Hacker. , 1 April 1989 Ty Coon, President of Vice That's all there is to it! --- The Artistic License 1.0 --- This software is Copyright (c) 2015 by Paul Evans . This is free software, licensed under: The Artistic License 1.0 The Artistic License Preamble The intent of this document is to state the conditions under which a Package may be copied, such that the Copyright Holder maintains some semblance of artistic control over the development of the package, while giving the users of the package the right to use and distribute the Package in a more-or-less customary fashion, plus the right to make reasonable modifications. Definitions: - "Package" refers to the collection of files distributed by the Copyright Holder, and derivatives of that collection of files created through textual modification. - "Standard Version" refers to such a Package if it has not been modified, or has been modified in accordance with the wishes of the Copyright Holder. - "Copyright Holder" is whoever is named in the copyright or copyrights for the package. - "You" is you, if you're thinking about copying or distributing this Package. - "Reasonable copying fee" is whatever you can justify on the basis of media cost, duplication charges, time of people involved, and so on. (You will not be required to justify it to the Copyright Holder, but only to the computing community at large as a market that must bear the fee.) - "Freely Available" means that no fee is charged for the item itself, though there may be fees involved in handling the item. It also means that recipients of the item may redistribute it under the same conditions they received it. 1. You may make and give away verbatim copies of the source form of the Standard Version of this Package without restriction, provided that you duplicate all of the original copyright notices and associated disclaimers. 2. You may apply bug fixes, portability fixes and other modifications derived from the Public Domain or from the Copyright Holder. A Package modified in such a way shall still be considered the Standard Version. 3. You may otherwise modify your copy of this Package in any way, provided that you insert a prominent notice in each changed file stating how and when you changed that file, and provided that you do at least ONE of the following: a) place your modifications in the Public Domain or otherwise make them Freely Available, such as by posting said modifications to Usenet or an equivalent medium, or placing the modifications on a major archive site such as ftp.uu.net, or by allowing the Copyright Holder to include your modifications in the Standard Version of the Package. b) use the modified Package only within your corporation or organization. c) rename any non-standard executables so the names do not conflict with standard executables, which must also be provided, and provide a separate manual page for each non-standard executable that clearly documents how it differs from the Standard Version. d) make other distribution arrangements with the Copyright Holder. 4. You may distribute the programs of this Package in object code or executable form, provided that you do at least ONE of the following: a) distribute a Standard Version of the executables and library files, together with instructions (in the manual page or equivalent) on where to get the Standard Version. b) accompany the distribution with the machine-readable source of the Package with your modifications. c) accompany any non-standard executables with their corresponding Standard Version executables, giving the non-standard executables non-standard names, and clearly documenting the differences in manual pages (or equivalent), together with instructions on where to get the Standard Version. d) make other distribution arrangements with the Copyright Holder. 5. You may charge a reasonable copying fee for any distribution of this Package. You may charge any fee you choose for support of this Package. You may not charge a fee for this Package itself. However, you may distribute this Package in aggregate with other (possibly commercial) programs as part of a larger (possibly commercial) software distribution provided that you do not advertise this Package as a product of your own. 6. The scripts and library files supplied as input to or produced as output from the programs of this Package do not automatically fall under the copyright of this Package, but belong to whomever generated them, and may be sold commercially, and may be aggregated with this Package. 7. C or perl subroutines supplied by you and linked into this Package shall not be considered part of this Package. 8. The name of the Copyright Holder may not be used to endorse or promote products derived from this software without specific prior written permission. 9. THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. The End ExtUtils-CChecker-0.10/Build.PL000444001750001750 76212532126355 15023 0ustar00leoleo000000000000use strict; use warnings; use Module::Build; my $build = Module::Build->new( module_name => 'ExtUtils::CChecker', requires => { 'ExtUtils::CBuilder' => 0, }, build_requires => { 'Module::Build' => 0, 'Test::Fatal' => 0, 'Test::More' => 0, }, auto_configure_requires => 0, # Don't add M::B to configure_requires license => 'perl', create_makefile_pl => 'traditional', create_license => 1, create_readme => 1, ); $build->create_build_script; ExtUtils-CChecker-0.10/MANIFEST000444001750001750 32512532126355 14653 0ustar00leoleo000000000000Build.PL Changes lib/ExtUtils/CChecker.pm LICENSE Makefile.PL MANIFEST This list of files META.json META.yml README t/00use.t t/01run.t t/02assert.t t/03accessors.t t/04module_build.t t/05defines_to.t t/99pod.t ExtUtils-CChecker-0.10/META.json000444001750001750 170512532126355 15166 0ustar00leoleo000000000000{ "abstract" : "configure-time utilities for using C headers,", "author" : [ "Paul Evans " ], "dynamic_config" : 1, "generated_by" : "Module::Build version 0.421", "license" : [ "perl_5" ], "meta-spec" : { "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", "version" : "2" }, "name" : "ExtUtils-CChecker", "prereqs" : { "build" : { "requires" : { "Module::Build" : "0", "Test::Fatal" : "0", "Test::More" : "0" } }, "runtime" : { "requires" : { "ExtUtils::CBuilder" : "0" } } }, "provides" : { "ExtUtils::CChecker" : { "file" : "lib/ExtUtils/CChecker.pm", "version" : "0.10" } }, "release_status" : "stable", "resources" : { "license" : [ "http://dev.perl.org/licenses/" ] }, "version" : "0.10" } ExtUtils-CChecker-0.10/Changes000444001750001750 312312532126355 15034 0ustar00leoleo000000000000Revision history for ExtUtils-CChecker 0.10 2015/05/29 19:14:24 [CHANGES] * Added 'config' passthrough constructor argument for EU:CB (RT95368) 0.09 2013/12/12 17:10:39 [CHANGES] * Add a 'quiet' option so EU:CB's quiet can be disabled (RT91363) * Fixed some typoes in docs 0.08 CHANGES: * Include PID in test file names to guard against collisions when building concurrently (RT76013) 0.07 CHANGES: * Use Test::Fatal instead of Test::Exception * Advise against calling it "config.h" as that gets confused with core perl's file 0.06 CHANGES: * Added 'defines_to' constructor arg for writing a "config.h" file instead of -DFOO on compiler commandline 0.05 CHANGES: * Added accessors for callers to push more library dirs or compiler or linker flags BUGFIXES: * Ensure that ->new_module_build merges its args and internal configuration correctly 0.04 CHANGES: * Provide optional variants of ->find_include_dirs_for and ->find_libs_for with try_... prefix 0.03 CHANGES: * Support defining compiler symbols based on successful test runs, so XS code can be conditional on it * Line-terminate test C code to avoid (harmless) warnings from some C compilers 0.02 CHANGES: * Support automatic construction of a preconfigured Module::Build * Also implement extra_compiler_flags for completeness 0.01 First version, released on an unsuspecting world. ExtUtils-CChecker-0.10/Makefile.PL000444001750001750 73112532126355 15475 0ustar00leoleo000000000000# Note: this file was auto-generated by Module::Build::Compat version 0.4210 use ExtUtils::MakeMaker; WriteMakefile ( 'NAME' => 'ExtUtils::CChecker', 'VERSION_FROM' => 'lib/ExtUtils/CChecker.pm', 'PREREQ_PM' => { 'ExtUtils::CBuilder' => 0, 'Module::Build' => 0, 'Test::Fatal' => 0, 'Test::More' => 0 }, 'INSTALLDIRS' => 'site', 'EXE_FILES' => [], 'PL_FILES' => {} ) ; ExtUtils-CChecker-0.10/README000444001750001750 2756012532126355 14454 0ustar00leoleo000000000000NAME `ExtUtils::CChecker' - configure-time utilities for using C headers, libraries, or OS features SYNOPSIS use Module::Build; use ExtUtils::CChecker; my $cc = ExtUtils::CChecker->new; $cc->assert_compile_run( diag => "no PF_MOONLASER", source => <<'EOF' ); #include #include int main(int argc, char *argv[]) { printf("PF_MOONLASER is %d\n", PF_MOONLASER); return 0; } EOF Module::Build->new( ... )->create_build_script; DESCRIPTION Often Perl modules are written to wrap functionality found in existing C headers, libraries, or to use OS-specific features. It is useful in the Build.PL or Makefile.PL file to check for the existance of these requirements before attempting to actually build the module. Objects in this class provide an extension around ExtUtils::CBuilder to simplify the creation of a .c file, compiling, linking and running it, to test if a certain feature is present. It may also be necessary to search for the correct library to link against, or for the right include directories to find header files in. This class also provides assistance here. CONSTRUCTOR $cc = ExtUtils::CChecker->new( %args ) Returns a new instance of a `ExtUtils::CChecker' object. Takes the following named parameters: defines_to => PATH If given, defined symbols will be written to a C preprocessor .h file of the given name, instead of by adding extra `-D*SYMBOL*' arguments to the compiler flags. quiet => BOOL If given, sets the `quiet' option to the underlying `ExtUtils::CBuilder' instance. If absent, defaults to enabled. To disable quietness, i.e. to print more verbosely, pass a defined-but-false value, such as `0'. config => HASH If given, passed through as the configuration of the underlying `ExtUtils::CBuilder' instance. METHODS $dirs = $cc->include_dirs Returns the currently-configured include directories in an ARRAY reference. $flags = $cc->extra_compiler_flags Returns the currently-configured extra compiler flags in an ARRAY reference. $flags = $cc->extra_linker_flags Returns the currently-configured extra linker flags in an ARRAY reference. $cc->push_include_dirs( @dirs ) Adds more include directories $cc->push_extra_compiler_flags( @flags ) Adds more compiler flags $cc->push_extra_linker_flags( @flags ) Adds more linker flags $success = $cc->try_compile_run( %args ) $success = $cc->try_compile_run( $source ) Try to compile, link, and execute a C program whose source is given. Returns true if the program compiled and linked, and exited successfully. Returns false if any of these steps fail. Takes the following named arguments. If a single argument is given, that is taken as the source string. * source => STRING The source code of the C program to try compiling, building, and running. * extra_compiler_flags => ARRAY Optional. If specified, pass extra flags to the compiler. * extra_linker_flags => ARRAY Optional. If specified, pass extra flags to the linker. * define => STRING Optional. If specified, then the named symbol will be defined if the program ran successfully. This will either on the C compiler commandline (by passing an option `-D*SYMBOL*'), or in the `defines_to' file. $cc->assert_compile_run( %args ) Calls `try_compile_run'. If it fails, die with an `OS unsupported' message. Useful to call from Build.PL or Makefile.PL. Takes one extra optional argument: * diag => STRING If present, this string will be appended to the failure message if one is generated. It may provide more useful information to the user on why the OS is unsupported. $success = $cc->try_find_include_dirs_for( %args ) Try to compile, link and execute the given source, using extra include directories. When a usable combination is found, the directories required are stored in the object for use in further compile operations, or returned by `include_dirs'. The method then returns true. If no a usable combination is found, it returns false. Takes the following arguments: * source => STRING Source code to compile * dirs => ARRAY of ARRAYs Gives a list of sets of dirs. Each set of dirs should be strings in its own array reference. * define => STRING Optional. If specified, then the named symbol will be defined if the program ran successfully. This will either on the C compiler commandline (by passing an option `-D*SYMBOL*'), or in the `defines_to' file. $success = $cc->try_find_libs_for( %args ) Try to compile, link and execute the given source, when linked against a given set of extra libraries. When a usable combination is found, the libraries required are stored in the object for use in further link operations, or returned by `extra_linker_flags'. The method then returns true. If no usable combination is found, it returns false. Takes the following arguments: * source => STRING Source code to compile * libs => ARRAY of STRINGs Gives a list of sets of libraries. Each set of libraries should be space-separated. * define => STRING Optional. If specified, then the named symbol will be defined if the program ran successfully. This will either on the C compiler commandline (by passing an option `-D*SYMBOL*'), or in the `defines_to' file. $cc->find_include_dirs_for( %args ) $cc->find_libs_for( %args ) Calls `try_find_include_dirs_for' or `try_find_libs_for' respectively. If it fails, die with an `OS unsupported' message. Each method takes one extra optional argument: * diag => STRING If present, this string will be appended to the failure message if one is generated. It may provide more useful information to the user on why the OS is unsupported. $mb = $cc->new_module_build( %args ) Construct and return a new Module::Build object, preconfigured with the `include_dirs', `extra_compiler_flags' and `extra_linker_flags' options that have been configured on this object, by the above methods. This is provided as a simple shortcut for the common use case, that a Build.PL file is using the `ExtUtils::CChecker' object to detect the required arguments to pass. EXAMPLES Socket Libraries Some operating systems provide the BSD sockets API in their primary libc. Others keep it in a separate library which should be linked against. The following example demonstrates how this would be handled. use ExtUtils::CChecker; my $cc = ExtUtils::CChecker->new; $cc->find_libs_for( diag => "no socket()", libs => [ "", "socket nsl" ], source => q[ #include int main(int argc, char *argv) { int fd = socket(PF_INET, SOCK_STREAM, 0); if(fd < 0) return 1; return 0; } ] ); $cc->new_module_build( module_name => "Your::Name::Here", requires => { 'IO::Socket' => 0, }, ... )->create_build_script; By using the `new_module_build' method, the detected `extra_linker_flags' value has been automatically passed into the new `Module::Build' object. Testing For Optional Features Sometimes a function or ability may be optionally provided by the OS, or you may wish your module to be useable when only partial support is provided, without requiring it all to be present. In these cases it is traditional to detect the presence of this optional feature in the Build.PL script, and define a symbol to declare this fact if it is found. The XS code can then use this symbol to select between differing implementations. For example, the Build.PL: use ExtUtils::CChecker; my $cc = ExtUtils::CChecker->new; $cc->try_compile_run( define => "HAVE_MANGO", source => <<'EOF' ); #include #include int main(void) { if(mango() != 0) exit(1); exit(0); } EOF $cc->new_module_build( ... )->create_build_script; If the C code compiles and runs successfully, and exits with a true status, the symbol `HAVE_MANGO' will be defined on the compiler commandline. This allows the XS code to detect it, for example int mango() CODE: #ifdef HAVE_MANGO RETVAL = mango(); #else croak("mango() not implemented"); #endif OUTPUT: RETVAL This module will then still compile even if the operating system lacks this particular function. Trying to invoke the function at runtime will simply throw an exception. Linux Kernel Headers Operating systems built on top of the Linux kernel often share a looser association with their kernel version than most other operating systems. It may be the case that the running kernel is newer, containing more features, than the distribution's libc headers would believe. In such circumstances it can be difficult to make use of new socket options, `ioctl()'s, etc.. without having the constants that define them and their parameter structures, because the relevant header files are not visible to the compiler. In this case, there may be little choice but to pull in some of the kernel header files, which will provide the required constants and structures. The Linux kernel headers can be found using the /lib/modules directory. A fragment in Build.PL like the following, may be appropriate. chomp( my $uname_r = `uname -r` ); my @dirs = ( [], [ "/lib/modules/$uname_r/source/include" ], ); $cc->find_include_dirs_for( diag => "no PF_MOONLASER", dirs => \@dirs, source => <<'EOF' ); #include #include int family = PF_MOONLASER; struct laserwl lwl; int main(int argc, char *argv[]) { return 0; } EOF This fragment will first try to compile the program as it stands, hoping that the libc headers will be sufficient. If it fails, it will then try including the kernel headers, which should make the constant and structure visible, allowing the program to compile. Creating an `#include' file Sometimes, rather than setting defined symbols on the compiler commandline, it is preferrable to have them written to a C preprocessor include (.h) file. This may be beneficial for cross-platform portability concerns, as not all C compilers may take extra `-D' arguments on the command line, or platforms may have small length restrictions on the length of a command line. use ExtUtils::CChecker; my $cc = ExtUtils::CChecker->new( defines_to => "mymodule-config.h", ); $cc->try_compile_run( define => "HAVE_MANGO", source => <<'EOF' ); #include #include #include "mymodule-config.h" int main(void) { if(mango() != 0) exit(1); exit(0); } EOF Because the mymodule-config.h file is written and flushed after every define operation, it will still be useable in later C fragments to test for features detected in earlier ones. It is suggested not to name the file simply config.h, as the core of Perl itself has a file of that name containing its own compile-time detected configuration. A confusion between the two could lead to surprising results. AUTHOR Paul Evans ExtUtils-CChecker-0.10/META.yml000444001750001750 114012532126355 15007 0ustar00leoleo000000000000--- abstract: 'configure-time utilities for using C headers,' author: - 'Paul Evans ' build_requires: Module::Build: '0' Test::Fatal: '0' Test::More: '0' dynamic_config: 1 generated_by: 'Module::Build version 0.421, CPAN::Meta::Converter version 2.142690' license: perl meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: '1.4' name: ExtUtils-CChecker provides: ExtUtils::CChecker: file: lib/ExtUtils/CChecker.pm version: '0.10' requires: ExtUtils::CBuilder: '0' resources: license: http://dev.perl.org/licenses/ version: '0.10' ExtUtils-CChecker-0.10/lib000755001750001750 012532126355 14153 5ustar00leoleo000000000000ExtUtils-CChecker-0.10/lib/ExtUtils000755001750001750 012532126355 15734 5ustar00leoleo000000000000ExtUtils-CChecker-0.10/lib/ExtUtils/CChecker.pm000444001750001750 4123112532126355 20117 0ustar00leoleo000000000000# You may distribute under the terms of either the GNU General Public License # or the Artistic License (the same terms as Perl itself) # # (C) Paul Evans, 2010-2015 -- leonerd@leonerd.org.uk package ExtUtils::CChecker; use strict; use warnings; our $VERSION = '0.10'; use Carp; use ExtUtils::CBuilder; =head1 NAME C - configure-time utilities for using C headers, libraries, or OS features =head1 SYNOPSIS use Module::Build; use ExtUtils::CChecker; my $cc = ExtUtils::CChecker->new; $cc->assert_compile_run( diag => "no PF_MOONLASER", source => <<'EOF' ); #include #include int main(int argc, char *argv[]) { printf("PF_MOONLASER is %d\n", PF_MOONLASER); return 0; } EOF Module::Build->new( ... )->create_build_script; =head1 DESCRIPTION Often Perl modules are written to wrap functionality found in existing C headers, libraries, or to use OS-specific features. It is useful in the F or F file to check for the existance of these requirements before attempting to actually build the module. Objects in this class provide an extension around L to simplify the creation of a F<.c> file, compiling, linking and running it, to test if a certain feature is present. It may also be necessary to search for the correct library to link against, or for the right include directories to find header files in. This class also provides assistance here. =cut =head1 CONSTRUCTOR =cut =head2 $cc = ExtUtils::CChecker->new( %args ) Returns a new instance of a C object. Takes the following named parameters: =over 8 =item defines_to => PATH If given, defined symbols will be written to a C preprocessor F<.h> file of the given name, instead of by adding extra C<-DI> arguments to the compiler flags. =item quiet => BOOL If given, sets the C option to the underlying C instance. If absent, defaults to enabled. To disable quietness, i.e. to print more verbosely, pass a defined-but-false value, such as C<0>. =item config => HASH If given, passed through as the configuration of the underlying C instance. =back =cut sub new { my $class = shift; my %args = @_; my $quiet = 1; $quiet = 0 if defined $args{quiet} and !$args{quiet}; my $cb = ExtUtils::CBuilder->new( quiet => $quiet, config => $args{config}, ); return bless { cb => $cb, seq => 0, defines_to => $args{defines_to}, include_dirs => [], extra_compiler_flags => [], extra_linker_flags => [], }, $class; } =head1 METHODS =cut =head2 $dirs = $cc->include_dirs Returns the currently-configured include directories in an ARRAY reference. =cut sub include_dirs { my $self = shift; # clone it just so caller can't modify ours return [ @{ $self->{include_dirs} } ]; } =head2 $flags = $cc->extra_compiler_flags Returns the currently-configured extra compiler flags in an ARRAY reference. =cut sub extra_compiler_flags { my $self = shift; # clone it just so caller can't modify ours return [ @{ $self->{extra_compiler_flags} } ]; } =head2 $flags = $cc->extra_linker_flags Returns the currently-configured extra linker flags in an ARRAY reference. =cut sub extra_linker_flags { my $self = shift; # clone it just so caller can't modify ours return [ @{ $self->{extra_linker_flags} } ]; } =head2 $cc->push_include_dirs( @dirs ) Adds more include directories =cut sub push_include_dirs { my $self = shift; push @{ $self->{include_dirs} }, @_; } =head2 $cc->push_extra_compiler_flags( @flags ) Adds more compiler flags =cut sub push_extra_compiler_flags { my $self = shift; push @{ $self->{extra_compiler_flags} }, @_; } =head2 $cc->push_extra_linker_flags( @flags ) Adds more linker flags =cut sub push_extra_linker_flags { my $self = shift; push @{ $self->{extra_linker_flags} }, @_; } sub cbuilder { my $self = shift; return $self->{cb}; } sub compile { my $self = shift; my %args = @_; $args{include_dirs} = [ map { defined $_ ? @$_ : () } $self->{include_dirs}, $args{include_dirs} ]; $args{extra_compiler_flags} = [ map { defined $_ ? @$_ : () } $self->{extra_compiler_flags}, $args{extra_compiler_flags} ]; $self->cbuilder->compile( %args ); } sub link_executable { my $self = shift; my %args = @_; $args{extra_linker_flags} = [ map { defined $_ ? @$_ : () } $self->{extra_linker_flags}, $args{extra_linker_flags} ]; $self->cbuilder->link_executable( %args ); } sub fail { my $self = shift; my ( $diag ) = @_; my $message = defined $diag ? "OS unsupported - $diag\n" : "OS unsupported\n"; die $message; } sub define { my $self = shift; my ( $symbol ) = @_; if( $self->{defines_to} ) { unless( $self->{defines_fh} ) { open $self->{defines_fh}, ">", $self->{defines_to} or croak "Cannot open $self->{defines_to} for writing - $!"; $self->{defines_fh}->autoflush(1); } $self->{defines_fh}->print( "#define $symbol /**/\n" ); } else { $self->push_extra_compiler_flags( "-D$symbol" ); } } =head2 $success = $cc->try_compile_run( %args ) =head2 $success = $cc->try_compile_run( $source ) Try to compile, link, and execute a C program whose source is given. Returns true if the program compiled and linked, and exited successfully. Returns false if any of these steps fail. Takes the following named arguments. If a single argument is given, that is taken as the source string. =over 8 =item * source => STRING The source code of the C program to try compiling, building, and running. =item * extra_compiler_flags => ARRAY Optional. If specified, pass extra flags to the compiler. =item * extra_linker_flags => ARRAY Optional. If specified, pass extra flags to the linker. =item * define => STRING Optional. If specified, then the named symbol will be defined if the program ran successfully. This will either on the C compiler commandline (by passing an option C<-DI>), or in the C file. =back =cut sub try_compile_run { my $self = shift; my %args = ( @_ == 1 ) ? ( source => $_[0] ) : @_; defined $args{source} or croak "Expected 'source'"; my $seq = $self->{seq}++; my $test_source = "test-$$-$seq.c"; open( my $test_source_fh, "> $test_source" ) or die "Cannot write $test_source - $!"; print $test_source_fh $args{source}; close $test_source_fh; my %compile_args = ( source => $test_source, ); $compile_args{include_dirs} = $args{include_dirs} if exists $args{include_dirs}; $compile_args{extra_compiler_flags} = $args{extra_compiler_flags} if exists $args{extra_compiler_flags}; my $test_obj = eval { $self->compile( %compile_args ) }; unlink $test_source; if( not defined $test_obj ) { return 0; } my %link_args = ( objects => $test_obj, ); $link_args{extra_linker_flags} = $args{extra_linker_flags} if exists $args{extra_linker_flags}; my $test_exe = eval { $self->link_executable( %link_args ) }; unlink $test_obj; if( not defined $test_exe ) { return 0; } if( system( "./$test_exe" ) != 0 ) { unlink $test_exe; return 0; } unlink $test_exe; $self->define( $args{define} ) if defined $args{define}; return 1; } =head2 $cc->assert_compile_run( %args ) Calls C. If it fails, die with an C message. Useful to call from F or F. Takes one extra optional argument: =over 8 =item * diag => STRING If present, this string will be appended to the failure message if one is generated. It may provide more useful information to the user on why the OS is unsupported. =back =cut sub assert_compile_run { my $self = shift; my %args = @_; my $diag = delete $args{diag}; $self->try_compile_run( %args ) or $self->fail( $diag ); } =head2 $success = $cc->try_find_include_dirs_for( %args ) Try to compile, link and execute the given source, using extra include directories. When a usable combination is found, the directories required are stored in the object for use in further compile operations, or returned by C. The method then returns true. If no a usable combination is found, it returns false. Takes the following arguments: =over 8 =item * source => STRING Source code to compile =item * dirs => ARRAY of ARRAYs Gives a list of sets of dirs. Each set of dirs should be strings in its own array reference. =item * define => STRING Optional. If specified, then the named symbol will be defined if the program ran successfully. This will either on the C compiler commandline (by passing an option C<-DI>), or in the C file. =back =cut sub try_find_include_dirs_for { my $self = shift; my %args = @_; ref( my $dirs = $args{dirs} ) eq "ARRAY" or croak "Expected 'dirs' as ARRAY ref"; foreach my $d ( @$dirs ) { ref $d eq "ARRAY" or croak "Expected 'dirs' element as ARRAY ref"; $self->try_compile_run( %args, include_dirs => $d ) or next; $self->push_include_dirs( @$d ); return 1; } return 0; } =head2 $success = $cc->try_find_libs_for( %args ) Try to compile, link and execute the given source, when linked against a given set of extra libraries. When a usable combination is found, the libraries required are stored in the object for use in further link operations, or returned by C. The method then returns true. If no usable combination is found, it returns false. Takes the following arguments: =over 8 =item * source => STRING Source code to compile =item * libs => ARRAY of STRINGs Gives a list of sets of libraries. Each set of libraries should be space-separated. =item * define => STRING Optional. If specified, then the named symbol will be defined if the program ran successfully. This will either on the C compiler commandline (by passing an option C<-DI>), or in the C file. =back =cut sub try_find_libs_for { my $self = shift; my %args = @_; ref( my $libs = $args{libs} ) eq "ARRAY" or croak "Expected 'libs' as ARRAY ref"; foreach my $l ( @$libs ) { my @extra_linker_flags = map { "-l$_" } split m/\s+/, $l; $self->try_compile_run( %args, extra_linker_flags => \@extra_linker_flags ) or next; $self->push_extra_linker_flags( @extra_linker_flags ); return 1; } return 0; } =head2 $cc->find_include_dirs_for( %args ) =head2 $cc->find_libs_for( %args ) Calls C or C respectively. If it fails, die with an C message. Each method takes one extra optional argument: =over 8 =item * diag => STRING If present, this string will be appended to the failure message if one is generated. It may provide more useful information to the user on why the OS is unsupported. =back =cut foreach ( qw( find_libs_for find_include_dirs_for ) ) { my $trymethod = "try_$_"; my $code = sub { my $self = shift; my %args = @_; my $diag = delete $args{diag}; $self->$trymethod( %args ) or $self->fail( $diag ); }; no strict 'refs'; *$_ = $code; } =head2 $mb = $cc->new_module_build( %args ) Construct and return a new L object, preconfigured with the C, C and C options that have been configured on this object, by the above methods. This is provided as a simple shortcut for the common use case, that a F file is using the C object to detect the required arguments to pass. =cut sub new_module_build { my $self = shift; my %args = @_; require Module::Build; foreach my $key (qw( include_dirs extra_compiler_flags extra_linker_flags )) { if( exists $args{$key} ) { $args{$key} = [ @{ $self->$key }, @{ $args{$key} } ]; } else { $args{$key} = $self->$key; } } return Module::Build->new( %args ); } =head1 EXAMPLES =head2 Socket Libraries Some operating systems provide the BSD sockets API in their primary F. Others keep it in a separate library which should be linked against. The following example demonstrates how this would be handled. use ExtUtils::CChecker; my $cc = ExtUtils::CChecker->new; $cc->find_libs_for( diag => "no socket()", libs => [ "", "socket nsl" ], source => q[ #include int main(int argc, char *argv) { int fd = socket(PF_INET, SOCK_STREAM, 0); if(fd < 0) return 1; return 0; } ] ); $cc->new_module_build( module_name => "Your::Name::Here", requires => { 'IO::Socket' => 0, }, ... )->create_build_script; By using the C method, the detected C value has been automatically passed into the new C object. =head2 Testing For Optional Features Sometimes a function or ability may be optionally provided by the OS, or you may wish your module to be useable when only partial support is provided, without requiring it all to be present. In these cases it is traditional to detect the presence of this optional feature in the F script, and define a symbol to declare this fact if it is found. The XS code can then use this symbol to select between differing implementations. For example, the F: use ExtUtils::CChecker; my $cc = ExtUtils::CChecker->new; $cc->try_compile_run( define => "HAVE_MANGO", source => <<'EOF' ); #include #include int main(void) { if(mango() != 0) exit(1); exit(0); } EOF $cc->new_module_build( ... )->create_build_script; If the C code compiles and runs successfully, and exits with a true status, the symbol C will be defined on the compiler commandline. This allows the XS code to detect it, for example int mango() CODE: #ifdef HAVE_MANGO RETVAL = mango(); #else croak("mango() not implemented"); #endif OUTPUT: RETVAL This module will then still compile even if the operating system lacks this particular function. Trying to invoke the function at runtime will simply throw an exception. =head2 Linux Kernel Headers Operating systems built on top of the F kernel often share a looser association with their kernel version than most other operating systems. It may be the case that the running kernel is newer, containing more features, than the distribution's F headers would believe. In such circumstances it can be difficult to make use of new socket options, Cs, etc.. without having the constants that define them and their parameter structures, because the relevant header files are not visible to the compiler. In this case, there may be little choice but to pull in some of the kernel header files, which will provide the required constants and structures. The Linux kernel headers can be found using the F directory. A fragment in F like the following, may be appropriate. chomp( my $uname_r = `uname -r` ); my @dirs = ( [], [ "/lib/modules/$uname_r/source/include" ], ); $cc->find_include_dirs_for( diag => "no PF_MOONLASER", dirs => \@dirs, source => <<'EOF' ); #include #include int family = PF_MOONLASER; struct laserwl lwl; int main(int argc, char *argv[]) { return 0; } EOF This fragment will first try to compile the program as it stands, hoping that the F headers will be sufficient. If it fails, it will then try including the kernel headers, which should make the constant and structure visible, allowing the program to compile. =head2 Creating an C<#include> file Sometimes, rather than setting defined symbols on the compiler commandline, it is preferrable to have them written to a C preprocessor include (F<.h>) file. This may be beneficial for cross-platform portability concerns, as not all C compilers may take extra C<-D> arguments on the command line, or platforms may have small length restrictions on the length of a command line. use ExtUtils::CChecker; my $cc = ExtUtils::CChecker->new( defines_to => "mymodule-config.h", ); $cc->try_compile_run( define => "HAVE_MANGO", source => <<'EOF' ); #include #include #include "mymodule-config.h" int main(void) { if(mango() != 0) exit(1); exit(0); } EOF Because the F file is written and flushed after every define operation, it will still be useable in later C fragments to test for features detected in earlier ones. It is suggested not to name the file simply F, as the core of Perl itself has a file of that name containing its own compile-time detected configuration. A confusion between the two could lead to surprising results. =head1 AUTHOR Paul Evans =cut 0x55AA; ExtUtils-CChecker-0.10/t000755001750001750 012532126355 13650 5ustar00leoleo000000000000ExtUtils-CChecker-0.10/t/02assert.t000444001750001750 73712532126355 15624 0ustar00leoleo000000000000#!/usr/bin/perl -w use strict; use Test::More tests => 2; use Test::Fatal; use ExtUtils::CChecker; my $cc = ExtUtils::CChecker->new; ok( !exception { $cc->assert_compile_run( source => "int main(void) { return 0; }\n", diag => "OK source" ); }, 'Trivial C program' ); like( exception { $cc->assert_compile_run( source => "int foo bar splot\n", diag => "broken source" ); }, qr/^OS unsupported - broken source$/, 'Broken C program does not compile and run' ); ExtUtils-CChecker-0.10/t/05defines_to.t000444001750001750 125112532126355 16455 0ustar00leoleo000000000000#!/usr/bin/perl -w use strict; use Test::More tests => 5; use ExtUtils::CChecker; my $cc = ExtUtils::CChecker->new( defines_to => "test-config.h", ); END { -e "test-config.h" and unlink "test-config.h"; } ok( defined $cc, 'defined $cc' ); isa_ok( $cc, "ExtUtils::CChecker", '$cc' ); $cc->try_compile_run( source => "int main(void) { return 0; }\n", define => "HAVE_C", ); is_deeply( $cc->extra_compiler_flags, [], 'extra_compiler_flags does not have -D' ); ok( -e "test-config.h", 'test-config.h exists' ); open my $fh, "<", "test-config.h" or die "Cannot read test-config.h - $!"; is( scalar <$fh>, "#define HAVE_C /**/\n", 'test-config.h has #define HAVE_C' ); ExtUtils-CChecker-0.10/t/00use.t000444001750001750 13412532126355 15104 0ustar00leoleo000000000000#!/usr/bin/perl -w use strict; use Test::More tests => 1; use_ok( "ExtUtils::CChecker" ); ExtUtils-CChecker-0.10/t/03accessors.t000444001750001750 144512532126355 16326 0ustar00leoleo000000000000#!/usr/bin/perl -w use strict; use Test::More tests => 6; use ExtUtils::CChecker; my $cc = ExtUtils::CChecker->new; is_deeply( $cc->include_dirs, [], 'include_dirs empty initially' ); $cc->push_include_dirs( "/usr/include/foo" ); is_deeply( $cc->include_dirs, [ "/usr/include/foo" ], 'include_dirs after push_include_dirs' ); is_deeply( $cc->extra_compiler_flags, [], 'extra_compiler_flags empty initially' ); $cc->push_extra_compiler_flags( "-DHAVE_FOO" ); is_deeply( $cc->extra_compiler_flags, [ "-DHAVE_FOO" ], 'extra_compiler_flags after push_extra_compiler_flags' ); is_deeply( $cc->extra_linker_flags, [], 'extra_linker_flags empty initially' ); $cc->push_extra_linker_flags( "-lfoo" ); is_deeply( $cc->extra_linker_flags, [ "-lfoo" ], 'extra_linker_flags after push_extra_linker_flags' ); ExtUtils-CChecker-0.10/t/99pod.t000444001750001750 22712532126355 15117 0ustar00leoleo000000000000#!/usr/bin/perl -w use Test::More; eval "use Test::Pod 1.00"; plan skip_all => "Test::Pod 1.00 required for testing POD" if $@; all_pod_files_ok(); ExtUtils-CChecker-0.10/t/01run.t000444001750001750 124012532126355 15134 0ustar00leoleo000000000000#!/usr/bin/perl -w use strict; use Test::More tests => 6; use ExtUtils::CChecker; my $cc = ExtUtils::CChecker->new; ok( defined $cc, 'defined $cc' ); isa_ok( $cc, "ExtUtils::CChecker", '$cc' ); ok( $cc->try_compile_run( "int main(void) { return 0; }\n" ), 'Trivial C program compiles and runs' ); ok( !$cc->try_compile_run( "int foo bar splot\n" ), 'Broken C program does not compile and run' ); ok( $cc->try_compile_run( source => "int main(void) { return 0; }\n" ), 'source named argument' ); $cc->try_compile_run( source => "int main(void) { return 0; }\n", define => "HAVE_C", ); is_deeply( $cc->extra_compiler_flags, [ "-DHAVE_C" ], 'HAVE_C defined' ); ExtUtils-CChecker-0.10/t/04module_build.t000444001750001750 276012532126355 17007 0ustar00leoleo000000000000#!/usr/bin/perl -w use strict; use Test::More tests => 4; use ExtUtils::CChecker; my $mbpackage; my %mbargs; my $mbret; no warnings 'once'; local *Module::Build::new = sub { ( $mbpackage, %mbargs ) = @_; return $mbret; }; my $cc = ExtUtils::CChecker->new; $cc->new_module_build( module_name => "Foo::Bar" ); is( $mbpackage, "Module::Build", '$mbpackage after ->new_module_build' ); is_deeply( \%mbargs, { module_name => "Foo::Bar", include_dirs => [], extra_compiler_flags => [], extra_linker_flags => [], }, '%mbargs after ->new_module_build' ); $cc->push_include_dirs( "/usr/include/foo" ); $cc->push_extra_compiler_flags( "-DHAVE_FOO" ); $cc->push_extra_linker_flags( "-lfoo" ); $cc->new_module_build( module_name => "Foo::Bar" ); is_deeply( \%mbargs, { module_name => "Foo::Bar", include_dirs => [ "/usr/include/foo" ], extra_compiler_flags => [ "-DHAVE_FOO" ], extra_linker_flags => [ "-lfoo" ], }, '%mbargs sees correct dirs and flags' ); $cc->new_module_build( module_name => "Foo::Bar", include_dirs => [ "/usr/include/bar" ], extra_compiler_flags => [ "-DHAVE_BAR" ], extra_linker_flags => [ "-lbar" ], ); is_deeply( \%mbargs, { module_name => "Foo::Bar", include_dirs => [ "/usr/include/foo", "/usr/include/bar" ], extra_compiler_flags => [ "-DHAVE_FOO", "-DHAVE_BAR" ], extra_linker_flags => [ "-lfoo", "-lbar" ], }, 'new_module_build merges %args and internal configuration' );