xref: /petsc/share/petsc/CMakeLists.txt (revision 6fc583b078fa519087a7295644beaf300e4e7847)
1#
2#  This is a SAMPLE CMakeLists.txt suitable for direct use with a new PETSc application or
3#  you can add fragments of the material below to an existing application's CMakeLists.txt
4#
5#  Usage:  To build ex1 from ex1.c
6#    rm -fr build
7#    cmake -B build
8#    cmake --build build
9#    ./build/ex1
10#
11#  By default it gets both the compiler information and the library information from PETSc
12#
13cmake_minimum_required(VERSION 3.1.0)
14include(FindPkgConfig REQUIRED)
15
16set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
17
18# Dummy project to determine compiler names and version
19project(detect_compilers LANGUAGES)
20
21# set root of location to find PETSc's pkg-config
22set(PETSC $ENV{PETSC_DIR}/$ENV{PETSC_ARCH})
23set(ENV{PKG_CONFIG_PATH} ${PETSC}/lib/pkgconfig)
24
25# Remove the lines below if you do not wish to have PETSc determine the compilers
26pkg_get_variable(CMAKE_C_COMPILER PETSc ccompiler)
27pkg_get_variable(CXX_COMPILER PETSc cxxcompiler)
28if (CXX_COMPILER)
29  set(CMAKE_CXX_COMPILER ${CXX_COMPILER})
30  enable_language(CXX)
31endif (CXX_COMPILER)
32pkg_get_variable(FORTRAN_COMPILER PETSc fcompiler)
33if (FORTRAN_COMPILER)
34  set(CMAKE_Fortran_COMPILER ${FORTRAN_COMPILER})
35  enable_language(Fortran)
36endif (FORTRAN_COMPILER)
37
38#  tells CMake to build the application ex1 from the source file ex1.c
39#  this must appear AFTER the compilers are set
40project(ex1)
41add_executable(ex1 ex1.c)
42
43find_package(PkgConfig REQUIRED)
44pkg_search_module(PETSC REQUIRED IMPORTED_TARGET PETSc)
45target_link_libraries(ex1 PkgConfig::PETSC)
46