Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add go_xxx to simplify cmake #2182

Merged
merged 13 commits into from
May 20, 2017
Merged

add go_xxx to simplify cmake #2182

merged 13 commits into from
May 20, 2017

Conversation

gangliao
Copy link
Contributor

@gangliao gangliao commented May 17, 2017

Travis CI already included golang

@gangliao gangliao requested a review from wangkuiyi May 17, 2017 13:10
@gangliao
Copy link
Contributor Author

#2154


set(Go_BIN_PATH
$ENV{GOPATH}
$ENV{GOROOT}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

go bin path 一般在$ENV{GOROOT}/bin

Copy link
Contributor Author

@gangliao gangliao May 17, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link
Contributor

@helinwang helinwang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, only one comment https://github.com/PaddlePaddle/Paddle/pull/2182/files#r117021742 , can be easily fixed if the comment is correct.

Copy link
Collaborator

@wangkuiyi wangkuiyi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Just one question: there might be more types of go_library -- those supposed to be called only by Go code don't fail in either c-archive or c-shared.

set(LIB_NAME "lib${TARGET_NAME}.so")
endif()
else()
set(BUILD_MODE "-buildmode=c-archive")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we might have to support more types of work in addition to c-shared and c-archive in go_library? These two types are Go libraries that can be called from C, and there are special requirements with the source code -- e.g., all exported symbols must be in package main, and there must be a main function defined. I think most Go libraries are supposed to be called only from Go code, and should be in some other type -- archive maybe? @helinwang

Copy link
Contributor

@helinwang helinwang May 17, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wangkuiyi Good question! You mean how to specify Go dependencies when using go_binary? I think it would be painful to specify Go dependencies manually in cmake (unlike C++ needs to use cmake or other tools for dependency management, Go already have it's dependency management tool). Maybe in cmake we can just let the go tool do it for us:

export GOPATH=./build/go
go get .path_to_go_binary_project/...

in this way, go_library is only for the library used by other languages.
reference: http://stackoverflow.com/a/30296041/852385

Copy link
Collaborator

@wangkuiyi wangkuiyi May 17, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I was talking about is that go_library should be able to generate the following types of outputs:

  1. A static library which is callable from other Go code. To build it, we need something like the following:

    go build \
    -o ~/work/paddle/build/libpaddle_go_pserver_libpserver.a \ 
    ~/work/paddle/paddle/go/pserver/pserver.go

    Please be aware of the correspondence between source file path and output archive file path.

  2. A static library which is callable from C/C++. We need something like

    go build -buildmode=c-archive \
    -o ~/work/paddle/build/libpaddle_go_pserver_cclient_cclient.a
    ~/work/paddle/paddle/go/pserver/cclient/cclient.go
  3. A dynamic library which is callable from C/C++.

    go build -buildmode=c-shared \
    -o ~/work/paddle/build/libpaddle_go_pserver_cclient_cclient.so
    ~/work/paddle/paddle/go/pserver/cclient/cclient.go

#include "gtest/gtest.h"
#include "libadder.h"

TEST(Cgo, Invoke) { EXPECT_EQ(GoAdder(30, 12), 42); }
Copy link
Contributor

@helinwang helinwang May 17, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the linking of this file's executable to work, we will need set (CMAKE_EXE_LINKER_FLAGS "-pthread"), because when linking a binary that contains Go code built with cgo enabled, pthread need to be linked. (I think now CI is failing because of this).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

-o "${CMAKE_CURRENT_BINARY_DIR}/${LIB_NAME}"
${go_library_SRCS}
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR})
add_custom_target(${TARGET_NAME}_lib ALL DEPENDS ${TARGET_NAME}_timestamp ${go_library_DEPS})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have not found where go_library_DEPS is set?
Also please consider let the Go tool to get Go dependency rather than manually specifying it in cmake: https://github.com/PaddlePaddle/Paddle/pull/2182/files/af065196400ca0254e61df3916888f4341306e97#r117078644

@wangkuiyi
Copy link
Collaborator

I wrote a design doc #2187 in hope that we all on the same page with the implementation of go_{library,binary,test}.

Copy link
Contributor

@helinwang helinwang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. As far as I know, cmake design doc is going to come out later, so we may need to change the implementation later.

@gangliao gangliao merged commit 4cf78e6 into PaddlePaddle:develop May 20, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants