Scientific Computing

Identifying Fortran compiler with CMake

Projects use a variety of methods to detect which compiler is being used to thereby set compilation options. Although this discussion focuses on Fortran, it is easily and equally applicable to other languages such as C and C++.

Robustly detect compiler in CMake CMAKE_Fortran_COMPILER_ID. This tells the compiler vendor (GNU, Intel, Clang, etc.) Don’t use CMAKE_Fortran_COMPILER because there are several compiler executables per vendor and this will not be robust over time. To get the compiler version, CMAKE_Fortran_COMPILER_VERSION allows version comparisons.

Example:

if(CMAKE_Fortran_COMPILER_ID MATCHES "^Intel")
  add_compile_options("$<$<COMPILE_LANGUAGE:Fortran>:-check;-warn;-traceback>")
elseif(CMAKE_Fortran_COMPILER_ID STREQUAL "GNU")
  # Gfortran
  add_compile_options("$<$<COMPILE_LANGUAGE:Fortran>:-Wall;-Werror=array-bounds;-Wextra;-Wpedantic>")
endif()

Cygwin startup environment variables

Cygwin startup configuration is controlled by Windows environment variable CYGWIN. Not populating Cygwin PATH from Windows PATH avoids confusion from Windows programs being used instead of Cygwin programs. Set Windows environment variable:

CYGWIN_NOWINPATH=1

will result in a basic Cygwin PATH like:

/usr/local/bin:/usr/bin

Look inside Cygwin “/etc/profile” for more environment variables that may be of interest.

NOTE: all Windows environment variables are imported by Cygwin. Override these variables in ~/.profile or scripts if desired.


Related: don’t populate WSL PATH from Windows PATH.

Use libutil on macOS and Linux

Libutil gives abstractions for OS-specific TTY operations. When using these abstractions across macOS and Linux, use this preprocessing statement for the appropriate header:

#ifdef __APPLE__
#include <util.h>
#else
#include <pty.h>
#endif

If using CMake, ensure the library and header are found:

find_library(UTIL_LIBRARY NAMES util)

if(APPLE)
  find_path(UTIL_INCLUDE_DIR NAMES util.h)
elseif(UNIX)
  find_path(UTIL_INCLUDE_DIR NAMES pty.h)
endif()

Cross-compile for DOS from Windows or Linux

OpenWatcom is an open-source C/C++ compiler that can compile for a variety of systems, particularly legacy 16/32-bit Windows and DOS systems. This can be of interest for retro gamers and those using old devices that have DOS-based software, including industrial controllers and two-way radio programming.

CMake supports OpenWatcom, and is conveniently used with a toolchain file. GitHub Actions has easy scripting for OpenWatcom to test building of DOS programs from Linux.

The easiest way to show this is by example: see the PDCurses CMake Watcom toolchain file and GitHub Actions yml for how to build with OpenWatcom for DOS from Windows/Linux and GitHub Actions build CI.

To run the programs build for DOS, use DOSBox-X.

Force older language standard in CMake

CMake target_compile_features sets a transitive MINIMUM language standard necessary. If the compiler defaults to a newer language standard, target_compile_features() allows that default higher language standard.

target_compile_features(<target> PRIVATE cxx_std_98)

This can make issues for legacy code that requires an older language standard. For example, an old C++98 code may need to have the compiler in C++98 mode. This is accomplished with the target property CXX_STANDARD. Other languages may have a similar property e.g. C_STANDARD.

Example: if C++98 needed for old code:

set_property(TARGET old PROPERTY CXX_STANDARD 98)

CMake import interface link

CMake can add interface linking to imported libraries. For example, a imported library obtained by find_package() or otherwise. Normally, this would work like the following example to say link “stdc++fs” to example imported library “imported::lib” for GCC older than 9.1.

if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 9.1.0)
  target_link_libraries(imported::lib INTERFACE stdc++fs)
endif()

Function target_link_libraries() should work, but does not always work for some projects with a configure time error. To workaround this issue if it arises, set target property INTERFACE_LINK_LIBRARIES directly like:

set_property(TARGET imported::lib PROPERTY INTERFACE_LINK_LIBRARIES stdc++fs)

CMake CTest single command build and test

Many CMake users have muscle memory for the three-stanza configure, build, test with CMake:

cmake -B build
# configure CMake and generate build files

cmake --build build
# compile and link binaries

ctest --test-dir build
# run program self-tests

This can be reduced to a single command for many programs:

ctest -S setup.cmake

where a single file setup.cmake is added to the project.

Command-line options like “-Dvar=yes” must be aggregated and passed along to ctest_configure(OPTIONS) in setup.cmake.

Get CPU count from Matlab

Capture the number of physical CPU cores available on a computer from Matlab:

function N = get_cpu_count()
%% get apparent number of physical CPU cores

N = maxNumCompThreads;
if N < 2  % happens on some HPC
  N = feature('NumCores');
end

end

Related: Python CPU count