Index: trunk/CMakeLists.txt
===================================================================
--- trunk/CMakeLists.txt	(revision 1956)
+++ trunk/CMakeLists.txt	(revision 1957)
@@ -19,7 +19,12 @@
 message( STATUS "casaroot = " ${casaroot} )
 
+# modules
+set( CMAKE_MODULE_PATH ${casaroot}/code/install )
+include( config )
+include( CASA )
+
 # flags
 set( DEFAULT_CXX_FLAGS
-     "-pipe -Wall -Wextra -Wno-non-template-friend -Wcast-align -Wno-comment" )
+     "-pipe -Wall -Wextra -Wno-non-template-friend -Wcast-align -Wno-comment -O3" )
 find_package( OpenMP )
 if( OPENMP_FOUND )
@@ -86,4 +91,5 @@
 # set flags for cpp compiler
 set( CMAKE_CXX_FLAGS ${DEFAULT_CXX_FLAGS} )
+
 
 #
@@ -144,28 +150,95 @@
 message( STATUS "CASACORE_INCLUDE_DIR = " ${CASACORE_INCLUDE_DIR} )
 
+
 #
 # Python
 #
-# tentative
-set( PYVERSION 2.6 )
-if (NOT PYTHON_ROOT )
-   set( PYTHON_ROOT ${casa_packages} )
-endif()
-find_package( PythonLibs REQUIRED python${PYVERSION} )
-
-if( NOT PYTHONLIBS_FOUND )
-   message( FATAL_ERROR "Python could not be found. Please check!" )
-endif()
-message( STATUS "PYTHON_INCLUDE_PATH = " ${PYTHON_INCLUDE_PATH} )
-message( STATUS "PYTHON_LIBRARIES = " ${PYTHON_LIBRARIES} )
-
-set( PYTHON_INCLUDE_DIR ${PYTHON_INCLUDE_PATH} )
-find_path( NUMPY_INC_DIR numpy/npy_interrupt.h
-           PATHS ${casa_packages}/lib/python${PYVERSION}/site-packages/numpy/core/include )
-message( STATUS "NUMPY_INC_DIR = " ${NUMPY_INC_DIR} )
-if( NOT NUMPY_INC_DIR STREQUAL NUMPY_INC_DIR-NOTFOUND ) 
-   list( APPEND PYTHON_INCLUDE_DIR ${NUMPY_INC_DIR} )
-endif()
+if( NOT PYTHON_FOUND )
+
+  if ( NOT PYTHON_LIBNAME )
+    set( _names 2.9 2.8 2.7 2.6 2.5.2 2.5 )
+    # (The library named libpython.2.5.2.dylib seems to exist only in the CASA world.)
+  else()
+    set( _names ${PYTHON_LIBNAME} )
+  endif()
+
+  set( _found False )
+  foreach( _v ${_names} )
+
+    casa_find( 
+      PYTHON${_v} 
+      LIBS python${_v} 
+      NO_REQUIRE 
+      )
+
+    if( PYTHON${_v}_FOUND )
+      set( PYTHON_LIBNAME ${_v} )
+      set( _found True )
+      break()
+    endif()
+
+  endforeach()
+  
+  if( NOT _found )
+    message( FATAL_ERROR "Could not find any PYTHON library with version one of ${_names}. Please check!" )
+  endif()
+
+endif()
+
+if( NOT PYTHON_LIBNAME )
+  # Found Python, but PYTHON_LIBNAME is undefined, that is impossible.
+  message( FATAL_ERROR "The variable PYTHON_LIBNAME is undefined. Most likely, CMake's cache is out of date and you need to redetect your PYTHON installation (\"cmake -U PYTHON*\")")
+endif()
+
+string( SUBSTRING ${PYTHON_LIBNAME} 0 3 PYTHONV )
+string( REPLACE "." "" PV ${PYTHONV} )
+set( python_library python${PYTHON_LIBNAME} )
+
+# Form the Python install prefix by stripping away "lib/libpython2.5.2.dylib" (example) from 
+# the full python library path
+string( REGEX MATCH "/lib(64)?/lib${python_library}" _match ${PYTHON${PYTHON_LIBNAME}_LIBRARIES} )
+if( _match )
+  string( REGEX REPLACE "/lib(64)?/lib${python_library}.*" "" python_prefix ${PYTHON${PYTHON_LIBNAME}_LIBRARIES} )
+else()
+  # Python library was not in a lib(64) directory!
+  message( WARNING "Python library path \"${PYTHON${PYTHON_LIBNAME}_LIBRARIES}\" does not contain \"/lib(64)/lib${python_library}\"" )
+  set( python_prefix ${casa_packages} )
+endif()
+
+# The python version and prefix is known, do the actual search
+if( NOT PYTHON_FOUND )
+  message( STATUS "Looking for PYTHON version ${PYTHONV}.x in ${python_prefix}" )
+endif()
+
+casa_find( PYTHON
+  VERSION 2.5    # minimum required
+  INCLUDES Python.h 
+     numpy/npy_interrupt.h   # for numpy
+  INCLUDES_SUFFIXES python${PYTHONV}
+  PREFIX_HINTS 
+     ${python_prefix}/lib/python${PYTHONV}/site-packages/numpy/core
+     ${python_prefix}/Library/Frameworks/Python.framework/Versions/${PYTHONV}
+     ${python_prefix}/Library/Frameworks/Python.framework/Versions/${PYTHONV}/lib/python${PYTHONV}/site-packages/numpy/core
+  LIBS ${python_library}
+  CPP_VERSION PY_VERSION )
+
+# Store PYTHON_LIBNAME in the cache
+set( PYTHON_LIBNAME ${PYTHON_LIBNAME} CACHE STRING "Python major and minor version to use" FORCE )
+
+# Define pyroot to two directory levels above Python.h.
+string( REGEX REPLACE "/[^/]+/[^/]+/?$" "" pyroot ${PYTHON_Python.h} )
+
 message( STATUS "PYTHON_INCLUDE_DIR = " ${PYTHON_INCLUDE_DIR} )
