Both Meson and CMake can handle your Fortran project requirements effectively, though they have different approaches to organizing build outputs.
For Meson, you'd create a meson.build file in your project root. Meson naturally separates source and build artifacts by using an out-of-source build directory. Here's how you could structure it:
project('your_fortran_project', 'fortran')
# Define source files from src directory
src_files = files('src/main.f90', 'src/module1.f90', 'src/module2.f90')
# Create executable
executable('your_program', src_files, install: true, install_dir: 'bin')
You'd then build with meson setup builddir followed by meson compile -C builddir. Meson automatically places object files and modules in the build directory, and you can specify where executables go with the install_dir parameter.
CMake offers more explicit control over output directories. Your CMakeLists.txt might look like:
cmake_minimum_required(VERSION 3.15)
project(your_fortran_project Fortran)
# Set output directories
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_Fortran_MODULE_DIRECTORY ${CMAKE_BINARY_DIR}/modules)
# Collect source files
file(GLOB_RECURSE SOURCES "src/*.f90")
# Create executable
add_executable(your_program ${SOURCES})
# Set properties for object file location
set_target_properties(your_program PROPERTIES
ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/obj
LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/obj
)
For your specific directory requirements, CMake gives you more granular control over where different file types end up. The CMAKE_Fortran_MODULE_DIRECTORY variable specifically handles Fortran module files, which is particularly useful since Fortran modules need to be findable during compilation.
Meson tends to be simpler to set up and has good Fortran support out of the box, while CMake offers more flexibility but requires more configuration. Both will handle Fortran module dependencies automatically, which is one of the main advantages over manual makefiles.
Which approach appeals more to you, or do you have specific features of your current makefile that you're concerned about replicating?