diff --git a/Makefile b/Makefile index b22816c..d1bada3 100644 --- a/Makefile +++ b/Makefile @@ -17,22 +17,30 @@ DOCKER_APPEND_MAKEFILES?= DOCKER_CMAKE_FLAGS?= DOCKER_SHELL?=bash -LOCAL_SRC_PATH?=${CURDIR} +LOCAL_SOURCE_PATH?=${CURDIR} DOCKER_SOURCE_PATH?=/${PROJECT_NAME} DOCKER_BUILD_DIR?=build DOCKER_CTEST_TIMEOUT?=5000 DOCKER_TEST_CORE_DIR?=${DOCKER_BUILD_DIR}/cores -ADDITIONAL_RUN_PARAMS?= +DOCKER_ADDITIONAL_RUN_PARAMS?= -BASIC_RUN_PARAMS?=-it --init --rm --privileged=true \ +DOCKER_USER_ROOT=OFF + +ifeq ($(DOCKER_USER_ROOT),OFF) + DOCKER_USER?=--user $(shell id -u):$(shell id -g) +else +endif + +DOCKER_BASIC_RUN_PARAMS?=-it --init --rm \ + ${DOCKER_USER} \ --memory-swap=-1 \ --ulimit core=-1 \ --name="${DOCKER_DEPS_CONTAINER}" \ --workdir=${DOCKER_SOURCE_PATH} \ - --mount type=bind,source=${LOCAL_SRC_PATH},target=${DOCKER_SOURCE_PATH} \ - ${ADDITIONAL_RUN_PARAMS} \ + --mount type=bind,source=${LOCAL_SOURCE_PATH},target=${DOCKER_SOURCE_PATH} \ + ${DOCKER_ADDITIONAL_RUN_PARAMS} \ ${DOCKER_DEPS_REPO}${DOCKER_DEPS_IMAGE}:${DOCKER_DEPS_VERSION} IF_CONTAINER_RUNS=$(shell docker container inspect -f '{{.State.Running}}' ${DOCKER_DEPS_CONTAINER} 2>/dev/null) @@ -48,7 +56,7 @@ help: ## .PHONY: gen_cmake gen_cmake: ## Generate cmake files, used internally - docker run ${BASIC_RUN_PARAMS} \ + docker run ${DOCKER_BASIC_RUN_PARAMS} \ ${DOCKER_SHELL} -c \ "mkdir -p ${DOCKER_SOURCE_PATH}/${DOCKER_BUILD_DIR} && \ cd ${DOCKER_BUILD_DIR} && \ @@ -59,7 +67,7 @@ gen_cmake: ## Generate cmake files, used internally .PHONY: build build: gen_cmake ## Build source. In order to build a specific target run: make TARGET=. - docker run ${BASIC_RUN_PARAMS} \ + docker run ${DOCKER_BASIC_RUN_PARAMS} \ ${DOCKER_SHELL} -c \ "cd ${DOCKER_BUILD_DIR} && \ make -j $$(nproc) ${TARGET}" @@ -68,7 +76,7 @@ build: gen_cmake ## Build source. In order to build a specific target run: make .PHONY: test test: ## Run all tests - docker run ${BASIC_RUN_PARAMS} \ + docker run ${DOCKER_BASIC_RUN_PARAMS} \ ${DOCKER_SHELL} -c \ "mkdir -p ${DOCKER_TEST_CORE_DIR} && \ cd ${DOCKER_BUILD_DIR} && \ @@ -76,14 +84,14 @@ test: ## Run all tests .PHONY: clean clean: ## Clean build directory - docker run ${BASIC_RUN_PARAMS} \ + docker run ${DOCKER_BASIC_RUN_PARAMS} \ ${DOCKER_SHELL} -c \ "rm -rf ${DOCKER_BUILD_DIR}" .PHONY: login login: ## Login to the container. Note: if the container is already running, login into existing one @if [ "${IF_CONTAINER_RUNS}" != "true" ]; then \ - docker run ${BASIC_RUN_PARAMS} \ + docker run ${DOCKER_BASIC_RUN_PARAMS} \ ${DOCKER_SHELL}; \ else \ docker exec -it ${DOCKER_DEPS_CONTAINER} \ diff --git a/README.md b/README.md index e004714..6d9bdbd 100644 --- a/README.md +++ b/README.md @@ -1,14 +1,18 @@ -# Dockerized C++ +[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) -This repository contains a single Makefile helping to build C+ projects in a docker container. +# Dockerized C/C++ Build Environment + +This repository contains a single highly customizable Makefile helping to build C/C++ projects in a docker container. ## Usage -Either download the file into your repository and update the variables or if you want to stay tuned, add it as a git submodule. -## Example -An example of usage can be found in the repository ["Dockerized C++ Build Example"](https://github.com/f-squirrel/dockerized_cpp_build_example). +* Add this repository as a git submodule to your project, for example as directory build_tools:
+`git submodule add https://github.com/f-squirrel/dockerized_cpp.git build_tools/` +* Create a Makefile in the root of your project's source tree +* Override the variables from the [list](#list-of-variables) in the Makefile +* Include the Makefile from this repository in your new Makefile: `include build_tools/Makefile` -In order to see availabe commands including user-defined, run `make help`: +In order to see available commands including user-defined ones, run `make help`: ```plain $ make help @@ -20,29 +24,32 @@ login Login to the container. Note: if the container is build-docker-deps-image Build the deps image. ``` -## List of variables +## Example -| Variable name | Default value | Description | -| ------------- | ------------- | ----------- | -| PROJECT_NAME | project | Name of the project, by default used in multiple places | -| DOCKER_CC | clang | C copmiler used in the project | -| DOCKER_CXX | clang++ | C++ copmiler used in the project | -| DOCKER_DEPS_REPO | ${PROJECT_NAME}/ | Docker repository where the build image is stored | -| DOCKER_DEPS_IMAGE | ${PROJECT_NAME}_build | The name of the build image | -| DOCKER_DEPS_VERSION | latest | Docker image version | -| DOCKER_DEPS_CONTAINER | ${DOCKER_DEPS_IMAGE} | The name of build's docker container | -| DOCKER_DEPS_FILE | DockerfileBuildEnv | Dockerfile used for building the build image | -| DOCKER_DEPS_IMAGE_BUILD_FLAGS | --no-cache=true | Flags used for building the image, note with this flag on, docker rebuilds from scratch the whole image | -| DOCKER_PREPEND_MAKEFILES | empty | The list of custom Makefiles to be included before default targets | -| DOCKER_APPEND_MAKEFILES | empty | The list of custom Makefiles to be included after default targets | -| DOCKER_CMAKE_FLAGS | empty | Project specific CMake flags | -| DOCKER_SHELL | bash | Shell used in the container | -| LOCAL_SRC_PATH | Current directory | The path to the source files | -| DOCKER_SOURCE_PATH | /${PROJECT_NAME} | Path where source files are mounted in the container | -| DOCKER_BUILD_DIR | build | Cmake build directory | -| DOCKER_CTEST_TIMEOUT | 5000 | CMake test timeout | -| DOCKER_TEST_CORE_DIR | ${DOCKER_BUILD_DIR}/cores | Path to the core files. For more information on configuring core dumps in docker please refer [here](https://ddanilov.me/how-to-configure-core-dump-in-docker-container) | -| ADDITIONAL_RUN_PARAMS | empty | Additional docker run commands | -| BASIC_RUN_PARAMS | [link to the code](https://github.com/f-squirrel/dockerized_cpp/blob/master/Makefile#L29) | Default commands used for running the build container | +An example of usage can be found in the repository ["Dockerized C/C++ Build Example"](https://github.com/f-squirrel/dockerized_cpp_build_example). +## List of variables +| Variable name | Default value | Description | +| ------------- | ------------- | ----------- | +| `PROJECT_NAME` | project | Name of the project, by default used in multiple places | +| `DOCKER_CC` | clang | C compiler used in the project | +| `DOCKER_CXX` | clang++ | C++ copmiler used in the project | +| `DOCKER_DEPS_REPO` | `${PROJECT_NAME}/` | Docker repository where the build image is stored | +| `DOCKER_DEPS_IMAGE` | `${PROJECT_NAME}_build` | The name of the build image | +| `DOCKER_DEPS_VERSION` | latest | Docker image version | +| `DOCKER_DEPS_CONTAINER` | `${DOCKER_DEPS_IMAGE}` | The name of build's docker container | +| `DOCKER_DEPS_FILE` | DockerfileBuildEnv | Dockerfile used for building the build image | +| `DOCKER_DEPS_IMAGE_BUILD_FLAGS` | `--no-cache=true` | Flags used for building the image, note with this flag on, docker rebuilds from scratch the whole image | +| `DOCKER_PREPEND_MAKEFILES` | empty | The list of space-separated custom Makefiles to be included before default targets | +| `DOCKER_APPEND_MAKEFILES` | empty | The list of space-separated ustom Makefiles to be included after default targets | +| `DOCKER_CMAKE_FLAGS` | empty | Project specific CMake flags | +| `DOCKER_SHELL` | bash | Shell used in the container | +| `LOCAL_SOURCE_PATH` | Current directory | The path to the source files | +| `DOCKER_SOURCE_PATH` | `/${PROJECT_NAME}` | Path where source files are mounted in the container | +| `DOCKER_BUILD_DIR` | build | Cmake build directory | +| `DOCKER_CTEST_TIMEOUT` | 5000 | CMake test timeout | +| `DOCKER_TEST_CORE_DIR` | `${DOCKER_BUILD_DIR}/cores` | Path to the core files. For more information on configuring core dumps in docker please refer [here](https://ddanilov.me/how-to-configure-core-dump-in-docker-container) | +| `DOCKER_ADDITIONAL_RUN_PARAMS` | empty | Additional docker run commands | +| `DOCKER_BASIC_RUN_PARAMS` | [link to the code](https://github.com/f-squirrel/dockerized_cpp/blob/master/Makefile#L29) | Default commands used for running the build container | +| `DOCKER_USER_ROOT` | `OFF` | Runs container as root user |