CMakeLists.txt 15.7 KB
Newer Older
1 2 3 4 5 6 7
cmake_minimum_required(VERSION 2.8)

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")

# Create main project.
project(getm Fortran)

8 9 10 11 12 13
# Set version information.
set(getm_VERSION_MAJOR 2)
set(getm_VERSION_MINOR 5)
set(getm_VERSION_PATCH 0)
set(getm_VERSION "${getm_VERSION_MAJOR}.${getm_VERSION_MINOR}.${getm_VERSION_PATCH}")

14 15 16 17
if(GETM_FLAGS)
   add_definitions(${GETM_FLAGS})
endif(GETM_FLAGS)

18 19 20 21 22 23 24 25 26 27 28
set(GIT_COMMIT_ID ${getm_VERSION})
configure_file("${PROJECT_SOURCE_DIR}/futils/getm_version.F90.in" getm_version.F90)
option(GETM_EMBED_VERSION "Embed GETM version information" OFF)
if(GETM_EMBED_VERSION)
  add_custom_target(getm_version
    ${CMAKE_COMMAND} -DINFILE=${PROJECT_SOURCE_DIR}/futils/getm_version.F90.in -DOUTFILE=${CMAKE_BINARY_DIR}/getm_version.F90 -P "${PROJECT_SOURCE_DIR}/cmake/Modules/GetGitInfo.cmake"
    WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
    COMMENT "Retrieving description of last GETM commit..."
    SOURCES ${PROJECT_SOURCE_DIR}/futils/getm_version.F90.in "${PROJECT_SOURCE_DIR}/cmake/Modules/GetGitInfo.cmake"
    VERBATIM
  )
29 30 31
endif()

# Generate include file with version information.
32 33 34
#configure_file(../include/version.h.in version.h)
#configure_file(../include/fortran_version.h.in fortran_version.h)
#configure_file(../include/git_revision.h.in git_revision.h)
35

36 37 38 39 40
# Use solution folders in IDEs
set_property(GLOBAL PROPERTY USE_FOLDERS ON)

# various compilation switches

41 42
option(GETM_USE_STATIC "Static compilation (domain size is set at compile time)" OFF)
if(GETM_USE_STATIC)
43
   add_definitions(-DSTATIC)
44
endif(GETM_USE_STATIC)
45

46 47
# used for naming executable
set(par_str "")
48 49
option(GETM_USE_PARALLEL "Enable parallel computation" OFF)
if(GETM_USE_PARALLEL)
50
   find_package(MPI REQUIRED)
51 52 53
   if(MPI_Fortran_INCLUDE_PATH)
     include_directories("${MPI_Fortran_INCLUDE_PATH}")
   endif()
54
   add_definitions(-DGETM_PARALLEL)
55
   set(HALO_MPI futils/halo_mpi.F90 ${MPI_Fortran_EXTRA_SOURCES})
56
   set(par_str "_parallel")
57
endif(GETM_USE_PARALLEL)
58

59 60
option(GETM_USE_DEBUG "Enable debug output" OFF)
if(GETM_USE_DEBUG)
61
   add_definitions(-DDEBUG)
62
endif(GETM_USE_DEBUG)
63

64 65
option(GETM_USE_PROFILING "Profiling compilation" OFF)
if(GETM_USE_PROFILING)
66
   add_definitions(-DPROF)
67
endif(GETM_USE_PROFILING)
68

69 70
option(GETM_USE_STATIC "Static compilation (domain size specified at compile time)" OFF)
if(GETM_USE_STATIC)
71
   add_definitions(-DSTATIC)
72
endif(GETM_USE_STATIC)
73

74 75
option(GETM_USE_FABM "Include support for Framework for Aquatic Biogeochemical Models (fabm.net)" OFF)
if(GETM_USE_FABM)
76 77
   add_definitions(-D_FABM_)
   set (GETM_FABM 3d/getm_fabm.F90)
78 79
   set(FABM_EMBED_VERSION ${GETM_EMBED_VERSION} CACHE BOOL "Embed FABM version information" FORCE)

80
endif(GETM_USE_FABM)
81

82
option(GETM_USE_FLEXIBLE_OUTPUT "Use new output manager" ON)
Knut's avatar
Knut committed
83 84 85 86
if(GETM_USE_FLEXIBLE_OUTPUT)
  add_definitions(-D_FLEXIBLE_OUTPUT_)
endif()

87 88
option(GETM_USE_STRUCTURE_FRICTION "Include structure friction" OFF)
if(GETM_USE_STRUCTURE_FRICTION)
89 90
   add_definitions(-DSTRUCTURE_FRICTION)
   set (STRUCTURE_FRICTION 3d/structure_friction_3d.F90)
