jedbrown / cmake-modules

CMake modules for some scientific libraries
BSD 2-Clause "Simplified" License
87 stars 54 forks source link

PETSc not discovered with empty PETSC_ARCH #31

Closed Laar closed 6 years ago

Laar commented 6 years ago

The fix of 2e65c1c788c to allow an absent PETSC_ARCH does not seem to work. From a bit of debugging it looks like that foreach (arch ${_petsc_arches}) skips over empty strings. Thus it does not try to the empty version of PETSC_ARCH.

As fix I have tried adding

  if (NOT PETSC_ARCH)
    message (STATUS "Trying without PETSC_ARCH")
    find_path (petscconf petscconf.h
      HINTS ${PETSC_DIR}
      PATH_SUFFIXES include bmake
      NO_DEFAULT_PATH)
    if (petscconf)
      message (STATUS "Install without PETSC_ARCH found")
      set (PETSC_ARCH "" CACHE STRING "PETSc build architecture")
      set (PETSC_ARCH "")
    endif (petscconf)
  endif (NOT PETSC_ARCH)
  set (petscconf "NOTFOUND" CACHE INTERNAL "Scratch variable" FORCE)

after the foreach loop that tries to detect the PETSC_ARCH.

This fortunately fixes the problems on my system, though with my minimal knowledge of CMake I am far from sure if this is a/the correct solution to the problem. I have already noticed two things:

P.S. The system is a standard Arch Linux, where PETSc is installed using the AUR. Thus it is located in /opt/petsc/linux-c-opt/, with PETSC_DIR=/opt/petsc/linux-c-opt/ and no PETSC_ARCH set.

jedbrown commented 6 years ago

What version of CMake are you using? I run the following with no environment variables set.

$ CC=mpicc cmake ..
-- The C compiler identification is GNU 8.1.1
-- The CXX compiler identification is GNU 8.1.1
-- Check for working C compiler: /bin/mpicc
-- Check for working C compiler: /bin/mpicc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /bin/c++
-- Check for working CXX compiler: /bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- petsc_lib_dir /opt/petsc/linux-c-opt/lib
-- Recognized PETSc install with single library for all packages                                                                                                                              
-- Performing Test MULTIPASS_TEST_1_petsc_works_minimal                                                                                                                                       
-- Performing Test MULTIPASS_TEST_1_petsc_works_minimal - Success                                                                                                                             
-- Minimal PETSc includes and libraries work.  This probably means we are building with shared libs.                                                                                          
-- Found PETSc: /opt/petsc/linux-c-opt/include;/opt/petsc/linux-c-opt/include (found version "3.9.2")                                                                                         
-- Configuring done                                                                                                                                                                           
-- Generating done                                                                                                                                                                            
-- Build files have been written to: /tmp/jed/build            

with CMakeLists.txt

cmake_minimum_required (VERSION 3.3)
project(Foo)
list (APPEND CMAKE_MODULE_PATH "/home/jed/dohp/cmake-modules")
find_package(PETSc)

Note that if you are forced to use CMake, you might find it easier to use the built-in pkgconfig support with modern versions of PETSc instead of installing this set of CMake modules that I have blissfully not needed in my own work for several years.

Laar commented 6 years ago

That also works for me (CMake 3.11.4). It seems that I overlooked that another makefile set the PETSC_ARCH to NOTFOUND.

Removing that from the project that I am working on seems to resolve the problem. While adding it to your test CMakeList.txt

cmake_minimum_required (VERSION 3.3)
project(Foo)
list (APPEND CMAKE_MODULE_PATH "/home/lars/Documents/Werk/UTwente/cmake-test/cmake-modules")
set(PETSC_ARCH "NOTFOUND" CACHE STRING "PETSc Arch")
find_package(PETSc)

breaks FindPETSC (tested without PETSC_ARCH, and both with and without PETSC_DIR as environment variables)

$ rm -rf ./*; CC=mpicc cmake ../
-- The C compiler identification is GNU 8.1.1
-- The CXX compiler identification is GNU 8.1.1
-- Check for working C compiler: /usr/bin/mpicc
-- Check for working C compiler: /usr/bin/mpicc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
Trying linux-gnu-c-debug as PETSC_ARCH
Trying linux-gnu-c-opt as PETSC_ARCH
Trying x86_64-unknown-linux-gnu as PETSC_ARCH
Trying i386-unknown-linux-gnu as PETSC_ARCH
CMake Error at cmake-modules/FindPETSc.cmake:141 (message):
  The pair PETSC_DIR=/opt/petsc/linux-c-opt PETSC_ARCH=NOTFOUND do not
  specify a valid PETSc installation
Call Stack (most recent call first):
  CMakeLists.txt:6 (find_package)

-- PETSc could not be found.  Be sure to set PETSC_DIR and PETSC_ARCH. (missing: PETSC_INCLUDES PETSC_LIBRARIES PETSC_EXECUTABLE_RUNS) 
-- Configuring incomplete, errors occurred!
See also "[path to CMakeList.txt]/build/CMakeFiles/CMakeOutput.log".

As you can see in the output I have added message ("Trying ${arch} as PETSC_ARCH") after the foreach in line 107, which causes the output

Trying linux-gnu-c-debug as PETSC_ARCH
Trying linux-gnu-c-opt as PETSC_ARCH
Trying x86_64-unknown-linux-gnu as PETSC_ARCH
Trying i386-unknown-linux-gnu as PETSC_ARCH

You can see that it does not try the empty PETSC_ARCH. The empty string that is added to _petsc_arches (line 103), does not have an effect. Therefore, I think that it is useful to remove that empty string, as it only confuses a reader that is less familiar with CMake. Whether support for a situation where PETSC_ARCH is set (or cached) is useful, is something that exceeds my knowledge of CMake.

Whether I can use the build-in pkgconfig support I do not know (I'm quite new to the project), but that might be something to look into.

jedbrown commented 6 years ago

Is it necessary for this module to work if another sets PETSC_ARCH=NOTFOUND? It should be unset or empty.

I'm inclined to just remove that empty string, but if we need a more elaborate solution, I guess it would need to be a special value that we match on.

Laar commented 6 years ago

After removing it (the setting of PETSC_ARCH), it seems to work perfectly. Thus I do not think that we need any special solution.

Why it was added in the first place is a half mystery to me, but unfortunately one that has to be left unsolved due to lack of information.

Laar commented 6 years ago

Thank you