summaryrefslogtreecommitdiff
path: root/.github/workflows/build.yml
blob: dd773f9980a284817de8ea3620016413c6564f4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
---
name: build

on:
  pull_request:
    types:
      - synchronize
      - reopened
      - opened
    branches:
      - master
    push:
      paths-ignore:
        - '**.md'
        - '.mailmap'
        - 'ChangeLog*'
        - 'whatsnew*'
        - 'LICENSE'
  push:
    branches:
      - master
    push:
      paths-ignore:
        - '**.md'
        - '.mailmap'
        - 'ChangeLog*'
        - 'whatsnew*'
        - 'LICENSE'

jobs:
  linux-cmake-job:
    runs-on: ${{ matrix.os }}
    if: "!contains(github.event.head_commit.message, 'ci/linux skip') && !contains(github.event.head_commit.message, 'ci/linux/cmake skip')"
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-18.04]
        EVENT_MATRIX:
          - DIST
          - NONE
          - NO_SSL
          - DISABLE_OPENSSL
          - DISABLE_THREAD_SUPPORT
          - DISABLE_DEBUG_MODE
          - DISABLE_MM_REPLACEMENT
          - COMPILER_CLANG
          - TEST_EXPORT_STATIC
          - TEST_EXPORT_SHARED
        include:
          - os: ubuntu-22.04
            EVENT_MATRIX: OPENSSL_3
          # use recent ubuntu with recent clang for recent sanitizers
          - os: ubuntu-22.04
            EVENT_MATRIX: TSAN
          - os: ubuntu-22.04
            EVENT_MATRIX: ASAN
          - os: ubuntu-22.04
            EVENT_MATRIX: UBSAN

    steps:
      - uses: actions/checkout@v2.0.0
      - name: Cache Build
        uses: actions/cache@v2
        with:
          path: build
          key: ${{ matrix.os }}-cmake-${{ matrix.EVENT_MATRIX }}-v3
      - name: Cache Dist Build
        uses: actions/cache@v2
        with:
          path: dist
          key: ${{ matrix.os }}-cmake-dist-${{ matrix.EVENT_MATRIX }}-v3

      - name: Install Depends
        run: |
          sudo apt-get update
          sudo apt-get install -y libmbedtls-dev

      - name: Build
        shell: bash
        run: |
          if [ "${{ matrix.EVENT_MATRIX }}" == "DIST" ]; then
            ./autogen.sh
            mkdir -p dist
            cd dist
            ../configure
            rm -fr *.tar.gz
            make dist
            archive=$(echo *.tar.gz)
            tar -vxf $archive
            cd $(basename $archive .tar.gz)
          fi

          if [ "${{ matrix.EVENT_MATRIX }}" == "DISABLE_OPENSSL" ]; then
            EVENT_CMAKE_OPTIONS="-DEVENT__DISABLE_OPENSSL=ON"
          elif [ "${{ matrix.EVENT_MATRIX }}" == "NO_SSL" ]; then
            EVENT_CMAKE_OPTIONS="-DEVENT__DISABLE_OPENSSL=ON -DEVENT__DISABLE_MBEDTLS=ON"
          elif [ "${{ matrix.EVENT_MATRIX }}" == "DISABLE_THREAD_SUPPORT" ]; then
            EVENT_CMAKE_OPTIONS="-DEVENT__DISABLE_THREAD_SUPPORT=ON"
          elif [ "${{ matrix.EVENT_MATRIX }}" == "DISABLE_DEBUG_MODE" ]; then
            EVENT_CMAKE_OPTIONS="-DEVENT__DISABLE_DEBUG_MODE=ON"
          elif [ "${{ matrix.EVENT_MATRIX }}" == "DISABLE_MM_REPLACEMENT" ]; then
            EVENT_CMAKE_OPTIONS="-DEVENT__DISABLE_MM_REPLACEMENT=ON"
          elif [ "${{ matrix.EVENT_MATRIX }}" == "COMPILER_CLANG" ]; then
            EVENT_CMAKE_OPTIONS=""
            export CC=clang
          elif [ "${{ matrix.EVENT_MATRIX }}" == "TEST_EXPORT_STATIC" ]; then
            EVENT_CMAKE_OPTIONS="-DEVENT__LIBRARY_TYPE=STATIC -DEVENT__DISABLE_TESTS=ON -DEVENT__DISABLE_SAMPLES=ON"
          elif [ "${{ matrix.EVENT_MATRIX }}" == "TEST_EXPORT_SHARED" ]; then
            EVENT_CMAKE_OPTIONS="-DEVENT__LIBRARY_TYPE=SHARED -DEVENT__DISABLE_TESTS=ON -DEVENT__DISABLE_SAMPLES=ON"
          elif [ "${{ matrix.EVENT_MATRIX }}" == "ASAN" ]; then
            EVENT_CMAKE_OPTIONS="-DCMAKE_C_FLAGS=-fsanitize=address -DCMAKE_C_COMPILER=clang -DCMAKE_BUILD_TYPE=debug"
          elif [ "${{ matrix.EVENT_MATRIX }}" == "TSAN" ]; then
            EVENT_CMAKE_OPTIONS="-DCMAKE_C_FLAGS=-fsanitize=thread -DCMAKE_C_COMPILER=clang -DCMAKE_BUILD_TYPE=debug"
          elif [ "${{ matrix.EVENT_MATRIX }}" == "UBSAN" ]; then
            EVENT_CMAKE_OPTIONS="-DCMAKE_C_FLAGS=-fsanitize=undefined -DCMAKE_C_COMPILER=clang -DCMAKE_BUILD_TYPE=debug"
          else
            EVENT_CMAKE_OPTIONS=""
          fi

          mkdir -p build
          cd build
          echo [cmake]: cmake .. -DEVENT__ENABLE_GCC_WARNINGS=ON $EVENT_CMAKE_OPTIONS
          cmake .. -DEVENT__ENABLE_GCC_WARNINGS=ON $EVENT_CMAKE_OPTIONS || (rm -rf * && cmake .. $EVENT_CMAKE_OPTIONS)
          cmake --build .

      - name: Test
        uses: nick-fields/retry@v2
        with:
          max_attempts: 3
          timeout_minutes: 20
          shell: bash
          command: |
            JOBS=20
            export CTEST_PARALLEL_LEVEL=$JOBS
            export CTEST_OUTPUT_ON_FAILURE=1

            export TSAN_OPTIONS=suppressions=$PWD/extra/tsan.supp
            export LSAN_OPTIONS=suppressions=$PWD/extra/lsan.supp

            if [ "${{ matrix.EVENT_MATRIX }}" == "DIST" ]; then
              cd dist
              archive=$(echo *.tar.gz)
              cd $(basename $archive .tar.gz)
            fi
            cd build

            if [ "${{ matrix.EVENT_MATRIX }}" == "TEST_EXPORT_STATIC" ]; then
              sudo python3 ../test-export/test-export.py static
            elif [ "${{ matrix.EVENT_MATRIX }}" == "TEST_EXPORT_SHARED" ]; then
              sudo python3 ../test-export/test-export.py shared
            else
              cmake --build . --target verify
            fi

      - uses: actions/upload-artifact@v1
        if: failure() && matrix.EVENT_MATRIX != 'DIST'
        with:
          name: ${{ matrix.os }}-cmake-${{ matrix.EVENT_MATRIX }}-build
          path: build
      - uses: actions/upload-artifact@v1
        if: failure() && matrix.EVENT_MATRIX == 'DIST'
        with:
          name: ${{ matrix.os }}-cmake-${{ matrix.EVENT_MATRIX }}-dist
          path: dist

  linux-autotools-job:
    runs-on: ${{ matrix.os }}
    if: "!contains(github.event.head_commit.message, 'ci/linux skip') && !contains(github.event.head_commit.message, 'ci/linux/autotools skip')"
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-18.04]
        EVENT_MATRIX:
          - DIST
          - NONE
          - NO_SSL
          - DISABLE_OPENSSL
          - DISABLE_THREAD_SUPPORT
          - DISABLE_DEBUG_MODE
          - DISABLE_MM_REPLACEMENT
          - COMPILER_CLANG
        include:
          - os: ubuntu-22.04
            EVENT_MATRIX: OPENSSL_3

    steps:
      - uses: actions/checkout@v2.0.0
      - name: Cache Build
        uses: actions/cache@v2
        with:
          path: build
          key: ${{ matrix.os }}-autotools-${{ matrix.EVENT_MATRIX }}-v3
      - name: Cache Dist Build
        uses: actions/cache@v2
        with:
          path: dist
          key: ${{ matrix.os }}-autotools-dist-${{ matrix.EVENT_MATRIX }}-v3

      - name: Install Depends
        run: |
          sudo apt-get update
          sudo apt-get install -y libmbedtls-dev

      - name: Build
        shell: bash
        run: |
          if [ "${{ matrix.EVENT_MATRIX }}" == "DISABLE_OPENSSL" ]; then
            EVENT_CONFIGURE_OPTIONS="--disable-openssl"

          elif [ "${{ matrix.EVENT_MATRIX }}" == "NO_SSL" ]; then
            EVENT_CONFIGURE_OPTIONS="--disable-openssl --disable-mbedtls"

          elif [ "${{ matrix.EVENT_MATRIX }}" == "DISABLE_THREAD_SUPPORT" ]; then
            EVENT_CONFIGURE_OPTIONS="--disable-thread-support"

          elif [ "${{ matrix.EVENT_MATRIX }}" == "DISABLE_DEBUG_MODE" ]; then
            EVENT_CONFIGURE_OPTIONS="--disable-debug-mode"

          elif [ "${{ matrix.EVENT_MATRIX }}" == "DISABLE_MM_REPLACEMENT" ]; then
            EVENT_CONFIGURE_OPTIONS="--disable-malloc-replacement"

          elif [ "${{ matrix.EVENT_MATRIX }}" == "COMPILER_CLANG" ]; then
            EVENT_CONFIGURE_OPTIONS=""
            export CC=clang

          else
            EVENT_CONFIGURE_OPTIONS=""
          fi

          ./autogen.sh

          if [ "${{ matrix.EVENT_MATRIX }}" == "DIST" ]; then
            mkdir -p dist
            cd dist
            rm -fr *.tar.gz
            ../configure $EVENT_CONFIGURE_OPTIONS
            make dist
            archive=$(echo *.tar.gz)
            tar -vxf $archive
            cd $(basename $archive .tar.gz)
          fi

          mkdir -p build
          cd build
          echo [configure]: ../configure --enable-gcc-warnings $EVENT_CONFIGURE_OPTIONS
          ../configure --enable-gcc-warnings $EVENT_CONFIGURE_OPTIONS
          make
      - name: Test
        uses: nick-fields/retry@v2
        with:
          max_attempts: 3
          timeout_minutes: 20
          shell: bash
          command: |
            JOBS=20
            if [ "${{ matrix.EVENT_MATRIX }}" == "DIST" ]; then
              cd dist
              archive=$(echo *.tar.gz)
              cd $(basename $archive .tar.gz)
            fi
            cd build
            make -j $JOBS verify

      - uses: actions/upload-artifact@v1
        if: failure() && matrix.EVENT_MATRIX != 'DIST'
        with:
          name: ${{ matrix.os }}-autotools-${{ matrix.EVENT_MATRIX }}-build
          path: build
      - uses: actions/upload-artifact@v1
        if: failure() && matrix.EVENT_MATRIX == 'DIST'
        with:
          name: ${{ matrix.os }}-autotools-${{ matrix.EVENT_MATRIX }}-dist
          path: dist

  windows-vs2019-job:
    runs-on: ${{ matrix.os }}
    if: "!contains(github.event.head_commit.message, 'ci/windows skip')"
    strategy:
      fail-fast: false
      matrix:
        os: [windows-2019]
        EVENT_MATRIX:
          - NONE
          - NO_SSL
          - LIBRARY_TYPE_STATIC
          - DISABLE_OPENSSL
          - DISABLE_THREAD_SUPPORT
          - DISABLE_DEBUG_MODE
          - DISABLE_MM_REPLACEMENT
          - DUNICODE
          - TEST_EXPORT_SHARED
          - TEST_EXPORT_STATIC

    steps:
      - uses: actions/checkout@v2.0.0

      - name: Cache Build
        uses: actions/cache@v2
        with:
          path: build
          key: ${{ matrix.os }}-${{ matrix.EVENT_MATRIX }}-v4

      - name: Prepare vcpkg
        uses: lukka/run-vcpkg@v7
        id: runvcpkg
        with:
          vcpkgArguments: zlib:x64-windows openssl:x64-windows mbedtls:x64-windows
          vcpkgDirectory: ${{ runner.workspace }}/vcpkg/
          vcpkgTriplet: x64-windows
          vcpkgGitCommitId: af2287382b1991dbdcb7e5112d236f3323b9dd7a

      - name: Build
        shell: powershell
        run: |
          $EVENT_BUILD_PARALLEL=10

          if ( "${{ matrix.EVENT_MATRIX }}" -eq "LIBRARY_TYPE_STATIC" ) {
            $EVENT_CMAKE_OPTIONS="-DEVENT__LIBRARY_TYPE=STATIC -DEVENT__MSVC_STATIC_RUNTIME=OFF"
          }
          elseif ( "${{ matrix.EVENT_MATRIX }}" -eq "DISABLE_OPENSSL" ) {
            $EVENT_CMAKE_OPTIONS="-DEVENT__DISABLE_OPENSSL=ON"
          }
          elseif ( "${{ matrix.EVENT_MATRIX }}" -eq "DISABLE_THREAD_SUPPORT" ) {
            $EVENT_CMAKE_OPTIONS="-DEVENT__DISABLE_THREAD_SUPPORT=ON"
          }
          elseif ( "${{ matrix.EVENT_MATRIX }}" -eq "DISABLE_DEBUG_MODE" ) {
            $EVENT_CMAKE_OPTIONS="-DEVENT__DISABLE_DEBUG_MODE=ON"
          }
          elseif ( "${{ matrix.EVENT_MATRIX }}" -eq "DISABLE_MM_REPLACEMENT" ) {
            $EVENT_CMAKE_OPTIONS="-DEVENT__DISABLE_MM_REPLACEMENT=ON"
          }
          elseif ( "${{ matrix.EVENT_MATRIX }}" -eq "UNICODE" ) {
            $EVENT_CMAKE_OPTIONS="-DCMAKE_C_FLAGS='-DUNICODE -D_UNICODE'"
          }
          elseif ( "${{ matrix.EVENT_MATRIX }}" -eq "TEST_EXPORT_SHARED" ) {
            $EVENT_CMAKE_OPTIONS="-DEVENT__DISABLE_TESTS=ON -DEVENT__DISABLE_SAMPLES=ON"
          }
          elseif ( "${{ matrix.EVENT_MATRIX }}" -eq "TEST_EXPORT_STATIC" ) {
            $EVENT_CMAKE_OPTIONS="-DEVENT__LIBRARY_TYPE=STATIC -DEVENT__MSVC_STATIC_RUNTIME=OFF -DEVENT__DISABLE_TESTS=ON -DEVENT__DISABLE_SAMPLES=ON"
          }
          elseif ( "${{ matrix.EVENT_MATRIX }}" -eq "NO_SSL" ) {
            $EVENT_CMAKE_OPTIONS="-DEVENT__DISABLE_OPENSSL=ON -DEVENT__DISABLE_MBEDTLS=ON"
          }
          else {
            $EVENT_CMAKE_OPTIONS=""
          }

          mkdir build -ea 0
          cd build

          $CMAKE_CMD="cmake -G 'Visual Studio 16 2019' -A x64 -DCMAKE_TOOLCHAIN_FILE=${{ runner.workspace }}/vcpkg/scripts/buildsystems/vcpkg.cmake .. $EVENT_CMAKE_OPTIONS"
          function cmake_configure($retry)
          {
            $errcode=0
            try {
              if ($retry -eq 0) {
                echo "[cmake configure retry] $CMAKE_CMD"
              } else {
                echo "[cmake configure] $CMAKE_CMD"
              }
              Invoke-Expression $CMAKE_CMD
              $errcode=$LastExitCode
            }
            catch {
              $errcode=1
            }
            finally {
              if ($errcode -ne 0) {
                if ($retry -eq 0) {
                  $host.SetShouldExit($LastExitCode)
                } else {
                  echo "Remove all entries in build directory"
                  rm -r -fo *
                  cmake_configure 0
                }
              }
            }
          }
          cmake_configure 1
          cmake --build . -j $EVENT_BUILD_PARALLEL -- /nologo /verbosity:minimal

      - name: Test
        uses: nick-fields/retry@v2
        with:
          max_attempts: 3
          timeout_minutes: 20
          shell: powershell
          command: |
            $EVENT_TESTS_PARALLEL=1

            cd build

            try {
              if ("${{ matrix.EVENT_MATRIX }}" -eq "TEST_EXPORT_STATIC") {
                python ../test-export/test-export.py static
              } elseif ("${{ matrix.EVENT_MATRIX }}" -eq "TEST_EXPORT_SHARED") {
                python ../test-export/test-export.py shared
              } else {
                ctest --output-on-failure -j $EVENT_TESTS_PARALLEL
                if ($LastExitCode -ne 0) { $host.SetShouldExit($LastExitCode) }
              }
            } catch {
              $host.SetShouldExit($LastExitCode)
            }

      - uses: actions/upload-artifact@v1
        if: failure()
        with:
          name: ${{ matrix.os }}-${{ matrix.EVENT_MATRIX }}-build
          path: build

  mingw-autotools-job:
    runs-on: windows-2019
    if: "!contains(github.event.head_commit.message, 'ci/mingw skip') && !contains(github.event.head_commit.message, 'ci/mingw/autotools skip')"
    strategy:
      fail-fast: false
      matrix:
        EVENT_MATRIX:
          - none
          - no-ssl
          - disable-openssl
          - disable-thread-support
          - disable-debug-mode
          - disable-malloc-replacement

    steps:
      - uses: actions/checkout@v2.0.0
      - name: Cache Build
        uses: actions/cache@v2
        with:
          path: build
          key: mingw-autotools-${{ matrix.EVENT_MATRIX }}-v5
      - name: Setup MSYS2
        uses: msys2/setup-msys2@v2
        with:
          msystem: MINGW64
          update: true
          install: mingw-w64-x86_64-gcc autoconf automake libtool mingw-w64-x86_64-openssl mingw-w64-x86_64-mbedtls pkg-config

      - name: Build
        shell: powershell
        run: |
          $env:EVENT_CONFIGURE_OPTIONS=""
          if ( "${{ matrix.EVENT_MATRIX }}" -eq "no-ssl" ) {
            $env:EVENT_CONFIGURE_OPTIONS="--disable-openssl --disable-mbedtls"
          }
          elseif ( "${{ matrix.EVENT_MATRIX }}" -ne "none" ) {
            $env:EVENT_CONFIGURE_OPTIONS="--${{ matrix.EVENT_MATRIX }}"
          }
          $env:EVENT_BUILD_PARALLEL=10

          $script='
          export PATH="/mingw64/bin:/usr/bin:/bin:/usr/local/bin:/opt/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl:$PATH"
          ./autogen.sh 2>&1 3>&1
          [[ $? -ne 0 ]] && exit 1
          mkdir -p build
          cd build
          [[ $? -ne 0 ]] && exit 1
          LDFLAGS="-L/mingw64/lib" CFLAGS="-I/mingw64/include" ../configure $EVENT_CONFIGURE_OPTIONS 2>&1
          [[ $? -ne 0 ]] && exit 1
          make -j $EVENT_BUILD_PARALLEL 2>&1
          [[ $? -ne 0 ]] && exit 1
          exit 0
          '
          D:\a\_temp\msys64\usr\bin\bash.exe -c $script

      - name: Test
        uses: nick-fields/retry@v2
        with:
          max_attempts: 3
          timeout_minutes: 20
          shell: powershell
          command: |
            $env:EVENT_TESTS_PARALLEL=1

            $script='
            export PATH="/mingw64/bin:/usr/bin:/bin:/usr/local/bin:/opt/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl:$PATH"
            cd build
            make verify -j $EVENT_TESTS_PARALLEL 2>&1 '
            D:\a\_temp\msys64\usr\bin\bash.exe -c $script

      - uses: actions/upload-artifact@v1
        if: failure()
        with:
          name: mingw-${{ matrix.EVENT_MATRIX }}-build
          path: build

  mingw-cmake-job:
    runs-on: windows-2019
    # FIXME: disabled for now, see [1].
    #   [1]: https://github.com/libevent/libevent/issues/1207
    if: "false && !contains(github.event.head_commit.message, 'ci/mingw skip') && !contains(github.event.head_commit.message, 'ci/mingw/cmake skip')"
    strategy:
      fail-fast: false
      matrix:
        EVENT_MATRIX:
          - NONE
          - NO_SSL
          - DISABLE_OPENSSL
          - DISABLE_THREAD_SUPPORT
          - DISABLE_DEBUG_MODE
          - DISABLE_MM_REPLACEMENT

    steps:
      - uses: actions/checkout@v2.0.0
      - name: Cache Build
        uses: actions/cache@v2
        with:
          path: build
          key: mingw-cmake-${{ matrix.EVENT_MATRIX }}-v4

      - name: Setup MSYS2
        uses: msys2/setup-msys2@v2
        with:
          msystem: MINGW64
          update: true
          install: mingw-w64-x86_64-gcc mingw-w64-x86_64-openssl mingw-w64-x86_64-mbedtls  

      - name: Build
        shell: powershell
        run: |
          $EVENT_CONFIGURE_OPTIONS=""
          if ( "${{ matrix.EVENT_MATRIX }}" -ne "NO_SSL" ) {
            $EVENT_CONFIGURE_OPTIONS="-DEVENT__DISABLE_OPENSSL=ON -DEVENT__DISABLE_MBEDTLS=ON"
          }
          elseif ( "${{ matrix.EVENT_MATRIX }}" -ne "NONE" ) {
            $EVENT_CONFIGURE_OPTIONS="-DEVENT__${{ matrix.EVENT_MATRIX }}=ON"
          }
          $env:PATH="D:\a\_temp\msys64\mingw64\bin;D:\a\_temp\msys64\usr\bin;$env:PATH"
          mkdir build -ea 0
          cd build
          function cmake_configure($retry)
          {
            $errcode=0
            try {
              cmake .. -G "MSYS Makefiles" $EVENT_CONFIGURE_OPTIONS -DCMAKE_C_FLAGS=-w
              $errcode=$LastExitCode
            }
            catch {
              $errcode=1
            }
            finally {
              if ($errcode -ne 0) {
                if ($retry -eq 0) {
                  $host.SetShouldExit($LastExitCode)
                } else {
                  echo "Remove all entries in build directory"
                  rm -r -fo *
                  cmake_configure 0
                }
              }
            }
          }
          cmake_configure 1
          cmake --build .

      - name: Test
        uses: nick-fields/retry@v2
        with:
          max_attempts: 3
          timeout_minutes: 20
          shell: powershell
          command: |
            cd build
            ctest --output-on-failure

      - uses: actions/upload-artifact@v1
        if: failure()
        with:
          name: mingw-${{ matrix.EVENT_MATRIX }}-build
          path: build

  macos-cmake-job:
    runs-on: ${{ matrix.os }}
    if: "!contains(github.event.head_commit.message, 'ci/macos skip') && !contains(github.event.head_commit.message, 'ci/macos/cmake skip')"
    strategy:
      fail-fast: false
      matrix:
        os: [macos-latest]
        EVENT_MATRIX:
          - NONE
          - NO_SSL
          - DISABLE_OPENSSL
          - DISABLE_THREAD_SUPPORT
          - DISABLE_DEBUG_MODE
          - DISABLE_MM_REPLACEMENT
          - TEST_EXPORT_STATIC
          - TEST_EXPORT_SHARED

    steps:
      - uses: actions/checkout@v2.0.0

      - name: Cache Build
        uses: actions/cache@v2
        with:
          path: build
          key: macos-10.15-cmake-${{ matrix.EVENT_MATRIX }}-v3

      - name: Install Depends
        run: brew install mbedtls@2

      - name: Build
        shell: bash
        run: |
          # NOTE: cmake does not support autodetection of OPENSSL_ROOT_DIR via brew
          export OPENSSL_ROOT_DIR=/usr/local/opt/openssl

          if [ "${{ matrix.EVENT_MATRIX }}" == "DISABLE_OPENSSL" ]; then
            EVENT_CMAKE_OPTIONS="-DEVENT__DISABLE_OPENSSL=ON"

          elif [ "${{ matrix.EVENT_MATRIX }}" == "NO_SSL" ]; then
            EVENT_CMAKE_OPTIONS="-DEVENT__DISABLE_OPENSSL=ON -DEVENT__DISABLE_MBEDTLS=ON"

          elif [ "${{ matrix.EVENT_MATRIX }}" == "DISABLE_THREAD_SUPPORT" ]; then
            EVENT_CMAKE_OPTIONS="-DEVENT__DISABLE_THREAD_SUPPORT=ON"

          elif [ "${{ matrix.EVENT_MATRIX }}" == "DISABLE_DEBUG_MODE" ]; then
            EVENT_CMAKE_OPTIONS="-DEVENT__DISABLE_DEBUG_MODE=ON"

          elif [ "${{ matrix.EVENT_MATRIX }}" == "DISABLE_MM_REPLACEMENT" ]; then
            EVENT_CMAKE_OPTIONS="-DEVENT__DISABLE_MM_REPLACEMENT=ON"

          elif [ "${{ matrix.EVENT_MATRIX }}" == "TEST_EXPORT_STATIC" ]; then
            EVENT_CMAKE_OPTIONS="-DEVENT__LIBRARY_TYPE=STATIC -DEVENT__DISABLE_TESTS=ON -DEVENT__DISABLE_SAMPLES=ON"

          elif [ "${{ matrix.EVENT_MATRIX }}" == "TEST_EXPORT_SHARED" ]; then
            EVENT_CMAKE_OPTIONS="-DEVENT__LIBRARY_TYPE=SHARED -DEVENT__DISABLE_TESTS=ON -DEVENT__DISABLE_SAMPLES=ON"

          else
            EVENT_CMAKE_OPTIONS=""
          fi
          EVENT_CMAKE_OPTIONS="$EVENT_CMAKE_OPTIONS -DMBEDTLS_ROOT_DIR=/usr/local/opt/mbedtls@2"

          mkdir -p build
          cd build
          echo [cmake]: cmake .. $EVENT_CMAKE_OPTIONS
          cmake .. $EVENT_CMAKE_OPTIONS || (rm -rf * && cmake .. $EVENT_CMAKE_OPTIONS)
          cmake --build .

      - name: Test
        uses: nick-fields/retry@v2
        with:
          max_attempts: 3
          timeout_minutes: 20
          shell: bash
          command: |
            JOBS=1
            export CTEST_PARALLEL_LEVEL=$JOBS
            export CTEST_OUTPUT_ON_FAILURE=1

            cd build

            if [ "${{ matrix.EVENT_MATRIX }}" == "TEST_EXPORT_STATIC" ]; then
              sudo python3 ../test-export/test-export.py static
            elif [ "${{ matrix.EVENT_MATRIX }}" == "TEST_EXPORT_SHARED" ]; then
              sudo python3 ../test-export/test-export.py shared
            else
              cmake --build . --target verify
            fi

      - uses: actions/upload-artifact@v1
        if: failure()
        with:
          name: ${{ matrix.os }}-cmake-${{ matrix.EVENT_MATRIX }}-build
          path: build

  macos-autotools-job:
    runs-on: ${{ matrix.os }}
    if: "!contains(github.event.head_commit.message, 'ci/macos skip') && !contains(github.event.head_commit.message, 'ci/macos/autotools skip')"
    strategy:
      fail-fast: false
      matrix:
        os: [macos-latest]
        EVENT_MATRIX:
          - NONE
          - NO_SSL
          - DISABLE_OPENSSL
          - DISABLE_THREAD_SUPPORT
          - DISABLE_DEBUG_MODE
          - DISABLE_MM_REPLACEMENT

    steps:
      - uses: actions/checkout@v2.0.0

      - name: Cache Build
        uses: actions/cache@v2
        with:
          path: build
          key: ${{ matrix.os }}-autotools-${{ matrix.EVENT_MATRIX }}-v3

      - name: Install Depends
        run: brew install autoconf automake libtool pkg-config mbedtls@2

      - name: Build
        shell: bash
        run: |
          export CPPFLAGS="-I/usr/local/opt/mbedtls@2/include"
          export  LDFLAGS="-L/usr/local/opt/mbedtls@2/lib"

          if [ "${{ matrix.EVENT_MATRIX }}" == "DISABLE_OPENSSL" ]; then
            EVENT_CONFIGURE_OPTIONS="--disable-openssl"

          elif [ "${{ matrix.EVENT_MATRIX }}" == "NO_SSL" ]; then
            EVENT_CONFIGURE_OPTIONS="--disable-openssl --disable-mbedtls"

          elif [ "${{ matrix.EVENT_MATRIX }}" == "DISABLE_THREAD_SUPPORT" ]; then
            EVENT_CONFIGURE_OPTIONS="--disable-thread-support"

          elif [ "${{ matrix.EVENT_MATRIX }}" == "DISABLE_DEBUG_MODE" ]; then
            EVENT_CONFIGURE_OPTIONS="--disable-debug-mode"

          elif [ "${{ matrix.EVENT_MATRIX }}" == "DISABLE_MM_REPLACEMENT" ]; then
            EVENT_CONFIGURE_OPTIONS="--disable-malloc-replacement"

          else
            EVENT_CONFIGURE_OPTIONS=""
          fi

          ./autogen.sh
          mkdir -p build
          cd build
          echo [configure]: ../configure $EVENT_CONFIGURE_OPTIONS
          ../configure $EVENT_CONFIGURE_OPTIONS
          make

      - name: Test
        uses: nick-fields/retry@v2
        with:
          max_attempts: 3
          timeout_minutes: 20
          shell: bash
          command: |
            JOBS=1
            cd build
            make -j $JOBS verify

      - uses: actions/upload-artifact@v1
        if: failure()
        with:
          name: ${{ matrix.os }}-autotools-${{ matrix.EVENT_MATRIX }}-build
          path: build