Skip to content

Commit

Permalink
编译为动态库
Browse files Browse the repository at this point in the history
  • Loading branch information
kongdd committed Oct 21, 2024
1 parent c984ada commit 56cae6a
Show file tree
Hide file tree
Showing 30 changed files with 148 additions and 54 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ docs/_build/*
*.py[cod]
build
bld

*.a
*.so
*.o
3 changes: 2 additions & 1 deletion cmake/SetBuildOptions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ endif()
# Set compiler specific flags.
if(COMPILER STREQUAL "GNU")
add_compile_definitions(CPRGNU)
add_compile_options(-fallow-argument-mismatch -shared -fPIC)
set(CMAKE_C_FLAGS "-std=gnu99 -fopenmp")
set(CMAKE_C_FLAGS_DEBUG "-fcheck=bounds")
set(CMAKE_C_FLAGS_RELEASE "-O")
Expand Down Expand Up @@ -62,4 +63,4 @@ message(STATUS " ******* ${CMAKE_PROJECT_NAME} build options ******* ")
message(STATUS " Build type = '${CMAKE_BUILD_TYPE}'")
message(STATUS " Compiler = '${COMPILER}'")
message(STATUS " Coupling mode = '${COUPLING_MODE}'")
message(STATUS " ********************************** ")
message(STATUS " ********************************** ")
4 changes: 4 additions & 0 deletions julia/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
gcc -c hello.c -o hello.o
ar rcs libhello.a hello.o

gcc -shared -o libhello.so -Wl,--whole-archive libhello.a -Wl,--no-whole-archive
4 changes: 4 additions & 0 deletions julia/hello.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Save this as example.c
int add(int a, int b) {
return a + b;
}
15 changes: 15 additions & 0 deletions julia/hello.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Define the function signature and call it
lib = "./libhello.so"

function add(a::Int32, b::Int32)::Int32
return ccall(
(:add, lib), # Function and static library path
Int32, # Return type
(Int32, Int32), # Argument types
a, b # Arguments
)
end

# Call the function
result = add(Int32(3), Int32(5))
println(result)
27 changes: 27 additions & 0 deletions julia_call.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# using NCDatasets

lib = "bld/clm5/libclm.so"
# lib = "bld/clm5/libclm.a"
# real(r8) function daylength(lat, decl)
const FT = Cdouble

daylength(lat::FT, decl::FT) =
@ccall lib.__daylengthmod_MOD_daylength(lat::Ref{FT}, decl::Ref{FT})::FT

lat = 20.0 |> deg2rad
decl = 20.0 |> deg2rad

@show daylength(lat, decl)

## Fix curl error
# > /home/kong/packages/julias/julia-1.10/bin/../lib/julia/libcurl.so.4: version `CURL_OPENSSL_4' not found (required by /lib/x86_64-linux-gnu/libnetcdf.so.19)

# /home/kong/packages/julias/julia-1.10/bin/../lib/julia/libcurl.so.4 -> libcurl.so.4.8.0
# ll /home/kong/packages/julias/julia-1.10/lib/julia/libcurl.so.4

# ln -sf /home/kong/packages/julias/julia-1.10/lib/julia/libcurl.so.4.8.0 /home/kong/packages/julias/julia-1.10/lib/julia/libcurl.so.4
# ln -sf /usr/lib/x86_64-linux-gnu/libcurl.so.4.7.0 /home/kong/packages/julias/julia-1.10/lib/julia/libcurl.so.4


## Julia
# ln -sf libcurl.so.4.8.0 /home/kong/packages/julias/julia-1.10/lib/julia/libcurl.so.4
35 changes: 35 additions & 0 deletions run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
## 之前版本
# export NETCDF=${HOME}/environment/netcdf
# export PNDIR=${HOME}/environment/pnetcdf
# export LD_LIBRARY_PATH="$NETCDF/lib:$PNDIR/lib"

## github actions版本
export NETCDF="/opt/netcdf_v4.9.2_openmpi"
export PNDIR="$NETCDF"
export LD_LIBRARY_PATH="$NETCDF/lib"

# export PATH=$NETCDF/bin:$PATH
# export NETCDF=$NETCDF
export NETCDF_C_PATH=$NETCDF
export NETCDF_FORTRAN_PATH=$NETCDF
export PNETCDF_PATH=$PNDIR


export CFLAGS="-fPIC -shared"
export FFLAGS="-fPIC -shared"

# User-specific variables
BUILD_DIR="bld"
INSTALL_DIR="eclm"

mkdir bld

# Run cmake
cmake -S src -B "$BUILD_DIR" \
-DCMAKE_INSTALL_PREFIX="$INSTALL_DIR" \
-DCMAKE_C_COMPILER=mpicc \
-DCMAKE_Fortran_FLAGS="-fallow-argument-mismatch -fallow-invalid-boz -fPIC" \
-DCMAKE_Fortran_COMPILER=mpifort

cd bld
make -j8
5 changes: 3 additions & 2 deletions src/clm5/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
project (clm LANGUAGES Fortran)

add_library(${PROJECT_NAME} STATIC)
# add_library(${PROJECT_NAME} STATIC)
add_library(${PROJECT_NAME} SHARED)

target_compile_definitions(${PROJECT_NAME} PRIVATE NDEBUG)
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_BINARY_DIR})
Expand Down Expand Up @@ -324,4 +325,4 @@ if(LAPACK_FOUND)
target_link_libraries(${PROJECT_NAME} PRIVATE LAPACK::LAPACK)
endif()
target_link_libraries(${PROJECT_NAME} PRIVATE csm_share)
install (TARGETS ${PROJECT_NAME} ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
install (TARGETS ${PROJECT_NAME} ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
2 changes: 1 addition & 1 deletion src/csm_share/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
project (csm_share LANGUAGES C Fortran)

add_library(${PROJECT_NAME} STATIC)
add_library(${PROJECT_NAME} SHARED)

# TODO: NUM_COMP_INST_<COMP> should be adjusted for ensemble runs. See the ff. for more info:
# https://esmci.github.io/cime/versions/cesm2.2/html/users_guide/multi-instance.html
Expand Down
4 changes: 2 additions & 2 deletions src/datm/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
project (datm LANGUAGES Fortran)

add_library(${PROJECT_NAME} STATIC)
add_library(${PROJECT_NAME} SHARED)

target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_BINARY_DIR})
target_link_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_BINARY_DIR})
Expand All @@ -12,4 +12,4 @@ target_sources(${PROJECT_NAME}
atm_comp_mct.F90
)
target_link_libraries(${PROJECT_NAME} PRIVATE csm_share)
install (TARGETS ${PROJECT_NAME} ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
install (TARGETS ${PROJECT_NAME} ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
4 changes: 2 additions & 2 deletions src/eclm/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
project (eclm LANGUAGES Fortran)

if(USE_PDAF)
add_library(${PROJECT_NAME} STATIC)
add_library(${PROJECT_NAME} SHARED)
else()
add_executable(${PROJECT_NAME})
set_target_properties(${PROJECT_NAME} PROPERTIES SUFFIX ".exe")
Expand Down Expand Up @@ -39,4 +39,4 @@ target_sources(${PROJECT_NAME}
)

target_link_libraries(${PROJECT_NAME} PRIVATE clm datm mosart ocn wav ice glc esp csm_share)
install (TARGETS ${PROJECT_NAME} ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
install (TARGETS ${PROJECT_NAME} ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
12 changes: 6 additions & 6 deletions src/externals/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ ExternalProject_Add(gptl_external
SHAREDPATH=${GPTL_BLD_DIR}
INSTALL_COMMAND ""
BUILD_ALWAYS YES
BUILD_BYPRODUCTS ${GPTL_BLD_DIR}/lib/libgptl.a
BUILD_BYPRODUCTS ${GPTL_BLD_DIR}/lib/libgptl.so
)

add_library(gptl INTERFACE IMPORTED GLOBAL)
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)
target_link_libraries(gptl INTERFACE libgptl.so)
add_dependencies(gptl gptl_external)
install (FILES ${GPTL_BLD_DIR}/lib/libgptl.a TYPE LIB)
install (FILES ${GPTL_BLD_DIR}/lib/libgptl.so TYPE LIB)


# ===========
Expand All @@ -58,13 +58,13 @@ if (BUILD_MCT)
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
BUILD_BYPRODUCTS ${GPTL_BLD_DIR}/lib/libmct.so ${GPTL_BLD_DIR}/lib/libmpeu.a
)
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)
target_link_libraries(mct INTERFACE libmct.so 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.so ${MCT_BLD_DIR}/lib/libmpeu.a TYPE LIB)
else()
find_package(MCT REQUIRED)
if (${MCT_FOUND})
Expand Down
2 changes: 1 addition & 1 deletion src/externals/gptl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ SET(SRCS_C GPTLget_memusage.c
SET(SRCS_F90 perf_mod.F90
perf_utils.F90)

ADD_LIBRARY(timing ${SRCS_F90} ${SRCS_C})
ADD_LIBRARY(timing SHARED ${SRCS_F90} ${SRCS_C})
9 changes: 6 additions & 3 deletions src/externals/gptl/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ OBJS = gptl.o GPTLutil.o GPTLget_memusage.o GPTLprint_memusage.o \
AR ?= ar
ARFLAGS ?= ruv

libgptl.a: $(OBJS)
CFLAGS=-fPIC -shared
FFLAGS=-fPIC -shared

libgptl.so: $(OBJS)
$(AR) $(ARFLAGS) $@ $(OBJS)


Expand All @@ -69,10 +72,10 @@ clean:
$(RM) -f *.f *.f90 *.d *.$(MOD_SUFFIX) $(OBJS)


install: libgptl.a
install: libgptl.so
cp -p $(GPTL_DIR)/gptl.h $(SHAREDPATH)/include
cp -p *.$(MOD_SUFFIX) $(SHAREDPATH)/include
cp -p libgptl.a $(SHAREDPATH)/lib
cp -p libgptl.so $(SHAREDPATH)/lib


perf_mod.o: perf_utils.o
Expand Down
2 changes: 1 addition & 1 deletion src/externals/mct/benchmarks/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ clean:

# DEPENDENCIES:

$(OBJS_ALL): $(MCTPATH)/libmct.a
$(OBJS_ALL): $(MCTPATH)/libmct.so



Expand Down
6 changes: 3 additions & 3 deletions src/externals/mct/configure
Original file line number Diff line number Diff line change
Expand Up @@ -5482,7 +5482,7 @@ elif echo $ac_fc_version_output | grep -i sx >/dev/null 2>&1; then
fi

# Test to see if fortran compiler supports the flag
# -fallow-argument-mismatch flag introduced in gfortran 10.
# -fallow-argument-mismatch -shared -fPIC flag introduced in gfortran 10.
#
# Also allow support for NAG compiler using the -mismatch_all flag.
#
Expand All @@ -5496,14 +5496,14 @@ Program test
USE ISO_C_BINDING, ONLY: C_PTRDIFF_T
End Program
EOF
doit='$FC -c ${FCFLAGS} ${FCFLAGS_f90} -fallow-argument-mismatch conftest.f90'
doit='$FC -c ${FCFLAGS} ${FCFLAGS_f90} -fallow-argument-mismatch -shared -fPIC conftest.f90'
if { { eval echo "\"\$as_me\":${as_lineno-$LINENO}: \"$doit\""; } >&5
(eval $doit) 2>&5
ac_status=$?
printf "%s\n" "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5
test $ac_status = 0; }; then
nf_allow_mismatch=yes
FCFLAGS="${FCFLAGS} -fallow-argument-mismatch"
FCFLAGS="${FCFLAGS} -fallow-argument-mismatch -shared -fPIC"
else
nf_allow_mismatch=no
fi
Expand Down
6 changes: 3 additions & 3 deletions src/externals/mct/configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ elif echo $ac_fc_version_output | grep -i sx >/dev/null 2>&1; then
fi

# Test to see if fortran compiler supports the flag
# -fallow-argument-mismatch flag introduced in gfortran 10.
# -fallow-argument-mismatch -shared -fPIC flag introduced in gfortran 10.
#
# Also allow support for NAG compiler using the -mismatch_all flag.
#
Expand All @@ -472,10 +472,10 @@ Program test
USE ISO_C_BINDING, ONLY: C_PTRDIFF_T
End Program
EOF
doit='$FC -c ${FCFLAGS} ${FCFLAGS_f90} -fallow-argument-mismatch conftest.f90'
doit='$FC -c ${FCFLAGS} ${FCFLAGS_f90} -fallow-argument-mismatch -shared -fPIC conftest.f90'
if AC_TRY_EVAL(doit); then
nf_allow_mismatch=yes
FCFLAGS="${FCFLAGS} -fallow-argument-mismatch"
FCFLAGS="${FCFLAGS} -fallow-argument-mismatch -shared -fPIC"
else
nf_allow_mismatch=no
fi
Expand Down
2 changes: 1 addition & 1 deletion src/externals/mct/examples/climate_concur1/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ clean:

# DEPENDENCIES:

$(OBJS_ALL): $(MCTPATH)/libmct.a
$(OBJS_ALL): $(MCTPATH)/libmct.so



Expand Down
2 changes: 1 addition & 1 deletion src/externals/mct/examples/climate_sequen1/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ clean:

# DEPENDENCIES:

$(OBJS_ALL): $(MCTPATH)/libmct.a
$(OBJS_ALL): $(MCTPATH)/libmct.so



Expand Down
2 changes: 1 addition & 1 deletion src/externals/mct/examples/simple/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,4 @@ clean:

# DEPENDENCIES:

$(OBJS_ALL): $(MCTPATH)/libmct.a
$(OBJS_ALL): $(MCTPATH)/libmct.so
8 changes: 4 additions & 4 deletions src/externals/mct/mct/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ include ../Makefile.conf

# TARGETS

all: lib$(MODULE).a
all: lib$(MODULE).so

lib$(MODULE).a: $(OBJS_ALL)
lib$(MODULE).so: $(OBJS_ALL)
$(RM) $@
$(AR) $@ $(OBJS_ALL)
$(RANLIB) $@
Expand All @@ -63,11 +63,11 @@ MCTFLAGS = $(INCFLAG)$(MPEUPATH)


clean:
${RM} *.o *.mod lib$(MODULE).a
${RM} *.o *.mod lib$(MODULE).so

install: all
$(MKINSTALLDIRS) $(libdir) $(includedir)
$(INSTALL) lib$(MODULE).a -m 644 $(libdir)
$(INSTALL) lib$(MODULE).so -m 644 $(libdir)
@for modfile in *.mod; do \
echo $(INSTALL) $$modfile -m 644 $(includedir); \
$(INSTALL) $$modfile -m 644 $(includedir); \
Expand Down
8 changes: 4 additions & 4 deletions src/externals/mct/mpeu/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ OBJS_ALL = $(SRCS_C:.c=.o) \

# TARGETS

all: lib$(MODULE).a
all: lib$(MODULE).so

lib$(MODULE).a: $(OBJS_ALL)
lib$(MODULE).so: $(OBJS_ALL)
$(RM) $@
$(AR) $@ $(OBJS_ALL)
$(RANLIB) $@
Expand All @@ -71,11 +71,11 @@ MPEUFLAGS =
$(FC) -c $(INCPATH) $(FPPDEFS) $(FCFLAGS) $(MPEUFLAGS) $<

clean:
${RM} *.o *.mod lib$(MODULE).a
${RM} *.o *.mod lib$(MODULE).so

install: all
$(MKINSTALLDIRS) $(libdir) $(includedir)
$(INSTALL) lib$(MODULE).a -m 644 $(libdir)
$(INSTALL) lib$(MODULE).so -m 644 $(libdir)
@for modfile in *.mod; do \
echo $(INSTALL) $$modfile -m 644 $(includedir); \
$(INSTALL) $$modfile -m 644 $(includedir); \
Expand Down
Loading

0 comments on commit 56cae6a

Please sign in to comment.