Adding support for go

Hi we have some go projects that we need to build. We have ben using custom commands which calls go build and custom targets to like them together. For the most part this works but our post build scripts (signing, installer, etc) rely heavily on cmake variables and generator expressions. Since these target based vars do not exist for the custom commands I started looking into adding language support for go.

I am able to add the language and set the compiler information. However I am still confused how this is actually invokes the build. My test project claims to compile successfully but no output is generated. From what I can tell nothing is actually calling go. I have added these settings but I am not seeing them invoked when i build my project.

if(NOT CMAKE_Go_COMPILE_OBJECT)
  set(CMAKE_Go_COMPILE_OBJECT "${CMAKE_Go_COMPILER} tool compile -l -N -o <OBJECT> <SOURCE> ")
endif()

if(NOT CMAKE_Go_LINK_EXECUTABLE)
  set(CMAKE_Go_LINK_EXECUTABLE "${CMAKE_Go_COMPILER} tool link -o <TARGET> <OBJECTS>  ")
endif()

I am setting these compiler settings as well

set(CMAKE_Go_COMPILER "/path/to/go")
set(CMAKE_Go_COMPILER_LOADED 1)

set(CMAKE_Go_SOURCE_FILE_EXTENSIONS go)
set(CMAKE_Go_OUTPUT_EXTENSION .exe)
set(CMAKE_Go_OUTPUT_EXTENSION_REPLACE 1)
set(CMAKE_Go_COMPILER_ENV_VAR "go1.22.8")

Can someone help me understand what is missing to be able to get this to invoke go.

1 Like

The Go compiler usually drops all intermediate files and leaves only the compiled binary.
You can try to use the “-work” flag:

From go help build:

-work
                print the name of the temporary work directory and
                do not delete it when exiting.