123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154(*********************************************************************************)(* Stog *)(* *)(* Copyright (C) 2012-2015 INRIA All rights reserved. *)(* Author: Maxence Guesdon, INRIA Saclay *)(* *)(* This program is free software; you can redistribute it and/or modify *)(* it under the terms of the GNU General Public License as *)(* published by the Free Software Foundation, version 3 of the License. *)(* *)(* This program is distributed in the hope that it will be useful, *)(* but WITHOUT ANY WARRANTY; without even the implied warranty of *)(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *)(* GNU General Public License for more details. *)(* *)(* You should have received a copy of the GNU General Public *)(* License along with this program; if not, write to the Free Software *)(* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA *)(* 02111-1307 USA *)(* *)(* As a special exception, you have permission to link this program *)(* with the OCaml compiler and distribute executables, as long as you *)(* follow the requirements of the GNU GPL in regard to all of the *)(* software in the executable aside from the OCaml compiler. *)(* *)(* Contact: Maxence.Guesdon@inria.fr *)(* *)(*********************************************************************************)(** *)lethack_cmxs=reffalse;;letcreate_cmxssrc=letdst=Filename.temp_file("stog-"^(Filename.basenamesrc))".cmxs"inletoptions=matchFilename.basename(Filename.chop_extensionsrc)with"ulexing"|"cryptokit"|"pcre"->" -linkall"|_->""inletincludes="-I "^(Filename.quote(Filename.dirnamesrc))inletcom="ocamlopt -shared -o "^(Filename.quotedst)^" "^includes^options^" "^(Filename.quotesrc)inmatchSys.commandcomwith0->dst|_->failwith("Command failed: "^com);;let_=Dynlink.allow_unsafe_modulestrue;;letload_filefile=matchFilename.chop_extension(Filename.basenamefile)with|"dynlink"|"ocamlcommon"->Log.info(funm->m"Ignoring loading of file %s"file);|_->Log.info(funm->m"Loading file %s"file);tryDynlink.loadfilefilewithDynlink.Errore->matchewith|Dynlink.Module_already_loaded_->()|_->failwith(Dynlink.error_messagee)letloaded_files=ref[];;lethack_load_filefile=(* special case for cryptokit, pcre, by now, who is missing reference to C code in .cmxs *)matchFilename.chop_extension(Filename.basenamefile)with"cryptokit"|"pcre"->letcmxs=create_cmxs((Filename.chop_extensionfile)^".cmxa")inload_filecmxs;Sys.removecmxs|_->matchFilename.check_suffixfile".cmxa"||Filename.check_suffixfile".cmx"withfalse->load_filefile|true->(* let's create a .cmxs from this .cmxa if not corresponding .cmxs exists *)letcmxs=(Filename.chop_extensionfile)^".cmxs"inifSys.file_existscmxsthenload_filecmxselse(letcmxs=create_cmxsfileinload_filecmxs;(*Sys.remove cmxs*));;letcheck_file_has_extensionfile=tryignore(Filename.chop_extensionfile)with_->failwith("Filename "^file^" has no extension.");;;letcheck_files_have_extensionfiles=List.iter(funfile->trycheck_file_has_extensionfilewithFailuremsg->letmsg=msg^"\nDid you mean --package ?"infailwithmsg)filesletload_files=letload_filefile=check_file_has_extensionfile;if!hack_cmxsthenhack_load_filefileelseload_file(Dynlink.adapt_filenamefile)inletffile=ifList.memfile!loaded_filesthenLog.info(funm->m"Not loading already loaded file %s"file)elsebeginload_filefile;loaded_files:=file::!loaded_files;endinList.iterf;;letfiles_of_packageskindpkg_names=letfile=Filename.temp_file"stog"".txt"inletcom=Printf.sprintf"ocamlfind query %s -predicates plugin,%s -r -format %%d/%%a > %s"(String.concat" "(List.mapFilename.quotepkg_names))(matchkindwith`Byte->"byte"|`Native->"native")(Filename.quotefile)inmatchSys.commandcomwith0->lets=Stog_base.Misc.string_of_filefileinSys.removefile;Stog_base.Misc.split_strings['\n']|n->letmsg=Printf.sprintf"Command failed (%d): %s"ncominfailwithmsg;;letload_packages_commapkg_names=letkind=ifDynlink.is_nativethen`Nativeelse`Byteinletpkg_names=Stog_base.Misc.split_stringpkg_names[',']inletfiles=files_of_packageskindpkg_namesinload_filesfiles;;letload_packagespackages=List.iterload_packages_commapackages;;