Creating a makefile build system using ChatGPT: Makefile … – Embedded
Advertisement
Advertisement
Embedded.com
Advertisement
In our last post, Creating a Makefile Build System using ChatGPT: Running Docker with Make, we built and ran a Docker container. Until now, we haven’t written any code ourselves, instead relying on the artificial intelligence (AI) tool ChatGPT to do it for us!
In this post, we will continue this work by using ChatGPT to update and improve the Makefile to manage a simulation and debug target. We won’t worry about cross-compiling for an Arm target today, but we will use Docker to compile and run our simulation target.
Let’s get started!
Creating “Hello World”
The first step is to have a simple application we can compile and run to verify our build environment so far. You don’t have to get too complicated compiling your first application on your new build system. A simple “Hello World” application will do just fine. While any developer can write one of these themselves, why not use ChatGPT?
Prompt: “Can you autogenerate a simple main.c file that prints ‘Hello World!’ ?”
ChatGPT: “Of course! Here’s a simple ‘main.c’ file that prints “Hello World!” using the C standard library:”
This is all we need to test a makefile and build a system.
Defining your project structure
You can’t just tell ChatGPT to create a makefile to build your project and expect to get good results. It would be best to start by telling it your desired directory structure. Every developer and manager reading this post will have their preferences. For our example, I’m going to use the following directory structure:
You might like this structure or something else. Reviewing this directory template, I see places where I deviate from this structure. It is essential to define what structure you will use for your project and remember that it can change over time.
We need at least a starting directory structure so that ChatGPT can understand our project organization and generate paths to our files. If it doesn’t know where things are, our makefile will be super generic and useless.
Creating a Simulation Target
Let’s now leverage the power of ChatGPT to get our makefile started. I used the following prompt to get started, which you can see specifies our basic needs:
Prompt: “I need to create a makefile for an embedded project that can compile for an arm cortex-m processor for a debug target and a simulation target that can run in a Linux docker container. My directory structure is as follows:
Can you create the makefile?”
ChatGPT: “Certainly! The Makefile I’ll provide will be a basic example and might need adjustments depending on the specifics of your setup (like compiler options, MCU specifics, etc.).
In the example below, I’ll assume you’re using arm-none-eabi-gcc for compiling for the ARM Cortex-M and gcc for the simulation target.”:
I like that ChatGPT tells us upfront that we must change the Makefile. This isn’t something that is just generated and done. If you carefully review the makefile, you’ll find it should compile our simulation target without any problems. If I provide a reasonable linker file, our debug target might compile, but we probably wouldn’t want to run it on our target without additional adjustments. (We’ll save those for a future post).
Analyzing the Makefile
When analyzing the output from any AI tool, we must get nit-picky. It helps remind us that this tool isn’t perfect, and we can’t trust what it is generating for us. I found it super interesting that when I requested the makefile, ChatGPT also included the following notes:
Notes:
The tool reminds us that this makefile pass isn’t perfect! We will need to modify it to reach our own end goals. For us today, I just wanted to see us compile and run a “Hello World” application within Docker. That leaves the question, “Can we use this makefile to compile the application?”.
I’d probably make several changes to this makefile, but I plan to expand on it and optimize it in future posts, so we’ll save that discussion for then. Instead, let’s see if this first pass output can compile and run a simulation target.
Compiling the “Hello World” Application
I have the “Hello World” program in the firmware/app path in a main.c file. Before I try to compile it, I’ll want to run my Docker container. You might have noticed that the docker recipes are no longer in the generated makefile! There are several ways to add them and merge this makefile and the last one together. I found the easiest way to copy the above linker file and replace the.phony line with the following:
and enter your Docker environment.
Now, try to run make simulation. You may find that you get the following compilation error:
The issue is that the generated simulation recipe isn’t quite correct. It’s not making the output directory if it doesn’t exist! Asking ChatGPT about the issue, it provided the following correction:
If you make these changes, you’ll find that it does, in fact, fix the issue. If you rerun the make simulation, you’ll find that the “Hello World” application does compile successfully. If you run./output/simulation, you’ll see the following output:
Success! Well, at least for getting the simulation target up and running. If you look closely at the makefile, you’ll find that we could make many improvements. We will look at these in a future post. For now, be excited that we have an AI-generated build system that took about 2 minutes to create!
Compiling a “Hello World” project may not be impressive, but it’s always the first critical step in proving a build system works. Today, you saw a few simple prompts we used to create a makefile with two additional targets: simulation and debug. The starting makefile isn’t too impressive. That is planned, though. We’re slowly walking through each step and prompt, leaving us with an excellent, modern build system.
Next time, we’ll focus on getting “Hello World” to compile for an Arm Cortex-M target and verify that we can collect for debug and simulation targets. 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 Sign in or Register to post a comment.
This site uses Akismet to reduce spam. Learn how your comment data is processed.
Advertisement
Advertisement
Advertisement