Skip to content

Commit 90bb865

Browse files
Modify the cmake to make it install package automatically.
1 parent d71d3e2 commit 90bb865

File tree

3 files changed

+332
-88
lines changed

3 files changed

+332
-88
lines changed

CMakeLists.txt

Lines changed: 159 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ else()
5151
endif()
5252

5353
# Set compiler flags
54-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 ${MPI_CXX_COMPILE_FLAGS} -Wall -Wno-unused-function")
54+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 ${MPI_CXX_COMPILE_FLAGS} -Wall -Wno-unused-function -Wno-unused-variable")
5555
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${MPI_C_COMPILE_FLAGS}")
5656
set(CMAKE_CXX_FLAGS_DEBUG "-g")
5757
set(CMAKE_CXX_FLAGS_RELEASE "-O3")
@@ -72,12 +72,146 @@ set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
7272

7373
add_definitions(-DRTE_USE_CBOOL)
7474

75+
# HDF5
76+
find_library(HDF5_LIB NAMES hdf5 hdf5_serial HINTS ${DEP_INSTALL_DIR}/lib NO_DEFAULT_PATH)
77+
find_library(HDF5_HL_LIB NAMES hdf5_hl HINTS ${DEP_INSTALL_DIR}/lib NO_DEFAULT_PATH)
78+
if(NOT HDF5_LIB OR NOT HDF5_HL_LIB)
79+
message(STATUS "HDF5 or HDF5 high-level library not found in ${DEP_INSTALL_DIR}, installing...")
80+
ExternalProject_Add(
81+
hdf5_ext
82+
URL https://support.hdfgroup.org/ftp/HDF5/releases/hdf5-1.10/hdf5-1.10.5/src/hdf5-1.10.5.tar.gz
83+
PREFIX ${CMAKE_SOURCE_DIR}/_deps/hdf5
84+
CONFIGURE_COMMAND ${CMAKE_COMMAND} -E env
85+
CC=${CMAKE_C_COMPILER}
86+
CXX=${CMAKE_CXX_COMPILER}
87+
<SOURCE_DIR>/configure
88+
--prefix=${DEP_INSTALL_DIR}
89+
--enable-cxx
90+
--enable-hl
91+
--disable-shared
92+
--enable-static
93+
--with-zlib=/usr
94+
BUILD_COMMAND make -j${N_CORES}
95+
INSTALL_COMMAND make install
96+
UPDATE_COMMAND ""
97+
BUILD_IN_SOURCE 1
98+
BUILD_ALWAYS FALSE
99+
)
100+
set(HDF5_NEEDS_INSTALL TRUE)
101+
set(HDF5_LIB ${DEP_INSTALL_DIR}/lib/libhdf5.a)
102+
set(HDF5_HL_LIB ${DEP_INSTALL_DIR}/lib/libhdf5_hl.a)
103+
else()
104+
message(STATUS "HDF5 found: ${HDF5_LIB}")
105+
message(STATUS "HDF5 high-level library found: ${HDF5_HL_LIB}")
106+
endif()
107+
108+
# NetCDF-C (depends on HDF5)
109+
find_library(NETCDF_LIB NAMES netcdf HINTS ${DEP_INSTALL_DIR}/lib NO_DEFAULT_PATH)
110+
if(NOT NETCDF_LIB)
111+
message(STATUS "NetCDF-C not found in ${DEP_INSTALL_DIR}, installing...")
112+
ExternalProject_Add(
113+
netcdf-c_ext
114+
URL https://github.com/Unidata/netcdf-c/archive/v4.3.3.1.tar.gz
115+
PREFIX ${CMAKE_SOURCE_DIR}/_deps/netcdf-c
116+
CONFIGURE_COMMAND <SOURCE_DIR>/configure
117+
--prefix=${DEP_INSTALL_DIR}
118+
CC=${CMAKE_C_COMPILER}
119+
CXX=${CMAKE_CXX_COMPILER}
120+
FC=${CMAKE_Fortran_COMPILER}
121+
--disable-dap
122+
--disable-byterange
123+
--with-hdf5=${DEP_INSTALL_DIR}
124+
BUILD_COMMAND make -j${N_CORES}
125+
INSTALL_COMMAND make install
126+
DEPENDS hdf5_ext
127+
UPDATE_COMMAND ""
128+
BUILD_ALWAYS FALSE
129+
)
130+
set(NETCDF_C_NEEDS_INSTALL TRUE)
131+
set(NETCDF_LIB ${DEP_INSTALL_DIR}/lib/libnetcdf.a)
132+
else()
133+
message(STATUS "NetCDF-C found in ${DEP_INSTALL_DIR}/lib")
134+
endif()
135+
136+
# NetCDF-C++ (depends on NetCDF-C and HDF5)
137+
find_library(NETCDF_CXX_LIB NAMES netcdf_c++4 HINTS ${DEP_INSTALL_DIR}/lib NO_DEFAULT_PATH)
138+
if(NOT NETCDF_CXX_LIB)
139+
message(STATUS "NetCDF-C++ not found in ${DEP_INSTALL_DIR}, installing...")
140+
ExternalProject_Add(
141+
netcdf-cxx_ext
142+
URL https://github.com/Unidata/netcdf-cxx4/archive/v4.3.0.tar.gz
143+
PREFIX ${CMAKE_SOURCE_DIR}/_deps/netcdf-cxx
144+
CONFIGURE_COMMAND <SOURCE_DIR>/configure
145+
--prefix=${DEP_INSTALL_DIR}
146+
CC=${CMAKE_C_COMPILER}
147+
CXX=${CMAKE_CXX_COMPILER}
148+
CPPFLAGS=-I${DEP_INSTALL_DIR}/include
149+
LDFLAGS=-L${DEP_INSTALL_DIR}/lib
150+
BUILD_COMMAND make -j${N_CORES}
151+
INSTALL_COMMAND make install
152+
DEPENDS netcdf-c_ext hdf5_ext
153+
UPDATE_COMMAND ""
154+
BUILD_ALWAYS FALSE
155+
)
156+
set(NETCDF_CXX_NEEDS_INSTALL TRUE)
157+
set(NETCDF_CXX_LIB ${DEP_INSTALL_DIR}/lib/libnetcdf_c++4.a)
158+
else()
159+
message(STATUS "NetCDF-C++ found in ${DEP_INSTALL_DIR}/lib")
160+
endif()
161+
162+
# Boost
163+
find_library(BOOST_SYSTEM_LIB NAMES boost_system HINTS ${DEP_INSTALL_DIR}/lib NO_DEFAULT_PATH)
164+
find_library(BOOST_FILESYSTEM_LIB NAMES boost_filesystem HINTS ${DEP_INSTALL_DIR}/lib NO_DEFAULT_PATH)
165+
if(NOT BOOST_SYSTEM_LIB OR NOT BOOST_FILESYSTEM_LIB)
166+
message(STATUS "Boost not found in ${DEP_INSTALL_DIR}, installing...")
167+
ExternalProject_Add(
168+
boost_ext
169+
URL https://archives.boost.io/release/1.87.0/source/boost_1_87_0.tar.bz2
170+
PREFIX ${CMAKE_SOURCE_DIR}/_deps/boost
171+
CONFIGURE_COMMAND <SOURCE_DIR>/bootstrap.sh
172+
--prefix=${DEP_INSTALL_DIR}
173+
--with-libraries=system,filesystem
174+
BUILD_COMMAND <SOURCE_DIR>/b2
175+
-j${N_CORES}
176+
install
177+
--prefix=${DEP_INSTALL_DIR}
178+
link=static
179+
threading=multi
180+
INSTALL_COMMAND ""
181+
UPDATE_COMMAND ""
182+
BUILD_IN_SOURCE 1
183+
BUILD_ALWAYS FALSE
184+
)
185+
set(BOOST_NEEDS_INSTALL TRUE)
186+
set(Boost_INCLUDE_DIRS ${DEP_INSTALL_DIR}/include)
187+
set(Boost_LIBRARIES ${DEP_INSTALL_DIR}/lib/libboost_system.a ${DEP_INSTALL_DIR}/lib/libboost_filesystem.a)
188+
else()
189+
message(STATUS "Boost found in ${DEP_INSTALL_DIR}/lib")
190+
set(Boost_INCLUDE_DIRS ${DEP_INSTALL_DIR}/include)
191+
set(Boost_LIBRARIES ${BOOST_SYSTEM_LIB} ${BOOST_FILESYSTEM_LIB})
192+
endif()
193+
194+
# Find system libraries
195+
find_library(DL_LIB NAMES dl)
196+
if(NOT DL_LIB)
197+
message(FATAL_ERROR "libdl not found. Please install the dynamic linking library.")
198+
else()
199+
message(STATUS "libdl found: ${DL_LIB}")
200+
endif()
201+
202+
find_library(Z_LIB NAMES z)
203+
if(NOT Z_LIB)
204+
message(FATAL_ERROR "zlib not found. Please install zlib.")
205+
else()
206+
message(STATUS "zlib found: ${Z_LIB}")
207+
endif()
208+
75209
# Include directories
76210
include_directories(
77211
include
78-
/home/Aaron/local/include
79212
${DEP_INSTALL_DIR}/include
80213
${MPI_INCLUDE_PATH}
214+
${Boost_INCLUDE_DIRS}
81215
${CMAKE_SOURCE_DIR}/external/rte-rrtmgp-cpp/include
82216
${CMAKE_SOURCE_DIR}/external/rte-rrtmgp-cpp/include_test
83217
)
@@ -87,7 +221,7 @@ add_subdirectory(external/rte-rrtmgp-cpp)
87221

88222
# Force rte-rrtmgp to use only _deps/install headers
89223
set_target_properties(rte_rrtmgp PROPERTIES
90-
INTERFACE_INCLUDE_DIRECTORIES "/home/Aaron/local/include;${CMAKE_SOURCE_DIR}/external/rte-rrtmgp-cpp/include"
224+
INTERFACE_INCLUDE_DIRECTORIES "${DEP_INSTALL_DIR}/include;${CMAKE_SOURCE_DIR}/external/rte-rrtmgp-cpp/include"
91225
)
92226

93227
# Create the P3 Fortran shared library
@@ -116,41 +250,48 @@ if(OpenMP_CXX_FOUND)
116250
endif()
117251

118252
# Link libraries
119-
find_library(libncPath netcdf "/home/Aaron/local/lib")
120-
find_library(libncxxPath netcdf_c++4 "/home/Aaron/local/lib")
121-
122253
target_link_libraries(vvm2d PRIVATE
123254
rte_rrtmgp
124-
${libncPath}
125-
${libncxxPath}
255+
${NETCDF_LIB}
256+
${NETCDF_CXX_LIB}
257+
${HDF5_HL_LIB}
258+
${HDF5_LIB}
259+
${Boost_LIBRARIES}
260+
${DL_LIB}
261+
${Z_LIB}
126262
stdc++
127263
)
128264

