summaryrefslogtreecommitdiff
path: root/.github/workflows/cosmocc.yml
blob: 4bcfa9a2c5aa445cd82086e998923fc5f59a5ff1 (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
name: Cosmocc

on:
  pull_request:
  push:
  release:
    types: [published]

jobs:
  check:
    runs-on: ubuntu-latest
    outputs:
      should-run: ${{ steps.check.outputs.should-run }}
    steps:
      - name: Random execution check
        id: check
        uses: actions/github-script@v7
        with:
          script: |
            const fs = require('fs');
            const outputFile = process.env.GITHUB_OUTPUT;

            // Always run for release events
            if (context.eventName === 'release') {
              fs.appendFileSync(outputFile, `should-run=true\n`);
              core.info('Release event detected. Will run tests.');
              return;
            }

            // Execution probability (default 50%, can be overridden via env)
            const probability = parseFloat(process.env.RUN_PROBABILITY || '0.5');

            // Generate deterministic "random" number based on commit SHA, run ID, and current time
            // Adding time ensures better randomness while keeping same commit/run consistent
            const timeSeed = Math.floor(Date.now() / (1000 * 60 * 60)); // Round to hour for consistency
            const seed = context.sha + context.runId + timeSeed;
            let hash = 0;
            for (let i = 0; i < seed.length; i++) {
              const char = seed.charCodeAt(i);
              hash = ((hash << 5) - hash) + char;
              hash = hash | 0; // Convert to 32-bit integer
            }
            // Normalize to 0-1 range
            const random = Math.abs(hash) / 2147483647;
            const shouldRun = random < probability;

            // Use environment file instead of deprecated set-output
            fs.appendFileSync(outputFile, `should-run=${shouldRun}\n`);
            if (shouldRun) {
              core.info(`Random check passed (${(random * 100).toFixed(2)}% < ${(probability * 100).toFixed(0)}%). Will run tests.`);
            } else {
              core.info(`Random check failed (${(random * 100).toFixed(2)}% >= ${(probability * 100).toFixed(0)}%). Skipping.`);
            }
        env:
          RUN_PROBABILITY: ${{ vars.COSMOCC_RUN_PROBABILITY || '0.5' }}

  build:
    #needs: check
    #if: needs.check.outputs.should-run == 'true'
    strategy:
      matrix:
        os: [ubuntu-latest, macos-15-intel]
        arch: [x86_64]

    runs-on: ${{ matrix.os }}

    concurrency:
      group: ${{ github.ref }}-${{ github.base_ref }}-${{ github.head_ref }}-Cosmocc-${{ matrix.os }}-${{ matrix.arch }}
      cancel-in-progress: true

    steps:
      - uses: actions/checkout@v2
        with:
          submodules: true

      - uses: little-core-labs/[email protected]
        id: tagName

      - name: Prepare local xmake
        run: cp -rf . ../xmake-source

      - uses: xmake-io/github-action-setup-xmake@v1
        with:
          xmake-version: local#../xmake-source

      - uses: bjia56/setup-cosmocc@main
        with:
          version: "4.0.2"

      - name: Build
        run: |
          cd core
          xmake f -p linux --cosmocc=y --embed=y -y -cvD
          xmake -v
          cd ..

      - name: Prepare (Linux)
        if: matrix.os == 'ubuntu-latest'
        run: |
          sudo apt update
          sudo apt install -y ruby ruby-dev rubygems build-essential llvm libc++-dev
          sudo apt install -y libgl1-mesa-dev libglu1-mesa-dev
          clang --version

      # In the tests, cmake tries to use make from the cosmopolitan toolchain and fails, so uninstall it
      - name: Remove cosmocc
        if: startsWith(matrix.os, 'macos')
        run: |
          INSTALL_DIR="${{runner.temp}}/cosmocc-4.0.2"
          echo "Removing cosmocc from PATH"
          echo "::remove-path::${INSTALL_DIR}/bin"
          rm -r "${INSTALL_DIR}"

      - name: Tests
        run: |
          ls -l core/build/
          cmake --version
          core/build/xmake --version
          core/build/xmake lua -v -D tests/run.lua

      - name: Artifact
        if: matrix.os == 'ubuntu-latest'
        uses: actions/upload-artifact@v4
        with:
          name: xmake-bundle.cosmocc
          path: core/build/xmake

      - name: Publish bundle binary
        if: github.event.action == 'published' && matrix.os == 'ubuntu-latest'
        uses: actions/[email protected]
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ github.event.release.upload_url }}
          asset_path: core/build/xmake
          asset_name: xmake-bundle-${{ steps.tagName.outputs.tag }}.cosmocc
          asset_content_type: application/zip