← Back to team overview

dolfin team mailing list archive

Re: [Branch ~dolfin-core/dolfin/main] Rev 4927: Get PETSc CMake working much better.

 

On Tue, Aug 10, 2010 at 6:12 PM,  <noreply@xxxxxxxxxxxxx> wrote:
> ------------------------------------------------------------
> revno: 4927
> committer: Garth N. Wells <gnw20@xxxxxxxxx>
> branch nick: dolfin-cmake
> timestamp: Tue 2010-08-10 17:10:17 +0100
> message:
>  Get PETSc CMake working much better.
>
>  Not sure how to handle the libraries. Johannes?
> modified:
>  CMakeLists.txt
>  cmake/FindPETSc.dolfin.cmake
>  cmake/FindPackageMultipass.cmake
>
>
> --
> lp:dolfin
> https://code.launchpad.net/~dolfin-core/dolfin/main
>
> Your team DOLFIN Core Team is subscribed to branch lp:dolfin.
> To unsubscribe from this branch go to https://code.launchpad.net/~dolfin-core/dolfin/main/+edit-subscription
>
> === modified file 'CMakeLists.txt'
> --- CMakeLists.txt      2010-08-10 14:49:21 +0000
> +++ CMakeLists.txt      2010-08-10 16:10:17 +0000
> @@ -84,9 +84,16 @@
>  if(MTL4_ENABLE_MTL4)
>   find_package(MTL4.dolfin)
>  endif(MTL4_ENABLE_MTL4)
> +
>  if(ENABLE_PETSC)
> -  include(FindPETSc.dolfin)
> +  find_package(PETSc.dolfin)
> +  if (PETSC_FOUND)
> +    add_definitions(-DHAS_PETSC)
> +    include_directories(${PETSC_INCLUDES})
> +    set(DOLFIN_LIBRARIES ${PETSC_LIBRARIES} ${DOLFIN_LIBRARIES})
> +  endif (PETSC_FOUND)
>  endif(ENABLE_PETSC)
> +

I have added these things to dolfin/CMakeLists.txt. For instance in
the PETSc case I would add something like this:

if(ENABLE_PETSC AND PETSC_FOUND)
  add_definitions(-DHAS_PETSC)
  list(APPEND DOLFIN_INCLUDE_DIRECTORIES ${PETSC_INCLUDES})
  list(APPEND DOLFIN_TARGET_LINK_LIBRARIES ${PETSC_LIBRARIES})
endif(ENABLE_PETSC AND PETSC_FOUND)

Johannes

