Creating a Makefile build system using ChatGPT: Running Docker … – Embedded

Advertisement

Advertisement
Embedded.com
Advertisement
Developing an efficient and modern build system is critical to ensuring you can meet your development cycle’s demands. Traditionally, embedded developers have relied on integrated build systems using vendor-supplied IDEs like Eclipse. Unfortunately, these tools hide many essential details and can be suboptimal when integrating them with modern processes like Docker and CI/CD solutions.
In our last post, Creating a Makefile Build System using ChatGPT: Starting with Docker, we developed a custom build system by creating a Docker container. We didn’t traditionally create the Dockerfile. Instead, we leverage the artificial intelligence (AI) tool ChatGPT to do it for us!
In this post, we will continue this work by using ChatGPT to create an initial Makefile that will let us create our Docker container and load it.
Defining our initial requirements
Before jumping straight into a ChatGPT prompt, you should stop for a moment to consider what exactly you want the tool to do. At this point, you have a Dockerfile from the last post, but you want to create a makefile that can be used to do the following:
That might be enough to let the AI run loose, but I prefer having more control over the output. For example, what make recipe do I want to be associated with build and run? Everyone will have their naming conventions, but for this exercise, I would like to tell the AI to create recipes for the following:
If you know anything about docker, you must also provide a name for your image and container. I’m going to use beningo/cpp-dev for mine. (Ah! CPP must mean C++, but we are embedded developers! Yes, the image we put together will allow us to compile embedded code for C/C++!).
Generate the Makefile, Engage!
Now that we have some basic requirements let’s craft a prompt for the start of our makefile. The first thing that I like to do is give the tool some context. So, my first prompt is nothing more than the following:
“Don’t do anything with this yet, but here is a Dockerfile that I have created for building embedded software:”
In addition to that sentence, I would also paste the Dockerfile generated in the last post. I’ve removed the text to keep things readable and manageable.
You’ll likely find if you follow along that ChatGPT can’t help itself and will analyze the Dockerfile. You can ignore it. If you ever need to pay for a tool, they charge by the amount of text generated (tokens), which explains why it’s so verbose in its responses.
With the context in place, we can now ask the tool for what we want:
click for full size image

The response that ChatGPT generates is as follows:
click for full size image

Not bad! Let’s try to run this makefile and see what happens.
Building a Docker Image with Make
If you are familiar with running makefiles, you’ll know it’s pretty easy to run these commands. Before you can run the image, though, you need to build it. So, start by running the following command in a terminal:
You should find that Docker will run and use your Dockerfile to create the container. I assume you put the Dockerfile and the Makefile in the same directory. If you didn’t, you might find that you have some make errors. It’s okay; just put them in the same directory and try it again. (Note: I’m also assuming, if you are trying this, that you installed Docker Desktop too!).
You will see that Docker will run and begin building your container. The output can be a bit verbose, so I won’t show it here. However, I will show you what happens when I run the command on an already-built image. You can see this below:
click for full size image

As you can see, there are, in total, about eleven steps that are followed to download tools and configure the image. If you look closely, it builds super-fast for me! The reason is that I’ve already run this command, and there isn’t anything for docker_image to do. You may find it takes 5 – 10 minutes for it to run on your machine entirely.
Running a Docker Image with Make
Now that you have an image on hand, you can run the image by typing the command:
When you do this, you’ll find yourself in a prompt that looks something like the following:
click for full size image

Congratulations! You are now running your Docker container and can build code and use the tools included in your image build. However, if you haven’t noticed yet, there is a problem!
If you put a main.c file into your project directory, and within your terminal, type ls; you’ll discover that you can’t see your code! How can you use your Docker container to build code when you can’t even see it? You need to make some changes to your docker_run recipe!
Back to ChatGPT for fixes
It’s time to craft a new prompt to fix our issues. I already know that the problem is related to docker_run not including the -v privilege option. In my own makefiles, I use something that looks like the following:
Let’s see if ChatGPT comes up with a similar solution based on my new prompt:
click for full size image

The results are similar but a little bit different:
click for full size image

Similar, but there are a couple of differences. First, I used $(PWD), not $(shell pwd). What’s the difference? Well, when I asked ChatGPT, I received the following response:
click for full size image

Hmm. I am not going to lie. I do like the more explicit approach of using the shell. While $(PWD) works, and I’ve never had a problem with it, using the shell to avoid path variable issues is much more desirable. (I also wouldn’t unquestioningly accept this, but verify it using a second source).
The difference between my bash and ChatGPT’s /bin/bash is similar. It’s just more explicit and less likely to cause issues.
With these adjustments, if you now run:
and then do a ls; you’ll see that you can access your local source code. Be warned that we are now giving our container access to the local kernel. There are security repercussions for doing so. This might not be an excellent idea for a web or cloud developer. For us embedded folks just looking to build our code, it’s probably okay, but you should carefully investigate the risks.
Conclusions
You’ve now had the opportunity to see ChatGPT generate a Dockerfile and a simple makefile to run it. The output from the tool isn’t perfect, but careful review and testing with some feedback can produce some exciting results. I hope you see that you can’t just ignore AI tools and that they already have a place in embedded software development.
Next time, we’ll put some example source code into the directory and use ChatGPT to add additional features to our makefile. Before too long, we’ll have a completely modern build system that dramatically improves how you develop embedded software.
Jacob Beningo is an embedded software consultant who specializes in real-time, microcontroller-based systems. He actively promotes software best practices through numerous articles, blogs, and webinars on topics from software architecture design, embedded DevOps, and implementation techniques. Jacob has 20 years of experience in the field and holds three degrees including a Masters of Engineering from the University of Michigan.
Related Contents:

Advertisement
You must or to post a comment.
This site uses Akismet to reduce spam. Learn how your comment data is processed.
Advertisement
Advertisement
Advertisement

source

Jesse
https://playwithchatgtp.com