From 617c7626441a9d3a3ab85f2dfc525dfde6d98fc7 Mon Sep 17 00:00:00 2001 From: evotodi Date: Wed, 1 Sep 2021 10:36:02 -0400 Subject: [PATCH 1/4] Added the ability to build as a shared library. --- .gitignore | 5 +++ CMakeLists.txt | 37 +++++++++++++++++-- README.md | 29 +++++++++++++++ .../antlr4-cpp-runtime/runtime/CMakeLists.txt | 12 +++++- cpp-dotenv.pc.in | 10 +++++ 5 files changed, 87 insertions(+), 6 deletions(-) create mode 100644 cpp-dotenv.pc.in diff --git a/.gitignore b/.gitignore index c40a1f0..98052af 100644 --- a/.gitignore +++ b/.gitignore @@ -42,3 +42,8 @@ # CMake specifics build/ +/.idea/ +/cmake-build-*/ + +# CLion +.clion.source.upload.marker \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 2c7a502..409f124 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,10 +1,16 @@ #----------------------- PROJECT CONFIGURATION -------------------------------- cmake_minimum_required(VERSION 3.10) -project(cpp-dotenv VERSION 1.0.0) + +project(cpp-dotenv + VERSION 1.0.0 + DESCRIPTION "Library to load environment variables from .env files for C++ projects." + ) set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD_REQUIRED ON) +option(DOETENV_SHARED_LIB "Build CPP-DOTENV as a shared library" NO) + if ("${CMAKE_BUILD_TYPE}" STREQUAL "") set(CMAKE_BUILD_TYPE RELEASE) else() @@ -12,6 +18,8 @@ else() endif() message(STATUS "Building CPP-DOTENV in ${CMAKE_BUILD_TYPE} mode") +find_package(PkgConfig REQUIRED) + #------------------- SUBDIRECTORY ADDITION ------------------------------------ add_subdirectory(common) @@ -19,13 +27,16 @@ add_subdirectory(src) #----------------------- LIBRARY CONFIGURATION -------------------------------- -set(CPP_DOTENV cpp_dotenv CACHE INTERNAL "") +set(CPP_DOTENV cpp-dotenv CACHE INTERNAL "") set(CPP_DOTENV_SRC src/dotenv.cpp include/dotenv.h ) - -add_library(${CPP_DOTENV} ${CPP_DOTENV_SRC}) +if(DOETENV_SHARED_LIB) + add_library(${CPP_DOTENV} SHARED ${CPP_DOTENV_SRC}) +else() + add_library(${CPP_DOTENV} ${CPP_DOTENV_SRC}) +endif() target_link_libraries(${CPP_DOTENV} ${ENVIRON_LIB} @@ -36,6 +47,7 @@ target_include_directories(${CPP_DOTENV} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include ) + if ("${CMAKE_BUILD_TYPE}" STREQUAL "DEBUG") target_compile_options(${CPP_DOTENV} PRIVATE -g -Wall -O0 @@ -45,3 +57,20 @@ else() -O3 ) endif() + +#include(GNUInstallDirs) + +#----------------------- PKG CONFIGURATION -------------------------------- +message(STATUS "Package CPP-DOTENV for ${CMAKE_BUILD_TYPE} version ${PROJECT_VERSION}") +set(TARGET1 ${CPP_DOTENV}) + +# Generate pkg-config file +configure_file(./cpp-dotenv.pc.in ${PROJECT_BINARY_DIR}/cpp-dotenv.pc @ONLY) + + +#----------------------- INSTALL -------------------------------- +if(DOETENV_SHARED_LIB) + install(TARGETS ${CPP_DOTENV} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}) + install(FILES ${CMAKE_SOURCE_DIR}/include/dotenv.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/) + install(FILES ${PROJECT_BINARY_DIR}/cpp-dotenv.pc DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig) +endif() diff --git a/README.md b/README.md index aad33c5..1fb09e5 100644 --- a/README.md +++ b/README.md @@ -33,13 +33,21 @@ C++ implementation of NodeJS [dotenv](https://github.com/motdotla/dotenv) projec ## Dependencies +**Shared Library Build** requires libantlr4-runtime-dev + +```sudo apt install libantlr4-runtime-dev``` + +otherwise... + **NONE**, for sure! :sunglasses: If it had any, it wouldn't follow the basic dotenv principles. All the needed libraries are shipped with this repository right out of the box. + ## Build Supported build methods are: - [CMake](#cmake) (>=3.10) +- [Shared Library](#share-library-build) ### CMake @@ -55,6 +63,27 @@ target_link_libraries(YOUR_TARGET cpp_dotenv) 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. +### Share Library Build + +#### Build steps +* Clone the repo +* cd into the repo directory +* ```mkdir build``` +* ```cd build``` +* ```cmake ../``` +* ```make install``` + +#### Cmake +``` +find_package(PkgConfig REQUIRED) + +pkg_search_module(CPP_DOTENV QUIET cpp-dotenv) +if(NOT CPP_DOTENV_FOUND) + message(ERROR "cpp-dotenv not found!") +endif() +target_link_libraries(YOUR_LIBRARY INTERFACE cpp-dotenv) +``` + ## Usage To be able to use the dotenv classes, simply include the main header file: diff --git a/common/libs/antlr4-cpp-runtime/runtime/CMakeLists.txt b/common/libs/antlr4-cpp-runtime/runtime/CMakeLists.txt index c960a19..a6e484b 100644 --- a/common/libs/antlr4-cpp-runtime/runtime/CMakeLists.txt +++ b/common/libs/antlr4-cpp-runtime/runtime/CMakeLists.txt @@ -308,8 +308,11 @@ set(ANTLR4_CPP_RUNTIME_SRC src/tree/xpath/XPathWildcardElement.cpp src/tree/xpath/XPathWildcardElement.h ) - -add_library(${ANTLR4_CPP_RUNTIME} ${ANTLR4_CPP_RUNTIME_SRC}) +if(DOETENV_SHARED_LIB) + add_library(${ANTLR4_CPP_RUNTIME} SHARED ${ANTLR4_CPP_RUNTIME_SRC}) +else() + add_library(${ANTLR4_CPP_RUNTIME} ${ANTLR4_CPP_RUNTIME_SRC}) +endif() target_include_directories(${ANTLR4_CPP_RUNTIME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src @@ -327,3 +330,8 @@ else() -Wno-attributes ) endif() + +#----------------------- INSTALL -------------------------------- +if(DOETENV_SHARED_LIB) + install(TARGETS ${ANTLR4_CPP_RUNTIME} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}) +endif() \ No newline at end of file diff --git a/cpp-dotenv.pc.in b/cpp-dotenv.pc.in new file mode 100644 index 0000000..13954ad --- /dev/null +++ b/cpp-dotenv.pc.in @@ -0,0 +1,10 @@ +prefix=@CPACK_PACKAGE_INSTALL_DIRECTORY@ +exec_prefix=${prefix} +includedir=${prefix}/include +libdir=${exec_prefix}/lib +Name: cpp-dotenv +Description: @CMAKE_PROJECT_DESCRIPTION@ +Version: @PROJECT_VERSION@ +Cflags: -I${includedir} +Requires: antlr4_cpp_runtime +Libs: -L${libdir} -l@TARGET1@ \ No newline at end of file From 034550b6759ede50e6db04e6d16900bbcc1f96f1 Mon Sep 17 00:00:00 2001 From: evotodi Date: Wed, 1 Sep 2021 13:31:28 -0400 Subject: [PATCH 2/4] Fixed a typo in cmake option. --- CMakeLists.txt | 6 +++--- common/libs/antlr4-cpp-runtime/runtime/CMakeLists.txt | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 409f124..9948a35 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,7 +9,7 @@ project(cpp-dotenv set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD_REQUIRED ON) -option(DOETENV_SHARED_LIB "Build CPP-DOTENV as a shared library" NO) +option(DOTENV_SHARED_LIB "Build CPP-DOTENV as a shared library" NO) if ("${CMAKE_BUILD_TYPE}" STREQUAL "") set(CMAKE_BUILD_TYPE RELEASE) @@ -32,7 +32,7 @@ set(CPP_DOTENV_SRC src/dotenv.cpp include/dotenv.h ) -if(DOETENV_SHARED_LIB) +if(DOTENV_SHARED_LIB) add_library(${CPP_DOTENV} SHARED ${CPP_DOTENV_SRC}) else() add_library(${CPP_DOTENV} ${CPP_DOTENV_SRC}) @@ -69,7 +69,7 @@ configure_file(./cpp-dotenv.pc.in ${PROJECT_BINARY_DIR}/cpp-dotenv.pc @ONLY) #----------------------- INSTALL -------------------------------- -if(DOETENV_SHARED_LIB) +if(DOTENV_SHARED_LIB) install(TARGETS ${CPP_DOTENV} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}) install(FILES ${CMAKE_SOURCE_DIR}/include/dotenv.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/) install(FILES ${PROJECT_BINARY_DIR}/cpp-dotenv.pc DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig) diff --git a/common/libs/antlr4-cpp-runtime/runtime/CMakeLists.txt b/common/libs/antlr4-cpp-runtime/runtime/CMakeLists.txt index a6e484b..9f1c30f 100644 --- a/common/libs/antlr4-cpp-runtime/runtime/CMakeLists.txt +++ b/common/libs/antlr4-cpp-runtime/runtime/CMakeLists.txt @@ -308,7 +308,7 @@ set(ANTLR4_CPP_RUNTIME_SRC src/tree/xpath/XPathWildcardElement.cpp src/tree/xpath/XPathWildcardElement.h ) -if(DOETENV_SHARED_LIB) +if(DOTENV_SHARED_LIB) add_library(${ANTLR4_CPP_RUNTIME} SHARED ${ANTLR4_CPP_RUNTIME_SRC}) else() add_library(${ANTLR4_CPP_RUNTIME} ${ANTLR4_CPP_RUNTIME_SRC}) @@ -332,6 +332,6 @@ else() endif() #----------------------- INSTALL -------------------------------- -if(DOETENV_SHARED_LIB) +if(DOTENV_SHARED_LIB) install(TARGETS ${ANTLR4_CPP_RUNTIME} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}) endif() \ No newline at end of file From 5b90e351f1bcc3d0f451a757620e2a1ceafa1e3e Mon Sep 17 00:00:00 2001 From: evotodi Date: Mon, 13 Sep 2021 11:09:46 -0400 Subject: [PATCH 3/4] added include(GNUInstallDirs) to fix empty CMAKE_INSTALL_LIBDIR --- CMakeLists.txt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9948a35..df2aff0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -34,8 +34,10 @@ set(CPP_DOTENV_SRC ) if(DOTENV_SHARED_LIB) add_library(${CPP_DOTENV} SHARED ${CPP_DOTENV_SRC}) + message(STATUS "Building as shared library") else() add_library(${CPP_DOTENV} ${CPP_DOTENV_SRC}) + message(STATUS "Building as static library") endif() target_link_libraries(${CPP_DOTENV} @@ -69,6 +71,7 @@ configure_file(./cpp-dotenv.pc.in ${PROJECT_BINARY_DIR}/cpp-dotenv.pc @ONLY) #----------------------- INSTALL -------------------------------- +include(GNUInstallDirs) if(DOTENV_SHARED_LIB) install(TARGETS ${CPP_DOTENV} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}) install(FILES ${CMAKE_SOURCE_DIR}/include/dotenv.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/) From 8a8c27797fcf2a8061b148dead5b5ba390735836 Mon Sep 17 00:00:00 2001 From: evotodi Date: Mon, 13 Sep 2021 11:28:17 -0400 Subject: [PATCH 4/4] remove antlr4 runtime from pkg-config requirements. --- cpp-dotenv.pc.in | 1 - 1 file changed, 1 deletion(-) diff --git a/cpp-dotenv.pc.in b/cpp-dotenv.pc.in index 13954ad..d49d51f 100644 --- a/cpp-dotenv.pc.in +++ b/cpp-dotenv.pc.in @@ -6,5 +6,4 @@ Name: cpp-dotenv Description: @CMAKE_PROJECT_DESCRIPTION@ Version: @PROJECT_VERSION@ Cflags: -I${includedir} -Requires: antlr4_cpp_runtime Libs: -L${libdir} -l@TARGET1@ \ No newline at end of file