+message( STATUS "PYTHON_LINRARIES = " ${PYTHON_LIBRARIES} )
+message( STATUS "PYTHONV = " ${PYTHONV} )
+
+set( PYTHON_DEFINITIONS ${PYTHON_DEFINITIONS}
+  -DAIPSROOT=\"${CMAKE_SOURCE_DIR}\"
+  -DAIPSARCH=\"${arch}\"
+  -DAIPSSITE=\"garching\"
+  -DPYTHONROOT=\"${pyroot}\"
+  -DPYTHONVER=\"${PYTHONV}\"
+  -DPYVERSION=${PV} )
+
 
 #
@@ -176,35 +249,105 @@
 endif()
 
-set( boost_components regex program_options filesystem )
+set( boost_components python )
 find_package( Boost REQUIRED ${boost_components} )
-
 if( NOT Boost_FOUND )
   message( FATAL_ERROR "Boost could not be found. Please check!" )
 endif()
-
-# For boost >= 1.35, we need to link also to boost-system
-if( Boost_VERSION GREATER 103499 )
-
-  # Force redetection by resetting these variables
-  if( NOT Boost_SYSTEM_FOUND )
-    message( STATUS "Boost version (${Boost_VERSION}) is 1.35.0 or newer" )
-    unset( Boost_FOUND )
-    unset( Boost_INCLUDE_DIR CACHE )
+message( STATUS "BOOST_INCLUDE_DIR = " ${Boost_INCLUDE_DIR} )
+message( STATUS "BOOST_LIBRARIES = " ${Boost_LIBRARIES} )
+
+
+#
+# cfitsio
+#
+casa_find( CFITSIO
+           VERSION 3.006
+           INCLUDES fitsio.h fitsio2.h
+           INCLUDES_SUFFIXES cfitsio
+           LIBS cfitsio
+           CPP_VERSION CFITSIO_VERSION
+           RUN_VERSION "(ffvers(&v), v)" )
+if( NOT CFITSIO_FOUND )
+   message( FATAL_ERROR "CFITSIO could not be found. Please check!" )
+endif()
+message( STATUS "CFITSIO_INCLUDE_DIRS = " ${CFITSIO_INCLUDE_DIRS} )
+message( STATUS "CFITSIO_LIBRARIES = " ${CFITSIO_LIBRARIES} )
+
+
+#
+# wcslib
+#
+set( _wcslib libwcs.${SO} )
+find_library( WCSLIB ${_wcslib} 
+              HINTS /usr/lib64 /usr/lib ${casaroot}/${arch}/lib )
+if( WCSLIB STREQUAL WCSLIB-NOTFOUND )
+   message( FATAL_ERROR "${_wcslib} could not be found. Please check!" )
+endif()
+message( STATUS "WCSLIB = " ${WCSLIB} )
+
+
+#
+# RPFITS (Fortran)
+#
+if( NOT FORTRAN_LIBRARIES )
+
+  message( STATUS "Looking for Fortran runtime libraries" )
+  set( _try  ${CMAKE_BINARY_DIR}/try_fortran.cc )
+  file( WRITE ${_try}
+    "int main() { return 0; }\n"
+    )
+  
+  if( _gfortran_lib_path )
+    try_compile( _have_gfortran ${CMAKE_BINARY_DIR} ${_try}
+      CMAKE_FLAGS -Wdev "-DCMAKE_EXE_LINKER_FLAGS=${_gfortran_lib_path}"
+      )
+  else()
+    try_compile( _have_gfortran ${CMAKE_BINARY_DIR} ${_try}
+      CMAKE_FLAGS -Wdev "-DCMAKE_EXE_LINKER_FLAGS=-lgfortran"
+      )
   endif()
