diff --git a/build_tools/build.juwels-centos8 b/build_tools/build.juwels-centos8 index 52d9eec985..207fa83679 100755 --- a/build_tools/build.juwels-centos8 +++ b/build_tools/build.juwels-centos8 @@ -5,6 +5,11 @@ ARCH="juwels-centos8" CLM5_TSMP_ROOT=$(git rev-parse --show-toplevel) BUILD_FOLDER="$CLM5_TSMP_ROOT/outputs/$ARCH/build" INSTALL_PATH="$CLM5_TSMP_ROOT/outputs/$ARCH/run" +if [ "$1" = "--debug" ] || [ "$1" = "-d" ]; then + BUILD_TYPE="DEBUG" +else + BUILD_TYPE="RELEASE" +fi # --------------------------------------------- module purge @@ -29,7 +34,10 @@ GENF90_PATH=$(pwd) rm -rf $BUILD_FOLDER cmake -S "$CLM5_TSMP_ROOT/src" \ -B "$BUILD_FOLDER" \ + -DCMAKE_INSTALL_PREFIX="$INSTALL_PATH" \ -DCMAKE_MODULE_PATH="$(pwd)/cmake" \ + -DCMAKE_BUILD_TYPE=$BUILD_TYPE \ + -DCOMPILER="Intel" \ -DCMAKE_C_COMPILER=mpicc \ -DCMAKE_Fortran_COMPILER=mpifort \ -DGENF90_PATH=$GENF90_PATH \ diff --git a/build_tools/build.ubuntu-20.04LTS b/build_tools/build.ubuntu-20.04LTS index 27d6cb1cb7..1ec8fd0bd1 100755 --- a/build_tools/build.ubuntu-20.04LTS +++ b/build_tools/build.ubuntu-20.04LTS @@ -5,6 +5,11 @@ ARCH="ubuntu-20.04LTS" CLM5_TSMP_ROOT=$(git rev-parse --show-toplevel) BUILD_FOLDER="$CLM5_TSMP_ROOT/outputs/$ARCH/build" INSTALL_PATH="$CLM5_TSMP_ROOT/outputs/$ARCH/run" +if [ "$1" = "--debug" ] || [ "$1" = "-d" ]; then + BUILD_TYPE="DEBUG" +else + BUILD_TYPE="RELEASE" +fi # --------------------------------------------- NetCDF_ROOT=/opt/custom @@ -14,10 +19,13 @@ cmake -S "$CLM5_TSMP_ROOT/src" \ -B "$BUILD_FOLDER" \ -DCMAKE_INSTALL_PREFIX="$INSTALL_PATH" \ -DCMAKE_MODULE_PATH="$(pwd)/cmake" \ + -DCMAKE_BUILD_TYPE=$BUILD_TYPE \ + -DCOMPILER="GNU" \ -DCMAKE_C_COMPILER=mpicc \ -DCMAKE_Fortran_COMPILER=mpifort \ -DGENF90_PATH=$GENF90_PATH \ -DNetCDF_PATH=$NetCDF_ROOT \ - -DPnetCDF_PATH=$NetCDF_ROOT + -DPnetCDF_PATH=$NetCDF_ROOT \ + cmake --build "$BUILD_FOLDER" --clean-first cmake --install "$BUILD_FOLDER" diff --git a/build_tools/cmake/SetBuildOptions.cmake b/build_tools/cmake/SetBuildOptions.cmake new file mode 100644 index 0000000000..0cd47fe450 --- /dev/null +++ b/build_tools/cmake/SetBuildOptions.cmake @@ -0,0 +1,42 @@ +# This module sets the required compile flags and definitions for all targets. + +if(UNIX AND NOT APPLE) + add_compile_definitions(LINUX) +endif() + +# Set default build = RELEASE if none was specified. +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") +endif() + +# Set default compiler = GNU if none was specified. +if(NOT COMPILER) + set(COMPILER "GNU" CACHE STRING "Choose compiler toolchain." FORCE) + set_property(CACHE COMPILER PROPERTY STRINGS "GNU" "Intel") +endif() + +if(COMPILER STREQUAL "GNU") + add_compile_definitions(CPRGNU) + set(CMAKE_C_FLAGS "-std=gnu99") + set(CMAKE_C_FLAGS_DEBUG "-fcheck=bounds") + set(CMAKE_C_FLAGS_RELEASE "-O") + set(CMAKE_Fortran_FLAGS "-fconvert=big-endian -ffree-line-length-none -ffixed-line-length-none -ffree-form") + set(CMAKE_Fortran_FLAGS_DEBUG "-g -Wall -Og -fbacktrace -ffpe-trap=zero,overflow -fcheck=bounds") + set(CMAKE_Fortran_FLAGS_RELEASE "-O") +elseif(COMPILER STREQUAL "Intel") + add_compile_definitions(CPRINTEL) + set(CMAKE_C_FLAGS "-qno-opt-dynamic-align -std=gnu99 -fp-model precise") + set(CMAKE_C_FLAGS_DEBUG "-O0 -g") + set(CMAKE_C_FLAGS_RELEASE "-O2 -debug minimal") + set(CMAKE_Fortran_FLAGS "-free -qno-opt-dynamic-align -ftz -traceback -convert big_endian -assume byterecl -assume realloc_lhs -fp-model source") + set(CMAKE_Fortran_FLAGS_DEBUG "-O0 -g -check uninit -check bounds -check pointers -fpe0 -check noarg_temp_created") + set(CMAKE_Fortran_FLAGS_RELEASE "-O2 -debug minimal") +else() + message(FATAL_ERROR "COMPILER='${COMPILER}' is not supported.") +endif() + +message(STATUS " ******* ${CMAKE_PROJECT_NAME} build options ******* ") +message(STATUS " Build type = '${CMAKE_BUILD_TYPE}'") +message(STATUS " Compiler = '${COMPILER}'") +message(STATUS " *************************************** ") \ No newline at end of file diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 88e74eafc0..5af8708240 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,32 +1,8 @@ cmake_minimum_required (VERSION 3.16.3) -project (clm5-tsmp LANGUAGES C Fortran) +project (CLM5-TSMP LANGUAGES C Fortran) -# Set global compile features and definitions -if(UNIX AND NOT APPLE) - add_compile_definitions(LINUX) -endif() - -if(CMAKE_C_COMPILER_ID STREQUAL "GNU") - set(cdefs CPRGNU) - set(cflags -O -std=gnu99) -elseif(CMAKE_C_COMPILER_ID STREQUAL "Intel") - set(cdefs CPRINTEL) - set(cflags -qno-opt-dynamic-align -std=gnu99 -O2 "SHELL:-fp-model precise -debug minimal") -endif() - -if(CMAKE_Fortran_COMPILER_ID STREQUAL "GNU") - set(fdefs CPRGNU) - set(fflags -ffree-form -ffree-line-length-none -fconvert=big-endian -O) -elseif(CMAKE_Fortran_COMPILER_ID STREQUAL "Intel") - set(fdefs CPRINTEL) - set(fflags -free -qno-opt-dynamic-align -ftz -traceback -O2 "SHELL:-convert big_endian -assume byterecl -assume realloc_lhs -fp-model source -debug minimal") -endif() - -add_compile_options("$<$:${fflags}>") -add_compile_options("$<$:${cflags}>") -add_compile_definitions("$<$:${fdefs}>") -add_compile_definitions($<$:${cdefs}>) +include(SetBuildOptions) add_subdirectory(externals) add_subdirectory(csm_share) diff --git a/src/externals/CMakeLists.txt b/src/externals/CMakeLists.txt index ce1ef9bac3..a521e3a431 100644 --- a/src/externals/CMakeLists.txt +++ b/src/externals/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required (VERSION 3.16.3) -project(externals) +project(externals LANGUAGES C Fortran) include(ExternalProject) # =========== @@ -7,15 +7,21 @@ include(ExternalProject) # =========== set(GPTL_BLD_DIR ${CMAKE_CURRENT_BINARY_DIR}/gptl) file(MAKE_DIRECTORY ${GPTL_BLD_DIR}/include ${GPTL_BLD_DIR}/lib) + +set(GPTL_MACROS_FILE ${GPTL_BLD_DIR}/Macros.make) +file(WRITE ${GPTL_MACROS_FILE} " MPICC := ${CMAKE_C_COMPILER}\n") +file(APPEND ${GPTL_MACROS_FILE} " MPIFC := ${CMAKE_Fortran_COMPILER}\n") +file(APPEND ${GPTL_MACROS_FILE} " CLAGS := ${CMAKE_C_FLAGS}\n") +file(APPEND ${GPTL_MACROS_FILE} " FFLAGS := ${CMAKE_Fortran_FLAGS}\n") +file(APPEND ${GPTL_MACROS_FILE} "CPPDEFS := -DFORTRANUNDERSCORE\n") + ExternalProject_Add(gptl_external PREFIX gptl SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/gptl BUILD_IN_SOURCE FALSE - CONFIGURE_COMMAND echo "CPPDEFS := $(CPPDEFS) -DFORTRANUNDERSCORE" > ${GPTL_BLD_DIR}/Macros.make + CONFIGURE_COMMAND "" BUILD_COMMAND make install -f ${CMAKE_CURRENT_SOURCE_DIR}/gptl/Makefile MACFILE=${GPTL_BLD_DIR}/Macros.make - MPICC=${CMAKE_C_COMPILER} - MPIFC=${CMAKE_Fortran_COMPILER} GPTL_DIR=${CMAKE_CURRENT_SOURCE_DIR}/gptl SHAREDPATH=${GPTL_BLD_DIR} INSTALL_COMMAND "" @@ -28,8 +34,7 @@ target_include_directories(gptl INTERFACE ${GPTL_BLD_DIR}/include) target_link_directories(gptl INTERFACE ${GPTL_BLD_DIR}/lib) target_link_libraries(gptl INTERFACE libgptl.a) add_dependencies(gptl gptl_external) -install (FILES ${GPTL_BLD_DIR}/lib/libgptl.a - TYPE LIB) +install (FILES ${GPTL_BLD_DIR}/lib/libgptl.a TYPE LIB) # =========== # MCT @@ -44,7 +49,9 @@ ExternalProject_Add(mct_external --prefix=${MCT_BLD_DIR} --exec-prefix=${MCT_BLD_DIR} CC=${CMAKE_C_COMPILER} - FC=${CMAKE_Fortran_COMPILER} + FC=${CMAKE_Fortran_COMPILER} + CFLAGS=${CMAKE_C_FLAGS} + FCFLAGS=${CMAKE_Fortran_FLAGS} BUILD_ALWAYS YES BUILD_BYPRODUCTS ${GPTL_BLD_DIR}/lib/libmct.a ${GPTL_BLD_DIR}/lib/libmpeu.a ) @@ -53,10 +60,9 @@ target_include_directories(mct INTERFACE ${MCT_BLD_DIR}/include) target_link_directories(mct INTERFACE ${MCT_BLD_DIR}/lib) target_link_libraries(mct INTERFACE libmct.a libmpeu.a) add_dependencies(mct mct_external) -install (FILES ${MCT_BLD_DIR}/lib/libmct.a ${MCT_BLD_DIR}/lib/libmpeu.a - TYPE LIB) +install (FILES ${MCT_BLD_DIR}/lib/libmct.a ${MCT_BLD_DIR}/lib/libmpeu.a TYPE LIB) # =========== # PIO # =========== -add_subdirectory(pio1) +add_subdirectory(pio1) \ No newline at end of file