Skip to content

Commit 5182e03

Browse files
committed
CMake-based build with package and AAR building support.
1 parent 79df644 commit 5182e03

18 files changed

Lines changed: 523 additions & 0 deletions

.cmake-format.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# SPDX-FileCopyrightText: 2022, Collabora, Ltd.
2+
# SPDX-License-Identifier: CC0-1.0
3+
4+
with section("format"):
5+
line_width = 100
6+
tab_size = 4
7+
8+
max_prefix_chars = 4
9+
10+
max_pargs_hwrap = 4
11+
max_rows_cmdline = 1
12+
13+
keyword_case = 'upper'
14+
15+
16+
# Do not reflow comments
17+
18+
with section("markup"):
19+
enable_markup = False

.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# SPDX-License-Identifier: CC0-1.0
2+
# SPDX-FileCopyrightText: 2022, Collabora, Ltd.
3+
4+
build*/
5+
install*/
6+
.vscode/
7+
CMakeUserPresets.json
8+
maven_repo/*
9+
/*.pom
10+
/*.aar

CMakeLists.txt

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# Copyright 2021-2022, Collabora, Ltd.
2+
#
3+
# SPDX-License-Identifier: BSL-1.0
4+
5+
cmake_minimum_required(VERSION 3.10)
6+
project(
7+
percetto
8+
VERSION 0.1.6.0
9+
DESCRIPTION "A C wrapper for the C++ Perfetto tracing library.")
10+
11+
if("${CMAKE_SOURCE_DIR}" STREQUAL "${CMAKE_CURRENT_SOURCE_DIR}")
12+
# we are the top level project
13+
option(BUILD_EXAMPLES "Should we build the examples?" ON)
14+
else()
15+
set(BUILD_EXAMPLES OFF)
16+
endif()
17+
18+
if(NOT PERFETTO_SDK_PATH)
19+
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/../perfetto/sdk)
20+
set(PERFETTO_SDK_PATH
21+
${CMAKE_CURRENT_SOURCE_DIR}/../perfetto/sdk
22+
CACHE PATH "The SDK directory of a recent Perfetto release.")
23+
else()
24+
message(FATAL_ERROR "Must set PERFETTO_SDK_PATH")
25+
endif()
26+
endif()
27+
28+
# Basic config stuff
29+
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
30+
set(CMAKE_CXX_STANDARD 17)
31+
include(GNUInstallDirs)
32+
find_package(Threads REQUIRED)
33+
34+
if(ANDROID)
35+
find_library(ANDROID_LOG log)
36+
37+
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
38+
option(INSTALL_FOR_PREFAB "Should we install for a manually-assembled prefab layout?" OFF)
39+
if(INSTALL_FOR_PREFAB)
40+
include(PrefabHelper)
41+
setup_prefab()
42+
43+
setup_prefab_module(percetto)
44+
endif()
45+
endif()
46+
47+
# Perfetto SDK
48+
add_library(perfetto OBJECT ${PERFETTO_SDK_PATH}/perfetto.cc ${PERFETTO_SDK_PATH}/perfetto.h)
49+
target_link_libraries(perfetto PUBLIC Threads::Threads)
50+
target_include_directories(perfetto PUBLIC $<BUILD_INTERFACE:${PERFETTO_SDK_PATH}>)
51+
target_compile_options(perfetto PRIVATE -ffunction-sections -fdata-sections)
52+
53+
# PerCetto
54+
add_subdirectory(src)
55+
56+
# Optional examples
57+
if(BUILD_EXAMPLES)
58+
add_subdirectory(examples)
59+
endif()
60+
61+
if("${CMAKE_SOURCE_DIR}" STREQUAL "${CMAKE_CURRENT_SOURCE_DIR}")
62+
# Version usable in both build and install dirs.
63+
include(CMakePackageConfigHelpers)
64+
write_basic_package_version_file(
65+
"${CMAKE_CURRENT_BINARY_DIR}/percetto/percettoConfigVersion.cmake"
66+
VERSION ${percetto_VERSION}
67+
COMPATIBILITY AnyNewerVersion)
68+
# This really minimal config file usable in both build and install dirs
69+
configure_file(cmake/percettoConfig.cmake
70+
"${CMAKE_CURRENT_BINARY_DIR}/percetto/percettoConfig.cmake" COPYONLY)
71+
72+
# Exported targets usable only for build
73+
export(
74+
EXPORT percetto
75+
FILE "${CMAKE_CURRENT_BINARY_DIR}/percetto/percettoTargets.cmake"
76+
NAMESPACE PerCetto::)
77+
78+
# Installed targets usable only for installed
79+
set(PACKAGE_INSTALL_DIR ${CMAKE_INSTALL_LIBDIR}/cmake/percetto)
80+
install(
81+
EXPORT percetto
82+
FILE percettoTargets.cmake
83+
NAMESPACE PerCetto
84+
DESTINATION ${PACKAGE_INSTALL_DIR})
85+
86+
# Install the files we share with the build tree.
87+
install(
88+
FILES cmake/percettoConfig.cmake
89+
"${CMAKE_CURRENT_BINARY_DIR}/percetto/percettoConfigVersion.cmake"
90+
DESTINATION ${PACKAGE_INSTALL_DIR})
91+
endif()

CMakePresets.json

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
{
2+
"version": 3,
3+
"cmakeMinimumRequired": {
4+
"major": 3,
5+
"minor": 21,
6+
"patch": 0
7+
},
8+
"configurePresets": [
9+
{
10+
"name": "Android_base",
11+
"displayName": "Base build for Android",
12+
"generator": "Ninja",
13+
"hidden": true,
14+
"cacheVariables": {
15+
"ANDROID_PLATFORM": "26",
16+
"ANDROID_STL": "c++_shared",
17+
"CMAKE_ANDROID_NDK": "$env{ANDROID_NDK_HOME}",
18+
"CMAKE_BUILD_TYPE": "RelWithDebInfo"
19+
},
20+
"toolchainFile": "$env{ANDROID_NDK_HOME}/build/cmake/android.toolchain.cmake",
21+
"binaryDir": "${sourceDir}/build/${presetName}",
22+
"installDir": "${sourceDir}/install/${presetName}"
23+
},
24+
{
25+
"name": "Android_x86",
26+
"inherits": "Android_base",
27+
"cacheVariables": {
28+
"ANDROID_ABI": "x86"
29+
}
30+
},
31+
{
32+
"name": "Android_x86_64",
33+
"inherits": "Android_base",
34+
"cacheVariables": {
35+
"ANDROID_ABI": "x86_64"
36+
}
37+
},
38+
{
39+
"name": "Android_armeabi-v7a",
40+
"inherits": "Android_base",
41+
"cacheVariables": {
42+
"ANDROID_ABI": "armeabi-v7a"
43+
}
44+
},
45+
{
46+
"name": "Android_arm64-v8a",
47+
"inherits": "Android_base",
48+
"cacheVariables": {
49+
"ANDROID_ABI": "arm64-v8a"
50+
}
51+
},
52+
{
53+
"name": "Android_aar_base",
54+
"inherits": "Android_base",
55+
"cacheVariables": {
56+
"INSTALL_FOR_PREFAB": "ON"
57+
},
58+
"installDir": "install/percetto",
59+
"hidden": true
60+
},
61+
{
62+
"name": "Android_x86_64_aar",
63+
"inherits": "Android_aar_base",
64+
"cacheVariables": {
65+
"ANDROID_ABI": "x86_64"
66+
}
67+
},
68+
{
69+
"name": "Android_armeabi-v7a_aar",
70+
"inherits": "Android_aar_base",
71+
"cacheVariables": {
72+
"ANDROID_ABI": "armeabi-v7a"
73+
}
74+
},
75+
{
76+
"name": "Android_arm64-v8a_aar",
77+
"inherits": "Android_aar_base",
78+
"cacheVariables": {
79+
"ANDROID_ABI": "arm64-v8a"
80+
}
81+
},
82+
{
83+
"name": "Android_x86_aar",
84+
"inherits": "Android_aar_base",
85+
"cacheVariables": {
86+
"ANDROID_ABI": "x86"
87+
}
88+
},
89+
{
90+
"name": "Linux_Ninja",
91+
"generator": "Ninja",
92+
"binaryDir": "${sourceDir}/build",
93+
"installDir": "${sourceDir}/install"
94+
},
95+
{
96+
"name": "Linux_Ninja_Clang",
97+
"inherits": "Linux_Ninja",
98+
"cacheVariables": {
99+
"CMAKE_C_COMPILER": "/usr/bin/clang",
100+
"CMAKE_CXX_COMPILER": "/usr/bin/clang++"
101+
}
102+
}
103+
],
104+
"buildPresets": [
105+
{
106+
"name": "Android_aar_base",
107+
"hidden": true,
108+
"targets": [
109+
"all",
110+
"install"
111+
],
112+
"verbose": true
113+
},
114+
{
115+
"name": "Android_x86_aar",
116+
"inherits": "Android_aar_base",
117+
"configurePreset": "Android_x86_aar"
118+
},
119+
{
120+
"name": "Android_x86_64_aar",
121+
"inherits": "Android_aar_base",
122+
"configurePreset": "Android_x86_64_aar"
123+
},
124+
{
125+
"name": "Android_armeabi-v7a_aar",
126+
"inherits": "Android_aar_base",
127+
"configurePreset": "Android_armeabi-v7a_aar"
128+
},
129+
{
130+
"name": "Android_arm64-v8a_aar",
131+
"inherits": "Android_aar_base",
132+
"configurePreset": "Android_arm64-v8a_aar"
133+
}
134+
]
135+
}

CMakePresets.json.license

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
SPDX-FileCopyrightText: 2022, Collabora, Ltd.
2+
SPDX-License-Identifier: CC0-1.0

build-aar.sh

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/usr/bin/env bash
2+
# Copyright 2021-2022, Collabora, Ltd.
3+
#
4+
# SPDX-License-Identifier: BSL-1.0
5+
6+
set -e
7+
8+
# shellcheck disable=SC2086
9+
ROOT=$(cd "$(dirname $0)" && pwd)
10+
PROJECT=percetto
11+
DEFAULT_NDK_VERSION=21.4.7075529
12+
13+
ANDROID_NDK_HOME=${ANDROID_NDK_HOME:-${HOME}/Android/Sdk/ndk/${DEFAULT_NDK_VERSION}}
14+
15+
if [ ! -f "${ANDROID_NDK_HOME}/build/cmake/android.toolchain.cmake" ]; then
16+
echo "Please set ANDROID_NDK_HOME to a valid, installed NDK!"
17+
exit 1
18+
fi
19+
20+
export ANDROID_NDK_HOME
21+
22+
BUILD_DIR=${BUILD_DIR:-${ROOT}/build}
23+
INSTALL_DIR=${INSTALL_DIR:-${ROOT}/install}
24+
GENERATION_DIR="${BUILD_DIR}/Android_x86_aar/src"
25+
26+
rm -f "${GENERATION_DIR}/*.pom" || true
27+
28+
rm -rf "${INSTALL_DIR}"
29+
for arch in x86 x86_64 armeabi-v7a arm64-v8a; do
30+
cmake --preset Android_${arch}_aar
31+
cmake --build --preset Android_${arch}_aar
32+
done
33+
# find latest pom
34+
DECORATED=$(cd "${GENERATION_DIR}" && find . -maxdepth 1 -name "${PROJECT}*.pom" | sort | tail -n 1)
35+
# strip leading ./
36+
DECORATED=${DECORATED#./}
37+
echo "DECORATED ${DECORATED}"
38+
DECORATED_STEM=${DECORATED%.pom}
39+
echo "DECORATED_STEM ${DECORATED_STEM}"
40+
VERSION=${DECORATED_STEM#${PROJECT}-}
41+
echo "VERSION ${VERSION}"
42+
DIR=$(pwd)/maven_repo/io/github/olvaffe/deps/${PROJECT}/${VERSION}
43+
44+
mkdir -p "${DIR}"
45+
cp "${GENERATION_DIR}/${DECORATED}" "${ROOT}"
46+
cp "${GENERATION_DIR}/${DECORATED}" "${DIR}"
47+
48+
(
49+
cd "$INSTALL_DIR/${PROJECT}"
50+
7za a -r ../${PROJECT}.zip ./*
51+
mv ../${PROJECT}.zip "$ROOT/${DECORATED_STEM}.aar"
52+
cp "$ROOT/${DECORATED_STEM}.aar" "$DIR/${DECORATED_STEM}.aar"
53+
)

cmake/PrefabHelper.cmake

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Copyright 2020-2021, Collabora, Ltd.
2+
#
3+
# SPDX-License-Identifier: BSL-1.0
4+
#
5+
# Helpers for creating an android "prefab" archive
6+
7+
# We must run the following at "include" time, not at function call time,
8+
# to find the path to this module rather than the path to a calling list file
9+
get_filename_component(_prefab_mod_dir ${CMAKE_CURRENT_LIST_FILE} PATH)
10+
11+
macro(setup_prefab)
12+
set(PREFAB_INSTALL_DIR prefab)
13+
unset(NDK_MAJOR_VERSION)
14+
if(CMAKE_ANDROID_NDK)
15+
file(STRINGS "${CMAKE_ANDROID_NDK}/source.properties" NDK_PROPERTIES)
16+
foreach(_line ${NDK_PROPERTIES})
17+
if("${_line}" MATCHES
18+
"Pkg.Revision = ([0-9]+)[.]([0-9]+)[.]([0-9]+)")
19+
set(NDK_MAJOR_VERSION ${CMAKE_MATCH_1})
20+
endif()
21+
endforeach()
22+
else()
23+
message(FATAL_ERROR "Please set CMAKE_ANDROID_NDK to your NDK root!")
24+
endif()
25+
if(NDK_MAJOR_VERSION)
26+
message(STATUS "Building using NDK major version ${NDK_MAJOR_VERSION}")
27+
else()
28+
message(
29+
FATAL_ERROR
30+
"Could not parse the major version from ${CMAKE_ANDROID_NDK}/source.properties"
31+
)
32+
endif()
33+
34+
configure_file(${_prefab_mod_dir}/prefab.json
35+
${CMAKE_CURRENT_BINARY_DIR}/prefab.json @ONLY)
36+
install(
37+
FILES ${CMAKE_CURRENT_BINARY_DIR}/prefab.json
38+
DESTINATION ${PREFAB_INSTALL_DIR}
39+
COMPONENT Prefab)
40+
endmacro()
41+
42+
macro(setup_prefab_module MODULE_NAME)
43+
set(PREFAB_MODULE_INSTALL_DIR ${PREFAB_INSTALL_DIR}/modules/${MODULE_NAME})
44+
set(CMAKE_INSTALL_LIBDIR
45+
${PREFAB_MODULE_INSTALL_DIR}/libs/android.${ANDROID_ABI})
46+
set(CMAKE_INSTALL_BINDIR ${CMAKE_INSTALL_LIBDIR})
47+
set(CMAKE_INSTALL_INCLUDEDIR ${PREFAB_MODULE_INSTALL_DIR}/${CMAKE_INSTALL_INCLUDEDIR})
48+
49+
configure_file(${_prefab_mod_dir}/abi.json
50+
${CMAKE_CURRENT_BINARY_DIR}/abi.json @ONLY)
51+
install(
52+
FILES ${CMAKE_CURRENT_BINARY_DIR}/abi.json
53+
DESTINATION ${CMAKE_INSTALL_LIBDIR}
54+
COMPONENT Prefab)
55+
56+
install(
57+
FILES ${_prefab_mod_dir}/module.json
58+
DESTINATION ${PREFAB_MODULE_INSTALL_DIR}
59+
COMPONENT Prefab)
60+
endmacro()

cmake/abi.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"abi": "@ANDROID_ABI@",
3+
"api": @CMAKE_SYSTEM_VERSION@,
4+
"ndk": @NDK_MAJOR_VERSION@,
5+
"stl": "@ANDROID_STL@"
6+
}

cmake/abi.json.license

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Copyright 2020-2021, Collabora, Ltd.
2+
3+
SPDX-License-Identifier: BSL-1.0

cmake/module.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"export_libraries": [],
3+
"android": {
4+
"export_libraries": null,
5+
"library_name": null
6+
}
7+
}

0 commit comments

Comments
 (0)