-  set( boost_components regex program_options filesystem system )
-  find_package( Boost REQUIRED ${boost_components} )
-
-  if( NOT Boost_FOUND )
-    message( FATAL_ERROR "Boost-system could not be found. Please check!" )
+  try_compile( _have_g2c ${CMAKE_BINARY_DIR} ${_try}
+    CMAKE_FLAGS -Wdev "-DCMAKE_EXE_LINKER_FLAGS=-lg2c"
+    )
+ 
+  if( _have_gfortran )
+    if( _gfortran_lib_path )
+      set( FORTRAN_LIBRARIES ${_gfortran_lib_path}
+	CACHE STRING "Fortran library linker option" FORCE )
+    else()
+      set( FORTRAN_LIBRARIES -lgfortran 
+        CACHE STRING "Fortran library linker option" FORCE )
+    endif()
+    message( STATUS "Looking for Fortran runtime libraries -- ${FORTRAN_LIBRARIES}" )
+  elseif( _have_g2c )
+    set( FORTRAN_LIBRARIES -lg2c
+      CACHE STRING "Fortran library linker option" FORCE )
+    message( STATUS "Looking for Fortran runtime libraries -- ${FORTRAN_LIBRARIES}" )
+  else()
+    set( FORTRAN_LIBRARIES ""
+      CACHE STRING "Fortran library linker option" FORCE )
+    message( STATUS "Looking for Fortran runtime libraries -- <none>" )
+    # Not a fatal error because it might work, if all Fortran dependencies were
+    # already linked statically to the Fortran runtime...
   endif()
-endif() 
-
-if( NOT Boost_VERSION GREATER 103199 AND NOT CASA_IGNORE_VERSION )
-  message( FATAL_ERROR "Boost version (${Boost_VERSION}) is too old! Must be 103200 (1.32.0) or newer. Please check!" )
-endif()
-
-
-
-# include path
+endif()
+
+casa_find( RPFITS
+  VERSION 2.11
+  INCLUDES RPFITS.h
+  LIBS rpfits
+  RUN_VERSION names_.rpfitsversion
+  DEPENDS FORTRAN 
+)
+if( NOT RPFITS_FOUND )
+   message( FATAL_ERROR "${_wcslib} could not be found. Please check!" )
+endif()
+message( STATUS "RPFITS_INCLUDE_DIR = " ${RPFITS_INCLUDE_DIR} )
+message( STATUS "RPFITS_LIBRARIES = " ${RPFITS_LIBRARIES} )
+
+
+#
+# common include path
+#
 message( STATUS "Boost_INCLUDE_DIR = " ${Boost_INCLUDE_DIR} )
 include_directories( ${CASACORE_INCLUDE_DIR} 
@@ -212,21 +355,38 @@
                      ${PYTHON_INCLUDE_DIR} )
 
+#
 # link path
+#
 set( CASACORE_LIBRARY_DIR ${casaroot}/${arch}/lib )
 link_directories( ${CASACORE_LIBRARY_DIR} )
 
 
+#
 # install directory
+#
 set( EXEC_INSTALL_DIR bin )
 set( LIB_INSTALL_DIR lib )
-set( PYTHON_INSTALL_DIR python/${PYVERSION}/asap )
+set( PYTHON_INSTALL_DIR python/${PYTHONV}/asap )
 set( SHARED_INSTALL_DIR share/asap )
 
+
+#
+# libraries and executables
+#
+set( ASAPLIB _asap )
+set( PYRAPLIB pyrap )
+set( ATNFLIB atnf )
+set( ASAP2TO3 asap2to3 )
+
+
+#
 # subdirectories
-#  asap2to3       apps
-#  libpyrap.so    external/libpyrap
-#  libatnf.so     external-alma/atnf
-#  _asap.so       src
-#  python modules python
+#  ASAP2TO3 asap2to3       apps
+#  PYRAPLIB libpyrap.so    external/libpyrap
+#  ATNFLIB  libatnf.so     external-alma/atnf
+#  ASAPLIB  _asap.so       src
+#  python modules           python
+#  shared files             share
+#
 add_subdirectory( apps )
 add_subdirectory( external/libpyrap )
