Skip to content

Commit 4d1fbbf

Browse files
authored
Merge pull request #2 from ProxySQL/main_260121_tap
Add BUILD_BOTH_LIBRARIES option for TAP tests
2 parents 8a813f1 + 9cf61b1 commit 4d1fbbf

File tree

2 files changed

+107
-15
lines changed

2 files changed

+107
-15
lines changed

CMakeLists.txt

Lines changed: 69 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,36 +12,92 @@ 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+
18+
# Validate that BUILD_BOTH_LIBRARIES and BUILD_SHARED_LIBS are not used together
19+
if(BUILD_BOTH_LIBRARIES AND BUILD_SHARED_LIBS)
20+
message(WARNING "BUILD_BOTH_LIBRARIES and BUILD_SHARED_LIBS should not be used together. BUILD_SHARED_LIBS will be ignored.")
21+
set(BUILD_SHARED_LIBS OFF CACHE BOOL "Build shared libraries" FORCE)
22+
endif()
23+
24+
# When building shared libraries that link against static libraries,
25+
# we need position-independent code for the static libraries
26+
if(BUILD_SHARED_LIBS OR BUILD_BOTH_LIBRARIES)
27+
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
28+
endif()
29+
1530
#------------------- SUBDIRECTORY ADDITION ------------------------------------
1631

1732
add_subdirectory(common)
1833
add_subdirectory(src)
1934

2035
#----------------------- LIBRARY CONFIGURATION --------------------------------
2136

22-
set(CPP_DOTENV cpp_dotenv CACHE INTERNAL "")
2337
set(CPP_DOTENV_SRC
2438
src/dotenv.cpp
2539
include/dotenv.h
2640
)
2741

28-
add_library(${CPP_DOTENV} ${CPP_DOTENV_SRC})
42+
# Common include directories
43+
set(CPP_DOTENV_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/include)
2944

30-
target_link_libraries(${CPP_DOTENV}
31-
${ENVIRON_LIB}
32-
${PARSER_LIB}
33-
)
45+
# Common compile options
46+
if ("${CMAKE_BUILD_TYPE}" STREQUAL "DEBUG")
47+
set(CPP_DOTENV_COMPILE_OPTIONS -g -Wall -O0)
48+
else()
49+
set(CPP_DOTENV_COMPILE_OPTIONS -O3)
50+
endif()
3451

35-
target_include_directories(${CPP_DOTENV} PUBLIC
36-
${CMAKE_CURRENT_SOURCE_DIR}/include
37-
)
52+
# Build both static and shared libraries if requested
53+
if(BUILD_BOTH_LIBRARIES)
54+
# Static library
55+
add_library(cpp_dotenv_static STATIC ${CPP_DOTENV_SRC})
56+
target_link_libraries(cpp_dotenv_static
57+
${ENVIRON_LIB}
58+
${PARSER_LIB}
59+
)
60+
target_include_directories(cpp_dotenv_static PUBLIC ${CPP_DOTENV_INCLUDE_DIRS})
61+
target_compile_options(cpp_dotenv_static PRIVATE ${CPP_DOTENV_COMPILE_OPTIONS})
62+
# Set output name to libcpp_dotenv.a (instead of libcpp_dotenv_static.a)
63+
set_target_properties(cpp_dotenv_static PROPERTIES OUTPUT_NAME cpp_dotenv)
3864

