Netlib F2C
This project provides a CMake framework, that automatically downloads and builds
the Fortran-to-C converter f2c
from Netlib (https://www.netlib.org/f2c/) and
the accompanying library libf2c
.
Prerequisites
What you need:
- Git (optional) for getting the code and contributing to the project
- CMake and Ninja for building the project
- C compiler
- GCC for Linux (https://gcc.gnu.org/)
- Clang for Mac OS X (https://clang.llvm.org/)
- Cygwin + GCC for Windows (https://www.cygwin.com/)
- LoadStaticSharedTargets (https://gitlab.aei.uni-hannover.de/cmake/loadstaticsharedtargets)
Linux (Ubuntu)
Use your distribution's package manager to install the necessary packages:
$ sudo apt-get install cmake ninja-build gcc
Mac OS X
You will need one of the community package managers for Mac OS X: Homebrew or MacPorts. For installing one of these, please refer to their respective installation instructions.
Homebrew
$ brew install cmake ninja llvm
MacPorts
$ sudo port -vb install cmake-devel ninja clang-<XX>
where you have to replace <XX>
with the latest stable version of Clang.
You can discover all available versions of Clang like this:
$ port search --name --line --regex '^clang-?\d*\.?\d*$'
Windows
The easiest thing to do is using the Windows Subsystem for Linux (WSL) and follow the Linux instructions above.
Otherwise, install Git for Windows for version
control and cygwin, which is a large
collection of GNU and Open Source tools which provide functionality similar to a
Linux distribution on Windows. During the cygwin
installation you'll be
prompted to add additional packages. Search and select the following:
-
cmake
,ninja
andgcc-core
for the compiler and the build system
After cygwin
finishes installing, use the cygwin terminal to start the build
process.
How to compile
-
(Optional) First, build the LoadStaticSharedTargets project and install it either system-wide or in a central location, e. g. somewhere in your home directory. Please refer to the project README for build instructions. If you skip this step, LoadStaticSharedTargets should automatically be downloaded and included in your build. But you might end up compiling some parts multiple times unnecessarily.
-
Clone the git repository or download the source code archive and unpack it to an arbitrary directory (e.g.
netlib-f2c
). -
Go to this directory and type
cmake --list-presets
. A list of available build configurations will be shown to you. -
For preparing a single configuration with
ninja
, typecmake --preset ninja-shared-release
, if you installed LoadStaticSharedTargets system-wide or not at all. Otherwise, type$ cmake --preset ninja-shared-release -D "CMAKE_PREFIX_PATH=/path/to/LoadStaticSharedTargets/install/prefix"
Replace the path to the LoadStaticSharedTargets install prefix with the actual path on your system! The license of the NetlibF2C source code will be shown to you. If you accept this license, add the required variable to the previous command:
$ cmake --preset ninja-shared-release -D NetlibF2C_LICENSE_ACCEPTED=YES
This will populate the build directory with a CMake configuration tree. You can also prepare all shared or static configurations at once by using
cmake --preset ninja-multi-shared
. By default, the configured installation directory is./install
. You can specify a different install location by setting theCMAKE_INSTALL_PREFIX
variable:$ cmake --preset ninja-shared-release -D "CMAKE_INSTALL_PREFIX=/path/to/install/prefix"
The
-D
option can be given multiple times, so you can combineCMAKE_PREFIX_PATH
,NetlibF2C_LICENSE_ACCEPTED
andCMAKE_INSTALL_PREFIX
. -
Now, you can build with
cmake --build --preset ninja-shared-release
. You should see some informative terminal output. If you used a Ninja Multi-Config in the previous step, you have to determine the config to build now:cmake --build --preset ninja-multi-shared-release
(repeat with the debug config). See all available build presets withcmake --build --list-presets
. -
Finally, install the built artifacts to the configured install prefix with
cmake --build --preset ninja-shared-release --target install
. If the configured install prefix is a system-wide location (like/usr/local
), installing might requiresudo
. If you used a Ninja Multi-Config in step 4, use the respective build preset for installing, too:cmake --build --preset ninja-multi-shared-release --target install
(repeat with the debug config). You can also install either only the executable or only the library withcmake --build --preset ninja-shared-release --target install --component executable
orcmake --build --preset ninja-shared-release --target install --component library
. -
Repeat this process for the single config presets
ninja-static-release
,ninja-shared-debug
andninja-static-debug
or for the other multi-config preset.
Now, the f2c
code is compiled and you can use it in other projects. The
installed f2c
package is discoverable by CMake as NetlibF2C. In the
CMakeLists.txt
of the other project, call:
include(FetchContent)
FetchContent_Declare(
NetlibF2C
GIT_REPOSITORY "git@gitlab.aei.uni-hannover.de:netlib/netlib-f2c.git"
GIT_TAG v1.7.1
SYSTEM
FIND_PACKAGE_ARGS 1.7.1 CONFIG NAMES NetlibF2C
)
set(NetlibF2C_INCLUDE_PACKAGING TRUE)
FetchContent_MakeAvailable(NetlibF2C)
This discovers an installed version of the NetlibF2C package or adds it to the other project's install targets. To help CMake discover the NetlibF2C package, call CMake for the other project like this:
$ cmake -B ./build -D "CMAKE_PREFIX_PATH=/path/to/NetlibF2C/install/prefix"
Replace the path to the NetlibF2C install prefix with the actual path on your
system! If NetlibF2C is installed in a proper system-wide location, the
CMAKE_PREFIX_PATH
shouldn't be necessary.
This makes two CMake targets available to you, the Netlib::f2c
executable
target and the Netlib::libf2c
library target (static or shared depends on the
value of BUILD_SHARED_LIBS
in the other project).
For more information read the f2c
executable's man page.
References
S. I. Feldman, David M. Gay, Mark W. Maimone, and N. L. Schryer. 1990. A Fortran-to-C Converter. AT&T Bell Laboratories Murray Hill, NJ 07974 (May 1990). Computing Science Technical Report No. 149. https://www.netlib.org/f2c/f2c.pdf