read directly from file

detour via dummy makefile not needed
This commit is contained in:
Martin Diehl 2021-05-19 17:19:11 +02:00
parent 15c498f1ed
commit d4f439ae8b
1 changed files with 12 additions and 42 deletions

View File

@ -16,54 +16,24 @@ if (PETSC_DIR STREQUAL "")
message (FATAL_ERROR "PETSc location (PETSC_DIR) is not defined")
endif ()
set (petsc_conf_variables "${PETSC_DIR}/lib/petsc/conf/variables")
# Use existing variables from PETSc
# https://github.com/jedbrown/cmake-modules/blob/master/FindPETSc.cmake
# Generate a temporary makefile to probe the PETSc configuration
# This file will be deleted once the settings from PETSc are parsed into CMake
exec_program (mktemp ARGS -d OUTPUT_VARIABLE TEMPDIR)
set (petsc_config_makefile "${TEMPDIR}/Makefile.petsc")
file (WRITE
"${petsc_config_makefile}"
"## This file was auto generated by CMake
# PETSC_DIR = ${PETSC_DIR}
SHELL = /bin/sh
include ${petsc_conf_variables}
INCLUDE_DIRS := \${PETSC_FC_INCLUDES}
LIBRARIES := \${PETSC_WITH_EXTERNAL_LIB}
includes:
\t@echo \${INCLUDE_DIRS}
extlibs:
\t@echo \${LIBRARIES}
")
# CMake will execute each target in petsc_config_makefile to acquire corresponding PETSc Variables.
find_program (MAKE_EXECUTABLE NAMES gmake make)
execute_process (COMMAND ${MAKE_EXECUTABLE} --no-print-directory -f ${petsc_config_makefile} "includes"
RESULT_VARIABLE PETSC_INCLUDES_RETURN
OUTPUT_VARIABLE petsc_includes
OUTPUT_STRIP_TRAILING_WHITESPACE)
execute_process (COMMAND ${MAKE_EXECUTABLE} --no-print-directory -f ${petsc_config_makefile} "extlibs"
RESULT_VARIABLE PETSC_EXTERNAL_LIB_RETURN
OUTPUT_VARIABLE petsc_external_lib
OUTPUT_STRIP_TRAILING_WHITESPACE)
file (REMOVE_RECURSE ${TEMPDIR})
# Remove duplicate compiler and linker flags
string (REGEX MATCHALL "-I([^\" ]+)" TMP_LIST "${petsc_includes}")
list (REMOVE_DUPLICATES TMP_LIST)
foreach (dir ${TMP_LIST})
set (PETSC_INCLUDES "${PETSC_INCLUDES} ${dir}")
endforeach (dir)
file (STRINGS "${PETSC_DIR}/lib/petsc/conf/petscvariables" petsc_external_lib REGEX "PETSC_WITH_EXTERNAL_LIB = .*$?")
string (REPLACE "=" ";" petsc_external_lib "${petsc_external_lib}")
list (GET petsc_external_lib 1 petsc_external_lib)
string (REGEX MATCHALL "-[lLW]([^\" ]+)" TMP_LIST "${petsc_external_lib}")
list (REMOVE_DUPLICATES TMP_LIST)
foreach (exlib ${TMP_LIST})
set (PETSC_EXTERNAL_LIB "${PETSC_EXTERNAL_LIB} ${exlib}")
endforeach (exlib)
file (STRINGS "${PETSC_DIR}/lib/petsc/conf/petscvariables" petsc_includes REGEX "PETSC_FC_INCLUDES = .*$?")
string (REPLACE "=" ";" petsc_includes "${petsc_includes}")
list (GET petsc_includes 1 petsc_includes)
string (REGEX MATCHALL "-I([^\" ]+)" TMP_LIST "${petsc_includes}")
list (REMOVE_DUPLICATES TMP_LIST)
foreach (dir ${TMP_LIST})
set (PETSC_INCLUDES "${PETSC_INCLUDES} ${dir}")
endforeach (dir)
message ("PETSC_DIR:\n${PETSC_DIR}\n" )
message ("PETSC_INCLUDES:\n${PETSC_INCLUDES}\n" )
message ("PETSC_EXTERNAL_LIB:\n${PETSC_EXTERNAL_LIB}\n")