summaryrefslogtreecommitdiff
path: root/.circleci/config2.yml
blob: 4cd84813143b785752f40ce1ce43964908f81524 (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
version: 2.1

commands:
  setup-toolchain:
    parameters:
        toolchain:
            type: string

    steps:
      - run:
          name: Set toolchain url and key
          command: |
            toolchain_url=$(jq -r '."<< parameters.toolchain >>"' .github/actions/setup_toolchain/toolchain.json)
            # only cache if not a github link
            if [[ $toolchain_url != "https://github.com"* ]]; then
              echo "<< parameters.toolchain >>-$toolchain_url" > toolchain_key
            fi
            echo "export toolchain_url=$toolchain_url" >> $BASH_ENV

      - restore_cache:
          name: Restore Toolchain Cache
          key: deps-{{ checksum "toolchain_key" }}
          paths:
            - ~/cache/<< parameters.toolchain >>

      - run:
          name: Install Toolchain
          command: |
            # download if folder does not exist (not cached)
            if [ ! -d ~/cache/<< parameters.toolchain >> ]; then
              mkdir -p ~/cache/<< parameters.toolchain >>
              if [[ << parameters.toolchain >> == rx-gcc ]]; then
                wget --progress=dot:giga $toolchain_url -O toolchain.run
                chmod +x toolchain.run
                ./toolchain.run -p ~/cache/<< parameters.toolchain >>/gnurx -y
              elif [[ << parameters.toolchain >> == ft9xx-gcc ]]; then
                wget --progress=dot:giga $toolchain_url -O ~/cache/<< parameters.toolchain >>/ft9xxtoolchain.deb
              elif [[ << parameters.toolchain >> == arm-iar ]]; then
                wget --progress=dot:giga https://netstorage.iar.com/FileStore/STANDARD/001/003/926/iar-lmsc-tools_1.8_amd64.deb -O ~/cache/<< parameters.toolchain >>/iar-lmsc-tools.deb
                wget --progress=dot:giga $toolchain_url -O ~/cache/<< parameters.toolchain >>/toolchain.deb
              else
                wget --progress=dot:giga $toolchain_url -O toolchain.tar.gz
                tar -C ~/cache/<< parameters.toolchain >> -xaf toolchain.tar.gz
              fi
            fi

            # Add toolchain to PATH
            if [[ << parameters.toolchain >> == arm-iar ]]; then
              # Install IAR since we only cache deb file
              sudo dpkg -i ~/cache/<< parameters.toolchain >>/iar-lmsc-tools.deb
              sudo dpkg --ignore-depends=libusb-1.0-0 -i ~/cache/<< parameters.toolchain >>/toolchain.deb
              echo "export PATH=$PATH:/opt/iar/cxarm/arm/bin" >> $BASH_ENV
            elif [[ << parameters.toolchain >> == ft9xx-gcc ]]; then
              sudo apt install -y ~/cache/<< parameters.toolchain >>/ft9xxtoolchain.deb
              echo "export PATH=$PATH:/opt/ft32/bin" >> $BASH_ENV
            else
              echo "export PATH=$PATH:`echo ~/cache/<< parameters.toolchain >>/*/bin`" >> $BASH_ENV
            fi

      - save_cache:
          name: Save Toolchain Cache
          key: deps-{{ checksum "toolchain_key" }}
          paths:
            - ~/cache/<< parameters.toolchain >>

  build:
    parameters:
      build-system:
        type: string
      toolchain:
        type: string
      family:
        type: string
      build-args:
        type: string
        default: ""

    steps:
      - checkout
      - run:
          name: Get Dependencies
          command: |
            python tools/get_deps.py << parameters.family >>

            # Install ninja  if cmake build system
            if [ << parameters.build-system >> == "cmake" ]; then
              NINJA_URL=https://github.com/ninja-build/ninja/releases/download/v1.12.1/ninja-linux.zip
              wget $NINJA_URL -O ninja-linux.zip
              unzip ninja-linux.zip -d ~/bin
            fi

            # rx-gcc is 32-bit binary
            if [[ << parameters.toolchain >> == rx-gcc ]]; then
              sudo dpkg --add-architecture i386
              sudo apt update
              sudo apt install libc6:i386 libstdc++6:i386 zlib1g:i386
            fi

            # Install Pico SDK
            if [ << parameters.family >> == "rp2040" ]; then
              git clone --depth 1 https://github.com/raspberrypi/pico-sdk.git ~/pico-sdk
              echo "export PICO_SDK_PATH=~/pico-sdk" >> $BASH_ENV
            fi

      - when:
          condition:
            not:
              equal: [esp-idf, << parameters.toolchain >>]
          steps:
            - setup-toolchain:
                toolchain: << parameters.toolchain >>

      - run:
          name: Build
          no_output_timeout: 20m
          command: |
            if [ << parameters.toolchain >> == esp-idf ]; then
              docker run --rm -v $PWD:/project -w /project espressif/idf:v5.5.3 python tools/build.py << parameters.build-args >> --target all << parameters.family >>
            else
              # Toolchain option default is gcc
              if [ << parameters.toolchain >> == arm-clang ]; then
                TOOLCHAIN_OPTION="--toolchain clang"
              elif [ << parameters.toolchain >> == arm-iar ]; then
                TOOLCHAIN_OPTION="--toolchain iar"
                iccarm --version
              elif [ << parameters.toolchain >> == arm-gcc ]; then
                TOOLCHAIN_OPTION="--toolchain gcc"
              fi

              # circleci docker return $nproc as 36 core, limit parallel to 4 (resource-class = large)
              # Required for IAR, also prevent crashed/killed by docker
              BUILD_PY_ARGS="-s << parameters.build-system >> $TOOLCHAIN_OPTION -j 4 << parameters.build-args >> --target all"
              if [ << parameters.build-system >> == "cmake" ]; then
                BUILD_PY_ARGS="$BUILD_PY_ARGS --target tinyusb_metrics"
              fi
              python tools/build.py $BUILD_PY_ARGS << parameters.family >>
            fi

      # Only collect and persist metrics for cmake builds (excluding esp-idf and --one-random)
      - when:
          condition:
            and:
              - equal: [ cmake, << parameters.build-system >> ]
              - not:
                  equal: [ esp-idf, << parameters.toolchain >> ]
              - not:
                  equal: [ arm-iar, << parameters.toolchain >> ]
          steps:
            - run:
                name: Collect Metrics
                command: |
                  # Create unique directory per toolchain to avoid workspace conflicts
                  METRICS_DIR="/tmp/metrics/<< parameters.toolchain >>"
                  mkdir -p "${METRICS_DIR}"
                  # Copy all metrics.json files
                  for f in cmake-build/cmake-build-*/metrics.json; do
                    if [ -f "$f" ]; then
                      BOARD_DIR=$(dirname "$f" | xargs basename)
                      cp "$f" "${METRICS_DIR}/${BOARD_DIR}.json"
                    fi
                  done

            - persist_to_workspace:
                root: /tmp
                paths:
                  - metrics/<< parameters.toolchain >>

jobs:
  # Build using docker
  build:
    parameters:
      resource_class:
        type: string
        default: large
      build-system:
        type: string
      toolchain:
        type: string
      family:
        type: string
      build-args:
        type: string
        default: ""

    docker:
      - image: cimg/base:current
    working_directory: ~/project/tinyusb
    resource_class: << parameters.resource_class >>

    steps:
      - build:
          build-system: << parameters.build-system >>
          toolchain: << parameters.toolchain >>
          family: << parameters.family >>
          build-args: << parameters.build-args >>

  # Build using VM
  build-vm:
    parameters:
      resource_class:
        type: string
        default: large
      build-system:
        type: string
      toolchain:
        type: string
      family:
        type: string
      build-args:
        type: string
        default: ""

    machine:
      image: ubuntu-2404:current
    working_directory: ~/project/tinyusb
    resource_class: << parameters.resource_class >>

    steps:
      - build:
          build-system: << parameters.build-system >>
          toolchain: << parameters.toolchain >>
          family: << parameters.family >>
          build-args: << parameters.build-args >>

  # Aggregate code metrics from all builds
  code-metrics:
    docker:
      - image: cimg/python:3.12
    resource_class: large
    steps:
      - checkout
      - attach_workspace:
          at: /tmp

      - run:
          name: Aggregate Code Metrics
          command: |
            python tools/get_deps.py
            # Combine all metrics files from all toolchain subdirectories
            ls -R /tmp/metrics
            if ls /tmp/metrics/*/*.json 1> /dev/null 2>&1; then
              python tools/metrics.py combine -j -m -f tinyusb/src /tmp/metrics/*/*.json
            else
              echo "No metrics files found"
              exit 1
            fi

      - store_artifacts:
          path: metrics.json
          destination: metrics.json

      # Compare with base master metrics on PR branches
      - when:
          condition:
            not:
              equal: [ master, << pipeline.git.branch >> ]
          steps:
            - run:
                name: Download Base Branch Metrics
                command: |
                  # Download metrics.json artifact from the latest successful build on master branch
                  mkdir -p base-metrics
                  # Use CircleCI API to get the latest artifact
                  curl -s -L "https://dl.circleci.com/api/v2/project/gh/${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}/latest/artifacts?branch=master&filter=successful" \
                    -H "Circle-Token: ${CIRCLE_TOKEN:-}" | \
                    jq -r '.items[] | select(.path == "metrics.json") | .url' | \
                    head -1 | xargs -I {} curl -s -L -o base-metrics/metrics.json {} || true

            - run:
                name: Compare with Base Branch
                command: |
                  if [ -f base-metrics/metrics.json ]; then
                    python tools/metrics.py compare -f tinyusb/src base-metrics/metrics.json metrics.json
                    cat metrics_compare.md
                  else
                    echo "No base metrics found, skipping comparison"
                    cp metrics.md metrics_compare.md
                  fi

            - store_artifacts:
                path: metrics_compare.md
                destination: metrics_compare.md

workflows:
  build:
    jobs:
# The jobs below are populated dynamically by config.yml set-matrix job
# Example entries that will be generated:
#      - build:
#          matrix:
#            alias: build-cmake-arm-gcc
#            parameters:
#              toolchain: [ 'arm-gcc' ]
#              build-system: [ 'cmake' ]
#              family: [ 'nrf' ]
#              resource_class: ['large']
#      - code-metrics:
#          requires:
#            - build-cmake-arm-gcc