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.

Thanks @leha-bot for the suggestions. The issue I was running into was that the Visual Studio generators were not able to invoke the commands but using Ninja instead this seems to be invoking the commands correctly.

I currently am now seeing the go commands are being called. I had to change generator, but now it is failing to build using go tool compile. It seems that we need to use go build instead of go tool compile. For whatever reason go tool compile is not able to build (at least on windows). I was reading somewhere that mentioned it is no longer supported and that we should be using go build instead. I am not sure if this statement is correct as I did not see this on any official go pages. That being said it is still not working an unable to build.

The problem with this approach is that cmake expects that there is a compile step then a separate link step specified by CMAKE_COMPILE_OBJECT and CMAKE_LINK_EXECUTABLE. In order for a language that is compiles and links into a single step is there a way to handle this in cmake? I tried to not set CMAKE_Go_COMPILE_OBJECT but this seems to be required for the generators to understand the new language.

I was able to hack this by running go build in the CMAKE_COMPILE_OBJECT then in the CMAKE_LINK_EXECUTABLE just copy this the back to the . This seams to work since the object is really the linked executable in this case. I am concerned that although this does seem to be working that this approach may have unseen side effects as this is clearly not how it is expected to be used.

Any help with a better solution would be greatly appreciated.