OpenAI + ChatGPT can even convert build systems
I have been playing with OpenAI/ChatGPT a lot over the last few days and I am constantly amazed at how will it works and what it can do. I've had it write programs from project euler, talk about historical facts, write fictional stories based on crazy parameters (a taco eating cat that attacked a hot dog stand and eats purple chocolate)...
but one of the hardest things I can imagine is converting a build system. There are so many ways to do it. I tried first with spin2cpp's makefile. That was, apparently, a bit too complicated and it just said something along the lines of "here's a basic cmake script" with a bunch of placeholders that i would have to fill out. Bummer. So then I searched for a bit simpler makefile and found p2load.
Is this specific example useful? Probably not. Not least of which because the owner of p2load probably doesn't care
But that doesn't change the fact that I think it is INCREDIBLE how well this works and how quick/easy it is to adjust the output based on human speech.
Enjoy the following conversation I had just now:
David Zemon:
can you convert this gnu make file to cmake
MKDIR=mkdir -p
RM=rm
CC=cc
ECHO=echoPROPCC=propeller-elf-gcc
PROPCFLAGS=-mp2
PROPOBJCOPY=propeller-elf-objcopyOS?=macosx
ifeq ($(OS),linux)
CFLAGS+=-DLINUX
EXT=
OSINT=osint_linux
LIBS=
endififeq ($(OS),cygwin)
CFLAGS+=-DCYGWIN
EXT=.exe
OSINT=osint_cygwin enumcom
LIBS=-lsetupapi
endififeq ($(OS),msys)
CFLAGS += -DMINGW
EXT=.exe
OSINT=osint_mingw enumcom
LIBS=-lsetupapi
endififeq ($(OS),macosx)
CFLAGS+=-DMACOSX
EXT=
OSINT=osint_linux
LIBS=
endififeq ($(OS),)
$(error OS not set)
endifSRCDIR=src
OBJDIR=obj/$(OS)
PROPOBJDIR=obj/propeller
BINDIR=bin/$(OS)TARGET=$(BINDIR)/p2load$(EXT)
HDRS=
OBJS=\
$(OBJDIR)/p2load.o \
$(OBJDIR)/loadelf.o \
$(OBJDIR)/p2_loadimage.o \
$(OBJDIR)/loader.o \
$(OBJDIR)/booter.o \
$(foreach x, $(OSINT), $(OBJDIR)/$(x).o)CFLAGS+=-Wall
LDFLAGS=$(CFLAGS).PHONY: default
default: $(TARGET)DIRS=$(OBJDIR) $(PROPOBJDIR) $(BINDIR)
$(TARGET): $(BINDIR) $(OBJDIR) $(OBJS)
@$(CC) $(LDFLAGS) -o $@ $(OBJS) $(LIBS)
@$(ECHO) link $@$(OBJDIR)/%.o: $(SRCDIR)/%.c $(HDRS) $(OBJDIR)
@$(CC) $(CFLAGS) -c $< -o $@
@$(ECHO) $(CC) $@$(OBJDIR)/%.o: $(OBJDIR)/%.c $(HDRS) $(OBJDIR)
@$(CC) $(CFLAGS) -c $< -o $@
@$(ECHO) $(CC) $@$(PROPOBJDIR)/%.o: $(SRCDIR)/%.s $(HDRS) $(PROPOBJDIR)
@$(PROPCC) $(PROPCFLAGS) -nostdlib $< -o $@
@$(ECHO) $(PROPCC) $@$(PROPOBJDIR)/%.bin: $(PROPOBJDIR)/%.o $(HDRS)
@$(PROPOBJCOPY) -O binary $< $@
@$(ECHO) cc $@$(OBJDIR)/%.c: $(PROPOBJDIR)/%.bin bin2c $(OBJDIR)
@$(BINDIR)/bin2c$(EXT) $< $@
@$(ECHO) bin2c $@.PHONY: bin2c
bin2c: $(BINDIR)/bin2c$(EXT)$(BINDIR)/bin2c$(EXT): $(SRCDIR)/tools/bin2c.c $(BINDIR)
@$(CC) $(CFLAGS) $(LDFLAGS) $(SRCDIR)/tools/bin2c.c -o $@
@$(ECHO) cc $@$(DIRS):
$(MKDIR) $@.PHONY: clean
clean:
@$(RM) -f -r $(OBJDIR)
@$(RM) -f -r $(PROPOBJDIR)
@$(RM) -f -r $(BINDIR).PHONY:
clean-all: clean
@$(RM) -f -r obj
@$(RM) -f -r bin
ChatGPT:
Here is a basic CMakeLists.txt file that should accomplish the same thing as the given GNU makefile:
# Set the minimum version of CMake required to build this project. cmake_minimum_required(VERSION 3.10) # Set the project name. project(p2load) # Set the compiler to use. set(CC cc) # Set the flags to use for the C and C++ compilers. set(CFLAGS -Wall -mp2) # Set the source files for the project. set(SRCS src/p2load.c src/loadelf.c src/p2_loadimage.c src/loader.c src/booter.c src/osint_linux.c src/enumcom.c ) # Set the output directories for the object files and the binary. set(OBJ_DIR obj/propeller) set(BIN_DIR bin/macosx) # Add a custom target to build the binary. add_custom_target(p2load COMMAND ${CC} ${CFLAGS} ${SRCS} -o ${BIN_DIR}/p2load DEPENDS ${SRCS} ) # Set the default target to build. set(default p2load)There are a few differences between this CMake script and the original makefile. For example, the CMake script does not explicitly create the output directories, but CMake will create them if they do not already exist when building the project. Additionally, the CMake script does not define the MKDIR, RM, or ECHO variables, as these are not used in the script.
It's also worth noting that the CMake script sets the CC variable to cc, while the original makefile sets it to cc. This means that the CMake script will use the cc compiler, which may not be the same as the cc compiler used by the makefile. You may need to modify the CMake script to use the correct compiler for your system.
David Zemon:
p2load is an executable. can you update the cmake script now that you know that
ChatGPT
Sure! Here is an updated version of the CMake script that includes a build target for the p2load binary:
# Set the minimum version of CMake required to build this project. cmake_minimum_required(VERSION 3.10) # Set the project name. project(p2load) # Set the compiler to use. set(CC cc) # Set the flags to use for the C and C++ compilers. set(CFLAGS -Wall -mp2) # Set the source files for the project. set(SRCS src/p2load.c src/loadelf.c src/p2_loadimage.c src/loader.c src/booter.c src/osint_linux.c src/enumcom.c ) # Set the output directories for the object files and the binary. set(OBJ_DIR obj/propeller) set(BIN_DIR bin/macosx) # Add a build target for the p2load binary. add_executable(p2load ${SRCS}) # Set the output directory for the p2load binary. set_target_properties(p2load PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${BIN_DIR} ) # Set the default target to build. set(default p2load)This script defines a p2load target that uses the add_executable command to create an executable binary from the specified source files. It also sets the output directory for the binary using the set_target_properties command. The default target is then set to p2load, so running cmake without any arguments will build the p2load binary.
You may need to modify the CFLAGS and BIN_DIR variables to match the settings in the original makefile. Additionally, you may need to adjust the CC variable if the cc compiler used by the CMake script is not the same as the one used by the makefile.
David Zemon:
doesn't cmake set the compiler for you? that variable isn't needed, is it?
ChatGPT:
CMake does not set the compiler for you automatically. Instead, it provides a default set of compilers that will be used if you do not specify a compiler explicitly. This default set of compilers is determined by the platform and the environment in which CMake is running.
In the case of the original makefile, the CC variable is set to cc, which specifies the compiler to use. This is necessary because the makefile uses the cc compiler, which may not be the same as the default compiler used by CMake.
In the CMake script, the CC variable is also set to cc to match the original makefile. However, this is not strictly necessary, as you could simply remove the CC variable and CMake would use the default compiler.
In general, it's a good idea to specify the compiler to use explicitly in your CMake scripts, rather than relying on the default compilers provided by CMake. This ensures that your build scripts will work consistently across different platforms and environments.
David Zemon:
i'm okay using the default compiler available on the system. can you update the cmake script knowing that?
ChatGPT:
Sure! Here is an updated version of the CMake script that does not specify a compiler explicitly:
# Set the minimum version of CMake required to build this project. cmake_minimum_required(VERSION 3.10) # Set the project name. project(p2load) # Set the flags to use for the C and C++ compilers. set(CFLAGS -Wall -mp2) # Set the source files for the project. set(SRCS src/p2load.c src/loadelf.c src/p2_loadimage.c src/loader.c src/booter.c src/osint_linux.c src/enumcom.c ) # Set the output directories for the object files and the binary. set(OBJ_DIR obj/propeller) set(BIN_DIR bin/macosx) # Add a build target for the p2load binary. add_executable(p2load ${SRCS}) # Set the output directory for the p2load binary. set_target_properties(p2load PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${BIN_DIR} ) # Set the default target to build. set(default p2load)In this script, the CC variable has been removed, so CMake will use the default compiler when building the p2load binary. Note that the default compiler may not be the same as the cc compiler used by the original makefile, so the resulting binary may not be identical to the one built by the makefile.
As mentioned previously, it's generally a good idea to specify the compiler to use explicitly in your CMake scripts to ensure that your builds are consistent across different environments. However, if you prefer to use the default compiler provided by CMake, you can simply remove the CC variable from the script.
David Zemon:
can you define the source files in the add_executable function call
ChatGPT:
Sure! Here is an updated version of the CMake script that defines the source files in the add_executable function call:
# Set the minimum version of CMake required to build this project. cmake_minimum_required(VERSION 3.10) # Set the project name. project(p2load) # Set the flags to use for the C and C++ compilers. set(CFLAGS -Wall -mp2) # Set the output directories for the object files and the binary. set(OBJ_DIR obj/propeller) set(BIN_DIR bin/macosx) # Add a build target for the p2load binary. add_executable(p2load src/p2load.c src/loadelf.c src/p2_loadimage.c src/loader.c src/booter.c src/osint_linux.c src/enumcom.c ) # Set the output directory for the p2load binary. set_target_properties(p2load PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${BIN_DIR} ) # Set the default target to build. set(default p2load)In this script, the source files for the p2load binary are defined directly in the add_executable function call, rather than being stored in a separate variable. This is a more concise way of defining the source files, and it eliminates the need for the SRCS variable.
This updated script should produce the same result as the previous versions of the script, and it should build the p2load binary in the bin/macosx directory. Note that the default compiler used by CMake may not be the same as the cc compiler used by the original makefile, so the resulting binary may not be identical to the one built by the makefile.
Comments
@ersmith, you can stop answering silly questions about spin2cpp now
David Zemon:
ChatGPT:
That is really incredible.
“… Skynet becomes self-aware at 2:14 a.m., EDT, on August 29, 1997…” 😳
A friend called me about this yesterday. He asked the system to write a Spin program to blink seven LEDs. Only the pin definitions and the wait-for-one second code were correct. This was the output from his original query.
My friend -- while an experienced programmer -- hasn't learned Spin yet, so he wondered.
It needs to output a certainty factor along with each answer. One would hope the Spin example had a 10% rating.
On a whim I asked it to write a short bit of a simple legal brief. It tanked rather badly. So at least MY job is secure for a few more weeks. 🤣
This is my biggest thought too. As I've tried to use it for real and productive use cases, I've always found myself wishing I could either get access to its source data or be given an idea of how confident it is. That would make the tool significantly more useful. Even ignoring all wishes of the UX designers though, I'm not sure such an ask is possible. Maybe it is? I don't know much/anything about AI though.