[cmake-developers] [RFC] golang support
Colin McCabe
cmccabe at alumni.cmu.edu
Fri Aug 5 09:58:13 EDT 2011
On Fri, Aug 5, 2011 at 12:46 AM, Michael Wild <themiwi at gmail.com> wrote:
> The problem is that add_dependencies() adds only dependencies between
> top-level targets, there's nothing that tells CMake to relink the
> executable when the library changes. It only ensures that the library is
> build *before* the executable. You would need to pass the library output
> files to the DEPENDS option of the linking command.
I guess I could create a single function or macro that takes both the
source files and the library files to build a go executable. That
would allow me to add the libraries to the DEPENDS of 6l/8l.
I originally structured it in terms of add_go_exe / target_to_libs
because I wanted tit to be similar to the C/C++ support.
>
> A few more comments:
>
> - Don't use macros unless you have to. Functions have much nicer properties.
>
> - You can simplify finding of the programs to
>
> find_program(GO_COMPILER NAMES 8g 6g 5g
> PATHS ENV GOROOT PATH_SUFFIXES bin
> NO_DEFAULT_PATH)
>
> - There's no 7g, you probably meant 5g.
>
> - You should only search for the "correct" compiler by determining the
> architecture you are compiling for first.
Yeah, ideally it would be dependent on architecture.
>
> - You should probably check the directory properties INCLUDE_DIRECTORIES
> and LINK_DIRECTORIES and add them with the -I and -L flags.
>
> - Don't hardcode the .exe suffix. Use ${CMAKE_EXECUTABLE_SUFFIX}
> instead. I don't know how it works with the libraries on Windows with
> Go, but consider using ${CMAKE_STATIC_LIBRARY_PREFIX} and
> ${CMAKE_STATIC_LIBRARY_SUFFIX}.
I haven't used Go on Windows, but on UNIX at least, it uses a .a
suffix for libraries.
Thanks for the comments!
cheers,
Colin
>
> - add_dependencies() accepts multiple dependees, so the loop is unnecessary.
>
> - You might want to take a look at my (unfinished) Vala module for some
> inspiration:
> http://wildcodes.com/git/CMake/commit/?h=patches/AddValaSupport It is
> still a bit buggy (typos, doesn't get all the dependencies right), and I
> had a much improved version, but lost it due to a disk failure before I
> pushed the changes to the repository...
>
>
> HTH
>
> Michael
>
> On 08/05/2011 07:08 AM, Colin McCabe wrote:
>> Hi all,
>>
>> Google Go is a fun language that I've been playing around with.
>> http://en.wikipedia.org/wiki/Go_(programming_language)
>>
>> I wrote a little cmake file to help me compile Google Go (golang)
>> source files using CMake.
>>
>> However, it's currently not working as expected. The go executables
>> are not rebuilt when the go libraries change.
>>
>> ======================================================
>> FIND_PROGRAM(GO_COMPILER
>> bin/6g.exe
>> bin/7g.exe
>> bin/8g.exe
>> bin/6g
>> bin/7g
>> bin/8g
>> PATHS
>> $ENV{GOROOT}
>> NO_DEFAULT_PATH
>> )
>> IF(NOT GO_COMPILER)
>> MESSAGE(FATAL_ERROR "Could not find the go compiler")
>> ENDIF(NOT GO_COMPILER)
>>
>> FIND_PROGRAM(GO_ARCHIVER
>> bin/gopack.exe
>> bin/gopack
>> PATHS
>> $ENV{GOROOT}
>> NO_DEFAULT_PATH
>> )
>> IF(NOT GO_ARCHIVER)
>> MESSAGE(FATAL_ERROR "Could not find the go packer")
>> ENDIF(NOT GO_ARCHIVER)
>>
>> FIND_PROGRAM(GO_LINKER
>> bin/6l.exe
>> bin/7l.exe
>> bin/8l.exe
>> bin/6l
>> bin/7l
>> bin/8l
>> PATHS
>> $ENV{GOROOT}
>> NO_DEFAULT_PATH
>> )
>> IF(NOT GO_LINKER)
>> MESSAGE(FATAL_ERROR "Could not find the go linker")
>> ENDIF(NOT GO_LINKER)
>>
>> MACRO(add_go_exe exefile)
>> SET(OBJ_FILE ${CMAKE_CURRENT_BINARY_DIR}/${exefile}.8)
>> SET(EXE_FILE ${CMAKE_CURRENT_BINARY_DIR}/${exefile})
>> ADD_CUSTOM_COMMAND(OUTPUT ${OBJ_FILE}
>> COMMAND ${GO_COMPILER}
>> ARGS -I ${CMAKE_CURRENT_BINARY_DIR} -I ${PROJECT_BINARY_DIR}
>> -o ${OBJ_FILE} ${ARGN}
>> MAIN_DEPENDENCY ${ARGN}
>> WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
>> )
>> ADD_CUSTOM_COMMAND(OUTPUT ${EXE_FILE}
>> COMMAND ${GO_LINKER}
>> ARGS -L ${PROJECT_BINARY_DIR} -L ${CMAKE_CURRENT_BINARY_DIR}
>> -o ${EXE_FILE} ${OBJ_FILE}
>> MAIN_DEPENDENCY ${OBJ_FILE}
>> WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
>> )
>> ADD_CUSTOM_TARGET(${exefile}.exe ALL DEPENDS ${EXE_FILE})
>> ENDMACRO (add_go_exe)
>>
>> MACRO(add_go_lib libfile)
>> ADD_CUSTOM_COMMAND(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${libfile}.8
>> COMMAND ${GO_COMPILER}
>> ARGS -I ${CMAKE_CURRENT_BINARY_DIR} -I ${PROJECT_BINARY_DIR}
>> -o ${CMAKE_CURRENT_BINARY_DIR}/${libfile}.8 ${ARGN}
>> MAIN_DEPENDENCY ${ARGN}
>> WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
>> )
>> ADD_CUSTOM_COMMAND(OUTPUT ${PROJECT_BINARY_DIR}/${libfile}.a
>> COMMAND ${GO_ARCHIVER}
>> ARGS crg ${PROJECT_BINARY_DIR}/${libfile}.a
>> ${CMAKE_CURRENT_BINARY_DIR}/${libfile}.8
>> MAIN_DEPENDENCY ${CMAKE_CURRENT_BINARY_DIR}/${libfile}.8
>> WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
>> )
>> ADD_CUSTOM_TARGET(${libfile} ALL DEPENDS
>> ${CMAKE_CURRENT_BINARY_DIR}/${libfile}.8
>> ${PROJECT_BINARY_DIR}/${libfile}.a)
>> ENDMACRO (add_go_lib)
>>
>> MACRO(target_link_go_libs exefile)
>> FOREACH(libfile ${ARGN})
>> ADD_DEPENDENCIES(${exefile}.exe ${libfile})
>> ENDFOREACH(libfile)
>> ENDMACRO(target_link_go_libs)
>> ======================================================
>>
>> I'm curious if anyone has any ideas about the best way to fix this.
>> Alternately, how difficult would it be to add native support to golang
>> in CMake, the same way Fortran, Ada, and D have?
>>
>> thanks,
>> Colin McCabe
>> _______________________________________________
>> cmake-developers mailing list
>> cmake-developers at cmake.org
>> http://public.kitware.com/cgi-bin/mailman/listinfo/cmake-developers
>
> _______________________________________________
> cmake-developers mailing list
> cmake-developers at cmake.org
> http://public.kitware.com/cgi-bin/mailman/listinfo/cmake-developers
>
More information about the cmake-developers
mailing list