Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the ability to build as a shared library. #23

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,8 @@

# CMake specifics
build/
/.idea/
/cmake-build-*/

# CLion
.clion.source.upload.marker
40 changes: 36 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,31 +1,44 @@
#----------------------- 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(DOTENV_SHARED_LIB "Build CPP-DOTENV as a shared library" NO)

if ("${CMAKE_BUILD_TYPE}" STREQUAL "")
set(CMAKE_BUILD_TYPE RELEASE)
else()
string(TOUPPER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE)
endif()
message(STATUS "Building CPP-DOTENV in ${CMAKE_BUILD_TYPE} mode")

find_package(PkgConfig REQUIRED)

#------------------- SUBDIRECTORY ADDITION ------------------------------------

add_subdirectory(common)
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(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}
${ENVIRON_LIB}
Expand All @@ -36,6 +49,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
Expand All @@ -45,3 +59,21 @@ 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 --------------------------------
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}/)
install(FILES ${PROJECT_BINARY_DIR}/cpp-dotenv.pc DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
endif()
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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:
Expand Down
12 changes: 10 additions & 2 deletions common/libs/antlr4-cpp-runtime/runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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(DOTENV_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
Expand All @@ -327,3 +330,8 @@ else()
-Wno-attributes
)
endif()

#----------------------- INSTALL --------------------------------
if(DOTENV_SHARED_LIB)
install(TARGETS ${ANTLR4_CPP_RUNTIME} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
endif()
9 changes: 9 additions & 0 deletions cpp-dotenv.pc.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
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}
Libs: -L${libdir} -l@TARGET1@