# Copyright 2024 Google LLC
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

steps:
  - id: "build-docker"
    name: "gcr.io/cloud-builders/docker"
    waitFor: ['-']
    script: |
        #!/usr/bin/env bash
        docker buildx create --name container-builder --driver docker-container --bootstrap --use

        export TAGS="-t ${_DOCKER_URI}:$SHORT_SHA -t ${_DOCKER_URI}:$REF_NAME"
        docker buildx build --platform linux/amd64,linux/arm64 --build-arg COMMIT_SHA=$(git rev-parse --short HEAD) $TAGS --push .

  - id: "install-dependencies"
    name: golang:1
    waitFor: ['-']
    env:
      - 'GOPATH=/gopath'
    volumes:
      - name: 'go'
        path: '/gopath'
    script: |
        go get -d ./...

  - id: "install-zig"
    name: golang:1
    waitFor: ['-']
    volumes:
      - name: 'zig'
        path: '/zig-tools'
    script: |
        #!/usr/bin/env bash
        set -e
        apt-get update && apt-get install -y xz-utils
        curl -fL "https://ziglang.org/download/0.15.2/zig-x86_64-linux-0.15.2.tar.xz" -o zig.tar.xz
        tar -xf zig.tar.xz -C /zig-tools --strip-components=1

  - id: "install-macos-sdk"
    name: golang:1
    waitFor: ['-']
    volumes:
      - name: 'macos-sdk'
        path: '/macos-sdk'
    script: |
        #!/usr/bin/env bash
        set -e
        apt-get update && apt-get install -y xz-utils
        echo "Downloading macOS 14.5 SDK..."
        curl -fL -o sdk.tar.xz https://github.com/alexey-lysiuk/macos-sdk/releases/download/14.5/MacOSX14.5.tar.xz
        
        mkdir -p /macos-sdk/MacOSX14.5.sdk
        echo "Unpacking macOS 14.5 SDK..."
        tar -xf sdk.tar.xz -C /macos-sdk/MacOSX14.5.sdk --strip-components=1

  - id: "build-linux-amd64"
    name: golang:1
    waitFor:
      - "install-dependencies"
      - "install-zig"
    env:
      - 'GOPATH=/gopath'
      - 'CGO_ENABLED=1'
      - 'GOOS=linux'
      - 'GOARCH=amd64'
      - 'CC=/zig-tools/zig cc -target x86_64-linux-gnu'
      - 'CXX=/zig-tools/zig c++ -target x86_64-linux-gnu'
    volumes:
      - name: 'go'
        path: '/gopath'
      - name: 'zig'
        path: '/zig-tools'
    script: |
        #!/usr/bin/env bash
        go build -ldflags "-X github.com/googleapis/genai-toolbox/cmd.commitSha=$(git rev-parse --short HEAD)" -o toolbox.linux.amd64

  - id: "store-linux-amd64"
    name: "gcr.io/cloud-builders/gcloud:latest"
    waitFor:
      - "build-linux-amd64"
    script: |
        #!/usr/bin/env bash
        gcloud storage cp toolbox.linux.amd64 gs://$_BUCKET_NAME/$REF_NAME/linux/amd64/toolbox

  - id: "build-linux-amd64-geminicli"
    name: golang:1
    waitFor:
      - "install-dependencies"
      - "install-zig"
    env:
      - 'GOPATH=/gopath'
      - 'CGO_ENABLED=1'
      - 'GOOS=linux'
      - 'GOARCH=amd64'
      - 'CC=/zig-tools/zig cc -target x86_64-linux-gnu'
      - 'CXX=/zig-tools/zig c++ -target x86_64-linux-gnu'
    volumes:
      - name: 'go'
        path: '/gopath'
      - name: 'zig'
        path: '/zig-tools'
    script: |
        #!/usr/bin/env bash
        export VERSION=$(cat ./cmd/version.txt)
        go build -ldflags "-X github.com/googleapis/genai-toolbox/cmd.commitSha=$(git rev-parse --short HEAD)" -o toolbox.geminicli.linux.amd64

  - id: "build-darwin-arm64"
    name: golang:1
    waitFor:
      - "install-dependencies"
      - "install-zig"
      - "install-macos-sdk"
    env:
      - 'GOPATH=/gopath'
      - 'CGO_ENABLED=1'
      - 'GOOS=darwin'
      - 'GOARCH=arm64'
      - 'SDK_PATH=/macos-sdk/MacOSX14.5.sdk'
      - 'MACOS_MIN_VER=10.14'
      - 'CGO_LDFLAGS=-mmacosx-version-min=10.14 --sysroot /macos-sdk/MacOSX14.5.sdk -F/macos-sdk/MacOSX14.5.sdk/System/Library/Frameworks -L/usr/lib'
      - 'COMMON_FLAGS=-mmacosx-version-min=10.14 -target aarch64-macos.11.0.0-none -isysroot /macos-sdk/MacOSX14.5.sdk -iwithsysroot /usr/include -iframeworkwithsysroot /System/Library/Frameworks'
      - 'CC=/zig-tools/zig cc -mmacosx-version-min=10.14 -target aarch64-macos.11.0.0-none -isysroot /macos-sdk/MacOSX14.5.sdk -iwithsysroot /usr/include -iframeworkwithsysroot /System/Library/Frameworks'
      - 'CXX=/zig-tools/zig c++ -mmacosx-version-min=10.14 -target aarch64-macos.11.0.0-none -isysroot /macos-sdk/MacOSX14.5.sdk -iwithsysroot /usr/include -iframeworkwithsysroot /System/Library/Frameworks'
    volumes:
      - name: 'go'
        path: '/gopath'
      - name: 'zig'
        path: '/zig-tools'
      - name: 'macos-sdk'
        path: '/macos-sdk'
    script: |
        #!/usr/bin/env bash
         go build -trimpath -buildmode=pie -ldflags "-s -w -X github.com/googleapis/genai-toolbox/cmd.commitSha=$(git rev-parse --short HEAD)" -o toolbox.darwin.arm64

  - id: "store-darwin-arm64"
    name: "gcr.io/cloud-builders/gcloud:latest"
    waitFor:
      - "build-darwin-arm64"
    script: |
        #!/usr/bin/env bash
        gcloud storage cp toolbox.darwin.arm64 gs://$_BUCKET_NAME/$REF_NAME/darwin/arm64/toolbox

  - id: "build-darwin-arm64-geminicli"
    name: golang:1
    waitFor:
      - "install-dependencies"
      - "install-zig"
      - "install-macos-sdk"
    env:
      - 'GOPATH=/gopath'
      - 'CGO_ENABLED=1'
      - 'GOOS=darwin'
      - 'GOARCH=arm64'
      - 'SDK_PATH=/macos-sdk/MacOSX14.5.sdk'
      - 'MACOS_MIN_VER=10.14'
      - 'CGO_LDFLAGS=-mmacosx-version-min=10.14 --sysroot /macos-sdk/MacOSX14.5.sdk -F/macos-sdk/MacOSX14.5.sdk/System/Library/Frameworks -L/usr/lib'
      - 'COMMON_FLAGS=-mmacosx-version-min=10.14 -target aarch64-macos.11.0.0-none -isysroot /macos-sdk/MacOSX14.5.sdk -iwithsysroot /usr/include -iframeworkwithsysroot /System/Library/Frameworks'
      - 'CC=/zig-tools/zig cc -mmacosx-version-min=10.14 -target aarch64-macos.11.0.0-none -isysroot /macos-sdk/MacOSX14.5.sdk -iwithsysroot /usr/include -iframeworkwithsysroot /System/Library/Frameworks'
      - 'CXX=/zig-tools/zig c++ -mmacosx-version-min=10.14 -target aarch64-macos.11.0.0-none -isysroot /macos-sdk/MacOSX14.5.sdk -iwithsysroot /usr/include -iframeworkwithsysroot /System/Library/Frameworks'
    volumes:
      - name: 'go'
        path: '/gopath'
      - name: 'zig'
        path: '/zig-tools'
      - name: 'macos-sdk'
        path: '/macos-sdk'
    script: |
        #!/usr/bin/env bash
         go build -trimpath -buildmode=pie -ldflags "-s -w -X github.com/googleapis/genai-toolbox/cmd.commitSha=$(git rev-parse --short HEAD)" -o toolbox.geminicli.darwin.arm64

  - id: "build-darwin-amd64"
    name: golang:1
    waitFor:
      - "install-dependencies"
      - "install-zig"
      - "install-macos-sdk"
    env:
      - 'GOPATH=/gopath'
      - 'CGO_ENABLED=1'
      - 'GOOS=darwin'
      - 'GOARCH=amd64'
      - 'SDK_PATH=/macos-sdk/MacOSX14.5.sdk'
      - 'MACOS_MIN_VER=10.14'
      - 'CGO_LDFLAGS=-mmacosx-version-min=10.14 --sysroot /macos-sdk/MacOSX14.5.sdk -F/macos-sdk/MacOSX14.5.sdk/System/Library/Frameworks -L/usr/lib'
      - 'COMMON_FLAGS=-mmacosx-version-min=10.14 -target x86_64-macos.11.0.0-none -isysroot /macos-sdk/MacOSX14.5.sdk -iwithsysroot /usr/include -iframeworkwithsysroot /System/Library/Frameworks'
      - 'CC=/zig-tools/zig cc -mmacosx-version-min=10.14 -target x86_64-macos.11.0.0-none -isysroot /macos-sdk/MacOSX14.5.sdk -iwithsysroot /usr/include -iframeworkwithsysroot /System/Library/Frameworks'
      - 'CXX=/zig-tools/zig c++ -mmacosx-version-min=10.14 -target x86_64-macos.11.0.0-none -isysroot /macos-sdk/MacOSX14.5.sdk -iwithsysroot /usr/include -iframeworkwithsysroot /System/Library/Frameworks'
    volumes:
      - name: 'go'
        path: '/gopath'
      - name: 'zig'
        path: '/zig-tools'
      - name: 'macos-sdk'
        path: '/macos-sdk'
    script: |
        #!/usr/bin/env bash
         go build -trimpath -buildmode=pie -ldflags "-s -w -X github.com/googleapis/genai-toolbox/cmd.commitSha=$(git rev-parse --short HEAD)" -o toolbox.darwin.amd64

  - id: "store-darwin-amd64"
    name: "gcr.io/cloud-builders/gcloud:latest"
    waitFor:
      - "build-darwin-amd64"
    script: |
        #!/usr/bin/env bash
        gcloud storage cp toolbox.darwin.amd64 gs://$_BUCKET_NAME/$REF_NAME/darwin/amd64/toolbox

  - id: "build-darwin-amd64-geminicli"
    name: golang:1
    waitFor:
      - "install-dependencies"
      - "install-zig"
      - "install-macos-sdk"
    env:
      - 'GOPATH=/gopath'
      - 'CGO_ENABLED=1'
      - 'GOOS=darwin'
      - 'GOARCH=amd64'
      - 'SDK_PATH=/macos-sdk/MacOSX14.5.sdk'
      - 'MACOS_MIN_VER=10.14'
      - 'CGO_LDFLAGS=-mmacosx-version-min=10.14 --sysroot /macos-sdk/MacOSX14.5.sdk -F/macos-sdk/MacOSX14.5.sdk/System/Library/Frameworks -L/usr/lib'
      - 'COMMON_FLAGS=-mmacosx-version-min=10.14 -target x86_64-macos.11.0.0-none -isysroot /macos-sdk/MacOSX14.5.sdk -iwithsysroot /usr/include -iframeworkwithsysroot /System/Library/Frameworks'
      - 'CC=/zig-tools/zig cc -mmacosx-version-min=10.14 -target x86_64-macos.11.0.0-none -isysroot /macos-sdk/MacOSX14.5.sdk -iwithsysroot /usr/include -iframeworkwithsysroot /System/Library/Frameworks'
      - 'CXX=/zig-tools/zig c++ -mmacosx-version-min=10.14 -target x86_64-macos.11.0.0-none -isysroot /macos-sdk/MacOSX14.5.sdk -iwithsysroot /usr/include -iframeworkwithsysroot /System/Library/Frameworks'
    volumes:
      - name: 'go'
        path: '/gopath'
      - name: 'zig'
        path: '/zig-tools'
      - name: 'macos-sdk'
        path: '/macos-sdk'
    script: |
        #!/usr/bin/env bash
         go build -trimpath -buildmode=pie -ldflags "-s -w -X github.com/googleapis/genai-toolbox/cmd.commitSha=$(git rev-parse --short HEAD)" -o toolbox.geminicli.darwin.amd64

  - id: "build-windows-amd64"
    name: golang:1
    waitFor:
      - "install-dependencies"
      - "install-zig"
    env:
      - 'GOPATH=/gopath'
      - 'CGO_ENABLED=1'
      - 'GOOS=windows'
      - 'GOARCH=amd64'
      - 'CC=/zig-tools/zig cc -target x86_64-windows-gnu'
      - 'CXX=/zig-tools/zig c++ -target x86_64-windows-gnu'
    volumes:
      - name: 'go'
        path: '/gopath'
      - name: 'zig'
        path: '/zig-tools'
    script: |
        #!/usr/bin/env bash
        go build -ldflags "-X github.com/googleapis/genai-toolbox/cmd.commitSha=$(git rev-parse --short HEAD)" -o toolbox.windows.amd64

  - id: "store-windows-amd64"
    name: "gcr.io/cloud-builders/gcloud:latest"
    waitFor:
      - "build-windows-amd64"
    script: |
        #!/usr/bin/env bash
        gcloud storage cp toolbox.windows.amd64 gs://$_BUCKET_NAME/$REF_NAME/windows/amd64/toolbox.exe

  - id: "build-windows-amd64-geminicli"
    name: golang:1
    waitFor:
      - "install-dependencies"
      - "install-zig"
    env:
      - 'GOPATH=/gopath'
      - 'CGO_ENABLED=1'
      - 'GOOS=windows'
      - 'GOARCH=amd64'
      - 'CC=/zig-tools/zig cc -target x86_64-windows-gnu'
      - 'CXX=/zig-tools/zig c++ -target x86_64-windows-gnu'
    volumes:
      - name: 'go'
        path: '/gopath'
      - name: 'zig'
        path: '/zig-tools'
    script: |
        #!/usr/bin/env bash
        export VERSION=$(cat ./cmd/version.txt)
        go build -ldflags "-X github.com/googleapis/genai-toolbox/cmd.commitSha=$(git rev-parse --short HEAD)" -o toolbox.geminicli.windows.amd64

options:
  automapSubstitutions: true
  dynamicSubstitutions: true
  logging: CLOUD_LOGGING_ONLY # Necessary for custom service account
  pool:
    name: projects/$PROJECT_ID/locations/us-central1/workerPools/run-release # to increase resource for running releases

substitutions:
  _REGION: us-central1
  _AR_HOSTNAME: ${_REGION}-docker.pkg.dev
  _AR_REPO_NAME: toolbox-dev
  _BUCKET_NAME: genai-toolbox-dev
  _DOCKER_URI: ${_AR_HOSTNAME}/${PROJECT_ID}/${_AR_REPO_NAME}/toolbox