ó
±Mc           @   sˆ   d  Z  d d l Z d d l m Z m Z m Z m Z m Z m Z d „  Z	 d „  Z
 e d k r„ d d l Z e
 e j ƒ e j ƒ  n  d S(   s  
#+
# NAME:
#	mk
# CALLING SEQUENCE:
#	mk.py main sub1 sub2 ... -dir=<dir>
# INPUTS:
#	main sub1 sub2 ...
#		name of main program, followed by additional
#		files needed for the compilation.
#		The file type, if specified, is ignored and is
#		replaced by .f
# OPTIONAL INPUTS:
#	-g77		use g77 compiler
#	-intel		use Intel Fortran 90 compiler
#	-pgi		use Portland compiler
#	-absoft 	use Absoft Fortran compiler
#
#	-nogen		do not include the forgen library
#	-nofits		do not include the cfitsio library
#	-noincl		do not use default include paths
#
#	If none of these command line switches is set then
#	the script will try to locate g77 first, then the Intel
#	compiler.
#
#	-dir=dir	destination directory for object files or executable/
#			The default destination is the current directory.
#	-cc		include libraries for linking C/C++ code
#	-static		adds switch -static to compile command
#	-obj		only creates object files (used by olb script)
#	-verbose	verbose mode
#	-keep_id	if set, the file "get_compiler.f" is NOT deleted.
# OUTPUTS:
#	The executable will be named 'main' (the name of the main program).
# RESTRICTIONS:
#	The g77 command includes the switches
#		-ffixed-line-length-none
# SIDE EFFECTS:
#	Cannot be executed in the 'root' account.
#
#	Include directories:
#		$SMEI_FOR/h, $SMEI_FOR/h/linux and $SMEI/for/h
#	Library directory  :
#		Libraries should be located in $SMEI_LIB or $FOR
#		Two libraries are expected: gen and cfitsio.
#		$SMEI_LIB should always contain these.
# MODIFICATION HISTORY:
#	JAN-2000, Paul Hick (UCSD/CASS)
#	JAN-2001, Paul Hick (UCSD/CASS)
#		Added loop to handle compilation of  multiple files.
#	SEP-2002, Paul Hick (UCSD/CASS)
#		Removed -fdollar-ok option from g77 compilations (all dollar
#		signs in variable names should have been removed by now).
#	DEC-2003, Paul Hick (UCSD/CASS)
#		Added -lcfitsio to compile command (for Fortran FITS routines).
#	MAY-2004, Paul Hick (UCSD/CASS)
#		Modifications to get this to run on the stesun Unix machines.
#		Use awk instead of gawk (on our Linux boxes awk is a soft link
#		to gawk). Changed the syntax for variable assignment from
#			awk -v i=$i '{print $i}' to awk '{print $i}' i=$i
#		The first one doesn't work properly on the stesun machines.
#	JUL-2004, Paul Hick (UCSD/CASS)
#		Added -Vaxlib switch for Intel compiler
#	SEP-2004, Paul Hick (UCSD/CASS)
#		The default location for the libgen library is now $SMEI_LIB
#		(was $FOR).
#	OCT-2004, Paul Hick (UCSD/CASS)
#		Added Portland compiler.
#	JAN-2005, Paul Hick (UCSD/CASS)
#		Added -cc switch (will add a couple of C/C++ libraries
#		needed to cross compile Fortran and C/C++ code).
#	JAN-2005, Paul Hick (UCSD/CASS)
#		Converted from bash to Python.
#	JUL-2005, Paul Hick (UCSD/CASS)
#		Added code to handle .o, .c and .cpp files
#		The main program still must be a Fortran program.
#	MAR-2007, Paul Hick (UCSD/CASS)
#		SpatialIndex library is no longer included for linking.
#	OCT-2007, Paul Hick (UCSD/CASS; pphick@ucsd.edu)
#		Added -nogen and -nofits keyword
#		Added test for existence of get_compiler_<f77id> function.
#		If it exists already, use it. If not, create one.
#		Previously the file was always created, resulting with
#		a conflict with an existing one.
#	MAR-2011, John Clover (UCSD/CASS)
#		Modified f77cc for intel compiler to build for 64 bit only
#- 
iÿÿÿÿN(   t   is_theret   startt   argst   hide_envt   whicht   run_cmdc         C   sk   d d  l  } t j d | _ | j d ƒ } d d d |  d d d	 g } t | d
 ƒ j d j | ƒ ƒ | S(   Niÿÿÿÿt   TUBs   get_compiler.fs   	subroutine get_compiler(f77id)s   	character	f77id*(*)s
   	f77id = 't   's   	returns   	endt   ws   
(   t   tempfilet   ost   environt   tempdirt   mktempt   opent   writet   join(   t   f77idR	   t   content(    (    s%   /home/soft/smei/ucsd/gen/python/mk.pyt   set_compiler^   s    	c   5      C   sœ  t  d g  f d g  f g ƒ } d } t j d } | d k rJ | d GH| St d |  ƒ } t d |  ƒ } t d	 |  ƒ } t d
 |  ƒ } t d |  ƒ } t d |  ƒ }	 t d |  ƒ }
 t d |  ƒ } t d |  ƒ } t d |  ƒ } | d k rt } t j d } n1 | j d ƒ d k } | r3| } t j d } n  g  } g  } g  } g  } | rìt j t j d ƒ } x¬ | D]¤ } t j j	 | ƒ } | d d k rª| j
 | d ƒ n  | d d k rÎ| j
 | d ƒ n  | d d k rò| j
 | d ƒ n  | d d k rn| j
 | ƒ qnqnWd } x­ | D]˜ } t t j j d | d ƒ ƒ j ƒ  } | j d ƒ } xQ | D]I } t | ƒ d k rd| j d ƒ j ƒ  j d  ƒ d k } | r­Pq­qdqdW| r#Pq#q#W| d! GH| S| j | ƒ | j d | ƒ nÞ t |  ƒ } | d t | ƒ !} x¼ | D]´ } t j j	 | ƒ } | d d k sJ| d d k r^| j
 | d ƒ n  | d d k r‚| j
 | d ƒ n  | d d k r¦| j
 | d ƒ n  | d d k r| j
 | ƒ qqWt | ƒ d k ré| d" GH| St j j | ƒ s| t | ƒ d# GH| St d$ |  ƒ r*d% } nf t d& |  ƒ rBd' } nN t d( |  ƒ rZd) } n6 t d* |  ƒ rrd+ } n t d, |  ƒ rŠd- } n d } | d k r| } t | ƒ d k rçd } | d) k rçt d. ƒ d k räd. } qäqçn  | d k r–| d/ | d0 GH| Sn‹ t d% ƒ r d% } nv t d' ƒ r5d' } na t d) ƒ rJd) } nL t d. ƒ r_d. } n7 t d+ ƒ rtd+ } n" t d- ƒ r‰d- } n | d1 GH| S| d% k r±d2 } d3 } nl | d' k rÌd2 } d3 } nQ | d) k rçd4 } d5 } n6 | d+ k rd6 } d7 } n | d- k rd } d } n  g  } | d% k rM| d8 } d9 t j d: }  n[| d' k rw| d8 } d9 t j d: }  n1| d. k r®| d; } d9 t j d< }  | j
 d= ƒ nú | d) k r:| d> } d9 t j d< }  | j
 d= ƒ t j d< j d? ƒ d k r| j
 d@ ƒ n  t j d< j dA ƒ d k r¨| dB } q¨nn | d+ k rd| dC } d9 t j dD }  nD | d- k r¨| dE } d9 t j dF }  | j
 dG ƒ | j
 dH ƒ n  | dI t | ƒ GH| rN| d% k rÝ| j
 dJ ƒ qN| d' k rù| j
 dJ ƒ qN| d) k r"| j
 dK ƒ | j
 dJ ƒ qN| d+ k rN| j
 dL ƒ | j
 dM ƒ qNn  | r]d }! nb dN t j j t j dO dP dQ ƒ dR t j j t j dS dQ ƒ dR t j j t j dS dQ dT ƒ }! | rÕ| j
 d ƒ n  g  }" g  }# d dU dS g }$ g  }% xR |$ D]J }& t j j |& ƒ rýt j |& } t j j | ƒ rG	|% j
 | ƒ qG	qýqýWx·| D]¯}' t j j	 |' ƒ }( |( d d k r‡	|( d }( n |' }( d } xk|% D]c}& xPt j |& ƒ D]?\ }) }* }+ x? dV dW dX dY g D]+ } |* j | ƒ d k rÒ	|* j | ƒ qÒ	qÒ	W|+ j |( d ƒ d k } | r°	t j j |) |( d ƒ }, |" j
 |, ƒ | t |, ƒ GHt j j | |( d ƒ }- dZ |- d[ | d\ |! d] |, d^ |- }. t |. | ƒ t j j |- ƒ rÇ
|# j
 |- ƒ n$ | rë
|' | d k rë
| d_ GHd  SPq°	q°	W| rš	Pqš	qš	WqR	Wd d` g }$ g  }% xR |$ D]J }& t j j |& ƒ rt j |& } t j j | ƒ rh|% j
 | ƒ qhqqWx‚| D]z}' t j j	 |' ƒ }( |( d d k r¨|( d }( n |' }( d } x6|% D].}& xt j |& ƒ D]
\ }) }* }+ x6 dY g D]+ } |* j | ƒ d k rê|* j | ƒ qêqêW|+ j |( d ƒ d k } | rÑt j j |) |( d ƒ }, |" j
 |, ƒ | t |, ƒ GHt j j | |( d ƒ }- dZ |- d[ | d\ |, d^ |- }. t |. | ƒ t j j |- ƒ r×|# j
 |- ƒ n  PqÑqÑW| r»Pq»q»WqsWd d` g }$ g  }% xR |$ D]J }& t j j |& ƒ r
