version: 2.1

commands:
  compress_ext:
    description: "Concatenate all files under ext. The checksum is then calculated on this file to cache the ext directory."
    steps:
      - run: find ext -type f -print | sort | xargs cat > ext_concat

jobs:
  build-arm-core:
    macos:
      xcode: 13.4.1
      resource_class: macos.m1.medium.gen1
    environment:
      CIBW_ENVIRONMENT: "MACOSX_DEPLOYMENT_TARGET=12"
      CIBW_ARCHS: "arm64"
      CIBW_BUILD: "cp39-* cp310-* cp311-* cp312-*"
    steps:
      - checkout
      - compress_ext
      - restore_cache:
          key: ext-arm-{{ checksum "ext_concat" }}
      - run: |
          bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
          /opt/homebrew/bin/brew install python@3.10

          .circleci/package_macos.bash
      - save_cache:
          key: ext-arm-{{ checksum "ext_concat" }}
          paths:
            - src/tmlt/core/ext
      - store_artifacts:
          path: dist

  build-intel-core:
    macos:
      xcode: 13.4.1
      resource_class: macos.m1.medium.gen1
    environment:
      CIBW_ENVIRONMENT: "MACOSX_DEPLOYMENT_TARGET=12"
      CIBW_ARCHS: "x86_64"
      CIBW_BUILD: "cp39-* cp310-* cp311-* cp312-*"
    steps:
      - checkout
      - compress_ext
      - restore_cache:
          key: ext-intel-{{ checksum "ext_concat" }}
      - run: |
          softwareupdate --install-rosetta --agree-to-license
          arch -x86_64 bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
          arch -x86_64 /usr/local/bin/brew install python@3.10

          arch -x86_64 .circleci/package_macos.bash
      - save_cache:
          key: ext-intel-{{ checksum "ext_concat" }}
          paths:
            - src/tmlt/core/ext
      - store_artifacts:
          path: dist

workflows:
  build-core-mac-wheels-workflow:
    jobs:
      - build-arm-core:
          filters:
            branches:
              only: dev
            tags:
              only: /.*/
      - build-intel-core:
          filters:
            branches:
              only: dev
            tags:
              only: /.*/
