; _______________________________________________________________________________________
;|                                                                                       |
;| ..::FreshLib::..  Free, open source. Licensed under "BSD 2-clause" license."          |
;|_______________________________________________________________________________________|
;
;  Description: Executable file formating macro library.
;
;  Target OS: Win32
;
;  Dependencies:
;
;  Notes:
;_________________________________________________________________________________________

TargetOS equ Win32
strTargetOS equ 'Win32'

; model can be "compact", "default", "manual"

macro @BinaryType type, model {
  if type eq GUI
    format PE GUI 4.0
    entry start
  end if

  if type eq console
    format PE Console 4.0
    entry start
  end if

  if type eq DLL
    format PE GUI 4.0 DLL
    entry EntryPoint
  end if


  postpone \{
    ?FAKE_COMPILE = 0
    match =TRUE, ___FAKE_COMPILE \\{
      ?FAKE_COMPILE = 1
    \\}

    if ?FAKE_COMPILE = 0
  ; imports
      if model eq compact
        data import
      else
        if (model eq )|(model eq default)
          section '.idata' import readable
        else
        end if
      end if

      if ~(model eq manual)
        \__create_all_imports
      end if

      if model eq compact
        end data
      end if

  ; global data
      if (model eq )|(model eq default)
        section '.data' data readable writeable
      end if

      if ~(model eq manual)
        \IncludeAllGlobals
      end if
    end if
  \}

  section '.code' code readable writeable executable
}



macro __create_dll_list {
  DynamicLibList equ
  rept DynamicLibList@count i:1 \{
    match any, DynamicLibList \\{
      match name:filename, DynamicLibList\\#i \\\{
        DynamicLibList equ DynamicLibList, name, \\\`name\\\#'.dll'
      \\\}
    \\}

    match , DynamicLibList \\{
      match name:filename, DynamicLibList\\#i \\\{
        DynamicLibList equ name, \\\`name\\\#'.dll'
      \\\}
    \\}
  \}
}

macro __create_all_imports {
   __create_dll_list

   match liblist, DynamicLibList \{
     library liblist
   \}

  rept DynamicLibList@count i:1 \{
    match name:filename, DynamicLibList\#i \\{
      \\include filename
    \\}
  \}
}


;macro @AllImportSection {
;  section '.idata' import readable
;    __create_all_imports
;}


;macro @AllImportEmbeded {
;  data import
;    __create_all_imports
;  end data
;}