39-
if ("${CMAKE_BUILD_TYPE}" STREQUAL "DEBUG")
40-
target_compile_options(${CPP_DOTENV} PRIVATE
41-
-g -Wall -O0
65+
# Shared library
66+
add_library(cpp_dotenv_shared SHARED ${CPP_DOTENV_SRC})
67+
target_link_libraries(cpp_dotenv_shared
68+
${ENVIRON_LIB}
69+
${PARSER_LIB}
4270
)
71+
target_include_directories(cpp_dotenv_shared PUBLIC ${CPP_DOTENV_INCLUDE_DIRS})
72+
target_compile_options(cpp_dotenv_shared PRIVATE ${CPP_DOTENV_COMPILE_OPTIONS})
73+
# Set output name to libcpp_dotenv.so/.dylib (instead of libcpp_dotenv_shared.so)
74+
set_target_properties(cpp_dotenv_shared PROPERTIES OUTPUT_NAME cpp_dotenv)
75+
# On Windows (MSVC), the import library would conflict with the static library.
76+
# Add a suffix to the import library name to avoid this conflict.
77+
if(MSVC)
78+
set_target_properties(cpp_dotenv_shared PROPERTIES IMPORT_SUFFIX "_shared.lib")
79+
endif()
80+
81+
# Alias for backward compatibility:
82+
# When BUILD_BOTH_LIBRARIES is ON, the cpp_dotenv target is an alias to
83+
# cpp_dotenv_static. Projects that need the shared library must explicitly
84+
# link against cpp_dotenv_shared instead of cpp_dotenv.
85+
add_library(cpp_dotenv ALIAS cpp_dotenv_static)
4386
else()
87+
# Single library (type determined by BUILD_SHARED_LIBS)
88+
set(CPP_DOTENV cpp_dotenv CACHE INTERNAL "")
89+
add_library(${CPP_DOTENV} ${CPP_DOTENV_SRC})
90+
91+
target_link_libraries(${CPP_DOTENV}
92+
${ENVIRON_LIB}
93+
${PARSER_LIB}
94+
)
95+
96+
target_include_directories(${CPP_DOTENV} PUBLIC
97+
${CPP_DOTENV_INCLUDE_DIRS}
98+
)
99+
44100
target_compile_options(${CPP_DOTENV} PRIVATE
45-
-O3
101+
${CPP_DOTENV_COMPILE_OPTIONS}
46102
)
47103
endif()

README.md

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ C++ implementation of NodeJS [dotenv](https://github.com/motdotla/dotenv) projec
3939

4040
Supported build methods are:
4141

42-
- [CMake](#cmake) (>=3.10)
42+
- [CMake](#cmake) (>=3.16)
4343

4444
### CMake
4545

46-
**cpp-dotenv** comes with support for `CMake` right out of the box. In order to use it, simply include this repository's directory and link the `cpp_dotenv` target to your own targets where needed:
46+
**cpp-dotenv** comes with support for `CMake` right out of the box (>=3.16). In order to use it, simply include this repository's directory and link the `cpp_dotenv` target to your own targets where needed:
4747

4848
```cmake
4949
add_subdirectory(cpp-dotenv)
@@ -55,6 +55,42 @@ target_link_libraries(YOUR_TARGET cpp_dotenv)
5555

5656
After this, you might use the library as described in [usage](#usage); no extra scoping, no need to worry about the project's directory structure.
5757

58+
#### Build Options
59+
60+
By default, only one library target named `cpp_dotenv` is built. The type of library (static or shared) is controlled by CMake's standard `BUILD_SHARED_LIBS` option:
61+
62+
```bash
63+
# Build static library (default)
64+
cmake -DBUILD_SHARED_LIBS=OFF ..
65+
66+
# Build shared library
67+
cmake -DBUILD_SHARED_LIBS=ON ..
68+
```
69+
70+
#### Building Both Static and Shared Libraries
71+
72+
To build both static and shared libraries in a single build directory, use the `BUILD_BOTH_LIBRARIES` option:
73+
74+
```bash
75+
cmake -DBUILD_BOTH_LIBRARIES=ON ..
76+
```
77+
78+
This creates two separate library targets:
79+
- `cpp_dotenv_static` - outputs as `libcpp_dotenv.a`
80+
- `cpp_dotenv_shared` - outputs as `libcpp_dotenv.so` (or `.dylib` on macOS)
81+
82+
For backward compatibility, the `cpp_dotenv` target remains available as an alias to `cpp_dotenv_static`. If you need to link against the shared library, explicitly specify `cpp_dotenv_shared`:
83+
84+
```cmake
85+
# Link against static library (default)
86+
target_link_libraries(YOUR_TARGET cpp_dotenv)
87+
88+
# Link against shared library
89+
target_link_libraries(YOUR_TARGET cpp_dotenv_shared)
90+
```
91+
92+
**Note:** When `BUILD_BOTH_LIBRARIES` is enabled, `BUILD_SHARED_LIBS` is ignored. Do not use both options simultaneously.
93+
5894
## Usage
5995

6096
To be able to use the dotenv classes, simply include the main header file:

0 commit comments

Comments
 (0)