使用 ChatGPT 创建 Makefile 构建系统:从 Docker 开始

使用 Docker 搭配 ChatGPT 创建 Makefile 构建系统

Makefile 构建系统是嵌入式软件团队实现其开发流程现代化的基础。构建系统不仅允许开发人员选择各种构建目标,还可以将这些构建集成到持续集成/持续部署 (CI/CD) 流程中。使用诸如 ChatGPT 这样的人工智能 (AI) 工具则能够提升这个现代化过程的趣味性,因为现在这些工具可以提供反馈,在某些情况下甚至能为你开发构建系统。在这个系列中,我会使用 ChatGPT Docker 开始着手创建 Makefile 构建系统。

Makefile 构建系统需要什么

无论你有没有意识到,大多数嵌入式软件 IDE 背后都是基于 Makefile 的构建系统。此类构建系统可能直接使用 GNU Make,也可能使用 Cmake。就本文而言,使用什么工具我真不在乎。使用 AI 工具能轻松生成所需的代码,无论是用于 GNU Make 还是 Cmake 都不在话下。首先是规划构建系统大纲。在我的如何定义你的理想化嵌入式构建系统一文和如何定义你的理想化嵌入式 CI/CD 流水线一文中,我提到了如何定义理想化的连续集成/连续部署流水线和构建系统。我强烈建议你阅读这些文章,并妥善制定相应策略,然后再尝试使用 AI 工具来完成工作。

出于试验目的,我将按图 1 所示定义理想化的构建系统。

1 - 结合任何现代化嵌入式系统的必要构建配置和软件堆栈的理想构建系统。(来源:嵌入式软件设计;第 23 页)

至此,就可以开始使用 ChatGPT AI 工具逐步创建构建系统了。但如果你有兴趣了解可供嵌入式软件开发人员使用的其他 AI 工具,可以查看我写的揭示嵌入式软件开发者可用的 5 大 AI 工具一文。

我还发现,使用 Docker 来封装所有工具和依赖项对于创建构建系统也同样极为有用可能许多公司的 IT 部门对于 Docker 深恶痛绝,但是它可以显著简化更新共享,帮助开发人员快速上手。如果你使用 Visual Studio Code 之类的 IDE,那么还会发现可以通过终端使用 Docker 和构建系统,或者也可以自定义 Visual Studio Code 通过用户接口来执行命令使用 AI 创建构建系统之前,我们将使用它来创建一个 Dockerfile,使用 Docker Desktop 来执行它,这将创建一个容器用于构建软件。

ChatGPT 中输入第一个提示

我给 ChatGPT 的第一个提示是告诉它我想要做什么,然后列出我的要求。例如:

我想创建一个 Dockerfile,用于为 Cortex®-M4 架构处理器进行嵌入式软件开发。这个 Dockerfile 具有以下要求:

  • 使用最新版本的 Ubuntu
  • 安装基本 Linux 支持工具,如 updatecleanautoconfcurl
  • /home/dev 下设置一个工具目录
  • 安装 gcc-arm-none-eabi 工具链,附带针对 newlib C++ 支持
  • 安装 Clang 工具用于格式化和静态分析

你能生成满足上述要求的 Dockerfile ?

在提示符中输入这些要求后,ChatGPT 生成了一些解释性文本及以下 Dockerfile 输出:

Certainly! Here's a `Dockerfile` that meets your requirements:

