For a Fortran project with your folder structure requirements, both CMake and Meson would work well. Here's how to set up each:
CMake Setup
Create a CMakeLists.txt in your project root:
cmake_minimum_required(VERSION 3.10)
project(MyFortranProject Fortran)
# Set output directories
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_Fortran_MODULE_DIRECTORY ${CMAKE_BINARY_DIR}/modules)
# Add source files
file(GLOB_RECURSE SOURCES src/*.f90 src/*.f)
# Create executable
add_executable(myprogram ${SOURCES})
# Set object files location (CMake puts them in CMakeFiles by default)
# If you want them in a specific folder, you'd need custom commands
To build:
mkdir build
cd build
cmake ..
make
Meson Setup
Create a meson.build in your project root:
project('myfortranproject', 'fortran',
default_options : ['buildtype=release'])
# Get all source files
src_files = files(
'src/main.f90',
'src/module1.f90',
# Add more files or use a loop
)
# Create executable
executable('myprogram',
src_files,
install : true,
install_dir : 'bin'
)
To build:
meson setup builddir
cd builddir
meson compile
Key Differences
CMake:
- More mature Fortran support
- Better control over module file locations
- More widely used in scientific computing
- Can explicitly set module directory
Meson:
- Cleaner, more readable syntax
- Faster build configuration
- Built-in support for modern build practices
- Object files and modules go into the build directory automatically
For your specific requirements, CMake might be slightly better because it gives you direct control over the Fortran module directory location. Meson handles build artifacts well but gives you less granular control over where intermediate files go.
Both will handle the separate src/bin folders well, and both put object files in the build directory (though in slightly different subdirectory structures).