t j |& } t j j | ƒ rT|% j
 | ƒ qTq
q
Wx†| D]~}' t j j	 |' ƒ }( |( d d k r”|( d }( n |' }( d } x:|% D]2}& xt j |& ƒ D]\ }) }* }+ x6 dY g D]+ } |* j | ƒ d k rÖ|* j | ƒ qÖqÖW|+ j |( d ƒ d k } | r½t j j |) |( d ƒ }, |" j
 |, ƒ | t |, ƒ GHt j j | |( d ƒ }- dZ |- d[ | d\ d] |, d^ |- }. t |. | ƒ t j j |- ƒ rÇ|# j
 |- ƒ n  Pq½q½W| r§Pq§q§Wq_W|# j | ƒ t |" ƒ d k r| da GH| St |# ƒ d k r,| db GH| S| r]t  d | | |  g f d |# f g ƒ }/ |/ St dc |  ƒ }0 |0 d k r±| d }0 t j j |0 ƒ d }0 t j j	 |0 ƒ d }0 n  t j j | |0 ƒ }0 dd t j de g }1 t j j dU ƒ r"t j dU } t j j | ƒ r"|1 j
 dd | ƒ q"n  g  }2 |	 s>|2 j
 df ƒ n  |
 sT|2 j
 dg ƒ n  g  }3 xs |2 D]k }4 t j j t j de dh |4 |  di ƒ } t j j | ƒ r¹|3 j
 dj |4 |  ƒ qa| t | ƒ dk GHqaWdl |  }, | j |, ƒ d k r†t |  ƒ }, t j j	 |, ƒ d d }- | d\ |, d^ |- }. t |. | ƒ t j j |- ƒ rZ|# j
 |- ƒ n  | rv| t |, ƒ dm GHq†t j |, ƒ n  | d] |! d] d] j |1 ƒ d] d] j |# ƒ d] d] j |3 ƒ d] d] j | ƒ d^ |0 }. t |. | ƒ t j j |0 ƒ rD| dn t |0 ƒ GH| rWt do |0 d] | t ƒ t j |0 ƒ qWn | dp t |0 ƒ GHt dZ d] j |# ƒ | ƒ t  d | | |  g f dq |0 f g ƒ }/ |/ S(r   Nt   f77t   objs   mk, t   USERt   roots   do not use from root accounts   -ccs   -statics   -verboses   -objs   -alls   -nogens   -nofitss   -noincls   -keep_ids   -dir=t    t   PWDt   :iÿÿÿÿR   i   s   .fi    s   .cs   .cpps   .ot   .s   
