-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompilerConfig.cmake
More file actions
131 lines (120 loc) · 5.4 KB
/
CompilerConfig.cmake
File metadata and controls
131 lines (120 loc) · 5.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# cmake/CompilerConfig.cmake
#
# Compiler and platform configuration for SIMPLE.
# Requires: CMake >= 3.25, GCC/GFortran >= 14 (Linux), GFortran + AppleClang (macOS).
# ------------------------------------------------------------------------------
# Basic compiler sanity checks
# ------------------------------------------------------------------------------
if(NOT CMAKE_Fortran_COMPILER_ID STREQUAL "GNU")
message(FATAL_ERROR "Only GFortran is supported for Fortran. Found: ${CMAKE_Fortran_COMPILER_ID}")
endif()
if(APPLE)
# On macOS, allow AppleClang for C/C++ if GFortran is used
if(NOT (CMAKE_C_COMPILER_ID MATCHES "Clang|AppleClang" OR CMAKE_C_COMPILER_ID STREQUAL "GNU"))
message(FATAL_ERROR "C compiler must be GCC or AppleClang on macOS. Found: ${CMAKE_C_COMPILER_ID}")
endif()
if(NOT (CMAKE_CXX_COMPILER_ID MATCHES "Clang|AppleClang" OR CMAKE_CXX_COMPILER_ID STREQUAL "GNU"))
message(FATAL_ERROR "C++ compiler must be G++ or AppleClang on macOS. Found: ${CMAKE_CXX_COMPILER_ID}")
endif()
else()
# On Linux, enforce GCC/G++
if(NOT CMAKE_C_COMPILER_ID STREQUAL "GNU")
message(FATAL_ERROR "Only GCC is supported for C on Linux. Found: ${CMAKE_C_COMPILER_ID}")
endif()
if(NOT CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
message(FATAL_ERROR "Only G++ is supported for C++ on Linux. Found: ${CMAKE_CXX_COMPILER_ID}")
endif()
endif()
# Require modern GFortran
if(CMAKE_Fortran_COMPILER_VERSION VERSION_LESS "14.0")
message(FATAL_ERROR "GFortran 14.0 or higher required. Found: ${CMAKE_Fortran_COMPILER_VERSION}")
endif()
# ------------------------------------------------------------------------------
# Language standards
# ------------------------------------------------------------------------------
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# ------------------------------------------------------------------------------
# Platform-specific tuning
# ------------------------------------------------------------------------------
if( USE_ARCHOPT )
if(APPLE)
execute_process(
COMMAND sysctl -n machdep.cpu.brand_string
OUTPUT_VARIABLE APPLE_CPU
OUTPUT_STRIP_TRAILING_WHITESPACE
)
if(APPLE_CPU MATCHES "Apple M")
set(ARCH_FLAG "-mtune=generic")
else()
set(ARCH_FLAG "-mtune=native")
endif()
else()
set(ARCH_FLAG "-march=native")
endif()
else()
set(ARCH_FLAG "")
endif()
# ------------------------------------------------------------------------------
# Global preprocessor definitions
# ------------------------------------------------------------------------------
if(APPLE)
add_compile_definitions(__FreeBSD__ MACOSX)
elseif(WIN32)
add_compile_definitions(_WIN32 WINDOWS)
else()
add_compile_definitions(__linux__ LINUX)
endif()
add_compile_definitions(
GNU # your historic define
USE_F08=1 # Fortran 2008 support
$<$<CONFIG:Debug>:_DEBUG>
)
# ------------------------------------------------------------------------------
# Fortran compile flags
# Use target_compile_options where possible, but keep defaults for all targets.
# ------------------------------------------------------------------------------
# Common Fortran flags (apply to all configurations)
string(APPEND CMAKE_Fortran_FLAGS
" -cpp -ffree-form -fimplicit-none -ffree-line-length-none"
" -fno-second-underscore -Wall -Waliasing -Wampersand"
" -Wsurprising -Wline-truncation"
" -D__FILENAME__='\"\\$(notdir \\$<)\"'")
# Release flags for Fortran
# for checking vectorization:
#-O3 -march=native -fopenmp -fopt-info-vec-optimized -fopt-info-vec-missed
# inspect vectorization reports: -qopt-report, -fopt-info-vec
# -ffast-math is unsafe, causes bugs
set(CMAKE_Fortran_FLAGS_RELEASE "-O3 -w -funroll-loops ${ARCH_FLAG} -fPIC"
CACHE STRING "Release flags for Fortran" FORCE)
# Debug flags for Fortran
set(CMAKE_Fortran_FLAGS_DEBUG "-O0 -g -fbacktrace -fbounds-check -fcheck=all -Wuninitialized -Wunused -fPIC"
CACHE STRING "Debug flags for Fortran" FORCE)
# ------------------------------------------------------------------------------
# C / C++ compile flags
# ------------------------------------------------------------------------------
set(CMAKE_C_FLAGS_RELEASE "-O3 ${ARCH_FLAG} -fPIC"
CACHE STRING "Release flags for C" FORCE)
set(CMAKE_C_FLAGS_DEBUG "-O0 -g -fPIC"
CACHE STRING "Debug flags for C" FORCE)
set(CMAKE_CXX_FLAGS_RELEASE "-O3 ${ARCH_FLAG} -fPIC"
CACHE STRING "Release flags for C++" FORCE)
set(CMAKE_CXX_FLAGS_DEBUG "-O0 -g -fPIC"
CACHE STRING "Debug flags for C++" FORCE)
# Suppress Clang deployment version warning on macOS
if(APPLE AND CMAKE_C_COMPILER_ID MATCHES "Clang|AppleClang")
string(APPEND CMAKE_C_FLAGS " -Wno-overriding-deployment-version")
string(APPEND CMAKE_CXX_FLAGS " -Wno-overriding-deployment-version")
endif()
# ------------------------------------------------------------------------------
# RPATH for installed shared libraries
# ------------------------------------------------------------------------------
if(APPLE)
set(CMAKE_INSTALL_RPATH "@executable_path/../lib;${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}")
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
else()
set(CMAKE_INSTALL_RPATH "$ORIGIN/../lib:${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}")
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
endif()