129265
# Link PETSc if enabled
130266
option(USE_PETSC "Enable PETSc support" OFF)
131267
if(USE_PETSC)
132268
message(STATUS "PETSc support enabled")
133-
find_library(libpetscPath petsc "${DEP_INSTALL_DIR}/lib" NO_DEFAULT_PATH)
134-
if(NOT libpetscPath)
269+
find_library(PETSC_LIB NAMES petsc HINTS ${DEP_INSTALL_DIR}/lib NO_DEFAULT_PATH)
270+
if(NOT PETSC_LIB)
135271
message(STATUS "PETSc not found in ${DEP_INSTALL_DIR}, installing...")
136272
ExternalProject_Add(
137273
petsc_ext
138274
GIT_REPOSITORY https://gitlab.com/petsc/petsc.git
139275
GIT_TAG v3.20.5
140276
GIT_SHALLOW TRUE
141277
PREFIX ${CMAKE_SOURCE_DIR}/_deps/petsc
142-
CONFIGURE_COMMAND <SOURCE_DIR>/configure --prefix=${DEP_INSTALL_DIR} --with-cc=${CMAKE_C_COMPILER} --with-cxx=${CMAKE_CXX_COMPILER} --with-fc=${CMAKE_Fortran_COMPILER} --with-mpi=1
278+
CONFIGURE_COMMAND <SOURCE_DIR>/configure
279+
--prefix=${DEP_INSTALL_DIR}
280+
--with-cc=${CMAKE_C_COMPILER}
281+
--with-cxx=${CMAKE_CXX_COMPILER}
282+
--with-fc=${CMAKE_Fortran_COMPILER}
283+
--with-mpi=1
143284
BUILD_COMMAND make -j${N_CORES}
144285
INSTALL_COMMAND make install
145286
UPDATE_COMMAND ""
146287
BUILD_ALWAYS FALSE
147288
)
148289
set(PETSC_NEEDS_INSTALL TRUE)
149-
set(libpetscPath ${DEP_INSTALL_DIR}/lib/libpetsc.so)
290+
set(PETSC_LIB ${DEP_INSTALL_DIR}/lib/libpetsc.so)
150291
else()
151292
message(STATUS "PETSc found in ${DEP_INSTALL_DIR}/lib")
152293
endif()
153-
target_link_libraries(vvm2d PRIVATE ${libpetscPath})
294+
target_link_libraries(vvm2d PRIVATE ${PETSC_LIB})
154295
endif()
155296

156297
# Add dependencies if needed
@@ -160,8 +301,11 @@ endif()
160301
if(NETCDF_C_NEEDS_INSTALL)
161302
add_dependencies(vvm2d netcdf-c_ext)
162303
endif()
163-
if(NETCDF_CXX4_NEEDS_INSTALL)
164-
add_dependencies(vvm2d netcdf-cxx4_ext)
304+
if(NETCDF_CXX_NEEDS_INSTALL)
305+
add_dependencies(vvm2d netcdf-cxx_ext)
306+
endif()
307+
if(BOOST_NEEDS_INSTALL)
308+
add_dependencies(vvm2d boost_ext)
165309
endif()
166310
if(USE_PETSC AND PETSC_NEEDS_INSTALL)
167311
add_dependencies(vvm2d petsc_ext)

0 commit comments

Comments
 (0)