Shop OBEX P1 Docs P2 Docs Learn Events
OpenAI + ChatGPT can even convert build systems — Parallax Forums

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 :grin:
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=echo

PROPCC=propeller-elf-gcc
PROPCFLAGS=-mp2
PROPOBJCOPY=propeller-elf-objcopy

OS?=macosx

ifeq ($(OS),linux)
CFLAGS+=-DLINUX
EXT=
OSINT=osint_linux
LIBS=
endif

ifeq ($(OS),cygwin)
CFLAGS+=-DCYGWIN
EXT=.exe
OSINT=osint_cygwin enumcom
LIBS=-lsetupapi
endif

ifeq ($(OS),msys)
CFLAGS += -DMINGW
EXT=.exe
OSINT=osint_mingw enumcom
LIBS=-lsetupapi
endif

ifeq ($(OS),macosx)
CFLAGS+=-DMACOSX
EXT=
OSINT=osint_linux
LIBS=
endif

ifeq ($(OS),)
$(error OS not set)
endif

SRCDIR=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 :smiley:

    David Zemon:

    how do i use spin2cpp

    ChatGPT:

    To use spin2cpp, you will first need to have it installed on your system. spin2cpp is typically installed along with other tools for developing Propeller projects, such as the Propeller GCC compiler and the Propeller Loader.

    Once spin2cpp is installed, you can use it from the command line to convert Spin files to C++ code. For example, to convert a Spin file called my_spin_file.spin to C++ code, you could use the following command:

    spin2cpp my_spin_file.spin
    

    This command will generate a C++ file with the same name as the input Spin file, but with a .cpp extension instead of a .spin extension. For example, if you ran the command above on my_spin_file.spin, it would generate a C++ file called my_spin_file.cpp.

    You can also specify additional options when using spin2cpp to control how the C++ code is generated. For example, the -o option allows you to specify the name of the output C++ file, rather than using the default name based on the input Spin file:

    spin2cpp -o my_cpp_file.cpp my_spin_file.spin
    

    This command will generate a C++ file called my_cpp_file.cpp from the input Spin file my_spin_file.spin. You can view the full list of options available for spin2cpp by running spin2cpp -h on the command line.

    Once you have generated the C++ code from your Spin files, you can use a C++ compiler to compile the code into a binary that can be run on the Propeller. This process will vary depending on the tools you are using for your Propeller project, but it typically involves using a Makefile or CMake script to compile the code and generate the binary.

  • That is really incredible.

    “… Skynet becomes self-aware at 2:14 a.m., EDT, on August 29, 1997…” 😳

  • JonnyMacJonnyMac Posts: 8,923
    edited 2022-12-08 20:09

    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.

    CON
        LED_PIN_0 = 0   ' Pin P0
        LED_PIN_1 = 1   ' Pin P1
        LED_PIN_2 = 2   ' Pin P2
        LED_PIN_3 = 3   ' Pin P3
        LED_PIN_4 = 4   ' Pin P4
        LED_PIN_5 = 5   ' Pin P5
        LED_PIN_6 = 6   ' Pin P6
    END CON
    
    ' Set the output direction for the seven LED pins
    dirs = [1, 1, 1, 1, 1, 1, 1]
    
    ' Turn off all seven LEDs
    leds = [0, 0, 0, 0, 0, 0, 0]
    
    ' Loop forever
    while(1)
    
        ' Turn on the first LED
        leds[LED_PIN_0] = 1
    
        ' Wait for one second
        waitcnt(clkfreq + cnt)
    
        ' Turn off the first LED and turn on the second LED
        leds[LED_PIN_0] = 0
        leds[LED_PIN_1] = 1
    
        ' Wait for one second
        waitcnt(clkfreq + cnt)
    
        ' Turn off the second LED and turn on the third LED
        leds[LED_PIN_1] = 0
        leds[LED_PIN_2] = 1
    
        ' Wait for one second
        waitcnt(clkfreq + cnt)
    
        ' Turn off the third LED and turn on the fourth LED
        leds[LED_PIN_2] = 0
        leds[LED_PIN_3] = 1
    
        ' Wait for one second
        waitcnt(clkfreq + cnt)
    
        ' Turn off the fourth LED and turn on the fifth LED
        leds[LED_PIN_3] = 0
        leds[LED_PIN_4] = 1
    
        ' Wait for one second
        waitcnt(clkfreq + cnt)
    
        ' Turn off the fifth LED and turn on the sixth LED
        leds[LED_PIN_4] = 0
        leds[LED_PIN_5] = 1
    
        ' Wait for one second
        waitcnt(clkfreq + cnt)
    
        ' Turn off the sixth LED and turn on the seventh LED
        leds[LED_PIN_5] = 0
        leds[LED_PIN_6] = 1
    
        ' Wait for one second
        waitcnt(clkfreq + cnt)
    
        ' Turn off the seventh LED
        leds[LED_PIN_6] = 0
    
    endwhile
    

    My friend -- while an experienced programmer -- hasn't learned Spin yet, so he wondered.

  • evanhevanh Posts: 15,187
    edited 2022-12-08 22:26

    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. 🤣

  • @evanh said:
    It needs to output a certainty factor along with each answer. One would hope the Spin example had a 10% rating. :)

    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.

Sign In or Register to comment.