Skip to content

Instantly share code, notes, and snippets.

@scivision
Last active September 11, 2026 22:42
Show Gist options
  • Select an option

  • Save scivision/5727a6813c6332e065a196f92c850240 to your computer and use it in GitHub Desktop.

Select an option

Save scivision/5727a6813c6332e065a196f92c850240 to your computer and use it in GitHub Desktop.
Find maximum source file length a Fortran compiler can handle with CMake

Maximum Fortran source code line length

The proposed Fortran 202x standard raised the individual line limit to 10000 characters, with unlimited continuation lines, and a maximum statement length of a million characters. These are stated as "hard limits" but some compilers allow going beyond 10000 characters in a line or even a million characters in a line. For example LLVMFlang is explicitly designed to not have a particular character limit per line, while even today's HPC may default to a Gfortran older than 14 that has a default Fortran source code line length maximum of 132 characters, which is too short for say generated source code containing a data path on a networked HPC drive.

It's important to be cognizant of the maximum Fortran line length across compilers as developer laptops often default to a recent Flang or Gfortran that both have effectively unlimited line length.

The maximum line length we experienced with no options follows.

Compiler Maximum Line Length
gfortran 8.5.0 132
gfortran 9.3.0 132
gfortran 10.2.0 132
gfortran 11.2.0 132
gfortran 12.2.0 132
gfortran 13.2.0 132
gfortran 14.3.0 10000
gfortran 15.2.0 10000
gfortran 16.2.0 10000
NVHPC SDK 23.5 1000
NVHPC SDK 25.5 1000
NVHPC SDK 26.5 1000
IntelLLVM ifx 2023.1 7196
IntelLLVM ifx 2024.0 7196
IntelLLVM ifx 2026.1 7196
LLVMFlang 22.1.8 > 1,000,000

Usage

cmake -B build

As usual, if desired specify compiler in the above command with -DCMAKE_Fortran_COMPILER=<compiler>. Set the character line length limit by -DN=<value>.

Examples:

cmake -Bbuild -DCMAKE_Fortran_COMPILER=gfortran -DN=10001

-- Try to compile source with line length 10001
-- Try to compile source with line length 10001 - FAIL
cmake -Bbuild -DCMAKE_Fortran_COMPILER=gfortran -DN=10001 -DCMAKE_Fortran_FLAGS=-ffree-line-length-none

-- Try to compile source with line length 10001
-- Try to compile source with line length 10001 - OK

For Gfortran only, the maximum line length can be arbitrarily set, which we optionally enable with -DM=<value> like:

cmake -Bbuild -DCMAKE_Fortran_COMPILER=gfortran -DN=133 -DM=132
# intentionally set N greater than M to demonstrate failure

Compiler options

The "compiler.cmake" file example shows how one can manipulate Fortran source code line limits.

Example error messages from Fortran excessive line length

Gfortran:

Error: Unterminated character constant beginning at (1)

Intel oneAPI ifx:

catastrophic error: Token too long

NVHPC nvfortran:

NVFORTRAN-F-0923-String too long

Flang: didn't have a practical line length limit.

cmake_minimum_required(VERSION 3.25)
project(MaxFortran LANGUAGES Fortran)
set(CMAKE_EXECUTE_PROCESS_COMMAND_ECHO STDOUT)
if(NOT DEFINED N)
if(CMAKE_Fortran_COMPILER_ID STREQUAL "NVHPC")
# tested with NVHPC SDK 26.5
set(N 1000)
elseif(CMAKE_Fortran_COMPILER_ID STREQUAL "GNU")
# tested with GCC 15.2.0 - 10000 is max without -free-line-length-none
set(N 10000)
# -Dopt="-ffree-line-length-none" then N is limited by computer RAM, basically unlimited for practical use
elseif(CMAKE_Fortran_COMPILER_ID STREQUAL "IntelLLVM")
set(N 7196)
# Severe: Input line too long for buffer
elseif(CMAKE_Fortran_COMPILER_ID STREQUAL "Cray")
# haven't tested Cray ftn yet
set(N 100000)
elseif(CMAKE_Fortran_COMPILER_ID STREQUAL "LLVMFlang")
# N is limited by computer RAM, basically unlimited for practical use
set(N 10000000)
else()
set(N 10000)
endif()
endif()
# Flang -ffree-line-length-* was removed:
# https://github.com/llvm/llvm-project/pull/199018
if(CMAKE_Fortran_COMPILER_ID STREQUAL "GNU")
if(DEFINED M)
set(opt -ffree-line-length-${M})
endif()
endif()
math(EXPR Nr "${N}-2")
string(RANDOM LENGTH "${Nr}" long_str)
set(src "program longer
character(*), parameter :: long_str = &
'${long_str}'
print '(a)', long_str
print '(i0)', len_trim(long_str)
end program"
)
# include(CheckSourceCompiles)
# set(CMAKE_REQUIRED_FLAGS "${opt}") # must be space-separated, not ;-list
# check_source_compiles(Fortran "${src}" ok)
message(CHECK_START "Try to compile source with line length ${N}")
try_compile(ok
SOURCE_FROM_VAR main.f90 src
COMPILE_DEFINITIONS "${opt}"
OUTPUT_VARIABLE out
NO_CACHE)
if(ok)
message(CHECK_PASS "OK")
else()
message(CHECK_FAIL "FAIL")
message(VERBOSE "${out}")
endif()
file(GENERATE OUTPUT .gitignore CONTENT "*")
if(CMAKE_Fortran_COMPILER_ID STREQUAL "GNU")
set(_fortran_free_flag "-ffree-line-length-none")
elseif(CMAKE_Fortran_COMPILER_ID STREQUAL "LLVMFlang")
# Flang does not have a free-form column limit https://github.com/llvm/llvm-project/issues/167912#issuecomment-3529004167
elseif(CMAKE_Fortran_COMPILER_ID STREQUAL "IntelLLVM")
# this diagnostic occurs if compiler option "-stand" was used
set(_fortran_free_flag "-diag-disable:5268")
elseif(CMAKE_Fortran_COMPILER_ID STREQUAL "NVHPC")
set(_fortran_free_flag "-Mextend")
elseif(CMAKE_Fortran_COMPILER_ID STREQUAL "Cray")
# already allows no limit for free form
else()
set(_fortran_free_flag "")
endif()
target_compile_options(mytarg PRIVATE $<$<COMPILE_LANGUAGE:Fortran>:${_fortran_free_flag}>)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment