version: 2.1

jobs:
  build_linux:
    description: Configure, build and package sudo (Linux)
    parameters:
      krb5:
        description: if true, build sudo's Kerberos V support
        default: false
        type: boolean
      ldap:
        description: if true, build sudo's LDAP support
        default: false
        type: boolean
      wolfssl:
        description: if true, build sudo with wolfSSL support
        default: false
        type: boolean
      static_sudoers:
        description: if true, build sudoers statically
        default: false
        type: boolean
      logsrvd:
        description: if true, build sudo_logsrvd and sudoers client
        default: true
        type: boolean
      intercept:
        description: if true, build intercept support
        default: true
        type: boolean
    docker:
      - image: docker.io/sudoproject/ubuntu:latest
        user: build
    steps:
      - checkout
      - run:
          name: "Building and packaging sudo (Linux)"
          command: ./scripts/mkpkg <<# parameters.krb5 >>--without-pam --with-kerb5 <</ parameters.krb5 >><<# parameters.ldap >>--flavor=ldap --with-sssd --with-sssd-lib=/usr/lib/x86_64-linux-gnu <</ parameters.ldap >><<# parameters.wolfssl >>--enable-wolfssl <</ parameters.wolfssl >><<^ parameters.logsrvd >>--disable-log-server --disable-log-client <</ parameters.logsrvd >><<^ parameters.intercept >>--disable-intercept <</ parameters.intercept >><<# parameters.static_sudoers >>--enable-static-sudoers <</ parameters.static_sudoers >>--enable-warnings --enable-werror --enable-sanitizer
      # Save workspace for subsequent jobs (i.e. test)
      - persist_to_workspace:
          root: .
          paths:
            - .
  test_linux:
    description: run sudo tests in a pre-built workspace (Linux)
    docker:
      - image: docker.io/sudoproject/ubuntu:latest
        user: build
    steps:
      # Reuse the workspace from the build job
      - attach_workspace:
          at: .
      - run:
          name: "Running tests (Linux)"
          command: make check
          environment:
              # Leak sanitizer requires ptrace, disable it
              ASAN_OPTIONS: detect_leaks=0

  build_macos:
    description: Configure, build and package sudo (macOS)
    macos:
      xcode: 14.2.0 # indicate our selected version of Xcode
    steps:
      - checkout
      - run:
          name: "Building and packaging sudo (macOS)"
          command: ./scripts/mkpkg --enable-warnings
      # Save workspace for subsequent jobs (i.e. test)
      - persist_to_workspace:
          root: .
          paths:
            - .
  test_macos:
    description: run sudo tests in a pre-built workspace (macOS)
    macos:
      xcode: 14.2.0 # indicate our selected version of Xcode
    steps:
      # Reuse the workspace from the build job
      - attach_workspace:
          at: .
      - run:
          name: "Running tests (macOS)"
          command: make check

# Orchestrate our job run sequence
workflows:
  version: 2
  build_and_test:
    jobs:
      - build_linux:
          name: build-linux-krb5
          krb5: true
          filters:
            branches:
              only: main
      - build_linux:
          name: build-linux-ldap
          ldap: true
          filters:
            branches:
              only: main
      - build_linux:
          name: build-linux-wolfssl
          wolfssl: true
          filters:
            branches:
              only: main
      - build_linux:
          name: build-linux-static-sudoers
          static_sudoers: true
          filters:
            branches:
              only: main
      - build_linux:
          name: build-linux-nologsrvd
          logsrvd: false
          filters:
            branches:
              only: main
      - build_linux:
          name: build-linux-nointercept
          logsrvd: false
          filters:
            branches:
              only: main
      - build_macos:
          name: build-macos
          filters:
            branches:
              only: main
      - test_linux:
          name: test-linux-krb5
          requires:
            - build-linux-krb5
      - test_linux:
          name: test-linux-ldap
          requires:
            - build-linux-ldap
      - test_linux:
          name: test-linux-wolfssl
          requires:
            - build-linux-wolfssl
      - test_linux:
          name: test-linux-static-sudoers
          requires:
            - build-linux-static-sudoers
      - test_linux:
          name: test-linux-nologsrvd
          requires:
            - build-linux-nologsrvd
      - test_linux:
          name: test-linux-nointercept
          requires:
            - build-linux-nointercept
      - test_macos:
          name: test-macos
          requires:
            - build-macos
