PROCEDURE ocp$create_linked_module, crelm (
  name, n: program_name = $required
  component, components, c: list of record
      library: file
      modules: list rest of any of
          program_name
          range of program_name
        anyend = $optional
    recend = $required
  ring_brackets, rb: record
      r1: integer 3..15
      r2: integer 3..15
      r3: integer 3..15
    recend = ($ring $ring $ring)
  retain_common_block, retain_common_blocks, rcb: (ADVANCED) any of
      key
        all
      keyend
      list of program_name
    anyend = $optional
  ignore_section_names, isn: (ADVANCED) boolean = true
  starting_segment, ss: (ADVANCED) integer 0..4097 = $optional
  output, o: file = $optional
  debug_table, dt: file = $optional
  next_available_segment, nas: (VAR ADVANCED) integer = $optional
  application_identifier, ai: (ADVANCED) name = $optional
  defer_entry_points, dep: (BY_NAME ADVANCED) any of
      key
        all, none
        ($not_retained, $nr)
      keyend
      record
        action: key
          ($defer_all_except, $dae)
        keyend
        entry_points: list rest of program_name
      recend
      list of program_name
    anyend = none
  defer_common_blocks, dcb: (BY_NAME ADVANCED) any of
      key
        all, none
      keyend
      record
        action: key
          ($defer_all_except, $dae)
        keyend
        common_blocks: list rest of program_name
      recend
      list of program_name
    anyend = none
  preset_value, pv: (BY_NAME) key
      (zero z)
      (floating_point_indefinite, fpi)
      (infinity, i)
      (alternate_ones, ao)
    keyend = zero
  status)

  "$FORMAT=OFF
  VAR
    local_status: status
    module_library: file = $unique($local)
    unique2: name = $unique(name)
    linked_module: file = $local//unique2
  VAREND
  "$FORMAT=ON"

  WHEN any_fault DO
    detach_file file=module_library status=local_status
    detach_file file=linked_module status=local_status
    EXIT_PROC WITH osv$status
  WHENEND

  CREATE_OBJECT_LIBRARY
    FOR EACH component IN components DO
      IF $field(component modules initialized) THEN
        add_module library=component.library modules=component.modules
      ELSE
        add_module library=component.library
      IFEND
    FOREND

    generate_library library=module_library
  QUIT
  LINK_VIRTUAL_ENVIRONMENT
    set_link_option mode=product
    set_link_option build_level=$substr($string(name) 1 22)
    set_link_option ignore_section_names=ignore_section_names
    set_link_option starting_segment=starting_segment
    add_object_file file=module_library ring_brackets=ring_brackets
    set_link_option preset_value=preset_value
    set_link_option link_map=output defer_entry_points=defer_entry_points ..
          defer_common_blocks=defer_common_blocks
    IF $specified(retain_common_blocks) THEN
      retain_common_blocks name=$apply(retain_common_blocks $string(x))
    IFEND
    generate_virtual_memory virtual_image=linked_module debug_table=debug_table
    IF $specified(next_available_segment) THEN
      next_available_segment = $next_available_segment
    IFEND
  QUIT

  detach_file file=module_library
  add_module library=linked_module return_file_when_complete=true

" The linker builds the product module with the module name equal to the last path name
" of the file specified for the virtual_image parameter on the GENERATE_VIRTUAL_MEMORY
" command.  The module name must be changed to the name specified on the request.

  change_module_attributes module=unique2 new_name=$string(name) application_identifier=application_identifier

PROCEND ocp$create_linked_module
