123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140(**************************************************************************)(* *)(* OCaml *)(* *)(* Florian Angeletti, projet Cambium, Inria Paris *)(* *)(* Copyright 2023 Institut National de Recherche en Informatique et *)(* en Automatique. *)(* *)(* All rights reserved. This file is distributed under the terms of *)(* the GNU Lesser General Public License version 2.1, with the *)(* special exception on linking described in the file LICENSE. *)(* *)(**************************************************************************)typeintf_or_impl=Intf|Impltypemodname=stringtypefilename=stringtypefile_prefix=stringtypeerror=Invalid_encodingofstringexceptionErroroferrortypet={source_file:filename;prefix:file_prefix;modname:modname;kind:intf_or_impl;}letsource_file(x:t)=x.source_fileletmodname(x:t)=x.modnameletkind(x:t)=x.kindletprefix(x:t)=x.prefixletbasename_chop_extensionsbasename=matchString.indexbasename'.'with|dot_pos->String.subbasename0dot_pos|exceptionNot_found->basenameletstrict_modulizes=matchMisc.Utf8_lexeme.capitalizeswith|Okx->x|Error_->raise(Error(Invalid_encodings))letmodulizes=matchMisc.Utf8_lexeme.capitalizeswithOkx|Errorx->x(* We re-export the [Misc] definition, and ignore encoding errors under the
assumption that we should focus our effort on not *producing* badly encoded
module names *)letnormalizex=Misc.normalized_unit_filenamexletstemsource_file=source_file|>Filename.basename|>basename_chop_extensionsletstrict_modname_from_sourcesource_file=source_file|>stem|>strict_modulizeletlax_modname_from_sourcesource_file=source_file|>stem|>modulize(* Check validity of module name *)letis_unit_namename=Misc.Utf8_lexeme.is_valid_identifiernameletcheck_unit_namefile=ifnot(is_unit_name(modnamefile))thenLocation.prerr_warning(Location.in_file(source_filefile))(Warnings.Bad_module_name(modnamefile))letmake?(check_modname=true)~source_filekindprefix=letmodname=strict_modname_from_sourceprefixinletp={modname;prefix;source_file;kind}inifcheck_modnamethencheck_unit_namep;pmoduleArtifact=structtypet={source_file:filenameoption;filename:filename;modname:modname;}letsource_filex=x.source_fileletfilenamex=x.filenameletmodnamex=x.modnameletprefixx=Filename.remove_extension(filenamex)letfrom_filenamefilename=letmodname=lax_modname_from_sourcefilenamein{modname;filename;source_file=None}endletmk_artifactextu={Artifact.filename=u.prefix^ext;modname=u.modname;source_file=Someu.source_file;}letcompanion_artifactextx={xwithArtifact.filename=Artifact.prefixx^ext}letcmif=mk_artifact".cmi"fletcmof=mk_artifact".cmo"fletcmxf=mk_artifact".cmx"fletobjf=mk_artifactConfig.ext_objfletcmtf=mk_artifact".cmt"fletcmtif=mk_artifact".cmti"fletannotf=mk_artifact".annot"fletcompanion_objf=companion_artifactConfig.ext_objfletcompanion_cmtf=companion_artifact".cmt"fletcompanion_cmif=letprefix=Misc.chop_extensionsf.Artifact.filenamein{fwithArtifact.filename=prefix^".cmi"}letmli_from_artifactf=Artifact.prefixf^!Config.interface_suffixletmli_from_sourceu=letprefix=Filename.remove_extension(source_fileu)inprefix^!Config.interface_suffixletis_cmif=Filename.check_suffix(Artifact.filenamef)".cmi"letfind_normalized_cmif=letfilename=modnamef^".cmi"inletfilename=Load_path.find_normalizedfilenamein{Artifact.filename;modname=modnamef;source_file=Somef.source_file}letreport_error=function|Invalid_encodingname->Location.errorf"Invalid encoding of output name: %s."namelet()=Location.register_error_of_exn(function|Errorerr->Some(report_errorerr)|_->None)