cc_project_template_cmake

A cmake, ninja, and googletest-based C++ project template
git clone https://git.bracken.jp/cc_project_template_cmake.git
Log | Files | Refs | Submodules | README | LICENSE

commit 22204f164e56767c5eea2a4cafe641afea7c5ed3
Author: Chris Bracken <chris@bracken.jp>
Date:   Thu, 18 Oct 2018 21:24:00 -0700

Initial version

Diffstat:
A.clang-format | 12++++++++++++
A.gitignore | 13+++++++++++++
A.gitmodules | 3+++
ACMakeLists.txt | 12++++++++++++
ALICENSE | 27+++++++++++++++++++++++++++
AREADME.md | 65+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/CMakeLists.txt | 40++++++++++++++++++++++++++++++++++++++++
Asrc/bar.cc | 6++++++
Asrc/bar.h | 6++++++
Asrc/bar_test.cc | 16++++++++++++++++
Asrc/foo.cc | 5+++++
Asrc/foo.h | 6++++++
Asrc/foo_test.cc | 15+++++++++++++++
Asrc/main.cc | 13+++++++++++++
Athird_party/CMakeLists.txt | 1+
Athird_party/googletest | 1+
16 files changed, 241 insertions(+), 0 deletions(-)

diff --git a/.clang-format b/.clang-format @@ -0,0 +1,12 @@ +# Defines the Chromium style for automatic reformatting. +# http://clang.llvm.org/docs/ClangFormatStyleOptions.html +BasedOnStyle: Chromium +# This defaults to 'Auto'. Explicitly set it for a while, so that +# 'vector<vector<int> >' in existing files gets formatted to +# 'vector<vector<int>>'. ('Auto' means that clang-format will only use +# 'int>>' if the file already contains at least one such instance.) +Standard: Cpp11 +SortIncludes: true +--- +Language: ObjC +ColumnLimit: 100 diff --git a/.gitignore b/.gitignore @@ -0,0 +1,13 @@ +# Output directories +bin/ +lib/ + +# CMake artifcats +*.cmake +CMakeCache.txt +CMakeFiles/ + +# Ninja artifacts +*.ninja +.ninja_deps +.ninja_log diff --git a/.gitmodules b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "third_party/googletest"] + path = third_party/googletest + url = git@github.com:google/googletest.git diff --git a/CMakeLists.txt b/CMakeLists.txt @@ -0,0 +1,12 @@ +cmake_minimum_required(VERSION 3.0) + +project(foobar + VERSION 0.0.1 +) + +set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) +set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) + +add_subdirectory(src) +add_subdirectory(third_party) diff --git a/LICENSE b/LICENSE @@ -0,0 +1,27 @@ +Copyright 2018 Chris Bracken. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + * Neither the name of Google Inc. nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/README.md b/README.md @@ -0,0 +1,65 @@ +# ProjectTemplate + +A [cmake](https://cmake.org), +[ninja](https://github.com/ninja-build/ninja), and +[googletest](https://github.com/google/googletest)-based C++ project template. + +## Prerequisites + +To build, you'll need [cmake](https://cmake.org), +[ninja](https://github.com/ninja-build/ninja), and a clang build toolchain +installed on your system. + +## Obtaining the source + +First, clone the repo: +```shell +git clone git@gitlab.com:cbracken/cc_project_template.git +``` + +Next, initialise and fetch git submodules: +```shell +# Initialise local configuration file. +git submodule init + +# Fetch submodules (googletest). +git submodule update +``` + +## Updating git submodules + +To update the git submodules to a newer commit, simply run: + +```shell +git submodule update --remote +``` + +## Building and running + +First, generate the ninja build files: + +```shell +# For debug build: +cmake -DCMAKE_BUILD_TYPE=Debug -GNinja + +# For release build: +cmake -DCMAKE_BUILD_TYPE=Release -GNinja +``` + +### Unit tests + +To build and run the unit tests, run: + +```shell +ninja +./bin/libfoo_tests +``` + +### Executable binary + +To build and run the binary: + +```shell +ninja +./bin/main +``` diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt @@ -0,0 +1,40 @@ +add_definitions(-std=c++17) + +set(CXX_FLAGS + "-Weverything" + "-Wno-c++98-compat" +) + +set(CXX_TEST_FLAGS + ${CXX_FLAGS} + "-Wno-global-constructors" +) + +set(LIBFOO_SRCS + foo.h + foo.cc + bar.h + bar.cc +) + +add_executable(main + main.cc + ${LIBFOO_SRCS} +) +target_compile_options(main PRIVATE ${CXX_FLAGS}) + +set(LIBFOO_TEST_SRCS + foo_test.cc + bar_test.cc +) + +add_executable(libfoo_tests + ${LIBFOO_SRCS} + ${LIBFOO_TEST_SRCS} +) +target_link_libraries(libfoo_tests + gtest + gtest_main +) +target_compile_options(libfoo_tests PRIVATE ${CXX_TEST_FLAGS}) + diff --git a/src/bar.cc b/src/bar.cc @@ -0,0 +1,6 @@ +#include "bar.h" + +int Bar(int x) { + return x - 1; +} + diff --git a/src/bar.h b/src/bar.h @@ -0,0 +1,6 @@ +#ifndef PROJNAME_BAR_H_ +#define PROJNAME_BAR_H_ + +int Bar(int x); + +#endif // PROJNAME_BAR_H_ diff --git a/src/bar_test.cc b/src/bar_test.cc @@ -0,0 +1,16 @@ +#include "bar.h" + +#include "gtest/gtest.h" + +TEST(Bar, DecrementsZero) { + EXPECT_EQ(-1, Bar(0)); +} + +TEST(Bar, DecrementsPositive) { + EXPECT_EQ(1, Bar(2)); +} + +TEST(Bar, DecrementsNegative) { + EXPECT_EQ(-2, Bar(-1)); +} + diff --git a/src/foo.cc b/src/foo.cc @@ -0,0 +1,5 @@ +#include "foo.h" + +int Foo(int x) { + return x + 1; +} diff --git a/src/foo.h b/src/foo.h @@ -0,0 +1,6 @@ +#ifndef PROJNAME_FOO_H_ +#define PROJNAME_FOO_H_ + +int Foo(int x); + +#endif // PROJNAME_FOO_H_ diff --git a/src/foo_test.cc b/src/foo_test.cc @@ -0,0 +1,15 @@ +#include "foo.h" + +#include "gtest/gtest.h" + +TEST(Foo, IncrementsZero) { + EXPECT_EQ(1, Foo(0)); +} + +TEST(Foo, IncrementsPositive) { + EXPECT_EQ(2, Foo(1)); +} + +TEST(Foo, IncrementsNegative) { + EXPECT_EQ(-1, Foo(-2)); +} diff --git a/src/main.cc b/src/main.cc @@ -0,0 +1,13 @@ +#include <iostream> + +#include "foo.h" +#include "bar.h" + +int main(int argc, char** argv) { + for (auto i = 0; i < argc; ++i) + std::cout << "argv[" << i << "]: " << argv[i] << std::endl; + + auto x = Foo(3); + auto y = Bar(x); + std::cout << "x=" << x << ",y=" << y << std::endl; +} diff --git a/third_party/CMakeLists.txt b/third_party/CMakeLists.txt @@ -0,0 +1 @@ +add_subdirectory(googletest) diff --git a/third_party/googletest b/third_party/googletest @@ -0,0 +1 @@ +Subproject commit 723f26663f1585e5ac633dbdf7a5cfb669ff9ef5