i	   i   s          program s   no main program founds(   usage: mk.py main file1 file2 -dir=<dir>s    does not exists   -g77t   g77s	   -gfortrant   gfortrans   -intelt   iforts   -pgit   pgf77s   -absoftt   abf77t   ifcs	   compiler s    not availables   no Fortran compiler foundt   gccs   g++t   icct   icpct   pgcct   pgCCs    -w -ffixed-line-length-nonet   _t   GNU_COMPILERs    -w -132t   INTEL_COMPILERs   -VaxlibsD    -O2 -shared-intel -mcmodel=large -parallel -w -132 -assume byterecls	   intel_9.0s   -no-ipos	   intel_9.1s    -warn nouncalleds    -w -Mextendt   PGI_COMPILERs    -W -V -N109t   ABSOFT_COMPILERs   -lU77s   -lV77s   using s   -lstdc++s   -cxxlib-gccs   -lCs   -lstds   -It   SMEIt   fort   hs    -It   SMEI_FORt   linuxt   FORt   wint   unixt   vmst   CVSs   rm -f s   ; s    -c t    s    -os    error compiling main programt   SMEI_CPPs   no source file(s) specifieds   no objects file(s) createds   -name=s   -Lt   SMEI_LIBt   gent   cfitsiot   libs   .as   -ls
    not foundt   get_compiler_s    not deleteds   exec: s   scp s   fail: t   exe(    t   dictR
   R   R    R   t   Falset   findt   listdirt   patht   splitextt   appendR   R   t   readt   splitt   lent
   expandtabst   lowert   removet   insertR   t   isdirR   R   t   has_keyt   walkt   countR   t   isfilet   extendR   t   True(5   t   argvt	   no_resultt   sayt   usert   incl_cct   statict   verboseR   t   allt   nogent   nofitst   noinclt   keep_idt   out_dirt	   to_remotet
   remote_dirt   fortran_filest   cminus_filest   cplus_filest   object_filest   namest   namet   tmpt   foundt   maint   lineR   t   f77_savet   cct   cppt   f77linkt   f77ccR   t   include_f77t   sourcest   objectst   top_dirst   source_dirst
   source_dirt   entryt	   base_nameR   t   dirst   filest   sourcet   objectt   cmdt   statust   exenamet   libsdirt   all_libst   libsR;   (    (    s%   /home/soft/smei/ucsd/gen/python/mk.pyt   mko   s|   	%$	 																		





	b*	"&		'
  (
Z't   __main__(   t   __doc__R
   t   tinyR    R   R   R   R   R   R   Rƒ   t   __name__t   sysRS   t   exit(    (    (    s%   /home/soft/smei/ucsd/gen/python/mk.pyt   <module>Z   s   .		ÿ Ò