summaryrefslogtreecommitdiff
path: root/gitlab/ci_template.yml
blob: 066a237327c6b14c241ff55cfe6c3c10f0594f6e (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
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
include:
  - remote: "https://gitlab.freedesktop.org/freedesktop/ci-templates/-/raw/7ea696055e322cc7aa4bcbe5422b56a198c4bdff/templates/ci-fairy.yml"

stages:
  - 'build docker'
  - 'preparation'
  - 'pre-build'
  - 'build'
  - 'test'
  # Use the resulting binaries
  - 'integrate'

variables:
  # Branch to track for modules that have no ref specified in the manifest
  GST_UPSTREAM_BRANCH: 'master'
  ORC_UPSTREAM_BRANCH: 'master'

  ###
  # IMPORTANT
  # These are the version tags for the docker images the CI runs against.
  # If you are hacking on them or need a them to rebuild, its enough
  # to change any part of the string of the image you want.
  ###
  FEDORA_TAG:  '2021-01-22.0'
  CERBERO_TAG: '2020-10-22.0'
  ANDROID_TAG: '2020-10-22.0'
  MANIFEST_TAG: '2020-10-22.0'
  TEST_MANIFEST_TAG: '2020-10-22.0'
  INDENT_TAG: '2020-10-22.0'
  WINDOWS_TAG: "2021-02-24.0"

  GST_UPSTREAM_REPO: 'gstreamer/gst-ci'

  FEDORA_AMD64_SUFFIX:  'amd64/fedora'
  CERBERO_AMD64_SUFFIX: 'amd64/cerbero-fedora'
  ANDROID_AMD64_SUFFIX: 'amd64/android-fedora'
  MANIFEST_AMD64_SUFFIX: 'amd64/build-manifest'
  TEST_MANIFEST_AMD64_SUFFIX: 'amd64/test-manifest'
  INDENT_AMD64_SUFFIX: 'amd64/gst-indent'
  WINDOWS_AMD64_SUFFIX: 'amd64/windows'

  FEDORA_IMAGE:   "$CI_REGISTRY_IMAGE/$FEDORA_AMD64_SUFFIX:$FEDORA_TAG-$GST_UPSTREAM_BRANCH"
  FEDORA_DOCS_IMAGE: "registry.freedesktop.org/gstreamer/gst-ci/amd64/fedora:2020-07-03.0-master"
  CERBERO_IMAGE:  "$CI_REGISTRY_IMAGE/$CERBERO_AMD64_SUFFIX:$CERBERO_TAG-$GST_UPSTREAM_BRANCH"
  ANDROID_IMAGE:  "$CI_REGISTRY_IMAGE/$ANDROID_AMD64_SUFFIX:$ANDROID_TAG-$GST_UPSTREAM_BRANCH"
  MANIFEST_IMAGE: "$CI_REGISTRY_IMAGE/$MANIFEST_AMD64_SUFFIX:$MANIFEST_TAG-$GST_UPSTREAM_BRANCH"
  TEST_MANIFEST_IMAGE: "$CI_REGISTRY_IMAGE/$TEST_MANIFEST_AMD64_SUFFIX:$TEST_MANIFEST_TAG-$GST_UPSTREAM_BRANCH"
  INDENT_IMAGE:   "$CI_REGISTRY_IMAGE/$INDENT_AMD64_SUFFIX:$INDENT_TAG-$GST_UPSTREAM_BRANCH"
  WINDOWS_IMAGE: "$CI_REGISTRY_IMAGE/$WINDOWS_AMD64_SUFFIX:$WINDOWS_TAG-$GST_UPSTREAM_BRANCH"
  WINDOWS_UPSTREAM_IMAGE: "$CI_REGISTRY/$GST_UPSTREAM_REPO/$WINDOWS_AMD64_SUFFIX:$WINDOWS_TAG-$GST_UPSTREAM_BRANCH"

  # Can't use $CI_* variables since we use this template in all repos, not just gst-ci
  CERBERO_SCRIPT_URL: "https://gitlab.freedesktop.org/gstreamer/gst-ci/raw/${GST_UPSTREAM_BRANCH}/gitlab/cerbero_setup.sh"
  WINDOWS_CONTAINER_SCRIPT_URL: "https://gitlab.freedesktop.org/gstreamer/gst-ci/raw/${GST_UPSTREAM_BRANCH}/docker/windows/container.ps1"

  GIT_STRATEGY: none
  MESON_BUILDTYPE_ARGS: --default-library=both
  DEFAULT_MESON_ARGS: >
    -Dlibnice:tests=disabled
    -Dlibnice:examples=disabled
    -Dopenh264:tests=disabled
    -Dpygobject:tests=false
    -Dpython=enabled
    -Dlibav=enabled
    -Dugly=enabled
    -Dbad=enabled
    -Ddevtools=enabled
    -Dges=enabled
    -Drtsp_server=enabled
    -Dvaapi=enabled
    -Dsharp=disabled

  MESON_GST_WERROR: >
    -Dgstreamer:werror=true
    -Dgst-plugins-base:werror=true
    -Dgst-plugins-good:werror=true
    -Dgst-plugins-ugly:werror=true
    -Dgst-plugins-bad:werror=true
    -Dgst-rtsp-server:werror=true
    -Dgst-libav:werror=true
    -Dgst-examples:werror=true
    -Dgst-editing-services:werror=true
    -Dgst-docs:werror=true
    -Dgst-omx:werror=true
    -Dgst-devtools:werror=true
    -Dgst-python:werror=true
    -Dgstreamer-vaapi:werror=true
    -Dgstreamer-sharp:werror=true

  DEFAULT_CERBERO_ARGS: >
    --variants werror
    --timestamps

workflow:
  rules:
    - if: $CI_MERGE_REQUEST_IID
    # don't create a pipeline if its a commit pipeline, on a branch and that branch has
    # open merge requests (bc we will get a MR build instead)
    - if: $CI_OPEN_MERGE_REQUESTS
      when: never
    - if: $CI_COMMIT_TAG
    - if: $CI_COMMIT_BRANCH

#
# Global CI policy
#
# This can be used to configure global behaviour our our jobs.
#
default:
  retry:
    max: 2
    when:
      - 'runner_system_failure'
      - 'stuck_or_timeout_failure'
      - 'scheduler_failure'
      - 'api_failure'
  interruptible: true


# Script to check if a docker image exists in the upstream registry
# and if so copy it to the forked registry so we can use it
#
# This is copied/adapted from citemplates/templates/fedora.yml
# https://gitlab.freedesktop.org/freedesktop/ci-templates/-/blob/96b621fe9f57ec2464f8d1a0940446afbf6c8f59/templates/fedora.yml
.check_image: &check_image_exists |
  # if-not-exists steps
  set -x

  if [[ -z "$GST_FORCE_REBUILD" ]]
  then
    # disable exit on failure
    set +e

    # check if our image is already in the current registry
    # we store the sha of the digest and the layers
    skopeo inspect docker://$LOCAL_IMAGE | jq '[.Digest, .Layers]' > local_sha

    # check if our image is already in our gst-ci registry fork
    skopeo inspect docker://$LOCAL_GST_CI_IMAGE | jq '[.Digest, .Layers]' > local_gst_ci_sha

    # check if our image is already in the upstream registry
    if [[ -z "$GST_UPSTREAM_REPO" ]]
    then
      echo "WARNING! Variable \$GST_UPSTREAM_REPO is undefined, cannot check for images"
    else
      skopeo inspect docker://$GST_UPSTREAM_IMAGE | jq '[.Digest, .Layers]' > upstream_sha
    fi

    # reenable exit on failure
    set -e

    # if the upstream repo has an image, ensure we use the same
    if [ -s upstream_sha ]
    then

      echo "Checking if $LOCAL_IMAGE is up to date"
      # ensure we use the same image from upstream
      diff upstream_sha local_sha && exit 0 || true

      echo "Copying image from gstreamer/gst-ci to local registry"
      # copy the original image into the current project registry namespace
      # we do 2 attempts with skopeo copy at most
      skopeo copy --dest-creds $CI_REGISTRY_USER:$CI_REGISTRY_PASSWORD \
                  docker://$GST_UPSTREAM_IMAGE \
                  docker://$LOCAL_IMAGE || \
      skopeo copy --dest-creds $CI_REGISTRY_USER:$CI_REGISTRY_PASSWORD \
                  docker://$GST_UPSTREAM_IMAGE \
                  docker://$LOCAL_IMAGE

      exit 0
    fi

    set +x

    # if the local ci fork repo has an image, ensure we use the same
    if [ -s local_gst_ci_sha ]
    then
      echo "Checking if $LOCAL_GST_CI_IMAGE is up to date"
      # ensure we use the same image from upstream
      diff local_gst_ci_sha local_sha && exit 0 || true

      echo "Copying image from $CI_PROJECT_NAMESPACE/gst-ci to local registry"
      # copy the original image into the current project registry namespace
      # we do 2 attempts with skopeo copy at most
      skopeo copy --dest-creds $CI_REGISTRY_USER:$CI_REGISTRY_PASSWORD \
                  docker://$LOCAL_GST_CI_IMAGE \
                  docker://$LOCAL_IMAGE || \
      skopeo copy --dest-creds $CI_REGISTRY_USER:$CI_REGISTRY_PASSWORD \
                  docker://$LOCAL_GST_CI_IMAGE \
                  docker://$LOCAL_IMAGE

      exit 0
    fi

    # if we have a local image but none in the upstream repo, use our
    if [ -s local_sha ]
    then
      echo "Using $LOCAL_IMAGE"
      exit 0
    fi
  fi


# Build docker images from Dockerfiles
#
# This is copied/adapted from citemplates/templates/fedora.yml
# https://gitlab.freedesktop.org/freedesktop/ci-templates/-/blob/96b621fe9f57ec2464f8d1a0940446afbf6c8f59/templates/fedora.yml
#
# CITemplates builds uses buildah run/commit workflow to build the images which doesn't
# fit us atm and our images need further adjustment to make use of it. Porting to
# ci-templates is the next step though.
#
# All the documentation from citemplates should be applicable here, so please refer there
.base:
  image: "$CI_REGISTRY/freedesktop/ci-templates/buildah:2020-07-20.1"
  variables:
    STORAGE_DRIVER: 'vfs'
    BUILDAH_FORMAT: 'docker'
    BUILDAH_ISOLATION: 'chroot'

    GIT_STRATEGY: fetch
  script:
    - export LOCAL_IMAGE="$CI_REGISTRY_IMAGE/$REPO_SUFFIX:$TAG"
    - export LOCAL_GST_CI_IMAGE="$CI_REGISTRY/$CI_PROJECT_NAMESPACE/gst-ci/$REPO_SUFFIX:$TAG"
    - export GST_UPSTREAM_IMAGE="$CI_REGISTRY/$GST_UPSTREAM_REPO/$REPO_SUFFIX:$TAG"

    # check if the docker registry is enabled, else the variable will be missing
    - |
      if [[ -z "$CI_REGISTRY_IMAGE" ]]
      then
        echo "ERROR! Looks like your repository/fork has disabled Docker Registries."
        echo "Pleae enable them by following the documentation from here:"
        echo "https://docs.gitlab.com/ee/user/packages/container_registry/#enable-the-container-registry-for-your-project"
        exit 1
      fi

    # Newer versions of podman/buildah try to set overlayfs mount options when
    # using the vfs driver, and this causes errors.
    - sed -i '/^mountopt =.*/d' /etc/containers/storage.conf

    - *check_image_exists

    # This part of the job should only ever be possible to be reach in a gst-ci fork
    - |
      if [[ "$CI_PROJECT_NAME" != 'gst-ci' ]]
      then
        echo "ERROR! Something is wrong!"
        echo "This part of the job is supposed to be executed only in gst-ci forks, and its probably going to fail else"
        exit 1
      fi

    - echo "Building image $LOCAL_IMAGE"

    - >
      buildah bud
      --build-arg DEFAULT_BRANCH=$GST_UPSTREAM_BRANCH
      --label ci.job_id=$CI_JOB_ID
      --label pipeline.url=$CI_PIPELINE_URL
      --label git.ref_name=$CI_COMMIT_REF_NAME
      --label git.sha=$CI_COMMIT_SHA
      --label gitlab.project_path=$CI_PROJECT_PATH
      --label gitlab.project_url=$CI_PROJECT_URL
      --label fdo.upstream-repo=$GST_UPSTREAM_REPO
      -f $DOCKERFILE
      -t $LOCAL_IMAGE
      $CONTEXT_DIR

    - buildah login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY

    # Sanity check
    - |
      if [ "$CI_PROJECT_PATH" = "gstreamer/gst-ci" ]; then
        if [ "$LOCAL_IMAGE" != "$GST_UPSTREAM_IMAGE" ]; then
          echo "ERROR!!! AAAAA! THE IMAGE URI DOES NOT MATCH THE EXPECTED UPSTREAM ONE"
          echo $LOCAL_IMAGE
          echo $GST_UPSTREAM_IMAGE
          exit 1
        fi
      fi

    -  buildah push $LOCAL_IMAGE

fedora amd64 docker:
  stage: "build docker"
  variables:
    REPO_SUFFIX: "$FEDORA_AMD64_SUFFIX"
    TAG: "$FEDORA_TAG-$GST_UPSTREAM_BRANCH"

    CONTEXT_DIR: "./docker/fedora/"
    DOCKERFILE: "docker/fedora/Dockerfile"
  extends: .base

cerbero fedora amd64 docker:
  stage: "build docker"
  variables:
    REPO_SUFFIX: "$CERBERO_AMD64_SUFFIX"
    TAG: "$CERBERO_TAG-$GST_UPSTREAM_BRANCH"

    CONTEXT_DIR: "docker/cerbero/"
    DOCKERFILE: "docker/cerbero/Dockerfile-fedora"
  extends: .base

android docker:
  stage: "build docker"
  variables:
    REPO_SUFFIX: "$ANDROID_AMD64_SUFFIX"
    TAG: "$ANDROID_TAG-$GST_UPSTREAM_BRANCH"

    CONTEXT_DIR: "docker/android/"
    DOCKERFILE: "docker/android/Dockerfile"
  extends: .base

alpine amd64 manifest builder docker:
  stage: "build docker"
  variables:
    REPO_SUFFIX: "$MANIFEST_AMD64_SUFFIX"
    TAG: '$MANIFEST_TAG-$GST_UPSTREAM_BRANCH'

    CONTEXT_DIR: "docker/build_manifest/"
    DOCKERFILE: "docker/build_manifest/Dockerfile"
  extends: .base

gst-indent amd64 docker:
  stage: "build docker"
  variables:
    REPO_SUFFIX: "$INDENT_AMD64_SUFFIX"
    TAG: "$INDENT_TAG-$GST_UPSTREAM_BRANCH"

    CONTEXT_DIR: "docker/indent/"
    DOCKERFILE: "docker/indent/Dockerfile"
  extends: .base

windows amd64 docker:
  stage: "build docker"
  variables:
    GIT_STRATEGY: fetch
    REPO_SUFFIX: "$WINDOWS_AMD64_SUFFIX"
    TAG: "$WINDOWS_TAG-$GST_UPSTREAM_BRANCH"

    # Unlike the buildah/linux jobs, this file
    # needs to be relative to docker/windows/ subdir
    # as it makes life easier in the powershell script
    #
    # We also don't need a CONTEXT_DIR var as its also
    # hardcoded to be docker/windows/
    DOCKERFILE: "Dockerfile"
  tags:
    - windows
    - shell
    - "1809"
  script:
    - $env:WINDOWS_CONTAINER_SCRIPT_PATH = "$env:CI_PROJECT_DIR\container.ps1"
    - echo "Fetching $env:WINDOWS_CONTAINER_SCRIPT_URL"
    - Invoke-WebRequest -Uri $env:WINDOWS_CONTAINER_SCRIPT_URL -OutFile $env:WINDOWS_CONTAINER_SCRIPT_PATH

    - "& $env:WINDOWS_CONTAINER_SCRIPT_PATH $CI_REGISTRY $CI_REGISTRY_USER $CI_REGISTRY_PASSWORD $WINDOWS_IMAGE $WINDOWS_UPSTREAM_IMAGE $DOCKERFILE"
    - |
      if (!($?)) {
        echo "Failed to build the image"
        Exit 1
      }

#
# Job to create the manifest.xml to used for our builds
#
manifest:
  image: $MANIFEST_IMAGE
  rules:
    # Always have automatic pipeline for branchs in cerbero and docs
    # cause they need to update artifacts, like the docs site or cerbero deps
    - if: '$CI_PROJECT_PATH == "gstreamer/cerbero"'
    - if: '$CI_PROJECT_PATH == "gstreamer/gst-docs"'

    # If the MR is assigned to the Merge bot, trigger the pipeline automatically
    - if: '$CI_MERGE_REQUEST_ASSIGNEES == "gstreamer-merge-bot"'
    # When the assignee isn't the merge bot, require an explicit action to trigger the pipeline
    # to avoid wasting CI resources
    - if: '$CI_MERGE_REQUEST_ASSIGNEES != "gstreamer-merge-bot"'
      when: 'manual'
    # If this matches, it means the pipeline is running against either the main
    # or a stable branch, so make it manual
    - if: '$CI_COMMIT_BRANCH == $GST_UPSTREAM_BRANCH'
      when: 'manual'

  stage: 'preparation'
  script:
    - cd /gst-ci
    - gitlab/build_manifest.py --self-update
    - gitlab/build_manifest.py ${CI_PROJECT_DIR}/manifest.xml
    - cat ${CI_PROJECT_DIR}/manifest.xml
  artifacts:
    expire_in: "7 days"
    paths:
      - "manifest.xml"

#
# gst-indent!!
#
gst indent:
  image: $INDENT_IMAGE
  stage: 'preparation'
  variables:
    GIT_STRATEGY: 'fetch'
  rules:
    - if: '$CI_PROJECT_NAME !~ /^(gstreamer-sharp|gst-integration-testsuites|cerbero|gst-docs)$/'
  script:
    # man indent. grep RETURN VALUE, grab a beer on my behalf...
    - indent --version || true
    - |
      filter_cmd=("cat")
      if test -f ".indentignore"; then
          filter_args=()
          while read -r line; do
              if test -n "$line"; then
                  filter_args+=("-e" "$line")
              fi
          done < ".indentignore"
          if [[ ${#filter_args[@]} -gt 0 ]]; then
              filter_cmd=("grep" "-v" "${filter_args[@]}")
          fi
      fi
      find . -name '*.c' | "${filter_cmd[@]}" | xargs -d '\n' gst-indent
    - |
      if git diff --quiet; then
          echo "Code is properly formatted"
      else
          git diff --color=always
          echo 'style diverges, please run gst-indent first'
          exit 1
      fi

#
# Check "allow-edit" checkbox on merge requests with ci-fairy
#
check allow-edit:
  extends: '.fdo.ci-fairy'
  needs: []
  stage: 'preparation'
  variables:
    GIT_STRATEGY: 'none'
  script:
    - ci-fairy check-merge-request --require-allow-collaboration
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'

#
# gst-build setup templates
#
.gst_build_template: &gst_build
  - echo $MESON_ARGS
  # Sometimes, gitlab-runner want to reuse
  # existing docker volumes without cleaning them up...
  # Make sure the docker volume is clean
  - rm -rf gst-build || true
  - rm -rf meson-logs || true
  - rm -rf validate-logs || true

  - curl -L -o clone_manifest_ref.py "https://gitlab.freedesktop.org/gstreamer/gst-ci/raw/${GST_UPSTREAM_BRANCH}/gitlab/clone_manifest_ref.py"
  - python3 clone_manifest_ref.py --manifest manifest.xml --project gst-build --destination gst-build
  - cd gst-build/
  # only copy immediate directories. We don't want to modify .wrap files
  - find /gst-build/subprojects/ -mindepth 1 -maxdepth 1 -type d -exec cp -r \{\} subprojects/ \;

  # Reset the state of the subprojects to what gst-build expects
  - meson subprojects update --reset
  - ./git-update --no-interaction --manifest="${CI_PROJECT_DIR}/manifest.xml"

  - meson build/ $MESON_ARGS
  - ninja -C build/
  - ccache --show-stats

.gst_build_ccache_vars:
  variables:
    CCACHE_COMPILERCHECK: 'content'
    CCACHE_COMPRESS: 'true'
    CCACHE_BASEDIR: '/cache/gstreamer/gst-build'
    CCACHE_DIR: '/cache/gstreamer/gst-build/ccache/'
    # shared across everything really
    CCACHE_MAXSIZE: '10G'
    CARGO_HOME: '/cache/gstreamer/cargo'

.simple_fedora_build: &simple_build >-
  ${DEFAULT_MESON_ARGS}
  -Dsharp=enabled
  -Domx=enabled
  -Dgst-omx:target=generic
  -Ddoc=disabled
  -Drs=disabled
  ${MESON_BUILDTYPE_ARGS}
  ${MESON_GST_WERROR}

.build:
  stage: 'build'
  extends:
    - '.gst_build_ccache_vars'
  needs:
    - "manifest"
  # Taking into account the slowest shared runner + time needed to upload the binaries to artifacts
  # Also need to take into account I/O of pulling docker images and uploading artifacts
  timeout: '45min'
  variables:
    MESON_ARGS: "${DEFAULT_MESON_ARGS} ${MESON_BUILDTYPE_ARGS} ${MESON_GST_WERROR}"
  rules:
    - if: '$CI_PROJECT_NAME != "cerbero"'
  script:
    *gst_build
  after_script:
    - mv gst-build/build/meson-logs/ meson-logs
    # Cleanup everything else to reduce the size
    # of the docker volume we leave behind
    - rm -rf gst-build
  artifacts:
    expire_in: "7 days"
    when: "always"
    paths:
      - 'meson-logs/'
      - 'manifest.xml'

.build fedora x86_64:
  extends: '.build'
  image: $FEDORA_IMAGE
  variables:
    MESON_ARGS: *simple_build
  rules:
    - if: '$CI_PROJECT_NAME !~ /^(cerbero|gst-omx|gst-plugins-rs)$/'

build nodebug fedora x86_64:
  extends: '.build'
  image: $FEDORA_IMAGE
  variables:
    MESON_ARGS: "${DEFAULT_MESON_ARGS} -Dsharp=enabled -Dgstreamer:gst_debug=false -Domx=enabled -Dgst-omx:target=generic ${MESON_BUILDTYPE_ARGS} ${MESON_GST_WERROR}"
  rules:
    - if: '$CI_PROJECT_NAME !~ /^(cerbero|gst-docs|gst-integration-testsuites|gst-omx|gst-plugins-rs)$/'

build static fedora x86_64:
  extends: '.build fedora x86_64'
  variables:
    MESON_BUILDTYPE_ARGS: "--default-library=static -Dintrospection=disabled -Ddoc=disabled"
  rules:
    - if: '$CI_PROJECT_NAME !~ /^(cerbero|gst-docs|gst-integration-testsuites|gst-omx|gst-plugins-rs)$/'

build static nodebug fedora x86_64:
  extends: 'build nodebug fedora x86_64'
  variables:
    MESON_BUILDTYPE_ARGS: "--default-library=static -Dintrospection=disabled -Ddoc=disabled"
  rules:
    - if: '$CI_PROJECT_NAME !~ /^(cerbero|gst-docs|gst-integration-testsuites|gst-omx|gst-plugins-rs)$/'

build clang fedora x86_64:
  extends: '.build fedora x86_64'
  variables:
    CC: 'ccache clang'
    CXX: 'ccache clang++'
  rules:
    - if: '$CI_PROJECT_NAME !~ /^(cerbero|gst-docs|gst-integration-testsuites|gst-omx|gst-plugins-rs)$/'

.test:
  stage: 'test'
  extends:
    - '.gst_build_ccache_vars'
  needs:
    - "manifest"
  variables:
    MESON_ARGS: *simple_build

    # Disable colored output to avoid weird rendering issues
    GST_DEBUG_NO_COLOR: "true"
    CI_ARTIFACTS_URL: "${CI_PROJECT_URL}/-/jobs/${CI_JOB_ID}/artifacts/raw/validate-logs/"
    GST_VALIDATE_LAUNCHER_FORCE_COLORS: "true"
    TIMEOUT_FACTOR: "2"
    CARGO_HOME: "/cache/gstreamer/cargo"
    # Enable the fault handler so we get backtraces on segfaults.
    # any non-empty string will do
    PYTHONFAULTHANDLER: "enabled"
  rules:
    - if: '$CI_PROJECT_NAME !~ /^(cerbero|gst-examples|gst-docs)$/'
  script:
    - *gst_build

    - echo "-> Running ${TEST_SUITE}"
    - >
      ./gst-uninstalled.py
      gst-validate-launcher ${TEST_SUITE}
      --dump-on-failure
      --mute
      --shuffle
      --no-display
      --meson-no-rebuild
      --timeout-factor "${TIMEOUT_FACTOR}"
      --fail-on-testlist-change
      -l "${CI_PROJECT_DIR}/validate-logs/"
      --xunit-file "${CI_PROJECT_DIR}/validate-logs/xunit.xml"
      ${EXTRA_VALIDATE_ARGS}
  after_script:
    - mv gst-build/build/meson-logs/ meson-logs
    # Cleanup everything else to reduce the size
    # of the docker volume we leave behind
    - rm -rf gst-build
  artifacts:
    expire_in: '14 days'
    when: always
    paths:
      - 'meson-logs/'
      - 'validate-logs'
      - 'manifest.xml'
    reports:
      junit:
        - "validate-logs/*.xml"

.test fedora x86_64:
  image: $FEDORA_IMAGE
  extends: '.test'
  tags: ['gstreamer']

check fedora:
  extends: '.test fedora x86_64'
  variables:
    TEST_SUITE: "check.gst*"
  rules:
    - if: '$CI_PROJECT_NAME !~ /^(cerbero|gst-docs|gst-integration-testsuites|gst-omx|gst-plugins-rs)$/'

integration testsuites fedora:
  extends: '.test fedora x86_64'
  parallel: 4
  variables:
    EXTRA_VALIDATE_ARGS: "--timeout-factor=2 --retry-on-failures --check-bugs --parts=${CI_NODE_TOTAL} --part-index=${CI_NODE_INDEX}"
    TEST_SUITE: "validate ges"
  rules:
    - if: '$CI_PROJECT_NAME !~ /^(cerbero|gst-docs|gst-examples|gst-omx|gstreamer-sharp|gst-plugins-rs)$/'

# gstreamer-full
gstreamer-full:
  extends: 'build static fedora x86_64'
  stage: integrate
  variables:
    MESON_ARGS: >
      --default-library=static
      -Dauto_features=disabled
      -Dgstreamer:check=enabled
      -Dtests=enabled
      $MESON_GST_WERROR
  rules:
    - if: '$CI_PROJECT_NAME =~ /^(gst-build|gst-ci)$/'

  script:
  - *gst_build
  - meson test -C build -v test-gst-full
  artifacts:
    expire_in: "7 days"
    when: "always"
    paths:
      - 'meson-logs/'

gstreamer-full-minimal:
  extends: 'build static fedora x86_64'
  stage: integrate
  variables:
    MESON_ARGS: >
      --default-library=static
      -Dauto_features=disabled
      -Dgstreamer:check=enabled
      -Dtests=enabled
      -Dgst-plugins-base:alsa=enabled
      -Dgst-plugins-base:typefind=enabled
      -Dgst-plugins-base:pbtypes=enabled
      -Dgst-full-elements=coreelements:filesrc,fakesink,identity,input-selector
      -Dgst-full-typefind-functions=typefindfunctions:wav,flv
      -Dgst-full-device-providers=alsa:alsadeviceprovider
      -Dgst-full-dynamic-types=pbtypes:video_multiview_flagset
      $MESON_GST_WERROR
  rules:
    - if: '$CI_PROJECT_NAME =~ /^(gst-build|gst-ci)$/'

  script:
  - *gst_build
  - meson test -C build -v test-gst-full
  - meson test -C build test-gst-full-features --test-args "-e filesrc,identity,fakesink -E filesink,capsfilter -t audio/x-wav -T video/vivo -d alsadeviceprovider -D v4l2deviceprovider -l GstVideoMultiviewFlagsSet"
  - strip build/libgstreamer-full-1.0.so
  - ls -l build/libgstreamer-full-1.0.so
  artifacts:
    expire_in: "7 days"
    when: "always"
    paths:
      - 'meson-logs/'

# Valgrind
.valgrind fedora x86_64:
  extends: '.test fedora x86_64'
  stage: 'test'
  variables:
    EXTRA_VALIDATE_ARGS: "--valgrind"

valgrind core:
  extends: '.valgrind fedora x86_64'
  variables:
    TEST_SUITE: "check.gstreamer\\..*"
  rules:
    - if: '$CI_PROJECT_NAME =~ /^(gstreamer|gst-ci|gst-devtools)$/'

valgrind base:
  extends: '.valgrind fedora x86_64'
  variables:
    TEST_SUITE: "check.gst-plugins-base\\..*"
  rules:
    - if: '$CI_PROJECT_NAME =~ /^(gstreamer|gst-plugins-base|gst-ci|gst-devtools)$/'

valgrind good:
  extends: '.valgrind fedora x86_64'
  variables:
    TEST_SUITE: "check.gst-plugins-good\\..*"
    # take longer time due to splitmux unit test
    TIMEOUT_FACTOR: "4"
  rules:
    - if: '$CI_PROJECT_NAME =~ /^(gstreamer|gst-plugins-base|gst-plugins-good|gst-ci|gst-devtools)$/'

valgrind ugly:
  extends: '.valgrind fedora x86_64'
  variables:
    TEST_SUITE: "check.gst-plugins-ugly\\..*"
  rules:
    - if: '$CI_PROJECT_NAME =~ /^(gstreamer|gst-plugins-base|gst-plugins-ugly|gst-ci|gst-devtools)$/'

valgrind bad:
  extends: '.valgrind fedora x86_64'
  variables:
    TEST_SUITE: "check.gst-plugins-bad\\..*"
  rules:
    - if: '$CI_PROJECT_NAME =~ /^(gstreamer|gst-plugins-base|gst-plugins-good|gst-plugins-bad|gst-ci|gst-devtools)$/'

valgrind ges:
  extends: '.valgrind fedora x86_64'
  variables:
    TEST_SUITE: "check.gst-editing-services\\..*"
  rules:
    - if: '$CI_PROJECT_NAME =~ /^(gstreamer|gst-plugins-base|gst-plugins-good|gst-editing-services|gst-ci|gst-devtools)$/'

# Template for Cerbero GStreamer Build
#
# Parameters:
# CONFIG: The name of the configuration file to use
# ARCH: The cerbero <os>_<cpu> (used in cache key)
#
.cerbero:
  stage: "build"
  image: $CERBERO_IMAGE
  rules:
    - if: '$CI_PROJECT_NAME == "cerbero"'
  needs:
    - "manifest"
  # Ensure that the runners it will be executed on
  # will have plenty of space for the cache
  tags: ['gstreamer']
  timeout: '3h'
  variables:
    CCACHE_COMPILERCHECK: "content"
    CCACHE_COMPRESS: "true"
    CCACHE_BASEDIR: "/cache/gstreamer/cerbero/"
    CCACHE_DIR: "/cache/gstreamer/cerbero/ccache/"
    # shared across everything really
    CCACHE_MAXSIZE: "50G"

    CERBERO_HOME: "cerbero-build"
    CERBERO_SOURCES: "cerbero-sources"
    CERBERO_ARGS: "${DEFAULT_CERBERO_ARGS}"
    CERBERO: "./cerbero-uninstalled -c config/${CONFIG} -c localconf.cbc -m manifest.xml"
    CERBERO_PACKAGE_ARGS: "-t"
    CERBERO_RUN_WRAPPER: "" # 'wine' on cross-winXX
    CERBERO_RUN_SUFFIX: "" # '.exe' on cross-winXX
    HAVE_CCACHE: "yes"
    # used by macos packages as we only ever install to a fixed directory
    CERBERO_OVERRIDDEN_DIST_DIR: ""
    # location where the cerbero git repo is stored on the image
    CERBERO_HOST_DIR: "/cerbero/"
  before_script:
    - echo "Fetching ${CERBERO_SCRIPT_URL}"
    - curl -L -o cerbero_setup.sh "${CERBERO_SCRIPT_URL}"
    - chmod +x cerbero_setup.sh
    - ./cerbero_setup.sh cerbero_before_script
  script:
    - ./cerbero_setup.sh cerbero_script
  cache:
    key: "${CI_JOB_NAME}"
    paths:
      - "${CERBERO_SOURCES}"
  artifacts:
    name: "${CI_JOB_NAME}_${CI_COMMIT_SHA}"
    expire_in: '5 days'
    when: 'always'
    paths:
      - "manifest.xml"
      - "${CERBERO_HOME}/logs"
      - "*.tar.*"

.build windows:
  image: $WINDOWS_IMAGE
  stage: 'build'
  needs:
    - 'manifest'
  tags:
    - 'docker'
    - 'windows'
    - '1809'
  timeout: '45min'
  variables:
    MESON_ARGS: >
      ${DEFAULT_MESON_ARGS}
      -Dpython=disabled
      -Dlibav=disabled
      -Dvaapi=disabled
      -Dgst-plugins-base:pango=enabled
      -Dgst-plugins-good:cairo=enabled
  rules:
    - if: '$CI_PROJECT_NAME !~ /^(cerbero|gst-docs|gst-integration-testsuites|gst-omx|gst-plugins-rs|gstreamer-vaapi)$/'
  script:
    # Sometimes there's seems to be an existing gst-build clone that comes either from the ether or
    # from a previous job due to some gitlab bug or implicit behavior?
    # So let's always check and clear it out if its there
    # https://gitlab.freedesktop.org/tpm/gstreamer-sharp/-/jobs/1672137
    - if (Test-Path $env:CI_PROJECT_DIR/gst-build) { Remove-Item -Recurse -Force $env:CI_PROJECT_DIR/gst-build }

    # Make sure powershell exists on errors
    # https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_preference_variables?view=powershell-6
    - $ErrorActionPreference = "Stop"

    - Invoke-WebRequest -Uri "https://gitlab.freedesktop.org/gstreamer/gst-ci/raw/$env:GST_UPSTREAM_BRANCH/gitlab/clone_manifest_ref.py" -OutFile $env:CI_PROJECT_DIR/clone_manifest_ref.py
    - python clone_manifest_ref.py --manifest manifest.xml --project gst-build --destination $env:CI_PROJECT_DIR/gst-build
    - cd $env:CI_PROJECT_DIR/gst-build

    # Update meson
    # FIXME: remove this once the windows image is rebuilt/updated
    - pip install -U meson==0.56.2

    - cp -r C:/subprojects/* subprojects/
    - cd $env:CI_PROJECT_DIR/gst-build
    # Reset the subprojects to invalidate the cache if needed
    - meson subprojects update --reset
    # Run the git-update script and feed it the manifest to setup the environment
    - python git-update --no-interaction --manifest=$env:CI_PROJECT_DIR/manifest.xml

    # For some reason, options are separated by newline instead of space, so we
    # have to replace them first.
    - $env:MESON_ARGS = $env:MESON_ARGS.replace("`n"," ")
    # Gitlab executes PowerShell in docker, but VsDevCmd.bat is a batch script.
    # Environment variables substitutions is done by PowerShell before calling
    # cmd.exe, that's why we use $env:FOO instead of %FOO%
    - cmd.exe /C "C:\BuildTools\Common7\Tools\VsDevCmd.bat -host_arch=amd64 -arch=$env:ARCH &&
        meson build $env:MESON_ARGS &&
        ninja -C build"
  artifacts:
    expire_in: "7 days"
    when: "always"
    paths:
      - 'gst-build/build/meson-logs/'

build vs2017 amd64:
  extends: '.build windows'
  variables:
    ARCH: 'amd64'

build vs2017 x86:
  extends: '.build windows'
  variables:
    ARCH: 'x86'

build msys2 :
  extends: '.build windows'
  timeout: '60min'
  rules:
    - if: '$CI_PROJECT_NAME !~ /^(cerbero|gst-docs|gst-integration-testsuites|gst-omx|gst-plugins-rs|gstreamer-vaapi)$/'
      allow_failure: true
      when: 'manual'
  script:
    # Sometimes there's seems to be an existing gst-build clone that comes either from the ether or
    # from a previous job due to some gitlab bug or implicit behavior?
    # So let's always check and clear it out if its there
    # https://gitlab.freedesktop.org/tpm/gstreamer-sharp/-/jobs/1672137
    - if (Test-Path $env:CI_PROJECT_DIR/gst-build) { Remove-Item -Recurse -Force $env:CI_PROJECT_DIR/gst-build }

    # Make sure powershell exists on errors
    # https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_preference_variables?view=powershell-6
    - $ErrorActionPreference = "Stop"

    # For some reason docker build hangs if this is included in the image, needs more troubleshooting
    - $env:PATH += ';C:\msys64\usr\bin;C:\msys64\mingw64\bin;C:\msys64\mingw32\bin'
    # Copied from https://github.com/msys2/setup-msys2/blob/master/main.js#L98
    - C:\msys64\usr\bin\bash -c "pacman-key --init && pacman-key --populate msys2 && pacman-key --refresh-keys || true"
    - C:\msys64\usr\bin\bash -c "sed -i 's/^CheckSpace/#CheckSpace/g' /etc/pacman.conf"
    - echo "Updating MSYS2"
    - C:\msys64\usr\bin\bash -c "pacman -Syuu --noconfirm || echo Update failed, ignoring"
    - echo "Killing all MSYS2 processes"
    - taskkill /F /FI "MODULES eq msys-2.0.dll"
    - echo "Completing MSYS2 update"
    - C:\msys64\usr\bin\bash -c "pacman -Suu --noconfirm"
    - echo "Installing needed MSYS2 packages"
    - C:\msys64\usr\bin\bash -c "pacman -Sy --noconfirm --needed mingw-w64-x86_64-toolchain ninja"

    - Invoke-WebRequest -Uri "https://gitlab.freedesktop.org/gstreamer/gst-ci/raw/$env:GST_UPSTREAM_BRANCH/gitlab/clone_manifest_ref.py" -OutFile $env:CI_PROJECT_DIR/clone_manifest_ref.py
    - python clone_manifest_ref.py --manifest manifest.xml --project gst-build --destination $env:CI_PROJECT_DIR/gst-build
    - cd $env:CI_PROJECT_DIR/gst-build

    - cp -r C:\subprojects\* subprojects\

    # Run the git-update script and feed it the manifest to setup the environment
    - cd $env:CI_PROJECT_DIR/gst-build; python git-update --no-interaction --manifest=$env:CI_PROJECT_DIR/manifest.xml
    # For some reason, options are separated by newline instead of space, so we
    # have to replace them first.
    - $env:MESON_ARGS = $env:MESON_ARGS.replace("`n"," ")
    # Replace forward slashes with backwards so bash doesn't complain
    - $env:_PROJECT_DIR = $env:CI_PROJECT_DIR.replace('\','/')
    - C:\msys64\usr\bin\bash -c "cd $env:_PROJECT_DIR/gst-build &&
        meson build $env:MESON_ARGS &&
        ninja -C build"


# Template for Cerbero GStreamer Deps
#
# This template is used by cerbero/ project to pre-built the GStreamer
# dependencies. When available, the .cerbero jobs will download this artifact
# in order to speed up the build.
#
# Parameters:
# CONFIG: The name of the configuration file to use
# ARCH: The cerbero <os>_<cpu> (used in cache key)
#
# Produce an artifact with the dist/ and .cache along
# with the associated build-tools.
.cerbero deps:
  extends: .cerbero
  rules:
    - if: '$CI_PROJECT_NAME == "cerbero"'
  script:
    - ./cerbero_setup.sh cerbero_deps_script

#
# Cerbero Linux X86_64 build
#
cerbero deps fedora x86_64:
  extends: '.cerbero deps'
  variables:
    CONFIG: "linux.config"
    ARCH: "linux_x86_64"

build cerbero fedora x86_64:
  extends: '.cerbero'
  rules:
    - if: '$CI_PROJECT_NAME !~ /^(cerbero|gst-build|gst-omx|gstreamer-vaapi|gst-docs|gst-integration-testsuites|gst-plugins-rs)$/'
  variables:
    CONFIG: "linux.config"

#
# Cerbero Android Universal build
#
.cerbero cross-android universal:
  variables:
    CONFIG: "cross-android-universal.cbc"
    ARCH: "android_universal"
  artifacts:
    name: "${CI_JOB_NAME}_${CI_COMMIT_SHA}"
    expire_in: '5 days'
    when: 'always'
    paths:
      - "manifest.xml"
      - "${CERBERO_HOME}/logs"
      - "*[0-9].tar.*"

cerbero deps cross-android universal:
  extends:
    - '.cerbero deps'
    - '.cerbero cross-android universal'

build cerbero cross-android universal:
  extends:
    - '.cerbero'
    - '.cerbero cross-android universal'
  rules:
    - if: '$CI_MERGE_REQUEST_IID && $CI_PROJECT_NAME == "gst-docs"'
    - if: '$CI_PROJECT_NAME !~ /^(cerbero|gst-build|gst-docs|gst-omx|gstreamer-vaapi|gst-integration-testsuites|gst-plugins-rs)$/'

#
# Cerbero Cross Windows builds
#
cerbero deps cross-windows x86:
  extends: '.cerbero deps'
  variables:
    CONFIG: "cross-win32.cbc"
    ARCH: "mingw_x86"

build cerbero cross win32:
  extends: '.cerbero'
  rules:
    - if: '$CI_PROJECT_NAME !~ /^(cerbero|gst-build|gst-docs|gst-omx|gstreamer-vaapi|gst-docs|gst-integration-testsuites|gst-plugins-rs)$/'
  variables:
    CONFIG: "cross-win32.cbc"
    CERBERO_RUN_WRAPPER: "wine"
    CERBERO_RUN_SUFFIX: ".exe"


cerbero deps cross-windows x86_64:
  extends: '.cerbero deps'
  variables:
    CONFIG: "cross-win64.cbc"
    ARCH: "mingw_x86_64"

build cerbero cross win64:
  extends: '.cerbero'
  rules:
    - if: '$CI_PROJECT_NAME !~ /^(cerbero|gst-build|gst-omx|gstreamer-vaapi|gst-docs|gst-integration-testsuites|gst-plugins-rs)$/'
  variables:
    CONFIG: "cross-win64.cbc"
    CERBERO_RUN_WRAPPER: "wine"
    CERBERO_RUN_SUFFIX: ".exe"

#
# Build an Android App using the android binaries
#
.cross-android universal examples:
  image: $ANDROID_IMAGE
  stage: 'integrate'
  variables:
    EXAMPLES_HOME: ${CI_PROJECT_DIR}/examples
    GSTREAMER_ROOT_ANDROID: ${CI_PROJECT_DIR}/examples/cerbero-android-universal
  script:
    - mkdir -p ${EXAMPLES_HOME}/outputs
    - curl -L -o clone_manifest_ref.py https://gitlab.freedesktop.org/gstreamer/gst-ci/raw/${GST_UPSTREAM_BRANCH}/gitlab/clone_manifest_ref.py
    - chmod +x clone_manifest_ref.py
    - ./clone_manifest_ref.py --manifest manifest.xml --project gst-examples --destination ${EXAMPLES_HOME}/gst-examples
    - ./clone_manifest_ref.py --manifest manifest.xml --project gst-docs --destination ${EXAMPLES_HOME}/gst-docs
    - rm clone_manifest_ref.py

    # extract our binaries
    - rm -f gstreamer-1.0-android-universal-*-runtime.tar.*
    - mkdir ${GSTREAMER_ROOT_ANDROID}
    - time tar -C ${GSTREAMER_ROOT_ANDROID} -xf gstreamer-1.0-android-universal-*.tar.*

    # gst-examples - player
    - chmod +x ${EXAMPLES_HOME}/gst-examples/playback/player/android/gradlew
    - ${EXAMPLES_HOME}/gst-examples/playback/player/android/gradlew --no-daemon --project-dir ${EXAMPLES_HOME}/gst-examples/playback/player/android assembleDebug
    - cp ${EXAMPLES_HOME}/gst-examples/playback/player/android/app/build/outputs/apk/debug/*.apk ${EXAMPLES_HOME}/outputs/

    # gst-examples - vulkan
    - chmod +x ${EXAMPLES_HOME}/gst-examples/vulkan/android/gradlew
    - ${EXAMPLES_HOME}/gst-examples/vulkan/android/gradlew --no-daemon --project-dir ${EXAMPLES_HOME}/gst-examples/vulkan/android assembleDebug
    - cp ${EXAMPLES_HOME}/gst-examples/vulkan/android/build/outputs/apk/debug/*.apk ${EXAMPLES_HOME}/outputs/

    # gst-docs android tutorials
    - chmod +x ${EXAMPLES_HOME}/gst-docs/examples/tutorials/android/gradlew
    - ${EXAMPLES_HOME}/gst-docs/examples/tutorials/android/gradlew --no-daemon --project-dir ${EXAMPLES_HOME}/gst-docs/examples/tutorials/android assembleDebug
    - cp ${EXAMPLES_HOME}/gst-docs/examples/tutorials/android/android-tutorial-*/build/outputs/apk/debug/*.apk ${EXAMPLES_HOME}/outputs/
  after_script:
    - rm -rf ${GSTREAMER_ROOT_ANDROID}
    - rm -rf ${EXAMPLES_HOME}/gst-examples ${EXAMPLES_HOME}/gst-docs
  artifacts:
    name: "${CI_JOB_NAME}_${CI_COMMIT_SHA}"
    expire_in: '5 days'
    when: 'always'
    paths:
      - "manifest.xml"
      - "${EXAMPLES_HOME}/outputs"

cross-android universal examples:
  extends: ".cross-android universal examples"
  rules:
    - if: '$CI_MERGE_REQUEST_IID && $CI_PROJECT_NAME == "gst-docs"'
    - if: '$CI_PROJECT_NAME !~ /^(cerbero|gst-build|gst-docs|gst-omx|gstreamer-vaapi|gst-integration-testsuites|gst-plugins-rs)$/'
  needs:
    - "build cerbero cross-android universal"

cerbero cross-android universal examples:
  extends: ".cross-android universal examples"
  rules:
    - if: '$CI_PROJECT_NAME == "cerbero"'
  needs:
    - "cerbero deps cross-android universal"

#
# Cerbero macOS X86_64 build
#
.cerbero macos x86_64:
  variables:
    ARCH: "darwin_x86_64"
    CONFIG: "osx-x86-64.cbc"
    CERBERO_PACKAGE_ARGS: ""
    HAVE_CCACHE: ""
    CERBERO_OVERRIDDEN_DIST_DIR: "/Library/Frameworks/GStreamer.framework/Versions/1.0"
    CERBERO_HOST_DIR: "/Users/gst-ci/cerbero/"
  tags:
    - gst-macos-11.1
  artifacts:
    name: "${CI_JOB_NAME}_${CI_COMMIT_SHA}"
    expire_in: '5 days'
    when: 'always'
    paths:
      - "manifest.xml"
      - "${CERBERO_HOME}/logs"
      - "gstreamer-1.0-1.*.pkg"
      - "gstreamer-1.0-devel-1.*.pkg"

cerbero deps macos x86_64:
  extends:
    - '.cerbero deps'
    - '.cerbero macos x86_64'

build cerbero macos x86_64:
  extends:
    - '.cerbero'
    - '.cerbero macos x86_64'
  rules:
    - if: '$CI_PROJECT_NAME !~ /^(cerbero|gst-build|gst-docs|gst-omx|gst-sharp|gstreamer-vaapi|gst-integration-testsuites|gst-plugins-rs)$/'

#
# Cerbero iOS build
#
.cerbero cross-ios universal:
  variables:
    ARCH: "ios_universal"
    CONFIG: "cross-ios-universal.cbc"
    CERBERO_ARGS: "${DEFAULT_CERBERO_ARGS} -v nowerror"
    CERBERO_PACKAGE_ARGS: ""
    HAVE_CCACHE: ""
    CERBERO_HOST_DIR: "/Users/gst-ci/cerbero/"
  tags:
    - gst-ios-14.3
  artifacts:
    name: "${CI_JOB_NAME}_${CI_COMMIT_SHA}"
    expire_in: '5 days'
    when: 'always'
    paths:
      - "manifest.xml"
      - "${CERBERO_HOME}/logs"
      - "gstreamer-1.0-*-ios-universal.pkg"

cerbero deps cross-ios universal:
  extends:
    - '.cerbero deps'
    - '.cerbero cross-ios universal'

build cerbero cross-ios universal:
  extends:
    - '.cerbero'
    - '.cerbero cross-ios universal'
  rules:
    - if: '$CI_MERGE_REQUEST_IID && $CI_PROJECT_NAME == "gst-docs"'
    - if: '$CI_PROJECT_NAME !~ /^(cerbero|gst-build|gst-docs|gst-omx|gst-sharp|gstreamer-vaapi|gst-integration-testsuites|gst-plugins-rs)$/'

documentation:
  image: $FEDORA_DOCS_IMAGE
  extends:
    - '.gst_build_ccache_vars'
  rules:
    - if: '$CI_PROJECT_NAME !~ /^(cerbero|gst-integration-testsuites|gst-plugins-rs|gst-python)$/'
  needs:
    - "manifest"
  stage: integrate
  variables:
    MESON_ARGS: *simple_build
    MESON_BUILDTYPE_ARGS: "-Ddoc=enabled"
    CI_ARTIFACTS_URL: "${CI_PROJECT_URL}/-/jobs/${CI_JOB_ID}/artifacts/raw/"
  script:
  - pip3 install --upgrade hotdoc

  # FIXME: remove this once the docs image is updated
  - pip3 install -U meson==0.56.2

  - *gst_build
  - ./gst-uninstalled.py ninja -C build/ plugins_doc_caches
  - |
    python3 -c "import os, subprocess, sys
    subprojects = [os.environ['CI_PROJECT_NAME']]
    if subprojects[0]  in ['gst-ci', 'gst-build']:
        subprojects = ['gstreamer', 'gst-plugins-ugly', 'gst-omx', 'gst-plugins-good', 'gst-plugins-base', 'gstreamer-vaapi', 'gst-libav', 'gst-editing-services', 'gst-rtsp-server', 'gst-plugins-bad',]

    diffsdir = '../plugins-cache-diffs'
    os.makedirs(diffsdir, exist_ok=True)
    res = 0
    for subproject in subprojects:
        cwd = os.path.join('subprojects', subproject)
        try:
            subprocess.check_call(['git', 'diff', '--quiet'], cwd=cwd)
        except subprocess.CalledProcessError:
            diffname = os.path.join(diffsdir, '%s_cache.diff' % subproject)
            res += 1
            with open(diffname, 'w') as diff:
                subprocess.check_call(['git', 'diff'], stdout=diff, cwd=cwd)
                print('\033[91mYou have a diff in the %s documentation cache. Please update with:\033[0m' % subproject)
                print('     $ curl %s/%s | git apply -' % (os.environ['CI_ARTIFACTS_URL'], diffname.replace('../', '')))
    if res != 0:
        print('(note that it might take a few minutes for artefacts to be available on the server)\n')
        sys.exit(res)"

  - ./gst-uninstalled.py hotdoc run --conf-file=build/subprojects/gst-docs/GStreamer-doc.json --fatal-warnings
  - cd -
  - mv gst-build/build/subprojects/gst-docs/GStreamer-doc/html documentation/

  artifacts:
    when: always
    paths:
    - documentation/
    - plugins-cache-diffs/

#
# Build an iOS App using the iOS binaries
#
.cross-ios universal examples:
  stage: 'integrate'
  variables:
    EXAMPLES_HOME: ${CI_PROJECT_DIR}/examples
    # disable codesigning so we don't need developer identities on the CI
    # machines
    XCODE_BUILD_ARGS: >
      CODE_SIGNING_REQUIRED="NO"
      CODE_SIGN_IDENTITY=""
      CODE_SIGNING_ALLOWED="NO"
      CODE_SIGN_ENTITLEMENTS=""
  script:
    # install the binaries
    - installer -pkg gstreamer-1.0-devel-*-ios-universal.pkg -target CurrentUserHomeDirectory -verbose

    - curl -L -o clone_manifest_ref.py https://gitlab.freedesktop.org/gstreamer/gst-ci/raw/${GST_UPSTREAM_BRANCH}/gitlab/clone_manifest_ref.py
    - chmod +x clone_manifest_ref.py
    - ./clone_manifest_ref.py --manifest manifest.xml --project gst-examples --destination ${EXAMPLES_HOME}/gst-examples
    - ./clone_manifest_ref.py --manifest manifest.xml --project gst-docs --destination ${EXAMPLES_HOME}/gst-docs
    - rm clone_manifest_ref.py

    # dump some useful information
    - xcodebuild -version
    - xcodebuild -showsdks

    # gst-docs ios tutorials
    - xcodebuild -showBuildSettings -alltargets -project ${EXAMPLES_HOME}/gst-docs/examples/tutorials/xcode\ iOS/GStreamer\ iOS\ Tutorials.xcodeproj
    - xcodebuild -alltargets -destination generic/platform=iOS -project ${EXAMPLES_HOME}/gst-docs/examples/tutorials/xcode\ iOS/GStreamer\ iOS\ Tutorials.xcodeproj ${XCODE_BUILD_ARGS}

    # gst-examples
    - xcodebuild -showBuildSettings -alltargets -project ${EXAMPLES_HOME}/gst-examples/playback/player/ios/GstPlay.xcodeproj
    - xcodebuild -alltargets -destination generic/platform=iOS -project ${EXAMPLES_HOME}/gst-examples/playback/player/ios/GstPlay.xcodeproj ${XCODE_BUILD_ARGS}
  after_script:
    - rm -rf ${EXAMPLES_HOME}/gst-examples ${EXAMPLES_HOME}/gst-docs
  tags:
    - gst-ios-14.3

cross-ios universal examples:
  extends: ".cross-ios universal examples"
  rules:
    - if: '$CI_MERGE_REQUEST_IID && $CI_PROJECT_NAME == "gst-docs"'
    - if: '$CI_PROJECT_NAME !~ /^(cerbero|gst-build|gst-docs|gst-omx|gstreamer-vaapi|gst-integration-testsuites|gst-plugins-rs)$/'
  needs:
    - "build cerbero cross-ios universal"

cerbero cross-ios universal examples:
  extends: ".cross-ios universal examples"
  rules:
    - if: '$CI_PROJECT_NAME == "cerbero"'
  needs:
    - "cerbero deps cross-ios universal"

build gst-omx zynq fedora x86_64:
  extends: '.build fedora x86_64'
  variables:
    MESON_ARGS: "-Domx=enabled -Dgst-omx:target=zynqultrascaleplus -Dgst-omx:header_path=${CI_PROJECT_DIR}/vcu-omx-il/omx_header -Dpython=disabled -Dlibav=disabled -Dlibnice=disabled -Dugly=disabled -Dbad=disabled -Ddevtools=disabled -Dges=disabled -Drtsp_server=disabled -Dvaapi=disabled -Dsharp=disabled -Dgst-examples=disabled -Drs=disabled ${MESON_BUILDTYPE_ARGS} $MESON_GST_WERROR"
  rules:
    - if: '$CI_PROJECT_NAME =~ /^(gst-omx|gst-ci)$/'
  before_script:
    - git clone https://github.com/Xilinx/vcu-omx-il.git --branch=release-2020.1 ${CI_PROJECT_DIR}/vcu-omx-il

build gst-omx tizonia fedora x86_64:
  extends: '.build fedora x86_64'
  variables:
    MESON_ARGS: "-Domx=enabled -Dgst-omx:target=tizonia -Dpython=disabled -Dlibav=disabled -Dlibnice=disabled -Dugly=disabled -Dbad=disabled -Ddevtools=disabled -Dges=disabled -Drtsp_server=disabled -Dvaapi=disabled -Dsharp=disabled -Dgst-examples=disabled -Drs=disabled ${MESON_BUILDTYPE_ARGS} $MESON_GST_WERROR"
    PKG_CONFIG_PATH: ${PKG_CONFIG_PATH}:${CI_PROJECT_DIR}/tizonia-install/lib64/pkgconfig/
  rules:
    - if: '$CI_PROJECT_NAME =~ /^(gst-omx|gst-ci)$/'
  before_script:
    - git clone https://github.com/tizonia/tizonia-openmax-il --branch v0.20.2
    - cd tizonia-openmax-il
    - meson build -Dclients=false -Dplugins='' -Dplayer=false -Dprefix=${CI_PROJECT_DIR}/tizonia-install
    - ninja -C build
    - ninja -C build install
    - cd ..

#
# Cerbero Native Windows builds
#
.cerbero windows native:
  needs: ['manifest']
  image: $WINDOWS_IMAGE
  tags:
    - 'docker'
    - 'windows'
    - 'gstreamer-windows'
    - '1809'
  variables:
    CONFIG: 'win64.cbc'
    ARCH: 'msvc_x86_64'
    CERBERO_HOST_DIR: "C:/cerbero"
    CERBERO_ARGS: "${DEFAULT_CERBERO_ARGS} -v visualstudio -v nowerror"
    CERBERO_RUN_SUFFIX: ".exe"
    HAVE_CCACHE: ""
  before_script:
    - $env:CI_PROJECT_DIR = $env:CI_PROJECT_DIR.replace('\', '/')
    - $env:CERBERO_SCRIPTS_PATH = "$env:CI_PROJECT_DIR/cerbero_setup.sh"
    - echo "Fetching $env:CERBERO_SCRIPT_URL"
    - Invoke-WebRequest -Uri $env:CERBERO_SCRIPT_URL -OutFile $env:CERBERO_SCRIPTS_PATH
    - C:\MinGW\msys\1.0\bin\bash.exe --login -c "cd $env:CI_PROJECT_DIR && $env:CERBERO_SCRIPTS_PATH cerbero_before_script"

cerbero deps msvc x86_64:
  extends: ['.cerbero deps', '.cerbero windows native']
  script:
    - C:\MinGW\msys\1.0\bin\bash.exe --login -c "cd $env:CI_PROJECT_DIR && $env:CERBERO_SCRIPTS_PATH cerbero_deps_script"

build cerbero msvc x86_64:
  extends: ['.cerbero', '.cerbero windows native']
  script:
    - C:\MinGW\msys\1.0\bin\bash.exe --login -c "cd $env:CI_PROJECT_DIR && $env:CERBERO_SCRIPTS_PATH cerbero_script"
  rules:
    - if: '$CI_PROJECT_NAME !~ /^(cerbero|gst-build|gst-omx|gstreamer-vaapi|gst-docs|gst-integration-testsuites|gst-plugins-rs)$/'