Skip to content

Commit 6f6a37f

Browse files
committed
New ccache binary recipe
1 parent 531faad commit 6f6a37f

File tree

7 files changed

+139
-0
lines changed

7 files changed

+139
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
include_guard()
2+
3+
# Enable cache if available
4+
set(CACHE_OPTION "ccache" CACHE STRING "Compiler cache to be used")
5+
set(CACHE_OPTION_VALUES "ccache" "sccache")
6+
set(CCACHE_ARGS "base_dir=<CONAN_HOME>;hash_dir=false")
7+
set_property(CACHE CACHE_OPTION PROPERTY STRINGS ${CACHE_OPTION_VALUES})
8+
list(FIND CACHE_OPTION_VALUES ${CACHE_OPTION} CACHE_OPTION_INDEX)
9+
10+
if(${CACHE_OPTION_INDEX} EQUAL -1)
11+
message(
12+
STATUS
13+
"Using custom compiler cache system: '${CACHE_OPTION}', explicitly supported entries are ${CACHE_OPTION_VALUES}"
14+
)
15+
endif()
16+
17+
find_program(CACHE_BINARY NAMES ${CACHE_OPTION_VALUES})
18+
if(CACHE_BINARY)
19+
message(STATUS "${CACHE_BINARY} found and enabled")
20+
set(CMAKE_CXX_COMPILER_LAUNCHER "${CACHE_BINARY};${CCACHE_ARGS}" CACHE FILEPATH "CXX compiler cache used")
21+
set(CMAKE_C_COMPILER_LAUNCHER "${CACHE_BINARY};${CCACHE_ARGS}" CACHE FILEPATH "C compiler cache used")
22+
else()
23+
message(WARNING "${CACHE_OPTION} is enabled but was not found. Not using it")
24+
endif()
25+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
sources:
2+
"4.10.2":
3+
Linux:
4+
x86_64:
5+
url: "https://github.com/ccache/ccache/releases/download/v4.10.2/ccache-4.10.2-linux-x86_64.tar.xz"
6+
sha256: "80cab87bd510eca796467aee8e663c398239e0df1c4800a0b5dff11dca0b4f18"
7+
Macos:
8+
universal:
9+
url: "https://github.com/ccache/ccache/releases/download/v4.10.2/ccache-4.10.2-darwin.tar.gz"
10+
sha256: "d90514fff15943a8607e84e3f42d45f823915a92f99984f3fc88202f6295d1e8"
11+
Windows:
12+
x86_64:
13+
url: "https://github.com/ccache/ccache/releases/download/v4.10.2/ccache-4.10.2-windows-x86_64.zip"
14+
sha256: "6252f081876a9a9f700fae13a5aec5d0d486b28261d7f1f72ac11c7ad9df4da9"

recipes/ccache/binary/conanfile.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import os
2+
from pathlib import Path
3+
4+
from conan import ConanFile
5+
from conan.errors import ConanInvalidConfiguration
6+
from conan.tools.files import get, copy, replace_in_file
7+
8+
required_conan_version = ">=2.7"
9+
10+
AUTOIJECT_CMAKE = "ccache-autoinject.cmake"
11+
12+
class CcacheConan(ConanFile):
13+
name = "ccache"
14+
package_type = "application"
15+
description = (
16+
"Ccache (or “ccache”) is a compiler cache. It speeds up recompilation "
17+
"by caching previous compilations and detecting when the same "
18+
"compilation is being done again."
19+
)
20+
license = "GPL-3.0-or-later"
21+
topics = ("compiler-cache", "recompilation", "cache", "compiler")
22+
homepage = "https://ccache.dev"
23+
url = "https://github.com/conan-io/conan-center-index"
24+
settings = "os", "arch"
25+
exports_sources = [AUTOIJECT_CMAKE]
26+
27+
def validate(self):
28+
if self.settings.os != "Macos" and self.settings.arch != "x86_64":
29+
raise ConanInvalidConfiguration("ccache binaries are only provided for x86_64 architectures")
30+
31+
def build(self):
32+
arch = str(self.settings.arch) if self.settings.os != "Macos" else "universal"
33+
get(self, **self.conan_data["sources"][self.version][str(self.settings.os)][arch],
34+
destination=self.source_folder, strip_root=True)
35+
36+
def package_id(self):
37+
if self.info.settings.os == "Macos":
38+
del self.info.settings.arch
39+
40+
def package(self):
41+
copy(self, "*GPL-*.txt", src=self.build_folder, dst=os.path.join(self.package_folder, "licenses"))
42+
copy(self, "LICENSE.*", src=self.build_folder, dst=os.path.join(self.package_folder, "licenses"))
43+
copy(self, "ccache", src=self.build_folder, dst=os.path.join(self.package_folder, "bin"))
44+
copy(self, AUTOIJECT_CMAKE, src=self.build_folder, dst=self.package_folder)
45+
46+
def finalize(self):
47+
copy(self, "*", src=self.immutable_package_folder, dst=self.package_folder)
48+
# TODO: find a way of retrieving conan home without accessing private API
49+
# replace_in_file(self, os.path.join(self.package_folder, AUTOIJECT_CMAKE), "<CONAN_HOME>", self._conan_helpers.cache.store)
50+
replace_in_file(self, os.path.join(self.package_folder, AUTOIJECT_CMAKE), "<CONAN_HOME>", str(Path.home()))
51+
52+
def package_info(self):
53+
self.cpp_info.libdirs = []
54+
self.cpp_info.includedirs = []
55+
if self.conf.get("user.ccache:auto_inject", default=True, check_type=bool):
56+
self.conf_info.append("tools.cmake.cmaketoolchain:user_toolchain", os.path.join(self.package_folder, AUTOIJECT_CMAKE))
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
cmake_minimum_required(VERSION 3.15)
2+
project(test_package LANGUAGES CXX)
3+
4+
add_executable(${PROJECT_NAME} test_package.cpp)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import os
2+
from conan import ConanFile
3+
from conan.tools.build import can_run
4+
from conan.tools.cmake import CMake, cmake_layout
5+
from conan.errors import ConanException
6+
7+
8+
class TestPackageConan(ConanFile):
9+
settings = "os", "arch", "compiler", "build_type"
10+
generators = "CMakeToolchain"
11+
12+
def build_requirements(self):
13+
self.tool_requires(self.tested_reference_str)
14+
15+
def test(self):
16+
generated_file = os.path.join(self.dependencies.build[self.tested_reference_str].package_folder, "ccache-autoinject.cmake")
17+
if not os.path.exists(generated_file):
18+
raise ConanException("ccache-autoinject.cmake toolchain file does not exist")
19+
user_toolchain = self.conf.get("tools.cmake.cmaketoolchain:user_toolchain", check_type=list)
20+
if generated_file not in user_toolchain:
21+
raise ConanException("ccache not found in user toolchain")
22+
23+
if can_run(self):
24+
self.run("ccache --version", env="conanbuild")
25+
26+
def layout(self):
27+
cmake_layout(self)
28+
29+
def build(self):
30+
cmake = CMake(self)
31+
cmake.configure()
32+
cmake.build()
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include <iostream>
2+
3+
int main() {
4+
std::cout << "Hello world!\n";
5+
}

recipes/ccache/config.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
versions:
2+
"4.10.2":
3+
folder: binary

0 commit comments

Comments
 (0)