Skip to content

Commit 09e4638

Browse files
committed
build: add --shared-perfetto flag
Signed-off-by: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent e2b33e2 commit 09e4638

10 files changed

Lines changed: 249 additions & 25 deletions

File tree

.github/workflows/test-shared.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ on:
2929
- deps/nghttp2/**
3030
- deps/ngtcp2/**
3131
- deps/openssl/*/**
32+
- deps/perfetto/**
3233
- deps/simdjson/**
3334
- deps/sqlite/**
3435
- deps/uv/**
@@ -81,6 +82,7 @@ on:
8182
- deps/nghttp2/**
8283
- deps/ngtcp2/**
8384
- deps/openssl/*/**
85+
- deps/perfetto/**
8486
- deps/simdjson/**
8587
- deps/sqlite/**
8688
- deps/uv/**

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1312,6 +1312,7 @@ ifeq ($(SKIP_SHARED_DEPS), 1)
13121312
$(RM) -r $(TARNAME)/deps/ngtcp2
13131313
find $(TARNAME)/deps/openssl -maxdepth 1 -type f ! -name 'nodejs-openssl.cnf' -exec $(RM) {} +
13141314
find $(TARNAME)/deps/openssl -mindepth 1 -maxdepth 1 -type d -exec $(RM) -r {} +
1315+
$(RM) -r $(TARNAME)/deps/perfetto
13151316
$(RM) -r $(TARNAME)/deps/simdjson
13161317
$(RM) -r $(TARNAME)/deps/sqlite
13171318
$(RM) -r $(TARNAME)/deps/uv

configure.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,29 @@
508508
dest='shared_openssl_libpath',
509509
help='a directory to search for the shared OpenSSL DLLs')
510510

511+
shared_optgroup.add_argument('--shared-perfetto',
512+
action='store_true',
513+
dest='shared_perfetto',
514+
default=None,
515+
help='link to a shared perfetto SDK instead of the one in deps/perfetto '
516+
'(requires --with-perfetto)')
517+
518+
shared_optgroup.add_argument('--shared-perfetto-includes',
519+
action='store',
520+
dest='shared_perfetto_includes',
521+
help='directory containing perfetto header files')
522+
523+
shared_optgroup.add_argument('--shared-perfetto-libname',
524+
action='store',
525+
dest='shared_perfetto_libname',
526+
default='perfetto',
527+
help='alternative lib name to link to [default: %(default)s]')
528+
529+
shared_optgroup.add_argument('--shared-perfetto-libpath',
530+
action='store',
531+
dest='shared_perfetto_libpath',
532+
help='a directory to search for the shared perfetto DLL')
533+
511534
shared_optgroup.add_argument('--shared-uvwasi',
512535
action='store_true',
513536
dest='shared_uvwasi',
@@ -2349,6 +2372,15 @@ def configure_lief(o):
23492372

23502373
configure_library('lief', o, pkgname='LIEF')
23512374

2375+
def configure_perfetto(o):
2376+
if not options.with_perfetto:
2377+
if options.shared_perfetto:
2378+
error('--shared-perfetto requires --with-perfetto')
2379+
o['variables']['node_shared_perfetto'] = b(False)
2380+
return
2381+
2382+
configure_library('perfetto', o)
2383+
23522384
def configure_sqlite(o):
23532385
o['variables']['node_use_sqlite'] = b(not options.without_sqlite)
23542386
if options.without_sqlite:
@@ -2858,6 +2890,7 @@ def make_bin_override():
28582890
configure_library('nghttp3', output, pkgname='libnghttp3')
28592891
configure_library('ngtcp2', output, pkgname='libngtcp2')
28602892
configure_lief(output);
2893+
configure_perfetto(output);
28612894
configure_sqlite(output);
28622895
configure_ffi(output);
28632896
configure_library('temporal_capi', output)

deps/perfetto/perfetto.gyp

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
'variables': {
3+
'node_shared_perfetto%': 'false',
34
'perfetto_sdk_sources': [
45
'sdk/perfetto.cc',
56
'sdk/perfetto.h',
@@ -8,15 +9,23 @@
89
'targets': [
910
{
1011
'target_name': 'perfetto_sdk',
11-
'type': 'static_library',
1212
'toolsets': ['host', 'target'],
13-
'include_dirs': [ 'sdk' ],
14-
'direct_dependent_settings': {
15-
# Use like `#include "perfetto.h"`
16-
'include_dirs': [ 'sdk' ],
17-
},
18-
'sources': [
19-
'<@(perfetto_sdk_sources)',
13+
'conditions': [
14+
['node_shared_perfetto=="true"', {
15+
# The SDK comes from the system, `include_dirs` and `libraries` are
16+
# provided by the configure script.
17+
'type': 'none',
18+
}, {
19+
'type': 'static_library',
20+
'include_dirs': [ 'sdk' ],
21+
'direct_dependent_settings': {
22+
# Use like `#include "perfetto.h"`
23+
'include_dirs': [ 'sdk' ],
24+
},
25+
'sources': [
26+
'<@(perfetto_sdk_sources)',
27+
],
28+
}],
2029
],
2130
},
2231
]

node.gyp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
'node_shared_nbytes%': 'false',
2727
'node_shared_nghttp2%': 'false',
2828
'node_shared_openssl%': 'false',
29+
'node_shared_perfetto%': 'false',
2930
'node_shared_sqlite%': 'false',
3031
'node_shared_ffi%': 'false',
3132
'node_shared_temporal_capi%': 'false',
@@ -964,8 +965,12 @@
964965
'sources': [
965966
'<@(node_tracing_perfetto_sources)',
966967
],
967-
'dependencies': [
968-
'deps/perfetto/perfetto.gyp:perfetto_sdk',
968+
'conditions': [
969+
['node_shared_perfetto=="false"', {
970+
'dependencies': [
971+
'deps/perfetto/perfetto.gyp:perfetto_sdk',
972+
],
973+
}],
969974
],
970975
}, {
971976
'sources': [
@@ -1428,7 +1433,7 @@
14281433
}, {
14291434
'sources!': [ '<@(node_cctest_quic_sources)' ],
14301435
}],
1431-
[ 'v8_use_perfetto==1', {
1436+
[ 'v8_use_perfetto==1 and node_shared_perfetto=="false"', {
14321437
'dependencies': [
14331438
'deps/perfetto/perfetto.gyp:perfetto_sdk',
14341439
],
@@ -1758,7 +1763,7 @@
17581763
'NODE_USE_NODE_CODE_CACHE=1',
17591764
],
17601765
}],
1761-
[ 'v8_use_perfetto==1', {
1766+
[ 'v8_use_perfetto==1 and node_shared_perfetto=="false"', {
17621767
'dependencies': [
17631768
'deps/perfetto/perfetto.gyp:perfetto_sdk',
17641769
],

shell.nix

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
withFFI ? true,
2020
withSSL ? true,
2121
withTemporal ? false,
22-
withPerfetto ? false,
22+
withPerfetto ? true,
2323
sharedLibDeps ? (
2424
import ./tools/nix/sharedLibDeps.nix {
2525
inherit
@@ -29,6 +29,7 @@
2929
withSQLite
3030
withFFI
3131
withSSL
32+
withPerfetto
3233
withTemporal
3334
;
3435
}
@@ -52,6 +53,7 @@ let
5253
useSharedAda = builtins.hasAttr "ada" sharedLibDeps;
5354
useSharedOpenSSL = builtins.hasAttr "openssl" sharedLibDeps;
5455

56+
useSharedPerfetto = builtins.hasAttr "perfetto" sharedLibDeps;
5557
useSharedTemporal = builtins.hasAttr "temporal_capi" sharedLibDeps;
5658
needsRustCompiler = withTemporal && !useSharedTemporal;
5759

@@ -73,6 +75,7 @@ let
7375
];
7476
buildInputs =
7577
pkgs.lib.optional useSharedICU icu
78+
++ pkgs.lib.optional (withPerfetto && useSharedPerfetto) sharedLibDeps.perfetto
7679
++ pkgs.lib.optional (withTemporal && useSharedTemporal) sharedLibDeps.temporal_capi;
7780

7881
# Put here only the configure flags that affect the V8 build
@@ -85,6 +88,7 @@ let
8588
)
8689
"--v8-${if withTemporal then "enable" else "disable"}-temporal-support"
8790
]
91+
++ pkgs.lib.optional (withPerfetto && useSharedPerfetto) "--shared-perfetto"
8892
++ pkgs.lib.optional (withTemporal && useSharedTemporal) "--shared-temporal_capi"
8993
++ pkgs.lib.optional withPerfetto "--with-perfetto";
9094
in

tools/nix/perfetto-sdk.nix

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
{
2+
cmake,
3+
fetchFromGitHub,
4+
fetchurl,
5+
lib,
6+
nix-update-script,
7+
pkg-config,
8+
stdenv,
9+
testers,
10+
unzip,
11+
}:
12+
13+
let
14+
inherit (stdenv.hostPlatform) isStatic isDarwin;
15+
libName = "libperfetto${if isStatic then ".a" else stdenv.hostPlatform.extensions.sharedLibrary}";
16+
17+
version = "57.2";
18+
in
19+
stdenv.mkDerivation (finalAttrs: {
20+
pname = "perfetto-sdk";
21+
inherit version;
22+
23+
# The amalgamated SDK sources (a single perfetto.cc / perfetto.h pair) are
24+
# published as a release artifact, skipping the need for the heavy Google build chain.
25+
src = fetchurl {
26+
url = "https://github.com/google/perfetto/releases/download/v${finalAttrs.version}/perfetto-cpp-sdk-src.zip";
27+
hash = "sha256-xvo9ia7jD32jlALJzReMny40RUT9pcIQn9hFfjGcOi8=";
28+
};
29+
30+
nativeBuildInputs = [ unzip ];
31+
32+
strictDeps = true;
33+
sourceRoot = ".";
34+
35+
# The release artifact ships no build system
36+
buildPhase = ''
37+
runHook preBuild
38+
39+
$CXX $CXXFLAGS -std=c++17 -fPIC -O2 -c perfetto.cc -o perfetto.o
40+
${
41+
if isStatic then
42+
"$AR rcs libperfetto.a perfetto.o"
43+
else
44+
"$CXX $CXXFLAGS $LDFLAGS ${
45+
if isDarwin then "-dynamiclib -install_name $out/lib/" else "-shared -lpthread -Wl,-soname,"
46+
}${libName} -o ${libName} perfetto.o"
47+
}
48+
49+
runHook postBuild
50+
'';
51+
52+
installPhase = ''
53+
runHook preInstall
54+
55+
install -Dm${if isStatic then "644" else "755"} ${libName} $out/lib/${libName}
56+
install -Dm644 perfetto.h $out/include/perfetto.h
57+
58+
mkdir -p $out/lib/pkgconfig
59+
cat -> $out/lib/pkgconfig/perfetto.pc << EOF
60+
prefix=$out
61+
exec_prefix=\''${prefix}
62+
libdir=\''${exec_prefix}/lib
63+
includedir=\''${prefix}/include
64+
65+
Name: perfetto
66+
Description: Perfetto tracing SDK (amalgamated C++ distribution)
67+
Version: ${finalAttrs.version}
68+
Cflags: -I\''${includedir}
69+
Libs: -L\''${libdir} -lperfetto
70+
Libs.private: -lpthread
71+
EOF
72+
73+
runHook postInstall
74+
'';
75+
76+
passthru = {
77+
tests = {
78+
pkg-config = testers.hasPkgConfigModules {
79+
package = finalAttrs.finalPackage;
80+
};
81+
82+
examples =
83+
let
84+
src = fetchFromGitHub {
85+
owner = "google";
86+
repo = "perfetto";
87+
tag = "v${version}";
88+
hash = "sha256-0Syqu43M+XWD15I3qSNaL8Vck6bconRz+6aK4V+0pBA=";
89+
sparseCheckout = [ "examples/sdk" ];
90+
};
91+
in
92+
(stdenv.mkDerivation {
93+
pname = "perfetto-sdk-examples";
94+
inherit version src;
95+
96+
postPatch = ''
97+
substituteInPlace CMakeLists.txt --replace-fail "add_library(perfetto STATIC ../../sdk/perfetto.cc)" "
98+
find_package(PkgConfig REQUIRED)
99+
pkg_check_modules(PERFETTO REQUIRED IMPORTED_TARGET perfetto)
100+
add_library(perfetto ALIAS PkgConfig::PERFETTO)
101+
"
102+
'';
103+
104+
sourceRoot = "${src.name}/examples/sdk";
105+
106+
nativeBuildInputs = [
107+
cmake
108+
finalAttrs.finalPackage
109+
pkg-config
110+
];
111+
112+
# The examples have no install rules. example_system_wide is a long running process, we
113+
# still build it to ensure it compiles but won't bother trying to run it.
114+
installPhase = ''
115+
runHook preInstall
116+
117+
find . -maxdepth 1 -type f -executable -not -name example_system_wide -exec install -Dt $out/bin {} +
118+
119+
runHook postInstall
120+
'';
121+
doInstallCheck = true;
122+
installCheckPhase = ''
123+
runHook preInstallCheck
124+
125+
for bin in $out/bin/*; do
126+
echo "Running $bin"
127+
"$bin"
128+
done
129+
130+
runHook postInstallCheck
131+
'';
132+
});
133+
};
134+
135+
updateScript = nix-update-script {
136+
extraArgs = [
137+
"--subpackage"
138+
"tests.examples"
139+
];
140+
};
141+
};
142+
143+
meta = {
144+
description = "Perfetto tracing SDK, the client library used to emit traces";
145+
longDescription = ''
146+
The amalgamated C++ distribution of the Perfetto SDK, i.e. the library an
147+
application links against to become a Perfetto producer. Tools such as
148+
`traced` or `trace_processor` are not part of this package.
149+
'';
150+
homepage = "https://perfetto.dev/";
151+
changelog = "https://github.com/google/perfetto/releases/tag/v${finalAttrs.version}";
152+
license = lib.licenses.asl20;
153+
maintainers = with lib.maintainers; [ aduh95 ];
154+
pkgConfigModules = [ "perfetto" ];
155+
platforms = lib.platforms.unix;
156+
};
157+
})

tools/nix/sharedLibDeps.nix

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
withSQLite ? true,
66
withSSL ? true,
77
withFFI ? true,
8+
withPerfetto ? false,
89
withTemporal ? false,
910
}:
1011
{
@@ -50,6 +51,9 @@
5051
// (pkgs.lib.optionalAttrs withSSL ({
5152
inherit (import ./openssl-matrix.nix { inherit pkgs; }) openssl;
5253
}))
54+
// (pkgs.lib.optionalAttrs withPerfetto {
55+
perfetto = pkgs.callPackage ./perfetto-sdk.nix { };
56+
})
5357
// (pkgs.lib.optionalAttrs withTemporal {
5458
inherit (pkgs) temporal_capi;
5559
})

tools/nix/v8.nix

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,10 @@ let
4444
../../tools/v8_gypfiles/toolchain.gypi
4545
../../tools/v8_gypfiles/v8.gyp
4646
]
47-
++ lib.optionals (builtins.elem "--with-perfetto" configureFlags) [
48-
../../deps/perfetto
49-
]
47+
++ lib.optional (
48+
builtins.elem "--with-perfetto" configureFlags
49+
&& !(builtins.elem "--shared-perfetto" configureFlags)
50+
) ../../deps/perfetto
5051
++ lib.optionals (icu != null) [
5152
../../tools/icu/icu_versions.json
5253
../../tools/icu/icu-system.gyp

0 commit comments

Comments
 (0)