Skip to content

Commit

Permalink
Enable CI build on macos and windows (#6)
Browse files Browse the repository at this point in the history
* Enable build on `macos-latest`

* Fix clang weak-vtables warning

* Upgrade `checkout` action to `v3`

* Fix `-Wdeprecated-copy-with-user-provided-dtor`

* Fix clang build

* Use constructor inheritance

* Enable build on windows-latest

* Split steps

* Use cmake to build

* Fix wrong folder for ctest running

* Handle MSVC build

* Fix MSVC build

* Try to fix MSVC build issue

* Fix `unreachable code` warning

* Add WINDOWS_EXPORT_ALL_SYMBOLS

* Set WINDOWS_EXPORT_ALL_SYMBOLS properly

* Try to fix ctest issue on windows

* Include CTest in tests CMakeLists.txt

* Revert "Include CTest in tests CMakeLists.txt"

This reverts commit 5f70f03.

* Debug

* Fix missing binary file issue on Windows

* Debug

* Add `-C` to ctest cmd run

* Enable examples build in CI
  • Loading branch information
hongshibao authored Nov 17, 2022
1 parent e3e2b8d commit f6984aa
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 15 deletions.
22 changes: 14 additions & 8 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ jobs:
matrix:
os:
- ubuntu-latest
# - windows-latest
# - macos-latest
- windows-latest
- macos-latest
# Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.)
buildtype:
- Debug
Expand All @@ -25,17 +25,23 @@ jobs:
runs-on: ${{ matrix.os }}
steps:
- name: Checkout Code
uses: actions/checkout@v2
uses: actions/checkout@v3
with:
submodules: 'recursive'

- name: Build and Test
if: ${{ matrix.buildtype != 'Coverage' }}
shell: bash
run: |
if [[ "${{ matrix.buildtype }}" == "Coverage" ]]; then
ctest -S cmake/CI.CTestScript.cmake --output-on-failure -V -DCTEST_SITE=GitHub -DCTEST_BUILD_NAME="${{ matrix.os }}-${{ matrix.buildtype }}"
else
mkdir build && cd build && cmake -DCMAKE_BUILD_TYPE=${{ matrix.buildtype }} .. && make -j4 && ctest -VV .
fi
cmake -B build -DCMAKE_BUILD_TYPE=${{ matrix.buildtype }} -DSINGLEFLIGHT_BUILD_EXAMPLES=ON
cmake --build build --config ${{ matrix.buildtype }}
cd build
ctest -VV -C ${{ matrix.buildtype }}
- name: Build and Test for Code Coverage
if: ${{ matrix.buildtype == 'Coverage' }}
run: |
ctest -S cmake/CI.CTestScript.cmake --output-on-failure -V -DCTEST_SITE=GitHub -DCTEST_BUILD_NAME="${{ matrix.os }}-${{ matrix.buildtype }}"
- name: Publish Code Coverage
if: ${{ matrix.buildtype == 'Coverage' }}
Expand Down
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,10 @@ set(INCLUDE_DIR

if(SINGLEFLIGHT_BUILD_SHARED_LIB)
add_library(${LIBRARY_NAME} SHARED ${SRC_FILES})
set(WINDOWS_EXPORT_ALL_SYMBOLS YES)
else()
add_library(${LIBRARY_NAME} STATIC ${SRC_FILES})
set(WINDOWS_EXPORT_ALL_SYMBOLS NO)
endif()

# Since we put the public interface headers in the include directory, we need to tell the compiler so that we can #include <file>.
Expand All @@ -93,6 +95,7 @@ set_target_properties(
CXX_STANDARD 14
CXX_STANDARD_REQUIRED YES
CXX_EXTENSIONS NO
WINDOWS_EXPORT_ALL_SYMBOLS ${WINDOWS_EXPORT_ALL_SYMBOLS}
)

# For custom settings.
Expand Down
14 changes: 12 additions & 2 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ include(FetchContent)
FetchContent_Declare(
spdlog
GIT_REPOSITORY https://github.com/gabime/spdlog
GIT_TAG v1.10.0
GIT_TAG v1.11.0
GIT_SHALLOW TRUE
FIND_PACKAGE_ARGS
)
Expand All @@ -31,5 +31,15 @@ set_target_properties(
CXX_STANDARD_REQUIRED YES
CXX_EXTENSIONS NO
)

target_set_warnings(${EXAMPLE_MAIN} ENABLE ALL AS_ERROR ALL DISABLE Annoying) # Set warnings.

# Disable some warnings for `fmt` library
if(NOT "${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
target_compile_options(
${EXAMPLE_MAIN}
PRIVATE
"-Wno-documentation-unknown-command"
"-Wno-float-equal"
"-Wno-undefined-func-template"
)
endif()
5 changes: 4 additions & 1 deletion examples/example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

using namespace std;

namespace {

// SingleFlight example code 1
void example_1(singleflight::SingleFlight<string, int>& sf) {
// Simulate a heavy function call
Expand Down Expand Up @@ -42,7 +44,6 @@ void example_2(singleflight::SingleFlight<string, int>& sf) {
spdlog::info("throwing_exception_func call by Thread {}", tid);
this_thread::sleep_for(500ms);
throw runtime_error{"std::runtime_error from throwing_exception_func"};
return 200;
};

// Thread entry function
Expand All @@ -69,6 +70,8 @@ void example_2(singleflight::SingleFlight<string, int>& sf) {
}
}

} // namespace

int main() {
singleflight::SingleFlight<string, int> sf;

Expand Down
11 changes: 10 additions & 1 deletion include/singleflight/singleflight.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,18 @@ namespace singleflight {

class FuncCallFailedException : public std::runtime_error {
public:
FuncCallFailedException(const std::string& msg) : std::runtime_error{msg} {}
using std::runtime_error::runtime_error;

FuncCallFailedException(const FuncCallFailedException&) = default;
FuncCallFailedException& operator=(const FuncCallFailedException&) = default;
FuncCallFailedException(FuncCallFailedException&&) = default;
FuncCallFailedException& operator=(FuncCallFailedException&&) = default;

~FuncCallFailedException() noexcept override;
};

FuncCallFailedException::~FuncCallFailedException() noexcept {}

template <typename TKey, typename TRet>
class SingleFlight {
public:
Expand Down
14 changes: 12 additions & 2 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ include(FetchContent)
FetchContent_Declare(
spdlog
GIT_REPOSITORY https://github.com/gabime/spdlog
GIT_TAG v1.10.0
GIT_TAG v1.11.0
GIT_SHALLOW TRUE
FIND_PACKAGE_ARGS
)
Expand All @@ -41,9 +41,19 @@ set_target_properties(
CXX_STANDARD_REQUIRED YES
CXX_EXTENSIONS NO
)

target_set_warnings(${TEST_MAIN} ENABLE ALL AS_ERROR ALL DISABLE Annoying) # Set warnings.

# Disable some warnings for `fmt` library
if(NOT "${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
target_compile_options(
${TEST_MAIN}
PRIVATE
"-Wno-documentation-unknown-command"
"-Wno-float-equal"
"-Wno-undefined-func-template"
)
endif()

add_test(

# Use some per-module/project prefix so that it is easier to run only tests for this module
Expand Down
5 changes: 4 additions & 1 deletion tests/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ using namespace std;
using namespace singleflight;
using Catch::Matchers::Message;

namespace {

template <typename ExType>
string ThrowingExceptionFunc(int tid, atomic_int& func_call_cnt, const ExType& ex) {
++func_call_cnt;
spdlog::info("throwing_exception_func call by Thread {}", tid);
this_thread::sleep_for(500ms);
throw ex;
return "Result from throwing_exception_func";
}

void LaunchAndWaitThreads(function<void(int)>&& thread_entry_func) {
Expand All @@ -35,6 +36,8 @@ void LaunchAndWaitThreads(function<void(int)>&& thread_entry_func) {
}
}

} // namespace

TEST_CASE("Multiple threads call a same func using a same key") {
SingleFlight<string, string> sf;
atomic_int func_call_cnt{0};
Expand Down

0 comments on commit f6984aa

Please sign in to comment.