Skip to content

Commit 9d3f65b

Browse files
committed
Add BUILD_BOTH_LIBRARIES option to build static and shared libs
Add a new CMake option BUILD_BOTH_LIBRARIES that allows building both static and shared libraries in a single build directory. This provides an alternative to the existing approach of building in separate directories with BUILD_SHARED_LIBS=ON/OFF, while maintaining backward compatibility with existing build scripts. Usage: cmake .. -DBUILD_BOTH_LIBRARIES=ON Produces: - libcpp_dotenv_static.a - libcpp_dotenv_shared.so (or .dylib on macOS)
1 parent 8a813f1 commit 9d3f65b

File tree

1 file changed

+45
-13
lines changed

1 file changed

+45
-13
lines changed

CMakeLists.txt

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,36 +12,68 @@ else()
1212
endif()
1313
message(STATUS "Building CPP-DOTENV in ${CMAKE_BUILD_TYPE} mode")
1414

15+
# Option to build both static and shared libraries
16+
option(BUILD_BOTH_LIBRARIES "Build both static and shared libraries" OFF)
17+
1518
#------------------- SUBDIRECTORY ADDITION ------------------------------------
1619

1720
add_subdirectory(common)
1821
add_subdirectory(src)
1922

2023
#----------------------- LIBRARY CONFIGURATION --------------------------------
2124

22-
set(CPP_DOTENV cpp_dotenv CACHE INTERNAL "")
2325
set(CPP_DOTENV_SRC
2426
src/dotenv.cpp
2527
include/dotenv.h
2628
)
2729

28-
add_library(${CPP_DOTENV} ${CPP_DOTENV_SRC})
30+
# Common include directories
31+
set(CPP_DOTENV_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/include)
2932

30-
target_link_libraries(${CPP_DOTENV}
31-
${ENVIRON_LIB}
32-
${PARSER_LIB}
33-
)
33+
# Common compile options
34+
if ("${CMAKE_BUILD_TYPE}" STREQUAL "DEBUG")
35+
set(CPP_DOTENV_COMPILE_OPTIONS -g -Wall -O0)
36+
else()
37+
set(CPP_DOTENV_COMPILE_OPTIONS -O3)
38+
endif()
3439

35-
target_include_directories(${CPP_DOTENV} PUBLIC
36-
${CMAKE_CURRENT_SOURCE_DIR}/include
37-
)
40+
# Build both static and shared libraries if requested
41+
if(BUILD_BOTH_LIBRARIES)
42+
# Static library
43+
add_library(cpp_dotenv_static STATIC ${CPP_DOTENV_SRC})
44+
target_link_libraries(cpp_dotenv_static
45+
${ENVIRON_LIB}
46+
${PARSER_LIB}
47+
)
48+
target_include_directories(cpp_dotenv_static PUBLIC ${CPP_DOTENV_INCLUDE_DIRS})
49+
target_compile_options(cpp_dotenv_static PRIVATE ${CPP_DOTENV_COMPILE_OPTIONS})
3850

39-
if ("${CMAKE_BUILD_TYPE}" STREQUAL "DEBUG")
40-
target_compile_options(${CPP_DOTENV} PRIVATE
41-
-g -Wall -O0
51+
# Shared library
52+
add_library(cpp_dotenv_shared SHARED ${CPP_DOTENV_SRC})
53+
target_link_libraries(cpp_dotenv_shared
54+
${ENVIRON_LIB}
55+
${PARSER_LIB}
4256
)
57+
target_include_directories(cpp_dotenv_shared PUBLIC ${CPP_DOTENV_INCLUDE_DIRS})
58+
target_compile_options(cpp_dotenv_shared PRIVATE ${CPP_DOTENV_COMPILE_OPTIONS})
59+
60+
# Alias for compatibility - default to static
61+
add_library(cpp_dotenv ALIAS cpp_dotenv_static)
4362
else()
63+
# Single library (type determined by BUILD_SHARED_LIBS)
64+
set(CPP_DOTENV cpp_dotenv CACHE INTERNAL "")
65+
add_library(${CPP_DOTENV} ${CPP_DOTENV_SRC})
66+
67+
target_link_libraries(${CPP_DOTENV}
68+
${ENVIRON_LIB}
69+
${PARSER_LIB}
70+
)
71+
72+
target_include_directories(${CPP_DOTENV} PUBLIC
73+
${CPP_DOTENV_INCLUDE_DIRS}
74+
)
75+
4476
target_compile_options(${CPP_DOTENV} PRIVATE
45-
-O3
77+
${CPP_DOTENV_COMPILE_OPTIONS}
4678
)
4779
endif()

0 commit comments

Comments
 (0)