From f0c6e96febd96f479df1c72f424809ae4ec1bb7a Mon Sep 17 00:00:00 2001 From: Mark Mandel Date: Tue, 7 May 2019 12:08:41 -0700 Subject: [PATCH] Convert C++ Example to Docker Build Pattern (#746) While this does make it a longer build time, it makes it easier to get started with the C++ example because there is no tooling dependencies other than Docker (Make is even optional, it just sets the default arguments up). Also provides an end to end example of compiling the SDK, installing it and then using it with another C++ project. Co-authored-by: Dmitry Sazonov --- .dockerignore | 3 +++ examples/cpp-simple/CMakeLists.txt | 5 ++++- examples/cpp-simple/Dockerfile | 28 +++++++++++++++++++++++--- examples/cpp-simple/Makefile | 32 +++++------------------------- 4 files changed, 37 insertions(+), 31 deletions(-) diff --git a/.dockerignore b/.dockerignore index e6f111520b..dab7a8c994 100644 --- a/.dockerignore +++ b/.dockerignore @@ -14,6 +14,9 @@ # project specific .* +/build +/cmd +/sdks/cpp/.build .idea *.zip /release diff --git a/examples/cpp-simple/CMakeLists.txt b/examples/cpp-simple/CMakeLists.txt index 904dc85b6b..0fa2fd0fa5 100644 --- a/examples/cpp-simple/CMakeLists.txt +++ b/examples/cpp-simple/CMakeLists.txt @@ -47,4 +47,7 @@ target_link_libraries(${PROJECT_NAME} PUBLIC agones) if (MSVS) target_compile_options(${PROJECT_NAME} PUBLIC /wd4101 /wd4146 /wd4251 /wd4661) -endif() \ No newline at end of file +endif() + +# Installation +install(TARGETS ${PROJECT_NAME} EXPORT ${PROJECT_NAME} RUNTIME DESTINATION .) \ No newline at end of file diff --git a/examples/cpp-simple/Dockerfile b/examples/cpp-simple/Dockerfile index d25def1f5e..ac6aa8bcdb 100644 --- a/examples/cpp-simple/Dockerfile +++ b/examples/cpp-simple/Dockerfile @@ -12,12 +12,34 @@ # See the License for the specific language governing permissions and # limitations under the License. +FROM gcc:8 as builder + +WORKDIR /project + +ADD https://cmake.org/files/v3.14/cmake-3.14.1-Linux-x86_64.sh /cmake-3.14.1-Linux-x86_64.sh +RUN mkdir /opt/cmake +RUN sh /cmake-3.14.1-Linux-x86_64.sh --prefix=/opt/cmake --skip-license +RUN ln -s /opt/cmake/bin/cmake /usr/local/bin/cmake +RUN cmake --version + +COPY ./sdks/cpp sdk +RUN cd sdk && mkdir -p .build && \ + cd .build && \ + cmake .. -DCMAKE_BUILD_TYPE=Release -G "Unix Makefiles" -Wno-dev && \ + cmake --build . --target install + +COPY ./examples/cpp-simple cpp-simple +RUN cd cpp-simple && mkdir -p .build && \ + cd .build && \ + cmake .. -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=.bin && \ + cmake --build . --target install + FROM debian:stretch RUN useradd -m server -COPY ./bin/server-static /home/server/server-static +COPY --from=builder /project/cpp-simple/.build/.bin/cpp-simple /home/server/cpp-simple RUN chown -R server /home/server && \ - chmod o+x /home/server/server-static + chmod o+x /home/server/cpp-simple USER server -ENTRYPOINT /home/server/server-static \ No newline at end of file +ENTRYPOINT /home/server/cpp-simple \ No newline at end of file diff --git a/examples/cpp-simple/Makefile b/examples/cpp-simple/Makefile index d0360ef704..c894868cc9 100644 --- a/examples/cpp-simple/Makefile +++ b/examples/cpp-simple/Makefile @@ -23,16 +23,13 @@ # \_/ \__,_|_| |_|\__,_|_.__/|_|\___|___/ # -CXX = g++ -CPPFLAGS += -I/usr/local/include -pthread -CXXFLAGS += -std=c++11 -LDFLAGS += -L/usr/local/lib -lagonessdk -lgrpc++_unsecure -lgrpc -lprotobuf -lpthread -ldl REPOSITORY = gcr.io/agones-images # Directory that this Makefile is in. mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST))) project_path := $(dir $(mkfile_path)) -server_tag = $(REPOSITORY)/cpp-simple-server:0.4 +server_tag = $(REPOSITORY)/cpp-simple-server:0.5 +root_path = $(realpath $(project_path)/../..) # _____ _ # |_ _|_ _ _ __ __ _ ___| |_ ___ @@ -41,25 +38,6 @@ server_tag = $(REPOSITORY)/cpp-simple-server:0.4 # |_|\__,_|_| \__, |\___|\__|___/ # |___/ -# build the library -build: ensure-bin server-dynamic server-static - -# Build a docker image for the server, and tag it -build-image: - docker build $(project_path) --tag=$(server_tag) - -# make the server executable -server-dynamic: server.o - $(CXX) $^ $(LDFLAGS) -o $(project_path)bin/$@ - -# make the server executable -server-static: server.o - $(CXX) $^ $(LDFLAGS) -o $(project_path)bin/$@ -static - -# make sure the bin directory exists -ensure-bin: - -mkdir $(project_path)/bin - -clean: - -rm -r $(project_path)/bin - -rm *.o +# build the cpp-simple binary +build: + cd $(root_path) && docker build -f $(project_path)/Dockerfile --tag=$(server_tag) .