v31.1-roots.1 · Commit 15: [PATCH 15/17] fix(build): classify Homebrew IPC dependency headers as system
ci/test/test_homebrew_capnp_headers.py
Before: /dev/null · After: ci/test/test_homebrew_capnp_headers.py
File metadata
new file mode 100644 index 0000000000000000000000000000000000000000..90bf9c4f57c054e617d3a1bd369ae38a1289c1a7
Download original patch · Release notes
Diff rows 1–137 of 137. Line numbers refer to the original before/after files.
+ Added− RemovedUnmarked lines are context.
Scroll the code area horizontally to read long lines. The two line-number columns refer to before and after.
ci/test/test_homebrew_capnp_headers.py
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# Copyright (c) 2026-present The Bitcoin Roots developers
|
|
3
|
+
# Distributed under the MIT software license, see the accompanying
|
|
4
|
+
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
5
|
+
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
import json
|
|
8
|
+
import os
|
|
9
|
+
import shlex
|
|
10
|
+
import shutil
|
|
11
|
+
import subprocess
|
|
12
|
+
import tempfile
|
|
13
|
+
import unittest
|
|
14
|
+
|
|
15
|
+
ROOT = Path(__file__).resolve().parents[2]
|
|
16
|
+
CLANG = shutil.which('clang++')
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
@unittest.skipUnless(CLANG and shutil.which('cmake'), 'CMake and Clang are required')
|
|
20
|
+
class HomebrewCapnpHeadersTest(unittest.TestCase):
|
|
21
|
+
def build(self, *, apple=True, mismatch=None, project_warning=False, implicit=True):
|
|
22
|
+
with tempfile.TemporaryDirectory(prefix='roots-homebrew-capnp-') as directory:
|
|
23
|
+
work = Path(directory)
|
|
24
|
+
cellar = work / 'Cellar/capnp/1.5.0'
|
|
25
|
+
prefix = work / 'homebrew prefix'
|
|
26
|
+
(prefix / 'include').mkdir(parents=True)
|
|
27
|
+
for family, filename in [('capnp', 'schema.h'), ('kj', 'windows-sanity.h')]:
|
|
28
|
+
headers = cellar / 'include' / family
|
|
29
|
+
headers.mkdir(parents=True)
|
|
30
|
+
text = '#if UNDEFINED_DEPENDENCY_MACRO\n#endif\n'
|
|
31
|
+
(headers / filename).write_text(text)
|
|
32
|
+
alias = prefix / 'include' / family
|
|
33
|
+
if mismatch == family:
|
|
34
|
+
alias.mkdir()
|
|
35
|
+
(alias / filename).write_text(text)
|
|
36
|
+
else:
|
|
37
|
+
alias.symlink_to(headers, target_is_directory=True)
|
|
38
|
+
tools = work / 'tools'
|
|
39
|
+
tools.mkdir()
|
|
40
|
+
brew = tools / 'brew'
|
|
41
|
+
brew.write_text('#!/bin/sh\ncase "$*" in\n'
|
|
42
|
+
f' "--prefix") printf "%s\\n" {shlex.quote(str(prefix))} ;;\n'
|
|
43
|
+
' *) exit 1 ;;\nesac\n')
|
|
44
|
+
brew.chmod(0o755)
|
|
45
|
+
source = work / 'source'
|
|
46
|
+
vendor = source / 'vendor'
|
|
47
|
+
vendor.mkdir(parents=True)
|
|
48
|
+
probe = '#include <capnp/schema.h>\n#include <kj/windows-sanity.h>\n'
|
|
49
|
+
if project_warning:
|
|
50
|
+
probe += '#if UNDEFINED_PROJECT_MACRO\n#endif\n'
|
|
51
|
+
(vendor / 'probe.cpp').write_text(probe)
|
|
52
|
+
(source / 'consumer.cpp').write_text(probe)
|
|
53
|
+
# Model CapnProto's directory-scoped imported targets and exported
|
|
54
|
+
# include directory, without editing or building vendored sources.
|
|
55
|
+
(vendor / 'CMakeLists.txt').write_text(
|
|
56
|
+
f'set(CAPNP_INCLUDE_DIRECTORY "{cellar / "include"}")\n'
|
|
57
|
+
'add_library(CapnProto::kj INTERFACE IMPORTED)\n'
|
|
58
|
+
'set_target_properties(CapnProto::kj PROPERTIES\n'
|
|
59
|
+
f' INTERFACE_INCLUDE_DIRECTORIES "{cellar / "include"}")\n'
|
|
60
|
+
'foreach(target multiprocess mputil mpgen)\n'
|
|
61
|
+
' add_library(${target} OBJECT probe.cpp)\n'
|
|
62
|
+
' target_link_libraries(${target} PUBLIC CapnProto::kj)\n'
|
|
63
|
+
'endforeach()\n'
|
|
64
|
+
'foreach(target mpcalculator mpprinter mpexample)\n'
|
|
65
|
+
' add_library(${target} INTERFACE)\n'
|
|
66
|
+
'endforeach()\n')
|
|
67
|
+
implicit_include = (f'list(APPEND CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES "{prefix / "include"}")\n'
|
|
68
|
+
if implicit else '')
|
|
69
|
+
(source / 'CMakeLists.txt').write_text(
|
|
70
|
+
'cmake_minimum_required(VERSION 3.22)\n'
|
|
71
|
+
'project(HomebrewCapnpHeaders LANGUAGES CXX)\n'
|
|
72
|
+
f'set(CMAKE_HOST_APPLE {"TRUE" if apple else "FALSE"})\n'
|
|
73
|
+
f'{implicit_include}'
|
|
74
|
+
'add_library(core_interface INTERFACE)\n'
|
|
75
|
+
'target_compile_options(core_interface INTERFACE -Wundef -Werror)\n'
|
|
76
|
+
f'include("{ROOT / "cmake/libmultiprocess.cmake"}")\n'
|
|
77
|
+
'add_libmultiprocess(vendor)\n'
|
|
78
|
+
'add_library(consumer OBJECT consumer.cpp)\n'
|
|
79
|
+
'target_link_libraries(consumer PRIVATE multiprocess)\n')
|
|
80
|
+
# Build-time CPATH reproduces alias selection without granting
|
|
81
|
+
# CMake's configure-time CPATH implicit-path filtering exemption.
|
|
82
|
+
configure_env = {**os.environ, 'PATH': f'{tools}:{os.environ["PATH"]}'}
|
|
83
|
+
configure_env.pop('CPATH', None)
|
|
84
|
+
build = work / 'build'
|
|
85
|
+
configure = subprocess.run(
|
|
86
|
+
['cmake', '-S', str(source), '-B', str(build),
|
|
87
|
+
f'-DCMAKE_CXX_COMPILER={CLANG}', '-DCMAKE_EXPORT_COMPILE_COMMANDS=ON'],
|
|
88
|
+
capture_output=True, text=True, env=configure_env)
|
|
89
|
+
self.assertEqual(configure.returncode, 0, configure.stdout + configure.stderr)
|
|
90
|
+
commands = json.loads((build / 'compile_commands.json').read_text())
|
|
91
|
+
self.assertEqual(len(commands), 4)
|
|
92
|
+
result = subprocess.run(
|
|
93
|
+
['cmake', '--build', str(build), '--parallel', '2', '--target',
|
|
94
|
+
'multiprocess', 'mputil', 'mpgen', 'consumer'],
|
|
95
|
+
capture_output=True, text=True,
|
|
96
|
+
env={**configure_env, 'CPATH': str(prefix / 'include')})
|
|
97
|
+
for entry in commands:
|
|
98
|
+
argv = shlex.split(entry['command'])
|
|
99
|
+
system_dirs = [argv[i + 1] for i, arg in enumerate(argv[:-1]) if arg == '-isystem']
|
|
100
|
+
self.assertEqual(str(prefix / 'include') in system_dirs,
|
|
101
|
+
apple and mismatch is None, entry['command'] + result.stderr)
|
|
102
|
+
self.assertIn('-Werror', argv)
|
|
103
|
+
self.assertIn('-Wundef', argv)
|
|
104
|
+
return result
|
|
105
|
+
|
|
106
|
+
def test_implicit_matching_headers_emit_flag_and_compile(self):
|
|
107
|
+
result = self.build()
|
|
108
|
+
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
|
|
109
|
+
|
|
110
|
+
def test_explicit_matching_headers_compile(self):
|
|
111
|
+
result = self.build(implicit=False)
|
|
112
|
+
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
|
|
113
|
+
|
|
114
|
+
def test_project_warnings_still_fail(self):
|
|
115
|
+
result = self.build(project_warning=True)
|
|
116
|
+
self.assertNotEqual(result.returncode, 0)
|
|
117
|
+
self.assertIn('UNDEFINED_PROJECT_MACRO', result.stderr)
|
|
118
|
+
self.assertIn('[-Werror,-Wundef]', result.stderr)
|
|
119
|
+
|
|
120
|
+
def test_mismatched_capnp_is_not_hidden(self):
|
|
121
|
+
result = self.build(mismatch='capnp')
|
|
122
|
+
self.assertNotEqual(result.returncode, 0)
|
|
123
|
+
self.assertIn('homebrew prefix/include/capnp/schema.h:', result.stderr)
|
|
124
|
+
|
|
125
|
+
def test_mismatched_kj_is_not_hidden(self):
|
|
126
|
+
result = self.build(mismatch='kj')
|
|
127
|
+
self.assertNotEqual(result.returncode, 0)
|
|
128
|
+
self.assertIn('homebrew prefix/include/kj/windows-sanity.h:', result.stderr)
|
|
129
|
+
|
|
130
|
+
def test_non_apple_host_is_unchanged(self):
|
|
131
|
+
result = self.build(apple=False)
|
|
132
|
+
self.assertNotEqual(result.returncode, 0)
|
|
133
|
+
self.assertIn('UNDEFINED_DEPENDENCY_MACRO', result.stderr)
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
if __name__ == '__main__':
|
|
137
|
+
unittest.main()
|