dhermes / bezier

Helper for Bézier Curves, Triangles, and Higher Order Objects
Apache License 2.0
266 stars 36 forks source link

Add support in `CMakeLists.txt` for `lfortran` #260

Open dhermes opened 3 years ago

dhermes commented 3 years ago
dhermes commented 3 years ago

To build:

HERE="$(git rev-parse --show-toplevel)"
SRC_DIR="${HERE}/src/fortran"
BUILD_DIR="${HERE}/build-lfortran/build"
INSTALL_PREFIX="${HERE}/build-lfortran/usr"
LFORTRAN_BIN="$(pyenv which lfortran)"
echo "LFORTRAN_BIN=${LFORTRAN_BIN}"
rm -fr "${HERE}/build-lfortran"
mkdir -p "${BUILD_DIR}"
cmake \
    -DCMAKE_Fortran_COMPILER="${LFORTRAN_BIN}" \
    -DCMAKE_Fortran_COMPILER_ID=LFort \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_INSTALL_PREFIX:PATH="${INSTALL_PREFIX}" \
    -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON \
    -S "${SRC_DIR}" \
    -B "${BUILD_DIR}"

cmake \
    --build "${BUILD_DIR}" \
    --config Debug \
    --target install

cmake -L "${BUILD_DIR}"

Note that -DCMAKE_Fortran_COMPILER_ID=LFort is necessary because of https://gitlab.kitware.com/cmake/cmake/-/issues/17440 / https://gitlab.com/lfortran/lfortran/-/issues/261

dhermes commented 3 years ago

Using this as a starting point:

diff --git a/src/fortran/CMakeLists.txt b/src/fortran/CMakeLists.txt
index c2d5088..fab229b 100644
--- a/src/fortran/CMakeLists.txt
+++ b/src/fortran/CMakeLists.txt
@@ -11,8 +11,8 @@ set(DESCRIPTION "Library for Bezier curves and triangles.")
 option(BUILD_SHARED_LIBS "Build shared libraries" ON)
 option(TARGET_NATIVE_ARCH "Optimize build for host (native) architecture" ON)

-if(NOT CMAKE_Fortran_COMPILER_ID MATCHES "^(GNU|Intel)$")
-  message(FATAL_ERROR "gfortran and ifort are the only supported compilers (current compiler ID is ${CMAKE_Fortran_COMPILER_ID})")
+if(NOT CMAKE_Fortran_COMPILER_ID MATCHES "^(GNU|Intel|LFort)$")
+  message(FATAL_ERROR "gfortran, ifort and lfortran are the only supported compilers (current compiler ID is ${CMAKE_Fortran_COMPILER_ID})")
 endif()

 if(NOT CMAKE_BUILD_TYPE)
@@ -77,7 +77,7 @@ endif()

 target_include_directories(bezier PUBLIC $<INSTALL_INTERFACE:include>)

-if(CMAKE_Fortran_COMPILER_ID STREQUAL "GNU")
+if(CMAKE_Fortran_COMPILER_ID MATCHES "^(GNU|LFort)$")
   # ``-Wextra`` includes ``no-compare-reals``, but we have comparisons like
   # ``value == 0.0_dp``.
   target_compile_options(
@@ -109,7 +109,7 @@ elseif(CMAKE_Fortran_COMPILER_ID STREQUAL "Intel")
     target_compile_options(quadpack PUBLIC -fast)
   endif()
 else()
-  message(FATAL_ERROR "gfortran and ifort are the only supported compilers (current compiler ID is ${CMAKE_Fortran_COMPILER_ID})")
+  message(FATAL_ERROR "gfortran, ifort and lfortran are the only supported compilers (current compiler ID is ${CMAKE_Fortran_COMPILER_ID})")
 endif()

 include(GNUInstallDirs)
diff --git a/src/fortran/quadpack/CMakeLists.txt b/src/fortran/quadpack/CMakeLists.txt
index 02e242e..23e5dec 100644
--- a/src/fortran/quadpack/CMakeLists.txt
+++ b/src/fortran/quadpack/CMakeLists.txt
@@ -5,8 +5,8 @@ project(
   DESCRIPTION "Library for numerical integration of one-dimensional functions."
   LANGUAGES Fortran)

-if(NOT CMAKE_Fortran_COMPILER_ID MATCHES "^(GNU|Intel)$")
-  message(FATAL_ERROR "gfortran and lfortran are the only supported compilers (current compiler ID is ${CMAKE_Fortran_COMPILER_ID})")
+if(NOT CMAKE_Fortran_COMPILER_ID MATCHES "^(GNU|Intel|LFort)$")
+  message(FATAL_ERROR "gfortran, ifort and lfortran are the only supported compilers (current compiler ID is ${CMAKE_Fortran_COMPILER_ID})")
 endif()

 if(NOT CMAKE_BUILD_TYPE)
@@ -34,7 +34,7 @@ if(${BUILD_SHARED_LIBS})
   set_target_properties(quadpack PROPERTIES POSITION_INDEPENDENT_CODE ON)
 endif()

-if(CMAKE_Fortran_COMPILER_ID STREQUAL "GNU")
+if(CMAKE_Fortran_COMPILER_ID MATCHES "^(GNU|LFort)$")
   # ``-Wextra`` includes ``no-compare-reals``, but we have comparisons like
   # ``value == 0.0_dp``.
   target_compile_options(
@@ -64,7 +64,7 @@ elseif(CMAKE_Fortran_COMPILER_ID STREQUAL "Intel")
     target_compile_options(quadpack PUBLIC -fast)
   endif()
 else()
-  message(FATAL_ERROR "gfortran and lfortran are the only supported compilers (current compiler ID is ${CMAKE_Fortran_COMPILER_ID})")
+  message(FATAL_ERROR "gfortran, ifort and lfortran are the only supported compilers (current compiler ID is ${CMAKE_Fortran_COMPILER_ID})")
 endif()

 # Restore all Fortran flags.

As mentioned compiling F77 / .f files fails. Also some of the gfortran flags don't carry over so I'll need to refine this a bit more.

dhermes commented 1 year ago

norm2() also a problem