Skip to content

Instantly share code, notes, and snippets.

@scivision
Last active October 21, 2025 14:37
Show Gist options
  • Save scivision/b85e6b5d70ba6b9d401bcaeb4249ce24 to your computer and use it in GitHub Desktop.
Save scivision/b85e6b5d70ba6b9d401bcaeb4249ce24 to your computer and use it in GitHub Desktop.
Diagnose issue with Fortran directive, CMake, Ninja, and Flang

Diagnose issues with CMake, Ninja, Flang

Preprocessed Fortran files may contain OpenMP directives with comment lines starting with:

!$omp

We observe that CMake + Ninja + Flang breaks when an OpenMP directive is inside a call subprocess statement as in mini.F

This is relevant to programs like MUMPS.

Test

cmake -P main.cmake

Result:

-- OK: gfortran Ninja
CMake Warning at main.cmake:45 (message):
  FAIL: flang Ninja


-- Error: 1
      [1/3] Generating Fortran dyndep file CMakeFiles/mini.dir/Fortran.dd
[2/3] Building Fortran object CMakeFiles/mini.dir/mini.F.o
FAILED: [code=1] CMakeFiles/mini.dir/mini.F.o 
/opt/homebrew/bin/flang -I/tmp/flangc -O2 -g -ffixed-line-length-72 -c CMakeFiles/mini.dir/mini.F-pp.f -o CMakeFiles/mini.dir/mini.F.o
error: Could not scan CMakeFiles/mini.dir/mini.F-pp.f
/tmp/flangc/mini.F:3:17: error: Unmatched '('
        call mysub(a, b
                  ^
/tmp/flangc/mini.F:5:8: error: Unmatched ')'
         )
         ^
ninja: build stopped: subcommand failed.

      
-- OK: gfortran Unix Makefiles
-- OK: flang Unix Makefiles

Versions

% flang --version
Homebrew flang version 21.1.3
Target: arm64-apple-darwin25.0.0
Thread model: posix
InstalledDir: /opt/homebrew/Cellar/flang/21.1.3/libexec
Configuration file: /opt/homebrew/Cellar/flang/21.1.3/libexec/flang.cfg
Configuration file: /opt/homebrew/etc/clang/arm64-apple-darwin25.cfg

% ninja --version
1.13.1

% cmake --version
cmake version 4.1.2
cmake_minimum_required(VERSION 3.20)
project(mini LANGUAGES Fortran)
message(STATUS "CMAKE Generator: ${CMAKE_GENERATOR}")
add_executable(mini mini.F)
# Try building a mini project with Flang and Gfortran, using Make and Ninja
#
# Usage:
# cmake -P main.cmake
set(gens "Ninja")
if(WIN32)
list(APPEND gens "MinGW Makefiles")
else()
list(APPEND gens "Unix Makefiles")
endif()
set(comps gfortran flang)
set(errc 0)
foreach(gen IN LISTS gens)
foreach(comp IN LISTS comps)
# Configure the project
execute_process(
COMMAND ${CMAKE_COMMAND}
-G ${gen}
-DCMAKE_Fortran_COMPILER=${comp}
-S ${CMAKE_SOURCE_DIR}
-B ${CMAKE_BINARY_DIR}/build_${gen}_${comp}
RESULT_VARIABLE r
OUTPUT_VARIABLE out
ERROR_VARIABLE err
)
if(NOT r EQUAL 0)
message(FATAL_ERROR "Configuration fail: ${comp} ${gen}
${r}
${err}")
endif()
# Build the project
execute_process(
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR}/build_${gen}_${comp}
RESULT_VARIABLE r
OUTPUT_VARIABLE out
ERROR_VARIABLE err
)
if(NOT r EQUAL 0)
math(EXPR errc "${errc} + 1")
message(WARNING "FAIL: ${comp} ${gen}")
message(STATUS "Error: ${r}
${out}
${err}")
continue()
endif()
message(STATUS "OK: ${comp} ${gen}")
endforeach()
endforeach()
if(errc GREATER 0)
message(FATAL_ERROR "${errc} build(s) failed")
endif()
program mini
call mysub(a, b
!$omp & ,dummy
& )
contains
subroutine mysub(a, b)
print *, "In mysub: a =", a, " b =", b
end subroutine mysub
end program
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment