-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
195 lines (165 loc) · 6.59 KB
/
CMakeLists.txt
File metadata and controls
195 lines (165 loc) · 6.59 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
cmake_minimum_required(VERSION 3.17.5)
# define build environments
set( CMAKE_INSTALL_PREFIX "$ENV{HOME}/.local/diffstruc"
CACHE STRING "Select where to install the library." )
execute_process(COMMAND pwd OUTPUT_VARIABLE CURRENT_WORKING_DIR OUTPUT_STRIP_TRAILING_WHITESPACE)
message("Current working directory: ${CURRENT_WORKING_DIR}")
set( CMAKE_BUILD_PREFIX ${CURRENT_WORKING_DIR}
CACHE STRING "Select where to build the library." )
# set the project name
project(diffstruc NONE)
# set the library name
set( LIB_NAME ${PROJECT_NAME} )
set( PROJECT_DESCRIPTION
"Fortran neural network" )
set( PROJECT_URL "https://github.com/nedtaylor/diffstruc" )
set( CMAKE_CONFIGURATION_TYPES "Release" "Parallel" "Serial" "Dev" "Debug" "Parallel_Dev"
CACHE STRING "List of configurations types." )
set( CMAKE_BUILD_TYPE "Release"
CACHE STRING "Select which configuration to build." )
# change name based on parallel
if (CMAKE_BUILD_TYPE MATCHES "Parallel*")
project(diffstruc_omp NONE) # change project name to parallel
message(FATAL_ERROR "Configuration stopped because Parallel is not yet set up")
endif()
# set compiler
set(CMAKE_Fortran_COMPILER gfortran
CACHE STRING "Select fortran compiler." ) # Change this to your desired compiler
set(CMAKE_Fortran_STANDARD 2018)
# set language
enable_language(Fortran)
# set up the coreutils dependency
include(FetchContent)
# Disable testing for dependencies - both standard variables
set(BUILD_TESTING OFF)
set(BUILD_TESTS OFF)
FetchContent_Declare(
coreutils
GIT_REPOSITORY https://github.com/nedtaylor/coreutils.git
GIT_TAG main
FIND_PACKAGE_ARGS NAMES coreutils
)
FetchContent_MakeAvailable(coreutils)
# Re-enable BUILD_TESTS for our project
set(BUILD_TESTS ON)
find_package(coreutils REQUIRED)
message(STATUS "coreutils home directory: ${coreutils_DIR}")
message(STATUS "coreutils include directory: ${coreutils_INCLUDE_DIRS}")
message(STATUS "coreutils source directory: ${coreutils_BUILD_DIRS}")
# Only enable testing and options when this is the main project
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
enable_testing()
# set options for building tests and examples
option(BUILD_TESTS "Build the unit tests" ON)
option(BUILD_EXAMPLES "Build the examples" ON)
option(USE_BLAS "Use BLAS for optimised matrix operations" OFF)
else()
# When used as a dependency, disable tests by default
set(BUILD_TESTS OFF)
set(BUILD_EXAMPLES OFF)
endif()
# Define the sources
set(SRC_DIR src)
set(LIB_DIR ${SRC_DIR}/diffstruc)
# Automatically find all Fortran files in lib/
file(GLOB LIB_FILES "${LIB_DIR}/*.f90")
# Collect the main source file
file(GLOB SRC_FILES "${SRC_DIR}/*.f90")
# Combine all source files
set(ALL_SOURCES ${SRC_FILES} ${LIB_FILES})
# Optional: print for debugging
message(STATUS "LIB_FILES: ${LIB_FILES}")
message(STATUS "SRC_FILES: ${SRC_FILES}")
message(STATUS "ALL_SOURCES: ${ALL_SOURCES}")
# initialise flags
set(CPPFLAGS "")
set(CFLAGS "")
set(MODULEFLAGS "")
set(MPFLAGS "")
set(WARNFLAGS "")
set(DEVFLAGS "")
set(DEBUGFLAGS "")
set(MEMFLAGS "")
set(OPTIMFLAGS "")
set(FASTFLAGS "")
# set flags based on compiler
if (CMAKE_Fortran_COMPILER MATCHES ".*gfortran.*" OR CMAKE_Fortran_COMPILER MATCHES ".*gcc.*")
message(STATUS "Using gfortran compiler")
set(PPFLAGS -cpp)
set(MPFLAGS -fopenmp)
set(WARNFLAGS -Wall)
set(DEVFLAGS -g -fbacktrace -fcheck=all -fbounds-check -Og)
set(DEBUGFLAGS -fbounds-check)
set(MEMFLAGS -mcmodel=large)
set(OPTIMFLAGS -O3 -march=native)
set(FASTFLAGS -Ofast -march=native)
elseif (CMAKE_Fortran_COMPILER MATCHES ".*nag.*")
message(STATUS "Using nag compiler")
set(PPFLAGS -f2018 -fpp)
set(MPFLAGS -openmp)
set(WARNFLAGS -Wall)
set(DEVFLAGS -g -mtrace -C=all -colour -O0)
set(DEBUGFLAGS -C=array)
set(MEMFLAGS -mcmodel=large)
set(OPTIMFLAGS -O3)
set(FASTFLAGS -Ofast)
elseif (CMAKE_Fortran_COMPILER MATCHES ".*ifort.*" OR CMAKE_Fortran_COMPILER MATCHES ".*ifx.*")
message(STATUS "Using intel compiler")
set(PPFLAGS -fpp)
set(MPFLAGS -qopenmp)
set(WARNFLAGS -warn all)
set(DEVFLAGS -check all -warn)
set(DEBUGFLAGS -check all -fpe0 -warn -tracekback -debug extended)
set(MEMFLAGS -mcmodel=large)
set(OPTIMFLAGS -O3)
set(FASTFLAGS -Ofast)
else()
# Code for other Fortran compilers
message(STATUS "Using a different Fortran compiler")
endif()
set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} ${PPFLAGS}")
# Add custom build types for different configurations
# add_compile_options("$<$<CONFIG:dev>:${DEVFLAGS}>")
# add_compile_options("$<$<CONFIG:debug>:${DEBUGFLAGS}>")
# add_compile_options("$<$<CONFIG:warn>:${WARNFLAGS}>")
# add_compile_options("$<$<CONFIG:optim>:${OPTIMFLAGS}>")
# add_compile_options("$<$<CONFIG:fast>:${FASTFLAGS}>")
# add_compile_options("$<$<CONFIG:mp>:${MPFLAGS}>")
# create the library
add_library(${PROJECT_NAME} STATIC ${ALL_SOURCES})
set(MODULE_DIR ${CMAKE_BUILD_PREFIX}/mod)
set_target_properties(${PROJECT_NAME} PROPERTIES Fortran_MODULE_DIRECTORY ${MODULE_DIR})
target_link_libraries(${PROJECT_NAME} PUBLIC)
# optionally enable BLAS
if(USE_BLAS)
find_package(BLAS REQUIRED)
target_compile_definitions(${PROJECT_NAME} PUBLIC USE_BLAS)
target_link_libraries(${PROJECT_NAME} PUBLIC ${BLAS_LIBRARIES})
message(STATUS "BLAS enabled: ${BLAS_LIBRARIES}")
endif()
# replace ".f90" with ".mod"
string(REGEX REPLACE "\\.[^.]*$" ".mod" MODULE_FILES "${SRC_FILES}")
# remove directory path from module files
foreach(i ${MODULE_FILES})
get_filename_component(fname ${i} NAME)
list(APPEND MODULE_FILES_CLEAN ${fname})
endforeach()
set(MODULE_FILES ${MODULE_FILES_CLEAN})
# installation
install(FILES ${MODULE_DIR}/${MODULE_FILES} DESTINATION include)
install(TARGETS ${PROJECT_NAME} DESTINATION lib)
# set compile options based on different build configurations
target_compile_options(${PROJECT_NAME} PUBLIC "$<$<CONFIG:Release>:${OPTIMFLAGS}>")
target_compile_options(${PROJECT_NAME} PUBLIC "$<$<CONFIG:Parallel>:${OPTIMFLAGS}>")
target_compile_options(${PROJECT_NAME} PUBLIC "$<$<CONFIG:Parallel>:${MPFLAGS}>")
target_compile_options(${PROJECT_NAME} PUBLIC "$<$<CONFIG:Dev>:${DEVFLAGS}>")
target_compile_options(${PROJECT_NAME} PUBLIC "$<$<CONFIG:Debug>:${DEBUGFLAGS}>")
target_compile_options(${PROJECT_NAME} PUBLIC "$<$<CONFIG:Parallel_Dev>:${MPFLAGS}>")
target_compile_options(${PROJECT_NAME} PUBLIC "$<$<CONFIG:Parallel_Dev>:${DEVFLAGS}>")
# add library dependencies
add_dependencies(${PROJECT_NAME} coreutils)
target_link_libraries(${PROJECT_NAME} PUBLIC coreutils)
# include the build test directory only if this is the main project
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME AND BUILD_TESTS)
add_subdirectory(test)
endif()