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

Add external dependencies option #547

Merged
merged 14 commits into from
May 24, 2024
30 changes: 30 additions & 0 deletions .github/workflows/linux-gxx-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,33 @@ jobs:

- name: Run docker container
run: docker run test

external_dependencies:
runs-on: ubuntu-24.04
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Configure python
uses: actions/setup-python@v5
with:
python-version: '3.10'
- name: Install conan and ninja
run: pip install conan ninja
- name: Install libraries dependencies
run: |
CC=gcc-13 CXX=g++-13 conan profile detect && \
conan install -r conancenter -s compiler.cppstd=20 \
--requires=fmt/10.2.1 --requires=gtest/1.14.0 \
-g CMakeToolchain -g CMakeDeps \
--build=missing -of conan \
-c tools.build:compiler_executables='{"c": "gcc-13", "cxx": "g++-13"}'
- name: CMake build
run: |
cmake -B build -S . -G Ninja \
-DUSE_SYSTEM_DEPENDENCIES=ON \
-DCMAKE_VERBOSE_MAKEFILE=ON \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_CXX_COMPILER=g++-13 \
-DCMAKE_C_COMPILER=gcc-13 \
-DCMAKE_TOOLCHAIN_FILE=conan/conan_toolchain.cmake && \
cmake --build build
58 changes: 36 additions & 22 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,15 @@ project(${PROJECT_NAME} CXX)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

include(CheckCXXSourceCompiles)
set(CMAKE_REQUIRED_FLAGS -std=c++20)
check_cxx_source_compiles("#include <format>\nint main(){ auto var = std::format(\"{}\", \"Hello\"); return 0; }" HAS_STD_FORMAT)

option(BUILD_FAKER_TESTS DEFAULT ON)
option(USE_SYSTEM_DEPENDENCIES "Use fmt and GTest from system" OFF)

if (MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /permissive- /bigobj")
else ()
set(CMAKE_CXX_FLAGS
"${CMAKE_CXX_FLAGS} -Wall -Wextra -Wpedantic -Wconversion -Wformat -Werror"
)
endif ()
if (NOT USE_SYSTEM_DEPENDENCIES)
set(CMAKE_REQUIRED_FLAGS -std=c++20)
include(CheckCXXSourceCompiles)
check_cxx_source_compiles("#include <format>\nint main(){ auto var = std::format(\"{}\", \"Hello\"); return 0; }"
HAS_STD_FORMAT)
endif()

set(LIBRARY_NAME faker-cxx)

Expand Down Expand Up @@ -113,6 +109,11 @@ target_include_directories(
$<INSTALL_INTERFACE:include>)

target_compile_features(${LIBRARY_NAME} PUBLIC cxx_std_20)
if (MSVC)
target_compile_options(${LIBRARY_NAME} PRIVATE /permissive- /bigobj)
else ()
target_compile_options(${LIBRARY_NAME} PRIVATE -Wall -Wextra -Wpedantic -Wconversion -Wformat)
endif ()

install(TARGETS ${LIBRARY_NAME}
EXPORT ${LIBRARY_NAME}-targets
Expand All @@ -131,22 +132,18 @@ install(EXPORT ${LIBRARY_NAME}-targets
FILE ${LIBRARY_NAME}-config.cmake
DESTINATION lib/cmake/${LIBRARY_NAME})

if (HAS_STD_FORMAT)
if (HAS_STD_FORMAT AND NOT USE_SYSTEM_DEPENDENCIES)
target_compile_definitions(${LIBRARY_NAME} PRIVATE HAS_STD_FORMAT)
elseif(USE_SYSTEM_DEPENDENCIES)
find_package(fmt REQUIRED)
target_link_libraries(${LIBRARY_NAME} PRIVATE fmt::fmt)
else ()
add_subdirectory(externals/fmt)
set(FMT_INCLUDE_DIR "${CMAKE_SOURCE_DIR}/externals/fmt/include")
target_link_libraries(${LIBRARY_NAME} PRIVATE fmt)
endif ()

if (BUILD_FAKER_TESTS)
add_subdirectory(externals/googletest)

set(GTEST_INCLUDE_DIR
"${CMAKE_SOURCE_DIR}/externals/googletest/googletest/include")
set(GMOCK_INCLUDE_DIR
"${CMAKE_SOURCE_DIR}/externals/googletest/googlemock/include")

enable_testing()

set(target_code_coverage_ALL 1)
Expand All @@ -156,9 +153,25 @@ if (BUILD_FAKER_TESTS)
add_code_coverage_all_targets()

add_executable(${LIBRARY_NAME}-UT ${FAKER_UT_SOURCES})
if (MSVC)
target_compile_options(${LIBRARY_NAME}-UT PRIVATE /permissive- /bigobj)
endif()

if (USE_SYSTEM_DEPENDENCIES)
find_package(GTest REQUIRED)
target_link_libraries(${LIBRARY_NAME}-UT PRIVATE GTest::gtest
GTest::gtest_main GTest::gmock GTest::gmock_main faker-cxx)
else ()
add_subdirectory(externals/googletest)

set(GTEST_INCLUDE_DIR
"${CMAKE_SOURCE_DIR}/externals/googletest/googletest/include")
set(GMOCK_INCLUDE_DIR
"${CMAKE_SOURCE_DIR}/externals/googletest/googlemock/include")

target_link_libraries(${LIBRARY_NAME}-UT PRIVATE gtest_main gmock_main
faker-cxx)
target_link_libraries(${LIBRARY_NAME}-UT PRIVATE gtest_main gmock_main
faker-cxx)
endif ()

if (HAS_STD_FORMAT)
target_include_directories(
Expand All @@ -167,7 +180,8 @@ if (BUILD_FAKER_TESTS)
${CMAKE_CURRENT_LIST_DIR})
target_compile_definitions(${LIBRARY_NAME}-UT PRIVATE HAS_STD_FORMAT)
else ()
target_link_libraries(${LIBRARY_NAME}-UT PRIVATE fmt)
target_link_libraries(${LIBRARY_NAME}-UT PRIVATE
$<IF:$<TARGET_EXISTS:fmt::fmt>,fmt::fmt,fmt>)
target_include_directories(
${LIBRARY_NAME}-UT
PRIVATE ${FMT_INCLUDE_DIR} ${GTEST_INCLUDE_DIR}
Expand Down
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,11 @@ int main()

```cmake
set(BUILD_FAKER_TESTS OFF)

add_subdirectory(externals/faker-cxx)

add_executable(main Main.cpp)

target_link_libraries(main faker-cxx)
```

Expand All @@ -126,10 +126,12 @@ If you have any confusion please refer to the respective guides.
- GTest (set `BUILD_FAKER_CXX_TESTS=OFF` CMake flag to disable this dependency)
- fmt (only for compilers that don't support std::format)

In order to use external dependencies installed in your system, you can set the `USE_SYSTEM_DEPENDENCIES` CMake flag to `ON`.

## ✨ Contributing

We would love it if you contributed to Faker C++! 🚀

Before contributing please review our [CONTRIBUTING](https://github.com/cieslarmichal/faker-cxx/blob/main/CONTRIBUTING.md) guide.
Before contributing please review our [CONTRIBUTING](https://github.com/cieslarmichal/faker-cxx/blob/main/CONTRIBUTING.md) guide.

Additionally, we encourage you to join our [Discord Channel](https://discord.gg/h2ur8H6mK6) for contributors.
Loading