博客论坛网站开发,深圳网站系统建设,一二年级的手工作品,购物网站cms使用 Docker 搭配 ChatGPT 创建 Makefile 构建系统
Makefile 构建系统是嵌入式软件团队实现其开发流程现代化的基础。构建系统不仅允许开发人员选择各种构建目标#xff0c;还可以将这些构建集成到持续集成/持续部署 (CI/CD) 流程中。使用诸如 ChatGPT 这样的人工智能 (AI) 工…使用 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 支持工具如 update、clean、autoconf、curl 等在 /home/dev 下设置一个工具目录安装 gcc-arm-none-eabi 工具链附带针对 newlib 的 C 支持安装 Clang 工具用于格式化和静态分析
你能生成满足上述要求的 Dockerfile 吗?
在提示符中输入这些要求后ChatGPT 生成了一些解释性文本及以下 Dockerfile 输出 Certainly! Heres 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 maintaineryour-emailexample.com # Set environment variables to non-interactive (this will prevent some prompts) ENV DEBIAN_FRONTENDnon-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-emailexample.com 替换为你的实际电子邮件或者其他标识符。
构建和运行 Docker 容器
将 Dockerfile 保存到目录中。在终端中导航到该目录。构建 Docker 镜像docker build -t arm-dev-env.运行 Docker 容器docker run -it arm-dev-env
这样会在 Docker 容器中为你提供一个 bash 提示并且你指定的所有工具均已安装完成并准备就绪可供使用。
近几年来我编写了多个 Dockerfile以供各种构建系统使用我授课时会用到这些构建系统也会把它们给客户。通过对比 ChatGPT 生成的内容我发现有几处细微差异
我编写的 Dockerfile 中不包含维护人标签因为我不会广泛发布我的构建系统。但是我使用刷新标签来标识上次更新 Dockerfile 的时间。其实有点浪费因为其实我只要检查 Git 仓库就行了。ChatGPT 额外包含了 ENV DEBIAN_FRONTENDnon-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 you’re 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 doesn’t have the necessary CA certificates installed by default. Here’s the updated Dockerfile”: # Use the latest version of Ubuntu as the base image FROM ubuntu:latest # Set the maintainer label LABEL maintaineryour-emailexample.com # Set environment variables to non-interactive (this will prevent some prompts) ENV DEBIAN_FRONTENDnon-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 maintaineryour-emailexample.com # Set environment variables to non-interactive (this will prevent some prompts) ENV DEBIAN_FRONTENDnon-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 maintaineryour-emailexample.com # Set environment variables to non-interactive (this will prevent some prompts) ENV DEBIAN_FRONTENDnon-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而且我知道应该会生成怎样的 Dockerfile。ChatGPT 能做到吗让我们一探究竟。我回到提示符中添加了以下内容 ChatGPT 则提供了如下输出 # Use the latest version of Ubuntu as the base image FROM ubuntu:latest # Set the maintainer label LABEL maintaineryour-emailexample.com # Set environment variables to non-interactive (this will prevent some prompts) ENV DEBIAN_FRONTENDnon-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 能做到吗你很快就会知道了。