From 35ca80dcc2e39aaa72d6e122d367da26f09018af Mon Sep 17 00:00:00 2001 From: PerseoGI Date: Wed, 9 Jul 2025 10:38:50 +0200 Subject: [PATCH] Added ccache examples --- examples/tools/ccache/advanced/CMakeLists.txt | 21 +++ examples/tools/ccache/advanced/conanfile.py | 37 ++++++ examples/tools/ccache/advanced/src/main.cpp | 30 +++++ .../ccache/advanced/test_package/conanfile.py | 14 ++ examples/tools/ccache/basic/CMakeLists.txt | 22 ++++ examples/tools/ccache/basic/src/basic.cpp | 120 ++++++++++++++++++ examples/tools/ccache/basic/src/basic.h | 14 ++ examples/tools/ccache/basic/src/main.cpp | 12 ++ 8 files changed, 270 insertions(+) create mode 100644 examples/tools/ccache/advanced/CMakeLists.txt create mode 100644 examples/tools/ccache/advanced/conanfile.py create mode 100644 examples/tools/ccache/advanced/src/main.cpp create mode 100644 examples/tools/ccache/advanced/test_package/conanfile.py create mode 100644 examples/tools/ccache/basic/CMakeLists.txt create mode 100644 examples/tools/ccache/basic/src/basic.cpp create mode 100644 examples/tools/ccache/basic/src/basic.h create mode 100644 examples/tools/ccache/basic/src/main.cpp diff --git a/examples/tools/ccache/advanced/CMakeLists.txt b/examples/tools/ccache/advanced/CMakeLists.txt new file mode 100644 index 00000000..a0bd710c --- /dev/null +++ b/examples/tools/ccache/advanced/CMakeLists.txt @@ -0,0 +1,21 @@ +cmake_minimum_required(VERSION 3.15) +project(ccache_demo) + +add_executable(ccache_demo src/main.cpp) + +find_package(fmt REQUIRED) +find_package(cpr REQUIRED) +find_package(RapidJSON REQUIRED) + +target_link_libraries(ccache_demo + PRIVATE + cpr::cpr + fmt::fmt + rapidjson +) + +install(TARGETS ccache_demo DESTINATION "." + RUNTIME DESTINATION bin + ARCHIVE DESTINATION lib + LIBRARY DESTINATION lib + ) diff --git a/examples/tools/ccache/advanced/conanfile.py b/examples/tools/ccache/advanced/conanfile.py new file mode 100644 index 00000000..c113a9e3 --- /dev/null +++ b/examples/tools/ccache/advanced/conanfile.py @@ -0,0 +1,37 @@ +from conan import ConanFile +from conan.tools.cmake import CMakeToolchain, CMake, cmake_layout, CMakeDeps + + +class CCacheDemoRecipe(ConanFile): + name = "ccache_demo" + version = "1.0" + package_type = "application" + + # Binary configuration + settings = "os", "compiler", "build_type", "arch" + + # Sources are located in the same place as this recipe, copy them to the recipe + exports_sources = "CMakeLists.txt", "src/*" + + def layout(self): + cmake_layout(self) + + def generate(self): + deps = CMakeDeps(self) + deps.generate() + tc = CMakeToolchain(self) + tc.generate() + + def build(self): + cmake = CMake(self) + cmake.configure() + cmake.build() + + def package(self): + cmake = CMake(self) + cmake.install() + + def requirements(self): + self.requires("cpr/1.11.2") + self.requires("fmt/11.2.0") + self.requires("rapidjson/1.1.0") diff --git a/examples/tools/ccache/advanced/src/main.cpp b/examples/tools/ccache/advanced/src/main.cpp new file mode 100644 index 00000000..b0f3e81d --- /dev/null +++ b/examples/tools/ccache/advanced/src/main.cpp @@ -0,0 +1,30 @@ +#include +#include +#include + + +int main() { + cpr::Response r = cpr::Get(cpr::Url{"https://api.github.com/repos/conan-io/conan"}); + if (r.status_code != 200) { + fmt::print("Failed to fetch data: {}\n", r.status_code); + return 1; + } + + rapidjson::Document doc; + if (doc.Parse(r.text.c_str()).HasParseError()) { + fmt::print("Error parsing JSON response\n"); + return 1; + } + + if (doc.HasMember("full_name") && doc["full_name"].IsString()) { + fmt::print("Repository: {}\n", doc["full_name"].GetString()); + } + if (doc.HasMember("stargazers_count") && doc["stargazers_count"].IsInt()) { + fmt::print("Stars: {}\n", doc["stargazers_count"].GetInt()); + } + if (doc.HasMember("forks_count") && doc["forks_count"].IsInt()) { + fmt::print("Forks: {}\n", doc["forks_count"].GetInt()); + } + + return 0; +} diff --git a/examples/tools/ccache/advanced/test_package/conanfile.py b/examples/tools/ccache/advanced/test_package/conanfile.py new file mode 100644 index 00000000..89663d47 --- /dev/null +++ b/examples/tools/ccache/advanced/test_package/conanfile.py @@ -0,0 +1,14 @@ +import os +from conan import ConanFile +from conan.tools.build import can_run + + +class pkgTestConan(ConanFile): + settings = "os", "compiler", "build_type", "arch" + + def requirements(self): + self.requires(self.tested_reference_str) + + def test(self): + if can_run(self): + self.run("ccache_demo", env="conanrun") diff --git a/examples/tools/ccache/basic/CMakeLists.txt b/examples/tools/ccache/basic/CMakeLists.txt new file mode 100644 index 00000000..24b5ecbb --- /dev/null +++ b/examples/tools/ccache/basic/CMakeLists.txt @@ -0,0 +1,22 @@ +cmake_minimum_required(VERSION 3.15) + +project(basic CXX) + +find_program(CCACHE_PROGRAM NAMES ccache) +if(CCACHE_PROGRAM) + message(STATUS "ccache found: ${CCACHE_PROGRAM}, enabling compiler launcher.") + # Set the compiler launcher property on the targets + set(CMAKE_CXX_COMPILER_LAUNCHER ${CCACHE_PROGRAM} CACHE FILEPATH "CXX compiler cache used") + set(CMAKE_C_COMPILER_LAUNCHER ${CCACHE_PROGRAM} CACHE FILEPATH "C compiler cache used") +else() + message(WARNING "ccache not found. Compiler cache disabled.") +endif() + + +add_executable(basic src/basic.cpp src/main.cpp) + +install(TARGETS basic DESTINATION "." + RUNTIME DESTINATION bin + ARCHIVE DESTINATION lib + LIBRARY DESTINATION lib + ) diff --git a/examples/tools/ccache/basic/src/basic.cpp b/examples/tools/ccache/basic/src/basic.cpp new file mode 100644 index 00000000..481d36e0 --- /dev/null +++ b/examples/tools/ccache/basic/src/basic.cpp @@ -0,0 +1,120 @@ +#include +#include "basic.h" + + + +void basic(){ + + + #ifdef NDEBUG + std::cout << "basic/1.0: Hello World Release!\n"; + #else + std::cout << "basic/1.0: Hello World Debug!\n"; + #endif + + // ARCHITECTURES + #ifdef _M_X64 + std::cout << " basic/1.0: _M_X64 defined\n"; + #endif + + #ifdef _M_IX86 + std::cout << " basic/1.0: _M_IX86 defined\n"; + #endif + + #ifdef _M_ARM64 + std::cout << " basic/1.0: _M_ARM64 defined\n"; + #endif + + #if __i386__ + std::cout << " basic/1.0: __i386__ defined\n"; + #endif + + #if __x86_64__ + std::cout << " basic/1.0: __x86_64__ defined\n"; + #endif + + #if __aarch64__ + std::cout << " basic/1.0: __aarch64__ defined\n"; + #endif + + // Libstdc++ + #if defined _GLIBCXX_USE_CXX11_ABI + std::cout << " basic/1.0: _GLIBCXX_USE_CXX11_ABI "<< _GLIBCXX_USE_CXX11_ABI << "\n"; + #endif + + // MSVC runtime + #if defined(_DEBUG) + #if defined(_MT) && defined(_DLL) + std::cout << " basic/1.0: MSVC runtime: MultiThreadedDebugDLL\n"; + #elif defined(_MT) + std::cout << " basic/1.0: MSVC runtime: MultiThreadedDebug\n"; + #endif + #else + #if defined(_MT) && defined(_DLL) + std::cout << " basic/1.0: MSVC runtime: MultiThreadedDLL\n"; + #elif defined(_MT) + std::cout << " basic/1.0: MSVC runtime: MultiThreaded\n"; + #endif + #endif + + // COMPILER VERSIONS + #if _MSC_VER + std::cout << " basic/1.0: _MSC_VER" << _MSC_VER<< "\n"; + #endif + + #if _MSVC_LANG + std::cout << " basic/1.0: _MSVC_LANG" << _MSVC_LANG<< "\n"; + #endif + + #if __cplusplus + std::cout << " basic/1.0: __cplusplus" << __cplusplus<< "\n"; + #endif + + #if __INTEL_COMPILER + std::cout << " basic/1.0: __INTEL_COMPILER" << __INTEL_COMPILER<< "\n"; + #endif + + #if __GNUC__ + std::cout << " basic/1.0: __GNUC__" << __GNUC__<< "\n"; + #endif + + #if __GNUC_MINOR__ + std::cout << " basic/1.0: __GNUC_MINOR__" << __GNUC_MINOR__<< "\n"; + #endif + + #if __clang_major__ + std::cout << " basic/1.0: __clang_major__" << __clang_major__<< "\n"; + #endif + + #if __clang_minor__ + std::cout << " basic/1.0: __clang_minor__" << __clang_minor__<< "\n"; + #endif + + #if __apple_build_version__ + std::cout << " basic/1.0: __apple_build_version__" << __apple_build_version__<< "\n"; + #endif + + // SUBSYSTEMS + + #if __MSYS__ + std::cout << " basic/1.0: __MSYS__" << __MSYS__<< "\n"; + #endif + + #if __MINGW32__ + std::cout << " basic/1.0: __MINGW32__" << __MINGW32__<< "\n"; + #endif + + #if __MINGW64__ + std::cout << " basic/1.0: __MINGW64__" << __MINGW64__<< "\n"; + #endif + + #if __CYGWIN__ + std::cout << " basic/1.0: __CYGWIN__" << __CYGWIN__<< "\n"; + #endif +} + +void basic_print_vector(const std::vector &strings) { + for(std::vector::const_iterator it = strings.begin(); it != strings.end(); ++it) { + std::cout << "basic/1.0 " << *it << std::endl; + } +} diff --git a/examples/tools/ccache/basic/src/basic.h b/examples/tools/ccache/basic/src/basic.h new file mode 100644 index 00000000..7fe627cd --- /dev/null +++ b/examples/tools/ccache/basic/src/basic.h @@ -0,0 +1,14 @@ +#pragma once + +#include +#include + + +#ifdef _WIN32 + #define BASIC_EXPORT __declspec(dllexport) +#else + #define BASIC_EXPORT +#endif + +BASIC_EXPORT void basic(); +BASIC_EXPORT void basic_print_vector(const std::vector &strings); diff --git a/examples/tools/ccache/basic/src/main.cpp b/examples/tools/ccache/basic/src/main.cpp new file mode 100644 index 00000000..13ae006e --- /dev/null +++ b/examples/tools/ccache/basic/src/main.cpp @@ -0,0 +1,12 @@ +#include "basic.h" +#include +#include + +int main() { + basic(); + + std::vector vec; + vec.push_back("test_package"); + + basic_print_vector(vec); +}