Skip to content

Commit

Permalink
perf: optimize cpp target generation (#203)
Browse files Browse the repository at this point in the history
Signed-off-by: msclock <[email protected]>
  • Loading branch information
msclock authored Apr 12, 2024
1 parent 6e9e3c6 commit 64cc3de
Show file tree
Hide file tree
Showing 40 changed files with 208 additions and 188 deletions.
4 changes: 2 additions & 2 deletions src/compile/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
set(target_name compile)

find_package(spdlog CONFIG REQUIRED)

set(target_name compile)

file(GLOB_RECURSE _srcs "src/*.cpp")
file(GLOB_RECURSE _hdrs "include/*.hpp")

Expand Down
19 changes: 2 additions & 17 deletions src/compile/include/compile.hpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,4 @@
#pragma once

#include <string>
#include "compile_export.hpp"

namespace compile {
namespace info {
/**
* \brief Get compile version string
* \return The distribution compile version provided.
*/
std::string_view COMPILE_EXPORT version() noexcept;

/**
* \return A bool represents if it is debug distribution.
*/
bool COMPILE_EXPORT is_debug() noexcept;
} // namespace info
} // namespace compile
#include "distribution.hpp"
#include "git.h"
12 changes: 12 additions & 0 deletions src/compile/include/distribution.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

#include "compile_export.hpp"

namespace compile {
namespace distribution {
/**
* \return A bool represents if it is debug distribution.
*/
bool COMPILE_EXPORT is_debug() noexcept;
} // namespace distribution
} // namespace compile
20 changes: 0 additions & 20 deletions src/compile/src/compile.cpp

This file was deleted.

13 changes: 13 additions & 0 deletions src/compile/src/distribution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "distribution.hpp"

namespace compile {
namespace distribution {
bool is_debug() noexcept {
#ifdef _DEBUG
return true;
#else
return false;
#endif
}
} // namespace distribution
} // namespace compile
1 change: 1 addition & 0 deletions src/compile/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
find_package(GTest CONFIG REQUIRED)

include(GoogleTest)

file(GLOB files "*.cpp")
Expand Down
14 changes: 7 additions & 7 deletions src/compile/tests/test_compile.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#include <gtest/gtest.h>
#include "compile.hpp"

TEST(info, version) {
const auto version = compile::info::version();
EXPECT_FALSE(version.empty());
TEST(compile, version) {
const auto version = git_ProjectVersion();
EXPECT_STRNE(version, "");
}

TEST(info, distribution) {
const auto is_debug = compile::info::is_debug();
TEST(compile, distribution) {
const auto is_debug = compile::distribution::is_debug();
#ifdef _DEBUG
GTEST_ASSERT_TRUE(is_debug);
EXPECT_TRUE(is_debug);
#else
GTEST_ASSERT_FALSE(is_debug);
EXPECT_FALSE(is_debug);
#endif
}
4 changes: 2 additions & 2 deletions src/exe/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
find_package(spdlog CONFIG REQUIRED)

# The private target will not be installed and only act as requirements for main
# and tests internally
set(target_name_private exe_private)

find_package(spdlog CONFIG REQUIRED)

add_library(${target_name_private} INTERFACE)

target_include_interface_directories(
Expand Down
15 changes: 15 additions & 0 deletions src/exe/include/distribution.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once

#include <spdlog/spdlog.h>

namespace exe {
namespace distribution {
inline bool is_debug() noexcept {
#ifdef _DEBUG
return true;
#else
return false;
#endif
}
} // namespace distribution
} // namespace exe
12 changes: 0 additions & 12 deletions src/exe/include/helpers.hpp

This file was deleted.

5 changes: 3 additions & 2 deletions src/exe/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#include <spdlog/spdlog.h>

#include "distribution.hpp"
#include "git.h"
#include "helpers.hpp"

int main() {
spdlog::info("Get a returned value: {} ; Version: {}", exe::helpers::some_fun(), git::ProjectVersion());
spdlog::info("Build Version: {}", git::ProjectVersion());
spdlog::info("Distribution Type: {}", exe::distribution::is_debug() ? "Debug" : "Release");
return 0;
}
1 change: 1 addition & 0 deletions src/exe/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
find_package(GTest CONFIG REQUIRED)

include(GoogleTest)

file(GLOB files "*.cpp")
Expand Down
17 changes: 17 additions & 0 deletions src/exe/tests/test_exe.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <gtest/gtest.h>
#include "distribution.hpp"
#include "git.h"

TEST(exe, version) {
const auto version = git_ProjectVersion();
EXPECT_STRNE(version, "");
}

TEST(exe, distribution) {
const auto is_debug = exe::distribution::is_debug();
#ifdef _DEBUG
EXPECT_TRUE(is_debug);
#else
EXPECT_FALSE(is_debug);
#endif
}
7 changes: 0 additions & 7 deletions src/exe/tests/test_exe_helper.cpp

This file was deleted.

4 changes: 2 additions & 2 deletions src/header/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
set(target_name header)

find_package(spdlog CONFIG REQUIRED)

set(target_name header)

add_library(${target_name} INTERFACE)

target_include_interface_directories(
Expand Down
13 changes: 13 additions & 0 deletions src/header/include/header/distribution.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once

namespace header {
namespace distribution {
inline bool is_debug() noexcept {
#ifdef _DEBUG
return true;
#else
return false;
#endif
}
} // namespace distribution
} // namespace header
16 changes: 1 addition & 15 deletions src/header/include/header/header.hpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,4 @@
#pragma once

#include <spdlog/spdlog.h>
#include "distribution.hpp"
#include "git.h"

namespace header {
namespace common {
inline int some_fun() {
spdlog::info("{}", git::ProjectVersion());
return 0;
}

const std::string_view const_string() {
spdlog::info("Calling {}", __FUNCTION__);
return "This is a header_only only const string";
}
} // namespace common
} // namespace header
1 change: 1 addition & 0 deletions src/header/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
find_package(GTest CONFIG REQUIRED)

include(GoogleTest)

file(GLOB files "*.cpp")
Expand Down
16 changes: 10 additions & 6 deletions src/header/tests/test_header.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
#include <gtest/gtest.h>
#include "header/header.hpp"

TEST(common, some_fun) {
const auto fun_ret = header::common::some_fun();
GTEST_ASSERT_EQ(0, fun_ret);
TEST(header, version) {
const auto version = git_ProjectVersion();
EXPECT_STRNE(version, "");
}

TEST(common, const_string) {
const auto fun_ret = header::common::const_string();
GTEST_ASSERT_GT(fun_ret.size(), 0);
TEST(header, distribution) {
const auto is_debug = header::distribution::is_debug();
#ifdef _DEBUG
EXPECT_TRUE(is_debug);
#else
EXPECT_FALSE(is_debug);
#endif
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
set(target_name {{ compile_target }})

find_package(spdlog CONFIG REQUIRED)

set(target_name {{ compile_target }})

file(GLOB_RECURSE _srcs "src/*.cpp")
file(GLOB_RECURSE _hdrs "include/*.hpp")

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

#include "{{ compile_target }}_export.hpp"

namespace {{ compile_target }} {
namespace distribution {
/**
* \return A bool represents if it is debug distribution.
*/
bool {{ compile_target|upper }}_EXPORT is_debug() noexcept;
} // namespace distribution
} // namespace {{ compile_target }}
Original file line number Diff line number Diff line change
@@ -1,19 +1,4 @@
#pragma once

#include <string>
#include "{{ compile_target }}_export.hpp"

namespace {{ compile_target }} {
namespace info {
/**
* \brief Get {{ compile_target }} version string
* \return The distribution {{ compile_target }} version provided.
*/
std::string_view {{ compile_target|upper }}_EXPORT version() noexcept;

/**
* \return A bool represents if it is debug distribution.
*/
bool {{ compile_target|upper }}_EXPORT is_debug() noexcept;
} // namespace info
} // namespace {{ compile_target }}
#include "distribution.hpp"
#include "git.h"
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "distribution.hpp"

namespace {{ compile_target }} {
namespace distribution {
bool is_debug() noexcept {
#ifdef _DEBUG
return true;
#else
return false;
#endif
}
} // namespace distribution
} // namespace {{ compile_target }}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
find_package(GTest CONFIG REQUIRED)

include(GoogleTest)

file(GLOB files "*.cpp")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#include <gtest/gtest.h>
#include "{{ compile_target }}.hpp"

TEST(info, version) {
const auto version = {{ compile_target }}::info::version();
EXPECT_FALSE(version.empty());
TEST({{ compile_target }}, version) {
const auto version = git_ProjectVersion();
EXPECT_STRNE(version, "");
}

TEST(info, distribution) {
const auto is_debug = {{ compile_target }}::info::is_debug();
TEST({{ compile_target }}, distribution) {
const auto is_debug = {{ compile_target }}::distribution::is_debug();
#ifdef _DEBUG
GTEST_ASSERT_TRUE(is_debug);
EXPECT_TRUE(is_debug);
#else
GTEST_ASSERT_FALSE(is_debug);
EXPECT_FALSE(is_debug);
#endif
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
find_package(spdlog CONFIG REQUIRED)

# The private target will not be installed and only act as requirements for main
# and tests internally
set(target_name_private {{ exe_target }}_private)

find_package(spdlog CONFIG REQUIRED)

add_library(${target_name_private} INTERFACE)

target_include_interface_directories(
Expand Down
Loading

0 comments on commit 64cc3de

Please sign in to comment.