FICO
FICO Xpress Optimization Examples Repository
FICO Optimization Community FICO Xpress Optimization Home
Back to examples browserPrevious exampleNext example

Generate an executable from a Mosel source file

Description
The 'mosdeploy' tool generates an executable from a Mosel source or bim file using the 'deploy' module. The result is either a binary file (produced thanks to a C-compiler) or a self-contained script embedding the bim file.

Usage (generation of a shell script, the type is determined depending on the detected operating system):
mosel mosdeploy.mos -- myfile.mos
or by first generating a shell script from this file:
mosel mosdeploy.mos -- mosdeploy.mos
mosdeploy myfile.mos
Alternatively, it is also possible to generate an executable from this file (requires a C-compiler):
mosel mosdeploy.mos -- -exe mosdeploy.mos
Other options include the specification of an output filename:
mosel mosdeploy.mos -- -o runfile myfile.mos
and a preselection of the output format (bat/cmd/sh) independent of the detected operating system:
mosel mosdeploy.mos -- -cmd myfile.mos


Source Files
By clicking on a file name, a preview is opened at the bottom of this page.
mosdeploy.mos[download]





mosdeploy.mos

(!******************************************************
   Mosel Example Programs
   ====================== 

   file mosdeploy.mos 
   ``````````````````
   Generate an executable from a Mosel source or bim file
   using the 'deploy' module.
   The result is either a binary file (produced thanks to a
   C-compiler) or a self-contained script embedding the bim file.

   Usage:
     mosel mosdeploy.mos -- myfile.mos

   (c) 2024 Fair Isaac Corporation
       author: Y. Colombani, Nov 2022
*******************************************************!)
model mosdeploy
version 0.0.1
uses 'deploy','mmsystem','mmjobs'

 declarations
  fn="tmp:"+newmuid                    ! Temporary file for encoded bim
  bim="tmp:"+newmuid                   ! Temporary file for bim file
  iswin=tolower(getsysinfo(SYS_NAME))="windows"

  dest,src:text                        ! Source and destination file names
  force:boolean                        ! Whether to overwrite existing files
  mode:string                          ! Mode of operation:exe/sh/cmd/bat
  useb64:boolean                       ! Use base64 instead of ascii85

 ! Internal subroutines
  procedure banner                     ! Display a banner
  procedure showhelp                   ! Display help text
  function basename(f:text):text       ! Aux. routine returning basename
 end-declarations

 writeln(versionnum("deploy"))
 if argc=1 then
   banner
   showhelp
   exit(0)
 else
  if versionnum("deploy")<3001000: useb64:=true
  repeat
   if argv(2)="-V" then
    banner
    exit(0)
   elif argv(2)="-f" then
    force:=true
   elif argv(2)="-o" and argc>2 then
    shiftargv
    dest:=argv(2)
   elif argv(2)="-exe" and mode="" then
    mode:="exe"
   elif argv(2)="-bat" and mode="" then
    mode:="bat"
   elif argv(2)="-cmd" and mode="" then
    mode:="cmd"
   elif argv(2)="-sh" and mode="" then
    mode:="sh"
   elif argv(2)="-b64" and mode="" then
    useb64:=true
   else
    break
   end-if
   shiftargv
  until argc<2

  if argc<2 or getchar(argv(2),1)=45 then
   banner
   showhelp
   exit(1)
  else
   fname:=argv(2)
  end-if

  if fname.size<5 or bittest(getfstat(fname),SYS_TYP)<>SYS_REG or getfsize(fname)<=0 or
     (not endswith(fname,".bim") and not endswith(fname,".mos")) then
    writeln("File '",fname,"' not found or invalid")
    exit(1)
  end-if

  if endswith(fname,".mos") then
   if compile("",fname,bim)<>0 then
    writeln("Failed to compile '",fname,"'.")
    writeln("Operation aborted.")
    exit(1)
   else
    src:=bim
   end-if
  else
   src:=fname
  end-if

  if mode="" then
   mode:=if(iswin,"bat","sh")
  end-if

  if dest="" then
   dest:=copytext(fname,1,getsize(fname)-4)
   if mode="bat" or mode="cmd" then
    dest+="."+mode
   end-if
  end-if

  if getfstat(dest)<>0 and not force then
   write("File '",dest,"' already exists, overwrite it?[y/N] ");fflush
   ans:="N"
   readln(ans)
   if ans.size<1 or (getchar(ans,1)<>89 and getchar(ans,1)<>121) then
     writeln("Operation aborted.")
     exit(1)
   end-if
  end-if

  if mode="exe" then
   fcopy(src,"deploy.exe:"+dest)
   if getsysstat<>0 then
    writeln("Failed to generate the executable")
    exit(2)
   end-if
  else
   setparam("ioctrl",true)
    fopen(dest,F_OUTPUT)
   setparam("ioctrl",false)
   if getparam("iostatus")<>0 then
    writeln("Failed to create '",dest,"'")
    exit(2)
   end-if
   if mode="sh" then
    writeln('#~~COMMENT12~~
    writeln("# File generated by '",basename(argv(1)),"' version ",getparam("model_version"))
    writeln("# Mosel version: ",getparam("parser_version"));
    if useb64 then
     writeln("exec mosel run \"zlib.deflate:mmssl.base64:deploy.skiphead:_BIMFILE_,$0\" deploy.progname=\"",basename(dest),"\" -- $@")
    else
     writeln("exec mosel run \"zlib.deflate:deploy.ascii85:$0\" deploy.progname=\"",basename(dest),"\" -- $@")
    end-if
   else
    writeln("@echo off")
    writeln("rem File generated by '",basename(argv(1)),"' version ",getparam("model_version"))
    writeln("rem Mosel version: ",getparam("parser_version"));
    if useb64 then
     writeln("mosel run \"zlib.deflate:mmssl.base64:deploy.skiphead:_BIMFILE_,%~f0\" deploy.progname=\"",basename(dest),"\" -- %*")
    else
     writeln("mosel run \"zlib.deflate:deploy.ascii85:%~f0\" deploy.progname=\"",basename(dest),"\" -- %*")
    end-if
    writeln("exit /b")
   end-if
   if useb64 :writeln("_BIMFILE_")
   fclose(F_OUTPUT)
   fcopy(src,if(useb64,"zlib.deflate:mmssl.base64:","zlib.deflate:deploy.ascii85:")+fn)
   if getsysstat<>0 then
    writeln("Failed to encode bim file")
    exit(2)
   end-if
   fcopy(fn,F_BINARY,dest,F_APPEND+F_BINARY)
   if getsysstat<>0 then
    writeln("Failed to append to bim file")
    exit(2)
   end-if
   if mode="sh" and not iswin then
    system("chmod","+x",dest)
   end-if
  end-if
  writeln("File '",dest,"' created")
 end-if

!***********************
!* Display Banner
!***********************
 procedure banner
   writeln("FICO Xpress ",basename(argv(1))," v",getparam("model_version"))
   writeln("(c) Copyright Fair Isaac Corporation 2023-2024. All rights reserved")
   writeln(_("Link date"),": ",getparam("parser_date")," ",getparam("parser_time"))
 end-procedure

!***********************
!* Display some help
!***********************
 procedure showhelp
   writeln_("\nUsage: ",basename(argv(1))," [-sh|-bat|-cmd|-exe|-V] [-f] [-o outfile] filename\n")
   write_(`
  -V: display banner and exit
  -f: overwrite existing file
  -sh: generate a sh script file (for posix systems)
  -cmd: generate a cmd script file (Windows)
  -bat: generate a bat script file (Windows)
  -exe: compile an executable for the current system
  -b64: for embedding the bim file use base64 even if ascii85 is available
  -o outfile: use 'outfile' for the destination file

 The file name must be a source Mosel file or a BIM file.
 By default a script for the current system is generated (bat or sh), the destination
 file name is produced using the provided input by updating the extension as necessary.
`)
 end-procedure

!**********************************************************************
!* Extract the basename of a path (i.e. strip directory and extension)
!**********************************************************************
 function basename(f:text):text
   returned:=pathsplit(SYS_FNAME,f)
   asproc(pathsplit(SYS_EXTN,returned,returned))
 end-function
end-model

Back to examples browserPrevious exampleNext example