Skip to content

Commit

Permalink
Use an object library to build shared and static libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
mdpiper committed Dec 10, 2024
1 parent 99a8678 commit 0f4c323
Showing 1 changed file with 23 additions and 23 deletions.
46 changes: 23 additions & 23 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,43 +18,43 @@ configure_file(
@ONLY
)

# Create static and shared libraries.
set(static_lib ${CMAKE_PROJECT_NAME}_static)
# Create shared and static libraries through an object library.
set(shared_lib ${CMAKE_PROJECT_NAME}_shared)
add_library(${static_lib} bmi.f90)
add_library(${shared_lib} SHARED bmi.f90)

# Change the output names of the libraries. On Windows, they can't
# have the same name, so change the shared library name.
set_target_properties(${static_lib} PROPERTIES
OUTPUT_NAME ${CMAKE_PROJECT_NAME})
if(WIN32)
set_target_properties(${shared_lib} PROPERTIES
OUTPUT_NAME ${CMAKE_PROJECT_NAME}_win)
else()
set_target_properties(${shared_lib} PROPERTIES
OUTPUT_NAME ${CMAKE_PROJECT_NAME})
endif()
set(static_lib ${CMAKE_PROJECT_NAME}_static)
add_library(obj_lib OBJECT bmi.f90)
add_library(${shared_lib} SHARED $<TARGET_OBJECTS:obj_lib>)
add_library(${static_lib} STATIC $<TARGET_OBJECTS:obj_lib>)

set_target_properties(${static_lib} PROPERTIES
VERSION ${CMAKE_PROJECT_VERSION}
)
set_target_properties(${shared_lib} PROPERTIES
set_target_properties(${shared_lib}
PROPERTIES
OUTPUT_NAME ${CMAKE_PROJECT_NAME}
VERSION ${CMAKE_PROJECT_VERSION}
PUBLIC_HEADER ${CMAKE_BINARY_DIR}/${mod_name}.mod
)

install(
TARGETS ${static_lib}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
set_target_properties(${static_lib}
PROPERTIES
OUTPUT_NAME ${CMAKE_PROJECT_NAME}
VERSION ${CMAKE_PROJECT_VERSION}
)
if(WIN32)
set_target_properties(${static_lib}
PROPERTIES
OUTPUT_NAME ${CMAKE_PROJECT_NAME}_static
)
endif()

install(
TARGETS ${shared_lib}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)
install(
TARGETS ${static_lib}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
)
install(
FILES ${CMAKE_BINARY_DIR}/${CMAKE_PROJECT_NAME}.pc
DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig
Expand Down

0 comments on commit 0f4c323

Please sign in to comment.