From 868a6a6e9acd4907e488ad0925fdac69fefcbe24 Mon Sep 17 00:00:00 2001 From: Uilian Ries Date: Fri, 24 May 2024 10:18:42 +0200 Subject: [PATCH] Add external dependencies option (#547) * Add external dependencies option Signed-off-by: Uilian Ries * Add entry documenation to explain option Signed-off-by: Uilian Ries * Add build for external dependencies Signed-off-by: Uilian Ries * build on feature branch Signed-off-by: Uilian Ries * Use Ubuntu 24.04 Signed-off-by: Uilian Ries * Use python setup Signed-off-by: Uilian Ries * fix conan install Signed-off-by: Uilian Ries * Use std format only when no deps Signed-off-by: Uilian Ries * No werror Signed-off-by: Uilian Ries * Add Werror Signed-off-by: Uilian Ries * Use compile options Signed-off-by: Uilian Ries * Pass msvc flags Signed-off-by: Uilian Ries * Drop -Werror Signed-off-by: Uilian Ries --------- Signed-off-by: Uilian Ries --- .github/workflows/linux-gxx-build.yml | 30 ++++++++++++++ CMakeLists.txt | 58 +++++++++++++++++---------- README.md | 10 +++-- 3 files changed, 72 insertions(+), 26 deletions(-) diff --git a/.github/workflows/linux-gxx-build.yml b/.github/workflows/linux-gxx-build.yml index c50c37ffb..6932bd6e2 100644 --- a/.github/workflows/linux-gxx-build.yml +++ b/.github/workflows/linux-gxx-build.yml @@ -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 diff --git a/CMakeLists.txt b/CMakeLists.txt index 2b359c9ec..4d9c3a088 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 \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 \nint main(){ auto var = std::format(\"{}\", \"Hello\"); return 0; }" + HAS_STD_FORMAT) +endif() set(LIBRARY_NAME faker-cxx) @@ -113,6 +109,11 @@ target_include_directories( $) 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 @@ -131,8 +132,11 @@ 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") @@ -140,13 +144,6 @@ else () 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) @@ -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( @@ -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 + $,fmt::fmt,fmt>) target_include_directories( ${LIBRARY_NAME}-UT PRIVATE ${FMT_INCLUDE_DIR} ${GTEST_INCLUDE_DIR} diff --git a/README.md b/README.md index a0ff440c1..860000e3d 100644 --- a/README.md +++ b/README.md @@ -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) ``` @@ -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.