├── .gitignore ├── LICENSE ├── META ├── Makefile ├── OCamlMakefile ├── README.md ├── aux.ml ├── aux.mli ├── dot.ml ├── dot.mli ├── sexpr.ml ├── sexpr.mli ├── value.ml ├── value.mli └── value_cimpl.c /.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | *.o 3 | *.a 4 | *.cm[aiox] 5 | *.cmxa 6 | *.so 7 | ._d 8 | 9 | .DS_Store 10 | ._* 11 | doc/ 12 | .\#* 13 | \#* 14 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The Library is distributed under the terms of the GNU Library General 2 | Public License version 2 (included below). 3 | 4 | As a special exception to the GNU Library General Public License, you 5 | may link, statically or dynamically, a "work that uses the Library" 6 | with a publicly distributed version of the Library to produce an 7 | executable file containing portions of the Library, and distribute 8 | that executable file under terms of your choice, without any of the 9 | additional requirements listed in clause 6 of the GNU Library General 10 | Public License. By "a publicly distributed version of the Library", 11 | we mean either the unmodified Library as distributed, or a 12 | modified version of the Library that is distributed under the 13 | conditions defined in clause 3 of the GNU Library General Public 14 | License. This exception does not however invalidate any other reasons 15 | why the executable file might be covered by the GNU Library General 16 | Public License. 17 | 18 | ------------ 19 | 20 | GNU LESSER GENERAL PUBLIC LICENSE 21 | Version 2.1, February 1999 22 | 23 | 24 | Copyright (C) 1991, 1999 Free Software Foundation, Inc. 25 | 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 26 | Everyone is permitted to copy and distribute verbatim copies 27 | of this license document, but changing it is not allowed. 28 | 29 | [This is the first released version of the Lesser GPL. It also counts 30 | as the successor of the GNU Library Public License, version 2, hence 31 | the version number 2.1.] 32 | 33 | Preamble 34 | The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public Licenses are intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. 35 | 36 | This license, the Lesser General Public License, applies to some specially designated software packages--typically libraries--of the Free Software Foundation and other authors who decide to use it. You can use it too, but we suggest you first think carefully about whether this license or the ordinary General Public License is the better strategy to use in any particular case, based on the explanations below. 37 | 38 | When we speak of free software, we are referring to freedom of use, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for this service if you wish); that you receive source code or can get it if you want it; that you can change the software and use pieces of it in new free programs; and that you are informed that you can do these things. 39 | 40 | To protect your rights, we need to make restrictions that forbid distributors to deny you these rights or to ask you to surrender these rights. These restrictions translate to certain responsibilities for you if you distribute copies of the library or if you modify it. 41 | 42 | For example, if you distribute copies of the library, whether gratis or for a fee, you must give the recipients all the rights that we gave you. You must make sure that they, too, receive or can get the source code. If you link other code with the library, you must provide complete object files to the recipients, so that they can relink them with the library after making changes to the library and recompiling it. And you must show them these terms so they know their rights. 43 | 44 | We protect your rights with a two-step method: (1) we copyright the library, and (2) we offer you this license, which gives you legal permission to copy, distribute and/or modify the library. 45 | 46 | To protect each distributor, we want to make it very clear that there is no warranty for the free library. Also, if the library is modified by someone else and passed on, the recipients should know that what they have is not the original version, so that the original author's reputation will not be affected by problems that might be introduced by others. 47 | 48 | Finally, software patents pose a constant threat to the existence of any free program. We wish to make sure that a company cannot effectively restrict the users of a free program by obtaining a restrictive license from a patent holder. Therefore, we insist that any patent license obtained for a version of the library must be consistent with the full freedom of use specified in this license. 49 | 50 | Most GNU software, including some libraries, is covered by the ordinary GNU General Public License. This license, the GNU Lesser General Public License, applies to certain designated libraries, and is quite different from the ordinary General Public License. We use this license for certain libraries in order to permit linking those libraries into non-free programs. 51 | 52 | When a program is linked with a library, whether statically or using a shared library, the combination of the two is legally speaking a combined work, a derivative of the original library. The ordinary General Public License therefore permits such linking only if the entire combination fits its criteria of freedom. The Lesser General Public License permits more lax criteria for linking other code with the library. 53 | 54 | We call this license the "Lesser" General Public License because it does Less to protect the user's freedom than the ordinary General Public License. It also provides other free software developers Less of an advantage over competing non-free programs. These disadvantages are the reason we use the ordinary General Public License for many libraries. However, the Lesser license provides advantages in certain special circumstances. 55 | 56 | For example, on rare occasions, there may be a special need to encourage the widest possible use of a certain library, so that it becomes a de-facto standard. To achieve this, non-free programs must be allowed to use the library. A more frequent case is that a free library does the same job as widely used non-free libraries. In this case, there is little to gain by limiting the free library to free software only, so we use the Lesser General Public License. 57 | 58 | In other cases, permission to use a particular library in non-free programs enables a greater number of people to use a large body of free software. For example, permission to use the GNU C Library in non-free programs enables many more people to use the whole GNU operating system, as well as its variant, the GNU/Linux operating system. 59 | 60 | Although the Lesser General Public License is Less protective of the users' freedom, it does ensure that the user of a program that is linked with the Library has the freedom and the wherewithal to run that program using a modified version of the Library. 61 | 62 | The precise terms and conditions for copying, distribution and modification follow. Pay close attention to the difference between a "work based on the library" and a "work that uses the library". The former contains code derived from the library, whereas the latter must be combined with the library in order to run. 63 | 64 | 65 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 66 | 0. This License Agreement applies to any software library or other program which contains a notice placed by the copyright holder or other authorized party saying it may be distributed under the terms of this Lesser General Public License (also called "this License"). Each licensee is addressed as "you". 67 | 68 | A "library" means a collection of software functions and/or data prepared so as to be conveniently linked with application programs (which use some of those functions and data) to form executables. 69 | 70 | The "Library", below, refers to any such software library or work which has been distributed under these terms. A "work based on the Library" means either the Library or any derivative work under copyright law: that is to say, a work containing the Library or a portion of it, either verbatim or with modifications and/or translated straightforwardly into another language. (Hereinafter, translation is included without limitation in the term "modification".) 71 | 72 | "Source code" for a work means the preferred form of the work for making modifications to it. For a library, complete source code means all the source code for all modules it contains, plus any associated interface definition files, plus the scripts used to control compilation and installation of the library. 73 | 74 | Activities other than copying, distribution and modification are not covered by this License; they are outside its scope. The act of running a program using the Library is not restricted, and output from such a program is covered only if its contents constitute a work based on the Library (independent of the use of the Library in a tool for writing it). Whether that is true depends on what the Library does and what the program that uses the Library does. 75 | 76 | 1. You may copy and distribute verbatim copies of the Library's complete 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 License and to the absence of any warranty; and distribute a copy of this License along with the Library. 77 | 78 | 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. 79 | 80 | 2. You may modify your copy or copies of the Library or any portion of it, thus forming a work based on the Library, and copy and distribute such modifications or work under the terms of Section 1 above, provided that you also meet all of these conditions: 81 | 82 | 83 | a) The modified work must itself be a software library. 84 | b) You must cause the files modified to carry prominent notices stating that you changed the files and the date of any change. 85 | c) You must cause the whole of the work to be licensed at no charge to all third parties under the terms of this License. 86 | d) If a facility in the modified Library refers to a function or a table of data to be supplied by an application program that uses the facility, other than as an argument passed when the facility is invoked, then you must make a good faith effort to ensure that, in the event an application does not supply such function or table, the facility still operates, and performs whatever part of its purpose remains meaningful. 87 | (For example, a function in a library to compute square roots has a purpose that is entirely well-defined independent of the application. Therefore, Subsection 2d requires that any application-supplied function or table used by this function must be optional: if the application does not supply it, the square root function must still compute square roots.) 88 | 89 | These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Library, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works. But when you distribute the same sections as part of a whole which is a work based on the Library, the distribution of the whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, and thus to each and every part regardless of who wrote it. 90 | 91 | Thus, it is not the intent of this section to claim rights or contest your rights to work written entirely by you; rather, the intent is to exercise the right to control the distribution of derivative or collective works based on the Library. 92 | 93 | In addition, mere aggregation of another work not based on the Library with the Library (or with a work based on the Library) on a volume of a storage or distribution medium does not bring the other work under the scope of this License. 94 | 95 | 3. You may opt to apply the terms of the ordinary GNU General Public License instead of this License to a given copy of the Library. To do this, you must alter all the notices that refer to this License, so that they refer to the ordinary GNU General Public License, version 2, instead of to this License. (If a newer version than version 2 of the ordinary GNU General Public License has appeared, then you can specify that version instead if you wish.) Do not make any other change in these notices. 96 | 97 | Once this change is made in a given copy, it is irreversible for that copy, so the ordinary GNU General Public License applies to all subsequent copies and derivative works made from that copy. 98 | 99 | This option is useful when you wish to copy part of the code of the Library into a program that is not a library. 100 | 101 | 4. You may copy and distribute the Library (or a portion or derivative of it, under Section 2) in object code or executable form under the terms of Sections 1 and 2 above provided that you accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange. 102 | 103 | If distribution of object code is made by offering access to copy from a designated place, then offering equivalent access to copy the source code from the same place satisfies the requirement to distribute the source code, even though third parties are not compelled to copy the source along with the object code. 104 | 105 | 5. A program that contains no derivative of any portion of the Library, but is designed to work with the Library by being compiled or linked with it, is called a "work that uses the Library". Such a work, in isolation, is not a derivative work of the Library, and therefore falls outside the scope of this License. 106 | 107 | However, linking a "work that uses the Library" with the Library creates an executable that is a derivative of the Library (because it contains portions of the Library), rather than a "work that uses the library". The executable is therefore covered by this License. Section 6 states terms for distribution of such executables. 108 | 109 | When a "work that uses the Library" uses material from a header file that is part of the Library, the object code for the work may be a derivative work of the Library even though the source code is not. Whether this is true is especially significant if the work can be linked without the Library, or if the work is itself a library. The threshold for this to be true is not precisely defined by law. 110 | 111 | If such an object file uses only numerical parameters, data structure layouts and accessors, and small macros and small inline functions (ten lines or less in length), then the use of the object file is unrestricted, regardless of whether it is legally a derivative work. (Executables containing this object code plus portions of the Library will still fall under Section 6.) 112 | 113 | Otherwise, if the work is a derivative of the Library, you may distribute the object code for the work under the terms of Section 6. Any executables containing that work also fall under Section 6, whether or not they are linked directly with the Library itself. 114 | 115 | 6. As an exception to the Sections above, you may also combine or link a "work that uses the Library" with the Library to produce a work containing portions of the Library, and distribute that work under terms of your choice, provided that the terms permit modification of the work for the customer's own use and reverse engineering for debugging such modifications. 116 | 117 | You must give prominent notice with each copy of the work that the Library is used in it and that the Library and its use are covered by this License. You must supply a copy of this License. If the work during execution displays copyright notices, you must include the copyright notice for the Library among them, as well as a reference directing the user to the copy of this License. Also, you must do one of these things: 118 | 119 | 120 | a) Accompany the work with the complete corresponding machine-readable source code for the Library including whatever changes were used in the work (which must be distributed under Sections 1 and 2 above); and, if the work is an executable linked with the Library, with the complete machine-readable "work that uses the Library", as object code and/or source code, so that the user can modify the Library and then relink to produce a modified executable containing the modified Library. (It is understood that the user who changes the contents of definitions files in the Library will not necessarily be able to recompile the application to use the modified definitions.) 121 | b) Use a suitable shared library mechanism for linking with the Library. A suitable mechanism is one that (1) uses at run time a copy of the library already present on the user's computer system, rather than copying library functions into the executable, and (2) will operate properly with a modified version of the library, if the user installs one, as long as the modified version is interface-compatible with the version that the work was made with. 122 | c) Accompany the work with a written offer, valid for at least three years, to give the same user the materials specified in Subsection 6a, above, for a charge no more than the cost of performing this distribution. 123 | d) If distribution of the work is made by offering access to copy from a designated place, offer equivalent access to copy the above specified materials from the same place. 124 | e) Verify that the user has already received a copy of these materials or that you have already sent this user a copy. 125 | For an executable, the required form of the "work that uses the Library" must include any data and utility programs needed for reproducing the executable from it. However, as a special exception, the materials to be distributed need not include anything that is normally distributed (in either source or binary form) with the major components (compiler, kernel, and so on) of the operating system on which the executable runs, unless that component itself accompanies the executable. 126 | 127 | It may happen that this requirement contradicts the license restrictions of other proprietary libraries that do not normally accompany the operating system. Such a contradiction means you cannot use both them and the Library together in an executable that you distribute. 128 | 129 | 7. You may place library facilities that are a work based on the Library side-by-side in a single library together with other library facilities not covered by this License, and distribute such a combined library, provided that the separate distribution of the work based on the Library and of the other library facilities is otherwise permitted, and provided that you do these two things: 130 | 131 | 132 | a) Accompany the combined library with a copy of the same work based on the Library, uncombined with any other library facilities. This must be distributed under the terms of the Sections above. 133 | b) Give prominent notice with the combined library of the fact that part of it is a work based on the Library, and explaining where to find the accompanying uncombined form of the same work. 134 | 8. You may not copy, modify, sublicense, link with, or distribute the Library except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense, link with, or distribute the Library is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance. 135 | 136 | 9. You are not required to accept this License, since you have not signed it. However, nothing else grants you permission to modify or distribute the Library or its derivative works. These actions are prohibited by law if you do not accept this License. Therefore, by modifying or distributing the Library (or any work based on the Library), you indicate your acceptance of this License to do so, and all its terms and conditions for copying, distributing or modifying the Library or works based on it. 137 | 138 | 10. Each time you redistribute the Library (or any work based on the Library), the recipient automatically receives a license from the original licensor to copy, distribute, link with or modify the Library subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. You are not responsible for enforcing compliance by third parties with this License. 139 | 140 | 11. If, as a consequence of a court judgment or allegation of patent infringement or for any other reason (not limited to patent issues), conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot distribute so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not distribute the Library at all. For example, if a patent license would not permit royalty-free redistribution of the Library by all those who receive copies directly or indirectly through you, then the only way you could satisfy both it and this License would be to refrain entirely from distribution of the Library. 141 | 142 | If any portion of this section is held invalid or unenforceable under any particular circumstance, the balance of the section is intended to apply, and the section as a whole is intended to apply in other circumstances. 143 | 144 | It is not the purpose of this section to induce you to infringe any patents or other property right claims or to contest validity of any such claims; this section has the sole purpose of protecting the integrity of the free software distribution system which is implemented by public license practices. Many people have made generous contributions to the wide range of software distributed through that system in reliance on consistent application of that system; it is up to the author/donor to decide if he or she is willing to distribute software through any other system and a licensee cannot impose that choice. 145 | 146 | This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License. 147 | 148 | 12. If the distribution and/or use of the Library is restricted in certain countries either by patents or by copyrighted interfaces, the original copyright holder who places the Library under this License may add an explicit geographical distribution limitation excluding those countries, so that distribution is permitted only in or among countries not thus excluded. In such case, this License incorporates the limitation as if written in the body of this License. 149 | 150 | 13. The Free Software Foundation may publish revised and/or new versions of the Lesser 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. 151 | 152 | Each version is given a distinguishing version number. If the Library specifies a version number of this 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 Library does not specify a license version number, you may choose any version ever published by the Free Software Foundation. 153 | 154 | 14. If you wish to incorporate parts of the Library into other free programs whose distribution conditions are incompatible with these, 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. 155 | 156 | NO WARRANTY 157 | 158 | 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE LIBRARY "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 LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 159 | 160 | 16. 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 LIBRARY 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 LIBRARY (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 LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 161 | 162 | 163 | END OF TERMS AND CONDITIONS 164 | How to Apply These Terms to Your New Libraries 165 | If you develop a new library, and you want it to be of the greatest possible use to the public, we recommend making it free software that everyone can redistribute and change. You can do so by permitting redistribution under these terms (or, alternatively, under the terms of the ordinary General Public License). 166 | 167 | To apply these terms, attach the following notices to the library. 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. 168 | 169 | 170 | one line to give the library's name and an idea of what it does. 171 | Copyright (C) year name of author 172 | 173 | This library is free software; you can redistribute it and/or 174 | modify it under the terms of the GNU Lesser General Public 175 | License as published by the Free Software Foundation; either 176 | version 2.1 of the License, or (at your option) any later version. 177 | 178 | This library is distributed in the hope that it will be useful, 179 | but WITHOUT ANY WARRANTY; without even the implied warranty of 180 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 181 | Lesser General Public License for more details. 182 | 183 | You should have received a copy of the GNU Lesser General Public 184 | License along with this library; if not, write to the Free Software 185 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 186 | 187 | Also add information on how to contact you by electronic and paper mail. 188 | 189 | You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the library, if necessary. Here is a sample; alter the names: 190 | 191 | 192 | Yoyodyne, Inc., hereby disclaims all copyright interest in 193 | the library `Frob' (a library for tweaking knobs) written 194 | by James Random Hacker. 195 | 196 | signature of Ty Coon, 1 April 1990 197 | Ty Coon, President of Vice 198 | 199 | That's all there is to it! 200 | -------------------------------------------------------------------------------- /META: -------------------------------------------------------------------------------- 1 | description = "Inspect and dump values on the OCaml heap." 2 | version = "0.2.1" 3 | archive(byte) = "inspect.cma" 4 | archive(native) = "inspect.cmxa" -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | .DEFAULT: all 2 | 3 | OCAMLMAKEFILE = ./OCamlMakefile 4 | 5 | RESULT = inspect 6 | 7 | OCAMLFLAGS = -w Aelz 8 | LIB_PACK_NAME = $(RESULT) 9 | SOURCES = aux.mli aux.ml value.mli value.ml value_cimpl.c dot.mli dot.ml sexpr.mli sexpr.ml 10 | 11 | OCAMLDOCFLAGS = -t "Caml-Inspect" 12 | DOC_FILES = aux.mli value.mli dot.mli sexpr.mli 13 | 14 | LIBINSTALL_FILES = aux.mli value.mli dot.mli sexpr.mli aux.cmi value.cmi dot.cmi sexpr.cmi $(RESULT).cmi $(RESULT).cma $(RESULT).cmxa $(RESULT).a lib$(RESULT)_stubs.a dll$(RESULT)_stubs.so 15 | 16 | all:ncl bcl htdoc 17 | install:libinstall 18 | uninstall:libuninstall 19 | 20 | -include $(OCAMLMAKEFILE) 21 | -------------------------------------------------------------------------------- /OCamlMakefile: -------------------------------------------------------------------------------- 1 | ########################################################################### 2 | # OCamlMakefile 3 | # Copyright (C) 1999- Markus Mottl 4 | # 5 | # For updates see: 6 | # http://www.ocaml.info/home/ocaml_sources.html 7 | # 8 | ########################################################################### 9 | 10 | # Modified by damien for .glade.ml compilation 11 | 12 | # Set these variables to the names of the sources to be processed and 13 | # the result variable. Order matters during linkage! 14 | 15 | ifndef SOURCES 16 | SOURCES := foo.ml 17 | endif 18 | export SOURCES 19 | 20 | ifndef RES_CLIB_SUF 21 | RES_CLIB_SUF := _stubs 22 | endif 23 | export RES_CLIB_SUF 24 | 25 | ifndef RESULT 26 | RESULT := foo 27 | endif 28 | export RESULT := $(strip $(RESULT)) 29 | 30 | export LIB_PACK_NAME 31 | 32 | ifndef DOC_FILES 33 | DOC_FILES := $(filter %.mli, $(SOURCES)) 34 | endif 35 | export DOC_FILES 36 | FIRST_DOC_FILE := $(firstword $(DOC_FILES)) 37 | 38 | export BCSUFFIX 39 | export NCSUFFIX 40 | 41 | ifndef TOPSUFFIX 42 | TOPSUFFIX := .top 43 | endif 44 | export TOPSUFFIX 45 | 46 | # Eventually set include- and library-paths, libraries to link, 47 | # additional compilation-, link- and ocamlyacc-flags 48 | # Path- and library information needs not be written with "-I" and such... 49 | # Define THREADS if you need it, otherwise leave it unset (same for 50 | # USE_CAMLP4)! 51 | 52 | export THREADS 53 | export VMTHREADS 54 | export ANNOTATE 55 | export USE_CAMLP4 56 | 57 | export INCDIRS 58 | export LIBDIRS 59 | export EXTLIBDIRS 60 | export RESULTDEPS 61 | export OCAML_DEFAULT_DIRS 62 | 63 | export LIBS 64 | export CLIBS 65 | export CFRAMEWORKS 66 | 67 | export OCAMLFLAGS 68 | export OCAMLNCFLAGS 69 | export OCAMLBCFLAGS 70 | 71 | export OCAMLLDFLAGS 72 | export OCAMLNLDFLAGS 73 | export OCAMLBLDFLAGS 74 | 75 | export OCAMLMKLIB_FLAGS 76 | 77 | ifndef OCAMLCPFLAGS 78 | OCAMLCPFLAGS := a 79 | endif 80 | export OCAMLCPFLAGS 81 | 82 | ifndef DOC_DIR 83 | DOC_DIR := doc 84 | endif 85 | export DOC_DIR 86 | 87 | export PPFLAGS 88 | 89 | export LFLAGS 90 | export YFLAGS 91 | export IDLFLAGS 92 | 93 | export OCAMLDOCFLAGS 94 | 95 | export OCAMLFIND_INSTFLAGS 96 | 97 | export DVIPSFLAGS 98 | 99 | export STATIC 100 | 101 | # Add a list of optional trash files that should be deleted by "make clean" 102 | export TRASH 103 | 104 | ECHO := echo 105 | 106 | ifdef REALLY_QUIET 107 | export REALLY_QUIET 108 | ECHO := true 109 | LFLAGS := $(LFLAGS) -q 110 | YFLAGS := $(YFLAGS) -q 111 | endif 112 | 113 | #################### variables depending on your OCaml-installation 114 | 115 | SYSTEM := $(shell ocamlc -config 2>/dev/null | grep system | sed 's/system: //') 116 | # This may be 117 | # - mingw 118 | # - win32 119 | # - cygwin 120 | # - some other string means Unix 121 | # - empty means ocamlc does not support -config 122 | 123 | ifeq ($(SYSTEM),mingw) 124 | MINGW=1 125 | endif 126 | ifeq ($(SYSTEM),win32) 127 | MSVC=1 128 | endif 129 | 130 | ifdef MINGW 131 | export MINGW 132 | WIN32 := 1 133 | # We are compiling with cygwin tools: 134 | CFLAGS_WIN32 := -mno-cygwin 135 | # The default value 'cc' makes 'ocamlc -cc "cc"' raises the error 'The 136 | # NTVDM CPU has encountered an illegal instruction'. 137 | CC := gcc 138 | # The OCaml C header files use this flag: 139 | CFLAGS += -D__MINGW32__ 140 | endif 141 | ifdef MSVC 142 | export MSVC 143 | WIN32 := 1 144 | ifndef STATIC 145 | CPPFLAGS_WIN32 := -DCAML_DLL 146 | endif 147 | CFLAGS_WIN32 += -nologo 148 | EXT_OBJ := obj 149 | EXT_LIB := lib 150 | ifeq ($(CC),gcc) 151 | # work around GNU Make default value 152 | ifdef THREADS 153 | CC := cl -MT 154 | else 155 | CC := cl 156 | endif 157 | endif 158 | ifeq ($(CXX),g++) 159 | # work around GNU Make default value 160 | CXX := $(CC) 161 | endif 162 | CFLAG_O := -Fo 163 | endif 164 | ifdef WIN32 165 | EXT_CXX := cpp 166 | EXE := .exe 167 | endif 168 | 169 | ifndef EXT_OBJ 170 | EXT_OBJ := o 171 | endif 172 | ifndef EXT_LIB 173 | EXT_LIB := a 174 | endif 175 | ifndef EXT_CXX 176 | EXT_CXX := cc 177 | endif 178 | ifndef EXE 179 | EXE := # empty 180 | endif 181 | ifndef CFLAG_O 182 | CFLAG_O := -o # do not delete this comment (preserves trailing whitespace)! 183 | endif 184 | 185 | export CC 186 | export CXX 187 | export CFLAGS 188 | export CXXFLAGS 189 | export LDFLAGS 190 | export CPPFLAGS 191 | 192 | ifndef RPATH_FLAG 193 | ifdef ELF_RPATH_FLAG 194 | RPATH_FLAG := $(ELF_RPATH_FLAG) 195 | else 196 | RPATH_FLAG := -R 197 | endif 198 | endif 199 | export RPATH_FLAG 200 | 201 | ifndef MSVC 202 | ifndef PIC_CFLAGS 203 | PIC_CFLAGS := -fPIC 204 | endif 205 | ifndef PIC_CPPFLAGS 206 | PIC_CPPFLAGS := -DPIC 207 | endif 208 | endif 209 | 210 | export PIC_CFLAGS 211 | export PIC_CPPFLAGS 212 | 213 | BCRESULT := $(addsuffix $(BCSUFFIX), $(RESULT)) 214 | NCRESULT := $(addsuffix $(NCSUFFIX), $(RESULT)) 215 | TOPRESULT := $(addsuffix $(TOPSUFFIX), $(RESULT)) 216 | 217 | ifndef OCAMLFIND 218 | OCAMLFIND := ocamlfind 219 | endif 220 | export OCAMLFIND 221 | 222 | ifndef OCAMLC 223 | OCAMLC := ocamlc 224 | endif 225 | export OCAMLC 226 | 227 | ifndef OCAMLOPT 228 | OCAMLOPT := ocamlopt 229 | endif 230 | export OCAMLOPT 231 | 232 | ifndef OCAMLMKTOP 233 | OCAMLMKTOP := ocamlmktop 234 | endif 235 | export OCAMLMKTOP 236 | 237 | ifndef OCAMLCP 238 | OCAMLCP := ocamlcp 239 | endif 240 | export OCAMLCP 241 | 242 | ifndef OCAMLDEP 243 | OCAMLDEP := ocamldep 244 | endif 245 | export OCAMLDEP 246 | 247 | ifndef OCAMLLEX 248 | OCAMLLEX := ocamllex 249 | endif 250 | export OCAMLLEX 251 | 252 | ifndef OCAMLYACC 253 | OCAMLYACC := ocamlyacc 254 | endif 255 | export OCAMLYACC 256 | 257 | ifndef OCAMLMKLIB 258 | OCAMLMKLIB := ocamlmklib 259 | endif 260 | export OCAMLMKLIB 261 | 262 | ifndef OCAML_GLADECC 263 | OCAML_GLADECC := lablgladecc2 264 | endif 265 | export OCAML_GLADECC 266 | 267 | ifndef OCAML_GLADECC_FLAGS 268 | OCAML_GLADECC_FLAGS := 269 | endif 270 | export OCAML_GLADECC_FLAGS 271 | 272 | ifndef CAMELEON_REPORT 273 | CAMELEON_REPORT := report 274 | endif 275 | export CAMELEON_REPORT 276 | 277 | ifndef CAMELEON_REPORT_FLAGS 278 | CAMELEON_REPORT_FLAGS := 279 | endif 280 | export CAMELEON_REPORT_FLAGS 281 | 282 | ifndef CAMELEON_ZOGGY 283 | CAMELEON_ZOGGY := camlp4o pa_zog.cma pr_o.cmo 284 | endif 285 | export CAMELEON_ZOGGY 286 | 287 | ifndef CAMELEON_ZOGGY_FLAGS 288 | CAMELEON_ZOGGY_FLAGS := 289 | endif 290 | export CAMELEON_ZOGGY_FLAGS 291 | 292 | ifndef OXRIDL 293 | OXRIDL := oxridl 294 | endif 295 | export OXRIDL 296 | 297 | ifndef CAMLIDL 298 | CAMLIDL := camlidl 299 | endif 300 | export CAMLIDL 301 | 302 | ifndef CAMLIDLDLL 303 | CAMLIDLDLL := camlidldll 304 | endif 305 | export CAMLIDLDLL 306 | 307 | ifndef NOIDLHEADER 308 | MAYBE_IDL_HEADER := -header 309 | endif 310 | export NOIDLHEADER 311 | 312 | export NO_CUSTOM 313 | 314 | ifndef CAMLP4 315 | CAMLP4 := camlp4 316 | endif 317 | export CAMLP4 318 | 319 | ifndef REAL_OCAMLFIND 320 | ifdef PACKS 321 | ifndef CREATE_LIB 322 | ifdef THREADS 323 | PACKS += threads 324 | endif 325 | endif 326 | empty := 327 | space := $(empty) $(empty) 328 | comma := , 329 | ifdef PREDS 330 | PRE_OCAML_FIND_PREDICATES := $(subst $(space),$(comma),$(PREDS)) 331 | PRE_OCAML_FIND_PACKAGES := $(subst $(space),$(comma),$(PACKS)) 332 | OCAML_FIND_PREDICATES := -predicates $(PRE_OCAML_FIND_PREDICATES) 333 | # OCAML_DEP_PREDICATES := -syntax $(PRE_OCAML_FIND_PREDICATES) 334 | OCAML_FIND_PACKAGES := $(OCAML_FIND_PREDICATES) -package $(PRE_OCAML_FIND_PACKAGES) 335 | OCAML_DEP_PACKAGES := $(OCAML_DEP_PREDICATES) -package $(PRE_OCAML_FIND_PACKAGES) 336 | else 337 | OCAML_FIND_PACKAGES := -package $(subst $(space),$(comma),$(PACKS)) 338 | OCAML_DEP_PACKAGES := 339 | endif 340 | OCAML_FIND_LINKPKG := -linkpkg 341 | REAL_OCAMLFIND := $(OCAMLFIND) 342 | endif 343 | endif 344 | 345 | export OCAML_FIND_PACKAGES 346 | export OCAML_DEP_PACKAGES 347 | export OCAML_FIND_LINKPKG 348 | export REAL_OCAMLFIND 349 | 350 | ifndef OCAMLDOC 351 | OCAMLDOC := ocamldoc 352 | endif 353 | export OCAMLDOC 354 | 355 | ifndef LATEX 356 | LATEX := latex 357 | endif 358 | export LATEX 359 | 360 | ifndef DVIPS 361 | DVIPS := dvips 362 | endif 363 | export DVIPS 364 | 365 | ifndef PS2PDF 366 | PS2PDF := ps2pdf 367 | endif 368 | export PS2PDF 369 | 370 | ifndef OCAMLMAKEFILE 371 | OCAMLMAKEFILE := OCamlMakefile 372 | endif 373 | export OCAMLMAKEFILE 374 | 375 | ifndef OCAMLLIBPATH 376 | OCAMLLIBPATH := \ 377 | $(shell $(OCAMLC) 2>/dev/null -where || echo /usr/local/lib/ocaml) 378 | endif 379 | export OCAMLLIBPATH 380 | 381 | ifndef OCAML_LIB_INSTALL 382 | OCAML_LIB_INSTALL := $(OCAMLLIBPATH)/contrib 383 | endif 384 | export OCAML_LIB_INSTALL 385 | 386 | ########################################################################### 387 | 388 | #################### change following sections only if 389 | #################### you know what you are doing! 390 | 391 | # delete target files when a build command fails 392 | .PHONY: .DELETE_ON_ERROR 393 | .DELETE_ON_ERROR: 394 | 395 | # for pedants using "--warn-undefined-variables" 396 | export MAYBE_IDL 397 | export REAL_RESULT 398 | export CAMLIDLFLAGS 399 | export THREAD_FLAG 400 | export RES_CLIB 401 | export MAKEDLL 402 | export ANNOT_FLAG 403 | export C_OXRIDL 404 | export SUBPROJS 405 | export CFLAGS_WIN32 406 | export CPPFLAGS_WIN32 407 | 408 | INCFLAGS := 409 | 410 | SHELL := /bin/sh 411 | 412 | MLDEPDIR := ._d 413 | BCDIDIR := ._bcdi 414 | NCDIDIR := ._ncdi 415 | 416 | FILTER_EXTNS := %.mli %.ml %.mll %.mly %.idl %.oxridl %.c %.m %.$(EXT_CXX) %.rep %.zog %.glade 417 | 418 | FILTERED := $(filter $(FILTER_EXTNS), $(SOURCES)) 419 | SOURCE_DIRS := $(filter-out ./, $(sort $(dir $(FILTERED)))) 420 | 421 | FILTERED_REP := $(filter %.rep, $(FILTERED)) 422 | DEP_REP := $(FILTERED_REP:%.rep=$(MLDEPDIR)/%.d) 423 | AUTO_REP := $(FILTERED_REP:.rep=.ml) 424 | 425 | FILTERED_ZOG := $(filter %.zog, $(FILTERED)) 426 | DEP_ZOG := $(FILTERED_ZOG:%.zog=$(MLDEPDIR)/%.d) 427 | AUTO_ZOG := $(FILTERED_ZOG:.zog=.ml) 428 | 429 | FILTERED_GLADE := $(filter %.glade, $(FILTERED)) 430 | DEP_GLADE := $(FILTERED_GLADE:%.glade=$(MLDEPDIR)/%.d) 431 | AUTO_GLADE := $(FILTERED_GLADE:.glade=.ml) 432 | 433 | FILTERED_ML := $(filter %.ml, $(FILTERED)) 434 | DEP_ML := $(FILTERED_ML:%.ml=$(MLDEPDIR)/%.d) 435 | 436 | FILTERED_MLI := $(filter %.mli, $(FILTERED)) 437 | DEP_MLI := $(FILTERED_MLI:.mli=.di) 438 | 439 | FILTERED_MLL := $(filter %.mll, $(FILTERED)) 440 | DEP_MLL := $(FILTERED_MLL:%.mll=$(MLDEPDIR)/%.d) 441 | AUTO_MLL := $(FILTERED_MLL:.mll=.ml) 442 | 443 | FILTERED_MLY := $(filter %.mly, $(FILTERED)) 444 | DEP_MLY := $(FILTERED_MLY:%.mly=$(MLDEPDIR)/%.d) $(FILTERED_MLY:.mly=.di) 445 | AUTO_MLY := $(FILTERED_MLY:.mly=.mli) $(FILTERED_MLY:.mly=.ml) 446 | 447 | FILTERED_IDL := $(filter %.idl, $(FILTERED)) 448 | DEP_IDL := $(FILTERED_IDL:%.idl=$(MLDEPDIR)/%.d) $(FILTERED_IDL:.idl=.di) 449 | C_IDL := $(FILTERED_IDL:%.idl=%_stubs.c) 450 | ifndef NOIDLHEADER 451 | C_IDL += $(FILTERED_IDL:.idl=.h) 452 | endif 453 | OBJ_C_IDL := $(FILTERED_IDL:%.idl=%_stubs.$(EXT_OBJ)) 454 | AUTO_IDL := $(FILTERED_IDL:.idl=.mli) $(FILTERED_IDL:.idl=.ml) $(C_IDL) 455 | 456 | FILTERED_OXRIDL := $(filter %.oxridl, $(FILTERED)) 457 | DEP_OXRIDL := $(FILTERED_OXRIDL:%.oxridl=$(MLDEPDIR)/%.d) $(FILTERED_OXRIDL:.oxridl=.di) 458 | AUTO_OXRIDL := $(FILTERED_OXRIDL:.oxridl=.mli) $(FILTERED_OXRIDL:.oxridl=.ml) $(C_OXRIDL) 459 | 460 | FILTERED_C_CXX := $(filter %.c %.m %.$(EXT_CXX), $(FILTERED)) 461 | OBJ_C_CXX := $(FILTERED_C_CXX:.c=.$(EXT_OBJ)) 462 | OBJ_C_CXX := $(OBJ_C_CXX:.m=.$(EXT_OBJ)) 463 | OBJ_C_CXX := $(OBJ_C_CXX:.$(EXT_CXX)=.$(EXT_OBJ)) 464 | 465 | PRE_TARGETS += $(AUTO_MLL) $(AUTO_MLY) $(AUTO_IDL) $(AUTO_OXRIDL) $(AUTO_ZOG) $(AUTO_REP) $(AUTO_GLADE) 466 | 467 | ALL_DEPS := $(DEP_ML) $(DEP_MLI) $(DEP_MLL) $(DEP_MLY) $(DEP_IDL) $(DEP_OXRIDL) $(DEP_ZOG) $(DEP_REP) $(DEP_GLADE) 468 | 469 | MLDEPS := $(filter %.d, $(ALL_DEPS)) 470 | MLIDEPS := $(filter %.di, $(ALL_DEPS)) 471 | BCDEPIS := $(MLIDEPS:%.di=$(BCDIDIR)/%.di) 472 | NCDEPIS := $(MLIDEPS:%.di=$(NCDIDIR)/%.di) 473 | 474 | ALLML := $(filter %.mli %.ml %.mll %.mly %.idl %.oxridl %.rep %.zog %.glade, $(FILTERED)) 475 | 476 | IMPLO_INTF := $(ALLML:%.mli=%.mli.__) 477 | IMPLO_INTF := $(foreach file, $(IMPLO_INTF), \ 478 | $(basename $(file)).cmi $(basename $(file)).cmo) 479 | IMPLO_INTF := $(filter-out %.mli.cmo, $(IMPLO_INTF)) 480 | IMPLO_INTF := $(IMPLO_INTF:%.mli.cmi=%.cmi) 481 | 482 | IMPLX_INTF := $(IMPLO_INTF:.cmo=.cmx) 483 | 484 | INTF := $(filter %.cmi, $(IMPLO_INTF)) 485 | IMPL_CMO := $(filter %.cmo, $(IMPLO_INTF)) 486 | IMPL_CMX := $(IMPL_CMO:.cmo=.cmx) 487 | IMPL_ASM := $(IMPL_CMO:.cmo=.asm) 488 | IMPL_S := $(IMPL_CMO:.cmo=.s) 489 | 490 | OBJ_LINK := $(OBJ_C_IDL) $(OBJ_C_CXX) 491 | OBJ_FILES := $(IMPL_CMO:.cmo=.$(EXT_OBJ)) $(OBJ_LINK) 492 | 493 | EXECS := $(addsuffix $(EXE), \ 494 | $(sort $(TOPRESULT) $(BCRESULT) $(NCRESULT))) 495 | ifdef WIN32 496 | EXECS += $(BCRESULT).dll $(NCRESULT).dll 497 | endif 498 | 499 | CLIB_BASE := $(RESULT)$(RES_CLIB_SUF) 500 | ifneq ($(strip $(OBJ_LINK)),) 501 | RES_CLIB := lib$(CLIB_BASE).$(EXT_LIB) 502 | endif 503 | 504 | ifdef WIN32 505 | DLLSONAME := dll$(CLIB_BASE).dll 506 | else 507 | DLLSONAME := dll$(CLIB_BASE).so 508 | endif 509 | 510 | NONEXECS := $(INTF) $(IMPL_CMO) $(IMPL_CMX) $(IMPL_ASM) $(IMPL_S) \ 511 | $(OBJ_FILES) $(PRE_TARGETS) $(BCRESULT).cma $(NCRESULT).cmxa \ 512 | $(NCRESULT).$(EXT_LIB) $(BCRESULT).cmi $(BCRESULT).cmo \ 513 | $(NCRESULT).cmi $(NCRESULT).cmx $(NCRESULT).$(EXT_OBJ) \ 514 | $(RES_CLIB) $(IMPL_CMO:.cmo=.annot) \ 515 | $(LIB_PACK_NAME).cmi $(LIB_PACK_NAME).cmo $(LIB_PACK_NAME).cmx \ 516 | $(LIB_PACK_NAME).$(EXT_OBJ) 517 | 518 | ifndef STATIC 519 | NONEXECS += $(DLLSONAME) 520 | endif 521 | 522 | ifndef LIBINSTALL_FILES 523 | LIBINSTALL_FILES := $(RESULT).mli $(RESULT).cmi $(RESULT).cma \ 524 | $(RESULT).cmxa $(RESULT).$(EXT_LIB) $(RES_CLIB) 525 | ifndef STATIC 526 | ifneq ($(strip $(OBJ_LINK)),) 527 | LIBINSTALL_FILES += $(DLLSONAME) 528 | endif 529 | endif 530 | endif 531 | 532 | export LIBINSTALL_FILES 533 | 534 | ifdef WIN32 535 | # some extra stuff is created while linking DLLs 536 | NONEXECS += $(BCRESULT).$(EXT_LIB) $(BCRESULT).exp $(NCRESULT).exp $(CLIB_BASE).exp $(CLIB_BASE).lib 537 | endif 538 | 539 | TARGETS := $(EXECS) $(NONEXECS) 540 | 541 | # If there are IDL-files 542 | ifneq ($(strip $(FILTERED_IDL)),) 543 | MAYBE_IDL := -cclib -lcamlidl 544 | endif 545 | 546 | ifdef USE_CAMLP4 547 | CAMLP4PATH := \ 548 | $(shell $(CAMLP4) -where 2>/dev/null || echo /usr/local/lib/camlp4) 549 | INCFLAGS := -I $(CAMLP4PATH) 550 | CINCFLAGS := -I$(CAMLP4PATH) 551 | endif 552 | 553 | DINCFLAGS := $(INCFLAGS) $(SOURCE_DIRS:%=-I %) $(OCAML_DEFAULT_DIRS:%=-I %) 554 | INCFLAGS := $(DINCFLAGS) $(INCDIRS:%=-I %) 555 | CINCFLAGS += $(SOURCE_DIRS:%=-I%) $(INCDIRS:%=-I%) $(OCAML_DEFAULT_DIRS:%=-I%) 556 | 557 | ifndef MSVC 558 | CLIBFLAGS += $(SOURCE_DIRS:%=-L%) $(LIBDIRS:%=-L%) \ 559 | $(EXTLIBDIRS:%=-L%) $(OCAML_DEFAULT_DIRS:%=-L%) 560 | 561 | ifeq ($(ELF_RPATH), yes) 562 | CLIBFLAGS += $(EXTLIBDIRS:%=-Wl,$(RPATH_FLAG)%) 563 | endif 564 | endif 565 | 566 | ifndef PROFILING 567 | INTF_OCAMLC := $(OCAMLC) 568 | else 569 | ifndef THREADS 570 | INTF_OCAMLC := $(OCAMLCP) -p $(OCAMLCPFLAGS) 571 | else 572 | # OCaml does not support profiling byte code 573 | # with threads (yet), therefore we force an error. 574 | ifndef REAL_OCAMLC 575 | $(error Profiling of multithreaded byte code not yet supported by OCaml) 576 | endif 577 | INTF_OCAMLC := $(OCAMLC) 578 | endif 579 | endif 580 | 581 | ifndef MSVC 582 | COMMON_LDFLAGS := $(LDFLAGS:%=-ccopt %) $(SOURCE_DIRS:%=-ccopt -L%) \ 583 | $(LIBDIRS:%=-ccopt -L%) $(EXTLIBDIRS:%=-ccopt -L%) \ 584 | $(EXTLIBDIRS:%=-ccopt -Wl $(OCAML_DEFAULT_DIRS:%=-ccopt -L%)) 585 | 586 | ifeq ($(ELF_RPATH),yes) 587 | COMMON_LDFLAGS += $(EXTLIBDIRS:%=-ccopt -Wl,$(RPATH_FLAG)%) 588 | endif 589 | else 590 | COMMON_LDFLAGS := -ccopt "/link -NODEFAULTLIB:LIBC $(LDFLAGS:%=%) $(SOURCE_DIRS:%=-LIBPATH:%) \ 591 | $(LIBDIRS:%=-LIBPATH:%) $(EXTLIBDIRS:%=-LIBPATH:%) \ 592 | $(OCAML_DEFAULT_DIRS:%=-LIBPATH:%) " 593 | endif 594 | 595 | CLIBS_OPTS := $(CLIBS:%=-cclib -l%) $(CFRAMEWORKS:%=-cclib '-framework %') 596 | ifdef MSVC 597 | ifndef STATIC 598 | # MSVC libraries do not have 'lib' prefix 599 | CLIBS_OPTS := $(CLIBS:%=-cclib %.lib) 600 | endif 601 | endif 602 | 603 | ifneq ($(strip $(OBJ_LINK)),) 604 | ifdef CREATE_LIB 605 | OBJS_LIBS := -cclib -l$(CLIB_BASE) $(CLIBS_OPTS) $(MAYBE_IDL) 606 | else 607 | OBJS_LIBS := $(OBJ_LINK) $(CLIBS_OPTS) $(MAYBE_IDL) 608 | endif 609 | else 610 | OBJS_LIBS := $(CLIBS_OPTS) $(MAYBE_IDL) 611 | endif 612 | 613 | ifdef LIB_PACK_NAME 614 | FOR_PACK_NAME := $(shell echo $(LIB_PACK_NAME) | awk '{print toupper(substr($$0,1,1))substr($$0,2)}') 615 | endif 616 | 617 | # If we have to make byte-code 618 | ifndef REAL_OCAMLC 619 | BYTE_OCAML := y 620 | 621 | # EXTRADEPS is added dependencies we have to insert for all 622 | # executable files we generate. Ideally it should be all of the 623 | # libraries we use, but it's hard to find the ones that get searched on 624 | # the path since I don't know the paths built into the compiler, so 625 | # just include the ones with slashes in their names. 626 | EXTRADEPS := $(addsuffix .cma,$(foreach i,$(LIBS),$(if $(findstring /,$(i)),$(i)))) 627 | 628 | 629 | ifndef LIB_PACK_NAME 630 | SPECIAL_OCAMLFLAGS := $(OCAMLBCFLAGS) 631 | else 632 | SPECIAL_OCAMLFLAGS := -for-pack $(FOR_PACK_NAME) $(OCAMLBCFLAGS) 633 | endif 634 | 635 | REAL_OCAMLC := $(INTF_OCAMLC) 636 | 637 | REAL_IMPL := $(IMPL_CMO) 638 | REAL_IMPL_INTF := $(IMPLO_INTF) 639 | IMPL_SUF := .cmo 640 | 641 | DEPFLAGS := 642 | MAKE_DEPS := $(MLDEPS) $(BCDEPIS) 643 | 644 | ifdef CREATE_LIB 645 | override CFLAGS := $(PIC_CFLAGS) $(CFLAGS) 646 | override CPPFLAGS := $(PIC_CPPFLAGS) $(CPPFLAGS) 647 | ifndef STATIC 648 | ifneq ($(strip $(OBJ_LINK)),) 649 | MAKEDLL := $(DLLSONAME) 650 | ALL_LDFLAGS := -dllib $(DLLSONAME) 651 | endif 652 | endif 653 | endif 654 | 655 | ifndef NO_CUSTOM 656 | ifneq "$(strip $(OBJ_LINK) $(THREADS) $(MAYBE_IDL) $(CLIBS) $(CFRAMEWORKS))" "" 657 | ALL_LDFLAGS += -custom 658 | endif 659 | endif 660 | 661 | ALL_LDFLAGS += $(INCFLAGS) $(OCAMLLDFLAGS) $(OCAMLBLDFLAGS) \ 662 | $(COMMON_LDFLAGS) $(LIBS:%=%.cma) 663 | CAMLIDLDLLFLAGS := 664 | 665 | ifdef THREADS 666 | ifdef VMTHREADS 667 | THREAD_FLAG := -vmthread 668 | else 669 | THREAD_FLAG := -thread 670 | endif 671 | ALL_LDFLAGS := $(THREAD_FLAG) $(ALL_LDFLAGS) 672 | ifndef CREATE_LIB 673 | ifndef REAL_OCAMLFIND 674 | ALL_LDFLAGS := unix.cma threads.cma $(ALL_LDFLAGS) 675 | endif 676 | endif 677 | endif 678 | 679 | # we have to make native-code 680 | else 681 | EXTRADEPS := $(addsuffix .cmxa,$(foreach i,$(LIBS),$(if $(findstring /,$(i)),$(i)))) 682 | ifndef PROFILING 683 | SPECIAL_OCAMLFLAGS := $(OCAMLNCFLAGS) 684 | PLDFLAGS := 685 | else 686 | SPECIAL_OCAMLFLAGS := -p $(OCAMLNCFLAGS) 687 | PLDFLAGS := -p 688 | endif 689 | 690 | ifndef LIB_PACK_NAME 691 | SPECIAL_OCAMLFLAGS := $(OCAMLNCFLAGS) 692 | else 693 | SPECIAL_OCAMLFLAGS := -for-pack $(FOR_PACK_NAME) $(OCAMLNCFLAGS) 694 | endif 695 | REAL_IMPL := $(IMPL_CMX) 696 | REAL_IMPL_INTF := $(IMPLX_INTF) 697 | IMPL_SUF := .cmx 698 | 699 | override CPPFLAGS := -DNATIVE_CODE $(CPPFLAGS) 700 | 701 | DEPFLAGS := -native 702 | MAKE_DEPS := $(MLDEPS) $(NCDEPIS) 703 | 704 | ALL_LDFLAGS := $(PLDFLAGS) $(INCFLAGS) $(OCAMLLDFLAGS) \ 705 | $(OCAMLNLDFLAGS) $(COMMON_LDFLAGS) 706 | CAMLIDLDLLFLAGS := -opt 707 | 708 | ifndef CREATE_LIB 709 | ALL_LDFLAGS += $(LIBS:%=%.cmxa) 710 | else 711 | override CFLAGS := $(PIC_CFLAGS) $(CFLAGS) 712 | override CPPFLAGS := $(PIC_CPPFLAGS) $(CPPFLAGS) 713 | endif 714 | 715 | ifdef THREADS 716 | THREAD_FLAG := -thread 717 | ALL_LDFLAGS := $(THREAD_FLAG) $(ALL_LDFLAGS) 718 | ifndef CREATE_LIB 719 | ifndef REAL_OCAMLFIND 720 | ALL_LDFLAGS := unix.cmxa threads.cmxa $(ALL_LDFLAGS) 721 | endif 722 | endif 723 | endif 724 | endif 725 | 726 | export MAKE_DEPS 727 | 728 | ifdef ANNOTATE 729 | ANNOT_FLAG := -dtypes 730 | else 731 | endif 732 | 733 | ALL_OCAMLCFLAGS := $(THREAD_FLAG) $(ANNOT_FLAG) $(OCAMLFLAGS) \ 734 | $(INCFLAGS) $(SPECIAL_OCAMLFLAGS) 735 | 736 | ifdef make_deps 737 | -include $(MAKE_DEPS) 738 | PRE_TARGETS := 739 | endif 740 | 741 | ########################################################################### 742 | # USER RULES 743 | 744 | # Call "OCamlMakefile QUIET=" to get rid of all of the @'s. 745 | QUIET=@ 746 | 747 | # generates byte-code (default) 748 | byte-code: $(PRE_TARGETS) 749 | $(QUIET)$(MAKE) -r -f $(OCAMLMAKEFILE) $(BCRESULT) \ 750 | REAL_RESULT="$(BCRESULT)" make_deps=yes 751 | bc: byte-code 752 | 753 | byte-code-nolink: $(PRE_TARGETS) 754 | $(QUIET)$(MAKE) -r -f $(OCAMLMAKEFILE) nolink \ 755 | REAL_RESULT="$(BCRESULT)" make_deps=yes 756 | bcnl: byte-code-nolink 757 | 758 | top: $(PRE_TARGETS) 759 | $(QUIET)$(MAKE) -r -f $(OCAMLMAKEFILE) $(TOPRESULT) \ 760 | REAL_RESULT="$(BCRESULT)" make_deps=yes 761 | 762 | # generates native-code 763 | 764 | native-code: $(PRE_TARGETS) 765 | $(QUIET)$(MAKE) -r -f $(OCAMLMAKEFILE) $(NCRESULT) \ 766 | REAL_RESULT="$(NCRESULT)" \ 767 | REAL_OCAMLC="$(OCAMLOPT)" \ 768 | make_deps=yes 769 | nc: native-code 770 | 771 | native-code-nolink: $(PRE_TARGETS) 772 | $(QUIET)$(MAKE) -r -f $(OCAMLMAKEFILE) nolink \ 773 | REAL_RESULT="$(NCRESULT)" \ 774 | REAL_OCAMLC="$(OCAMLOPT)" \ 775 | make_deps=yes 776 | ncnl: native-code-nolink 777 | 778 | # generates byte-code libraries 779 | byte-code-library: $(PRE_TARGETS) 780 | $(QUIET)$(MAKE) -r -f $(OCAMLMAKEFILE) \ 781 | $(RES_CLIB) $(BCRESULT).cma \ 782 | REAL_RESULT="$(BCRESULT)" \ 783 | CREATE_LIB=yes \ 784 | make_deps=yes 785 | bcl: byte-code-library 786 | 787 | # generates native-code libraries 788 | native-code-library: $(PRE_TARGETS) 789 | $(QUIET)$(MAKE) -r -f $(OCAMLMAKEFILE) \ 790 | $(RES_CLIB) $(NCRESULT).cmxa \ 791 | REAL_RESULT="$(NCRESULT)" \ 792 | REAL_OCAMLC="$(OCAMLOPT)" \ 793 | CREATE_LIB=yes \ 794 | make_deps=yes 795 | ncl: native-code-library 796 | 797 | ifdef WIN32 798 | # generates byte-code dll 799 | byte-code-dll: $(PRE_TARGETS) 800 | $(QUIET)$(MAKE) -r -f $(OCAMLMAKEFILE) \ 801 | $(RES_CLIB) $(BCRESULT).dll \ 802 | REAL_RESULT="$(BCRESULT)" \ 803 | make_deps=yes 804 | bcd: byte-code-dll 805 | 806 | # generates native-code dll 807 | native-code-dll: $(PRE_TARGETS) 808 | $(QUIET)$(MAKE) -r -f $(OCAMLMAKEFILE) \ 809 | $(RES_CLIB) $(NCRESULT).dll \ 810 | REAL_RESULT="$(NCRESULT)" \ 811 | REAL_OCAMLC="$(OCAMLOPT)" \ 812 | make_deps=yes 813 | ncd: native-code-dll 814 | endif 815 | 816 | # generates byte-code with debugging information 817 | debug-code: $(PRE_TARGETS) 818 | $(QUIET)$(MAKE) -r -f $(OCAMLMAKEFILE) $(BCRESULT) \ 819 | REAL_RESULT="$(BCRESULT)" make_deps=yes \ 820 | OCAMLFLAGS="-g $(OCAMLFLAGS)" \ 821 | OCAMLLDFLAGS="-g $(OCAMLLDFLAGS)" 822 | dc: debug-code 823 | 824 | debug-code-nolink: $(PRE_TARGETS) 825 | $(QUIET)$(MAKE) -r -f $(OCAMLMAKEFILE) nolink \ 826 | REAL_RESULT="$(BCRESULT)" make_deps=yes \ 827 | OCAMLFLAGS="-g $(OCAMLFLAGS)" \ 828 | OCAMLLDFLAGS="-g $(OCAMLLDFLAGS)" 829 | dcnl: debug-code-nolink 830 | 831 | # generates byte-code with debugging information (native code) 832 | debug-native-code: $(PRE_TARGETS) 833 | $(QUIET)$(MAKE) -r -f $(OCAMLMAKEFILE) $(NCRESULT) \ 834 | REAL_RESULT="$(NCRESULT)" make_deps=yes \ 835 | REAL_OCAMLC="$(OCAMLOPT)" \ 836 | OCAMLFLAGS="-g $(OCAMLFLAGS)" \ 837 | OCAMLLDFLAGS="-g $(OCAMLLDFLAGS)" 838 | dnc: debug-native-code 839 | 840 | debug-native-code-nolink: $(PRE_TARGETS) 841 | $(QUIET)$(MAKE) -r -f $(OCAMLMAKEFILE) nolink \ 842 | REAL_RESULT="$(NCRESULT)" make_deps=yes \ 843 | REAL_OCAMLC="$(OCAMLOPT)" \ 844 | OCAMLFLAGS="-g $(OCAMLFLAGS)" \ 845 | OCAMLLDFLAGS="-g $(OCAMLLDFLAGS)" 846 | dncnl: debug-native-code-nolink 847 | 848 | # generates byte-code libraries with debugging information 849 | debug-code-library: $(PRE_TARGETS) 850 | $(QUIET)$(MAKE) -r -f $(OCAMLMAKEFILE) \ 851 | $(RES_CLIB) $(BCRESULT).cma \ 852 | REAL_RESULT="$(BCRESULT)" make_deps=yes \ 853 | CREATE_LIB=yes \ 854 | OCAMLFLAGS="-g $(OCAMLFLAGS)" \ 855 | OCAMLLDFLAGS="-g $(OCAMLLDFLAGS)" 856 | dcl: debug-code-library 857 | 858 | # generates byte-code libraries with debugging information (native code) 859 | debug-native-code-library: $(PRE_TARGETS) 860 | $(QUIET)$(MAKE) -r -f $(OCAMLMAKEFILE) \ 861 | $(RES_CLIB) $(NCRESULT).cma \ 862 | REAL_RESULT="$(NCRESULT)" make_deps=yes \ 863 | REAL_OCAMLC="$(OCAMLOPT)" \ 864 | CREATE_LIB=yes \ 865 | OCAMLFLAGS="-g $(OCAMLFLAGS)" \ 866 | OCAMLLDFLAGS="-g $(OCAMLLDFLAGS)" 867 | dncl: debug-native-code-library 868 | 869 | # generates byte-code for profiling 870 | profiling-byte-code: $(PRE_TARGETS) 871 | $(QUIET)$(MAKE) -r -f $(OCAMLMAKEFILE) $(BCRESULT) \ 872 | REAL_RESULT="$(BCRESULT)" PROFILING="y" \ 873 | make_deps=yes 874 | pbc: profiling-byte-code 875 | 876 | # generates native-code 877 | 878 | profiling-native-code: $(PRE_TARGETS) 879 | $(QUIET)$(MAKE) -r -f $(OCAMLMAKEFILE) $(NCRESULT) \ 880 | REAL_RESULT="$(NCRESULT)" \ 881 | REAL_OCAMLC="$(OCAMLOPT)" \ 882 | PROFILING="y" \ 883 | make_deps=yes 884 | pnc: profiling-native-code 885 | 886 | # generates byte-code libraries 887 | profiling-byte-code-library: $(PRE_TARGETS) 888 | $(QUIET)$(MAKE) -r -f $(OCAMLMAKEFILE) \ 889 | $(RES_CLIB) $(BCRESULT).cma \ 890 | REAL_RESULT="$(BCRESULT)" PROFILING="y" \ 891 | CREATE_LIB=yes \ 892 | make_deps=yes 893 | pbcl: profiling-byte-code-library 894 | 895 | # generates native-code libraries 896 | profiling-native-code-library: $(PRE_TARGETS) 897 | $(QUIET)$(MAKE) -r -f $(OCAMLMAKEFILE) \ 898 | $(RES_CLIB) $(NCRESULT).cmxa \ 899 | REAL_RESULT="$(NCRESULT)" PROFILING="y" \ 900 | REAL_OCAMLC="$(OCAMLOPT)" \ 901 | CREATE_LIB=yes \ 902 | make_deps=yes 903 | pncl: profiling-native-code-library 904 | 905 | # packs byte-code objects 906 | pack-byte-code: $(PRE_TARGETS) 907 | $(QUIET)$(MAKE) -r -f $(OCAMLMAKEFILE) $(BCRESULT).cmo \ 908 | REAL_RESULT="$(BCRESULT)" \ 909 | PACK_LIB=yes make_deps=yes 910 | pabc: pack-byte-code 911 | 912 | # packs native-code objects 913 | pack-native-code: $(PRE_TARGETS) 914 | $(QUIET)$(MAKE) -r -f $(OCAMLMAKEFILE) \ 915 | $(NCRESULT).cmx $(NCRESULT).$(EXT_OBJ) \ 916 | REAL_RESULT="$(NCRESULT)" \ 917 | REAL_OCAMLC="$(OCAMLOPT)" \ 918 | PACK_LIB=yes make_deps=yes 919 | panc: pack-native-code 920 | 921 | # generates HTML-documentation 922 | htdoc: $(DOC_DIR)/$(RESULT)/html/index.html 923 | 924 | # generates Latex-documentation 925 | ladoc: $(DOC_DIR)/$(RESULT)/latex/doc.tex 926 | 927 | # generates PostScript-documentation 928 | psdoc: $(DOC_DIR)/$(RESULT)/latex/doc.ps 929 | 930 | # generates PDF-documentation 931 | pdfdoc: $(DOC_DIR)/$(RESULT)/latex/doc.pdf 932 | 933 | # generates all supported forms of documentation 934 | doc: htdoc ladoc psdoc pdfdoc 935 | 936 | ########################################################################### 937 | # LOW LEVEL RULES 938 | 939 | $(REAL_RESULT): $(REAL_IMPL_INTF) $(OBJ_LINK) $(EXTRADEPS) $(RESULTDEPS) 940 | $(REAL_OCAMLFIND) $(REAL_OCAMLC) \ 941 | $(OCAML_FIND_PACKAGES) $(OCAML_FIND_LINKPKG) \ 942 | $(ALL_LDFLAGS) $(OBJS_LIBS) -o $@$(EXE) \ 943 | $(REAL_IMPL) 944 | 945 | nolink: $(REAL_IMPL_INTF) $(OBJ_LINK) 946 | 947 | ifdef WIN32 948 | $(REAL_RESULT).dll: $(REAL_IMPL_INTF) $(OBJ_LINK) 949 | $(CAMLIDLDLL) $(CAMLIDLDLLFLAGS) $(OBJ_LINK) $(CLIBS) \ 950 | -o $@ $(REAL_IMPL) 951 | endif 952 | 953 | %$(TOPSUFFIX): $(REAL_IMPL_INTF) $(OBJ_LINK) $(EXTRADEPS) 954 | $(REAL_OCAMLFIND) $(OCAMLMKTOP) \ 955 | $(OCAML_FIND_PACKAGES) $(OCAML_FIND_LINKPKG) \ 956 | $(ALL_LDFLAGS) $(OBJS_LIBS) -o $@$(EXE) \ 957 | $(REAL_IMPL) 958 | 959 | .SUFFIXES: .mli .ml .cmi .cmo .cmx .cma .cmxa .$(EXT_OBJ) \ 960 | .mly .di .d .$(EXT_LIB) .idl %.oxridl .c .m .$(EXT_CXX) .h .so \ 961 | .rep .zog .glade 962 | 963 | ifndef STATIC 964 | ifdef MINGW 965 | # From OCaml 3.11.0, ocamlmklib is available on windows 966 | OCAMLMLIB_EXISTS = $(shell which $(OCAMLMKLIB)) 967 | ifeq ($(strip $(OCAMLMLIB_EXISTS)),) 968 | $(DLLSONAME): $(OBJ_LINK) 969 | $(CC) $(CFLAGS) $(CFLAGS_WIN32) $(OBJ_LINK) -shared -o $@ \ 970 | $(wildcard $(foreach dir,$(LIBDIRS),$(CLIBS:%=$(dir)/lib%.a))) \ 971 | '$(OCAMLLIBPATH)/ocamlrun.a' \ 972 | -Wl,--whole-archive \ 973 | -Wl,--export-all-symbols \ 974 | -Wl,--allow-multiple-definition \ 975 | -Wl,--enable-auto-import 976 | else 977 | $(DLLSONAME): $(OBJ_LINK) 978 | $(OCAMLMKLIB) $(INCFLAGS) $(CLIBFLAGS) \ 979 | -o $(CLIB_BASE) $(OBJ_LINK) $(CLIBS:%=-l%) \ 980 | $(CFRAMEWORKS:%=-framework %) \ 981 | $(OCAMLMKLIB_FLAGS) 982 | endif 983 | else 984 | ifdef MSVC 985 | $(DLLSONAME): $(OBJ_LINK) 986 | link /NOLOGO /DLL /OUT:$@ $(OBJ_LINK) \ 987 | $(wildcard $(foreach dir,$(LIBDIRS),$(CLIBS:%=$(dir)/%.lib))) \ 988 | '$(OCAMLLIBPATH)/ocamlrun.lib' 989 | 990 | else 991 | $(DLLSONAME): $(OBJ_LINK) 992 | $(OCAMLMKLIB) $(INCFLAGS) $(CLIBFLAGS) \ 993 | -o $(CLIB_BASE) $(OBJ_LINK) $(CLIBS:%=-l%) $(CFRAMEWORKS:%=-framework %) \ 994 | $(OCAMLMKLIB_FLAGS) 995 | endif 996 | endif 997 | endif 998 | 999 | ifndef LIB_PACK_NAME 1000 | $(RESULT).cma: $(REAL_IMPL_INTF) $(MAKEDLL) $(EXTRADEPS) $(RESULTDEPS) 1001 | $(REAL_OCAMLFIND) $(REAL_OCAMLC) -a $(ALL_LDFLAGS) $(OBJS_LIBS) -o $@ $(REAL_IMPL) 1002 | 1003 | $(RESULT).cmxa $(RESULT).$(EXT_LIB): $(REAL_IMPL_INTF) $(EXTRADEPS) $(RESULTDEPS) 1004 | $(REAL_OCAMLFIND) $(OCAMLOPT) -a $(ALL_LDFLAGS) $(OBJS_LIBS) -o $@ $(REAL_IMPL) 1005 | else 1006 | # Packing a bytecode library 1007 | ifdef BYTE_OCAML 1008 | $(LIB_PACK_NAME).cmi $(LIB_PACK_NAME).cmo: $(REAL_IMPL_INTF) 1009 | $(REAL_OCAMLFIND) $(REAL_OCAMLC) -pack -o $(LIB_PACK_NAME).cmo $(OCAMLLDFLAGS) $(REAL_IMPL) 1010 | # Packing into a unit which can be transformed into a library 1011 | # Remember the .ml's must have been compiled with -for-pack $(LIB_PACK_NAME) 1012 | else 1013 | $(LIB_PACK_NAME).cmi $(LIB_PACK_NAME).cmx: $(REAL_IMPL_INTF) 1014 | $(REAL_OCAMLFIND) $(OCAMLOPT) -pack -o $(LIB_PACK_NAME).cmx $(OCAMLLDFLAGS) $(REAL_IMPL) 1015 | endif 1016 | 1017 | $(RESULT).cma: $(LIB_PACK_NAME).cmi $(LIB_PACK_NAME).cmo $(MAKEDLL) $(EXTRADEPS) $(RESULTDEPS) 1018 | $(REAL_OCAMLFIND) $(REAL_OCAMLC) -a $(ALL_LDFLAGS) $(OBJS_LIBS) -o $@ $(LIB_PACK_NAME).cmo 1019 | 1020 | $(RESULT).cmxa $(RESULT).$(EXT_LIB): $(LIB_PACK_NAME).cmi $(LIB_PACK_NAME).cmx $(EXTRADEPS) $(RESULTDEPS) 1021 | $(REAL_OCAMLFIND) $(OCAMLOPT) -a $(filter-out -custom, $(ALL_LDFLAGS)) $(OBJS_LIBS) -o $@ $(LIB_PACK_NAME).cmx 1022 | endif 1023 | 1024 | $(RES_CLIB): $(OBJ_LINK) 1025 | ifndef MSVC 1026 | ifneq ($(strip $(OBJ_LINK)),) 1027 | $(AR) rcs $@ $(OBJ_LINK) 1028 | endif 1029 | else 1030 | ifneq ($(strip $(OBJ_LINK)),) 1031 | lib -nologo -debugtype:cv -out:$(RES_CLIB) $(OBJ_LINK) 1032 | endif 1033 | endif 1034 | 1035 | .mli.cmi: $(EXTRADEPS) 1036 | $(QUIET)pp=`sed -n -e '/^#/d' -e 's/(\*pp \([^*]*\) \*)/\1/p;q' $<`; \ 1037 | if [ -z "$$pp" ]; then \ 1038 | $(ECHO) $(REAL_OCAMLFIND) $(INTF_OCAMLC) $(OCAML_FIND_PACKAGES) \ 1039 | -c $(THREAD_FLAG) $(ANNOT_FLAG) \ 1040 | $(OCAMLFLAGS) $(INCFLAGS) $<; \ 1041 | $(REAL_OCAMLFIND) $(INTF_OCAMLC) $(OCAML_FIND_PACKAGES) \ 1042 | -c $(THREAD_FLAG) $(ANNOT_FLAG) \ 1043 | $(OCAMLFLAGS) $(INCFLAGS) $<; \ 1044 | else \ 1045 | $(ECHO) $(REAL_OCAMLFIND) $(INTF_OCAMLC) $(OCAML_FIND_PACKAGES) \ 1046 | -c -pp \"$$pp $(PPFLAGS)\" $(THREAD_FLAG) $(ANNOT_FLAG) \ 1047 | $(OCAMLFLAGS) $(INCFLAGS) $<; \ 1048 | $(REAL_OCAMLFIND) $(INTF_OCAMLC) $(OCAML_FIND_PACKAGES) \ 1049 | -c -pp "$$pp $(PPFLAGS)" $(THREAD_FLAG) $(ANNOT_FLAG) \ 1050 | $(OCAMLFLAGS) $(INCFLAGS) $<; \ 1051 | fi 1052 | 1053 | .ml.cmi .ml.$(EXT_OBJ) .ml.cmx .ml.cmo: $(EXTRADEPS) 1054 | $(QUIET)pp=`sed -n -e '/^#/d' -e 's/(\*pp \([^*]*\) \*)/\1/p;q' $<`; \ 1055 | if [ -z "$$pp" ]; then \ 1056 | $(ECHO) $(REAL_OCAMLFIND) $(REAL_OCAMLC) $(OCAML_FIND_PACKAGES) \ 1057 | -c $(ALL_OCAMLCFLAGS) $<; \ 1058 | $(REAL_OCAMLFIND) $(REAL_OCAMLC) $(OCAML_FIND_PACKAGES) \ 1059 | -c $(ALL_OCAMLCFLAGS) $<; \ 1060 | else \ 1061 | $(ECHO) $(REAL_OCAMLFIND) $(REAL_OCAMLC) $(OCAML_FIND_PACKAGES) \ 1062 | -c -pp \"$$pp $(PPFLAGS)\" $(ALL_OCAMLCFLAGS) $<; \ 1063 | $(REAL_OCAMLFIND) $(REAL_OCAMLC) $(OCAML_FIND_PACKAGES) \ 1064 | -c -pp "$$pp $(PPFLAGS)" $(ALL_OCAMLCFLAGS) $<; \ 1065 | fi 1066 | 1067 | .PRECIOUS: %.ml 1068 | %.ml: %.mll 1069 | $(OCAMLLEX) $(LFLAGS) $< 1070 | 1071 | .PRECIOUS: %.ml %.mli 1072 | %.ml %.mli: %.mly 1073 | $(OCAMLYACC) $(YFLAGS) $< 1074 | $(QUIET)pp=`sed -n -e 's/.*(\*pp \([^*]*\) \*).*/\1/p;q' $<`; \ 1075 | if [ ! -z "$$pp" ]; then \ 1076 | mv $*.ml $*.ml.temporary; \ 1077 | echo "(*pp $$pp $(PPFLAGS)*)" > $*.ml; \ 1078 | cat $*.ml.temporary >> $*.ml; \ 1079 | rm $*.ml.temporary; \ 1080 | mv $*.mli $*.mli.temporary; \ 1081 | echo "(*pp $$pp $(PPFLAGS)*)" > $*.mli; \ 1082 | cat $*.mli.temporary >> $*.mli; \ 1083 | rm $*.mli.temporary; \ 1084 | fi 1085 | 1086 | 1087 | .PRECIOUS: %.ml 1088 | %.ml: %.rep 1089 | $(CAMELEON_REPORT) $(CAMELEON_REPORT_FLAGS) -gen $< 1090 | 1091 | .PRECIOUS: %.ml 1092 | %.ml: %.zog 1093 | $(CAMELEON_ZOGGY) $(CAMELEON_ZOGGY_FLAGS) -impl $< > $@ 1094 | 1095 | .PRECIOUS: %.ml 1096 | %.ml: %.glade 1097 | $(OCAML_GLADECC) $(OCAML_GLADECC_FLAGS) $< > $@ 1098 | 1099 | .PRECIOUS: %.ml %.mli 1100 | %.ml %.mli: %.oxridl 1101 | $(OXRIDL) $< 1102 | 1103 | .PRECIOUS: %.ml %.mli %_stubs.c %.h 1104 | %.ml %.mli %_stubs.c %.h: %.idl 1105 | $(CAMLIDL) $(MAYBE_IDL_HEADER) $(IDLFLAGS) \ 1106 | $(CAMLIDLFLAGS) $< 1107 | $(QUIET)if [ $(NOIDLHEADER) ]; then touch $*.h; fi 1108 | 1109 | .c.$(EXT_OBJ): 1110 | $(OCAMLC) -c -cc "$(CC)" -ccopt "$(CFLAGS) \ 1111 | $(CPPFLAGS) $(CPPFLAGS_WIN32) \ 1112 | $(CFLAGS_WIN32) $(CINCFLAGS) $(CFLAG_O)$@ " $< 1113 | 1114 | .m.$(EXT_OBJ): 1115 | $(CC) -c $(CFLAGS) $(CINCFLAGS) $(CPPFLAGS) \ 1116 | -I'$(OCAMLLIBPATH)' \ 1117 | $< $(CFLAG_O)$@ 1118 | 1119 | .$(EXT_CXX).$(EXT_OBJ): 1120 | $(CXX) -c $(CXXFLAGS) $(CINCFLAGS) $(CPPFLAGS) \ 1121 | -I'$(OCAMLLIBPATH)' \ 1122 | $< $(CFLAG_O)$@ 1123 | 1124 | $(MLDEPDIR)/%.d: %.ml 1125 | $(QUIET)if [ ! -d $(@D) ]; then mkdir -p $(@D); fi 1126 | $(QUIET)pp=`sed -n -e '/^#/d' -e 's/(\*pp \([^*]*\) \*)/\1/p;q' $<`; \ 1127 | if [ -z "$$pp" ]; then \ 1128 | $(ECHO) $(REAL_OCAMLFIND) $(OCAMLDEP) $(OCAML_DEP_PACKAGES) \ 1129 | $(DINCFLAGS) $< \> $@; \ 1130 | $(REAL_OCAMLFIND) $(OCAMLDEP) $(OCAML_DEP_PACKAGES) \ 1131 | $(DINCFLAGS) $< > $@; \ 1132 | else \ 1133 | $(ECHO) $(REAL_OCAMLFIND) $(OCAMLDEP) $(OCAML_DEP_PACKAGES) \ 1134 | -pp \"$$pp $(PPFLAGS)\" $(DINCFLAGS) $< \> $@; \ 1135 | $(REAL_OCAMLFIND) $(OCAMLDEP) $(OCAML_DEP_PACKAGES) \ 1136 | -pp "$$pp $(PPFLAGS)" $(DINCFLAGS) $< > $@; \ 1137 | fi 1138 | 1139 | $(BCDIDIR)/%.di $(NCDIDIR)/%.di: %.mli 1140 | $(QUIET)if [ ! -d $(@D) ]; then mkdir -p $(@D); fi 1141 | $(QUIET)pp=`sed -n -e '/^#/d' -e 's/(\*pp \([^*]*\) \*)/\1/p;q' $<`; \ 1142 | if [ -z "$$pp" ]; then \ 1143 | $(ECHO) $(REAL_OCAMLFIND) $(OCAMLDEP) $(DEPFLAGS) $(DINCFLAGS) $< \> $@; \ 1144 | $(REAL_OCAMLFIND) $(OCAMLDEP) $(DEPFLAGS) $(DINCFLAGS) $< > $@; \ 1145 | else \ 1146 | $(ECHO) $(REAL_OCAMLFIND) $(OCAMLDEP) $(DEPFLAGS) \ 1147 | -pp \"$$pp $(PPFLAGS)\" $(DINCFLAGS) $< \> $@; \ 1148 | $(REAL_OCAMLFIND) $(OCAMLDEP) $(DEPFLAGS) \ 1149 | -pp "$$pp $(PPFLAGS)" $(DINCFLAGS) $< > $@; \ 1150 | fi 1151 | 1152 | $(DOC_DIR)/$(RESULT)/html: 1153 | mkdir -p $@ 1154 | 1155 | $(DOC_DIR)/$(RESULT)/html/index.html: $(DOC_DIR)/$(RESULT)/html $(DOC_FILES) 1156 | rm -rf $) is the original author 12 | and currently also the maintainer of this library. 13 | 14 | Installation 15 | ---------------------------------------------------------------------- 16 | 17 | If you have OPAM installed, you should be able to simply do 18 | 19 | opam install inspect 20 | 21 | If that should not be the case, either download an apropriate source 22 | archive form the [project homepage][Homepage] and unzip or untar in 23 | any directory, or clone the [git repository][GitRepository], then run 24 | 25 | make 26 | 27 | to generate the library and documentation. 28 | To install the library using findlib, simply type 29 | 30 | make install 31 | 32 | And to uninstall it 33 | 34 | make uninstall 35 | 36 | For development, you may instead run 37 | 38 | sudo ln -s `pwd` `ocamlfind printconf path`/inspect 39 | 40 | Usage 41 | ---------------------------------------------------------------------- 42 | 43 | If you have findlib installed, using the library is as simple as 44 | typing 45 | 46 | #use "topfind";; 47 | #require "inspect";; 48 | 49 | into your OCaml prompt. I suggest you open the `Inspect` module as well. 50 | 51 | open Inspect;; 52 | 53 | For starters, both the `Dot` and the `Sexpr` library provide a test_data 54 | function to generate some interesting data to dump. 55 | 56 | Sexpr.dump (Sexpr.test_data ());; 57 | Dot.dump (Dot.test_data ());; 58 | 59 | It is possible to let the dump functions inspect themselves but I'm 60 | less and less sure that this is a good idea, as there's a lot of 61 | mutation going on behind closed doors, which might confuse the current 62 | object graph walker. 63 | 64 | On any platform, you should be able to dump values 65 | 66 | If you are on a Mac, the `Inspect.Dot.dump_osx` function should be of 67 | interest. It writes the DOT output to a temporary file, uses Graphviz 68 | to render the graph as a pdf document, and displays it using the open 69 | command. 70 | 71 | Dot.dump_osx (Dot.test_data ());; 72 | 73 | It goes without saying that you should have [Graphviz][] installed for 74 | this last part to work. 75 | 76 | Representation of OCaml Values 77 | ---------------------------------------------------------------------- 78 | 79 | OCaml values all share a common low-level representation. In contrast 80 | to dynamically typed languages, it is usually not possible to infer 81 | the type of a value from the low-level representation, because several 82 | distinct OCaml types can share the same representation. This is not 83 | really an issue, because OCaml is statically typed after all. So 84 | except for some trickery with `Obj.magic`, it is simply not possible 85 | to apply a function to a value of the wrong type. 86 | 87 | ### The Value Type 88 | 89 | The basic building block that is used by the runtime-system (which is 90 | written in the C programming language) to represent any value in the 91 | OCaml universe is the value type. Values are always word-sized. A word 92 | is either 32 or 64 bits wide, depending on the architecture (see 93 | `Sys.word_size`). 94 | 95 | A value can either be a pointer to a block of values in the OCaml 96 | heap, a pointer to an object outside of the heap, or an unboxed 97 | integer. Naturally, blocks in the heap are garbage-collected. 98 | 99 | To distinguish between unboxed integers and pointers, the system uses 100 | the least-significant bit of the value as a flag. If the LSB is set, 101 | the value is unboxed. If the LSB is cleared, the value is a pointer to 102 | some other region of memory. This encoding also explains why the int 103 | type in OCaml is only 31 bits wide (63 bits wide on 64 bit platforms). 104 | 105 | Because blocks in the heap are garbage-collected, they have strict 106 | structure constraints. Information like the tag of a block and its 107 | size (in words) is encoded in the header of each block. 108 | 109 | There are two categories of blocks with respect to the garbage collector: 110 | 111 | - **Structured blocks** may only contain well-formed values, as they are 112 | recursively traversed by the garbage collector. 113 | - **Raw blocks** are not scanned by the garbage collector, and can thus 114 | contain arbitrary values. 115 | 116 | Structured blocks have tag values lower than `Obj.no_scan_tag`, while 117 | raw blocks have tags equal or greater than `Obj.no_scan_tag`. 118 | 119 | ### Heap Blocks 120 | 121 | The chapter on *Interfacing C with Objective Caml* in the [OCaml 122 | manual][OCamlManual] describes the following types of blocks. The type 123 | of a block is its tag, which is stored in the block header. (see 124 | `Obj.tag`). 125 | 126 | - `0` to `Obj.no_scan_tag-1`, A structured block (an array of Caml 127 | objects). Each field is a value. 128 | - `Obj.closure_tag`: A closure representing a functional value. The 129 | first word is a pointer to a piece of code, the remaining words are 130 | values containing the environment. 131 | - `Obj.string_tag`: A character string. 132 | - `Obj.double_tag`: A double-precision floating-point number. 133 | - `Obj.double_array_tag`: An array or record of double-precision 134 | floating-point numbers. 135 | - `Obj.abstract_tag`: A block representing an abstract datatype. 136 | - `Obj.custom_tag`: A block representing an abstract datatype with 137 | user-defined finalization, comparison, hashing, serialization and 138 | deserialization functions atttached. 139 | 140 | There are a few more structured block types which are not directly 141 | described in the [manual][OCamlManual]. 142 | 143 | - `Obj.object_tag`: A structured block representing an object. The first 144 | field is a value that describes the class of the object. The second 145 | field is a unique object id (see `Oo.id`). The rest of the block 146 | represents the variables of the object. 147 | - `Obj.lazy_tag`, `Obj.forward_tag`: These two block types are used by the 148 | runtime-system to implement lazy-evaulation. 149 | - `Obj.infix_tag`: A special block contained within a closure block. 150 | 151 | ### Summary 152 | 153 | This section is only a summary of the most important things. The 154 | chapter on *Interfacing C with Objective Caml* in the [OCaml 155 | manual][OCamlManual] gives a much better explanation over the 156 | translation of OCaml types to their actual representation. 157 | 158 | + **Atomic types** 159 | + `int`: Unboxed integer values. 160 | + `char`: Unboxed integer values (ASCII code). 161 | + `float`: Blocks with tag `Obj.double_tag`. 162 | + `string`: Blocks with tag `Obj.string_tag`. 163 | + `int32`/`int64`/`nativeint`: Blocks with `Obj.custom_tag`. 164 | + **Tuples and records**: Blocks with tag 0. 165 | + **Arrays**: Blocks with tag 0. 166 | + **Arrays and records of floats**: Blocks with tag 167 | `Obj.double_array_tag`. 168 | + **Concrete types** 169 | + *Constant constructors*: Represented by unboxed integers, first 170 | declared constant constructor is 0, second declared constant 171 | constructor is 1, and so on. 172 | + *Non-constant constructors*: Blocks with a tag lower than 173 | `Obj.no_scan_tag` that encodes the constructor, numbered in order 174 | of declaration, starting at 0. 175 | + **Objects**: Blocks with tag `Obj.object_tag`. The first field 176 | refers to the class of the object and its associated method 177 | suite. The second field contains a unique object ID. The remaining 178 | fields are the instance variables of the object. 179 | + **Variants**: Variants are similar to constructed terms. There are a 180 | few differences however. 181 | + Variant constructors are identified by their hash value. 182 | + Non-constant variant constructors are not flattened. They are 183 | always blocks of size 2, where the first field is the hash. The 184 | second field can either contain a single value, or a pointer to 185 | another structured block (just like a tuple). 186 | 187 | If in doubt, dump it out. 188 | 189 | References 190 | ---------------------------------------------------------------------- 191 | 192 | * [OCaml Manual][OCamlManual] - The Objective Caml system, Documentation and user's manual 193 | * [Graphviz][] - Graph Visualization Software 194 | * [Caml-Inspect][] - Source code repository for OCaml Inspect 195 | * [Homepage][] - Project homepage 196 | 197 | [OCamlManual]: http://caml.inria.fr/pub/docs/manual-ocaml/ "OCaml Manual" 198 | [Graphviz]: http://www.graphviz.org/ "Graphviz - Graph Visualization Software" 199 | [Caml-Inspect]: http://github.com/krohrer/caml-inspect "Source code repository" 200 | [GitRepository]: http://github.com/krohrer/caml-inspect 201 | [Homepage]: http://krohrer.github.com/caml-inspect 202 | 203 | Licence 204 | ---------------------------------------------------------------------- 205 | 206 | > This library is free software; you can redistribute it and/or 207 | > modify it under the terms of the GNU Lesser General Public 208 | > License as published by the Free Software Foundation; either 209 | > version 2.1 of the License, or (at your option) any later version, 210 | > with the special exception on linking described in file LICENSE. 211 | 212 | > This library is distributed in the hope that it will be useful, 213 | > but WITHOUT ANY WARRANTY; without even the implied warranty of 214 | > MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 215 | > Lesser General Public License for more details. 216 | 217 | > You should have received a copy of the GNU Lesser General Public 218 | > License along with this library; if not, write to the Free Software 219 | > Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 220 | 221 | -------------------------------------------------------------------------------- /aux.ml: -------------------------------------------------------------------------------- 1 | (* Kaspar Rohrer, Tue Apr 20 18:57:21 CEST 2010 *) 2 | 3 | let with_file_out_channel filename f = 4 | let outc = open_out filename in 5 | try 6 | f outc; 7 | flush outc; 8 | close_out outc 9 | with 10 | | e -> 11 | close_out outc; 12 | raise e 13 | 14 | let with_buffer n = 15 | let b = Buffer.create n in 16 | ( fun f -> 17 | Buffer.clear b; 18 | f b; 19 | Buffer.contents b 20 | ) 21 | 22 | let (|>>) a b = b a 23 | -------------------------------------------------------------------------------- /aux.mli: -------------------------------------------------------------------------------- 1 | (* Kaspar Rohrer, Tue Apr 20 18:57:19 CEST 2010 *) 2 | 3 | (** Auxiliary functions for general use. *) 4 | 5 | val with_file_out_channel : string -> (out_channel -> unit) -> unit 6 | (** [with_file_out_channel path writer] tries to open a new 7 | [out_channel] for writing to the file at [path]. It then applies 8 | [writer] to the resulting [out_channel]. Finally, it closes the 9 | out_channel, even if an exception was raised. 10 | 11 | {b Note:} The file will be truncated if it already exists. *) 12 | 13 | val with_buffer : int -> (Buffer.t -> unit) -> string 14 | (** [with_buffer size writer] creates a new [Buffer.t] with space 15 | for at least [size] characters. It then applies [writer] to the 16 | resulting buffer, and returns the contents of it. 17 | 18 | The idea is to be able to reuse the buffer by partial 19 | application. *) 20 | 21 | val (|>>) : 'a -> ('a -> 'b) -> 'b 22 | (** Pipeline operator (apply function to previous result) *) 23 | -------------------------------------------------------------------------------- /dot.ml: -------------------------------------------------------------------------------- 1 | (* Kaspar Rohrer, Wed Apr 14 01:57:49 CEST 2010 *) 2 | 3 | module HT = Hashtbl.Make(Value) 4 | 5 | open Format 6 | open Aux 7 | 8 | type dot_attrs = (string * string) list 9 | 10 | type follow = src:Obj.t -> field:int -> dst:Obj.t -> bool 11 | 12 | (*----------------------------------------------------------------------------*) 13 | 14 | class type context_t = 15 | object 16 | method graph_attrs : dot_attrs 17 | method all_nodes_attrs : dot_attrs 18 | method all_edges_attrs : dot_attrs 19 | method node_attrs : ?root:bool -> Obj.t -> dot_attrs 20 | method edge_attrs : src:Obj.t -> field:int -> dst:Obj.t -> dot_attrs 21 | method label_attrs : string -> dot_attrs 22 | method label_edge_attrs : string -> dot_attrs 23 | 24 | method should_inline : Obj.t -> bool 25 | method should_follow_edge : src:Obj.t -> field:int -> dst:Obj.t -> bool 26 | method max_fields_for_node : Obj.t -> int 27 | end 28 | 29 | (* See http://www.graphviz.org/doc/info/colors.html for more info. *) 30 | 31 | let attrs_colorscheme_for_value ?(k=2.) ?lower ?upper color_scheme n r attrs = 32 | let f2i = int_of_float and i2f = float_of_int in 33 | let lower = match lower with None -> 1 | Some l -> l in 34 | let upper = match upper with None -> n - 1 | Some u -> u in 35 | (* Arcsin seems like a good choice here, it starts of almost 36 | linear, but the derivative seems to decay at an exponential 37 | rate. k adjusts the shape in that approximates the distance 38 | between the first two colors. *) 39 | let arcsinh x = log (x +. sqrt (x*.x +. 1.)) in 40 | let x = i2f (Value.heap_words r) in 41 | let y = f2i (arcsinh ( x /. k )) in 42 | let i = min upper (y + lower) in 43 | let attrs = 44 | ("colorscheme", color_scheme ^ string_of_int n) 45 | :: ("fillcolor", string_of_int i) 46 | :: ("color", string_of_int n) 47 | :: attrs 48 | in 49 | if i = n then 50 | ("fontcolor", "white") :: attrs 51 | else 52 | attrs 53 | 54 | let attrs_for_value r attrs = 55 | match Value.tag r with 56 | | Value.Infix 57 | | Value.Forward -> 58 | (* attrs_colorscheme_for_value "purples" 9 r attrs *) 59 | attrs_colorscheme_for_value ~lower:2 "ylorbr" 3 r attrs 60 | 61 | | Value.Lazy 62 | | Value.Closure -> 63 | attrs_colorscheme_for_value ~k:1.2 ~lower:2 "rdpu" 9 r attrs 64 | 65 | | Value.Object -> 66 | attrs_colorscheme_for_value ~k:1.2 ~lower:4 "purples" 9 r attrs 67 | 68 | | Value.Block -> 69 | attrs_colorscheme_for_value ~lower:2 "blues" 9 r attrs 70 | 71 | | Value.Int 72 | | Value.String 73 | | Value.Double 74 | | Value.Double_array -> 75 | attrs_colorscheme_for_value ~lower:2 "bugn" 9 r attrs 76 | 77 | | Value.Custom when Value.custom_is_int r -> ( 78 | match Value.custom_value r with 79 | | Value.Custom_nativeint _ 80 | | Value.Custom_int32 _ 81 | | Value.Custom_int64 _ -> 82 | attrs_colorscheme_for_value ~lower:2 "bugn" 9 r attrs 83 | | _ -> 84 | attrs_colorscheme_for_value ~k:1.0 ~lower:5 "reds" 9 r attrs ) 85 | 86 | | Value.Out_of_heap 87 | | Value.Unaligned 88 | | Value.Abstract 89 | | Value.Custom -> 90 | attrs_colorscheme_for_value ~k:1.0 ~lower:5 "reds" 9 r attrs 91 | 92 | let label_of_value context r = 93 | let bprint_fields b n bf = 94 | let max_fields = context#max_fields_for_node r in 95 | let n' = min max_fields n in 96 | let cutoff = if n' = max_fields then n' - 1 else max_int in 97 | for i = 0 to n' - 1 do 98 | Buffer.add_string b "| "; 99 | if i = cutoff then 100 | Buffer.add_string b "..." 101 | else 102 | Buffer.add_string b (bf i) 103 | done 104 | in 105 | let bprint b = 106 | Buffer.add_string b (Value.description r); 107 | match Value.tag r with 108 | | _ when Obj.tag r < Obj.no_scan_tag -> 109 | let n = Obj.size r in 110 | bprint_fields b n (fun i -> Value.abbrev (Obj.field r i)) 111 | 112 | | Value.Double_array -> 113 | assert (Obj.tag r = Obj.double_array_tag); 114 | let a : float array = Obj.magic r in 115 | let n = Array.length a in 116 | bprint_fields b n (fun i -> string_of_float a.(i)) 117 | 118 | | Value.Custom | Value.Abstract -> 119 | assert (Obj.tag r = Obj.custom_tag || Obj.tag r = Obj.abstract_tag); 120 | let n = Obj.size r in 121 | bprint_fields b n (fun _ -> Value.mnemonic_unknown) 122 | 123 | | Value.String -> 124 | assert (Obj.tag r = Obj.string_tag); 125 | let nbytes = 10 in 126 | let lsub = nbytes in 127 | let s : string = Obj.magic r in 128 | let l = String.length s in 129 | let n = (l + lsub - 1) / lsub in 130 | bprint_fields b n ( 131 | fun i -> 132 | let isub = i * nbytes in 133 | let len = min (l - isub) lsub in 134 | if l <= isub + nbytes then 135 | sprintf "%S" (String.sub s isub len) 136 | else 137 | sprintf "%S..." (String.sub s isub len) 138 | ) 139 | 140 | | _ -> 141 | () 142 | in 143 | with_buffer 20 bprint 144 | 145 | let follow_all ~src ~field ~dst = 146 | true 147 | 148 | class context ?(max_fields=5) ?(follow=follow_all) () : context_t = 149 | object(self) 150 | method graph_attrs = 151 | [ 152 | "rankdir", "LR"; 153 | "splines", "true"; 154 | "overlap", "false"; 155 | "sep", "0.1" 156 | ] 157 | 158 | method all_nodes_attrs = 159 | [ 160 | "shape", "record"; 161 | "penwidth", "2.0"; 162 | "style", "rounded, filled" 163 | ] 164 | 165 | method all_edges_attrs = 166 | [ 167 | "dir", "both"; 168 | "arrowtail", "odot" 169 | ] 170 | 171 | method node_attrs ?(root=false) r = 172 | let attrs_for_root attrs = 173 | if root then 174 | (* ("fontcolor", "#ff0000") :: *) ("penwidth", "8.0") :: attrs 175 | else 176 | attrs 177 | in 178 | [ "label", label_of_value self r ] |>> attrs_for_value r |>> attrs_for_root 179 | 180 | method edge_attrs ~src ~field ~dst = 181 | [ "label", string_of_int field ] 182 | 183 | method label_attrs s = 184 | [ "style", "rounded, filled" ; 185 | "fillcolor", "yellow" ; 186 | "label", s ; 187 | ] 188 | method label_edge_attrs _ = 189 | [ "style", "dashed" ] 190 | 191 | method should_inline r = 192 | match Value.tag r with 193 | | Value.Custom -> ( 194 | match Value.custom_value r with 195 | | Value.Custom_nativeint _ 196 | | Value.Custom_int32 _ 197 | | Value.Custom_int64 _ -> true 198 | | _ -> false ) 199 | | Value.Int 200 | | Value.Unaligned 201 | | Value.Out_of_heap 202 | | Value.Double -> true 203 | | _ -> false 204 | 205 | method should_follow_edge ~src ~field ~dst = 206 | follow ~src ~field ~dst 207 | 208 | method max_fields_for_node r = 209 | max_fields 210 | end 211 | 212 | let make_context ?max_fields ?follow () = new context ?max_fields ?follow ();; 213 | let default_context = make_context () 214 | 215 | (*----------------------------------------------------------------------------*) 216 | 217 | let dump_list_with_formatter ?(context=default_context) fmt objs = 218 | let queue = Queue.create () in 219 | 220 | let rec value2nid = HT.create 31337 221 | and node_id_of_value r = 222 | try node_id_find r with Not_found -> ( 223 | let id = sprintf "%s_%d" (Value.mnemonic r) (HT.length value2nid) in 224 | (* Make sure the node will exist. *) 225 | Queue.add r queue; 226 | HT.add value2nid r id; 227 | id 228 | ) 229 | and node_id_find r = 230 | HT.find value2nid r 231 | in 232 | 233 | let node_open fmt id = 234 | fprintf fmt "@[<2>%s@ [" id 235 | and node_close fmt () = 236 | fprintf fmt "];@]@," 237 | and link_open fmt id i fid = 238 | let src = id in 239 | let dst = fid in 240 | fprintf fmt "@[<2>%s ->@ %s@ [" src dst 241 | and link_close fmt () = 242 | fprintf fmt "];@]@," 243 | and attr_open fmt name = 244 | fprintf fmt "@[%s = " name 245 | and attr_close fmt () = 246 | fprintf fmt ",@]@ " 247 | in 248 | 249 | let rec node_one fmt id attrs = 250 | node_open fmt id; 251 | attr_list fmt attrs; 252 | node_close fmt () 253 | and link_one fmt id i fid attrs = 254 | link_open fmt id i fid; 255 | attr_list fmt attrs; 256 | link_close fmt () 257 | and attr_one fmt name value = 258 | attr_open fmt name; 259 | fprintf fmt "%S" value; 260 | attr_close fmt () 261 | and attr_list fmt attrs = 262 | (* The list has to be reversed because of the way Graphviz handles 263 | duplicate attributes. *) 264 | List.iter (fun (k,v) -> attr_one fmt k v) (List.rev attrs) 265 | in 266 | 267 | let rec dot_fields fmt id r = 268 | if Obj.tag r < Obj.no_scan_tag then ( 269 | let n = Obj.size r in 270 | for i = 0 to n - 1 do 271 | let dst = Obj.field r i and src = r in 272 | let dont_inline = not (context#should_inline dst) 273 | and do_follow = context#should_follow_edge ~src ~field:i ~dst 274 | in 275 | if dont_inline && do_follow then ( 276 | (* Make sure the node will exist *) 277 | let edge_attrs = context#edge_attrs ~src ~field:i ~dst in 278 | let fid = node_id_of_value dst in 279 | link_one fmt id i fid edge_attrs 280 | ) 281 | done 282 | ) 283 | and dot_value ?(root=false) fmt id r = 284 | let node_attrs = context#node_attrs ~root r in 285 | node_one fmt id node_attrs; 286 | dot_fields fmt id r 287 | in 288 | let dot_label = 289 | let counter = ref 0 in 290 | fun fmt (label, node_id) -> 291 | match label with 292 | "" -> () 293 | | _ -> 294 | incr counter ; 295 | let label_node_id = "label_"^(string_of_int !counter) in 296 | let attrs = context#label_attrs label in 297 | node_one fmt label_node_id attrs ; 298 | fprintf fmt "@[<2>%s ->@ %s@ [" label_node_id node_id; 299 | attr_list fmt (context#label_edge_attrs label); 300 | fprintf fmt "];@]@," 301 | in 302 | 303 | let labels_ids = 304 | List.map (fun (label,o) -> (label, node_id_of_value (Obj.repr o))) objs 305 | in 306 | fprintf fmt "@[@[digraph {@,"; 307 | let graph_attrs = 308 | (match labels_ids with 309 | [] -> [] 310 | | (_,id) :: _ -> ["root", id] 311 | ) @ context#graph_attrs 312 | in 313 | node_one fmt "graph" graph_attrs ; 314 | node_one fmt "node" context#all_nodes_attrs; 315 | node_one fmt "edge" context#all_edges_attrs; 316 | while not (Queue.is_empty queue) do 317 | let r = Queue.pop queue in 318 | dot_value fmt (node_id_of_value r) r 319 | done; 320 | List.iter (dot_label fmt) labels_ids; 321 | fprintf fmt "@]@,}@]"; 322 | pp_print_newline fmt () 323 | 324 | (*----------------------------------------------------------------------------*) 325 | let dump_with_formatter ?context fmt o = 326 | dump_list_with_formatter ?context fmt ["", o] 327 | 328 | let dump ?context o = 329 | let fmt = Format.std_formatter in 330 | dump_with_formatter ?context fmt o 331 | 332 | let dump_list ?context objs = 333 | let fmt = Format.std_formatter in 334 | dump_with_formatter ?context fmt objs 335 | 336 | let dump_to_out_channel ?context outc o = 337 | let fmt = Format.formatter_of_out_channel outc in 338 | dump_with_formatter ?context fmt o 339 | 340 | let dump_list_to_out_channel ?context outc objs = 341 | let fmt = Format.formatter_of_out_channel outc in 342 | dump_list_with_formatter ?context fmt objs 343 | 344 | let dump_to_file ?context filename o = 345 | with_file_out_channel filename (fun outc -> dump_to_out_channel ?context outc o) 346 | 347 | let dump_list_to_file ?context filename objs = 348 | with_file_out_channel filename (fun outc -> dump_list_to_out_channel ?context outc objs) 349 | 350 | let dump_list_and_open ?context ?(cmd="dot") ~format ~viewer objs = 351 | let exec cmd = 352 | if Sys.command cmd <> 0 then ( 353 | Printf.eprintf "OCaml Inspect: Could not execute command: %s" cmd; 354 | false 355 | ) 356 | else 357 | true 358 | in 359 | let basename = Filename.temp_file "camldump" "." in 360 | let dotfile = basename ^ "dot" in 361 | let outfile = basename ^ format in 362 | dump_list_to_file ?context dotfile objs; 363 | let dotcmd = sprintf "%S -T%s -o %S %S" cmd format outfile dotfile in 364 | let outcmd = sprintf "%S %S" viewer outfile in 365 | if exec dotcmd && exec outcmd then 366 | () 367 | 368 | let dump_and_open ?context ?cmd ~format ~viewer o = 369 | dump_list_and_open ?context ?cmd ~format ~viewer ["",o] 370 | 371 | let dump_list_osx ?context ?(cmd="dot") objs = 372 | dump_list_and_open ?context ~cmd ~format:"pdf" ~viewer:"open" objs 373 | 374 | let dump_osx ?context ?cmd o = 375 | dump_list_osx ?context ?cmd ["",o] 376 | 377 | (*----------------------------------------------------------------------------*) 378 | 379 | exception TestException of string * int 380 | 381 | let test_data () = 382 | let rec l = 1 :: 2 :: 3 :: 4 :: 5 :: 6 :: 7 :: l in 383 | let rec drop l i = 384 | if i = 0 then 385 | l 386 | else 387 | drop (List.tl l) (i - 1) 388 | in 389 | let rec f x = 390 | l 391 | and g y = 392 | f (y :: l) 393 | in 394 | let o = object 395 | val brog = 4 396 | val brag = 51251 397 | method blah = 3 398 | method foo () a = a 399 | end in 400 | let data = 401 | ([|1|], l, (1,2), [|3; 4|], flush, 1.0, [|2.0; 3.0|], 402 | TestException ("TestException", -1), 403 | String.make 1000000 'a', 404 | ("Hello world", lazy (3 + 5)), g, f, let s = "STRING" in (s, "STRING", s), 405 | Array.init 20 (drop l), 406 | stdout, Format.printf, (o, default_context), 407 | [String.make 10 'a'; String.make 100 'a'; String.make 1000 'a'; String.make 10000000 'a'], 408 | [Array.make 1 1; Array.make 4 4; Array.make 16 16; Array.make 64 64; Array.make 256 256; 409 | Array.make 1024 1024; Array.make 1000000 0], 410 | [Array.make 1 1.; Array.make 4 4.; Array.make 16 16.; Array.make 64 64.; Array.make 256 256.; 411 | Array.make 1024 1024.; Array.make 1000000 0.] 412 | ) 413 | in 414 | Obj.repr data 415 | 416 | (*----------------------------------------------------------------------------*) 417 | -------------------------------------------------------------------------------- /dot.mli: -------------------------------------------------------------------------------- 1 | (* Kaspar Rohrer, Wed Apr 14 02:20:31 CEST 2010 *) 2 | 3 | (** Dumping arbitrary values in the Graphviz DOT language 4 | 5 | Context objects are used to configure the dumping process. A 6 | sensible [default_context] is already provided. 7 | 8 | If you are on OS X and have Graphviz installed, you can try the 9 | [Inspect.Dot.dump_osx] function to dump directly to a 10 | temporary PDF file, which will then be opened in Preview. *) 11 | 12 | type dot_attrs = (string * string) list 13 | 14 | class type context_t = 15 | object 16 | method graph_attrs : dot_attrs 17 | method all_nodes_attrs : dot_attrs 18 | method all_edges_attrs : dot_attrs 19 | method node_attrs : ?root:bool -> Obj.t -> dot_attrs 20 | method edge_attrs : src:Obj.t -> field:int -> dst:Obj.t -> dot_attrs 21 | method label_attrs : string -> dot_attrs 22 | method label_edge_attrs : string -> dot_attrs 23 | 24 | method should_inline : Obj.t -> bool 25 | method should_follow_edge : src:Obj.t -> field:int -> dst:Obj.t -> bool 26 | method max_fields_for_node : Obj.t -> int 27 | end 28 | (** The context is used to configure the dumping process *) 29 | 30 | type follow = src:Obj.t -> field:int -> dst:Obj.t -> bool 31 | (** Edge filter predicate *) 32 | 33 | class context : ?max_fields: int -> ?follow:follow -> unit -> context_t 34 | (** The default context class. *) 35 | 36 | val default_context : context 37 | (** Context with sensible default values, used as the default 38 | argument for the [dump] function family. *) 39 | 40 | val make_context : ?max_fields:int -> ?follow:follow -> unit -> context 41 | (** Create a custom context. [max_fields] controls how many fields 42 | should be expanded for block nodes. *) 43 | 44 | val dump : ?context:context -> 'a -> unit 45 | (** Dump to [stdout] *) 46 | 47 | val dump_list : ?context:context -> (string * 'a) list -> unit 48 | (** Same as {!dump} but for a list of labeled values.*) 49 | 50 | val dump_to_file : ?context:context -> string -> 'a -> unit 51 | (** Dump directly to a file. {b The file will be overwritten if it 52 | already exists.} *) 53 | 54 | val dump_list_to_file : ?context:context -> string -> (string * 'a) list -> unit 55 | (** Same as {!dump_to_file} but for a list of labeled values.*) 56 | 57 | val dump_with_formatter : ?context:context -> Format.formatter -> 'a -> unit 58 | (** Dump using the [Format] module for pretty printing. *) 59 | 60 | val dump_list_with_formatter : ?context:context -> Format.formatter -> (string * 'a) list -> unit 61 | (** Same as {!dump_with_formatter} but for a list of labeled values.*) 62 | 63 | val dump_and_open : ?context:context -> ?cmd:string -> format:string -> viewer:string -> 'a -> unit 64 | (** [dump_and_open ?context ?cmd ~format ~viewer o] dumps the value [o] to a 65 | temporary file, runs the Graphviz program given by [cmd] on it 66 | to generate output as specified by [format], and then opens 67 | the generated output with the [viewer] command. 68 | 69 | E.g. [Inspect.Dot.dump_osx ~cmd:"neato" (Inspect.test_data ())] 70 | 71 | This function will block while the graph is being generated. *) 72 | 73 | val dump_list_and_open : ?context:context -> ?cmd:string -> 74 | format:string -> viewer:string -> (string * 'a) list -> unit 75 | (** Same as {!dump_and_open} but for a list of labeled values.*) 76 | 77 | val dump_osx : ?context:context -> ?cmd:string -> 'a -> unit 78 | (** Call {!val:dump_and_open} with [format] "pdf" and [viewer] "open". *) 79 | 80 | val dump_list_osx : ?context:context -> ?cmd:string -> (string * 'a) list -> unit 81 | (** Same as {!dump_osx} but for a list of labeled values.*) 82 | 83 | val test_data : unit -> Obj.t 84 | (** Generate test data to inspect *) 85 | -------------------------------------------------------------------------------- /sexpr.ml: -------------------------------------------------------------------------------- 1 | (* Kaspar Rohrer, Wed Apr 14 13:46:21 CEST 2010 *) 2 | 3 | module HT = Hashtbl.Make(Value) 4 | 5 | open Format 6 | open Aux 7 | 8 | (*----------------------------------------------------------------------------*) 9 | 10 | class type context = 11 | object 12 | method is_not_too_deep : depth:int -> Value.t -> bool 13 | method should_expand : Value.t -> bool 14 | end 15 | 16 | let make_context ?(nesting=20) () = 17 | object 18 | method is_not_too_deep ~depth r = 19 | depth < nesting 20 | 21 | method should_expand r = 22 | match Value.tag r with 23 | | _ -> true 24 | end 25 | 26 | let default_context = make_context () 27 | 28 | (*----------------------------------------------------------------------------*) 29 | 30 | let dump_with_formatter ?(context=default_context) fmt o = 31 | let queue = Queue.create () in 32 | let indentation_for_string id = 3 (* String.length id + 2 *) in 33 | 34 | let rec value2id = HT.create 31337 35 | and id_of_value r = 36 | try 37 | id_find r 38 | with Not_found -> ( 39 | let tid = Value.mnemonic r in 40 | let n = HT.length value2id in 41 | let id = sprintf "%s/%d" tid n in 42 | HT.add value2id r id; 43 | id 44 | ) 45 | and id_find r = 46 | HT.find value2id r 47 | in 48 | 49 | let sexpr_open fmt id = 50 | fprintf fmt "@[(%s" (indentation_for_string id) id 51 | and sexpr_close fmt () = 52 | fprintf fmt ")@]" 53 | and sexpr_sep fmt () = 54 | fprintf fmt "@ " 55 | (* and sexpr_mnemo fmt r = *) 56 | (* pp_print_string fmt (Value.mnemonic r) *) 57 | and sexpr_ref fmt id = 58 | fprintf fmt "@@%s" id 59 | in 60 | 61 | let rec sexpr_one body ~depth fmt r = 62 | if depth = 0 then ( 63 | (* At depth 0 we are never too deep, no mather what the (maybe 64 | too whiny) context might say. We HAVE TO consume the queue 65 | somehow. *) 66 | sexpr_open fmt (id_of_value r); 67 | body (); 68 | sexpr_close fmt () 69 | ) 70 | else ( 71 | if context#is_not_too_deep ~depth r then ( 72 | (* It still is reasonable to dump this *) 73 | try sexpr_ref fmt (id_find r) with Not_found -> 74 | (* Print reference to already printed sexpr, or print now *) 75 | sexpr_open fmt (id_of_value r); 76 | body (); 77 | sexpr_close fmt () 78 | ) 79 | else ( 80 | try sexpr_ref fmt (id_find r) with Not_found -> 81 | (* Print reference to already printed sexpr, or queue for later *) 82 | Queue.add r queue 83 | ) 84 | ) 85 | 86 | and sexpr_string ~depth fmt r = 87 | let body () = 88 | assert (Obj.tag r = Obj.string_tag); 89 | let s : string = Obj.magic r in 90 | sexpr_sep fmt (); 91 | fprintf fmt ":LEN %d" (String.length s); 92 | if context#should_expand r then ( 93 | sexpr_sep fmt (); 94 | fprintf fmt "%S" s 95 | ) 96 | in 97 | sexpr_one body ~depth fmt r 98 | 99 | and sexpr_abstract ~depth fmt r= 100 | let body () = 101 | assert (Obj.tag r = Obj.abstract_tag); 102 | sexpr_sep fmt (); 103 | fprintf fmt ":SIZE %d" (Value.heap_words r); 104 | in 105 | sexpr_one body ~depth fmt r 106 | 107 | and sexpr_custom ~depth fmt r = 108 | let body () = 109 | assert (Obj.tag r = Obj.custom_tag); 110 | sexpr_sep fmt (); 111 | fprintf fmt ":ID %S" (Value.custom_identifier r); 112 | if context#should_expand r then ( 113 | sexpr_sep fmt (); 114 | fprintf fmt ":SIZE %d" (Value.heap_words r); 115 | sexpr_sep fmt (); 116 | fprintf fmt ":OPS %s" (Value.custom_ops_info r) 117 | ) 118 | in 119 | sexpr_one body ~depth fmt r 120 | 121 | and sexpr_block ~depth fmt r = 122 | let body () = 123 | assert (Obj.tag r < Obj.no_scan_tag); 124 | let n = Obj.size r and depth = depth + 1 in 125 | sexpr_sep fmt (); 126 | fprintf fmt ":TAG %d" (Obj.tag r); 127 | if context#should_expand r then ( 128 | sexpr_sep fmt (); 129 | fprintf fmt ":VALUES"; 130 | for i = 0 to n - 1 do 131 | let f = Obj.field r i in 132 | sexpr_sep fmt (); 133 | sexpr_value ~depth fmt f 134 | done 135 | ) 136 | else ( 137 | sexpr_sep fmt (); 138 | fprintf fmt ":SIZE %d" n 139 | ) 140 | in 141 | sexpr_one body ~depth fmt r 142 | 143 | and sexpr_double_array ~depth fmt r = 144 | let body () = 145 | assert (Obj.tag r = Obj.double_array_tag); 146 | let a : float array = Obj.magic r in 147 | let n = Array.length a in 148 | if context#should_expand r then ( 149 | for i = 0 to n - 1 do 150 | sexpr_sep fmt (); 151 | fprintf fmt "%g" a.(i) 152 | done 153 | ) 154 | else ( 155 | sexpr_sep fmt (); 156 | fprintf fmt ":SIZE %d" n 157 | ) 158 | in 159 | sexpr_one body ~depth fmt r 160 | 161 | and sexpr_float fmt r = 162 | assert (Obj.tag r = Obj.double_tag); 163 | fprintf fmt "%f" (Obj.magic r : float) 164 | 165 | and sexpr_int fmt r = 166 | assert (Obj.tag r = Obj.int_tag); 167 | fprintf fmt "%d" (Obj.magic r : int) 168 | 169 | and sexpr_nativeint fmt ni = 170 | fprintf fmt "%ndn" ni 171 | 172 | and sexpr_int32 fmt i32 = 173 | fprintf fmt "%ldl" i32 174 | 175 | and sexpr_int64 fmt i64 = 176 | fprintf fmt "%LdL" i64 177 | 178 | and sexpr_addr fmt r = 179 | fprintf fmt "0x%nX" (Value.bits r) 180 | 181 | and sexpr_value ~depth fmt r = 182 | (* The great dispatch! I wonder how this would look in Java *) 183 | let t = Value.tag r in 184 | match t with 185 | | Value.Lazy 186 | | Value.Closure 187 | | Value.Object 188 | | Value.Infix 189 | | Value.Forward 190 | | Value.Block -> sexpr_block ~depth fmt r 191 | | Value.Abstract -> sexpr_abstract ~depth fmt r 192 | | Value.Custom -> ( 193 | match Value.custom_value r with 194 | | Value.Custom_nativeint ni -> sexpr_nativeint fmt ni 195 | | Value.Custom_int32 i32 -> sexpr_int32 fmt i32 196 | | Value.Custom_int64 i64 -> sexpr_int64 fmt i64 197 | | _ -> sexpr_custom ~depth fmt r ) 198 | | Value.Double_array -> sexpr_double_array ~depth fmt r 199 | | Value.Unaligned 200 | | Value.Out_of_heap -> sexpr_addr fmt r 201 | | Value.Double -> sexpr_float fmt r 202 | | Value.Int -> sexpr_int fmt r 203 | | Value.String -> sexpr_string ~depth fmt r 204 | in 205 | 206 | let values = "DUMP" in 207 | let r = Obj.repr o in 208 | pp_open_vbox fmt 0; 209 | sexpr_open fmt values; 210 | Queue.push r queue; 211 | while not (Queue.is_empty queue) do 212 | let r = Queue.pop queue in 213 | sexpr_sep fmt (); 214 | sexpr_value ~depth:0 fmt r 215 | done; 216 | sexpr_close fmt (); 217 | pp_close_box fmt () 218 | 219 | (*----------------------------------------------------------------------------*) 220 | 221 | let dump ?context o = 222 | let fmt = Format.std_formatter in 223 | dump_with_formatter ?context fmt o 224 | 225 | let dump_to_out_channel ?context outc o = 226 | let fmt = Format.formatter_of_out_channel outc in 227 | dump_with_formatter ?context fmt o 228 | 229 | let dump_to_file ?context filename o = 230 | with_file_out_channel filename (fun outc -> dump_to_out_channel ?context outc o) 231 | 232 | (*----------------------------------------------------------------------------*) 233 | 234 | exception TestException of string * int 235 | 236 | let rec test_data () = 237 | let rec l = 1 :: 2 :: 3 :: 4 :: 5 :: 6 :: 7 :: l in 238 | let rec drop l i = 239 | if i = 0 then 240 | l 241 | else 242 | drop (List.tl l) (i - 1) 243 | in 244 | let rec f x = 245 | l 246 | and g y = 247 | f (y :: l) 248 | in 249 | let o = object 250 | val brog = 4 251 | val brag = 51251 252 | method blah = 3 253 | method foo () a = a 254 | end in 255 | let data = 256 | ([|1|], l, (1,2), [|3; 4|], flush, 1.0, [|2.0; 3.0|], 257 | TestException ("TestException", -1), 258 | test_data, 259 | ("Hello world", lazy (3 + 5)), g, f, let s = "STRING" in (s, "STRING", s), 260 | Array.init 20 (drop l), 261 | stdout, Format.printf, (o, default_context)) 262 | in 263 | Obj.repr data 264 | 265 | (*----------------------------------------------------------------------------*) 266 | -------------------------------------------------------------------------------- /sexpr.mli: -------------------------------------------------------------------------------- 1 | (* Kaspar Rohrer, Wed Apr 14 13:39:33 CEST 2010 *) 2 | 3 | (** Dumping arbitrary values as S-expression 4 | 5 | Context objects are used to configure the dumping process. A 6 | sensible [default_context] is already provided. *) 7 | 8 | type context 9 | (** The context is used to configure the dumping process *) 10 | 11 | val default_context : context 12 | (** Context with sensible default values, used as the default 13 | argument for the [dump] function family. *) 14 | 15 | val make_context : ?nesting:int -> unit -> context 16 | (** Create a custom context. [nesting] is how deep the printer 17 | recurses before printing *) 18 | 19 | val dump : ?context:context -> 'a -> unit 20 | (** Dump to [stdout] *) 21 | 22 | val dump_to_file : ?context:context -> string -> 'a -> unit 23 | (** Dump directly to a file. {b The file will be overwritten if it 24 | already exists.} *) 25 | 26 | val dump_with_formatter : ?context:context -> Format.formatter -> 'a -> unit 27 | (** Dump using the [Format] module for pretty printing. *) 28 | 29 | val test_data : unit -> Obj.t 30 | -------------------------------------------------------------------------------- /value.ml: -------------------------------------------------------------------------------- 1 | open Printf 2 | 3 | type t = Obj.t 4 | 5 | let compare = compare 6 | let equal = (==) 7 | let hash = Hashtbl.hash 8 | 9 | type tag = 10 | | Lazy 11 | | Closure 12 | | Object 13 | | Infix 14 | | Forward 15 | | Block 16 | | Abstract 17 | | String 18 | | Double 19 | | Double_array 20 | | Custom 21 | | Int 22 | | Out_of_heap 23 | | Unaligned 24 | 25 | type custom = 26 | | Custom_nativeint of nativeint 27 | | Custom_int32 of int32 28 | | Custom_int64 of int64 29 | | Custom_bigarray 30 | | Custom_channel 31 | | Custom_unknown 32 | | Not_custom 33 | 34 | external bits : t -> nativeint = "inspect_bits" 35 | 36 | let hex_digits = "0123456789ABCDEF" 37 | 38 | let dec_of_bits v = 39 | sprintf "%nd" v 40 | 41 | let hex_of_bits v = 42 | let (lsr) = Nativeint.shift_right in 43 | let (land) = Nativeint.logand in 44 | let ndig = Nativeint.size / 4 in 45 | let b = Buffer.create (2 + ndig + 1) in 46 | Buffer.add_string b "0x"; 47 | for i = ndig - 1 downto 0 do 48 | let d = (v lsr (i * 4)) land 0xFn in 49 | Buffer.add_char b hex_digits.[Nativeint.to_int d] 50 | done; 51 | Buffer.contents b 52 | 53 | let bin_of_bits v = 54 | let (lsr) = Nativeint.shift_right in 55 | let (land) = Nativeint.logand in 56 | let ndig = Nativeint.size in 57 | let b = Buffer.create (2 + ndig + 1) in 58 | (* three seems reasonable, prefix and maybe null? *) 59 | Buffer.add_string b "0b"; 60 | for i = Sys.word_size - 1 downto 0 do 61 | let d = (v lsr i) land 1n in 62 | Buffer.add_string b (if d = 1n then "1" else "0") 63 | done; 64 | Buffer.contents b 65 | 66 | let bits_to_string ?(base=`Hex) v = 67 | let bs = bits v in 68 | match base with 69 | | `Dec -> dec_of_bits bs 70 | | `Hex -> hex_of_bits bs 71 | | `Bin -> bin_of_bits bs 72 | 73 | external custom_identifier : t -> string = "inspect_custom_id" 74 | 75 | external custom_has_finalize : t -> bool = "inspect_custom_has_finalize" 76 | external custom_has_compare : t -> bool = "inspect_custom_has_compare" 77 | external custom_has_hash : t -> bool = "inspect_custom_has_hash" 78 | external custom_has_serialize : t -> bool = "inspect_custom_has_serialize" 79 | external custom_has_deserialize : t -> bool = "inspect_custom_has_deserialize" 80 | 81 | let custom_ops_info r = 82 | sprintf "%c%c%c%c%c" 83 | (if custom_has_finalize r then 'F' else '-') 84 | (if custom_has_compare r then 'C' else '-') 85 | (if custom_has_hash r then 'H' else '-') 86 | (if custom_has_serialize r then 'S' else '-') 87 | (if custom_has_deserialize r then 'D' else '-') 88 | 89 | let nativeint_id = "_n" 90 | let int32_id = "_i" 91 | let int64_id = "_j" 92 | let bigarray_id = "_bigarray" 93 | let channel_id = "_chan" 94 | 95 | module TagSet = 96 | struct 97 | include Set.Make(struct type t = tag let compare = compare end) 98 | 99 | let of_list tlist = 100 | List.fold_left (fun s t -> add t s) empty tlist 101 | 102 | let all = 103 | of_list [ 104 | Lazy; 105 | Closure; 106 | Object; 107 | Infix; 108 | Forward; 109 | Block; 110 | Abstract; 111 | String; 112 | Double; 113 | Double_array; 114 | Custom; 115 | Int; 116 | Out_of_heap; 117 | Unaligned; 118 | ] 119 | end 120 | 121 | (* Make sure the known custom identifiers are in sync. *) 122 | let _ = 123 | let rnat = Obj.repr 0n and ri32 = Obj.repr 0l and ri64 = Obj.repr 0L in 124 | assert (Obj.tag rnat = Obj.custom_tag); 125 | assert (Obj.tag ri32 = Obj.custom_tag); 126 | assert (Obj.tag ri64 = Obj.custom_tag); 127 | assert (nativeint_id = custom_identifier rnat); 128 | assert (int32_id = custom_identifier ri32); 129 | assert (int64_id = custom_identifier ri64); 130 | (* assert (bigarray_id = custom_identifier ...); *) 131 | assert (channel_id = custom_identifier (Obj.repr stdout)); 132 | () 133 | 134 | let custom_value r = 135 | if Obj.tag r = Obj.custom_tag then ( 136 | let id = custom_identifier r in 137 | if id = nativeint_id then 138 | Custom_nativeint (Obj.magic r : nativeint) 139 | else if id = int32_id then 140 | Custom_int32 (Obj.magic r : int32) 141 | else if id = int64_id then 142 | Custom_int64 (Obj.magic r : int64) 143 | else if id = channel_id then 144 | Custom_channel 145 | else if id = bigarray_id then 146 | Custom_bigarray 147 | else 148 | Custom_unknown 149 | ) 150 | else 151 | Not_custom 152 | 153 | let custom_is_int r = 154 | match custom_value r with 155 | | Custom_nativeint _ -> false 156 | | Custom_int32 _ -> true 157 | | Custom_int64 _ -> true 158 | | _ -> false 159 | 160 | (* Matching an integer value should be faster than a series of if 161 | statements. 162 | That's why all these assertions are here, to make sure 163 | that the integer literals used in the match statement actually 164 | correspond to the tags defined by the Obj module. *) 165 | let _ = 166 | assert (Obj.lazy_tag = 246); 167 | assert (Obj.closure_tag = 247); 168 | assert (Obj.object_tag = 248); 169 | assert (Obj.infix_tag = 249); 170 | assert (Obj.forward_tag = 250); 171 | assert (Obj.no_scan_tag = 251); 172 | assert (Obj.abstract_tag = 251); 173 | assert (Obj.string_tag = 252); 174 | assert (Obj.double_tag = 253); 175 | assert (Obj.double_array_tag = 254); 176 | assert (Obj.custom_tag = 255); 177 | assert (Obj.int_tag = 1000); 178 | assert (Obj.out_of_heap_tag = 1001); 179 | assert (Obj.unaligned_tag = 1002); 180 | () 181 | 182 | (* Slower and safer. 183 | let value_tag r = 184 | match tag r with 185 | | x when x = lazy_tag -> Lazy 186 | | x when x = closure_tag -> Closure 187 | | x when x = object_tag -> Object 188 | | x when x = infix_tag -> Infix 189 | | x when x = forward_tag -> Forward 190 | | x when x < no_scan_tag -> Block 191 | | x when x = abstract_tag -> Abstract 192 | | x when x = string_tag -> String 193 | | x when x = double_tag -> Double 194 | | x when x = double_array_tag -> Double_array 195 | | x when x = custom_tag -> Custom 196 | | x when x = int_tag -> Int 197 | | x when x = out_of_heap_tag -> Out_of_heap 198 | | x when x = unaligned_tag -> Unaligned 199 | | x -> failwith (sprintf "OCaml value with unknown tag = %d" x) 200 | *) 201 | 202 | (* Faster but more dangerous *) 203 | let tag r = 204 | match Obj.tag r with 205 | | x when x < 246 -> Block 206 | | 246 -> Lazy 207 | | 247 -> Closure 208 | | 248 -> Object 209 | | 249 -> Infix 210 | | 250 -> Forward 211 | | 251 -> Abstract 212 | | 252 -> String 213 | | 253 -> Double 214 | | 254 -> Double_array 215 | | 255 -> Custom 216 | | 1000 -> Int 217 | | 1001 -> Out_of_heap 218 | | 1002 -> Unaligned 219 | | x -> failwith (sprintf "OCaml value with unknown tag = %d" x) 220 | 221 | (* Slower? and safer 222 | let is_in_heap r = 223 | let x = Obj.tag r in 224 | not (x = Obj.int_tag || x = Obj.out_of_heap_tag || x = Obj.unaligned_tag) 225 | *) 226 | 227 | (* Faster but more dangerous *) 228 | let is_in_heap r = 229 | let x = Obj.tag r in 230 | x < 1000 || 1002 < x 231 | 232 | let heap_words r = 233 | if is_in_heap r then Obj.size r else 0 234 | 235 | let mnemonic r = 236 | match tag r with 237 | | Lazy -> "LAZY" 238 | | Closure -> "CLOS" 239 | | Object -> "OBJ" 240 | | Infix -> "INFX" 241 | | Forward -> "FWD" 242 | | Block -> "BLK" 243 | | Abstract -> "ABST" 244 | | String -> "STR" 245 | | Double -> "DBL" 246 | | Double_array -> "DBLA" 247 | | Custom -> "CUST" 248 | | Int -> "INT" 249 | | Out_of_heap -> "OADR" 250 | | Unaligned -> "UADR" 251 | 252 | let mnemonic_unknown = 253 | "????" 254 | 255 | let abbrev r = 256 | match tag r with 257 | | Lazy 258 | | Closure 259 | | Object 260 | | Infix 261 | | Forward 262 | | Block 263 | | Double_array 264 | | String 265 | | Abstract -> sprintf "%s#%d" (mnemonic r) (heap_words r) 266 | | Double -> sprintf "%g" (Obj.magic r : float) 267 | | Custom -> ( 268 | match custom_value r with 269 | | Custom_nativeint n -> sprintf "%ndn" n 270 | | Custom_int32 i -> sprintf "%ldl" i 271 | | Custom_int64 i -> sprintf "%LdL" i 272 | | Custom_bigarray -> "Bigarray" 273 | | Custom_channel -> "Channel" 274 | | Custom_unknown -> sprintf "%S %s" (custom_identifier r) (custom_ops_info r) 275 | | Not_custom -> failwith "Value.description: should be a custom value" 276 | ) 277 | | Int -> string_of_int (Obj.magic r : int) 278 | | Out_of_heap -> sprintf "0x%nX" (bits r) 279 | | Unaligned -> sprintf "0x%nX" (bits r) 280 | 281 | let description r = 282 | match tag r with 283 | | Lazy -> "Lazy: #" ^ string_of_int (Obj.size r) 284 | | Closure -> "Closure: #" ^ string_of_int (Obj.size r) 285 | | Object -> "Object: #" ^ string_of_int (Obj.size r) 286 | | Infix -> "Infix: #" ^ string_of_int (Obj.size r) 287 | | Forward -> "Forward: #" ^ string_of_int (Obj.size r) 288 | | Block -> sprintf "Block(%d): #%d" (Obj.tag r) (Obj.size r) 289 | | Abstract -> "Abstract: #" ^ string_of_int (Obj.size r) 290 | | String -> 291 | let len = String.length (Obj.magic r : string) in 292 | sprintf "String: %d char%s" len (if len > 1 then "s" else "") 293 | | Double -> sprintf "Double: %g" (Obj.magic r : float) 294 | | Double_array -> sprintf "Double_array: %d floats" (Array.length (Obj.magic r : float array)) 295 | | Custom -> ( 296 | match custom_value r with 297 | | Custom_nativeint n -> sprintf "Nativeint: %nd" n 298 | | Custom_int32 i -> sprintf "Int32: %ld" i 299 | | Custom_int64 i -> sprintf "Int64: %Ld" i 300 | | Custom_bigarray -> "Bigarray" 301 | | Custom_channel -> "Channel" 302 | | Custom_unknown -> sprintf "Custom: %S %s" (custom_identifier r) (custom_ops_info r) 303 | | Not_custom -> failwith "Value.description: should be a custom value" 304 | ) 305 | | Int -> sprintf "Int: %d" (Obj.magic r : int) 306 | | Out_of_heap -> sprintf "Out_of_heap (0x%nX)" (bits r) 307 | | Unaligned -> sprintf "Unaligned (0x%nX)" (bits r) 308 | -------------------------------------------------------------------------------- /value.mli: -------------------------------------------------------------------------------- 1 | (** Arbitrary values (based on the [Obj] module) 2 | 3 | This module builds upon the {!Obj} module. It provides the {!tag} 4 | and {!custom} types to provide better dispatch on the internal 5 | type of any OCaml value. It also provides functions to inspect the 6 | representation of values, and to generate human-readable 7 | descriptions and mnemonics for use by the {!Dot} and {!Sexpr} 8 | module. 9 | *) 10 | 11 | (** {6 Types} *) 12 | 13 | type t = Obj.t 14 | (** [Value.t] is the same as [Obj.t] *) 15 | 16 | (** This allows better dispatch than comparing the tags. *) 17 | type tag = 18 | | Lazy 19 | | Closure 20 | | Object 21 | | Infix 22 | | Forward 23 | | Block 24 | | Abstract 25 | | String 26 | | Double 27 | | Double_array 28 | | Custom 29 | | Int 30 | | Out_of_heap 31 | | Unaligned 32 | 33 | (** Information about custom types *) 34 | type custom = 35 | | Custom_nativeint of nativeint 36 | | Custom_int32 of int32 37 | | Custom_int64 of int64 38 | | Custom_bigarray 39 | | Custom_channel 40 | | Custom_unknown 41 | | Not_custom 42 | 43 | (** A set of tags *) 44 | module TagSet : sig 45 | include Set.S with type elt = tag 46 | 47 | val all : t 48 | (** All tags in a set *) 49 | 50 | val of_list : tag list -> t 51 | (** Conversion from list to set *) 52 | end 53 | 54 | (** {6 Hashtbl.HashedType} *) 55 | 56 | (** Equality predicate (physical equality only!) *) 57 | val equal : t -> t -> bool 58 | 59 | (** Hashing function (used*) 60 | val hash : t -> int 61 | 62 | (** {6 Value representation} *) 63 | 64 | val bits : t -> nativeint 65 | (** Return the raw bits of a value *) 66 | 67 | val bits_to_string : ?base:[`Dec|`Hex|`Bin] -> t -> string 68 | (** Return a string describing the low-level bit pattern of a 69 | value. *) 70 | 71 | val tag : t -> tag 72 | (** Tag type of a value *) 73 | 74 | val heap_words : t -> int 75 | (** Number of words that the value occupies on the heap (without 76 | the block header). *) 77 | 78 | val is_in_heap : t -> bool 79 | (** [Int], [Out_of_heap] and [Unaligned] are out. *) 80 | 81 | (** {6 Custom Blocks} *) 82 | 83 | val custom_identifier : t -> string 84 | (** Returns the identifier of the custom block *) 85 | 86 | val custom_value : t -> custom 87 | (** *) 88 | 89 | val custom_ops_info : t -> string 90 | (** Info about the available operations (finalize / compare / hash 91 | / serialize / deserialize), which the custom block provides. 92 | 93 | E.g. {e FCHSD}, {e FC---}, {e -----} 94 | *) 95 | 96 | val custom_is_int : t -> bool 97 | (** Is the custom value a [int32], [int64] or [nativeint]?*) 98 | 99 | val custom_has_finalize : t -> bool 100 | (** Supports finalization? *) 101 | 102 | val custom_has_compare : t -> bool 103 | (** Supports comparison? *) 104 | 105 | val custom_has_hash : t -> bool 106 | (** Supports hashing? *) 107 | 108 | val custom_has_serialize : t -> bool 109 | (** Supports serialization? *) 110 | 111 | val custom_has_deserialize : t -> bool 112 | (** Supports deserialization? *) 113 | 114 | (** {6 Value Descriptions} *) 115 | 116 | val mnemonic : t -> string 117 | (** Mnemonic or identifier for a value *) 118 | 119 | val mnemonic_unknown : string 120 | (** Mnemonic for an unknown/abstract value, *) 121 | 122 | val abbrev : t -> string 123 | (** Abbreviated, readable description of a value*) 124 | 125 | val description : t -> string 126 | (** Readable description of a value*) 127 | -------------------------------------------------------------------------------- /value_cimpl.c: -------------------------------------------------------------------------------- 1 | #include "caml/mlvalues.h" 2 | #include "caml/alloc.h" 3 | #include "caml/memory.h" 4 | #include "caml/fail.h" 5 | #include "caml/callback.h" 6 | #include "caml/custom.h" 7 | 8 | CAMLexport value inspect_custom_id(value v) 9 | { 10 | CAMLparam1(v); 11 | CAMLlocal1(result); 12 | if (Tag_val(v) == Custom_tag) { 13 | result = caml_copy_string(Custom_ops_val(v)->identifier); 14 | } 15 | else { 16 | caml_invalid_argument("Value.custom_id"); 17 | } 18 | CAMLreturn(result); 19 | } 20 | 21 | CAMLexport value inspect_bits(value v) 22 | { 23 | CAMLparam1(v); 24 | CAMLreturn(caml_copy_nativeint(v)); 25 | } 26 | 27 | #define Inspect_custom_has(op) \ 28 | CAMLexport value inspect_custom_has_##op(value v) \ 29 | { return Val_bool(Tag_val(v) == Custom_tag && Custom_ops_val(v)->op != NULL); } 30 | 31 | Inspect_custom_has (finalize) 32 | Inspect_custom_has (compare) 33 | Inspect_custom_has (hash) 34 | Inspect_custom_has (serialize) 35 | Inspect_custom_has (deserialize) 36 | --------------------------------------------------------------------------------