---
- name: Build Bazel
  ansible.builtin.include_role:
    name: bazel
  vars:
    bazel_version: "{{ tfio_ver.value.bazel_version }}"

- name: Create {{ py_ver }} virtualenv
  ansible.builtin.include_role:
    name: python
    tasks_from: virtualenv.yml

- name: Install TensorFlow {{ tensorflow_version }}
  ansible.builtin.pip:
    name: "tensorflow_aarch64=={{ tensorflow_version }}"
    virtualenv: "{{ virtualenv_path }}"
    virtualenv_python: "/opt/python/{{ py_ver }}/bin/python3"
    extra_args: "--extra-index-url {{ pip_extra_index_url }}"

- name: Build TensorFlow IO {{ tensorflow_io_version }} for {{ py_ver }}  # noqa no-changed-when
  ansible.builtin.shell:
    cmd: |
      set -xe
      source {{ virtualenv_path }}/bin/activate

      python3 tools/build/configure.py
      export BAZEL_OPTIMIZATION="-j {{ ansible_processor_vcpus }}"
      bazel build   ${BAZEL_OPTIMIZATION}   -- //tensorflow_io/...  //tensorflow_io_gcs_filesystem/...

      mkdir -p build
      cp -r bazel-bin/tensorflow_io                 build/tensorflow_io
      cp -r bazel-bin/tensorflow_io_gcs_filesystem  build/tensorflow_io_gcs_filesystem
    chdir: "{{ build_dir }}/io-{{ tensorflow_io_version }}/"
    executable: /bin/bash

- name: Stop bazel server
  ansible.builtin.include_role:
    name: bazel
    tasks_from: stop.yml

- name: Go through each Python version
  ansible.builtin.include_role:
    name: tensorflow_io
    tasks_from: build_wheel.yml
  loop: "{{ tfio_ver.value.python_versions }}"
  loop_control:
    loop_var: python_version

- name: Audit TensorFlow IO wheels
  ansible.builtin.shell:
    cmd: |
      for wheel in dist/*.whl
      do
        LD_LIBRARY_PATH=$LD_LIBRARY_PATH:{{ virtualenv_path }}/lib/python3.8/site-packages/tensorflow ./tools/build/auditwheel repair -w "{{ wheels_dir }}" "$wheel"
      done
    chdir: "{{ build_dir }}/io-{{ tensorflow_io_version }}/"
    executable: /bin/bash
  when: run_audit

- name: Copy un-audited wheels into place
  ansible.builtin.shell:
    cmd: "cp {{ build_dir }}/io-{{ tensorflow_io_version }}/dist/tensorflow*.whl {{ wheels_dir }}"
  when: not run_audit