>  #if(DOLFIN_ENABLE_PARMETIS)
>  #  include(FindParMETIS.dolfin)
>  #endif(DOLFIN_ENABLE_PARMETIS)
>
> === modified file 'cmake/FindPETSc.dolfin.cmake'
> --- cmake/FindPETSc.dolfin.cmake        2010-08-10 15:11:45 +0000
> +++ cmake/FindPETSc.dolfin.cmake        2010-08-10 16:10:17 +0000
> @@ -19,56 +19,60 @@
>  # Redistribution and use is allowed according to the terms of the BSD license.
>  # For details see the accompanying COPYING-CMAKE-SCRIPTS file.
>  #
> -
> -set(PETSC_FOUND 0)
> -
> -message(STATUS "checking for package 'PETSc'")
> -
>  find_path (PETSC_DIR include/petsc.h
>   HINTS ENV PETSC_DIR
>   PATHS
> -  /usr/lib/petscdir/3.0.0 /usr/lib/petscdir/2.3.3 /usr/lib/petscdir/2.3.2 # Debian
> +  /usr/lib/petscdir/3.1 /usr/lib/petscdir/3.0.0 /usr/lib/petscdir/2.3.3 /usr/lib/petscdir/2.3.2 # Debian
>   $ENV{HOME}/petsc
>   DOC "PETSc Directory")
>
> -IF ( "${CMAKE_BUILD_TYPE}" STREQUAL "Debug" )
> -  set( DEBIAN_FLAVORS "linux-gnu-c-debug linux-gnu-c-opt")
> -ELSE()
> -  set( DEBIAN_FLAVORS "linux-gnu-c-opt linux-gnu-c-debug")
> -ENDIF()
> +
> +foreach( debian_arches linux kfreebsd )
> +  IF ( "${CMAKE_BUILD_TYPE}" STREQUAL "Debug" )
> +    set( DEBIAN_FLAVORS ${debian_arches}-gnu-c-debug ${debian_arches}-gnu-c-opt ${DEBIAN_FLAVORS})
> +  ELSE()
> +    set( DEBIAN_FLAVORS ${debian_arches}-gnu-c-opt ${debian_arches}-gnu-c-debug ${DEBIAN_FLAVORS})
> +  ENDIF()
> +endforeach()
>
>  if (PETSC_DIR AND NOT PETSC_ARCH)
>   set (_petsc_arches
>     $ENV{PETSC_ARCH}                   # If set, use environment variable first
> -    ${DEBIAN_FLAVORS}                  # Debian defaults
> +    ${DEBIAN_FLAVORS}  # Debian defaults
>     x86_64-unknown-linux-gnu i386-unknown-linux-gnu)
>   set (petscconf "NOTFOUND" CACHE FILEPATH "Cleared" FORCE)
>   foreach (arch ${_petsc_arches})
>     if (NOT PETSC_ARCH)
>       find_path (petscconf petscconf.h
> -      HINTS ${PETSC_DIR}
> -      PATH_SUFFIXES ${arch}/include bmake/${arch}
> -      NO_DEFAULT_PATH)
> +       HINTS ${PETSC_DIR}
> +       PATH_SUFFIXES ${arch}/include bmake/${arch}
> +       NO_DEFAULT_PATH)
>       if (petscconf)
> -        set (PETSC_ARCH "${arch}" CACHE STRING "PETSc build architecture")
> +       set (PETSC_ARCH "${arch}" CACHE STRING "PETSc build architecture")
>       endif (petscconf)
>     endif (NOT PETSC_ARCH)
>   endforeach (arch)
>   set (petscconf "NOTFOUND" CACHE INTERNAL "Scratch variable" FORCE)
>  endif (PETSC_DIR AND NOT PETSC_ARCH)
>
> -#set (petsc_slaves LIBRARIES_SYS LIBRARIES_VEC LIBRARIES_MAT LIBRARIES_DM LIBRARIES_KSP LIBRARIES_SNES LIBRARIES_TS
> -#  INCLUDE_DIR INCLUDE_CONF)
> -#include (FindPackageMultipass)
> -#find_package_multipass (PETSc petsc_config_current
> -#  STATES DIR ARCH
> -#  DEPENDENTS INCLUDES LIBRARIES COMPILER MPIEXEC ${petsc_slaves})
> + message (STATUS "PETSc ARCH=${PETSC_ARCH}")
> +
> +set (petsc_slaves LIBRARIES_SYS LIBRARIES_VEC LIBRARIES_MAT LIBRARIES_DM LIBRARIES_KSP LIBRARIES_SNES LIBRARIES_TS
> +  INCLUDE_DIR INCLUDE_CONF)
> +include (FindPackageMultipass)
> +find_package_multipass (PETSc petsc_config_current
> +  STATES DIR ARCH
> +  DEPENDENTS INCLUDES LIBRARIES COMPILER MPIEXEC ${petsc_slaves})
>
>  # Determine whether the PETSc layout is old-style (through 2.3.3) or
>  # new-style (3.0.0)
>  if (EXISTS ${PETSC_DIR}/${PETSC_ARCH}/include/petscconf.h)   # > 2.3.3
> -  #set (petsc_conf_base ${PETSC_DIR}/conf/base)
> -  set (petsc_conf_base ${PETSC_DIR}/conf)
> +  if ( EXISTS( ${PETSC_DIR}/conf/base) )
> +    set (petsc_conf_base ${PETSC_DIR}/conf/base)
> +  else()
> +    set (petsc_conf_base ${PETSC_DIR}/conf/rules)
> +    set (petsc_conf_vars ${PETSC_DIR}/conf/variables)
> +  endif()
>   set (PETSC_VERSION "3.0.0")
>  elseif (EXISTS ${PETSC_DIR}/bmake/${PETSC_ARCH}/petscconf.h) # <= 2.3.3
>   set (petsc_conf_base ${PETSC_DIR}/bmake/common/base)
> @@ -77,6 +81,8 @@
>   set (petsc_conf_base "NOTFOUND")
>  endif (EXISTS ${PETSC_DIR}/${PETSC_ARCH}/include/petscconf.h)
>
> +OPTION(PETSC_LIB_BASIC "enable PETSc single library" ON)
> +
>  if (petsc_conf_base AND NOT petsc_config_current)
>   # Put variables into environment since they are needed to get
>   # configuration (petscvariables) in the PETSc makefile
> @@ -85,45 +91,47 @@
>
>   # A temporary makefile to probe the PETSc configuration
>   set (petsc_config_makefile ${PROJECT_BINARY_DIR}/Makefile.petsc)
> -  file (WRITE ${petsc_config_makefile}
> -"# Retrive various flags from PETSc settings.
> -
> -PETSC_DIR=${PETSC_DIR}
> -PETSC_ARCH=${PETSC_ARCH}
> -
> -include ${petsc_conf_base}/variables
> -
> -get_petsc_include:
> -       -@echo -I\${PETSC_DIR}/\${PETSC_ARCH}/include -I\${PETSC_DIR}/include \${MPI_INCLUDE} \${X11_INCLUDE}
> -get_petsc_libs:
> -       -@echo   \${C_SH_LIB_PATH} -L\${PETSC_LIB_DIR} \${PETSC_LIB_BASIC} \${X11_LIB}
> -get_petsc_cc:
> -       -@echo \${PCC}
> -get_petsc_ld:
> -       -@echo \${PCC_LINKER}
> -get_petsc_lib_dir:
> -       -@echo \${PETSC_LIB_DIR}
> -get_petsc_cpp_flags:
> -       -@echo \${PETSC_CCPPFLAGS}
> -")
> -
> +  if ( DEFINED petsc_conf_vars )
> +    file (WRITE ${petsc_config_makefile}
> +"## This file was autogenerated by FindPETSc.cmake
> +# PETSC_DIR  = ${PETSC_DIR}
> +# PETSC_ARCH = ${PETSC_ARCH}
> +include ${petsc_conf_vars}
> +include ${petsc_conf_base}
> +show :
> +       -@echo -n \${\${VARIABLE}}
> +")
> +
> +  else()
> +    file (WRITE ${petsc_config_makefile}
> +"## This file was autogenerated by FindPETSc.cmake
> +# PETSC_DIR  = ${PETSC_DIR}
> +# PETSC_ARCH = ${PETSC_ARCH}
> +include ${petsc_conf_base}
> +show :
> +       -@echo -n \${\${VARIABLE}}
> +")
> +endif()
>   macro (PETSC_GET_VARIABLE name var)
>     set (${var} "NOTFOUND" CACHE INTERNAL "Cleared" FORCE)
> -    execute_process (COMMAND ${CMAKE_BUILD_TOOL} -f ${petsc_config_makefile} ${name}
> -      OUTPUT_VARIABLE ${var} RESULT_VARIABLE petsc_return OUTPUT_STRIP_TRAILING_WHITESPACE)
> +    execute_process (COMMAND ${CMAKE_MAKE_PROGRAM} -f ${petsc_config_makefile} show VARIABLE=${name}
> +      OUTPUT_VARIABLE ${var}
> +      RESULT_VARIABLE petsc_return)
>   endmacro (PETSC_GET_VARIABLE)
> -  petsc_get_variable (get_petsc_lib_dir         petsc_lib_dir)
> -  petsc_get_variable (get_petsc_libs            petsc_libs)
> -  petsc_get_variable (get_petsc_include         petsc_include_dir)
> -  petsc_get_variable (get_petsc_cc              petsc_cc)
> -  petsc_get_variable (get_petsc_cpp_flags       petsc_cpp_flags)
> -
> -  # We are done with the temporary Makefile, calling PETSC_GET_VARIABLE after this point is invalid
> +  petsc_get_variable (PETSC_LIB_DIR            petsc_lib_dir)
> +  petsc_get_variable (PETSC_EXTERNAL_LIB_BASIC petsc_libs_external)
> +  petsc_get_variable (PETSC_CCPPFLAGS          petsc_cpp_line)
> +  petsc_get_variable (PETSC_INCLUDE            petsc_include)
> +  #petsc_get_variable (PCC                      petsc_cc)
> +  #petsc_get_variable (MPIEXEC                  petsc_mpiexec)
> +  # We are done with the temporary Makefile, calling PETSC_GET_VARIABLE after this point is invalid!
>   file (REMOVE ${petsc_config_makefile})
>
> +  include (ResolveCompilerPaths)
>   # Extract include paths and libraries from compile command line
> -  include (ResolveCompilerPaths)
> -  resolve_includes (petsc_includes_all "${petsc_cpp_flags}")
> +  resolve_includes (petsc_includes_all "${petsc_cpp_line}")
> +
> +  message (STATUS "petsc_lib_dir ${petsc_lib_dir}")
>
>   macro (PETSC_FIND_LIBRARY suffix name)
>     set (PETSC_LIBRARY_${suffix} "NOTFOUND" CACHE INTERNAL "Cleared" FORCE)
> @@ -131,44 +139,47 @@
>     set (PETSC_LIBRARIES_${suffix} "${PETSC_LIBRARY_${suffix}}")
>     mark_as_advanced (PETSC_LIBRARY_${suffix})
>   endmacro (PETSC_FIND_LIBRARY suffix name)
> -  petsc_find_library (SYS  petsc)
> -  #petsc_find_library (VEC  petscvec)
> -  #petsc_find_library (MAT  petscmat)
> -  #petsc_find_library (DM   petscdm)
> -  #petsc_find_library (KSP  petscksp)
> -  #petsc_find_library (SNES petscsnes)
> -  #petsc_find_library (TS   petscts)
> +  if ( PETSC_LIB_BASIC )
> +    petsc_find_library (SYS  petsc)
> +    petsc_find_library (VEC  petsc)
> +    petsc_find_library (MAT  petsc)
> +    petsc_find_library (DM   petsc)
> +    petsc_find_library (KSP  petsc)
> +    petsc_find_library (SNES petsc)
> +    petsc_find_library (TS   petsc)
> +  else ()
> +    petsc_find_library (SYS  petsc)
> +    petsc_find_library (VEC  petscvec)
> +    petsc_find_library (MAT  petscmat)
> +    petsc_find_library (DM   petscdm)
> +    petsc_find_library (KSP  petscksp)
> +    petsc_find_library (SNES petscsnes)
> +    petsc_find_library (TS   petscts)
> +  endif()
>
> -  # Try compiling and running test program
> -  include(CheckCXXSourceRuns)
> -  set(CMAKE_REQUIRED_INCLUDES  ${petsc_includes_all})
> -  set(CMAKE_REQUIRED_LIBRARIES ${petsc_libs})
> -  check_cxx_source_runs("
> +  include (CheckCXXSourceRuns)
> +  macro (PETSC_TEST_RUNS includes libraries runs)
> +    multipass_c_source_runs ("${includes}" "${libraries}" "
> +static const char help[] = \"PETSc test program.\";
> +#include \"petsc.h\"
>  #include \"petscts.h\"
> -int main(int argc,char *argv[])
> -{
> +int main(int argc,char *argv[]) {
>   PetscErrorCode ierr;
>   TS ts;
>
> -  ierr = PetscInitializeNoArguments();CHKERRQ(ierr);
> -  //ierr = PetscInitialize(&argc,&argv,0,help);CHKERRQ(ierr);
> +  ierr = PetscInitialize(&argc,&argv,0,help);CHKERRQ(ierr);
>   ierr = TSCreate(PETSC_COMM_WORLD,&ts);CHKERRQ(ierr);
>   ierr = TSSetFromOptions(ts);CHKERRQ(ierr);
>   ierr = TSDestroy(ts);CHKERRQ(ierr);
>   ierr = PetscFinalize();CHKERRQ(ierr);
> -
>   return 0;
>  }
> -" PETSC_TEST_RUNS)
> -
> -if(NOT PETSC_TEST_RUNS)
> -  message("PETSc was found but a test program could not be run.")
> -else(NOT PETSC_TEST_RUNS)
> -  message("PETSc was found and test program succeeded.")
> -  set(PETSC_FOUND 1)
> -  include_directories(${petsc_includes_all})
> -  add_definitions(-DHAS_PETSC)
> -endif(NOT PETSC_TEST_RUNS)
> +" ${runs})
> +    if (${${runs}})
> +      set (PETSC_EXECUTABLE_RUNS "YES" CACHE BOOL
> +       "Can the system successfully run a PETSc executable?  This variable can be manually set to \"YES\" to force CMake to accept a given PETSc configuration, but this will almost always result in a broken build.  If you change PETSC_DIR, PETSC_ARCH, or PETSC_CURRENT you would have to reset this variable." FORCE)
> +    endif (${${runs}})
> +  endmacro (PETSC_TEST_RUNS)
>
>   macro (PETSC_JOIN libs deps)
>     list (APPEND PETSC_LIBRARIES_${libs} ${PETSC_LIBRARIES_${deps}})
> @@ -181,10 +192,49 @@
>   petsc_join (TS   SNES)
>   petsc_join (ALL  TS)
>
> +
> +  find_path (PETSC_INCLUDE_DIR petscts.h HINTS "${PETSC_DIR}" PATH_SUFFIXES include NO_DEFAULT_PATH)
> +  find_path (PETSC_INCLUDE_CONF petscconf.h HINTS "${PETSC_DIR}" PATH_SUFFIXES "${PETSC_ARCH}/include" "bmake/${PETSC_ARCH}" NO_DEFAULT_PATH)
> +  mark_as_advanced (PETSC_INCLUDE_DIR PETSC_INCLUDE_CONF)
> +  set (petsc_includes_minimal ${PETSC_INCLUDE_CONF} ${PETSC_INCLUDE_DIR})
> +
> +  petsc_test_runs ("${petsc_includes_minimal}" "${PETSC_LIBRARIES_TS}" petsc_works_minimal)
> +  if (petsc_works_minimal)
> +    message (STATUS "Minimal PETSc includes and libraries work.  This probably means we are building with shared libs.")
> +    set (petsc_includes_needed "${petsc_includes_minimal}")
> +  else (petsc_works_minimal)   # Minimal includes fail, see if just adding full includes fixes it
> +    petsc_test_runs ("${petsc_includes_all}" "${PETSC_LIBRARIES_TS}" petsc_works_allincludes)
> +    if (petsc_works_allincludes) # It does, we just need all the includes (
> +      message (STATUS "PETSc requires extra include paths, but links correctly with only interface libraries.  This is an unexpected configuration (but it seems to work fine).")
> +      set (petsc_includes_needed ${petsc_includes_all})
> +    else (petsc_works_allincludes) # We are going to need to link the external libs explicitly
> +      resolve_libraries (petsc_libraries_external "${petsc_libs_external}")
> +      foreach (pkg SYS VEC MAT DM KSP SNES TS ALL)
> +           list (APPEND PETSC_LIBRARIES_${pkg}  ${petsc_libraries_external})
> +      endforeach (pkg)
> +      petsc_test_runs ("${petsc_includes_minimal}" "${PETSC_LIBRARIES_TS}" petsc_works_alllibraries)
> +      if (petsc_works_alllibraries)
> +        message (STATUS "PETSc only need minimal includes, but requires explicit linking to all dependencies.  This is expected when PETSc is built with static libraries.")
> +       set (petsc_includes_needed ${petsc_includes_minimal})
> +      else (petsc_works_alllibraries)
> +       # It looks like we really need everything, should have listened to Matt
> +       set (petsc_includes_needed ${petsc_includes_all})
> +       petsc_test_runs ("${petsc_includes_all}" "${PETSC_LIBRARIES_TS}" petsc_works_all)
> +       if (petsc_works_all) # We fail anyways
> +         message (STATUS "PETSc requires extra include paths and explicit linking to all dependencies.  This probably means you have shared libraries and something unexpected in PETSc headers.")
> +       else (petsc_works_all) # We fail anyways
> +         message (STATUS "PETSc could not be used, maybe the install is broken.")
> +       endif (petsc_works_all)
> +      endif (petsc_works_alllibraries)
> +    endif (petsc_works_allincludes)
> +  endif (petsc_works_minimal)
> +
> +  # We do an out-of-source build so __FILE__ will be an absolute path, hence __SDIR__ is superfluous
>   set (PETSC_DEFINITIONS "-D__SDIR__=\"\"" CACHE STRING "PETSc definitions" FORCE)
> +  # Sometimes this can be used to assist FindMPI.cmake
>   set (PETSC_MPIEXEC ${petsc_mpiexec} CACHE FILEPATH "Executable for running PETSc MPI programs" FORCE)
> -  set (PETSC_INCLUDES ${petsc_includes_all} CACHE STRING "PETSc include path" FORCE)
> -  set (PETSC_LIBRARIES ${petsc_libs} CACHE STRING "PETSc libraries" FORCE)
> +  set (PETSC_INCLUDES ${petsc_includes_needed} CACHE STRING "PETSc include path" FORCE)
> +  set (PETSC_LIBRARIES ${PETSC_LIBRARIES_ALL} CACHE STRING "PETSc libraries" FORCE)
>   set (PETSC_COMPILER ${petsc_cc} CACHE FILEPATH "PETSc compiler" FORCE)
>   # Note that we have forced values for all these choices.  If you
>   # change these, you are telling the system to trust you that they
> @@ -192,3 +242,16 @@
>   mark_as_advanced (PETSC_INCLUDES PETSC_LIBRARIES PETSC_COMPILER PETSC_DEFINITIONS PETSC_MPIEXEC PETSC_EXECUTABLE_RUNS)
>  endif (petsc_conf_base AND NOT petsc_config_current)
>
> +
> +include (FindPackageHandleStandardArgs)
> +find_package_handle_standard_args (PETSc
> +  "PETSc could not be found.  Be sure to set PETSC_DIR and PETSC_ARCH."
> +#  PETSC_INCLUDES PETSC_LIBRARIES PETSC_EXECUTABLE_RUNS)
> +  PETSC_INCLUDES PETSC_LIBRARIES )
> +
> +
> +if ( PETSC_FOUND )
> +  #add_definitions( -DHAVE_PETSC -DHAVE_PETSC_H )
> +  #SET( HAVE_PETSC 1 )
> +  MARK_AS_ADVANCED( PETSC_CURRENT PETSC_DIR PETSC_ARCH )
> +endif( PETSC_FOUND )
>
> === modified file 'cmake/FindPackageMultipass.cmake'
> --- cmake/FindPackageMultipass.cmake    2010-08-10 14:49:21 +0000
> +++ cmake/FindPackageMultipass.cmake    2010-08-10 16:10:17 +0000
> @@ -39,7 +39,7 @@
>       # The name of the stored value for the given state
>       set (_stored_var PACKAGE_MULTIPASS_${_NAME}_${_state})
>       if (NOT "${${_stored_var}}" STREQUAL "${${_NAME}_${_state}}")
> -        set (_states_current "NO")
> +       set (_states_current "NO")
>       endif (NOT "${${_stored_var}}" STREQUAL "${${_NAME}_${_state}}")
>       set (${_stored_var} "${${_NAME}_${_state}}" CACHE INTERNAL "Stored state for ${_name}." FORCE)
>       list (REMOVE_AT _args 0)
> @@ -71,7 +71,7 @@
>
>
>  macro (MULTIPASS_C_SOURCE_RUNS includes libraries source runs)
> -  include (CheckCSourceRuns)
> +  include (CheckCXXSourceRuns)
>   # This is a ridiculous hack.  CHECK_C_SOURCE_* thinks that if the
>   # *name* of the return variable doesn't change, then the test does
>   # not need to be re-run.  We keep an internal count which we
> @@ -86,6 +86,6 @@
>   set (testname MULTIPASS_TEST_${MULTIPASS_TEST_COUNT}_${runs})
>   set (CMAKE_REQUIRED_INCLUDES ${includes})
>   set (CMAKE_REQUIRED_LIBRARIES ${libraries})
> -  check_c_source_runs ("${source}" ${testname})
> +  check_cxx_source_runs ("${source}" ${testname})
>   set (${runs} "${${testname}}")
>  endmacro (MULTIPASS_C_SOURCE_RUNS)
>
>
>



Follow ups