From 0ed940a70457c02520ecbe118955fb372025a0e9 Mon Sep 17 00:00:00 2001 From: Peter Heywood Date: Mon, 4 Apr 2022 17:00:11 +0100 Subject: [PATCH] Only build Swig from source on linux if GNU compiler Swig 4.0.2 does not appear to build from source with NVHPC/Clang by default --- swig/CMakeLists.txt | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/swig/CMakeLists.txt b/swig/CMakeLists.txt index e58297e85..1f5f9cca7 100644 --- a/swig/CMakeLists.txt +++ b/swig/CMakeLists.txt @@ -69,8 +69,8 @@ if(NOT SWIG_FOUND) set(SWIG_EXECUTABLE "${swig_SOURCE_DIR}/swig.exe") set(SWIG_EXECUTABLE "${swig_SOURCE_DIR}/swig.exe" CACHE FILEPATH "Path to SWIG executable") endif() - else() - # Under linux, download the .tar.gz, extract, build and install. + elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") + # Under linux, with gcc as the host compiler download the .tar.gz, extract, build and install. # This must be done at configure time, as FindSwig requires the swig executable. # FetchContent allows download at configure time, but must use execute_process to run commands at configure time. @@ -91,6 +91,21 @@ if(NOT SWIG_FOUND) set(swig_make_ERROR_FILE "${swig_BINARY_DIR}/swig-error-make.log") set(swig_makeinstall_ERROR_FILE "${swig_BINARY_DIR}/swig-error-make-install.log") + message(STATUS "[swig] ./autogen.sh") + execute_process( + COMMAND "./autogen.sh" + WORKING_DIRECTORY ${swig_SOURCE_DIR} + RESULT_VARIABLE swig_configure_RESULT + OUTPUT_VARIABLE swig_configure_OUTPUT + ERROR_FILE ${swig_configure_ERROR_FILE} + ) + if(NOT swig_configure_RESULT EQUAL "0") + message(FATAL_ERROR + " [swig] Error during SWIG ${SWIG_DOWNLOAD_VERSION} ./autogen.sh\n" + " Error log: ${swig_configure_ERROR_FILE}\n" + " Consider installing SWIG ${SWIG_MINIMUM_SUPPORTED_VERSION} yourself and passing -DSWIG_EXECUTABLE=/path/to/swig.") + endif() + # run ./configure with an appropraite prefix to install into the _deps/swig-bin directory message(STATUS "[swig] ./configure --prefix ${swig_BINARY_DIR}") execute_process( @@ -142,6 +157,14 @@ if(NOT SWIG_FOUND) set(SWIG_EXECUTABLE "${swig_BINARY_DIR}/bin/swig") set(SWIG_EXECUTABLE "${swig_BINARY_DIR}/bin/swig" CACHE FILEPATH "Path to SWIG executable") endif() + else() + # Swig's autogen.sh / ./configure do not result in a successful makefile when using nvhpc/clang as the c/c++ compiler + # E.g. -ansi is not a valid flag for nvhpc. + # So suggest the user builds installs it manually instead. + message(FATAL_ERROR + " Unsupported C/C++ compiler ${CMAKE_CXX_COMPILER_ID} for swig compilation.\n" + " Please use gcc/g++ as your host compiler, or manually install Swig >= ${SWIG_MINIMUM_SUPPORTED_VERSION} using gcc/g++ or install swig via your package manager\n" + ) endif() # Attempt to find swig again, but as REQUIRED. find_package(SWIG ${SWIG_MINIMUM_SUPPORTED_VERSION} REQUIRED)