Skip to content

Commit e956af8

Browse files
committed
how to process c++ third-party dependency
1 parent 3de18b8 commit e956af8

File tree

8 files changed

+139
-5
lines changed

8 files changed

+139
-5
lines changed

CMakeLists.txt

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,51 @@
1-
cmake_minimum_required(VERSION 3.15)
2-
project(create_example)
1+
cmake_minimum_required(VERSION 3.15 FATAL_ERROR)
2+
project(libminimal_example)
3+
4+
5+
6+
# ================================================================
7+
# prerequist
8+
# ================================================================
9+
10+
# cxx compile standard
11+
set(CXX_STANDARD 14)
12+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
13+
14+
# option for unittest switch
15+
option(ENABLE_UNITTEST "unittest for libminimal" OFF)
16+
17+
# project version setup
18+
set(LIBMINIMAL_MAJOR_VERSION "0")
19+
set(LIBMINIMAL_MINOR_VERSION "0")
20+
set(LIBMINIMAL_PATCH_VERSION "2")
21+
22+
# debug message
23+
message(STATUS "======================================================")
24+
message(STATUS "libminimal release version: ${LIBMINIMAL_MAJOR_VERSION}.${LIBMINIMAL_MINOR_VERSION}.${LIBMINIMAL_PATCH_VERSION}")
25+
message(STATUS "Creature unittest flag: ${ENABLE_UNITTEST}")
26+
message(STATUS "======================================================")
27+
28+
# load custom cmake module
29+
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/api.cmake)
330

431
# generate library
532
add_subdirectory(libminimal)
6-
add_subdirectory(example)
33+
add_subdirectory(example)
34+
35+
if(ENABLE_UNITTEST)
36+
# download googletest, function from mod_fetch_googletest.cmake
37+
download_googletest()
38+
39+
#enable cmake google test
40+
enable_testing()
41+
42+
# include gtest library
43+
#include_directories(${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR})
44+
#message(STATUS "gtest source directory is: ${gtest_SOURCE_DIR}")
45+
46+
# unittest directory
47+
add_subdirectory(test)
48+
else()
49+
# clear cache
50+
unset(GTEST_VER)
51+
endif()

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ minimal cpp library template
66
# how to run
77

88
~~~
9-
1 mkdir build & cd build
9+
1 mkdir build && cd build
1010
11-
2 cmake ..
11+
2 cmake -DENABLE_UNITTEST=ON ..
1212
1313
3 make
1414
~~~

cmake/api.cmake

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Create project third party open api
2+
# auto load cmake module startwith "mod" and endwith ".cmake"
3+
4+
# using cmake filesystem to glob all cmake modules
5+
FILE(GLOB EXTEND_MODULE "${CMAKE_CURRENT_LIST_DIR}/mod*.cmake")
6+
7+
# loop glob list and load module
8+
foreach(ext ${EXTEND_MODULE})
9+
message(STATUS "load ${ext} module successfully")
10+
include(${ext})
11+
endforeach()

cmake/mod_fetch_googletest.cmake

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Create project third party --googletest
2+
3+
function(download_googletest)
4+
# googletest root dir
5+
set(GTEST_DIR ${CMAKE_CURRENT_SOURCE_DIR}/third_party/googletest)
6+
7+
# if googletest exists, then skip download step
8+
if(EXISTS ${GTEST_DIR})
9+
message(STATUS "${GTEST_DIR} is already exists, skip download googletest, auto building ...")
10+
# auto compile gtest
11+
add_subdirectory(${GTEST_DIR})
12+
# clear cache
13+
unset(GTEST_VER)
14+
else()
15+
# set googletest git repository(using cn source.)
16+
set(GTEST_URL "https://github.com.cnpmjs.org/google/googletest.git")
17+
# Read googletest version form cache or set release-1.11.0
18+
set(GTEST_VER "1.11.0" CACHE STRING "Determines googletest version to download(for more visit offical site).")
19+
set(GTEST_TAG "release-${GTEST_VER}")
20+
21+
# debug message
22+
message(STATUS "Downloading googletest tag ${GTEST_TAG},fetch url is: ${GTEST_URL}")
23+
24+
# download googletest to third_party folder
25+
include(FetchContent)
26+
27+
FetchContent_Declare(
28+
gtest_suites
29+
GIT_REPOSITORY ${GTEST_URL}
30+
GIT_TAG ${GTEST_TAG}
31+
SOURCE_DIR ${GTEST_DIR}
32+
)
33+
34+
FetchContent_MakeAvailable(gtest_suites)
35+
36+
# debug message
37+
message(STATUS "download googletest successfully")
38+
endif()
39+
40+
# avoid googletest auto install
41+
set(INSTALL_GTEST OFF)
42+
43+
endfunction(download_googletest)

test/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# unittest case for c++
2+
project(unittest)
3+
4+
# source file path
5+
set(TEST_PATH ${PROJECT_SOURCE_DIR})
6+
set(GTEST_LIB gtest gtest_main)
7+
8+
# low_level_funcs test
9+
add_executable(testLowLevelFunc ${TEST_PATH}/test_low_level_funcs.cpp)
10+
target_link_libraries(testLowLevelFunc PRIVATE libminimal ${GTEST_LIB})
11+
add_test(NAME TestLowLevelFuncs COMMAND testLowLevelFunc)

test/test_low_level_funcs.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <gtest/gtest.h>
2+
#include <libminimal/libminimal.h>
3+
4+
class TestLowLevelFuncs : public ::testing::Test {
5+
protected:
6+
int the_first_int;
7+
int the_second_int;
8+
9+
10+
void SetUp() override {
11+
the_first_int = 3;
12+
the_second_int = 5;
13+
}
14+
};
15+
16+
17+
TEST_F(TestLowLevelFuncs, add) {
18+
int expected_output = the_first_int + the_second_int;
19+
20+
int real_output = libminimal::low_level::add(the_first_int, the_second_int);
21+
EXPECT_EQ(expected_output, real_output);
22+
}
23+

third_party/.gitkeep

Whitespace-only changes.

third_party/googletest

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit e2239ee6043f73722e7aa812a459f54a28552929

0 commit comments

Comments
 (0)