;;; ----------------------------------------------------------------------
;;;
;;; ILTBINDCPLIB: Generates a binding to the C++ standard library
;;;
;;; ----------------------------------------------------------------------
;;;
;;; This file is part of ILOG Talk.
;;; Inquiries to ILOG S.A.
;;;              9 rue de Verdun, BP 85,
;;;              94253 Gentilly Cedex, France.
;;;
;;; Copyright (c) 1994-1996 by ILOG
;;; ----------------------------------------------------------------------
;;;
;;; Call
;;;
;;;     (bind-cplib)
;;;
;;; This will generate in the $ILT_DIR/iltcplib hierarchy:
;;;
;;; - the libraries libiltcplibe and libiltcplibm
;;; - the workspace iltcplibws.
;;;

(defun bind-cplib ()
  (.setup-environment)
  (.build-externals)
  (.build-binding)
  (.build-prunits)
  'done)

(defun .setup-environment ()
  ;; Setup the environment for the Views binding generation.
  (fprintf (stderr) "\nPreparing workspace\n")
  (fprintf (stderr) "-------------------\n")
  (unless (access #f"${ILT_DIR}/iltcplib/${PORTNAME}")
    (mkdir #f"${ILT_DIR}/iltcplib/${PORTNAME}")
    (mkdir #f"${ILT_DIR}/iltcplib/${PORTNAME}/o"))
  (unless (access #f"${ILT_DIR}/iltcplib/c")
    (mkdir #f"${ILT_DIR}/iltcplib/c"))
  (pushf (workspace-path) #f"${ILT_DIR}/iltcplib/src")
  (with-frozen-td-cache
    (setf (tdref 'iltcplibsws 'program-units:) ())
    (write-td-file 'iltcplibsws))
  (build-program-unit 'iltcplibsws)  
  (setf (default-workspace) 'iltcplibsws)
  (in-workspaces '(talkws)))

(defun .build-externals ()
  (fprintf (stderr) "\nCompiling externals\n")
  (fprintf (stderr) "-------------------\n")
  (let ((src-dir (sprintf () "$ILT_DIR/iltcplib/common"))
	(dst-dir (sprintf () "$ILT_DIR/iltcplib/%s/o" (port-name))))
    (for ((e in: '(iltcplibrtecc)))
      (fprintf (stderr) "Compiling %s\n" e)
      (system
       (sprintf () "llcpp %s %s %s -d standard"
		e dst-dir src-dir)))))

(defun .build-binding ()
  (bind-cpp-api '(#f"iltiostream.h")
                #f"${ILT_DIR}/iltcplib/src/iltcplib"
                cppparse-options: "-DILT_BINDING_TIME"
                cpp-options: .cpp-options
                link-options: .link-options
                buildp: ()
) )

(defun .build-prunits ()
  ;; Analyze and build the binding program units.
  (fprintf (stderr) "\nBuilding program units\n")
  (fprintf (stderr) "----------------------\n")
  (for ((p in: '(iltcplibm iltcplibtypm libiltcplibm)))
    (analyze-program-unit p buildp: t))
  (load-program-unit 'libiltcplibm)
  (for ((p in: '(iltcplibrte iltcplibe iltcplibtype libiltcplibe)))
    (analyze-program-unit p buildp: t))
  (build-program-unit 'iltcplibws)
)

(defun .changed-iltcplib-cpp-options ()
  ;; Call this when the .cpp-options have changed.
  (for ((m in: '(iltcplibe)))
    (with-frozen-td-cache
      (setf (tdref m 'cpp-options:) .cpp-options)
      (write-td-file m)
) ) )

(defun .changed-iltcplib-link-options ()
  ;; Call this when the .link-options have changed.
  (for ((m in: '(iltcplibe)))
    (with-frozen-td-cache
      (setf (tdref m 'link-options:) internal.link-options)
      (write-td-file m)
  ) )
  (analyze-program-unit 'libiltcplibe)
  (analyze-program-unit 'italkcplib)
)

(defglobal .cpp-options
  ;; Options to be passed to the C++ compiler.
  '((|sparc_4_2.1|   "-I${ILT_DIR}/iltcplib/common")
    (|sparc_4_3.0|   "-I${ILT_DIR}/iltcplib/common")
    (|sparc_4_4.0|   "-I${ILT_DIR}/iltcplib/common")
    (sparc_4_gnu     "-I${ILT_DIR}/iltcplib/common")
    (|sparc_5_3.0|   "-I${ILT_DIR}/iltcplib/common")
    (|sparc_5_4.0|   "-I${ILT_DIR}/iltcplib/common")
    (sparc_5_gnu     "-I${ILT_DIR}/iltcplib/common")
    (hp700           "-I${ILT_DIR}/iltcplib/common")
    (hp_10           "-I${ILT_DIR}/iltcplib/common")
    (rs6000          "-I${ILT_DIR}/iltcplib/common")
    (|sgi_5.2_3|     "-I${ILT_DIR}/iltcplib/common")
    (|sgi_5.3_4|     "-I${ILT_DIR}/iltcplib/common")
    (|sgi_6.2_6.2|   "-I${ILT_DIR}/iltcplib/common")
    (alpha_3         "-I${ILT_DIR}/iltcplib/common")
    (|i86_linux1.2_gnu2.7| "-I${ILT_DIR}/iltcplib/common")
    (|i86_sunos5_3.0| "-I${ILT_DIR}/iltcplib/common")
    (|i86_sunos5_4.0| "-I${ILT_DIR}/iltcplib/common")
    (msvc32          "/I%ILT_DIR%\\iltcplib\\common")
    (msvc4           "/I%ILT_DIR%\\iltcplib\\common")
   )
)

(defglobal .link-options
  ;; Options to be passed to the linker.
  '((|sparc_4_2.1| "-lC")
    (|sparc_4_3.0| "-lC")
    (|sparc_4_4.0| "-lC")
    (|sparc_5_3.0| "/usr/lib/libC.so.3")
    (|sparc_5_4.0| "/usr/lib/libC.so.5")
    (|i86_sunos5_3.0| "/usr/lib/libC.so.3")
    (|i86_sunos5_4.0| "/usr/lib/libC.so.5")
    (hp700 "-lC")
    (hp_10 "-lC")
    (rs6000 "-L/usr/lpp/xlC/lib -lC")
    (|sgi_5.2_3| "-lC")
    (|sgi_5.3_4| "-lC")
    (|sgi_6.2_6.2| "-lC")
    (alpha_3 "-L/lib/cmplrs/cxx -lcxx"))
)
