Skip to content

Commit

Permalink
Convert C++ Example to Docker Build Pattern
Browse files Browse the repository at this point in the history
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 <dsazonoff@gmail.com>
  • Loading branch information
markmandel and dsazonoff committed Apr 25, 2019
1 parent 83a3e04 commit e98e2b8
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 31 deletions.
3 changes: 3 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

# project specific
.*
/build
/cmd
/sdks/cpp/.build
.idea
*.zip
/release
Expand Down
5 changes: 4 additions & 1 deletion examples/cpp-simple/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,7 @@ target_link_libraries(${PROJECT_NAME} PUBLIC agones)

if (MSVS)
target_compile_options(${PROJECT_NAME} PUBLIC /wd4101 /wd4146 /wd4251 /wd4661)
endif()
endif()

# Installation
install(TARGETS ${PROJECT_NAME} EXPORT ${PROJECT_NAME} RUNTIME DESTINATION .)
28 changes: 25 additions & 3 deletions examples/cpp-simple/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
ENTRYPOINT /home/server/cpp-simple
32 changes: 5 additions & 27 deletions examples/cpp-simple/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)/../..)

# _____ _
# |_ _|_ _ _ __ __ _ ___| |_ ___
Expand All @@ -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) .

0 comments on commit e98e2b8

Please sign in to comment.