Skip to content

Commit

Permalink
use configure_file instead of preprocessing
Browse files Browse the repository at this point in the history
  • Loading branch information
scivision committed Apr 19, 2020
1 parent b7def64 commit 4bb821c
Show file tree
Hide file tree
Showing 21 changed files with 119 additions and 151 deletions.
16 changes: 3 additions & 13 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@ jobs:
with:
python-version: '3.x'

- run: pip install meson
- name: Install packages
run: |
sudo apt update -yq
sudo apt install -yq --no-install-recommends ninja-build
- run: pip install meson ninja

- run: meson setup build
env:
Expand All @@ -46,10 +42,7 @@ jobs:
env:
FC: gfortran-9

- run: cmake --build build --parallel

- run: ctest -V
working-directory: build
- run: ctest -S setup.cmake -VV
- uses: actions/upload-artifact@v1
if: failure()
with:
Expand All @@ -60,10 +53,7 @@ jobs:
env:
FC: gfortran-9

- run: cmake --build build --parallel

- run: ctest -V
working-directory: build
- run: ctest -S setup.cmake -VV
- uses: actions/upload-artifact@v1
if: failure()
with:
Expand Down
59 changes: 29 additions & 30 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,47 +1,46 @@
cmake_minimum_required(VERSION 3.14)

if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release CACHE STRING "Debug or Release")
set(CMAKE_BUILD_TYPE Release CACHE STRING "default build type")
endif()

project(MapTran
LANGUAGES Fortran
VERSION 1.1.1
VERSION 1.1.2
HOMEPAGE_URL https://github.com/geospace-code/maptran3d)
enable_testing()

if(NOT realbits)
set(realbits 64)
endif()
enable_testing()
include(CTest)
include(FeatureSummary)

include(cmake/compilers.cmake)

if(realbits EQUAL 32)
set(wp_real "wp=>real32")
else()
set(wp_real "wp=>real64")
endif()

# OPTIONAL link-time optimization
if(CMAKE_BUILD_TYPE STREQUAL Release)
include(CheckIPOSupported)
check_ipo_supported(RESULT lto_ok OUTPUT _err)

if(lto_ok)
message(STATUS "IPO / LTO enabled")
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
else()
message(STATUS "IPO / LTO disabled: ${_err}")
endif()
if(PROJECT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
if(CMAKE_BUILD_TYPE STREQUAL Release)
include(CheckIPOSupported)
check_ipo_supported(RESULT lto_ok OUTPUT _err)

if(lto_ok)
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
endif()
endif()
endif()

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
set(CTEST_TEST_TIMEOUT 30)

# --- Maptran library
add_library(maptran)
target_compile_definitions(maptran PRIVATE REALBITS=${realbits})
target_include_directories(maptran INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include>
$<INSTALL_INTERFACE:include>)
set_target_properties(maptran PROPERTIES
Fortran_MODULE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/include)
add_library(maptran::maptran ALIAS maptran)
install(TARGETS maptran
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib)

add_subdirectory(src)
add_subdirectory(src/tests)

if(PROJECT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
add_subdirectory(src/tests)

add_feature_info(ipo lto_ok "Interprocess / Link optimization ${_err}")
feature_summary(WHAT ALL)
endif()
29 changes: 8 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,40 +12,27 @@ and Matlab / GNU Octave
## Install

Requires Fortran 2008 compiler, such as `gfortran`, `ifort`, PGI, `nagfor`, `flang`, Cray, IBM XL, etc.
Use CMake or Meson to build the suite, which creates `libmaptran.so` or similar, a shared library with compile-time polymorphism enabled by configuring Fortran preprocessor with one of:
Use CMake or Meson to build the suite, which creates `libmaptran.a` or similar.
Compile-time polymorphism enabled by configuring with one of:

* `-Drealbits=32`
* `-Drealbits=64`
* `-Drealbits=128`

Note: as with any program or programming language, the accuracy of 32-bit reals can be significantly degraded, by orders of magnitude compared to 64-bit reals that are the default for many years.
The large real values typical of map coordinates can lead to large error with 32-bit reals.
64-bit reals are the default.

### Meson

```sh
meson build

meson test -C build
```
64-bit real is the default.

### CMake

```sh
cmake -B build

cmake --build build --parallel

cd build

ctest -V
ctest -S setup.cmake -VV
```

Optionally, verify Fortran functionality:
### Meson

```sh
ctest -V
meson build

meson test -C build
```

## Usage
Expand Down
15 changes: 4 additions & 11 deletions cmake/compilers.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ else()
string(APPEND CMAKE_Fortran_FLAGS " -march=native -stand f18 -traceback -warn -heap-arrays")
endif()
elseif(CMAKE_Fortran_COMPILER_ID STREQUAL GNU)
string(APPEND CMAKE_Fortran_FLAGS " -Wall -Wextra -fimplicit-none")
string(APPEND CMAKE_Fortran_FLAGS " -fimplicit-none")
string(APPEND CMAKE_Fortran_FLAGS_DEBUG " -fcheck=all -Werror=array-bounds")
# -march=native is not for all CPU arches with GCC.
add_compile_options(-mtune=native)
add_compile_options(-mtune=native -Wall -Wextra)

if(CMAKE_Fortran_COMPILER_VERSION VERSION_GREATER_EQUAL 8)
string(APPEND CMAKE_Fortran_FLAGS " -std=f2018")
Expand All @@ -24,14 +24,7 @@ elseif(CMAKE_Fortran_COMPILER_ID STREQUAL NAG)
endif()

include(CheckFortranSourceCompiles)
check_fortran_source_compiles("implicit none (external); end" f2018impnone SRC_EXT f90)
check_fortran_source_compiles("implicit none (type, external); end" f2018impnone SRC_EXT f90)
if(NOT f2018impnone)
message(FATAL_ERROR "Compiler does not support Fortran 2018 IMPLICIT NONE (EXTERNAL): ${CMAKE_Fortran_COMPILER_ID} ${CMAKE_Fortran_COMPILER_VERSION}")
message(FATAL_ERROR "Compiler does not support Fortran 2018 IMPLICIT NONE (type, external): ${CMAKE_Fortran_COMPILER_ID} ${CMAKE_Fortran_COMPILER_VERSION}")
endif()

include(CheckFortranSourceRuns)
check_fortran_source_runs("use, intrinsic :: ieee_arithmetic, only: ieee_value, ieee_quiet_nan, ieee_is_nan
real :: r
r = ieee_value(1., ieee_quiet_nan)
if (.not.ieee_is_nan(r)) error stop
end program" f03nan)
16 changes: 9 additions & 7 deletions meson.build
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
project('MapTran', 'fortran',
version: '1.1.1',
version: '1.1.2',
meson_version : '>=0.51.2',
default_options : ['default_library=static', 'buildtype=release', 'warning_level=3'])

realbits = '-DREALBITS=' + get_option('realbits')
default_options : ['default_library=static', 'buildtype=release', 'warning_level=2'])

fc = meson.get_compiler('fortran')
if fc.get_id() == 'intel'
Expand All @@ -12,11 +10,15 @@ elif fc.get_id() == 'intel-cl'
add_project_arguments('/fpp', '/heap-arrays', language : 'fortran')
endif

wp_real = get_option('realbits')=='32' ? 'wp=>real32' : 'wp=>real64'

wp_conf = configuration_data()
wp_conf.set('wp_real', wp_real)

subdir('src')
# --- Maptran library
maptran = library('maptran',
sources: srcs,
fortran_args : realbits,
install: true)
sources: srcs,
install: true)

subdir('src/tests')
2 changes: 1 addition & 1 deletion meson_options.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
option('realbits', type : 'combo', choices : ['64', '32', '128'], description: 'bits of precision for real')
option('realbits', type : 'combo', choices : ['64', '32'], description: 'bits of precision for real')
27 changes: 21 additions & 6 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
target_sources(maptran PRIVATE maptran.F90 aer.f90 ecef.f90 enu.f90 vallado.F90 utils.f90)
if(f03nan)
target_sources(maptran PRIVATE sphere.f90)
else()
target_sources(maptran PRIVATE no_sphere.f90)
endif()
configure_file(maptran.in.f90 maptran.f90)
configure_file(vallado.in.f90 vallado.f90)

add_library(maptran
${CMAKE_CURRENT_BINARY_DIR}/maptran.f90 ${CMAKE_CURRENT_BINARY_DIR}/vallado.f90
aer.f90 ecef.f90 enu.f90 sphere.f90 utils.f90)

target_include_directories(maptran INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include>
$<INSTALL_INTERFACE:include>)

set_target_properties(maptran PROPERTIES
ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}
LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}
Fortran_MODULE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/include)