```Dockerfile

# Use the latest version of Ubuntu as the base image

FROM ubuntu:latest

# Set the maintainer label

LABEL maintainer="your-email@example.com"

# Set environment variables to non-interactive (this will prevent some prompts)

ENV DEBIAN_FRONTEND=non-interactive

# Install basic linux support tools

RUN apt-get update -y && \

    apt-get install -y --no-install-recommends \

    autoconf \

    curl \

    build-essential \

    git \

    libtool \

    make \

    pkg-config && \

    apt-get clean && \

    rm -rf /var/lib/apt/lists/*

# Setup a tool directory in /home/dev

RUN mkdir -p /home/dev

WORKDIR /home/dev

# Install the gcc-arm-none-eabi toolchain with C++ support for newlib

RUN apt-get update -y && \

    apt-get install -y --no-install-recommends software-properties-common && \

    apt-add-repository ppa:team-gcc-arm-embedded/ppa && \

    apt-get update -y && \

    apt-get install -y --no-install-recommends gcc-arm-embedded && \

    apt-get clean && \

    rm -rf /var/lib/apt/lists/*

# Install clang tools for formatting and static analysis

RUN apt-get update -y && \

    apt-get install -y --no-install-recommends clang-format clang-tidy && \

    apt-get clean && \

    rm -rf /var/lib/apt/lists/*

# Set the default command to bash

CMD ["/bin/bash"]

请注意把 `your-email@example.com` 替换为你的实际电子邮件或者其他标识符。

构建和运行 Docker 容器:

  1. Dockerfile 保存到目录中。
  2. 在终端中导航到该目录。
  3. 构建 Docker 镜像:`docker build -t arm-dev-env.`
  4. 运行 Docker 容器:`docker run -it arm-dev-env`

这样会在 Docker 容器中为你提供一个 bash 提示,并且你指定的所有工具均已安装完成,并准备就绪可供使用。

近几年来,我编写了多个 Dockerfile,以供各种构建系统使用,我授课时会用到这些构建系统,也会把它们给客户。通过对比 ChatGPT 生成的内容,我发现有几处细微差异:

  • 我编写的 Dockerfile 中不包含维护人标签,因为我不会广泛发布我的构建系统。但是,我使用刷新标签来标识上次更新 Dockerfile 的时间。(其实有点浪费,因为其实我只要检查 Git 仓库就行了)。
  • ChatGPT 额外包含了 ENV DEBIAN_FRONTEND=non-interactive,这招不错。我通常只在安装单个封装包时才会包含这部分。ENV 变量在整个 Dockerfile 中全局包含了这部分。
  • ARM 编译器是使用 PPA 服务安装的,而不是通过直接下载或其他方法。
  • 默认命令设置为 bash。我从未显式包含此命令,但我觉得这个做法不错。

Dockerfile 看起来与我写的非常相似,但效率也不高。你只要仔细查看,就能看到其中多次运行更新,并且对文件进行了清理和移除操作。就首通 (first pass) 而言,也算不错了。

修复首通的问题

事实证明,如果用以下命令来运行 ChatGPT 生成的第一个 Dockerfile

  docker build -t beningo/cpp-dev

你会发现 Docker 镜像会失败! 事实证明,使用 PPA 可能会导致一些问题,而且可能与最新版本的 Ubuntu 不兼容。我使用以下提示将此错误输入 ChatGPT 后:

它就会识别这个问题并自动调整方法。这次它决定直接下载 ARM 编译器可执行文件并进行安装,如下 Dockerfile 所示:

# Install the gcc-arm-none-eabi toolchain with C++ support for newlib

RUN curl -L https://developer.arm.com/-/media/Files/downloads/gnu-rm/10-2020q4/gcc-arm-none-eabi-10-2020-q4-major-x86_64-linux.tar.bz2 | tar xj && \

    echo "export PATH=$PATH:/home/dev/gcc-arm-none-eabi-10-2020-q4-major/bin" >> ~/.bashrc

说到这里,你可能会很兴奋,但我可不是第一次肆无忌惮玩野了。通过检查 Dockerfile 可见,我以前就用过这种下载方式,并且遇到过 SSL 证书问题。不过既然要让 AI 驱动,我决定这次循规蹈矩,运行一下 Dockerfile。看到 SSL 错误后,我向 ChatGPT 发送了以下提示:

ChatGPT 能够获取错误代码并识别问题所在。它称:

The error youre seeing is due to curl not being able to verify the SSL certificate when trying to fetch the ARM toolchain from the website. This is because the Docker image doesnt have the necessary CA certificates installed by default. Heres the updated Dockerfile:

# Use the latest version of Ubuntu as the base image

FROM ubuntu:latest

# Set the maintainer label

LABEL maintainer="your-email@example.com"

# Set environment variables to non-interactive (this will prevent some prompts)

ENV DEBIAN_FRONTEND=non-interactive

# Install basic linux support tools and ca-certificates for SSL verification

RUN apt-get update -y && \

    apt-get install -y --no-install-recommends \

    autoconf \

    curl \

    build-essential \

    git \

    libtool \

    make \

    pkg-config \

    ca-certificates && \

    apt-get clean && \

    rm -rf /var/lib/apt/lists/*

# Setup a tool directory in /home/dev

RUN mkdir -p /home/dev

WORKDIR /home/dev

# Install the gcc-arm-none-eabi toolchain with C++ support for newlib

RUN curl -L https://developer.arm.com/-/media/Files/downloads/gnu-rm/10-2020q4/gcc-arm-none-eabi-10-2020-q4-major-x86_64-linux.tar.bz2 | tar xj && \

    echo "export PATH=$PATH:/home/dev/gcc-arm-none-eabi-10-2020-q4-major/bin" >> ~/.bashrc

# Install clang tools for formatting and static analysis

RUN apt-get update -y && \

    apt-get install -y --no-install-recommends clang-format clang-tidy && \

    apt-get clean && \

    rm -rf /var/lib/apt/lists/*

# Set the default command to bash

CMD ["/bin/bash"]

瞧!这回得到的 Dockerfile 能成功构建了!

测试 Docker 镜像

我启动 Docker 镜像时,很高兴地发现我的所有工具都安装好了,而且镜像也在正常运行。不过,我注意到了一个小问题。当我启动镜像时,总是会进入 /home/dev 文件夹中,而不是我的应用程序所在的 /home/app 文件夹。在我自己的 Dockerfiles 中,我在文件末尾添加了以下内容:

WORKDIR /home/app

ChatGPT 也会提供同样的解决方案吗?接下来,我输入了以下提示:

我得到的响应是:

# Use the latest version of Ubuntu as the base image

FROM ubuntu:latest

# Set the maintainer label

LABEL maintainer="your-email@example.com"

# Set environment variables to non-interactive (this will prevent some prompts)

ENV DEBIAN_FRONTEND=non-interactive

# Install basic linux support tools and ca-certificates for SSL verification

RUN apt-get update -y && \

    apt-get install -y --no-install-recommends \

    autoconf \

    curl \

    build-essential \

    git \

    libtool \

    make \

    pkg-config \

    ca-certificates && \

    apt-get clean && \

    rm -rf /var/lib/apt/lists/*

# Setup a tool directory in /home/dev

RUN mkdir -p /home/dev

# Install the gcc-arm-none-eabi toolchain with C++ support for newlib in /home/dev

WORKDIR /home/dev

RUN curl -L https://developer.arm.com/-/media/Files/downloads/gnu-rm/10-2020q4/gcc-arm-none-eabi-10-2020-q4-major-x86_64-linux.tar.bz2 | tar xj && \

    echo "export PATH=$PATH:/home/dev/gcc-arm-none-eabi-10-2020-q4-major/bin" >> ~/.bashrc

# Install clang tools for formatting and static analysis

RUN apt-get update -y && \

    apt-get install -y --no-install-recommends clang-format clang-tidy && \

    apt-get clean && \

    rm -rf /var/lib/apt/lists/*

# Create and set the working directory to /home/app

WORKDIR /home/app

# Set the default command to bash

CMD ["/bin/bash"]

成功!ChatGPT 得出了与我相同的解决方案和结论!

重构和清理 Dockerfile 

到目前为止,ChatGPT 工作非常出色!不过还有一个问题,就是 Dockerfile 的优化仍不及预期。我可以自己重来一遍,手动对它进行优化......或者让 AI 来做!回到 ChatGPT 中,我输入了以下内容作为下一项提示:

Dockerfile 效率不高,原因可能是多次安装和清理。可以改善吗?

得到的响应是:

修改后的 Dockerfile

# Use the latest version of Ubuntu as the base image

FROM ubuntu:latest

# Set the maintainer label

LABEL maintainer="your-email@example.com"

# Set environment variables to non-interactive (this will prevent some prompts)

ENV DEBIAN_FRONTEND=non-interactive

# Update package lists, install basic tools, toolchains, and clean up in one step

RUN apt-get update -y && \

    apt-get install -y --no-install-recommends \

    autoconf \

    curl \

    build-essential \

    git \

    libtool \

    make \

    pkg-config \

    ca-certificates \

    software-properties-common \

    clang-format \

    clang-tidy && \

    apt-get clean && \

    rm -rf /var/lib/apt/lists/*

# Setup a tool directory in /home/dev and download the ARM toolchain

WORKDIR /home/dev

RUN curl -L https://developer.arm.com/-/media/Files/downloads/gnu-rm/10-2020q4/gcc-arm-none-eabi-10-2020-q4-major-x86_64-linux.tar.bz2 | tar xj && \

    echo "export PATH=$PATH:/home/dev/gcc-arm-none-eabi-10-2020-q4-major/bin" >> ~/.bashrc

# Create and set the working directory to /home/app

WORKDIR /home/app

# Set the default command to bash

CMD ["/bin/bash"]

我不知道你怎么想,但我很兴奋。最新的 dockerfile 看起来与我近几年来从头开始写的 Dockerfile 非常相似。我仍然使用不同的方法来安装 Arm 编译器,但目前我们 Dockerfile 中的方法并不太寒酸。(不过它已经被修复了,所以我们没法知道是否有新版本的 GNU Arm 编译器可用。奇怪的是,已经有大概两年没更新了。我不知道什么原因,但这个话题下次再讨论)。

添加测试工具

有时,即使面对如此精品,你也会忍不住对它“调教”一番。毕竟,只要弄不坏它,那就可以试试把它做得更好,对吧?眼下有一个 Dockerfile,其中包含为 Arm 微控制器编译代码所需的工具。但实际上,我们还应该在这个镜像中添加不少东西。比如指标分析,可能最重要的是,添加测试工具?我觉得在 Dockerfile 中添加 cpputest 会很有趣。毕竟,我自己的许多工程都会用 cpputest,而且我知道应该会生成怎样的 DockerfileChatGPT 能做到吗?让我们一探究竟。我回到提示符中,添加了以下内容:

ChatGPT 则提供了如下输出:

# Use the latest version of Ubuntu as the base image

FROM ubuntu:latest

# Set the maintainer label

LABEL maintainer="your-email@example.com"

# Set environment variables to non-interactive (this will prevent some prompts)

ENV DEBIAN_FRONTEND=non-interactive

# Update package lists, install basic tools, toolchains, and clean up in one step

RUN apt-get update -y && \

    apt-get install -y --no-install-recommends \

    autoconf \

    automake \

    curl \

    build-essential \

    git \

    libtool \

    make \

    pkg-config \

    ca-certificates \

    software-properties-common \

    clang-format \

    clang-tidy && \

    apt-get clean && \

    rm -rf /var/lib/apt/lists/*

# Setup a tool directory in /home/dev and download the ARM toolchain

WORKDIR /home/dev

RUN curl -L https://developer.arm.com/-/media/Files/downloads/gnu-rm/10-2020q4/gcc-arm-none-eabi-10-2020-q4-major-x86_64-linux.tar.bz2 | tar xj && \

    echo "export PATH=$PATH:/home/dev/gcc-arm-none-eabi-10-2020-q4-major/bin" >> ~/.bashrc

# Clone, build, and install CppUTest

WORKDIR /home

RUN git clone https://github.com/cpputest/cpputest.git && \

    cd cpputest && \

   ./autogen.sh && \

   ./configure && \

    make install

# Set the CPPUTEST_HOME environment variable

ENV CPPUTEST_HOME=/home/cpputest

# Create and set the working directory to /home/app

WORKDIR /home/app

# Set the default command to bash

CMD ["/bin/bash"]

没错。这基本上与我自己的 Dockerfile 中的内容差不多。哪天我不得不从头写个文件时,就可以用 ChatGPT 5 分钟内完成。

结论

ChatGPT 这样的 AI 工具可以用来编写和改进你的嵌入式构建系统。这篇文章探讨了如何使用 ChatGPT 写一个 Dockerfile,并且其中包括我在开发嵌入式软件时使用的一些工具。这个示例虽然有一定的局限性,但它展示了如何使用 AI 工具快速创建代码草稿,并对其进行快速迭代。我之所以能快速完成这些,是因为我已经为我使用的各种构建系统写了很多 Dockerfile。我拥有丰富的经验,知道 AI 给的输出究竟是有效的、需要改进的还是一文不值的。我相信,如果没有这些经验,你难免会发现自己深陷泥潭难以自拔。

下次,我们来聊聊如何使用 AI 从头生成一个构建系统,然后搭配 Dockerfile 一起使用。你觉得 AI 能做到吗?你很快就会知道了。

相关推荐

  1. 开始:如何使用Docker构建微服务架构

    2023-12-05 22:36:06       43 阅读
  2. docker创建使用

    2023-12-05 22:36:06       35 阅读
  3. 基于 Makefile 的 FPGA 构建系统

    2023-12-05 22:36:06       42 阅读
  4. 使用Pytorch开始构建RNN

    2023-12-05 22:36:06       57 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-05 22:36:06       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-05 22:36:06       106 阅读
  3. 在Django里面运行非项目文件

    2023-12-05 22:36:06       87 阅读
  4. Python语言-面向对象

    2023-12-05 22:36:06       96 阅读

热门阅读

  1. Python学习杂记

    2023-12-05 22:36:06       55 阅读
  2. 实用攻略——SD-WAN网络配置步骤详解

    2023-12-05 22:36:06       66 阅读
  3. Spring Boot项目打包指定包名

    2023-12-05 22:36:06       60 阅读
  4. PTA 7-236 验证哥德巴赫猜想

    2023-12-05 22:36:06       64 阅读
  5. convert_from_pinhole_camera_parameters 失败

    2023-12-05 22:36:06       62 阅读
  6. redis中使用lua脚本处理业务逻辑

    2023-12-05 22:36:06       57 阅读
  7. WPF-本地保存登录账号密码

    2023-12-05 22:36:06       57 阅读
  8. k8s(一):基本概念

    2023-12-05 22:36:06       56 阅读