91
endif(GETM_USE_STRUCTURE_FRICTION)
92 93 94 95 96 97 98 99 100 101 102

add_definitions(-DFORTRAN95)

# Specify default build type for single-type systems (not VS)
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
  set (CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose the type of build." FORCE)
  set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel" "RelWithDebInfo")
endif()

# Customize compiler flags
if(${CMAKE_Fortran_COMPILER_ID} STREQUAL "GNU")
103
  set (CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -ffree-line-length-none")
104 105 106
  add_definitions(-DGFORTRAN)
elseif(${CMAKE_Fortran_COMPILER_ID} STREQUAL "Intel")
  add_definitions(-DIFORT)
107 108 109 110
  if(WIN32)
    set(CMAKE_Fortran_FLAGS_DEBUG "${CMAKE_Fortran_FLAGS_DEBUG} /Od")
    set(CMAKE_Fortran_STACK_SIZE 128000000)
  endif()
111
elseif(${CMAKE_Fortran_COMPILER_ID} STREQUAL "Cray")
112
  set (CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -N 1023")
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
endif()

# Use use position-independent code (-fPIC) everywhere if building shared libraries
if(BUILD_SHARED_LIBS)
  set(CMAKE_POSITION_INDEPENDENT_CODE ON)
endif()

# Set default installation prefix.
if(WIN32)
  if(DEFINED ENV{LOCALAPPDATA})
    set(DEFAULT_PREFIX "$ENV{LOCALAPPDATA}/getm")
  else()
    set(DEFAULT_PREFIX "$ENV{APPDATA}/getm")
  endif()
else()
  set(DEFAULT_PREFIX "$ENV{HOME}/local/getm")
endif()
if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
  # Still on CMake default - override with our own default.
  set(CMAKE_INSTALL_PREFIX ${DEFAULT_PREFIX} CACHE PATH "Directory to install GETM in" FORCE)
else()
  # Just set the doc string for the variable.
  set(CMAKE_INSTALL_PREFIX ${DEFAULT_PREFIX} CACHE PATH "Directory to install GETM in")
endif()

138 139 140 141 142 143
# Find NetCDF and - on Windows - compile against static runtime if NetCDF was compiled as such.
find_package(NetCDF REQUIRED)
if (NetCDF_STATIC_MSVC_BUILD)
  add_compile_options("/libs:static")
endif()

144
# Global include directories
145 146
include_directories("${CMAKE_BINARY_DIR}"
                    "${PROJECT_SOURCE_DIR}/../include"
147 148 149 150 151 152 153 154 155 156 157 158 159 160
                   )
# Libraries added in alphabetic order - dependencies are dealt with below 
add_library(2d OBJECT
            2d/adv_arakawa_j7_2dh.F90
            2d/advection.F90
            2d/adv_fct_2dh.F90
            2d/adv_split_u.F90
            2d/adv_split_v.F90
            2d/adv_upstream_2dh.F90
            2d/bottom_friction.F90
            2d/cfl_check.F90
            2d/depth_update.F90
            2d/m2d.F90
            2d/momentum.F90
Knut's avatar
Knut committed
161
            2d/physical_dissipation.F90
162 163 164 165 166 167 168
            2d/residual.F90
            2d/sealevel.F90
            2d/update_2d_bdy.F90
            2d/uv_advect.F90
            2d/uv_diff_2dh.F90
            2d/uv_diffusion.F90
            2d/variables_2d.F90
169
            2d/velocity_update.F90
170
           )
171
set_property(TARGET 2d APPEND PROPERTY INCLUDE_DIRECTORIES "${PROJECT_SOURCE_DIR}/2d")
172 173 174 175 176 177 178

add_library(3d OBJECT
            3d/adaptive_coordinates.F90
            3d/advection_3d.F90
            3d/adv_split_w.F90
            3d/bdy_3d.F90
            3d/bottom_friction_3d.F90
Knut's avatar
Knut committed
179
            3d/buoyancy_frequency.F90
180 181 182 183 184
            3d/check_h.F90
            3d/coordinates.F90
            3d/eqstate.F90
            3d/general_coordinates.F90
#            3d/getm_bio.F90
185
            ${GETM_FABM}
186 187 188 189 190 191 192 193 194 195 196 197 198
            3d/gotm.F90
            3d/hcc_check.F90
            3d/hybrid_coordinates.F90
            3d/internal_pressure.F90
            3d/ip_blumberg_mellor.F90
            3d/ip_blumberg_mellor_lin.F90
            3d/ip_chu_fan.F90
            3d/ip_shchepetkin_mcwilliams.F90
            3d/ip_song_wright.F90
            3d/ip_stelling_vankester.F90
            3d/ip_z_interpol.F90
            3d/m3d.F90
            3d/numerical_mixing.F90
Knut's avatar
Knut committed
199
            3d/physical_dissipation_3d.F90
200 201 202 203
            3d/physical_mixing.F90
            3d/preadapt_coordinates.F90
            3d/rivers.F90
            3d/salinity.F90
Knut's avatar
Knut committed
204
            3d/shear_frequency.F90
205 206 207
            3d/sigma_coordinates.F90
            3d/slow_bottom_friction.F90
            3d/slow_terms.F90
Karsten Bolding's avatar
Karsten Bolding committed
208
#            3d/spm.F90
209 210 211
            3d/start_macro.F90
            3d/stop_macro.F90
            3d/stresses_3d.F90
212
            ${STRUCTURE_FRICTION}
213 214 215 216 217 218
            3d/temperature.F90
            3d/tke_eps_advect_3d.F90
            3d/uu_momentum_3d.F90
            3d/uv_advect_3d.F90
            3d/uv_diffusion_3d.F90
            3d/variables_3d.F90
219
            3d/velocity_update_3d.F90
220
            3d/vv_momentum_3d.F90
Karsten Bolding's avatar
Karsten Bolding committed
221
            3d/ww_momentum_3d.F90
222
           )
223
set_property(TARGET 3d APPEND PROPERTY INCLUDE_DIRECTORIES "${PROJECT_SOURCE_DIR}/3d")
224 225 226 227 228 229 230 231 232 233 234

add_library(domain OBJECT
            domain/bdy_spec.F90
            domain/domain.F90
            domain/have_bdy.F90
            domain/mirror_bdy_2d.F90
            domain/mirror_bdy_3d.F90
            domain/part_domain.F90
            domain/print_bdy.F90
            domain/uv_depths.F90
           )
235
set_property(TARGET domain APPEND PROPERTY INCLUDE_DIRECTORIES "${PROJECT_SOURCE_DIR}/domain")
236 237

add_library(futils OBJECT
238
#            git_revision.h
239 240 241 242 243 244 245 246 247 248
            futils/c2x.F90
            futils/check_3d_fields.F90
            futils/cnv_2d.F90
            futils/cnv_3d.F90
            futils/col_interpol.F90
            futils/eta_mask.F90
            futils/exceptions.F90
            futils/getm_error.F90
            futils/getm_timers.F90
            futils/grid_interpol.F90
249
            ${HALO_MPI}
250 251 252 253 254 255
            futils/halo_zones.F90
            futils/kbk_interpol.F90
            futils/parallel.F90
            futils/parameters.F90
            futils/pos.F90
            futils/read_par_setup.F90
256
            futils/strip_string.F90
257 258 259 260 261 262 263
            futils/time.F90
            futils/to_2d_u.F90
            futils/to_2d_vel.F90
            futils/to_2d_v.F90
            futils/to_3d_uu.F90
            futils/to_3d_vel.F90
            futils/to_3d_vv.F90
Knut's avatar
Knut committed
264 265 266 267 268 269
            futils/to_fluxu.F90
            futils/to_fluxv.F90
            futils/to_fluxw.F90
            futils/to_u.F90
            futils/to_v.F90
            futils/to_w.F90
270 271
            futils/tridiagonal.F90
            futils/ver_interpol.F90
272
            ${CMAKE_BINARY_DIR}/getm_version.F90
273
           )
274 275 276
if(GETM_EMBED_VERSION)
  add_dependencies(futils getm_version)
endif()
277 278 279

add_library(getm OBJECT
            getm/cleanup.F90
280
            getm/compilation_options.F90
281 282
            getm/initialise.F90
            getm/integration.F90
283
            getm/register_all_variables.F90
284
            getm/print_version.F90
285 286 287 288 289 290 291 292 293
           )

add_library(input OBJECT
            input/get_2d_bdy.F90
            input/get_2d_field.F90
            input/get_3d_bdy.F90
            input/get_3d_field.F90
            input/get_meteo_data.F90
            input/get_river_data.F90
294
            input/inquire_file.F90
295 296 297 298 299 300 301 302 303 304 305 306 307
            input/init_2d_bdy.F90
            input/init_3d_bdy.F90
            input/init_meteo_input.F90
            input/init_river_input.F90
            input/input.F90
            input/read_profile.F90
            input/read_topo_file.F90
           )

add_library(meteo OBJECT
            meteo/exchange_coefficients.F90
            meteo/fluxes.F90
            meteo/meteo.F90
308
            meteo/solar_zenith_angle.F90
309
            meteo/short_wave_radiation.F90
310
            meteo/albedo_water.F90
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
           )

add_library(ncdf OBJECT
            ncdf/create_restart_ncdf.F90
            ncdf/grid_ncdf.F90
            ncdf/init_2d_ncdf.F90
            ncdf/init_3d_ncdf.F90
            ncdf/init_grid_ncdf.F90
            ncdf/init_mean_ncdf.F90
            ncdf/ncdf_2d_bdy.F90
            ncdf/ncdf_2d.F90
            ncdf/ncdf_3d_bdy.F90
            ncdf/ncdf_3d_bio_bdy.F90
            ncdf/ncdf_3d.F90
            ncdf/ncdf_close.F90
            ncdf/ncdf_common.F90
            ncdf/ncdf_get_field.F90
328
            ncdf/get_2d_field_ncdf_by_id.F90
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
            ncdf/ncdf_in.F90
            ncdf/ncdf_mean.F90
            ncdf/ncdf_meteo.F90
            ncdf/ncdf_restart.F90
            ncdf/ncdf_rivers.F90
            ncdf/ncdf_topo.F90
            ncdf/open_restart_ncdf.F90
            ncdf/read_restart_ncdf.F90
            ncdf/save_2d_ncdf.F90
            ncdf/save_3d_ncdf.F90
            ncdf/save_grid_ncdf.F90
            ncdf/save_mean_ncdf.F90
            ncdf/set_attributes.F90
            ncdf/write_restart_ncdf.F90
           )
344
set_property(TARGET ncdf APPEND PROPERTY INCLUDE_DIRECTORIES "${PROJECT_SOURCE_DIR}/ncdf")
345 346 347 348 349 350 351 352 353 354 355

add_library(output OBJECT
            output/ascii_out.F90
            output/calc_mean_fields.F90
            output/diagnose.F90
            output/diagnostic_variables.F90
            output/ncdf_out.F90
            output/nesting.F90
            output/output.F90
           )

356 357 358 359 360
#option(DEFINE_ICE "Include ICE support" OFF)
#if(DEFINE_ICE)
#  add_library(ice OBJECT
#             )
#endif(DEFINE_ICE)
361 362

add_dependencies(domain futils)
Karsten Bolding's avatar
Karsten Bolding committed
363
add_dependencies(meteo futils domain)
364
add_dependencies(2d domain)
Karsten Bolding's avatar
Karsten Bolding committed
365 366 367 368
add_dependencies(3d meteo domain 2d)
add_dependencies(output futils domain 2d 3d meteo)
add_dependencies(ncdf futils domain output)
add_dependencies(input futils 2d 3d ncdf)
369 370 371 372 373 374
add_dependencies(getm input)

set_property(TARGET futils ncdf input APPEND PROPERTY INCLUDE_DIRECTORIES "${NetCDF_INCLUDE_DIRS}")
add_definitions(-DNETCDF_FMT -DREAL_4B=real\(4\))

# Use GOTM_PREFIX to locate include and lib directories
375
set(GOTM_EMBED_VERSION ${GETM_EMBED_VERSION} CACHE BOOL "Embed GOTM version information" FORCE)
376 377 378
find_path(GOTM_BASE src/gotm/gotm.F90 DOC "Path to GOTM source directory.")
if(GOTM_BASE)
  set(GOTM_BUILD_LIBRARIES_ONLY ON)
379
  set(GOTM_USE_FABM ${GETM_USE_FABM} CACHE BOOL "Include support for Framework for Aquatic Biogeochemical Models (fabm.net)" FORCE)
380
  set(GOTM_USE_FLEXIBLE_OUTPUT ${GETM_USE_FLEXIBLE_OUTPUT} CACHE BOOL "Use new output manager" FORCE)
381 382 383
  add_subdirectory(${GOTM_BASE}/src gotm)
  set(GOTM_INCLUDE_DIRS ${CMAKE_BINARY_DIR}/gotm/modules)
  set(GOTM_LIBRARIES turbulence util)
Knut's avatar
Knut committed
384 385 386
  if(GETM_USE_FLEXIBLE_OUTPUT)
    set(GOTM_LIBRARIES ${GOTM_LIBRARIES} output_manager)
  endif()
387 388
  if(GETM_USE_FABM)
    set(GOTM_LIBRARIES ${GOTM_LIBRARIES} gotm_fabm)
389
    set_property(TARGET 3d getm APPEND PROPERTY INCLUDE_DIRECTORIES $<TARGET_PROPERTY:gotm_fabm,INTERFACE_INCLUDE_DIRECTORIES>)
390
  endif()
391
  mark_as_advanced(GOTM_PREFIX GOTM_USE_FABM)
392
#KB  add_dependencies(output ${GOTM_LIBRARIES})
393
  add_dependencies(3d ${GOTM_LIBRARIES})
394 395
else()
  find_package(GOTM REQUIRED)
396 397 398 399

  # Use FABM_PREFIX to locate include and lib directories
  if(GETM_USE_FABM)
    find_package(FABM REQUIRED)
400
    set_property(TARGET 3d getm APPEND PROPERTY INCLUDE_DIRECTORIES "${FABM_INCLUDE_DIRS}")
401
  endif()
402
endif()
403
set_property(TARGET 2d APPEND PROPERTY INCLUDE_DIRECTORIES "${GOTM_INCLUDE_DIRS}")
404
set_property(TARGET 3d getm APPEND PROPERTY INCLUDE_DIRECTORIES "${GOTM_INCLUDE_DIRS}")
405 406 407 408
set_property(TARGET output APPEND PROPERTY INCLUDE_DIRECTORIES "${GOTM_INCLUDE_DIRS}")
set_property(TARGET ncdf APPEND PROPERTY INCLUDE_DIRECTORIES "${GOTM_INCLUDE_DIRS}")
set_property(TARGET input APPEND PROPERTY INCLUDE_DIRECTORIES "${GOTM_INCLUDE_DIRS}")
set_property(TARGET getm APPEND PROPERTY INCLUDE_DIRECTORIES "${GOTM_INCLUDE_DIRS}")
409 410 411 412 413 414 415 416 417

# Build GETM executable.
add_executable(getm_exe
               getm/main.F90
               $<TARGET_OBJECTS:getm>
               $<TARGET_OBJECTS:output>
               $<TARGET_OBJECTS:meteo>
               $<TARGET_OBJECTS:3d>
               $<TARGET_OBJECTS:2d>
Karsten Bolding's avatar
Karsten Bolding committed
418
               $<TARGET_OBJECTS:input>
419 420 421 422
               $<TARGET_OBJECTS:domain>
               $<TARGET_OBJECTS:ncdf>
               $<TARGET_OBJECTS:futils>
              )
423
set_property(TARGET getm_exe PROPERTY OUTPUT_NAME "getm")
424
set_property(TARGET getm_exe APPEND PROPERTY INCLUDE_DIRECTORIES "${GOTM_INCLUDE_DIRS}")
Karsten Bolding's avatar
Karsten Bolding committed
425

426
target_link_libraries(getm_exe ${GOTM_LIBRARIES} ${NetCDF_LIBRARIES})
427
if (NetCDF_STATIC_MSVC_BUILD)
428
  set_property(TARGET getm_exe PROPERTY LINK_FLAGS_DEBUG "/NODEFAULTLIB:\"libcmt\"")
429
endif()
430
if(GETM_USE_PARALLEL AND MPI_Fortran_LIBRARIES)
431
  target_link_libraries(getm_exe ${MPI_Fortran_LIBRARIES})
432
endif()
433

434
if(GETM_USE_FABM AND NOT GOTM_BASE)
Karsten Bolding's avatar
Karsten Bolding committed
435
  target_link_libraries(getm_exe ${FABM_LIBRARIES})
436
endif()
437

438
# Install GETM executable
439
install(TARGETS getm_exe DESTINATION bin)
440 441 442

# Begin testing section

443 444 445 446
# add_custom_target(test_all COMMENT "building tests - nothing yet :-)")
add_executable(test_speed_adv_upstream_2dh EXCLUDE_FROM_ALL
               2d/test_speed_adv_upstream_2dh.F90
              )
447 448 449 450 451 452

add_executable(test_rotation EXCLUDE_FROM_ALL
               futils/test_rotation.F90
               $<TARGET_OBJECTS:futils>
              )
target_link_libraries(test_rotation ${NetCDF_LIBRARIES})
453 454 455
if (NetCDF_STATIC_MSVC_BUILD)
  set_property(TARGET test_rotation PROPERTY LINK_FLAGS_DEBUG "/NODEFAULTLIB:\"libcmt\"")
endif()
456 457 458 459
if(GETM_USE_PARALLEL AND MPI_Fortran_LIBRARIES)
  target_link_libraries(test_rotation ${MPI_Fortran_LIBRARIES})
endif()

460
add_custom_target(test_all WORKING_DIRECTORY tests COMMENT "building tests")
461
add_dependencies( test_all test_speed_adv_upstream_2dh test_rotation )
462 463

# End of testing section