add_library(maptran::maptran ALIAS maptran)

install(TARGETS maptran
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib)
2 changes: 1 addition & 1 deletion src/aer.f90
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
submodule (maptran) aer

implicit none (external)
implicit none (type, external)

contains

Expand Down
2 changes: 1 addition & 1 deletion src/ecef.f90
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
submodule (maptran) ecef

implicit none (external)
implicit none (type, external)

contains

Expand Down
2 changes: 1 addition & 1 deletion src/enu.f90
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
submodule (maptran) enu

implicit none (external)
implicit none (type, external)

contains

Expand Down
13 changes: 4 additions & 9 deletions src/maptran.F90 → src/maptran.in.f90
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
module maptran
use, intrinsic:: ieee_arithmetic, only: ieee_quiet_nan, ieee_value
#if REALBITS==32
use, intrinsic:: iso_fortran_env, only: wp=>real32
#elif REALBITS==128
use, intrinsic :: iso_fortran_env, only : real128
#else
use, intrinsic:: iso_fortran_env, only: wp=>real64
#endif

implicit none (external)
use, intrinsic:: iso_fortran_env, only: @wp_real@


implicit none (type, external)
private
public :: wp, pi, ecef2geodetic, geodetic2ecef, aer2enu, enu2aer, aer2ecef, ecef2aer, &
enu2ecef, ecef2enu, ecef2enuv, aer2geodetic, geodetic2enu, enu2uvw,&
Expand Down
15 changes: 14 additions & 1 deletion src/meson.build
Original file line number Diff line number Diff line change
@@ -1 +1,14 @@
srcs = files('maptran.F90', 'vallado.F90', 'aer.f90', 'ecef.f90', 'enu.f90', 'sphere.f90', 'utils.f90')
configure_file(
input : 'maptran.in.f90',
output : 'maptran.f90',
configuration : wp_conf)

configure_file(
input : 'vallado.in.f90',
output : 'vallado.f90',
configuration : wp_conf
)

srcs = files('aer.f90', 'ecef.f90', 'enu.f90', 'sphere.f90', 'utils.f90',
meson.current_build_dir() / 'maptran.f90',
meson.current_build_dir() / 'vallado.f90')
11 changes: 0 additions & 11 deletions src/no_sphere.f90

This file was deleted.

2 changes: 1 addition & 1 deletion src/sphere.f90
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
submodule (maptran) sphere

implicit none (external)
implicit none (type, external)

contains

Expand Down
8 changes: 3 additions & 5 deletions src/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
add_executable(test_maptran test_mod.f90 assert.F90)
configure_file(assert.in.f90 assert.f90)

add_executable(test_maptran test_mod.f90 ${CMAKE_CURRENT_BINARY_DIR}/assert.f90)
target_link_libraries(test_maptran maptran::maptran)
target_compile_definitions(test_maptran PRIVATE REALBITS=${realbits})
add_test(NAME unit:maptran COMMAND $<TARGET_FILE:test_maptran>)

if(NOT PROJECT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
return()
endif()

add_executable(benchmark_maptran benchmark.f90)
target_link_libraries(benchmark_maptran maptran::maptran)
12 changes: 2 additions & 10 deletions src/tests/assert.F90 → src/tests/assert.in.f90
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,10 @@ module assert
!! Gfortran >= 6 needed for ieee_arithmetic: ieee_is_nan

use, intrinsic:: iso_c_binding, only: sp=>c_float, dp=>c_double
use, intrinsic:: iso_fortran_env, only: stderr=>error_unit, real32, real64, real128
use, intrinsic:: iso_fortran_env, only: stderr=>error_unit, @wp_real@
use, intrinsic:: ieee_arithmetic, only: ieee_is_finite, ieee_is_nan

implicit none (external)

#if REALBITS==32
integer,parameter :: wp=real32
#elif REALBITS==128
integer,parameter :: wp=real128
#else
integer,parameter :: wp=real64
#endif
implicit none (type, external)

private

Expand Down
2 changes: 1 addition & 1 deletion src/tests/benchmark.f90
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ program benchmark_maptran
use, intrinsic:: iso_fortran_env, only: int64, real64
use maptran, only: wp, geodetic2ecef

implicit none (external)
implicit none (type, external)

integer(int64) :: tic, toc, rate
real(real64) :: time
Expand Down
Loading

0 comments on commit 4bb821c

Please sign in to comment.